Skip to content

Commit

Permalink
Implement PUSH_SELF|ARG_1|ARG_2
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <git@stefan-marr.de>
  • Loading branch information
smarr committed Jul 13, 2024
1 parent 767ef1a commit c62eb0c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 25 deletions.
19 changes: 19 additions & 0 deletions src/compiler/BytecodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ void BytecodeGenerator::EmitPUSHLOCAL(MethodGenerationContext* mgenc, long idx,

void BytecodeGenerator::EmitPUSHARGUMENT(MethodGenerationContext* mgenc,
long idx, int ctx) {
assert(idx >= 0);
assert(ctx >= 0);

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

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

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

Expand Down
6 changes: 3 additions & 3 deletions src/compiler/MethodGenerationContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ uint8_t MethodGenerationContext::ComputeStackDepth() {
i++;
break;
case BC_DUP:
depth++;
i++;
break;
case BC_PUSH_LOCAL_0:
case BC_PUSH_LOCAL_1:
case BC_PUSH_LOCAL_2:
case BC_PUSH_SELF:
case BC_PUSH_ARG_1:
case BC_PUSH_ARG_2:
depth++;
i += 1;
break;
Expand Down
25 changes: 23 additions & 2 deletions src/interpreter/InterpreterLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,32 @@ vm_oop_t Start() {
DISPATCH_NOGC();

LABEL_BC_PUSH_ARGUMENT:
PROLOGUE(3);
doPushArgument(bytecodeIndexGlobal - 3);
DISPATCH_NOGC();

LABEL_BC_PUSH_SELF:
PROLOGUE(1);
{
vm_oop_t argument = GetFrame()->GetArgumentInCurrentContext(0);
GetFrame()->Push(argument);
}
DISPATCH_NOGC();

LABEL_BC_PUSH_ARG_1:
PROLOGUE(1);
{
vm_oop_t argument = GetFrame()->GetArgumentInCurrentContext(1);
GetFrame()->Push(argument);
}
DISPATCH_NOGC();

LABEL_BC_PUSH_ARG_2:
PROLOGUE(3);
doPushArgument(bytecodeIndexGlobal - 3);
PROLOGUE(1);
{
vm_oop_t argument = GetFrame()->GetArgumentInCurrentContext(2);
GetFrame()->Push(argument);
}
DISPATCH_NOGC();

LABEL_BC_PUSH_FIELD:
Expand Down
54 changes: 34 additions & 20 deletions src/unitTests/BytecodeGenerationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ std::vector<uint8_t> BytecodeGenerationTest::blockToBytecode(const char* source,
void BytecodeGenerationTest::testEmptyMethodReturnsSelf() {
auto bytecodes = methodToBytecode("test = ( )");

CPPUNIT_ASSERT_EQUAL((size_t) 4, bytecodes.size());

check(bytecodes, {
BC_PUSH_ARGUMENT, 0, 0,
BC_PUSH_SELF,
BC_RETURN_LOCAL});
}

Expand All @@ -95,7 +93,7 @@ void BytecodeGenerationTest::testPushConstant() {
BC_PUSH_CONSTANT_0, BC_POP,
BC_PUSH_CONSTANT_1, BC_POP,
BC_PUSH_CONSTANT_2, BC_POP,
BC_PUSH_ARGUMENT, 0, 0,
BC_PUSH_SELF,
BC_RETURN_LOCAL
});
}
Expand All @@ -116,7 +114,7 @@ void BytecodeGenerationTest::testIfPushConstantSame() {
BC_PUSH_BLOCK, 5,
BC_SEND, 6,
BC_POP,
BC_PUSH_ARGUMENT, 0, 0,
BC_PUSH_SELF,
BC_RETURN_LOCAL
});
}
Expand All @@ -136,7 +134,7 @@ void BytecodeGenerationTest::testIfPushConstantDifferent() {
BC_PUSH_BLOCK, 5,
BC_SEND, 6,
BC_POP,
BC_PUSH_ARGUMENT, 0, 0,
BC_PUSH_SELF,
BC_RETURN_LOCAL
});
}
Expand All @@ -145,7 +143,7 @@ void BytecodeGenerationTest::testExplicitReturnSelf() {
auto bytecodes = methodToBytecode("test = ( ^ self )");

check(bytecodes, {
BC_PUSH_ARGUMENT, 0, 0,
BC_PUSH_SELF,
BC_RETURN_LOCAL
});
}
Expand All @@ -158,7 +156,7 @@ void BytecodeGenerationTest::testDupPopArgumentPop() {
BC_DUP,
BC_POP_ARGUMENT, 1, 0,
BC_POP,
BC_PUSH_ARGUMENT, 0, 0,
BC_PUSH_SELF,
BC_RETURN_LOCAL
});
}
Expand All @@ -171,7 +169,7 @@ void BytecodeGenerationTest::testDupPopArgumentPopImplicitReturnSelf() {
BC_DUP,
BC_POP_ARGUMENT, 1, 0,
BC_POP,
BC_PUSH_ARGUMENT, 0, 0,
BC_PUSH_SELF,
BC_RETURN_LOCAL
});
}
Expand All @@ -184,7 +182,7 @@ void BytecodeGenerationTest::testDupPopLocalPop() {
BC_DUP,
BC_POP_LOCAL, 0, 0,
BC_POP,
BC_PUSH_ARGUMENT, 0, 0,
BC_PUSH_SELF,
BC_RETURN_LOCAL
});
}
Expand All @@ -198,7 +196,7 @@ void BytecodeGenerationTest::testDupPopField0Pop() {
BC_DUP,
BC_POP_FIELD, 0,
BC_POP,
BC_PUSH_ARGUMENT, 0, 0,
BC_PUSH_SELF,
BC_RETURN_LOCAL
});
}
Expand All @@ -216,7 +214,7 @@ void BytecodeGenerationTest::testDupPopFieldPop() {
BC_DUP,
BC_POP_FIELD, 4,
BC_POP,
BC_PUSH_ARGUMENT, 0, 0,
BC_PUSH_SELF,
BC_RETURN_LOCAL
});
}
Expand All @@ -226,11 +224,11 @@ void BytecodeGenerationTest::testDupPopFieldReturnSelf() {
auto bytecodes = methodToBytecode("test: val = ( field := val )");

check(bytecodes, {
BC_PUSH_ARGUMENT, 1, 0,
BC_PUSH_ARG_1,
BC_DUP,
BC_POP_FIELD, 0,
BC_POP,
BC_PUSH_ARGUMENT, 0, 0,
BC_PUSH_SELF,
BC_RETURN_LOCAL
});
}
Expand All @@ -245,11 +243,11 @@ void BytecodeGenerationTest::testDupPopFieldNReturnSelf() {
auto bytecodes = methodToBytecode("test: val = ( field := val )");

check(bytecodes, {
BC_PUSH_ARGUMENT, 1, 0,
BC_PUSH_ARG_1,
BC_DUP,
BC_POP_FIELD, 5,
BC_POP,
BC_PUSH_ARGUMENT, 0, 0,
BC_PUSH_SELF,
BC_RETURN_LOCAL
});
}
Expand All @@ -259,7 +257,7 @@ void BytecodeGenerationTest::testSendDupPopFieldReturnLocal() {
auto bytecodes = methodToBytecode("test = ( ^ field := self method )");

check(bytecodes, {
BC_PUSH_ARGUMENT, 1, 0,
BC_PUSH_SELF,
BC_SEND, 0,
BC_DUP,
BC_POP_FIELD, 0,
Expand All @@ -272,7 +270,7 @@ void BytecodeGenerationTest::testSendDupPopFieldReturnLocalPeriod() {
auto bytecodes = methodToBytecode("test = ( ^ field := self method. )");

check(bytecodes, {
BC_PUSH_ARGUMENT, 1, 0,
BC_PUSH_SELF,
BC_SEND, 0,
BC_DUP,
BC_POP_FIELD, 0,
Expand All @@ -289,7 +287,7 @@ void BytecodeGenerationTest::testBlockDupPopArgumentPopReturnArg() {
BC_DUP,
BC_POP_ARGUMENT, 1, 0,
BC_POP,
BC_PUSH_ARGUMENT, 1, 0,
BC_PUSH_ARG_1,
BC_RETURN_LOCAL
});
}
Expand Down Expand Up @@ -358,7 +356,23 @@ void BytecodeGenerationTest::testPushLocalOpt() {
BC_PUSH_LOCAL_2, BC_POP,
BC_PUSH_LOCAL, 3, 0,
BC_POP,
BC_PUSH_ARGUMENT, 0, 0,
BC_PUSH_SELF,
BC_RETURN_LOCAL
});
}

void BytecodeGenerationTest::testPushArgOpt() {
auto bytecodes = methodToBytecode(R"""(
test: a1 and: a2 and: a3 and: a4 = (
self. a1. a2. a3. a4.
) )""");
check(bytecodes, {
BC_PUSH_SELF, BC_POP,
BC_PUSH_ARG_1, BC_POP,
BC_PUSH_ARG_2, BC_POP,
BC_PUSH_ARGUMENT, 3, 0, BC_POP,
BC_PUSH_ARGUMENT, 4, 0, BC_POP,
BC_PUSH_SELF,
BC_RETURN_LOCAL
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/unitTests/BytecodeGenerationTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class BytecodeGenerationTest: public CPPUNIT_NS::TestCase {
CPPUNIT_TEST(testBlockDupPopFieldReturnLocal);
CPPUNIT_TEST(testBlockDupPopFieldReturnLocalDot);
CPPUNIT_TEST(testPushLocalOpt);
CPPUNIT_TEST(testPushArgOpt);

CPPUNIT_TEST_SUITE_END();

Expand Down Expand Up @@ -86,6 +87,7 @@ class BytecodeGenerationTest: public CPPUNIT_NS::TestCase {
void testBlockDupPopFieldReturnLocalDot();

void testPushLocalOpt();
void testPushArgOpt();

void dump(MethodGenerationContext* mgenc);

Expand Down
4 changes: 4 additions & 0 deletions src/vmobjects/VMFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ class VMFrame: public VMObject {
return load_ptr(context->arguments[index]);
}

inline vm_oop_t GetArgumentInCurrentContext(long index) {
return load_ptr(this->arguments[index]);
}

void SetArgument(long, long, vm_oop_t);
void PrintStackTrace() const;
long ArgumentStackIndex(long index) const;
Expand Down

0 comments on commit c62eb0c

Please sign in to comment.