Skip to content
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

Add inlining of #whileTrue:/#whileFalse: #33

Merged
merged 6 commits into from
Aug 1, 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: 2 additions & 0 deletions SOM.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
0A335F6F1832D65100D7CFC8 /* Primitive.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Primitive.cpp; sourceTree = "<group>"; };
0A335F701832D65100D7CFC8 /* Primitive.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Primitive.h; sourceTree = "<group>"; };
0A3FABE724F4670D002D4D69 /* BasicInterpreterTests.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; fileEncoding = 4; name = BasicInterpreterTests.h; path = unitTests/BasicInterpreterTests.h; sourceTree = "<group>"; };
0A5A7E8E2C5A37450011C783 /* StringUtil.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = StringUtil.h; sourceTree = "<group>"; };
0A67EA6719ACD37200830E3B /* unittests */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = unittests; sourceTree = BUILT_PRODUCTS_DIR; };
0A67EA7019ACD43A00830E3B /* CloneObjectsTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CloneObjectsTest.cpp; path = unitTests/CloneObjectsTest.cpp; sourceTree = "<group>"; };
0A67EA7119ACD43A00830E3B /* main.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = main.cpp; path = unitTests/main.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -557,6 +558,7 @@
0A707529297DF97700EB9F59 /* ParseInteger.cpp */,
0A1C98592C432E0E00735850 /* VectorUtil.h */,
0A1C986D2C4F1D3900735850 /* debug.cpp */,
0A5A7E8E2C5A37450011C783 /* StringUtil.h */,
);
path = misc;
sourceTree = "<group>";
Expand Down
67 changes: 67 additions & 0 deletions src/compiler/BytecodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,70 @@ void EmitRETURNLOCAL(MethodGenerationContext& mgenc) {
void EmitRETURNNONLOCAL(MethodGenerationContext& mgenc) {
Emit1(mgenc, BC_RETURN_NON_LOCAL, 0);
}

size_t EmitJumpOnBoolWithDummyOffset(MethodGenerationContext& mgenc, bool isIfTrue, bool needsPop) {
// Remember: true and false seem flipped here.
// This is because if the test passes, the block is inlined directly.
// But if the test fails, we need to jump.
// Thus, an `#ifTrue:` needs to generated a jump_on_false.
uint8_t bc;
size_t stackEffect;

if (needsPop) {
bc = isIfTrue ? BC_JUMP_ON_FALSE_POP : BC_JUMP_ON_TRUE_POP;
stackEffect = -1;
} else {
bc = isIfTrue ? BC_JUMP_ON_FALSE_TOP_NIL : BC_JUMP_ON_TRUE_TOP_NIL;
stackEffect = 0;
}

Emit1(mgenc, bc, stackEffect);

size_t idx = mgenc.AddBytecodeArgumentAndGetIndex(0);
mgenc.AddBytecodeArgument(0);
return idx;
}

void EmitJumpBackwardWithOffset(MethodGenerationContext& mgenc, size_t jumpOffset) {
uint8_t jumpBytecode = jumpOffset <= 0xFF ? BC_JUMP_BACKWARD : BC_JUMP2_BACKWARD;
Emit3(mgenc, jumpBytecode, jumpOffset & 0xFF, jumpOffset >> 8, 0);
}

size_t Emit3WithDummy(MethodGenerationContext& mgenc, uint8_t bytecode, size_t stackEffect) {
mgenc.AddBytecode(bytecode, stackEffect);
size_t index = mgenc.AddBytecodeArgumentAndGetIndex(0);
mgenc.AddBytecodeArgument(0);
return index;
}

void EmitPushFieldWithIndex(MethodGenerationContext& mgenc, uint8_t fieldIdx, uint8_t ctxLevel) {
// if (ctxLevel == 0) {
if (fieldIdx == 0) {
Emit1(mgenc, BC_PUSH_FIELD_0, 1);
return;
}

if (fieldIdx == 1) {
Emit1(mgenc, BC_PUSH_FIELD_1, 1);
return;
}
// }

Emit2(mgenc, BC_PUSH_FIELD, fieldIdx, 1);
}

void EmitPopFieldWithIndex(MethodGenerationContext& mgenc, uint8_t fieldIdx, uint8_t ctxLevel) {
// if (ctxLevel == 0) {
if (fieldIdx == 0) {
Emit1(mgenc, BC_POP_FIELD_0, 1);
return;
}

if (fieldIdx == 1) {
Emit1(mgenc, BC_POP_FIELD_1, 1);
return;
}
// }

Emit2(mgenc, BC_POP_FIELD, fieldIdx, 1);
}
8 changes: 8 additions & 0 deletions src/compiler/BytecodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ void EmitSEND(MethodGenerationContext& mgenc, VMSymbol* msg);
void EmitSUPERSEND(MethodGenerationContext& mgenc, VMSymbol* msg);
void EmitRETURNLOCAL(MethodGenerationContext& mgenc);
void EmitRETURNNONLOCAL(MethodGenerationContext& mgenc);

size_t EmitJumpOnBoolWithDummyOffset(MethodGenerationContext& mgenc, bool isIfTrue, bool needsPop);
void EmitJumpBackwardWithOffset(MethodGenerationContext& mgenc, size_t jumpOffset);
size_t Emit3WithDummy(MethodGenerationContext& mgenc, uint8_t bytecode, size_t stackEffect);

void EmitPushFieldWithIndex(MethodGenerationContext& mgenc, uint8_t fieldIdx, uint8_t ctxLevel);
void EmitPopFieldWithIndex(MethodGenerationContext& mgenc, uint8_t fieldIdx, uint8_t ctxLevel);

60 changes: 42 additions & 18 deletions src/compiler/Disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,27 @@ void Disassembler::dumpMethod(uint8_t* bytecodes, size_t numberOfBytecodes, cons
}
break;
}
case BC_JUMP_IF_FALSE:
case BC_JUMP_IF_TRUE:
case BC_JUMP: {
int target = 0;
target |= bytecodes[bc_idx + 1];
target |= bytecodes[bc_idx + 2] << 8;
target |= bytecodes[bc_idx + 3] << 16;
target |= bytecodes[bc_idx + 4] << 24;
DebugPrint("(target: %d)\n", target);
case BC_JUMP:
case BC_JUMP_ON_FALSE_POP:
case BC_JUMP_ON_TRUE_POP:
case BC_JUMP_ON_FALSE_TOP_NIL:
case BC_JUMP_ON_TRUE_TOP_NIL:
case BC_JUMP_BACKWARD:
case BC_JUMP2:
case BC_JUMP2_ON_FALSE_POP:
case BC_JUMP2_ON_TRUE_POP:
case BC_JUMP2_ON_FALSE_TOP_NIL:
case BC_JUMP2_ON_TRUE_TOP_NIL:
case BC_JUMP2_BACKWARD: {
uint16_t offset = ComputeOffset(bytecodes[bc_idx + 1], bytecodes[bc_idx + 2]);

int32_t target;
if (bytecode == BC_JUMP_BACKWARD || bytecode == BC_JUMP2_BACKWARD) {
target = ((int32_t) bc_idx) - offset;
} else {
target = ((int32_t) bc_idx) + offset;
}
DebugPrint("(jump offset: %d -> jump target: %d)\n", offset, target);
break;
}
default: {
Expand Down Expand Up @@ -520,15 +532,27 @@ void Disassembler::DumpBytecode(VMFrame* frame, VMMethod* method, long bc_idx) {
indentc--; ikind='<'; //visual
break;
}
case BC_JUMP_IF_FALSE:
case BC_JUMP_IF_TRUE:
case BC_JUMP: {
int target = 0;
target |= method->GetBytecode(bc_idx + 1);
target |= method->GetBytecode(bc_idx + 2) << 8;
target |= method->GetBytecode(bc_idx + 3) << 16;
target |= method->GetBytecode(bc_idx + 4) << 24;
DebugPrint("(target: %d)\n", target);
case BC_JUMP:
case BC_JUMP_ON_FALSE_POP:
case BC_JUMP_ON_TRUE_POP:
case BC_JUMP_ON_FALSE_TOP_NIL:
case BC_JUMP_ON_TRUE_TOP_NIL:
case BC_JUMP_BACKWARD:
case BC_JUMP2:
case BC_JUMP2_ON_FALSE_POP:
case BC_JUMP2_ON_TRUE_POP:
case BC_JUMP2_ON_FALSE_TOP_NIL:
case BC_JUMP2_ON_TRUE_TOP_NIL:
case BC_JUMP2_BACKWARD: {
uint16_t offset = ComputeOffset(method->GetBytecode(bc_idx + 1), method->GetBytecode(bc_idx + 2));

int32_t target;
if (bc == BC_JUMP_BACKWARD || bc == BC_JUMP2_BACKWARD) {
target = ((int32_t) bc_idx) - offset;
} else {
target = ((int32_t) bc_idx) + offset;
}
DebugPrint("(jump offset: %d -> jump target: %d)", offset, target);
break;
}
default:
Expand Down
Loading