From 3d89031748aac8b9ed958112de036cef7ff16a2b Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Wed, 20 Nov 2024 15:07:39 +0100 Subject: [PATCH] test program stack viz --- runtime/main.cpp | 4 +++- runtime/vm.cpp | 2 +- runtime/vm.hpp | 10 +++++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/runtime/main.cpp b/runtime/main.cpp index 1c1ee44..5f2d29d 100644 --- a/runtime/main.cpp +++ b/runtime/main.cpp @@ -171,5 +171,7 @@ int main() .code_coverage = true, }); vm.run_until_done(); - std::cout << std::format("done\n{}\n", vm.stack_repr_string()); + std::cout << std::format("done\n{}\n", vm.stack_repr_string(4)); + auto flame_graph = vm.flame_graph_json(); + std::cout << std::format("flame graph: {}\n", flame_graph); } diff --git a/runtime/vm.cpp b/runtime/vm.cpp index 476339f..da33055 100644 --- a/runtime/vm.cpp +++ b/runtime/vm.cpp @@ -86,7 +86,7 @@ void VM::run_n_instructions(size_t amount) void VM::run_instruction() { std::cout << std::format(" {:>4}: {:<12}{}\n", this->pc, - maybe_op_to_string(this->program[this->pc]), stack_repr_string()); + maybe_op_to_string(this->program[this->pc]), stack_repr_string(4)); auto op = eat_op(); switch (op) { case Op::Nop: diff --git a/runtime/vm.hpp b/runtime/vm.hpp index a225cec..d6d3b82 100644 --- a/runtime/vm.hpp +++ b/runtime/vm.hpp @@ -156,16 +156,20 @@ public: return this->stack; } - inline auto stack_repr_string() const -> std::string + inline auto stack_repr_string(size_t max_items) const -> std::string { auto result = std::string(); result += "→"; const auto& stack = view_stack(); - for (size_t i = 0; i < stack.size(); ++i) { + for (size_t i = 0; i < stack.size() and i < max_items; ++i) { if (i != 0) { result += " "; } - result += stack[stack.size() - i - 1].to_repr_string(); + result += std::format( + "{:<11}", stack[stack.size() - i - 1].to_repr_string()); + } + if (stack.size() >= max_items) { + result += std::format(" ... + {}", stack.size() - max_items + 1); } return result; }