diff --git a/compiler/arch.ts b/compiler/arch.ts index 203a1f1..851e67a 100644 --- a/compiler/arch.ts +++ b/compiler/arch.ts @@ -1,18 +1,6 @@ -import { Pos } from "./Token.ts"; - -export type Output = { - program: Program; - sourceMap: SourceMapping[]; -}; - export type Program = Ins[]; export type Ins = Ops | number; -export type SourceMapping = { - bytecodeOffset: number; - pos: Pos; -}; - // NOTICE: keep up to date with runtime/arch.hpp export type Ops = typeof Ops; @@ -41,4 +29,5 @@ export const Ops = { Or: 21, Xor: 22, Not: 23, + SourceMap: 24, } as const; diff --git a/runtime/arch.hpp b/runtime/arch.hpp index 317ed5b..5443a62 100644 --- a/runtime/arch.hpp +++ b/runtime/arch.hpp @@ -31,6 +31,7 @@ enum class Op : uint32_t { Or = 21, Xor = 22, Not = 23, + SourceMap = 24, }; } diff --git a/runtime/vm.cpp b/runtime/vm.cpp index 9dca2ad..e67d579 100644 --- a/runtime/vm.cpp +++ b/runtime/vm.cpp @@ -195,11 +195,20 @@ void VM::run() stack_push(Bool { .value = value }); break; } - case Op::Not: + case Op::Not: { assert_stack_has(1); auto value = !stack_pop().as_bool().value; stack_push(Bool { .value = value }); break; + } + case Op::SourceMap: { + assert_program_has(3); + auto index = eat_int32(); + auto line = eat_int32(); + auto col = eat_int32(); + this->current_pos = { index, line, col }; + break; + } } } } diff --git a/runtime/vm.hpp b/runtime/vm.hpp index 20758fb..87314ad 100644 --- a/runtime/vm.hpp +++ b/runtime/vm.hpp @@ -10,10 +10,40 @@ namespace sliger { +struct SourcePos { + int index; + int line; + int col; +}; + +struct FGNode { + uint32_t fn; + int64_t acc; + int64_t ic_start; + size_t parent; + std::vector children; +}; + +class FlameGraphBuilder { +public: + inline auto report_call(size_t fn, int64_t ic_start) { } + + inline auto report_return(int64_t ic_end) { } + +private: + std::vector nodes; +}; + +struct VMOpts { + bool flameGraph; + bool codeCoverage; +}; + class VM { public: - VM(const std::vector& program) - : program(program.data()) + VM(const std::vector& program, VMOpts opts) + : opts(opts) + , program(program.data()) , program_size(program.size()) { } @@ -92,11 +122,14 @@ public: } private: + VMOpts opts; uint32_t pc = 0; uint32_t bp = 0; const uint32_t* program; size_t program_size; std::vector stack; std::vector pool_heap; + SourcePos current_pos = { 0, 1, 1 }; }; + }