Skip to content

Commit 4cb0f46

Browse files
committed
Add Optimizer
1 parent e35fd3f commit 4cb0f46

13 files changed

+943
-34
lines changed

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ endif()
4949

5050
file(GLOB_RECURSE SOURCES_ASC "src/*.hpp" "src/*.cpp" "${LIBRARY_PATH_B64}/base64.cpp")
5151
SOURCE_GROUP("src" FILES ${SOURCES_ASC})
52+
53+
file(GLOB_RECURSE SOURCES_ASC_OPT "src/optimizer/*.hpp" "src/optimizer/*.cpp")
54+
SOURCE_GROUP("src" FILES ${SOURCES_ASC_OPT})
55+
5256
#zstd
5357
SET(ZSTD_BUILD_PROGRAMS OFF CACHE BOOL "no programs" FORCE)
5458
set(ZSTD_USE_STATIC_RUNTIME ON CACHE BOOL "yes" FORCE)
@@ -87,7 +91,7 @@ add_definitions(/DLOADFILE_CACHE) #SQF VM fileio.cpp cache
8791

8892

8993

90-
add_executable("ArmaScriptCompiler" ${SOURCES_ASC} ${SOURCES_SQFVM})
94+
add_executable("ArmaScriptCompiler" ${SOURCES_ASC} ${SOURCES_SQFVM} ${SOURCES_ASC_OPT})
9195

9296
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${INCLUDE_PATH_ZSTD} ${LIBRARY_PATH_B64}) # ${INCLUDE_PATH_ZSTD} ${INCLUDE_PATH_SQFVM}
9397

@@ -121,6 +125,7 @@ if(CMAKE_COMPILER_IS_GNUCXX)
121125
target_link_options("ArmaScriptCompiler" PRIVATE "-m32" "-fPIC" "-static" "-static-libgcc" "-static-libstdc++"
122126
"$<$<CONFIG:Release>:-Wl,--gc-sections>"
123127
"$<$<CONFIG:Release>:-Wl,--strip-all>"
128+
"-Wl,--stack,4194304" #STack size
124129
)
125130
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
126131

@@ -136,6 +141,7 @@ else()
136141
"$<$<CONFIG:Release>:/Oi>"
137142
"$<$<CONFIG:Release>:/Ot>"
138143
"$<$<CONFIG:Release>:/GL>"
144+
"/F4194304" #Stack size
139145
)
140146

141147
target_compile_options(

CMakeSettings.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "x64-Release",
5+
"generator": "Ninja",
6+
"configurationType": "RelWithDebInfo",
7+
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
8+
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
9+
"cmakeCommandArgs": "",
10+
"buildCommandArgs": "-v",
11+
"ctestCommandArgs": "",
12+
"inheritEnvironments": [ "msvc_x64_x64" ],
13+
"variables": []
14+
},
15+
{
16+
"name": "x64-Debug",
17+
"generator": "Ninja",
18+
"configurationType": "Debug",
19+
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
20+
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
21+
"cmakeCommandArgs": "",
22+
"buildCommandArgs": "-v",
23+
"ctestCommandArgs": "",
24+
"inheritEnvironments": [ "msvc_x64_x64" ],
25+
"variables": []
26+
}
27+
]
28+
}

src/compiledCode.hpp

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ enum class InstructionType {
3838
makeArray
3939
};
4040

41+
42+
static std::string_view instructionTypeToString(InstructionType type) {
43+
switch (type) {
44+
case InstructionType::endStatement: return "endStatement"sv;
45+
case InstructionType::push: return "push"sv;
46+
case InstructionType::callUnary: return "callUnary"sv;
47+
case InstructionType::callBinary: return "callBinary"sv;
48+
case InstructionType::callNular: return "callNular"sv;
49+
case InstructionType::assignTo: return "assignTo"sv;
50+
case InstructionType::assignToLocal: return "assignToLocal"sv;
51+
case InstructionType::getVariable: return "getVariable"sv;
52+
case InstructionType::makeArray: return "makeArray"sv;
53+
default: __debugbreak();
54+
}
55+
}
56+
57+
4158
struct ScriptInstruction {
4259
InstructionType type;
4360
size_t offset;
@@ -51,7 +68,8 @@ enum class ConstantType {
5168
code,
5269
string,
5370
scalar,
54-
boolean
71+
boolean,
72+
array
5573
};
5674

5775
struct ScriptCodePiece {
@@ -70,24 +88,43 @@ struct ScriptCodePiece {
7088
contentSplit.offset = offset;
7189
}
7290
ScriptCodePiece(std::vector<ScriptInstruction>&& c, uint64_t content) : code(c), contentString(content) {}
91+
//ScriptCodePiece(ScriptCodePiece&& o) noexcept : code(std::move(o.code)), contentString(o.contentString) {}
92+
//ScriptCodePiece(const ScriptCodePiece& o) noexcept : code(o.code), contentString(o.contentString) {}
93+
//
94+
//ScriptCodePiece& operator=(ScriptCodePiece&& o) noexcept {
95+
// code = std::move(o.code);
96+
// contentString = o.contentString;
97+
// return *this;
98+
//}
99+
//ScriptCodePiece& operator=(const ScriptCodePiece& o) noexcept {
100+
// code = o.code;
101+
// contentString = o.contentString;
102+
// return *this;
103+
//}
104+
73105
ScriptCodePiece(): contentString(0) {}
74-
75106
};
76107

108+
struct ScriptConstantArray;
109+
110+
using ScriptConstant = std::variant<ScriptCodePiece, STRINGTYPE, float, bool, ScriptConstantArray>;
111+
112+
struct ScriptConstantArray {
113+
std::vector<ScriptConstant> content;
114+
};
77115

78-
using ScriptConstant = std::variant<ScriptCodePiece, STRINGTYPE, float, bool>;
79116

80117
constexpr ConstantType getConstantType(const ScriptConstant& c) {
81118
switch (c.index()) {
82119
case 0: return ConstantType::code;
83120
case 1: return ConstantType::string;
84121
case 2: return ConstantType::scalar;
85122
case 3: return ConstantType::boolean;
123+
case 4: return ConstantType::array;
86124
}
87125
__debugbreak();
88126
}
89127

90-
91128
struct CompiledCodeData {
92129
uint32_t version{1};
93130
uint64_t codeIndex; //index to main code in constants
@@ -100,4 +137,28 @@ struct CompiledCodeData {
100137

101138

102139
//#TODO compress constants, don't have duplicates for a number or string
103-
};
140+
};
141+
142+
143+
144+
template<typename T>
145+
class Singleton {
146+
Singleton(const Singleton&) = delete;
147+
Singleton(Singleton&&) = delete;
148+
Singleton& operator=(const Singleton&) = delete;
149+
Singleton& operator=(Singleton&&) = delete;
150+
public:
151+
static __forceinline T& get() noexcept {
152+
return _singletonInstance;
153+
}
154+
static void release() {
155+
}
156+
protected:
157+
Singleton() noexcept {}
158+
static T _singletonInstance;
159+
static bool _initialized;
160+
};
161+
template<typename T>
162+
T Singleton<T>::_singletonInstance;
163+
template<typename T>
164+
bool Singleton<T>::_initialized = false;

src/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ void compileRecursive(std::filesystem::path inputDir) {
3636
if (i->path().filename().extension() == ".sqf"sv) {
3737
if (i->path().filename() == "fnc_zeusAttributes.sqf") continue; //Hard ignore for missing include file
3838
//if (i->path().filename() != "test.sqf") continue; //Hard ignore for missing include file
39-
//if (i->path().string().find("3DEN") != std::string::npos) continue; //CBA trying to format a code piece
40-
//if (i->path().filename().string().find("initDisplay3DEN") == std::string::npos) continue; //Hard ignore unit tests
39+
if (i->path().string().find("keybinding") == std::string::npos) continue; //CBA trying to format a code piece
40+
if (i->path().filename().string().find("XEH_preStart") == std::string::npos) continue; //Hard ignore unit tests
4141
tasks.emplace(i->path());
4242
}
4343
}
@@ -61,12 +61,12 @@ void processFile(ScriptCompiler& comp, std::filesystem::path path) {
6161

6262
//ScriptSerializer::compiledToBinary(compiledData, output);
6363
outputFile.flush();
64-
std::istringstream data2(data, std::istringstream::binary);
64+
//std::istringstream data2(data, std::istringstream::binary);
6565
//auto res = ScriptSerializer::binaryToCompiledCompressed(data2);
6666

6767

6868

69-
auto outputPath2 = path.parent_path() / (path.stem().string() + ".sqfa");
69+
//auto outputPath2 = path.parent_path() / (path.stem().string() + ".sqfa");
7070
//std::ofstream output2(outputPath2, std::ofstream::binary);
7171
//ScriptSerializer::compiledToHumanReadable(compiledData, output2);
7272
//output2.flush();

src/optimizer/optimizer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "optimizer.h"
2+
3+
void Optimizer::optimize(Node& node) {
4+
optimizeConstantFold(node);
5+
}

src/optimizer/optimizer.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
#include "optimizerModuleConstantFold.hpp"
3+
4+
5+
class Optimizer : public OptimizerModuleConstantFold {
6+
public:
7+
8+
void optimize(Node& node);
9+
10+
11+
};

0 commit comments

Comments
 (0)