Skip to content

Commit

Permalink
refactor: inline CallStack::Jump()
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Mar 9, 2024
1 parent 361f176 commit 206bdec
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/bytecode/vm/CallStack.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
15 changes: 5 additions & 10 deletions src/bytecode/vm/CallStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
#include "bytecode/objects/StackObject.h"
#include <cstdint>
#include <cstdlib>
#include <vector>

namespace Bytecode {
struct alignas(4) StackFrame {
struct StackFrame {
uint32_t segment;
uint32_t current_line;
uint32_t args;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/bytecode/vm/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 206bdec

Please sign in to comment.