summaryrefslogtreecommitdiff
path: root/avx512-0037785/compiler/avr/agavrgas.pas
blob: d4df03db2abd8bdc0e25e3d76c8897a6f702a280 (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
{
    Copyright (c) 2003 by Florian Klaempfl

    This unit implements an asm for the ARM

    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.

 ****************************************************************************
}
{ This unit implements the GNU Assembler writer for the ARM
}

unit agavrgas;

{$i fpcdefs.inc}

  interface

    uses
       globtype,systems,
       aasmtai,aasmdata,
       assemble,aggas,
       cpubase;

    type

      { TAVRGNUAssembler }

      TAVRGNUAssembler=class(TGNUassembler)
        constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
       function MakeCmdLine: TCmdStr; override;
      end;

     TAVRInstrWriter=class(TCPUInstrWriter)
        procedure WriteInstruction(hp : tai);override;
     end;


  implementation

    uses
       cutils,globals,verbose,
       aasmbase,aasmcpu,
       itcpugas,
       cpuinfo,
       cgbase,cgutils;

{****************************************************************************}
{                         GNU Arm Assembler writer                           }
{****************************************************************************}

    constructor TAVRGNUAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
      begin
        inherited;
        InstrWriter := TAVRInstrWriter.create(self);
      end;


{****************************************************************************}
{                  Helper routines for Instruction Writer                    }
{****************************************************************************}


    Procedure TAVRInstrWriter.WriteInstruction(hp : tai);

      var
        op: TAsmOp;

      function getreferencestring(var ref : treference) : string;
        var
          s : string;
        begin
           s:='';
           with ref do
            begin
  {$ifdef extdebug}
              // if base=NR_NO then
              //   internalerror(200308292);

              // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
              //   internalerror(200308293);
  {$endif extdebug}
              if index<>NR_NO then
                internalerror(2011021707)
              else if base<>NR_NO then
                begin
                  if addressmode=AM_PREDRECEMENT then
                    s:='-';

                  case base of
                    NR_R26:
                      s:=s+'X';
                    NR_R28:
                      s:=s+'Y';
                    NR_R30:
                      s:=s+'Z';
                    else
                      s:=s+gas_regname(base);
                  end;
                  if addressmode=AM_POSTINCREMENT then
                    s:=s+'+'
                  else if addressmode = AM_UNCHANGED then
                    begin
                      if (offset>0) or ((offset=0) and (op in [A_LDD,A_STD])) then
                        s:=s+'+'+tostr(offset)
                      else if offset<0 then
                        s:=s+tostr(offset);
                    end;
                end
              else if assigned(symbol) or (offset<>0) then
                begin
                  if assigned(symbol) then
                    s:=ApplyAsmSymbolRestrictions(symbol.name);

                  if s='' then
                    s:=tostr(offset)
                  else if offset<0 then
                    s:=s+tostr(offset)
                  else if offset>0 then
                    s:=s+'+'+tostr(offset);
                  case refaddr of
                    addr_hi8:
                      s:='hi8('+s+')';
                    addr_hi8_gs:
                      s:='hi8(gs('+s+'))';
                    addr_lo8:
                      s:='lo8('+s+')';
                    addr_lo8_gs:
                      s:='lo8(gs('+s+'))';
                    else
                      s:='('+s+')';
                  end;
                end
              { reference to address 0? }
              else if not(assigned(symbol)) and (offset=0) then
                s:='(0)';
            end;
          getreferencestring:=s;
        end;


      function getopstr(const o:toper) : string;
        var
          hs : string;
        begin
          case o.typ of
            top_reg:
              getopstr:=gas_regname(o.reg);
            top_const:
              getopstr:=tostr(longint(o.val));
            top_ref:
              if o.ref^.refaddr=addr_full then
                begin
                  hs:=ApplyAsmSymbolRestrictions(o.ref^.symbol.name);
                  if o.ref^.offset>0 then
                    hs:=hs+'+'+tostr(o.ref^.offset)
                  else if o.ref^.offset<0 then
                    hs:=hs+tostr(o.ref^.offset);
                  getopstr:=hs;
                end
              else
                getopstr:=getreferencestring(o.ref^);
            else
              internalerror(2002070604);
          end;
        end;

    var
      s: string;
      i: byte;
      sep: string[3];
    begin
      op:=taicpu(hp).opcode;
      s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition];
      if taicpu(hp).ops<>0 then
        begin
          sep:=#9;
          for i:=0 to taicpu(hp).ops-1 do
            begin
              s:=s+sep+getopstr(taicpu(hp).oper[i]^);
              sep:=',';
            end;
        end;
      owner.writer.AsmWriteLn(s);
    end;


    function TAVRGNUAssembler.MakeCmdLine: TCmdStr;
      begin
        result := '-mmcu='+lower(cputypestr[current_settings.cputype])+' '+inherited MakeCmdLine;
      end;


    const
       as_avr_gas_info : tasminfo =
          (
            id     : as_gas;

            idtxt  : 'AS';
            asmbin : 'as';
            asmcmd : '-o $OBJ $EXTRAOPT $ASM';
            supported_targets : [system_avr_embedded];
            flags : [af_needar,af_smartlink_sections];
            labelprefix : '.L';
            labelmaxlen : -1;
            comment : '# ';
            dollarsign: 's';
          );


begin
  RegisterAssembler(as_avr_gas_info,TAVRGNUAssembler);
end.