summaryrefslogtreecommitdiff
path: root/compiler/llvm/nllvmcal.pas
blob: 14a7e36aaa62cd362f48c3fe9559cc667ed82a02 (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
{
    Copyright (c) 2014 by Jonas Maebe

    Generate LLVM bytecode for call 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 nllvmcal;

{$i fpcdefs.inc}

interface

    uses
      parabase,
      ncal,ncgcal,
      cgutils;

    type
      tllvmcallparanode = class(tcgcallparanode)
      end;

      tllvmcallnode = class(tcgcallnode)
       protected
        function paraneedsinlinetemp(para: tcallparanode; const pushconstaddr, complexpara: boolean): boolean; override;
        function can_call_ref(var ref: treference): boolean; override;
        procedure pushparas; override;
        procedure pass_generate_code; override;
      end;


implementation

     uses
       verbose,
       aasmbase,aasmdata,aasmllvm,
       symconst,symdef;

{*****************************************************************************
                           TLLVMCALLNODE
 *****************************************************************************}

    function tllvmcallnode.paraneedsinlinetemp(para: tcallparanode; const pushconstaddr, complexpara: boolean): boolean;
      begin
        { We don't insert type conversions for self node trees to the type of
          the self parameter (and doing so is quite hard due to all kinds of
          ugly hacks with this parameter). This means that if we pass on a
          self parameter through multiple levels of inlining, it may no
          longer match the actual type of the parameter it has been passed to
          -> always store in a temp which by definition will have the right
          type (if it's a pointer-like type) }
        if (vo_is_self in para.parasym.varoptions) and
           (is_class_or_interface_or_dispinterface(para.parasym.vardef) or
            is_classhelper(para.parasym.vardef) or
            ((para.parasym.vardef.typ=classrefdef) and
             is_class(tclassrefdef(para.parasym.vardef).pointeddef))) then
          result:=true
        else
          result:=inherited;
      end;

    function tllvmcallnode.can_call_ref(var ref: treference): boolean;
      begin
        result:=false;
      end;


    procedure tllvmcallnode.pushparas;
      var
        n: tcgcallparanode;
        paraindex: longint;
      begin
        { we just pass the temp paralocs here }
        if not assigned(varargsparas) then
          setlength(paralocs,procdefinition.paras.count)
        else
          setlength(paralocs,procdefinition.paras.count+varargsparas.count);
        n:=tcgcallparanode(left);
        while assigned(n) do
          begin
            { TODO: check whether this is correct for left-to-right calling
              conventions, may also depend on whether or not llvm knows about
              the calling convention }
            if not(cpf_varargs_para in n.callparaflags) then
              paraindex:=procdefinition.paras.indexof(n.parasym)
            else
              paraindex:=procdefinition.paras.count+varargsparas.indexof(n.parasym);
            if paraindex=-1 then
             internalerror(2014010602);
            paralocs[paraindex]:=@n.tempcgpara;
            n:=tcgcallparanode(n.right);
         end;
      end;


    procedure tllvmcallnode.pass_generate_code;
      var
        asmsym: tasmsymbol;
      begin
        inherited;
        if assigned(overrideprocnamedef) and
           not overrideprocnamedef.in_currentunit then
          begin
            { insert an llvm declaration for this def if it's not defined in
              the current unit, because otherwise we will define it in the
              LLVM IR using the def for which this procdef's name is used
              first, which may be something completely different from the original
              def. LLVM can take the original def into account to load certain
              registers, so if we use a wrong def this can result in wrong code
              generation. }
           asmsym:=current_asmdata.RefAsmSymbol(overrideprocnamedef.mangledname,AT_FUNCTION);
           if not asmsym.declared then
             begin
               current_asmdata.AsmLists[al_imports].Concat(taillvmdecl.createdecl(asmsym,overrideprocnamedef,nil,sec_code,overrideprocnamedef.alignment));
             end;
          end;
      end;

begin
  ccallnode:=tllvmcallnode;
end.