Mercurial > projects > ddmd
annotate dmd/AssignExp.d @ 130:60bb0fe4563e
dmdfe 2.037 first main iteration
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Thu, 09 Sep 2010 22:51:44 +0100 |
parents | 1765f3ef917d |
children | e3afd1303184 |
rev | line source |
---|---|
72 | 1 module dmd.AssignExp; |
2 | |
114 | 3 import dmd.common; |
72 | 4 import dmd.Expression; |
5 import dmd.Identifier; | |
6 import dmd.backend.elem; | |
7 import dmd.InterState; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
8 import dmd.Parameter; |
72 | 9 import dmd.IndexExp; |
10 import dmd.CallExp; | |
11 import dmd.CastExp; | |
12 import dmd.TypeSArray; | |
13 import dmd.StructLiteralExp; | |
14 import dmd.ArrayLengthExp; | |
15 import dmd.TypeStruct; | |
16 import dmd.StructDeclaration; | |
17 import dmd.VarExp; | |
18 import dmd.TY; | |
19 import dmd.SliceExp; | |
20 import dmd.CommaExp; | |
21 import dmd.ArrayExp; | |
22 import dmd.AggregateDeclaration; | |
23 import dmd.CondExp; | |
24 import dmd.DotVarExp; | |
25 import dmd.WANT; | |
26 import dmd.Id; | |
27 import dmd.TypeClass; | |
28 import dmd.OutBuffer; | |
29 import dmd.Loc; | |
30 import dmd.TypeNext; | |
31 import dmd.TupleExp; | |
32 import dmd.VarDeclaration; | |
33 import dmd.Scope; | |
34 import dmd.IRState; | |
35 import dmd.ArrayTypes; | |
36 import dmd.BinExp; | |
37 import dmd.TOK; | |
38 import dmd.Global; | |
39 import dmd.Declaration; | |
40 import dmd.TypeFunction; | |
41 import dmd.Type; | |
42 import dmd.RET; | |
43 import dmd.STC; | |
44 import dmd.DotIdExp; | |
45 | |
46 import dmd.backend.Util; | |
47 import dmd.backend.Symbol; | |
48 import dmd.backend.OPER; | |
49 import dmd.backend.TYM; | |
50 import dmd.backend.RTLSYM; | |
51 import dmd.codegen.Util; | |
52 import dmd.expression.Util; | |
53 | |
0 | 54 class AssignExp : BinExp |
55 { | |
56 int ismemset = 0; | |
57 | |
58 this(Loc loc, Expression e1, Expression e2) | |
59 { | |
60 super(loc, TOK.TOKassign, AssignExp.sizeof, e1, e2); | |
61 } | |
62 | |
72 | 63 override Expression semantic(Scope sc) |
0 | 64 { |
72 | 65 Expression e1old = e1; |
66 | |
67 version (LOGSEMANTIC) { | |
68 printf("AssignExp.semantic('%s')\n", toChars()); | |
69 } | |
70 //printf("e1.op = %d, '%s'\n", e1.op, Token.toChars(e1.op)); | |
71 //printf("e2.op = %d, '%s'\n", e2.op, Token.toChars(e2.op)); | |
72 | |
73 if (type) | |
74 return this; | |
75 | |
76 if (e2.op == TOK.TOKcomma) | |
77 { | |
78 /* Rewrite to get rid of the comma from rvalue | |
79 */ | |
80 AssignExp ea = new AssignExp(loc, e1, (cast(CommaExp)e2).e2); | |
81 ea.op = op; | |
82 Expression e = new CommaExp(loc, (cast(CommaExp)e2).e1, ea); | |
83 return e.semantic(sc); | |
84 } | |
85 | |
86 /* Look for operator overloading of a[i]=value. | |
87 * Do it before semantic() otherwise the a[i] will have been | |
88 * converted to a.opIndex() already. | |
89 */ | |
90 if (e1.op == TOK.TOKarray) | |
91 { | |
92 ArrayExp ae = cast(ArrayExp)e1; | |
93 AggregateDeclaration ad; | |
94 Identifier id = Id.index; | |
95 | |
96 ae.e1 = ae.e1.semantic(sc); | |
97 Type t1 = ae.e1.type.toBasetype(); | |
98 if (t1.ty == TY.Tstruct) | |
99 { | |
100 ad = (cast(TypeStruct)t1).sym; | |
101 goto L1; | |
102 } | |
103 else if (t1.ty == TY.Tclass) | |
104 { | |
105 ad = (cast(TypeClass)t1).sym; | |
106 L1: | |
107 // Rewrite (a[i] = value) to (a.opIndexAssign(value, i)) | |
108 if (search_function(ad, Id.indexass)) | |
109 { | |
110 Expression e = new DotIdExp(loc, ae.e1, Id.indexass); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
111 auto a = ae.arguments.copy(); |
72 | 112 |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
113 a.insert(0, e2); |
72 | 114 e = new CallExp(loc, e, a); |
115 e = e.semantic(sc); | |
116 return e; | |
117 } | |
118 else | |
119 { | |
120 // Rewrite (a[i] = value) to (a.opIndex(i, value)) | |
121 if (search_function(ad, id)) | |
122 { | |
123 Expression e = new DotIdExp(loc, ae.e1, id); | |
124 | |
125 if (1 || !global.params.useDeprecated) | |
126 error("operator [] assignment overload with opIndex(i, value) illegal, use opIndexAssign(value, i)"); | |
127 | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
128 e = new CallExp(loc, e, ae.arguments[0], e2); |
72 | 129 e = e.semantic(sc); |
130 return e; | |
131 } | |
132 } | |
133 } | |
134 } | |
135 /* Look for operator overloading of a[i..j]=value. | |
136 * Do it before semantic() otherwise the a[i..j] will have been | |
137 * converted to a.opSlice() already. | |
138 */ | |
139 if (e1.op == TOK.TOKslice) | |
140 { | |
141 Type t1; | |
142 SliceExp ae = cast(SliceExp)e1; | |
143 AggregateDeclaration ad; | |
144 Identifier id = Id.index; | |
145 | |
146 ae.e1 = ae.e1.semantic(sc); | |
147 ae.e1 = resolveProperties(sc, ae.e1); | |
148 t1 = ae.e1.type.toBasetype(); | |
149 if (t1.ty == TY.Tstruct) | |
150 { | |
151 ad = (cast(TypeStruct)t1).sym; | |
152 goto L2; | |
153 } | |
154 else if (t1.ty == TY.Tclass) | |
155 { | |
156 ad = (cast(TypeClass)t1).sym; | |
157 L2: | |
158 // Rewrite (a[i..j] = value) to (a.opIndexAssign(value, i, j)) | |
159 if (search_function(ad, Id.sliceass)) | |
160 { | |
161 Expression e = new DotIdExp(loc, ae.e1, Id.sliceass); | |
162 Expressions a = new Expressions(); | |
163 | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
164 a.push(e2); |
72 | 165 if (ae.lwr) |
166 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
167 a.push(ae.lwr); |
72 | 168 assert(ae.upr); |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
169 a.push(ae.upr); |
72 | 170 } |
171 else | |
172 assert(!ae.upr); | |
173 | |
174 e = new CallExp(loc, e, a); | |
175 e = e.semantic(sc); | |
176 return e; | |
177 } | |
178 } | |
179 } | |
180 | |
181 BinExp.semantic(sc); | |
182 | |
183 if (e1.op == TOK.TOKdottd) | |
184 { | |
185 // Rewrite a.b=e2, when b is a template, as a.b(e2) | |
186 Expression e = new CallExp(loc, e1, e2); | |
187 e = e.semantic(sc); | |
188 return e; | |
189 } | |
190 | |
191 e2 = resolveProperties(sc, e2); | |
192 assert(e1.type); | |
193 | |
194 /* Rewrite tuple assignment as a tuple of assignments. | |
195 */ | |
196 if (e1.op == TOK.TOKtuple && e2.op == TOK.TOKtuple) | |
197 { | |
198 TupleExp tup1 = cast(TupleExp)e1; | |
199 TupleExp tup2 = cast(TupleExp)e2; | |
200 size_t dim = tup1.exps.dim; | |
201 if (dim != tup2.exps.dim) | |
202 { | |
203 error("mismatched tuple lengths, %d and %d", cast(int)dim, cast(int)tup2.exps.dim); | |
204 } | |
205 else | |
206 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
207 auto exps = new Expressions; |
72 | 208 exps.setDim(dim); |
209 | |
210 for (int i = 0; i < dim; i++) | |
211 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
212 auto ex1 = tup1.exps[i]; |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
213 auto ex2 = tup2.exps[i]; |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
214 exps[i] = new AssignExp(loc, ex1, ex2); |
72 | 215 } |
216 Expression e = new TupleExp(loc, exps); | |
217 e = e.semantic(sc); | |
218 return e; | |
219 } | |
220 } | |
221 | |
222 // Determine if this is an initialization of a reference | |
223 int refinit = 0; | |
224 if (op == TOK.TOKconstruct && e1.op == TOK.TOKvar) | |
225 { | |
226 VarExp ve = cast(VarExp)e1; | |
227 VarDeclaration v = ve.var.isVarDeclaration(); | |
228 if (v.storage_class & (STC.STCout | STC.STCref)) | |
229 refinit = 1; | |
230 } | |
231 | |
232 Type t1 = e1.type.toBasetype(); | |
233 | |
234 if (t1.ty == TY.Tfunction) | |
235 { | |
236 // Rewrite f=value to f(value) | |
237 Expression e = new CallExp(loc, e1, e2); | |
238 e = e.semantic(sc); | |
239 return e; | |
240 } | |
241 | |
242 /* If it is an assignment from a 'foreign' type, | |
243 * check for operator overloading. | |
244 */ | |
245 if (t1.ty == TY.Tstruct) | |
246 { | |
247 StructDeclaration sd = (cast(TypeStruct)t1).sym; | |
248 if (op == TOK.TOKassign) | |
249 { | |
250 Expression e = op_overload(sc); | |
251 if (e) | |
252 return e; | |
253 } | |
254 else if (op == TOK.TOKconstruct && !refinit) | |
255 { | |
256 Type t2 = e2.type.toBasetype(); | |
257 if (t2.ty == TY.Tstruct && sd == (cast(TypeStruct)t2).sym && sd.cpctor) | |
258 { | |
259 /* We have a copy constructor for this | |
260 */ | |
261 if (e2.op == TOK.TOKquestion) | |
262 { /* Write as: | |
263 * a ? e1 = b : e1 = c; | |
264 */ | |
265 CondExp ec = cast(CondExp)e2; | |
266 AssignExp ea1 = new AssignExp(ec.e1.loc, e1, ec.e1); | |
267 ea1.op = op; | |
268 AssignExp ea2 = new AssignExp(ec.e1.loc, e1, ec.e2); | |
269 ea2.op = op; | |
270 Expression e = new CondExp(loc, ec.econd, ea1, ea2); | |
271 return e.semantic(sc); | |
272 } | |
273 else if (e2.op == TOK.TOKvar || e2.op == TOK.TOKdotvar || e2.op == TOK.TOKstar || e2.op == TOK.TOKindex) | |
274 { /* Write as: | |
275 * e1.cpctor(e2); | |
276 */ | |
277 Expression e = new DotVarExp(loc, e1, sd.cpctor, 0); | |
278 e = new CallExp(loc, e, e2); | |
279 return e.semantic(sc); | |
280 } | |
281 } | |
282 } | |
283 } | |
284 else if (t1.ty == TY.Tclass) | |
285 { | |
286 // Disallow assignment operator overloads for same type | |
287 if (!e2.type.implicitConvTo(e1.type)) | |
288 { | |
289 Expression e = op_overload(sc); | |
290 if (e) | |
291 return e; | |
292 } | |
293 } | |
294 | |
295 if (t1.ty == TY.Tsarray && !refinit) | |
296 { | |
297 // Convert e1 to e1[] | |
298 Expression e = new SliceExp(e1.loc, e1, null, null); | |
299 e1 = e.semantic(sc); | |
300 t1 = e1.type.toBasetype(); | |
301 } | |
302 | |
303 e2.rvalue(); | |
304 | |
305 if (e1.op == TOK.TOKarraylength) | |
306 { | |
307 // e1 is not an lvalue, but we let code generator handle it | |
308 ArrayLengthExp ale = cast(ArrayLengthExp)e1; | |
309 ale.e1 = ale.e1.modifiableLvalue(sc, e1); | |
310 } | |
311 else if (e1.op == TOK.TOKslice) | |
312 { | |
313 Type tn = e1.type.nextOf(); | |
314 if (tn && !tn.isMutable() && op != TOK.TOKconstruct) | |
315 error("slice %s is not mutable", e1.toChars()); | |
316 } | |
317 else | |
318 { | |
319 // Try to do a decent error message with the expression | |
320 // before it got constant folded | |
321 if (e1.op != TOK.TOKvar) | |
322 e1 = e1.optimize(WANT.WANTvalue); | |
323 | |
324 if (op != TOK.TOKconstruct) | |
325 e1 = e1.modifiableLvalue(sc, e1old); | |
326 } | |
327 | |
328 Type t2 = e2.type; | |
329 if (e1.op == TOK.TOKslice && t1.nextOf() && e2.implicitConvTo(t1.nextOf())) | |
330 { | |
331 // memset | |
332 ismemset = 1; // make it easy for back end to tell what this is | |
333 e2 = e2.implicitCastTo(sc, t1.nextOf()); | |
334 } | |
335 else if (t1.ty == TY.Tsarray) | |
336 { | |
337 /* Should have already converted e1 => e1[] | |
338 */ | |
339 assert(op == TOK.TOKconstruct); | |
340 //error("cannot assign to static array %s", e1.toChars()); | |
341 } | |
342 else if (e1.op == TOK.TOKslice) | |
343 { | |
344 e2 = e2.implicitCastTo(sc, e1.type.constOf()); | |
345 } | |
346 else | |
347 { | |
348 e2 = e2.implicitCastTo(sc, e1.type); | |
349 } | |
350 | |
351 /* Look for array operations | |
352 */ | |
353 if (e1.op == TOK.TOKslice && !ismemset && | |
354 (e2.op == TOK.TOKadd || e2.op == TOK.TOKmin || | |
355 e2.op == TOK.TOKmul || e2.op == TOK.TOKdiv || | |
356 e2.op == TOK.TOKmod || e2.op == TOK.TOKxor || | |
357 e2.op == TOK.TOKand || e2.op == TOK.TOKor || | |
358 e2.op == TOK.TOKtilde || e2.op == TOK.TOKneg)) | |
359 { | |
360 type = e1.type; | |
361 return arrayOp(sc); | |
362 } | |
363 | |
364 type = e1.type; | |
365 assert(type); | |
0 | 366 return this; |
367 } | |
368 | |
72 | 369 override Expression checkToBoolean() |
0 | 370 { |
371 assert(false); | |
372 } | |
373 | |
72 | 374 override Expression interpret(InterState istate) |
0 | 375 { |
63 | 376 return interpretAssignCommon(istate, null); |
0 | 377 } |
378 | |
72 | 379 override Identifier opId() |
0 | 380 { |
381 return Id.assign; | |
382 } | |
383 | |
72 | 384 override void buildArrayIdent(OutBuffer buf, Expressions arguments) |
0 | 385 { |
72 | 386 /* Evaluate assign expressions right to left |
387 */ | |
388 e2.buildArrayIdent(buf, arguments); | |
389 e1.buildArrayIdent(buf, arguments); | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
390 buf.writestring("Assign"); |
0 | 391 } |
392 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
393 override Expression buildArrayLoop(Parameters fparams) |
0 | 394 { |
72 | 395 /* Evaluate assign expressions right to left |
396 */ | |
397 Expression ex2 = e2.buildArrayLoop(fparams); | |
398 version (DMDV2) { | |
399 /* Need the cast because: | |
400 * b = c + p[i]; | |
401 * where b is a byte fails because (c + p[i]) is an int | |
402 * which cannot be implicitly cast to byte. | |
403 */ | |
404 ex2 = new CastExp(Loc(0), ex2, e1.type.nextOf()); | |
405 } | |
406 Expression ex1 = e1.buildArrayLoop(fparams); | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
407 auto param = fparams[0]; |
72 | 408 param.storageClass = STCundefined; |
409 Expression e = new AssignExp(Loc(0), ex1, ex2); | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
410 return e; |
0 | 411 } |
412 | |
72 | 413 override elem* toElem(IRState* irs) |
0 | 414 { |
72 | 415 Type t1b; |
416 | |
417 //printf("AssignExp.toElem('%s')\n", toChars()); | |
418 t1b = e1.type.toBasetype(); | |
419 | |
420 // Look for array.length = n | |
421 if (e1.op == TOK.TOKarraylength) | |
422 { | |
423 // Generate: | |
424 // _d_arraysetlength(e2, sizeelem, &ale.e1); | |
425 | |
426 ArrayLengthExp ale = cast(ArrayLengthExp)e1; | |
427 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
428 auto p1 = e2.toElem(irs); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
429 auto p3 = ale.e1.toElem(irs); |
72 | 430 p3 = addressElem(p3, null); |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
431 Type t1 = ale.e1.type.toBasetype(); |
72 | 432 |
433 // call _d_arraysetlengthT(ti, e2, &ale.e1); | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
434 auto p2 = t1.getTypeInfo(null).toElem(irs); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
435 auto ep = el_params(p3, p1, p2, null); // c function |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
436 int r = t1.nextOf().isZeroInit(Loc(0)) ? RTLSYM.RTLSYM_ARRAYSETLENGTHT : RTLSYM.RTLSYM_ARRAYSETLENGTHIT; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
437 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
438 auto e = el_bin(OPER.OPcall, type.totym(), el_var(rtlsym[r]), ep); |
72 | 439 el_setLoc(e, loc); |
440 return e; | |
441 } | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
442 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
443 elem *e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
444 IndexExp ae; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
445 |
72 | 446 // Look for array[]=n |
447 if (e1.op == TOK.TOKslice) | |
448 { | |
449 Type t1 = t1b; | |
450 Type t2 = e2.type.toBasetype(); | |
451 | |
452 // which we do if the 'next' types match | |
453 if (ismemset) | |
454 { | |
455 // Do a memset for array[]=v | |
456 //printf("Lpair %s\n", toChars()); | |
457 SliceExp are = cast(SliceExp)e1; | |
458 elem* elwr; | |
459 elem* eupr; | |
460 elem* n1; | |
461 elem* evalue; | |
462 elem* enbytes; | |
463 elem* elength; | |
464 elem* einit; | |
465 long value; | |
466 Type ta = are.e1.type.toBasetype(); | |
467 Type tb = ta.nextOf().toBasetype(); | |
468 int sz = cast(uint)tb.size(); | |
469 tym_t tym = type.totym(); | |
470 | |
471 n1 = are.e1.toElem(irs); | |
472 elwr = are.lwr ? are.lwr.toElem(irs) : null; | |
473 eupr = are.upr ? are.upr.toElem(irs) : null; | |
474 | |
475 elem* n1x = n1; | |
476 | |
477 // Look for array[]=n | |
478 if (ta.ty == TY.Tsarray) | |
479 { | |
480 TypeSArray ts = cast(TypeSArray)ta; | |
481 n1 = array_toPtr(ta, n1); | |
482 enbytes = ts.dim.toElem(irs); | |
483 n1x = n1; | |
484 n1 = el_same(&n1x); | |
485 einit = resolveLengthVar(are.lengthVar, &n1, ta); | |
486 } | |
487 else if (ta.ty == TY.Tarray) | |
488 { | |
489 n1 = el_same(&n1x); | |
490 einit = resolveLengthVar(are.lengthVar, &n1, ta); | |
491 enbytes = el_copytree(n1); | |
492 n1 = array_toPtr(ta, n1); | |
493 enbytes = el_una(OPER.OP64_32, TYM.TYint, enbytes); | |
494 } | |
495 else if (ta.ty == TY.Tpointer) | |
496 { | |
497 n1 = el_same(&n1x); | |
498 enbytes = el_long(TYM.TYint, -1); // largest possible index | |
499 einit = null; | |
500 } | |
501 | |
502 // Enforce order of evaluation of n1[elwr..eupr] as n1,elwr,eupr | |
503 elem* elwrx = elwr; | |
504 if (elwr) elwr = el_same(&elwrx); | |
505 elem* euprx = eupr; | |
506 if (eupr) eupr = el_same(&euprx); | |
507 | |
508 static if (false) { | |
509 printf("sz = %d\n", sz); | |
510 printf("n1x\n"); | |
511 elem_print(n1x); | |
512 printf("einit\n"); | |
513 elem_print(einit); | |
514 printf("elwrx\n"); | |
515 elem_print(elwrx); | |
516 printf("euprx\n"); | |
517 elem_print(euprx); | |
518 printf("n1\n"); | |
519 elem_print(n1); | |
520 printf("elwr\n"); | |
521 elem_print(elwr); | |
522 printf("eupr\n"); | |
523 elem_print(eupr); | |
524 printf("enbytes\n"); | |
525 elem_print(enbytes); | |
526 } | |
527 einit = el_combine(n1x, einit); | |
528 einit = el_combine(einit, elwrx); | |
529 einit = el_combine(einit, euprx); | |
530 | |
531 evalue = this.e2.toElem(irs); | |
532 | |
533 static if (false) { | |
534 printf("n1\n"); | |
535 elem_print(n1); | |
536 printf("enbytes\n"); | |
537 elem_print(enbytes); | |
538 } | |
539 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
540 if (irs.arrayBoundsCheck() && eupr && ta.ty != TY.Tpointer) |
72 | 541 { |
542 elem *c1; | |
543 elem *c2; | |
544 elem *ea; | |
545 elem *eb; | |
546 elem *enbytesx; | |
547 | |
548 assert(elwr); | |
549 enbytesx = enbytes; | |
550 enbytes = el_same(&enbytesx); | |
551 c1 = el_bin(OPER.OPle, TYM.TYint, el_copytree(eupr), enbytesx); | |
552 c2 = el_bin(OPER.OPle, TYM.TYint, el_copytree(elwr), el_copytree(eupr)); | |
553 c1 = el_bin(OPER.OPandand, TYM.TYint, c1, c2); | |
554 | |
555 // Construct: (c1 || ModuleArray(line)) | |
556 Symbol *sassert; | |
557 | |
558 sassert = irs.blx.module_.toModuleArray(); | |
559 ea = el_bin(OPER.OPcall,TYM.TYvoid,el_var(sassert), el_long(TYM.TYint, loc.linnum)); | |
560 eb = el_bin(OPER.OPoror,TYM.TYvoid,c1,ea); | |
561 einit = el_combine(einit, eb); | |
562 } | |
563 | |
564 if (elwr) | |
565 { | |
566 elem *elwr2; | |
567 | |
568 el_free(enbytes); | |
569 elwr2 = el_copytree(elwr); | |
570 elwr2 = el_bin(OPER.OPmul, TYM.TYint, elwr2, el_long(TYM.TYint, sz)); | |
571 n1 = el_bin(OPER.OPadd, TYM.TYnptr, n1, elwr2); | |
572 enbytes = el_bin(OPER.OPmin, TYM.TYint, eupr, elwr); | |
573 elength = el_copytree(enbytes); | |
574 } | |
575 else | |
576 elength = el_copytree(enbytes); | |
577 | |
578 e = setArray(n1, enbytes, tb, evalue, irs, op); | |
579 Lpair: | |
580 e = el_pair(TYM.TYullong, elength, e); | |
581 Lret2: | |
582 e = el_combine(einit, e); | |
583 //elem_print(e); | |
584 goto Lret; | |
585 } | |
586 ///static if (false) { | |
587 /// else if (e2.op == TOK.TOKadd || e2.op == TOK.TOKmin) | |
588 /// { | |
589 /// /* It's ea[] = eb[] +- ec[] | |
590 /// */ | |
591 /// BinExp e2a = cast(BinExp)e2; | |
592 /// Type t = e2.type.toBasetype().nextOf().toBasetype(); | |
593 /// if (t.ty != TY.Tfloat32 && t.ty != TY.Tfloat64 && t.ty != TY.Tfloat80) | |
594 /// { | |
595 /// e2.error("array add/min for %s not supported", t.toChars()); | |
596 /// return el_long(TYM.TYint, 0); | |
597 /// } | |
598 /// elem* ea = e1.toElem(irs); | |
599 /// ea = array_toDarray(e1.type, ea); | |
600 /// elem* eb = e2a.e1.toElem(irs); | |
601 /// eb = array_toDarray(e2a.e1.type, eb); | |
602 /// elem* ec = e2a.e2.toElem(irs); | |
603 /// ec = array_toDarray(e2a.e2.type, ec); | |
604 /// | |
605 /// int rtl = RTLSYM.RTLSYM_ARRAYASSADDFLOAT; | |
606 /// if (t.ty == Tfloat64) | |
607 /// rtl = RTLSYM.RTLSYM_ARRAYASSADDDOUBLE; | |
608 /// else if (t.ty == Tfloat80) | |
609 /// rtl = RTLSYM.RTLSYM_ARRAYASSADDREAL; | |
610 /// if (e2.op == TOK.TOKmin) | |
611 /// { | |
612 /// rtl = RTLSYM.RTLSYM_ARRAYASSMINFLOAT; | |
613 /// if (t.ty == Tfloat64) | |
614 /// rtl = RTLSYM.RTLSYM_ARRAYASSMINDOUBLE; | |
615 /// else if (t.ty == Tfloat80) | |
616 /// rtl = RTLSYM.RTLSYM_ARRAYASSMINREAL; | |
617 /// } | |
618 /// | |
619 /// /* Set parameters so the order of evaluation is eb, ec, ea | |
620 /// */ | |
621 /// elem* ep = el_params(eb, ec, ea, null); | |
622 /// e = el_bin(OPER.OPcall, type.totym(), el_var(rtlsym[rtl]), ep); | |
623 /// goto Lret; | |
624 /// } | |
625 ///} | |
626 else | |
627 { | |
628 /* It's array1[]=array2[] | |
629 * which is a memcpy | |
630 */ | |
631 elem* ep; | |
632 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
633 auto eto = e1.toElem(irs); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
634 auto efrom = e2.toElem(irs); |
72 | 635 |
636 uint size = cast(uint)t1.nextOf().size(); | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
637 auto esize = el_long(TYM.TYint, size); |
72 | 638 |
639 /* Determine if we need to do postblit | |
640 */ | |
641 int postblit = 0; | |
642 if (needsPostblit(t1)) | |
643 postblit = 1; | |
644 | |
645 assert(e2.type.ty != TY.Tpointer); | |
646 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
647 if (!postblit && !irs.arrayBoundsCheck()) |
72 | 648 { |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
649 auto ex = el_same(&eto); |
72 | 650 |
651 // Determine if elen is a constant | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
652 elem *elen; |
72 | 653 if (eto.Eoper == OPER.OPpair && eto.E1.Eoper == OPER.OPconst) |
654 { | |
655 elen = el_copytree(eto.E1); | |
656 } | |
657 else | |
658 { | |
659 // It's not a constant, so pull it from the dynamic array | |
660 elen = el_una(OPER.OP64_32, TYM.TYint, el_copytree(ex)); | |
661 } | |
662 | |
663 esize = el_bin(OPER.OPmul, TYM.TYint, elen, esize); | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
664 auto epto = array_toPtr(e1.type, ex); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
665 auto epfr = array_toPtr(e2.type, efrom); |
72 | 666 static if (true) { |
667 // memcpy() is faster, so if we can't beat 'em, join 'em | |
668 e = el_params(esize, epfr, epto, null); | |
669 e = el_bin(OPER.OPcall, TYM.TYnptr, el_var(rtlsym[RTLSYM.RTLSYM_MEMCPY]), e); | |
670 } else { | |
671 e = el_bin(OPER.OPmemcpy, TYM.TYnptr, epto, el_param(epfr, esize)); | |
672 } | |
673 e = el_pair(eto.Ety, el_copytree(elen), e); | |
674 e = el_combine(eto, e); | |
675 } | |
676 ///version (DMDV2) { | |
677 else if (postblit && op != TOK.TOKblit) | |
678 { | |
679 /* Generate: | |
680 * _d_arrayassign(ti, efrom, eto) | |
681 * or: | |
682 * _d_arrayctor(ti, efrom, eto) | |
683 */ | |
684 el_free(esize); | |
685 Expression ti = t1.nextOf().toBasetype().getTypeInfo(null); | |
686 ep = el_params(eto, efrom, ti.toElem(irs), null); | |
687 int rtl = (op == TOK.TOKconstruct) ? RTLSYM.RTLSYM_ARRAYCTOR : RTLSYM.RTLSYM_ARRAYASSIGN; | |
688 e = el_bin(OPER.OPcall, type.totym(), el_var(rtlsym[rtl]), ep); | |
689 } | |
690 ///} | |
691 else | |
692 { | |
693 // Generate: | |
694 // _d_arraycopy(eto, efrom, esize) | |
695 | |
696 ep = el_params(eto, efrom, esize, null); | |
697 e = el_bin(OPER.OPcall, type.totym(), el_var(rtlsym[RTLSYM.RTLSYM_ARRAYCOPY]), ep); | |
698 } | |
699 el_setLoc(e, loc); | |
700 return e; | |
701 } | |
702 } | |
703 | |
704 if (e1.op == TOK.TOKindex) | |
705 { | |
706 elem* eb; | |
707 elem* ei; | |
708 elem* ev; | |
709 TY ty; | |
710 Type ta; | |
711 | |
712 ae = cast(IndexExp)e1; | |
713 ta = ae.e1.type.toBasetype(); | |
714 ty = ta.ty; | |
715 } | |
716 | |
717 version (DMDV2) { | |
718 /* Look for reference initializations | |
719 */ | |
720 if (op == TOK.TOKconstruct && e1.op == TOK.TOKvar) | |
721 { | |
722 VarExp ve = cast(VarExp)e1; | |
723 Declaration s = ve.var; | |
724 if (s.storage_class & STC.STCref) | |
725 { | |
726 static if (false) { | |
727 Expression ae = e2.addressOf(null); | |
728 e = ae.toElem(irs); | |
729 } else { | |
730 e = e2.toElem(irs); | |
731 e = addressElem(e, e2.type); | |
732 } | |
733 elem* es = el_var(s.toSymbol()); | |
734 es.Ety = TYM.TYnptr; | |
735 e = el_bin(OPER.OPeq, TYM.TYnptr, es, e); | |
736 // BUG: type is struct, and e2 is TOKint64 | |
737 goto Lret; | |
738 } | |
739 } | |
740 } | |
741 | |
742 static if (true) { | |
743 /* This will work if we can distinguish an assignment from | |
744 * an initialization of the lvalue. It'll work if the latter. | |
745 * If the former, because of aliasing of the return value with | |
746 * function arguments, it'll fail. | |
747 */ | |
748 if (op == TOK.TOKconstruct && e2.op == TOK.TOKcall) | |
749 { | |
750 CallExp ce = cast(CallExp)e2; | |
751 | |
752 Type t = ce.e1.type.toBasetype(); | |
753 if (t.ty == TY.Tfunction) { | |
754 TypeFunction tf = cast(TypeFunction)t; | |
755 if (tf.retStyle() == RET.RETstack) | |
756 { | |
757 elem* ehidden = e1.toElem(irs); | |
758 ehidden = el_una(OPER.OPaddr, TYM.TYnptr, ehidden); | |
759 assert(!irs.ehidden); | |
760 irs.ehidden = ehidden; | |
761 e = e2.toElem(irs); | |
762 goto Lret; | |
763 } | |
764 } | |
765 } | |
766 } | |
767 //printf("test2 %d\n", op); | |
768 //if (op == TOK.TOKconstruct) printf("construct\n"); | |
769 if (t1b.ty == TY.Tstruct) | |
770 { | |
771 elem* eleft = e1.toElem(irs); | |
772 | |
773 if (e2.op == TOK.TOKint64) | |
774 { | |
775 /* Implement: | |
776 * (struct = 0) | |
777 * with: | |
778 * memset(&struct, 0, struct.sizeof) | |
779 */ | |
780 elem* ey = null; | |
781 int sz = cast(int)e1.type.size(); | |
782 StructDeclaration sd = (cast(TypeStruct)t1b).sym; | |
783 if (sd.isnested && op == TOK.TOKconstruct) | |
784 { | |
785 ey = el_una(OPER.OPaddr, TYM.TYnptr, eleft); | |
786 eleft = el_same(&ey); | |
787 ey = setEthis(loc, irs, ey, sd); | |
788 sz = sd.vthis.offset; | |
789 } | |
790 | |
791 elem *el = eleft; | |
792 elem *enbytes = el_long(TYM.TYint, sz); | |
793 elem *evalue = el_long(TYM.TYint, 0); | |
794 | |
795 if (!(sd.isnested && op == TOK.TOKconstruct)) | |
796 el = el_una(OPER.OPaddr, TYM.TYnptr, el); | |
797 | |
798 e = el_param(enbytes, evalue); | |
799 e = el_bin(OPER.OPmemset, TYM.TYnptr,el,e); | |
800 e = el_combine(ey, e); | |
801 el_setLoc(e, loc); | |
802 //e = el_una(OPER.OPind, TYM.TYstruct, e); | |
803 } | |
804 else | |
805 { | |
806 //printf("toElemBin() '%s'\n", toChars()); | |
807 | |
808 tym_t tym = type.totym(); | |
809 | |
810 elem* e1 = eleft; | |
811 elem* ex = e1; | |
812 if (e1.Eoper == OPER.OPind) | |
813 ex = e1.E1; | |
814 | |
815 if (this.e2.op == TOK.TOKstructliteral && ex.Eoper == OPER.OPvar && ex.EV.sp.Voffset == 0) | |
816 { | |
817 StructLiteralExp se = cast(StructLiteralExp)this.e2; | |
818 | |
819 Symbol* symSave = se.sym; | |
820 size_t soffsetSave = se.soffset; | |
821 int fillHolesSave = se.fillHoles; | |
822 | |
823 se.sym = ex.EV.sp.Vsym; | |
824 se.soffset = 0; | |
825 se.fillHoles = (op == TOK.TOKconstruct || op == TOK.TOKblit) ? 1 : 0; | |
826 | |
827 el_free(e1); | |
828 e = this.e2.toElem(irs); | |
829 | |
830 se.sym = symSave; | |
831 se.soffset = soffsetSave; | |
832 se.fillHoles = fillHolesSave; | |
833 } | |
834 else | |
835 { | |
836 elem* e2 = this.e2.toElem(irs); | |
837 e = el_bin(OPER.OPstreq,tym,e1,e2); | |
838 e.Enumbytes = cast(uint)this.e1.type.size(); | |
839 } | |
840 goto Lret; | |
841 } | |
842 } | |
843 else | |
844 e = toElemBin(irs,OPER.OPeq); | |
845 | |
846 return e; | |
847 | |
848 Lret: | |
849 el_setLoc(e,loc); | |
0 | 850 return e; |
851 } | |
852 } | |
853 |