Replace mwscript program serialization into a vector with simple struct

Mostly to avoid string literal lookup by index with iteration over all preciding
literals and calling strlen. This is very inefficient. In genral this makes code
much more straightforward but also makes it portable since now int and float of
different sizes are properly supported.
This commit is contained in:
elsid 2023-01-10 04:10:18 +01:00
parent 60eede6a1d
commit b88f0d2dbd
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625
21 changed files with 93 additions and 162 deletions

View file

@ -1,56 +1,10 @@
#include "literals.hpp"
#include <algorithm>
#include <cstring>
namespace Compiler
{
int Literals::getIntegerSize() const
{
return static_cast<int>(mIntegers.size() * sizeof(Interpreter::Type_Integer));
}
int Literals::getFloatSize() const
{
return static_cast<int>(mFloats.size() * sizeof(Interpreter::Type_Float));
}
int Literals::getStringSize() const
{
int size = 0;
for (std::vector<std::string>::const_iterator iter(mStrings.begin()); iter != mStrings.end(); ++iter)
size += static_cast<int>(iter->size()) + 1;
if (size % 4) // padding
size += 4 - size % 4;
return size;
}
void Literals::append(std::vector<Interpreter::Type_Code>& code) const
{
for (const int& mInteger : mIntegers)
code.push_back(*reinterpret_cast<const Interpreter::Type_Code*>(&mInteger));
for (const float& mFloat : mFloats)
code.push_back(*reinterpret_cast<const Interpreter::Type_Code*>(&mFloat));
int stringBlockSize = getStringSize();
int size = static_cast<int>(code.size());
code.resize(size + stringBlockSize / 4);
size_t offset = 0;
for (const auto& mString : mStrings)
{
size_t stringSize = mString.size() + 1;
std::copy(mString.c_str(), mString.c_str() + stringSize, reinterpret_cast<char*>(&code[size]) + offset);
offset += stringSize;
}
}
int Literals::addInteger(Interpreter::Type_Integer value)
{
int index = static_cast<int>(mIntegers.size());