forked from red-prig/fpPS4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrCFGParser.pas
626 lines (528 loc) · 11.5 KB
/
srCFGParser.pas
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
unit srCFGParser;
{$mode ObjFPC}{$H+}
interface
uses
ps4_pssl,
ginodes,
srNode,
srCFGLabel;
type
PsrCFGBlock=^TsrCFGBlock;
TsrCFGBlockList=specialize TNodeList<PsrCFGBlock>;
TsrCFGBlock=object
pParent,pPrev,pNext:PsrCFGBlock;
FList:TsrCFGBlockList;
pBLabel:PsrLabel;
pELabel:PsrLabel;
bType:TsrBlockType;
term_id:Integer;
function IsEndOf(Adr:TSrcAdr):Boolean;
function IsBigOf(Adr:TSrcAdr):Boolean;
function IsContain(Adr:TSrcAdr):Boolean;
function FindBlock(Adr:TSrcAdr):PsrCFGBlock;
function DownBlock(Adr:TSrcAdr):PsrCFGBlock;
function UpBlock(Adr:TSrcAdr):PsrCFGBlock;
function FindUpLoop:PsrCFGBlock;
Procedure InsertBlockList(New,child:PsrCFGBlock);
function get_level:DWORD;
end;
PsrCodeBlock=^TsrCodeBlock;
TsrCodeBlock=object
pNext:PsrCodeBlock;
//----
FEmit:TCustomEmit;
Body:Pointer;
Size:ptruint;
FLabels:TsrLabels;
FTop:TsrCFGBlock;
Function FindLabel(Adr:TSrcAdr):PsrLabel;
Function FetchLabel(Adr:TSrcAdr):PsrLabel;
Function IsContain(P:Pointer):Boolean;
end;
TsrCFGParser=object
FCursor:TsrLCursor;
pCode:PsrCodeBlock;
pBlock:PsrCFGBlock;
FSPI:TSPI;
procedure _print_label(Adr:TSrcAdr);
procedure Print(base:Pointer);
function Parse(base:Pointer):Byte;
function NextParse:Byte;
Procedure Finalize;
function CheckBlock:Boolean;
Procedure CheckLabel;
Procedure CheckTerm;
function emit_SOP1:Boolean;
function emit_SOPP:Boolean;
Function NewBlock:PsrCFGBlock;
Procedure PushBlock(New:PsrCFGBlock);
function PopBlock:Boolean;
procedure emit_S_BRANCH;
end;
function parse_code_cfg(bType:TsrBlockType;base:Pointer;pCode:PsrCodeBlock):Byte;
implementation
function parse_code_cfg(bType:TsrBlockType;base:Pointer;pCode:PsrCodeBlock):Byte;
var
LParser:TsrCFGParser;
begin
pCode^.FTop.bType:=bType;
LParser:=Default(TsrCFGParser);
LParser.pCode:=pCode;
Result:=LParser.Parse(base);
//LParser.Print(base);
end;
function TsrCFGBlock.IsEndOf(Adr:TSrcAdr):Boolean;
begin
Result:=False;
if (pELabel<>nil) then
begin
Result:=(pELabel^.Adr.get_pc<=Adr.get_pc);
end;
end;
function TsrCFGBlock.IsBigOf(Adr:TSrcAdr):Boolean;
begin
Result:=False;
if (pELabel<>nil) then
begin
Result:=(pELabel^.Adr.get_pc<Adr.get_pc);
end;
end;
function TsrCFGBlock.IsContain(Adr:TSrcAdr):Boolean;
var
b,e:Boolean;
begin
b:=true;
if (pBLabel<>nil) then
begin
b:=(pBLabel^.Adr.get_pc<=Adr.get_pc);
end;
e:=true;
if (pELabel<>nil) then
begin
e:=(pELabel^.Adr.get_pc>Adr.get_pc);
end;
Result:=b and e;
end;
function TsrCFGBlock.FindBlock(Adr:TSrcAdr):PsrCFGBlock;
var
node:PsrCFGBlock;
begin
Result:=nil;
node:=FList.pHead;
While (node<>nil) do
begin
if node^.IsContain(Adr) then Exit(node);
node:=node^.pNext;
end;
end;
function TsrCFGBlock.DownBlock(Adr:TSrcAdr):PsrCFGBlock;
var
next:PsrCFGBlock;
begin
Result:=@Self;
repeat
next:=Result^.FindBlock(Adr);
if (next=nil) then Exit;
Result:=next;
until false;
end;
function TsrCFGBlock.UpBlock(Adr:TSrcAdr):PsrCFGBlock;
var
next:PsrCFGBlock;
begin
Result:=@Self;
While (Result^.IsEndOf(Adr)) do
begin
next:=Result^.pParent;
if (next=nil) then Exit;
Result:=next;
end;
end;
function TsrCFGBlock.FindUpLoop:PsrCFGBlock;
var
node:PsrCFGBlock;
begin
Result:=nil;
node:=@Self;
While (node<>nil) do
begin
if (node^.bType=btLoop) then Exit(node);
node:=node^.pParent;
end;
end;
Procedure TsrCFGBlock.InsertBlockList(New,child:PsrCFGBlock);
var
next:PsrCFGBlock;
begin
if (New=nil) then Exit;
New^.pParent:=@Self;
While (child<>nil) do
begin
Assert(child^.pParent=@Self);
next:=child^.pNext;
FList.Remove(child);
New^.FList.Push_tail(child);
child^.pParent:=New;
child:=next;
end;
FList.Push_tail(New);
end;
function TsrCFGBlock.get_level:DWORD;
var
node:PsrCFGBlock;
begin
node:=@Self;
Result:=0;
While (node<>nil) do
begin
Inc(Result);
node:=node^.pParent;
end;
end;
procedure TsrCFGParser._print_label(Adr:TSrcAdr);
var
pLabel:PsrLabel;
begin
pLabel:=pCode^.FindLabel(Adr);
if (pLabel<>nil) then
begin
Write('label_',HexStr(Adr.Offdw*4,4),': ;');
if (ltBranch in pLabel^.lType) then Write('ltBranch ');
if (ltUnknow in pLabel^.lType) then Write('ltUnknow ');
if (ltBegAdr in pLabel^.lType) then Write('ltBegAdr ');
if (ltEndAdr in pLabel^.lType) then Write('ltEndAdr ');
if (ltBegCond in pLabel^.lType) then Write('ltBegCond ');
if (ltEndCond in pLabel^.lType) then Write('ltEndCond ');
if (ltBegLoop in pLabel^.lType) then Write('ltBegLoop ');
if (ltEndLoop in pLabel^.lType) then Write('ltEndLoop ');
writeln;
end;
end;
procedure TsrCFGParser.Print(base:Pointer);
var
Adr:TSrcAdr;
level:DWORD;
i:Byte;
begin
FCursor.Init(base);
pBlock:=@pCode^.FTop;
repeat
Adr:=FCursor.Adr;
pBlock:=pBlock^.DownBlock(Adr);
level:=pBlock^.get_level;
_print_label(Adr);
Write(' ',HexStr(FCursor.OFFSET_DW*4,4));
Write(Space(level));
FSPI:=Default(TSPI);
i:=FCursor.Next(FSPI);
Case i of
0,1:begin
print_spi(FSPI);
Adr:=FCursor.Adr;
pBlock:=pBlock^.UpBlock(Adr);
end;
end;
until (i<>0);
_print_label(Adr);
end;
function TsrCFGParser.Parse(base:Pointer):Byte;
begin
if (pCode=nil) then Exit(4);
pCode^.Body:=base;
FCursor.Init(base);
pBlock:=@pCode^.FTop;
pCode^.FTop.pBLabel:=pCode^.FetchLabel(FCursor.Adr);
repeat
Result:=NextParse;
Case Result of
0:;
1:Break;
else
Break;
end;
until false;
end;
function TsrCFGParser.NextParse:Byte;
begin
FSPI:=Default(TSPI);
Result:=FCursor.Next(FSPI);
Case Result of
0,1:begin
Case FSPI.CMD.EN of
W_SOP1:if emit_SOP1 then Result:=1;
W_SOPP:if emit_SOPP then Result:=1;
end;
While (CheckBlock) do;
if (Result=0) then
begin
CheckTerm;
end;
CheckLabel;
if (Result=1) then
begin
Finalize;
end;
end;
end;
end;
Procedure TsrCFGParser.Finalize;
begin
pCode^.FTop.pELabel:=pCode^.FetchLabel(FCursor.Adr);
pCode^.Size:=FCursor.OFFSET_DW*4;
end;
function TsrCFGParser.CheckBlock:Boolean;
var
pLabel:PsrLabel;
begin
Result:=False;
if (pBlock=nil) then Exit;
Result:=pBlock^.IsEndOf(FCursor.Adr);
if Result then
begin
Case pBlock^.bType of
btAdr:
begin
pLabel:=pBlock^.pELabel;
pLabel^.AddType(ltEndAdr);
end;
btLoop:
begin
pLabel:=pBlock^.pELabel;
pLabel^.AddType(ltEndLoop);
end;
else;
end;
PopBlock;
end;
end;
Procedure TsrCFGParser.CheckLabel;
var
c_adr:TSrcAdr;
pLabel:array[0..1] of PsrLabel;
node,prev:PsrCFGBlock;
begin
c_adr:=FCursor.Adr;
pLabel[0]:=pCode^.FindLabel(c_adr);
if (pLabel[0]=nil) then Exit;
if not (pLabel[0]^.IsType(ltUnknow)) then Exit;
prev:=pBlock^.FList.pTail;
While (prev<>nil) do
begin
if prev^.IsContain(c_adr) then Break;
prev:=prev^.pPrev;
end;
if (prev=nil) then Exit;
if not (prev^.IsContain(c_adr)) then Exit;
pLabel[1]:=prev^.pBLabel;
pLabel[0]^.RemType(ltUnknow); //ltEndLoop later
pLabel[1]^.AddType(ltBegLoop);
node:=NewBlock;
node^.pBLabel:=pLabel[1];
node^.pELabel:=pLabel[0];
node^.bType:=btLoop;
pBlock^.InsertBlockList(node,prev);
end;
Procedure TsrCFGParser.CheckTerm;
var
c_adr:TSrcAdr;
pLabel:array[0..1] of PsrLabel;
node,prev:PsrCFGBlock;
begin
With pBlock^ do
begin
if (term_id>=0) then Exit;
term_id:=Abs(term_id);
end;
c_adr:=FCursor.Adr;
pLabel[0]:=pCode^.FetchLabel(c_adr);
pLabel[1]:=nil;
pLabel[0]^.RemType(ltUnknow);
pLabel[0]^.AddType(ltBegAdr);
prev:=pBlock^.pParent;
if (prev<>nil) then
begin
pLabel[1]:=prev^.pELabel;
end;
node:=NewBlock;
node^.pBLabel:=pLabel[0];
node^.pELabel:=pLabel[1];
node^.bType:=btAdr;
PushBlock(node);
end;
function TsrCFGParser.emit_SOP1:Boolean;
begin
Result:=False;
Case FSPI.SOP1.OP of
S_SETPC_B64:if (pBlock^.bType=btSetpc) then Result:=True;
end;
end;
function TsrCFGParser.emit_SOPP:Boolean;
var
c_adr:TSrcAdr;
pLabel:PsrLabel;
begin
Result:=False;
Case FSPI.SOPP.OP of
S_ENDPGM:
begin
if (pBlock^.bType=btAdr) then
begin
c_adr:=FCursor.Adr;
pLabel:=pCode^.FetchLabel(c_adr);
pLabel^.AddType(ltEndAdr);
pBlock^.pELabel:=pLabel;
PopBlock;
end;
With pBlock^ do
begin
term_id:=-(Abs(term_id)+1);
end;
end;
S_CBRANCH_SCC0 ,
S_CBRANCH_SCC1 ,
S_CBRANCH_VCCZ ,
S_CBRANCH_VCCNZ ,
S_CBRANCH_EXECZ ,
S_CBRANCH_EXECNZ,
S_BRANCH :emit_S_BRANCH;
else;
end;
end;
Function TsrCodeBlock.FindLabel(Adr:TSrcAdr):PsrLabel;
var
node:TsrLabel;
begin
Result:=nil;
node:=Default(TsrLabel);
node.Adr:=Adr;
Result:=FLabels.FNTree.Find(@node);
end;
Function TsrCodeBlock.FetchLabel(Adr:TSrcAdr):PsrLabel;
var
node:TsrLabel;
begin
Result:=nil;
node:=Default(TsrLabel);
node.Adr:=Adr;
Result:=FLabels.FNTree.Find(@node);
if (Result=nil) then
begin
Result:=FEmit.Alloc(SizeOf(TsrLabel));
Result^.Adr:=Adr;
FLabels.FNTree.Insert(Result);
end;
end;
Function TsrCodeBlock.IsContain(P:Pointer):Boolean;
begin
Result:=(Body<=P) and ((Body+Size)>P);
end;
Function TsrCFGParser.NewBlock:PsrCFGBlock;
begin
Result:=pCode^.FEmit.Alloc(SizeOf(TsrCFGBlock));
end;
Procedure TsrCFGParser.PushBlock(New:PsrCFGBlock);
begin
if (New=nil) then Exit;
pBlock^.FList.Push_tail(New);
New^.pParent:=pBlock;
pBlock:=New;
end;
function TsrCFGParser.PopBlock:Boolean;
begin
Result:=False;
if (pBlock=nil) then Exit;
if (pBlock^.pParent=nil) then Exit;
pBlock:=pBlock^.pParent;
Result:=True;
end;
procedure TsrCFGParser.emit_S_BRANCH;
var
pLabel:array[0..1] of PsrLabel;
c_adr,b_adr,t_adr:TSrcAdr;
node,child,prev,parent:PsrCFGBlock;
begin
c_adr:=FCursor.Adr;
b_adr:=c_adr;
b_adr.Offdw:=get_branch_offset(FSPI);
pLabel[0]:=pCode^.FetchLabel(c_adr);
pLabel[1]:=pCode^.FetchLabel(b_adr);
pLabel[0]^.AddType(ltBranch);
if (SmallInt(FSPI.SOPP.SIMM)<0) then //up
begin
child:=pBlock^.FList.pTail;
parent:=pBlock;
repeat
if (parent^.pBLabel=nil) then
begin
t_adr:=c_adr;
t_adr.Offdw:=0;
end else
begin
t_adr:=parent^.pBLabel^.Adr;
end;
if (parent^.bType=btLoop) and (t_adr.get_pc=b_adr.get_pc) then //is exist loop
begin
pLabel[0]^.RemType(ltUnknow); //ltEndLoop later
if (parent^.pELabel=nil) then
begin
parent^.pELabel:=pLabel[0]; //set end
end else
begin
t_adr:=parent^.pELabel^.Adr;
if (t_adr.get_pc<c_adr.get_pc) then
begin
parent^.pELabel:=pLabel[0]; //update end of loop
end;
end;
Exit;
end;
if (t_adr.get_pc<=b_adr.get_pc) then Break;
if (parent^.pParent=nil) then Break;
child:=parent;
parent:=parent^.pParent;
until false;
if (child<>nil) then
repeat //up list
if child^.IsContain(t_adr) then Assert(false);
prev:=child^.pPrev;
if (prev=nil) then Break;
if (prev^.pELabel=nil) then
begin
t_adr:=c_adr;
end else
begin
t_adr:=prev^.pELabel^.Adr;
end;
if (t_adr.get_pc<=b_adr.get_pc) then Break;
child:=prev;
until false;
//new loop block
pLabel[0]^.RemType(ltUnknow); //ltEndLoop later
pLabel[1]^.AddType(ltBegLoop);
node:=NewBlock;
node^.pBLabel:=pLabel[1];
node^.pELabel:=pLabel[0];
node^.bType:=btLoop;
parent^.InsertBlockList(node,child);
end else //down
begin
if (pBlock^.pELabel<>nil) then
if (pBlock^.pELabel^.Adr.get_pc<b_adr.get_pc) then
begin
//push adr or loop end
if not pLabel[1]^.IsType(ltBegAdr) then
begin
pLabel[1]^.AddType(ltUnknow);
end;
Exit;
end;
pLabel[0]^.AddType(ltBegCond);
pLabel[1]^.AddType(ltEndCond);
node:=NewBlock;
node^.pBLabel:=pLabel[0];
node^.pELabel:=pLabel[1];
node^.bType:=btCond;
PushBlock(node);
end;
end;
end.