Skip to content

Commit

Permalink
Stop emitting a SET and POP opcode right before a RETURN opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
john-z-yang committed Dec 10, 2023
1 parent e94c63b commit 1ffa8c0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
40 changes: 20 additions & 20 deletions docs/opcodes.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# Opcodes

| Opcode | Description |
| ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **MAKE_CLOSURE** _i (IS_LOCAL j)*_ | Push a ClosureAtom onto the stack, created from the FnAtom at `const[i]`. For each upvalue in the ClosureAtom, capture it from `stack[j]` if `IS_LOCAL` is `1`, capture from `upvalues[j]` otherwise. |
| **CALL** _argc_ | Call the function at **TOS** - `argc` with `argc` parameters. |
| **RETURN** | Return **TOS**. |
| **POP_TOP** | Pop **TOS**. |
| **POP** _argc_ | Pop **TOS** `argc` times. |
| **CLOSE_UPVALUE** | Close the free variables at **TOS**. |
| **LOAD_CONST** _idx_ | Push `const[idx]` onto the stack. |
| **LOAD_SYM** _idx_ | Push `globals[const[idx]]` onto the stack. |
| **DEF_SYM** _idx_ | Define `globals[const[idx]]` as **TOS**. |
| **SET_SYM** _idx_ | Set `globals[const[idx]]` as **TOS**. |
| **LOAD_UPVALUE** _idx_ | Push `upvalues[idx]` onto the stack. |
| **SET_UPVALUE** _idx_ | Set `upvalues[idx]` as **TOS**. |
| **LOAD_STACK** _idx_ | Push `upvalues[idx]` onto the stack. |
| **SET_STACK** _idx_ | Set `upvalues[idx]` to **TOS**. |
| **JUMP** _offset_ | Set `ip` of current frame to `offset`. |
| **POP_JUMP_IF_FALSE** _offset_ | Set `ip` of current frame to `offset` if **TOS** is not _truthy_. |
| **MAKE_LIST** _offset_ | Pop all elements from **BASE_PTR** + _offset_ to **TOS**, push those elements as cons list onto the stack. |
| **MAKE_NIL** | Push `'()` onto the stack |
| Opcode | Description |
| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **MAKE_CLOSURE** _i (IS_LOCAL j)*_ | Push a Closure onto the stack, created from the Prototype at `const[i]`. For each upvalue in the Closure, capture it from `stack[j]` if `IS_LOCAL` is `1`, capture from `upvalues[j]` otherwise. |
| **CALL** _argc_ | Call the function at **TOS** - `argc` with `argc` parameters. |
| **RETURN** | Return **TOS**. |
| **POP_TOP** | Pop **TOS**. |
| **POP** _argc_ | Pop **TOS** `argc` times. |
| **CLOSE_UPVALUE** | Close the free variables at **TOS**. |
| **LOAD_CONST** _idx_ | Push `const[idx]` onto the stack. |
| **LOAD_SYM** _idx_ | Push `globals[const[idx]]` onto the stack. |
| **DEF_SYM** _idx_ | Define `globals[const[idx]]` as **TOS**. |
| **SET_SYM** _idx_ | Set `globals[const[idx]]` as **TOS**. |
| **LOAD_UPVALUE** _idx_ | Push `upvalues[idx]` onto the stack. |
| **SET_UPVALUE** _idx_ | Set `upvalues[idx]` as **TOS**. |
| **LOAD_STACK** _idx_ | Push `upvalues[idx]` onto the stack. |
| **SET_STACK** _idx_ | Set `upvalues[idx]` to **TOS**. |
| **JUMP** _offset_ | Set `ip` of current frame to `offset`. |
| **POP_JUMP_IF_FALSE** _offset_ | Set `ip` of current frame to `offset` if **TOS** is not _truthy_. |
| **MAKE_LIST** _offset_ | Pop all elements from **BASE_PTR** + _offset_ to **TOS**, push those elements as cons list onto the stack. |
| **MAKE_NIL** | Push `'()` onto the stack |
3 changes: 0 additions & 3 deletions src/compile/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,15 +556,12 @@ void Compiler::emitRet() {
handleTypeError(lambdaGrammar, te.expected, te.actual);
}

emitCode(OpCode::SET_STACK, 0);

for (const auto &local : locals) {
if (local.isCaptured) {
emitCode(OpCode::CLOSE_UPVALUE, local.stackOffset);
}
}

emitCode(OpCode::POP, stackOffset);
emitCode(OpCode::RETURN);
}

Expand Down
6 changes: 4 additions & 2 deletions src/runtime/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const SExpr *VM::exec() {
MAKE_CLOSURE: {
const auto proto = cast<Prototype>(readConst());
std::vector<std::shared_ptr<Upvalue>> upvalues;
for (unsigned int i{0}; i < proto->numUpvals; ++i) {
for (auto i = 0U; i < proto->numUpvals; ++i) {
auto isLocal = readByte();
auto idx = readByte();
if (isLocal == 1) {
Expand All @@ -144,15 +144,17 @@ CALL: { call(readByte()); }
goto *dispatchTable[readByte()];

RETURN: {
const auto res = stack.back();
if (callFrames.size() == 1) [[unlikely]] {
const auto res = stack.back();
reset();
return res;
}
stack.erase(stack.end() - (stack.size() - bp - 1), stack.end());
ip = callFrames.back().ip;
bp = callFrames.back().bp;
closure = callFrames.back().closure;
callFrames.pop_back();
stack.back() = res;
}
goto *dispatchTable[readByte()];

Expand Down

0 comments on commit 1ffa8c0

Please sign in to comment.