Mercurial > projects > ddmd
annotate dmd/AssignExp.d @ 114:e28b18c23469
added a module dmd.common for commonly used stuff
it currently holds code for consistency checking of predefined versions
also added a VisualD project file
author | Trass3r |
---|---|
date | Wed, 01 Sep 2010 18:21:58 +0200 |
parents | 39648eb578f6 |
children | 1765f3ef917d |
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; | |
8 import dmd.Argument; | |
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 | |
72 | 393 override Expression buildArrayLoop(Arguments 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); | |
407 Argument param = cast(Argument)fparams.data[0]; | |
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 elem* e; |
416 IndexExp ae; | |
417 int r; | |
418 Type t1b; | |
419 | |
420 //printf("AssignExp.toElem('%s')\n", toChars()); | |
421 t1b = e1.type.toBasetype(); | |
422 | |
423 // Look for array.length = n | |
424 if (e1.op == TOK.TOKarraylength) | |
425 { | |
426 // Generate: | |
427 // _d_arraysetlength(e2, sizeelem, &ale.e1); | |
428 | |
429 ArrayLengthExp ale = cast(ArrayLengthExp)e1; | |
430 elem* p1; | |
431 elem* p2; | |
432 elem* p3; | |
433 elem* ep; | |
434 Type t1; | |
435 | |
436 p1 = e2.toElem(irs); | |
437 p3 = ale.e1.toElem(irs); | |
438 p3 = addressElem(p3, null); | |
439 t1 = ale.e1.type.toBasetype(); | |
440 | |
441 static if (true) { | |
442 // call _d_arraysetlengthT(ti, e2, &ale.e1); | |
443 p2 = t1.getTypeInfo(null).toElem(irs); | |
444 ep = el_params(p3, p1, p2, null); // c function | |
445 r = t1.nextOf().isZeroInit(Loc(0)) ? RTLSYM.RTLSYM_ARRAYSETLENGTHT : RTLSYM.RTLSYM_ARRAYSETLENGTHIT; | |
446 } else { | |
447 if (t1.next.isZeroInit()) | |
448 { | |
449 p2 = t1.getTypeInfo(null).toElem(irs); | |
450 ep = el_params(p3, p1, p2, null); // c function | |
451 r = RTLSYM.RTLSYM_ARRAYSETLENGTHT; | |
452 } | |
453 else | |
454 { | |
455 p2 = el_long(TYM.TYint, t1.next.size()); | |
456 ep = el_params(p3, p2, p1, null); // c function | |
457 Expression init = t1.next.defaultInit(); | |
458 ep = el_param(el_long(TYM.TYint, init.type.size()), ep); | |
459 elem* ei = init.toElem(irs); | |
460 ep = el_param(ei, ep); | |
461 r = RTLSYM.RTLSYM_ARRAYSETLENGTH3; | |
462 } | |
463 } | |
464 e = el_bin(OPER.OPcall, type.totym(), el_var(rtlsym[r]), ep); | |
465 el_setLoc(e, loc); | |
466 return e; | |
467 } | |
468 | |
469 // Look for array[]=n | |
470 if (e1.op == TOK.TOKslice) | |
471 { | |
472 Type t1 = t1b; | |
473 Type t2 = e2.type.toBasetype(); | |
474 | |
475 // which we do if the 'next' types match | |
476 if (ismemset) | |
477 { | |
478 // Do a memset for array[]=v | |
479 //printf("Lpair %s\n", toChars()); | |
480 SliceExp are = cast(SliceExp)e1; | |
481 elem* elwr; | |
482 elem* eupr; | |
483 elem* n1; | |
484 elem* evalue; | |
485 elem* enbytes; | |
486 elem* elength; | |
487 elem* einit; | |
488 long value; | |
489 Type ta = are.e1.type.toBasetype(); | |
490 Type tb = ta.nextOf().toBasetype(); | |
491 int sz = cast(uint)tb.size(); | |
492 tym_t tym = type.totym(); | |
493 | |
494 n1 = are.e1.toElem(irs); | |
495 elwr = are.lwr ? are.lwr.toElem(irs) : null; | |
496 eupr = are.upr ? are.upr.toElem(irs) : null; | |
497 | |
498 elem* n1x = n1; | |
499 | |
500 // Look for array[]=n | |
501 if (ta.ty == TY.Tsarray) | |
502 { | |
503 TypeSArray ts = cast(TypeSArray)ta; | |
504 n1 = array_toPtr(ta, n1); | |
505 enbytes = ts.dim.toElem(irs); | |
506 n1x = n1; | |
507 n1 = el_same(&n1x); | |
508 einit = resolveLengthVar(are.lengthVar, &n1, ta); | |
509 } | |
510 else if (ta.ty == TY.Tarray) | |
511 { | |
512 n1 = el_same(&n1x); | |
513 einit = resolveLengthVar(are.lengthVar, &n1, ta); | |
514 enbytes = el_copytree(n1); | |
515 n1 = array_toPtr(ta, n1); | |
516 enbytes = el_una(OPER.OP64_32, TYM.TYint, enbytes); | |
517 } | |
518 else if (ta.ty == TY.Tpointer) | |
519 { | |
520 n1 = el_same(&n1x); | |
521 enbytes = el_long(TYM.TYint, -1); // largest possible index | |
522 einit = null; | |
523 } | |
524 | |
525 // Enforce order of evaluation of n1[elwr..eupr] as n1,elwr,eupr | |
526 elem* elwrx = elwr; | |
527 if (elwr) elwr = el_same(&elwrx); | |
528 elem* euprx = eupr; | |
529 if (eupr) eupr = el_same(&euprx); | |
530 | |
531 static if (false) { | |
532 printf("sz = %d\n", sz); | |
533 printf("n1x\n"); | |
534 elem_print(n1x); | |
535 printf("einit\n"); | |
536 elem_print(einit); | |
537 printf("elwrx\n"); | |
538 elem_print(elwrx); | |
539 printf("euprx\n"); | |
540 elem_print(euprx); | |
541 printf("n1\n"); | |
542 elem_print(n1); | |
543 printf("elwr\n"); | |
544 elem_print(elwr); | |
545 printf("eupr\n"); | |
546 elem_print(eupr); | |
547 printf("enbytes\n"); | |
548 elem_print(enbytes); | |
549 } | |
550 einit = el_combine(n1x, einit); | |
551 einit = el_combine(einit, elwrx); | |
552 einit = el_combine(einit, euprx); | |
553 | |
554 evalue = this.e2.toElem(irs); | |
555 | |
556 static if (false) { | |
557 printf("n1\n"); | |
558 elem_print(n1); | |
559 printf("enbytes\n"); | |
560 elem_print(enbytes); | |
561 } | |
562 | |
563 if (global.params.useArrayBounds && eupr && ta.ty != TY.Tpointer) | |
564 { | |
565 elem *c1; | |
566 elem *c2; | |
567 elem *ea; | |
568 elem *eb; | |
569 elem *enbytesx; | |
570 | |
571 assert(elwr); | |
572 enbytesx = enbytes; | |
573 enbytes = el_same(&enbytesx); | |
574 c1 = el_bin(OPER.OPle, TYM.TYint, el_copytree(eupr), enbytesx); | |
575 c2 = el_bin(OPER.OPle, TYM.TYint, el_copytree(elwr), el_copytree(eupr)); | |
576 c1 = el_bin(OPER.OPandand, TYM.TYint, c1, c2); | |
577 | |
578 // Construct: (c1 || ModuleArray(line)) | |
579 Symbol *sassert; | |
580 | |
581 sassert = irs.blx.module_.toModuleArray(); | |
582 ea = el_bin(OPER.OPcall,TYM.TYvoid,el_var(sassert), el_long(TYM.TYint, loc.linnum)); | |
583 eb = el_bin(OPER.OPoror,TYM.TYvoid,c1,ea); | |
584 einit = el_combine(einit, eb); | |
585 } | |
586 | |
587 if (elwr) | |
588 { | |
589 elem *elwr2; | |
590 | |
591 el_free(enbytes); | |
592 elwr2 = el_copytree(elwr); | |
593 elwr2 = el_bin(OPER.OPmul, TYM.TYint, elwr2, el_long(TYM.TYint, sz)); | |
594 n1 = el_bin(OPER.OPadd, TYM.TYnptr, n1, elwr2); | |
595 enbytes = el_bin(OPER.OPmin, TYM.TYint, eupr, elwr); | |
596 elength = el_copytree(enbytes); | |
597 } | |
598 else | |
599 elength = el_copytree(enbytes); | |
600 | |
601 e = setArray(n1, enbytes, tb, evalue, irs, op); | |
602 Lpair: | |
603 e = el_pair(TYM.TYullong, elength, e); | |
604 Lret2: | |
605 e = el_combine(einit, e); | |
606 //elem_print(e); | |
607 goto Lret; | |
608 } | |
609 ///static if (false) { | |
610 /// else if (e2.op == TOK.TOKadd || e2.op == TOK.TOKmin) | |
611 /// { | |
612 /// /* It's ea[] = eb[] +- ec[] | |
613 /// */ | |
614 /// BinExp e2a = cast(BinExp)e2; | |
615 /// Type t = e2.type.toBasetype().nextOf().toBasetype(); | |
616 /// if (t.ty != TY.Tfloat32 && t.ty != TY.Tfloat64 && t.ty != TY.Tfloat80) | |
617 /// { | |
618 /// e2.error("array add/min for %s not supported", t.toChars()); | |
619 /// return el_long(TYM.TYint, 0); | |
620 /// } | |
621 /// elem* ea = e1.toElem(irs); | |
622 /// ea = array_toDarray(e1.type, ea); | |
623 /// elem* eb = e2a.e1.toElem(irs); | |
624 /// eb = array_toDarray(e2a.e1.type, eb); | |
625 /// elem* ec = e2a.e2.toElem(irs); | |
626 /// ec = array_toDarray(e2a.e2.type, ec); | |
627 /// | |
628 /// int rtl = RTLSYM.RTLSYM_ARRAYASSADDFLOAT; | |
629 /// if (t.ty == Tfloat64) | |
630 /// rtl = RTLSYM.RTLSYM_ARRAYASSADDDOUBLE; | |
631 /// else if (t.ty == Tfloat80) | |
632 /// rtl = RTLSYM.RTLSYM_ARRAYASSADDREAL; | |
633 /// if (e2.op == TOK.TOKmin) | |
634 /// { | |
635 /// rtl = RTLSYM.RTLSYM_ARRAYASSMINFLOAT; | |
636 /// if (t.ty == Tfloat64) | |
637 /// rtl = RTLSYM.RTLSYM_ARRAYASSMINDOUBLE; | |
638 /// else if (t.ty == Tfloat80) | |
639 /// rtl = RTLSYM.RTLSYM_ARRAYASSMINREAL; | |
640 /// } | |
641 /// | |
642 /// /* Set parameters so the order of evaluation is eb, ec, ea | |
643 /// */ | |
644 /// elem* ep = el_params(eb, ec, ea, null); | |
645 /// e = el_bin(OPER.OPcall, type.totym(), el_var(rtlsym[rtl]), ep); | |
646 /// goto Lret; | |
647 /// } | |
648 ///} | |
649 else | |
650 { | |
651 /* It's array1[]=array2[] | |
652 * which is a memcpy | |
653 */ | |
654 elem* eto; | |
655 elem* efrom; | |
656 elem* esize; | |
657 elem* ep; | |
658 | |
659 eto = e1.toElem(irs); | |
660 efrom = e2.toElem(irs); | |
661 | |
662 uint size = cast(uint)t1.nextOf().size(); | |
663 esize = el_long(TYM.TYint, size); | |
664 | |
665 /* Determine if we need to do postblit | |
666 */ | |
667 int postblit = 0; | |
668 if (needsPostblit(t1)) | |
669 postblit = 1; | |
670 | |
671 assert(e2.type.ty != TY.Tpointer); | |
672 | |
673 if (!postblit && !global.params.useArrayBounds) | |
674 { | |
675 elem* epto; | |
676 elem* epfr; | |
677 elem* elen; | |
678 elem* ex; | |
679 | |
680 ex = el_same(&eto); | |
681 | |
682 // Determine if elen is a constant | |
683 if (eto.Eoper == OPER.OPpair && eto.E1.Eoper == OPER.OPconst) | |
684 { | |
685 elen = el_copytree(eto.E1); | |
686 } | |
687 else | |
688 { | |
689 // It's not a constant, so pull it from the dynamic array | |
690 elen = el_una(OPER.OP64_32, TYM.TYint, el_copytree(ex)); | |
691 } | |
692 | |
693 esize = el_bin(OPER.OPmul, TYM.TYint, elen, esize); | |
694 epto = array_toPtr(e1.type, ex); | |
695 epfr = array_toPtr(e2.type, efrom); | |
696 static if (true) { | |
697 // memcpy() is faster, so if we can't beat 'em, join 'em | |
698 e = el_params(esize, epfr, epto, null); | |
699 e = el_bin(OPER.OPcall, TYM.TYnptr, el_var(rtlsym[RTLSYM.RTLSYM_MEMCPY]), e); | |
700 } else { | |
701 e = el_bin(OPER.OPmemcpy, TYM.TYnptr, epto, el_param(epfr, esize)); | |
702 } | |
703 e = el_pair(eto.Ety, el_copytree(elen), e); | |
704 e = el_combine(eto, e); | |
705 } | |
706 ///version (DMDV2) { | |
707 else if (postblit && op != TOK.TOKblit) | |
708 { | |
709 /* Generate: | |
710 * _d_arrayassign(ti, efrom, eto) | |
711 * or: | |
712 * _d_arrayctor(ti, efrom, eto) | |
713 */ | |
714 el_free(esize); | |
715 Expression ti = t1.nextOf().toBasetype().getTypeInfo(null); | |
716 ep = el_params(eto, efrom, ti.toElem(irs), null); | |
717 int rtl = (op == TOK.TOKconstruct) ? RTLSYM.RTLSYM_ARRAYCTOR : RTLSYM.RTLSYM_ARRAYASSIGN; | |
718 e = el_bin(OPER.OPcall, type.totym(), el_var(rtlsym[rtl]), ep); | |
719 } | |
720 ///} | |
721 else | |
722 { | |
723 // Generate: | |
724 // _d_arraycopy(eto, efrom, esize) | |
725 | |
726 ep = el_params(eto, efrom, esize, null); | |
727 e = el_bin(OPER.OPcall, type.totym(), el_var(rtlsym[RTLSYM.RTLSYM_ARRAYCOPY]), ep); | |
728 } | |
729 el_setLoc(e, loc); | |
730 return e; | |
731 } | |
732 } | |
733 | |
734 if (e1.op == TOK.TOKindex) | |
735 { | |
736 elem* eb; | |
737 elem* ei; | |
738 elem* ev; | |
739 TY ty; | |
740 Type ta; | |
741 | |
742 ae = cast(IndexExp)e1; | |
743 ta = ae.e1.type.toBasetype(); | |
744 ty = ta.ty; | |
745 } | |
746 | |
747 version (DMDV2) { | |
748 /* Look for reference initializations | |
749 */ | |
750 if (op == TOK.TOKconstruct && e1.op == TOK.TOKvar) | |
751 { | |
752 VarExp ve = cast(VarExp)e1; | |
753 Declaration s = ve.var; | |
754 if (s.storage_class & STC.STCref) | |
755 { | |
756 static if (false) { | |
757 Expression ae = e2.addressOf(null); | |
758 e = ae.toElem(irs); | |
759 } else { | |
760 e = e2.toElem(irs); | |
761 e = addressElem(e, e2.type); | |
762 } | |
763 elem* es = el_var(s.toSymbol()); | |
764 es.Ety = TYM.TYnptr; | |
765 e = el_bin(OPER.OPeq, TYM.TYnptr, es, e); | |
766 // BUG: type is struct, and e2 is TOKint64 | |
767 goto Lret; | |
768 } | |
769 } | |
770 } | |
771 | |
772 static if (true) { | |
773 /* This will work if we can distinguish an assignment from | |
774 * an initialization of the lvalue. It'll work if the latter. | |
775 * If the former, because of aliasing of the return value with | |
776 * function arguments, it'll fail. | |
777 */ | |
778 if (op == TOK.TOKconstruct && e2.op == TOK.TOKcall) | |
779 { | |
780 CallExp ce = cast(CallExp)e2; | |
781 | |
782 Type t = ce.e1.type.toBasetype(); | |
783 if (t.ty == TY.Tfunction) { | |
784 TypeFunction tf = cast(TypeFunction)t; | |
785 if (tf.retStyle() == RET.RETstack) | |
786 { | |
787 elem* ehidden = e1.toElem(irs); | |
788 ehidden = el_una(OPER.OPaddr, TYM.TYnptr, ehidden); | |
789 assert(!irs.ehidden); | |
790 irs.ehidden = ehidden; | |
791 e = e2.toElem(irs); | |
792 goto Lret; | |
793 } | |
794 } | |
795 } | |
796 } | |
797 //printf("test2 %d\n", op); | |
798 //if (op == TOK.TOKconstruct) printf("construct\n"); | |
799 if (t1b.ty == TY.Tstruct) | |
800 { | |
801 elem* eleft = e1.toElem(irs); | |
802 | |
803 if (e2.op == TOK.TOKint64) | |
804 { | |
805 /* Implement: | |
806 * (struct = 0) | |
807 * with: | |
808 * memset(&struct, 0, struct.sizeof) | |
809 */ | |
810 elem* ey = null; | |
811 int sz = cast(int)e1.type.size(); | |
812 StructDeclaration sd = (cast(TypeStruct)t1b).sym; | |
813 if (sd.isnested && op == TOK.TOKconstruct) | |
814 { | |
815 ey = el_una(OPER.OPaddr, TYM.TYnptr, eleft); | |
816 eleft = el_same(&ey); | |
817 ey = setEthis(loc, irs, ey, sd); | |
818 sz = sd.vthis.offset; | |
819 } | |
820 | |
821 elem *el = eleft; | |
822 elem *enbytes = el_long(TYM.TYint, sz); | |
823 elem *evalue = el_long(TYM.TYint, 0); | |
824 | |
825 if (!(sd.isnested && op == TOK.TOKconstruct)) | |
826 el = el_una(OPER.OPaddr, TYM.TYnptr, el); | |
827 | |
828 e = el_param(enbytes, evalue); | |
829 e = el_bin(OPER.OPmemset, TYM.TYnptr,el,e); | |
830 e = el_combine(ey, e); | |
831 el_setLoc(e, loc); | |
832 //e = el_una(OPER.OPind, TYM.TYstruct, e); | |
833 } | |
834 else | |
835 { | |
836 //printf("toElemBin() '%s'\n", toChars()); | |
837 | |
838 tym_t tym = type.totym(); | |
839 | |
840 elem* e1 = eleft; | |
841 elem* ex = e1; | |
842 if (e1.Eoper == OPER.OPind) | |
843 ex = e1.E1; | |
844 | |
845 if (this.e2.op == TOK.TOKstructliteral && ex.Eoper == OPER.OPvar && ex.EV.sp.Voffset == 0) | |
846 { | |
847 StructLiteralExp se = cast(StructLiteralExp)this.e2; | |
848 | |
849 Symbol* symSave = se.sym; | |
850 size_t soffsetSave = se.soffset; | |
851 int fillHolesSave = se.fillHoles; | |
852 | |
853 se.sym = ex.EV.sp.Vsym; | |
854 se.soffset = 0; | |
855 se.fillHoles = (op == TOK.TOKconstruct || op == TOK.TOKblit) ? 1 : 0; | |
856 | |
857 el_free(e1); | |
858 e = this.e2.toElem(irs); | |
859 | |
860 se.sym = symSave; | |
861 se.soffset = soffsetSave; | |
862 se.fillHoles = fillHolesSave; | |
863 } | |
864 else | |
865 { | |
866 elem* e2 = this.e2.toElem(irs); | |
867 e = el_bin(OPER.OPstreq,tym,e1,e2); | |
868 e.Enumbytes = cast(uint)this.e1.type.size(); | |
869 } | |
870 goto Lret; | |
871 } | |
872 } | |
873 else | |
874 e = toElemBin(irs,OPER.OPeq); | |
875 | |
876 return e; | |
877 | |
878 Lret: | |
879 el_setLoc(e,loc); | |
0 | 880 return e; |
881 } | |
882 } | |
883 |