From 206bdec828b07ab7b3b93869d5a8aaaf0d4b2945 Mon Sep 17 00:00:00 2001 From: "Mr.UNIX" Date: Sat, 9 Mar 2024 09:40:55 +0100 Subject: [PATCH] refactor: inline CallStack::Jump() --- src/bytecode/vm/CallStack.cpp | 2 +- src/bytecode/vm/CallStack.h | 15 +++++---------- src/bytecode/vm/Interpreter.cpp | 4 ++-- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/bytecode/vm/CallStack.cpp b/src/bytecode/vm/CallStack.cpp index 2945861..cc76b15 100644 --- a/src/bytecode/vm/CallStack.cpp +++ b/src/bytecode/vm/CallStack.cpp @@ -1,7 +1,7 @@ #include "CallStack.h" namespace Bytecode { - void CallStack::setLocal(size_t reg, const StackObject sObject) { + void CallStack::setLocal(const size_t reg, const StackObject sObject) const { local_registers[local_registers_used - stackTop->args + reg] = sObject; } diff --git a/src/bytecode/vm/CallStack.h b/src/bytecode/vm/CallStack.h index b362d71..4a30a5c 100644 --- a/src/bytecode/vm/CallStack.h +++ b/src/bytecode/vm/CallStack.h @@ -4,10 +4,9 @@ #include "bytecode/objects/StackObject.h" #include #include -#include namespace Bytecode { - struct alignas(4) StackFrame { + struct StackFrame { uint32_t segment; uint32_t current_line; uint32_t args; @@ -42,11 +41,11 @@ namespace Bytecode { free(stack); } - void newStackFrame(uint32_t segment) { + void newStackFrame(const uint32_t segment) { newStackFrame(segment, 0, nullptr); } - void newStackFrame(uint32_t segment, uint32_t args, Stack *program_stack) { + void newStackFrame(const uint32_t segment, const uint32_t args, Stack *program_stack) { if (stackframe_used + 1 > stackframe_capacity) { stackframe_capacity *= 2; stack = (StackFrame *) realloc(stack, stackframe_capacity); @@ -79,14 +78,10 @@ namespace Bytecode { stackTop = stack + stackframe_used - 1; } - void jump(size_t line) { - stack[stackframe_used - 1].current_line = line - 1; - } - - StackObject getLocal(size_t reg) { + [[nodiscard]] StackObject getLocal(const size_t reg) const { return local_registers[local_registers_used - stackTop->args + reg]; } - void setLocal(size_t reg, StackObject sObject); + void setLocal(size_t reg, StackObject sObject) const; }; }// namespace Bytecode \ No newline at end of file diff --git a/src/bytecode/vm/Interpreter.cpp b/src/bytecode/vm/Interpreter.cpp index 7b00948..25706da 100644 --- a/src/bytecode/vm/Interpreter.cpp +++ b/src/bytecode/vm/Interpreter.cpp @@ -72,10 +72,10 @@ namespace Bytecode { case InstructionType::CondJumpIfNot: if (const auto cond = vm.program_stack.pop(); !cond.asBoolean()) - vm.call_stack.jump(currentInstruction->param); + vm.call_stack.stackTop->current_line = currentInstruction->param - 1; break; case InstructionType::Jump: - vm.call_stack.jump(currentInstruction->param); + vm.call_stack.stackTop->current_line = currentInstruction->param - 1; break; case InstructionType::Call: vm.call_stack.newStackFrame(