summaryrefslogtreecommitdiff
path: root/compiler/sparc64/cpupara.pas
blob: fb7044472786e94a4e41d45fd2c8fd4d565eb92f (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
{
    Copyright (c) 1998-2002 by Florian Klaempfl

    Calling conventions for the SPARC64

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

{$i fpcdefs.inc}

interface

    uses
      globtype,
      cclasses,
      aasmtai,aasmdata,
      cpubase,cpuinfo,
      sppara,
      symconst,symbase,symsym,symtype,symdef,paramgr,parabase,cgbase,cgutils;

    type
      tcpuparamanager=class(TSparcParaManager)
        procedure create_paraloc_info_intern(p : tabstractprocdef; side : tcallercallee; paras : tparalist; var curintreg : LongInt;
          curfloatreg : tsuperregister; var cur_stack_offset : aword);override;
        function push_addr_param(varspez : tvarspez; def : tdef; calloption : tproccalloption) : boolean;override;
        function ret_in_param(def : tdef; pd : tabstractprocdef) : boolean;override;
        function get_funcretloc(p : tabstractprocdef; side : tcallercallee; forcetempdef : tdef) : tcgpara;override;
      private
        function push_addr_param_intern(varspez : tvarspez; def : tdef; calloption : tproccalloption; recsizelimit : aword) : boolean;
        procedure create_paraloc1_info_intern(p : tabstractprocdef; side : tcallercallee; paradef : tdef; var loc : TCGPara; varspez : tvarspez; varoptions : tvaroptions; recsizelimit : aword;
          var curintreg : LongInt; var curfloatreg : tsuperregister; var cur_stack_offset : aword);
      end;

implementation

    uses
      cutils,verbose,systems,
      defutil,
      cgobj;

    { true if a parameter is too large to copy and only the address is pushed }
    function tcpuparamanager.push_addr_param_intern(varspez:tvarspez;def : tdef;calloption : tproccalloption;recsizelimit : aword) : boolean;
      begin
        result:=false;
        { var,out,constref always require address }
        if varspez in [vs_var,vs_out,vs_constref] then
          begin
            result:=true;
            exit;
          end;
        case def.typ of
          arraydef:
            result:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
                             is_open_array(def) or
                             is_array_of_const(def) or
                             is_array_constructor(def);
          recorddef:
            result:=def.size>recsizelimit;
          variantdef:
            result:=false;
          formaldef :
            result:=true;
          objectdef :
            result:=(is_object(def) and (def.size>recsizelimit));
          stringdef :
            result:=(tstringdef(def).stringtype in [st_shortstring,st_longstring]);
          procvardef :
            result:=false;
          setdef :
            result:=not is_smallset(def);
          else
            ;
        end;
      end;


    { true if a parameter is too large to copy and only the address is pushed }
    function tcpuparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
      begin
        result:=push_addr_param_intern(varspez,def,calloption,16);
      end;


    function tcpuparamanager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
      begin
        if handle_common_ret_in_param(def,pd,result) then
          exit;

        case def.typ of
          { it is a matter of interpretation, if objects should be returned in registers according to the abi as the
            abi talks only about structures and unions

            at least for the compiler, it is a problem, if an object is returned in registers

            consider
            tobject1 = object
              function f : tobject1;
              ...
              contructor init;
            end;

            the constructor changes the size of tobject1, so its return location might change from register to memory, this
            is something the compiler could not handle currently, so we do not return objects in registers yet
          objectdef:
            begin
              result:=is_object(def) and (def.size>32);
              exit;
            end;}
          recorddef:
            begin
              result:=def.size>32;
              exit;
            end;
          else
            ;
        end;
        result:=inherited ret_in_param(def,pd);
      end;


    procedure tcpuparamanager.create_paraloc1_info_intern(
      p : tabstractprocdef; side: tcallercallee;paradef:tdef;var loc : TCGPara;varspez : tvarspez;varoptions : tvaroptions;recsizelimit : aword;
      var curintreg: LongInt; var curfloatreg: tsuperregister; var cur_stack_offset : aword);

      procedure nextloc(currsize : TCgSize);
        begin
          if curintreg>high(tparasupregs) then
            begin
              if (currsize<low(tcgsize2size)) or (currsize>high(tcgsize2size)) then
                internalerror(2017080101);
              { Parameters are aligned at 8 bytes }
              inc(cur_stack_offset,align(tcgsize2size[currsize],sizeof(pint)));
            end;
          inc(curintreg);
          if currsize=OS_F128 then
            inc(curfloatreg,4)
          else
            inc(curfloatreg,2);
        end;

      var
        paraloc      : pcgparalocation;
        paracgsize   : tcgsize;
        hparasupregs : pparasupregs;
        paralen      : longint;
      begin
        if side=callerside then
          hparasupregs:=@paraoutsupregs
        else
          hparasupregs:=@parainsupregs;
        { currently only support C-style array of const,
          there should be no location assigned to the vararg array itself }
        if (p.proccalloption in cstylearrayofconst) and
           is_array_of_const(paradef) then
          begin
            paraloc:=loc.add_location;
            { hack: the paraloc must be valid, but is not actually used }
            paraloc^.loc:=LOC_REGISTER;
            paraloc^.register:=NR_G0;
            paraloc^.size:=OS_ADDR;
            paraloc^.def:=voidpointertype;
            exit;
          end;

        if push_addr_param_intern(varspez,paradef,p.proccalloption,recsizelimit) then
          begin
            paracgsize:=OS_ADDR;
            paradef:=cpointerdef.getreusable_no_free(paradef);
          end
        else
          begin
            paracgsize:=def_cgsize(paradef);
            if paradef.typ=formaldef then
              begin
                paracgsize:=OS_ADDR;
                paradef:=voidpointertype;
              end;
          end;
        loc.reset;
        loc.size:=paracgsize;
        loc.def:=paradef;
        if side=callerside then
          loc.Alignment:=std_param_align
        else
          loc.Alignment:=paradef.alignment;
        { sparc64 returns records up to a size of 32 in register, we cannot encode this
          in paracgsize, so paracgsize is OS_NO in this case }
        if paracgsize=OS_NO then
          paralen:=paradef.size
        else
          paralen:=tcgsize2size[paracgsize];
        loc.intsize:=paralen;
        while paralen>0 do
          begin
            paraloc:=loc.add_location;
            paraloc^.size:=paracgsize;
            paraloc^.def:=paradef;

            { ret in param? }
            if vo_is_funcret in varoptions then
              begin
                paraloc^.loc:=LOC_REFERENCE;
                paraloc^.reference.offset:=128;
                if side=callerside then
                  paraloc^.reference.index:=NR_STACK_POINTER_REG
                else
                  paraloc^.reference.index:=NR_FRAME_POINTER_REG;
                inc(paraloc^.reference.offset,STACK_BIAS);
              end
            { In case of po_delphi_nested_cc, the parent frame pointer
              is always passed on the stack. }
            else if (curintreg<=high(tparasupregs)) and
               (not(vo_is_parentfp in varoptions) or
                not(po_delphi_nested_cc in p.procoptions)) then
              begin
                if paraloc^.size in [OS_F32,OS_F64,OS_F128] then
                  begin
                    paraloc^.loc:=LOC_FPUREGISTER;
                    case paraloc^.size of
                      OS_F32:
                        { singles are put into the uneven register }
                        paraloc^.register:=newreg(R_FPUREGISTER,curfloatreg+1,R_SUBFS);
                      OS_F64:
                        paraloc^.register:=newreg(R_FPUREGISTER,curfloatreg,R_SUBFD);
                      OS_F128:
                        paraloc^.register:=newreg(R_FPUREGISTER,curfloatreg,R_SUBFQ);
                      else
                        Internalerror(2017072301);
                    end;
                  end
                else
                  begin
                    if paracgsize in [OS_NO,OS_128,OS_S128] then
                      begin
                        if paralen>4 then
                          begin
                            paraloc^.size:=OS_INT;
                            paraloc^.def:=u64inttype;
                          end
                        else
                          begin
                            { for 3-byte records }
                            paraloc^.size:=OS_32;
                            paraloc^.def:=u32inttype;
                          end;
                      end;

                    paraloc^.loc:=LOC_REGISTER;
                    paraloc^.register:=newreg(R_INTREGISTER,hparasupregs^[curintreg],R_SUBWHOLE);
                    { left align }
                    if (target_info.endian=endian_big) and
                       not(paraloc^.size in [OS_64,OS_S64]) and
                       (paradef.typ in [setdef,recorddef,arraydef,objectdef]) then
                      begin
                        paraloc^.shiftval:=-(8-tcgsize2size[paraloc^.size])*8;
                        paraloc^.Size:=OS_64;
                      end;
                  end;
                nextloc(paraloc^.Size);
              end
            else
              begin
                paraloc^.loc:=LOC_REFERENCE;
                paraloc^.reference.offset:=target_info.first_parm_offset+cur_stack_offset;
                if side=callerside then
                  paraloc^.reference.index:=NR_STACK_POINTER_REG
                else
                  paraloc^.reference.index:=NR_FRAME_POINTER_REG;
                inc(paraloc^.reference.offset,STACK_BIAS);

                if (target_info.endian=endian_big) and
                   (paralen<tcgsize2size[OS_INT]) and
                   (paradef.typ<>recorddef) then
                  inc(paraloc^.reference.offset,4-paralen);

                { Parameters are aligned to 8 byte boundaries }
                inc(cur_stack_offset,align(paralen,8));

                { a stack location covers always the remainder of a parameter }
                exit;
              end;
            dec(paralen,tcgsize2size[paraloc^.size]);
          end;
      end;


    procedure tcpuparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
                                                  var curintreg: LongInt; curfloatreg: tsuperregister; var cur_stack_offset : aword);
      var
        i : integer;
      begin
        for i:=0 to paras.count-1 do
          create_paraloc1_info_intern(p,side,tparavarsym(paras[i]).vardef,tparavarsym(paras[i]).paraloc[side],tparavarsym(paras[i]).varspez,
            tparavarsym(paras[i]).varoptions,16,curintreg,curfloatreg,cur_stack_offset);
      end;


    function tcpuparamanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
      var
        paraloc : pcgparalocation;
        retcgsize  : tcgsize;
        curintreg : LongInt;
        curfloatreg : tsuperregister;
        cur_stack_offset : aword;
      begin
        if set_common_funcretloc_info(p,forcetempdef,retcgsize,result) then
          exit;

        if ret_in_param(result.def,p) then
          Internalerror(2017080601);

        if is_record(result.def) or is_object(result.def) then
          begin
            curintreg:=0;
            curfloatreg:=RS_F0;
            cur_stack_offset:=0;

            create_paraloc1_info_intern(p,side,result.def,result,vs_value,
              [],32,curintreg,curfloatreg,cur_stack_offset);

            { sparc64 calling conventions are difficult, so better check if everything is ok }
            if result.location^.loc=LOC_INVALID then
              Internalerror(2017080501);
          end
        else
          begin
            paraloc:=result.add_location;
            { Return in FPU register? }
            if result.def.typ=floatdef then
              begin
                paraloc^.loc:=LOC_FPUREGISTER;
                paraloc^.register:=NR_FPU_RESULT_REG;
                if retcgsize=OS_F64 then
                  setsubreg(paraloc^.register,R_SUBFD);
                paraloc^.size:=retcgsize;
                paraloc^.def:=result.def;
              end
            else
             { Return in register }
              begin
                paraloc^.loc:=LOC_REGISTER;
                paraloc^.size:=retcgsize;
                paraloc^.def:=result.def;
                if (side=callerside) then
                  paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,cgsize2subreg(R_INTREGISTER,retcgsize))
                else
                  paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(R_INTREGISTER,retcgsize));
              end;
          end;
      end;


begin
   ParaManager:=tcpuparamanager.create;
end.