1 | // Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #include "vm/globals.h" |
6 | #if !defined(DART_PRECOMPILED_RUNTIME) |
7 | |
8 | #include "vm/compiler/assembler/disassembler_kbc.h" |
9 | |
10 | #include "platform/assert.h" |
11 | #include "vm/compiler/frontend/bytecode_reader.h" |
12 | #include "vm/constants_kbc.h" |
13 | #include "vm/cpu.h" |
14 | #include "vm/instructions.h" |
15 | |
16 | namespace dart { |
17 | |
18 | static const char* kOpcodeNames[] = { |
19 | #define BYTECODE_NAME(name, encoding, kind, op1, op2, op3) #name, |
20 | KERNEL_BYTECODES_LIST(BYTECODE_NAME) |
21 | #undef BYTECODE_NAME |
22 | }; |
23 | |
24 | static const size_t kOpcodeCount = |
25 | sizeof(kOpcodeNames) / sizeof(kOpcodeNames[0]); |
26 | static_assert(kOpcodeCount <= 256, "Opcode should fit into a byte" ); |
27 | |
28 | typedef void (*BytecodeFormatter)(char* buffer, |
29 | intptr_t size, |
30 | KernelBytecode::Opcode opcode, |
31 | const KBCInstr* instr); |
32 | typedef void (*Fmt)(char** buf, |
33 | intptr_t* size, |
34 | const KBCInstr* instr, |
35 | int32_t value); |
36 | |
37 | template <typename ValueType> |
38 | void FormatOperand(char** buf, |
39 | intptr_t* size, |
40 | const char* fmt, |
41 | ValueType value) { |
42 | intptr_t written = Utils::SNPrint(*buf, *size, fmt, value); |
43 | if (written < *size) { |
44 | *buf += written; |
45 | *size += written; |
46 | } else { |
47 | *size = -1; |
48 | } |
49 | } |
50 | |
51 | static void Fmt___(char** buf, |
52 | intptr_t* size, |
53 | const KBCInstr* instr, |
54 | int32_t value) {} |
55 | |
56 | static void Fmttgt(char** buf, |
57 | intptr_t* size, |
58 | const KBCInstr* instr, |
59 | int32_t value) { |
60 | if (FLAG_disassemble_relative) { |
61 | FormatOperand(buf, size, "-> %" Pd, value); |
62 | } else { |
63 | FormatOperand(buf, size, "-> %" Px, instr + value); |
64 | } |
65 | } |
66 | |
67 | static void Fmtlit(char** buf, |
68 | intptr_t* size, |
69 | const KBCInstr* instr, |
70 | int32_t value) { |
71 | FormatOperand(buf, size, "k%d" , value); |
72 | } |
73 | |
74 | static void Fmtreg(char** buf, |
75 | intptr_t* size, |
76 | const KBCInstr* instr, |
77 | int32_t value) { |
78 | FormatOperand(buf, size, "r%d" , value); |
79 | } |
80 | |
81 | static void Fmtxeg(char** buf, |
82 | intptr_t* size, |
83 | const KBCInstr* instr, |
84 | int32_t value) { |
85 | if (value < 0) { |
86 | FormatOperand(buf, size, "FP[%d]" , value); |
87 | } else { |
88 | Fmtreg(buf, size, instr, value); |
89 | } |
90 | } |
91 | |
92 | static void Fmtnum(char** buf, |
93 | intptr_t* size, |
94 | const KBCInstr* instr, |
95 | int32_t value) { |
96 | FormatOperand(buf, size, "#%d" , value); |
97 | } |
98 | |
99 | static void Apply(char** buf, |
100 | intptr_t* size, |
101 | const KBCInstr* instr, |
102 | Fmt fmt, |
103 | int32_t value, |
104 | const char* suffix) { |
105 | if (*size <= 0) { |
106 | return; |
107 | } |
108 | |
109 | fmt(buf, size, instr, value); |
110 | if (*size > 0) { |
111 | FormatOperand(buf, size, "%s" , suffix); |
112 | } |
113 | } |
114 | |
115 | static void Format0(char* buf, |
116 | intptr_t size, |
117 | KernelBytecode::Opcode opcode, |
118 | const KBCInstr* instr, |
119 | Fmt op1, |
120 | Fmt op2, |
121 | Fmt op3) {} |
122 | |
123 | static void FormatA(char* buf, |
124 | intptr_t size, |
125 | KernelBytecode::Opcode opcode, |
126 | const KBCInstr* instr, |
127 | Fmt op1, |
128 | Fmt op2, |
129 | Fmt op3) { |
130 | const int32_t a = KernelBytecode::DecodeA(instr); |
131 | Apply(&buf, &size, instr, op1, a, "" ); |
132 | } |
133 | |
134 | static void FormatD(char* buf, |
135 | intptr_t size, |
136 | KernelBytecode::Opcode opcode, |
137 | const KBCInstr* instr, |
138 | Fmt op1, |
139 | Fmt op2, |
140 | Fmt op3) { |
141 | const int32_t bc = KernelBytecode::DecodeD(instr); |
142 | Apply(&buf, &size, instr, op1, bc, "" ); |
143 | } |
144 | |
145 | static void FormatX(char* buf, |
146 | intptr_t size, |
147 | KernelBytecode::Opcode opcode, |
148 | const KBCInstr* instr, |
149 | Fmt op1, |
150 | Fmt op2, |
151 | Fmt op3) { |
152 | const int32_t bc = KernelBytecode::DecodeX(instr); |
153 | Apply(&buf, &size, instr, op1, bc, "" ); |
154 | } |
155 | |
156 | static void FormatT(char* buf, |
157 | intptr_t size, |
158 | KernelBytecode::Opcode opcode, |
159 | const KBCInstr* instr, |
160 | Fmt op1, |
161 | Fmt op2, |
162 | Fmt op3) { |
163 | const int32_t x = KernelBytecode::DecodeT(instr); |
164 | Apply(&buf, &size, instr, op1, x, "" ); |
165 | } |
166 | |
167 | static void FormatA_E(char* buf, |
168 | intptr_t size, |
169 | KernelBytecode::Opcode opcode, |
170 | const KBCInstr* instr, |
171 | Fmt op1, |
172 | Fmt op2, |
173 | Fmt op3) { |
174 | const int32_t a = KernelBytecode::DecodeA(instr); |
175 | const int32_t e = KernelBytecode::DecodeE(instr); |
176 | Apply(&buf, &size, instr, op1, a, ", " ); |
177 | Apply(&buf, &size, instr, op2, e, "" ); |
178 | } |
179 | |
180 | static void FormatA_Y(char* buf, |
181 | intptr_t size, |
182 | KernelBytecode::Opcode opcode, |
183 | const KBCInstr* instr, |
184 | Fmt op1, |
185 | Fmt op2, |
186 | Fmt op3) { |
187 | const int32_t a = KernelBytecode::DecodeA(instr); |
188 | const int32_t y = KernelBytecode::DecodeY(instr); |
189 | Apply(&buf, &size, instr, op1, a, ", " ); |
190 | Apply(&buf, &size, instr, op2, y, "" ); |
191 | } |
192 | |
193 | static void FormatD_F(char* buf, |
194 | intptr_t size, |
195 | KernelBytecode::Opcode opcode, |
196 | const KBCInstr* instr, |
197 | Fmt op1, |
198 | Fmt op2, |
199 | Fmt op3) { |
200 | const int32_t d = KernelBytecode::DecodeD(instr); |
201 | const int32_t f = KernelBytecode::DecodeF(instr); |
202 | Apply(&buf, &size, instr, op1, d, ", " ); |
203 | Apply(&buf, &size, instr, op2, f, "" ); |
204 | } |
205 | |
206 | static void FormatA_B_C(char* buf, |
207 | intptr_t size, |
208 | KernelBytecode::Opcode opcode, |
209 | const KBCInstr* instr, |
210 | Fmt op1, |
211 | Fmt op2, |
212 | Fmt op3) { |
213 | const int32_t a = KernelBytecode::DecodeA(instr); |
214 | const int32_t b = KernelBytecode::DecodeB(instr); |
215 | const int32_t c = KernelBytecode::DecodeC(instr); |
216 | Apply(&buf, &size, instr, op1, a, ", " ); |
217 | Apply(&buf, &size, instr, op2, b, ", " ); |
218 | Apply(&buf, &size, instr, op3, c, "" ); |
219 | } |
220 | |
221 | #define BYTECODE_FORMATTER(name, encoding, kind, op1, op2, op3) \ |
222 | static void Format##name(char* |
---|