-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisassembler.cpp
69 lines (61 loc) · 1.89 KB
/
Disassembler.cpp
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
/*
* Disassembler.cpp
* lex
*
* Created by Alexandru Chiculita on 9/12/09.
*
*/
#include "Platform.h"
#include "Disassembler.h"
#include "BytecodeGenerator.h"
#include "OpCodes.h"
struct BytecodeMetaData
{
const char* name;
const char* structure;
int length;
};
void Disassemble(GlobalData* globalData, const std::vector<Bytecode>* buffer)
{
static BytecodeMetaData bytecodeList[op_last];
static bool initialized = false;
if (!initialized)
{
#define INITBYTECODE(_opcode, _structure, _length) \
bytecodeList[_opcode].name= #_opcode; \
bytecodeList[_opcode].structure = _structure; \
bytecodeList[_opcode].length = _length; \
OPCODES(INITBYTECODE)
#undef INITBYTECODE
initialized = true;
}
for(unsigned i=0; i<buffer->size(); ++i)
{
assert(buffer->at(i).Code < op_last);
BytecodeMetaData* meta = &bytecodeList[buffer->at(i).Code];
printf("%d:\t%s", i, meta->name);
const char* structure = meta->structure;
while(*structure)
{
i++;
Bytecode byte = buffer->at(i);
switch(*structure)
{
case 'r':
printf("\n\tregister %d", byte.RegisterNumber);
break;
case 's':
printf("\n\tconstant string index %d \"%s\"", byte.ConstantStringIndex, globalData->GetConstantString(byte.ConstantStringIndex).c_str());
break;
case 'i':
printf("\n\tconstant int %d", byte.ConstantInt);
break;
case 'f':
printf("\n\tconstant float index %d %lf", byte.ConstantFloatIndex, globalData->GetConstantFloat(byte.ConstantFloatIndex));
break;
}
++ structure;
}
printf("\n");
}
}