Skip to content

Micro optimizations of interpreter loop #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ int main(int argc, char** argv) {

Universe::Start(argc, argv);

Universe::Quit(ERR_SUCCESS);
Quit(ERR_SUCCESS);
}
4 changes: 2 additions & 2 deletions src/compiler/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <string>

#include "../misc/defs.h"
#include "../vm/Universe.h"
#include "../vm/Print.h"

Lexer::Lexer(istream& file) : infile(file), peekDone(false) {}

Expand Down Expand Up @@ -322,7 +322,7 @@ Symbol Lexer::Peek() {
const LexerState old = state;

if (peekDone) {
Universe::ErrorExit("Cannot Peek twice!\n");
ErrorExit("Cannot Peek twice!\n");
}
GetSym();
const Symbol nextSym = state.sym;
Expand Down
15 changes: 8 additions & 7 deletions src/compiler/MethodGenerationContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "../interpreter/bytecodes.h"
#include "../misc/VectorUtil.h"
#include "../vm/Globals.h"
#include "../vm/Print.h"
#include "../vm/Universe.h"
#include "../vmobjects/ObjectFormats.h"
#include "../vmobjects/VMMethod.h"
Expand Down Expand Up @@ -147,7 +148,7 @@ VMTrivialMethod* MethodGenerationContext::assembleLiteralReturn(
}
}

GetUniverse()->ErrorExit(
ErrorExit(
"Unexpected situation when trying to create trivial method that "
"returns a literal");
}
Expand All @@ -159,7 +160,7 @@ VMTrivialMethod* MethodGenerationContext::assembleGlobalReturn() {
}

if (literals.size() != 1) {
GetUniverse()->ErrorExit(
ErrorExit(
"Unexpected situation when trying to create trivial method that "
"reads a global. New Bytecode?");
}
Expand Down Expand Up @@ -228,7 +229,7 @@ VMTrivialMethod* MethodGenerationContext::assembleFieldSetter() {
break;
}
default: {
GetUniverse()->ErrorExit("Unexpected bytecode");
ErrorExit("Unexpected bytecode");
}
}

Expand All @@ -246,7 +247,7 @@ VMTrivialMethod* MethodGenerationContext::assembleFieldSetter() {
break;
}
default: {
GetUniverse()->ErrorExit("Unexpected bytecode");
ErrorExit("Unexpected bytecode");
}
}

Expand Down Expand Up @@ -708,7 +709,7 @@ uint8_t MethodGenerationContext::GetInlinedLocalIdx(const Variable* var) const {
"Unexpected issue trying to find an inlined variable. %s could not "
"be found.",
qualifiedName.data());
Universe::ErrorExit(msg);
ErrorExit(msg);
}

void MethodGenerationContext::checkJumpOffset(size_t jumpOffset,
Expand All @@ -718,7 +719,7 @@ void MethodGenerationContext::checkJumpOffset(size_t jumpOffset,
snprintf(msg, 100,
"The jumpOffset for the %s bytecode is out of range: %zu\n",
Bytecode::GetBytecodeName(bytecode), jumpOffset);
Universe::ErrorExit(msg);
ErrorExit(msg);
}
}

Expand Down Expand Up @@ -786,7 +787,7 @@ size_t MethodGenerationContext::getOffsetOfLastBytecode(size_t indexFromEnd) {
for (size_t i = 0; i < indexFromEnd + 1; i += 1) {
uint8_t actual = last4Bytecodes.at(NUM_LAST_BYTECODES - 1 - i);
if (actual == BC_INVALID) {
GetUniverse()->ErrorExit("The requested bytecode is invalid");
ErrorExit("The requested bytecode is invalid");
}

bcOffset -= Bytecode::GetBytecodeLength(actual);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ __attribute__((noreturn)) void Parser::parseError(const char* msg,
ReplacePattern(msgWithMeta, "%(found)s", foundStr);

ErrorPrint(msgWithMeta);
GetUniverse()->Quit(ERR_FAIL);
Quit(ERR_FAIL);
}

__attribute__((noreturn)) void Parser::parseError(const char* msg,
Expand Down
7 changes: 4 additions & 3 deletions src/interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "../misc/defs.h"
#include "../vm/Globals.h"
#include "../vm/IsValidObject.h"
#include "../vm/Print.h"
#include "../vm/Universe.h"
#include "../vmobjects/IntegerBox.h"
#include "../vmobjects/ObjectFormats.h"
Expand Down Expand Up @@ -237,7 +238,7 @@ void Interpreter::doPushFieldWithIndex(uint8_t fieldIndex) {

if (unlikely(IS_TAGGED(self))) {
o = nullptr;
Universe()->ErrorExit("Integers do not have fields!");
ErrorExit("Integers do not have fields!");
} else {
o = ((VMObject*)self)->GetField(fieldIndex);
}
Expand Down Expand Up @@ -320,7 +321,7 @@ void Interpreter::doPopFieldWithIndex(uint8_t fieldIndex) {
vm_oop_t o = GetFrame()->Pop();

if (unlikely(IS_TAGGED(self))) {
GetUniverse()->ErrorExit("Integers do not have fields that can be set");
ErrorExit("Integers do not have fields that can be set");
} else {
((VMObject*)self)->SetField(fieldIndex, o);
}
Expand Down Expand Up @@ -436,7 +437,7 @@ void Interpreter::doInc() {
double d = static_cast<VMDouble*>(val)->GetEmbeddedDouble();
val = GetUniverse()->NewDouble(d + 1.0);
} else {
GetUniverse()->ErrorExit("unsupported");
ErrorExit("unsupported");
}

GetFrame()->SetTop(store_root(val));
Expand Down
7 changes: 3 additions & 4 deletions src/memory/CopyingHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "../misc/defs.h"
#include "../vm/Print.h"
#include "../vm/Universe.h"
#include "../vmobjects/AbstractObject.h"
#include "CopyingCollector.h"
#include "Heap.h"
Expand Down Expand Up @@ -52,7 +51,7 @@ void CopyingHeap::switchBuffers(bool increaseMemory) {
currentBuffer = malloc(newSize);

if (currentBuffer == nullptr) {
Universe::ErrorExit("unable to allocate heap memory");
ErrorExit("unable to allocate heap memory");
}

currentBufferEnd = (void*)((size_t)currentBuffer + newSize);
Expand Down Expand Up @@ -91,7 +90,7 @@ void CopyingHeap::invalidateOldBuffer() {
oldBuffer = malloc(currentBufSize);

if (oldBuffer == nullptr) {
Universe::ErrorExit("unable to allocate heap memory");
ErrorExit("unable to allocate heap memory");
}

oldBufferEnd = (void*)((size_t)oldBuffer + currentBufSize);
Expand All @@ -103,7 +102,7 @@ AbstractVMObject* CopyingHeap::AllocateObject(size_t size) {
nextFreePosition = (void*)((size_t)nextFreePosition + size);
if (nextFreePosition > currentBufferEnd) {
ErrorPrint("\nFailed to allocate " + to_string(size) + " Bytes.\n");
Universe::Quit(-1);
Quit(-1);
}

// let's see if we have to trigger the GC
Expand Down
3 changes: 1 addition & 2 deletions src/memory/DebugCopyingHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <string>

#include "../vm/Print.h"
#include "../vm/Universe.h"
#include "../vmobjects/AbstractObject.h"

void DebugCopyingHeap::switchBuffers(bool increaseMemory) {
Expand Down Expand Up @@ -57,7 +56,7 @@ AbstractVMObject* DebugCopyingHeap::AllocateObject(size_t size) {

if (currentHeapUsage > currentHeapSize) {
ErrorPrint("\nFailed to allocate " + to_string(size) + " Bytes.\n");
Universe::Quit(-1);
Quit(-1);
}

// let's see if we have to trigger the GC
Expand Down
5 changes: 2 additions & 3 deletions src/memory/GenerationalHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <vector>

#include "../vm/Print.h"
#include "../vm/Universe.h"
#include "../vmobjects/AbstractObject.h"
#include "../vmobjects/ObjectFormats.h"
#include "../vmobjects/VMObjectBase.h"
Expand Down Expand Up @@ -39,7 +38,7 @@ AbstractVMObject* GenerationalHeap::AllocateNurseryObject(size_t size) {
if ((size_t)nextFreePosition > nursery_end) {
ErrorPrint("\nFailed to allocate " + to_string(size) +
" Bytes in nursery.\n");
GetUniverse()->Quit(-1);
Quit(-1);
}
// let's see if we have to trigger the GC
if (nextFreePosition > collectionLimit) {
Expand All @@ -52,7 +51,7 @@ AbstractVMObject* GenerationalHeap::AllocateMatureObject(size_t size) {
AbstractVMObject* newObject = (AbstractVMObject*)malloc(size);
if (newObject == nullptr) {
ErrorPrint("\nFailed to allocate " + to_string(size) + " Bytes.\n");
GetUniverse()->Quit(-1);
Quit(-1);
}
allocatedObjects->push_back(newObject);
matureObjectsSize += size;
Expand Down
3 changes: 1 addition & 2 deletions src/memory/MarkSweepHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "../memory/Heap.h"
#include "../vm/Print.h"
#include "../vm/Universe.h"
#include "../vmobjects/AbstractObject.h"
#include "MarkSweepCollector.h"

Expand All @@ -23,7 +22,7 @@ AbstractVMObject* MarkSweepHeap::AllocateObject(size_t size) {
AbstractVMObject* newObject = (AbstractVMObject*)malloc(size);
if (newObject == nullptr) {
ErrorPrint("\nFailed to allocate " + to_string(size) + " Bytes.\n");
Universe::Quit(-1);
Quit(-1);
}
spcAlloc += size;
memset((void*)newObject, 0, size);
Expand Down
4 changes: 2 additions & 2 deletions src/primitives/Double.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include "../primitivesCore/PrimitiveContainer.h"
#include "../vm/Globals.h"
#include "../vm/Print.h"
#include "../vm/Universe.h"
#include "../vmobjects/ObjectFormats.h"
#include "../vmobjects/VMDouble.h"
Expand All @@ -56,8 +57,7 @@ double coerceDouble(vm_oop_t x) {
} else if (cl == load_ptr(integerClass)) {
return (double)static_cast<VMInteger*>(x)->GetEmbeddedInteger();
} else {
GetUniverse()->ErrorExit(
"Attempt to apply Double operation to non-number.");
ErrorExit("Attempt to apply Double operation to non-number.");
}

return 0.0f;
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static vm_oop_t sysLoad_(vm_oop_t, vm_oop_t rightObj) {

static vm_oop_t sysExit_(vm_oop_t, vm_oop_t err) {
long err_no = INT_VAL(err);
GetUniverse()->Quit(err_no);
Quit(err_no);
}

static vm_oop_t sysPrintString_(vm_oop_t leftObj, vm_oop_t rightObj) {
Expand Down
34 changes: 32 additions & 2 deletions src/vm/Print.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
#include "Print.h"

#include <cstdlib>
#include <iostream>
#include <mutex>
#include <string>

#include "../misc/defs.h"
#include "LogAllocation.h"
#include "Universe.h"

using namespace std;

static mutex output_mutex;

void Print(StdString str) {
void Print(std::string str) {
lock_guard<mutex> lock(output_mutex);
cout << str << flush;
}

void ErrorPrint(StdString str) {
void ErrorPrint(std::string str) {
lock_guard<mutex> lock(output_mutex);
cerr << str << flush;
}

void Print(const char* str) {
lock_guard<mutex> lock(output_mutex);
cout << str << flush;
}

void ErrorPrint(const char* str) {
lock_guard<mutex> lock(output_mutex);
cerr << str << flush;
}

__attribute__((noreturn)) __attribute__((noinline)) void ErrorExit(
const char* err) {
ErrorPrint("Runtime error: " + StdString(err) + "\n");
Quit(ERR_FAIL);
}

__attribute__((noreturn)) __attribute__((noinline)) void Quit(long err) {
ErrorPrint("Time spent in GC: [" +
to_string(Timer::GCTimer->GetTotalTime()) + "] msec\n");

Universe::Shutdown();

OutputAllocationLogFile();
exit((int)err);
}
11 changes: 8 additions & 3 deletions src/vm/Print.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#pragma once

#include "../misc/defs.h"
#include <string>

void Print(StdString str);
void ErrorPrint(StdString str);
void Print(std::string str);
void Print(const char* str);
void ErrorPrint(std::string str);
void ErrorPrint(const char* str);

__attribute__((noreturn)) __attribute__((noinline)) void Quit(long);
__attribute__((noreturn)) __attribute__((noinline)) void ErrorExit(const char*);
3 changes: 2 additions & 1 deletion src/vm/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "../vmobjects/VMInvokable.h"
#include "../vmobjects/VMMethod.h"
#include "Globals.h"
#include "Print.h"
#include "Symbols.h"
#include "Universe.h"

Expand Down Expand Up @@ -65,7 +66,7 @@ void Shell::Start(Interpreter* interp) {
#define QUIT_CMD_L 11 + 1

if (bootstrapMethod == nullptr) {
GetUniverse()->ErrorExit("Shell needs bootstrap method!");
ErrorExit("Shell needs bootstrap method!");
}
// the statement to evaluate
char inbuf[INPUT_MAX_SIZE];
Expand Down
13 changes: 2 additions & 11 deletions src/vm/Universe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void Universe::BasicInit() {
theUniverse = new Universe();
}

__attribute__((noreturn)) void Universe::Quit(long err) {
void Universe::Shutdown() {
ErrorPrint("Time spent in GC: [" +
to_string(Timer::GCTimer->GetTotalTime()) + "] msec\n");
#ifdef GENERATE_INTEGER_HISTOGRAM
Expand Down Expand Up @@ -136,18 +136,9 @@ __attribute__((noreturn)) void Universe::Quit(long err) {
}
#endif

OutputAllocationLogFile();

if (theUniverse) {
delete (theUniverse);
}

exit((int)err);
}

__attribute__((noreturn)) void Universe::ErrorExit(const char* err) {
ErrorPrint("Runtime error: " + StdString(err) + "\n");
Quit(ERR_FAIL);
}

vector<StdString> Universe::handleArguments(long argc, char** argv) {
Expand Down Expand Up @@ -619,7 +610,7 @@ void Universe::LoadSystemClass(VMClass* systemClass) {

if (!result) {
ErrorPrint("Can't load system class: " + s + "\n");
Universe::Quit(ERR_FAIL);
Quit(ERR_FAIL);
}

if (result->HasPrimitives() || result->GetClass()->HasPrimitives()) {
Expand Down
Loading