-
Notifications
You must be signed in to change notification settings - Fork 13
/
scriptCompiler.cpp
462 lines (389 loc) · 19.2 KB
/
scriptCompiler.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include "scriptCompiler.hpp"
#include "commandList.hpp"
#include <charconv>
#include <fstream>
#include <virtualmachine.h>
#include <parsepreprocessor.h>
#include <parsesqf.h>
#include <callstack.h>
#include <commandmap.h>
#include <iostream>
#include <mutex>
#include "stringdata.h"
#include "optimizer/optimizerModuleBase.hpp"
#include "optimizer/optimizer.h"
#include "scriptSerializer.hpp"
std::once_flag commandMapInitFlag;
ScriptCompiler::ScriptCompiler(const std::vector<std::filesystem::path>& includePaths) {
vm = std::make_unique<sqf::virtualmachine>();
vm->err(&std::cout);
vm->wrn(&std::cout);
std::call_once(commandMapInitFlag, []() {
CommandList::init();
//sqf::commandmap::get().init();
});
initIncludePaths(includePaths);
}
CompiledCodeData ScriptCompiler::compileScript(std::filesystem::path file) {
std::ifstream inputFile(file);
auto filesize = std::filesystem::file_size(file);
std::string scriptCode;
scriptCode.resize(filesize);
inputFile.read(scriptCode.data(), filesize);
bool errflag = false;
if (
static_cast<unsigned char>(scriptCode[0]) == 0xef &&
static_cast<unsigned char>(scriptCode[1]) == 0xbb &&
static_cast<unsigned char>(scriptCode[2]) == 0xbf
) {
scriptCode.erase(0, 3);
}
if (scriptCode.find("script_component.hpp\"") == std::string::npos) {
throw std::domain_error("no include");
}
auto preprocessedScript = sqf::parse::preprocessor::parse(vm.get(), scriptCode, errflag, file.string());
vm->err_buffprint();
vm->wrn_buffprint();
if (errflag) {
__debugbreak();
vm->err_clear();
return CompiledCodeData();
}
bool errorflag = false;
auto ast = vm->parse_sqf_cst(preprocessedScript, errorflag);
if (errorflag) __debugbreak();
//print_navigate_ast(&std::cout, ast, sqf::parse::sqf::astkindname);
CompiledCodeData stuff;
CompileTempData temp;
ScriptCodePiece mainCode;
if (true) {
auto node = OptimizerModuleBase::nodeFromAST(ast);
//std::ofstream nodeo("P:\\node.txt");
//node.dumpTree(nodeo, 0);
//nodeo.close();
auto res = node.bottomUpFlatten();
Optimizer opt;
opt.optimize(node);
//std::ofstream nodeop("P:\\nodeOpt.txt");
//node.dumpTree(nodeop, 0);
//nodeop.close();
ASTToInstructions(stuff, temp, mainCode.code, node);
mainCode.contentString = stuff.constants.size();
stuff.constants.emplace_back(std::move(preprocessedScript));
stuff.codeIndex = stuff.constants.size();
stuff.constants.emplace_back(std::move(mainCode));
//std::ofstream output2("P:\\outOpt.sqfa", std::ofstream::binary);
//ScriptSerializer::compiledToHumanReadable(stuff, output2);
//output2.flush();
} else {
ASTToInstructions(stuff, temp, mainCode.code, ast);
mainCode.contentString = stuff.constants.size();
stuff.constants.emplace_back(std::move(preprocessedScript));
stuff.codeIndex = stuff.constants.size();
stuff.constants.emplace_back(std::move(mainCode));
}
//auto outputPath2 = file.parent_path() / (file.stem().string() + ".sqfa");
//std::ofstream output2(outputPath2, std::ofstream::binary);
std::ofstream output2("P:\\outOrig.sqfa", std::ofstream::binary);
ScriptSerializer::compiledToHumanReadable(stuff, output2);
output2.flush();
return stuff;
}
void ScriptCompiler::ASTToInstructions(CompiledCodeData& output, CompileTempData& temp, std::vector<ScriptInstruction>& instructions, const astnode& node) const {
auto getFileIndex = [&](const std::string& filename) -> uint8_t
{
auto found = temp.fileLoc.find(filename);
if (found != temp.fileLoc.end())
return found->second;
auto index = static_cast<uint8_t>(output.fileNames.size());
output.fileNames.emplace_back(filename);
temp.fileLoc.insert({ filename, index });
return index;
};
//#TODO get constant index and keep sets of bool/float/string constants
auto nodeType = static_cast<sqf::parse::sqf::sqfasttypes::sqfasttypes>(node.kind);
switch (nodeType) {
case sqf::parse::sqf::sqfasttypes::ASSIGNMENT:
case sqf::parse::sqf::sqfasttypes::ASSIGNMENTLOCAL: {
auto varname = node.children[0].content;
//need value on stack first
ASTToInstructions(output, temp, instructions, node.children[1]);
std::transform(varname.begin(), varname.end(), varname.begin(), ::tolower);
instructions.emplace_back(ScriptInstruction{
nodeType == sqf::parse::sqf::sqfasttypes::ASSIGNMENT ?
InstructionType::assignTo
:
InstructionType::assignToLocal
, node.offset, getFileIndex(node.file), node.line, varname });
}
break;
case sqf::parse::sqf::sqfasttypes::BEXP1:
case sqf::parse::sqf::sqfasttypes::BEXP2:
case sqf::parse::sqf::sqfasttypes::BEXP3:
//number
//binaryop
//number
case sqf::parse::sqf::sqfasttypes::BEXP4:
//unary left arg
//binary command
//code on right
case sqf::parse::sqf::sqfasttypes::BEXP5:
case sqf::parse::sqf::sqfasttypes::BEXP6:
//constant
//binaryop
//unaryop
case sqf::parse::sqf::sqfasttypes::BEXP7:
case sqf::parse::sqf::sqfasttypes::BEXP8:
case sqf::parse::sqf::sqfasttypes::BEXP9:
case sqf::parse::sqf::sqfasttypes::BEXP10:
case sqf::parse::sqf::sqfasttypes::BINARYEXPRESSION: {
//get left arg on stack
ASTToInstructions(output, temp, instructions, node.children[0]);
//get right arg on stack
ASTToInstructions(output, temp, instructions, node.children[2]);
//push binary op
auto name = node.children[1].content;
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
instructions.emplace_back(ScriptInstruction{ InstructionType::callBinary, node.offset, getFileIndex(node.file), node.line, name });
break;
}
case sqf::parse::sqf::sqfasttypes::BINARYOP: __debugbreak(); break;
case sqf::parse::sqf::sqfasttypes::PRIMARYEXPRESSION: __debugbreak(); break;
case sqf::parse::sqf::sqfasttypes::NULAROP: {
//push nular op
auto name = node.content;
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
if (name == "true"sv) {
ScriptConstant newConst;
newConst = true;
auto index = output.constants.size();
output.constants.emplace_back(std::move(newConst));
instructions.emplace_back(ScriptInstruction{ InstructionType::push, node.offset, getFileIndex(node.file), node.line, index });
} else if (name == "false"sv) {
ScriptConstant newConst;
newConst = false;
auto index = output.constants.size();
output.constants.emplace_back(std::move(newConst));
instructions.emplace_back(ScriptInstruction{ InstructionType::push, node.offset, getFileIndex(node.file), node.line, index });
} else
instructions.emplace_back(ScriptInstruction{ InstructionType::callNular, node.offset, getFileIndex(node.file), node.line, name });
break;
}
case sqf::parse::sqf::sqfasttypes::UNARYEXPRESSION: {
//unary operator
//right arg
//get right arg on stack
ASTToInstructions(output, temp, instructions, node.children[1]);
//push unary op
auto name = node.children[0].content;
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
instructions.emplace_back(ScriptInstruction{ InstructionType::callUnary, node.offset, getFileIndex(node.file), node.line, name });
break;
}
case sqf::parse::sqf::sqfasttypes::UNARYOP: __debugbreak(); break;
case sqf::parse::sqf::sqfasttypes::NUMBER:
case sqf::parse::sqf::sqfasttypes::HEXNUMBER: {
ScriptConstant newConst;
float val;
auto res =
(nodeType == sqf::parse::sqf::sqfasttypes::HEXNUMBER) ?
std::from_chars(node.content.data()+2, node.content.data() + node.content.size(), val, std::chars_format::hex)
:
std::from_chars(node.content.data(), node.content.data() + node.content.size(), val);
if (res.ec == std::errc::invalid_argument) {
throw std::runtime_error("invalid scalar at: " + node.file + ":" + std::to_string(node.line));
}
else if (res.ec == std::errc::result_out_of_range) {
throw std::runtime_error("scalar out of range at: " + node.file + ":" + std::to_string(node.line));
}
newConst = val;
auto index = output.constants.size();
output.constants.emplace_back(std::move(newConst));
instructions.emplace_back(ScriptInstruction{ InstructionType::push, node.offset, getFileIndex(node.file), node.line, index });
break;
}
case sqf::parse::sqf::sqfasttypes::VARIABLE: {
//getvariable
auto varname = node.content;
std::transform(varname.begin(), varname.end(), varname.begin(), ::tolower);
instructions.emplace_back(ScriptInstruction{ InstructionType::getVariable, node.offset, getFileIndex(node.file), node.line, varname });
break;
}
case sqf::parse::sqf::sqfasttypes::STRING: {
ScriptConstant newConst;
newConst = sqf::stringdata::parse_from_sqf(node.content);
auto index = output.constants.size();
output.constants.emplace_back(std::move(newConst));
instructions.emplace_back(ScriptInstruction{ InstructionType::push, node.offset, getFileIndex(node.file), node.line, index });
break;
}
case sqf::parse::sqf::sqfasttypes::CODE: {
ScriptConstant newConst;
std::vector<ScriptInstruction> instr;
for (auto& it : node.children) {
instr.emplace_back(ScriptInstruction{ InstructionType::endStatement, node.offset, 0, 0 });
ASTToInstructions(output, temp, instr, it);
}
newConst = ScriptCodePiece(std::move(instr), node.length-2, node.offset+1 );
//#TODO duplicate detection
auto index = output.constants.size();
output.constants.emplace_back(std::move(newConst));
instructions.emplace_back(ScriptInstruction{ InstructionType::push, node.offset, getFileIndex(node.file), node.line, index });
break;
}
case sqf::parse::sqf::sqfasttypes::ARRAY: {
//push elements first
for (auto& it : node.children)
ASTToInstructions(output, temp, instructions, it);
//#TODO can already check here if all arguments are const and push a const
//AST is const function. That just checks whether the whole tree only contains constants
//make array instruction
//array instruction has size as argument
instructions.emplace_back(ScriptInstruction{ InstructionType::makeArray, node.offset, getFileIndex(node.file), node.line, node.children.size() });
break;
}
//case sqf::parse::sqf::sqfasttypes::NA:
//case sqf::parse::sqf::sqfasttypes::SQF:
//case sqf::parse::sqf::sqfasttypes::STATEMENT:
//case sqf::parse::sqf::sqfasttypes::BRACKETS:
// for (auto& it : node.children)
// stuffAST(output, instructions, it);
default:
for (size_t i = 0; i < node.children.size(); i++) {
if (i != 0 || instructions.empty()) //end statement
instructions.emplace_back(ScriptInstruction{ InstructionType::endStatement, node.offset, 0, 0 });
ASTToInstructions(output, temp, instructions, node.children[i]);
}
}
}
ScriptConstantArray ScriptCompiler::ASTParseArray(CompiledCodeData& output, CompileTempData& temp, const OptimizerModuleBase::Node& node) const {
ScriptConstantArray newConst;
for (auto& it : node.children) {
if (it.value.index() == 0) {//is code
ScriptCodePiece codeConst;
std::vector<ScriptInstruction> instr;
for (auto& codeIt : it.children) {
//std::stringstream dbg;
//codeIt.dumpTree(dbg, 0);
//auto res = dbg.str();
instr.emplace_back(ScriptInstruction{ InstructionType::endStatement, node.offset, 0, 0 });
ASTToInstructions(output, temp, instr, codeIt);
}
codeConst.contentString = std::get<ScriptCodePiece>(it.value).contentString;
codeConst.code = std::move(instr);
newConst.content.emplace_back(std::move(codeConst));
} else if (it.value.index() == 4) {//array
newConst.content.emplace_back(ASTParseArray(output, temp, it));
} else {
newConst.content.emplace_back(it.value);
}
}
return newConst;
}
void ScriptCompiler::ASTToInstructions(CompiledCodeData& output, CompileTempData& temp,
std::vector<ScriptInstruction>& instructions, const OptimizerModuleBase::Node& node) const {
auto getFileIndex = [&](const std::string & filename) -> uint8_t
{
auto found = temp.fileLoc.find(filename);
if (found != temp.fileLoc.end())
return found->second;
auto index = static_cast<uint8_t>(output.fileNames.size());
output.fileNames.emplace_back(filename);
temp.fileLoc.insert({ filename, index });
return index;
};
switch (node.type) {
case InstructionType::push: {
switch (node.value.index()) {
case 0: {//Code
ScriptConstant newConst;
std::vector<ScriptInstruction> instr;
for (auto& it : node.children) {
instr.emplace_back(ScriptInstruction{ InstructionType::endStatement, node.offset, 0, 0 });
ASTToInstructions(output, temp, instr, it);
}
newConst = node.value;
std::get<ScriptCodePiece>(newConst).code = std::move(instr);
//#TODO duplicate detection
auto index = output.constants.size();
output.constants.emplace_back(std::move(newConst));
instructions.emplace_back(ScriptInstruction{ InstructionType::push, node.offset, getFileIndex(node.file), node.line, index });
} break;
case 1: {//String
auto index = output.constants.size();
output.constants.emplace_back(node.value);
instructions.emplace_back(ScriptInstruction{ InstructionType::push, node.offset, getFileIndex(node.file), node.line, index });
} break;
case 2: {//Number
auto index = output.constants.size();
output.constants.emplace_back(node.value);
instructions.emplace_back(ScriptInstruction{ InstructionType::push, node.offset, getFileIndex(node.file), node.line, index });
} break;
case 3: {//Bool
auto index = output.constants.size();
output.constants.emplace_back(node.value);
instructions.emplace_back(ScriptInstruction{ InstructionType::push, node.offset, getFileIndex(node.file), node.line, index });
} break;
case 4: {//Array
auto value = ASTParseArray(output, temp, node);
auto index = output.constants.size();
output.constants.emplace_back(value);
instructions.emplace_back(ScriptInstruction{ InstructionType::push, node.offset, getFileIndex(node.file), node.line, index });
} break;
}
}break;
case InstructionType::callUnary: {
ASTToInstructions(output, temp, instructions, node.children[0]);
//push unary op
auto name = std::get<STRINGTYPE>(node.value);
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
instructions.emplace_back(ScriptInstruction{ InstructionType::callUnary, node.offset, getFileIndex(node.file), node.line, name });
} break;
case InstructionType::callBinary: {
//get left arg on stack
ASTToInstructions(output, temp, instructions, node.children[0]);
//get right arg on stack
ASTToInstructions(output, temp, instructions, node.children[1]);
//push binary op
auto name = std::get<STRINGTYPE>(node.value);
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
instructions.emplace_back(ScriptInstruction{ InstructionType::callBinary, node.offset, getFileIndex(node.file), node.line, name });
} break;
case InstructionType::callNular: {
auto name = std::get<STRINGTYPE>(node.value);
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
instructions.emplace_back(ScriptInstruction{ InstructionType::callNular, node.offset, getFileIndex(node.file), node.line, name });
} break;
case InstructionType::assignTo:
case InstructionType::assignToLocal: {
auto varname = std::get<STRINGTYPE>(node.value);
//need value on stack first
ASTToInstructions(output, temp, instructions, node.children[0]);
std::transform(varname.begin(), varname.end(), varname.begin(), ::tolower);
instructions.emplace_back(ScriptInstruction{ node.type, node.offset, getFileIndex(node.file), node.line, varname });
} break;
case InstructionType::getVariable: {
auto varname = std::get<STRINGTYPE>(node.value);
std::transform(varname.begin(), varname.end(), varname.begin(), ::tolower);
instructions.emplace_back(ScriptInstruction{ InstructionType::getVariable, node.offset, getFileIndex(node.file), node.line, varname });
} break;
case InstructionType::makeArray: {
for (auto& it : node.children)
ASTToInstructions(output, temp, instructions, it);
//#TODO can already check here if all arguments are const and push a const
instructions.emplace_back(ScriptInstruction{ InstructionType::makeArray, node.offset, getFileIndex(node.file), node.line, node.children.size() });
} break;
case InstructionType::endStatement: {
for (size_t i = 0; i < node.children.size(); i++) {
if (i != 0 || instructions.empty()) //end statement
instructions.emplace_back(ScriptInstruction{ InstructionType::endStatement, node.offset, 0, 0 });
ASTToInstructions(output, temp, instructions, node.children[i]);
}
} break;
}
}
void ScriptCompiler::initIncludePaths(const std::vector<std::filesystem::path>& paths) const {
for (auto& includefolder : paths) {
vm->get_filesystem().add_mapping_auto(includefolder.string());
}
}