Skip to content

Commit

Permalink
Optimise the compilation by not placing BLOCK_THREAD and GOTO start i…
Browse files Browse the repository at this point in the history
…f has default

If there is a default case, these machine instructions will never be reached.
  • Loading branch information
JothamWong committed Apr 16, 2024
1 parent 979097d commit 93d0e04
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/vm/oogavm-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ const compileComp = {
// we complete the select case on a single successful case so we need to jump to the end
let jumps: number[] = [];
let start: number = wc;
let hasDefault: boolean = false;
for (let i = 0; i < comp.cases.length; i++) {
const compCase = comp.cases[i];
log('Compiling: ' + unparse(compCase));
Expand Down Expand Up @@ -393,18 +394,20 @@ const compileComp = {
jof.addr = wc;
} else if (compCase.tag === 'SelectDefaultCase') {
console.log("Default case");
// default has no checks and is always the last case as per our parser rules
hasDefault = true;
compile(compCase.body, ce);
jumps.push(wc);
instrs[wc++] = { tag: Opcodes.GOTO, addr: 0};
} else {
throw new CompilerError('Unsupported select case in SelectStatement');
}
}
// If none of the cases were handled, this is when we reach this compile section
// If none of the cases were handled and there is no default, this is when we reach this compile section
// we will push a BLOCK_THREAD instruction followed by a GOTO to the start of the case
instrs[wc++] = { tag: Opcodes.BLOCK_THREAD };
instrs[wc++] = { tag: Opcodes.GOTO, addr: start };
if (!hasDefault) {
instrs[wc++] = { tag: Opcodes.BLOCK_THREAD };
instrs[wc++] = { tag: Opcodes.GOTO, addr: start };
}
for (let jumpInstr of jumps) {
instrs[jumpInstr].addr = wc;
}
Expand Down

0 comments on commit 93d0e04

Please sign in to comment.