Skip to content

Commit

Permalink
Adopt more bits from other SOMs (#22)
Browse files Browse the repository at this point in the history
 - track stack effect while emitting bytecode
 - use std::vector, removing ExtendedList
- use VMSymbol more aggressively, might not be efficient and increase
size of symbol table, but simplifies code
  • Loading branch information
smarr authored Jul 13, 2024
2 parents 540d972 + 6a2bd4d commit 16a02cc
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 416 deletions.
24 changes: 12 additions & 12 deletions SOM.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
0A1887441832C62100A2CBCA /* README.md in CopyFiles */ = {isa = PBXBuildFile; fileRef = 0A18873D1832C62100A2CBCA /* README.md */; };
0A1887451832C62100A2CBCA /* Smalltalk in CopyFiles */ = {isa = PBXBuildFile; fileRef = 0A18873E1832C62100A2CBCA /* Smalltalk */; };
0A1887471832C62100A2CBCA /* TestSuite in CopyFiles */ = {isa = PBXBuildFile; fileRef = 0A1887401832C62100A2CBCA /* TestSuite */; };
0A1C98582C3DD88500735850 /* BytecodeGenerationTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C98572C3DD88500735850 /* BytecodeGenerationTest.cpp */; };
0A1C98582C3DD88500735850 /* unitTests/BytecodeGenerationTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C98572C3DD88500735850 /* unitTests/BytecodeGenerationTest.cpp */; };
0A32B7FE199D6143002825DF /* IntegerBox.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A32B7FC199D6143002825DF /* IntegerBox.cpp */; };
0A3A3C921A5D546D004CB03B /* Array.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3F5203120FA6624C00E75857 /* Array.cpp */; };
0A3A3C931A5D546D004CB03B /* Block.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3F5203160FA6624C00E75857 /* Block.cpp */; };
Expand Down Expand Up @@ -183,8 +183,9 @@
0A18873D1832C62100A2CBCA /* README.md */ = {isa = PBXFileReference; lastKnownFileType = text; name = README.md; path = "core-lib/README.md"; sourceTree = "<group>"; };
0A18873E1832C62100A2CBCA /* Smalltalk */ = {isa = PBXFileReference; lastKnownFileType = folder; name = Smalltalk; path = "core-lib/Smalltalk"; sourceTree = "<group>"; };
0A1887401832C62100A2CBCA /* TestSuite */ = {isa = PBXFileReference; lastKnownFileType = folder; name = TestSuite; path = "core-lib/TestSuite"; sourceTree = "<group>"; };
0A1C98562C3DD87300735850 /* BytecodeGenerationTest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "unitTests/BytecodeGenerationTest.h"; sourceTree = "<group>"; };
0A1C98572C3DD88500735850 /* BytecodeGenerationTest.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = "unitTests/BytecodeGenerationTest.cpp"; sourceTree = "<group>"; };
0A1C98562C3DD87300735850 /* unitTests/BytecodeGenerationTest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = unitTests/BytecodeGenerationTest.h; sourceTree = "<group>"; };
0A1C98572C3DD88500735850 /* unitTests/BytecodeGenerationTest.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = unitTests/BytecodeGenerationTest.cpp; sourceTree = "<group>"; };
0A1C98592C432E0E00735850 /* VectorUtil.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VectorUtil.h; sourceTree = "<group>"; };
0A32B7FC199D6143002825DF /* IntegerBox.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IntegerBox.cpp; sourceTree = "<group>"; };
0A32B7FD199D6143002825DF /* IntegerBox.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IntegerBox.h; sourceTree = "<group>"; };
0A32B80119A12A03002825DF /* VMObjectBase.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VMObjectBase.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -234,7 +235,6 @@
3F52030A0FA6624C00E75857 /* Heap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Heap.h; sourceTree = "<group>"; };
3F52030C0FA6624C00E75857 /* debug.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = debug.h; sourceTree = "<group>"; };
3F52030D0FA6624C00E75857 /* defs.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = defs.h; sourceTree = "<group>"; };
3F52030E0FA6624C00E75857 /* ExtendedList.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.h; path = ExtendedList.h; sourceTree = "<group>"; tabWidth = 4; };
3F52030F0FA6624C00E75857 /* gettimeofday.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gettimeofday.h; sourceTree = "<group>"; };
3F5203120FA6624C00E75857 /* Array.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Array.cpp; sourceTree = "<group>"; };
3F5203130FA6624C00E75857 /* Array.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Array.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -416,8 +416,8 @@
0A67EAA619ACE09700830E3B /* WriteBarrierTest.h */,
0A67EA7319ACD43A00830E3B /* WalkObjectsTest.cpp */,
0A67EA7419ACD43A00830E3B /* WriteBarrierTest.cpp */,
0A1C98562C3DD87300735850 /* BytecodeGenerationTest.h */,
0A1C98572C3DD88500735850 /* BytecodeGenerationTest.cpp */,
0A1C98562C3DD87300735850 /* unitTests/BytecodeGenerationTest.h */,
0A1C98572C3DD88500735850 /* unitTests/BytecodeGenerationTest.cpp */,
);
name = unittests;
sourceTree = "<group>";
Expand Down Expand Up @@ -517,12 +517,12 @@
children = (
3F52030C0FA6624C00E75857 /* debug.h */,
3F52030D0FA6624C00E75857 /* defs.h */,
3F52030E0FA6624C00E75857 /* ExtendedList.h */,
3F52030F0FA6624C00E75857 /* gettimeofday.h */,
0A1887371832C12E00A2CBCA /* Timer.cpp */,
0A1887381832C12E00A2CBCA /* Timer.h */,
0A707528297DF36F00EB9F59 /* ParseInteger.h */,
0A707529297DF97700EB9F59 /* ParseInteger.cpp */,
0A1C98592C432E0E00735850 /* VectorUtil.h */,
);
path = misc;
sourceTree = "<group>";
Expand Down Expand Up @@ -892,7 +892,7 @@
0AB80AD92C394806006B6419 /* Globals.cpp in Sources */,
0A3A3CA71A5D546D004CB03B /* Object.cpp in Sources */,
0A3A3CB21A5D5476004CB03B /* PrimitiveContainer.cpp in Sources */,
0A1C98582C3DD88500735850 /* BytecodeGenerationTest.cpp in Sources */,
0A1C98582C3DD88500735850 /* unitTests/BytecodeGenerationTest.cpp in Sources */,
0A67EA8419ACD74800830E3B /* VMFrame.cpp in Sources */,
0A67EA7D19ACD74800830E3B /* Signature.cpp in Sources */,
0A67EA7E19ACD74800830E3B /* VMArray.cpp in Sources */,
Expand Down Expand Up @@ -948,7 +948,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LANGUAGE_STANDARD = "c++17";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
Expand All @@ -959,7 +959,7 @@
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_C_LANGUAGE_STANDARD = c17;
GCC_DYNAMIC_NO_PIC = NO;
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
Expand Down Expand Up @@ -987,7 +987,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LANGUAGE_STANDARD = "c++17";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
Expand All @@ -1000,7 +1000,7 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_C_LANGUAGE_STANDARD = c17;
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand Down
1 change: 0 additions & 1 deletion src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

#include "memory/Heap.h"

#include "misc/ExtendedList.h"
#include "misc/defs.h"

#include "vm/Universe.h"
Expand Down
122 changes: 55 additions & 67 deletions src/compiler/BytecodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,26 @@
#include <vmobjects/VMObject.h>
#include <vmobjects/VMMethod.h>
#include <vmobjects/VMSymbol.h>
#include <vmobjects/Signature.h>

#define EMIT1(BC) \
mgenc->AddBytecode(BC)
#define EMIT1(BC, stackEffect) \
mgenc->AddBytecode(BC, stackEffect)

#define EMIT2(BC, IDX) \
mgenc->AddBytecode(BC);\
mgenc->AddBytecode(IDX)
#define EMIT2(BC, IDX, stackEffect) \
mgenc->AddBytecode(BC, stackEffect);\
mgenc->AddBytecodeArgument(IDX)

#define EMIT3(BC, IDX, CTX) \
mgenc->AddBytecode(BC);\
mgenc->AddBytecode(IDX);\
mgenc->AddBytecode(CTX)
#define EMIT3(BC, IDX, CTX, stackEffect) \
mgenc->AddBytecode(BC, stackEffect);\
mgenc->AddBytecodeArgument(IDX);\
mgenc->AddBytecodeArgument(CTX)

void BytecodeGenerator::EmitHALT(MethodGenerationContext* mgenc) {
EMIT1(BC_HALT);
EMIT1(BC_HALT, 0);
}

void BytecodeGenerator::EmitDUP(MethodGenerationContext* mgenc) {
EMIT1(BC_DUP);
EMIT1(BC_DUP, 1);
}

void BytecodeGenerator::EmitPUSHLOCAL(MethodGenerationContext* mgenc, long idx,
Expand All @@ -59,19 +60,19 @@ void BytecodeGenerator::EmitPUSHLOCAL(MethodGenerationContext* mgenc, long idx,
assert(ctx >= 0);
if (ctx == 0) {
if (idx == 0) {
EMIT1(BC_PUSH_LOCAL_0);
EMIT1(BC_PUSH_LOCAL_0, 1);
return;
}
if (idx == 1) {
EMIT1(BC_PUSH_LOCAL_1);
EMIT1(BC_PUSH_LOCAL_1, 1);
return;
}
if (idx == 2) {
EMIT1(BC_PUSH_LOCAL_2);
EMIT1(BC_PUSH_LOCAL_2, 1);
return;
}
}
EMIT3(BC_PUSH_LOCAL, idx, ctx);
EMIT3(BC_PUSH_LOCAL, idx, ctx, 1);
}

void BytecodeGenerator::EmitPUSHARGUMENT(MethodGenerationContext* mgenc,
Expand All @@ -81,81 +82,81 @@ void BytecodeGenerator::EmitPUSHARGUMENT(MethodGenerationContext* mgenc,

if (ctx == 0) {
if (idx == 0) {
EMIT1(BC_PUSH_SELF);
EMIT1(BC_PUSH_SELF, 1);
return;
}

if (idx == 1) {
EMIT1(BC_PUSH_ARG_1);
EMIT1(BC_PUSH_ARG_1, 1);
return;
}

if (idx == 2) {
EMIT1(BC_PUSH_ARG_2);
EMIT1(BC_PUSH_ARG_2, 1);
return;
}
}
EMIT3(BC_PUSH_ARGUMENT, idx, ctx);
EMIT3(BC_PUSH_ARGUMENT, idx, ctx, 1);
}

void BytecodeGenerator::EmitPUSHFIELD(MethodGenerationContext* mgenc, VMSymbol* field) {
uint8_t idx = mgenc->GetFieldIndex(field);
if (idx == 0) {
EMIT1(BC_PUSH_FIELD_0);
EMIT1(BC_PUSH_FIELD_0, 1);
} else if (idx == 1) {
EMIT1(BC_PUSH_FIELD_1);
EMIT1(BC_PUSH_FIELD_1, 1);
} else {
EMIT2(BC_PUSH_FIELD, idx);
EMIT2(BC_PUSH_FIELD, idx, 1);
}
}

void BytecodeGenerator::EmitPUSHBLOCK(MethodGenerationContext* mgenc, VMMethod* block) {
int8_t idx = mgenc->AddLiteralIfAbsent(block);
EMIT2(BC_PUSH_BLOCK, idx);
EMIT2(BC_PUSH_BLOCK, idx, 1);
}

void BytecodeGenerator::EmitPUSHCONSTANT(MethodGenerationContext* mgenc, vm_oop_t cst) {
if (CLASS_OF(cst) == load_ptr(integerClass)) {
if (INT_VAL(cst) == 0ll) {
EMIT1(BC_PUSH_0);
EMIT1(BC_PUSH_0, 1);
return;
}
if (INT_VAL(cst) == 1ll) {
EMIT1(BC_PUSH_1);
EMIT1(BC_PUSH_1, 1);
return;
}
}

if (cst == load_ptr(nilObject)) {
EMIT1(BC_PUSH_NIL);
EMIT1(BC_PUSH_NIL, 1);
return;
}

int8_t idx = mgenc->AddLiteralIfAbsent(cst);
if (idx == 0) {
EMIT1(BC_PUSH_CONSTANT_0);
EMIT1(BC_PUSH_CONSTANT_0, 1);
return;
}
if (idx == 1) {
EMIT1(BC_PUSH_CONSTANT_1);
EMIT1(BC_PUSH_CONSTANT_1, 1);
return;
}
if (idx == 2) {
EMIT1(BC_PUSH_CONSTANT_2);
EMIT1(BC_PUSH_CONSTANT_2, 1);
return;
}

EMIT2(BC_PUSH_CONSTANT, idx);
EMIT2(BC_PUSH_CONSTANT, idx, 1);
}

void BytecodeGenerator::EmitPUSHCONSTANT(MethodGenerationContext* mgenc,
uint8_t literalIndex) {
EMIT2(BC_PUSH_CONSTANT, literalIndex);
EMIT2(BC_PUSH_CONSTANT, literalIndex, 1);
}

void BytecodeGenerator::EmitPUSHCONSTANTString(MethodGenerationContext* mgenc,
VMString* str) {
EMIT2(BC_PUSH_CONSTANT, mgenc->FindLiteralIndex(str));
EMIT2(BC_PUSH_CONSTANT, mgenc->FindLiteralIndex(str), 1);
}

void BytecodeGenerator::EmitPUSHGLOBAL(MethodGenerationContext* mgenc, VMSymbol* global) {
Expand All @@ -167,12 +168,12 @@ void BytecodeGenerator::EmitPUSHGLOBAL(MethodGenerationContext* mgenc, VMSymbol*
EmitPUSHCONSTANT(mgenc, load_ptr(falseObject));
} else {
int8_t idx = mgenc->AddLiteralIfAbsent(global);
EMIT2(BC_PUSH_GLOBAL, idx);
EMIT2(BC_PUSH_GLOBAL, idx, 1);
}
}

void BytecodeGenerator::EmitPOP(MethodGenerationContext* mgenc) {
EMIT1(BC_POP);
EMIT1(BC_POP, -1);
}

void BytecodeGenerator::EmitPOPLOCAL(MethodGenerationContext* mgenc, long idx,
Expand All @@ -181,76 +182,63 @@ void BytecodeGenerator::EmitPOPLOCAL(MethodGenerationContext* mgenc, long idx,
assert(ctx >= 0);
if (ctx == 0) {
if (idx == 0) {
EMIT1(BC_POP_LOCAL_0);
EMIT1(BC_POP_LOCAL_0, -1);
return;
}

if (idx == 1) {
EMIT1(BC_POP_LOCAL_1);
EMIT1(BC_POP_LOCAL_1, -1);
return;
}

if (idx == 2) {
EMIT1(BC_POP_LOCAL_2);
EMIT1(BC_POP_LOCAL_2, -1);
return;
}
}

EMIT3(BC_POP_LOCAL, idx, ctx);
EMIT3(BC_POP_LOCAL, idx, ctx, -1);
}

void BytecodeGenerator::EmitPOPARGUMENT(MethodGenerationContext* mgenc,
long idx, int ctx) {
EMIT3(BC_POP_ARGUMENT, idx, ctx);
EMIT3(BC_POP_ARGUMENT, idx, ctx, -1);
}

void BytecodeGenerator::EmitPOPFIELD(MethodGenerationContext* mgenc, VMSymbol* field) {
uint8_t idx = mgenc->GetFieldIndex(field);

if (idx == 0) {
EMIT1(BC_POP_FIELD_0);
EMIT1(BC_POP_FIELD_0, -1);
} else if (idx == 1) {
EMIT1(BC_POP_FIELD_1);
EMIT1(BC_POP_FIELD_1, -1);
} else {
EMIT2(BC_POP_FIELD, idx);
EMIT2(BC_POP_FIELD, idx, -1);
}
}

void BytecodeGenerator::EmitSEND(MethodGenerationContext* mgenc, VMSymbol* msg) {
int8_t idx = mgenc->AddLiteralIfAbsent(msg);
EMIT2(BC_SEND, idx);

int numArgs = Signature::GetNumberOfArguments(msg);
size_t stackEffect = -numArgs + 1; // +1 for the result

EMIT2(BC_SEND, idx, stackEffect);
}

void BytecodeGenerator::EmitSUPERSEND(MethodGenerationContext* mgenc, VMSymbol* msg) {
int8_t idx = mgenc->AddLiteralIfAbsent(msg);
EMIT2(BC_SUPER_SEND, idx);

int numArgs = Signature::GetNumberOfArguments(msg);
size_t stackEffect = -numArgs + 1; // +1 for the result

EMIT2(BC_SUPER_SEND, idx, stackEffect);
}

void BytecodeGenerator::EmitRETURNLOCAL(MethodGenerationContext* mgenc) {
EMIT1(BC_RETURN_LOCAL);
EMIT1(BC_RETURN_LOCAL, 0);
}

void BytecodeGenerator::EmitRETURNNONLOCAL(MethodGenerationContext* mgenc) {
EMIT1(BC_RETURN_NON_LOCAL);
}

size_t emitJump(MethodGenerationContext* mgenc, uint8_t jumpBC) {
size_t pos = mgenc->AddBytecode(jumpBC);
EMIT1(0);
EMIT1(0);
EMIT1(0);
EMIT1(0);
return pos;
}

size_t BytecodeGenerator::EmitJUMP_IF_FALSE(MethodGenerationContext* mgenc) {
return emitJump(mgenc, BC_JUMP_IF_FALSE);
}

size_t BytecodeGenerator::EmitJUMP_IF_TRUE(MethodGenerationContext* mgenc) {
return emitJump(mgenc, BC_JUMP_IF_TRUE);
}

size_t BytecodeGenerator::EmitJUMP(MethodGenerationContext* mgenc) {
return emitJump(mgenc, BC_JUMP);
EMIT1(BC_RETURN_NON_LOCAL, 0);
}
4 changes: 0 additions & 4 deletions src/compiler/BytecodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,4 @@ class BytecodeGenerator {
void EmitSUPERSEND(MethodGenerationContext* mgenc, VMSymbol* msg);
void EmitRETURNLOCAL(MethodGenerationContext* mgenc);
void EmitRETURNNONLOCAL(MethodGenerationContext* mgenc);

size_t EmitJUMP_IF_FALSE(MethodGenerationContext* mgenc);
size_t EmitJUMP_IF_TRUE(MethodGenerationContext* mgenc);
size_t EmitJUMP(MethodGenerationContext* mgenc);
};
Loading

0 comments on commit 16a02cc

Please sign in to comment.