Skip to content

Commit

Permalink
Add Optimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
dedmen committed Apr 8, 2019
1 parent e35fd3f commit 4cb0f46
Show file tree
Hide file tree
Showing 13 changed files with 943 additions and 34 deletions.
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ endif()

file(GLOB_RECURSE SOURCES_ASC "src/*.hpp" "src/*.cpp" "${LIBRARY_PATH_B64}/base64.cpp")
SOURCE_GROUP("src" FILES ${SOURCES_ASC})

file(GLOB_RECURSE SOURCES_ASC_OPT "src/optimizer/*.hpp" "src/optimizer/*.cpp")
SOURCE_GROUP("src" FILES ${SOURCES_ASC_OPT})

#zstd
SET(ZSTD_BUILD_PROGRAMS OFF CACHE BOOL "no programs" FORCE)
set(ZSTD_USE_STATIC_RUNTIME ON CACHE BOOL "yes" FORCE)
Expand Down Expand Up @@ -87,7 +91,7 @@ add_definitions(/DLOADFILE_CACHE) #SQF VM fileio.cpp cache



add_executable("ArmaScriptCompiler" ${SOURCES_ASC} ${SOURCES_SQFVM})
add_executable("ArmaScriptCompiler" ${SOURCES_ASC} ${SOURCES_SQFVM} ${SOURCES_ASC_OPT})

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

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

Expand All @@ -136,6 +141,7 @@ else()
"$<$<CONFIG:Release>:/Oi>"
"$<$<CONFIG:Release>:/Ot>"
"$<$<CONFIG:Release>:/GL>"
"/F4194304" #Stack size
)

target_compile_options(
Expand Down
28 changes: 28 additions & 0 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"configurations": [
{
"name": "x64-Release",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x64_x64" ],
"variables": []
},
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x64_x64" ],
"variables": []
}
]
}
71 changes: 66 additions & 5 deletions src/compiledCode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ enum class InstructionType {
makeArray
};


static std::string_view instructionTypeToString(InstructionType type) {
switch (type) {
case InstructionType::endStatement: return "endStatement"sv;
case InstructionType::push: return "push"sv;
case InstructionType::callUnary: return "callUnary"sv;
case InstructionType::callBinary: return "callBinary"sv;
case InstructionType::callNular: return "callNular"sv;
case InstructionType::assignTo: return "assignTo"sv;
case InstructionType::assignToLocal: return "assignToLocal"sv;
case InstructionType::getVariable: return "getVariable"sv;
case InstructionType::makeArray: return "makeArray"sv;
default: __debugbreak();
}
}


struct ScriptInstruction {
InstructionType type;
size_t offset;
Expand All @@ -51,7 +68,8 @@ enum class ConstantType {
code,
string,
scalar,
boolean
boolean,
array
};

struct ScriptCodePiece {
Expand All @@ -70,24 +88,43 @@ struct ScriptCodePiece {
contentSplit.offset = offset;
}
ScriptCodePiece(std::vector<ScriptInstruction>&& c, uint64_t content) : code(c), contentString(content) {}
//ScriptCodePiece(ScriptCodePiece&& o) noexcept : code(std::move(o.code)), contentString(o.contentString) {}
//ScriptCodePiece(const ScriptCodePiece& o) noexcept : code(o.code), contentString(o.contentString) {}
//
//ScriptCodePiece& operator=(ScriptCodePiece&& o) noexcept {
// code = std::move(o.code);
// contentString = o.contentString;
// return *this;
//}
//ScriptCodePiece& operator=(const ScriptCodePiece& o) noexcept {
// code = o.code;
// contentString = o.contentString;
// return *this;
//}

ScriptCodePiece(): contentString(0) {}

};

struct ScriptConstantArray;

using ScriptConstant = std::variant<ScriptCodePiece, STRINGTYPE, float, bool, ScriptConstantArray>;

struct ScriptConstantArray {
std::vector<ScriptConstant> content;
};

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

constexpr ConstantType getConstantType(const ScriptConstant& c) {
switch (c.index()) {
case 0: return ConstantType::code;
case 1: return ConstantType::string;
case 2: return ConstantType::scalar;
case 3: return ConstantType::boolean;
case 4: return ConstantType::array;
}
__debugbreak();
}


struct CompiledCodeData {
uint32_t version{1};
uint64_t codeIndex; //index to main code in constants
Expand All @@ -100,4 +137,28 @@ struct CompiledCodeData {


//#TODO compress constants, don't have duplicates for a number or string
};
};



template<typename T>
class Singleton {
Singleton(const Singleton&) = delete;
Singleton(Singleton&&) = delete;
Singleton& operator=(const Singleton&) = delete;
Singleton& operator=(Singleton&&) = delete;
public:
static __forceinline T& get() noexcept {
return _singletonInstance;
}
static void release() {
}
protected:
Singleton() noexcept {}
static T _singletonInstance;
static bool _initialized;
};
template<typename T>
T Singleton<T>::_singletonInstance;
template<typename T>
bool Singleton<T>::_initialized = false;
8 changes: 4 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ void compileRecursive(std::filesystem::path inputDir) {
if (i->path().filename().extension() == ".sqf"sv) {
if (i->path().filename() == "fnc_zeusAttributes.sqf") continue; //Hard ignore for missing include file
//if (i->path().filename() != "test.sqf") continue; //Hard ignore for missing include file
//if (i->path().string().find("3DEN") != std::string::npos) continue; //CBA trying to format a code piece
//if (i->path().filename().string().find("initDisplay3DEN") == std::string::npos) continue; //Hard ignore unit tests
if (i->path().string().find("keybinding") == std::string::npos) continue; //CBA trying to format a code piece
if (i->path().filename().string().find("XEH_preStart") == std::string::npos) continue; //Hard ignore unit tests
tasks.emplace(i->path());
}
}
Expand All @@ -61,12 +61,12 @@ void processFile(ScriptCompiler& comp, std::filesystem::path path) {

//ScriptSerializer::compiledToBinary(compiledData, output);
outputFile.flush();
std::istringstream data2(data, std::istringstream::binary);
//std::istringstream data2(data, std::istringstream::binary);
//auto res = ScriptSerializer::binaryToCompiledCompressed(data2);



auto outputPath2 = path.parent_path() / (path.stem().string() + ".sqfa");
//auto outputPath2 = path.parent_path() / (path.stem().string() + ".sqfa");
//std::ofstream output2(outputPath2, std::ofstream::binary);
//ScriptSerializer::compiledToHumanReadable(compiledData, output2);
//output2.flush();
Expand Down
5 changes: 5 additions & 0 deletions src/optimizer/optimizer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "optimizer.h"

void Optimizer::optimize(Node& node) {
optimizeConstantFold(node);
}
11 changes: 11 additions & 0 deletions src/optimizer/optimizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include "optimizerModuleConstantFold.hpp"


class Optimizer : public OptimizerModuleConstantFold {
public:

void optimize(Node& node);


};
Loading

0 comments on commit 4cb0f46

Please sign in to comment.