summaryrefslogtreecommitdiff
path: root/compiler/arm/hlcgcpu.pas
blob: 81b791e047814162ca265bd0f134bc2da7445034 (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
{
    Copyright (c) 1998-2010 by Florian Klaempfl and Jonas Maebe
    Member of the Free Pascal development team

    This unit contains routines to create a pass-through high-level code
    generator. This is used by most regular code generators.

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

{$i fpcdefs.inc}

interface

  uses
    globtype,
    aasmdata,
    symdef,
    hlcg2ll;

  type
    tbasehlcgarm = class(thlcg2ll)
      procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
    end;

    tarmhlcgcpu = class(tbasehlcgarm)
    end;

    tthumbhlcgcpu = class(tbasehlcgarm)
      procedure a_jmp_external_name(list: TAsmList; const externalname: TSymStr); override;
    end;

implementation

  uses
    globals,verbose,
    procinfo,fmodule,
    symconst,
    aasmbase,aasmtai,aasmcpu, cpuinfo,
    hlcgobj,
    cgbase, cgutils, cpubase, cgobj, cgcpu;

  procedure tbasehlcgarm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);

    procedure loadvmttor12;
      var
        tmpref,
        href : treference;
        l : TAsmLabel;
      begin
        reference_reset_base(href,voidpointertype,NR_R0,0,ctempposinvalid,sizeof(pint),[]);
        if GenerateThumbCode then
          begin
            if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
              begin
                list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
                cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
                list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
                list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
              end
            else
              begin
                list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
                { create consts entry }
                reference_reset(tmpref,4,[]);
                current_asmdata.getjumplabel(l);
                current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
                cg.a_label(current_procinfo.aktlocaldata,l);
                tmpref.symboldata:=current_procinfo.aktlocaldata.last;
                current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
                tmpref.symbol:=l;
                tmpref.base:=NR_PC;
                list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
                href.offset:=0;
                href.index:=NR_R1;
                cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
                list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
                list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
              end;
          end
        else
          cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
      end;


    procedure op_onr12methodaddr;
      var
        tmpref,
        href : treference;
        l : TAsmLabel;
      begin
        if (procdef.extnumber=$ffff) then
          Internalerror(2000061311);
        if GenerateThumbCode then
          begin
            reference_reset_base(href,voidpointertype,NR_R0,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),ctempposinvalid,sizeof(pint),[]);
            if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
              begin
                list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
                list.concat(taicpu.op_reg_reg(A_MOV,NR_R0,NR_R12));
                cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
                list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
                list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
              end
            else
              begin
                list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
                { create consts entry }
                reference_reset(tmpref,4,[]);
                current_asmdata.getjumplabel(l);
                current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
                cg.a_label(current_procinfo.aktlocaldata,l);
                tmpref.symboldata:=current_procinfo.aktlocaldata.last;
                current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
                tmpref.symbol:=l;
                tmpref.base:=NR_PC;
                list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
                list.concat(taicpu.op_reg_reg(A_MOV,NR_R0,NR_R12));
                href.offset:=0;
                href.base:=NR_R0;
                href.index:=NR_R1;
                cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
                list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
                list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
              end;
          end
        else
          begin
            reference_reset_base(href,voidpointertype,NR_R12,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),ctempposinvalid,sizeof(pint),[]);
            cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
          end;
        if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
          list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12))
        else
          list.concat(taicpu.op_reg(A_BX,NR_R12));
      end;

    var
      make_global : boolean;
      tmpref : treference;
      l : TAsmLabel;
    begin
      if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
        Internalerror(200006137);
      if not assigned(procdef.struct) or
         (procdef.procoptions*[po_classmethod, po_staticmethod,
           po_methodpointer, po_interrupt, po_iocheck]<>[]) then
        Internalerror(200006138);
      if procdef.owner.symtabletype<>ObjectSymtable then
        Internalerror(200109191);

        if GenerateThumbCode or GenerateThumb2Code then
          list.concat(tai_directive.Create(asd_thumb_func,''));

      make_global:=false;
      if (not current_module.is_unit) or
         create_smartlink or
         (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
        make_global:=true;

      if make_global then
        list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0,procdef))
      else
        list.concat(Tai_symbol.Createname_hidden(labelname,AT_FUNCTION,0,procdef));

      { the wrapper might need aktlocaldata for the additional data to
        load the constant }
      current_procinfo:=cprocinfo.create(nil);

      { set param1 interface to self  }
      g_adjust_self_value(list,procdef,ioffset);

      { case 4 }
      if (po_virtualmethod in procdef.procoptions) and
          not is_objectpascal_helper(procdef.struct) then
        begin
          loadvmttor12;
          op_onr12methodaddr;
        end
      { case 0 }
      else if GenerateThumbCode then
        begin
          { bl cannot be used here because it destroys lr }

          list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));

          { create consts entry }
          reference_reset(tmpref,4,[]);
          current_asmdata.getjumplabel(l);
          current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
          cg.a_label(current_procinfo.aktlocaldata,l);
          tmpref.symboldata:=current_procinfo.aktlocaldata.last;
          current_procinfo.aktlocaldata.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(procdef.mangledname,AT_FUNCTION)));

          tmpref.symbol:=l;
          tmpref.base:=NR_PC;
          cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,NR_R0);
          list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
          list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
          list.concat(taicpu.op_reg(A_BX,NR_R12));
        end
      else
        list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname,AT_FUNCTION)));
      list.concatlist(current_procinfo.aktlocaldata);

      current_procinfo.Free;
      current_procinfo:=nil;

      list.concat(Tai_symbol_end.Createname(labelname));
    end;


  { tthumbhlcgcpu }

  procedure tthumbhlcgcpu.a_jmp_external_name(list: TAsmList; const externalname: TSymStr);
    var
      tmpref : treference;
      l : tasmlabel;
    begin
      { there is no branch instruction on thumb which allows big distances and which leaves LR as it is
        and which allows to switch the instruction set }

      { create const entry }
      reference_reset(tmpref,4,[]);
      current_asmdata.getjumplabel(l);
      tmpref.symbol:=l;
      tmpref.base:=NR_PC;
      list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
      list.concat(taicpu.op_reg_ref(A_LDR,NR_R0,tmpref));
      list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
      list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
      list.concat(taicpu.op_reg(A_BX,NR_R12));

      { append const entry }
      list.Concat(tai_align.Create(4));
      list.Concat(tai_label.create(l));
      list.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(externalname,AT_FUNCTION)));
    end;



  procedure create_hlcodegen_cpu;
    begin
      if GenerateThumbCode then
        hlcg:=tthumbhlcgcpu.create
      else
        hlcg:=tarmhlcgcpu.create;
      create_codegen;
    end;

begin
  chlcgobj:=tbasehlcgarm;
  create_hlcodegen:=@create_hlcodegen_cpu;
end.