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
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
|
{
Copyright (c) 1998-2013 by the Free Pascal team
This unit implements the generic part of the LLVM IR writer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit agllvm;
{$i fpcdefs.inc}
interface
uses
cclasses,
globtype,globals,systems,
aasmbase,aasmtai,aasmdata,
assemble,
aasmllvm, aasmllvmmetadata;
type
TLLVMInstrWriter = class;
TLLVMModuleInlineAssemblyDecorator = class(IExternalAssemblerOutputFileDecorator)
function LineFilter(const s: AnsiString): AnsiString;
function LinePrefix: AnsiString;
function LinePostfix: AnsiString;
function LineEnding(const deflineending: ShortString): ShortString;
end;
TLLVMFunctionInlineAssemblyDecorator = class(IExternalAssemblerOutputFileDecorator)
function LineFilter(const s: AnsiString): AnsiString;
function LinePrefix: AnsiString;
function LinePostfix: AnsiString;
function LineEnding(const deflineending: ShortString): ShortString;
end;
TLLVMAssember=class(texternalassembler)
protected
ffuncinlasmdecorator: TLLVMFunctionInlineAssemblyDecorator;
fdecllevel: longint;
procedure WriteExtraHeader;virtual;
procedure WriteExtraFooter;virtual;
procedure WriteInstruction(hp: tai);
procedure WriteLlvmInstruction(hp: tai);
procedure WriteDirectiveName(dir: TAsmDirective); virtual;
procedure WriteRealConst(hp: tai_realconst; do_line: boolean);
procedure WriteOrdConst(hp: tai_const);
procedure WriteTai(const replaceforbidden: boolean; const do_line, inmetadata: boolean; var InlineLevel: cardinal; var asmblock: boolean; var hp: tai);
public
constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
procedure WriteTree(p:TAsmList);override;
procedure WriteAsmList;override;
procedure WriteFunctionInlineAsmList(list: tasmlist);
destructor destroy; override;
protected
InstrWriter: TLLVMInstrWriter;
end;
TLLVMClangAssember=class(TLLVMAssember)
public
function MakeCmdLine: TCmdStr; override;
function DoAssemble: boolean; override;
function RerunAssembler: boolean; override;
protected
function DoPipe: boolean; override;
private
fnextpass: byte;
end;
{# This is the base class for writing instructions.
The WriteInstruction() method must be overridden
to write a single instruction to the assembler
file.
}
TLLVMInstrWriter = class
constructor create(_owner: TLLVMAssember);
procedure WriteInstruction(hp : tai);
protected
owner: TLLVMAssember;
fstr: TSymStr;
function getopcodestr(hp: taillvm): TSymStr;
function getopstr(const o:toper; refwithalign: boolean) : TSymStr;
procedure writeparas(const paras: tfplist);
procedure WriteAsmRegisterAllocationClobbers(list: tasmlist);
end;
implementation
uses
SysUtils,
cutils,cfileutl,
fmodule,verbose,
objcasm,
aasmcnst,symconst,symdef,symtable,
llvmbase,itllvm,llvmdef,
cgbase,cgutils,cpubase,cpuinfo,triplet,llvminfo;
const
line_length = 70;
type
{$ifdef cpuextended}
t80bitarray = array[0..9] of byte;
{$endif cpuextended}
t64bitarray = array[0..7] of byte;
t32bitarray = array[0..3] of byte;
{****************************************************************************}
{ Support routines }
{****************************************************************************}
function single2str(d : single) : string;
var
hs : string;
begin
str(d,hs);
{ replace space with + }
if hs[1]=' ' then
hs[1]:='+';
single2str:=hs
end;
function double2str(d : double) : string;
var
hs : string;
begin
str(d,hs);
{ replace space with + }
if hs[1]=' ' then
hs[1]:='+';
double2str:=hs
end;
function extended2str(e : extended) : string;
var
hs : string;
begin
str(e,hs);
{ replace space with + }
if hs[1]=' ' then
hs[1]:='+';
extended2str:=hs
end;
{****************************************************************************}
{ Decorator for module-level inline assembly }
{****************************************************************************}
function TLLVMModuleInlineAssemblyDecorator.LineFilter(const s: AnsiString): AnsiString;
var
i: longint;
begin
result:='';
for i:=1 to length(s) do
begin
case s[i] of
#0..#31,
#127..#255,
'"','\':
result:=result+
'\'+
chr((ord(s[i]) shr 4)+ord('0'))+
chr((ord(s[i]) and $f)+ord('0'));
else
result:=result+s[i];
end;
end;
end;
function TLLVMModuleInlineAssemblyDecorator.LinePrefix: AnsiString;
begin
result:='module asm "';
end;
function TLLVMModuleInlineAssemblyDecorator.LinePostfix: AnsiString;
begin
result:='"';
end;
function TLLVMModuleInlineAssemblyDecorator.LineEnding(const deflineending: ShortString): ShortString;
begin
result:=deflineending
end;
{****************************************************************************}
{ Decorator for function-level inline assembly }
{****************************************************************************}
function TLLVMFunctionInlineAssemblyDecorator.LineFilter(const s: AnsiString): AnsiString;
var
i: longint;
begin
result:='';
for i:=1 to length(s) do
begin
case s[i] of
{ escape dollars }
'$':
result:=result+'$$';
{ ` is used as placeholder for a single dollar (reference to
argument to the inline assembly) }
'`':
result:=result+'$';
#0..#31,
#127..#255,
'"','\':
result:=result+
'\'+
chr((ord(s[i]) shr 4)+ord('0'))+
chr((ord(s[i]) and $f)+ord('0'));
else
result:=result+s[i];
end;
end;
end;
function TLLVMFunctionInlineAssemblyDecorator.LinePrefix: AnsiString;
begin
result:='';
end;
function TLLVMFunctionInlineAssemblyDecorator.LinePostfix: AnsiString;
begin
result:='';
end;
function TLLVMFunctionInlineAssemblyDecorator.LineEnding(const deflineending: ShortString): ShortString;
begin
result:='\0A';
end;
{****************************************************************************}
{ LLVM Instruction writer }
{****************************************************************************}
function getregisterstring(reg: tregister): ansistring;
begin
if getregtype(reg)=R_METADATAREGISTER then
result:='!"'+tllvmmetadata.getregstring(reg)+'"'
else
begin
if getregtype(reg)=R_TEMPREGISTER then
result:='%tmp.'
else
result:='%reg.'+tostr(byte(getregtype(reg)))+'_';
result:=result+tostr(getsupreg(reg));
end;
end;
function getreferencealignstring(var ref: treference) : ansistring;
begin
result:=', align '+tostr(ref.alignment);
end;
function getreferencestring(var ref : treference; withalign: boolean) : ansistring;
begin
result:='';
if assigned(ref.relsymbol) or
(assigned(ref.symbol) and
(ref.base<>NR_NO)) or
(ref.index<>NR_NO) or
(ref.offset<>0) then
begin
result:=' **(error ref: ';
if assigned(ref.symbol) then
result:=result+'sym='+ref.symbol.name+', ';
if assigned(ref.relsymbol) then
result:=result+'sym='+ref.relsymbol.name+', ';
if ref.base=NR_NO then
result:=result+'base=NR_NO, ';
if ref.index<>NR_NO then
result:=result+'index<>NR_NO, ';
if ref.offset<>0 then
result:=result+'offset='+tostr(ref.offset);
result:=result+')**';
internalerror(2013060203);
end;
if ref.base<>NR_NO then
result:=result+getregisterstring(ref.base)
else if assigned(ref.symbol) then
result:=result+LlvmAsmSymName(ref.symbol)
else
result:=result+'null';
if withalign then
result:=result+getreferencealignstring(ref);
end;
procedure TLLVMInstrWriter.writeparas(const paras: tfplist);
var
i: longint;
tmpinline: cardinal;
para: pllvmcallpara;
tmpasmblock: boolean;
hp: tai;
begin
tmpinline:=1;
tmpasmblock:=false;
owner.writer.AsmWrite(fstr);
fstr:='';
owner.writer.AsmWrite('(');
for i:=0 to paras.count-1 do
begin
if i<>0 then
owner.writer.AsmWrite(', ');
para:=pllvmcallpara(paras[i]);
owner.writer.AsmWrite(llvmencodetypename(para^.def));
if para^.valueext<>lve_none then
owner.writer.AsmWrite(llvmvalueextension2str[para^.valueext]);
if para^.byval then
owner.writer.AsmWrite(' byval');
if para^.sret then
owner.writer.AsmWrite(' sret');
{ For byval, this means "alignment on the stack" and of the passed source data.
For other pointer parameters, this means "alignment of the passed source data" }
if (para^.alignment<>std_param_align) or
(para^.alignment<0) then
begin
owner.writer.AsmWrite(' align ');
owner.writer.AsmWrite(tostr(abs(para^.alignment)));
end;
case para^.typ of
top_reg:
begin
owner.writer.AsmWrite(' ');
owner.writer.AsmWrite(getregisterstring(para^.register));
end;
top_ref:
begin
owner.writer.AsmWrite(' ');
owner.writer.AsmWrite(llvmasmsymname(para^.sym));
end;
top_const:
begin
owner.writer.AsmWrite(' ');
owner.writer.AsmWrite(tostr(para^.value));
end;
top_tai:
begin
tmpinline:=1;
tmpasmblock:=false;
hp:=para^.ai;
owner.writer.AsmWrite(fstr);
fstr:='';
owner.WriteTai(false,false,para^.def=llvm_metadatatype,tmpinline,tmpasmblock,hp);
end;
{ empty records }
top_undef:
owner.writer.AsmWrite(' undef');
else
internalerror(2014010801);
end;
end;
owner.writer.AsmWrite(')');
end;
function llvmdoubletostr(const d: double): TSymStr;
type
tdoubleval = record
case byte of
1: (d: double);
2: (i: int64);
end;
begin
{ "When using the hexadecimal form, constants of types half,
float, and double are represented using the 16-digit form shown
above (which matches the IEEE754 representation for double)"
And always in big endian form (sign bit leftmost)
}
result:='0x'+hexstr(tdoubleval(d).i,16);
end;
{$if defined(cpuextended) and (defined(FPC_HAS_TYPE_EXTENDED) or defined(FPC_SOFT_FPUX80))}
function llvmextendedtostr(const e: extended): TSymStr;
var
extendedval: record
case byte of
1: (e: extended);
2: (r: packed record
{$ifdef FPC_LITTLE_ENDIAN}
l: int64;
h: word;
{$else FPC_LITTLE_ENDIAN}
h: int64;
l: word;
{$endif FPC_LITTLE_ENDIAN}
end;
);
end;
begin
extendedval.e:=e;
{ hex format is always big endian in llvm }
result:='0xK'+hexstr(extendedval.r.h,sizeof(extendedval.r.h)*2)+
hexstr(extendedval.r.l,sizeof(extendedval.r.l)*2);
end;
{$endif cpuextended}
function TLLVMInstrWriter.getopstr(const o:toper; refwithalign: boolean) : TSymStr;
var
hp: tai;
tmpinline: cardinal;
tmpasmblock: boolean;
begin
case o.typ of
top_reg:
getopstr:=getregisterstring(o.reg);
top_const:
getopstr:=tostr(int64(o.val));
top_ref:
if o.ref^.refaddr=addr_full then
begin
getopstr:='';
if assigned(o.ref^.symbol) then
getopstr:=LlvmAsmSymName(o.ref^.symbol)
else
getopstr:='null';
if o.ref^.offset<>0 then
internalerror(2013060202);
end
else
getopstr:=getreferencestring(o.ref^,refwithalign);
top_def:
begin
getopstr:=llvmencodetypename(o.def);
end;
top_cond:
begin
getopstr:=llvm_cond2str[o.cond];
end;
top_fpcond:
begin
getopstr:=llvm_fpcond2str[o.fpcond];
end;
top_single,
top_double:
begin
{ "When using the hexadecimal form, constants of types half,
float, and double are represented using the 16-digit form shown
above (which matches the IEEE754 representation for double)"
And always in big endian form (sign bit leftmost)
}
if o.typ=top_double then
result:=llvmdoubletostr(o.dval)
else
result:=llvmdoubletostr(o.sval)
end;
top_para:
begin
writeparas(o.paras);
result:='';
end;
top_tai:
begin
if assigned(o.ai) then
begin
tmpinline:=1;
tmpasmblock:=false;
hp:=o.ai;
owner.writer.AsmWrite(fstr);
fstr:='';
owner.WriteTai(false,false,false,tmpinline,tmpasmblock,hp);
end;
result:='';
end;
{$if defined(cpuextended) and (defined(FPC_HAS_TYPE_EXTENDED) or defined(FPC_SOFT_FPUX80))}
top_extended80:
begin
result:=llvmextendedtostr(o.eval);
end;
{$endif cpuextended}
top_undef:
result:='undef';
top_callingconvention:
result:=llvm_callingconvention_name(o.callingconvention);
else
internalerror(2013060227);
end;
end;
procedure TLLVMInstrWriter.WriteAsmRegisterAllocationClobbers(list: tasmlist);
var
hp: tai;
begin
hp:=tai(list.first);
while assigned(hp) do
begin
if (hp.typ=ait_regalloc) and
(tai_regalloc(hp).ratype=ra_alloc) then
begin
owner.writer.AsmWrite(',~{');
owner.writer.AsmWrite(std_regname(tai_regalloc(hp).reg));
owner.writer.AsmWrite('}');
end;
hp:=tai(hp.next);
end;
end;
procedure TLLVMInstrWriter.WriteInstruction(hp: tai);
var
op: tllvmop;
tmpstr,
sep: TSymStr;
i, opstart: longint;
nested: boolean;
opdone,
done: boolean;
begin
op:=taillvm(hp).llvmopcode;
{ we write everything immediately rather than adding it into a string,
because operands may contain other tai that will also write things out
(and their output must come after everything that was processed in this
instruction, such as its opcode or previous operands) }
if owner.fdecllevel=0 then
owner.writer.AsmWrite(#9);
sep:=' ';
opdone:=false;
done:=false;
opstart:=0;
nested:=false;
case op of
la_type:
begin
owner.writer.AsmWrite(llvmtypeidentifier(taillvm(hp).oper[0]^.def));
owner.writer.AsmWrite(' = type ');
owner.writer.AsmWrite(llvmencodetypedecl(taillvm(hp).oper[0]^.def));
done:=true;
end;
la_asmblock:
begin
owner.writer.AsmWrite('call void asm sideeffect "');
owner.WriteFunctionInlineAsmList(taillvm(hp).oper[0]^.asmlist);
owner.writer.AsmWrite('","');
{ we pass all accessed local variables as in/out address parameters,
since we don't analyze the assembly code to determine what exactly
happens to them; this is also compatible with the regular code
generators, which always place local place local variables
accessed from assembly code in memory }
for i:=0 to taillvm(hp).oper[1]^.paras.Count-1 do
begin
owner.writer.AsmWrite('=*m,');
end;
owner.writer.AsmWrite('~{memory},~{fpsr},~{flags}');
WriteAsmRegisterAllocationClobbers(taillvm(hp).oper[0]^.asmlist);
owner.writer.AsmWrite('"');
writeparas(taillvm(hp).oper[1]^.paras);
done:=true;
end;
la_load,
la_getelementptr:
begin
if (taillvm(hp).oper[0]^.typ<>top_reg) or
(taillvm(hp).oper[0]^.reg<>NR_NO) then
owner.writer.AsmWrite(getopstr(taillvm(hp).oper[0]^,false)+' = ')
else
nested:=true;
opstart:=1;
owner.writer.AsmWrite(getopcodestr(taillvm(hp)));
opdone:=true;
if nested then
owner.writer.AsmWrite(' (')
else
owner.writer.AsmWrite(' ');
{ can't just dereference the type, because it may be an
implicit pointer type such as a class -> resort to string
manipulation... Not very clean :( }
tmpstr:=llvmencodetypename(taillvm(hp).spilling_get_reg_type(0));
if op=la_getelementptr then
begin
if tmpstr[length(tmpstr)]<>'*' then
begin
writeln(tmpstr);
internalerror(2016071101);
end
else
setlength(tmpstr,length(tmpstr)-1);
end;
owner.writer.AsmWrite(tmpstr);
owner.writer.AsmWrite(',');
end;
la_ret, la_br, la_switch, la_indirectbr,
la_resume,
la_unreachable,
la_store,
la_fence,
la_cmpxchg,
la_atomicrmw,
la_catch,
la_filter,
la_cleanup:
begin
{ instructions that never have a result }
end;
la_call,
la_invoke:
begin
if taillvm(hp).oper[1]^.reg<>NR_NO then
owner.writer.AsmWrite(getregisterstring(taillvm(hp).oper[1]^.reg)+' = ');
opstart:=2;
owner.writer.AsmWrite(getopcodestr(taillvm(hp)));
tmpstr:=llvm_callingconvention_name(taillvm(hp).oper[2]^.callingconvention);
if tmpstr<>'' then
begin
owner.writer.AsmWrite(' ');
owner.writer.AsmWrite(tmpstr);
end;
opdone:=true;
tmpstr:=llvmencodetypename(taillvm(hp).oper[3]^.def);
if tmpstr[length(tmpstr)]<>'*' then
begin
writeln(tmpstr);
internalerror(2016071102);
end
else
setlength(tmpstr,length(tmpstr)-1);
owner.writer.AsmWrite(tmpstr);
opstart:=4;
end;
la_blockaddress:
begin
{ nested -> no type }
if owner.fdecllevel = 0 then
begin
owner.writer.AsmWrite(getopstr(taillvm(hp).oper[0]^,false));
owner.writer.AsmWrite(' ');
end;
owner.writer.AsmWrite('blockaddress(');
owner.writer.AsmWrite(getopstr(taillvm(hp).oper[1]^,false));
{ getopstr would add a "label" qualifier, which blockaddress does
not want }
owner.writer.AsmWrite(',%');
with taillvm(hp).oper[2]^ do
begin
if (typ<>top_ref) or
(ref^.refaddr<>addr_full) then
internalerror(2016112001);
owner.writer.AsmWrite(ref^.symbol.name);
end;
nested:=true;
done:=true;
end;
la_alloca:
begin
owner.writer.AsmWrite(getreferencestring(taillvm(hp).oper[0]^.ref^,false)+' = ');
sep:=' ';
opstart:=1;
end;
la_trunc, la_zext, la_sext, la_fptrunc, la_fpext,
la_fptoui, la_fptosi, la_uitofp, la_sitofp,
la_ptrtoint, la_inttoptr,
la_bitcast:
begin
{ destination can be empty in case of nested constructs, or
data initialisers }
if (taillvm(hp).oper[0]^.typ<>top_reg) or
(taillvm(hp).oper[0]^.reg<>NR_NO) then
owner.writer.AsmWrite(getopstr(taillvm(hp).oper[0]^,false)+' = ')
else
nested:=true;
owner.writer.AsmWrite(getopcodestr(taillvm(hp)));
if not nested then
owner.writer.AsmWrite(' ')
else
owner.writer.AsmWrite(' (');
owner.writer.AsmWrite(getopstr(taillvm(hp).oper[1]^,false));
{ if there's a tai operand, its def is used instead of an
explicit def operand }
if taillvm(hp).ops=4 then
begin
owner.writer.AsmWrite(' ');
owner.writer.AsmWrite(getopstr(taillvm(hp).oper[2]^,false));
opstart:=3;
end
else
opstart:=2;
owner.writer.AsmWrite(' to ');
owner.writer.AsmWrite(getopstr(taillvm(hp).oper[opstart]^,false));
done:=true;
end
else
begin
if (taillvm(hp).oper[0]^.typ<>top_reg) or
(taillvm(hp).oper[0]^.reg<>NR_NO) then
begin
owner.writer.AsmWrite(getopstr(taillvm(hp).oper[0]^,true)+' = ');
end
else
nested:=true;
sep:=' ';
opstart:=1
end;
end;
{ process operands }
if not done then
begin
if not opdone then
begin
owner.writer.AsmWrite(getopcodestr(taillvm(hp)));
if nested then
owner.writer.AsmWrite(' (');
end;
if taillvm(hp).ops<>0 then
begin
for i:=opstart to taillvm(hp).ops-1 do
begin
owner.writer.AsmWrite(sep);
{ special invoke interjections: "to label X unwind label Y" }
if (op=la_invoke) then
case i of
6: owner.writer.AsmWrite('to ');
7: owner.writer.AsmWrite('unwind ');
end;
owner.writer.AsmWrite(getopstr(taillvm(hp).oper[i]^,op in [la_load,la_store]));
if (taillvm(hp).oper[i]^.typ in [top_def,top_cond,top_fpcond]) or
(op in [la_call,la_invoke,la_landingpad,la_catch,la_filter,la_cleanup]) then
sep :=' '
else
sep:=', ';
end;
end;
end;
if op=la_alloca then
owner.writer.AsmWrite(getreferencealignstring(taillvm(hp).oper[0]^.ref^));
if nested then
owner.writer.AsmWrite(')')
else if owner.fdecllevel=0 then
owner.writer.AsmLn;
end;
function TLLVMInstrWriter.getopcodestr(hp: taillvm): TSymStr;
begin
result:=llvm_op2str[hp.llvmopcode];
case hp.llvmopcode of
la_load:
begin
if vol_read in hp.oper[2]^.ref^.volatility then
result:=result+' volatile';
end;
la_store:
begin
if vol_write in hp.oper[3]^.ref^.volatility then
result:=result+' volatile';
end;
else
;
end;
end;
{****************************************************************************}
{ LLVM Assembler writer }
{****************************************************************************}
destructor TLLVMAssember.Destroy;
begin
InstrWriter.free;
ffuncinlasmdecorator.free;
inherited destroy;
end;
procedure TLLVMAssember.WriteTree(p:TAsmList);
var
hp : tai;
InlineLevel : cardinal;
asmblock: boolean;
do_line : boolean;
replaceforbidden: boolean;
begin
if not assigned(p) then
exit;
replaceforbidden:=asminfo^.dollarsign<>'$';
InlineLevel:=0;
asmblock:=false;
{ lineinfo is only needed for al_procedures (PFV) }
do_line:=(cs_asm_source in current_settings.globalswitches) or
((cs_lineinfo in current_settings.moduleswitches)
and (p=current_asmdata.asmlists[al_procedures]));
hp:=tai(p.first);
while assigned(hp) do
begin
prefetch(pointer(hp.next)^);
if not(hp.typ in SkipLineInfo) then
begin
current_filepos:=tailineinfo(hp).fileinfo;
{ no line info for inlined code }
if do_line and (inlinelevel=0) then
WriteSourceLine(hp as tailineinfo);
end;
WriteTai(replaceforbidden, do_line, false, InlineLevel, asmblock, hp);
hp:=tai(hp.next);
end;
end;
procedure TLLVMAssember.WriteExtraHeader;
begin
writer.AsmWrite('target datalayout = "');
writer.AsmWrite(target_info.llvmdatalayout);
writer.AsmWriteln('"');
writer.AsmWrite('target triple = "');
writer.AsmWrite(targettriplet(triplet_llvm));
writer.AsmWriteln('"');
end;
procedure TLLVMAssember.WriteExtraFooter;
begin
end;
procedure TLLVMAssember.WriteInstruction(hp: tai);
begin
end;
procedure TLLVMAssember.WriteLlvmInstruction(hp: tai);
begin
InstrWriter.WriteInstruction(hp);
end;
procedure TLLVMAssember.WriteRealConst(hp: tai_realconst; do_line: boolean);
begin
if fdecllevel=0 then
begin
case tai_realconst(hp).realtyp of
aitrealconst_s32bit:
writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s32val));
aitrealconst_s64bit:
writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s64val));
{$if defined(cpuextended) and (defined(FPC_HAS_TYPE_EXTENDED) or defined(FPC_SOFT_FPUX80))}
{ can't write full 80 bit floating point constants yet on non-x86 }
aitrealconst_s80bit:
writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s80val));
{$endif cpuextended}
aitrealconst_s64comp:
writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s64compval));
else
internalerror(2014050603);
end;
internalerror(2016120202);
end;
case hp.realtyp of
aitrealconst_s32bit:
writer.AsmWrite(llvmdoubletostr(hp.value.s32val));
aitrealconst_s64bit:
writer.AsmWriteln(llvmdoubletostr(hp.value.s64val));
{$if defined(cpuextended) and (defined(FPC_HAS_TYPE_EXTENDED) or defined(FPC_SOFT_FPUX80))}
aitrealconst_s80bit:
writer.AsmWrite(llvmextendedtostr(hp.value.s80val));
{$endif defined(cpuextended)}
aitrealconst_s64comp:
{ handled as int64 most of the time in llvm }
writer.AsmWrite(tostr(round(hp.value.s64compval)));
else
internalerror(2014062401);
end;
end;
procedure TLLVMAssember.WriteOrdConst(hp: tai_const);
var
consttyp: taiconst_type;
begin
if fdecllevel=0 then
internalerror(2016120203);
consttyp:=hp.consttype;
case consttyp of
aitconst_got,
aitconst_gotoff_symbol,
aitconst_uleb128bit,
aitconst_sleb128bit,
aitconst_rva_symbol,
aitconst_secrel32_symbol,
aitconst_darwin_dwarf_delta32,
aitconst_darwin_dwarf_delta64,
aitconst_half16bit,
aitconst_gs:
internalerror(2014052901);
aitconst_128bit,
aitconst_64bit,
aitconst_32bit,
aitconst_16bit,
aitconst_8bit,
aitconst_16bit_unaligned,
aitconst_32bit_unaligned,
aitconst_64bit_unaligned:
begin
if fdecllevel=0 then
writer.AsmWrite(asminfo^.comment);
{ can't have compile-time differences between symbols; these are
normally for PIC, but llvm takes care of that for us }
if assigned(hp.endsym) then
internalerror(2014052902);
if assigned(hp.sym) then
begin
writer.AsmWrite(LlvmAsmSymName(hp.sym));
{ can't have offsets }
if hp.value<>0 then
if fdecllevel<>0 then
internalerror(2014052903)
else
writer.AsmWrite(' -- symbol offset: ' + tostr(hp.value));
end
else if hp.value=0 then
writer.AsmWrite('zeroinitializer')
else
writer.AsmWrite(tostr(hp.value));
{
// activate in case of debugging IE 2016120203
if fdecllevel=0 then
writer.AsmLn;
}
end;
else
internalerror(2007042504);
end;
end;
procedure TLLVMAssember.WriteTai(const replaceforbidden: boolean; const do_line, inmetadata: boolean; var InlineLevel: cardinal; var asmblock: boolean; var hp: tai);
procedure WriteLinkageVibilityFlags(bind: TAsmSymBind; is_definition: boolean);
begin
{ re-declaration of a symbol defined in the current module (in an
assembler block) }
if not is_definition then
begin
writer.AsmWrite(' external');
exit;
end;
case bind of
AB_EXTERNAL,
AB_EXTERNAL_INDIRECT:
writer.AsmWrite(' external');
AB_COMMON:
writer.AsmWrite(' common');
AB_LOCAL:
writer.AsmWrite(' internal');
AB_GLOBAL,
AB_INDIRECT:
;
AB_WEAK_EXTERNAL:
writer.AsmWrite(' extern_weak');
AB_PRIVATE_EXTERN:
writer.AsmWrite(' hidden')
else
internalerror(2014020104);
end;
end;
procedure WriteFunctionFlags(pd: tprocdef);
begin
{ function attributes }
if (pos('FPC_SETJMP',upper(pd.mangledname))<>0) or
(pd.mangledname=(target_info.cprefix+'setjmp')) then
writer.AsmWrite(' returns_twice');
if po_inline in pd.procoptions then
writer.AsmWrite(' inlinehint')
else if (po_noinline in pd.procoptions) or
(pio_inline_forbidden in pd.implprocoptions) then
writer.AsmWrite(' noinline');
{ ensure that functions that happen to have the same name as a
standard C library function, but which are implemented in Pascal,
are not considered to have the same semantics as the C function with
the same name }
if not(po_external in pd.procoptions) then
writer.AsmWrite(' nobuiltin');
if po_noreturn in pd.procoptions then
writer.AsmWrite(' noreturn');
if pio_thunk in pd.implprocoptions then
writer.AsmWrite(' "thunk"');
if llvmflag_null_pointer_valid in llvmversion_properties[current_settings.llvmversion] then
writer.AsmWrite(' "null-pointer-is-valid"="true"')
else if llvmflag_null_pointer_valid_new in llvmversion_properties[current_settings.llvmversion] then
writer.AsmWrite(' null_pointer_is_valid');
if not(pio_fastmath in pd.implprocoptions) then
writer.AsmWrite(' strictfp');
end;
procedure WriteTypedConstData(hp: tai_abstracttypedconst; metadata: boolean);
var
p: tai_abstracttypedconst;
pval: tai;
defstr: TSymStr;
first, gotstring: boolean;
begin
if hp.def<>llvm_metadatatype then
begin
defstr:=llvmencodetypename(hp.def)
end
else
begin
defstr:=''
end;
{ write the struct, array or simple type }
case hp.adetyp of
tck_record:
begin
if not(metadata) then
begin
writer.AsmWrite(defstr);
if not(df_llvm_no_struct_packing in hp.def.defoptions) then
writer.AsmWrite(' <{')
else
writer.AsmWrite(' {')
end
else
begin
writer.AsmWrite(' !{');
end;
first:=true;
for p in tai_aggregatetypedconst(hp) do
begin
if not first then
writer.AsmWrite(', ')
else
first:=false;
WriteTypedConstData(p,metadata);
end;
if not(metadata) then
begin
if not(df_llvm_no_struct_packing in hp.def.defoptions) then
writer.AsmWrite(' }>')
else
writer.AsmWrite(' }')
end
else
begin
writer.AsmWrite(' }');
end;
end;
tck_array:
begin
if not(metadata) then
begin
writer.AsmWrite(defstr);
end;
first:=true;
gotstring:=false;
for p in tai_aggregatetypedconst(hp) do
begin
if not first then
writer.AsmWrite(', ')
else
begin
writer.AsmWrite(' ');
if (tai_abstracttypedconst(p).adetyp=tck_simple) and
(tai_simpletypedconst(p).val.typ=ait_string) then
begin
gotstring:=true;
end
else
begin
if not metadata then
begin
writer.AsmWrite('[');
end
else
begin
writer.AsmWrite('!{');
end;
end;
first:=false;
end;
{ cannot concat strings and other things }
if gotstring and
not metadata and
((tai_abstracttypedconst(p).adetyp<>tck_simple) or
(tai_simpletypedconst(p).val.typ<>ait_string)) then
internalerror(2014062701);
WriteTypedConstData(p,metadata);
end;
if not gotstring then
begin
if not metadata then
begin
writer.AsmWrite(']');
end
else
begin
writer.AsmWrite('}');
end;
end;
end;
tck_simple:
begin
pval:=tai_simpletypedconst(hp).val;
if (pval.typ<>ait_string) and
(defstr<>'') then
begin
writer.AsmWrite(defstr);
writer.AsmWrite(' ');
end;
WriteTai(replaceforbidden,do_line,metadata,InlineLevel,asmblock,pval);
end;
end;
end;
procedure WriteLlvmMetadataNode(hp: tai_llvmbasemetadatanode);
begin
{ must only appear at the top level }
if fdecllevel<>0 then
internalerror(2019050111);
writer.AsmWrite('!');
writer.AsmWrite(tai_llvmbasemetadatanode(hp).name);
writer.AsmWrite(' =');
inc(fdecllevel);
WriteTypedConstData(hp,true);
writer.AsmLn;
dec(fdecllevel);
end;
var
hp2: tai;
s: string;
sstr: TSymStr;
i: longint;
ch: ansichar;
begin
case hp.typ of
ait_align,
ait_section :
begin
{ ignore, specified as part of declarations -- don't write
comment, because could appear in the middle of an aggregate
constant definition }
end;
ait_datablock :
begin
writer.AsmWrite(asminfo^.comment);
writer.AsmWriteln('datablock');
end;
ait_const:
begin
WriteOrdConst(tai_const(hp));
end;
ait_realconst :
begin
WriteRealConst(tai_realconst(hp), do_line);
end;
ait_string :
begin
if fdecllevel=0 then
internalerror(2016120201);
if not inmetadata then
writer.AsmWrite('c"')
else
writer.AsmWrite('!"');
for i:=1 to tai_string(hp).len do
begin
ch:=tai_string(hp).str[i-1];
case ch of
#0, {This can't be done by range, because a bug in FPC}
#1..#31,
#128..#255,
'"',
'\' : s:='\'+hexStr(ord(ch),2);
else
s:=ch;
end;
writer.AsmWrite(s);
end;
writer.AsmWrite('"');
end;
ait_label :
begin
if not asmblock and
(tai_label(hp).labsym.is_used) then
begin
if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
begin
{ should be emitted as part of the variable/function def }
internalerror(2013010703);
end;
if tai_label(hp).labsym.bind in [AB_GLOBAL, AB_PRIVATE_EXTERN] then
begin
{ should be emitted as part of the variable/function def }
//internalerror(2013010704);
writer.AsmWriteln(asminfo^.comment+'global/privateextern label: '+tai_label(hp).labsym.name);
end;
if replaceforbidden then
writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name))
else
writer.AsmWrite(tai_label(hp).labsym.name);
writer.AsmWriteLn(':');
end;
end;
ait_symbol :
begin
if fdecllevel=0 then
writer.AsmWrite(asminfo^.comment);
writer.AsmWriteln(LlvmAsmSymName(tai_symbol(hp).sym));
{ todo }
if tai_symbol(hp).has_value then
internalerror(2014062402);
end;
ait_llvmdecl:
begin
if taillvmdecl(hp).def.typ=procdef then
begin
if not(ldf_definition in taillvmdecl(hp).flags) then
begin
writer.AsmWrite('declare');
writer.AsmWrite(llvmencodeproctype(tprocdef(taillvmdecl(hp).def), taillvmdecl(hp).namesym.name, lpd_decl));
WriteFunctionFlags(tprocdef(taillvmdecl(hp).def));
writer.AsmLn;
end
else
begin
writer.AsmWrite('define');
if ldf_weak in taillvmdecl(hp).flags then
writer.AsmWrite(' weak');
WriteLinkageVibilityFlags(taillvmdecl(hp).namesym.bind, true);
writer.AsmWrite(llvmencodeproctype(tprocdef(taillvmdecl(hp).def), '', lpd_def));
WriteFunctionFlags(tprocdef(taillvmdecl(hp).def));
if assigned(tprocdef(taillvmdecl(hp).def).personality) then
begin
writer.AsmWrite(' personality i8* bitcast (');
writer.AsmWrite(llvmencodeproctype(tprocdef(taillvmdecl(hp).def).personality, '', lpd_procvar));
writer.AsmWrite('* ');
writer.AsmWrite(llvmmangledname(tprocdef(taillvmdecl(hp).def).personality.mangledname));
writer.AsmWrite(' to i8*)');
end;
writer.AsmWriteln(' {');
end;
end
else
begin
writer.AsmWrite(LlvmAsmSymName(taillvmdecl(hp).namesym));
writer.AsmWrite(' =');
if ldf_weak in taillvmdecl(hp).flags then
writer.AsmWrite(' weak');
if ldf_appending in taillvmdecl(hp).flags then
writer.AsmWrite(' appending');
WriteLinkageVibilityFlags(taillvmdecl(hp).namesym.bind, ldf_definition in taillvmdecl(hp).flags);
writer.AsmWrite(' ');
if (ldf_tls in taillvmdecl(hp).flags) then
writer.AsmWrite('thread_local ');
if ldf_unnamed_addr in taillvmdecl(hp).flags then
writer.AsmWrite('unnamed_addr ');
if taillvmdecl(hp).sec in [sec_rodata,sec_rodata_norel] then
writer.AsmWrite('constant ')
else
writer.AsmWrite('global ');
if not assigned(taillvmdecl(hp).initdata) then
begin
writer.AsmWrite(llvmencodetypename(taillvmdecl(hp).def));
if ldf_definition in taillvmdecl(hp).flags then
writer.AsmWrite(' zeroinitializer');
end
else
begin
inc(fdecllevel);
{ can't have an external symbol with initialisation data }
if taillvmdecl(hp).namesym.bind in [AB_EXTERNAL, AB_WEAK_EXTERNAL] then
internalerror(2014052905);
{ bitcast initialisation data to the type of the constant }
{ write initialisation data }
hp2:=tai(taillvmdecl(hp).initdata.first);
while assigned(hp2) do
begin
WriteTai(replaceforbidden,do_line,inmetadata,InlineLevel,asmblock,hp2);
hp2:=tai(hp2.next);
end;
dec(fdecllevel);
end;
{ custom section name? }
case taillvmdecl(hp).sec of
sec_user:
begin
writer.AsmWrite(', section "');
writer.AsmWrite(taillvmdecl(hp).secname);
writer.AsmWrite('"');
end;
low(TObjCAsmSectionType)..high(TObjCAsmSectionType):
begin
writer.AsmWrite(', section "');
writer.AsmWrite(objc_section_name(taillvmdecl(hp).sec));
writer.AsmWrite('"');
end;
else
;
end;
{ sections whose name starts with 'llvm.' are for LLVM
internal use and don't have an alignment }
if pos('llvm.',taillvmdecl(hp).secname)<>1 then
begin
{ alignment }
writer.AsmWrite(', align ');
writer.AsmWriteln(tostr(taillvmdecl(hp).alignment));
end
else
writer.AsmLn;
end;
end;
ait_llvmalias:
begin
writer.AsmWrite(LlvmAsmSymName(taillvmalias(hp).newsym));
writer.AsmWrite(' = alias ');
WriteLinkageVibilityFlags(taillvmalias(hp).bind, true);
if taillvmalias(hp).def.typ=procdef then
sstr:=llvmencodeproctype(tabstractprocdef(taillvmalias(hp).def), '', lpd_alias)
else
sstr:=llvmencodetypename(taillvmalias(hp).def);
writer.AsmWrite(sstr);
writer.AsmWrite(', ');
writer.AsmWrite(sstr);
writer.AsmWrite('* ');
writer.AsmWriteln(LlvmAsmSymName(taillvmalias(hp).oldsym));
end;
ait_llvmmetadatanode:
begin
WriteLlvmMetadataNode(tai_llvmbasemetadatanode(hp));
end;
ait_llvmmetadatareftypedconst:
begin
{ must only appear as an element in a typed const }
if fdecllevel=0 then
internalerror(2019050110);
writer.AsmWrite('!');
writer.AsmWrite(tai_llvmbasemetadatanode(tai_llvmmetadatareftypedconst(hp).val).name);
end;
ait_llvmmetadatarefoperand:
begin
{ must only appear as an operand }
if fdecllevel=0 then
internalerror(2019050101);
writer.AsmWrite('!');
writer.AsmWrite(tai_llvmmetadatareferenceoperand(hp).id);
writer.AsmWrite(' !');
writer.AsmWrite(tai_llvmmetadatareferenceoperand(hp).value.name);
end;
ait_symbolpair:
begin
{ should be emitted as part of the symbol def }
internalerror(2013010708);
end;
ait_symbol_end :
begin
if tai_symbol_end(hp).sym.typ=AT_FUNCTION then
writer.AsmWriteln('}')
else
writer.AsmWriteln('; ait_symbol_end error, should not be generated');
// internalerror(2013010711);
end;
ait_instruction :
begin
WriteInstruction(hp);
end;
ait_llvmins:
begin
WriteLlvmInstruction(hp);
end;
ait_stab :
begin
internalerror(2013010712);
end;
ait_force_line,
ait_function_name :
;
ait_cutobject :
begin
end;
ait_marker :
case
tai_marker(hp).kind of
mark_NoLineInfoStart:
inc(InlineLevel);
mark_NoLineInfoEnd:
dec(InlineLevel);
{ these cannot be nested }
mark_AsmBlockStart:
asmblock:=true;
mark_AsmBlockEnd:
asmblock:=false;
else
;
end;
ait_directive :
begin
{ CPU directive is commented out for the LLVM }
if tai_directive(hp).directive=asd_cpu then
writer.AsmWrite(asminfo^.comment);
WriteDirectiveName(tai_directive(hp).directive);
if tai_directive(hp).name <>'' then
writer.AsmWrite(tai_directive(hp).name);
if fdecllevel<>0 then
internalerror(2015090602);
writer.AsmLn;
end;
ait_seh_directive :
begin
internalerror(2013010713);
end;
ait_typedconst:
begin
WriteTypedConstData(tai_abstracttypedconst(hp),false);
end
else
if not WriteComments(hp) then
internalerror(2019012010);
end;
end;
constructor TLLVMAssember.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
begin
inherited;
InstrWriter:=TLLVMInstrWriter.create(self);
end;
procedure TLLVMAssember.WriteDirectiveName(dir: TAsmDirective);
begin
writer.AsmWrite('.'+directivestr[dir]+' ');
end;
procedure TLLVMAssember.WriteAsmList;
var
hal : tasmlisttype;
a: TExternalAssembler;
decorator: TLLVMModuleInlineAssemblyDecorator;
begin
WriteExtraHeader;
for hal:=low(TasmlistType) to high(TasmlistType) do
begin
if not assigned(current_asmdata.asmlists[hal]) or
current_asmdata.asmlists[hal].Empty then
continue;
writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
if not(hal in [al_pure_assembler,al_dwarf_frame]) then
writetree(current_asmdata.asmlists[hal])
else
begin
{ write routines using the target-specific external assembler
writer, filtered using the LLVM module-level assembly
decorator }
decorator:=TLLVMModuleInlineAssemblyDecorator.Create;
writer.decorator:=decorator;
a:=GetExternalGnuAssemblerWithAsmInfoWriter(asminfo,writer);
a.WriteTree(current_asmdata.asmlists[hal]);
writer.decorator:=nil;
decorator.free;
a.free;
end;
writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmlistTypeStr[hal]);
end;
writer.AsmLn;
end;
procedure TLLVMAssember.WriteFunctionInlineAsmList(list: tasmlist);
var
a: TExternalAssembler;
begin
if not assigned(ffuncinlasmdecorator) then
ffuncinlasmdecorator:=TLLVMFunctionInlineAssemblyDecorator.create;
if assigned(writer.decorator) then
internalerror(2016110201);
writer.decorator:=ffuncinlasmdecorator;
a:=GetExternalGnuAssemblerWithAsmInfoWriter(asminfo,writer);
a.WriteTree(list);
a.free;
writer.decorator:=nil;
end;
{****************************************************************************}
{ LLVM Instruction Writer }
{****************************************************************************}
constructor TLLVMInstrWriter.create(_owner: TLLVMAssember);
begin
inherited create;
owner := _owner;
end;
{****************************************************************************}
{ clang Assember }
{****************************************************************************}
function TLLVMClangAssember.MakeCmdLine: TCmdStr;
var
wpostr,
optstr: TCmdStr;
begin
wpostr:='';
if cs_lto in current_settings.moduleswitches then
begin
case fnextpass of
0:
begin
ObjFileName:=ChangeFileExt(ObjFileName,'.bc');
wpostr:=' -flto';
end;
1:
begin
ObjFileName:=ChangeFileExt(ObjFileName,'.o');
end;
end;
end;
result:=inherited;
if cs_opt_level3 in current_settings.optimizerswitches then
optstr:='-O3'
else if cs_opt_level2 in current_settings.optimizerswitches then
optstr:='-O2'
else if cs_opt_level1 in current_settings.optimizerswitches then
optstr:='-O1'
else
optstr:='-O0';
optstr:=optstr+wpostr;
{ stack frame elimination }
if not(cs_opt_stackframe in current_settings.optimizerswitches) then
optstr:=optstr+' -fno-omit-frame-pointer'
else
optstr:=optstr+' -fomit-frame-pointer';
{ fast math }
if cs_opt_fastmath in current_settings.optimizerswitches then
optstr:=optstr+' -ffast-math';
{ smart linking }
if cs_create_smart in current_settings.moduleswitches then
optstr:=optstr+' -fdata-sections -ffunction-sections';
{ pic }
if cs_create_pic in current_settings.moduleswitches then
optstr:=optstr+' -fpic'
else if not(target_info.system in systems_darwin) then
optstr:=optstr+' -static'
else
optstr:=optstr+' -mdynamic-no-pic';
if fputypestrllvm[current_settings.fputype]<>'' then
optstr:=optstr+' -m'+fputypestrllvm[current_settings.fputype];
replace(result,'$OPT',optstr);
inc(fnextpass);
end;
function TLLVMClangAssember.DoAssemble: boolean;
begin
fnextpass:=0;
result:=inherited;
end;
function TLLVMClangAssember.RerunAssembler: boolean;
begin
result:=
(cs_lto in current_settings.moduleswitches) and
(fnextpass<=1);
end;
function TLLVMClangAssember.DoPipe: boolean;
begin
result:=
not(cs_lto in current_settings.moduleswitches) and
inherited;
end;
const
as_clang_llvm_info : tasminfo =
(
id : as_clang_llvm;
idtxt : 'CLANG-LLVM';
asmbin : 'clang';
asmcmd: '-x ir $OPT -target $TRIPLET -c -o $OBJ $ASM $EXTRAOPT';
supported_targets : [system_x86_64_linux,system_x86_64_darwin,system_aarch64_darwin,system_aarch64_linux,system_arm_linux];
flags : [af_smartlink_sections,af_llvm];
labelprefix : 'L';
labelmaxlen : -1;
comment : '; ';
dollarsign: '$';
);
begin
RegisterAssembler(as_clang_llvm_info,TLLVMClangAssember);
end.
|