/
disassembler.h
429 lines (419 loc) · 13.7 KB
/
disassembler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#ifndef IV_LV5_RAILGUN_DISASSEMBLER_H_
#define IV_LV5_RAILGUN_DISASSEMBLER_H_
#include <vector>
#include <sstream>
#include <iv/detail/array.h>
#include <iv/detail/unordered_map.h>
#include <iv/noncopyable.h>
#include <iv/string_view.h>
#include <iv/conversions.h>
#include <iv/lv5/railgun/fwd.h>
#include <iv/lv5/railgun/op.h>
#include <iv/lv5/railgun/code.h>
#include <iv/lv5/railgun/direct_threading.h>
namespace iv {
namespace lv5 {
namespace railgun {
template<typename Derived>
class DisAssembler : private core::Noncopyable<> {
public:
explicit DisAssembler(Context* ctx) { }
void DisAssembleGlobal(const Code& code) {
std::ostringstream ss;
ss << "total code size: " << code.GetData()->size();
OutputLine(ss.str());
DisAssemble(code, true);
}
static std::string ExtractReg(int reg) {
char prefix = 'r';
if (reg >= FrameConstant<>::kConstantOffset) {
// This is constant register
prefix = 'k';
reg -= FrameConstant<>::kConstantOffset;
}
std::string result(1, prefix);
core::Int32ToString(reg, std::back_inserter(result));
return result;
}
void DisAssemble(const Code& code, bool all = true) {
{
// code description
std::ostringstream ss;
ss << "[code]"
<< " local: " << code.stack_size()
<< " heap: " << code.heap_size()
<< " registers: " << code.registers()
<< " frame size: " << code.FrameSize();
OutputLine(ss.str());
}
const Code::Codes& codes = code.codes();
std::vector<char> line;
int index = 0;
char buf[30];
for (const Instruction* it = code.begin(),
*last = code.end(); it != last;) {
const uint32_t opcode = it->GetOP();
const uint32_t length = kOPLength[opcode];
const int len = snprintf(buf, sizeof(buf) - 1, "%06d: ", index);
assert(len >= 0); // %05d, so always pass
line.insert(line.end(), buf, buf + len);
DisAssemble(code, it, index, opcode, length, &line);
OutputLine(core::string_view(line.data(), line.size()));
line.clear();
std::advance(it, length);
index += length;
}
if (all) {
for (Code::Codes::const_iterator it = codes.begin(),
last = codes.end(); it != last; ++it) {
DisAssemble(**it);
}
}
}
// DisAssemble main function
// dispatch by instruction layout
void DisAssemble(const Code& code,
const Instruction* instr,
int index,
uint32_t opcode,
uint32_t length,
std::vector<char>* line) {
const char* op = OP::String(opcode);
char buf[80];
int len = 0;
switch (static_cast<OP::Type>(opcode)) {
case OP::POP_ENV:
case OP::DEBUGGER:
case OP::ENTER:
case OP::NOP: {
len = snprintf(buf, sizeof(buf) - 1, "%s", op);
break;
}
case OP::RETURN_SUBROUTINE:
case OP::POSTFIX_INCREMENT:
case OP::POSTFIX_DECREMENT:
case OP::TYPEOF:
case OP::UNARY_BIT_NOT:
case OP::UNARY_NOT:
case OP::UNARY_NEGATIVE:
case OP::UNARY_POSITIVE:
case OP::MV: {
const int r0 = instr[1].i16[0], r1 = instr[1].i16[1];
len = snprintf(
buf,
sizeof(buf) - 1,
"%s %s %s",
op, ExtractReg(r0).c_str(), ExtractReg(r1).c_str());
break;
}
case OP::TO_PRIMITIVE_AND_TO_STRING:
case OP::TO_NUMBER:
case OP::WITH_SETUP:
case OP::INCREMENT:
case OP::DECREMENT:
case OP::FORIN_LEAVE:
case OP::LOAD_ARGUMENTS:
case OP::THROW:
case OP::RESULT:
case OP::RETURN: {
const int r0 = instr[1].i32[0];
len = snprintf(
buf,
sizeof(buf) - 1,
"%s %s",
op, ExtractReg(r0).c_str());
break;
}
case OP::BUILD_ENV: {
const unsigned int size = instr[1].u32[0], mu = instr[1].u32[1];
len = snprintf(buf, sizeof(buf) - 1, "%s %u %u", op, size, mu);
break;
}
case OP::RAISE: {
const Error::Code code = static_cast<Error::Code>(instr[1].u32[0]);
const unsigned int constant = instr[1].u32[1];
len = snprintf(buf, sizeof(buf) - 1, "%s %s k%u", op, Error::CodeString(code), constant);
break;
}
case OP::INSTANTIATE_VARIABLE_BINDING:
case OP::INSTANTIATE_DECLARATION_BINDING: {
const unsigned int name = instr[1].u32[0];
const char* boolean = (instr[1].u32[1]) ? "true" : "false";
len = snprintf(buf, sizeof(buf) - 1, "%s %u %s", op, name, boolean);
break;
}
case OP::LOAD_REGEXP:
case OP::LOAD_FUNCTION:
case OP::LOAD_ARRAY:
case OP::DUP_ARRAY:
case OP::TRY_CATCH_SETUP:
case OP::INCREMENT_NAME:
case OP::DECREMENT_NAME:
case OP::POSTFIX_INCREMENT_NAME:
case OP::POSTFIX_DECREMENT_NAME:
case OP::TYPEOF_NAME:
case OP::DELETE_NAME:
case OP::STORE_NAME:
case OP::LOAD_NAME:
case OP::LOAD_CONST:
case OP::INITIALIZE_HEAP_IMMUTABLE: {
const int r0 = instr[1].ssw.i16[0];
const unsigned int offset = instr[1].ssw.u32;
len = snprintf(
buf,
sizeof(buf) - 1,
"%s %s %u",
op, ExtractReg(r0).c_str(), offset);
break;
}
case OP::POSTFIX_INCREMENT_HEAP:
case OP::POSTFIX_DECREMENT_HEAP:
case OP::INCREMENT_HEAP:
case OP::DECREMENT_HEAP:
case OP::TYPEOF_HEAP:
case OP::DELETE_HEAP:
case OP::STORE_HEAP:
case OP::LOAD_HEAP: {
const int r0 = instr[1].ssw.i16[0];
const bool imm = !!instr[1].ssw.i16[1];
const unsigned int name = instr[1].ssw.u32;
const unsigned int offset = instr[2].u32[0];
const unsigned int nest = instr[2].u32[1];
len = snprintf(
buf,
sizeof(buf) - 1,
"%s %s %s %u %u %u",
op, ExtractReg(r0).c_str(), (imm) ? "immutable" : "mutable", name, offset, nest);
break;
}
case OP::TYPEOF_GLOBAL:
case OP::DELETE_GLOBAL:
case OP::STORE_GLOBAL:
case OP::LOAD_GLOBAL: {
const int r0 = instr[1].ssw.i16[0];
const unsigned int name = instr[1].ssw.u32;
const Map* map = instr[2].map;
const unsigned int offset = instr[3].u32[0];
len = snprintf(
buf,
sizeof(buf) - 1,
"%s %s %u %p %u",
op, ExtractReg(r0).c_str(), name,
reinterpret_cast<const void*>(map), offset);
break;
}
case OP::LOAD_GLOBAL_DIRECT:
case OP::STORE_GLOBAL_DIRECT: {
const int r0 = instr[1].i32[0];
const StoredSlot* slot = instr[2].slot;
len = snprintf(
buf,
sizeof(buf) - 1,
"%s %s %p",
op, ExtractReg(r0).c_str(),
reinterpret_cast<const void*>(slot));
break;
}
case OP::BINARY_BIT_OR:
case OP::BINARY_BIT_XOR:
case OP::BINARY_BIT_AND:
case OP::BINARY_STRICT_NE:
case OP::BINARY_NE:
case OP::BINARY_STRICT_EQ:
case OP::BINARY_EQ:
case OP::BINARY_IN:
case OP::BINARY_INSTANCEOF:
case OP::BINARY_GTE:
case OP::BINARY_LTE:
case OP::BINARY_GT:
case OP::BINARY_LT:
case OP::BINARY_RSHIFT_LOGICAL:
case OP::BINARY_RSHIFT:
case OP::BINARY_LSHIFT:
case OP::BINARY_MODULO:
case OP::BINARY_DIVIDE:
case OP::BINARY_MULTIPLY:
case OP::BINARY_SUBTRACT:
case OP::BINARY_ADD:
case OP::POSTFIX_INCREMENT_ELEMENT:
case OP::POSTFIX_DECREMENT_ELEMENT:
case OP::INCREMENT_ELEMENT:
case OP::DECREMENT_ELEMENT:
case OP::DELETE_ELEMENT:
case OP::STORE_ELEMENT:
case OP::LOAD_ELEMENT: {
const int r0 = instr[1].i16[0],
r1 = instr[1].i16[1], r2 = instr[1].i16[2];
len = snprintf(
buf,
sizeof(buf) - 1,
"%s %s %s %s",
op, ExtractReg(r0).c_str(), ExtractReg(r1).c_str(), ExtractReg(r2).c_str());
break;
}
case OP::STORE_OBJECT_SET:
case OP::STORE_OBJECT_GET:
case OP::STORE_OBJECT_DATA:
case OP::STORE_OBJECT_INDEXED:
case OP::INIT_VECTOR_ARRAY_ELEMENT:
case OP::INIT_SPARSE_ARRAY_ELEMENT: {
const int r0 = instr[1].i16[0], r1 = instr[1].i16[1];
const unsigned int index = instr[2].u32[0], size = instr[2].u32[1];
len = snprintf(
buf,
sizeof(buf) - 1,
"%s %s %s %u %u",
op, ExtractReg(r0).c_str(), ExtractReg(r1).c_str(), index, size);
break;
}
case OP::CALL:
case OP::EVAL:
case OP::CONSTRUCT:
case OP::CONCAT:
case OP::PREPARE_DYNAMIC_CALL:
case OP::POSTFIX_INCREMENT_PROP:
case OP::POSTFIX_DECREMENT_PROP:
case OP::INCREMENT_PROP:
case OP::DECREMENT_PROP:
case OP::DELETE_PROP:
case OP::STORE_PROP_GENERIC:
case OP::LOAD_PROP_GENERIC:
case OP::LOAD_PROP: {
const int r0 = instr[1].ssw.i16[0], r1 = instr[1].ssw.i16[1];
const unsigned int name = instr[1].ssw.u32;
len = snprintf(
buf,
sizeof(buf) - 1,
"%s %s %s %u",
op, ExtractReg(r0).c_str(), ExtractReg(r1).c_str(), name);
break;
}
case OP::LOAD_OBJECT: {
const int r0 = instr[1].i32[0];
const Map* map = instr[2].map;
len = snprintf(
buf, sizeof(buf) - 1,
"%s %s %p",
op, ExtractReg(r0).c_str(), reinterpret_cast<const void*>(map));
break;
}
case OP::STORE_PROP:
case OP::LOAD_PROP_OWN: {
const int r0 = instr[1].ssw.i16[0], r1 = instr[1].ssw.i16[1];
const unsigned int name = instr[1].ssw.u32;
const Map* map = instr[2].map;
const unsigned int offset = instr[3].u32[0];
len = snprintf(
buf,
sizeof(buf) - 1,
"%s %s %s %u %p %u",
op, ExtractReg(r0).c_str(), ExtractReg(r1).c_str(), name,
reinterpret_cast<const void*>(map), offset);
break;
}
case OP::LOAD_PROP_PROTO: {
const int r0 = instr[1].ssw.i16[0], r1 = instr[1].ssw.i16[1];
const unsigned int name = instr[1].ssw.u32;
const Map* map1 = instr[2].map;
const Map* map2 = instr[3].map;
const unsigned int offset = instr[4].u32[0];
len = snprintf(
buf, sizeof(buf) - 1,
"%s %s %s %u %p %p %u",
op, ExtractReg(r0).c_str(), ExtractReg(r1).c_str(), name,
reinterpret_cast<const void*>(map1),
reinterpret_cast<const void*>(map2), offset);
break;
}
case OP::LOAD_PROP_CHAIN: {
const int r0 = instr[1].ssw.i16[0], r1 = instr[1].ssw.i16[1];
const unsigned int name = instr[1].ssw.u32;
const Chain* chain = instr[2].chain;
const Map* map = instr[3].map;
const unsigned int offset = instr[4].u32[0];
len = snprintf(
buf, sizeof(buf) - 1, "%s %s %s %u %p %p %u",
op, ExtractReg(r0).c_str(), ExtractReg(r1).c_str(), name,
reinterpret_cast<const void*>(chain),
reinterpret_cast<const void*>(map), offset);
break;
}
case OP::IF_TRUE:
case OP::IF_FALSE: {
const int r0 = instr[1].jump.i16[0], jump = instr[1].jump.to;
const int to = index + jump;
len = snprintf(
buf,
sizeof(buf) - 1, "%s %d %s ; => %d",
op, jump, ExtractReg(r0).c_str(), to);
break;
}
case OP::IF_TRUE_BINARY_LT:
case OP::IF_FALSE_BINARY_LT:
case OP::IF_TRUE_BINARY_LTE:
case OP::IF_FALSE_BINARY_LTE:
case OP::IF_TRUE_BINARY_GT:
case OP::IF_FALSE_BINARY_GT:
case OP::IF_TRUE_BINARY_GTE:
case OP::IF_FALSE_BINARY_GTE:
case OP::IF_TRUE_BINARY_INSTANCEOF:
case OP::IF_FALSE_BINARY_INSTANCEOF:
case OP::IF_TRUE_BINARY_IN:
case OP::IF_FALSE_BINARY_IN:
case OP::IF_TRUE_BINARY_EQ:
case OP::IF_FALSE_BINARY_EQ:
case OP::IF_TRUE_BINARY_NE:
case OP::IF_FALSE_BINARY_NE:
case OP::IF_TRUE_BINARY_STRICT_EQ:
case OP::IF_FALSE_BINARY_STRICT_EQ:
case OP::IF_TRUE_BINARY_STRICT_NE:
case OP::IF_FALSE_BINARY_STRICT_NE:
case OP::IF_TRUE_BINARY_BIT_AND:
case OP::IF_FALSE_BINARY_BIT_AND:
case OP::FORIN_ENUMERATE:
case OP::FORIN_SETUP:
case OP::JUMP_SUBROUTINE: {
const int r0 = instr[1].jump.i16[0],
r1 = instr[1].jump.i16[1], jump = instr[1].jump.to;
const int to = index + jump;
len = snprintf(
buf, sizeof(buf) - 1, "%s %d %s %s ; => %d",
op, jump, ExtractReg(r0).c_str(), ExtractReg(r1).c_str(), to);
break;
}
case OP::JUMP_BY: {
const int jump = instr[1].jump.to;
const int to = index + jump;
len = snprintf(buf, sizeof(buf) - 1, "%s %d ; => %d", op, jump, to);
break;
}
case OP::NUM_OF_OP: {
UNREACHABLE();
break;
}
}
line->insert(line->end(), buf, buf + len);
}
private:
void OutputLine(const core::string_view& str) {
static_cast<Derived*>(this)->OutputLine(str);
}
};
class OutputDisAssembler : public DisAssembler<OutputDisAssembler> {
public:
OutputDisAssembler(Context* ctx, FILE* file)
: DisAssembler<OutputDisAssembler>(ctx), file_(file) { }
void OutputLine(const core::string_view& str) {
const std::size_t rv = std::fwrite(str.data(), 1, str.size(), file_);
if (rv == str.size()) {
std::fputc('\n', file_);
} else {
std::abort();
}
std::fflush(file_);
}
private:
FILE* file_;
};
} } } // namespace iv::lv5::railgun
#endif // IV_LV5_RAILGUN_DISASSEMBLER_H_