diff --git a/runtime/json.hpp b/runtime/json.hpp index 8789356..38b73f2 100644 --- a/runtime/json.hpp +++ b/runtime/json.hpp @@ -295,8 +295,8 @@ public: auto parse_val() -> Res>; private: - inline auto unexpected_tok_err( - TokTyp expected, std::string_view msg) -> Res> + inline auto unexpected_tok_err(TokTyp expected, std::string_view msg) + -> Res> { return Err { .pos = this->cur.val().pos, diff --git a/runtime/main.cpp b/runtime/main.cpp index 98843d2..51c3ac3 100644 --- a/runtime/main.cpp +++ b/runtime/main.cpp @@ -2,12 +2,91 @@ #include "vm.hpp" #include #include +#include #include #include +enum class AsmLineType { + Op, + Lit, + Loc, + Ref, +}; + +struct Loc { + explicit Loc(std::string value) + : value(value) + { + } + + std::string value; +}; + +struct Ref { + explicit Ref(std::string value) + : value(value) + { + } + + std::string value; +}; + +struct AsmLine { + /* clang-format off */ + AsmLine(sliger::Op value) : type(AsmLineType::Op), value(value) {} + AsmLine(uint32_t value) : type(AsmLineType::Lit), value(value) {} + AsmLine(Loc value) : type(AsmLineType::Loc), value(value) {} + AsmLine(Ref value) : type(AsmLineType::Ref), value(value) {} + /* clang-format on */ + + AsmLineType type; + std::variant value; +}; + +auto compile_asm(const std::vector& lines) -> std::vector +{ + size_t ip = 0; + auto output = std::vector(); + auto locs = std::unordered_map(); + auto refs = std::unordered_map(); + for (const auto& line : lines) { + switch (line.type) { + case AsmLineType::Op: { + output.push_back( + std::to_underlying(std::get(line.value))); + ip += 1; + break; + } + case AsmLineType::Lit: { + output.push_back(std::get(line.value)); + ip += 1; + break; + } + case AsmLineType::Loc: { + locs.insert_or_assign(std::get(line.value).value, ip + 1); + break; + } + case AsmLineType::Ref: { + output.push_back(0); + refs.insert_or_assign(ip, std::get(line.value).value); + ip += 1; + break; + } + } + } + for (size_t i = 0; i < output.size(); ++i) { + if (refs.contains(i)) { + output.at(i) = static_cast(locs.at(refs.at(i))); + } + } + return output; +} + int main() { - using sliger::Op; + using R = Ref; + using L = Loc; + using enum sliger::Op; // fn add(a, b) { // + a b @@ -21,101 +100,56 @@ int main() // } // result = add(result, 5); // } - auto program = std::vector { - std::to_underlying(Op::SourceMap), - 0, - 0, - 0, - std::to_underlying(Op::PushPtr), - 15, // main - std::to_underlying(Op::Call), - 0, - std::to_underlying(Op::Pop), - std::to_underlying(Op::PushPtr), - 87, // .l3 - std::to_underlying(Op::Jump), - std::to_underlying(Op::Pop), - /*add*/ std::to_underlying(Op::SourceMap), - 19, - 2, - 5, - std::to_underlying(Op::Add), - std::to_underlying(Op::Return), - /*main*/ std::to_underlying(Op::SourceMap), - 28, - 5, - 1, - std::to_underlying(Op::PushInt), - 0, - std::to_underlying(Op::SourceMap), - 44, - 6, - 1, - std::to_underlying(Op::PushInt), - 0, - std::to_underlying(Op::SourceMap), - 55, - 7, - 1, - /*.l0*/ std::to_underlying(Op::SourceMap), - 66, - 8, - 5, - std::to_underlying(Op::LoadLocal), - 1, - std::to_underlying(Op::PushInt), - 0, - std::to_underlying(Op::LessThan), - std::to_underlying(Op::Not), - std::to_underlying(Op::PushPtr), - 53, // .l1 - std::to_underlying(Op::JumpIfFalse), - std::to_underlying(Op::SourceMap), - 87, - 9, - 9, - std::to_underlying(Op::PushPtr), - 83, // .l2 - std::to_underlying(Op::Jump), - /*.l1*/ std::to_underlying(Op::SourceMap), - 104, - 11, - 5, - std::to_underlying(Op::LoadLocal), - 0, - std::to_underlying(Op::PushInt), - 5, - std::to_underlying(Op::PushPtr), - 9, // add - std::to_underlying(Op::Call), - 2, - std::to_underlying(Op::StoreLocal), - 0, - std::to_underlying(Op::SourceMap), - 133, - 12, - 5, - std::to_underlying(Op::LoadLocal), - 1, - std::to_underlying(Op::PushInt), - 1, - std::to_underlying(Op::Add), - std::to_underlying(Op::StoreLocal), - 1, - std::to_underlying(Op::PushPtr), - 33, // .l0 - std::to_underlying(Op::Jump), - /*.l2*/ std::to_underlying(Op::Pop), - std::to_underlying(Op::Pop), - std::to_underlying(Op::PushNull), - std::to_underlying(Op::Return), - /*.l3*/ std::to_underlying(Op::SourceMap), - 147, - 15, - 1, - std::to_underlying(Op::PushInt), + auto program_asm = std::vector { + // clang-format off + SourceMap, 0, 0, 0, + PushPtr, R("main"), + Pop, + PushPtr, R("_exit"), + Jump, + Pop, + L("add"), + SourceMap, 19, 2, 5, + Add, + Return, + SourceMap, 28, 5, 1, + PushInt, 0, + SourceMap, 44, 6, 1, + PushInt, 0, + SourceMap, 55, 7, 1, + L("1"), + SourceMap, 66, 8, 5, + LoadLocal, 1, + PushInt, 0, + LessThan, + Not, + PushPtr, R("1"), + JumpIfFalse, + SourceMap, 87, 9, 9, + PushPtr, R("2"), + Jump, + SourceMap, 104, 11, 5, + LoadLocal, 0, + PushInt, 5, + PushPtr, R("add"), + Call, 2, + StoreLocal, 0, + SourceMap, 133, 12, 5, + LoadLocal, 1, + PushInt, 1, + Add, + StoreLocal, 1, + PushPtr, R("0"), + Jump, + L("2"), + Pop, + PushNull, + Return, + L("_exit"), + SourceMap, 147, 15, 1 + // clang-format on }; - + auto program = compile_asm(program_asm); auto vm = sliger::VM(program, { .flame_graph = true, diff --git a/runtime/to_json_2.cpp b/runtime/to_json_2.cpp new file mode 100644 index 0000000..a8df02c --- /dev/null +++ b/runtime/to_json_2.cpp @@ -0,0 +1,14 @@ +#include "json.hpp" +#include "vm.hpp" + +using namespace sliger; + +void FlameGraphBuilder::to_json(json::Writer& writer) const +{ + writer.add("null"); +} + +void CodeCoverageBuilder::to_json(json::Writer& writer) const +{ + writer.add("null"); +}