[hir] Refactor HIRBuilder.build to use HIRBuilder.terminate
It's a bit weird that HIRBuilder is generating terminals, a follow up PR will remove this entirely from HIRBuilder. This is a first step towards that.
Sathya Gunasekaran committed
Apr 11, 2023 at 15:37 UTC
c42cd3f558651426ce4d7986d1b860b1087b8f7f
1 file changed
+9
-12
compiler/forget/src/HIR/HIRBuilder.ts
+9
-12
@@ -257,20 +257,15 @@ export default class HIRBuilder {
257
* Construct a final CFG from this context
258
*/
259
build(): HIR {
260
- const { id: blockId, instructions } = this.#current;
261
- this.#completed.set(blockId, {
262
- kind: "block",
263
- id: blockId,
264
- instructions,
265
- terminal: {
260
+ this.terminate(
261
+ {
262
kind: "return",
263
loc: GeneratedSource,
264
value: null,
265
id: makeInstructionId(0),
266
},
271
- preds: new Set(),
272
- phis: new Set(),
273
- });
267
+ null
268
+ );
269
let ir: HIR = {
270
blocks: this.#completed,
271
entry: this.#entry,
@@ -293,7 +288,7 @@ export default class HIRBuilder {
288
/**
289
* Terminate the current block w the given terminal, and start a new block
290
*/
296
- terminate(terminal: Terminal, nextBlockKind: BlockKind): void {
291
+ terminate(terminal: Terminal, nextBlockKind: BlockKind | null): void {
292
const { id: blockId, kind, instructions } = this.#current;
293
this.#completed.set(blockId, {
294
kind,
@@ -303,8 +298,10 @@ export default class HIRBuilder {
298
preds: new Set(),
299
phis: new Set(),
300
});
306
- const nextId = this.#env.nextBlockId;
307
- this.#current = newBlock(nextId, nextBlockKind);
301
+ if (nextBlockKind) {
302
+ const nextId = this.#env.nextBlockId;
303
+ this.#current = newBlock(nextId, nextBlockKind);
304
+ }
305
}
306
307
/**