-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunk.cpp
286 lines (254 loc) · 11.4 KB
/
Chunk.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
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
#include "Chunk.h"
#include <iomanip>
#include <sstream>
#include "Version.h"
#include "Utils.h"
#include "Object.h"
#include "Logger.h"
namespace lwscript
{
Chunk::Chunk(const OpCodes &opcodes, const std::vector<Value> &constants)
: opCodes(opcodes), constants(constants)
{
}
#ifndef NDEBUG
STD_STRING Chunk::ToString() const
{
STD_STRING result;
result += OpCodeToString(opCodes);
for (const auto &c : constants)
{
if (IS_FUNCTION_VALUE(c))
result += TO_FUNCTION_VALUE(c)->ToStringWithChunk();
}
return result;
}
#endif
std::vector<uint8_t> Chunk::Serialize() const
{
std::vector<uint8_t> result;
auto magNumberBytes = ByteConverter::ToU32ByteList(LWSCRIPT_BINARY_FILE_MAGIC_NUMBER);
result.insert(result.end(), magNumberBytes.begin(), magNumberBytes.end());
auto versionBytes = ByteConverter::ToU32ByteList(LWSCRIPT_VERSION_BINARY);
result.insert(result.end(), versionBytes.begin(), versionBytes.end());
auto opCodeCount = ByteConverter::ToU32ByteList(opCodes.size());
result.insert(result.end(), opCodeCount.begin(), opCodeCount.end());
result.insert(result.end(), opCodes.begin(), opCodes.end());
auto constantsCount = ByteConverter::ToU32ByteList(constants.size());
result.insert(result.end(), constantsCount.begin(), constantsCount.end());
for (const auto &constant : constants)
{
auto bytes = constant.Serialize();
auto size = ByteConverter::ToU32ByteList(bytes.size());
result.insert(result.end(), size.begin(), size.end());
result.insert(result.end(), bytes.begin(), bytes.end());
}
return result;
}
void Chunk::Deserialize(const std::vector<uint8_t> &data)
{
auto magicNumber = ByteConverter::GetU32Integer(data,0);
if (magicNumber != LWSCRIPT_BINARY_FILE_MAGIC_NUMBER)
Logger::Error(TEXT("Invalid lwscript binary file,cannot deserialize from this file"));
auto versionNumber = ByteConverter::GetU32Integer(data,4);
if (versionNumber != LWSCRIPT_VERSION_BINARY)
Logger::Error(TEXT("Invalid lwscript binary file version of {},current version is {}"), versionNumber, LWSCRIPT_VERSION_BINARY);
auto opCodesCount = ByteConverter::GetU32Integer(data,8);
opCodes.insert(opCodes.end(), data.begin() + 12, data.begin() + 12 + opCodesCount);
auto idx = 12 + opCodesCount;
auto constantsCount = ByteConverter::GetU32Integer(data,idx);
idx += 4;
for (int32_t i = 0; i < constantsCount; ++i)
{
auto constantSize = ByteConverter::GetU32Integer(data,idx);
idx += 4;
std::vector<uint8_t> constantBytes(data.begin() + idx, data.begin() + idx + constantSize);
Value v;
v.Deserialize(constantBytes);
constants.emplace_back(v);
}
}
STD_STRING Chunk::OpCodeToString(const OpCodes &opcodes) const
{
#define CASE(opCode) \
case opCode: \
{ \
auto tok = opCodeRelatedTokens[opcodes[i + 1]]; \
auto tokStr = tok->ToString(); \
STD_STRING tokGap(maxTokenShowSize - tokStr.size(), TCHAR(' ')); \
tokStr += tokGap; \
cout << tokStr << std::setfill(TCHAR('0')) << std::setw(8) << i << TEXT("\t") << TEXT(#opCode) << std::endl; \
i += 1; \
break; \
}
#define CASE_JUMP(opCode, op) \
case opCode: \
{ \
auto tok = opCodeRelatedTokens[opcodes[i + 1]]; \
uint16_t addressOffset = opcodes[i + 2] << 8 | opcodes[i + 3]; \
auto tokStr = tok->ToString(); \
STD_STRING tokGap(maxTokenShowSize - tokStr.size(), TCHAR(' ')); \
tokStr += tokGap; \
cout << tokStr << std::setfill(TCHAR('0')) << std::setw(8) << i << TEXT("\t") << TEXT(#opCode) << TEXT("\t") << i << "->" << i op addressOffset + 3 << std::endl; \
i += 3; \
break; \
}
#define CASE_1(opCode) \
case opCode: \
{ \
auto tok = opCodeRelatedTokens[opcodes[i + 1]]; \
auto pos = opcodes[i + 2]; \
auto tokStr = tok->ToString(); \
STD_STRING tokGap(maxTokenShowSize - tokStr.size(), TCHAR(' ')); \
tokStr += tokGap; \
cout << tokStr << std::setfill(TCHAR('0')) << std::setw(8) << i << TEXT("\t") << TEXT(#opCode) << TEXT("\t") << pos << std::endl; \
i += 2; \
break; \
}
const uint32_t maxTokenShowSize = GetBiggestTokenLength() + 4; // 4 for a gap " "
STD_STRING_STREAM cout;
for (int32_t i = 0; i < opcodes.size(); ++i)
{
switch (opcodes[i])
{
CASE(OP_NULL)
CASE(OP_ADD)
CASE(OP_SUB)
CASE(OP_MUL)
CASE(OP_DIV)
CASE(OP_LESS)
CASE(OP_GREATER)
CASE(OP_NOT)
CASE(OP_MINUS)
CASE(OP_BIT_AND)
CASE(OP_BIT_OR)
CASE(OP_BIT_XOR)
CASE(OP_BIT_NOT)
CASE(OP_BIT_LEFT_SHIFT)
CASE(OP_BIT_RIGHT_SHIFT)
CASE(OP_EQUAL)
CASE(OP_FACTORIAL)
CASE(OP_CLOSE_UPVALUE)
CASE(OP_GET_INDEX)
CASE(OP_SET_INDEX)
CASE(OP_POP)
CASE(OP_GET_BASE)
CASE(OP_SET_PROPERTY)
CASE(OP_GET_PROPERTY)
CASE_JUMP(OP_JUMP_IF_FALSE, +)
CASE_JUMP(OP_JUMP, +)
CASE_JUMP(OP_LOOP, -)
CASE_1(OP_RETURN)
CASE_1(OP_ARRAY)
CASE_1(OP_DICT)
CASE_1(OP_SET_GLOBAL)
CASE_1(OP_GET_GLOBAL)
CASE_1(OP_SET_LOCAL)
CASE_1(OP_GET_LOCAL)
CASE_1(OP_SET_UPVALUE)
CASE_1(OP_GET_UPVALUE)
CASE_1(OP_REF_GLOBAL)
CASE_1(OP_REF_LOCAL)
CASE_1(OP_REF_INDEX_GLOBAL)
CASE_1(OP_REF_INDEX_LOCAL)
CASE_1(OP_REF_UPVALUE)
CASE_1(OP_REF_INDEX_UPVALUE)
CASE_1(OP_CALL)
CASE_1(OP_STRUCT)
CASE_1(OP_APPREGATE_RESOLVE)
CASE_1(OP_APPREGATE_RESOLVE_VAR_ARG)
CASE_1(OP_RESET)
case OP_CONSTANT:
{
auto tok = opCodeRelatedTokens[opcodes[i + 1]];
auto pos = opcodes[i + 2];
STD_STRING constantStr = constants[pos].ToString();
auto tokStr = tok->ToString();
STD_STRING tokGap(maxTokenShowSize - tokStr.size(), TCHAR(' '));
tokStr += tokGap;
cout << tokStr << std::setfill(TCHAR('0')) << std::setw(8) << i << TEXT("\tOP_CONSTANT\t") << pos << TEXT("\t'") << constantStr << TEXT("'") << std::endl;
i += 2;
break;
}
case OP_CLASS:
{
auto tok = opCodeRelatedTokens[opcodes[i + 1]];
auto ctorCount = opcodes[i + 2];
auto varCount = opcodes[i + 3];
auto constCount = opcodes[i + 4];
auto parentClassCount = opcodes[i + 5];
auto tokStr = tok->ToString();
STD_STRING tokGap(maxTokenShowSize - tokStr.size(), TCHAR(' '));
tokStr += tokGap;
cout << tokStr << std::setfill(TCHAR('0')) << std::setw(8) << i << TEXT("\tOP_CLASS\t") << ctorCount << TEXT("\t") << varCount << TEXT("\t") << constCount << TEXT("\t") << parentClassCount << std::endl;
i += 5;
break;
}
case OP_CLOSURE:
{
auto tok = opCodeRelatedTokens[opcodes[i + 1]];
auto pos = opcodes[i + 2];
STD_STRING funcStr = (TEXT("<fn ") + TO_FUNCTION_VALUE(constants[pos])->name + TEXT(":0x") + PointerAddressToString((void *)TO_FUNCTION_VALUE(constants[pos])) + TEXT(">"));
auto tokStr = tok->ToString();
STD_STRING tokGap(maxTokenShowSize - tokStr.size(), TCHAR(' '));
tokStr += tokGap;
cout << tokStr << std::setfill(TCHAR('0')) << std::setw(8) << i << TEXT("\tOP_CLOSURE\t") << pos << TEXT("\t") << funcStr << std::endl;
auto upvalueCount = TO_FUNCTION_VALUE(constants[pos])->upValueCount;
if (upvalueCount > 0)
{
cout << TEXT(" upvalues:") << std::endl;
for (auto j = 0; j < upvalueCount; ++j)
{
cout << TEXT(" location ") << opcodes[++i];
cout << TEXT(" | ");
cout << TEXT("depth ") << opcodes[++i] << std::endl;
}
}
i += 2;
break;
}
case OP_MODULE:
{
auto tok = opCodeRelatedTokens[opcodes[i + 1]];
auto varCount = opcodes[i + 2];
auto constCount = opcodes[i + 3];
auto tokStr = tok->ToString();
STD_STRING tokGap(maxTokenShowSize - tokStr.size(), TCHAR(' '));
tokStr += tokGap;
cout << tokStr << std::setfill(TCHAR('0')) << std::setw(8) << i << TEXT("\tOP_MODULE\t") << varCount << TEXT("\t") << constCount << std::endl;
i += 3;
break;
}
default:
break;
}
}
return cout.str();
}
uint32_t Chunk::GetBiggestTokenLength() const
{
uint32_t length = 0;
for (const auto &t : opCodeRelatedTokens)
{
auto l = (uint32_t)t->ToString().size();
if (length < l)
length = l;
}
return length;
}
bool operator==(const Chunk &left, const Chunk &right)
{
if (left.opCodes != right.opCodes)
return false;
if (left.constants.size() != right.constants.size())
return false;
for (int32_t i = 0; i < left.constants.size(); ++i)
if (left.constants[i] != right.constants[i])
return false;
return true;
}
bool operator!=(const Chunk &left, const Chunk &right)
{
return !(left == right);
}
}