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
|
{
Copyright (c) 1998-2003 by Jonas Maebe
This unit implements the generic implementation of 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 ncgopt;
{$i fpcdefs.inc}
interface
uses node, nopt;
type
tcgaddsstringcharoptnode = class(taddsstringcharoptnode)
function pass_typecheck: tnode; override;
function pass_1: tnode; override;
procedure pass_generate_code; override;
end;
implementation
uses
globtype,globals,
pass_1,defutil,
symdef,
aasmbase,aasmdata,
ncnv, ncon, pass_2,
cgbase, cpubase,
tgobj, cgobj, hlcgobj, cgutils;
{*****************************************************************************
TCGADDOPTNODE
*****************************************************************************}
function tcgaddsstringcharoptnode.pass_typecheck: tnode;
begin
pass_typecheck := nil;
typecheckpass(left);
typecheckpass(right);
if codegenerror then
exit;
{ update the curmaxlen field (before converting to a string!) }
updatecurmaxlen;
if not is_shortstring(left.resultdef) then
inserttypeconv(left,cshortstringtype);
resultdef:=left.resultdef;
end;
function tcgaddsstringcharoptnode.pass_1: tnode;
begin
pass_1 := nil;
firstpass(left);
firstpass(right);
if codegenerror then
exit;
expectloc:=LOC_REFERENCE;
end;
procedure tcgaddsstringcharoptnode.pass_generate_code;
var
l: tasmlabel;
href,href2 : treference;
hreg, lengthreg: tregister;
checklength: boolean;
len : integer;
begin
l:=nil;
{ first, we have to more or less replicate some code from }
{ ti386addnode.pass_generate_code }
secondpass(left);
if not(tg.istemp(left.location.reference) and
(tg.sizeoftemp(current_asmdata.CurrAsmList,left.location.reference) = 256)) then
begin
tg.gethltemp(current_asmdata.CurrAsmList,cshortstringtype,256,tt_normal,href);
hlcg.g_copyshortstring(current_asmdata.CurrAsmList,left.location.reference,href,tstringdef(cshortstringtype));
location_freetemp(current_asmdata.CurrAsmList,left.location);
{ return temp reference }
location_reset_ref(left.location,LOC_REFERENCE,def_cgsize(resultdef),1,[]);
left.location.reference:=href;
end;
secondpass(right);
{ special case for string := string + char (JM) }
hreg:=NR_NO;
{ we have to load the char before checking the length, because we }
{ may need registers from the reference }
{ is it a constant char? }
if not is_constcharnode(right) then
{ no, make sure it is in a register }
if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
begin
{ get register for the char }
hreg := cg.getintregister(current_asmdata.CurrAsmList,OS_8);
cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_8,OS_8,right.location.reference,hreg);
{ I don't think a temp char exists, but it won't hurt (JM) }
tg.ungetiftemp(current_asmdata.CurrAsmList,right.location.reference);
end
else hreg := right.location.register;
{ load the current string length }
lengthreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_8,OS_INT,left.location.reference,lengthreg);
{ do we have to check the length ? }
if tg.istemp(left.location.reference) then
checklength := curmaxlen = 255
else
checklength := curmaxlen >= tstringdef(left.resultdef).len;
if checklength then
begin
{ is it already maximal? }
current_asmdata.getjumplabel(l);
if tg.istemp(left.location.reference) then
len:=255
else
len:=tstringdef(left.resultdef).len;
cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,len,lengthreg,l)
end;
{ no, so increase the length and add the new character }
href2 := left.location.reference;
{ we need a new reference to store the character }
{ at the end of the string. Check if the base or }
{ index register is still free }
if (href2.base <> NR_NO) and
(href2.index <> NR_NO) then
begin
{ they're not free, so add the base reg to }
{ the string length (since the index can }
{ have a scalefactor) and use lengthreg as base }
cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_INT,href2.base,lengthreg);
href2.base := lengthreg;
end
else
{ at least one is still free, so put EDI there }
if href2.base = NR_NO then
href2.base := lengthreg
else
begin
href2.index := lengthreg;
{$ifdef x86}
href2.scalefactor := 1;
{$endif x86}
end;
{ we need to be one position after the last char }
inc(href2.offset);
{ store the character at the end of the string }
if (right.nodetype <> ordconstn) then
begin
{ no new_reference(href2) because it's only }
{ used once (JM) }
cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,hreg,href2);
end
else
cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,tordconstnode(right).value.svalue,href2);
lengthreg:=cg.makeregsize(current_asmdata.CurrAsmList,lengthreg,OS_8);
{ increase the string length }
cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,OS_8,1,lengthreg);
cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,lengthreg,left.location.reference);
if checklength then
cg.a_label(current_asmdata.CurrAsmList,l);
location_copy(location,left.location);
end;
begin
caddsstringcharoptnode := tcgaddsstringcharoptnode;
end.
|