1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#include "interpreter.hpp"
#include <cassert>
#include <format>
#include <stdexcept>
#include <string>
#include "opcodes.hpp"
#include "program.hpp"
namespace Interpreter
{
namespace
{
[[noreturn]] void abortUnknownCode(int segment, int opcode)
{
const std::string error = std::format("unknown opcode {} in segment {}", opcode, segment);
throw std::runtime_error(error);
}
[[noreturn]] void abortUnknownSegment(Type_Code code)
{
const std::string error = std::format("opcode outside of the allocated segment range: {}", code);
throw std::runtime_error(error);
}
template <typename T>
auto& getDispatcher(const T& segment, unsigned int seg, int opcode)
{
auto it = segment.find(opcode);
if (it == segment.end())
{
abortUnknownCode(seg, opcode);
}
return it->second;
}
}
[[noreturn]] void Interpreter::abortDuplicateInstruction(std::string_view name, int code)
{
throw std::invalid_argument(
std::format("Duplicated interpreter instruction code in segment {}: {:#x}", name, code));
}
void Interpreter::execute(Type_Code code)
{
unsigned int segSpec = code >> 30;
switch (segSpec)
{
case 0:
{
const int opcode = code >> 24;
const unsigned int arg0 = code & 0xffffff;
return getDispatcher(mSegment0, 0, opcode)->execute(mRuntime, arg0);
}
case 2:
{
const int opcode = (code >> 20) & 0x3ff;
const unsigned int arg0 = code & 0xfffff;
return getDispatcher(mSegment2, 2, opcode)->execute(mRuntime, arg0);
}
}
segSpec = code >> 26;
switch (segSpec)
{
case 0x30:
{
const int opcode = (code >> 8) & 0x3ffff;
const unsigned int arg0 = code & 0xff;
return getDispatcher(mSegment3, 3, opcode)->execute(mRuntime, arg0);
}
case 0x32:
{
const int opcode = code & 0x3ffffff;
return getDispatcher(mSegment5, 5, opcode)->execute(mRuntime);
}
}
abortUnknownSegment(code);
}
void Interpreter::begin()
{
if (mRunning)
{
mCallstack.push(mRuntime);
mRuntime.clear();
}
else
{
mRunning = true;
}
}
void Interpreter::end()
{
if (mCallstack.empty())
{
mRuntime.clear();
mRunning = false;
}
else
{
mRuntime = mCallstack.top();
mCallstack.pop();
}
}
void Interpreter::run(const Program& program, Context& context)
{
begin();
try
{
mRuntime.configure(program, context);
while (mRuntime.getPC() >= 0 && static_cast<std::size_t>(mRuntime.getPC()) < program.mInstructions.size())
{
const Type_Code instruction = program.mInstructions[mRuntime.getPC()];
mRuntime.setPC(mRuntime.getPC() + 1);
execute(instruction);
}
}
catch (...)
{
end();
throw;
}
end();
}
}
|