0
|
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.IndexExp;
|
|
8 import dmd.CallExp;
|
|
9 import dmd.TypeSArray;
|
|
10 import dmd.StructLiteralExp;
|
|
11 import dmd.ArrayLengthExp;
|
|
12 import dmd.TypeStruct;
|
|
13 import dmd.StructDeclaration;
|
|
14 import dmd.VarExp;
|
|
15 import dmd.TY;
|
|
16 import dmd.SliceExp;
|
|
17 import dmd.CommaExp;
|
|
18 import dmd.ArrayExp;
|
|
19 import dmd.AggregateDeclaration;
|
|
20 import dmd.CondExp;
|
|
21 import dmd.DotVarExp;
|
|
22 import dmd.WANT;
|
|
23 import dmd.Id;
|
|
24 import dmd.TypeClass;
|
|
25 import dmd.OutBuffer;
|
|
26 import dmd.Loc;
|
|
27 import dmd.TupleExp;
|
|
28 import dmd.VarDeclaration;
|
|
29 import dmd.Scope;
|
|
30 import dmd.IRState;
|
|
31 import dmd.ArrayTypes;
|
|
32 import dmd.BinExp;
|
|
33 import dmd.TOK;
|
|
34 import dmd.Global;
|
|
35 import dmd.Declaration;
|
|
36 import dmd.TypeFunction;
|
|
37 import dmd.Type;
|
|
38 import dmd.RET;
|
|
39 import dmd.STC;
|
|
40 import dmd.DotIdExp;
|
|
41
|
|
42 import dmd.backend.Util;
|
|
43 import dmd.backend.Symbol;
|
|
44 import dmd.backend.OPER;
|
|
45 import dmd.backend.TYM;
|
|
46 import dmd.backend.RTLSYM;
|
|
47 import dmd.codegen.Util;
|
|
48 import dmd.expression.Util;
|
|
49
|
|
50 class AssignExp : BinExp
|
|
51 {
|
|
52 int ismemset = 0;
|
|
53
|
|
54 this(Loc loc, Expression e1, Expression e2)
|
|
55 {
|
|
56 super(loc, TOK.TOKassign, AssignExp.sizeof, e1, e2);
|
|
57 }
|
|
58
|
|
59 Expression semantic(Scope sc)
|
|
60 {
|
|
61 Expression e1old = e1;
|
|
62
|
|
63 version (LOGSEMANTIC) {
|
|
64 printf("AssignExp.semantic('%s')\n", toChars());
|
|
65 }
|
|
66 //printf("e1.op = %d, '%s'\n", e1.op, Token.toChars(e1.op));
|
|
67 //printf("e2.op = %d, '%s'\n", e2.op, Token.toChars(e2.op));
|
|
68
|
|
69 if (type)
|
|
70 return this;
|
|
71
|
|
72 if (e2.op == TOK.TOKcomma)
|
|
73 {
|
|
74 /* Rewrite to get rid of the comma from rvalue
|
|
75 */
|
|
76 AssignExp ea = new AssignExp(loc, e1, (cast(CommaExp)e2).e2);
|
|
77 ea.op = op;
|
|
78 Expression e = new CommaExp(loc, (cast(CommaExp)e2).e1, ea);
|
|
79 return e.semantic(sc);
|
|
80 }
|
|
81
|
|
82 /* Look for operator overloading of a[i]=value.
|
|
83 * Do it before semantic() otherwise the a[i] will have been
|
|
84 * converted to a.opIndex() already.
|
|
85 */
|
|
86 if (e1.op == TOK.TOKarray)
|
|
87 {
|
|
88 ArrayExp ae = cast(ArrayExp)e1;
|
|
89 AggregateDeclaration ad;
|
|
90 Identifier id = Id.index;
|
|
91
|
|
92 ae.e1 = ae.e1.semantic(sc);
|
|
93 Type t1 = ae.e1.type.toBasetype();
|
|
94 if (t1.ty == TY.Tstruct)
|
|
95 {
|
|
96 ad = (cast(TypeStruct)t1).sym;
|
|
97 goto L1;
|
|
98 }
|
|
99 else if (t1.ty == TY.Tclass)
|
|
100 {
|
|
101 ad = (cast(TypeClass)t1).sym;
|
|
102 L1:
|
|
103 // Rewrite (a[i] = value) to (a.opIndexAssign(value, i))
|
|
104 if (search_function(ad, Id.indexass))
|
|
105 {
|
|
106 Expression e = new DotIdExp(loc, ae.e1, Id.indexass);
|
|
107 Expressions a = cast(Expressions)ae.arguments.copy();
|
|
108
|
|
109 a.insert(0, cast(void*)e2);
|
|
110 e = new CallExp(loc, e, a);
|
|
111 e = e.semantic(sc);
|
|
112 return e;
|
|
113 }
|
|
114 else
|
|
115 {
|
|
116 // Rewrite (a[i] = value) to (a.opIndex(i, value))
|
|
117 if (search_function(ad, id))
|
|
118 {
|
|
119 Expression e = new DotIdExp(loc, ae.e1, id);
|
|
120
|
|
121 if (1 || !global.params.useDeprecated)
|
|
122 error("operator [] assignment overload with opIndex(i, value) illegal, use opIndexAssign(value, i)");
|
|
123
|
|
124 e = new CallExp(loc, e, cast(Expression)ae.arguments.data[0], e2);
|
|
125 e = e.semantic(sc);
|
|
126 return e;
|
|
127 }
|
|
128 }
|
|
129 }
|
|
130 }
|
|
131 /* Look for operator overloading of a[i..j]=value.
|
|
132 * Do it before semantic() otherwise the a[i..j] will have been
|
|
133 * converted to a.opSlice() already.
|
|
134 */
|
|
135 if (e1.op == TOK.TOKslice)
|
|
136 {
|
|
137 Type t1;
|
|
138 SliceExp ae = cast(SliceExp)e1;
|
|
139 AggregateDeclaration ad;
|
|
140 Identifier id = Id.index;
|
|
141
|
|
142 ae.e1 = ae.e1.semantic(sc);
|
|
143 ae.e1 = resolveProperties(sc, ae.e1);
|
|
144 t1 = ae.e1.type.toBasetype();
|
|
145 if (t1.ty == TY.Tstruct)
|
|
146 {
|
|
147 ad = (cast(TypeStruct)t1).sym;
|
|
148 goto L2;
|
|
149 }
|
|
150 else if (t1.ty == TY.Tclass)
|
|
151 {
|
|
152 ad = (cast(TypeClass)t1).sym;
|
|
153 L2:
|
|
154 // Rewrite (a[i..j] = value) to (a.opIndexAssign(value, i, j))
|
|
155 if (search_function(ad, Id.sliceass))
|
|
156 {
|
|
157 Expression e = new DotIdExp(loc, ae.e1, Id.sliceass);
|
|
158 Expressions a = new Expressions();
|
|
159
|
|
160 a.push(cast(void*)e2);
|
|
161 if (ae.lwr)
|
|
162 {
|
|
163 a.push(cast(void*)ae.lwr);
|
|
164 assert(ae.upr);
|
|
165 a.push(cast(void*)ae.upr);
|
|
166 }
|
|
167 else
|
|
168 assert(!ae.upr);
|
|
169
|
|
170 e = new CallExp(loc, e, a);
|
|
171 e = e.semantic(sc);
|
|
172 return e;
|
|
173 }
|
|
174 }
|
|
175 }
|
|
176
|
|
177 BinExp.semantic(sc);
|
|
178
|
|
179 if (e1.op == TOK.TOKdottd)
|
|
180 {
|
|
181 // Rewrite a.b=e2, when b is a template, as a.b(e2)
|
|
182 Expression e = new CallExp(loc, e1, e2);
|
|
183 e = e.semantic(sc);
|
|
184 return e;
|
|
185 }
|
|
186
|
|
187 e2 = resolveProperties(sc, e2);
|
|
188 assert(e1.type);
|
|
189
|
|
190 /* Rewrite tuple assignment as a tuple of assignments.
|
|
191 */
|
|
192 if (e1.op == TOK.TOKtuple && e2.op == TOK.TOKtuple)
|
|
193 {
|
|
194 TupleExp tup1 = cast(TupleExp)e1;
|
|
195 TupleExp tup2 = cast(TupleExp)e2;
|
|
196 size_t dim = tup1.exps.dim;
|
|
197 if (dim != tup2.exps.dim)
|
|
198 {
|
|
199 error("mismatched tuple lengths, %d and %d", cast(int)dim, cast(int)tup2.exps.dim);
|
|
200 }
|
|
201 else
|
|
202 {
|
|
203 Expressions exps = new Expressions;
|
|
204 exps.setDim(dim);
|
|
205
|
|
206 for (int i = 0; i < dim; i++)
|
|
207 {
|
|
208 Expression ex1 = cast(Expression)tup1.exps.data[i];
|
|
209 Expression ex2 = cast(Expression)tup2.exps.data[i];
|
|
210 exps.data[i] = cast(void*) new AssignExp(loc, ex1, ex2);
|
|
211 }
|
|
212 Expression e = new TupleExp(loc, exps);
|
|
213 e = e.semantic(sc);
|
|
214 return e;
|
|
215 }
|
|
216 }
|
|
217
|
|
218 // Determine if this is an initialization of a reference
|
|
219 int refinit = 0;
|
|
220 if (op == TOK.TOKconstruct && e1.op == TOK.TOKvar)
|
|
221 {
|
|
222 VarExp ve = cast(VarExp)e1;
|
|
223 VarDeclaration v = ve.var.isVarDeclaration();
|
|
224 if (v.storage_class & (STC.STCout | STC.STCref))
|
|
225 refinit = 1;
|
|
226 }
|
|
227
|
|
228 Type t1 = e1.type.toBasetype();
|
|
229
|
|
230 if (t1.ty == TY.Tfunction)
|
|
231 {
|
|
232 // Rewrite f=value to f(value)
|
|
233 Expression e = new CallExp(loc, e1, e2);
|
|
234 e = e.semantic(sc);
|
|
235 return e;
|
|
236 }
|
|
237
|
|
238 /* If it is an assignment from a 'foreign' type,
|
|
239 * check for operator overloading.
|
|
240 */
|
|
241 if (t1.ty == TY.Tstruct)
|
|
242 {
|
|
243 StructDeclaration sd = (cast(TypeStruct)t1).sym;
|
|
244 if (op == TOK.TOKassign)
|
|
245 {
|
|
246 Expression e = op_overload(sc);
|
|
247 if (e)
|
|
248 return e;
|
|
249 }
|
|
250 else if (op == TOK.TOKconstruct && !refinit)
|
|
251 {
|
|
252 Type t2 = e2.type.toBasetype();
|
|
253 if (t2.ty == TY.Tstruct && sd == (cast(TypeStruct)t2).sym && sd.cpctor)
|
|
254 {
|
|
255 /* We have a copy constructor for this
|
|
256 */
|
|
257 if (e2.op == TOK.TOKquestion)
|
|
258 { /* Write as:
|
|
259 * a ? e1 = b : e1 = c;
|
|
260 */
|
|
261 CondExp ec = cast(CondExp)e2;
|
|
262 AssignExp ea1 = new AssignExp(ec.e1.loc, e1, ec.e1);
|
|
263 ea1.op = op;
|
|
264 AssignExp ea2 = new AssignExp(ec.e1.loc, e1, ec.e2);
|
|
265 ea2.op = op;
|
|
266 Expression e = new CondExp(loc, ec.econd, ea1, ea2);
|
|
267 return e.semantic(sc);
|
|
268 }
|
|
269 else if (e2.op == TOK.TOKvar || e2.op == TOK.TOKdotvar || e2.op == TOK.TOKstar || e2.op == TOK.TOKindex)
|
|
270 { /* Write as:
|
|
271 * e1.cpctor(e2);
|
|
272 */
|
|
273 Expression e = new DotVarExp(loc, e1, sd.cpctor, 0);
|
|
274 e = new CallExp(loc, e, e2);
|
|
275 return e.semantic(sc);
|
|
276 }
|
|
277 }
|
|
278 }
|
|
279 }
|
|
280 else if (t1.ty == TY.Tclass)
|
|
281 {
|
|
282 // Disallow assignment operator overloads for same type
|
|
283 if (!e2.type.implicitConvTo(e1.type))
|
|
284 {
|
|
285 Expression e = op_overload(sc);
|
|
286 if (e)
|
|
287 return e;
|
|
288 }
|
|
289 }
|
|
290
|
|
291 if (t1.ty == TY.Tsarray && !refinit)
|
|
292 {
|
|
293 // Convert e1 to e1[]
|
|
294 Expression e = new SliceExp(e1.loc, e1, null, null);
|
|
295 e1 = e.semantic(sc);
|
|
296 t1 = e1.type.toBasetype();
|
|
297 }
|
|
298
|
|
299 e2.rvalue();
|
|
300
|
|
301 if (e1.op == TOK.TOKarraylength)
|
|
302 {
|
|
303 // e1 is not an lvalue, but we let code generator handle it
|
|
304 ArrayLengthExp ale = cast(ArrayLengthExp)e1;
|
|
305 ale.e1 = ale.e1.modifiableLvalue(sc, e1);
|
|
306 }
|
|
307 else if (e1.op == TOK.TOKslice)
|
|
308 {
|
|
309 Type tn = e1.type.nextOf();
|
|
310 if (tn && !tn.isMutable() && op != TOK.TOKconstruct)
|
|
311 error("slice %s is not mutable", e1.toChars());
|
|
312 }
|
|
313 else
|
|
314 {
|
|
315 // Try to do a decent error message with the expression
|
|
316 // before it got constant folded
|
|
317 if (e1.op != TOK.TOKvar)
|
|
318 e1 = e1.optimize(WANT.WANTvalue);
|
|
319
|
|
320 if (op != TOK.TOKconstruct)
|
|
321 e1 = e1.modifiableLvalue(sc, e1old);
|
|
322 }
|
|
323
|
|
324 Type t2 = e2.type;
|
|
325 if (e1.op == TOK.TOKslice && t1.nextOf() && e2.implicitConvTo(t1.nextOf()))
|
|
326 {
|
|
327 // memset
|
|
328 ismemset = 1; // make it easy for back end to tell what this is
|
|
329 e2 = e2.implicitCastTo(sc, t1.nextOf());
|
|
330 }
|
|
331 else if (t1.ty == TY.Tsarray)
|
|
332 {
|
|
333 /* Should have already converted e1 => e1[]
|
|
334 */
|
|
335 assert(op == TOK.TOKconstruct);
|
|
336 //error("cannot assign to static array %s", e1.toChars());
|
|
337 }
|
|
338 else if (e1.op == TOK.TOKslice)
|
|
339 {
|
|
340 e2 = e2.implicitCastTo(sc, e1.type.constOf());
|
|
341 }
|
|
342 else
|
|
343 {
|
|
344 e2 = e2.implicitCastTo(sc, e1.type);
|
|
345 }
|
|
346
|
|
347 /* Look for array operations
|
|
348 */
|
|
349 if (e1.op == TOK.TOKslice && !ismemset &&
|
|
350 (e2.op == TOK.TOKadd || e2.op == TOK.TOKmin ||
|
|
351 e2.op == TOK.TOKmul || e2.op == TOK.TOKdiv ||
|
|
352 e2.op == TOK.TOKmod || e2.op == TOK.TOKxor ||
|
|
353 e2.op == TOK.TOKand || e2.op == TOK.TOKor ||
|
|
354 e2.op == TOK.TOKtilde || e2.op == TOK.TOKneg))
|
|
355 {
|
|
356 type = e1.type;
|
|
357 return arrayOp(sc);
|
|
358 }
|
|
359
|
|
360 type = e1.type;
|
|
361 assert(type);
|
|
362 return this;
|
|
363 }
|
|
364
|
|
365 Expression checkToBoolean()
|
|
366 {
|
|
367 assert(false);
|
|
368 }
|
|
369
|
|
370 Expression interpret(InterState* istate)
|
|
371 {
|
|
372 assert(false);
|
|
373 }
|
|
374
|
|
375 Identifier opId()
|
|
376 {
|
|
377 return Id.assign;
|
|
378 }
|
|
379
|
|
380 void buildArrayIdent(OutBuffer buf, Expressions arguments)
|
|
381 {
|
|
382 assert(false);
|
|
383 }
|
|
384
|
|
385 Expression buildArrayLoop(Arguments fparams)
|
|
386 {
|
|
387 assert(false);
|
|
388 }
|
|
389
|
|
390 elem* toElem(IRState* irs)
|
|
391 {
|
|
392 elem* e;
|
|
393 IndexExp ae;
|
|
394 int r;
|
|
395 Type t1b;
|
|
396
|
|
397 //printf("AssignExp.toElem('%s')\n", toChars());
|
|
398 t1b = e1.type.toBasetype();
|
|
399
|
|
400 // Look for array.length = n
|
|
401 if (e1.op == TOK.TOKarraylength)
|
|
402 {
|
|
403 // Generate:
|
|
404 // _d_arraysetlength(e2, sizeelem, &ale.e1);
|
|
405
|
|
406 ArrayLengthExp ale = cast(ArrayLengthExp)e1;
|
|
407 elem* p1;
|
|
408 elem* p2;
|
|
409 elem* p3;
|
|
410 elem* ep;
|
|
411 Type t1;
|
|
412
|
|
413 p1 = e2.toElem(irs);
|
|
414 p3 = ale.e1.toElem(irs);
|
|
415 p3 = addressElem(p3, null);
|
|
416 t1 = ale.e1.type.toBasetype();
|
|
417
|
|
418 static if (true) {
|
|
419 // call _d_arraysetlengthT(ti, e2, &ale.e1);
|
|
420 p2 = t1.getTypeInfo(null).toElem(irs);
|
|
421 ep = el_params(p3, p1, p2, null); // c function
|
|
422 r = t1.nextOf().isZeroInit(Loc(0)) ? RTLSYM.RTLSYM_ARRAYSETLENGTHT : RTLSYM.RTLSYM_ARRAYSETLENGTHIT;
|
|
423 } else {
|
|
424 if (t1.next.isZeroInit())
|
|
425 {
|
|
426 p2 = t1.getTypeInfo(null).toElem(irs);
|
|
427 ep = el_params(p3, p1, p2, null); // c function
|
|
428 r = RTLSYM.RTLSYM_ARRAYSETLENGTHT;
|
|
429 }
|
|
430 else
|
|
431 {
|
|
432 p2 = el_long(TYM.TYint, t1.next.size());
|
|
433 ep = el_params(p3, p2, p1, null); // c function
|
|
434 Expression init = t1.next.defaultInit();
|
|
435 ep = el_param(el_long(TYM.TYint, init.type.size()), ep);
|
|
436 elem* ei = init.toElem(irs);
|
|
437 ep = el_param(ei, ep);
|
|
438 r = RTLSYM.RTLSYM_ARRAYSETLENGTH3;
|
|
439 }
|
|
440 }
|
|
441 e = el_bin(OPER.OPcall, type.totym(), el_var(rtlsym[r]), ep);
|
|
442 el_setLoc(e, loc);
|
|
443 return e;
|
|
444 }
|
|
445
|
|
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
|
|
540 if (global.params.useArrayBounds && eupr && ta.ty != TY.Tpointer)
|
|
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* eto;
|
|
632 elem* efrom;
|
|
633 elem* esize;
|
|
634 elem* ep;
|
|
635
|
|
636 eto = e1.toElem(irs);
|
|
637 efrom = e2.toElem(irs);
|
|
638
|
|
639 uint size = cast(uint)t1.nextOf().size();
|
|
640 esize = el_long(TYM.TYint, size);
|
|
641
|
|
642 /* Determine if we need to do postblit
|
|
643 */
|
|
644 int postblit = 0;
|
|
645 if (needsPostblit(t1))
|
|
646 postblit = 1;
|
|
647
|
|
648 assert(e2.type.ty != TY.Tpointer);
|
|
649
|
|
650 if (!postblit && !global.params.useArrayBounds)
|
|
651 {
|
|
652 elem* epto;
|
|
653 elem* epfr;
|
|
654 elem* elen;
|
|
655 elem* ex;
|
|
656
|
|
657 ex = el_same(&eto);
|
|
658
|
|
659 // Determine if elen is a constant
|
|
660 if (eto.Eoper == OPER.OPpair && eto.E1.Eoper == OPER.OPconst)
|
|
661 {
|
|
662 elen = el_copytree(eto.E1);
|
|
663 }
|
|
664 else
|
|
665 {
|
|
666 // It's not a constant, so pull it from the dynamic array
|
|
667 elen = el_una(OPER.OP64_32, TYM.TYint, el_copytree(ex));
|
|
668 }
|
|
669
|
|
670 esize = el_bin(OPER.OPmul, TYM.TYint, elen, esize);
|
|
671 epto = array_toPtr(e1.type, ex);
|
|
672 epfr = array_toPtr(e2.type, efrom);
|
|
673 static if (true) {
|
|
674 // memcpy() is faster, so if we can't beat 'em, join 'em
|
|
675 e = el_params(esize, epfr, epto, null);
|
|
676 e = el_bin(OPER.OPcall, TYM.TYnptr, el_var(rtlsym[RTLSYM.RTLSYM_MEMCPY]), e);
|
|
677 } else {
|
|
678 e = el_bin(OPER.OPmemcpy, TYM.TYnptr, epto, el_param(epfr, esize));
|
|
679 }
|
|
680 e = el_pair(eto.Ety, el_copytree(elen), e);
|
|
681 e = el_combine(eto, e);
|
|
682 }
|
|
683 ///version (DMDV2) {
|
|
684 else if (postblit && op != TOK.TOKblit)
|
|
685 {
|
|
686 /* Generate:
|
|
687 * _d_arrayassign(ti, efrom, eto)
|
|
688 * or:
|
|
689 * _d_arrayctor(ti, efrom, eto)
|
|
690 */
|
|
691 el_free(esize);
|
|
692 Expression ti = t1.nextOf().toBasetype().getTypeInfo(null);
|
|
693 ep = el_params(eto, efrom, ti.toElem(irs), null);
|
|
694 int rtl = (op == TOK.TOKconstruct) ? RTLSYM.RTLSYM_ARRAYCTOR : RTLSYM.RTLSYM_ARRAYASSIGN;
|
|
695 e = el_bin(OPER.OPcall, type.totym(), el_var(rtlsym[rtl]), ep);
|
|
696 }
|
|
697 ///}
|
|
698 else
|
|
699 {
|
|
700 // Generate:
|
|
701 // _d_arraycopy(eto, efrom, esize)
|
|
702
|
|
703 ep = el_params(eto, efrom, esize, null);
|
|
704 e = el_bin(OPER.OPcall, type.totym(), el_var(rtlsym[RTLSYM.RTLSYM_ARRAYCOPY]), ep);
|
|
705 }
|
|
706 el_setLoc(e, loc);
|
|
707 return e;
|
|
708 }
|
|
709 }
|
|
710
|
|
711 if (e1.op == TOK.TOKindex)
|
|
712 {
|
|
713 elem* eb;
|
|
714 elem* ei;
|
|
715 elem* ev;
|
|
716 TY ty;
|
|
717 Type ta;
|
|
718
|
|
719 ae = cast(IndexExp)e1;
|
|
720 ta = ae.e1.type.toBasetype();
|
|
721 ty = ta.ty;
|
|
722 }
|
|
723
|
|
724 version (DMDV2) {
|
|
725 /* Look for reference initializations
|
|
726 */
|
|
727 if (op == TOK.TOKconstruct && e1.op == TOK.TOKvar)
|
|
728 {
|
|
729 VarExp ve = cast(VarExp)e1;
|
|
730 Declaration s = ve.var;
|
|
731 if (s.storage_class & STC.STCref)
|
|
732 {
|
|
733 static if (false) {
|
|
734 Expression ae = e2.addressOf(null);
|
|
735 e = ae.toElem(irs);
|
|
736 } else {
|
|
737 e = e2.toElem(irs);
|
|
738 e = addressElem(e, e2.type);
|
|
739 }
|
|
740 elem* es = el_var(s.toSymbol());
|
|
741 es.Ety = TYM.TYnptr;
|
|
742 e = el_bin(OPER.OPeq, TYM.TYnptr, es, e);
|
|
743 // BUG: type is struct, and e2 is TOKint64
|
|
744 goto Lret;
|
|
745 }
|
|
746 }
|
|
747 }
|
|
748
|
|
749 static if (true) {
|
|
750 /* This will work if we can distinguish an assignment from
|
|
751 * an initialization of the lvalue. It'll work if the latter.
|
|
752 * If the former, because of aliasing of the return value with
|
|
753 * function arguments, it'll fail.
|
|
754 */
|
|
755 if (op == TOK.TOKconstruct && e2.op == TOK.TOKcall)
|
|
756 {
|
|
757 CallExp ce = cast(CallExp)e2;
|
|
758
|
|
759 TypeFunction tf = cast(TypeFunction)ce.e1.type.toBasetype();
|
|
760 if (tf.ty == TY.Tfunction && tf.retStyle() == RET.RETstack)
|
|
761 {
|
|
762 elem* ehidden = e1.toElem(irs);
|
|
763 ehidden = el_una(OPER.OPaddr, TYM.TYnptr, ehidden);
|
|
764 assert(!irs.ehidden);
|
|
765 irs.ehidden = ehidden;
|
|
766 e = e2.toElem(irs);
|
|
767 goto Lret;
|
|
768 }
|
|
769 }
|
|
770 }
|
|
771 //printf("test2 %d\n", op);
|
|
772 //if (op == TOK.TOKconstruct) printf("construct\n");
|
|
773 if (t1b.ty == TY.Tstruct)
|
|
774 {
|
|
775 elem* eleft = e1.toElem(irs);
|
|
776
|
|
777 if (e2.op == TOK.TOKint64)
|
|
778 {
|
|
779 /* Implement:
|
|
780 * (struct = 0)
|
|
781 * with:
|
|
782 * memset(&struct, 0, struct.sizeof)
|
|
783 */
|
|
784 elem* ey = null;
|
|
785 int sz = cast(int)e1.type.size();
|
|
786 StructDeclaration sd = (cast(TypeStruct)t1b).sym;
|
|
787 if (sd.isnested && op == TOK.TOKconstruct)
|
|
788 {
|
|
789 ey = el_una(OPER.OPaddr, TYM.TYnptr, eleft);
|
|
790 eleft = el_same(&ey);
|
|
791 ey = setEthis(loc, irs, ey, sd);
|
|
792 sz = sd.vthis.offset;
|
|
793 }
|
|
794
|
|
795 elem *el = eleft;
|
|
796 elem *enbytes = el_long(TYM.TYint, sz);
|
|
797 elem *evalue = el_long(TYM.TYint, 0);
|
|
798
|
|
799 if (!(sd.isnested && op == TOK.TOKconstruct))
|
|
800 el = el_una(OPER.OPaddr, TYM.TYnptr, el);
|
|
801
|
|
802 e = el_param(enbytes, evalue);
|
|
803 e = el_bin(OPER.OPmemset, TYM.TYnptr,el,e);
|
|
804 e = el_combine(ey, e);
|
|
805 el_setLoc(e, loc);
|
|
806 //e = el_una(OPER.OPind, TYM.TYstruct, e);
|
|
807 }
|
|
808 else
|
|
809 {
|
|
810 //printf("toElemBin() '%s'\n", toChars());
|
|
811
|
|
812 tym_t tym = type.totym();
|
|
813
|
|
814 elem* e1 = eleft;
|
|
815 elem* ex = e1;
|
|
816 if (e1.Eoper == OPER.OPind)
|
|
817 ex = e1.E1;
|
|
818
|
|
819 if (this.e2.op == TOK.TOKstructliteral && ex.Eoper == OPER.OPvar && ex.EV.sp.Voffset == 0)
|
|
820 {
|
|
821 StructLiteralExp se = cast(StructLiteralExp)this.e2;
|
|
822
|
|
823 Symbol* symSave = se.sym;
|
|
824 size_t soffsetSave = se.soffset;
|
|
825 int fillHolesSave = se.fillHoles;
|
|
826
|
|
827 se.sym = ex.EV.sp.Vsym;
|
|
828 se.soffset = 0;
|
|
829 se.fillHoles = (op == TOK.TOKconstruct || op == TOK.TOKblit) ? 1 : 0;
|
|
830
|
|
831 el_free(e1);
|
|
832 e = this.e2.toElem(irs);
|
|
833
|
|
834 se.sym = symSave;
|
|
835 se.soffset = soffsetSave;
|
|
836 se.fillHoles = fillHolesSave;
|
|
837 }
|
|
838 else
|
|
839 {
|
|
840 elem* e2 = this.e2.toElem(irs);
|
|
841 e = el_bin(OPER.OPstreq,tym,e1,e2);
|
|
842 e.Enumbytes = cast(uint)this.e1.type.size();
|
|
843 }
|
|
844 goto Lret;
|
|
845 }
|
|
846 }
|
|
847 else
|
|
848 e = toElemBin(irs,OPER.OPeq);
|
|
849
|
|
850 return e;
|
|
851
|
|
852 Lret:
|
|
853 el_setLoc(e,loc);
|
|
854 return e;
|
|
855 }
|
|
856 }
|
|
857
|