Skip to content

Commit 725c203

Browse files
committed
Add array serialization
1 parent f8cc0c1 commit 725c203

File tree

4 files changed

+105
-79
lines changed

4 files changed

+105
-79
lines changed

src/optimizer/optimizerModuleConstantFold.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ class OptimizerConstantFoldActionMap : public Singleton<OptimizerConstantFoldAct
9797
// node.constant = true;
9898
//};
9999

100-
binaryActions["/"] = [](OptimizerModuleBase::Node & node) -> void {
101-
float leftArg = std::get<float>(node.children[0].value);
102-
float rightArg = std::get<float>(node.children[1].value);
103-
104-
node.type = InstructionType::push;
105-
node.children.clear();
106-
node.constant = true;
107-
node.value = leftArg / rightArg;
108-
};
100+
//binaryActions["/"] = [](OptimizerModuleBase::Node & node) -> void { //#TODO could be config with two strings?
101+
// float leftArg = std::get<float>(node.children[0].value);
102+
// float rightArg = std::get<float>(node.children[1].value);
103+
//
104+
// node.type = InstructionType::push;
105+
// node.children.clear();
106+
// node.constant = true;
107+
// node.value = leftArg / rightArg;
108+
//};
109109
binaryActions["*"] = [](OptimizerModuleBase::Node & node) -> void {
110110
float leftArg = std::get<float>(node.children[0].value);
111111
float rightArg = std::get<float>(node.children[1].value);

src/scriptCompiler.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,49 +69,46 @@ CompiledCodeData ScriptCompiler::compileScript(std::filesystem::path file) {
6969
ScriptCodePiece mainCode;
7070

7171

72-
{
72+
if (true) {
7373
auto node = OptimizerModuleBase::nodeFromAST(ast);
74-
74+
7575
//std::ofstream nodeo("P:\\node.txt");
7676
//node.dumpTree(nodeo, 0);
7777
//nodeo.close();
78-
78+
7979
auto res = node.bottomUpFlatten();
80-
80+
8181
Optimizer opt;
82-
82+
8383
opt.optimize(node);
84-
84+
8585
//std::ofstream nodeop("P:\\nodeOpt.txt");
8686
//node.dumpTree(nodeop, 0);
8787
//nodeop.close();
88-
89-
CompiledCodeData stuff;
90-
CompileTempData temp;
91-
ScriptCodePiece mainCode;
92-
93-
88+
9489
ASTToInstructions(stuff, temp, mainCode.code, node);
9590
mainCode.contentString = stuff.constants.size();
9691
stuff.constants.emplace_back(std::move(preprocessedScript));
9792
stuff.codeIndex = stuff.constants.size();
9893
stuff.constants.emplace_back(std::move(mainCode));
99-
100-
94+
if (stuff.codeIndex == 220) __debugbreak();
95+
10196
//std::ofstream output2("P:\\outOpt.sqfa", std::ofstream::binary);
10297
//ScriptSerializer::compiledToHumanReadable(stuff, output2);
10398
//output2.flush();
99+
} else {
100+
ASTToInstructions(stuff, temp, mainCode.code, ast);
101+
mainCode.contentString = stuff.constants.size();
102+
stuff.constants.emplace_back(std::move(preprocessedScript));
103+
stuff.codeIndex = stuff.constants.size();
104+
stuff.constants.emplace_back(std::move(mainCode));
104105
}
105106

106107

107108

108109

109110

110-
ASTToInstructions(stuff, temp, mainCode.code, ast);
111-
mainCode.contentString = stuff.constants.size();
112-
stuff.constants.emplace_back(std::move(preprocessedScript));
113-
stuff.codeIndex = stuff.constants.size();
114-
stuff.constants.emplace_back(std::move(mainCode));
111+
115112

116113

117114
//auto outputPath2 = file.parent_path() / (file.stem().string() + ".sqfa");

src/scriptSerializer.cpp

Lines changed: 77 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -314,32 +314,87 @@ std::vector<ScriptInstruction> ScriptSerializer::binaryToInstructions(const Comp
314314
return result;
315315
}
316316

317+
void ScriptSerializer::writeConstant(const CompiledCodeData& code, const ScriptConstant& constant, std::ostream& output) {
318+
auto type = getConstantType(constant);
319+
writeT(static_cast<uint8_t>(type), output);
320+
321+
switch (type) {
322+
case ConstantType::code: {
323+
auto& instructions = std::get<ScriptCodePiece>(constant);
324+
writeT<uint64_t>(instructions.contentString, output);
325+
instructionsToBinary(code, instructions.code, output);
326+
} break;
327+
case ConstantType::string:
328+
output.write(std::get<STRINGTYPE>(constant).c_str(), std::get<STRINGTYPE>(constant).size() + 1);
329+
break;
330+
case ConstantType::scalar:
331+
writeT(std::get<float>(constant), output);
332+
break;
333+
case ConstantType::boolean:
334+
writeT(std::get<bool>(constant), output);
335+
break;
336+
case ConstantType::array: {
337+
auto& array = std::get<ScriptConstantArray>(constant);
338+
writeT<uint32_t>(array.content.size(), output);
339+
340+
for (auto& cnst : array.content)
341+
writeConstant(code, cnst, output);
342+
} break;
343+
default: __debugbreak();
344+
}
345+
}
346+
347+
ScriptConstant ScriptSerializer::readConstant(CompiledCodeData& code, std::istream& input) {
348+
349+
auto typeRaw = readT<uint8_t>(input);
350+
351+
auto type = static_cast<ConstantType>(typeRaw);
352+
353+
switch (type) {
354+
case ConstantType::code: {
355+
ScriptCodePiece piece;
356+
piece.contentString = readT<uint64_t>(input);
357+
piece.code = binaryToInstructions(code, input);
358+
359+
return piece;
360+
} break;
361+
case ConstantType::string: {
362+
std::string content;
363+
std::getline(input, content, '\0');
364+
#ifndef ASC_INTERCEPT
365+
return content;
366+
#else
367+
return static_cast<STRINGTYPE>(content);
368+
#endif
369+
} break;
370+
case ConstantType::scalar: {
371+
auto data = readT<float>(input);
372+
return data;
373+
} break;
374+
case ConstantType::boolean: {
375+
auto data = readT<bool>(input);
376+
return data;
377+
} break;
378+
case ConstantType::array: {
379+
auto size = readT<uint32_t>(input);
380+
ScriptConstantArray arr;
381+
arr.content.reserve(size);
382+
for (int i = 0; i < size; ++i) {
383+
arr.content.emplace_back(readConstant(code, input));
384+
}
385+
return arr;
386+
} break;
387+
default: __debugbreak();
388+
}
389+
390+
}
391+
317392
void ScriptSerializer::writeConstants(const CompiledCodeData& code, std::ostream& output) {
318393
writeT(static_cast<uint8_t>(SerializedBlockType::constant), output);
319394
writeT(static_cast<uint16_t>(code.constants.size()), output);
320395
int index = 0;
321396
for (auto& constant : code.constants) {
322-
auto type = getConstantType(constant);
323-
writeT(static_cast<uint8_t>(type), output);
324-
325-
index++;
326-
327-
switch (type) {
328-
case ConstantType::code: {
329-
auto& instructions = std::get<ScriptCodePiece>(constant);
330-
writeT<uint64_t>(instructions.contentString, output);
331-
instructionsToBinary(code, instructions.code, output);
332-
} break;
333-
case ConstantType::string:
334-
output.write(std::get<STRINGTYPE>(constant).c_str(), std::get<STRINGTYPE>(constant).size() + 1);
335-
break;
336-
case ConstantType::scalar:
337-
writeT(std::get<float>(constant), output);
338-
break;
339-
case ConstantType::boolean:
340-
writeT(std::get<bool>(constant), output);
341-
break;
342-
}
397+
writeConstant(code, constant, output);
343398
}
344399
}
345400

@@ -348,36 +403,7 @@ void ScriptSerializer::readConstants(CompiledCodeData& code, std::istream& input
348403

349404
code.constants.reserve(constantCount);
350405
for (int i = 0; i < constantCount; ++i) {
351-
auto typeRaw = readT<uint8_t>(input);
352-
353-
auto type = static_cast<ConstantType>(typeRaw);
354-
355-
switch (type) {
356-
case ConstantType::code: {
357-
ScriptCodePiece piece;
358-
piece.contentString = readT<uint64_t>(input);
359-
piece.code = binaryToInstructions(code, input);
360-
361-
code.constants.emplace_back(std::move(piece));
362-
} break;
363-
case ConstantType::string: {
364-
std::string content;
365-
std::getline(input, content, '\0');
366-
#ifndef ASC_INTERCEPT
367-
code.constants.emplace_back(std::move(content));
368-
#else
369-
code.constants.emplace_back(static_cast<STRINGTYPE>(content));
370-
#endif
371-
} break;
372-
case ConstantType::scalar: {
373-
auto data = readT<float>(input);
374-
code.constants.emplace_back(data);
375-
} break;
376-
case ConstantType::boolean: {
377-
auto data = readT<bool>(input);
378-
code.constants.emplace_back(data);
379-
} break;
380-
}
406+
code.constants.emplace_back(readConstant(code, input));
381407
}
382408

383409
}

src/scriptSerializer.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class ScriptSerializer {
2222
static ScriptInstruction binaryToInstruction(const CompiledCodeData& code, std::istream& input);
2323
static std::vector<ScriptInstruction> binaryToInstructions(const CompiledCodeData& code, std::istream& input);;
2424

25+
static void writeConstant(const CompiledCodeData& code, const ScriptConstant& constant, std::ostream& output);
26+
static ScriptConstant readConstant(CompiledCodeData& code, std::istream& input);
27+
2528
static void writeConstants(const CompiledCodeData& code, std::ostream& output);
2629
static void readConstants(CompiledCodeData& code, std::istream& input);
2730

0 commit comments

Comments
 (0)