-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLuaSSA.pas
More file actions
2757 lines (2553 loc) · 85 KB
/
Copy pathLuaSSA.pas
File metadata and controls
2757 lines (2553 loc) · 85 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
unit LuaSSA;
{
LuaSSA.pas - Control Flow Graph construction, SSA lifting, Phi-insertion,
and renaming pass for Lua 5.1 bytecode.
Pipeline:
1. BuildCFG – partition instructions into basic blocks, add edges.
2. ComputeDominators – iterative dataflow (Cooper/Harvey/Kennedy).
3. ComputeDF – dominance frontier sets.
4. InsertPhiFunctions – standard liveness-based Phi placement.
5. RenamePass – walk dominator tree, rename defs/uses.
IR node kinds cover every Lua 5.x opcode. Phi-nodes are represented as
SSA_PHI with a variable-length Operands array.
Memory: call FreeSSAFunction to release all allocations.
}
{$mode objfpc}{$H+}
interface
uses
SysUtils, LuaChunk, LuaOpcodes;
{ ======================================================================
SSA value reference
====================================================================== }
type
{ Every SSA value is identified by (Register, Version).
Register -1 = "no value" / undefined.
Register -2 = constant slot (used for RK constants embedded in IR). }
TSSARef = record
Reg : Integer; { Lua register index, or -1/−2 }
Version : Integer; { SSA rename count }
ConstIdx: Integer; { valid when Reg = -2 }
end;
const
SSA_NO_REF : TSSARef = (Reg: -1; Version: 0; ConstIdx: 0);
SSA_CONST_REG = -2;
function MakeRef(Reg, Ver: Integer): TSSARef;
function MakeConstRef(Idx: Integer): TSSARef;
function RefEqual(const A, B: TSSARef): Boolean;
function RefToStr(const R: TSSARef): AnsiString;
{ ======================================================================
SSA node kinds
====================================================================== }
type
TSSAKind = (
SSA_NOP,
SSA_PHI,
SSA_MOVE,
SSA_LOADK,
SSA_LOADBOOL,
SSA_LOADNIL,
SSA_GETUPVAL,
SSA_GETGLOBAL,
SSA_GETTABLE,
SSA_SETGLOBAL,
SSA_SETUPVAL,
SSA_SETTABLE,
SSA_NEWTABLE,
SSA_SELF,
SSA_BINOP, { ADD SUB MUL DIV MOD POW }
SSA_UNOP, { UNM NOT LEN }
SSA_CONCAT,
SSA_JUMP,
SSA_BRANCH, { EQ LT LE TEST TESTSET }
SSA_CALL,
SSA_TAILCALL,
SSA_RETURN,
SSA_FORPREP,
SSA_FORLOOP,
SSA_TFORLOOP,
SSA_SETLIST,
SSA_CLOSE,
SSA_CLOSURE,
SSA_VARARG
);
TBinOp = (bopAdd, bopSub, bopMul, bopDiv, bopMod, bopPow,
bopIDiv, bopBAnd, bopBOr, bopBXor, bopSHL, bopSHR);
TUnOp = (uopNeg, uopNot, uopLen, uopBNot);
TRelOp = (ropEQ, ropLT, ropLE, ropTest, ropTestSet);
{ ------------------------------------------------------------------ }
{ Single SSA instruction }
{ ------------------------------------------------------------------ }
PSSANode = ^TSSANode;
TSSANode = record
Kind : TSSAKind;
PC : Integer; { source instruction index (0-based) }
Line : Integer; { source line number, -1 if unknown }
Dest : TSSARef; { output value (may be SSA_NO_REF) }
{ generic operand slots }
OpA : TSSARef;
OpB : TSSARef;
OpC : TSSARef;
{ Phi-node: list of incoming (value, predecessor-block-index) pairs }
Operands : array of TSSARef; { for SSA_PHI: one per predecessor }
PhiBlocks : array of Integer; { predecessor block indices }
{ operator selectors }
BinOp : TBinOp;
UnOp : TUnOp;
RelOp : TRelOp;
Invert: Boolean; { for EQ/LT/LE: result is inverted if A=1 }
{ jump targets (block indices) }
TargetTrue : Integer; { -1 = fall-through / not used }
TargetFalse : Integer;
{ raw integer immediates }
ImmA, ImmB, ImmC: Integer;
{ pointer into proto's constant / sub-proto table }
ConstIdx : Integer; { LOADK, GETGLOBAL, SETGLOBAL, CLOSURE }
UpvalIdx : Integer; { GETUPVAL, SETUPVAL }
{ SSA-versioned argument references for CALL/TAILCALL/RETURN }
ArgRefs : array of TSSARef;
{ LocVar binding: index into Proto^.LocVars[], -1 = no LocVar (temp) }
LocVarIdx : Integer;
{ True for LOADNIL pseudo-def nodes (per-register) - metadata only, not emitted }
IsMetaOnly: Boolean;
end;
{ ------------------------------------------------------------------ }
{ Basic block }
{ ------------------------------------------------------------------ }
PBasicBlock = ^TBasicBlock;
TBasicBlock = record
Index : Integer;
StartPC : Integer; { first instruction PC (inclusive) }
EndPC : Integer; { last instruction PC (inclusive) }
Nodes : array of PSSANode;
Succs : array of Integer; { successor block indices }
Preds : array of Integer; { predecessor block indices }
{ dominator tree }
IDom : Integer; { immediate dominator index, -1 for entry }
DomChildren: array of Integer;
{ dominance frontier }
DF : array of Integer;
end;
{ ------------------------------------------------------------------ }
{ Implicit def record: tracks (Reg,Version) --> parent node for }
{ extra registers defined by TFORLOOP/CALL(C>=3)/SELF that have }
{ no SSA node with matching Dest in AllNodes. }
{ ------------------------------------------------------------------ }
TImplicitDef = record
Reg: Integer;
Version: Integer;
ParentNode: PSSANode;
end;
{ ------------------------------------------------------------------ }
{ Complete SSA function (one per TProto) }
{ ------------------------------------------------------------------ }
TSSAFunction = record
Proto : PProto; { source proto (not owned) }
Blocks : array of TBasicBlock;
AllNodes: array of PSSANode; { flat list for easy freeing }
NumRegs : Integer; { = Proto^.MaxStack }
LuaVersion : Byte; { $51, $52, $53, $54, $55 - for version-specific logic }
ImplicitDefs: array of TImplicitDef;
end;
PSSAFunction = ^TSSAFunction;
{ ======================================================================
Public API
====================================================================== }
{ Build SSA function from a parsed proto (Lua 5.1 only, existing path) }
function BuildSSAFunction(Proto: PProto): PSSAFunction; overload;
{ Build SSA function using a version-aware opcode table.
When OT is nil, falls back to the existing 5.1 path. }
function BuildSSAFunction(Proto: PProto; const OT: POpcodeTable): PSSAFunction; overload;
{ Add a synthetic integer constant to Proto^.K[], return its index }
function AddSyntheticConst(Proto: PProto; IVal: Int64): Integer;
{ Add a synthetic float constant to Proto^.K[], return its index }
function AddSyntheticFloatConst(Proto: PProto; FVal: Double): Integer;
{ Dump SSA IR to stdout for debugging }
procedure DumpSSA(const F: PSSAFunction);
{ Free all SSA structures }
procedure FreeSSAFunction(F: PSSAFunction);
implementation
{ ======================================================================
TSSARef helpers
====================================================================== }
function MakeRef(Reg, Ver: Integer): TSSARef;
begin
Result.Reg := Reg;
Result.Version := Ver;
Result.ConstIdx := 0;
end;
function MakeConstRef(Idx: Integer): TSSARef;
begin
Result.Reg := SSA_CONST_REG;
Result.Version := 0;
Result.ConstIdx := Idx;
end;
function RefEqual(const A, B: TSSARef): Boolean;
begin
Result := (A.Reg = B.Reg) and (A.Version = B.Version) and
(A.ConstIdx = B.ConstIdx);
end;
function RefToStr(const R: TSSARef): AnsiString;
begin
if R.Reg = -1 then
Result := '_'
else if R.Reg = SSA_CONST_REG then
Result := 'K' + IntToStr(R.ConstIdx)
else
Result := 'R' + IntToStr(R.Reg) + '_' + IntToStr(R.Version);
end;
{ ======================================================================
Helper: new SSANode
====================================================================== }
function NewNode(Kind: TSSAKind; PC, Line: Integer): PSSANode;
begin
New(Result);
Result^ := Default(TSSANode);
Result^.Kind := Kind;
Result^.PC := PC;
Result^.Line := Line;
Result^.Dest := SSA_NO_REF;
Result^.OpA := SSA_NO_REF;
Result^.OpB := SSA_NO_REF;
Result^.OpC := SSA_NO_REF;
Result^.TargetTrue := -1;
Result^.TargetFalse := -1;
Result^.ConstIdx := -1;
Result^.UpvalIdx := -1;
Result^.LocVarIdx := -1;
Result^.IsMetaOnly := False;
end;
{ ======================================================================
Step 1 – Build CFG
====================================================================== }
{ Returns true if opcode causes a block boundary (is a leader or ends a block) }
function IsJump(Op: TOpCode): Boolean;
begin
Result := Op in [OP_JMP, OP_EQ, OP_LT, OP_LE, OP_TEST, OP_TESTSET,
OP_FORLOOP, OP_FORPREP, OP_TFORLOOP,
OP_RETURN, OP_TAILCALL];
end;
procedure ComputeLeaders(const Proto: PProto; var Leaders: array of Boolean);
var
I, N, Target: Integer;
Inst: TInstruction;
Op: TOpCode;
begin
N := Length(Proto^.Code);
{ PC 0 is always a leader }
Leaders[0] := True;
I := 0;
while I < N do begin
Inst := Proto^.Code[I];
Op := GET_OPCODE(Inst);
case Op of
OP_JMP: begin
Target := I + 1 + GETARG_sBx(Inst);
if (Target >= 0) and (Target < N) then Leaders[Target] := True;
if I + 1 < N then Leaders[I + 1] := True;
end;
OP_EQ, OP_LT, OP_LE, OP_TEST, OP_TESTSET: begin
{ These skip the next instruction if condition is false;
the instruction AFTER the skip target is also a leader }
if I + 1 < N then Leaders[I + 1] := True;
if I + 2 < N then Leaders[I + 2] := True;
{ the JMP that follows often gives the actual branch target }
end;
OP_FORLOOP: begin
Target := I + 1 + GETARG_sBx(Inst);
if (Target >= 0) and (Target < N) then Leaders[Target] := True;
if I + 1 < N then Leaders[I + 1] := True;
end;
OP_FORPREP: begin
Target := I + 1 + GETARG_sBx(Inst);
if (Target >= 0) and (Target < N) then Leaders[Target] := True;
if I + 1 < N then Leaders[I + 1] := True;
end;
OP_TFORLOOP: begin
{ followed by a JMP; the instruction after the JMP is also a leader }
if I + 1 < N then Leaders[I + 1] := True;
if I + 2 < N then Leaders[I + 2] := True;
end;
OP_LOADBOOL: begin
{ LOADBOOL with C=1 skips the next instruction }
if GETARG_C(Inst) <> 0 then begin
if I + 1 < N then Leaders[I + 1] := True;
if I + 2 < N then Leaders[I + 2] := True;
end;
end;
OP_RETURN, OP_TAILCALL: begin
if I + 1 < N then Leaders[I + 1] := True;
end;
end;
Inc(I);
end;
end;
{ Map from PC to block index }
function PCToBlock(const PCMap: array of Integer; PC: Integer): Integer;
begin
if (PC >= 0) and (PC < Length(PCMap)) then
Result := PCMap[PC]
else
Result := -1;
end;
procedure BuildCFG(SF: PSSAFunction);
var
Proto : PProto;
N : Integer;
Leaders : array of Boolean;
PCMap : array of Integer; { PC -> block index }
I, BIdx : Integer;
Inst : TInstruction;
Op : TOpCode;
Target : Integer;
NBB : Integer;
Blk : PBasicBlock;
procedure AddSucc(Src, Dst: Integer);
var J: Integer;
begin
if Dst < 0 then Exit;
for J := 0 to High(SF^.Blocks[Src].Succs) do
if SF^.Blocks[Src].Succs[J] = Dst then Exit; { no duplicate }
SetLength(SF^.Blocks[Src].Succs, Length(SF^.Blocks[Src].Succs) + 1);
SF^.Blocks[Src].Succs[High(SF^.Blocks[Src].Succs)] := Dst;
SetLength(SF^.Blocks[Dst].Preds, Length(SF^.Blocks[Dst].Preds) + 1);
SF^.Blocks[Dst].Preds[High(SF^.Blocks[Dst].Preds)] := Src;
end;
begin
Proto := SF^.Proto;
N := Length(Proto^.Code);
if N = 0 then Exit;
SetLength(Leaders, N);
SetLength(PCMap, N);
for I := 0 to N - 1 do begin Leaders[I] := False; PCMap[I] := -1; end;
ComputeLeaders(Proto, Leaders);
{ Count blocks and assign block indices }
NBB := 0;
for I := 0 to N - 1 do
if Leaders[I] then begin
PCMap[I] := NBB;
Inc(NBB);
end;
{ Fill non-leader PCs with their block index }
BIdx := -1;
for I := 0 to N - 1 do begin
if Leaders[I] then BIdx := PCMap[I];
PCMap[I] := BIdx;
end;
SetLength(SF^.Blocks, NBB);
for I := 0 to NBB - 1 do begin
SF^.Blocks[I] := Default(TBasicBlock);
SF^.Blocks[I].Index := I;
SF^.Blocks[I].StartPC := -1;
SF^.Blocks[I].EndPC := -1;
SF^.Blocks[I].IDom := -1;
end;
{ Set StartPC / EndPC for each block }
BIdx := -1;
for I := 0 to N - 1 do begin
if Leaders[I] then begin
BIdx := PCMap[I];
SF^.Blocks[BIdx].StartPC := I;
end;
SF^.Blocks[BIdx].EndPC := I;
end;
{ Add edges by examining the terminator of each block }
for BIdx := 0 to NBB - 1 do begin
Blk := @SF^.Blocks[BIdx];
I := Blk^.EndPC;
Inst := Proto^.Code[I];
Op := GET_OPCODE(Inst);
case Op of
OP_JMP: begin
Target := I + 1 + GETARG_sBx(Inst);
AddSucc(BIdx, PCToBlock(PCMap, Target));
end;
OP_EQ, OP_LT, OP_LE, OP_TEST, OP_TESTSET: begin
{ Next instruction is always JMP in well-formed Lua bytecode;
we add the JMP target AND fall-through. }
AddSucc(BIdx, PCToBlock(PCMap, I + 1)); { the JMP block }
AddSucc(BIdx, PCToBlock(PCMap, I + 2)); { skip target }
end;
OP_FORLOOP: begin
Target := I + 1 + GETARG_sBx(Inst); { loop back }
AddSucc(BIdx, PCToBlock(PCMap, Target));
AddSucc(BIdx, PCToBlock(PCMap, I + 1)); { exit }
end;
OP_FORPREP: begin
Target := I + 1 + GETARG_sBx(Inst);
AddSucc(BIdx, PCToBlock(PCMap, Target));
end;
OP_TFORLOOP: begin
AddSucc(BIdx, PCToBlock(PCMap, I + 1)); { JMP or next }
AddSucc(BIdx, PCToBlock(PCMap, I + 2));
end;
OP_LOADBOOL: begin
{ LOADBOOL with C=1 skips the next instruction }
if GETARG_C(Inst) <> 0 then
AddSucc(BIdx, PCToBlock(PCMap, I + 2))
else if I + 1 < N then
AddSucc(BIdx, PCToBlock(PCMap, I + 1));
end;
OP_RETURN, OP_TAILCALL: { no successors } ;
else
{ fall-through }
if I + 1 < N then
AddSucc(BIdx, PCToBlock(PCMap, I + 1));
end;
end;
end;
{ ======================================================================
Step 2 – Compute Dominators (Cooper/Harvey/Kennedy iterative)
====================================================================== }
{ Returns the nearest common dominator of blocks A and B using idom array }
function Intersect(const IDom: array of Integer; A, B: Integer): Integer;
begin
while A <> B do begin
while A > B do A := IDom[A];
while B > A do B := IDom[B];
end;
Result := A;
end;
procedure ComputeDominators(SF: PSSAFunction);
var
N : Integer;
IDom : array of Integer;
Changed: Boolean;
I, J, P, New_IDom: Integer;
begin
N := Length(SF^.Blocks);
if N = 0 then Exit;
SetLength(IDom, N);
for I := 0 to N - 1 do IDom[I] := -1;
IDom[0] := 0;
Changed := True;
while Changed do begin
Changed := False;
{ Post-order traversal approximation: iterate in reverse }
for I := 1 to N - 1 do begin
New_IDom := -1;
for J := 0 to High(SF^.Blocks[I].Preds) do begin
P := SF^.Blocks[I].Preds[J];
if IDom[P] <> -1 then begin
if New_IDom = -1 then
New_IDom := P
else
New_IDom := Intersect(IDom, P, New_IDom);
end;
end;
if New_IDom = -1 then New_IDom := 0; { unreachable -> entry }
if IDom[I] <> New_IDom then begin
IDom[I] := New_IDom;
Changed := True;
end;
end;
end;
for I := 0 to N - 1 do
SF^.Blocks[I].IDom := IDom[I];
{ Build dominator tree children lists }
for I := 1 to N - 1 do begin
J := IDom[I];
if J >= 0 then begin
SetLength(SF^.Blocks[J].DomChildren,
Length(SF^.Blocks[J].DomChildren) + 1);
SF^.Blocks[J].DomChildren[High(SF^.Blocks[J].DomChildren)] := I;
end;
end;
end;
{ ======================================================================
Step 3 – Dominance Frontier
====================================================================== }
procedure AddToDF(SF: PSSAFunction; B, Runner: Integer);
var
I: Integer;
begin
for I := 0 to High(SF^.Blocks[B].DF) do
if SF^.Blocks[B].DF[I] = Runner then Exit;
SetLength(SF^.Blocks[B].DF, Length(SF^.Blocks[B].DF) + 1);
SF^.Blocks[B].DF[High(SF^.Blocks[B].DF)] := Runner;
end;
procedure ComputeDominanceFrontier(SF: PSSAFunction);
var
I, J, Runner: Integer;
begin
for I := 0 to High(SF^.Blocks) do begin
if Length(SF^.Blocks[I].Preds) >= 2 then begin
for J := 0 to High(SF^.Blocks[I].Preds) do begin
Runner := SF^.Blocks[I].Preds[J];
while Runner <> SF^.Blocks[I].IDom do begin
AddToDF(SF, Runner, I);
if Runner = 0 then Break;
Runner := SF^.Blocks[Runner].IDom;
end;
end;
end;
end;
end;
{ ======================================================================
Step 4 – Lift instructions to un-versioned SSA nodes
====================================================================== }
{ Map raw RK argument to TSSARef (unversioned, version=0) }
function RKRef(X: Integer): TSSARef;
begin
if ISK(X) then
Result := MakeConstRef(INDEXRK(X))
else
Result := MakeRef(X, 0);
end;
{ ======================================================================
Synthetic constant helpers
====================================================================== }
function AddSyntheticConst(Proto: PProto; IVal: Int64): Integer;
var
I, N: Integer;
C: TLuaConst;
begin
{ Check if this constant already exists to avoid duplicates }
N := Length(Proto^.K);
for I := 0 to N - 1 do begin
if (Proto^.K[I].Kind = lckInteger) and (Proto^.K[I].IValue = IVal) then begin
Result := I;
Exit;
end;
if (Proto^.K[I].Kind = lckNumber) and (Proto^.K[I].NValue = IVal) and
(Frac(Proto^.K[I].NValue) = 0) then begin
Result := I;
Exit;
end;
end;
{ Append new constant }
SetLength(Proto^.K, N + 1);
C := Default(TLuaConst);
C.Kind := lckInteger;
C.IValue := IVal;
C.NValue := IVal;
Proto^.K[N] := C;
Result := N;
end;
function AddSyntheticFloatConst(Proto: PProto; FVal: Double): Integer;
var
I, N: Integer;
C: TLuaConst;
begin
{ Check if this constant already exists }
N := Length(Proto^.K);
for I := 0 to N - 1 do begin
if (Proto^.K[I].Kind in [lckFloat, lckNumber]) and
(Proto^.K[I].NValue = FVal) then begin
Result := I;
Exit;
end;
end;
{ Append new constant }
SetLength(Proto^.K, N + 1);
C := Default(TLuaConst);
C.Kind := lckFloat;
C.NValue := FVal;
Proto^.K[N] := C;
Result := N;
end;
{ ======================================================================
Version-aware RK resolution for 5.2-5.3
====================================================================== }
function RKRefV(const OT: TOpcodeTable; X: Integer): TSSARef;
begin
if IsRK(OT, X) then
Result := MakeConstRef(RKIndex(OT, X))
else
Result := MakeRef(X, 0);
end;
procedure LiftBlock(SF: PSSAFunction; BIdx: Integer);
var
Proto : PProto;
Blk : PBasicBlock;
I, PC : Integer;
Inst : TInstruction;
Op : TOpCode;
A, B, C, Bx, sBx: Integer;
Node : PSSANode;
Line : Integer;
J, ArgCount, TopSetReg: Integer;
procedure Add(N: PSSANode);
begin
SetLength(Blk^.Nodes, Length(Blk^.Nodes) + 1);
Blk^.Nodes[High(Blk^.Nodes)] := N;
SetLength(SF^.AllNodes, Length(SF^.AllNodes) + 1);
SF^.AllNodes[High(SF^.AllNodes)] := N;
end;
begin
Proto := SF^.Proto;
Blk := @SF^.Blocks[BIdx];
PC := Blk^.StartPC;
while PC <= Blk^.EndPC do begin
Inst := Proto^.Code[PC];
Op := GET_OPCODE(Inst);
A := GETARG_A(Inst);
B := GETARG_B(Inst);
C := GETARG_C(Inst);
Bx := GETARG_Bx(Inst);
sBx := GETARG_sBx(Inst);
if PC < Length(Proto^.LineInfo) then Line := Proto^.LineInfo[PC]
else Line := -1;
case Op of
OP_MOVE: begin
Node := NewNode(SSA_MOVE, PC, Line);
Node^.Dest := MakeRef(A, 0);
Node^.OpA := MakeRef(B, 0);
Add(Node);
end;
OP_LOADK: begin
Node := NewNode(SSA_LOADK, PC, Line);
Node^.Dest := MakeRef(A, 0);
Node^.ConstIdx := Bx;
Add(Node);
end;
OP_LOADBOOL: begin
Node := NewNode(SSA_LOADBOOL, PC, Line);
Node^.Dest := MakeRef(A, 0);
Node^.ImmA := B; { 0 or 1 }
Node^.ImmB := C; { skip flag }
Add(Node);
end;
OP_LOADNIL: begin
Node := NewNode(SSA_LOADNIL, PC, Line);
Node^.ImmA := A; { first reg }
Node^.ImmB := B; { last reg }
{ LOADNIL defines registers A..B; dest is the range start }
Node^.Dest := MakeRef(A, 0);
Add(Node);
end;
OP_GETUPVAL: begin
Node := NewNode(SSA_GETUPVAL, PC, Line);
Node^.Dest := MakeRef(A, 0);
Node^.UpvalIdx := B;
Add(Node);
end;
OP_GETGLOBAL: begin
Node := NewNode(SSA_GETGLOBAL, PC, Line);
Node^.Dest := MakeRef(A, 0);
Node^.ConstIdx := Bx;
Add(Node);
end;
OP_GETTABLE: begin
Node := NewNode(SSA_GETTABLE, PC, Line);
Node^.Dest := MakeRef(A, 0);
Node^.OpA := MakeRef(B, 0);
Node^.OpB := RKRef(C);
Add(Node);
end;
OP_SETGLOBAL: begin
Node := NewNode(SSA_SETGLOBAL, PC, Line);
Node^.OpA := MakeRef(A, 0);
Node^.ConstIdx := Bx;
Add(Node);
end;
OP_SETUPVAL: begin
Node := NewNode(SSA_SETUPVAL, PC, Line);
Node^.OpA := MakeRef(A, 0);
Node^.UpvalIdx := B;
Add(Node);
end;
OP_SETTABLE: begin
Node := NewNode(SSA_SETTABLE, PC, Line);
Node^.OpA := MakeRef(A, 0);
Node^.OpB := RKRef(B);
Node^.OpC := RKRef(C);
Add(Node);
end;
OP_NEWTABLE: begin
Node := NewNode(SSA_NEWTABLE, PC, Line);
Node^.Dest := MakeRef(A, 0);
Node^.ImmA := B; { array size hint }
Node^.ImmB := C; { hash size hint }
Add(Node);
end;
OP_SELF: begin
Node := NewNode(SSA_SELF, PC, Line);
Node^.Dest := MakeRef(A, 0); { method }
Node^.OpA := MakeRef(B, 0); { table }
Node^.OpB := RKRef(C); { key }
Node^.ImmA := A + 1; { self slot }
Add(Node);
end;
OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD, OP_POW: begin
Node := NewNode(SSA_BINOP, PC, Line);
Node^.Dest := MakeRef(A, 0);
Node^.OpA := RKRef(B);
Node^.OpB := RKRef(C);
case Op of
OP_ADD: Node^.BinOp := bopAdd;
OP_SUB: Node^.BinOp := bopSub;
OP_MUL: Node^.BinOp := bopMul;
OP_DIV: Node^.BinOp := bopDiv;
OP_MOD: Node^.BinOp := bopMod;
OP_POW: Node^.BinOp := bopPow;
end;
Add(Node);
end;
OP_UNM, OP_NOT, OP_LEN: begin
Node := NewNode(SSA_UNOP, PC, Line);
Node^.Dest := MakeRef(A, 0);
Node^.OpA := MakeRef(B, 0);
case Op of
OP_UNM: Node^.UnOp := uopNeg;
OP_NOT: Node^.UnOp := uopNot;
OP_LEN: Node^.UnOp := uopLen;
end;
Add(Node);
end;
OP_CONCAT: begin
Node := NewNode(SSA_CONCAT, PC, Line);
Node^.Dest := MakeRef(A, 0);
Node^.ImmA := B; { first register }
Node^.ImmB := C; { last register }
Add(Node);
end;
OP_JMP: begin
Node := NewNode(SSA_JUMP, PC, Line);
Node^.TargetTrue := PC + 1 + sBx;
Add(Node);
end;
OP_EQ: begin
Node := NewNode(SSA_BRANCH, PC, Line);
Node^.RelOp := ropEQ;
Node^.Invert := A <> 0;
Node^.OpA := RKRef(B);
Node^.OpB := RKRef(C);
Add(Node);
end;
OP_LT: begin
Node := NewNode(SSA_BRANCH, PC, Line);
Node^.RelOp := ropLT;
Node^.Invert := A <> 0;
Node^.OpA := RKRef(B);
Node^.OpB := RKRef(C);
Add(Node);
end;
OP_LE: begin
Node := NewNode(SSA_BRANCH, PC, Line);
Node^.RelOp := ropLE;
Node^.Invert := A <> 0;
Node^.OpA := RKRef(B);
Node^.OpB := RKRef(C);
Add(Node);
end;
OP_TEST: begin
Node := NewNode(SSA_BRANCH, PC, Line);
Node^.RelOp := ropTest;
Node^.OpA := MakeRef(A, 0);
Node^.Invert := C <> 0;
Add(Node);
end;
OP_TESTSET: begin
Node := NewNode(SSA_BRANCH, PC, Line);
Node^.RelOp := ropTestSet;
Node^.Dest := MakeRef(A, 0);
Node^.OpA := MakeRef(B, 0);
Node^.Invert := C <> 0;
Add(Node);
end;
OP_CALL: begin
Node := NewNode(SSA_CALL, PC, Line);
Node^.Dest := MakeRef(A, 0); { base of result registers }
Node^.OpA := MakeRef(A, 0); { function register (SSA use) }
Node^.ImmA := A;
Node^.ImmB := B; { arg count + 1 }
Node^.ImmC := C; { ret count + 1 }
if B > 1 then begin
SetLength(Node^.ArgRefs, B - 1);
for J := 0 to B - 2 do
Node^.ArgRefs[J] := MakeRef(A + 1 + J, 0);
end else if B = 0 then begin
{ Variable args: R(A+1) to stack top.
Scan backward for the stack-top-setting instruction. }
TopSetReg := -1;
for J := High(Blk^.Nodes) downto 0 do begin
if (Blk^.Nodes[J]^.Kind = SSA_CALL) and (Blk^.Nodes[J]^.ImmC = 0) then begin
TopSetReg := Blk^.Nodes[J]^.ImmA;
Break;
end;
if (Blk^.Nodes[J]^.Kind = SSA_VARARG) and
(Blk^.Nodes[J]^.ImmB = 0) then begin
TopSetReg := Blk^.Nodes[J]^.Dest.Reg;
Break;
end;
end;
if TopSetReg > A then begin
ArgCount := TopSetReg - A;
SetLength(Node^.ArgRefs, ArgCount);
for J := 0 to ArgCount - 1 do
Node^.ArgRefs[J] := MakeRef(A + 1 + J, 0);
end;
end;
Add(Node);
end;
OP_TAILCALL: begin
Node := NewNode(SSA_TAILCALL, PC, Line);
Node^.OpA := MakeRef(A, 0); { function register }
Node^.ImmA := A;
Node^.ImmB := B;
Node^.ImmC := C;
if B > 1 then begin
SetLength(Node^.ArgRefs, B - 1);
for J := 0 to B - 2 do
Node^.ArgRefs[J] := MakeRef(A + 1 + J, 0);
end else if B = 0 then begin
{ Variable args: R(A+1) to stack top.
Scan backward for the stack-top-setting instruction. }
TopSetReg := -1;
for J := High(Blk^.Nodes) downto 0 do begin
if (Blk^.Nodes[J]^.Kind = SSA_CALL) and (Blk^.Nodes[J]^.ImmC = 0) then begin
TopSetReg := Blk^.Nodes[J]^.ImmA;
Break;
end;
if (Blk^.Nodes[J]^.Kind = SSA_VARARG) and
(Blk^.Nodes[J]^.ImmB = 0) then begin
TopSetReg := Blk^.Nodes[J]^.Dest.Reg;
Break;
end;
end;
if TopSetReg > A then begin
ArgCount := TopSetReg - A;
SetLength(Node^.ArgRefs, ArgCount);
for J := 0 to ArgCount - 1 do
Node^.ArgRefs[J] := MakeRef(A + 1 + J, 0);
end;
end;
Add(Node);
end;
OP_RETURN: begin
Node := NewNode(SSA_RETURN, PC, Line);
Node^.ImmA := A;
Node^.ImmB := B;
if B > 1 then begin
SetLength(Node^.ArgRefs, B - 1);
for J := 0 to B - 2 do
Node^.ArgRefs[J] := MakeRef(A + J, 0);
end else if B = 0 then begin
{ Return all from R(A) to top - scan backward for top-setter }
TopSetReg := -1;
for J := High(Blk^.Nodes) downto 0 do begin
if (Blk^.Nodes[J]^.Kind = SSA_CALL) and (Blk^.Nodes[J]^.ImmC = 0) then begin
TopSetReg := Blk^.Nodes[J]^.ImmA;
Break;
end;
if (Blk^.Nodes[J]^.Kind = SSA_VARARG) and
(Blk^.Nodes[J]^.ImmB = 0) then begin
TopSetReg := Blk^.Nodes[J]^.Dest.Reg;
Break;
end;
end;
if TopSetReg >= A then begin
ArgCount := TopSetReg - A + 1;
SetLength(Node^.ArgRefs, ArgCount);
for J := 0 to ArgCount - 1 do
Node^.ArgRefs[J] := MakeRef(A + J, 0);
end;
end;
Add(Node);
end;
OP_FORLOOP: begin
Node := NewNode(SSA_FORLOOP, PC, Line);
Node^.ImmA := A;
Node^.Dest := MakeRef(A + 3, 0); { exposed loop variable R(A+3) }
Node^.TargetTrue := PC + 1 + sBx; { loop back }
Add(Node);
end;
OP_FORPREP: begin
Node := NewNode(SSA_FORPREP, PC, Line);
Node^.ImmA := A;
Node^.Dest := MakeRef(A, 0); { internal index R(A) }
Node^.TargetTrue := PC + 1 + sBx; { -> FORLOOP }
Add(Node);
end;
OP_TFORLOOP: begin
Node := NewNode(SSA_TFORLOOP, PC, Line);
Node^.ImmA := A;
Node^.ImmC := C; { number of results }
Add(Node);
end;
OP_SETLIST: begin
Node := NewNode(SSA_SETLIST, PC, Line);
Node^.ImmA := A;
Node^.ImmB := B;
Node^.ImmC := C;
Add(Node);
end;
OP_CLOSE: begin
Node := NewNode(SSA_CLOSE, PC, Line);
Node^.ImmA := A;
Add(Node);
end;
OP_CLOSURE: begin
Node := NewNode(SSA_CLOSURE, PC, Line);
Node^.Dest := MakeRef(A, 0);
Node^.ConstIdx := Bx; { sub-proto index }
Add(Node);
{ CLOSURE is followed by NUP pseudo-instructions (MOVE/GETUPVAL)
for upvalue capture; skip them by consuming the block's codes }
if Bx < Length(Proto^.P) then begin
for I := 1 to Proto^.P[Bx]^.NUps do begin
Inc(PC);
if PC > Blk^.EndPC then Break;
{ emit as NOP to preserve PC mapping }
Node := NewNode(SSA_NOP, PC, -1);
Add(Node);
end;
end;
end;
OP_VARARG: begin
Node := NewNode(SSA_VARARG, PC, Line);
Node^.Dest := MakeRef(A, 0);
Node^.ImmA := A;
Node^.ImmB := B; { number of results + 1, 0 = variable }
Add(Node);
end;
end; { case Op }
Inc(PC);
end; { while PC }
end;
procedure LiftAllBlocks(SF: PSSAFunction);
var
I: Integer;
begin
for I := 0 to High(SF^.Blocks) do
LiftBlock(SF, I);
end;
{ ======================================================================
Step 5 – Phi-function insertion (standard worklist algorithm)
====================================================================== }
{ Return True if register Reg is defined anywhere in the block's original code }
function BlockDefinesReg(const Blk: TBasicBlock; Reg: Integer;
LuaVersion: Byte): Boolean;
var
I: Integer;
N: PSSANode;
TForBase: Integer; { first user-visible loop var register }