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
|
{
Copyright (c) 1998-2002,2012 by Florian Klaempfl, Jonas Maebe
This unit implements the LLVM-specific temp. generator
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 tgllvm;
{$i fpcdefs.inc}
interface
uses
cclasses,
globals,globtype,
symtype,
cpubase,cpuinfo,cgbase,cgutils,
aasmbase,aasmtai,aasmdata,
tgobj;
type
{ LLVM temp manager: in LLVM, you allocate every temp separately using
the "alloca" instrinsic. Every such temp is a separate stack slot, but
can be turned into a regvar (or be decomposed) by LLVM. To avoid
problems with turning stack slots into regvars, we don't allocate one
big blob of memory that we manage ourselves using the regular temp
manager. Instead, we just allocate a new "stack pointer register"
(R_TEMPREGISTER) every time we need a new temp. This allows for having
the generic code generator modify the offset without interfering with
our ability to determine which temp the reference points to.
Temps are currently not reused, but that should probably be added in
the future (except if adding liveness information for the temps enables
llvm to do so by itself and we don't run out of temp registers).
}
{ ttgllvm }
ttgllvm = class(ttgobj)
protected
procedure alloctemp(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; def: tdef; fini: boolean; out ref: treference); override;
procedure gethltempintern(list: TAsmList; def: tdef; alignment: shortint; forcesize: asizeint; temptype: ttemptype; out ref: treference);
procedure freetemphook(list: TAsmList; temp: ptemprecord); override;
procedure emit_lifetime(list: TAsmList; const procname: string; temp: ptemprecord);
public
alloclist: tasmlist;
constructor create; override;
destructor destroy; override;
procedure setfirsttemp(l: asizeint); override;
procedure temp_to_ref(p: ptemprecord; out ref: treference); override;
procedure getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref: treference); override;
procedure gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference); override;
end;
var
orgtgclass: ttgobjclass;
implementation
uses
cutils,
systems,verbose,
procinfo,
llvmbase,aasmllvm,
symconst,symtable,symdef,defutil,
paramgr,parabase,cgobj,hlcgobj
;
{ ttgllvm }
procedure ttgllvm.alloctemp(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; def: tdef; fini: boolean; out ref: treference);
var
tl: ptemprecord;
reg: tregister;
oldfileinfo: tfileposinfo;
begin
reg:=cg.gettempregister(list);
new(tl);
tl^.temptype:=temptype;
tl^.def:=def;
tl^.fini:=fini;
tl^.alignment:=alignment;
tl^.pos:=getsupreg(reg);
tl^.size:=size;
tl^.next:=templist;
tl^.nextfree:=nil;
templist:=tl;
temp_to_ref(tl,ref);
list.concat(tai_tempalloc.alloc(tl^.pos,tl^.size));
emit_lifetime(list,'llvm_lifetime_start',tl);
inc(lasttemp);
{ allocation for the temp -- should have lineinfo of the start of the
routine }
if assigned(current_procinfo) then
begin
oldfileinfo:=current_filepos;
current_filepos:=current_procinfo.procdef.fileinfo;
end
else
{ avoid uninitialised warning later }
oldfileinfo.line:=0;
alloclist.concat(taillvm.op_ref_size(la_alloca,ref,def));
if assigned(current_procinfo) then
current_filepos:=oldfileinfo;
end;
procedure ttgllvm.gethltempintern(list: TAsmList; def: tdef; alignment: shortint; forcesize: asizeint; temptype: ttemptype; out ref: treference);
begin
{ empty array (can happen for arrayconstructors) -> don't request the
size, as that will internalerror }
if (def.typ=arraydef) and
(tarraydef(def).highrange<tarraydef(def).lowrange) then
alloctemp(list,0,alignment,temptype,def,false,ref)
else
alloctemp(list,def.size,alignment,temptype,def,false,ref);
end;
procedure ttgllvm.freetemphook(list: TAsmList; temp: ptemprecord);
begin
inherited;
emit_lifetime(list,'llvm_lifetime_end',temp);
end;
procedure ttgllvm.emit_lifetime(list: TAsmList; const procname: string; temp: ptemprecord);
var
sizepara, ptrpara: tcgpara;
pd: tprocdef;
ref: treference;
begin
if (temp^.size<>0) and
not is_managed_type(temp^.def) then
begin
temp_to_ref(temp,ref);
sizepara.init;
ptrpara.init;
pd:=search_system_proc(procname);
paramanager.getcgtempparaloc(list,pd,1,sizepara);
paramanager.getcgtempparaloc(list,pd,2,ptrpara);
hlcg.a_load_const_cgpara(list,sizepara.def,temp^.size,sizepara);
hlcg.a_loadaddr_ref_cgpara(list,temp^.def,ref,ptrpara);
hlcg.g_call_system_proc(list,pd,[@sizepara,@ptrpara],nil).resetiftemp;
sizepara.reset;
ptrpara.reset;
end;
end;
procedure ttgllvm.temp_to_ref(p: ptemprecord; out ref: treference);
var
temppos: treftemppos;
begin
{ on the LLVM target, every temp is independent and encoded via a
separate temp register whose superregister number is stored in p^.pos }
temppos.val:=p^.pos;
reference_reset_base(ref,newreg(R_TEMPREGISTER,p^.pos,R_SUBWHOLE),0,temppos,p^.alignment,[]);
end;
constructor ttgllvm.create;
begin
inherited create;
direction:=1;
alloclist:=TAsmList.create;
end;
destructor ttgllvm.destroy;
begin
alloclist.free;
inherited destroy;
end;
procedure ttgllvm.setfirsttemp(l: asizeint);
begin
firsttemp:=l;
lasttemp:=l;
end;
procedure ttgllvm.getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref: treference);
begin
alignment:=used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
gethltempintern(list,def,alignment,size,tt_persistent,ref);
end;
procedure ttgllvm.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
begin
gethltempintern(list,def,def.alignment,forcesize,temptype,ref);
end;
begin
if not assigned(tgobjclass) then
begin
writeln('Internalerror 2018052004');
halt(1);
end;
orgtgclass:=tgobjclass;
tgobjclass:=ttgllvm;
end.
|