1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2010 Ben Gruver (JesusFreke)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29package org.jf.baksmali.Adaptors;
30
31import org.jf.dexlib.Util.Utf8Utils;
32import org.jf.util.IndentingWriter;
33import org.jf.dexlib.CodeItem;
34import org.jf.dexlib.StringIdItem;
35import org.jf.dexlib.TypeIdItem;
36
37import java.io.IOException;
38
39public abstract class DebugMethodItem extends MethodItem {
40    private final double sortOrder;
41
42    public DebugMethodItem(int codeAddress, double sortOrder) {
43        super(codeAddress);
44        this.sortOrder = sortOrder;
45    }
46
47    public double getSortOrder() {
48        return sortOrder;
49    }
50
51    protected static void writeLine(IndentingWriter writer, int line) throws IOException {
52        writer.write(".line ");
53        writer.printSignedIntAsDec(line);
54    }
55
56    protected static void writeEndPrologue(IndentingWriter writer) throws IOException {
57        writer.write(".prologue");
58    }
59
60    protected static void writeBeginEpilogue(IndentingWriter writer) throws IOException {
61        writer.write(".epilogue");
62    }
63
64    protected static void writeStartLocal(IndentingWriter writer, CodeItem codeItem, int register,
65                                          StringIdItem name, TypeIdItem type, StringIdItem signature)
66                                          throws IOException {
67        writer.write(".local ");
68        RegisterFormatter.writeTo(writer, codeItem, register);
69        writer.write(", ");
70        writer.write(name.getStringValue());
71        writer.write(':');
72        writer.write(type.getTypeDescriptor());
73        if (signature != null) {
74            writer.write(",\"");
75            writer.write(signature.getStringValue());
76            writer.write('"');
77        }
78    }
79
80    protected static void writeEndLocal(IndentingWriter writer, CodeItem codeItem, int register, StringIdItem name,
81                                       TypeIdItem type, StringIdItem signature) throws IOException {
82        writer.write(".end local ");
83        RegisterFormatter.writeTo(writer, codeItem, register);
84
85        if (name != null) {
86            writer.write("           #");
87            writer.write(name.getStringValue());
88            writer.write(':');
89            writer.write(type.getTypeDescriptor());
90            if (signature != null) {
91                writer.write(",\"");
92                writer.write(signature.getStringValue());
93                writer.write('"');
94            }
95        }
96    }
97
98
99    protected static void writeRestartLocal(IndentingWriter writer, CodeItem codeItem, int register,
100                                         StringIdItem name, TypeIdItem type, StringIdItem signature)
101                                         throws IOException {
102        writer.write(".restart local ");
103        RegisterFormatter.writeTo(writer, codeItem, register);
104
105        if (name != null) {
106            writer.write("       #");
107            writer.write(name.getStringValue());
108            writer.write(':');
109            writer.write(type.getTypeDescriptor());
110            if (signature != null) {
111                writer.write(",\"");
112                writer.write(signature.getStringValue());
113                writer.write('"');
114            }
115        }
116    }
117
118    protected static void writeSetFile(IndentingWriter writer, String fileName) throws IOException {
119        writer.write(".source \"");
120        Utf8Utils.writeEscapedString(writer, fileName);
121        writer.write('"');
122    }
123}
124