Mercurial > projects > ldc
annotate dmd/constfold.c @ 700:716684a0d63f
Updated rebuild profile for .o output as default, compiler needs to be fixed still.
Fixed segfault with ' ldc foo.o -offoo '.
author | Tomas Lindquist Olsen <tomas.l.olsen@gmail.com> |
---|---|
date | Mon, 13 Oct 2008 18:19:43 +0200 |
parents | 50383e476c7e |
children | a26b0c5d5942 |
rev | line source |
---|---|
336 | 1 |
2 // Compiler implementation of the D programming language | |
3 // Copyright (c) 1999-2007 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 <stdlib.h> | |
13 #include <assert.h> | |
14 #include <math.h> | |
15 | |
16 #if __DMC__ | |
17 #include <complex.h> | |
18 #endif | |
19 | |
20 #include "mem.h" | |
21 #include "root.h" | |
22 | |
23 #include "mtype.h" | |
24 #include "expression.h" | |
25 #include "aggregate.h" | |
26 #include "declaration.h" | |
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 #define LOG 0 | |
38 | |
39 Expression *expType(Type *type, Expression *e) | |
40 { | |
41 if (type != e->type) | |
42 { | |
43 e = e->copy(); | |
44 e->type = type; | |
45 } | |
46 return e; | |
47 } | |
48 | |
49 /* ================================== isConst() ============================== */ | |
50 | |
51 int Expression::isConst() | |
52 { | |
53 //printf("Expression::isConst(): %s\n", toChars()); | |
54 return 0; | |
55 } | |
56 | |
57 int IntegerExp::isConst() | |
58 { | |
59 return 1; | |
60 } | |
61 | |
62 int RealExp::isConst() | |
63 { | |
64 return 1; | |
65 } | |
66 | |
67 int ComplexExp::isConst() | |
68 { | |
69 return 1; | |
70 } | |
71 | |
72 int SymOffExp::isConst() | |
73 { | |
74 return 2; | |
75 } | |
76 | |
77 /* =============================== constFold() ============================== */ | |
78 | |
79 /* The constFold() functions were redundant with the optimize() ones, | |
80 * and so have been folded in with them. | |
81 */ | |
82 | |
83 /* ========================================================================== */ | |
84 | |
85 Expression *Neg(Type *type, Expression *e1) | |
86 { Expression *e; | |
87 Loc loc = e1->loc; | |
88 | |
89 if (e1->type->isreal()) | |
90 { | |
91 e = new RealExp(loc, -e1->toReal(), type); | |
92 } | |
93 else if (e1->type->isimaginary()) | |
94 { | |
95 e = new RealExp(loc, -e1->toImaginary(), type); | |
96 } | |
97 else if (e1->type->iscomplex()) | |
98 { | |
99 e = new ComplexExp(loc, -e1->toComplex(), type); | |
100 } | |
101 else | |
102 e = new IntegerExp(loc, -e1->toInteger(), type); | |
103 return e; | |
104 } | |
105 | |
106 Expression *Com(Type *type, Expression *e1) | |
107 { Expression *e; | |
108 Loc loc = e1->loc; | |
109 | |
110 e = new IntegerExp(loc, ~e1->toInteger(), type); | |
111 return e; | |
112 } | |
113 | |
114 Expression *Not(Type *type, Expression *e1) | |
115 { Expression *e; | |
116 Loc loc = e1->loc; | |
117 | |
118 e = new IntegerExp(loc, e1->isBool(0), type); | |
119 return e; | |
120 } | |
121 | |
122 Expression *Bool(Type *type, Expression *e1) | |
123 { Expression *e; | |
124 Loc loc = e1->loc; | |
125 | |
126 e = new IntegerExp(loc, e1->isBool(1), type); | |
127 return e; | |
128 } | |
129 | |
130 Expression *Add(Type *type, Expression *e1, Expression *e2) | |
131 { Expression *e; | |
132 Loc loc = e1->loc; | |
133 | |
134 #if LOG | |
135 printf("Add(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); | |
136 #endif | |
137 if (type->isreal()) | |
138 { | |
139 e = new RealExp(loc, e1->toReal() + e2->toReal(), type); | |
140 } | |
141 else if (type->isimaginary()) | |
142 { | |
143 e = new RealExp(loc, e1->toImaginary() + e2->toImaginary(), type); | |
144 } | |
145 else if (type->iscomplex()) | |
146 { | |
147 // This rigamarole is necessary so that -0.0 doesn't get | |
148 // converted to +0.0 by doing an extraneous add with +0.0 | |
149 complex_t c1; | |
150 real_t r1; | |
151 real_t i1; | |
152 | |
153 complex_t c2; | |
154 real_t r2; | |
155 real_t i2; | |
156 | |
157 complex_t v; | |
158 int x; | |
159 | |
160 if (e1->type->isreal()) | |
161 { r1 = e1->toReal(); | |
162 x = 0; | |
163 } | |
164 else if (e1->type->isimaginary()) | |
165 { i1 = e1->toImaginary(); | |
166 x = 3; | |
167 } | |
168 else | |
169 { c1 = e1->toComplex(); | |
170 x = 6; | |
171 } | |
172 | |
173 if (e2->type->isreal()) | |
174 { r2 = e2->toReal(); | |
175 } | |
176 else if (e2->type->isimaginary()) | |
177 { i2 = e2->toImaginary(); | |
178 x += 1; | |
179 } | |
180 else | |
181 { c2 = e2->toComplex(); | |
182 x += 2; | |
183 } | |
184 | |
185 switch (x) | |
186 { | |
187 #if __DMC__ | |
188 case 0+0: v = (complex_t) (r1 + r2); break; | |
189 case 0+1: v = r1 + i2 * I; break; | |
190 case 0+2: v = r1 + c2; break; | |
191 case 3+0: v = i1 * I + r2; break; | |
192 case 3+1: v = (complex_t) ((i1 + i2) * I); break; | |
193 case 3+2: v = i1 * I + c2; break; | |
194 case 6+0: v = c1 + r2; break; | |
195 case 6+1: v = c1 + i2 * I; break; | |
196 case 6+2: v = c1 + c2; break; | |
197 #else | |
198 case 0+0: v = complex_t(r1 + r2, 0); break; | |
199 case 0+1: v = complex_t(r1, i2); break; | |
200 case 0+2: v = complex_t(r1 + creall(c2), cimagl(c2)); break; | |
201 case 3+0: v = complex_t(r2, i1); break; | |
202 case 3+1: v = complex_t(0, i1 + i2); break; | |
203 case 3+2: v = complex_t(creall(c2), i1 + cimagl(c2)); break; | |
204 case 6+0: v = complex_t(creall(c1) + r2, cimagl(c2)); break; | |
205 case 6+1: v = complex_t(creall(c1), cimagl(c1) + i2); break; | |
206 case 6+2: v = c1 + c2; break; | |
207 #endif | |
208 default: assert(0); | |
209 } | |
210 e = new ComplexExp(loc, v, type); | |
211 } | |
212 else if (e1->op == TOKsymoff) | |
213 { | |
214 SymOffExp *soe = (SymOffExp *)e1; | |
215 e = new SymOffExp(loc, soe->var, soe->offset + e2->toInteger()); | |
216 e->type = type; | |
217 } | |
218 else if (e2->op == TOKsymoff) | |
219 { | |
220 SymOffExp *soe = (SymOffExp *)e2; | |
221 e = new SymOffExp(loc, soe->var, soe->offset + e1->toInteger()); | |
222 e->type = type; | |
223 } | |
224 else | |
225 e = new IntegerExp(loc, e1->toInteger() + e2->toInteger(), type); | |
226 return e; | |
227 } | |
228 | |
229 | |
230 Expression *Min(Type *type, Expression *e1, Expression *e2) | |
231 { Expression *e; | |
232 Loc loc = e1->loc; | |
233 | |
234 if (type->isreal()) | |
235 { | |
236 e = new RealExp(loc, e1->toReal() - e2->toReal(), type); | |
237 } | |
238 else if (type->isimaginary()) | |
239 { | |
240 e = new RealExp(loc, e1->toImaginary() - e2->toImaginary(), type); | |
241 } | |
242 else if (type->iscomplex()) | |
243 { | |
244 // This rigamarole is necessary so that -0.0 doesn't get | |
245 // converted to +0.0 by doing an extraneous add with +0.0 | |
246 complex_t c1; | |
247 real_t r1; | |
248 real_t i1; | |
249 | |
250 complex_t c2; | |
251 real_t r2; | |
252 real_t i2; | |
253 | |
254 complex_t v; | |
255 int x; | |
256 | |
257 if (e1->type->isreal()) | |
258 { r1 = e1->toReal(); | |
259 x = 0; | |
260 } | |
261 else if (e1->type->isimaginary()) | |
262 { i1 = e1->toImaginary(); | |
263 x = 3; | |
264 } | |
265 else | |
266 { c1 = e1->toComplex(); | |
267 x = 6; | |
268 } | |
269 | |
270 if (e2->type->isreal()) | |
271 { r2 = e2->toReal(); | |
272 } | |
273 else if (e2->type->isimaginary()) | |
274 { i2 = e2->toImaginary(); | |
275 x += 1; | |
276 } | |
277 else | |
278 { c2 = e2->toComplex(); | |
279 x += 2; | |
280 } | |
281 | |
282 switch (x) | |
283 { | |
284 #if __DMC__ | |
285 case 0+0: v = (complex_t) (r1 - r2); break; | |
286 case 0+1: v = r1 - i2 * I; break; | |
287 case 0+2: v = r1 - c2; break; | |
288 case 3+0: v = i1 * I - r2; break; | |
289 case 3+1: v = (complex_t) ((i1 - i2) * I); break; | |
290 case 3+2: v = i1 * I - c2; break; | |
291 case 6+0: v = c1 - r2; break; | |
292 case 6+1: v = c1 - i2 * I; break; | |
293 case 6+2: v = c1 - c2; break; | |
294 #else | |
295 case 0+0: v = complex_t(r1 - r2, 0); break; | |
296 case 0+1: v = complex_t(r1, -i2); break; | |
297 case 0+2: v = complex_t(r1 - creall(c2), -cimagl(c2)); break; | |
298 case 3+0: v = complex_t(-r2, i1); break; | |
299 case 3+1: v = complex_t(0, i1 - i2); break; | |
300 case 3+2: v = complex_t(-creall(c2), i1 - cimagl(c2)); break; | |
301 case 6+0: v = complex_t(creall(c1) - r2, cimagl(c1)); break; | |
302 case 6+1: v = complex_t(creall(c1), cimagl(c1) - i2); break; | |
303 case 6+2: v = c1 - c2; break; | |
304 #endif | |
305 default: assert(0); | |
306 } | |
307 e = new ComplexExp(loc, v, type); | |
308 } | |
309 else if (e1->op == TOKsymoff) | |
310 { | |
311 SymOffExp *soe = (SymOffExp *)e1; | |
312 e = new SymOffExp(loc, soe->var, soe->offset - e2->toInteger()); | |
313 e->type = type; | |
314 } | |
315 else | |
316 { | |
317 e = new IntegerExp(loc, e1->toInteger() - e2->toInteger(), type); | |
318 } | |
319 return e; | |
320 } | |
321 | |
322 Expression *Mul(Type *type, Expression *e1, Expression *e2) | |
323 { Expression *e; | |
324 Loc loc = e1->loc; | |
325 | |
326 if (type->isfloating()) | |
327 { complex_t c; | |
328 #ifdef IN_GCC | |
329 real_t r; | |
330 #else | |
331 d_float80 r; | |
332 #endif | |
333 | |
334 if (e1->type->isreal()) | |
335 { | |
336 #if __DMC__ | |
337 c = e1->toReal() * e2->toComplex(); | |
338 #else | |
339 r = e1->toReal(); | |
340 c = e2->toComplex(); | |
341 c = complex_t(r * creall(c), r * cimagl(c)); | |
342 #endif | |
343 } | |
344 else if (e1->type->isimaginary()) | |
345 { | |
346 #if __DMC__ | |
347 c = e1->toImaginary() * I * e2->toComplex(); | |
348 #else | |
349 r = e1->toImaginary(); | |
350 c = e2->toComplex(); | |
351 c = complex_t(-r * cimagl(c), r * creall(c)); | |
352 #endif | |
353 } | |
354 else if (e2->type->isreal()) | |
355 { | |
356 #if __DMC__ | |
357 c = e2->toReal() * e1->toComplex(); | |
358 #else | |
359 r = e2->toReal(); | |
360 c = e1->toComplex(); | |
361 c = complex_t(r * creall(c), r * cimagl(c)); | |
362 #endif | |
363 } | |
364 else if (e2->type->isimaginary()) | |
365 { | |
366 #if __DMC__ | |
367 c = e1->toComplex() * e2->toImaginary() * I; | |
368 #else | |
369 r = e2->toImaginary(); | |
370 c = e1->toComplex(); | |
371 c = complex_t(-r * cimagl(c), r * creall(c)); | |
372 #endif | |
373 } | |
374 else | |
375 c = e1->toComplex() * e2->toComplex(); | |
376 | |
377 if (type->isreal()) | |
378 e = new RealExp(loc, creall(c), type); | |
379 else if (type->isimaginary()) | |
380 e = new RealExp(loc, cimagl(c), type); | |
381 else if (type->iscomplex()) | |
382 e = new ComplexExp(loc, c, type); | |
383 else | |
384 assert(0); | |
385 } | |
386 else | |
387 { | |
388 e = new IntegerExp(loc, e1->toInteger() * e2->toInteger(), type); | |
389 } | |
390 return e; | |
391 } | |
392 | |
393 Expression *Div(Type *type, Expression *e1, Expression *e2) | |
394 { Expression *e; | |
395 Loc loc = e1->loc; | |
396 | |
397 if (type->isfloating()) | |
398 { complex_t c; | |
399 #ifdef IN_GCC | |
400 real_t r; | |
401 #else | |
402 d_float80 r; | |
403 #endif | |
404 | |
405 //e1->type->print(); | |
406 //e2->type->print(); | |
407 if (e2->type->isreal()) | |
408 { | |
409 if (e1->type->isreal()) | |
410 { | |
411 e = new RealExp(loc, e1->toReal() / e2->toReal(), type); | |
412 return e; | |
413 } | |
414 #if __DMC__ | |
415 //r = e2->toReal(); | |
416 //c = e1->toComplex(); | |
417 //printf("(%Lg + %Lgi) / %Lg\n", creall(c), cimagl(c), r); | |
418 | |
419 c = e1->toComplex() / e2->toReal(); | |
420 #else | |
421 r = e2->toReal(); | |
422 c = e1->toComplex(); | |
423 c = complex_t(creall(c) / r, cimagl(c) / r); | |
424 #endif | |
425 } | |
426 else if (e2->type->isimaginary()) | |
427 { | |
428 #if __DMC__ | |
429 //r = e2->toImaginary(); | |
430 //c = e1->toComplex(); | |
431 //printf("(%Lg + %Lgi) / %Lgi\n", creall(c), cimagl(c), r); | |
432 | |
433 c = e1->toComplex() / (e2->toImaginary() * I); | |
434 #else | |
435 r = e2->toImaginary(); | |
436 c = e1->toComplex(); | |
437 c = complex_t(cimagl(c) / r, -creall(c) / r); | |
438 #endif | |
439 } | |
440 else | |
441 { | |
442 c = e1->toComplex() / e2->toComplex(); | |
443 } | |
444 | |
445 if (type->isreal()) | |
446 e = new RealExp(loc, creall(c), type); | |
447 else if (type->isimaginary()) | |
448 e = new RealExp(loc, cimagl(c), type); | |
449 else if (type->iscomplex()) | |
450 e = new ComplexExp(loc, c, type); | |
451 else | |
452 assert(0); | |
453 } | |
454 else | |
455 { sinteger_t n1; | |
456 sinteger_t n2; | |
457 sinteger_t n; | |
458 | |
459 n1 = e1->toInteger(); | |
460 n2 = e2->toInteger(); | |
461 if (n2 == 0) | |
462 { e2->error("divide by 0"); | |
463 e2 = new IntegerExp(0, 1, e2->type); | |
464 n2 = 1; | |
465 } | |
466 if (e1->type->isunsigned() || e2->type->isunsigned()) | |
467 n = ((d_uns64) n1) / ((d_uns64) n2); | |
468 else | |
469 n = n1 / n2; | |
470 e = new IntegerExp(loc, n, type); | |
471 } | |
472 return e; | |
473 } | |
474 | |
475 Expression *Mod(Type *type, Expression *e1, Expression *e2) | |
476 { Expression *e; | |
477 Loc loc = e1->loc; | |
478 | |
479 if (type->isfloating()) | |
480 { | |
481 complex_t c; | |
482 | |
483 if (e2->type->isreal()) | |
484 { real_t r2 = e2->toReal(); | |
485 | |
486 #ifdef __DMC__ | |
487 c = fmodl(e1->toReal(), r2) + fmodl(e1->toImaginary(), r2) * I; | |
488 #elif defined(IN_GCC) | |
489 c = complex_t(e1->toReal() % r2, e1->toImaginary() % r2); | |
637
29dc68c949b0
Applied the FreeBSD patch from Ralith, closes ticket #95 , slightly changed but basically the same. Thanx Ralith :)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
490 #elif defined(__FreeBSD__) && __FreeBSD_version < 800000 |
29dc68c949b0
Applied the FreeBSD patch from Ralith, closes ticket #95 , slightly changed but basically the same. Thanx Ralith :)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
491 // freebsd is kinda messed up. the STABLE branch doesn't support C99's fmodl !?! |
29dc68c949b0
Applied the FreeBSD patch from Ralith, closes ticket #95 , slightly changed but basically the same. Thanx Ralith :)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
492 c = complex_t(fmod(e1->toReal(), r2), fmod(e1->toImaginary(), r2)); |
336 | 493 #else |
494 c = complex_t(fmodl(e1->toReal(), r2), fmodl(e1->toImaginary(), r2)); | |
495 #endif | |
496 } | |
497 else if (e2->type->isimaginary()) | |
498 { real_t i2 = e2->toImaginary(); | |
499 | |
500 #ifdef __DMC__ | |
501 c = fmodl(e1->toReal(), i2) + fmodl(e1->toImaginary(), i2) * I; | |
502 #elif defined(IN_GCC) | |
503 c = complex_t(e1->toReal() % i2, e1->toImaginary() % i2); | |
637
29dc68c949b0
Applied the FreeBSD patch from Ralith, closes ticket #95 , slightly changed but basically the same. Thanx Ralith :)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
504 #elif defined(__FreeBSD__) && __FreeBSD_version < 800000 |
29dc68c949b0
Applied the FreeBSD patch from Ralith, closes ticket #95 , slightly changed but basically the same. Thanx Ralith :)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
505 // freebsd is kinda messed up. the STABLE branch doesn't support C99's fmodl !?! |
29dc68c949b0
Applied the FreeBSD patch from Ralith, closes ticket #95 , slightly changed but basically the same. Thanx Ralith :)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
336
diff
changeset
|
506 c = complex_t(fmod(e1->toReal(), i2), fmod(e1->toImaginary(), i2)); |
336 | 507 #else |
508 c = complex_t(fmodl(e1->toReal(), i2), fmodl(e1->toImaginary(), i2)); | |
509 #endif | |
510 } | |
511 else | |
512 assert(0); | |
513 | |
514 if (type->isreal()) | |
515 e = new RealExp(loc, creall(c), type); | |
516 else if (type->isimaginary()) | |
517 e = new RealExp(loc, cimagl(c), type); | |
518 else if (type->iscomplex()) | |
519 e = new ComplexExp(loc, c, type); | |
520 else | |
521 assert(0); | |
522 } | |
523 else | |
524 { sinteger_t n1; | |
525 sinteger_t n2; | |
526 sinteger_t n; | |
527 | |
528 n1 = e1->toInteger(); | |
529 n2 = e2->toInteger(); | |
530 if (n2 == 0) | |
531 { e2->error("divide by 0"); | |
532 e2 = new IntegerExp(0, 1, e2->type); | |
533 n2 = 1; | |
534 } | |
535 if (e1->type->isunsigned() || e2->type->isunsigned()) | |
536 n = ((d_uns64) n1) % ((d_uns64) n2); | |
537 else | |
538 n = n1 % n2; | |
539 e = new IntegerExp(loc, n, type); | |
540 } | |
541 return e; | |
542 } | |
543 | |
544 Expression *Shl(Type *type, Expression *e1, Expression *e2) | |
545 { Expression *e; | |
546 Loc loc = e1->loc; | |
547 | |
548 e = new IntegerExp(loc, e1->toInteger() << e2->toInteger(), type); | |
549 return e; | |
550 } | |
551 | |
552 Expression *Shr(Type *type, Expression *e1, Expression *e2) | |
553 { Expression *e; | |
554 Loc loc = e1->loc; | |
555 unsigned count; | |
556 integer_t value; | |
557 | |
558 value = e1->toInteger(); | |
559 count = e2->toInteger(); | |
560 switch (e1->type->toBasetype()->ty) | |
561 { | |
562 case Tint8: | |
563 value = (d_int8)(value) >> count; | |
564 break; | |
565 | |
566 case Tuns8: | |
567 value = (d_uns8)(value) >> count; | |
568 break; | |
569 | |
570 case Tint16: | |
571 value = (d_int16)(value) >> count; | |
572 break; | |
573 | |
574 case Tuns16: | |
575 value = (d_uns16)(value) >> count; | |
576 break; | |
577 | |
578 case Tint32: | |
579 value = (d_int32)(value) >> count; | |
580 break; | |
581 | |
582 case Tuns32: | |
583 value = (d_uns32)(value) >> count; | |
584 break; | |
585 | |
586 case Tint64: | |
587 value = (d_int64)(value) >> count; | |
588 break; | |
589 | |
590 case Tuns64: | |
591 value = (d_uns64)(value) >> count; | |
592 break; | |
593 | |
594 default: | |
595 assert(0); | |
596 } | |
597 e = new IntegerExp(loc, value, type); | |
598 return e; | |
599 } | |
600 | |
601 Expression *Ushr(Type *type, Expression *e1, Expression *e2) | |
602 { Expression *e; | |
603 Loc loc = e1->loc; | |
604 unsigned count; | |
605 integer_t value; | |
606 | |
607 value = e1->toInteger(); | |
608 count = e2->toInteger(); | |
609 switch (e1->type->toBasetype()->ty) | |
610 { | |
611 case Tint8: | |
612 case Tuns8: | |
613 assert(0); // no way to trigger this | |
614 value = (value & 0xFF) >> count; | |
615 break; | |
616 | |
617 case Tint16: | |
618 case Tuns16: | |
619 assert(0); // no way to trigger this | |
620 value = (value & 0xFFFF) >> count; | |
621 break; | |
622 | |
623 case Tint32: | |
624 case Tuns32: | |
625 value = (value & 0xFFFFFFFF) >> count; | |
626 break; | |
627 | |
628 case Tint64: | |
629 case Tuns64: | |
630 value = (d_uns64)(value) >> count; | |
631 break; | |
632 | |
633 default: | |
634 assert(0); | |
635 } | |
636 e = new IntegerExp(loc, value, type); | |
637 return e; | |
638 } | |
639 | |
640 Expression *And(Type *type, Expression *e1, Expression *e2) | |
641 { Expression *e; | |
642 Loc loc = e1->loc; | |
643 | |
644 e = new IntegerExp(loc, e1->toInteger() & e2->toInteger(), type); | |
645 return e; | |
646 } | |
647 | |
648 Expression *Or(Type *type, Expression *e1, Expression *e2) | |
649 { Expression *e; | |
650 Loc loc = e1->loc; | |
651 | |
652 e = new IntegerExp(loc, e1->toInteger() | e2->toInteger(), type); | |
653 return e; | |
654 } | |
655 | |
656 Expression *Xor(Type *type, Expression *e1, Expression *e2) | |
657 { Expression *e; | |
658 Loc loc = e1->loc; | |
659 | |
660 e = new IntegerExp(loc, e1->toInteger() ^ e2->toInteger(), type); | |
661 return e; | |
662 } | |
663 | |
664 /* Also returns EXP_CANT_INTERPRET if cannot be computed. | |
665 */ | |
666 Expression *Equal(enum TOK op, Type *type, Expression *e1, Expression *e2) | |
667 { Expression *e; | |
668 Loc loc = e1->loc; | |
669 int cmp; | |
670 real_t r1; | |
671 real_t r2; | |
672 | |
673 //printf("Equal(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); | |
674 | |
675 assert(op == TOKequal || op == TOKnotequal); | |
676 | |
677 if (e1->op == TOKnull) | |
678 { | |
679 if (e2->op == TOKnull) | |
680 cmp = 1; | |
681 else if (e2->op == TOKstring) | |
682 { StringExp *es2 = (StringExp *)e2; | |
683 cmp = (0 == es2->len); | |
684 } | |
685 else if (e2->op == TOKarrayliteral) | |
686 { ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2; | |
687 cmp = !es2->elements || (0 == es2->elements->dim); | |
688 } | |
689 else | |
690 return EXP_CANT_INTERPRET; | |
691 } | |
692 else if (e2->op == TOKnull) | |
693 { | |
694 if (e1->op == TOKstring) | |
695 { StringExp *es1 = (StringExp *)e1; | |
696 cmp = (0 == es1->len); | |
697 } | |
698 else if (e1->op == TOKarrayliteral) | |
699 { ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1; | |
700 cmp = !es1->elements || (0 == es1->elements->dim); | |
701 } | |
702 else | |
703 return EXP_CANT_INTERPRET; | |
704 } | |
705 else if (e1->op == TOKstring && e2->op == TOKstring) | |
706 { StringExp *es1 = (StringExp *)e1; | |
707 StringExp *es2 = (StringExp *)e2; | |
708 | |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
709 if (es1->sz != es2->sz) |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
710 { |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
711 assert(global.errors); |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
712 return EXP_CANT_INTERPRET; |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
713 } |
336 | 714 if (es1->len == es2->len && |
715 memcmp(es1->string, es2->string, es1->sz * es1->len) == 0) | |
716 cmp = 1; | |
717 else | |
718 cmp = 0; | |
719 } | |
720 else if (e1->op == TOKarrayliteral && e2->op == TOKarrayliteral) | |
721 { ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1; | |
722 ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2; | |
723 | |
724 if ((!es1->elements || !es1->elements->dim) && | |
725 (!es2->elements || !es2->elements->dim)) | |
726 cmp = 1; // both arrays are empty | |
727 else if (!es1->elements || !es2->elements) | |
728 cmp = 0; | |
729 else if (es1->elements->dim != es2->elements->dim) | |
730 cmp = 0; | |
731 else | |
732 { | |
733 for (size_t i = 0; i < es1->elements->dim; i++) | |
734 { Expression *ee1 = (Expression *)es1->elements->data[i]; | |
735 Expression *ee2 = (Expression *)es2->elements->data[i]; | |
736 | |
737 Expression *v = Equal(TOKequal, Type::tint32, ee1, ee2); | |
738 if (v == EXP_CANT_INTERPRET) | |
739 return EXP_CANT_INTERPRET; | |
740 cmp = v->toInteger(); | |
741 if (cmp == 0) | |
742 break; | |
743 } | |
744 } | |
745 } | |
746 else if (e1->op == TOKarrayliteral && e2->op == TOKstring) | |
747 { // Swap operands and use common code | |
748 Expression *e = e1; | |
749 e1 = e2; | |
750 e2 = e; | |
751 goto Lsa; | |
752 } | |
753 else if (e1->op == TOKstring && e2->op == TOKarrayliteral) | |
754 { | |
755 Lsa: | |
756 StringExp *es1 = (StringExp *)e1; | |
757 ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2; | |
758 size_t dim1 = es1->len; | |
759 size_t dim2 = es2->elements ? es2->elements->dim : 0; | |
760 if (dim1 != dim2) | |
761 cmp = 0; | |
762 else | |
763 { | |
764 for (size_t i = 0; i < dim1; i++) | |
765 { | |
766 uinteger_t c = es1->charAt(i); | |
767 Expression *ee2 = (Expression *)es2->elements->data[i]; | |
768 if (ee2->isConst() != 1) | |
769 return EXP_CANT_INTERPRET; | |
770 cmp = (c == ee2->toInteger()); | |
771 if (cmp == 0) | |
772 break; | |
773 } | |
774 } | |
775 } | |
776 else if (e1->op == TOKstructliteral && e2->op == TOKstructliteral) | |
777 { StructLiteralExp *es1 = (StructLiteralExp *)e1; | |
778 StructLiteralExp *es2 = (StructLiteralExp *)e2; | |
779 | |
780 if (es1->sd != es2->sd) | |
781 cmp = 0; | |
782 else if ((!es1->elements || !es1->elements->dim) && | |
783 (!es2->elements || !es2->elements->dim)) | |
784 cmp = 1; // both arrays are empty | |
785 else if (!es1->elements || !es2->elements) | |
786 cmp = 0; | |
787 else if (es1->elements->dim != es2->elements->dim) | |
788 cmp = 0; | |
789 else | |
790 { | |
791 cmp = 1; | |
792 for (size_t i = 0; i < es1->elements->dim; i++) | |
793 { Expression *ee1 = (Expression *)es1->elements->data[i]; | |
794 Expression *ee2 = (Expression *)es2->elements->data[i]; | |
795 | |
796 if (ee1 == ee2) | |
797 continue; | |
798 if (!ee1 || !ee2) | |
799 { cmp = 0; | |
800 break; | |
801 } | |
802 Expression *v = Equal(TOKequal, Type::tint32, ee1, ee2); | |
803 if (v == EXP_CANT_INTERPRET) | |
804 return EXP_CANT_INTERPRET; | |
805 cmp = v->toInteger(); | |
806 if (cmp == 0) | |
807 break; | |
808 } | |
809 } | |
810 } | |
811 #if 0 // Should handle this | |
812 else if (e1->op == TOKarrayliteral && e2->op == TOKstring) | |
813 { | |
814 } | |
815 #endif | |
816 else if (e1->isConst() != 1 || e2->isConst() != 1) | |
817 return EXP_CANT_INTERPRET; | |
818 else if (e1->type->isreal()) | |
819 { | |
820 r1 = e1->toReal(); | |
821 r2 = e2->toReal(); | |
822 goto L1; | |
823 } | |
824 else if (e1->type->isimaginary()) | |
825 { | |
826 r1 = e1->toImaginary(); | |
827 r2 = e2->toImaginary(); | |
828 L1: | |
829 #if __DMC__ | |
830 cmp = (r1 == r2); | |
831 #else | |
832 if (isnan(r1) || isnan(r2)) // if unordered | |
833 { | |
834 cmp = 0; | |
835 } | |
836 else | |
837 { | |
838 cmp = (r1 == r2); | |
839 } | |
840 #endif | |
841 } | |
842 else if (e1->type->iscomplex()) | |
843 { | |
844 cmp = e1->toComplex() == e2->toComplex(); | |
845 } | |
846 else if (e1->type->isintegral()) | |
847 { | |
848 cmp = (e1->toInteger() == e2->toInteger()); | |
849 } | |
850 else | |
851 return EXP_CANT_INTERPRET; | |
852 if (op == TOKnotequal) | |
853 cmp ^= 1; | |
854 e = new IntegerExp(loc, cmp, type); | |
855 return e; | |
856 } | |
857 | |
858 Expression *Identity(enum TOK op, Type *type, Expression *e1, Expression *e2) | |
859 { Expression *e; | |
860 Loc loc = e1->loc; | |
861 int cmp; | |
862 | |
863 if (e1->op == TOKnull && e2->op == TOKnull) | |
864 { | |
865 cmp = 1; | |
866 } | |
867 else if (e1->op == TOKsymoff && e2->op == TOKsymoff) | |
868 { | |
869 SymOffExp *es1 = (SymOffExp *)e1; | |
870 SymOffExp *es2 = (SymOffExp *)e2; | |
871 | |
872 cmp = (es1->var == es2->var && es1->offset == es2->offset); | |
873 } | |
874 else if (e1->isConst() == 1 && e2->isConst() == 1) | |
875 return Equal((op == TOKidentity) ? TOKequal : TOKnotequal, | |
876 type, e1, e2); | |
877 else | |
878 assert(0); | |
879 if (op == TOKnotidentity) | |
880 cmp ^= 1; | |
881 return new IntegerExp(loc, cmp, type); | |
882 } | |
883 | |
884 | |
885 Expression *Cmp(enum TOK op, Type *type, Expression *e1, Expression *e2) | |
886 { Expression *e; | |
887 Loc loc = e1->loc; | |
888 integer_t n; | |
889 real_t r1; | |
890 real_t r2; | |
891 | |
892 if (e1->type->isreal()) | |
893 { | |
894 r1 = e1->toReal(); | |
895 r2 = e2->toReal(); | |
896 goto L1; | |
897 } | |
898 else if (e1->type->isimaginary()) | |
899 { | |
900 r1 = e1->toImaginary(); | |
901 r2 = e2->toImaginary(); | |
902 L1: | |
903 #if __DMC__ | |
904 // DMC is the only compiler I know of that handles NAN arguments | |
905 // correctly in comparisons. | |
906 switch (op) | |
907 { | |
908 case TOKlt: n = r1 < r2; break; | |
909 case TOKle: n = r1 <= r2; break; | |
910 case TOKgt: n = r1 > r2; break; | |
911 case TOKge: n = r1 >= r2; break; | |
912 | |
913 case TOKleg: n = r1 <>= r2; break; | |
914 case TOKlg: n = r1 <> r2; break; | |
915 case TOKunord: n = r1 !<>= r2; break; | |
916 case TOKue: n = r1 !<> r2; break; | |
917 case TOKug: n = r1 !<= r2; break; | |
918 case TOKuge: n = r1 !< r2; break; | |
919 case TOKul: n = r1 !>= r2; break; | |
920 case TOKule: n = r1 !> r2; break; | |
921 | |
922 default: | |
923 assert(0); | |
924 } | |
925 #else | |
926 // Don't rely on compiler, handle NAN arguments separately | |
927 #if IN_GCC | |
928 if (real_isnan(&r1) || real_isnan(&r2)) // if unordered | |
929 #else | |
930 if (isnan(r1) || isnan(r2)) // if unordered | |
931 #endif | |
932 { | |
933 switch (op) | |
934 { | |
935 case TOKlt: n = 0; break; | |
936 case TOKle: n = 0; break; | |
937 case TOKgt: n = 0; break; | |
938 case TOKge: n = 0; break; | |
939 | |
940 case TOKleg: n = 0; break; | |
941 case TOKlg: n = 0; break; | |
942 case TOKunord: n = 1; break; | |
943 case TOKue: n = 1; break; | |
944 case TOKug: n = 1; break; | |
945 case TOKuge: n = 1; break; | |
946 case TOKul: n = 1; break; | |
947 case TOKule: n = 1; break; | |
948 | |
949 default: | |
950 assert(0); | |
951 } | |
952 } | |
953 else | |
954 { | |
955 switch (op) | |
956 { | |
957 case TOKlt: n = r1 < r2; break; | |
958 case TOKle: n = r1 <= r2; break; | |
959 case TOKgt: n = r1 > r2; break; | |
960 case TOKge: n = r1 >= r2; break; | |
961 | |
962 case TOKleg: n = 1; break; | |
963 case TOKlg: n = r1 != r2; break; | |
964 case TOKunord: n = 0; break; | |
965 case TOKue: n = r1 == r2; break; | |
966 case TOKug: n = r1 > r2; break; | |
967 case TOKuge: n = r1 >= r2; break; | |
968 case TOKul: n = r1 < r2; break; | |
969 case TOKule: n = r1 <= r2; break; | |
970 | |
971 default: | |
972 assert(0); | |
973 } | |
974 } | |
975 #endif | |
976 } | |
977 else if (e1->type->iscomplex()) | |
978 { | |
979 assert(0); | |
980 } | |
981 else | |
982 { sinteger_t n1; | |
983 sinteger_t n2; | |
984 | |
985 n1 = e1->toInteger(); | |
986 n2 = e2->toInteger(); | |
987 if (e1->type->isunsigned() || e2->type->isunsigned()) | |
988 { | |
989 switch (op) | |
990 { | |
991 case TOKlt: n = ((d_uns64) n1) < ((d_uns64) n2); break; | |
992 case TOKle: n = ((d_uns64) n1) <= ((d_uns64) n2); break; | |
993 case TOKgt: n = ((d_uns64) n1) > ((d_uns64) n2); break; | |
994 case TOKge: n = ((d_uns64) n1) >= ((d_uns64) n2); break; | |
995 | |
996 case TOKleg: n = 1; break; | |
997 case TOKlg: n = ((d_uns64) n1) != ((d_uns64) n2); break; | |
998 case TOKunord: n = 0; break; | |
999 case TOKue: n = ((d_uns64) n1) == ((d_uns64) n2); break; | |
1000 case TOKug: n = ((d_uns64) n1) > ((d_uns64) n2); break; | |
1001 case TOKuge: n = ((d_uns64) n1) >= ((d_uns64) n2); break; | |
1002 case TOKul: n = ((d_uns64) n1) < ((d_uns64) n2); break; | |
1003 case TOKule: n = ((d_uns64) n1) <= ((d_uns64) n2); break; | |
1004 | |
1005 default: | |
1006 assert(0); | |
1007 } | |
1008 } | |
1009 else | |
1010 { | |
1011 switch (op) | |
1012 { | |
1013 case TOKlt: n = n1 < n2; break; | |
1014 case TOKle: n = n1 <= n2; break; | |
1015 case TOKgt: n = n1 > n2; break; | |
1016 case TOKge: n = n1 >= n2; break; | |
1017 | |
1018 case TOKleg: n = 1; break; | |
1019 case TOKlg: n = n1 != n2; break; | |
1020 case TOKunord: n = 0; break; | |
1021 case TOKue: n = n1 == n2; break; | |
1022 case TOKug: n = n1 > n2; break; | |
1023 case TOKuge: n = n1 >= n2; break; | |
1024 case TOKul: n = n1 < n2; break; | |
1025 case TOKule: n = n1 <= n2; break; | |
1026 | |
1027 default: | |
1028 assert(0); | |
1029 } | |
1030 } | |
1031 } | |
1032 e = new IntegerExp(loc, n, type); | |
1033 return e; | |
1034 } | |
1035 | |
1036 /* Also returns EXP_CANT_INTERPRET if cannot be computed. | |
1037 * to: type to cast to | |
1038 * type: type to paint the result | |
1039 */ | |
1040 | |
1041 Expression *Cast(Type *type, Type *to, Expression *e1) | |
1042 { Expression *e = EXP_CANT_INTERPRET; | |
1043 Loc loc = e1->loc; | |
1044 | |
1045 //printf("Cast(type = %s, to = %s, e1 = %s)\n", type->toChars(), to->toChars(), e1->toChars()); | |
1046 //printf("e1->type = %s\n", e1->type->toChars()); | |
1047 if (type->equals(e1->type) && to->equals(type)) | |
1048 return e1; | |
1049 | |
1050 if (e1->isConst() != 1) | |
1051 return EXP_CANT_INTERPRET; | |
1052 | |
1053 Type *tb = to->toBasetype(); | |
1054 if (tb->ty == Tbool) | |
1055 e = new IntegerExp(loc, e1->toInteger() != 0, type); | |
1056 else if (type->isintegral()) | |
1057 { | |
1058 if (e1->type->isfloating()) | |
1059 { integer_t result; | |
1060 real_t r = e1->toReal(); | |
1061 | |
1062 switch (type->toBasetype()->ty) | |
1063 { | |
1064 case Tint8: result = (d_int8)r; break; | |
1065 case Tchar: | |
1066 case Tuns8: result = (d_uns8)r; break; | |
1067 case Tint16: result = (d_int16)r; break; | |
1068 case Twchar: | |
1069 case Tuns16: result = (d_uns16)r; break; | |
1070 case Tint32: result = (d_int32)r; break; | |
1071 case Tdchar: | |
1072 case Tuns32: result = (d_uns32)r; break; | |
1073 case Tint64: result = (d_int64)r; break; | |
1074 case Tuns64: result = (d_uns64)r; break; | |
1075 default: | |
1076 assert(0); | |
1077 } | |
1078 | |
1079 e = new IntegerExp(loc, result, type); | |
1080 } | |
1081 else if (type->isunsigned()) | |
1082 e = new IntegerExp(loc, e1->toUInteger(), type); | |
1083 else | |
1084 e = new IntegerExp(loc, e1->toInteger(), type); | |
1085 } | |
1086 else if (tb->isreal()) | |
1087 { real_t value = e1->toReal(); | |
1088 | |
1089 e = new RealExp(loc, value, type); | |
1090 } | |
1091 else if (tb->isimaginary()) | |
1092 { real_t value = e1->toImaginary(); | |
1093 | |
1094 e = new RealExp(loc, value, type); | |
1095 } | |
1096 else if (tb->iscomplex()) | |
1097 { complex_t value = e1->toComplex(); | |
1098 | |
1099 e = new ComplexExp(loc, value, type); | |
1100 } | |
1101 else if (tb->isscalar()) | |
1102 e = new IntegerExp(loc, e1->toInteger(), type); | |
1103 else if (tb->ty == Tvoid) | |
1104 e = EXP_CANT_INTERPRET; | |
1105 else if (tb->ty == Tstruct && e1->op == TOKint64) | |
1106 { // Struct = 0; | |
1107 StructDeclaration *sd = tb->toDsymbol(NULL)->isStructDeclaration(); | |
1108 assert(sd); | |
1109 Expressions *elements = new Expressions; | |
1110 for (size_t i = 0; i < sd->fields.dim; i++) | |
1111 { Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
1112 VarDeclaration *v = s->isVarDeclaration(); | |
1113 assert(v); | |
1114 | |
1115 Expression *exp = new IntegerExp(0); | |
1116 exp = Cast(v->type, v->type, exp); | |
1117 if (exp == EXP_CANT_INTERPRET) | |
1118 return exp; | |
1119 elements->push(exp); | |
1120 } | |
1121 e = new StructLiteralExp(loc, sd, elements); | |
1122 e->type = type; | |
1123 } | |
1124 else | |
1125 { | |
1126 error(loc, "cannot cast %s to %s", e1->type->toChars(), type->toChars()); | |
1127 e = new IntegerExp(loc, 0, type); | |
1128 } | |
1129 return e; | |
1130 } | |
1131 | |
1132 | |
1133 Expression *ArrayLength(Type *type, Expression *e1) | |
1134 { Expression *e; | |
1135 Loc loc = e1->loc; | |
1136 | |
1137 if (e1->op == TOKstring) | |
1138 { StringExp *es1 = (StringExp *)e1; | |
1139 | |
1140 e = new IntegerExp(loc, es1->len, type); | |
1141 } | |
1142 else if (e1->op == TOKarrayliteral) | |
1143 { ArrayLiteralExp *ale = (ArrayLiteralExp *)e1; | |
1144 size_t dim; | |
1145 | |
1146 dim = ale->elements ? ale->elements->dim : 0; | |
1147 e = new IntegerExp(loc, dim, type); | |
1148 } | |
1149 else if (e1->op == TOKassocarrayliteral) | |
1150 { AssocArrayLiteralExp *ale = (AssocArrayLiteralExp *)e1; | |
1151 size_t dim = ale->keys->dim; | |
1152 | |
1153 e = new IntegerExp(loc, dim, type); | |
1154 } | |
1155 else | |
1156 e = EXP_CANT_INTERPRET; | |
1157 return e; | |
1158 } | |
1159 | |
1160 /* Also return EXP_CANT_INTERPRET if this fails | |
1161 */ | |
1162 Expression *Index(Type *type, Expression *e1, Expression *e2) | |
1163 { Expression *e = EXP_CANT_INTERPRET; | |
1164 Loc loc = e1->loc; | |
1165 | |
1166 //printf("Index(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); | |
1167 assert(e1->type); | |
1168 if (e1->op == TOKstring && e2->op == TOKint64) | |
1169 { StringExp *es1 = (StringExp *)e1; | |
1170 uinteger_t i = e2->toInteger(); | |
1171 | |
1172 if (i >= es1->len) | |
1173 e1->error("string index %llu is out of bounds [0 .. %"PRIuSIZE"]", i, es1->len); | |
1174 else | |
1175 { unsigned value = es1->charAt(i); | |
1176 e = new IntegerExp(loc, value, type); | |
1177 } | |
1178 } | |
1179 else if (e1->type->toBasetype()->ty == Tsarray && e2->op == TOKint64) | |
1180 { TypeSArray *tsa = (TypeSArray *)e1->type->toBasetype(); | |
1181 uinteger_t length = tsa->dim->toInteger(); | |
1182 uinteger_t i = e2->toInteger(); | |
1183 | |
1184 if (i >= length) | |
1185 { | |
1186 e2->error("array index %llu is out of bounds %s[0 .. %llu]", i, e1->toChars(), length); | |
1187 } | |
1188 else if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2)) | |
1189 { ArrayLiteralExp *ale = (ArrayLiteralExp *)e1; | |
1190 e = (Expression *)ale->elements->data[i]; | |
1191 e->type = type; | |
1192 } | |
1193 } | |
1194 else if (e1->type->toBasetype()->ty == Tarray && e2->op == TOKint64) | |
1195 { | |
1196 uinteger_t i = e2->toInteger(); | |
1197 | |
1198 if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2)) | |
1199 { ArrayLiteralExp *ale = (ArrayLiteralExp *)e1; | |
1200 if (i >= ale->elements->dim) | |
1201 { | |
1202 e2->error("array index %llu is out of bounds %s[0 .. %u]", i, e1->toChars(), ale->elements->dim); | |
1203 } | |
1204 else | |
1205 { e = (Expression *)ale->elements->data[i]; | |
1206 e->type = type; | |
1207 } | |
1208 } | |
1209 } | |
1210 else if (e1->op == TOKassocarrayliteral && !e1->checkSideEffect(2)) | |
1211 { | |
1212 AssocArrayLiteralExp *ae = (AssocArrayLiteralExp *)e1; | |
1213 /* Search the keys backwards, in case there are duplicate keys | |
1214 */ | |
1215 for (size_t i = ae->keys->dim; i;) | |
1216 { | |
1217 i--; | |
1218 Expression *ekey = (Expression *)ae->keys->data[i]; | |
1219 Expression *ex = Equal(TOKequal, Type::tbool, ekey, e2); | |
1220 if (ex == EXP_CANT_INTERPRET) | |
1221 return ex; | |
1222 if (ex->isBool(TRUE)) | |
1223 { e = (Expression *)ae->values->data[i]; | |
1224 e->type = type; | |
1225 break; | |
1226 } | |
1227 } | |
1228 } | |
1229 return e; | |
1230 } | |
1231 | |
1232 /* Also return EXP_CANT_INTERPRET if this fails | |
1233 */ | |
1234 Expression *Slice(Type *type, Expression *e1, Expression *lwr, Expression *upr) | |
1235 { Expression *e = EXP_CANT_INTERPRET; | |
1236 Loc loc = e1->loc; | |
1237 | |
1238 #if LOG | |
1239 printf("Slice()\n"); | |
1240 if (lwr) | |
1241 { printf("\te1 = %s\n", e1->toChars()); | |
1242 printf("\tlwr = %s\n", lwr->toChars()); | |
1243 printf("\tupr = %s\n", upr->toChars()); | |
1244 } | |
1245 #endif | |
1246 if (e1->op == TOKstring && lwr->op == TOKint64 && upr->op == TOKint64) | |
1247 { StringExp *es1 = (StringExp *)e1; | |
1248 uinteger_t ilwr = lwr->toInteger(); | |
1249 uinteger_t iupr = upr->toInteger(); | |
1250 | |
1251 if (iupr > es1->len || ilwr > iupr) | |
1252 e1->error("string slice [%llu .. %llu] is out of bounds", ilwr, iupr); | |
1253 else | |
1254 { integer_t value; | |
1255 void *s; | |
1256 size_t len = iupr - ilwr; | |
1257 int sz = es1->sz; | |
1258 StringExp *es; | |
1259 | |
1260 s = mem.malloc((len + 1) * sz); | |
1261 memcpy((unsigned char *)s, (unsigned char *)es1->string + ilwr * sz, len * sz); | |
1262 memset((unsigned char *)s + len * sz, 0, sz); | |
1263 | |
1264 es = new StringExp(loc, s, len, es1->postfix); | |
1265 es->sz = sz; | |
1266 es->committed = 1; | |
1267 es->type = type; | |
1268 e = es; | |
1269 } | |
1270 } | |
1271 else if (e1->op == TOKarrayliteral && | |
1272 lwr->op == TOKint64 && upr->op == TOKint64 && | |
1273 !e1->checkSideEffect(2)) | |
1274 { ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1; | |
1275 uinteger_t ilwr = lwr->toInteger(); | |
1276 uinteger_t iupr = upr->toInteger(); | |
1277 | |
1278 if (iupr > es1->elements->dim || ilwr > iupr) | |
1279 e1->error("array slice [%llu .. %llu] is out of bounds", ilwr, iupr); | |
1280 else | |
1281 { | |
1282 Expressions *elements = new Expressions(); | |
1283 elements->setDim(iupr - ilwr); | |
1284 memcpy(elements->data, | |
1285 es1->elements->data + ilwr, | |
1286 (iupr - ilwr) * sizeof(es1->elements->data[0])); | |
1287 e = new ArrayLiteralExp(e1->loc, elements); | |
1288 e->type = type; | |
1289 } | |
1290 } | |
1291 return e; | |
1292 } | |
1293 | |
1294 /* Also return EXP_CANT_INTERPRET if this fails | |
1295 */ | |
1296 Expression *Cat(Type *type, Expression *e1, Expression *e2) | |
1297 { Expression *e = EXP_CANT_INTERPRET; | |
1298 Loc loc = e1->loc; | |
1299 Type *t; | |
1300 | |
1301 //printf("Cat(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); | |
1302 | |
1303 if (e1->op == TOKnull && (e2->op == TOKint64 || e2->op == TOKstructliteral)) | |
1304 { e = e2; | |
1305 goto L2; | |
1306 } | |
1307 else if ((e1->op == TOKint64 || e1->op == TOKstructliteral) && e2->op == TOKnull) | |
1308 { e = e1; | |
1309 L2: | |
1310 Type *tn = e->type->toBasetype(); | |
1311 if (tn->ty == Tchar || tn->ty == Twchar || tn->ty == Tdchar) | |
1312 { | |
1313 // Create a StringExp | |
1314 void *s; | |
1315 StringExp *es; | |
1316 size_t len = 1; | |
1317 int sz = tn->size(); | |
1318 integer_t v = e->toInteger(); | |
1319 | |
1320 s = mem.malloc((len + 1) * sz); | |
1321 memcpy((unsigned char *)s, &v, sz); | |
1322 | |
1323 // Add terminating 0 | |
1324 memset((unsigned char *)s + len * sz, 0, sz); | |
1325 | |
1326 es = new StringExp(loc, s, len); | |
1327 es->sz = sz; | |
1328 es->committed = 1; | |
1329 e = es; | |
1330 } | |
1331 else | |
1332 { // Create an ArrayLiteralExp | |
1333 Expressions *elements = new Expressions(); | |
1334 elements->push(e); | |
1335 e = new ArrayLiteralExp(e->loc, elements); | |
1336 } | |
1337 e->type = type; | |
1338 return e; | |
1339 } | |
1340 else if (e1->op == TOKstring && e2->op == TOKstring) | |
1341 { | |
1342 // Concatenate the strings | |
1343 void *s; | |
1344 StringExp *es1 = (StringExp *)e1; | |
1345 StringExp *es2 = (StringExp *)e2; | |
1346 StringExp *es; | |
1347 Type *t; | |
1348 size_t len = es1->len + es2->len; | |
1349 int sz = es1->sz; | |
1350 | |
658
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1351 if (sz != es2->sz) |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1352 { |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1353 /* Can happen with: |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1354 * auto s = "foo"d ~ "bar"c; |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1355 */ |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1356 assert(global.errors); |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1357 return e; |
50383e476c7e
Upgraded frontend to DMD 1.035
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
637
diff
changeset
|
1358 } |
336 | 1359 s = mem.malloc((len + 1) * sz); |
1360 memcpy(s, es1->string, es1->len * sz); | |
1361 memcpy((unsigned char *)s + es1->len * sz, es2->string, es2->len * sz); | |
1362 | |
1363 // Add terminating 0 | |
1364 memset((unsigned char *)s + len * sz, 0, sz); | |
1365 | |
1366 es = new StringExp(loc, s, len); | |
1367 es->sz = sz; | |
1368 es->committed = es1->committed | es2->committed; | |
1369 if (es1->committed) | |
1370 t = es1->type; | |
1371 else | |
1372 t = es2->type; | |
1373 es->type = type; | |
1374 e = es; | |
1375 } | |
1376 else if (e1->op == TOKstring && e2->op == TOKint64) | |
1377 { | |
1378 // Concatenate the strings | |
1379 void *s; | |
1380 StringExp *es1 = (StringExp *)e1; | |
1381 StringExp *es; | |
1382 Type *t; | |
1383 size_t len = es1->len + 1; | |
1384 int sz = es1->sz; | |
1385 integer_t v = e2->toInteger(); | |
1386 | |
1387 s = mem.malloc((len + 1) * sz); | |
1388 memcpy(s, es1->string, es1->len * sz); | |
1389 memcpy((unsigned char *)s + es1->len * sz, &v, sz); | |
1390 | |
1391 // Add terminating 0 | |
1392 memset((unsigned char *)s + len * sz, 0, sz); | |
1393 | |
1394 es = new StringExp(loc, s, len); | |
1395 es->sz = sz; | |
1396 es->committed = es1->committed; | |
1397 t = es1->type; | |
1398 es->type = type; | |
1399 e = es; | |
1400 } | |
1401 else if (e1->op == TOKint64 && e2->op == TOKstring) | |
1402 { | |
1403 // Concatenate the strings | |
1404 void *s; | |
1405 StringExp *es2 = (StringExp *)e2; | |
1406 StringExp *es; | |
1407 Type *t; | |
1408 size_t len = 1 + es2->len; | |
1409 int sz = es2->sz; | |
1410 integer_t v = e1->toInteger(); | |
1411 | |
1412 s = mem.malloc((len + 1) * sz); | |
1413 memcpy((unsigned char *)s, &v, sz); | |
1414 memcpy((unsigned char *)s + sz, es2->string, es2->len * sz); | |
1415 | |
1416 // Add terminating 0 | |
1417 memset((unsigned char *)s + len * sz, 0, sz); | |
1418 | |
1419 es = new StringExp(loc, s, len); | |
1420 es->sz = sz; | |
1421 es->committed = es2->committed; | |
1422 t = es2->type; | |
1423 es->type = type; | |
1424 e = es; | |
1425 } | |
1426 else if (e1->op == TOKarrayliteral && e2->op == TOKarrayliteral && | |
1427 e1->type->equals(e2->type)) | |
1428 { | |
1429 // Concatenate the arrays | |
1430 ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1; | |
1431 ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2; | |
1432 | |
1433 es1 = new ArrayLiteralExp(es1->loc, (Expressions *)es1->elements->copy()); | |
1434 es1->elements->insert(es1->elements->dim, es2->elements); | |
1435 e = es1; | |
1436 | |
1437 if (type->toBasetype()->ty == Tsarray) | |
1438 { | |
1439 e->type = new TypeSArray(e1->type->toBasetype()->next, new IntegerExp(0, es1->elements->dim, Type::tindex)); | |
1440 e->type = e->type->semantic(loc, NULL); | |
1441 } | |
1442 else | |
1443 e->type = type; | |
1444 } | |
1445 else if ((e1->op == TOKarrayliteral || e1->op == TOKnull) && | |
1446 e1->type->toBasetype()->nextOf()->equals(e2->type)) | |
1447 { | |
1448 ArrayLiteralExp *es1; | |
1449 if (e1->op == TOKarrayliteral) | |
1450 { es1 = (ArrayLiteralExp *)e1; | |
1451 es1 = new ArrayLiteralExp(es1->loc, (Expressions *)es1->elements->copy()); | |
1452 es1->elements->push(e2); | |
1453 } | |
1454 else | |
1455 { | |
1456 es1 = new ArrayLiteralExp(e1->loc, e2); | |
1457 } | |
1458 e = es1; | |
1459 | |
1460 if (type->toBasetype()->ty == Tsarray) | |
1461 { | |
1462 e->type = new TypeSArray(e2->type, new IntegerExp(0, es1->elements->dim, Type::tindex)); | |
1463 e->type = e->type->semantic(loc, NULL); | |
1464 } | |
1465 else | |
1466 e->type = type; | |
1467 } | |
1468 else if (e2->op == TOKarrayliteral && | |
1469 e2->type->toBasetype()->nextOf()->equals(e1->type)) | |
1470 { | |
1471 ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2; | |
1472 | |
1473 es2 = new ArrayLiteralExp(es2->loc, (Expressions *)es2->elements->copy()); | |
1474 es2->elements->shift(e1); | |
1475 e = es2; | |
1476 | |
1477 if (type->toBasetype()->ty == Tsarray) | |
1478 { | |
1479 e->type = new TypeSArray(e1->type, new IntegerExp(0, es2->elements->dim, Type::tindex)); | |
1480 e->type = e->type->semantic(loc, NULL); | |
1481 } | |
1482 else | |
1483 e->type = type; | |
1484 } | |
1485 else if (e1->op == TOKnull && e2->op == TOKstring) | |
1486 { | |
1487 t = e1->type; | |
1488 e = e2; | |
1489 goto L1; | |
1490 } | |
1491 else if (e1->op == TOKstring && e2->op == TOKnull) | |
1492 { e = e1; | |
1493 t = e2->type; | |
1494 L1: | |
1495 Type *tb = t->toBasetype(); | |
1496 if (tb->ty == Tarray && tb->nextOf()->equals(e->type)) | |
1497 { Expressions *expressions = new Expressions(); | |
1498 expressions->push(e); | |
1499 e = new ArrayLiteralExp(loc, expressions); | |
1500 e->type = t; | |
1501 } | |
1502 if (!e->type->equals(type)) | |
1503 { StringExp *se = (StringExp *)e->copy(); | |
1504 e = se->castTo(NULL, type); | |
1505 } | |
1506 } | |
1507 return e; | |
1508 } | |
1509 | |
1510 Expression *Ptr(Type *type, Expression *e1) | |
1511 { | |
1512 //printf("Ptr(e1 = %s)\n", e1->toChars()); | |
1513 if (e1->op == TOKadd) | |
1514 { AddExp *ae = (AddExp *)e1; | |
1515 if (ae->e1->op == TOKaddress && ae->e2->op == TOKint64) | |
1516 { AddrExp *ade = (AddrExp *)ae->e1; | |
1517 if (ade->e1->op == TOKstructliteral) | |
1518 { StructLiteralExp *se = (StructLiteralExp *)ade->e1; | |
1519 unsigned offset = ae->e2->toInteger(); | |
1520 Expression *e = se->getField(type, offset); | |
1521 if (!e) | |
1522 e = EXP_CANT_INTERPRET; | |
1523 return e; | |
1524 } | |
1525 } | |
1526 } | |
1527 return EXP_CANT_INTERPRET; | |
1528 } | |
1529 |