summaryrefslogtreecommitdiff
path: root/avx512-0037785/compiler/llvm/llvmpara.pas
blob: 930d003630aca80e23eae228752867f87a4608b0 (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
{
    Copyright (c) 2013 by Jonas Maebe

    Includes the llvm parameter manager

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

{$i fpcdefs.inc}

  interface

    uses
      globtype,aasmdata,
      symconst,symtype,symdef,symsym,
      parabase,cgbase,
      cpupara;

    type
      { LLVM stands for "low level code generator", and regarding parameter
        handling it is indeed very low level. We are responsible for decomposing
        aggregate parameters into multiple simple parameters in case they have
        to be passed in special registers (such as floating point or SSE), and
        also for indicating whether e.g. 8 bit parameters need to be sign or
        zero exntended. This corresponds to pretty much what we do when creating
        parameter locations, so we reuse the original parameter manager and then
        process its output.

        The future will tell whether we can do this without
        architecture-specific code, or whether we will have to integrate parts
        into the various tcpuparamanager classes }
      tllvmparamanager = class(tcpuparamanager)
        procedure getcgtempparaloc(list: TAsmList; pd: tabstractprocdef; nr: longint; var cgpara: tcgpara); override;
        function param_use_paraloc(const cgpara: tcgpara): boolean; override;
        procedure createtempparaloc(list: TAsmList; calloption: tproccalloption; parasym: tparavarsym; can_use_final_stack_loc: boolean; var cgpara: TCGPara); override;
        function create_paraloc_info(p: tabstractprocdef; side: tcallercallee): longint; override;
        function create_varargs_paraloc_info(p: tabstractprocdef; side: tcallercallee; varargspara: tvarargsparalist): longint; override;
        function get_funcretloc(p: tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara; override;
        function has_strict_proc_signature: boolean; override;
       private
        procedure create_paraloc_info_internllvm(p: tabstractprocdef; side: tcallercallee);
        procedure set_llvm_paraloc_name(p: tabstractprocdef; hp: tparavarsym; var para: tcgpara);
        procedure add_llvm_callee_paraloc_names(p: tabstractprocdef);
        procedure reducetosingleregparaloc(paraloc: PCGParaLocation; def: tdef; reg: tregister);
        procedure reduceparalocs(p: tabstractprocdef; side: tcallercallee; paras: tparalist);
      end;


  implementation

    uses
      verbose,
      aasmbase,
      llvmsym,
      paramgr,defutil,llvmdef,
      cgutils,tgobj,hlcgobj;

  { tllvmparamanager }

  procedure tllvmparamanager.getcgtempparaloc(list: TAsmList; pd: tabstractprocdef; nr: longint; var cgpara: tcgpara);
    begin
      if (nr<1) or (nr>pd.paras.count) then
        InternalError(2015040402);
      pd.init_paraloc_info(callerside);
      createtempparaloc(list,pd.proccalloption,tparavarsym(pd.paras[nr-1]),true,cgpara);
    end;


  function tllvmparamanager.param_use_paraloc(const cgpara: tcgpara): boolean;
    begin
      { we can use the paraloc on the callee side if the SSA property is
        guaranteed, i.e., if it is a constant location (and if it's not been
        split up into multiple locations for ABI reasons). We can't deduce that
        from the paraloc though, we need the parasym for that. Potential
        future optimisation, although llvm will probably optimise away the
        temps we create anyway }
      result:=false;
    end;


  procedure tllvmparamanager.reducetosingleregparaloc(paraloc: PCGParaLocation; def: tdef; reg: tregister);
    var
      nextloc: pcgparalocation;
    begin
      paraloc^.def:=def;
      paraloc^.size:=def_cgsize(def);
      paraloc^.register:=reg;
      paraloc^.shiftval:=0;
      { remove all other paralocs }
      while assigned(paraloc^.next) do
        begin
          nextloc:=paraloc^.next;
          paraloc^.next:=nextloc^.next;
          dispose(nextloc);
        end;
    end;


  procedure tllvmparamanager.reduceparalocs(p: tabstractprocdef; side: tcallercallee; paras: tparalist);
    var
      paranr: longint;
      hp: tparavarsym;
      paraloc: PCGParaLocation;
    begin
      for paranr:=0 to paras.count-1 do
        begin
          hp:=tparavarsym(paras[paranr]);
          paraloc:=hp.paraloc[side].location;
          if assigned(paraloc) and
             assigned(paraloc^.next) and
             (hp.paraloc[side].def.typ in [orddef,enumdef,floatdef]) then
            begin
              if not(paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER]) then
                internalerror(2019011902);
              reducetosingleregparaloc(paraloc,hp.paraloc[side].def,paraloc^.register);
            end
          else if paraloc^.def=llvm_metadatatype then
            begin
              paraloc^.Loc:=LOC_REGISTER;
              // will be overwritten with a "register" whose superregister is an index in the LLVM metadata table
              paraloc^.register:=NR_INVALID;
            end;
        end;
    end;

  procedure tllvmparamanager.createtempparaloc(list: TAsmList; calloption: tproccalloption; parasym: tparavarsym; can_use_final_stack_loc: boolean; var cgpara: TCGPara);
    var
      paraloc: pcgparalocation;
      paralocdef: tdef;
    begin
      inherited;
      paraloc:=cgpara.location;
      { No need to set paraloc^.llvmloc.*, these are not used/needed for temp
        paralocs }
      while assigned(paraloc) do
        begin
          if (vo_is_funcret in parasym.varoptions)
 {$ifdef aarch64}
             { see AArch64's tcpuparamanager.create_paraloc_info_intern() }
             and not is_managed_type(parasym.vardef)
 {$endif aarch64}
             then
            paraloc^.retvalloc:=true;
          { ordinal parameters must be passed as a single paraloc }
          if (cgpara.def.typ in [orddef,enumdef,floatdef]) and
             assigned(paraloc^.next) then
            begin
              paraloc^.loc:=LOC_REGISTER;
              reducetosingleregparaloc(paraloc,cgpara.def,hlcg.getintregister(list,cgpara.def));
            end;

          { varargs parameters do not have a parasym.owner, but they're always
            by value }
          if (assigned(parasym.owner) and
              paramanager.push_addr_param(parasym.varspez,parasym.vardef,tabstractprocdef(parasym.owner.defowner).proccalloption)) or
             not llvmbyvalparaloc(paraloc) then
            begin
              case paraloc^.loc of
                LOC_REFERENCE:
                  begin
                    case hlcg.def2regtyp(paraloc^.def) of
                      R_INTREGISTER,
                      R_ADDRESSREGISTER:
                        paraloc^.loc:=LOC_REGISTER;
                      R_FPUREGISTER:
                        paraloc^.loc:=LOC_FPUREGISTER;
                      R_MMREGISTER:
                        paraloc^.Loc:=LOC_MMREGISTER;
                      else
                        internalerror(2013012308);
                    end;
                    paraloc^.register:=hlcg.getregisterfordef(list,paraloc^.def);
                    paraloc^.llvmvalueloc:=true;
                    { paraloc^.reference overlaid this field, so zero it now
                      that we turned it into a register location }
                    paraloc^.shiftval:=0;
                  end;
                LOC_REGISTER,
                LOC_FPUREGISTER,
                LOC_MMREGISTER:
                  begin
                    paraloc^.llvmvalueloc:=true;
                  end;
                LOC_VOID:
                  begin
                    { for empty records, ensure these don't get a byval
                      attribute }
                    paraloc^.llvmvalueloc:=true;
                  end;
                else
                  internalerror(2014012302);
              end;
            end
          else
            begin
              { turn this paraloc into the "byval" parameter: at the llvm level,
                a pointer to the value that it should place on the stack (or
                passed in registers, in some cases) }
              paraloc^.llvmvalueloc:=false;
              paraloc^.loc:=LOC_REGISTER;
              paralocdef:=cpointerdef.getreusable_no_free(paraloc^.def);
              reducetosingleregparaloc(paraloc,paralocdef,hlcg.getaddressregister(list,paralocdef));
            end;
          paraloc^.llvmloc.loc:=paraloc^.loc;
          paraloc^.llvmloc.reg:=paraloc^.register;
          paraloc:=paraloc^.next;
        end;
    end;


  function tllvmparamanager.create_paraloc_info(p: tabstractprocdef; side: tcallercallee): longint;
    begin
      result:=inherited;
      create_paraloc_info_internllvm(p,side);
    end;


  function tllvmparamanager.create_varargs_paraloc_info(p: tabstractprocdef; side: tcallercallee; varargspara: tvarargsparalist): longint;
    begin
      result:=inherited;
      create_paraloc_info_internllvm(p,side);
      if assigned(varargspara) then
        reduceparalocs(p,side,varargspara);
    end;


  function tllvmparamanager.get_funcretloc(p: tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
    var
      paraloc: pcgparalocation;
    begin
      result:=inherited;
      paraloc:=result.location;
      repeat
        paraloc^.llvmvalueloc:=true;
        paraloc:=paraloc^.next;
      until not assigned(paraloc);
      paraloc:=result.location;
      if assigned(paraloc^.next) and
         (result.def.typ in [orddef,enumdef,floatdef]) and
         ((side=callerside) or
          not(po_assembler in p.procoptions)) then
        begin
          if not(paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER]) then
            internalerror(2019011901);
          reducetosingleregparaloc(paraloc,result.def,paraloc^.register);
        end;
    end;


  function tllvmparamanager.has_strict_proc_signature: boolean;
    begin
      result:=true;
    end;


  procedure tllvmparamanager.create_paraloc_info_internllvm(p: tabstractprocdef; side: tcallercallee);
    begin
      { on the calleeside, llvm declares the parameters similar to Pascal or C
        (a list of parameters and their types), but they correspond more
        closely to parameter locations than to parameters -> add names to the
        locations }
      if (side=calleeside) and
         not(po_assembler in p.procoptions) then
        begin
          add_llvm_callee_paraloc_names(p);
          reduceparalocs(p,side,p.paras);
        end
      else if side=callerside then
        begin
          reduceparalocs(p,side,p.paras);
        end;
    end;


  { hp non-nil: parasym to check
    hp nil: function result
  }
  procedure tllvmparamanager.set_llvm_paraloc_name(p: tabstractprocdef; hp: tparavarsym; var para: tcgpara);
    var
      paraloc: PCGParaLocation;
      paralocnr: longint;
    begin
      paraloc:=hp.paraloc[calleeside].location;
      paralocnr:=0;
      repeat
        paraloc^.llvmloc.loc:=LOC_REFERENCE;
        paraloc^.llvmloc.sym:=current_asmdata.DefineAsmSymbol(llvmparaname(hp,paralocnr),AB_TEMP,AT_DATA,paraloc^.def);
        { byval: a pointer to a type that should actually be passed by
            value (e.g. a record that should be passed on the stack) }
        paraloc^.llvmvalueloc:=
          paramanager.push_addr_param(hp.varspez,hp.vardef,p.proccalloption) or
          not llvmbyvalparaloc(paraloc);
        paraloc:=paraloc^.next;
        inc(paralocnr);
      until not assigned(paraloc);
    end;


  procedure tllvmparamanager.add_llvm_callee_paraloc_names(p: tabstractprocdef);
    var
      paranr: longint;
      hp: tparavarsym;
    begin
      for paranr:=0 to p.paras.count-1 do
        begin
          hp:=tparavarsym(p.paras[paranr]);
          set_llvm_paraloc_name(p,hp,hp.paraloc[calleeside]);
        end;
    end;

begin
  if not assigned(paramanager) then
    begin
      writeln('Internalerror 2018052006');
      halt(1);
    end;
  { replace the native parameter manager. Maybe this has to be moved to a
    procedure like the creations of the code generators, but possibly not since
    we still call the original paramanager }
  paramanager.free;
  paramanager:=tllvmparamanager.create;
end.