Skip to content

Commit

Permalink
Refactor ControlInstructions and VMThread
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandezseb committed Jan 23, 2024
1 parent 5857dea commit 1c1ee10
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 92 deletions.
94 changes: 15 additions & 79 deletions src/VM/Instructions/ControlInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
#include "VM/VM.h"
#include "Data/Variable.h"

static void returnCat1Var(VMThread* thread)
{
const Variable returnVal = thread->m_currentFrame->popOperand();
thread->popFrame();
thread->returnVar(returnVal);
}

void gotoInstruction(INSTRUCTION_ARGS)
{
u1 byte1 = args[0];
Expand All @@ -30,98 +37,27 @@ void gotoInstruction(INSTRUCTION_ARGS)

void freturnInstruction(INSTRUCTION_ARGS)
{
StackFrame* stackFrame = thread->m_currentFrame;
thread->m_pc = stackFrame->previousPc;
thread->m_currentClass = stackFrame->previousClass;
thread->m_currentMethod = stackFrame->previousMethod;

Variable returnVal = thread->m_currentFrame->popOperand();

thread->m_stack.frames.pop_back();
if (thread->m_stack.frames.size() > 0)
{
thread->m_currentFrame = &thread->m_stack.frames[thread->m_stack.frames.size()-1];
thread->m_currentFrame->operands.push_back(returnVal);
} else
{
thread->m_currentFrame = 0;
}
returnCat1Var(thread);
}

void ireturnInstruction(INSTRUCTION_ARGS)
{
StackFrame* stackFrame = thread->m_currentFrame;
thread->m_pc = stackFrame->previousPc;
thread->m_currentClass = stackFrame->previousClass;
thread->m_currentMethod = stackFrame->previousMethod;

Variable returnVal = thread->m_currentFrame->popOperand();

thread->m_stack.frames.pop_back();
if (thread->m_stack.frames.size() > 0)
{
thread->m_currentFrame = &thread->m_stack.frames[thread->m_stack.frames.size()-1];
thread->m_currentFrame->operands.push_back(returnVal);
} else
{
thread->m_currentFrame = 0;
}
returnCat1Var(thread);
}

void dreturnInstruction(INSTRUCTION_ARGS) {
StackFrame* stackFrame = thread->m_currentFrame;
thread->m_pc = stackFrame->previousPc;
thread->m_currentClass = stackFrame->previousClass;
thread->m_currentMethod = stackFrame->previousMethod;

Variable lowByte = thread->m_currentFrame->popOperand();
Variable highByte = thread->m_currentFrame->popOperand();

thread->m_stack.frames.pop_back();
if (thread->m_stack.frames.size() > 0)
{
thread->m_currentFrame = &thread->m_stack.frames[thread->m_stack.frames.size()-1];
thread->m_currentFrame->operands.push_back(highByte);
thread->m_currentFrame->operands.push_back(lowByte);
} else
{
thread->m_currentFrame = 0;
}
const Variable lowByte = thread->m_currentFrame->popOperand();
const Variable highByte = thread->m_currentFrame->popOperand();
thread->popFrame();
thread->returnVar(highByte, lowByte);
}

void areturnInstruction(INSTRUCTION_ARGS)
{
StackFrame* stackFrame = thread->m_currentFrame;
thread->m_pc = stackFrame->previousPc;
thread->m_currentClass = stackFrame->previousClass;
thread->m_currentMethod = stackFrame->previousMethod;

Variable returnVal = thread->m_currentFrame->popOperand();


thread->m_stack.frames.pop_back();

StackFrame* nonNativeFrame = thread->getTopFrameNonNative();
thread->m_currentFrame = &thread->m_stack.frames[thread->m_stack.frames.size()-1];
if (nonNativeFrame != nullptr)
{
nonNativeFrame->operands.push_back(returnVal);
}

returnCat1Var(thread);
}

void returnInstruction(INSTRUCTION_ARGS)
{
StackFrame* stackFrame = thread->m_currentFrame;
thread->m_pc = stackFrame->previousPc;
thread->m_currentClass = stackFrame->previousClass;
thread->m_currentMethod = stackFrame->previousMethod;
thread->m_stack.frames.pop_back();
if (thread->m_stack.frames.size() > 0)
{
thread->m_currentFrame = &thread->m_stack.frames[thread->m_stack.frames.size()-1];
} else
{
thread->m_currentFrame = 0;
}
thread->popFrame();
}
27 changes: 15 additions & 12 deletions src/VM/VMThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <stack>

static constexpr std::string_view NoNonNativeStackFrameFound{"Can't return to previous frame because there is no previous non-native frame"};

void VMThread::pushStackFrameWithoutParams(ClassInfo* classInfo, const MethodInfo* methodInfo)
{
StackFrame stackFrame;
Expand Down Expand Up @@ -123,9 +125,9 @@ void VMThread::pushStackFrameSpecial(ClassInfo* classInfo, const MethodInfo* met
}
}

void VMThread::internalError(const char* error)
void VMThread::internalError(const std::string_view error) const
{
fprintf(stdout, "Unhandled VM error in thread \"%s\": %s\n", m_name.data(), error);
fprintf(stdout, "Unhandled VM error in thread \"%s\": %s\n", m_name.data(), error.data());
for (i8 currentFrame = m_stack.frames.size() - 1; currentFrame >= 0; --currentFrame)
{
const StackFrame frame = m_stack.frames[currentFrame];
Expand Down Expand Up @@ -155,26 +157,27 @@ StackFrame* VMThread::getTopFrameNonNative()

void VMThread::returnVar(const Variable returnValue)
{
if (m_stack.frames.size() > 1)
StackFrame* targetFrame = getTopFrameNonNative();
if (targetFrame != nullptr)
{
StackFrame* previousStackFrame = &m_stack.frames[m_stack.frames.size()-2];
previousStackFrame->operands.push_back(returnValue);
} else
targetFrame->operands.push_back(returnValue);
}
else
{
internalError("Can't return to previous frame because there is no previous frame");
internalError(NoNonNativeStackFrameFound);
}
}

void VMThread::returnVar(Variable highByte, Variable lowByte)
{
if (m_stack.frames.size() > 1)
StackFrame* targetFrame = getTopFrameNonNative();
if (targetFrame != nullptr)
{
StackFrame* previousStackFrame = &m_stack.frames[m_stack.frames.size()-2];
previousStackFrame->operands.push_back(highByte);
previousStackFrame->operands.push_back(lowByte);
targetFrame->operands.push_back(highByte);
targetFrame->operands.push_back(lowByte);
} else
{
internalError("Can't return to previous frame because there is no previous frame");
internalError(NoNonNativeStackFrameFound);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/VM/VMThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class VMThread
void pushStackFrameSpecial(ClassInfo* classInfo, const MethodInfo* methodInfo, StackFrame* previousFrame, JavaHeap* heap);
void returnVar(Variable returnValue);
void returnVar(Variable highByte, Variable lowByte);
void internalError(const char* error);
void internalError(std::string_view error) const;

StackFrame* getTopFrameNonNative();
};

0 comments on commit 1c1ee10

Please sign in to comment.