Skip to content

Commit

Permalink
generalize to getLastInstr
Browse files Browse the repository at this point in the history
  • Loading branch information
Pat-Lafon committed Sep 10, 2023
1 parent 1a276e5 commit 67ddbe2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions bril-ts/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,26 @@ export class Builder {
* Checks whether the last emitted instruction in the current function is the specified op code.
* Useful for checking for terminating instructions.
*/
isLastEmittedEffectOp(op: bril.EffectOpCode): boolean {
getLastInstr(): bril.Instruction | undefined {
if (!this.curFunction) {
return false
return undefined
}

if (!this.curFunction.instrs) {
return false
return undefined
}

if (this.curFunction.instrs.length === 0) {
return false
return undefined
}

const last_instr : bril.Instruction | bril.Label = this.curFunction.instrs[this.curFunction.instrs.length - 1];

if ('label' in last_instr) {
return false
return undefined
}

return last_instr.op === op
return last_instr
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ts2bril.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function emitBril(prog: ts.Node, checker: ts.TypeChecker): bril.Program {
// Statement chunks.
builder.buildLabel(thenLab);
emit(if_.thenStatement);
const then_branch_terminated = builder.isLastEmittedEffectOp("ret");
const then_branch_terminated = builder.getLastInstr()?.op === "ret";
if (!then_branch_terminated) {
builder.buildEffect("jmp", [], undefined, [endLab]);
}
Expand Down

0 comments on commit 67ddbe2

Please sign in to comment.