summaryrefslogtreecommitdiff
path: root/compiler/llvm/nllvmbas.pas
blob: 5480923c9c2d58ac858f83159418c7ca13553aa9 (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
{
    Copyright (c) 2015 by Jonas Maebe

    This unit implements llvm support for some basic nodes

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

{$i fpcdefs.inc}

interface

    uses
       globtype,cclasses,
       aasmbase,aasmtai,aasmdata,
       nbas,ncgbas,
       symsym;

    type
       tllvmasmnode = class(tcgasmnode)
        protected
         { map a tasmsymbol to an index in fsymboldata }
         fsymbollookup: THashSet;
         { the LLVM symbolic inline assembly operands to which the ones in the
           source code are mapped }
         fsymboldata: tfplist;
         function getllvmasmopindexforsym(sym: tabstractnormalvarsym): longint;
         function getllvmasmparasym(sym: tabstractnormalvarsym): tasmsymbol;
         procedure ResolveRef(const filepos: tfileposinfo; var op: toper); override;
        public
         constructor create(p : TAsmList); override;
         destructor destroy; override;
         procedure pass_generate_code; override;
       end;

      tllvmtempinfoaccessor = class(ttempinfoaccessor)
       protected
        class procedure settempinfoflags(tempinfo: ptempinfo; const flags: ttempinfoflags); override;
      end;

       tllvmtempcreatenode = class(tcgtempcreatenode)
          procedure pass_generate_code;override;
       end;

  implementation

    uses
      verbose,cutils,
      cgbase,cgutils,paramgr,
      symconst,symdef,procinfo,
      node,
      cpubase,llvmbase,aasmllvm
      ;

{*****************************************************************************
                             TLLVMASMNODE
*****************************************************************************}

    function tllvmasmnode.getllvmasmopindexforsym(sym: tabstractnormalvarsym): longint;
      var
        key: record
          sym: pointer;
        end;
        res: PHashSetItem;
        callpara: pllvmcallpara;
      begin
        key.sym:=sym;
        res:=fsymbollookup.FindOrAdd(@key,sizeof(key));
        if not assigned(res^.Data) then
          begin
            new(callpara);
            callpara^.def:=cpointerdef.getreusable(sym.vardef);
            if (sym.typ=paravarsym) and
               paramanager.push_addr_param(sym.varspez,sym.vardef,current_procinfo.procdef.proccalloption) then
              callpara^.def:=cpointerdef.getreusable(callpara^.def);
            callpara^.sret:=false;
            callpara^.byval:=false;
            callpara^.valueext:=lve_none;
            callpara^.loc:=LOC_REGISTER;
            { address must be a temp register }
            if (sym.localloc.loc<>LOC_REFERENCE) or
               (sym.localloc.reference.base=NR_NO) or
               (sym.localloc.reference.index<>NR_NO) or
               (sym.localloc.reference.offset<>0) or
               assigned(sym.localloc.reference.symbol) then
              internalerror(2016111001);
            callpara^.reg:=sym.localloc.reference.base;
            fsymboldata.add(callpara);
            ptruint(res^.Data):=fsymboldata.count-1;
          end;
        result:=longint(ptruint(res^.Data));
      end;


    function tllvmasmnode.getllvmasmparasym(sym: tabstractnormalvarsym): tasmsymbol;
      begin
        { these have to be transformed from `nr into into $nr; we use ` because
          we also have to double all other occurrences of '$' in the assembly
          code, and we can't differentiate between these and other '$'s in
          agllvm }
        result:=current_asmdata.RefAsmSymbol('`'+tostr(getllvmasmopindexforsym(sym)),AT_DATA,false);
      end;


    procedure tllvmasmnode.ResolveRef(const filepos: tfileposinfo; var op: toper);
      var
        sym: tabstractnormalvarsym;
        ref: treference;
        sofs: pint;
        indexreg : tregister;
        getoffset: boolean;
{$ifdef x86}
        scale : byte;
{$endif x86}
      begin
        { pure assembler routines are handled by the regular code generator }
        if po_assembler in current_procinfo.procdef.procoptions then
          begin
            inherited;
            exit;
          end;
        { translate all symbolic references to "parameters" of the llvm
          assembler statements }
        case op.typ of
          top_local:
            begin
              sofs:=op.localoper^.localsymofs;
              indexreg:=op.localoper^.localindexreg;
{$ifdef x86}
              scale:=op.localoper^.localscale;
{$endif x86}
              getoffset:=op.localoper^.localgetoffset;
              sym:=tabstractnormalvarsym(op.localoper^.localsym);
              dispose(op.localoper);
              case sym.localloc.loc of
                LOC_REFERENCE:
                  begin
                    if getoffset then
                      begin
                        { todo: print proper error. You cannot get the offset
                          of a local variable since it may be in a register
                          outside the assembler block with llvm }
                        internalerror(2016102001);
                      end
                    else
                      begin
                        op.typ:=top_ref;
                        new(op.ref);
                        reference_reset_symbol(op.ref^,getllvmasmparasym(sym),sofs,
                          newalignment(sym.localloc.reference.alignment,sofs),[]);
                        op.ref^.index:=indexreg;
{$ifdef x86}
                        op.ref^.scalefactor:=scale;
{$endif x86}
                      end;
                  end
                else
                  { all locals accessed from assembler are forced into memory
                    by FPC }
                  internalerror(2016101506);
              end;
            end;
          else
            ;
        end;
      end;


    constructor tllvmasmnode.create(p: TAsmList);
      begin
        inherited;
      end;


    destructor tllvmasmnode.destroy;
      begin
        { normally already freed in pass_generate_code, but in case an error
          occurred that may not have happened }
        fsymboldata.free;
        fsymbollookup.free;
        inherited;
      end;


    procedure tllvmasmnode.pass_generate_code;
      var
        oldasmlist: tasmlist;
        asmai: tai;
      begin
        oldasmlist:=nil;
        if not(po_assembler in current_procinfo.procdef.procoptions) and
           not(nf_get_asm_position in flags) then
          begin
            { store the assembler code in a separate list, so we can make it
              the argument of an asmblock instruction }
            oldasmlist:=current_asmdata.CurrAsmList;
            current_asmdata.CurrAsmList:=tasmlist.create;
            { record relation between parameters and replaced local assembler
              operands }
            fsymboldata:=tfplist.create;
            fsymbollookup:=THashSet.Create(8,True,False);
          end;
        inherited;
        if not(po_assembler in current_procinfo.procdef.procoptions) and
           not(nf_get_asm_position in flags) then
          begin
            asmai:=taillvm.asm_paras(current_asmdata.CurrAsmList,fsymboldata);
            fsymboldata:=nil;
            fsymbollookup.free;
            fsymbollookup:=nil;
            oldasmlist.concat(asmai);
            current_asmdata.CurrAsmList:=oldasmlist;
          end;
      end;

{*****************************************************************************
                          TLLVMTEMPINFOACCESSOR
*****************************************************************************}

    class procedure tllvmtempinfoaccessor.settempinfoflags(tempinfo: ptempinfo; const flags: ttempinfoflags);
        begin
          { it is not possible to typecast between e.g. an integer and a record
            in a register, which is a problem if such a typecast is performed on
            an lvalue (since we then have to store it first to a temp in memory,
            which means we no longer have an lvalue).

            Disable regvars altogether since LLVM will put the values in registers
            anyway if possible/useful. }
          inherited settempinfoflags(tempinfo,flags-[ti_may_be_in_reg]);
        end;


{*****************************************************************************
                          TTEMPCREATENODE
*****************************************************************************}

    procedure tllvmtempcreatenode.pass_generate_code;
      begin
        inherited;

        { if a temp is in a register and we never assign anything to it (e.g.
          because it's the register for an inlined function result that never
          gets assigned a value), then llvm will be confused the first time
          we try to read from it (since it's never been defined) -> always
          immediately assign undef to such registers }
        if tempinfo^.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_FPUREGISTER,
             LOC_CFPUREGISTER,LOC_MMREGISTER,LOC_CMMREGISTER] then
          current_asmdata.CurrAsmList.concat(
            taillvm.op_reg_size_undef(la_bitcast,tempinfo^.location.register,tempinfo^.typedef)
          );
      end;


begin
   casmnode:=tllvmasmnode;
   ctempinfoaccessor:=tllvmtempinfoaccessor;
   ctempcreatenode:=tllvmtempcreatenode;
end.