Value block final value has an InstrId
Joe Savona committed
Jan 12, 2023 at 09:12 UTC
09246207c7e9ffdee93731b194239b61406497fd
11 files changed
+53
-34
compiler/forget/src/HIR/Codegen.ts
+3
-3
@@ -141,12 +141,12 @@ class CodegenVisitor
141
}
142
leaveValueBlock(
143
block: t.Statement[],
144
- place: t.Expression | null
144
+ place: { value: t.Expression; id: InstructionId } | null
145
): t.Expression {
146
this.depth--;
147
if (block.length === 0) {
148
invariant(place !== null, "Unexpected empty value block");
149
- return place;
149
+ return place.value;
150
}
151
const expressions = block.map((stmt) => {
152
switch (stmt.type) {
@@ -160,7 +160,7 @@ class CodegenVisitor
160
}
161
});
162
if (place !== null) {
163
- expressions.push(place);
163
+ expressions.push(place.value);
164
}
165
return t.sequenceExpression(expressions);
166
}
compiler/forget/src/HIR/HIR.ts
+4
-1
@@ -61,7 +61,10 @@ export type ReactiveBlock = Array<ReactiveStatement>;
61
export type ReactiveValueBlock = {
62
kind: "value-block";
63
instructions: ReactiveBlock;
64
- value: InstructionValue | null;
64
+ last: {
65
+ value: InstructionValue;
66
+ id: InstructionId;
67
+ } | null;
68
};
69
70
export type ReactiveStatement =
compiler/forget/src/HIR/HIRTreeVisitor.ts
+8
-2
@@ -519,7 +519,10 @@ class Driver<
519
}
520
const value =
521
lastValue !== null
522
- ? this.visitor.visitValue(lastValue.value, lastValue.id)
522
+ ? {
523
+ value: this.visitor.visitValue(lastValue.value, lastValue.id),
524
+ id: lastValue.id,
525
+ }
526
: null;
527
return this.visitor.leaveValueBlock(valueBlock, value);
528
}
@@ -873,7 +876,10 @@ export interface Visitor<
876
* Converts the visitor's value block (and final value) to the visitor's
877
* value representation.
878
*/
876
- leaveValueBlock(block: TValueBlock, value: TValue | null): TValue;
879
+ leaveValueBlock(
880
+ block: TValueBlock,
881
+ value: { value: TValue; id: InstructionId } | null
882
+ ): TValue;
883
884
enterInitBlock(block: TBlockBuilder): TValueBlock;
885
compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts
+8
-4
@@ -152,7 +152,7 @@ class ReactiveFunctionBuilder
152
return {
153
kind: "value-block",
154
instructions: [],
155
- value: null,
155
+ last: null,
156
};
157
}
158
appendValueBlock(block: ReactiveValueBlock, item: ReactiveStatement): void {
@@ -160,14 +160,18 @@ class ReactiveFunctionBuilder
160
}
161
leaveValueBlock(
162
block: ReactiveValueBlock,
163
- value: InstructionValue | ReactiveValueBlock | null
163
+ last: {
164
+ value: InstructionValue | ReactiveValueBlock;
165
+ id: InstructionId;
166
+ } | null
167
): InstructionValue | ReactiveValueBlock {
165
- if (value !== null) {
168
+ if (last !== null) {
169
+ const { id, value } = last;
170
invariant(
171
value.kind !== "value-block",
172
"Expected value block to end in a value"
173
);
170
- block.value = value;
174
+ block.last = { id, value };
175
}
176
return block;
177
}
compiler/forget/src/ReactiveScopes/BuildReactiveFunctionWithoutScopes.ts
+9
-9
@@ -19,11 +19,7 @@ import {
19
ReactiveTerminal,
20
ReactiveValueBlock,
21
} from "../HIR/HIR";
22
-import {
23
- BlockTerminal,
24
- Visitor,
25
- visitTreeForReactiveFunction as visitTree,
26
-} from "../HIR/ReactiveFunctionVisitor";
22
+import { BlockTerminal, Visitor, visitTree } from "../HIR/HIRTreeVisitor";
23
import { assertExhaustive } from "../Utils/utils";
24
25
export function buildReactiveFunction(fn: HIRFunction): ReactiveFunction {
@@ -86,7 +82,7 @@ class ReactiveFunctionBuilder
82
return {
83
kind: "value-block",
84
instructions: [],
89
- value: null,
85
+ last: null,
86
};
87
}
88
appendValueBlock(block: ReactiveValueBlock, item: ReactiveStatement): void {
@@ -94,14 +90,18 @@ class ReactiveFunctionBuilder
90
}
91
leaveValueBlock(
92
block: ReactiveValueBlock,
97
- value: InstructionValue | ReactiveValueBlock | null
93
+ last: {
94
+ value: InstructionValue | ReactiveValueBlock;
95
+ id: InstructionId;
96
+ } | null
97
): InstructionValue | ReactiveValueBlock {
99
- if (value !== null) {
98
+ if (last !== null) {
99
+ const { id, value } = last;
100
invariant(
101
value.kind !== "value-block",
102
"Expected value block to end in a value"
103
);
104
- block.value = value;
104
+ block.last = { id, value };
105
}
106
return block;
107
}
compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts
+4
-4
@@ -331,12 +331,12 @@ function codegenForInit(
331
init: ReactiveValueBlock
332
): t.Expression | t.VariableDeclaration | null {
333
const body = codegenBlock(cx, init.instructions).body;
334
- if (init.value !== null) {
334
+ if (init.last !== null) {
335
invariant(
336
body.length === 0,
337
"Expected for init block to produce only temporaries"
338
);
339
- return codegenInstructionValue(cx.temp, init.value);
339
+ return codegenInstructionValue(cx.temp, init.last.value);
340
} else {
341
invariant(
342
body.length === 1,
@@ -363,8 +363,8 @@ function codegenValueBlock(
363
todoInvariant(false, `Handle conversion of ${stmt.type} to expression`);
364
}
365
});
366
- if (block.value !== null) {
367
- const value = codegenInstructionValue(cx.temp, block.value);
366
+ if (block.last !== null) {
367
+ const value = codegenInstructionValue(cx.temp, block.last.value);
368
expressions.push(value);
369
}
370
invariant(
compiler/forget/src/ReactiveScopes/InferReactiveScopes.ts
+9
-3
@@ -268,7 +268,10 @@ class MergeOverlappingReactiveScopesVisitor
268
leaveInitBlock(block: void): void {
269
this.leaveBlock();
270
}
271
- leaveValueBlock(block: void, value: void): void {
271
+ leaveValueBlock(
272
+ block: void,
273
+ value: { value: void; id: InstructionId } | null
274
+ ): void {
275
this.leaveBlock();
276
}
277
visitValue(value: InstructionValue, id: InstructionId): void {
@@ -389,7 +392,10 @@ class AlignReactiveScopesToBlockScopeRangeVisitor
392
this.blockScopes.push({ kind: "value", scopes: [] });
393
}
394
appendValueBlock(block: void, item: void): void {}
392
- leaveValueBlock(block: void, value: void): void {
395
+ leaveValueBlock(
396
+ block: void,
397
+ value: { value: void; id: InstructionId } | null
398
+ ): void {
399
const lastScope = this.blockScopes.pop();
400
invariant(
401
lastScope !== undefined && lastScope.kind === "value",
@@ -409,7 +415,7 @@ class AlignReactiveScopesToBlockScopeRangeVisitor
415
}
416
appendInitBlock(block: void, item: void): void {}
417
leaveInitBlock(block: void): void {
412
- this.leaveValueBlock(block);
418
+ this.leaveValueBlock(block, null);
419
}
420
421
visitInstruction(instruction: Instruction, value: void): void {
compiler/forget/src/ReactiveScopes/PrintReactiveFunction.ts
+2
-2
@@ -104,8 +104,8 @@ function printValueBlock(writer: Writer, block: ReactiveValueBlock): void {
104
for (const instr of block.instructions) {
105
printReactiveInstruction(writer, instr);
106
}
107
- if (block.value !== null) {
108
- writer.writeLine(printInstructionValue(block.value));
107
+ if (block.last !== null) {
108
+ writer.writeLine(printInstructionValue(block.last.value));
109
}
110
});
111
}
compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts
+2
-2
@@ -260,8 +260,8 @@ function visitValueBlock(context: Context, block: ReactiveValueBlock): void {
260
visitInstruction(context, initItem.instruction);
261
}
262
}
263
- if (block.value !== null) {
264
- visitInstructionValue(context, block.value, null);
263
+ if (block.last !== null) {
264
+ visitInstructionValue(context, block.last.value, null);
265
}
266
}
267
compiler/forget/src/ReactiveScopes/RenameVariables.ts
+2
-2
@@ -83,8 +83,8 @@ function visitValueBlock(scopes: Scopes, block: ReactiveValueBlock): void {
83
scopes.visit(stmt.instruction.lvalue.place.identifier);
84
}
85
}
86
- if (block.value !== null) {
87
- for (const operand of eachInstructionValueOperand(block.value)) {
86
+ if (block.last !== null) {
87
+ for (const operand of eachInstructionValueOperand(block.last.value)) {
88
scopes.visit(operand.identifier);
89
}
90
}
compiler/forget/src/ReactiveScopes/visitors.ts
+2
-2
@@ -74,8 +74,8 @@ export function visitFunction(
74
}
75
function visitValueBlock(block: ReactiveValueBlock): void {
76
visitBlock(block.instructions);
77
- if (block.value !== null && visitValue) {
78
- visitValue(block.value);
77
+ if (block.last !== null && visitValue) {
78
+ visitValue(block.last.value);
79
}
80
}
81
visitBlock(fn.body);