summaryrefslogtreecommitdiff
path: root/compiler/llvm/nllvmld.pas
blob: d5d0aa22ed7c04d8a679e153c164bdd90cb789be (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
{
    Copyright (c) 2012 by Jonas Maebe

    Generate LLVM bytecode for nodes that handle loads and assignments

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

{$i fpcdefs.inc}

interface

    uses
      globtype,
      cgutils,
      symtype,
      node,ncgld,ncgnstld;

    type
      tllvmloadnode = class(tcgnestloadnode)
        function pass_1: tnode; override;
        procedure pass_generate_code; override;
      end;

      { tllvmarrayconstructornode }

      tllvmarrayconstructornode = class(tcgarrayconstructornode)
       protected
        procedure makearrayref(var ref: treference; eledef: tdef); override;
      end;


implementation

     uses
       systems,
       aasmbase,aasmdata,aasmllvm,
       nld,
       symtable,symconst,symdef,symsym,defutil,
       procinfo,tgobj,
       llvmbase,cgbase,hlcgobj;

function tllvmloadnode.pass_1: tnode;
  begin
    result:=inherited;
    if assigned(result) then
      exit;
    case symtableentry.typ of
      procsym:
        begin
          if assigned(left) then
            expectloc:=LOC_REFERENCE;
        end;
    end;
  end;

procedure tllvmloadnode.pass_generate_code;
  var
    pvdef: tprocvardef;
    href, mpref: treference;
    field: tfieldvarsym;
    procreg, selfreg: tregister;
    selfdef: tdef;
    ai: taillvm;
  begin
    inherited;
    case symtableentry.typ of
      procsym:
        begin
          { if the result is returned in two registers, force it to memory into
            a single memory location, as we don't use the registerhi/register
            location hack for llvm (llvm will put it back into registers itself)
          }
          if assigned(left) and
            (resultdef.typ in [symconst.procdef,procvardef]) and
             not tabstractprocdef(resultdef).is_addressonly then
            begin
              pvdef:=tprocvardef(procdef.getcopyas(procvardef,pc_normal,''));
              { on little endian, location.register contains proc and
                location.registerhi contains self; on big endian, it's the
                other way around }
              tg.gethltemp(current_asmdata.CurrAsmList,pvdef,pvdef.size,tt_normal,href);
              if target_info.endian=endian_little then
                begin
                  procreg:=location.register;
                  selfreg:=location.registerhi
                end
              else
                begin
                  procreg:=location.registerhi;
                  selfreg:=location.register
                end;
              { left can be a pointerdef when we take the address of a nested
                procedure, as left will then be a pointer to the nestedfpstruct
              }
              if is_implicit_pointer_object_type(left.resultdef) or
                  (left.resultdef.typ in [classrefdef,pointerdef]) then
                selfdef:=left.resultdef
              else
                selfdef:=cpointerdef.getreusable(left.resultdef);
              mpref:=href;
              hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,cpointerdef.getreusable(pvdef),cpointerdef.getreusable(methodpointertype),mpref);
              hlcg.g_load_reg_field_by_name(current_asmdata.CurrAsmList,cprocvardef.getreusableprocaddr(procdef),trecorddef(methodpointertype),procreg,'proc',mpref);
              hlcg.g_load_reg_field_by_name(current_asmdata.CurrAsmList,selfdef,trecorddef(methodpointertype),selfreg,'self',mpref);
              location_reset_ref(location,LOC_REFERENCE,location.size,href.alignment,href.volatility);
              location.reference:=href;
            end;
        end;
      labelsym:
        begin
          selfreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidcodepointertype);
          ai:=taillvm.blockaddress(voidcodepointertype,
              current_asmdata.RefAsmSymbol(current_procinfo.procdef.mangledname,AT_FUNCTION),
              location.reference.symbol
            );
          current_asmdata.CurrAsmList.concat(
            taillvm.op_reg_tai_size(la_bitcast,selfreg,ai,voidcodepointertype)
          );
          reference_reset_base(location.reference,selfreg,0,ctempposinvalid,location.reference.alignment,location.reference.volatility);
        end;
    end;
  end;

{ tllvmarrayconstructornode }

procedure tllvmarrayconstructornode.makearrayref(var ref: treference; eledef: tdef);
  begin
    { the array elements are addressed as pointer to the individual elements ->
      convert }
    hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef),cpointerdef.getreusable(eledef),ref);
  end;


begin
  cloadnode:=tllvmloadnode;
  carrayconstructornode:=tllvmarrayconstructornode;
end.