summaryrefslogtreecommitdiff
path: root/avx512-0037785/compiler/x86/agx86nsm.pas
blob: cbff1917561ed3e081edfbf427b76655268082ca (plain)
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
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
{
    Copyright (c) 1998-2002 by Florian Klaempfl

    This unit implements an asmoutput class for the Nasm assembler with
    Intel syntax for the i386+

    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 agx86nsm;

{$i fpcdefs.inc}

interface

    uses
      cclasses,cpubase,globtype,
      aasmbase,aasmtai,aasmdata,aasmcpu,assemble,cgutils;

    type

      { TX86NasmAssembler }

      TX86NasmAssembler = class(texternalassembler)
      strict private
        type

          { TX86NasmSection }

          TX86NasmSection=class(TFPHashObject)
          end;

          { TX86NasmGroup }

          TX86NasmGroup=class(TFPHashObject)
            Sections: TFPHashObjectList;
            constructor Create(HashObjectList:TFPHashObjectList;const s:TSymStr);
            destructor Destroy;override;
          end;
      private
        FSections: TFPHashObjectList;
        FGroups: TFPHashObjectList;
        using_relative : boolean;
        function CodeSectionName(const aname:string): string;
        procedure WriteReference(var ref : treference);
        procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
        procedure WriteOper_jmp(const o:toper; ai : taicpu);
        procedure WriteSection(atype:TAsmSectiontype;const aname:string;alignment : longint);
        procedure WriteHiddenSymbolAttribute(sym: TAsmSymbol);
        procedure ResetSectionsList;
        procedure AddGroup(const grpname: string);
        procedure AddSegmentToGroup(const grpname,segname: string);
        procedure WriteGroup(data:TObject;arg:pointer);
        procedure WriteGroups;
      protected
        function single2str(d: single): string; override;
        function double2str(d: double): string; override;
        function extended2str(e: extended): string; override;
      public
        destructor Destroy;override;
        procedure WriteTree(p:TAsmList);override;
        procedure WriteAsmList;override;
        procedure WriteExternals;
        procedure WriteSmartExternals;
        procedure WriteHeader;
        function  MakeCmdLine: TCmdStr;override;
      end;



  implementation

    uses
      cutils,globals,systems,fpccrc,
      fmodule,finput,verbose,cpuinfo,cgbase,omfbase
      ;

    const
      line_length = 64;

      nasm_regname_table : array[tregisterindex] of string[13] = (
        {r386nasm.inc contains the Nasm name of each register.}
{$if defined(x86_64)}
        {$i r8664nasm.inc}
{$elseif defined(i386)}
        {$i r386nasm.inc}
{$elseif defined(i8086)}
        {$i r8086nasm.inc}
{$endif}
      );
      { nasm 2.13 expects lowercase cpu names }
      nasm_cpu_name : array[tcputype] of string = (
{$if defined(x86_64)}
        'ia64',        // cpu_none,
        'x64',         // cpu_athlon64,
        'ia64',        // cpu_core_i,
        'ia64',        // cpu_core_avx,
        'ia64'         // cpu_core_avx2
{$elseif defined(i386)}
        'ia64',     // cpu_none,
        '386',      // cpu_386,
        '486',      // cpu_486,
        'pentium',  // cpu_Pentium,
        'p2',       // cpu_Pentium2,
        'p3',       // cpu_Pentium3,
        'p4',       // cpu_Pentium4,
        'p4',       // cpu_PentiumM,
        'ia64',     // cpu_core_i,
        'ia64',     // cpu_core_avx,
        'ia64'      // cpu_core_avx2
{$elseif defined(i8086)}
        'ia64',    // cpu_none
        '8086',    // cpu_8086
        '186',     // cpu_186
        '286',     // cpu_286
        '386',     // cpu_386
        '486',     // cpu_486
        'pentium', // cpu_Pentium
        'p2',      // cpu_Pentium2
        'p3',      // cpu_Pentium3
        'p4',      // cpu_Pentium4
        'p4'       // cpu_PentiumM
{$endif}
      );

    function nasm_regname(r:Tregister):string;
      var
        p : tregisterindex;
      begin
        p:=findreg_by_number(r);
        if p<>0 then
          result:=nasm_regname_table[p]
        else
          result:=generic_regname(r);
      end;


    function TX86NasmAssembler.single2str(d: single): string;
      var
         hs : string;
         p : longint;
      begin
         str(d,hs);
      { nasm expects a lowercase e }
         p:=pos('E',hs);
         if p>0 then
          hs[p]:='e';
         p:=pos('+',hs);
         if p>0 then
          delete(hs,p,1);
         single2str:=lower(hs);
      end;


    function TX86NasmAssembler.double2str(d: double): string;
      var
         hs : string;
         p : longint;
      begin
         str(d,hs);
      { nasm expects a lowercase e }
         p:=pos('E',hs);
         if p>0 then
          hs[p]:='e';
         p:=pos('+',hs);
         if p>0 then
          delete(hs,p,1);
         double2str:=lower(hs);
      end;


    function TX86NasmAssembler.extended2str(e: extended): string;
      var
         hs : string;
         p : longint;
      begin
         str(e,hs);
      { nasm expects a lowercase e }
         p:=pos('E',hs);
         if p>0 then
          hs[p]:='e';
         p:=pos('+',hs);
         if p>0 then
          delete(hs,p,1);
         extended2str:=lower(hs);
      end;


    destructor TX86NasmAssembler.Destroy;
      begin
        FSections.Free;
        FGroups.Free;
        inherited Destroy;
      end;


    function sizestr(s:topsize;dest:boolean):string;
      begin
        case s of
           S_B : sizestr:='byte ';
           S_W : sizestr:='word ';
           S_L : sizestr:='dword ';
           S_Q : sizestr:='qword ';
           S_IS : sizestr:='word ';
           S_IL : sizestr:='dword ';
           S_IQ : sizestr:='qword ';
           S_FS : sizestr:='dword ';
           S_FL : sizestr:='qword ';
           S_FX : sizestr:='tword ';
           S_BW : if dest then
               sizestr:='word '
             else
               sizestr:='byte ';
           S_BL : if dest then
               sizestr:='dword '
             else
               sizestr:='byte ';
           S_WL : if dest then
               sizestr:='dword '
             else
               sizestr:='word ';
{$ifdef x86_64}
           S_BQ : if dest then
                   sizestr:='qword '
                  else
                   sizestr:='byte ';
           S_WQ : if dest then
                   sizestr:='qword '
                  else
                   sizestr:='word ';
           S_LQ : if dest then
                   sizestr:='qword '
                  else
                   sizestr:='dword ';
           { Nothing needed for XMM registers }
           S_XMM: sizestr:='';

{$endif x86_64}
          else { S_NO }
            sizestr:='';
        end;
      end;


    Function PadTabs(const p:string;addch:char):string;
      var
        s : string;
        i : longint;
      begin
        i:=length(p);
        if addch<>#0 then
         begin
           inc(i);
           s:=p+addch;
         end
        else
         s:=p;
        if i<8 then
         PadTabs:=s+#9#9
        else
         PadTabs:=s+#9;
      end;



{****************************************************************************
                       TX86NasmAssembler.TX86NasmGroup
 ****************************************************************************}


    constructor TX86NasmAssembler.TX86NasmGroup.Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
      begin
        inherited;
        Sections:=TFPHashObjectList.Create;
      end;


    destructor TX86NasmAssembler.TX86NasmGroup.Destroy;
      begin
        Sections.Free;
        inherited Destroy;
      end;


{****************************************************************************
                               TX86NasmAssembler
 ****************************************************************************}


    function TX86NasmAssembler.CodeSectionName(const aname:string): string;
      begin
{$ifdef i8086}
        if current_settings.x86memorymodel in x86_far_code_models then
          begin
            if cs_huge_code in current_settings.moduleswitches then
              result:=TrimStrCRC32(aname,30) + '_TEXT'
            else
              result:=current_module.modulename^ + '_TEXT';
          end
        else
          result:='_TEXT';
{$else i8086}
        result:='.text';
{$endif}
      end;


    procedure TX86NasmAssembler.WriteReference(var ref : treference);
      var
        first : boolean;
        base_done : boolean;
      begin
        with ref do
         begin
           writer.AsmWrite('[');
           first:=true;
           base_done:=false;
           if (segment<>NR_NO) then
             writer.AsmWrite(nasm_regname(segment)+':');
{$ifdef x86_64}
          if (base=NR_RIP) then
            begin
              { nasm RIP is implicit for pic }
              if not (ref.refaddr in [addr_pic,addr_pic_no_got]) and not using_relative then
                writer.AsmWrite('$ + ');
              base_done:=true;
            end;
{$endif x86_64}
           if assigned(symbol) then
            begin
              writer.AsmWrite(ApplyAsmSymbolRestrictions(symbol.name));
              if SmartAsm then
                AddSymbol(symbol.name,false);
              first:=false;
            end;
           if (base<>NR_NO) and not base_done then
            begin
              if not(first) then
               writer.AsmWrite('+')
              else
               first:=false;
              writer.AsmWrite(nasm_regname(base))
            end;
           if (index<>NR_NO) then
             begin
               if not(first) then
                 writer.AsmWrite('+')
               else
                 first:=false;
               writer.AsmWrite(nasm_regname(index));
               if scalefactor<>0 then
                 writer.AsmWrite('*'+tostr(scalefactor));
             end;
           if offset<0 then
             begin
               writer.AsmWrite(tostr(offset));
               first:=false;
             end
           else if (offset>0) then
             begin
               writer.AsmWrite('+'+tostr(offset));
               first:=false;
             end;
           if first then
             writer.AsmWrite('0');
           writer.AsmWrite(']');
         end;
       end;


    procedure TX86NasmAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
      begin
        case o.typ of
          top_reg :
            writer.AsmWrite(nasm_regname(o.reg));
          top_const :
            begin
              if (ops=1) and (opcode<>A_RET) then
               writer.AsmWrite(sizestr(s,dest));
              writer.AsmWrite(tostr(o.val));
            end;
          top_ref :
            begin
              if o.ref^.refaddr in [addr_no,addr_pic,addr_pic_no_got] then
                begin
                  if not ((opcode = A_LEA) or (opcode = A_LGS) or
                          (opcode = A_LSS) or (opcode = A_LFS) or
{$ifndef x86_64}
                          (opcode = A_LES) or (opcode = A_LDS) or
{$endif x86_64}
                         // (opcode = A_SHR) or (opcode = A_SHL) or
                         // (opcode = A_SAR) or (opcode = A_SAL) or
                          (opcode = A_OUT) or (opcode = A_IN)) then
                    writer.AsmWrite(sizestr(s,dest));
                  WriteReference(o.ref^);
                end
{$ifdef i8086}
              else if o.ref^.refaddr=addr_dgroup then
                begin
                  writer.AsmWrite('DGROUP');
                  { Make sure GROUP DGROUP is generated }
                  AddGroup('DGROUP');
                end
              else if o.ref^.refaddr=addr_fardataseg then
                begin
                  writer.AsmWrite(current_module.modulename^+'_DATA');
                end
{$endif i8086}
              else
                begin
{$ifdef x86_64}
                  if s=S_L then
                    writer.AsmWrite('dword ')
                  else
                    writer.AsmWrite('qword ');
{$endif}
{$ifdef i386}
                  writer.AsmWrite('dword ');
{$endif i386}
{$ifdef i8086}
                  if o.ref^.refaddr=addr_seg then
                    writer.AsmWrite('SEG ')
                  else
                    writer.AsmWrite('word ');
{$endif i8086}
                  if assigned(o.ref^.symbol) then
                   begin
                    if SmartAsm then
                      AddSymbol(o.ref^.symbol.name,false);
                    writer.AsmWrite(ApplyAsmSymbolRestrictions(o.ref^.symbol.name));
                    if o.ref^.offset=0 then
                      exit;
                   end;
                  if o.ref^.offset>0 then
                   writer.AsmWrite('+');
                  writer.AsmWrite(tostr(o.ref^.offset));
                end;
            end;
          else
            internalerror(2020100812);
        end;
      end;


    procedure TX86NasmAssembler.WriteOper_jmp(const o:toper; ai : taicpu);
      begin
        case o.typ of
          top_reg :
            writer.AsmWrite(nasm_regname(o.reg));
          top_ref :
            if o.ref^.refaddr=addr_no then
              begin
                if ai.opsize=S_FAR then
                  writer.AsmWrite('far ');
                WriteReference(o.ref^);
              end
            else
              begin
                if ai.opsize=S_FAR then
                  writer.AsmWrite('far ');
                { else
                   writer.AsmWrite('near ') just disables short branches, increasing code size.
                   Omitting it does not cause any bad effects, tested with nasm 2.11. }

                writer.AsmWrite(ApplyAsmSymbolRestrictions(o.ref^.symbol.name));
                if SmartAsm then
                  AddSymbol(o.ref^.symbol.name,false);
                if o.ref^.offset>0 then
                 writer.AsmWrite('+'+tostr(o.ref^.offset))
                else
                 if o.ref^.offset<0 then
                  writer.AsmWrite(tostr(o.ref^.offset));
              end;
          top_const :
            writer.AsmWrite(tostr(aint(o.val)));
          else
            internalerror(2020100813);
        end;
      end;


    const
      ait_const2str : array[aitconst_128bit..aitconst_64bit_unaligned] of string[30]=(
        #9'FIXME_128BIT'#9,#9'DQ'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
        #9'FIXME_SLEB128BIT'#9,#9'FIXME_ULEB128BIT'#9,
        #9'RVA'#9,#9'SECREL32'#9,#9'FIXME_darwin_dwarf_delta64'#9,
        #9'FIXME_darwin_dwarf_delta32'#9,#9'FIXME_half16bit'#9,#9'FIXME_gs'#9,
        #9'DW'#9,#9'DD'#9,#9'FIXME_64BIT_UNALIGNED'#9
      );

    procedure TX86NasmAssembler.WriteSection(atype : TAsmSectiontype;
      const aname : string; alignment : longint);
      const
        secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
          '.text',
          '.data',
          '.data',
          '.rodata',
          '.bss',
          '.tbss',
          '.pdata',
          '.text','.data','.data','.data','.data',
          '.stab',
          '.stabstr',
          '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
          '.eh_frame',
          '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
          '.fpc',
          '',
          '.init',
          '.fini',
          '.objc_class',
          '.objc_meta_class',
          '.objc_cat_cls_meth',
          '.objc_cat_inst_meth',
          '.objc_protocol',
          '.objc_string_object',
          '.objc_cls_meth',
          '.objc_inst_meth',
          '.objc_cls_refs',
          '.objc_message_refs',
          '.objc_symbols',
          '.objc_category',
          '.objc_class_vars',
          '.objc_instance_vars',
          '.objc_module_info',
          '.objc_class_names',
          '.objc_meth_var_types',
          '.objc_meth_var_names',
          '.objc_selector_strs',
          '.objc_protocol_ext',
          '.objc_class_ext',
          '.objc_property',
          '.objc_image_info',
          '.objc_cstring_object',
          '.objc_sel_fixup',
          '__DATA,__objc_data',
          '__DATA,__objc_const',
          '.objc_superrefs',
          '__DATA, __datacoal_nt,coalesced',
          '.objc_classlist',
          '.objc_nlclasslist',
          '.objc_catlist',
          '.obcj_nlcatlist',
          '.objc_protolist',
          '.stack',
          '.heap',
          ',gcc_except_table',
          ',ARM_attributes'
        );
      var
        secname,secgroup: string;
      begin
        writer.AsmLn;
        writer.AsmWrite('SECTION ');
        { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
          Thus, data which normally goes into .rodata and .rodata_norel sections must
          end up in .data section }
        if (atype in [sec_rodata,sec_rodata_norel]) and
          (target_info.system=system_i386_go32v2) then
          writer.AsmWrite('.data')
        else if (atype=sec_threadvar) and
          (target_info.system in (systems_windows+systems_wince)) then
          writer.AsmWrite('.tls'#9'bss')
        else if target_info.system in [system_i8086_msdos,system_i8086_win16,system_i8086_embedded] then
          begin
            if (atype=sec_user) then
              secname:=aname
            else if secnames[atype]='.text' then
              secname:=CodeSectionName(aname)
            else if omf_segclass(atype)='FAR_DATA' then
              secname:=current_module.modulename^ + '_DATA'
            else
              secname:=omf_secnames[atype];
            writer.AsmWrite(secname);
            { first use of this section in the object file? }
            if FSections.Find(secname)=nil then
              begin
                { yes -> write the section attributes as well }
                if atype=sec_stack then
                  writer.AsmWrite(' stack');
                if atype in [sec_debug_frame,sec_debug_info,sec_debug_line,sec_debug_abbrev,sec_debug_aranges,sec_debug_ranges] then
                  writer.AsmWrite(' use32')
                else
                  writer.AsmWrite(' use16');
                writer.AsmWrite(' class='+omf_segclass(atype)+
                  ' align='+tostr(omf_sectiontype2align(atype)));
                TX86NasmSection.Create(FSections,secname);
                secgroup:=omf_section_primary_group(atype,aname);
                if secgroup<>'' then
                  AddSegmentToGroup(secgroup,secname);
              end;
          end
        else if (atype=sec_user) then
          writer.AsmWrite(aname)
        else if secnames[atype]='.text' then
          writer.AsmWrite(CodeSectionName(aname))
        else
          writer.AsmWrite(secnames[atype]);
        if create_smartlink_sections and
           (atype<>sec_bss) and
           (aname<>'') then
          begin
            writer.AsmWrite('.');
            writer.AsmWrite(aname);
            if atype in [sec_init, sec_fini, sec_stub, sec_code] then
              writer.AsmWrite(' code align='+tostr(alignment))
            else if  atype in [sec_rodata, sec_rodata_norel] then
              writer.AsmWrite(' rdata align='+tostr(alignment))
            else
              writer.AsmWrite(' data align='+tostr(alignment))
          end;
        writer.AsmLn;
        LastSecType:=atype;
      end;

    procedure TX86NasmAssembler.WriteHiddenSymbolAttribute(sym: TAsmSymbol);
      begin
        if target_info.system in systems_windows then
          exit;
        if target_info.system in systems_darwin then
          writer.AsmWrite(':private_extern')
        else
          case sym.typ of
            AT_FUNCTION:
              writer.AsmWrite(':function hidden');
            AT_DATA:
              writer.AsmWrite(':data hidden');
            else
              Internalerror(2020111301);
          end;
      end;

    procedure TX86NasmAssembler.ResetSectionsList;
      begin
        FSections.Free;
        FSections:=TFPHashObjectList.Create;
        FGroups.Free;
        FGroups:=TFPHashObjectList.Create;
      end;

    procedure TX86NasmAssembler.AddGroup(const grpname: string);
      begin
        if FGroups.Find(grpname)=nil then
          TX86NasmGroup.Create(FGroups,grpname);
      end;

    procedure TX86NasmAssembler.AddSegmentToGroup(const grpname, segname: string);
      var
        grp: TX86NasmGroup;
      begin
        grp:=TX86NasmGroup(FGroups.Find(grpname));
        if grp=nil then
          grp:=TX86NasmGroup.Create(FGroups,grpname);
        TX86NasmSection.Create(grp.Sections,segname);
      end;

    procedure TX86NasmAssembler.WriteGroup(data: TObject; arg: pointer);
      var
        grp: TX86NasmGroup;
        i: Integer;
      begin
        grp:=TX86NasmGroup(data);
        writer.AsmWrite('GROUP '+grp.Name);
        for i:=0 to grp.Sections.Count-1 do
          writer.AsmWrite(' '+grp.Sections.NameOfIndex(i));
        writer.AsmLn;
      end;

    procedure TX86NasmAssembler.WriteGroups;
      {$ifdef i8086}
      var
        i: Integer;
      {$endif i8086}
      begin
{$ifdef i8086}
        if target_info.system in [system_i8086_msdos,system_i8086_win16,system_i8086_embedded] then
          begin
            if current_settings.x86memorymodel=mm_huge then
              WriteSection(sec_data,'',2);
            writer.AsmLn;
            FGroups.ForEachCall(@WriteGroup,nil);
          end;
{$endif i8086}
      end;

    procedure TX86NasmAssembler.WriteTree(p:TAsmList);
{$ifdef cpuextended}
    type
      t80bitarray = array[0..9] of byte;
{$endif cpuextended}
    var
      s : string;
      hp       : tai;
      counter,
      lines,
      i,j,l    : longint;
      InlineLevel : longint;
      consttype : taiconst_type;
      do_line, SkipNewLine,
      quoted   : boolean;
      fixed_opcode: TAsmOp;
      prefix, LastSecName  : string;
      LastAlign : LongInt;
      cpu: tcputype;
      prevfileinfo : tfileposinfo;
      previnfile : tinputfile;
      NewObject :  boolean;
    begin
      if not assigned(p) then
       exit;
      InlineLevel:=0;
      NewObject:=true;

      { 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
            previnfile:=lastinfile;
            prevfileinfo:=lastfileinfo;
            current_filepos:=tailineinfo(hp).fileinfo;

            { no line info for inlined code }
            if do_line and (inlinelevel=0) then
              WriteSourceLine(hp as tailineinfo);
            if (lastfileinfo.line<>prevfileinfo.line) or
               (previnfile<>lastinfile) then
              begin
                { +0 postfix means no line increment per assembler instruction }
                writer.AsmWrite('%LINE '+tostr(current_filepos.line)+'+0');
                if assigned(lastinfile) and ((previnfile<>lastinfile) or NewObject) then
                  writer.AsmWriteLn(' '+lastinfile.name)
                else
                  writer.AsmLn;
                NewObject:=false;
              end;

          end;

         case hp.typ of
           ait_section :
             begin
               if tai_section(hp).sectype<>sec_none then
                 WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secalign);
               LastSecType:=tai_section(hp).sectype;
             end;

           ait_align :
             begin
               if (tai_align(hp).aligntype>1) then
                 begin
                   if (LastSecType=sec_bss) or (
                      (LastSecType=sec_threadvar) and
                      (target_info.system in (systems_windows+systems_wince))
                     ) then
                      writer.AsmWriteLn(#9'ALIGNB '+tostr(tai_align(hp).aligntype))
                    else if tai_align_abstract(hp).use_op then
                      writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype)+',DB '+tostr(tai_align_abstract(hp).fillop))
                    else if LastSecType in [sec_code,sec_stub,sec_init,sec_fini] then
                      writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype))
                    else
                      writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype)+',DB 0');
                 end;
             end;

           ait_datablock :
             begin
               if tai_datablock(hp).is_global or SmartAsm then
                begin
                  writer.AsmWrite(#9'GLOBAL ');
                  writer.AsmWrite(tai_datablock(hp).sym.name);
                  if tai_datablock(hp).sym.bind=AB_PRIVATE_EXTERN then
                    WriteHiddenSymbolAttribute(tai_datablock(hp).sym);
                  writer.AsmLn;
                end;
               writer.AsmWrite(PadTabs(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name),':'));
               if SmartAsm then
                 AddSymbol(tai_datablock(hp).sym.name,true);
               writer.AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
             end;

           ait_const:
             begin
               consttype:=tai_const(hp).consttype;
               case consttype of
                 aitconst_uleb128bit,
                 aitconst_sleb128bit,
                 aitconst_128bit:
                    begin
                      writer.AsmWriteLn(asminfo^.comment+'Unsupported const type '+
                        ait_const2str[consttype]);
                    end;
{$ifdef i8086}
                 aitconst_farptr:
                   begin
                     writer.AsmWrite(ait_const2str[aitconst_16bit]);
                     if assigned(tai_const(hp).sym) then
                       begin
                         if SmartAsm then
                           AddSymbol(tai_const(hp).sym.name,false);
                         writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_const(hp).sym.name));
                         if tai_const(hp).value<>0 then
                           writer.AsmWrite(tostr_with_plus(tai_const(hp).value));
                         writer.AsmLn;
                         writer.AsmWrite(ait_const2str[aitconst_16bit]);
                         writer.AsmWrite('SEG ');
                         writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_const(hp).sym.name));
                       end
                     else
                       writer.AsmWrite(tostr(lo(longint(tai_const(hp).value)))+','+
                                tostr(hi(longint(tai_const(hp).value))));
                     writer.AsmLn;
                   end;
                 aitconst_seg:
                   begin
                     writer.AsmWrite(ait_const2str[aitconst_16bit]);
                     if assigned(tai_const(hp).sym) then
                       begin
                         if SmartAsm then
                           AddSymbol(tai_const(hp).sym.name,false);
                         writer.AsmWrite('SEG ');
                         writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_const(hp).sym.name));
                       end
                     else
                       internalerror(2015110501);
                     writer.AsmLn;
                   end;
                 aitconst_dgroup:
                   writer.AsmWriteLn(#9'DW'#9'DGROUP');
                 aitconst_fardataseg:
                   writer.AsmWriteLn(#9'DW'#9+current_module.modulename^+'_DATA');
{$endif i8086}
{$ifdef x86_64}
                 aitconst_rva_symbol,
                 aitconst_secrel32_symbol: ;
{$endif x86_64}
{$ifdef i386}
                 aitconst_rva_symbol,
                 aitconst_secrel32_symbol,
{$endif i386}
                 aitconst_64bit,
                 aitconst_32bit,
                 aitconst_16bit,
                 aitconst_8bit,
                 aitconst_16bit_unaligned,
                 aitconst_32bit_unaligned,
                 aitconst_64bit_unaligned:
                   begin
                     writer.AsmWrite(ait_const2str[tai_const(hp).consttype]);
                     l:=0;
                     repeat
                       if assigned(tai_const(hp).sym) then
                         begin
                           if SmartAsm then
                             begin
                               AddSymbol(tai_const(hp).sym.name,false);
                               if assigned(tai_const(hp).endsym) then
                                 AddSymbol(tai_const(hp).endsym.name,false);
                             end;
                           if assigned(tai_const(hp).endsym) then
                             s:=ApplyAsmSymbolRestrictions(tai_const(hp).endsym.name)+'-'+ApplyAsmSymbolRestrictions(tai_const(hp).sym.name)
                           else
                             s:=ApplyAsmSymbolRestrictions(tai_const(hp).sym.name);
                           if tai_const(hp).value<>0 then
                             s:=s+tostr_with_plus(tai_const(hp).value);
                         end
                       else
                         s:=tostr(tai_const(hp).value);
                       writer.AsmWrite(s);
                       inc(l,length(s));
                       if (l>line_length) or
                          (hp.next=nil) or
                          (tai(hp.next).typ<>ait_const) or
                          (tai_const(hp.next).consttype<>consttype) then
                         break;
                       hp:=tai(hp.next);
                       writer.AsmWrite(',');
                     until false;
                     writer.AsmLn;
                   end;
                 else
                   internalerror(200704252);
               end;
             end;

           ait_realconst:
             begin
               WriteRealConstAsBytes(tai_realconst(hp),#9#9'DB'#9,do_line);
             end;

           ait_string :
             begin
               counter := 0;
               lines := tai_string(hp).len div line_length;
             { separate lines in different parts }
               if tai_string(hp).len > 0 then
                Begin
                  for j := 0 to lines-1 do
                   begin
                     writer.AsmWrite(#9#9'DB'#9);
                     quoted:=false;
                     for i:=counter to counter+line_length-1 do
                        begin
                          { it is an ascii character. }
                          if (ord(tai_string(hp).str[i])>31) and
                             (ord(tai_string(hp).str[i])<128) and
                             (tai_string(hp).str[i]<>'"') then
                              begin
                                if not(quoted) then
                                    begin
                                      if i>counter then
                                        writer.AsmWrite(',');
                                      writer.AsmWrite('"');
                                    end;
                                writer.AsmWrite(tai_string(hp).str[i]);
                                quoted:=true;
                              end { if > 31 and < 128 and ord('"') }
                          else
                              begin
                                  if quoted then
                                      writer.AsmWrite('"');
                                  if i>counter then
                                      writer.AsmWrite(',');
                                  quoted:=false;
                                  writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
                              end;
                       end; { end for i:=0 to... }
                     if quoted then writer.AsmWrite('"');
                       writer.AsmWrite(target_info.newline);
                     inc(counter,line_length);
                  end; { end for j:=0 ... }
                { do last line of lines }
                if counter<tai_string(hp).len then
                  writer.AsmWrite(#9#9'DB'#9);
                quoted:=false;
                for i:=counter to tai_string(hp).len-1 do
                  begin
                    { it is an ascii character. }
                    if (ord(tai_string(hp).str[i])>31) and
                       (ord(tai_string(hp).str[i])<128) and
                       (tai_string(hp).str[i]<>'"') then
                        begin
                          if not(quoted) then
                              begin
                                if i>counter then
                                  writer.AsmWrite(',');
                                writer.AsmWrite('"');
                              end;
                          writer.AsmWrite(tai_string(hp).str[i]);
                          quoted:=true;
                        end { if > 31 and < 128 and " }
                    else
                        begin
                          if quoted then
                            writer.AsmWrite('"');
                          if i>counter then
                              writer.AsmWrite(',');
                          quoted:=false;
                          writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
                        end;
                  end; { end for i:=0 to... }
                if quoted then
                  writer.AsmWrite('"');
                end;
               writer.AsmLn;
             end;

           ait_label :
             begin
               if tai_label(hp).labsym.is_used then
                 begin
                   if SmartAsm and (tai_label(hp).labsym.bind=AB_GLOBAL) then
                     begin
                       writer.AsmWrite(#9'GLOBAL ');
                       writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name));
                     end;
                   writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name)+':');
                 end;
               if SmartAsm then
                 AddSymbol(tai_label(hp).labsym.name,true);
             end;

           ait_symbol :
             begin
               if tai_symbol(hp).has_value then
                 internalerror(2009090803);
               if tai_symbol(hp).is_global or SmartAsm then
                begin
                  writer.AsmWrite(#9'GLOBAL ');
                  writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name));
                  if tai_symbol(hp).sym.bind=AB_PRIVATE_EXTERN then
                    WriteHiddenSymbolAttribute(tai_symbol(hp).sym);
                  writer.AsmLn;
                end;
               writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name));
               if SmartAsm then
                 AddSymbol(tai_symbol(hp).sym.name,true);
               if (not assigned(hp.next)) or (assigned(hp.next) and not(tai(hp.next).typ in
                  [ait_const,ait_realconst,ait_string])) then
                writer.AsmWriteLn(':')
             end;

           ait_symbol_end : ;

           ait_instruction :
             begin
               fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
               { We need intel order, no At&t }
               taicpu(hp).SetOperandOrder(op_intel);
               { LOCK must be on same line as opcode }
               if (taicpu(hp).ops = 0) and
                   (fixed_opcode = A_LOCK) then
                 SkipNewLine:=true
               else
                 SkipNewLine:=false;

               s:='';
               if ((fixed_opcode=A_FADDP) or
                   (fixed_opcode=A_FMULP))
                  and (taicpu(hp).ops=0) then
                 begin
                   taicpu(hp).allocate_oper(2);
                   taicpu(hp).oper[0]^.typ:=top_reg;
                   taicpu(hp).oper[0]^.reg:=NR_ST1;
                   taicpu(hp).oper[1]^.typ:=top_reg;
                   taicpu(hp).oper[1]^.reg:=NR_ST;
                 end;
                 { NASM only accepts move for loading of
                   simple symbol address }
                  if ((taicpu(hp).opcode=A_LEA) and
                      (taicpu(hp).ops=2) and
                      (taicpu(hp).oper[0]^.typ=top_reg) and
                      (reg2opsize(taicpu(hp).oper[0]^.reg) in [S_NO,S_Q]) and
                      (taicpu(hp).oper[1]^.typ=top_ref) and
                      (taicpu(hp).oper[1]^.ref^.refaddr<>addr_no) and
                      assigned(taicpu(hp).oper[1]^.ref^.symbol) and
                      (taicpu(hp).oper[1]^.ref^.base=NR_NO)) then
                    begin
                      writer.AsmWrite(asminfo^.comment);
                      writer.AsmWriteln('Converting LEA to MOV instruction');
                      taicpu(hp).opcode:=A_MOV;
                    end;
               if fixed_opcode=A_FWAIT then
                writer.AsmWriteln(#9#9'DB'#9'09bh')
               else if (fixed_opcode=A_XLAT) and (taicpu(hp).ops=1) and
                       (taicpu(hp).oper[0]^.typ=top_ref) then
                begin
                  writer.AsmWrite(#9#9);
                  if (taicpu(hp).oper[0]^.ref^.segment<>NR_NO) and
                     (taicpu(hp).oper[0]^.ref^.segment<>NR_DS) then
                    writer.AsmWrite(std_regname(taicpu(hp).oper[0]^.ref^.segment)+' ');
                  case get_ref_address_size(taicpu(hp).oper[0]^.ref^) of
                    16:
                      writer.AsmWrite('a16 ');
                    32:
                      writer.AsmWrite('a32 ');
                    64:
                      writer.AsmWrite('a64 ');
                  end;
                  writer.AsmWriteLn('xlatb');
                end
               else if is_x86_parameterized_string_op(fixed_opcode) then
                begin
                  writer.AsmWrite(#9#9);
                  i:=get_x86_string_op_si_param(fixed_opcode);
                  if (i<>-1) and (taicpu(hp).oper[i]^.typ=top_ref) and
                     (taicpu(hp).oper[i]^.ref^.segment<>NR_NO) and
                     (taicpu(hp).oper[i]^.ref^.segment<>NR_DS) then
                    writer.AsmWrite(std_regname(taicpu(hp).oper[i]^.ref^.segment)+' ');
                  for i:=0 to taicpu(hp).ops-1 do
                    if taicpu(hp).oper[i]^.typ=top_ref then
                      begin
                        case get_ref_address_size(taicpu(hp).oper[i]^.ref^) of
                          16:
                            writer.AsmWrite('a16 ');
                          32:
                            writer.AsmWrite('a32 ');
                          64:
                            writer.AsmWrite('a64 ');
                        end;
                        break;
                      end;
                  writer.AsmWrite(std_op2str[fixed_opcode]);

                  case taicpu(hp).opsize of
                    S_B:
                      writer.AsmWrite('b');
                    S_W:
                      writer.AsmWrite('w');
                    S_L:
                      writer.AsmWrite('d');
                    S_Q:
                      writer.AsmWrite('q');
                    else
                      internalerror(2017101101);
                  end;
                  writer.AsmLn;
                end
               else
                begin
                  prefix:='';
{$ifndef i8086}
                  { We need to explicitely set
                    word prefix to get selectors
                    to be pushed in 2 bytes  PM }
                  if (taicpu(hp).opsize=S_W) and
                     ((fixed_opcode=A_PUSH) or
                      (fixed_opcode=A_POP)) and
                      (taicpu(hp).oper[0]^.typ=top_reg) and
                      (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
                    writer.AsmWriteln(#9#9'DB'#9'066h');
{$endif not i8086}
                  if (fixed_opcode=A_RETW) or (fixed_opcode=A_RETNW) or (fixed_opcode=A_RETFW) or
{$ifdef x86_64}
                     (fixed_opcode=A_RETQ) or (fixed_opcode=A_RETNQ) or (fixed_opcode=A_RETFQ) or
{$else x86_64}
                     (fixed_opcode=A_RETD) or (fixed_opcode=A_RETND) or
{$endif x86_64}
                     (fixed_opcode=A_RETFD) then
                   begin
                     case fixed_opcode of
                       A_RETW:
                         writer.AsmWrite(#9#9'o16 ret');
                       A_RETNW:
                         writer.AsmWrite(#9#9'o16 retn');
                       A_RETFW:
                         writer.AsmWrite(#9#9'o16 retf');
{$ifdef x86_64}
                       A_RETQ,
                       A_RETNQ:
                         writer.AsmWrite(#9#9'ret');
                       A_RETFQ:
                         writer.AsmWrite(#9#9'o64 retf');
{$else x86_64}
                       A_RETD:
                         writer.AsmWrite(#9#9'o32 ret');
                       A_RETND:
                         writer.AsmWrite(#9#9'o32 retn');
{$endif x86_64}
                       A_RETFD:
                         writer.AsmWrite(#9#9'o32 retf');
                       else
                         internalerror(2017111001);
                     end;
                   end
                  else if (fixed_opcode=A_SEGCS) or (fixed_opcode=A_SEGDS) or
                          (fixed_opcode=A_SEGSS) or (fixed_opcode=A_SEGES) or
                          (fixed_opcode=A_SEGFS) or (fixed_opcode=A_SEGGS) then
                    begin
                      case fixed_opcode of
                        A_SEGCS:
                          writer.AsmWrite(#9#9'cs');
                        A_SEGDS:
                          writer.AsmWrite(#9#9'ds');
                        A_SEGSS:
                          writer.AsmWrite(#9#9'ss');
                        A_SEGES:
                          writer.AsmWrite(#9#9'es');
                        A_SEGFS:
                          writer.AsmWrite(#9#9'fs');
                        A_SEGGS:
                          writer.AsmWrite(#9#9'gs');
                        else
                          internalerror(2018020101);
                      end;
                    end
                  else
                    writer.AsmWrite(#9#9+prefix+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]);
                  if taicpu(hp).ops<>0 then
                   begin
                     if is_calljmp(fixed_opcode) then
                      begin
                        writer.AsmWrite(#9);
                        WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp));
                      end
                     else
                      begin
                        for i:=0 to taicpu(hp).ops-1 do
                         begin
                           if i=0 then
                            writer.AsmWrite(#9)
                           else
                            writer.AsmWrite(',');
                           WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,taicpu(hp).ops,(i=2));
                         end;
                      end;
                   end;
                  if not SkipNewLine then
                    writer.AsmLn;
                end;
             end;

           ait_stab,
           ait_force_line,
           ait_function_name : ;

           ait_cutobject :
             begin
               if SmartAsm then
                begin
                 { only reset buffer if nothing has changed }
                 if not writer.ClearIfEmpty then
                  begin
                    if SmartAsm then
                      begin
                        WriteSmartExternals;
                        FreeExternChainList;
                      end;
                    WriteGroups;
                    writer.AsmClose;
                    DoAssemble;
                    writer.AsmCreate(tai_cutobject(hp).place);
                    ResetSectionsList;
                    WriteHeader;
                  end;
               { avoid empty files }
                 LastSecType:=sec_none;
                 LastSecName:='';
                 LastAlign:=4;
                 while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
                  begin
                    if tai(hp.next).typ=ait_section then
                      begin
                        LastSecType:=tai_section(hp.next).sectype;
                        LastSecName:=tai_section(hp.next).name^;
                        LastAlign:=tai_section(hp.next).secalign;
                      end;
                    hp:=tai(hp.next);
                  end;
                 if LastSecType<>sec_none then
                   WriteSection(LastSecType,LastSecName,LastAlign);
                 writer.MarkEmpty;
                 NewObject:=true;
               end;
             end;

           ait_marker :
             if tai_marker(hp).kind=mark_NoLineInfoStart then
               inc(InlineLevel)
             else if tai_marker(hp).kind=mark_NoLineInfoEnd then
               dec(InlineLevel);

           ait_directive :
             begin
               case tai_directive(hp).directive of
                 asd_nasm_import,
                 asd_extern :
                   begin
                     case tai_directive(hp).directive of
                       asd_nasm_import :
                         writer.AsmWrite('import ');
                       asd_extern :
                         writer.AsmWrite('EXTERN ');
                       else
                         internalerror(200509191);
                     end;
                     if tai_directive(hp).name<>'' then
                       begin

                         if SmartAsm then
                           AddSymbol(tai_directive(hp).name,false);

                         writer.AsmWrite(tai_directive(hp).name);
                       end;
                   end;
                 asd_cpu :
                   begin
                     writer.AsmWrite('CPU ');
                     for cpu:=low(tcputype) to high(tcputype) do
                       begin
                         if tai_directive(hp).name=CPUTypeStr[CPU] then
                           begin
                             writer.AsmWriteLn(nasm_cpu_name[cpu]);
                             break;
                           end;
                       end;
                   end;
{$ifdef OMFOBJSUPPORT}
                 asd_omf_linnum_line :
                   writer.AsmWriteLn('; OMF LINNUM Line '+tai_directive(hp).name);
{$endif OMFOBJSUPPORT}
                 else
                   internalerror(2005091903);
               end;
               writer.AsmLn;
             end;
           ait_seh_directive :
             { Ignore for now };
           else
             if not WriteComments(hp) then
               internalerror(2020100801);
         end;
         hp:=tai(hp.next);
       end;
    end;


    procedure TX86NasmAssembler.WriteExternals;
      var
        sym : TAsmSymbol;
        i   : longint;
      begin
        for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
          begin
            sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
            if sym.bind in [AB_EXTERNAL,AB_EXTERNAL_INDIRECT] then
              writer.AsmWriteln('EXTERN'#9+ApplyAsmSymbolRestrictions(sym.name));
          end;
      end;

    procedure TX86NasmAssembler.WriteSmartExternals;
      var
        EC : PExternChain;
      begin
        EC:=FEC;
        while assigned(EC) do
          begin
            if not EC^.is_defined then
              writer.AsmWriteln('EXTERN'#9+ApplyAsmSymbolRestrictions(EC^.psym^));
            EC:=EC^.next;
          end;
      end;

    procedure TX86NasmAssembler.WriteHeader;
      begin
{$if defined(i8086)}
        writer.AsmWriteLn('BITS 16');
{$elseif defined(i386)}
        writer.AsmWriteLn('BITS 32');
        using_relative:=false;
{$elseif defined(x86_64)}
        writer.AsmWriteLn('BITS 64');
        writer.AsmWriteLn('default rel');
        using_relative:=true;
{$endif}
        writer.AsmWriteLn('CPU '+nasm_cpu_name[current_settings.cputype]);
      end;


    procedure TX86NasmAssembler.WriteAsmList;
    var
      hal : tasmlisttype;
    begin
{$ifdef EXTDEBUG}
      if current_module.mainsource<>'' then
       comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource);
{$endif}
      ResetSectionsList;
      WriteHeader;
      writer.AsmLn;

      WriteExternals;

      for hal:=low(TasmlistType) to high(TasmlistType) do
        begin
          if not (current_asmdata.asmlists[hal].empty) then
            begin
              writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
              writetree(current_asmdata.asmlists[hal]);
              writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
            end;
        end;

      writer.AsmLn;
      if SmartAsm then
        begin
          WriteSmartExternals;
          FreeExternChainList;
        end;
      WriteGroups;
{$ifdef EXTDEBUG}
      if current_module.mainsource<>'' then
       comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource);
{$endif EXTDEBUG}
   end;

    function TX86NasmAssembler.MakeCmdLine: TCmdStr;
      var
        FormatName : string;
      begin
        result:=Inherited MakeCmdLine;
{$ifdef i8086}
        case target_info.system of
          system_i8086_msdos,
          system_i8086_win16,
          system_i8086_embedded:
            begin
              FormatName:='obj';
              if (cs_debuginfo in current_settings.moduleswitches) or
                 (cs_asm_source in current_settings.globalswitches) then
                Replace(result,'$DEBUG','-g')
              else
                Replace(result,'$DEBUG','');
            end
          else
            internalerror(2014082060);
        end;
{$endif i8086}
{$ifdef i386}
        case target_info.system of
          system_i386_go32v2:
            FormatName:='coff';
          system_i386_wdosx,
          system_i386_win32:
            FormatName:='win32';
          system_i386_embedded:
            FormatName:='obj';
          system_i386_linux,
          system_i386_beos:
            FormatName:='elf';
          system_i386_darwin:
            FormatName:='macho32';
        else
          FormatName:='elf';
        end;
{$endif i386}
{$ifdef x86_64}
        case target_info.system of
          system_x86_64_win64:
            FormatName:='win64';
          system_x86_64_darwin:
            FormatName:='macho64';
          system_x86_64_embedded:
            FormatName:='obj';
          system_x86_64_linux:
            FormatName:='elf64';
        else
          FormatName:='elf64';
        end;
{$endif x86_64}
        Replace(result,'$FORMAT',FormatName);
      end;

{*****************************************************************************
                                  Initialize
*****************************************************************************}

{$ifdef i8086}
    const
        as_i8086_nasm_info : tasminfo =
          (
            id           : as_i8086_nasm;
            idtxt  : 'NASM';
            asmbin : 'nasm';
            asmcmd : '-f $FORMAT $DEBUG -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_i8086_msdos,system_i8086_win16,system_i8086_embedded];
            flags : [af_needar,af_no_debug];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );
        as_i8086_nasmobj_info : tasminfo =
          (
            id           : as_i8086_nasmobj;
            idtxt  : 'NASMOBJ';
            asmbin : 'nasm';
            asmcmd : '-f obj -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_i8086_msdos,system_i8086_win16,system_i8086_embedded];
            flags : [af_needar,af_no_debug];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );
{$endif i8086}
{$ifdef i386}
    const
        as_i386_nasmcoff_info : tasminfo =
          (
            id           : as_i386_nasmcoff;
            idtxt  : 'NASMCOFF';
            asmbin : 'nasm';
            asmcmd : '-f coff -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_i386_go32v2];
            flags : [af_needar,af_no_debug];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );

       as_i386_nasmwin32_info : tasminfo =
          (
            id           : as_i386_nasmwin32;
            idtxt  : 'NASMWIN32';
            asmbin : 'nasm';
            asmcmd : '-f win32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_i386_win32];
            flags : [af_needar,af_no_debug,af_smartlink_sections];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );

       as_i386_nasmobj_info : tasminfo =
          (
            id           : as_i386_nasmobj;
            idtxt  : 'NASMOBJ';
            asmbin : 'nasm';
            asmcmd : '-f obj -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_i386_embedded, system_i8086_msdos];
            flags : [af_needar,af_no_debug];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );

       as_i386_nasmwdosx_info : tasminfo =
          (
            id           : as_i386_nasmwdosx;
            idtxt  : 'NASMWDOSX';
            asmbin : 'nasm';
            asmcmd : '-f win32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_i386_wdosx];
            flags : [af_needar,af_no_debug];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );


       as_i386_nasmelf_info : tasminfo =
          (
            id           : as_i386_nasmelf;
            idtxt  : 'NASMELF';
            asmbin : 'nasm';
            asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_i386_linux];
            flags : [af_needar,af_no_debug];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );
{
       as_i386_nasmdarwin_info : tasminfo =
          (
            id           : as_i386_nasmdarwin;
            idtxt  : 'NASMDARWIN';
            asmbin : 'nasm';
            asmcmd : '-f macho32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_i386_darwin];
            flags : [af_needar,af_no_debug];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );
}
       as_i386_nasmbeos_info : tasminfo =
          (
            id           : as_i386_nasmbeos;
            idtxt  : 'NASMELF';
            asmbin : 'nasm';
            asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_i386_beos];
            flags : [af_needar,af_no_debug];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );

       as_i386_nasmhaiku_info : tasminfo =
          (
            id           : as_i386_nasmhaiku;
            idtxt  : 'NASMELF';
            asmbin : 'nasm';
            asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_i386_haiku];
            flags : [af_needar,af_no_debug];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );
       as_i386_nasm_info : tasminfo =
          (
            id           : as_i386_nasm;
            idtxt  : 'NASM';
            asmbin : 'nasm';
            asmcmd : '-f $FORMAT -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_any];
            flags : [af_needar,af_no_debug];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );

{$endif i386}
{$ifdef x86_64}
    const
       as_x86_64_nasm_info : tasminfo =
          (
            id           : as_x86_64_nasm;
            idtxt  : 'NASM';
            asmbin : 'nasm';
            asmcmd : '-f $FORMAT -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_any];
            flags : [af_needar{,af_no_debug}];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );

       as_x86_64_nasmwin64_info : tasminfo =
          (
            id           : as_x86_64_nasmwin64;
            idtxt  : 'NASMWIN64';
            asmbin : 'nasm';
            asmcmd : '-f win64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_x86_64_win64];
            flags : [af_needar,af_no_debug];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );

       as_x86_64_nasmelf_info : tasminfo =
          (
            id           : as_x86_64_nasmelf;
            idtxt  : 'NASMELF';
            asmbin : 'nasm';
            asmcmd : '-f elf64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_x86_64_linux];
            flags : [af_needar,af_no_debug];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );


       as_x86_64_nasmdarwin_info : tasminfo =
          (
            id           : as_x86_64_nasmdarwin;
            idtxt  : 'NASMDARWIN';
            asmbin : 'nasm';
            asmcmd : '-f macho64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
            supported_targets : [system_x86_64_darwin];
            flags : [af_needar,af_no_debug];
            labelprefix : '..@';
            labelmaxlen : -1;
            comment : '; ';
            dollarsign: '$';
          );

{$endif x86_64}


initialization
{$ifdef i8086}
  RegisterAssembler(as_i8086_nasm_info,TX86NasmAssembler);
  RegisterAssembler(as_i8086_nasmobj_info,TX86NasmAssembler);
{$endif i8086}
{$ifdef i386}
  RegisterAssembler(as_i386_nasmcoff_info,TX86NasmAssembler);
  RegisterAssembler(as_i386_nasmwin32_info,TX86NasmAssembler);
  RegisterAssembler(as_i386_nasmwdosx_info,TX86NasmAssembler);
  RegisterAssembler(as_i386_nasmobj_info,TX86NasmAssembler);
  RegisterAssembler(as_i386_nasmbeos_info,TX86NasmAssembler);
  RegisterAssembler(as_i386_nasmhaiku_info,TX86NasmAssembler);
  RegisterAssembler(as_i386_nasmelf_info,TX86NasmAssembler);
  RegisterAssembler(as_i386_nasm_info,TX86NasmAssembler);
{$endif i386}
{$ifdef x86_64}
  RegisterAssembler(as_x86_64_nasm_info,TX86NasmAssembler);
  RegisterAssembler(as_x86_64_nasmwin64_info,TX86NasmAssembler);
  RegisterAssembler(as_x86_64_nasmelf_info,TX86NasmAssembler);
  RegisterAssembler(as_x86_64_nasmdarwin_info,TX86NasmAssembler);
{$endif x86_64}
end.