From 85e06d19c7990b0747706213cccf69303ecf287b Mon Sep 17 00:00:00 2001 From: "Mr.UNIX" Date: Mon, 29 Jul 2024 17:49:13 +0100 Subject: [PATCH] VM: Only update segment when pushing to/popping from the call stack --- src/vm.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/vm.cpp b/src/vm.cpp index dc46d49..80c8a7f 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -113,19 +113,21 @@ void VM::run(const Program &program) { callStack.front().locals = newPtr; } } + auto *segment = &program.segments[callStack.back().segmentIndex]; for (;;) { - auto &segment = program.segments[callStack.back().segmentIndex]; - auto &instruction = segment.instructions[callStack.back().currentInstruction]; + auto &instruction = segment->instructions[callStack.back().currentInstruction]; switch (instruction.type) { case Instruction::InstructionType::Invalid: throw std::runtime_error("[VM::run] Invalid instruction!"); case Instruction::InstructionType::Return: { popStackFrame(); + segment = &program.segments[callStack.back().segmentIndex]; continue; } case Instruction::InstructionType::Call: { callStack.back().currentInstruction++; newStackFrame(program.segments[instruction.params.index]); + segment = &program.segments[instruction.params.index]; continue; } case Instruction::InstructionType::JumpIfFalse: {