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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
{
Copyright (c) 1998-2002 by Jonas Maebe
This unit implements optimized 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 nopt;
{$i fpcdefs.inc}
interface
uses node,nbas,nadd;
type
tsubnodetype = (
addsstringcharoptn, { shorstring + char }
addsstringcsstringoptn { shortstring + constant shortstring }
);
taddoptnode = class(taddnode)
subnodetype: tsubnodetype;
constructor create(ts: tsubnodetype; l,r : tnode); virtual;
{ pass_1 will be overridden by the separate subclasses }
{ By default, pass_generate_code is the same as for addnode }
{ Only if there's a processor specific implementation, it }
{ will be overridden. }
function dogetcopy: tnode; override;
function docompare(p: tnode): boolean; override;
end;
taddsstringoptnode = class(taddoptnode)
{ maximum length of the string until now, allows us to skip a compare }
{ sometimes (it's initialized/updated by calling updatecurmaxlen) }
curmaxlen: byte;
{ pass_1 must be overridden, otherwise we get an endless loop }
function pass_typecheck: tnode; override;
function pass_1: tnode; override;
function dogetcopy: tnode; override;
function docompare(p: tnode): boolean; override;
protected
procedure updatecurmaxlen;
end;
{ add a char to a shortstring }
taddsstringcharoptnode = class(taddsstringoptnode)
constructor create(l,r : tnode); virtual;
end;
taddsstringcharoptnodeclass = class of taddsstringcharoptnode;
{ add a constant string to a short string }
taddsstringcsstringoptnode = class(taddsstringoptnode)
constructor create(l,r : tnode); virtual;
function pass_1: tnode; override;
end;
taddsstringcsstringoptnodeclass = class of taddsstringcsstringoptnode;
function canbeaddsstringcharoptnode(p: taddnode): boolean;
function genaddsstringcharoptnode(p: taddnode): tnode;
function canbeaddsstringcsstringoptnode(p: taddnode): boolean;
function genaddsstringcsstringoptnode(p: taddnode): tnode;
function canbemultistringadd(p: taddnode): boolean;
function genmultistringadd(p: taddnode): tnode;
function is_addsstringoptnode(p: tnode): boolean;
var
caddsstringcharoptnode: taddsstringcharoptnodeclass;
caddsstringcsstringoptnode: taddsstringcsstringoptnodeclass;
implementation
uses cutils, htypechk, defutil, defcmp, globtype, globals, cpubase, ncnv, ncon,ncal,nld,nmem,
verbose, symconst,symdef, cgbase, procinfo;
{*****************************************************************************
TADDOPTNODE
*****************************************************************************}
constructor taddoptnode.create(ts: tsubnodetype; l,r : tnode);
begin
{ we need to keep the addn nodetype, otherwise taddnode.pass_generate_code will be }
{ confused. Comparison for equal nodetypes therefore has to be }
{ implemented using the classtype() method (JM) }
inherited create(addn,l,r);
subnodetype := ts;
end;
function taddoptnode.dogetcopy: tnode;
var
hp: taddoptnode;
begin
hp := taddoptnode(inherited dogetcopy);
hp.subnodetype := subnodetype;
dogetcopy := hp;
end;
function taddoptnode.docompare(p: tnode): boolean;
begin
docompare :=
inherited docompare(p) and
(subnodetype = taddoptnode(p).subnodetype);
end;
{*****************************************************************************
TADDSSTRINGOPTNODE
*****************************************************************************}
function taddsstringoptnode.pass_typecheck: tnode;
begin
result := nil;
updatecurmaxlen;
{ left and right are already firstpass'ed by taddnode.pass_1 }
if not is_shortstring(left.resultdef) then
inserttypeconv(left,cshortstringtype);
if not is_shortstring(right.resultdef) then
inserttypeconv(right,cshortstringtype);
resultdef := left.resultdef;
end;
function taddsstringoptnode.pass_1: tnode;
begin
pass_1 := nil;
expectloc:= LOC_REFERENCE;
calcregisters(self,0,0,0);
{ here we call STRCONCAT or STRCMP or STRCOPY }
include(current_procinfo.flags,pi_do_call);
end;
function taddsstringoptnode.dogetcopy: tnode;
var
hp: taddsstringoptnode;
begin
hp := taddsstringoptnode(inherited dogetcopy);
hp.curmaxlen := curmaxlen;
dogetcopy := hp;
end;
function taddsstringoptnode.docompare(p: tnode): boolean;
begin
docompare :=
inherited docompare(p) and
(curmaxlen = taddsstringcharoptnode(p).curmaxlen);
end;
function is_addsstringoptnode(p: tnode): boolean;
begin
is_addsstringoptnode :=
p.inheritsfrom(taddsstringoptnode);
end;
procedure taddsstringoptnode.updatecurmaxlen;
begin
if is_addsstringoptnode(left) then
begin
{ made it a separate block so no other if's are processed (would be a }
{ simple waste of time) (JM) }
if (taddsstringoptnode(left).curmaxlen < 255) then
case subnodetype of
addsstringcharoptn:
curmaxlen := succ(taddsstringoptnode(left).curmaxlen);
addsstringcsstringoptn:
curmaxlen := min(taddsstringoptnode(left).curmaxlen +
tstringconstnode(right).len,255)
else
internalerror(291220001);
end
else curmaxlen := 255;
end
else if (left.nodetype = stringconstn) then
curmaxlen := min(tstringconstnode(left).len,255)
else if is_char(left.resultdef) then
curmaxlen := 1
else if (left.nodetype = typeconvn) then
begin
case ttypeconvnode(left).convtype of
tc_char_2_string:
curmaxlen := 1;
{ doesn't work yet, don't know why (JM)
tc_chararray_2_string:
curmaxlen :=
min(ttypeconvnode(left).left.resultdef.size,255); }
else curmaxlen := 255;
end;
end
else
curmaxlen := 255;
end;
{*****************************************************************************
TADDSSTRINGCHAROPTNODE
*****************************************************************************}
constructor taddsstringcharoptnode.create(l,r : tnode);
begin
inherited create(addsstringcharoptn,l,r);
end;
{*****************************************************************************
TADDSSTRINGCSSTRINGOPTNODE
*****************************************************************************}
constructor taddsstringcsstringoptnode.create(l,r : tnode);
begin
inherited create(addsstringcsstringoptn,l,r);
end;
function taddsstringcsstringoptnode.pass_1: tnode;
begin
{ create the call to the concat routine both strings as arguments }
result := ccallnode.createintern('fpc_shortstr_append_shortstr',
ccallparanode.create(left,ccallparanode.create(right,nil)));
left:=nil;
right:=nil;
end;
{*****************************************************************************
HELPERS
*****************************************************************************}
function canbeaddsstringcharoptnode(p: taddnode): boolean;
begin
canbeaddsstringcharoptnode :=
(cs_opt_level1 in current_settings.optimizerswitches) and
{ the shortstring will be gotten through conversion if necessary (JM)
is_shortstring(p.left.resultdef) and }
((p.nodetype = addn) and
is_char(p.right.resultdef));
end;
function genaddsstringcharoptnode(p: taddnode): tnode;
var
hp: tnode;
begin
hp := caddsstringcharoptnode.create(p.left.getcopy,p.right.getcopy);
hp.flags := p.flags;
genaddsstringcharoptnode := hp;
end;
function canbeaddsstringcsstringoptnode(p: taddnode): boolean;
begin
canbeaddsstringcsstringoptnode :=
(cs_opt_level1 in current_settings.optimizerswitches) and
{ the shortstring will be gotten through conversion if necessary (JM)
is_shortstring(p.left.resultdef) and }
((p.nodetype = addn) and
(p.right.nodetype = stringconstn));
end;
function genaddsstringcsstringoptnode(p: taddnode): tnode;
var
hp: tnode;
begin
hp := caddsstringcsstringoptnode.create(p.left.getcopy,p.right.getcopy);
hp.flags := p.flags;
genaddsstringcsstringoptnode := hp;
end;
function canbemultistringadd(p: taddnode): boolean;
var
hp : tnode;
i : longint;
begin
result:=false;
if p.resultdef.deftype<>stringdef then
exit;
i:=0;
hp:=p;
while assigned(hp) and (hp.nodetype=addn) do
begin
inc(i);
hp:=taddnode(hp).left;
end;
result:=(i>1);
end;
function genmultistringadd(p: taddnode): tnode;
var
hp,sn : tnode;
arrp : tarrayconstructornode;
newstatement : tstatementnode;
tempnode : ttempcreatenode;
is_shortstr : boolean;
begin
arrp:=nil;
hp:=p;
is_shortstr:=is_shortstring(p.resultdef);
while assigned(hp) and (hp.nodetype=addn) do
begin
sn:=taddnode(hp).right.getcopy;
inserttypeconv(sn,p.resultdef);
if is_shortstr then
begin
sn:=caddrnode.create(sn);
include(sn.flags,nf_internal);
end;
arrp:=carrayconstructornode.create(sn,arrp);
hp:=taddnode(hp).left;
end;
sn:=hp.getcopy;
inserttypeconv(sn,p.resultdef);
if is_shortstr then
begin
sn:=caddrnode.create(sn);
include(sn.flags,nf_internal);
end;
arrp:=carrayconstructornode.create(sn,arrp);
if assigned(aktassignmentnode) and
(aktassignmentnode.right=p) and
(aktassignmentnode.left.resultdef=p.resultdef) and
valid_for_var(aktassignmentnode.left,false) then
begin
result:=ccallnode.createintern('fpc_'+
tstringdef(p.resultdef).stringtypname+'_concat_multi',
ccallparanode.create(arrp,
ccallparanode.create(aktassignmentnode.left.getcopy,nil)));
include(aktassignmentnode.flags,nf_assign_done_in_right);
end
else
begin
result:=internalstatements(newstatement);
tempnode:=ctempcreatenode.create(p.resultdef,p.resultdef.size,tt_persistent ,true);
addstatement(newstatement,tempnode);
addstatement(newstatement,ccallnode.createintern('fpc_'+
tstringdef(p.resultdef).stringtypname+'_concat_multi',
ccallparanode.create(arrp,
ccallparanode.create(ctemprefnode.create(tempnode),nil))));
addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
addstatement(newstatement,ctemprefnode.create(tempnode));
end;
end;
begin
caddsstringcharoptnode := taddsstringcharoptnode;
caddsstringcsstringoptnode := taddsstringcsstringoptnode;
end.
|