This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisassembler-context.cpp
More file actions
204 lines (151 loc) · 4.72 KB
/
disassembler-context.cpp
File metadata and controls
204 lines (151 loc) · 4.72 KB
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
#ifndef JDECOMPILER_DISASSEMBLER_CONTEXT_CPP
#define JDECOMPILER_DISASSEMBLER_CONTEXT_CPP
#include "context.cpp"
namespace jdecompiler {
struct DisassemblerContext final: Context {
public:
const ConstantPool& constPool;
const uint32_t length;
const uint8_t* const bytes;
index_t index = 0;
private:
vector<Instruction*> instructions;
mutable vector<const Block*> blocks, inactiveBlocks;
const Block* currentBlock = nullptr;
map<pos_t, index_t> indexMap;
map<index_t, pos_t> posMap;
public:
DisassemblerContext(const ConstantPool& constPool, uint32_t length, const uint8_t bytes[]):
constPool(constPool), length(length), bytes(bytes) {
if(length == 0)
return;
while(available()) {
indexMap[pos] = index;
posMap[index] = pos;
index++;
instructions.push_back(nextInstruction());
next();
}
}
protected:
friend struct Method;
void decompile() {
const index_t size = instructions.size();
currentBlock = new RootBlock(size);
index = 0;
for(const Instruction* instruction = instructions[0]; index < size; instruction = instructions[++index]) {
pos = posMap[index];
if(instruction != nullptr) {
const Block* block = instruction->toBlock(*this);
if(block != nullptr)
addBlock(block);
}
updateBlocks();
}
}
public:
inline const vector<Instruction*>& getInstructions() const {
return instructions;
}
const Instruction* getInstruction(index_t index) const {
if(index >= instructions.size())
throw IndexOutOfBoundsException(index, instructions.size());
return instructions[index];
}
const Instruction* getInstructionNoexcept(index_t index) const noexcept {
if(index >= instructions.size())
return nullptr;
return instructions[index];
}
inline const vector<const Block*>& getBlocks() const {
return blocks;
}
inline const Block* getCurrentBlock() const {
return currentBlock;
}
inline void addBlock(const Block* block) const {
blocks.push_back(block);
inactiveBlocks.push_back(block);
}
protected:
void updateBlocks() {
while(index >= currentBlock->end()) {
if(currentBlock->parentBlock == nullptr)
throw DecompilationException("Unexpected end of global function block " + currentBlock->toDebugString());
log(index, " end of ", currentBlock->toDebugString());
currentBlock = currentBlock->parentBlock;
}
for(auto i = inactiveBlocks.begin(); i != inactiveBlocks.end(); ) {
const Block* block = *i;
if(index >= block->start()) {
if(block->end() > currentBlock->end()) {
if(block->end() != static_cast<index_t>(-1))
throw DecompilationException("Block " + block->toDebugString() +
" is out of bounds of the parent block " + currentBlock->toDebugString());
block->endIndex = currentBlock->end();
}
log(index, "start of ", block->toDebugString());
currentBlock->addInnerBlock(block);
assert(block != currentBlock);
if(block->parentBlock == nullptr) {
block->parentBlock = currentBlock; // crutch for tryBlocks
} else {
assert(block->parentBlock == currentBlock);
}
currentBlock = block;
inactiveBlocks.erase(i);
} else
++i;
}
}
inline uint8_t next() {
return static_cast<uint8_t>(bytes[++pos]);
}
inline int8_t nextByte() {
return static_cast<int8_t>(next());
}
inline uint8_t nextUByte() {
return static_cast<uint8_t>(next());
}
inline int16_t nextShort() {
return static_cast<int16_t>(nextUShort());
}
inline uint16_t nextUShort() {
return static_cast<uint16_t>(next() << 8 | next());
}
inline int32_t nextInt() {
return static_cast<int32_t>(nextUInt());
}
inline uint32_t nextUInt() {
return static_cast<uint32_t>(next() << 24 | next() << 16 | next() << 8 | next());
}
inline uint8_t current() const {
return bytes[pos];
}
inline bool available() const {
return length - pos > 0;
}
Instruction* nextInstruction();
public:
inline void skip(int32_t count) {
pos += static_cast<uint32_t>(count);
}
index_t posToIndex(pos_t pos) const {
const auto foundPos = indexMap.find(pos);
if(foundPos != indexMap.end())
return foundPos->second;
throw BytecodePosOutOfBoundsException(pos, length);
}
pos_t indexToPos(index_t index) const {
const auto foundIndex = posMap.find(index);
if(foundIndex != posMap.end())
return foundIndex->second;
throw BytecodeIndexOutOfBoundsException(index, length);
}
template<typename... Args>
inline void warning(Args... args) const {
print(cerr << "Disassembler warning: ", args...);
}
};
}
#endif