Mercurial > projects > ldc
annotate dmd/optimize.c @ 1598:20799faa301d
Merged the patch for dmd bugzilla #3620 by request of downs.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Sun, 20 Dec 2009 13:58:08 +0000 |
parents | def7a1d494fd |
children | 0a4be54234d6 |
rev | line source |
---|---|
1587 | 1 |
2 // Compiler implementation of the D programming language | |
3 // Copyright (c) 1999-2009 by Digital Mars | |
4 // All Rights Reserved | |
5 // written by Walter Bright | |
6 // http://www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
11 #include <stdio.h> | |
12 #include <ctype.h> | |
13 #include <assert.h> | |
14 #include <math.h> | |
15 | |
16 #if __DMC__ | |
17 #include <complex.h> | |
18 #endif | |
19 | |
20 #include "lexer.h" | |
21 #include "mtype.h" | |
22 #include "expression.h" | |
23 #include "declaration.h" | |
24 #include "aggregate.h" | |
25 #include "init.h" | |
26 | |
27 | |
28 #ifdef IN_GCC | |
29 #include "d-gcc-real.h" | |
30 | |
31 /* %% fix? */ | |
32 extern "C" bool real_isnan (const real_t *); | |
33 #endif | |
34 | |
35 static real_t zero; // work around DMC bug for now | |
36 | |
37 | |
38 /************************************* | |
39 * If expression is a variable with a const initializer, | |
40 * return that initializer. | |
41 */ | |
42 | |
43 Expression *fromConstInitializer(Expression *e1) | |
44 { | |
45 //printf("fromConstInitializer(%s)\n", e1->toChars()); | |
46 if (e1->op == TOKvar) | |
47 { VarExp *ve = (VarExp *)e1; | |
48 VarDeclaration *v = ve->var->isVarDeclaration(); | |
49 if (v && v->isConst() && v->init) | |
50 { Expression *ei = v->init->toExpression(); | |
51 if (ei && ei->type) | |
52 e1 = ei; | |
53 } | |
54 } | |
55 return e1; | |
56 } | |
57 | |
58 | |
59 Expression *Expression::optimize(int result) | |
60 { | |
61 //printf("Expression::optimize(result = x%x) %s\n", result, toChars()); | |
62 return this; | |
63 } | |
64 | |
65 Expression *VarExp::optimize(int result) | |
66 { | |
67 if (result & WANTinterpret) | |
68 { | |
69 return fromConstInitializer(this); | |
70 } | |
71 return this; | |
72 } | |
73 | |
74 Expression *TupleExp::optimize(int result) | |
75 { | |
76 for (size_t i = 0; i < exps->dim; i++) | |
77 { Expression *e = (Expression *)exps->data[i]; | |
78 | |
79 e = e->optimize(WANTvalue | (result & WANTinterpret)); | |
80 exps->data[i] = (void *)e; | |
81 } | |
82 return this; | |
83 } | |
84 | |
85 Expression *ArrayLiteralExp::optimize(int result) | |
86 { | |
87 if (elements) | |
88 { | |
89 for (size_t i = 0; i < elements->dim; i++) | |
90 { Expression *e = (Expression *)elements->data[i]; | |
91 | |
92 e = e->optimize(WANTvalue | (result & WANTinterpret)); | |
93 elements->data[i] = (void *)e; | |
94 } | |
95 } | |
96 return this; | |
97 } | |
98 | |
99 Expression *AssocArrayLiteralExp::optimize(int result) | |
100 { | |
101 assert(keys->dim == values->dim); | |
102 for (size_t i = 0; i < keys->dim; i++) | |
103 { Expression *e = (Expression *)keys->data[i]; | |
104 | |
105 e = e->optimize(WANTvalue | (result & WANTinterpret)); | |
106 keys->data[i] = (void *)e; | |
107 | |
108 e = (Expression *)values->data[i]; | |
109 e = e->optimize(WANTvalue | (result & WANTinterpret)); | |
110 values->data[i] = (void *)e; | |
111 } | |
112 return this; | |
113 } | |
114 | |
115 Expression *StructLiteralExp::optimize(int result) | |
116 { | |
117 if (elements) | |
118 { | |
119 for (size_t i = 0; i < elements->dim; i++) | |
120 { Expression *e = (Expression *)elements->data[i]; | |
121 if (!e) | |
122 continue; | |
123 e = e->optimize(WANTvalue | (result & WANTinterpret)); | |
124 elements->data[i] = (void *)e; | |
125 } | |
126 } | |
127 return this; | |
128 } | |
129 | |
130 Expression *TypeExp::optimize(int result) | |
131 { | |
132 return this; | |
133 } | |
134 | |
135 Expression *UnaExp::optimize(int result) | |
136 { | |
137 e1 = e1->optimize(result); | |
138 return this; | |
139 } | |
140 | |
141 Expression *NegExp::optimize(int result) | |
142 { Expression *e; | |
143 | |
144 e1 = e1->optimize(result); | |
145 if (e1->isConst() == 1) | |
146 { | |
147 e = Neg(type, e1); | |
148 } | |
149 else | |
150 e = this; | |
151 return e; | |
152 } | |
153 | |
154 Expression *ComExp::optimize(int result) | |
155 { Expression *e; | |
156 | |
157 e1 = e1->optimize(result); | |
158 if (e1->isConst() == 1) | |
159 { | |
160 e = Com(type, e1); | |
161 } | |
162 else | |
163 e = this; | |
164 return e; | |
165 } | |
166 | |
167 Expression *NotExp::optimize(int result) | |
168 { Expression *e; | |
169 | |
170 e1 = e1->optimize(result); | |
171 if (e1->isConst() == 1) | |
172 { | |
173 e = Not(type, e1); | |
174 } | |
175 else | |
176 e = this; | |
177 return e; | |
178 } | |
179 | |
180 Expression *BoolExp::optimize(int result) | |
181 { Expression *e; | |
182 | |
183 e1 = e1->optimize(result); | |
184 if (e1->isConst() == 1) | |
185 { | |
186 e = Bool(type, e1); | |
187 } | |
188 else | |
189 e = this; | |
190 return e; | |
191 } | |
192 | |
193 Expression *AddrExp::optimize(int result) | |
194 { Expression *e; | |
195 | |
196 //printf("AddrExp::optimize(result = %d) %s\n", result, toChars()); | |
197 // never try to interpret: it could change the semantics by turning | |
198 // const p = &s; into an something like const p = &(Struct()); | |
199 e1 = e1->optimize(result & ~WANTinterpret); | |
200 // Convert &*ex to ex | |
201 if (e1->op == TOKstar) | |
202 { Expression *ex; | |
203 | |
204 ex = ((PtrExp *)e1)->e1; | |
205 if (type->equals(ex->type)) | |
206 e = ex; | |
207 else | |
208 { | |
209 e = ex->copy(); | |
210 e->type = type; | |
211 } | |
212 return e; | |
213 } | |
1598
20799faa301d
Merged the patch for dmd bugzilla #3620 by request of downs.
Robert Clipsham <robert@octarineparrot.com>
parents:
1587
diff
changeset
|
214 if (e1->op == TOKdotvar) |
20799faa301d
Merged the patch for dmd bugzilla #3620 by request of downs.
Robert Clipsham <robert@octarineparrot.com>
parents:
1587
diff
changeset
|
215 { DotVarExp *de = (DotVarExp *) e1; |
20799faa301d
Merged the patch for dmd bugzilla #3620 by request of downs.
Robert Clipsham <robert@octarineparrot.com>
parents:
1587
diff
changeset
|
216 if (de->e1->op == TOKvar && de->var->isVarDeclaration()) |
20799faa301d
Merged the patch for dmd bugzilla #3620 by request of downs.
Robert Clipsham <robert@octarineparrot.com>
parents:
1587
diff
changeset
|
217 { |
20799faa301d
Merged the patch for dmd bugzilla #3620 by request of downs.
Robert Clipsham <robert@octarineparrot.com>
parents:
1587
diff
changeset
|
218 e = new SymOffExp(loc, ((VarExp*) de->e1)->var, ((VarDeclaration*) de->var)->offset); |
20799faa301d
Merged the patch for dmd bugzilla #3620 by request of downs.
Robert Clipsham <robert@octarineparrot.com>
parents:
1587
diff
changeset
|
219 e->type = type; |
20799faa301d
Merged the patch for dmd bugzilla #3620 by request of downs.
Robert Clipsham <robert@octarineparrot.com>
parents:
1587
diff
changeset
|
220 return e; |
20799faa301d
Merged the patch for dmd bugzilla #3620 by request of downs.
Robert Clipsham <robert@octarineparrot.com>
parents:
1587
diff
changeset
|
221 } |
20799faa301d
Merged the patch for dmd bugzilla #3620 by request of downs.
Robert Clipsham <robert@octarineparrot.com>
parents:
1587
diff
changeset
|
222 } |
1587 | 223 #if !IN_LLVM |
224 if (e1->op == TOKvar) | |
225 { VarExp *ve = (VarExp *)e1; | |
226 if (!ve->var->isOut() && !ve->var->isRef() && | |
227 !ve->var->isImportedSymbol()) | |
228 { | |
229 e = new SymOffExp(loc, ve->var, 0); | |
230 e->type = type; | |
231 return e; | |
232 } | |
233 } | |
234 if (e1->op == TOKindex) | |
235 { // Convert &array[n] to &array+n | |
236 IndexExp *ae = (IndexExp *)e1; | |
237 | |
238 if (ae->e2->op == TOKint64 && ae->e1->op == TOKvar) | |
239 { | |
240 dinteger_t index = ae->e2->toInteger(); | |
241 VarExp *ve = (VarExp *)ae->e1; | |
242 if (ve->type->ty == Tsarray && ve->type->next->ty != Tbit | |
243 && !ve->var->isImportedSymbol()) | |
244 { | |
245 TypeSArray *ts = (TypeSArray *)ve->type; | |
246 dinteger_t dim = ts->dim->toInteger(); | |
247 if (index < 0 || index >= dim) | |
248 error("array index %jd is out of bounds [0..%jd]", index, dim); | |
249 e = new SymOffExp(loc, ve->var, index * ts->next->size()); | |
250 e->type = type; | |
251 return e; | |
252 } | |
253 } | |
254 } | |
255 #endif | |
256 return this; | |
257 } | |
258 | |
259 Expression *PtrExp::optimize(int result) | |
260 { | |
261 //printf("PtrExp::optimize(result = x%x) %s\n", result, toChars()); | |
262 e1 = e1->optimize(result); | |
263 // Convert *&ex to ex | |
264 if (e1->op == TOKaddress) | |
265 { Expression *e; | |
266 Expression *ex; | |
267 | |
268 ex = ((AddrExp *)e1)->e1; | |
269 if (type->equals(ex->type)) | |
270 e = ex; | |
271 else | |
272 { | |
273 e = ex->copy(); | |
274 e->type = type; | |
275 } | |
276 return e; | |
277 } | |
278 // Constant fold *(&structliteral + offset) | |
279 if (e1->op == TOKadd) | |
280 { | |
281 Expression *e; | |
282 e = Ptr(type, e1); | |
283 if (e != EXP_CANT_INTERPRET) | |
284 return e; | |
285 } | |
286 | |
287 return this; | |
288 } | |
289 | |
290 Expression *DotVarExp::optimize(int result) | |
291 { | |
292 //printf("DotVarExp::optimize(result = x%x) %s\n", result, toChars()); | |
293 e1 = e1->optimize(result); | |
294 | |
295 #if DMDV2 | |
296 if (e1->op == TOKvar) | |
297 { VarExp *ve = (VarExp *)e1; | |
298 VarDeclaration *v = ve->var->isVarDeclaration(); | |
299 Expression *e = expandVar(result, v); | |
300 if (e && e->op == TOKstructliteral) | |
301 { StructLiteralExp *sle = (StructLiteralExp *)e; | |
302 VarDeclaration *vf = var->isVarDeclaration(); | |
303 if (vf) | |
304 { | |
305 e = sle->getField(type, vf->offset); | |
306 if (e && e != EXP_CANT_INTERPRET) | |
307 return e; | |
308 } | |
309 } | |
310 } | |
311 else | |
312 #endif | |
313 if (e1->op == TOKstructliteral) | |
314 { StructLiteralExp *sle = (StructLiteralExp *)e1; | |
315 VarDeclaration *vf = var->isVarDeclaration(); | |
316 if (vf) | |
317 { | |
318 Expression *e = sle->getField(type, vf->offset); | |
319 if (e && e != EXP_CANT_INTERPRET) | |
320 return e; | |
321 } | |
322 } | |
323 | |
324 return this; | |
325 } | |
326 | |
327 Expression *NewExp::optimize(int result) | |
328 { | |
329 if (thisexp) | |
330 thisexp = thisexp->optimize(WANTvalue); | |
331 | |
332 // Optimize parameters | |
333 if (newargs) | |
334 { | |
335 for (size_t i = 0; i < newargs->dim; i++) | |
336 { Expression *e = (Expression *)newargs->data[i]; | |
337 | |
338 e = e->optimize(WANTvalue); | |
339 newargs->data[i] = (void *)e; | |
340 } | |
341 } | |
342 | |
343 if (arguments) | |
344 { | |
345 for (size_t i = 0; i < arguments->dim; i++) | |
346 { Expression *e = (Expression *)arguments->data[i]; | |
347 | |
348 e = e->optimize(WANTvalue); | |
349 arguments->data[i] = (void *)e; | |
350 } | |
351 } | |
352 return this; | |
353 } | |
354 | |
355 Expression *CallExp::optimize(int result) | |
356 { Expression *e = this; | |
357 | |
358 // Optimize parameters | |
359 if (arguments) | |
360 { | |
361 for (size_t i = 0; i < arguments->dim; i++) | |
362 { Expression *e = (Expression *)arguments->data[i]; | |
363 | |
364 e = e->optimize(WANTvalue); | |
365 arguments->data[i] = (void *)e; | |
366 } | |
367 } | |
368 | |
369 e1 = e1->optimize(result); | |
370 if (e1->op == TOKvar && result & WANTinterpret) | |
371 { | |
372 FuncDeclaration *fd = ((VarExp *)e1)->var->isFuncDeclaration(); | |
373 if (fd) | |
374 { | |
375 Expression *eresult = fd->interpret(NULL, arguments); | |
376 if (eresult && eresult != EXP_VOID_INTERPRET) | |
377 e = eresult; | |
378 else if (result & WANTinterpret) | |
379 error("cannot evaluate %s at compile time", toChars()); | |
380 } | |
381 } | |
382 return e; | |
383 } | |
384 | |
385 | |
386 Expression *CastExp::optimize(int result) | |
387 { | |
388 //printf("CastExp::optimize(result = %d) %s\n", result, toChars()); | |
389 //printf("from %s to %s\n", type->toChars(), to->toChars()); | |
390 //printf("from %s\n", type->toChars()); | |
391 //printf("e1->type %s\n", e1->type->toChars()); | |
392 //printf("type = %p\n", type); | |
393 assert(type); | |
394 enum TOK op1 = e1->op; | |
395 | |
396 e1 = e1->optimize(result); | |
397 if (result & WANTinterpret) | |
398 e1 = fromConstInitializer(e1); | |
399 | |
400 if ((e1->op == TOKstring || e1->op == TOKarrayliteral) && | |
401 (type->ty == Tpointer || type->ty == Tarray) && | |
402 type->next->equals(e1->type->next) | |
403 ) | |
404 { | |
405 // make a copy before adjusting type to avoid | |
406 // messing up the type of an existing initializer | |
407 e1 = e1->syntaxCopy(); | |
408 e1->type = type; | |
409 return e1; | |
410 } | |
411 /* The first test here is to prevent infinite loops | |
412 */ | |
413 if (op1 != TOKarrayliteral && e1->op == TOKarrayliteral) | |
414 return e1->castTo(NULL, to); | |
415 if (e1->op == TOKnull && | |
416 (type->ty == Tpointer || type->ty == Tclass)) | |
417 { | |
418 e1->type = type; | |
419 return e1; | |
420 } | |
421 | |
422 if (result & WANTflags && type->ty == Tclass && e1->type->ty == Tclass) | |
423 { | |
424 // See if we can remove an unnecessary cast | |
425 ClassDeclaration *cdfrom; | |
426 ClassDeclaration *cdto; | |
427 int offset; | |
428 | |
429 cdfrom = e1->type->isClassHandle(); | |
430 cdto = type->isClassHandle(); | |
431 if (cdto->isBaseOf(cdfrom, &offset) && offset == 0) | |
432 { | |
433 e1->type = type; | |
434 return e1; | |
435 } | |
436 } | |
437 | |
438 Expression *e; | |
439 | |
440 if (e1->isConst()) | |
441 { | |
442 if (e1->op == TOKsymoff) | |
443 { | |
444 if (type->size() == e1->type->size() && | |
445 type->toBasetype()->ty != Tsarray) | |
446 { | |
447 e1->type = type; | |
448 return e1; | |
449 } | |
450 return this; | |
451 } | |
452 if (to->toBasetype()->ty == Tvoid) | |
453 e = this; | |
454 else | |
455 e = Cast(type, to, e1); | |
456 } | |
457 else | |
458 e = this; | |
459 return e; | |
460 } | |
461 | |
462 Expression *BinExp::optimize(int result) | |
463 { | |
464 //printf("BinExp::optimize(result = %d) %s\n", result, toChars()); | |
465 e1 = e1->optimize(result); | |
466 e2 = e2->optimize(result); | |
467 if (op == TOKshlass || op == TOKshrass || op == TOKushrass) | |
468 { | |
469 if (e2->isConst() == 1) | |
470 { | |
471 dinteger_t i2 = e2->toInteger(); | |
472 d_uns64 sz = e1->type->size() * 8; | |
473 if (i2 < 0 || i2 > sz) | |
474 { error("shift assign by %jd is outside the range 0..%zu", i2, sz); | |
475 e2 = new IntegerExp(0); | |
476 } | |
477 } | |
478 } | |
479 return this; | |
480 } | |
481 | |
482 Expression *AddExp::optimize(int result) | |
483 { Expression *e; | |
484 | |
485 //printf("AddExp::optimize(%s)\n", toChars()); | |
486 e1 = e1->optimize(result); | |
487 e2 = e2->optimize(result); | |
488 if (e1->isConst() && e2->isConst()) | |
489 { | |
490 if (e1->op == TOKsymoff && e2->op == TOKsymoff) | |
491 return this; | |
492 e = Add(type, e1, e2); | |
493 } | |
494 else | |
495 e = this; | |
496 return e; | |
497 } | |
498 | |
499 Expression *MinExp::optimize(int result) | |
500 { Expression *e; | |
501 | |
502 e1 = e1->optimize(result); | |
503 e2 = e2->optimize(result); | |
504 if (e1->isConst() && e2->isConst()) | |
505 { | |
506 if (e2->op == TOKsymoff) | |
507 return this; | |
508 e = Min(type, e1, e2); | |
509 } | |
510 else | |
511 e = this; | |
512 return e; | |
513 } | |
514 | |
515 Expression *MulExp::optimize(int result) | |
516 { Expression *e; | |
517 | |
518 //printf("MulExp::optimize(result = %d) %s\n", result, toChars()); | |
519 e1 = e1->optimize(result); | |
520 e2 = e2->optimize(result); | |
521 if (e1->isConst() == 1 && e2->isConst() == 1) | |
522 { | |
523 e = Mul(type, e1, e2); | |
524 } | |
525 else | |
526 e = this; | |
527 return e; | |
528 } | |
529 | |
530 Expression *DivExp::optimize(int result) | |
531 { Expression *e; | |
532 | |
533 //printf("DivExp::optimize(%s)\n", toChars()); | |
534 e1 = e1->optimize(result); | |
535 e2 = e2->optimize(result); | |
536 if (e1->isConst() == 1 && e2->isConst() == 1) | |
537 { | |
538 e = Div(type, e1, e2); | |
539 } | |
540 else | |
541 e = this; | |
542 return e; | |
543 } | |
544 | |
545 Expression *ModExp::optimize(int result) | |
546 { Expression *e; | |
547 | |
548 e1 = e1->optimize(result); | |
549 e2 = e2->optimize(result); | |
550 if (e1->isConst() == 1 && e2->isConst() == 1) | |
551 { | |
552 e = Mod(type, e1, e2); | |
553 } | |
554 else | |
555 e = this; | |
556 return e; | |
557 } | |
558 | |
559 Expression *shift_optimize(int result, BinExp *e, Expression *(*shift)(Type *, Expression *, Expression *)) | |
560 { Expression *ex = e; | |
561 | |
562 e->e1 = e->e1->optimize(result); | |
563 e->e2 = e->e2->optimize(result); | |
564 if (e->e2->isConst() == 1) | |
565 { | |
566 dinteger_t i2 = e->e2->toInteger(); | |
567 d_uns64 sz = e->e1->type->size() * 8; | |
568 if (i2 < 0 || i2 > sz) | |
569 { e->error("shift by %jd is outside the range 0..%zu", i2, sz); | |
570 e->e2 = new IntegerExp(0); | |
571 } | |
572 if (e->e1->isConst() == 1) | |
573 ex = (*shift)(e->type, e->e1, e->e2); | |
574 } | |
575 return ex; | |
576 } | |
577 | |
578 Expression *ShlExp::optimize(int result) | |
579 { | |
580 //printf("ShlExp::optimize(result = %d) %s\n", result, toChars()); | |
581 return shift_optimize(result, this, Shl); | |
582 } | |
583 | |
584 Expression *ShrExp::optimize(int result) | |
585 { | |
586 //printf("ShrExp::optimize(result = %d) %s\n", result, toChars()); | |
587 return shift_optimize(result, this, Shr); | |
588 } | |
589 | |
590 Expression *UshrExp::optimize(int result) | |
591 { | |
592 //printf("UshrExp::optimize(result = %d) %s\n", result, toChars()); | |
593 return shift_optimize(result, this, Ushr); | |
594 } | |
595 | |
596 Expression *AndExp::optimize(int result) | |
597 { Expression *e; | |
598 | |
599 e1 = e1->optimize(result); | |
600 e2 = e2->optimize(result); | |
601 if (e1->isConst() == 1 && e2->isConst() == 1) | |
602 e = And(type, e1, e2); | |
603 else | |
604 e = this; | |
605 return e; | |
606 } | |
607 | |
608 Expression *OrExp::optimize(int result) | |
609 { Expression *e; | |
610 | |
611 e1 = e1->optimize(result); | |
612 e2 = e2->optimize(result); | |
613 if (e1->isConst() == 1 && e2->isConst() == 1) | |
614 e = Or(type, e1, e2); | |
615 else | |
616 e = this; | |
617 return e; | |
618 } | |
619 | |
620 Expression *XorExp::optimize(int result) | |
621 { Expression *e; | |
622 | |
623 e1 = e1->optimize(result); | |
624 e2 = e2->optimize(result); | |
625 if (e1->isConst() == 1 && e2->isConst() == 1) | |
626 e = Xor(type, e1, e2); | |
627 else | |
628 e = this; | |
629 return e; | |
630 } | |
631 | |
632 Expression *CommaExp::optimize(int result) | |
633 { Expression *e; | |
634 | |
635 //printf("CommaExp::optimize(result = %d) %s\n", result, toChars()); | |
636 e1 = e1->optimize(result & WANTinterpret); | |
637 e2 = e2->optimize(result); | |
638 if (!e1 || e1->op == TOKint64 || e1->op == TOKfloat64 || !e1->checkSideEffect(2)) | |
639 { | |
640 e = e2; | |
641 if (e) | |
642 e->type = type; | |
643 } | |
644 else | |
645 e = this; | |
646 //printf("-CommaExp::optimize(result = %d) %s\n", result, e->toChars()); | |
647 return e; | |
648 } | |
649 | |
650 Expression *ArrayLengthExp::optimize(int result) | |
651 { Expression *e; | |
652 | |
653 //printf("ArrayLengthExp::optimize(result = %d) %s\n", result, toChars()); | |
654 e1 = e1->optimize(WANTvalue | (result & WANTinterpret)); | |
655 e = this; | |
656 if (e1->op == TOKstring || e1->op == TOKarrayliteral || e1->op == TOKassocarrayliteral) | |
657 { | |
658 e = ArrayLength(type, e1); | |
659 } | |
660 return e; | |
661 } | |
662 | |
663 Expression *EqualExp::optimize(int result) | |
664 { Expression *e; | |
665 | |
666 //printf("EqualExp::optimize(result = %x) %s\n", result, toChars()); | |
667 e1 = e1->optimize(WANTvalue | (result & WANTinterpret)); | |
668 e2 = e2->optimize(WANTvalue | (result & WANTinterpret)); | |
669 e = this; | |
670 | |
671 Expression *e1 = fromConstInitializer(this->e1); | |
672 Expression *e2 = fromConstInitializer(this->e2); | |
673 | |
674 e = Equal(op, type, e1, e2); | |
675 if (e == EXP_CANT_INTERPRET) | |
676 e = this; | |
677 return e; | |
678 } | |
679 | |
680 Expression *IdentityExp::optimize(int result) | |
681 { Expression *e; | |
682 | |
683 //printf("IdentityExp::optimize(result = %d) %s\n", result, toChars()); | |
684 e1 = e1->optimize(WANTvalue | (result & WANTinterpret)); | |
685 e2 = e2->optimize(WANTvalue | (result & WANTinterpret)); | |
686 e = this; | |
687 | |
688 if (this->e1->isConst() && this->e2->isConst()) | |
689 { | |
690 e = Identity(op, type, this->e1, this->e2); | |
691 if (e == EXP_CANT_INTERPRET) | |
692 e = this; | |
693 } | |
694 return e; | |
695 } | |
696 | |
697 Expression *IndexExp::optimize(int result) | |
698 { Expression *e; | |
699 | |
700 //printf("IndexExp::optimize(result = %d) %s\n", result, toChars()); | |
701 Expression *e1 = this->e1->optimize(WANTvalue | (result & WANTinterpret)); | |
702 if (result & WANTinterpret) | |
703 e1 = fromConstInitializer(e1); | |
704 e2 = e2->optimize(WANTvalue | (result & WANTinterpret)); | |
705 e = Index(type, e1, e2); | |
706 if (e == EXP_CANT_INTERPRET) | |
707 e = this; | |
708 return e; | |
709 } | |
710 | |
711 Expression *SliceExp::optimize(int result) | |
712 { Expression *e; | |
713 | |
714 //printf("SliceExp::optimize(result = %d) %s\n", result, toChars()); | |
715 e = this; | |
716 e1 = e1->optimize(WANTvalue | (result & WANTinterpret)); | |
717 if (!lwr) | |
718 { if (e1->op == TOKstring) | |
719 { // Convert slice of string literal into dynamic array | |
720 Type *t = e1->type->toBasetype(); | |
721 if (t->next) | |
722 e = e1->castTo(NULL, t->next->arrayOf()); | |
723 } | |
724 return e; | |
725 } | |
726 if (result & WANTinterpret) | |
727 e1 = fromConstInitializer(e1); | |
728 lwr = lwr->optimize(WANTvalue | (result & WANTinterpret)); | |
729 upr = upr->optimize(WANTvalue | (result & WANTinterpret)); | |
730 e = Slice(type, e1, lwr, upr); | |
731 if (e == EXP_CANT_INTERPRET) | |
732 e = this; | |
733 return e; | |
734 } | |
735 | |
736 Expression *AndAndExp::optimize(int result) | |
737 { Expression *e; | |
738 | |
739 //printf("AndAndExp::optimize(%d) %s\n", result, toChars()); | |
740 e1 = e1->optimize(WANTflags | (result & WANTinterpret)); | |
741 e = this; | |
742 if (e1->isBool(FALSE)) | |
743 { | |
744 e = new CommaExp(loc, e1, new IntegerExp(loc, 0, type)); | |
745 e->type = type; | |
746 e = e->optimize(result); | |
747 } | |
748 else | |
749 { | |
750 e2 = e2->optimize(WANTflags | (result & WANTinterpret)); | |
751 if (result && e2->type->toBasetype()->ty == Tvoid && !global.errors) | |
752 error("void has no value"); | |
753 if (e1->isConst()) | |
754 { | |
755 if (e2->isConst()) | |
756 { int n1 = e1->isBool(1); | |
757 int n2 = e2->isBool(1); | |
758 | |
759 e = new IntegerExp(loc, n1 && n2, type); | |
760 } | |
761 else if (e1->isBool(TRUE)) | |
762 e = new BoolExp(loc, e2, type); | |
763 } | |
764 } | |
765 return e; | |
766 } | |
767 | |
768 Expression *OrOrExp::optimize(int result) | |
769 { Expression *e; | |
770 | |
771 e1 = e1->optimize(WANTflags | (result & WANTinterpret)); | |
772 e = this; | |
773 if (e1->isBool(TRUE)) | |
774 { // Replace with (e1, 1) | |
775 e = new CommaExp(loc, e1, new IntegerExp(loc, 1, type)); | |
776 e->type = type; | |
777 e = e->optimize(result); | |
778 } | |
779 else | |
780 { | |
781 e2 = e2->optimize(WANTflags | (result & WANTinterpret)); | |
782 if (result && e2->type->toBasetype()->ty == Tvoid && !global.errors) | |
783 error("void has no value"); | |
784 if (e1->isConst()) | |
785 { | |
786 if (e2->isConst()) | |
787 { int n1 = e1->isBool(1); | |
788 int n2 = e2->isBool(1); | |
789 | |
790 e = new IntegerExp(loc, n1 || n2, type); | |
791 } | |
792 else if (e1->isBool(FALSE)) | |
793 e = new BoolExp(loc, e2, type); | |
794 } | |
795 } | |
796 return e; | |
797 } | |
798 | |
799 Expression *CmpExp::optimize(int result) | |
800 { Expression *e; | |
801 | |
802 //printf("CmpExp::optimize() %s\n", toChars()); | |
803 e1 = e1->optimize(result); | |
804 e2 = e2->optimize(result); | |
805 if (e1->isConst() == 1 && e2->isConst() == 1) | |
806 { | |
807 e = Cmp(op, type, this->e1, this->e2); | |
808 } | |
809 else | |
810 e = this; | |
811 return e; | |
812 } | |
813 | |
814 Expression *CatExp::optimize(int result) | |
815 { Expression *e; | |
816 | |
817 //printf("CatExp::optimize(%d) %s\n", result, toChars()); | |
818 e1 = e1->optimize(result); | |
819 e2 = e2->optimize(result); | |
820 e = Cat(type, e1, e2); | |
821 if (e == EXP_CANT_INTERPRET) | |
822 e = this; | |
823 return e; | |
824 } | |
825 | |
826 | |
827 Expression *CondExp::optimize(int result) | |
828 { Expression *e; | |
829 | |
830 econd = econd->optimize(WANTflags | (result & WANTinterpret)); | |
831 if (econd->isBool(TRUE)) | |
832 e = e1->optimize(result); | |
833 else if (econd->isBool(FALSE)) | |
834 e = e2->optimize(result); | |
835 else | |
836 { e1 = e1->optimize(result); | |
837 e2 = e2->optimize(result); | |
838 e = this; | |
839 } | |
840 return e; | |
841 } | |
842 | |
843 |