Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Add defines to cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mitrokosta committed Jan 19, 2022
1 parent 65646ec commit 290ca25
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
79 changes: 71 additions & 8 deletions src/apps/engine/src/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,10 @@ bool COMPILER::Compile(SEGMENT_DESC &Segment, char *pInternalCode, uint32_t pInt
SetError("define redefinition: %s", di.name);
return false;
}
if (use_script_cache_)
{
script_cache_.defines.emplace_back(di.name, di.deftype, di.data4b);
}
}
break;

Expand Down Expand Up @@ -7471,7 +7475,7 @@ void COMPILER::SaveVariablesToCache(storm::script_cache::Writer &writer)
}
}

void COMPILER::WriteFunctionsToCache(storm::script_cache::Writer &writer)
void COMPILER::SaveFunctionsToCache(storm::script_cache::Writer &writer)
{
writer.WriteData(script_cache_.functions.size());
for (auto &[fi, arguments, local_variables] : script_cache_.functions)
Expand Down Expand Up @@ -7501,7 +7505,7 @@ void COMPILER::WriteFunctionsToCache(storm::script_cache::Writer &writer)
}
}

void COMPILER::WriteScriptLibrariesToCache(storm::script_cache::Writer &writer)
void COMPILER::SaveScriptLibrariesToCache(storm::script_cache::Writer &writer)
{
writer.WriteData(script_cache_.script_libs.size());
for (auto &lib_name : script_cache_.script_libs)
Expand All @@ -7510,7 +7514,7 @@ void COMPILER::WriteScriptLibrariesToCache(storm::script_cache::Writer &writer)
}
}

void COMPILER::WriteEventHandlersToCache(storm::script_cache::Writer &writer)
void COMPILER::SaveEventHandlersToCache(storm::script_cache::Writer &writer)
{
writer.WriteData(script_cache_.event_handlers.size());
for (auto &[event, handler] : script_cache_.event_handlers)
Expand All @@ -7520,7 +7524,7 @@ void COMPILER::WriteEventHandlersToCache(storm::script_cache::Writer &writer)
}
}

void COMPILER::WriteByteCodeToCache(storm::script_cache::Writer &writer, const SEGMENT_DESC &segment)
void COMPILER::SaveByteCodeToCache(storm::script_cache::Writer &writer, const SEGMENT_DESC &segment)
{
auto code = std::string_view(segment.pCode, segment.pCode + segment.BCode_Program_size);
writer.WriteBytes(code);
Expand Down Expand Up @@ -7590,25 +7594,52 @@ void COMPILER::SaveSegmentToCache(const SEGMENT_DESC &segment)

auto writer = storm::script_cache::Writer();

// defines (in case of new compiled code depending on defines from cache)
SaveDefinesToCache(writer);

// variables with values
SaveVariablesToCache(writer);

// functions
WriteFunctionsToCache(writer);
SaveFunctionsToCache(writer);

// script libs
WriteScriptLibrariesToCache(writer);
SaveScriptLibrariesToCache(writer);

// event handlers
WriteEventHandlersToCache(writer);
SaveEventHandlersToCache(writer);

// bytecode
WriteByteCodeToCache(writer, segment);
SaveByteCodeToCache(writer, segment);

auto &data = writer.GetData();
stream.write(data.data(), data.size());
}

void COMPILER::SaveDefinesToCache(storm::script_cache::Writer &writer)
{
writer.WriteData(script_cache_.defines.size());
for (auto &[name, type, value] : script_cache_.defines)
{
writer.WriteBytes(name);
writer.WriteData(type);
switch (type)
{
case VAR_INTEGER:
writer.WriteData(static_cast<int32_t>(value));
break;

case VAR_FLOAT:
writer.WriteData(static_cast<float>(value));
break;

case VAR_STRING:
writer.WriteBytes(reinterpret_cast<const char*>(value));
break;
}
}
}

bool COMPILER::LoadSegmentFromCache(SEGMENT_DESC &segment)
{
const auto path = GetSegmentCachePath(segment);
Expand All @@ -7629,6 +7660,9 @@ bool COMPILER::LoadSegmentFromCache(SEGMENT_DESC &segment)
stream.read(data.data(), cache_size);
auto reader = storm::script_cache::Reader(data);

// defines (in case of new compiled code depending on defines from cache)
LoadDefinesFromCache(reader, segment);

// variables with values
LoadVariablesFromCache(reader, segment);

Expand All @@ -7647,6 +7681,35 @@ bool COMPILER::LoadSegmentFromCache(SEGMENT_DESC &segment)
return true;
}

void COMPILER::LoadDefinesFromCache(storm::script_cache::Reader &reader, SEGMENT_DESC &segment)
{
const auto size = reader.ReadData<size_t>();
for (size_t i = 0; i < *size; ++i)
{
auto di = DEFINFO();

di.segment_id = segment.id;
di.name = const_cast<char *>(reader.ReadBytes().data());
di.deftype = *reader.ReadData<uint32_t>();
switch (di.deftype)
{
case VAR_INTEGER:
di.data4b = static_cast<uintptr_t>(*reader.ReadData<int32_t>());
break;

case VAR_FLOAT:
di.data4b = static_cast<uintptr_t>(*reader.ReadData<float>());
break;

case VAR_STRING:
di.data4b = reinterpret_cast<uintptr_t>(const_cast<char *>(reader.ReadBytes().data()));
break;
}

DefTab.AddDef(di);
}
}

void COMPILER::FormatAllDialog(const char *directory_name)
{
const auto vPaths = fio->_GetPathsOrFilenamesByMask(directory_name, "*.c", true);
Expand Down
10 changes: 6 additions & 4 deletions src/apps/engine/src/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,20 @@ class COMPILER : public VIRTUAL_COMPILER
[[nodiscard]] std::filesystem::path GetSegmentCachePath(const SEGMENT_DESC &segment) const;

bool LoadSegmentFromCache(SEGMENT_DESC &segment);
void LoadDefinesFromCache(storm::script_cache::Reader &reader, SEGMENT_DESC &segment);
void LoadVariablesFromCache(storm::script_cache::Reader &reader, SEGMENT_DESC &segment);
void LoadFunctionsFromCache(storm::script_cache::Reader &reader, SEGMENT_DESC &segment);
void LoadScriptLibrariesFromCache(storm::script_cache::Reader &reader);
void LoadEventHandlersFromCache(storm::script_cache::Reader &reader);
void LoadByteCodeFromCache(storm::script_cache::Reader &reader, SEGMENT_DESC &segment);

void SaveSegmentToCache(const SEGMENT_DESC &segment);
void SaveDefinesToCache(storm::script_cache::Writer &writer);
void SaveVariablesToCache(storm::script_cache::Writer &writer);
void WriteFunctionsToCache(storm::script_cache::Writer &writer);
void WriteScriptLibrariesToCache(storm::script_cache::Writer &writer);
void WriteEventHandlersToCache(storm::script_cache::Writer &writer);
void WriteByteCodeToCache(storm::script_cache::Writer &writer, const SEGMENT_DESC &segment);
void SaveFunctionsToCache(storm::script_cache::Writer &writer);
void SaveScriptLibrariesToCache(storm::script_cache::Writer &writer);
void SaveEventHandlersToCache(storm::script_cache::Writer &writer);
void SaveByteCodeToCache(storm::script_cache::Writer &writer, const SEGMENT_DESC &segment);

COMPILER_STAGE CompilerStage;
STRINGS_LIST LabelTable;
Expand Down
8 changes: 8 additions & 0 deletions src/apps/engine/src/script_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ struct EventHandler
std::string function_name;
};

struct Define
{
std::string name;
uint32_t type;
uintptr_t value;
};

class Reader final
{
public:
Expand Down Expand Up @@ -149,6 +156,7 @@ inline void WriteScriptData(Writer &writer, S_TOKEN_TYPE type, DATA *data)

struct ScriptCache
{
std::vector<script_cache::Define> defines;
std::vector<std::string> script_libs;
std::vector<std::string> files;
std::vector<script_cache::Function> functions;
Expand Down

0 comments on commit 290ca25

Please sign in to comment.