All calls to lowerExpression lower to a unique temporary
This is more followup toward deleting Instruction.lvalue. The previous PR ensured that all Instruction.lvalue identifiers are only ever assigned to once. Now we ensure that `lowerExpression()` is _only_ called via `lowerExpressionToTemporary()`, ie we now always lower every single expression to a temporary. This will make it easier to make lvalue a part of the InstructionValue instead of the instruction itself, in follow-up PRs.
Joe Savona committed
Mar 3, 2023 at 17:09 UTC
8233e8f95a14ddc6c92c757a2f0d0c35a1eceeee
1 file changed
+9
-54
compiler/forget/src/HIR/BuildHIR.ts
+9
-54
@@ -313,7 +313,7 @@ function lowerStatement(
313
});
314
return { kind: "unsupported", id: makeInstructionId(0) };
315
}
316
- lowerExpressionToVoid(builder, update as NodePath<t.Expression>);
316
+ lowerExpressionToTemporary(builder, update as NodePath<t.Expression>);
317
return {
318
kind: "goto",
319
block: testBlock.id,
@@ -639,21 +639,7 @@ function lowerStatement(
639
case "ExpressionStatement": {
640
const stmt = stmtPath as NodePath<t.ExpressionStatement>;
641
const expression = stmt.get("expression");
642
- const value = lowerExpression(builder, expression);
643
- if (expression.isAssignmentExpression() && value.kind === "LoadLocal") {
644
- // already lowered to a place
645
- return;
646
- }
647
- const place = buildTemporaryPlace(
648
- builder,
649
- stmt.node.loc ?? GeneratedSource
650
- );
651
- builder.push({
652
- id: makeInstructionId(0),
653
- lvalue: place,
654
- value,
655
- loc: stmt.node.loc ?? GeneratedSource,
656
- });
642
+ lowerExpressionToTemporary(builder, expression);
643
return;
644
}
645
case "DoWhileStatement": {
@@ -1171,10 +1157,15 @@ function lowerExpression(
1157
},
1158
testBlock
1159
);
1160
+ const leftValue = lowerExpressionToTemporary(builder, expr.get("left"));
1161
builder.push({
1162
id: makeInstructionId(0),
1163
lvalue: { ...leftPlace },
1177
- value: lowerExpression(builder, expr.get("left")),
1164
+ value: {
1165
+ kind: "LoadLocal",
1166
+ place: leftValue,
1167
+ loc: exprLoc,
1168
+ },
1169
loc: exprLoc,
1170
});
1171
builder.terminateWithContinuation(
@@ -1195,7 +1186,7 @@ function lowerExpression(
1186
1187
if (builder.currentBlockKind() === "value") {
1188
// try lowering the RHS in case it also contains errors
1198
- lowerExpression(builder, expr.get("right"));
1189
+ lowerExpressionToTemporary(builder, expr.get("right"));
1190
builder.errors.push({
1191
reason: `(BuildHIR::lowerExpression) Handle AssignmentExpression within a LogicalExpression or ConditionalExpression`,
1192
severity: ErrorSeverity.Todo,
@@ -1853,42 +1844,6 @@ function lowerExpressionToTemporary(
1844
return place;
1845
}
1846
1856
-function lowerExpressionToPlace(
1857
- builder: HIRBuilder,
1858
- exprPath: NodePath<t.Expression>
1859
-): Place {
1860
- const value = lowerExpression(builder, exprPath);
1861
- if (value.kind === "LoadLocal") {
1862
- return value.place;
1863
- }
1864
- const exprLoc = exprPath.node.loc ?? GeneratedSource;
1865
- const place: Place = buildTemporaryPlace(builder, exprLoc);
1866
- builder.push({
1867
- id: makeInstructionId(0),
1868
- value: value,
1869
- loc: exprLoc,
1870
- lvalue: { ...place },
1871
- });
1872
- return place;
1873
-}
1874
-
1875
-/**
1876
- * Lowers an expression to an instruction with no lvalue
1877
- */
1878
-function lowerExpressionToVoid(
1879
- builder: HIRBuilder,
1880
- exprPath: NodePath<t.Expression>
1881
-): void {
1882
- const instr = lowerExpression(builder, exprPath);
1883
- const exprLoc = exprPath.node.loc ?? GeneratedSource;
1884
- builder.push({
1885
- id: makeInstructionId(0),
1886
- value: instr,
1887
- loc: exprLoc,
1888
- lvalue: buildTemporaryPlace(builder, exprLoc),
1889
- });
1890
-}
1891
-
1847
function lowerIdentifier(
1848
builder: HIRBuilder,
1849
exprPath: NodePath<t.Identifier | t.JSXIdentifier>