1
|
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);
|
|
490 #else
|
|
491 c = complex_t(fmodl(e1->toReal(), r2), fmodl(e1->toImaginary(), r2));
|
|
492 #endif
|
|
493 }
|
|
494 else if (e2->type->isimaginary())
|
|
495 { real_t i2 = e2->toImaginary();
|
|
496
|
|
497 #ifdef __DMC__
|
|
498 c = fmodl(e1->toReal(), i2) + fmodl(e1->toImaginary(), i2) * I;
|
|
499 #elif defined(IN_GCC)
|
|
500 c = complex_t(e1->toReal() % i2, e1->toImaginary() % i2);
|
|
501 #else
|
|
502 c = complex_t(fmodl(e1->toReal(), i2), fmodl(e1->toImaginary(), i2));
|
|
503 #endif
|
|
504 }
|
|
505 else
|
|
506 assert(0);
|
|
507
|
|
508 if (type->isreal())
|
|
509 e = new RealExp(loc, creall(c), type);
|
|
510 else if (type->isimaginary())
|
|
511 e = new RealExp(loc, cimagl(c), type);
|
|
512 else if (type->iscomplex())
|
|
513 e = new ComplexExp(loc, c, type);
|
|
514 else
|
|
515 assert(0);
|
|
516 }
|
|
517 else
|
|
518 { sinteger_t n1;
|
|
519 sinteger_t n2;
|
|
520 sinteger_t n;
|
|
521
|
|
522 n1 = e1->toInteger();
|
|
523 n2 = e2->toInteger();
|
|
524 if (n2 == 0)
|
|
525 { e2->error("divide by 0");
|
|
526 e2 = new IntegerExp(0, 1, e2->type);
|
|
527 n2 = 1;
|
|
528 }
|
|
529 if (e1->type->isunsigned() || e2->type->isunsigned())
|
|
530 n = ((d_uns64) n1) % ((d_uns64) n2);
|
|
531 else
|
|
532 n = n1 % n2;
|
|
533 e = new IntegerExp(loc, n, type);
|
|
534 }
|
|
535 return e;
|
|
536 }
|
|
537
|
|
538 Expression *Shl(Type *type, Expression *e1, Expression *e2)
|
|
539 { Expression *e;
|
|
540 Loc loc = e1->loc;
|
|
541
|
|
542 e = new IntegerExp(loc, e1->toInteger() << e2->toInteger(), type);
|
|
543 return e;
|
|
544 }
|
|
545
|
|
546 Expression *Shr(Type *type, Expression *e1, Expression *e2)
|
|
547 { Expression *e;
|
|
548 Loc loc = e1->loc;
|
|
549 unsigned count;
|
|
550 integer_t value;
|
|
551
|
|
552 value = e1->toInteger();
|
|
553 count = e2->toInteger();
|
|
554 switch (e1->type->toBasetype()->ty)
|
|
555 {
|
|
556 case Tint8:
|
|
557 value = (d_int8)(value) >> count;
|
|
558 break;
|
|
559
|
|
560 case Tuns8:
|
|
561 value = (d_uns8)(value) >> count;
|
|
562 break;
|
|
563
|
|
564 case Tint16:
|
|
565 value = (d_int16)(value) >> count;
|
|
566 break;
|
|
567
|
|
568 case Tuns16:
|
|
569 value = (d_uns16)(value) >> count;
|
|
570 break;
|
|
571
|
|
572 case Tint32:
|
|
573 value = (d_int32)(value) >> count;
|
|
574 break;
|
|
575
|
|
576 case Tuns32:
|
|
577 value = (d_uns32)(value) >> count;
|
|
578 break;
|
|
579
|
|
580 case Tint64:
|
|
581 value = (d_int64)(value) >> count;
|
|
582 break;
|
|
583
|
|
584 case Tuns64:
|
|
585 value = (d_uns64)(value) >> count;
|
|
586 break;
|
|
587
|
|
588 default:
|
|
589 assert(0);
|
|
590 }
|
|
591 e = new IntegerExp(loc, value, type);
|
|
592 return e;
|
|
593 }
|
|
594
|
|
595 Expression *Ushr(Type *type, Expression *e1, Expression *e2)
|
|
596 { Expression *e;
|
|
597 Loc loc = e1->loc;
|
|
598 unsigned count;
|
|
599 integer_t value;
|
|
600
|
|
601 value = e1->toInteger();
|
|
602 count = e2->toInteger();
|
|
603 switch (e1->type->toBasetype()->ty)
|
|
604 {
|
|
605 case Tint8:
|
|
606 case Tuns8:
|
|
607 assert(0); // no way to trigger this
|
|
608 value = (value & 0xFF) >> count;
|
|
609 break;
|
|
610
|
|
611 case Tint16:
|
|
612 case Tuns16:
|
|
613 assert(0); // no way to trigger this
|
|
614 value = (value & 0xFFFF) >> count;
|
|
615 break;
|
|
616
|
|
617 case Tint32:
|
|
618 case Tuns32:
|
|
619 value = (value & 0xFFFFFFFF) >> count;
|
|
620 break;
|
|
621
|
|
622 case Tint64:
|
|
623 case Tuns64:
|
|
624 value = (d_uns64)(value) >> count;
|
|
625 break;
|
|
626
|
|
627 default:
|
|
628 assert(0);
|
|
629 }
|
|
630 e = new IntegerExp(loc, value, type);
|
|
631 return e;
|
|
632 }
|
|
633
|
|
634 Expression *And(Type *type, Expression *e1, Expression *e2)
|
|
635 { Expression *e;
|
|
636 Loc loc = e1->loc;
|
|
637
|
|
638 e = new IntegerExp(loc, e1->toInteger() & e2->toInteger(), type);
|
|
639 return e;
|
|
640 }
|
|
641
|
|
642 Expression *Or(Type *type, Expression *e1, Expression *e2)
|
|
643 { Expression *e;
|
|
644 Loc loc = e1->loc;
|
|
645
|
|
646 e = new IntegerExp(loc, e1->toInteger() | e2->toInteger(), type);
|
|
647 return e;
|
|
648 }
|
|
649
|
|
650 Expression *Xor(Type *type, Expression *e1, Expression *e2)
|
|
651 { Expression *e;
|
|
652 Loc loc = e1->loc;
|
|
653
|
|
654 e = new IntegerExp(loc, e1->toInteger() ^ e2->toInteger(), type);
|
|
655 return e;
|
|
656 }
|
|
657
|
|
658 /* Also returns EXP_CANT_INTERPRET if cannot be computed.
|
|
659 */
|
|
660 Expression *Equal(enum TOK op, Type *type, Expression *e1, Expression *e2)
|
|
661 { Expression *e;
|
|
662 Loc loc = e1->loc;
|
|
663 int cmp;
|
|
664 real_t r1;
|
|
665 real_t r2;
|
|
666
|
|
667 //printf("Equal(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars());
|
|
668
|
|
669 assert(op == TOKequal || op == TOKnotequal);
|
|
670
|
19
|
671 if (e1->op == TOKnull)
|
|
672 {
|
|
673 if (e2->op == TOKnull)
|
|
674 cmp = 1;
|
|
675 else if (e2->op == TOKstring)
|
|
676 { StringExp *es2 = (StringExp *)e2;
|
|
677 cmp = (0 == es2->len);
|
|
678 }
|
|
679 else if (e2->op == TOKarrayliteral)
|
|
680 { ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
|
|
681 cmp = !es2->elements || (0 == es2->elements->dim);
|
|
682 }
|
|
683 else
|
|
684 return EXP_CANT_INTERPRET;
|
|
685 }
|
|
686 else if (e2->op == TOKnull)
|
|
687 {
|
|
688 if (e1->op == TOKstring)
|
|
689 { StringExp *es1 = (StringExp *)e1;
|
|
690 cmp = (0 == es1->len);
|
|
691 }
|
|
692 else if (e1->op == TOKarrayliteral)
|
|
693 { ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
|
|
694 cmp = !es1->elements || (0 == es1->elements->dim);
|
|
695 }
|
|
696 else
|
|
697 return EXP_CANT_INTERPRET;
|
|
698 }
|
|
699 else if (e1->op == TOKstring && e2->op == TOKstring)
|
1
|
700 { StringExp *es1 = (StringExp *)e1;
|
|
701 StringExp *es2 = (StringExp *)e2;
|
|
702
|
|
703 assert(es1->sz == es2->sz);
|
|
704 if (es1->len == es2->len &&
|
|
705 memcmp(es1->string, es2->string, es1->sz * es1->len) == 0)
|
|
706 cmp = 1;
|
|
707 else
|
|
708 cmp = 0;
|
|
709 }
|
|
710 else if (e1->op == TOKarrayliteral && e2->op == TOKarrayliteral)
|
|
711 { ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
|
|
712 ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
|
|
713
|
|
714 if ((!es1->elements || !es1->elements->dim) &&
|
|
715 (!es2->elements || !es2->elements->dim))
|
|
716 cmp = 1; // both arrays are empty
|
|
717 else if (!es1->elements || !es2->elements)
|
|
718 cmp = 0;
|
|
719 else if (es1->elements->dim != es2->elements->dim)
|
|
720 cmp = 0;
|
|
721 else
|
|
722 {
|
|
723 for (size_t i = 0; i < es1->elements->dim; i++)
|
|
724 { Expression *ee1 = (Expression *)es1->elements->data[i];
|
|
725 Expression *ee2 = (Expression *)es2->elements->data[i];
|
|
726
|
|
727 Expression *v = Equal(TOKequal, Type::tint32, ee1, ee2);
|
|
728 if (v == EXP_CANT_INTERPRET)
|
|
729 return EXP_CANT_INTERPRET;
|
|
730 cmp = v->toInteger();
|
|
731 if (cmp == 0)
|
|
732 break;
|
|
733 }
|
|
734 }
|
|
735 }
|
|
736 else if (e1->op == TOKstructliteral && e2->op == TOKstructliteral)
|
|
737 { StructLiteralExp *es1 = (StructLiteralExp *)e1;
|
|
738 StructLiteralExp *es2 = (StructLiteralExp *)e2;
|
|
739
|
|
740 if (es1->sd != es2->sd)
|
|
741 cmp = 0;
|
|
742 else if ((!es1->elements || !es1->elements->dim) &&
|
|
743 (!es2->elements || !es2->elements->dim))
|
|
744 cmp = 1; // both arrays are empty
|
|
745 else if (!es1->elements || !es2->elements)
|
|
746 cmp = 0;
|
|
747 else if (es1->elements->dim != es2->elements->dim)
|
|
748 cmp = 0;
|
|
749 else
|
|
750 {
|
|
751 cmp = 1;
|
|
752 for (size_t i = 0; i < es1->elements->dim; i++)
|
|
753 { Expression *ee1 = (Expression *)es1->elements->data[i];
|
|
754 Expression *ee2 = (Expression *)es2->elements->data[i];
|
|
755
|
|
756 if (ee1 == ee2)
|
|
757 continue;
|
|
758 if (!ee1 || !ee2)
|
|
759 { cmp = 0;
|
|
760 break;
|
|
761 }
|
|
762 Expression *v = Equal(TOKequal, Type::tint32, ee1, ee2);
|
|
763 if (v == EXP_CANT_INTERPRET)
|
|
764 return EXP_CANT_INTERPRET;
|
|
765 cmp = v->toInteger();
|
|
766 if (cmp == 0)
|
|
767 break;
|
|
768 }
|
|
769 }
|
|
770 }
|
|
771 #if 0 // Should handle this
|
|
772 else if (e1->op == TOKarrayliteral && e2->op == TOKstring)
|
|
773 {
|
|
774 }
|
|
775 #endif
|
|
776 else if (e1->isConst() != 1 || e2->isConst() != 1)
|
|
777 return EXP_CANT_INTERPRET;
|
|
778 else if (e1->type->isreal())
|
|
779 {
|
|
780 r1 = e1->toReal();
|
|
781 r2 = e2->toReal();
|
|
782 goto L1;
|
|
783 }
|
|
784 else if (e1->type->isimaginary())
|
|
785 {
|
|
786 r1 = e1->toImaginary();
|
|
787 r2 = e2->toImaginary();
|
|
788 L1:
|
|
789 #if __DMC__
|
|
790 cmp = (r1 == r2);
|
|
791 #else
|
|
792 if (isnan(r1) || isnan(r2)) // if unordered
|
|
793 {
|
|
794 cmp = 0;
|
|
795 }
|
|
796 else
|
|
797 {
|
|
798 cmp = (r1 == r2);
|
|
799 }
|
|
800 #endif
|
|
801 }
|
|
802 else if (e1->type->iscomplex())
|
|
803 {
|
|
804 cmp = e1->toComplex() == e2->toComplex();
|
|
805 }
|
|
806 else if (e1->type->isintegral())
|
|
807 {
|
|
808 cmp = (e1->toInteger() == e2->toInteger());
|
|
809 }
|
|
810 else
|
|
811 return EXP_CANT_INTERPRET;
|
|
812 if (op == TOKnotequal)
|
|
813 cmp ^= 1;
|
|
814 e = new IntegerExp(loc, cmp, type);
|
|
815 return e;
|
|
816 }
|
|
817
|
|
818 Expression *Identity(enum TOK op, Type *type, Expression *e1, Expression *e2)
|
|
819 { Expression *e;
|
|
820 Loc loc = e1->loc;
|
|
821 int cmp;
|
|
822
|
19
|
823 if (e1->op == TOKnull && e2->op == TOKnull)
|
|
824 {
|
|
825 cmp = 1;
|
|
826 }
|
|
827 else if (e1->op == TOKsymoff && e2->op == TOKsymoff)
|
1
|
828 {
|
|
829 SymOffExp *es1 = (SymOffExp *)e1;
|
|
830 SymOffExp *es2 = (SymOffExp *)e2;
|
|
831
|
|
832 cmp = (es1->var == es2->var && es1->offset == es2->offset);
|
|
833 }
|
|
834 else if (e1->isConst() == 1 && e2->isConst() == 1)
|
|
835 return Equal((op == TOKidentity) ? TOKequal : TOKnotequal,
|
|
836 type, e1, e2);
|
|
837 else
|
|
838 assert(0);
|
|
839 if (op == TOKnotidentity)
|
|
840 cmp ^= 1;
|
|
841 return new IntegerExp(loc, cmp, type);
|
|
842 }
|
|
843
|
|
844
|
|
845 Expression *Cmp(enum TOK op, Type *type, Expression *e1, Expression *e2)
|
|
846 { Expression *e;
|
|
847 Loc loc = e1->loc;
|
|
848 integer_t n;
|
|
849 real_t r1;
|
|
850 real_t r2;
|
|
851
|
|
852 if (e1->type->isreal())
|
|
853 {
|
|
854 r1 = e1->toReal();
|
|
855 r2 = e2->toReal();
|
|
856 goto L1;
|
|
857 }
|
|
858 else if (e1->type->isimaginary())
|
|
859 {
|
|
860 r1 = e1->toImaginary();
|
|
861 r2 = e2->toImaginary();
|
|
862 L1:
|
|
863 #if __DMC__
|
|
864 // DMC is the only compiler I know of that handles NAN arguments
|
|
865 // correctly in comparisons.
|
|
866 switch (op)
|
|
867 {
|
|
868 case TOKlt: n = r1 < r2; break;
|
|
869 case TOKle: n = r1 <= r2; break;
|
|
870 case TOKgt: n = r1 > r2; break;
|
|
871 case TOKge: n = r1 >= r2; break;
|
|
872
|
|
873 case TOKleg: n = r1 <>= r2; break;
|
|
874 case TOKlg: n = r1 <> r2; break;
|
|
875 case TOKunord: n = r1 !<>= r2; break;
|
|
876 case TOKue: n = r1 !<> r2; break;
|
|
877 case TOKug: n = r1 !<= r2; break;
|
|
878 case TOKuge: n = r1 !< r2; break;
|
|
879 case TOKul: n = r1 !>= r2; break;
|
|
880 case TOKule: n = r1 !> r2; break;
|
|
881
|
|
882 default:
|
|
883 assert(0);
|
|
884 }
|
|
885 #else
|
|
886 // Don't rely on compiler, handle NAN arguments separately
|
|
887 #if IN_GCC
|
|
888 if (real_isnan(&r1) || real_isnan(&r2)) // if unordered
|
|
889 #else
|
|
890 if (isnan(r1) || isnan(r2)) // if unordered
|
|
891 #endif
|
|
892 {
|
|
893 switch (op)
|
|
894 {
|
|
895 case TOKlt: n = 0; break;
|
|
896 case TOKle: n = 0; break;
|
|
897 case TOKgt: n = 0; break;
|
|
898 case TOKge: n = 0; break;
|
|
899
|
|
900 case TOKleg: n = 0; break;
|
|
901 case TOKlg: n = 0; break;
|
|
902 case TOKunord: n = 1; break;
|
|
903 case TOKue: n = 1; break;
|
|
904 case TOKug: n = 1; break;
|
|
905 case TOKuge: n = 1; break;
|
|
906 case TOKul: n = 1; break;
|
|
907 case TOKule: n = 1; break;
|
|
908
|
|
909 default:
|
|
910 assert(0);
|
|
911 }
|
|
912 }
|
|
913 else
|
|
914 {
|
|
915 switch (op)
|
|
916 {
|
|
917 case TOKlt: n = r1 < r2; break;
|
|
918 case TOKle: n = r1 <= r2; break;
|
|
919 case TOKgt: n = r1 > r2; break;
|
|
920 case TOKge: n = r1 >= r2; break;
|
|
921
|
|
922 case TOKleg: n = 1; break;
|
|
923 case TOKlg: n = r1 != r2; break;
|
|
924 case TOKunord: n = 0; break;
|
|
925 case TOKue: n = r1 == r2; break;
|
|
926 case TOKug: n = r1 > r2; break;
|
|
927 case TOKuge: n = r1 >= r2; break;
|
|
928 case TOKul: n = r1 < r2; break;
|
|
929 case TOKule: n = r1 <= r2; break;
|
|
930
|
|
931 default:
|
|
932 assert(0);
|
|
933 }
|
|
934 }
|
|
935 #endif
|
|
936 }
|
|
937 else if (e1->type->iscomplex())
|
|
938 {
|
|
939 assert(0);
|
|
940 }
|
|
941 else
|
|
942 { sinteger_t n1;
|
|
943 sinteger_t n2;
|
|
944
|
|
945 n1 = e1->toInteger();
|
|
946 n2 = e2->toInteger();
|
|
947 if (e1->type->isunsigned() || e2->type->isunsigned())
|
|
948 {
|
|
949 switch (op)
|
|
950 {
|
|
951 case TOKlt: n = ((d_uns64) n1) < ((d_uns64) n2); break;
|
|
952 case TOKle: n = ((d_uns64) n1) <= ((d_uns64) n2); break;
|
|
953 case TOKgt: n = ((d_uns64) n1) > ((d_uns64) n2); break;
|
|
954 case TOKge: n = ((d_uns64) n1) >= ((d_uns64) n2); break;
|
|
955
|
|
956 case TOKleg: n = 1; break;
|
|
957 case TOKlg: n = ((d_uns64) n1) != ((d_uns64) n2); break;
|
|
958 case TOKunord: n = 0; break;
|
|
959 case TOKue: n = ((d_uns64) n1) == ((d_uns64) n2); break;
|
|
960 case TOKug: n = ((d_uns64) n1) > ((d_uns64) n2); break;
|
|
961 case TOKuge: n = ((d_uns64) n1) >= ((d_uns64) n2); break;
|
|
962 case TOKul: n = ((d_uns64) n1) < ((d_uns64) n2); break;
|
|
963 case TOKule: n = ((d_uns64) n1) <= ((d_uns64) n2); break;
|
|
964
|
|
965 default:
|
|
966 assert(0);
|
|
967 }
|
|
968 }
|
|
969 else
|
|
970 {
|
|
971 switch (op)
|
|
972 {
|
|
973 case TOKlt: n = n1 < n2; break;
|
|
974 case TOKle: n = n1 <= n2; break;
|
|
975 case TOKgt: n = n1 > n2; break;
|
|
976 case TOKge: n = n1 >= n2; break;
|
|
977
|
|
978 case TOKleg: n = 1; break;
|
|
979 case TOKlg: n = n1 != n2; break;
|
|
980 case TOKunord: n = 0; break;
|
|
981 case TOKue: n = n1 == n2; break;
|
|
982 case TOKug: n = n1 > n2; break;
|
|
983 case TOKuge: n = n1 >= n2; break;
|
|
984 case TOKul: n = n1 < n2; break;
|
|
985 case TOKule: n = n1 <= n2; break;
|
|
986
|
|
987 default:
|
|
988 assert(0);
|
|
989 }
|
|
990 }
|
|
991 }
|
|
992 e = new IntegerExp(loc, n, type);
|
|
993 return e;
|
|
994 }
|
|
995
|
|
996 /* Also returns EXP_CANT_INTERPRET if cannot be computed.
|
|
997 * to: type to cast to
|
|
998 * type: type to paint the result
|
|
999 */
|
|
1000
|
|
1001 Expression *Cast(Type *type, Type *to, Expression *e1)
|
|
1002 { Expression *e = EXP_CANT_INTERPRET;
|
|
1003 Loc loc = e1->loc;
|
|
1004
|
|
1005 //printf("Cast(type = %s, to = %s, e1 = %s)\n", type->toChars(), to->toChars(), e1->toChars());
|
|
1006 //printf("e1->type = %s\n", e1->type->toChars());
|
|
1007 if (type->equals(e1->type) && to->equals(type))
|
|
1008 return e1;
|
|
1009
|
|
1010 if (e1->isConst() != 1)
|
|
1011 return EXP_CANT_INTERPRET;
|
|
1012
|
|
1013 Type *tb = to->toBasetype();
|
|
1014 if (tb->ty == Tbool)
|
|
1015 e = new IntegerExp(loc, e1->toInteger() != 0, type);
|
|
1016 else if (type->isintegral())
|
|
1017 {
|
|
1018 if (e1->type->isfloating())
|
|
1019 { integer_t result;
|
|
1020 real_t r = e1->toReal();
|
|
1021
|
|
1022 switch (type->toBasetype()->ty)
|
|
1023 {
|
|
1024 case Tint8: result = (d_int8)r; break;
|
|
1025 case Tchar:
|
|
1026 case Tuns8: result = (d_uns8)r; break;
|
|
1027 case Tint16: result = (d_int16)r; break;
|
|
1028 case Twchar:
|
|
1029 case Tuns16: result = (d_uns16)r; break;
|
|
1030 case Tint32: result = (d_int32)r; break;
|
|
1031 case Tdchar:
|
|
1032 case Tuns32: result = (d_uns32)r; break;
|
|
1033 case Tint64: result = (d_int64)r; break;
|
|
1034 case Tuns64: result = (d_uns64)r; break;
|
|
1035 default:
|
|
1036 assert(0);
|
|
1037 }
|
|
1038
|
|
1039 e = new IntegerExp(loc, result, type);
|
|
1040 }
|
|
1041 else if (type->isunsigned())
|
|
1042 e = new IntegerExp(loc, e1->toUInteger(), type);
|
|
1043 else
|
|
1044 e = new IntegerExp(loc, e1->toInteger(), type);
|
|
1045 }
|
|
1046 else if (tb->isreal())
|
|
1047 { real_t value = e1->toReal();
|
|
1048
|
|
1049 e = new RealExp(loc, value, type);
|
|
1050 }
|
|
1051 else if (tb->isimaginary())
|
|
1052 { real_t value = e1->toImaginary();
|
|
1053
|
|
1054 e = new RealExp(loc, value, type);
|
|
1055 }
|
|
1056 else if (tb->iscomplex())
|
|
1057 { complex_t value = e1->toComplex();
|
|
1058
|
|
1059 e = new ComplexExp(loc, value, type);
|
|
1060 }
|
|
1061 else if (tb->isscalar())
|
|
1062 e = new IntegerExp(loc, e1->toInteger(), type);
|
|
1063 else if (tb->ty == Tvoid)
|
|
1064 e = EXP_CANT_INTERPRET;
|
|
1065 else if (tb->ty == Tstruct && e1->op == TOKint64)
|
|
1066 { // Struct = 0;
|
|
1067 StructDeclaration *sd = tb->toDsymbol(NULL)->isStructDeclaration();
|
|
1068 assert(sd);
|
|
1069 Expressions *elements = new Expressions;
|
|
1070 for (size_t i = 0; i < sd->fields.dim; i++)
|
|
1071 { Dsymbol *s = (Dsymbol *)sd->fields.data[i];
|
|
1072 VarDeclaration *v = s->isVarDeclaration();
|
|
1073 assert(v);
|
|
1074
|
|
1075 Expression *exp = new IntegerExp(0);
|
|
1076 exp = Cast(v->type, v->type, exp);
|
|
1077 if (exp == EXP_CANT_INTERPRET)
|
|
1078 return exp;
|
|
1079 elements->push(exp);
|
|
1080 }
|
|
1081 e = new StructLiteralExp(loc, sd, elements);
|
|
1082 e->type = type;
|
|
1083 }
|
|
1084 else
|
|
1085 {
|
19
|
1086 error(loc, "cannot cast %s to %s", e1->type->toChars(), type->toChars());
|
1
|
1087 e = new IntegerExp(loc, 0, type);
|
|
1088 }
|
|
1089 return e;
|
|
1090 }
|
|
1091
|
|
1092
|
|
1093 Expression *ArrayLength(Type *type, Expression *e1)
|
|
1094 { Expression *e;
|
|
1095 Loc loc = e1->loc;
|
|
1096
|
|
1097 if (e1->op == TOKstring)
|
|
1098 { StringExp *es1 = (StringExp *)e1;
|
|
1099
|
|
1100 e = new IntegerExp(loc, es1->len, type);
|
|
1101 }
|
|
1102 else if (e1->op == TOKarrayliteral)
|
|
1103 { ArrayLiteralExp *ale = (ArrayLiteralExp *)e1;
|
|
1104 size_t dim;
|
|
1105
|
|
1106 dim = ale->elements ? ale->elements->dim : 0;
|
|
1107 e = new IntegerExp(loc, dim, type);
|
|
1108 }
|
|
1109 else if (e1->op == TOKassocarrayliteral)
|
|
1110 { AssocArrayLiteralExp *ale = (AssocArrayLiteralExp *)e1;
|
|
1111 size_t dim = ale->keys->dim;
|
|
1112
|
|
1113 e = new IntegerExp(loc, dim, type);
|
|
1114 }
|
|
1115 else
|
|
1116 e = EXP_CANT_INTERPRET;
|
|
1117 return e;
|
|
1118 }
|
|
1119
|
|
1120 /* Also return EXP_CANT_INTERPRET if this fails
|
|
1121 */
|
|
1122 Expression *Index(Type *type, Expression *e1, Expression *e2)
|
|
1123 { Expression *e = EXP_CANT_INTERPRET;
|
|
1124 Loc loc = e1->loc;
|
|
1125
|
|
1126 //printf("Index(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars());
|
|
1127 assert(e1->type);
|
|
1128 if (e1->op == TOKstring && e2->op == TOKint64)
|
|
1129 { StringExp *es1 = (StringExp *)e1;
|
|
1130 uinteger_t i = e2->toInteger();
|
|
1131
|
|
1132 if (i >= es1->len)
|
|
1133 e1->error("string index %ju is out of bounds [0 .. %zu]", i, es1->len);
|
|
1134 else
|
|
1135 { integer_t value;
|
|
1136
|
|
1137 switch (es1->sz)
|
|
1138 {
|
|
1139 case 1:
|
|
1140 value = ((unsigned char *)es1->string)[i];
|
|
1141 break;
|
|
1142
|
|
1143 case 2:
|
|
1144 value = ((unsigned short *)es1->string)[i];
|
|
1145 break;
|
|
1146
|
|
1147 case 4:
|
|
1148 value = ((unsigned int *)es1->string)[i];
|
|
1149 break;
|
|
1150
|
|
1151 default:
|
|
1152 assert(0);
|
|
1153 break;
|
|
1154 }
|
|
1155 e = new IntegerExp(loc, value, type);
|
|
1156 }
|
|
1157 }
|
|
1158 else if (e1->type->toBasetype()->ty == Tsarray && e2->op == TOKint64)
|
|
1159 { TypeSArray *tsa = (TypeSArray *)e1->type->toBasetype();
|
|
1160 uinteger_t length = tsa->dim->toInteger();
|
|
1161 uinteger_t i = e2->toInteger();
|
|
1162
|
|
1163 if (i >= length)
|
|
1164 { e2->error("array index %ju is out of bounds %s[0 .. %ju]", i, e1->toChars(), length);
|
|
1165 }
|
|
1166 else if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2))
|
|
1167 { ArrayLiteralExp *ale = (ArrayLiteralExp *)e1;
|
|
1168 e = (Expression *)ale->elements->data[i];
|
|
1169 e->type = type;
|
|
1170 }
|
|
1171 }
|
|
1172 else if (e1->type->toBasetype()->ty == Tarray && e2->op == TOKint64)
|
|
1173 {
|
|
1174 uinteger_t i = e2->toInteger();
|
|
1175
|
|
1176 if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2))
|
|
1177 { ArrayLiteralExp *ale = (ArrayLiteralExp *)e1;
|
|
1178 if (i >= ale->elements->dim)
|
|
1179 { e2->error("array index %ju is out of bounds %s[0 .. %u]", i, e1->toChars(), ale->elements->dim);
|
|
1180 }
|
|
1181 else
|
|
1182 { e = (Expression *)ale->elements->data[i];
|
|
1183 e->type = type;
|
|
1184 }
|
|
1185 }
|
|
1186 }
|
|
1187 else if (e1->op == TOKassocarrayliteral && !e1->checkSideEffect(2))
|
|
1188 {
|
|
1189 AssocArrayLiteralExp *ae = (AssocArrayLiteralExp *)e1;
|
|
1190 /* Search the keys backwards, in case there are duplicate keys
|
|
1191 */
|
|
1192 for (size_t i = ae->keys->dim; i;)
|
|
1193 {
|
|
1194 i--;
|
|
1195 Expression *ekey = (Expression *)ae->keys->data[i];
|
|
1196 Expression *ex = Equal(TOKequal, Type::tbool, ekey, e2);
|
|
1197 if (ex == EXP_CANT_INTERPRET)
|
|
1198 return ex;
|
|
1199 if (ex->isBool(TRUE))
|
|
1200 { e = (Expression *)ae->values->data[i];
|
|
1201 e->type = type;
|
|
1202 break;
|
|
1203 }
|
|
1204 }
|
|
1205 }
|
|
1206 return e;
|
|
1207 }
|
|
1208
|
|
1209 /* Also return EXP_CANT_INTERPRET if this fails
|
|
1210 */
|
|
1211 Expression *Slice(Type *type, Expression *e1, Expression *lwr, Expression *upr)
|
|
1212 { Expression *e = EXP_CANT_INTERPRET;
|
|
1213 Loc loc = e1->loc;
|
|
1214
|
|
1215 #if LOG
|
|
1216 printf("Slice()\n");
|
|
1217 if (lwr)
|
|
1218 { printf("\te1 = %s\n", e1->toChars());
|
|
1219 printf("\tlwr = %s\n", lwr->toChars());
|
|
1220 printf("\tupr = %s\n", upr->toChars());
|
|
1221 }
|
|
1222 #endif
|
|
1223 if (e1->op == TOKstring && lwr->op == TOKint64 && upr->op == TOKint64)
|
|
1224 { StringExp *es1 = (StringExp *)e1;
|
|
1225 uinteger_t ilwr = lwr->toInteger();
|
|
1226 uinteger_t iupr = upr->toInteger();
|
|
1227
|
|
1228 if (iupr > es1->len || ilwr > iupr)
|
|
1229 e1->error("string slice [%ju .. %ju] is out of bounds", ilwr, iupr);
|
|
1230 else
|
|
1231 { integer_t value;
|
|
1232 void *s;
|
|
1233 size_t len = iupr - ilwr;
|
|
1234 int sz = es1->sz;
|
|
1235 StringExp *es;
|
|
1236
|
|
1237 s = mem.malloc((len + 1) * sz);
|
|
1238 memcpy((unsigned char *)s, (unsigned char *)es1->string + ilwr * sz, len * sz);
|
|
1239 memset((unsigned char *)s + len * sz, 0, sz);
|
|
1240
|
|
1241 es = new StringExp(loc, s, len, es1->postfix);
|
|
1242 es->sz = sz;
|
|
1243 es->committed = 1;
|
|
1244 es->type = type;
|
|
1245 e = es;
|
|
1246 }
|
|
1247 }
|
|
1248 else if (e1->op == TOKarrayliteral &&
|
|
1249 lwr->op == TOKint64 && upr->op == TOKint64 &&
|
|
1250 !e1->checkSideEffect(2))
|
|
1251 { ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
|
|
1252 uinteger_t ilwr = lwr->toInteger();
|
|
1253 uinteger_t iupr = upr->toInteger();
|
|
1254
|
|
1255 if (iupr > es1->elements->dim || ilwr > iupr)
|
|
1256 e1->error("array slice [%ju .. %ju] is out of bounds", ilwr, iupr);
|
|
1257 else
|
|
1258 {
|
|
1259 Expressions *elements = new Expressions();
|
|
1260 elements->setDim(iupr - ilwr);
|
|
1261 memcpy(elements->data,
|
|
1262 es1->elements->data + ilwr,
|
|
1263 (iupr - ilwr) * sizeof(es1->elements->data[0]));
|
|
1264 e = new ArrayLiteralExp(e1->loc, elements);
|
|
1265 e->type = type;
|
|
1266 }
|
|
1267 }
|
|
1268 return e;
|
|
1269 }
|
|
1270
|
|
1271 /* Also return EXP_CANT_INTERPRET if this fails
|
|
1272 */
|
|
1273 Expression *Cat(Type *type, Expression *e1, Expression *e2)
|
|
1274 { Expression *e = EXP_CANT_INTERPRET;
|
|
1275 Loc loc = e1->loc;
|
|
1276 Type *t;
|
|
1277
|
|
1278 //printf("Cat(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars());
|
|
1279
|
19
|
1280 if (e1->op == TOKnull && (e2->op == TOKint64 || e2->op == TOKstructliteral))
|
1
|
1281 { e = e2;
|
|
1282 goto L2;
|
|
1283 }
|
19
|
1284 else if ((e1->op == TOKint64 || e1->op == TOKstructliteral) && e2->op == TOKnull)
|
1
|
1285 { e = e1;
|
|
1286 L2:
|
|
1287 Type *tn = e->type->toBasetype();
|
|
1288 if (tn->ty == Tchar || tn->ty == Twchar || tn->ty == Tdchar)
|
|
1289 {
|
|
1290 // Create a StringExp
|
|
1291 void *s;
|
|
1292 StringExp *es;
|
|
1293 size_t len = 1;
|
|
1294 int sz = tn->size();
|
|
1295 integer_t v = e->toInteger();
|
|
1296
|
|
1297 s = mem.malloc((len + 1) * sz);
|
|
1298 memcpy((unsigned char *)s, &v, sz);
|
|
1299
|
|
1300 // Add terminating 0
|
|
1301 memset((unsigned char *)s + len * sz, 0, sz);
|
|
1302
|
|
1303 es = new StringExp(loc, s, len);
|
|
1304 es->sz = sz;
|
|
1305 es->committed = 1;
|
|
1306 e = es;
|
|
1307 }
|
|
1308 else
|
|
1309 { // Create an ArrayLiteralExp
|
|
1310 Expressions *elements = new Expressions();
|
|
1311 elements->push(e);
|
|
1312 e = new ArrayLiteralExp(e->loc, elements);
|
|
1313 }
|
|
1314 e->type = type;
|
|
1315 return e;
|
|
1316 }
|
|
1317 else if (e1->op == TOKstring && e2->op == TOKstring)
|
|
1318 {
|
|
1319 // Concatenate the strings
|
|
1320 void *s;
|
|
1321 StringExp *es1 = (StringExp *)e1;
|
|
1322 StringExp *es2 = (StringExp *)e2;
|
|
1323 StringExp *es;
|
|
1324 Type *t;
|
|
1325 size_t len = es1->len + es2->len;
|
|
1326 int sz = es1->sz;
|
|
1327
|
|
1328 assert(sz == es2->sz);
|
|
1329 s = mem.malloc((len + 1) * sz);
|
|
1330 memcpy(s, es1->string, es1->len * sz);
|
|
1331 memcpy((unsigned char *)s + es1->len * sz, es2->string, es2->len * sz);
|
|
1332
|
|
1333 // Add terminating 0
|
|
1334 memset((unsigned char *)s + len * sz, 0, sz);
|
|
1335
|
|
1336 es = new StringExp(loc, s, len);
|
|
1337 es->sz = sz;
|
|
1338 es->committed = es1->committed | es2->committed;
|
|
1339 if (es1->committed)
|
|
1340 t = es1->type;
|
|
1341 else
|
|
1342 t = es2->type;
|
|
1343 es->type = type;
|
|
1344 e = es;
|
|
1345 }
|
|
1346 else if (e1->op == TOKstring && e2->op == TOKint64)
|
|
1347 {
|
|
1348 // Concatenate the strings
|
|
1349 void *s;
|
|
1350 StringExp *es1 = (StringExp *)e1;
|
|
1351 StringExp *es;
|
|
1352 Type *t;
|
|
1353 size_t len = es1->len + 1;
|
|
1354 int sz = es1->sz;
|
|
1355 integer_t v = e2->toInteger();
|
|
1356
|
|
1357 s = mem.malloc((len + 1) * sz);
|
|
1358 memcpy(s, es1->string, es1->len * sz);
|
|
1359 memcpy((unsigned char *)s + es1->len * sz, &v, sz);
|
|
1360
|
|
1361 // Add terminating 0
|
|
1362 memset((unsigned char *)s + len * sz, 0, sz);
|
|
1363
|
|
1364 es = new StringExp(loc, s, len);
|
|
1365 es->sz = sz;
|
|
1366 es->committed = es1->committed;
|
|
1367 t = es1->type;
|
|
1368 es->type = type;
|
|
1369 e = es;
|
|
1370 }
|
|
1371 else if (e1->op == TOKint64 && e2->op == TOKstring)
|
|
1372 {
|
|
1373 // Concatenate the strings
|
|
1374 void *s;
|
|
1375 StringExp *es2 = (StringExp *)e2;
|
|
1376 StringExp *es;
|
|
1377 Type *t;
|
|
1378 size_t len = 1 + es2->len;
|
|
1379 int sz = es2->sz;
|
|
1380 integer_t v = e1->toInteger();
|
|
1381
|
|
1382 s = mem.malloc((len + 1) * sz);
|
|
1383 memcpy((unsigned char *)s, &v, sz);
|
|
1384 memcpy((unsigned char *)s + sz, es2->string, es2->len * sz);
|
|
1385
|
|
1386 // Add terminating 0
|
|
1387 memset((unsigned char *)s + len * sz, 0, sz);
|
|
1388
|
|
1389 es = new StringExp(loc, s, len);
|
|
1390 es->sz = sz;
|
|
1391 es->committed = es2->committed;
|
|
1392 t = es2->type;
|
|
1393 es->type = type;
|
|
1394 e = es;
|
|
1395 }
|
|
1396 else if (e1->op == TOKarrayliteral && e2->op == TOKarrayliteral &&
|
|
1397 e1->type->equals(e2->type))
|
|
1398 {
|
|
1399 // Concatenate the arrays
|
|
1400 ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
|
|
1401 ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
|
|
1402
|
|
1403 es1 = new ArrayLiteralExp(es1->loc, (Expressions *)es1->elements->copy());
|
|
1404 es1->elements->insert(es1->elements->dim, es2->elements);
|
|
1405 e = es1;
|
|
1406
|
|
1407 if (type->toBasetype()->ty == Tsarray)
|
|
1408 {
|
|
1409 e->type = new TypeSArray(e1->type->toBasetype()->next, new IntegerExp(0, es1->elements->dim, Type::tindex));
|
|
1410 e->type = e->type->semantic(loc, NULL);
|
|
1411 }
|
|
1412 else
|
|
1413 e->type = type;
|
|
1414 }
|
|
1415 else if (e1->op == TOKarrayliteral &&
|
19
|
1416 e1->type->toBasetype()->nextOf()->equals(e2->type))
|
1
|
1417 {
|
|
1418 ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
|
|
1419
|
|
1420 es1 = new ArrayLiteralExp(es1->loc, (Expressions *)es1->elements->copy());
|
|
1421 es1->elements->push(e2);
|
|
1422 e = es1;
|
|
1423
|
|
1424 if (type->toBasetype()->ty == Tsarray)
|
|
1425 {
|
|
1426 e->type = new TypeSArray(e2->type, new IntegerExp(0, es1->elements->dim, Type::tindex));
|
|
1427 e->type = e->type->semantic(loc, NULL);
|
|
1428 }
|
|
1429 else
|
|
1430 e->type = type;
|
|
1431 }
|
|
1432 else if (e2->op == TOKarrayliteral &&
|
19
|
1433 e2->type->toBasetype()->nextOf()->equals(e1->type))
|
1
|
1434 {
|
|
1435 ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
|
|
1436
|
|
1437 es2 = new ArrayLiteralExp(es2->loc, (Expressions *)es2->elements->copy());
|
|
1438 es2->elements->shift(e1);
|
|
1439 e = es2;
|
|
1440
|
|
1441 if (type->toBasetype()->ty == Tsarray)
|
|
1442 {
|
|
1443 e->type = new TypeSArray(e1->type, new IntegerExp(0, es2->elements->dim, Type::tindex));
|
|
1444 e->type = e->type->semantic(loc, NULL);
|
|
1445 }
|
|
1446 else
|
|
1447 e->type = type;
|
|
1448 }
|
|
1449 else if (e1->op == TOKnull && e2->op == TOKstring)
|
|
1450 {
|
|
1451 t = e1->type;
|
|
1452 e = e2;
|
|
1453 goto L1;
|
|
1454 }
|
|
1455 else if (e1->op == TOKstring && e2->op == TOKnull)
|
|
1456 { e = e1;
|
|
1457 t = e2->type;
|
|
1458 L1:
|
|
1459 Type *tb = t->toBasetype();
|
19
|
1460 if (tb->ty == Tarray && tb->nextOf()->equals(e->type))
|
1
|
1461 { Expressions *expressions = new Expressions();
|
|
1462 expressions->push(e);
|
|
1463 e = new ArrayLiteralExp(loc, expressions);
|
|
1464 e->type = t;
|
|
1465 }
|
|
1466 if (!e->type->equals(type))
|
|
1467 { StringExp *se = (StringExp *)e->copy();
|
|
1468 e = se->castTo(NULL, type);
|
|
1469 }
|
|
1470 }
|
|
1471 return e;
|
|
1472 }
|
|
1473
|
|
1474 Expression *Ptr(Type *type, Expression *e1)
|
|
1475 {
|
|
1476 //printf("Ptr(e1 = %s)\n", e1->toChars());
|
|
1477 if (e1->op == TOKadd)
|
|
1478 { AddExp *ae = (AddExp *)e1;
|
|
1479 if (ae->e1->op == TOKaddress && ae->e2->op == TOKint64)
|
|
1480 { AddrExp *ade = (AddrExp *)ae->e1;
|
|
1481 if (ade->e1->op == TOKstructliteral)
|
|
1482 { StructLiteralExp *se = (StructLiteralExp *)ade->e1;
|
|
1483 unsigned offset = ae->e2->toInteger();
|
|
1484 Expression *e = se->getField(type, offset);
|
|
1485 if (!e)
|
|
1486 e = EXP_CANT_INTERPRET;
|
|
1487 return e;
|
|
1488 }
|
|
1489 }
|
|
1490 }
|
|
1491 return EXP_CANT_INTERPRET;
|
|
1492 }
|
|
1493
|