Mercurial > projects > ldc
comparison dmd2/constfold.c @ 1452:638d16625da2
LDC 2 compiles again.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Sat, 30 May 2009 17:23:32 +0100 |
parents | f04dde6e882c |
children | 54b3c1394d62 |
comparison
equal
deleted
inserted
replaced
1423:42bd767ec5a4 | 1452:638d16625da2 |
---|---|
1 | 1 |
2 // Compiler implementation of the D programming language | 2 // Compiler implementation of the D programming language |
3 // Copyright (c) 1999-2007 by Digital Mars | 3 // Copyright (c) 1999-2009 by Digital Mars |
4 // All Rights Reserved | 4 // All Rights Reserved |
5 // written by Walter Bright | 5 // written by Walter Bright |
6 // http://www.digitalmars.com | 6 // http://www.digitalmars.com |
7 // License for redistribution is by either the Artistic License | 7 // License for redistribution is by either the Artistic License |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | 8 // in artistic.txt, or the GNU General Public License in gnu.txt. |
15 | 15 |
16 #if __DMC__ | 16 #if __DMC__ |
17 #include <complex.h> | 17 #include <complex.h> |
18 #endif | 18 #endif |
19 | 19 |
20 #include "mem.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" |
27 | 28 |
28 #ifdef IN_GCC | 29 #if __FreeBSD__ |
29 #include "d-gcc-real.h" | 30 #define fmodl fmod // hack for now, fix later |
30 | 31 #endif |
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 | 32 |
37 #define LOG 0 | 33 #define LOG 0 |
38 | 34 |
39 Expression *expType(Type *type, Expression *e) | 35 Expression *expType(Type *type, Expression *e) |
40 { | 36 { |
553 | 549 |
554 Expression *Shr(Type *type, Expression *e1, Expression *e2) | 550 Expression *Shr(Type *type, Expression *e1, Expression *e2) |
555 { Expression *e; | 551 { Expression *e; |
556 Loc loc = e1->loc; | 552 Loc loc = e1->loc; |
557 unsigned count; | 553 unsigned count; |
558 integer_t value; | 554 dinteger_t value; |
559 | 555 |
560 value = e1->toInteger(); | 556 value = e1->toInteger(); |
561 count = e2->toInteger(); | 557 count = e2->toInteger(); |
562 switch (e1->type->toBasetype()->ty) | 558 switch (e1->type->toBasetype()->ty) |
563 { | 559 { |
602 | 598 |
603 Expression *Ushr(Type *type, Expression *e1, Expression *e2) | 599 Expression *Ushr(Type *type, Expression *e1, Expression *e2) |
604 { Expression *e; | 600 { Expression *e; |
605 Loc loc = e1->loc; | 601 Loc loc = e1->loc; |
606 unsigned count; | 602 unsigned count; |
607 integer_t value; | 603 dinteger_t value; |
608 | 604 |
609 value = e1->toInteger(); | 605 value = e1->toInteger(); |
610 count = e2->toInteger(); | 606 count = e2->toInteger(); |
611 switch (e1->type->toBasetype()->ty) | 607 switch (e1->type->toBasetype()->ty) |
612 { | 608 { |
824 r2 = e2->toImaginary(); | 820 r2 = e2->toImaginary(); |
825 L1: | 821 L1: |
826 #if __DMC__ | 822 #if __DMC__ |
827 cmp = (r1 == r2); | 823 cmp = (r1 == r2); |
828 #else | 824 #else |
829 if (isnan(r1) || isnan(r2)) // if unordered | 825 if (Port::isNan(r1) || Port::isNan(r2)) // if unordered |
830 { | 826 { |
831 cmp = 0; | 827 cmp = 0; |
832 } | 828 } |
833 else | 829 else |
834 { | 830 { |
880 | 876 |
881 | 877 |
882 Expression *Cmp(enum TOK op, Type *type, Expression *e1, Expression *e2) | 878 Expression *Cmp(enum TOK op, Type *type, Expression *e1, Expression *e2) |
883 { Expression *e; | 879 { Expression *e; |
884 Loc loc = e1->loc; | 880 Loc loc = e1->loc; |
885 integer_t n; | 881 dinteger_t n; |
886 real_t r1; | 882 real_t r1; |
887 real_t r2; | 883 real_t r2; |
888 | 884 |
889 //printf("Cmp(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); | 885 //printf("Cmp(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars()); |
890 | 886 |
957 default: | 953 default: |
958 assert(0); | 954 assert(0); |
959 } | 955 } |
960 #else | 956 #else |
961 // Don't rely on compiler, handle NAN arguments separately | 957 // Don't rely on compiler, handle NAN arguments separately |
962 #if IN_GCC | 958 if (Port::isNan(r1) || Port::isNan(r2)) // if unordered |
963 if (real_isnan(&r1) || real_isnan(&r2)) // if unordered | |
964 #else | |
965 if (isnan(r1) || isnan(r2)) // if unordered | |
966 #endif | |
967 { | 959 { |
968 switch (op) | 960 switch (op) |
969 { | 961 { |
970 case TOKlt: n = 0; break; | 962 case TOKlt: n = 0; break; |
971 case TOKle: n = 0; break; | 963 case TOKle: n = 0; break; |
1103 if (tb->ty == Tbool) | 1095 if (tb->ty == Tbool) |
1104 e = new IntegerExp(loc, e1->toInteger() != 0, type); | 1096 e = new IntegerExp(loc, e1->toInteger() != 0, type); |
1105 else if (type->isintegral()) | 1097 else if (type->isintegral()) |
1106 { | 1098 { |
1107 if (e1->type->isfloating()) | 1099 if (e1->type->isfloating()) |
1108 { integer_t result; | 1100 { dinteger_t result; |
1109 real_t r = e1->toReal(); | 1101 real_t r = e1->toReal(); |
1110 | 1102 |
1111 switch (typeb->ty) | 1103 switch (typeb->ty) |
1112 { | 1104 { |
1113 case Tint8: result = (d_int8)r; break; | 1105 case Tint8: result = (d_int8)r; break; |
1217 if (e1->op == TOKstring && e2->op == TOKint64) | 1209 if (e1->op == TOKstring && e2->op == TOKint64) |
1218 { StringExp *es1 = (StringExp *)e1; | 1210 { StringExp *es1 = (StringExp *)e1; |
1219 uinteger_t i = e2->toInteger(); | 1211 uinteger_t i = e2->toInteger(); |
1220 | 1212 |
1221 if (i >= es1->len) | 1213 if (i >= es1->len) |
1222 e1->error("string index %llu is out of bounds [0 .. %"PRIuSIZE"]", i, es1->len); | 1214 e1->error("string index %ju is out of bounds [0 .. %zu]", i, es1->len); |
1223 else | 1215 else |
1224 { unsigned value = es1->charAt(i); | 1216 { unsigned value = es1->charAt(i); |
1225 e = new IntegerExp(loc, value, type); | 1217 e = new IntegerExp(loc, value, type); |
1226 } | 1218 } |
1227 } | 1219 } |
1229 { TypeSArray *tsa = (TypeSArray *)e1->type->toBasetype(); | 1221 { TypeSArray *tsa = (TypeSArray *)e1->type->toBasetype(); |
1230 uinteger_t length = tsa->dim->toInteger(); | 1222 uinteger_t length = tsa->dim->toInteger(); |
1231 uinteger_t i = e2->toInteger(); | 1223 uinteger_t i = e2->toInteger(); |
1232 | 1224 |
1233 if (i >= length) | 1225 if (i >= length) |
1234 { | 1226 { e2->error("array index %ju is out of bounds %s[0 .. %ju]", i, e1->toChars(), length); |
1235 e2->error("array index %llu is out of bounds %s[0 .. %llu]", i, e1->toChars(), length); | |
1236 } | 1227 } |
1237 else if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2)) | 1228 else if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2)) |
1238 { ArrayLiteralExp *ale = (ArrayLiteralExp *)e1; | 1229 { ArrayLiteralExp *ale = (ArrayLiteralExp *)e1; |
1239 e = (Expression *)ale->elements->data[i]; | 1230 e = (Expression *)ale->elements->data[i]; |
1240 e->type = type; | 1231 e->type = type; |
1245 uinteger_t i = e2->toInteger(); | 1236 uinteger_t i = e2->toInteger(); |
1246 | 1237 |
1247 if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2)) | 1238 if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2)) |
1248 { ArrayLiteralExp *ale = (ArrayLiteralExp *)e1; | 1239 { ArrayLiteralExp *ale = (ArrayLiteralExp *)e1; |
1249 if (i >= ale->elements->dim) | 1240 if (i >= ale->elements->dim) |
1250 { | 1241 { e2->error("array index %ju is out of bounds %s[0 .. %u]", i, e1->toChars(), ale->elements->dim); |
1251 e2->error("array index %llu is out of bounds %s[0 .. %u]", i, e1->toChars(), ale->elements->dim); | |
1252 } | 1242 } |
1253 else | 1243 else |
1254 { e = (Expression *)ale->elements->data[i]; | 1244 { e = (Expression *)ale->elements->data[i]; |
1255 e->type = type; | 1245 e->type = type; |
1256 } | 1246 } |
1296 { StringExp *es1 = (StringExp *)e1; | 1286 { StringExp *es1 = (StringExp *)e1; |
1297 uinteger_t ilwr = lwr->toInteger(); | 1287 uinteger_t ilwr = lwr->toInteger(); |
1298 uinteger_t iupr = upr->toInteger(); | 1288 uinteger_t iupr = upr->toInteger(); |
1299 | 1289 |
1300 if (iupr > es1->len || ilwr > iupr) | 1290 if (iupr > es1->len || ilwr > iupr) |
1301 e1->error("string slice [%llu .. %llu] is out of bounds", ilwr, iupr); | 1291 e1->error("string slice [%ju .. %ju] is out of bounds", ilwr, iupr); |
1302 else | 1292 else |
1303 { integer_t value; | 1293 { dinteger_t value; |
1304 void *s; | 1294 void *s; |
1305 size_t len = iupr - ilwr; | 1295 size_t len = iupr - ilwr; |
1306 int sz = es1->sz; | 1296 int sz = es1->sz; |
1307 StringExp *es; | 1297 StringExp *es; |
1308 | 1298 |
1323 { ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1; | 1313 { ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1; |
1324 uinteger_t ilwr = lwr->toInteger(); | 1314 uinteger_t ilwr = lwr->toInteger(); |
1325 uinteger_t iupr = upr->toInteger(); | 1315 uinteger_t iupr = upr->toInteger(); |
1326 | 1316 |
1327 if (iupr > es1->elements->dim || ilwr > iupr) | 1317 if (iupr > es1->elements->dim || ilwr > iupr) |
1328 e1->error("array slice [%llu .. %llu] is out of bounds", ilwr, iupr); | 1318 e1->error("array slice [%ju .. %ju] is out of bounds", ilwr, iupr); |
1329 else | 1319 else |
1330 { | 1320 { |
1331 Expressions *elements = new Expressions(); | 1321 Expressions *elements = new Expressions(); |
1332 elements->setDim(iupr - ilwr); | 1322 elements->setDim(iupr - ilwr); |
1333 memcpy(elements->data, | 1323 memcpy(elements->data, |
1365 // Create a StringExp | 1355 // Create a StringExp |
1366 void *s; | 1356 void *s; |
1367 StringExp *es; | 1357 StringExp *es; |
1368 size_t len = 1; | 1358 size_t len = 1; |
1369 int sz = tn->size(); | 1359 int sz = tn->size(); |
1370 integer_t v = e->toInteger(); | 1360 dinteger_t v = e->toInteger(); |
1371 | 1361 |
1372 s = mem.malloc((len + 1) * sz); | 1362 s = mem.malloc((len + 1) * sz); |
1373 memcpy((unsigned char *)s, &v, sz); | 1363 memcpy((unsigned char *)s, &v, sz); |
1374 | 1364 |
1375 // Add terminating 0 | 1365 // Add terminating 0 |
1432 StringExp *es1 = (StringExp *)e1; | 1422 StringExp *es1 = (StringExp *)e1; |
1433 StringExp *es; | 1423 StringExp *es; |
1434 Type *t; | 1424 Type *t; |
1435 size_t len = es1->len + 1; | 1425 size_t len = es1->len + 1; |
1436 int sz = es1->sz; | 1426 int sz = es1->sz; |
1437 integer_t v = e2->toInteger(); | 1427 dinteger_t v = e2->toInteger(); |
1438 | 1428 |
1439 s = mem.malloc((len + 1) * sz); | 1429 s = mem.malloc((len + 1) * sz); |
1440 memcpy(s, es1->string, es1->len * sz); | 1430 memcpy(s, es1->string, es1->len * sz); |
1441 memcpy((unsigned char *)s + es1->len * sz, &v, sz); | 1431 memcpy((unsigned char *)s + es1->len * sz, &v, sz); |
1442 | 1432 |
1457 StringExp *es2 = (StringExp *)e2; | 1447 StringExp *es2 = (StringExp *)e2; |
1458 StringExp *es; | 1448 StringExp *es; |
1459 Type *t; | 1449 Type *t; |
1460 size_t len = 1 + es2->len; | 1450 size_t len = 1 + es2->len; |
1461 int sz = es2->sz; | 1451 int sz = es2->sz; |
1462 integer_t v = e1->toInteger(); | 1452 dinteger_t v = e1->toInteger(); |
1463 | 1453 |
1464 s = mem.malloc((len + 1) * sz); | 1454 s = mem.malloc((len + 1) * sz); |
1465 memcpy((unsigned char *)s, &v, sz); | 1455 memcpy((unsigned char *)s, &v, sz); |
1466 memcpy((unsigned char *)s + sz, es2->string, es2->len * sz); | 1456 memcpy((unsigned char *)s + sz, es2->string, es2->len * sz); |
1467 | 1457 |