Model assignment as value

This PR subtly changes how we represent assignment expressions in order to 
accurately model them _as expressions_. Specifically, the result of lowering an 
assignment is now the temporary created for the assignment's lvalue. This allows 
us to restore the assignment as a value (expression) during codegen. Note how 
this fixes a bug and cleans up some output.
This commit is contained in:
Joe Savona
2023-03-21 10:01:08 -07:00
parent f4c4dcb1b3
commit bf1db812a8
11 changed files with 169 additions and 167 deletions
+1 -1
View File
@@ -2009,7 +2009,7 @@ function lowerAssignment(
},
loc,
});
return { kind: "LoadLocal", place, loc: temporary.loc };
return { kind: "LoadLocal", place: temporary, loc: temporary.loc };
}
case "MemberExpression": {
// This can only occur because of a coding error, parsers enforce this condition
@@ -404,10 +404,13 @@ function codegenInstructionNullable(
}
case InstructionKind.Reassign: {
invariant(value !== null, "Expected a value for reassignment");
return createExpressionStatement(
instr.loc,
t.assignmentExpression("=", codegenLValue(lvalue), value)
);
const expr = t.assignmentExpression("=", codegenLValue(lvalue), value);
if (instr.lvalue !== null) {
cx.temp.set(instr.lvalue.identifier.id, expr);
return null;
} else {
return createExpressionStatement(instr.loc, expr);
}
}
default: {
assertExhaustive(kind, `Unexpected instruction kind '${kind}'`);
@@ -13,8 +13,8 @@ function f(y) {
```javascript
function f(y) {
const x = y;
return x + 2 + 2;
let x = y;
return x + (x = 2) + 2;
}
```
@@ -21,8 +21,7 @@ function Component() {
let x;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
x = foo();
x = bar();
t0 = x(x, 5);
t0 = x((x = bar()), 5);
$[0] = t0;
$[1] = x;
} else {
@@ -16,7 +16,7 @@ function ternary(props) {
function ternary(props) {
let x = undefined;
const y = props.a ? ((x = 1), 1) : ((x = 2), 2);
const y = props.a ? (x = 1) : (x = 2);
return x + y;
}
@@ -17,45 +17,45 @@ function foo(props) {
```javascript
bb0 (block):
[1] store $35[1:33] = Array []
[2] store $37[2:33] = StoreLocal Let mutate x$36[2:33] = capture $35[1:33]
[3] mutate $38[3:33] = LoadLocal capture x$36[2:33]
[4] mutate $39 = LoadLocal read props$34
[5] mutate $40 = PropertyLoad read $39.bar
[6] mutate $41 = PropertyCall mutate $38[3:33].push(read $40)
[1] store $33[1:31] = Array []
[2] store $35[2:31] = StoreLocal Let mutate x$34[2:31] = capture $33[1:31]
[3] mutate $36[3:31] = LoadLocal capture x$34[2:31]
[4] mutate $37 = LoadLocal read props$32
[5] mutate $38 = PropertyLoad read $37.bar
[6] mutate $39 = PropertyCall mutate $36[3:31].push(read $38)
[7] Ternary test:bb2 fallthrough=bb1
bb2 (value):
predecessor blocks: bb0
[8] mutate $42 = LoadLocal read props$34
[9] mutate $43 = PropertyLoad read $42.cond
[10] Branch (read $43) then:bb3 else:bb4
[8] mutate $40 = LoadLocal read props$32
[9] mutate $41 = PropertyLoad read $40.cond
[10] Branch (read $41) then:bb3 else:bb4
bb3 (value):
predecessor blocks: bb2
[14] store $48[14:33] = Array []
[15] store $50[15:33] = StoreLocal Reassign mutate x$36[15:33] = capture $48[14:33]
[17] mutate $52[17:33] = LoadLocal capture x$36[15:33]
[18] mutate $53 = LoadLocal read props$34
[19] mutate $54 = PropertyLoad read $53.foo
[20] mutate $55[20:30] = PropertyCall mutate $52[17:33].push(read $54)
[21] store $57[21:30] = StoreLocal Const mutate $56[7:30] = capture $55[20:30]
[22] Goto bb1
[13] store $45[13:31] = Array []
[14] store $47[14:31] = StoreLocal Reassign mutate x$34[14:31] = capture $45[13:31]
[15] mutate $48[15:31] = LoadLocal capture x$34[14:31]
[16] mutate $49 = LoadLocal read props$32
[17] mutate $50 = PropertyLoad read $49.foo
[18] mutate $51[18:28] = PropertyCall mutate $48[15:31].push(read $50)
[19] store $53[19:28] = StoreLocal Const mutate $52[7:28] = capture $51[18:28]
[20] Goto bb1
bb4 (value):
predecessor blocks: bb2
[23] mutate $58[23:30]:TPrimitive = null
[24] store $60[24:30]:TPrimitive = StoreLocal Const mutate $56[7:30] = read $58[23:30]:TPrimitive
[25] Goto bb1
[21] mutate $54[21:28]:TPrimitive = null
[22] store $56[22:28]:TPrimitive = StoreLocal Const mutate $52[7:28] = read $54[21:28]:TPrimitive
[23] Goto bb1
bb1 (block):
predecessor blocks: bb3 bb4
$63[7:30]:TPhi: phi(bb3: $56, bb4: $59)
x$36[2:33]:TPhi: phi(bb3: x$36, bb4: x$36)
[26] store $62[26:30] = StoreLocal Const mutate _$61[26:30] = capture $56[7:30]
[27] mutate $64 = Global console
[28] mutate $65[28:30] = LoadLocal capture _$61[26:30]
[29] mutate $66 = PropertyCall read $64.log(mutate $65[28:30])
[30] mutate $67:TFunction = Global mut
[31] mutate $68[31:33] = LoadLocal capture x$36[2:33]
[32] mutate $70 = Call read $67:TFunction(mutate $68[31:33])
[33] mutate $71 = LoadLocal capture x$36[2:33]
[34] Return freeze $71
$59[7:28]:TPhi: phi(bb3: $52, bb4: $55)
x$34[2:31]:TPhi: phi(bb3: x$34, bb4: x$34)
[24] store $58[24:28] = StoreLocal Const mutate _$57[24:28] = capture $52[7:28]
[25] mutate $60 = Global console
[26] mutate $61[26:28] = LoadLocal capture _$57[24:28]
[27] mutate $62 = PropertyCall read $60.log(mutate $61[26:28])
[28] mutate $63:TFunction = Global mut
[29] mutate $64[29:31] = LoadLocal capture x$34[2:31]
[30] mutate $66 = Call read $63:TFunction(mutate $64[29:31])
[31] mutate $67 = LoadLocal capture x$34[2:31]
[32] Return freeze $67
```
@@ -16,42 +16,42 @@ function foo(props) {
```javascript
bb0 (block):
[1] store $32[1:7] = Array []
[2] store $34[2:7] = StoreLocal Let mutate x$33[2:7] = capture $32[1:7]
[3] mutate $35[3:7] = LoadLocal capture x$33[2:7]
[4] mutate $36 = LoadLocal read props$31
[5] mutate $37 = PropertyLoad read $36.bar
[6] mutate $38 = PropertyCall mutate $35[3:7].push(read $37)
[1] store $30[1:7] = Array []
[2] store $32[2:7] = StoreLocal Let mutate x$31[2:7] = capture $30[1:7]
[3] mutate $33[3:7] = LoadLocal capture x$31[2:7]
[4] mutate $34 = LoadLocal read props$29
[5] mutate $35 = PropertyLoad read $34.bar
[6] mutate $36 = PropertyCall mutate $33[3:7].push(read $35)
[7] Ternary test:bb2 fallthrough=bb1
bb2 (value):
predecessor blocks: bb0
[8] mutate $39 = LoadLocal read props$31
[9] mutate $40 = PropertyLoad read $39.cond
[10] Branch (read $40) then:bb3 else:bb4
[8] mutate $37 = LoadLocal read props$29
[9] mutate $38 = PropertyLoad read $37.cond
[10] Branch (read $38) then:bb3 else:bb4
bb3 (value):
predecessor blocks: bb2
[14] store $45[14:21] = Array []
[15] store $47[15:21] = StoreLocal Reassign mutate x$33[15:21] = capture $45[14:21]
[17] mutate $49[17:21] = LoadLocal capture x$33[15:21]
[18] mutate $50 = LoadLocal read props$31
[19] mutate $51 = PropertyLoad read $50.foo
[20] mutate $52[20:30] = PropertyCall mutate $49[17:21].push(read $51)
[21] store $54[21:30] = StoreLocal Const mutate $53[7:30] = capture $52[20:30]
[22] Goto bb1
[13] store $42[13:19] = Array []
[14] store $44[14:19] = StoreLocal Reassign mutate x$31[14:19] = capture $42[13:19]
[15] mutate $45[15:19] = LoadLocal capture x$31[14:19]
[16] mutate $46 = LoadLocal read props$29
[17] mutate $47 = PropertyLoad read $46.foo
[18] mutate $48[18:28] = PropertyCall mutate $45[15:19].push(read $47)
[19] store $50[19:28] = StoreLocal Const mutate $49[7:28] = capture $48[18:28]
[20] Goto bb1
bb4 (value):
predecessor blocks: bb2
[23] mutate $55[23:30]:TPrimitive = null
[24] store $57[24:30]:TPrimitive = StoreLocal Const mutate $53[7:30] = read $55[23:30]:TPrimitive
[25] Goto bb1
[21] mutate $51[21:28]:TPrimitive = null
[22] store $53[22:28]:TPrimitive = StoreLocal Const mutate $49[7:28] = read $51[21:28]:TPrimitive
[23] Goto bb1
bb1 (block):
predecessor blocks: bb3 bb4
$60[7:30]:TPhi: phi(bb3: $53, bb4: $56)
x$33:TPhi: phi(bb3: x$33, bb4: x$33)
[26] store $59[26:30] = StoreLocal Const mutate _$58[26:30] = capture $53[7:30]
[27] mutate $61 = Global console
[28] mutate $62[28:30] = LoadLocal capture _$58[26:30]
[29] mutate $63 = PropertyCall read $61.log(mutate $62[28:30])
[30] mutate $64 = LoadLocal capture x$33
[31] Return freeze $64
$56[7:28]:TPhi: phi(bb3: $49, bb4: $52)
x$31:TPhi: phi(bb3: x$31, bb4: x$31)
[24] store $55[24:28] = StoreLocal Const mutate _$54[24:28] = capture $49[7:28]
[25] mutate $57 = Global console
[26] mutate $58[26:28] = LoadLocal capture _$54[24:28]
[27] mutate $59 = PropertyCall read $57.log(mutate $58[26:28])
[28] mutate $60 = LoadLocal capture x$31
[29] Return freeze $60
```
@@ -19,50 +19,50 @@ function foo(props) {
```javascript
bb0 (block):
[1] store $44[1:7] = Array []
[2] store $46[2:7] = StoreLocal Let mutate x$45[2:42] = capture $44[1:7]
[3] mutate $47[3:7] = LoadLocal capture x$45[2:42]
[4] mutate $48 = LoadLocal read props$43
[5] mutate $49 = PropertyLoad read $48.bar
[6] mutate $50 = PropertyCall mutate $47[3:7].push(read $49)
[1] store $40[1:7] = Array []
[2] store $42[2:7] = StoreLocal Let mutate x$41[2:38] = capture $40[1:7]
[3] mutate $43[3:7] = LoadLocal capture x$41[2:38]
[4] mutate $44 = LoadLocal read props$39
[5] mutate $45 = PropertyLoad read $44.bar
[6] mutate $46 = PropertyCall mutate $43[3:7].push(read $45)
[7] Ternary test:bb2 fallthrough=bb1
bb2 (value):
predecessor blocks: bb0
[8] mutate $51 = LoadLocal read props$43
[9] mutate $52 = PropertyLoad read $51.cond
[10] Branch (read $52) then:bb3 else:bb4
[8] mutate $47 = LoadLocal read props$39
[9] mutate $48 = PropertyLoad read $47.cond
[10] Branch (read $48) then:bb3 else:bb4
bb3 (value):
predecessor blocks: bb2
[14] store $57[14:42] = Array []
[15] store $59[15:42] = StoreLocal Reassign mutate x$45[15:42] = capture $57[14:42]
[17] mutate $61[17:42] = LoadLocal capture x$45[15:42]
[18] mutate $62 = LoadLocal read props$43
[19] mutate $63 = PropertyLoad read $62.foo
[20] mutate $64[20:39] = PropertyCall mutate $61[17:42].push(read $63)
[21] store $66[21:39] = StoreLocal Const mutate $65[7:39] = capture $64[20:39]
[22] Goto bb1
[13] store $52[13:38] = Array []
[14] store $54[14:38] = StoreLocal Reassign mutate x$41[14:38] = capture $52[13:38]
[15] mutate $55[15:38] = LoadLocal capture x$41[14:38]
[16] mutate $56 = LoadLocal read props$39
[17] mutate $57 = PropertyLoad read $56.foo
[18] mutate $58[18:35] = PropertyCall mutate $55[15:38].push(read $57)
[19] store $60[19:35] = StoreLocal Const mutate $59[7:35] = capture $58[18:35]
[20] Goto bb1
bb4 (value):
predecessor blocks: bb2
[26] store $71[26:42] = Array []
[27] store $73[27:42] = StoreLocal Reassign mutate x$45[27:42] = capture $71[26:42]
[29] mutate $75[29:42] = LoadLocal capture x$45[27:42]
[30] mutate $76 = LoadLocal read props$43
[31] mutate $77 = PropertyLoad read $76.bar
[32] mutate $78[32:39] = PropertyCall mutate $75[29:42].push(read $77)
[33] store $80[33:39] = StoreLocal Const mutate $65[7:39] = capture $78[32:39]
[34] Goto bb1
[23] store $64[23:38] = Array []
[24] store $66[24:38] = StoreLocal Reassign mutate x$41[24:38] = capture $64[23:38]
[25] mutate $67[25:38] = LoadLocal capture x$41[24:38]
[26] mutate $68 = LoadLocal read props$39
[27] mutate $69 = PropertyLoad read $68.bar
[28] mutate $70[28:35] = PropertyCall mutate $67[25:38].push(read $69)
[29] store $72[29:35] = StoreLocal Const mutate $59[7:35] = capture $70[28:35]
[30] Goto bb1
bb1 (block):
predecessor blocks: bb3 bb4
$83[7:39]:TPhi: phi(bb3: $65, bb4: $79)
x$45[15:42]:TPhi: phi(bb3: x$45, bb4: x$45)
[35] store $82[35:39] = StoreLocal Const mutate _$81[35:39] = capture $65[7:39]
[36] mutate $84 = Global console
[37] mutate $85[37:39] = LoadLocal capture _$81[35:39]
[38] mutate $86 = PropertyCall read $84.log(mutate $85[37:39])
[39] mutate $87:TFunction = Global mut
[40] mutate $88[40:42] = LoadLocal capture x$45[15:42]
[41] mutate $90 = Call read $87:TFunction(mutate $88[40:42])
[42] mutate $91 = LoadLocal capture x$45[15:42]
[43] Return freeze $91
$75[7:35]:TPhi: phi(bb3: $59, bb4: $71)
x$41[14:38]:TPhi: phi(bb3: x$41, bb4: x$41)
[31] store $74[31:35] = StoreLocal Const mutate _$73[31:35] = capture $59[7:35]
[32] mutate $76 = Global console
[33] mutate $77[33:35] = LoadLocal capture _$73[31:35]
[34] mutate $78 = PropertyCall read $76.log(mutate $77[33:35])
[35] mutate $79:TFunction = Global mut
[36] mutate $80[36:38] = LoadLocal capture x$41[14:38]
[37] mutate $82 = Call read $79:TFunction(mutate $80[36:38])
[38] mutate $83 = LoadLocal capture x$41[14:38]
[39] Return freeze $83
```
@@ -18,47 +18,47 @@ function foo(props) {
```javascript
bb0 (block):
[1] store $41[1:7] = Array []
[2] store $43[2:7] = StoreLocal Let mutate x$42[2:7] = capture $41[1:7]
[3] mutate $44[3:7] = LoadLocal capture x$42[2:7]
[4] mutate $45 = LoadLocal read props$40
[5] mutate $46 = PropertyLoad read $45.bar
[6] mutate $47 = PropertyCall mutate $44[3:7].push(read $46)
[1] store $37[1:7] = Array []
[2] store $39[2:7] = StoreLocal Let mutate x$38[2:7] = capture $37[1:7]
[3] mutate $40[3:7] = LoadLocal capture x$38[2:7]
[4] mutate $41 = LoadLocal read props$36
[5] mutate $42 = PropertyLoad read $41.bar
[6] mutate $43 = PropertyCall mutate $40[3:7].push(read $42)
[7] Ternary test:bb2 fallthrough=bb1
bb2 (value):
predecessor blocks: bb0
[8] mutate $48 = LoadLocal read props$40
[9] mutate $49 = PropertyLoad read $48.cond
[10] Branch (read $49) then:bb3 else:bb4
[8] mutate $44 = LoadLocal read props$36
[9] mutate $45 = PropertyLoad read $44.cond
[10] Branch (read $45) then:bb3 else:bb4
bb3 (value):
predecessor blocks: bb2
[14] store $54[14:21] = Array []
[15] store $56[15:21] = StoreLocal Reassign mutate x$42[15:21] = capture $54[14:21]
[17] mutate $58[17:21] = LoadLocal capture x$42[15:21]
[18] mutate $59 = LoadLocal read props$40
[19] mutate $60 = PropertyLoad read $59.foo
[20] mutate $61[20:39] = PropertyCall mutate $58[17:21].push(read $60)
[21] store $63[21:39] = StoreLocal Const mutate $62[7:39] = capture $61[20:39]
[22] Goto bb1
[13] store $49[13:19] = Array []
[14] store $51[14:19] = StoreLocal Reassign mutate x$38[14:19] = capture $49[13:19]
[15] mutate $52[15:19] = LoadLocal capture x$38[14:19]
[16] mutate $53 = LoadLocal read props$36
[17] mutate $54 = PropertyLoad read $53.foo
[18] mutate $55[18:35] = PropertyCall mutate $52[15:19].push(read $54)
[19] store $57[19:35] = StoreLocal Const mutate $56[7:35] = capture $55[18:35]
[20] Goto bb1
bb4 (value):
predecessor blocks: bb2
[26] store $68[26:33] = Array []
[27] store $70[27:33] = StoreLocal Reassign mutate x$42[27:33] = capture $68[26:33]
[29] mutate $72[29:33] = LoadLocal capture x$42[27:33]
[30] mutate $73 = LoadLocal read props$40
[31] mutate $74 = PropertyLoad read $73.bar
[32] mutate $75[32:39] = PropertyCall mutate $72[29:33].push(read $74)
[33] store $77[33:39] = StoreLocal Const mutate $62[7:39] = capture $75[32:39]
[34] Goto bb1
[23] store $61[23:29] = Array []
[24] store $63[24:29] = StoreLocal Reassign mutate x$38[24:29] = capture $61[23:29]
[25] mutate $64[25:29] = LoadLocal capture x$38[24:29]
[26] mutate $65 = LoadLocal read props$36
[27] mutate $66 = PropertyLoad read $65.bar
[28] mutate $67[28:35] = PropertyCall mutate $64[25:29].push(read $66)
[29] store $69[29:35] = StoreLocal Const mutate $56[7:35] = capture $67[28:35]
[30] Goto bb1
bb1 (block):
predecessor blocks: bb3 bb4
$80[7:39]:TPhi: phi(bb3: $62, bb4: $76)
x$42:TPhi: phi(bb3: x$42, bb4: x$42)
[35] store $79[35:39] = StoreLocal Const mutate _$78[35:39] = capture $62[7:39]
[36] mutate $81 = Global console
[37] mutate $82[37:39] = LoadLocal capture _$78[35:39]
[38] mutate $83 = PropertyCall read $81.log(mutate $82[37:39])
[39] mutate $84 = LoadLocal capture x$42
[40] Return freeze $84
$72[7:35]:TPhi: phi(bb3: $56, bb4: $68)
x$38:TPhi: phi(bb3: x$38, bb4: x$38)
[31] store $71[31:35] = StoreLocal Const mutate _$70[31:35] = capture $56[7:35]
[32] mutate $73 = Global console
[33] mutate $74[33:35] = LoadLocal capture _$70[31:35]
[34] mutate $75 = PropertyCall read $73.log(mutate $74[33:35])
[35] mutate $76 = LoadLocal capture x$38
[36] Return freeze $76
```
@@ -24,40 +24,40 @@ function foo(props) {
```javascript
bb0 (block):
[1] store $36[1:7] = Array []
[2] store $38[2:7] = StoreLocal Let mutate x$37[2:35] = capture $36[1:7]
[3] mutate $39[3:7] = LoadLocal capture x$37[2:35]
[4] mutate $40 = LoadLocal read props$35
[5] mutate $41 = PropertyLoad read $40.bar
[6] mutate $42 = PropertyCall mutate $39[3:7].push(read $41)
[7] mutate $43 = LoadLocal read props$35
[8] mutate $44 = PropertyLoad read $43.cond
[9] If (read $44) then:bb2 else:bb3 fallthrough=bb1
[1] store $32[1:7] = Array []
[2] store $34[2:7] = StoreLocal Let mutate x$33[2:31] = capture $32[1:7]
[3] mutate $35[3:7] = LoadLocal capture x$33[2:31]
[4] mutate $36 = LoadLocal read props$31
[5] mutate $37 = PropertyLoad read $36.bar
[6] mutate $38 = PropertyCall mutate $35[3:7].push(read $37)
[7] mutate $39 = LoadLocal read props$31
[8] mutate $40 = PropertyLoad read $39.cond
[9] If (read $40) then:bb2 else:bb3 fallthrough=bb1
bb2 (block):
predecessor blocks: bb0
[13] store $49[13:35] = Array []
[14] store $51[14:35] = StoreLocal Reassign mutate x$37[14:35] = capture $49[13:35]
[16] mutate $53[16:35] = LoadLocal capture x$37[14:35]
[17] mutate $54 = LoadLocal read props$35
[18] mutate $55 = PropertyLoad read $54.foo
[19] mutate $56 = PropertyCall mutate $53[16:35].push(read $55)
[20] Goto bb1
[12] store $44[12:31] = Array []
[13] store $46[13:31] = StoreLocal Reassign mutate x$33[13:31] = capture $44[12:31]
[14] mutate $47[14:31] = LoadLocal capture x$33[13:31]
[15] mutate $48 = LoadLocal read props$31
[16] mutate $49 = PropertyLoad read $48.foo
[17] mutate $50 = PropertyCall mutate $47[14:31].push(read $49)
[18] Goto bb1
bb3 (block):
predecessor blocks: bb0
[24] store $61[24:35] = Array []
[25] store $63[25:35] = StoreLocal Reassign mutate x$37[25:35] = capture $61[24:35]
[27] mutate $65[27:35] = LoadLocal capture x$37[25:35]
[28] mutate $66 = LoadLocal read props$35
[29] mutate $67 = PropertyLoad read $66.bar
[30] mutate $68 = PropertyCall mutate $65[27:35].push(read $67)
[31] Goto bb1
[21] store $54[21:31] = Array []
[22] store $56[22:31] = StoreLocal Reassign mutate x$33[22:31] = capture $54[21:31]
[23] mutate $57[23:31] = LoadLocal capture x$33[22:31]
[24] mutate $58 = LoadLocal read props$31
[25] mutate $59 = PropertyLoad read $58.bar
[26] mutate $60 = PropertyCall mutate $57[23:31].push(read $59)
[27] Goto bb1
bb1 (block):
predecessor blocks: bb2 bb3
x$37[14:35]:TPhi: phi(bb2: x$37, bb3: x$37)
[32] mutate $69:TFunction = Global mut
[33] mutate $70[33:35] = LoadLocal capture x$37[14:35]
[34] mutate $72 = Call read $69:TFunction(mutate $70[33:35])
[35] mutate $73 = LoadLocal capture x$37[14:35]
[36] Return freeze $73
x$33[13:31]:TPhi: phi(bb2: x$33, bb3: x$33)
[28] mutate $61:TFunction = Global mut
[29] mutate $62[29:31] = LoadLocal capture x$33[13:31]
[30] mutate $64 = Call read $61:TFunction(mutate $62[29:31])
[31] mutate $65 = LoadLocal capture x$33[13:31]
[32] Return freeze $65
```