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
|
{
Copyright (c) 2011 by Jonas Maebe
Support for load nodes on targets that have to group all local variables
and parameters accessed by nested routines into structs (and then pass the
address of these structs to nested routines rather than the frame pointer,
and access the local variables as fields thereof)
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 ncgnstld;
{$i fpcdefs.inc}
interface
uses
node,
symtype,
nld,
ncgld;
type
tcgnestloadnode = class(tcgloadnode)
protected
nestsym: tsym;
nestsymderef: tderef;
procedure generate_nested_access(vs: tsym);override;
function keep_param_address_in_nested_struct: boolean; virtual;
public
function pass_typecheck: tnode; override;
function pass_1:tnode;override;
function dogetcopy: tnode; override;
function docompare(p: tnode): boolean; override;
constructor ppuload(t: tnodetype; ppufile: tcompilerppufile); override;
procedure ppuwrite(ppufile: tcompilerppufile); override;
procedure buildderefimpl; override;
procedure derefimpl; override;
end;
implementation
uses
cutils,verbose,globtype,globals,systems,constexp,
symnot,
defutil,defcmp,
htypechk,pass_1,procinfo,paramgr,
cpuinfo,
symconst,symbase,symsym,symdef,symtable,symcreat,
ncon,ninl,ncnv,nmem,ncal,nutils,nbas,
pass_2,cgbase
;
{*****************************************************************************
TCGNESTLOADNODE
*****************************************************************************}
procedure tcgnestloadnode.generate_nested_access(vs: tsym);
begin
{ left has been transformed into a string of accesses that result in
the location of the original variable's copy in the appropriate
parentfpstruct (via tcgnestloadparentfpnode.pass_1). In case it is a
var/out/constref parameter, that "copy" will have been a copy of the
address so the normal handling of such parameters in ncgld is ok) }
secondpass(left);
location:=left.location;
end;
function tcgnestloadnode.keep_param_address_in_nested_struct: boolean;
begin
result:=is_addr_param_load;
end;
function tcgnestloadnode.pass_typecheck: tnode;
var
nestedvars: tsym;
begin
result:=inherited pass_typecheck;
if assigned(result) then
exit;
case symtableentry.typ of
paravarsym,
localvarsym :
begin
{ Nested variable? Then we have to move it to a structure that
can be passed by reference to nested routines }
if assigned(current_procinfo) and
(symtable.symtabletype in [localsymtable,parasymtable]) and
((symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) or
{ also redirect loads of locals/paras that have been moved to
the parentfpstruct inside the routine in which they were
originally declared, except in the initialisation code for
the parentfpstruct (nf_internal flag) }
(tabstractnormalvarsym(symtableentry).inparentfpstruct and
not(nf_internal in flags))) then
begin
{ get struct holding all locals accessed by nested routines }
nestedvars:=tprocdef(symtable.defowner).parentfpstruct;
{ don't add the parentfpstruct to itself! }
if nestedvars=symtableentry then
exit;
if not assigned(nestedvars) then
begin
{ create this struct }
build_parentfpstruct(tprocdef(symtable.defowner));
nestedvars:=tprocdef(symtable.defowner).parentfpstruct;
end;
{ store result for use in pass_1 }
nestsym:=maybe_add_sym_to_parentfpstruct(tprocdef(symtableentry.owner.defowner),symtableentry,resultdef,keep_param_address_in_nested_struct);
{ left normally holds the parentfp node. If it's not assigned,
this is an access to a local variable/para from the routine
in which it was actually declared -> redirect to its
equivalent in the parentfp struct }
if not assigned(left) then
begin
left:=caddrnode.create_internal(cloadnode.create(tprocdef(symtableentry.owner.defowner).parentfpstruct,tprocdef(symtableentry.owner.defowner).parentfpstruct.owner));
include(left.flags,nf_typedaddr);
end;
typecheckpass(left);
end;
end;
end;
end;
function tcgnestloadnode.pass_1:tnode;
var
thissym,
nestedvars: tsym;
nestedvarsdef: tdef;
begin
result:=inherited;
if assigned(result) then
exit;
case symtableentry.typ of
paravarsym,
localvarsym :
begin
{ Nested variable? Then we have to move it to a structure that
can be passed by reference to nested routines }
if assigned(current_procinfo) and
(symtable.symtabletype in [localsymtable,parasymtable]) and
((symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) or
(tabstractnormalvarsym(symtableentry).inparentfpstruct and
not(nf_internal in flags))) then
begin
{ get struct holding all locals accessed by nested routines }
nestedvars:=tprocdef(symtable.defowner).parentfpstruct;
if not assigned(nestedvars) then
begin
{ create this struct }
build_parentfpstruct(tprocdef(symtable.defowner));
nestedvars:=tprocdef(symtable.defowner).parentfpstruct;
end;
nestedvarsdef:=tlocalvarsym(nestedvars).vardef;
if nestedvars<>symtableentry then
thissym:=nestsym
else
thissym:=find_sym_in_parentfpstruct(tprocdef(symtableentry.owner.defowner),symtableentry);
if not assigned(thissym) then
internalerror(2011060406);
{ firstpass the parentfpnode. This will transform it into
a load of the appropriate parentfpstruct }
if not assigned(left) then
internalerror(2011060104);
firstpass(left);
{ subscript it to get the variable }
left:=csubscriptnode.create(thissym,cderefnode.create(left));
firstpass(left);
end;
end;
end;
end;
function tcgnestloadnode.dogetcopy: tnode;
begin
result:=inherited dogetcopy;
tcgnestloadnode(result).nestsym:=nestsym;
end;
function tcgnestloadnode.docompare(p: tnode): boolean;
begin
result:=
inherited docompare(p) and
(tcgnestloadnode(p).nestsym=nestsym);
end;
constructor tcgnestloadnode.ppuload(t: tnodetype; ppufile: tcompilerppufile);
begin
inherited ppuload(t, ppufile);
ppufile.getderef(nestsymderef);
end;
procedure tcgnestloadnode.ppuwrite(ppufile: tcompilerppufile);
begin
inherited ppuwrite(ppufile);
ppufile.putderef(nestsymderef);
end;
procedure tcgnestloadnode.buildderefimpl;
begin
inherited buildderefimpl;
nestsymderef.build(nestsym);
end;
procedure tcgnestloadnode.derefimpl;
begin
inherited derefimpl;
nestsym:=tsym(nestsymderef.resolve);
end;
begin
cloadnode:=tcgnestloadnode;
end.
|