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