summaryrefslogtreecommitdiff
path: root/compiler/x86/rax86att.pas
blob: a81314a5f984ed5c9c2941d1a106515fa272397c (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
{
    Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman

    Does the parsing for the x86 GNU AS styled inline assembler.

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

{$i fpcdefs.inc}

Interface

  uses
    cpubase,
    raatt,rax86, rautils;

  type

    { tx86attreader }

    tx86attreader = class(tattreader)
      ActOpsize : topsize;
      function is_asmopcode(const s: string):boolean;override;
      procedure handleopcode;override;
      procedure BuildReference(oper : tx86operand);
      procedure BuildOperand(oper : tx86operand);
      procedure BuildOpCode(instr : tx86instruction);
      procedure handlepercent;override;
     protected
      procedure MaybeGetPICModifier(var oper: tx86operand);
    end;

    { Tx86attInstruction }

    Tx86attInstruction = class(Tx86Instruction)
      procedure AddReferenceSizes; override;
      procedure FixupOpcode;override;
    end;

Implementation

    uses
      { helpers }
      cutils,
      { global }
      globtype,verbose,
      systems,
      { aasm }
      aasmbase,aasmdata,aasmcpu,
      { symtable }
      symconst,symsym,symdef,
      { parser }
      scanner,
      procinfo,
      itcpugas,
      paramgr,
      cgbase
      ;

    { Tx86attInstruction }


    procedure Tx86attInstruction.AddReferenceSizes;
    var
      i: integer;
    begin
      if (Opsize <> S_NO) and
         (MemRefInfo(opcode).ExistsSSEAVX) and
         (MemRefInfo(opcode).MemRefSize in MemRefMultiples) then
      begin
        for i := 1 to ops do
        begin
          if operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL] then
          begin
            if (tx86operand(operands[i]).opsize = S_NO) then
             tx86operand(operands[i]).opsize := Opsize;
          end;
        end;
      end;

      inherited AddReferenceSizes;
    end;

    procedure Tx86attInstruction.FixupOpcode;
      begin
        case opcode of
          A_MOVQ:
            begin
              { May be either real 'movq' or a generic 'mov' with 'q' suffix. Convert to mov
                if source is a constant, or if neither operand is an mmx/xmm register }
{$ifdef x86_64}
              if (ops=2) and
                (
                  (operands[1].opr.typ=OPR_CONSTANT) or not
                    (
                      ((operands[1].opr.typ=OPR_REGISTER) and
                        (getregtype(operands[1].opr.reg) in [R_MMXREGISTER,R_MMREGISTER])) or
                      ((operands[2].opr.typ=OPR_REGISTER) and
                        (getregtype(operands[2].opr.reg) in [R_MMXREGISTER,R_MMREGISTER]))
                    )
                ) then
                opcode:=A_MOV;
{$endif x86_64}
            end;
          else
            ;
        end;
      end;

    { Tx86attReader }

    procedure tx86attreader.handlepercent;
      var
        len : longint;
      begin
        len:=1;
        actasmpattern[len]:='%';
        c:=current_scanner.asmgetchar;
        { to be a register there must be a letter and not a number }
        if c in ['0'..'9'] then
         begin
           actasmtoken:=AS_MOD;
         end
        else
         begin
           while c in ['a'..'z','A'..'Z','0'..'9'] do
            Begin
              inc(len);
              actasmpattern[len]:=c;
              c:=current_scanner.asmgetchar;
            end;
           actasmpattern[0]:=chr(len);
           uppervar(actasmpattern);
           if (actasmpattern = '%ST') and (c='(') then
            Begin
              actasmpattern:=actasmpattern+c;
              c:=current_scanner.asmgetchar;
              if c in ['0'..'9'] then
               actasmpattern:=actasmpattern + c
              else
               Message(asmr_e_invalid_fpu_register);
              c:=current_scanner.asmgetchar;
              if c <> ')' then
               Message(asmr_e_invalid_fpu_register)
              else
               Begin
                 actasmpattern:=actasmpattern + c;
                 c:=current_scanner.asmgetchar; { let us point to next character. }
               end;
            end;
           if is_register(actasmpattern) then
            exit;
           Message(asmr_e_invalid_register);
           actasmtoken:=raatt.AS_NONE;
         end;
      end;


    Procedure tx86attreader.BuildReference(oper : tx86operand);

      procedure Consume_RParen;
        begin
          if actasmtoken <> AS_RPAREN then
           Begin
             Message(asmr_e_invalid_reference_syntax);
             RecoverConsume(true);
           end
          else
           begin
             Consume(AS_RPAREN);
             if not (actasmtoken in [AS_COMMA,AS_SEPARATOR,AS_END]) then
              Begin
                Message(asmr_e_invalid_reference_syntax);
                RecoverConsume(true);
              end;
           end;
        end;


      procedure Consume_Scale;
        var
          l : aint;
        begin
          { we have to process the scaling }
          l:=BuildConstExpression(false,true);
          if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) then
           oper.opr.ref.scalefactor:=l
          else
           Begin
             Message(asmr_e_wrong_scale_factor);
             oper.opr.ref.scalefactor:=0;
           end;
        end;


      begin
        oper.InitRef;
        Consume(AS_LPAREN);
        Case actasmtoken of
          AS_INTNUM,
          AS_MINUS,
          AS_PLUS: { absolute offset, such as fs:(0x046c) }
            Begin
              { offset(offset) is invalid }
              If oper.opr.Ref.Offset <> 0 Then
               Begin
                 Message(asmr_e_invalid_reference_syntax);
                 RecoverConsume(true);
               End
              Else
               Begin
                 oper.opr.Ref.Offset:=BuildConstExpression(false,true);
                 Consume_RParen;
               end;
              exit;
            End;
          AS_REGISTER: { (reg ...  }
            Begin
              { Check if there is already a base (mostly ebp,esp) than this is
                not allowed, because it will give crashing code }
              if ((oper.opr.typ=OPR_REFERENCE) and (oper.opr.ref.base<>NR_NO)) or
                 ((oper.opr.typ=OPR_LOCAL) and (oper.opr.localsym.localloc.loc<>LOC_REGISTER)) then
                message(asmr_e_cannot_index_relative_var);
              oper.opr.ref.base:=actasmregister;
{$ifdef x86_64}
              { non-GOT based RIP-relative accesses are also position-independent }
              if (oper.opr.ref.base=NR_RIP) and
                 (oper.opr.ref.refaddr<>addr_pic) then
                oper.opr.ref.refaddr:=addr_pic_no_got;
{$endif x86_64}
              Consume(AS_REGISTER);
              { can either be a register or a right parenthesis }
              { (reg)        }
              if actasmtoken=AS_RPAREN then
               Begin
                 Consume_RParen;
                 exit;
               end;
              { (reg,reg ..  }
              Consume(AS_COMMA);
              if actasmtoken=AS_REGISTER then
               Begin
                 oper.opr.ref.index:=actasmregister;
                 Consume(AS_REGISTER);
                 { check for scaling ... }
                 case actasmtoken of
                   AS_RPAREN:
                     Begin
                       Consume_RParen;
                       exit;
                     end;
                   AS_COMMA:
                     Begin
                       Consume(AS_COMMA);
                       Consume_Scale;
                       Consume_RParen;
                     end;
                 else
                   Begin
                     Message(asmr_e_invalid_reference_syntax);
                     RecoverConsume(false);
                   end;
                 end; { end case }
               end
              else
               Begin
                 Message(asmr_e_invalid_reference_syntax);
                 RecoverConsume(false);
               end;
            end; {end case }
          AS_COMMA: { (, ...  can either be scaling, or index }
            Begin
              Consume(AS_COMMA);
              { Index }
              if (actasmtoken=AS_REGISTER) then
               Begin
                 oper.opr.ref.index:=actasmregister;
                 Consume(AS_REGISTER);
                 { check for scaling ... }
                 case actasmtoken of
                   AS_RPAREN:
                     Begin
                       Consume_RParen;
                       exit;
                     end;
                   AS_COMMA:
                     Begin
                       Consume(AS_COMMA);
                       Consume_Scale;
                       Consume_RParen;
                     end;
                 else
                   Begin
                     Message(asmr_e_invalid_reference_syntax);
                     RecoverConsume(false);
                   end;
                 end; {end case }
               end
              { Scaling }
              else
               Begin
                 Consume_Scale;
                 Consume_RParen;
                 exit;
               end;
            end;
        else
          Begin
            Message(asmr_e_invalid_reference_syntax);
            RecoverConsume(false);
          end;
        end;
      end;


    Procedure tx86attreader.MaybeGetPICModifier(var oper: tx86operand);
      var
        relsym: string;
        asmsymtyp: tasmsymtype;
        l: tcgint;
        sym: tasmsymbol;
      begin
        case actasmtoken of
          AS_AT:
            begin
              { darwin/i386 needs a relsym instead, and we can't }
              { generate this automatically                      }
              if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
                Message(asmr_e_invalid_reference_syntax);
              consume(AS_AT);
              if actasmtoken=AS_ID then
                begin
{$ifdef x86_64}
                  if (actasmpattern='GOTPCREL') or
		     (actasmpattern='PLT') then
{$endif x86_64}
{$ifdef i386}
                  if actasmpattern='GOT' then
{$endif i386}
{$ifdef i8086}
                  if actasmpattern='GOT' then
{$endif i8086}
                    begin
                      case oper.opr.typ of
                        OPR_SYMBOL:
                          begin
                            sym:=oper.opr.symbol;
                            if oper.opr.symofs<>0 then
                              Message(asmr_e_invalid_reference_syntax);
                            oper.opr.typ:=OPR_REFERENCE;
                            fillchar(oper.opr.ref,sizeof(oper.opr.ref),0);
                            oper.opr.ref.symbol:=sym;
                          end;
                        OPR_REFERENCE:
                          begin
                            { ok }
                          end;
                        else
                          Message(asmr_e_invalid_reference_syntax)
                      end;
                      oper.opr.ref.refaddr:=addr_pic;
                      consume(AS_ID);
                    end
                  else
                    Message(asmr_e_invalid_reference_syntax);
                end
              else
                Message(asmr_e_invalid_reference_syntax);
            end;
          AS_MINUS:
            begin
              { relsym? }
              Consume(AS_MINUS);
              BuildConstSymbolExpression(true,true,false,l,relsym,asmsymtyp);
              if (relsym<>'') then
                if not assigned(oper.opr.ref.relsymbol) then
                  oper.opr.ref.relsymbol:=current_asmdata.RefAsmSymbol(relsym,asmsymtyp)
                else
                  Message(asmr_e_invalid_reference_syntax)
              else
                dec(oper.opr.ref.offset,l);
            end;
          else
            ;
        end;
      end;


    Procedure tx86attreader.BuildOperand(oper : tx86operand);
      var
        tempstr,
        expr : string;
        typesize,l,k : tcgint;


        procedure AddLabelOperand(hl:tasmlabel);
          begin
            if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
               is_calljmp(actopcode) then
             begin
               oper.opr.typ:=OPR_SYMBOL;
               oper.opr.symbol:=hl;
             end
            else
             begin
               oper.InitRef;
               oper.opr.ref.symbol:=hl;
             end;
          end;


        procedure MaybeRecordOffset;
          var
            mangledname: string;
            hasdot  : boolean;
            l,
            toffset,
            tsize   : tcgint;
          begin
            if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
             exit;
            l:=0;
            mangledname:='';
            hasdot:=(actasmtoken=AS_DOT);
            if hasdot then
             begin
               if expr<>'' then
                 begin
                   BuildRecordOffsetSize(expr,toffset,tsize,mangledname,false);
                   if (oper.opr.typ<>OPR_CONSTANT) and
                      (mangledname<>'') then
                    Message(asmr_e_wrong_sym_type);
                   inc(l,toffset);
                   oper.SetSize(tsize,true);

                   case oper.opr.typ of
                     OPR_REFERENCE: oper.opr.varsize := tsize;
                         OPR_LOCAL: oper.opr.localvarsize := tsize;
                     else
                       ;
                   end;

                 end;
             end;
            if actasmtoken in [AS_PLUS,AS_MINUS] then
             inc(l,BuildConstExpression(true,false));
            case oper.opr.typ of
              OPR_LOCAL :
                begin
                  { don't allow direct access to fields of parameters, because that
                    will generate buggy code. Allow it only for explicit typecasting }
                  if hasdot and
                     (not oper.hastype) then
                    checklocalsubscript(oper.opr.localsym);
                  inc(oper.opr.localsymofs,l);
                  inc(oper.opr.localconstoffset,l);
                end;
              OPR_CONSTANT :
                if (mangledname<>'') then
                  begin
                    if (oper.opr.val<>0) then
                      Message(asmr_e_wrong_sym_type);
                    oper.opr.typ:=OPR_SYMBOL;
                    oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname,AT_FUNCTION);
                  end
                else
                  inc(oper.opr.val,l);
              OPR_REFERENCE :
                begin
                  inc(oper.opr.ref.offset,l);
                  inc(oper.opr.constoffset,l);
                end;
              OPR_SYMBOL:
                Message(asmr_e_invalid_symbol_ref);
              else
                internalerror(200309221);
            end;
          end;


        function MaybeBuildReference:boolean;
          { Try to create a reference, if not a reference is found then false
            is returned }
          var
            mangledname: string;
          begin
            MaybeBuildReference:=true;
            case actasmtoken of
              AS_INTNUM:
                Begin
                  { allow %segmentregister:number }
                  if oper.opr.ref.segment<>NR_NO then
                    begin
                      // already done before calling oper.InitRef;
                      if oper.opr.Ref.Offset <> 0 Then
                        Message(asmr_e_invalid_reference_syntax)
                      else
                        begin
                          oper.opr.Ref.Offset:=BuildConstExpression(true,false);
                          if actasmtoken=AS_LPAREN then
                            BuildReference(oper)
                          else if (oper.opr.ref.segment <> NR_FS) and
                            (oper.opr.ref.segment <> NR_GS) then
                           Message(asmr_w_general_segment_with_constant);
                        end;
                    end
                  else
                    begin
                      oper.opr.ref.offset:=BuildConstExpression(True,False);
                      BuildReference(oper);
                    end;
                end;
              AS_MINUS,
              AS_PLUS:
                Begin
                  oper.opr.ref.offset:=BuildConstExpression(True,False);
                  if actasmtoken<>AS_LPAREN then
                    Message(asmr_e_invalid_reference_syntax)
                  else
                    BuildReference(oper);
                end;
              AS_LPAREN:
                BuildReference(oper);
              AS_ID: { only a variable is allowed ... }
                Begin
                  tempstr:=actasmpattern;
                  Consume(AS_ID);
                  { typecasting? }
                  if (actasmtoken=AS_LPAREN) and
                     SearchType(tempstr,typesize) then
                   begin
                     oper.hastype:=true;
                     Consume(AS_LPAREN);
                     BuildOperand(oper);
                     Consume(AS_RPAREN);
                     if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
                       oper.SetSize(typesize,true);
                   end
                  else
                   if not oper.SetupVar(tempstr,false) then
                    Message1(sym_e_unknown_id,tempstr);
                  { record.field ? }
                  if actasmtoken=AS_DOT then
                   begin
                     BuildRecordOffsetSize(tempstr,l,k,mangledname,false);
                     if (mangledname<>'') then
                       Message(asmr_e_invalid_reference_syntax);
                     inc(oper.opr.ref.offset,l);

                     case oper.opr.typ of
                       OPR_REFERENCE: oper.opr.varsize := k;
                           OPR_LOCAL: oper.opr.localvarsize := k;
                       else
                         ;
                     end;
                   end;
                  MaybeGetPICModifier(oper);
                  case actasmtoken of
                    AS_END,
                    AS_SEPARATOR,
                    AS_COMMA: ;
                    AS_LPAREN:
                      BuildReference(oper);
                  else
                    Begin
                      Message(asmr_e_invalid_reference_syntax);
                      Consume(actasmtoken);
                    end;
                  end; {end case }
                end;
              else
               MaybeBuildReference:=false;
            end; { end case }
          end;

      var
        tempreg : tregister;
        hl      : tasmlabel;
      Begin
        expr:='';
        case actasmtoken of
          AS_LPAREN: { Memory reference or constant expression }
            Begin
              oper.InitRef;
              BuildReference(oper);
            end;

          AS_DOLLAR: { Constant expression  }
            Begin
              Consume(AS_DOLLAR);
              BuildConstantOperand(oper);
            end;

          AS_INTNUM,
          AS_MINUS,
          AS_PLUS:
            Begin
              { Constant memory offset }
              { This must absolutely be followed by (  }
              oper.InitRef;
              oper.opr.ref.offset:=asizeint(BuildConstExpression(True,False));
              if actasmtoken<>AS_LPAREN then
                Message(asmr_e_invalid_reference_syntax)
              else
                BuildReference(oper);
            end;

          AS_STAR: { Call from memory address }
            Begin
              Consume(AS_STAR);
              if actasmtoken=AS_REGISTER then
               begin
                 oper.opr.typ:=OPR_REGISTER;
                 oper.opr.reg:=actasmregister;
                 oper.SetSize(tcgsize2size[reg_cgsize(actasmregister)],true);
                 Consume(AS_REGISTER);
               end
              else
               begin
                 oper.InitRef;
                 if not MaybeBuildReference then
                  Message(asmr_e_syn_operand);
               end;
              { this is only allowed for call's and jmp's }
              if not is_calljmp(actopcode) then
               Message(asmr_e_syn_operand);
            end;

          AS_ID: { A constant expression, or a Variable ref.  }
            Begin
              { Local Label ? }
              if is_locallabel(actasmpattern) then
               begin
                 CreateLocalLabel(actasmpattern,hl,false);
                 Consume(AS_ID);
                 AddLabelOperand(hl);
                 MaybeGetPICModifier(oper);
               end
              else
               { Check for label }
               if SearchLabel(actasmpattern,hl,false) then
                begin
                  Consume(AS_ID);
                  AddLabelOperand(hl);
                  MaybeGetPICModifier(oper);
                end
              else
               { probably a variable or normal expression }
               { or a procedure (such as in CALL ID)      }
               Begin
                 { is it a constant ? }
                 if SearchIConstant(actasmpattern,l) then
                  Begin
                    if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
                     Message(asmr_e_invalid_operand_type);
                    BuildConstantOperand(oper);
                  end
                 else
                  begin
                    expr:=actasmpattern;
                    Consume(AS_ID);
                    { typecasting? }
                    if (actasmtoken=AS_LPAREN) and
                       SearchType(expr,typesize) then
                     begin
                       oper.hastype:=true;
                       Consume(AS_LPAREN);
                       BuildOperand(oper);
                       Consume(AS_RPAREN);
                       if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
                         oper.SetSize(typesize,true);
                     end
                    else
                     begin
                       if oper.SetupVar(expr,false) then
                         MaybeGetPICModifier(oper)
                       else
                        Begin
                          { look for special symbols ... }
                          if expr= '__HIGH' then
                            begin
                              consume(AS_LPAREN);
                              if not oper.setupvar('high'+actasmpattern,false) then
                                Message1(sym_e_unknown_id,'high'+actasmpattern);
                              consume(AS_ID);
                              consume(AS_RPAREN);
                            end
                          else
                           if expr = '__RESULT' then
                            oper.SetUpResult
                          else
                           if expr = '__SELF' then
                            oper.SetupSelf
                          else
                           if expr = '__OLDEBP' then
                            oper.SetupOldEBP
                          else
                            Message1(sym_e_unknown_id,expr);
                        end;
                     end;
                  end;
                  if oper.opr.typ<>OPR_NONE Then
                    begin
                      if (actasmtoken=AS_DOT) then
                        MaybeRecordOffset;
                      { add a constant expression? }
                      if (actasmtoken=AS_PLUS) then
                        begin
                          l:=BuildConstExpression(true,false);
                          if errorcount=0 then
                            case oper.opr.typ of
                              OPR_CONSTANT :
                                inc(oper.opr.val,l);
                              OPR_LOCAL :
                                begin
                                  inc(oper.opr.localsymofs,l);
                                  inc(oper.opr.localconstoffset, l);
                                end;
                              OPR_REFERENCE :
                                begin
                                  inc(oper.opr.ref.offset,l);
                                  inc(oper.opr.constoffset, l);
                                end;
                              else
                                internalerror(2003092011);
                            end;
                        end;
                    end;
               end;
              { Do we have a indexing reference, then parse it also }
              if actasmtoken=AS_LPAREN then
                BuildReference(oper);
            end;

          AS_REGISTER: { Register, a variable reference or a constant reference  }
            Begin
              { save the type of register used. }
              tempreg:=actasmregister;
              Consume(AS_REGISTER);
              if actasmtoken = AS_COLON then
               Begin
                 Consume(AS_COLON);
                 oper.InitRef;
                 if not is_segment_reg(tempreg) then
                   Message(asmr_e_invalid_seg_override);
{$ifdef x86_64}
                 if (tempreg=NR_CS) or (tempreg=NR_DS) or (tempreg=NR_SS) or (tempreg=NR_ES) then
                   Message1(asmr_w_segment_override_ignored_in_64bit_mode,gas_regname(tempreg));
{$endif x86_64}
                 oper.opr.ref.segment:=tempreg;
                 { This must absolutely be followed by a reference }
                 if not MaybeBuildReference then
                  Begin
                    Message(asmr_e_invalid_seg_override);
                    Consume(actasmtoken);
                  end;
               end
              { Simple register  }
              else if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
                Begin
                  if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
                    Message(asmr_e_invalid_operand_type);
                  oper.opr.typ:=OPR_REGISTER;
                  oper.opr.reg:=tempreg;
                  oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
                end
              else
                Message(asmr_e_syn_operand);
            end;
          AS_END,
          AS_SEPARATOR,
          AS_COMMA: ;
        else
          Begin
            Message(asmr_e_syn_operand);
            Consume(actasmtoken);
          end;
        end; { end case }
      end;


    procedure tx86attreader.BuildOpCode(instr : tx86instruction);
      var
        operandnum : longint;
        PrefixOp,OverrideOp: tasmop;
        di_param, si_param: ShortInt;
      Begin
        PrefixOp:=A_None;
        OverrideOp:=A_None;
        { prefix seg opcode / prefix opcode }
        repeat
          if is_prefix(actopcode) then
            begin
              PrefixOp:=ActOpcode;
              with instr do
                begin
                  opcode:=ActOpcode;
                  condition:=ActCondition;
                  opsize:=ActOpsize;
                  ConcatInstruction(curlist);
                end;
              Consume(AS_OPCODE);
            end
          else
            if is_override(actopcode) then
              begin
                OverrideOp:=ActOpcode;
                with instr do
                  begin
                    opcode:=ActOpcode;
                    condition:=ActCondition;
                    opsize:=ActOpsize;
                    ConcatInstruction(curlist);
                  end;
                Consume(AS_OPCODE);
              end
            else
              break;
          { allow for newline as in gas styled syntax }
          while actasmtoken=AS_SEPARATOR do
            Consume(AS_SEPARATOR);
        until (actasmtoken<>AS_OPCODE);
        { opcode }
        if (actasmtoken<>AS_OPCODE) then
         Begin
           Message(asmr_e_invalid_or_missing_opcode);
           RecoverConsume(true);
           exit;
         end;
        { Fill the instr object with the current state }
        with instr do
          begin
            Opcode:=ActOpcode;
            condition:=ActCondition;
            opsize:=ActOpsize;
          end;

        { Valid combination of prefix/override and instruction ?  }

        if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
           Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);

        if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
          Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
        { We are reading operands, so opcode will be an AS_ID }
        operandnum:=1;
        Consume(AS_OPCODE);
        { Zero operand opcode ?  }
        if actasmtoken in [AS_SEPARATOR,AS_END] then
         begin
           operandnum:=0;
           exit;
         end;
        { Read the operands }
        repeat
          case actasmtoken of
            AS_COMMA: { Operand delimiter }
              Begin
                if operandnum > Max_Operands then
                  Message(asmr_e_too_many_operands)
                else
                  Inc(operandnum);
                Consume(AS_COMMA);
              end;
            AS_SEPARATOR,
            AS_END : { End of asm operands for this opcode  }
              begin
                break;
              end;
          else
            BuildOperand(instr.Operands[operandnum] as tx86operand);
          end; { end case }
        until false;
        instr.Ops:=operandnum;
        { handle string instructions with parameters }
        with instr do
          if is_x86_parameterless_string_op(opcode) and
             (Ops>=1) and (Ops<=2) then
            begin
              if opcode=A_MOVSD then
                begin
                  { distinguish between MOVS and the SSE MOVSD instruction:
                    MOVS must have memory 2 reference operands (there's no need
                    to distinguish from SSE CMPSD, because the SSE version has 3
                    arguments and we've already checked that above) }
                  if (Ops=2) and (operands[1].opr.typ=OPR_REFERENCE) and (operands[2].opr.typ=OPR_REFERENCE) then
                    begin
                      opcode:=A_MOVS;
                      opsize:=S_L;
                    end;
                end
              else
                begin
                  opsize:=get_x86_string_op_size(opcode);
                  opcode:=x86_param2paramless_string_op(opcode);
                end;
            end;
        { Check for invalid ES: overrides }
        if is_x86_parameterized_string_op(instr.opcode) then
          begin
            si_param:=get_x86_string_op_si_param(instr.opcode);
            if si_param<>-1 then
              begin
                si_param:=x86_parameterized_string_op_param_count(instr.opcode)-si_param;
                if si_param<=operandnum then
                  with instr.operands[si_param] do
                    if (opr.typ=OPR_REFERENCE) then
                      begin
                        if not((((opr.ref.index<>NR_NO) and
                                 (opr.ref.base=NR_NO) and
                                 (getregtype(opr.ref.index)=R_INTREGISTER) and
                                 (getsupreg(opr.ref.index)=RS_ESI)) or
                                ((opr.ref.index=NR_NO) and
                                 (opr.ref.base<>NR_NO) and
                                 (getregtype(opr.ref.base)=R_INTREGISTER) and
                                 (getsupreg(opr.ref.base)=RS_ESI))) and
                               (opr.ref.offset=0) and
                               (opr.ref.scalefactor<=1) and
                               (opr.ref.refaddr=addr_no) and
                               (opr.ref.symbol=nil) and
                               (opr.ref.relsymbol=nil)) then
{$if defined(i8086)}
                          Message1(asmr_w_invalid_reference,'(%si)');
{$elseif defined(i386)}
                          Message1(asmr_w_invalid_reference,'(%esi)');
{$elseif defined(x86_64)}
                          Message1(asmr_w_invalid_reference,'(%rsi)');
{$endif}
                      end;
              end;
            di_param:=get_x86_string_op_di_param(instr.opcode);
            if di_param<>-1 then
              begin
                di_param:=x86_parameterized_string_op_param_count(instr.opcode)-di_param;
                if di_param<=operandnum then
                  with instr.operands[di_param] do
                    if (opr.typ=OPR_REFERENCE) then
                      begin
                        if (opr.ref.segment<>NR_NO) and
                           (opr.ref.segment<>NR_ES) then
                          Message(asmr_e_cannot_override_es_segment);
                        if not((((opr.ref.index<>NR_NO) and
                                 (opr.ref.base=NR_NO) and
                                 (getregtype(opr.ref.index)=R_INTREGISTER) and
                                 (getsupreg(opr.ref.index)=RS_EDI)) or
                                ((opr.ref.index=NR_NO) and
                                 (opr.ref.base<>NR_NO) and
                                 (getregtype(opr.ref.base)=R_INTREGISTER) and
                                 (getsupreg(opr.ref.base)=RS_EDI))) and
                               (opr.ref.offset=0) and
                               (opr.ref.scalefactor<=1) and
                               (opr.ref.refaddr=addr_no) and
                               (opr.ref.symbol=nil) and
                               (opr.ref.relsymbol=nil)) then
{$if defined(i8086)}
                          Message1(asmr_w_invalid_reference,'(%di)');
{$elseif defined(i386)}
                          Message1(asmr_w_invalid_reference,'(%edi)');
{$elseif defined(x86_64)}
                          Message1(asmr_w_invalid_reference,'(%rdi)');
{$endif}
                      end;
              end;
            { if two memory parameters, check whether their address sizes are equal }
            if (si_param<>-1) and (di_param<>-1) and
               (si_param<=operandnum) and (di_param<=operandnum) and
               (instr.operands[si_param].opr.typ=OPR_REFERENCE) and
               (instr.operands[di_param].opr.typ=OPR_REFERENCE) then
              begin
                if get_ref_address_size(instr.operands[si_param].opr.ref)<>
                   get_ref_address_size(instr.operands[di_param].opr.ref) then
                  Message(asmr_e_address_sizes_do_not_match);
              end;
          end;
      end;


    function tx86attreader.is_asmopcode(const s: string):boolean;
      var
        cond : string[4];
        cnd  : tasmcond;
        len,
        j,
        sufidx,
        suflen : longint;
      Begin
        is_asmopcode:=FALSE;

        actopcode:=A_None;
        actcondition:=C_None;
        actopsize:=S_NO;

        { search for all possible suffixes }
        for sufidx:=low(att_sizesuffixstr) to high(att_sizesuffixstr) do
         begin
           suflen:=length(att_sizesuffixstr[sufidx]);
           len:=length(s)-suflen;
           if copy(s,len+1,suflen)=att_sizesuffixstr[sufidx] then
            begin
              { Search opcodes }
              if len>0 then
                begin
                  actopcode:=tasmop(PtrUInt(iasmops.Find(copy(s,1,len))));

                  { movsd needs special handling because it has two namings in at&t syntax (movsl for string handling and
                    movsd for the sse instruction) while only one in intel syntax (movsd, both string and sse)
                    this cannot be expressed by the instruction table format so we have to hack around this here }
                  if (actopcode = A_NONE) and (upper(s) = 'MOVSD') then
                    actopcode := A_MOVSD;
                  { cmpsd also needs special handling for pretty much the same reasons as movsd }
                  if (actopcode = A_NONE) and (upper(s) = 'CMPSD') then
                    actopcode := A_CMPSD;
                  { disambiguation between A_MOVS (movsb/movsw/movsl/movsq) and
                    A_MOVSX (movsbw/movsbl/movswl/movsbq/movswq) }
                  if (actopcode = A_MOVS) and (suflen=2) then
                    actopcode := A_MOVSX;

                  { two-letter suffix is allowed by just a few instructions (movsx,movzx),
                    and it is always required whenever allowed }
                  if (gas_needsuffix[actopcode]=attsufINTdual) xor (suflen=2) then
                    continue;

                  if actopcode<>A_NONE then
                    begin
                      if gas_needsuffix[actopcode]=attsufFPU then
                       actopsize:=att_sizefpusuffix[sufidx]
                      else if gas_needsuffix[actopcode]=attsufFPUint then
                       actopsize:=att_sizefpuintsuffix[sufidx]
                      else if gas_needsuffix[actopcode]=attsufMM then
                       actopsize:=att_sizemmsuffix[sufidx]
                      else if gas_needsuffix[actopcode]=attsufMMX then
                       actopsize:=att_sizemmXsuffix[sufidx]
                      else
                       actopsize:=att_sizesuffix[sufidx];
                      { only accept suffix from the same category that the opcode belongs to }
                      if (actopsize<>S_NO) or (suflen=0) then
                        begin
                          actasmtoken:=AS_OPCODE;
                          is_asmopcode:=TRUE;
                          exit;
                        end;
                    end;
                end;
              { not found, check condition opcodes }
              j:=0;
              while (j<CondAsmOps) do
               begin
                 if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
                  begin
                    cond:=Copy(s,Length(CondAsmOpStr[j])+1,len-Length(CondAsmOpStr[j]));
                    if cond<>'' then
                     begin
                       for cnd:=low(TasmCond) to high(TasmCond) do
                        if Cond=Upper(cond2str[cnd]) then
                         begin
                           actopcode:=CondASmOp[j];
                           { conditional instructions (cmovcc, setcc) use only INT suffixes;
                             other stuff like fcmovcc is represented as group of individual instructions }
                           if gas_needsuffix[actopcode]=attsufINT then
                            actopsize:=att_sizesuffix[sufidx];
                           { only accept suffix from the same category that the opcode belongs to }
                           if (actopsize<>S_NO) or (suflen=0) then
                             begin
                               actcondition:=cnd;
                               actasmtoken:=AS_OPCODE;
                               is_asmopcode:=TRUE;
                               exit;
                             end;
                         end;
                     end;
                  end;
                 inc(j);
               end;
           end;
         end;
      end;


    procedure tx86attreader.handleopcode;
      var
        instr : Tx86Instruction;
      begin
        instr:=Tx86attInstruction.Create(Tx86Operand);
        BuildOpcode(instr);
        instr.AddReferenceSizes;
        instr.SetInstructionOpsize;
        instr.CheckOperandSizes;
        instr.FixupOpcode;
        instr.ConcatInstruction(curlist);
        instr.Free;
      end;



end.