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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
|
#include "compiler/PCodeAssembly.h"
#include "compiler/CError.h"
#include "compiler/CFunc.h"
#include "compiler/CMangler.h"
#include "compiler/CParser.h"
#include "compiler/CodeGen.h"
#include "compiler/ObjGenMachO.h"
#include "compiler/PCode.h"
#include "compiler/PCodeInfo.h"
#include "compiler/PCodeListing.h"
#include "compiler/PPCError.h"
#include "compiler/RegisterInfo.h"
#include "compiler/StackFrame.h"
#include "compiler/TOC.h"
#include "compiler/objects.h"
static UInt32 codebase;
static SInt32 pcode_update_mem_labeldiff_imm(PCode *instr, const PCodeArg *op, WeirdOperand *wop) {
SInt32 offset;
Object *object;
UInt8 arg;
if (op->kind == PCOp_MEMORY) {
object = op->data.mem.obj;
offset = op->data.mem.offset;
switch (object->datatype) {
case DLOCAL:
switch ((UInt8) op->arg) {
case RefType_1:
offset += local_offset_16(object);
break;
case RefType_D:
offset = local_offset_lo(object, offset);
break;
case RefType_C:
offset = local_offset_ha(object, offset);
break;
default:
CError_FATAL(83);
}
break;
case DDATA:
case DFUNC:
case DVFUNC:
case DNONLAZYPTR:
switch ((UInt8) op->arg) {
case RefType_6:
wop->type = MW_RELOC_5_LO16;
break;
case RefType_2:
wop->type = MW_RELOC_3;
break;
case RefType_3:
wop->type = MW_RELOC_4;
break;
case RefType_8:
wop->type = MW_RELOC_7_HA16;
break;
case RefType_7:
wop->type = MW_RELOC_6_HI16;
break;
default:
CError_FATAL(135);
}
wop->x2 = object;
CError_ASSERT(144, offset == 0);
break;
default:
CError_FATAL(164);
}
} else if (op->kind == PCOp_LABELDIFF) {
arg = op->arg;
offset = op->data.labeldiff.labelA->block->codeOffset - op->data.labeldiff.labelB->block->codeOffset;
offset += op->data.labeldiff.offset;
if (arg == 1)
offset = -offset;
if (offset > 0x7FFF)
PPCError_Error(PPCErrorStr109);
else if (offset < -0x8000)
PPCError_Error(PPCErrorStr109);
} else if (op->kind == PCOp_IMMEDIATE) {
offset = op->data.imm.value;
} else {
CError_FATAL(193);
}
return offset;
}
UInt32 assemblepcode(PCode *instr, UInt32 offset, WeirdOperand *wop) {
UInt32 bits;
bits = opcodeinfo[instr->op].insn;
wop->type = -1;
wop->x6 = 0;
switch (instr->op) {
case PC_BL: {
int flag = PCODE_FLAG_SET_T(instr) & fAbsolute;
if (instr->args[0].kind == PCOp_MEMORY) {
bits |= instr->args[0].data.mem.offset & 0x3FFFFFC;
wop->type = MW_RELOC_2_BR24;
wop->x2 = instr->args[0].data.mem.obj;
if (flag == 0)
wop->type = MW_RELOC_2_BR24;
else
CError_FATAL(246);
} else if (instr->args[0].kind == PCOp_IMMEDIATE) {
bits |= instr->args[0].data.imm.value & 0x3FFFFFC;
if (flag)
bits |= 2;
} else {
bits |= (instr->args[0].data.label.label->block->codeOffset - offset) & 0x3FFFFFC;
if (flag)
CError_FATAL(261);
}
break;
}
case PC_B: {
int flag = PCODE_FLAG_SET_T(instr) & fAbsolute;
if (instr->args[0].kind == PCOp_MEMORY) {
bits |= instr->args[0].data.mem.offset & 0x3FFFFFC;
wop->x2 = instr->args[0].data.mem.obj;
if (flag == 0)
wop->type = MW_RELOC_2_BR24;
else
CError_FATAL(288);
} else if (instr->args[0].kind == PCOp_IMMEDIATE) {
bits |= instr->args[0].data.imm.value & 0x3FFFFFC;
if (flag)
bits |= 2;
} else {
bits |= (instr->args[0].data.label.label->block->codeOffset - offset) & 0x3FFFFFC;
if (flag)
CError_FATAL(302);
}
if (PCODE_FLAG_SET_T(instr) & fLink)
bits |= 1;
break;
}
case PC_BDNZ:
case PC_BDZ: {
int flag = PCODE_FLAG_SET_T(instr) & fAbsolute;
if (instr->args[0].kind == PCOp_MEMORY) {
bits |= instr->args[0].data.mem.offset & 0xFFFC;
wop->x2 = instr->args[0].data.mem.obj;
if (flag == 0)
wop->type = MW_RELOC_8;
else
CError_FATAL(333);
} else {
SInt32 value;
if (instr->args[0].kind == PCOp_IMMEDIATE)
value = instr->args[0].data.imm.value;
else
value = instr->args[0].data.label.label->block->codeOffset - offset;
bits |= value & 0xFFFF;
if (value < 0) {
if (PCODE_FLAG_SET_T(instr) & fBranchNotTaken)
bits |= 0x200000u;
} else {
if (PCODE_FLAG_SET_T(instr) & fBranchTaken)
bits |= 0x200000u;
}
}
if (PCODE_FLAG_SET_T(instr) & fLink)
bits |= 1;
break;
}
case PC_BC: {
int flag = PCODE_FLAG_SET_T(instr) & fAbsolute;
bits |= (instr->args[0].data.imm.value & 31) << 21;
bits |= ((instr->args[1].data.reg.reg * 4 + instr->args[2].data.imm.value) & 31) << 16;
if (instr->args[3].kind == PCOp_MEMORY) {
bits |= instr->args[3].data.mem.offset & 0xFFFC;
wop->x2 = instr->args[3].data.mem.obj;
if (flag == 0)
wop->type = MW_RELOC_8;
else
CError_FATAL(387);
} else {
SInt32 value;
if (instr->args[3].kind == PCOp_IMMEDIATE)
value = instr->args[3].data.imm.value;
else
value = instr->args[3].data.label.label->block->codeOffset - offset;
bits |= value & 0xFFFF;
if (value < 0) {
if (PCODE_FLAG_SET_T(instr) & fBranchNotTaken)
bits |= 0x200000u;
} else {
if (PCODE_FLAG_SET_T(instr) & fBranchTaken)
bits |= 0x200000u;
}
}
if (PCODE_FLAG_SET_T(instr) & fLink)
bits |= 1;
break;
}
case PC_BT:
case PC_BF:
case PC_BDNZT:
case PC_BDNZF:
case PC_BDZT:
case PC_BDZF: {
int flag = PCODE_FLAG_SET_T(instr) & fAbsolute;
bits |= ((instr->args[0].data.reg.reg * 4 + instr->args[1].data.imm.value) & 31) << 16;
if (instr->args[2].kind == PCOp_MEMORY) {
bits |= instr->args[2].data.mem.offset & 0xFFFC;
wop->x2 = instr->args[2].data.mem.obj;
if (flag == 0)
wop->type = MW_RELOC_8;
else
CError_FATAL(446);
} else {
SInt32 value;
if (instr->args[2].kind == PCOp_IMMEDIATE) {
value = instr->args[2].data.imm.value;
if (flag)
bits |= 2;
} else {
value = instr->args[2].data.label.label->block->codeOffset - offset;
CError_ASSERT(458, !flag);
}
bits |= value & 0xFFFF;
if (value < 0) {
if (PCODE_FLAG_SET_T(instr) & fBranchNotTaken)
bits |= 0x200000u;
} else {
if (PCODE_FLAG_SET_T(instr) & fBranchTaken)
bits |= 0x200000u;
}
}
if (PCODE_FLAG_SET_T(instr) & fLink)
bits |= 1;
break;
}
case PC_BTLR:
case PC_BTCTR:
case PC_BFLR:
case PC_BFCTR:
bits |= ((instr->args[0].data.reg.reg * 4 + instr->args[1].data.imm.value) & 31) << 16;
if (PCODE_FLAG_SET_T(instr) & fLink)
bits |= 1;
if (PCODE_FLAG_SET_T(instr) & fBranchTaken)
bits |= 0x200000u;
break;
case PC_BCLR:
case PC_BCCTR:
bits |= instr->args[0].data.imm.value << 21;
bits |= ((instr->args[1].data.reg.reg * 4 + instr->args[2].data.imm.value) & 31) << 16;
case PC_BLR:
case PC_BCTR:
case PC_BCTRL:
case PC_BLRL:
if (PCODE_FLAG_SET_T(instr) & fLink)
bits |= 1;
if (PCODE_FLAG_SET_T(instr) & fBranchTaken)
bits |= 0x200000u;
break;
case PC_CRAND:
case PC_CRANDC:
case PC_CREQV:
case PC_CRNAND:
case PC_CRNOR:
case PC_CROR:
case PC_CRORC:
case PC_CRXOR:
bits |= ((instr->args[0].data.reg.reg * 4 + instr->args[1].data.imm.value) & 31) << 21;
bits |= ((instr->args[2].data.reg.reg * 4 + instr->args[3].data.imm.value) & 31) << 16;
bits |= ((instr->args[4].data.reg.reg * 4 + instr->args[5].data.imm.value) & 31) << 11;
break;
case PC_MCRF:
bits |= instr->args[0].data.reg.reg << 23;
bits |= instr->args[1].data.reg.reg << 18;
break;
case PC_LBZ:
case PC_LBZU:
case PC_LHZ:
case PC_LHZU:
case PC_LHA:
case PC_LHAU:
case PC_LWZ:
case PC_LWZU:
case PC_LMW:
case PC_STB:
case PC_STBU:
case PC_STH:
case PC_STHU:
case PC_STW:
case PC_STWU:
case PC_STMW:
case PC_LFS:
case PC_LFSU:
case PC_LFD:
case PC_LFDU:
case PC_STFS:
case PC_STFSU:
case PC_STFD:
case PC_STFDU:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= pcode_update_mem_labeldiff_imm(instr, &instr->args[2], wop) & 0xFFFF;
break;
case PC_LBZX:
case PC_LBZUX:
case PC_LHZX:
case PC_LHZUX:
case PC_LHAX:
case PC_LHAUX:
case PC_LHBRX:
case PC_LWZX:
case PC_LWZUX:
case PC_LWBRX:
case PC_STBX:
case PC_STBUX:
case PC_STHX:
case PC_STHUX:
case PC_STHBRX:
case PC_STWX:
case PC_STWUX:
case PC_STWBRX:
case PC_LFSX:
case PC_LFSUX:
case PC_LFDX:
case PC_LFDUX:
case PC_STFSX:
case PC_STFSUX:
case PC_STFDX:
case PC_STFDUX:
case PC_LWARX:
case PC_LSWX:
case PC_STFIWX:
case PC_STSWX:
case PC_STWCX:
case PC_ECIWX:
case PC_ECOWX:
case PC_DCREAD:
case PC_TLBSX:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_DCBF:
case PC_DCBST:
case PC_DCBT:
case PC_DCBTST:
case PC_DCBZ:
case PC_DCBI:
case PC_ICBI:
case PC_DCCCI:
case PC_ICBT:
case PC_ICCCI:
case PC_ICREAD:
case PC_DCBA:
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[1].data.reg.reg << 11;
break;
case PC_ADD:
case PC_ADDC:
case PC_ADDE:
case PC_DIVW:
case PC_DIVWU:
case PC_MULHW:
case PC_MULHWU:
case PC_MULLW:
case PC_SUBF:
case PC_SUBFC:
case PC_SUBFE:
bits |= instr->args[2].data.reg.reg << 11;
case PC_ADDME:
case PC_ADDZE:
case PC_NEG:
case PC_SUBFME:
case PC_SUBFZE:
case PC_MFROM:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
if (PCODE_FLAG_SET_F(instr) & fOverflow)
bits |= 0x400;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_ADDI:
case PC_ADDIC:
case PC_ADDICR:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= pcode_update_mem_labeldiff_imm(instr, &instr->args[2], wop) & 0xFFFF;
break;
case PC_ADDIS:
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[0].data.reg.reg << 21;
bits |= pcode_update_mem_labeldiff_imm(instr, &instr->args[2], wop) & 0xFFFF;
break;
case PC_MULLI:
case PC_SUBFIC:
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[2].data.imm.value & 0xFFFF;
break;
case PC_LI:
case PC_LIS:
bits |= instr->args[0].data.reg.reg << 21;
bits |= pcode_update_mem_labeldiff_imm(instr, &instr->args[1], wop) & 0xFFFF;
break;
case PC_ANDI:
case PC_ANDIS:
case PC_ORI:
case PC_ORIS:
case PC_XORI:
case PC_XORIS:
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[1].data.reg.reg << 21;
bits |= pcode_update_mem_labeldiff_imm(instr, &instr->args[2], wop) & 0xFFFF;
break;
case PC_AND:
case PC_OR:
case PC_XOR:
case PC_NAND:
case PC_NOR:
case PC_EQV:
case PC_ANDC:
case PC_ORC:
case PC_SLW:
case PC_SRW:
case PC_SRAW:
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[1].data.reg.reg << 21;
bits |= instr->args[2].data.reg.reg << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_EXTSH:
case PC_EXTSB:
case PC_CNTLZW:
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[1].data.reg.reg << 21;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_MR:
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[1].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_NOT:
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[1].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_SRAWI:
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[1].data.reg.reg << 21;
bits |= (instr->args[2].data.imm.value & 31) << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_RLWINM:
case PC_RLWIMI:
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[1].data.reg.reg << 21;
bits |= (instr->args[2].data.imm.value & 31) << 11;
bits |= (instr->args[3].data.imm.value & 31) << 6;
bits |= (instr->args[4].data.imm.value & 31) << 1;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_RLWNM:
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[1].data.reg.reg << 21;
bits |= instr->args[2].data.reg.reg << 11;
bits |= (instr->args[3].data.imm.value & 31) << 6;
bits |= (instr->args[4].data.imm.value & 31) << 1;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_CMP:
case PC_CMPL:
bits |= instr->args[0].data.reg.reg << 23;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
break;
case PC_CMPI:
case PC_CMPLI:
bits |= instr->args[0].data.reg.reg << 23;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.imm.value & 0xFFFF;
break;
case PC_MTXER:
case PC_MTCTR:
case PC_MTLR:
case PC_MTMSR:
case PC_MFMSR:
case PC_MFXER:
case PC_MFCTR:
case PC_MFLR:
case PC_MFCR:
bits |= instr->args[0].data.reg.reg << 21;
break;
case PC_MFFS:
bits |= instr->args[0].data.reg.reg << 21;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_MTCRF:
bits |= instr->args[0].data.imm.value << 12;
bits |= instr->args[1].data.reg.reg << 21;
break;
case PC_MTFSF:
bits |= (instr->args[0].data.imm.value & 0xFF) << 17;
bits |= instr->args[1].data.reg.reg << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_FMR:
case PC_FABS:
case PC_FNEG:
case PC_FNABS:
case PC_FRES:
case PC_FRSQRTE:
case PC_FRSP:
case PC_FCTIW:
case PC_FCTIWZ:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_FADD:
case PC_FADDS:
case PC_FSUB:
case PC_FSUBS:
case PC_FDIV:
case PC_FDIVS:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_FMADD:
case PC_FMADDS:
case PC_FMSUB:
case PC_FMSUBS:
case PC_FNMADD:
case PC_FNMADDS:
case PC_FNMSUB:
case PC_FNMSUBS:
case PC_FSEL:
bits |= instr->args[3].data.reg.reg << 11;
case PC_FMUL:
case PC_FMULS:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 6;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_FCMPU:
case PC_FCMPO:
bits |= instr->args[0].data.reg.reg << 23;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
break;
case PC_MTSPR:
if (instr->args[0].kind == PCOp_REGISTER) {
CError_ASSERT(1027, instr->args[0].arg == RegClass_SPR);
CError_ASSERT(1028, instr->args[0].data.reg.reg < 4);
bits |= ((spr_to_sysreg[instr->args[0].data.reg.reg] & 0x1F) << 16) +
((spr_to_sysreg[instr->args[0].data.reg.reg] & 0x3E0) << 6);
} else if (instr->args[0].kind == PCOp_SYSREG && instr->args[0].arg == 0) {
bits |= ((instr->args[0].data.reg.reg & 0x1F) << 16) +
((instr->args[0].data.reg.reg & 0x3E0) << 6);
} else {
CError_FATAL(1033);
}
bits |= instr->args[1].data.reg.reg << 21;
break;
case PC_MTDCR:
if (instr->args[0].kind == PCOp_IMMEDIATE) {
bits |= ((instr->args[0].data.imm.value & 0x1F) << 16) +
((instr->args[0].data.imm.value & 0x3E0) << 6);
} else {
CError_FATAL(1042);
}
bits |= instr->args[1].data.reg.reg << 21;
break;
case PC_MFSPR:
bits |= instr->args[0].data.reg.reg << 21;
if (instr->args[1].kind == PCOp_REGISTER && instr->args[1].arg == RegClass_SPR) {
CError_ASSERT(1055, instr->args[1].data.reg.reg < 4);
bits |= ((spr_to_sysreg[instr->args[1].data.reg.reg] & 0x1F) << 16) +
((spr_to_sysreg[instr->args[1].data.reg.reg] & 0x3E0) << 6);
} else if (instr->args[1].kind == PCOp_SYSREG && instr->args[1].arg == 0) {
bits |= ((instr->args[1].data.reg.reg & 0x1F) << 16) +
((instr->args[1].data.reg.reg & 0x3E0) << 6);
} else {
CError_FATAL(1060);
}
break;
case PC_MFDCR:
bits |= instr->args[0].data.reg.reg << 21;
if (instr->args[1].kind == PCOp_IMMEDIATE) {
bits |= ((instr->args[1].data.imm.value & 0x1F) << 16) +
((instr->args[1].data.imm.value & 0x3E0) << 6);
} else {
CError_FATAL(1069);
}
break;
case PC_LSWI:
case PC_STSWI:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= (instr->args[2].data.imm.value & 31) << 11;
break;
case PC_MCRFS:
bits |= (instr->args[1].data.imm.value & 7) << 18;
case PC_MCRXR:
bits |= instr->args[0].data.reg.reg << 23;
break;
case PC_MFTB:
bits |= instr->args[0].data.reg.reg << 21;
if (instr->args[1].kind == PCOp_SYSREG && instr->args[1].arg == 0) {
if (instr->args[1].data.reg.reg == 284)
bits |= 0xC4000u;
else if (instr->args[1].data.reg.reg == 285)
bits |= 0xD4000u;
else
CError_FATAL(1100);
} else {
CError_FATAL(1103);
}
break;
case PC_MTSR:
bits |= instr->args[1].data.reg.reg << 21;
bits |= (instr->args[0].data.imm.value & 15) << 16;
break;
case PC_MFSR:
bits |= instr->args[0].data.reg.reg << 21;
bits |= (instr->args[1].data.imm.value & 15) << 16;
break;
case PC_MFSRIN:
case PC_MTSRIN:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 11;
break;
case PC_MTFSB0:
case PC_MTFSB1:
bits |= (instr->args[0].data.imm.value & 31) << 21;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_MTFSFI:
bits |= instr->args[0].data.reg.reg << 23;
bits |= (instr->args[1].data.imm.value & 15) << 12;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_FSQRT:
case PC_FSQRTS:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_TLBIE:
case PC_TLBLD:
case PC_TLBLI:
bits |= instr->args[0].data.reg.reg << 11;
break;
case PC_TW:
bits |= (instr->args[0].data.imm.value & 31) << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
break;
case PC_TWI:
bits |= (instr->args[0].data.imm.value & 31) << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.imm.value & 0xFFFF;
break;
case PC_OPWORD:
CError_ASSERT(1176, instr->args[0].kind != PCOp_MEMORY);
bits = pcode_update_mem_labeldiff_imm(instr, &instr->args[0], wop);
break;
case PC_MASKG:
case PC_MASKIR:
bits |= instr->args[1].data.reg.reg << 21;
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_LSCBX:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_DIV:
case PC_DIVS:
case PC_DOZ:
case PC_MUL:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
if (PCODE_FLAG_SET_F(instr) & fOverflow)
bits |= 0x400;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_NABS:
case PC_ABS:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
if (PCODE_FLAG_SET_F(instr) & fOverflow)
bits |= 0x400;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_CLCS:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_DOZI:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.imm.value & 0xFFFF;
break;
case PC_RLMI:
bits |= instr->args[1].data.reg.reg << 21;
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
bits |= (instr->args[3].data.imm.value & 31) << 6;
bits |= (instr->args[4].data.imm.value & 31) << 1;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_SLE:
case PC_SLEQ:
case PC_SLLQ:
case PC_SLQ:
case PC_SRAQ:
case PC_SRE:
case PC_SREA:
case PC_SREQ:
case PC_SRLQ:
case PC_SRQ:
case PC_RRIB:
bits |= instr->args[1].data.reg.reg << 21;
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_SLIQ:
case PC_SLLIQ:
case PC_SRAIQ:
case PC_SRIQ:
case PC_SRLIQ:
bits |= instr->args[1].data.reg.reg << 21;
bits |= instr->args[0].data.reg.reg << 16;
bits |= (instr->args[2].data.imm.value & 31) << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 1;
break;
case PC_TLBRE:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= (instr->args[2].data.imm.value & 1) << 11;
break;
case PC_TLBWE:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= (instr->args[2].data.imm.value & 1) << 11;
break;
case PC_WRTEE:
bits |= instr->args[0].data.reg.reg << 21;
break;
case PC_WRTEEI:
bits |= instr->args[0].data.imm.value << 15;
break;
case PC_DSTT:
case PC_DSTSTT:
bits |= 0x2000000u;
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[1].data.reg.reg << 11;
bits |= (instr->args[2].data.imm.value & 3) << 21;
break;
case PC_DST:
case PC_DSTST:
bits |= (instr->args[3].data.imm.value & 1) << 25;
bits |= instr->args[0].data.reg.reg << 16;
bits |= instr->args[1].data.reg.reg << 11;
bits |= (instr->args[2].data.imm.value & 3) << 21;
break;
case PC_DSSALL:
bits |= 0x2000000u;
break;
case PC_DSS:
bits |= (instr->args[1].data.imm.value & 1) << 25;
bits |= (instr->args[0].data.imm.value & 3) << 21;
break;
case PC_LVEBX:
case PC_LVEHX:
case PC_LVEWX:
case PC_LVSL:
case PC_LVSR:
case PC_LVX:
case PC_LVXL:
case PC_STVEBX:
case PC_STVEHX:
case PC_STVEWX:
case PC_STVX:
case PC_STVXL:
case PC_VADDCUW:
case PC_VADDFP:
case PC_VADDSBS:
case PC_VADDSHS:
case PC_VADDSWS:
case PC_VADDUBM:
case PC_VADDUBS:
case PC_VADDUHM:
case PC_VADDUHS:
case PC_VADDUWM:
case PC_VADDUWS:
case PC_VAND:
case PC_VANDC:
case PC_VAVGSB:
case PC_VAVGSH:
case PC_VAVGSW:
case PC_VAVGUB:
case PC_VAVGUH:
case PC_VAVGUW:
case PC_VMAXFP:
case PC_VMAXSB:
case PC_VMAXSH:
case PC_VMAXSW:
case PC_VMAXUB:
case PC_VMAXUH:
case PC_VMAXUW:
case PC_VMINFP:
case PC_VMINSB:
case PC_VMINSH:
case PC_VMINSW:
case PC_VMINUB:
case PC_VMINUH:
case PC_VMINUW:
case PC_VMRGHB:
case PC_VMRGHH:
case PC_VMRGHW:
case PC_VMRGLB:
case PC_VMRGLH:
case PC_VMRGLW:
case PC_VMULESB:
case PC_VMULESH:
case PC_VMULEUB:
case PC_VMULEUH:
case PC_VMULOSB:
case PC_VMULOSH:
case PC_VMULOUB:
case PC_VMULOUH:
case PC_VNOR:
case PC_VOR:
case PC_VPKPX:
case PC_VPKSHSS:
case PC_VPKSHUS:
case PC_VPKSWSS:
case PC_VPKSWUS:
case PC_VPKUHUM:
case PC_VPKUHUS:
case PC_VPKUWUM:
case PC_VPKUWUS:
case PC_VRLB:
case PC_VRLH:
case PC_VRLW:
case PC_VSL:
case PC_VSLB:
case PC_VSLH:
case PC_VSLO:
case PC_VSLW:
case PC_VSR:
case PC_VSRAB:
case PC_VSRAH:
case PC_VSRAW:
case PC_VSRB:
case PC_VSRH:
case PC_VSRO:
case PC_VSRW:
case PC_VSUBCUW:
case PC_VSUBFP:
case PC_VSUBSBS:
case PC_VSUBSHS:
case PC_VSUBSWS:
case PC_VSUBUBM:
case PC_VSUBUBS:
case PC_VSUBUHM:
case PC_VSUBUHS:
case PC_VSUBUWM:
case PC_VSUBUWS:
case PC_VSUMSWS:
case PC_VSUM2SWS:
case PC_VSUM4SBS:
case PC_VSUM4SHS:
case PC_VSUM4UBS:
case PC_VXOR:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
break;
case PC_VCFSX:
case PC_VCFUX:
case PC_VCTSXS:
case PC_VCTUXS:
case PC_VSPLTB:
case PC_VSPLTH:
case PC_VSPLTW:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 11;
bits |= (instr->args[2].data.imm.value & 31) << 16;
break;
case PC_VEXPTEFP:
case PC_VLOGEFP:
case PC_VREFP:
case PC_VRFIM:
case PC_VRFIN:
case PC_VRFIP:
case PC_VRFIZ:
case PC_VRSQRTEFP:
case PC_VUPKHPX:
case PC_VUPKHSB:
case PC_VUPKHSH:
case PC_VUPKLPX:
case PC_VUPKLSB:
case PC_VUPKLSH:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 11;
break;
case PC_VCMPBFP:
case PC_VCMPEQFP:
case PC_VCMPEQUB:
case PC_VCMPEQUH:
case PC_VCMPEQUW:
case PC_VCMPGEFP:
case PC_VCMPGTFP:
case PC_VCMPGTSB:
case PC_VCMPGTSH:
case PC_VCMPGTSW:
case PC_VCMPGTUB:
case PC_VCMPGTUH:
case PC_VCMPGTUW:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
if (PCODE_FLAG_SET_F(instr) & fRecordBit)
bits |= 0x400;
break;
case PC_VSPLTISB:
case PC_VSPLTISH:
case PC_VSPLTISW:
bits |= instr->args[0].data.reg.reg << 21;
bits |= (instr->args[1].data.imm.value & 31) << 16;
break;
case PC_VMHADDSHS:
case PC_VMHRADDSHS:
case PC_VMLADDUHM:
case PC_VMSUMMBM:
case PC_VMSUMSHM:
case PC_VMSUMSHS:
case PC_VMSUMUBM:
case PC_VMSUMUHM:
case PC_VMSUMUHS:
case PC_VPERM:
case PC_VSEL:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
bits |= instr->args[3].data.reg.reg << 6;
break;
case PC_VMADDFP:
case PC_VNMSUBFP:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 6;
bits |= instr->args[3].data.reg.reg << 11;
break;
case PC_VSLDOI:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[2].data.reg.reg << 11;
bits |= (instr->args[3].data.imm.value & 15) << 6;
break;
case PC_VMR:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[1].data.reg.reg << 11;
break;
case PC_VMRP:
bits |= instr->args[0].data.reg.reg << 21;
bits |= instr->args[1].data.reg.reg << 16;
bits |= instr->args[1].data.reg.reg << 11;
break;
case PC_MFVSCR:
bits |= instr->args[0].data.reg.reg << 21;
break;
case PC_MTVSCR:
bits |= instr->args[0].data.reg.reg << 11;
break;
case PC_EIEIO:
case PC_ISYNC:
case PC_SYNC:
case PC_RFI:
case PC_NOP:
case PC_SC:
case PC_TLBIA:
case PC_TLBSYNC:
case PC_TRAP:
case PC_DSA:
case PC_ESA:
case PC_RFCI:
break;
default:
CError_FATAL(2203);
}
return CTool_EndianConvertWord32(bits);
}
static PCode *targetinstruction(PCodeLabel *label) {
PCodeBlock *block = label->block;
while (block->pcodeCount == 0)
block = block->nextBlock;
return block->firstPCode;
}
static void invertybit(PCode *instr, SInt32 value) {
if (instr->op == PC_BC) {
if (instr->args[0].data.imm.value & 1) {
if (value < 0)
instr->flags |= fBranchNotTaken;
else
instr->flags |= fBranchTaken;
}
} else if (instr->op == PC_BCCTR || instr->op == PC_BCLR) {
if (instr->args[0].data.imm.value & 1)
instr->flags |= fBranchTaken;
}
if (PCODE_FLAG_SET_T(instr) & fBranchTaken)
instr->flags = (instr->flags & ~fBranchTaken) | fBranchNotTaken;
else if (PCODE_FLAG_SET_T(instr) & fBranchNotTaken)
instr->flags = (instr->flags & ~fBranchNotTaken) | fBranchTaken;
else if (value < 0)
instr->flags = (instr->flags & ~fBranchTaken) | fBranchNotTaken;
else
instr->flags = (instr->flags & ~fBranchNotTaken) | fBranchTaken;
}
static void insertlongbranches(SInt32 mask) {
PCodeBlock *block;
PCodeLabel *label;
SInt32 i;
i = 0;
for (block = pcbasicblocks; block; block = block->nextBlock) {
block->codeOffset = i;
if (block->pcodeCount) {
i += block->pcodeCount * 4;
if (block->pcodeCount && (block->lastPCode->flags & fIsBranch))
i += 4;
}
}
for (block = pcbasicblocks; block; block = block->nextBlock) {
if (block->pcodeCount && (block->lastPCode->flags & fIsBranch)) {
switch (block->lastPCode->op) {
case PC_BT:
case PC_BF:
i = block->codeOffset + ((block->pcodeCount - 1) * 4);
if (block->lastPCode->args[2].kind == PCOp_LABEL) {
label = block->lastPCode->args[2].data.label.label;
i = label->block->codeOffset - i;
if (i != ((SInt16) (i & mask))) {
block->lastPCode->op = (block->lastPCode->op == PC_BT) ? PC_BF : PC_BT;
block->lastPCode->args[2].data.label.label = block->nextBlock->labels;
invertybit(block->lastPCode, i);
appendpcode(block, makepcode(PC_B, label));
}
}
break;
case PC_BC:
i = block->codeOffset + ((block->pcodeCount - 1) * 4);
if (block->lastPCode->args[3].kind == PCOp_LABEL) {
label = block->lastPCode->args[3].data.label.label;
i = label->block->codeOffset - i;
invertybit(block->lastPCode, i);
if (i != ((SInt16) (i & mask))) {
switch (block->lastPCode->args[0].data.imm.value & 30) {
case 0:
case 2:
case 8:
case 10:
block->lastPCode->args[0].data.imm.value ^= 11;
block->lastPCode->args[3].data.label.label = block->nextBlock->labels;
break;
case 16:
case 18:
block->lastPCode->args[0].data.imm.value ^= 3;
block->lastPCode->args[3].data.label.label = block->nextBlock->labels;
break;
case 4:
case 12:
block->lastPCode->args[0].data.imm.value ^= 9;
block->lastPCode->args[3].data.label.label = block->nextBlock->labels;
break;
case 20:
deletepcode(block->lastPCode);
break;
default:
CError_FATAL(2368);
}
appendpcode(block, makepcode(PC_B, label));
}
}
break;
case PC_BDNZ:
case PC_BDZ:
i = block->codeOffset + ((block->pcodeCount - 1) * 4);
if (block->lastPCode->args[0].kind == PCOp_LABEL) {
label = block->lastPCode->args[0].data.label.label;
i = label->block->codeOffset - i;
if (i != ((SInt16) (i & mask))) {
switch (block->lastPCode->op) {
case PC_BDZ:
block->lastPCode->op = PC_BDNZ;
break;
case PC_BDNZ:
block->lastPCode->op = PC_BDZ;
break;
default:
CError_FATAL(2389);
}
block->lastPCode->args[0].data.label.label = block->nextBlock->labels;
invertybit(block->lastPCode, i);
appendpcode(block, makepcode(PC_B, label));
}
}
break;
case PC_BDNZT:
case PC_BDNZF:
case PC_BDZT:
case PC_BDZF:
i = block->codeOffset + ((block->pcodeCount - 1) * 4);
if (block->lastPCode->args[2].kind == PCOp_LABEL) {
label = block->lastPCode->args[2].data.label.label;
i = label->block->codeOffset - i;
if (i != ((SInt16) (i & mask))) {
switch (block->lastPCode->op) {
case PC_BDNZT:
block->lastPCode->op = PC_BDZF;
break;
case PC_BDNZF:
block->lastPCode->op = PC_BDZT;
break;
case PC_BDZT:
block->lastPCode->op = PC_BDNZF;
break;
case PC_BDZF:
block->lastPCode->op = PC_BDNZT;
break;
default:
CError_FATAL(2420);
}
block->lastPCode->args[2].data.label.label = block->nextBlock->labels;
invertybit(block->lastPCode, i);
appendpcode(block, makepcode(PC_B, label));
}
}
break;
}
}
}
}
SInt32 optimizefinalbranches(SInt32 codesize) {
PCodeBlock *block;
PCode *instr;
SInt32 offset;
int changed;
int deleted;
PCodeLabel *label;
PCode *target;
do {
changed = deleted = 0;
for (block = pcbasicblocks; block; block = block->nextBlock) {
if (block->pcodeCount == 0)
continue;
instr = block->lastPCode;
if (!(instr->flags & fIsBranch))
continue;
offset = block->codeOffset + (block->pcodeCount - 1) * 4;
if (instr->op == PC_B && instr->args[0].kind == PCOp_LABEL) {
label = instr->args[0].data.label.label;
target = targetinstruction(label);
if (label->block->codeOffset == (offset + 4)) {
deletepcode(instr);
changed = deleted = 1;
} else if (target->op == PC_B) {
if (target->args[0].kind == PCOp_LABEL && target->args[0].data.label.label != instr->args[0].data.label.label) {
instr->args[0].data.label.label = target->args[0].data.label.label;
changed = 1;
}
} else if (target->op == PC_BLR) {
instr->op = PC_BLR;
changed = 1;
}
continue;
}
if ((instr->op == PC_BT || instr->op == PC_BF) && instr->args[2].kind == PCOp_LABEL) {
PCodeBlock *block2 = instr->block;
label = instr->args[2].data.label.label;
target = targetinstruction(label);
if (label->block->codeOffset == (offset + 4)) {
deletepcode(instr);
changed = deleted = 1;
} else if (target->op == PC_B) {
if (target->args[0].kind == PCOp_LABEL && target->args[0].data.label.label != instr->args[2].data.label.label) {
instr->args[2].data.label.label = target->args[0].data.label.label;
changed = 1;
}
} else if (copts.opt_bcc_lr_ctr) {
if (target->op == PC_BLR) {
if (instr->op == PC_BT)
instr->op = PC_BTLR;
else
instr->op = PC_BFLR;
instr->argCount = 2;
changed = 1;
} else if (target->op == PC_BCTR) {
if (instr->op == PC_BT)
instr->op = PC_BTCTR;
else
instr->op = PC_BFCTR;
instr->argCount = 2;
changed = 1;
} else if (
block2->nextBlock &&
block2->nextBlock->firstPCode &&
block2->nextBlock->firstPCode->op == PC_BLR &&
label->block->codeOffset == (offset + 8)
) {
if (
block2->nextBlock->predecessors &&
block2->nextBlock->predecessors->block == block2 &&
!block2->nextBlock->predecessors->nextLink
) {
if (instr->op == PC_BT)
instr->op = PC_BFLR;
else
instr->op = PC_BTLR;
change_num_operands(instr, 2);
deletepcode(block2->nextBlock->firstPCode);
changed = deleted = 1;
}
} else if (
block2->nextBlock &&
block2->nextBlock->firstPCode &&
block2->nextBlock->firstPCode->op == PC_BCTR &&
label->block->codeOffset == (offset + 8)
) {
if (
block2->nextBlock->predecessors &&
block2->nextBlock->predecessors->block == block2 &&
!block2->nextBlock->predecessors->nextLink
) {
if (instr->op == PC_BT)
instr->op = PC_BFCTR;
else
instr->op = PC_BTCTR;
change_num_operands(instr, 2);
deletepcode(block2->nextBlock->firstPCode);
changed = deleted = 1;
}
}
}
continue;
}
if (
instr->op == PC_BC &&
instr->args[3].kind == PCOp_LABEL &&
!(PCODE_FLAG_SET_T(instr) & (fSideEffects | fLink))
)
{
PCodeBlock *block2 = instr->block;
label = instr->args[3].data.label.label;
target = targetinstruction(label);
if (label->block->codeOffset == (offset + 4)) {
deletepcode(instr);
changed = deleted = 1;
} else if (target->op == PC_B) {
if (target->args[0].kind == PCOp_LABEL && target->args[0].data.label.label != instr->args[3].data.label.label) {
instr->args[3].data.label.label = target->args[0].data.label.label;
changed = 1;
}
} else if (copts.opt_bcc_lr_ctr) {
if (target->op == PC_BLR) {
instr->op = PC_BCLR;
instr->argCount = 3;
changed = 1;
} else if (target->op == PC_BCTR) {
instr->op = PC_BCCTR;
instr->argCount = 3;
changed = 1;
} else if (
block2->nextBlock &&
block2->nextBlock->firstPCode &&
block2->nextBlock->firstPCode->op == PC_BLR &&
label->block->codeOffset == (offset + 8)
) {
SInt32 val = instr->args[0].data.imm.value & 30;
if (
block2->nextBlock->predecessors &&
block2->nextBlock->predecessors->block == block2 &&
!block2->nextBlock->predecessors->nextLink
) {
if ((val & 30) == 4)
instr->args[0].data.imm.value = val | 12;
else if ((val & 30) == 12)
instr->args[0].data.imm.value = val & 23;
instr->op = PC_BCLR;
instr->argCount = 3;
deletepcode(block2->nextBlock->firstPCode);
changed = deleted = 1;
}
} else if (
block2->nextBlock &&
block2->nextBlock->firstPCode &&
block2->nextBlock->firstPCode->op == PC_BCTR &&
label->block->codeOffset == (offset + 8)
) {
SInt32 val = instr->args[0].data.imm.value & 30;
if (
block2->nextBlock->predecessors &&
block2->nextBlock->predecessors->block == block2 &&
!block2->nextBlock->predecessors->nextLink
) {
if ((val & 30) == 4)
instr->args[0].data.imm.value = val | 12;
else if ((val & 30) == 12)
instr->args[0].data.imm.value = val & 23;
instr->op = PC_BCCTR;
instr->argCount = 3;
deletepcode(block2->nextBlock->firstPCode);
changed = deleted = 1;
}
}
}
}
}
if (deleted)
codesize = pccomputeoffsets();
} while (changed);
return codesize;
}
static SInt32 insert_align_nops(Object *func, SInt32 codesize) {
PCodeBlock *block;
int changed;
PCodeBlock *prev;
do {
changed = 0;
for (block = pcbasicblocks; block; block = block->nextBlock) {
if (
(block->flags & fPCBlockFlag6000) == fPCBlockFlag2000 &&
(block->codeOffset & 7) &&
block->pcodeCount < 8 &&
(prev = block->prevBlock) &&
!(prev->flags & fPCBlockFlag2000)
)
{
if (prev->lastPCode && prev->lastPCode->op == PC_NOP && !(prev->lastPCode->flags & fSideEffects)) {
deletepcode(prev->lastPCode);
} else {
PCode *nop = makepcode(PC_NOP);
nop->flags &= ~fSideEffects;
appendpcode(prev, nop);
}
codesize = pccomputeoffsets();
changed = 1;
}
}
if (changed) {
pclistblocks(CMangler_GetLinkName(func)->name, "AFTER INSERT ALIGN NOPs");
if (codesize > 32766) {
insertlongbranches(32766);
codesize = pccomputeoffsets();
}
}
} while (changed);
return codesize;
}
SInt32 assemblefunction(Object *func, EntryPoint *entrypoints) {
void *tbdata;
GList *gl;
PCodeBlock *block;
PCode *instr;
SInt32 offset2;
SInt32 codesize;
SInt32 tbsize;
SInt32 offset;
SectionHandle section;
EntryPoint *ep;
WeirdOperand wop;
codesize = pccomputeoffsets();
if (codesize <= 0)
PPCError_Error(PPCErrorStr190, func->name->name);
if (copts.peephole || copts.optimizationlevel >= 3)
codesize = optimizefinalbranches(codesize);
if (codesize > 32766) {
insertlongbranches(32766);
codesize = pccomputeoffsets();
}
if (copts.function_align > 4)
codesize = insert_align_nops(func, codesize);
tbsize = 0;
if (copts.traceback)
tbdata = generate_traceback(codesize, CMangler_GetLinkName(func)->name, &tbsize, func);
if (func->section == SECT_DEFAULT)
func->section = SECT_TEXT;
offset = tbsize;
section = ObjGen_DeclareCode(func, codesize + tbsize);
gl = ObjGen_GetSectionGList(section);
codebase = gl->size;
AppendGListNoData(gl, codesize + tbsize);
if (copts.filesyminfo) {
ObjGen_SymFunc(func);
ObjGen_Line(functionbodyoffset, 0);
ObjGen_DeclareSymInfo();
}
if (uses_globals && pic_base_reg)
ObjGen_DeclarePICBase(func, pic_base_pcodelabel->block->codeOffset);
if (entrypoints) {
for (ep = entrypoints; ep; ep = ep->next) {
ObjGen_DeclareEntry(ep->object, ep->block->codeOffset);
}
}
for (block = pcbasicblocks; block; block = block->nextBlock) {
for (offset2 = block->codeOffset, instr = block->firstPCode; instr; instr = instr->nextPCode, offset2 += 4) {
if (copts.filesyminfo && instr->sourceoffset != -1)
ObjGen_Line(instr->sourceoffset, offset2);
*((UInt32 *) (*gl->data + codebase + offset2)) = assemblepcode(instr, offset2, &wop);
if (wop.type != -1)
ObjGen_RelocateObj(section, offset2, wop.x2, wop.type);
}
}
if (copts.filesyminfo)
ObjGenMach_SymFuncEnd(func, codesize);
if (copts.traceback)
memcpy(*gl->data + codebase + codesize, tbdata, offset);
if (copts.traceback)
return codesize + tbsize;
else
return codesize;
}
|