|
|
@ -1,4 +1,5 @@
|
|
|
|
#include "runtime.hpp"
|
|
|
|
#include "runtime.hpp"
|
|
|
|
|
|
|
|
#include "program.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstring>
|
|
|
@ -6,81 +7,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
namespace Interpreter
|
|
|
|
namespace Interpreter
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Runtime::Runtime()
|
|
|
|
|
|
|
|
: mContext(nullptr)
|
|
|
|
|
|
|
|
, mCode(nullptr)
|
|
|
|
|
|
|
|
, mCodeSize(0)
|
|
|
|
|
|
|
|
, mPC(0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int Runtime::getPC() const
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return mPC;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int Runtime::getIntegerLiteral(int index) const
|
|
|
|
int Runtime::getIntegerLiteral(int index) const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= static_cast<int>(mCode[1]))
|
|
|
|
if (index < 0 || mProgram->mIntegers.size() <= static_cast<std::size_t>(index))
|
|
|
|
throw std::out_of_range("out of range");
|
|
|
|
throw std::out_of_range("Invalid integer index");
|
|
|
|
|
|
|
|
|
|
|
|
const Type_Code* literalBlock = mCode + 4 + mCode[0];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return *reinterpret_cast<const int*>(&literalBlock[index]);
|
|
|
|
return mProgram->mIntegers[static_cast<std::size_t>(index)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float Runtime::getFloatLiteral(int index) const
|
|
|
|
float Runtime::getFloatLiteral(int index) const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= static_cast<int>(mCode[2]))
|
|
|
|
if (index < 0 || mProgram->mFloats.size() <= static_cast<std::size_t>(index))
|
|
|
|
throw std::out_of_range("out of range");
|
|
|
|
throw std::out_of_range("Invalid float index");
|
|
|
|
|
|
|
|
|
|
|
|
const Type_Code* literalBlock = mCode + 4 + mCode[0] + mCode[1];
|
|
|
|
return mProgram->mFloats[static_cast<std::size_t>(index)];
|
|
|
|
|
|
|
|
|
|
|
|
return *reinterpret_cast<const float*>(&literalBlock[index]);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string_view Runtime::getStringLiteral(int index) const
|
|
|
|
std::string_view Runtime::getStringLiteral(int index) const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (index < 0 || static_cast<int>(mCode[3]) <= 0)
|
|
|
|
if (index < 0 || mProgram->mStrings.size() <= static_cast<std::size_t>(index))
|
|
|
|
throw std::out_of_range("out of range");
|
|
|
|
throw std::out_of_range("Invalid string literal index");
|
|
|
|
|
|
|
|
|
|
|
|
const char* literalBlock = reinterpret_cast<const char*>(mCode + 4 + mCode[0] + mCode[1] + mCode[2]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (; index; --index)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
offset += std::strlen(literalBlock + offset) + 1;
|
|
|
|
|
|
|
|
if (offset / 4 >= mCode[3])
|
|
|
|
|
|
|
|
throw std::out_of_range("out of range");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return literalBlock + offset;
|
|
|
|
return mProgram->mStrings[static_cast<std::size_t>(index)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Runtime::configure(const Type_Code* code, int codeSize, Context& context)
|
|
|
|
void Runtime::configure(const Program& program, Context& context)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
clear();
|
|
|
|
|
|
|
|
|
|
|
|
mContext = &context;
|
|
|
|
mContext = &context;
|
|
|
|
mCode = code;
|
|
|
|
mProgram = &program;
|
|
|
|
mCodeSize = codeSize;
|
|
|
|
|
|
|
|
mPC = 0;
|
|
|
|
mPC = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Runtime::clear()
|
|
|
|
void Runtime::clear()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mContext = nullptr;
|
|
|
|
mContext = nullptr;
|
|
|
|
mCode = nullptr;
|
|
|
|
mProgram = nullptr;
|
|
|
|
mCodeSize = 0;
|
|
|
|
|
|
|
|
mStack.clear();
|
|
|
|
mStack.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Runtime::setPC(int PC)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mPC = PC;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Runtime::push(const Data& data)
|
|
|
|
void Runtime::push(const Data& data)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mStack.push_back(data);
|
|
|
|
mStack.push_back(data);
|
|
|
@ -108,12 +74,12 @@ namespace Interpreter
|
|
|
|
mStack.pop_back();
|
|
|
|
mStack.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Data& Runtime::operator[](int Index)
|
|
|
|
Data& Runtime::operator[](int index)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (Index < 0 || Index >= static_cast<int>(mStack.size()))
|
|
|
|
if (index < 0 || index >= static_cast<int>(mStack.size()))
|
|
|
|
throw std::runtime_error("stack index out of range");
|
|
|
|
throw std::runtime_error("stack index out of range");
|
|
|
|
|
|
|
|
|
|
|
|
return mStack[mStack.size() - Index - 1];
|
|
|
|
return mStack[mStack.size() - index - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Context& Runtime::getContext()
|
|
|
|
Context& Runtime::getContext()
|
|
|
|