-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLuaDecompMulti.inc
More file actions
2545 lines (2396 loc) · 99.6 KB
/
Copy pathLuaDecompMulti.inc
File metadata and controls
2545 lines (2396 loc) · 99.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ ======================================================================
LuaDecompMulti.inc - Multi-assignment pattern detection and emission.
DESIGN INVARIANT (SSA-first):
- All local/scope/naming decisions use node-based functions ONLY:
NodeIsEmittableLocal, NodeIsLocal, NodeNeedsDecl, NodeLocalName,
NodeMarkDeclared, DefNodeFor(Operand), TempName(Reg).
- NodeIsEmittableLocal filters out internal names ((for index),
implicit arg, etc.) - replaces the old IsUserLocal filtering
without any PC scanning.
- ZERO calls to PC-based heuristics:
NO IsUserLocal, NO ShouldEmitAsLocal, NO NeedsLocalDecl,
NO LocalNameForEmit, NO LocalName.
- Each LHS slot tracks an "authority node" (the SSA node defining
that LHS register) for naming and declaration decisions.
- Declaration guard: NodeNeedsDecl only when NodeIsEmittableLocal
is True. Never emit 'local' for non-emittable names.
- SSA_NOP nodes are skipped (Continue) in all scan loops.
- Four detectors return TMultiAssignResult (maNotMatched/maEmitted).
EmitTempLoweredMultiAssign provides a fallback for alias-unsafe groups.
- MultiAssignNeedsTemps detects LHS-RHS aliasing, side effects, and
metamethod risk to decide direct vs temp-lowered emission.
====================================================================== }
function TDecompiler.TryEmitMultiLocal(BIdx, StartIdx: Integer; out ConsumedCount: Integer): Boolean;
{ Detect patterns like:
LOADK R5 (=0) --> v
LOADNIL R6 --> w
where both define user-locals, and the last N instructions are LOADNIL.
Emits: local v, w = 0 (trailing nil stripped) }
var
Blk: TBasicBlock;
N, First, Next: PSSANode;
I, Count, BaseReg: Integer;
Names, Values: AnsiString;
TrailingNils: Integer;
begin
Result := False;
ConsumedCount := 0;
Blk := FSF^.Blocks[BIdx];
if StartIdx + 1 >= Length(Blk.Nodes) then Exit;
First := Blk.Nodes[StartIdx];
{ Skip SSA_NOP (Lua 5.4+ metamethod hints) }
if First^.Kind = SSA_NOP then Exit;
{ First node must be a non-nil value-defining instruction for a user-local }
if not (First^.Kind in [SSA_LOADK, SSA_LOADBOOL, SSA_MOVE, SSA_GETGLOBAL,
SSA_GETTABLE, SSA_GETUPVAL, SSA_BINOP, SSA_UNOP,
SSA_CONCAT]) then Exit;
BaseReg := First^.Dest.Reg;
if not NodeIsEmittableLocal(First) then Exit;
{ The next node must be LOADNIL to the next register, also a user-local.
This ensures we only fire when there's a trailing nil to strip. }
Next := Blk.Nodes[StartIdx + 1];
if Next^.Kind <> SSA_LOADNIL then Exit;
if Next^.Dest.Reg <> BaseReg + 1 then Exit;
if not NodeIsEmittableLocal(Next) then Exit;
{ Scan forward to collect all consecutive locals }
Count := 2;
I := StartIdx + 2;
while I <= High(Blk.Nodes) do begin
N := Blk.Nodes[I];
if N^.Kind = SSA_NOP then begin Inc(I); Continue; end; { skip metamethod hints }
if N^.Dest.Reg <> BaseReg + Count then Break;
if not (N^.Kind in [SSA_LOADK, SSA_LOADBOOL, SSA_LOADNIL, SSA_MOVE,
SSA_GETGLOBAL, SSA_GETTABLE, SSA_GETUPVAL,
SSA_BINOP, SSA_UNOP, SSA_CONCAT]) then Break;
if not NodeIsEmittableLocal(N) then Break;
Inc(Count);
Inc(I);
end;
{ Count trailing LOADNILs }
TrailingNils := 0;
for I := StartIdx + Count - 1 downto StartIdx do begin
if Blk.Nodes[I]^.Kind = SSA_LOADNIL then
Inc(TrailingNils)
else
Break;
end;
if TrailingNils = 0 then Exit;
{ Build the declaration }
Names := '';
Values := '';
for I := 0 to Count - 1 do begin
N := Blk.Nodes[StartIdx + I];
if Names <> '' then Names := Names + ', ';
Names := Names + NodeLocalName(N);
NodeMarkDeclared(N); { mark as declared }
{ Only add value if not a trailing nil }
if I < Count - TrailingNils then begin
if Values <> '' then Values := Values + ', ';
Values := Values + NodeExpr(N);
end;
end;
if Values <> '' then
EmitLine('local ' + Names + ' = ' + Values)
else
EmitLine('local ' + Names);
ConsumedCount := Count;
Result := True;
end;
{ ======================================================================
TryEmitMultiAssign - detect multiple assignment patterns like:
local a, b, c = 1, 2, 3
Compiled as consecutive LOADKs to consecutive registers R, R+1, R+2
where all the corresponding LocVars share the same StartPC.
====================================================================== }
function TDecompiler.TryEmitMultiAssign(BIdx, StartIdx: Integer; out ConsumedCount: Integer): TMultiAssignResult;
var
Blk: PBasicBlock;
FirstNode, CurNode: PSSANode;
BaseReg: Integer;
Count, I, J, TotalNodes: Integer;
SharedStartPC: Integer;
AuthNode: PSSANode;
AllNeedDecl: Boolean;
{ FinalNodeIdx[i] = index into Blk^.Nodes of the final definition for BaseReg+i }
FinalNodeIdx: array of Integer;
Names: array of AnsiString;
Values: array of AnsiString;
LHS, RHS, Line: AnsiString;
ExpectedReg: Integer;
LastFinalReg: Integer;
begin
Result := maNotMatched;
ConsumedCount := 0;
Blk := @FSF^.Blocks[BIdx];
FirstNode := Blk^.Nodes[StartIdx];
{ Skip SSA_NOP (Lua 5.4+ metamethod hints) }
if FirstNode^.Kind = SSA_NOP then Exit;
{ Skip range LOADNIL - these define multiple registers and are handled
by EmitNode's LOADNIL range handler which properly groups all registers
in the range into a single "local a, b, c" declaration. }
if (FirstNode^.Kind = SSA_LOADNIL) and (FirstNode^.ImmA < FirstNode^.ImmB) then Exit;
BaseReg := FirstNode^.Dest.Reg;
if BaseReg < 0 then Exit;
{ The first node must be a value-defining instruction that is NOT
already recognized as a user local at its own PC or PC+1 }
if (FirstNode^.Kind = SSA_CALL) then begin
if FirstNode^.ImmC < 2 then Exit; { void call not a value definition }
end
else if not (FirstNode^.Kind in [SSA_LOADK, SSA_LOADBOOL, SSA_LOADNIL,
SSA_GETGLOBAL, SSA_GETTABLE, SSA_GETUPVAL,
SSA_MOVE, SSA_BINOP, SSA_UNOP, SSA_CONCAT]) then Exit;
{ Count consecutive value-defining nodes to consecutive registers.
Allow intermediate writes to the same register: e.g., GETGLOBAL R1 + GETTABLE R1
forms a pair where GETTABLE is the "final" definition for R1.
Pattern: nodes must target registers in range [BaseReg .. BaseReg+N-1],
each new register must appear in order (no gaps or backward jumps except
re-writes to the current register). }
SetLength(FinalNodeIdx, 64); { pre-allocate generously }
FinalNodeIdx[0] := StartIdx;
Count := 1; { number of distinct consecutive registers found }
TotalNodes := 1; { total SSA nodes consumed }
ExpectedReg := BaseReg; { last register assigned = current group end }
LastFinalReg := BaseReg;
while (StartIdx + TotalNodes) <= High(Blk^.Nodes) do begin
CurNode := Blk^.Nodes[StartIdx + TotalNodes];
{ Lua 5.4+: MMBIN/MMBINI/MMBINK hint instructions appear as SSA_NOP
nodes interspersed after arithmetic BINOPs. Skip them transparently. }
if CurNode^.Kind = SSA_NOP then begin
Inc(TotalNodes);
Continue;
end;
if CurNode^.Kind = SSA_CALL then begin
{ Allow value-producing CALLs (ImmC >= 2) as part of multi-assign.
E.g., local d, e, f = multi(), 10, 20 has CALL(C=2) for d. }
if CurNode^.ImmC < 2 then Break;
end
else if not (CurNode^.Kind in [SSA_LOADK, SSA_LOADBOOL, SSA_LOADNIL,
SSA_GETGLOBAL, SSA_GETTABLE, SSA_GETUPVAL,
SSA_MOVE, SSA_BINOP, SSA_UNOP, SSA_CONCAT]) then Break;
if CurNode^.Dest.Reg = LastFinalReg then begin
{ Re-write to the same register (e.g., GETGLOBAL R1 then GETTABLE R1).
Update the final node for this register. }
FinalNodeIdx[Count - 1] := StartIdx + TotalNodes;
Inc(TotalNodes);
end
else if CurNode^.Dest.Reg = LastFinalReg + 1 then begin
{ Next consecutive register - new entry in the group }
LastFinalReg := CurNode^.Dest.Reg;
if Count >= Length(FinalNodeIdx) then
SetLength(FinalNodeIdx, Count * 2);
FinalNodeIdx[Count] := StartIdx + TotalNodes;
Inc(Count);
Inc(TotalNodes);
end
else
Break; { gap or non-consecutive - stop }
end;
{ Need at least 2 for a multi-assignment }
if Count < 2 then Exit;
{ Get SharedStartPC from the first authority node's LocVar.
All authority nodes must be emittable locals with the same StartPC. }
AuthNode := Blk^.Nodes[FinalNodeIdx[0]];
if not NodeIsEmittableLocal(AuthNode) then Exit;
SharedStartPC := FProto^.LocVars[AuthNode^.LocVarIdx].StartPC;
{ SharedStartPC must be >= first instruction PC.
If StartPC < FirstNode^.PC, this LocVar was declared earlier
and this is a reassignment, not a fresh multi-local declaration. }
if SharedStartPC < FirstNode^.PC then Exit;
{ The LocVar must start very close to the last instruction in the group.
For "local a, b, c = 1, 2, 3" the LocVars start at the PC of the last
value-defining instruction (or one after). Allow +2 for Lua 5.4+ where
a trailing MMBIN NOP pushes the LocVar StartPC one further.
For Lua 5.1-5.3, use strict +1 to avoid false positives (e.g.,
complexassign03 where CALL arg GETGLOBALs would be incorrectly
matched as a multi-assign group). }
CurNode := Blk^.Nodes[FinalNodeIdx[Count - 1]];
if FSF^.LuaVersion >= $54 then begin
if SharedStartPC > CurNode^.PC + 2 then Exit;
end else begin
if SharedStartPC > CurNode^.PC + 1 then Exit;
end;
{ Verify ALL registers in the group have emittable locals sharing the same StartPC }
for I := 1 to Count - 1 do begin
AuthNode := Blk^.Nodes[FinalNodeIdx[I]];
if not NodeIsEmittableLocal(AuthNode) then begin
Count := I;
Break;
end;
if FProto^.LocVars[AuthNode^.LocVarIdx].StartPC <> SharedStartPC then begin
Count := I;
Break;
end;
end;
if Count < 2 then Exit;
{ Guard against CALL argument setup being mistaken for a multi-local.
After the verify step may have shrunk Count, there could be remaining
value-defining nodes (e.g. more GETGLOBALs) between the group and a CALL.
Scan past any remaining value nodes and NOPs; if we find a CALL whose
register range overlaps with [BaseReg .. BaseReg+Count-1], these nodes
are argument setup, not a multi-local declaration.
CALL ImmB: 0=vararg (uses all regs from Dest), >=2 means ImmB-1 args,
so call occupies [Dest .. Dest+ImmB-1]. }
I := FinalNodeIdx[Count - 1] + 1;
while (I <= High(Blk^.Nodes)) and
(Blk^.Nodes[I]^.Kind in [SSA_NOP, SSA_LOADK, SSA_LOADBOOL, SSA_LOADNIL,
SSA_GETGLOBAL, SSA_GETTABLE, SSA_GETUPVAL,
SSA_MOVE, SSA_BINOP, SSA_UNOP, SSA_CONCAT]) do
Inc(I);
if (I <= High(Blk^.Nodes)) and
(Blk^.Nodes[I]^.Kind in [SSA_CALL, SSA_TAILCALL]) then begin
J := Blk^.Nodes[I]^.Dest.Reg;
{ If the CALL's function register falls within the multi-assign group,
the group node for that register is CALL setup (loading the function
reference), not a genuine assignment. Reject the group.
Example: LOADK R0 1, GETTABUP R1 "f", CALL R1 - R1 is CALL setup. }
if (J >= BaseReg) and (J <= BaseReg + Count - 1) then Exit;
if Blk^.Nodes[I]^.ImmB = 0 then begin
{ Vararg call: uses all registers from Dest upward }
if BaseReg >= J then Exit;
end else if Blk^.Nodes[I]^.ImmB >= 2 then begin
{ Fixed arg call: uses [Dest .. Dest+ImmB-1] }
if (BaseReg >= J) and (BaseReg <= J + Blk^.Nodes[I]^.ImmB - 1) then Exit;
end;
end;
{ Recalculate TotalNodes based on final Count (in case we shrank).
Include any trailing SSA_NOP nodes after the last value-defining node
(MMBIN hints in Lua 5.4+). }
TotalNodes := (FinalNodeIdx[Count - 1] - StartIdx) + 1;
while ((StartIdx + TotalNodes) <= High(Blk^.Nodes)) and
(Blk^.Nodes[StartIdx + TotalNodes]^.Kind = SSA_NOP) do
Inc(TotalNodes);
{ Build the names and values }
SetLength(Names, Count);
SetLength(Values, Count);
for I := 0 to Count - 1 do begin
Names[I] := NodeLocalName(Blk^.Nodes[FinalNodeIdx[I]]);
Values[I] := NodeExpr(Blk^.Nodes[FinalNodeIdx[I]]);
{ Cache expression for ALL versions of this register in the group so
ExprOf resolution works correctly }
J := NodeIdx(Blk^.Nodes[FinalNodeIdx[I]]^.Dest.Reg,
Blk^.Nodes[FinalNodeIdx[I]]^.Dest.Version);
if (J >= 0) and (J < Length(FExprStr)) then
FExprStr[J] := Names[I];
end;
{ Strip trailing nil values from the RHS.
"local v, w = 0, nil" --> "local v, w = 0"
"local a, b = nil, nil" --> "local a, b" }
J := Count;
while (J > 0) and (Values[J - 1] = 'nil') do
Dec(J);
{ J = number of non-trailing-nil values }
{ Build: local a, b, c = 1, 2, 3 }
LHS := '';
RHS := '';
for I := 0 to Count - 1 do begin
if LHS <> '' then LHS := LHS + ', ';
LHS := LHS + Names[I];
if I < J then begin
if RHS <> '' then RHS := RHS + ', ';
RHS := RHS + Values[I];
end;
end;
{ Check if this needs 'local' declaration - all authority nodes must need it }
Line := '';
AllNeedDecl := True;
for I := 0 to Count - 1 do begin
if not NodeNeedsDecl(Blk^.Nodes[FinalNodeIdx[I]]) then begin
AllNeedDecl := False;
Break;
end;
end;
if AllNeedDecl then
Line := 'local ';
if RHS <> '' then
Line := Line + LHS + ' = ' + RHS
else
Line := Line + LHS;
EmitLine(Line);
{ Mark all the local declarations as done }
for I := 0 to Count - 1 do
NodeMarkDeclared(Blk^.Nodes[FinalNodeIdx[I]]);
ConsumedCount := TotalNodes;
Result := maEmitted;
end;
{ ======================================================================
TryEmitMultiReturnAssign - detect CALL with multiple returns followed
by MOVE assignments to user locals.
Pattern: CALL R(base) ImmC=N+1 returns N values into R(base)..R(base+N-1)
followed by N MOVEs that move R(base+N-1), ..., R(base) to user locals
(in reverse order, as Lua assigns right-to-left).
Emits: local? p, a, s = func(args)
====================================================================== }
function TDecompiler.TryEmitMultiReturnAssign(BIdx, StartIdx: Integer; out ConsumedCount: Integer): TMultiAssignResult;
var
Blk: PBasicBlock;
CallNode, MoveNode: PSSANode;
NumReturns, I, ArgI, BaseReg: Integer;
FuncName, Args, LHS, RHS, Key, CallExpr: AnsiString;
PC: Integer;
DestRegs: array of Integer; { register targets of each MOVE }
DestNames: array of AnsiString; { name of each target }
MoveCount: Integer;
AllUserLocals: Boolean;
begin
Result := maNotMatched;
ConsumedCount := 0;
Blk := @FSF^.Blocks[BIdx];
CallNode := Blk^.Nodes[StartIdx];
{ Must be a CALL with multiple returns (ImmC >= 3 means 2+ return values)
or a VARARG with multiple results (ImmB >= 3 means 2+ varargs). }
if CallNode^.Kind = SSA_CALL then begin
if CallNode^.ImmC < 3 then Exit;
NumReturns := CallNode^.ImmC - 1;
BaseReg := CallNode^.ImmA;
end
else if CallNode^.Kind = SSA_VARARG then begin
if CallNode^.ImmB < 3 then Exit;
NumReturns := CallNode^.ImmB - 1;
BaseReg := CallNode^.ImmA;
end
else
Exit;
PC := CallNode^.PC;
{ Must have enough MOVEs after the CALL }
if (StartIdx + NumReturns) > High(Blk^.Nodes) then Exit;
{ Check that the following N nodes are MOVEs or SETGLOBALs from the CALL
result registers to named targets. MOVEs go to user locals, SETGLOBALs
go to globals. All must reference CALL return registers. }
SetLength(DestRegs, NumReturns);
SetLength(DestNames, NumReturns);
MoveCount := 0;
AllUserLocals := True;
for I := 1 to NumReturns do begin
MoveNode := Blk^.Nodes[StartIdx + I];
if MoveNode^.Kind = SSA_MOVE then begin
{ Source must be one of the CALL return registers }
if (MoveNode^.OpA.Reg < BaseReg) or
(MoveNode^.OpA.Reg >= BaseReg + NumReturns) then begin
AllUserLocals := False;
Break;
end;
{ Destination must be a user local }
if not NodeIsEmittableLocal(MoveNode) then begin
AllUserLocals := False;
Break;
end;
DestRegs[I - 1] := MoveNode^.OpA.Reg;
DestNames[I - 1] := NodeLocalName(MoveNode);
end
else if MoveNode^.Kind = SSA_SETGLOBAL then begin
{ Source register in OpA must be a CALL return register }
if (MoveNode^.OpA.Reg < BaseReg) or
(MoveNode^.OpA.Reg >= BaseReg + NumReturns) then begin
AllUserLocals := False;
Break;
end;
DestRegs[I - 1] := MoveNode^.OpA.Reg;
{ Get the global name from constants }
if (MoveNode^.ConstIdx >= 0) and (MoveNode^.ConstIdx < Length(FProto^.K)) then
DestNames[I - 1] := FProto^.K[MoveNode^.ConstIdx].SValue
else
DestNames[I - 1] := 'g' + IntToStr(MoveNode^.ConstIdx);
end
else begin
AllUserLocals := False;
Break;
end;
Inc(MoveCount);
end;
if (not AllUserLocals) or (MoveCount <> NumReturns) then Exit;
{ Build the CALL/VARARG expression }
if CallNode^.Kind = SSA_VARARG then begin
{ VARARG: expression is simply "..." }
CallExpr := '...';
end else begin
FuncName := ExprOf(CallNode^.OpA, PC);
if Copy(FuncName, 1, 8) = 'function' then
FuncName := '(' + FuncName + ')';
ArgI := 0;
{ Detect SELF method call - skip implicit self argument }
if (CallNode^.OpA.Reg >= 0) and (CallNode^.OpA.Reg <> SSA_CONST_REG) then begin
for I := 0 to High(FSF^.AllNodes) do
if RefEqual(FSF^.AllNodes[I]^.Dest, CallNode^.OpA) and
(FSF^.AllNodes[I]^.Kind = SSA_SELF) then begin
ArgI := 1; Break;
end;
end;
Args := '';
while ArgI <= High(CallNode^.ArgRefs) do begin
if Args <> '' then Args := Args + ', ';
Args := Args + ExprOf(CallNode^.ArgRefs[ArgI], PC);
Inc(ArgI);
end;
CallExpr := FuncName + '(' + Args + ')';
end;
{ Build LHS: the target names in the order of the return registers.
MOVEs/SETGLOBALs may be in reverse order, so map by source register.
DestRegs[J] holds the source register and DestNames[J] holds the target name. }
LHS := '';
Key := '';
for I := 0 to NumReturns - 1 do begin
{ Find which assignment node reads from BaseReg + I }
RHS := '';
for ArgI := 0 to MoveCount - 1 do begin
if DestRegs[ArgI] = BaseReg + I then begin
RHS := DestNames[ArgI];
{ Check if the target needs 'local' declaration (only for MOVEs to locals) }
MoveNode := Blk^.Nodes[StartIdx + ArgI + 1];
if (MoveNode^.Kind = SSA_MOVE) and
NodeNeedsDecl(MoveNode) then
Key := 'local ';
Break;
end;
end;
if RHS = '' then Exit; { shouldn't happen }
if LHS <> '' then LHS := LHS + ', ';
LHS := LHS + RHS;
end;
{ Cache expression in the CALL's dest so ExprOf doesn't inline it }
I := NodeIdx(CallNode^.Dest.Reg, CallNode^.Dest.Version);
if (I >= 0) and (I < Length(FExprStr)) then
FExprStr[I] := '__multiret__';
EmitLine(Key + LHS + ' = ' + CallExpr);
{ Mark all MOVE target declarations as done }
for I := 1 to NumReturns do begin
MoveNode := Blk^.Nodes[StartIdx + I];
if MoveNode^.Kind = SSA_MOVE then
NodeMarkDeclared(MoveNode);
end;
ConsumedCount := 1 + NumReturns; { CALL/VARARG + N MOVEs }
Result := maEmitted;
end;
{ ======================================================================
TryEmitReverseMultiAssign - detect value-compute + reverse-MOVE patterns.
Handles: a, b = ... (VARARG + reverse MOVEs)
a, b = ..., 2 (VARARG + LOADK + reverse MOVEs)
a, b, c = 1, f(1,2,3) (LOADK + CALL(multi) + reverse MOVEs)
a, b, c = f(), 1, 2 (CALL(1-ret) + LOADKs + reverse MOVEs)
====================================================================== }
function TDecompiler.TryEmitReverseMultiAssign(BIdx, StartIdx: Integer; out ConsumedCount: Integer): TMultiAssignResult;
var
Blk: PBasicBlock;
I, J, K, MoveStart, MoveEnd, NumMoves: Integer;
SrcDef: PSSANode;
TargetReg, LowestTarget, HighestTarget: Integer;
Node, MoveNode: PSSANode;
{ Track the assignment: target register --> (source expression, node index) }
Targets: array of Integer; { target register for each MOVE }
Sources: array of Integer; { source register for each MOVE }
{ Value definitions for temp registers }
ValExprs: array of AnsiString; { expressions for each target position }
LHS, RHS, Line, ExprStr: AnsiString;
BaseReg, SrcReg, TempBaseReg: Integer;
NumTargets: Integer;
AllNamed, HasMultiRet, FoundDef, NeedsDeclAll: Boolean;
TargetAuthNodes: array of PSSANode;
MultiRetNode: PSSANode;
MultiRetIdx, MultiRetBase, MultiRetCount: Integer;
FuncName, Args: AnsiString;
ArgI, L: Integer;
IsSelf: Boolean;
begin
Result := maNotMatched;
ConsumedCount := 0;
Blk := @FSF^.Blocks[BIdx];
{ Scan forward from StartIdx to find a sequence of reverse MOVEs.
The first node must be a value definition (LOADK, VARARG, CALL, etc.)
that writes to a temp register (not a user local). }
Node := Blk^.Nodes[StartIdx];
{ Skip SSA_NOP (Lua 5.4+ metamethod hints) }
if Node^.Kind = SSA_NOP then Exit;
if not (Node^.Kind in [SSA_LOADK, SSA_LOADBOOL, SSA_LOADNIL,
SSA_GETGLOBAL, SSA_GETTABLE, SSA_GETUPVAL,
SSA_MOVE, SSA_BINOP, SSA_UNOP, SSA_CONCAT,
SSA_VARARG, SSA_CALL, SSA_NEWTABLE]) then Exit;
{ Reject CALL with no return values (ImmC=0 or ImmC=1) - these are
side-effect statements (e.g., f()), not value-producing expressions. }
if (Node^.Kind = SSA_CALL) and (Node^.ImmC <= 1) then Exit;
BaseReg := Node^.Dest.Reg;
{ The starting node must write to a temp register (above user locals).
For the pattern to work, the first node's register must be above
the eventual target registers. Skip if it's already a user local. }
if NodeIsEmittableLocal(Node) then Exit;
{ Find where the reverse MOVEs start.
Scan forward: value-defining nodes write to temp registers,
then MOVEs copy from temp registers to user locals.
Track if value phase contains VARARG or CALL - this detector only
handles patterns with multi-return sources (VARARG/CALL). Regular
single-value assignments are handled by TryEmitMultiAssign/TryEmitSwap. }
MoveStart := -1;
HasMultiRet := (Node^.Kind in [SSA_VARARG, SSA_CALL]);
for I := StartIdx + 1 to High(Blk^.Nodes) do begin
MoveNode := Blk^.Nodes[I];
{ Resolve source definer; if implicit-def parent (Dest ≠ OpA), clear it
so NodeIsLocal checks the right register, not the parent's primary def. }
SrcDef := DefNodeFor(MoveNode^.OpA);
if (SrcDef <> nil) and (not RefEqual(SrcDef^.Dest, MoveNode^.OpA)) then
SrcDef := nil;
if (MoveNode^.Kind = SSA_MOVE) and
NodeIsEmittableLocal(MoveNode) and
(not NodeIsLocal(SrcDef)) and
(MoveNode^.OpA.Reg >= BaseReg) then begin
MoveStart := I;
Break;
end;
{ Skip SSA_NOP (Lua 5.4+ metamethod hints) }
if MoveNode^.Kind = SSA_NOP then Continue;
{ Track VARARG/CALL presence }
if MoveNode^.Kind in [SSA_VARARG, SSA_CALL] then
HasMultiRet := True;
{ Allow value-defining nodes and direct-to-local LOADKs between start and MOVEs }
if MoveNode^.Kind in [SSA_LOADK, SSA_LOADBOOL, SSA_LOADNIL,
SSA_GETGLOBAL, SSA_GETTABLE, SSA_GETUPVAL,
SSA_MOVE, SSA_BINOP, SSA_UNOP, SSA_CONCAT,
SSA_VARARG, SSA_CALL, SSA_NEWTABLE] then
Continue
else
Exit; { unexpected node kind }
end;
if MoveStart < 0 then Exit;
{ Reject false matches where the start node is actually a CALL argument.
If the start node is NOT a CALL/VARARG, and there's a CALL between
StartIdx and MoveStart with ImmA < BaseReg, the start node is
likely a CALL argument setup (e.g., LOADK R4 before CALL R3). }
if not (Blk^.Nodes[StartIdx]^.Kind in [SSA_CALL, SSA_VARARG]) then begin
for I := StartIdx + 1 to MoveStart - 1 do begin
if (Blk^.Nodes[I]^.Kind = SSA_CALL) and (Blk^.Nodes[I]^.ImmA < BaseReg) then
Exit;
end;
end;
{ Count reverse MOVEs: they must write to consecutive user locals,
with each MOVE reading from a temp register >= BaseReg.
The targets should decrease (reverse order): e.g., R2, R1, R0. }
NumMoves := 0;
HighestTarget := -1;
LowestTarget := MaxInt;
MoveEnd := MoveStart;
SetLength(Targets, 16);
SetLength(Sources, 16);
I := MoveStart;
while I <= High(Blk^.Nodes) do begin
MoveNode := Blk^.Nodes[I];
if (MoveNode^.Kind <> SSA_MOVE) then Break;
if not NodeIsEmittableLocal(MoveNode) then Break;
{ Source must be a temp register from the value phase }
if MoveNode^.OpA.Reg < BaseReg then Break;
if NumMoves >= Length(Targets) then begin
SetLength(Targets, NumMoves * 2);
SetLength(Sources, NumMoves * 2);
end;
Targets[NumMoves] := MoveNode^.Dest.Reg;
Sources[NumMoves] := MoveNode^.OpA.Reg;
if MoveNode^.Dest.Reg > HighestTarget then
HighestTarget := MoveNode^.Dest.Reg;
if MoveNode^.Dest.Reg < LowestTarget then
LowestTarget := MoveNode^.Dest.Reg;
Inc(NumMoves);
MoveEnd := I;
Inc(I);
end;
if NumMoves < 1 then Exit;
NumTargets := HighestTarget - LowestTarget + 1;
{ Also check for direct writes to user locals between StartIdx and MoveStart.
E.g., "a, b = ..., 2" may have LOADK directly to R1 (user local b).
"a, b, c = f(), 1, 2" may have LOADK directly to R2 (user local c).
Also handles MOVE to user local where source < BaseReg (direct copy,
e.g., "d = duration" where duration is a parameter and no temp needed). }
{ Collect all direct-to-local writes }
for I := StartIdx to MoveStart - 1 do begin
Node := Blk^.Nodes[I];
if Node^.Kind = SSA_NOP then Continue; { skip metamethod hints }
if (Node^.Kind in [SSA_LOADK, SSA_LOADBOOL, SSA_LOADNIL,
SSA_GETGLOBAL, SSA_GETTABLE, SSA_GETUPVAL,
SSA_BINOP, SSA_UNOP, SSA_CONCAT]) and
NodeIsEmittableLocal(Node) and
((Node^.Dest.Reg >= LowestTarget) and (Node^.Dest.Reg <= HighestTarget + 1)) then begin
{ This is a direct write - add it as a "virtual MOVE" }
if NumMoves >= Length(Targets) then begin
SetLength(Targets, NumMoves * 2);
SetLength(Sources, NumMoves * 2);
end;
Targets[NumMoves] := Node^.Dest.Reg;
Sources[NumMoves] := -1; { -1 = direct value, not a MOVE source }
Inc(NumMoves);
if Node^.Dest.Reg > HighestTarget then
HighestTarget := Node^.Dest.Reg;
if Node^.Dest.Reg < LowestTarget then
LowestTarget := Node^.Dest.Reg;
end
{ MOVE to user local where source < BaseReg: direct param/local copy
that doesn't use the temp range (e.g., d = duration). }
else if (Node^.Kind = SSA_MOVE) and
(Node^.OpA.Reg < BaseReg) and
NodeIsEmittableLocal(Node) and
((Node^.Dest.Reg >= LowestTarget) and (Node^.Dest.Reg <= HighestTarget + 1)) then begin
if NumMoves >= Length(Targets) then begin
SetLength(Targets, NumMoves * 2);
SetLength(Sources, NumMoves * 2);
end;
Targets[NumMoves] := Node^.Dest.Reg;
Sources[NumMoves] := -1; { -1 = direct value, use NodeExpr }
Inc(NumMoves);
if Node^.Dest.Reg > HighestTarget then
HighestTarget := Node^.Dest.Reg;
if Node^.Dest.Reg < LowestTarget then
LowestTarget := Node^.Dest.Reg;
end;
end;
NumTargets := HighestTarget - LowestTarget + 1;
if NumTargets < 2 then Exit;
if NumTargets > 16 then Exit; { sanity }
{ Build TargetAuthNodes: find authority node for each LHS position }
SetLength(TargetAuthNodes, NumTargets);
for I := 0 to NumTargets - 1 do begin
TargetReg := LowestTarget + I;
TargetAuthNodes[I] := nil;
{ Check write-back MOVEs first }
for K := MoveStart to MoveEnd do begin
if (Blk^.Nodes[K]^.Kind = SSA_MOVE) and
(Blk^.Nodes[K]^.Dest.Reg = TargetReg) then begin
TargetAuthNodes[I] := Blk^.Nodes[K];
Break;
end;
end;
{ If not found in MOVEs, check direct writes in value phase }
if TargetAuthNodes[I] = nil then begin
for K := MoveStart - 1 downto StartIdx do begin
if Blk^.Nodes[K]^.Dest.Reg = TargetReg then begin
TargetAuthNodes[I] := Blk^.Nodes[K];
Break;
end;
end;
end;
end;
{ Build value expressions for each target position }
SetLength(ValExprs, NumTargets);
for I := 0 to NumTargets - 1 do
ValExprs[I] := '';
{ Build value expressions for each target position.
For each target register (LowestTarget..HighestTarget), find its value:
- If it's a MOVE target with source SrcReg, find the definition of SrcReg
- If it's a direct write, use the node's expression
For multi-return VARARG/CALL, the expression expands to fill remaining slots
(only valid at the last position in the RHS). }
for J := 0 to NumTargets - 1 do begin
TargetReg := LowestTarget + J;
ValExprs[J] := '';
{ Find which MOVE/direct writes to this target }
SrcReg := -2; { -2 = not found }
for I := 0 to NumMoves - 1 do begin
if Targets[I] = TargetReg then begin
SrcReg := Sources[I]; Break;
end;
end;
if SrcReg = -2 then Exit; { target not covered }
if SrcReg = -1 then begin
{ Direct write - find the node that writes directly to this target.
Handles LOADK, LOADBOOL, LOADNIL, GETGLOBAL, GETTABLE, etc.
Also handles MOVE where source < BaseReg (direct param/local copy). }
for K := MoveStart - 1 downto StartIdx do begin
Node := Blk^.Nodes[K];
if (Node^.Dest.Reg = TargetReg) then begin
if Node^.Kind = SSA_MOVE then
ValExprs[J] := ExprOf(Node^.OpA, Node^.PC)
else
ValExprs[J] := NodeExpr(Node);
Break;
end;
end;
if ValExprs[J] = '' then Exit;
end else begin
{ MOVE from SrcReg - find LAST definition by scanning backwards.
This is important because CALL arguments (LOADK/GETGLOBAL) may
initially define a register, then the CALL overwrites it with
return values. Scanning backwards finds the CALL first. }
FoundDef := False;
for K := MoveStart - 1 downto StartIdx do begin
Node := Blk^.Nodes[K];
{ Check implicit multi-return FIRST: VARARG/CALL that implicitly
defines SrcReg as an extra return register.
ImmB=0 means variable number of results (fills from ImmA upward).
ImmB>1 means fixed count: ImmB-1 results from ImmA to ImmA+ImmB-2. }
if (Node^.Kind = SSA_VARARG) and
((Node^.ImmB = 0) or (Node^.ImmB > 1)) and
(SrcReg >= Node^.ImmA) then begin
{ For fixed count, check upper bound }
if (Node^.ImmB > 1) and (SrcReg > Node^.ImmA + Node^.ImmB - 2) then
{ SrcReg outside fixed range - not from this VARARG }
else begin
if SrcReg = Node^.ImmA then
ValExprs[J] := '...'
else begin
{ Extra return register - covered by VARARG expansion }
end;
FoundDef := True; Break;
end;
end;
if (Node^.Kind = SSA_CALL) and (Node^.ImmC > 1) and
(SrcReg >= Node^.ImmA) and (SrcReg <= Node^.ImmA + Node^.ImmC - 2) then begin
if SrcReg = Node^.ImmA then begin
{ First return register - build the call expression }
FuncName := ExprOf(Node^.OpA, Node^.PC);
if Copy(FuncName, 1, 8) = 'function' then
FuncName := '(' + FuncName + ')';
ArgI := 0; IsSelf := False;
if (Node^.OpA.Reg >= 0) and (Node^.OpA.Reg <> SSA_CONST_REG) then
for L := 0 to High(FSF^.AllNodes) do
if RefEqual(FSF^.AllNodes[L]^.Dest, Node^.OpA) and
(FSF^.AllNodes[L]^.Kind = SSA_SELF) then begin
ArgI := 1; IsSelf := True; Break;
end;
Args := '';
while ArgI <= High(Node^.ArgRefs) do begin
if Args <> '' then Args := Args + ', ';
Args := Args + ExprOf(Node^.ArgRefs[ArgI], Node^.PC);
Inc(ArgI);
end;
ValExprs[J] := FuncName + '(' + Args + ')';
end;
{ Else: extra return register - covered by CALL expansion }
FoundDef := True; Break;
end;
{ Exact match on Dest.Reg }
if Node^.Dest.Reg = SrcReg then begin
if Node^.Kind = SSA_VARARG then begin
ValExprs[J] := '...'; FoundDef := True;
end
else if Node^.Kind = SSA_CALL then begin
FuncName := ExprOf(Node^.OpA, Node^.PC);
if Copy(FuncName, 1, 8) = 'function' then
FuncName := '(' + FuncName + ')';
ArgI := 0; IsSelf := False;
if (Node^.OpA.Reg >= 0) and (Node^.OpA.Reg <> SSA_CONST_REG) then
for L := 0 to High(FSF^.AllNodes) do
if RefEqual(FSF^.AllNodes[L]^.Dest, Node^.OpA) and
(FSF^.AllNodes[L]^.Kind = SSA_SELF) then begin
ArgI := 1; IsSelf := True; Break;
end;
Args := '';
while ArgI <= High(Node^.ArgRefs) do begin
if Args <> '' then Args := Args + ', ';
Args := Args + ExprOf(Node^.ArgRefs[ArgI], Node^.PC);
Inc(ArgI);
end;
ValExprs[J] := FuncName + '(' + Args + ')'; FoundDef := True;
end
else begin
ValExprs[J] := NodeExpr(Node); FoundDef := True;
end;
Break;
end;
end;
if not FoundDef then Exit;
end;
end;
{ Build RHS: collect non-empty expressions in order.
Skip empty positions (covered by multi-return expansion). }
RHS := '';
if FOpts.Debug then
for J := 0 to NumTargets - 1 do
WriteLn(StdErr, 'TryEmitReverseMulti: ValExprs[', J, '] = "', ValExprs[J], '"');
for J := 0 to NumTargets - 1 do begin
if ValExprs[J] <> '' then begin
if RHS <> '' then RHS := RHS + ', ';
RHS := RHS + ValExprs[J];
end;
end;
if RHS = '' then Exit;
{ Build LHS names using authority nodes }
LHS := '';
for I := 0 to NumTargets - 1 do begin
if LHS <> '' then LHS := LHS + ', ';
if (TargetAuthNodes[I] <> nil) and NodeIsEmittableLocal(TargetAuthNodes[I]) then
LHS := LHS + NodeLocalName(TargetAuthNodes[I])
else
LHS := LHS + 't' + IntToStr(LowestTarget + I);
end;
{ Check if this needs 'local' declaration - all authority nodes must need it }
Line := '';
NeedsDeclAll := True;
for I := 0 to NumTargets - 1 do begin
if (TargetAuthNodes[I] = nil) or not NodeNeedsDecl(TargetAuthNodes[I]) then begin
NeedsDeclAll := False;
Break;
end;
end;
if NeedsDeclAll then
Line := 'local ';
Line := Line + LHS + ' = ' + RHS;
EmitLine(Line);
{ Mark local declarations as done }
for I := 0 to NumTargets - 1 do
if TargetAuthNodes[I] <> nil then
NodeMarkDeclared(TargetAuthNodes[I]);
{ Mark all value-phase and MOVE nodes as consumed to prevent re-emission }
for I := StartIdx to MoveEnd do begin
Node := Blk^.Nodes[I];
K := NodeIdx(Node^.Dest.Reg, Node^.Dest.Version);
if (K >= 0) and (K < Length(FInlined)) then
FInlined[K] := True;
if Node^.Kind in [SSA_CALL, SSA_VARARG] then begin
if (K >= 0) and (K < Length(FExprStr)) then
FExprStr[K] := '__multiret__';
end;
end;
ConsumedCount := (MoveEnd - StartIdx) + 1;
Result := maEmitted;
end;
{ ======================================================================
TryEmitSwap - detect variable swap patterns in MOVE sequences
====================================================================== }
function TDecompiler.TryEmitSwap(BIdx, StartIdx: Integer; out ConsumedCount: Integer): TMultiAssignResult;
{ Detect multi-assignment patterns compiled from:
a, b, c, d = expr1, expr2, expr3, expr4
The compiler generates:
Phase 1 (save): MOVE temp := old_local (save values that will be overwritten)
Phase 2 (compute): BINOP temp/local := ... (compute new values)
Phase 3 (write): MOVE local := temp (write saved/computed values to locals)
The write-back MOVEs appear in reverse order of the original LHS.
BINOPs that write directly to user-locals are interleaved with the pattern.
In Lua 5.4+, MMBIN/MMBINI/MMBINK metamethod hints (SSA_NOP) are
interspersed after each BINOP. We skip NOPs transparently and track
actual node indices via SaveIdx/MidIdx/WriteIdx arrays. }
var
Blk: TBasicBlock;
N, WB: PSSANode;
I, J, K, SaveCount, MidCount, WriteCount, NopCount, Total: Integer;
SaveIdx, MidIdx, WriteIdx: array of Integer;
LHS, RHS: AnsiString;
IsLocal, UsedByWriteBack: Boolean;
begin
Result := maNotMatched;
ConsumedCount := 0;
Blk := FSF^.Blocks[BIdx];
if StartIdx >= Length(Blk.Nodes) then Exit;
SetLength(SaveIdx, 16);
SetLength(MidIdx, 16);
SetLength(WriteIdx, 16);
{ Phase 1: Count consecutive temp-save MOVEs (dest = non-local temp).
A MOVE is a save if its dest register is later used as an OpA source by
another MOVE within the pattern. We check this by doing a forward scan. }
SaveCount := 0;
NopCount := 0;
I := StartIdx;
while I <= High(Blk.Nodes) do begin
N := Blk.Nodes[I];
if N^.Kind <> SSA_MOVE then Break;
if NodeIsLocal(N) then Break;
{ Check if this MOVE's dest is used as a source by a later MOVE,
or as a key/value in a SETTABLE write-back within this block.
We scan past BINOPs, NOPs, LOADKs, GETTABLEs, SETTABLEs because
Phase 2 and Phase 3 may include these instruction types. }
IsLocal := False;
for J := I + 1 to High(Blk.Nodes) do begin
if not (Blk.Nodes[J]^.Kind in [SSA_MOVE, SSA_BINOP, SSA_NOP,
SSA_LOADK, SSA_LOADBOOL, SSA_GETTABLE, SSA_SETTABLE]) then Break;
{ Write-back MOVE uses the saved temp as source }
if (Blk.Nodes[J]^.Kind = SSA_MOVE) and
(Blk.Nodes[J]^.OpA.Reg = N^.Dest.Reg) and
(Blk.Nodes[J]^.OpA.Version = N^.Dest.Version) then begin
IsLocal := True; Break;
end;
{ SETTABLE uses the saved temp as key or value }
if (Blk.Nodes[J]^.Kind = SSA_SETTABLE) then begin
if ((Blk.Nodes[J]^.OpB.Reg = N^.Dest.Reg) and
(Blk.Nodes[J]^.OpB.Version = N^.Dest.Version)) or
((Blk.Nodes[J]^.OpC.Reg = N^.Dest.Reg) and
(Blk.Nodes[J]^.OpC.Version = N^.Dest.Version)) then begin
IsLocal := True; Break;
end;
end;
end;
if not IsLocal then Break;
if SaveCount >= Length(SaveIdx) then SetLength(SaveIdx, SaveCount * 2);
SaveIdx[SaveCount] := I;
Inc(SaveCount);
Inc(I);
end;
if SaveCount < 1 then Exit;
{ Phase 2: Count value-computing instructions (BINOPs, LOADKs, etc).
These may write to either temps or directly to user-locals.
In Lua 5.4+, skip SSA_NOP nodes (MMBIN/MMBINI/MMBINK hints). }
MidCount := 0;
while I <= High(Blk.Nodes) do begin
N := Blk.Nodes[I];
if N^.Kind = SSA_NOP then begin Inc(NopCount); Inc(I); Continue; end;
if not (N^.Kind in [SSA_BINOP, SSA_LOADK, SSA_LOADBOOL]) then Break;
if MidCount >= Length(MidIdx) then SetLength(MidIdx, MidCount * 2 + 4);
MidIdx[MidCount] := I;
Inc(MidCount);
Inc(I);
end;
{ Phase 3: Count write-back nodes:
- MOVEs (dest = local via SSA binding, or PHI use)
- GETTABLE (dest = local) - reads from table into a local
- SETTABLE - writes to table (uses saved temps from Phase 1) }
WriteCount := 0;