Mercurial > projects > ldc
comparison dmd/constfold.c @ 1367:8026319762be
Merged DMD 1.045 !!!
author | Tomas Lindquist Olsen <tomas.l.olsen gmail com> |
---|---|
date | Sat, 16 May 2009 22:21:31 +0200 |
parents | 6bae1c30480f |
children | def7a1d494fd |
comparison
equal
deleted
inserted
replaced
1366:81121ac19f61 | 1367:8026319762be |
---|---|
17 #include <complex.h> | 17 #include <complex.h> |
18 #endif | 18 #endif |
19 | 19 |
20 #include "rmem.h" | 20 #include "rmem.h" |
21 #include "root.h" | 21 #include "root.h" |
22 #include "port.h" | |
22 | 23 |
23 #include "mtype.h" | 24 #include "mtype.h" |
24 #include "expression.h" | 25 #include "expression.h" |
25 #include "aggregate.h" | 26 #include "aggregate.h" |
26 #include "declaration.h" | 27 #include "declaration.h" |
31 /* %% fix? */ | 32 /* %% fix? */ |
32 extern "C" bool real_isnan (const real_t *); | 33 extern "C" bool real_isnan (const real_t *); |
33 #endif | 34 #endif |
34 | 35 |
35 static real_t zero; // work around DMC bug for now | 36 static real_t zero; // work around DMC bug for now |
37 | |
38 #if __FreeBSD__ | |
39 #define fmodl fmod // hack for now, fix later | |
40 #endif | |
36 | 41 |
37 #define LOG 0 | 42 #define LOG 0 |
38 | 43 |
39 Expression *expType(Type *type, Expression *e) | 44 Expression *expType(Type *type, Expression *e) |
40 { | 45 { |
458 | 463 |
459 n1 = e1->toInteger(); | 464 n1 = e1->toInteger(); |
460 n2 = e2->toInteger(); | 465 n2 = e2->toInteger(); |
461 if (n2 == 0) | 466 if (n2 == 0) |
462 { e2->error("divide by 0"); | 467 { e2->error("divide by 0"); |
463 e2 = new IntegerExp(0, 1, e2->type); | 468 e2 = new IntegerExp(loc, 1, e2->type); |
464 n2 = 1; | 469 n2 = 1; |
465 } | 470 } |
466 if (e1->type->isunsigned() || e2->type->isunsigned()) | 471 if (e1->type->isunsigned() || e2->type->isunsigned()) |
467 n = ((d_uns64) n1) / ((d_uns64) n2); | 472 n = ((d_uns64) n1) / ((d_uns64) n2); |
468 else | 473 else |
529 | 534 |
530 n1 = e1->toInteger(); | 535 n1 = e1->toInteger(); |
531 n2 = e2->toInteger(); | 536 n2 = e2->toInteger(); |
532 if (n2 == 0) | 537 if (n2 == 0) |
533 { e2->error("divide by 0"); | 538 { e2->error("divide by 0"); |
534 e2 = new IntegerExp(0, 1, e2->type); | 539 e2 = new IntegerExp(loc, 1, e2->type); |
535 n2 = 1; | 540 n2 = 1; |
536 } | 541 } |
537 if (e1->type->isunsigned() || e2->type->isunsigned()) | 542 if (e1->type->isunsigned() || e2->type->isunsigned()) |
538 n = ((d_uns64) n1) % ((d_uns64) n2); | 543 n = ((d_uns64) n1) % ((d_uns64) n2); |
539 else | 544 else |
638 e = new IntegerExp(loc, value, type); | 643 e = new IntegerExp(loc, value, type); |
639 return e; | 644 return e; |
640 } | 645 } |
641 | 646 |
642 Expression *And(Type *type, Expression *e1, Expression *e2) | 647 Expression *And(Type *type, Expression *e1, Expression *e2) |
643 { Expression *e; | 648 { |
644 Loc loc = e1->loc; | 649 Expression *e; |
645 | 650 e = new IntegerExp(e1->loc, e1->toInteger() & e2->toInteger(), type); |
646 e = new IntegerExp(loc, e1->toInteger() & e2->toInteger(), type); | |
647 return e; | 651 return e; |
648 } | 652 } |
649 | 653 |
650 Expression *Or(Type *type, Expression *e1, Expression *e2) | 654 Expression *Or(Type *type, Expression *e1, Expression *e2) |
651 { Expression *e; | 655 { Expression *e; |
652 Loc loc = e1->loc; | 656 e = new IntegerExp(e1->loc, e1->toInteger() | e2->toInteger(), type); |
653 | |
654 e = new IntegerExp(loc, e1->toInteger() | e2->toInteger(), type); | |
655 return e; | 657 return e; |
656 } | 658 } |
657 | 659 |
658 Expression *Xor(Type *type, Expression *e1, Expression *e2) | 660 Expression *Xor(Type *type, Expression *e1, Expression *e2) |
659 { Expression *e; | 661 { Expression *e; |
660 Loc loc = e1->loc; | 662 e = new IntegerExp(e1->loc, e1->toInteger() ^ e2->toInteger(), type); |
661 | |
662 e = new IntegerExp(loc, e1->toInteger() ^ e2->toInteger(), type); | |
663 return e; | 663 return e; |
664 } | 664 } |
665 | 665 |
666 /* Also returns EXP_CANT_INTERPRET if cannot be computed. | 666 /* Also returns EXP_CANT_INTERPRET if cannot be computed. |
667 */ | 667 */ |
829 r2 = e2->toImaginary(); | 829 r2 = e2->toImaginary(); |
830 L1: | 830 L1: |
831 #if __DMC__ | 831 #if __DMC__ |
832 cmp = (r1 == r2); | 832 cmp = (r1 == r2); |
833 #else | 833 #else |
834 if (isnan(r1) || isnan(r2)) // if unordered | 834 if (Port::isNan(r1) || Port::isNan(r2)) // if unordered |
835 { | 835 { |
836 cmp = 0; | 836 cmp = 0; |
837 } | 837 } |
838 else | 838 else |
839 { | 839 { |
924 default: | 924 default: |
925 assert(0); | 925 assert(0); |
926 } | 926 } |
927 #else | 927 #else |
928 // Don't rely on compiler, handle NAN arguments separately | 928 // Don't rely on compiler, handle NAN arguments separately |
929 #if IN_GCC | 929 if (Port::isNan(r1) || Port::isNan(r2)) // if unordered |
930 if (real_isnan(&r1) || real_isnan(&r2)) // if unordered | |
931 #else | |
932 if (isnan(r1) || isnan(r2)) // if unordered | |
933 #endif | |
934 { | 930 { |
935 switch (op) | 931 switch (op) |
936 { | 932 { |
937 case TOKlt: n = 0; break; | 933 case TOKlt: n = 0; break; |
938 case TOKle: n = 0; break; | 934 case TOKle: n = 0; break; |
1311 Type *t; | 1307 Type *t; |
1312 Type *t1 = e1->type->toBasetype(); | 1308 Type *t1 = e1->type->toBasetype(); |
1313 Type *t2 = e2->type->toBasetype(); | 1309 Type *t2 = e2->type->toBasetype(); |
1314 | 1310 |
1315 //printf("Cat(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); | 1311 //printf("Cat(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); |
1312 //printf("\tt1 = %s, t2 = %s\n", t1->toChars(), t2->toChars()); | |
1316 | 1313 |
1317 if (e1->op == TOKnull && (e2->op == TOKint64 || e2->op == TOKstructliteral)) | 1314 if (e1->op == TOKnull && (e2->op == TOKint64 || e2->op == TOKstructliteral)) |
1318 { e = e2; | 1315 { e = e2; |
1319 goto L2; | 1316 goto L2; |
1320 } | 1317 } |
1496 } | 1493 } |
1497 e = es1; | 1494 e = es1; |
1498 | 1495 |
1499 if (type->toBasetype()->ty == Tsarray) | 1496 if (type->toBasetype()->ty == Tsarray) |
1500 { | 1497 { |
1501 e->type = new TypeSArray(e2->type, new IntegerExp(0, es1->elements->dim, Type::tindex)); | 1498 e->type = new TypeSArray(e2->type, new IntegerExp(loc, es1->elements->dim, Type::tindex)); |
1502 e->type = e->type->semantic(loc, NULL); | 1499 e->type = e->type->semantic(loc, NULL); |
1503 } | 1500 } |
1504 else | 1501 else |
1505 e->type = type; | 1502 e->type = type; |
1506 } | 1503 } |
1513 es2->elements->shift(e1); | 1510 es2->elements->shift(e1); |
1514 e = es2; | 1511 e = es2; |
1515 | 1512 |
1516 if (type->toBasetype()->ty == Tsarray) | 1513 if (type->toBasetype()->ty == Tsarray) |
1517 { | 1514 { |
1518 e->type = new TypeSArray(e1->type, new IntegerExp(0, es2->elements->dim, Type::tindex)); | 1515 e->type = new TypeSArray(e1->type, new IntegerExp(loc, es2->elements->dim, Type::tindex)); |
1519 e->type = e->type->semantic(loc, NULL); | 1516 e->type = e->type->semantic(loc, NULL); |
1520 } | 1517 } |
1521 else | 1518 else |
1522 e->type = type; | 1519 e->type = type; |
1523 } | 1520 } |