Unified · Split
| @@ -41,8 +41,12 @@ typedef struct Sym { · 40 unmodified lines | ||
| 41 | 41 | #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */ |
| 42 | 42 | #define SYM_FIELD 0x20000000 /* struct/union field symbol space */ |
| 43 | 43 | |
| 44 | +#define FUNC_NEW 1 /* ansi function prototype */ | |
| 45 | +#define FUNC_OLD 2 /* old function prototype */ | |
| 46 | +#define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */ | |
| 47 | + | |
| 44 | 48 | typedef struct { |
| 45 | − FILE *file; /* stdio file */ | |
| 49 | + FILE *file; | |
| 46 | 50 | char *filename; |
| 47 | 51 | int line_num; |
| 48 | 52 | } IncludeFile; |
| @@ -139,12 +143,17 @@ IncludeFile include_stack[INCLUDE_STACK_SIZE], *include_stack_ptr; · 90 unmodified lines | ||
| 139 | 143 | #define TOK_SIZEOF 289 |
| 140 | 144 | #define TOK_INCLUDE 290 |
| 141 | 145 | |
| 142 | −#define TOK_EQ 0x94 /* warning: depend on asm code */ | |
| 143 | −#define TOK_NE 0x95 /* warning: depend on asm code */ | |
| 144 | −#define TOK_LT 0x9c /* warning: depend on asm code */ | |
| 145 | −#define TOK_GE 0x9d /* warning: depend on asm code */ | |
| 146 | −#define TOK_LE 0x9e /* warning: depend on asm code */ | |
| 147 | −#define TOK_GT 0x9f /* warning: depend on asm code */ | |
| 146 | +/* warning: the following compare tokens depend on i386 asm code */ | |
| 147 | +#define TOK_ULT 0x92 | |
| 148 | +#define TOK_UGE 0x93 | |
| 149 | +#define TOK_EQ 0x94 | |
| 150 | +#define TOK_NE 0x95 | |
| 151 | +#define TOK_ULE 0x96 | |
| 152 | +#define TOK_UGT 0x97 | |
| 153 | +#define TOK_LT 0x9c | |
| 154 | +#define TOK_GE 0x9d | |
| 155 | +#define TOK_LE 0x9e | |
| 156 | +#define TOK_GT 0x9f | |
| 148 | 157 | |
| 149 | 158 | #define TOK_LAND 0xa0 |
| 150 | 159 | #define TOK_LOR 0xa1 |
| @@ -197,28 +206,41 @@ int isnum(c) · 46 unmodified lines | ||
| 197 | 206 | } |
| 198 | 207 | |
| 199 | 208 | #ifndef TINY |
| 209 | + | |
| 210 | +void printline() | |
| 211 | +{ | |
| 212 | + IncludeFile *f; | |
| 213 | + for(f = include_stack; f < include_stack_ptr; f++) | |
| 214 | + printf("In file included from %s:%d:\n", f->filename, f->line_num); | |
| 215 | + printf("%s:%d: ", filename, line_num); | |
| 216 | +} | |
| 217 | + | |
| 200 | 218 | /* XXX: use stderr ? */ |
| 201 | 219 | void error(char *msg) |
| 202 | 220 | { |
| 203 | − printf("%s:%d: %s\n", filename, line_num, msg); | |
| 221 | + printline(); | |
| 222 | + printf("%s\n", msg); | |
| 204 | 223 | exit(1); |
| 205 | 224 | } |
| 206 | 225 | |
| 207 | 226 | void expect(char *msg) |
| 208 | 227 | { |
| 209 | − printf("%s:%d: %s expected\n", filename, line_num, msg); | |
| 228 | + printline(); | |
| 229 | + printf("%s expected\n", msg); | |
| 210 | 230 | exit(1); |
| 211 | 231 | } |
| 212 | 232 | |
| 213 | 233 | void warning(char *msg) |
| 214 | 234 | { |
| 215 | − printf("%s:%d: warning: %s\n", filename, line_num, msg); | |
| 235 | + printline(); | |
| 236 | + printf("warning: %s\n", msg); | |
| 216 | 237 | } |
| 217 | 238 | |
| 218 | 239 | void skip(c) |
| 219 | 240 | { |
| 220 | 241 | if (tok != c) { |
| 221 | − printf("%s:%d: '%c' expected\n", filename, line_num, c); | |
| 242 | + printline(); | |
| 243 | + printf("'%c' expected\n", c); | |
| 222 | 244 | exit(1); |
| 223 | 245 | } |
| 224 | 246 | next(); |
| @@ -1106,6 +1128,14 @@ void gen_op(op) · 881 unmodified lines | ||
| 1106 | 1128 | op = TOK_UDIV; |
| 1107 | 1129 | else if (op == '%') |
| 1108 | 1130 | op = TOK_UMOD; |
| 1131 | + else if (op == TOK_LT) | |
| 1132 | + op = TOK_ULT; | |
| 1133 | + else if (op == TOK_GT) | |
| 1134 | + op = TOK_UGT; | |
| 1135 | + else if (op == TOK_LE) | |
| 1136 | + op = TOK_ULE; | |
| 1137 | + else if (op == TOK_GE) | |
| 1138 | + op = TOK_UGE; | |
| 1109 | 1139 | } |
| 1110 | 1140 | gen_opc(op); |
| 1111 | 1141 | } |
| @@ -1335,31 +1365,31 @@ int ist() · 223 unmodified lines | ||
| 1335 | 1365 | return t; |
| 1336 | 1366 | } |
| 1337 | 1367 | |
| 1338 | −int post_type(u, t) | |
| 1368 | +int post_type(t) | |
| 1339 | 1369 | { |
| 1340 | − int p, n, pt, l; | |
| 1370 | + int p, n, pt, l, a; | |
| 1371 | + Sym *last, *s; | |
| 1341 | 1372 | |
| 1342 | 1373 | if (tok == '(') { |
| 1343 | 1374 | /* function declaration */ |
| 1344 | 1375 | next(); |
| 1345 | − /* push a dummy symbol to force local symbol stack usage */ | |
| 1346 | − sym_push1(&local_stack, 0, 0, 0); | |
| 1347 | − p = 4; | |
| 1376 | + a = 4; | |
| 1348 | 1377 | l = 0; |
| 1378 | + last = NULL; | |
| 1349 | 1379 | while (tok != ')') { |
| 1350 | 1380 | /* read param name and compute offset */ |
| 1351 | − if (l != 2) { | |
| 1381 | + if (l != FUNC_OLD) { | |
| 1352 | 1382 | if (!(pt = ist())) { |
| 1353 | 1383 | if (l) { |
| 1354 | 1384 | error("invalid type"); |
| 1355 | 1385 | } else { |
| 1356 | − l = 2; | |
| 1386 | + l = FUNC_OLD; | |
| 1357 | 1387 | goto old_proto; |
| 1358 | 1388 | } |
| 1359 | 1389 | } |
| 1360 | 1390 | if (pt & VT_VOID && tok == ')') |
| 1361 | 1391 | break; |
| 1362 | − l = 1; | |
| 1392 | + l = FUNC_NEW; | |
| 1363 | 1393 | pt = typ(&n, pt); /* XXX: should accept |
| 1364 | 1394 | both arg/non arg if v == 0 */ |
| 1365 | 1395 | } else { |
| @@ -1370,23 +1400,27 @@ int post_type(u, t) · 4 unmodified lines | ||
| 1370 | 1400 | } |
| 1371 | 1401 | /* array must be transformed to pointer according to ANSI C */ |
| 1372 | 1402 | pt &= ~VT_ARRAY; |
| 1373 | − p = p + 4; | |
| 1374 | − sym_push(n, VT_LOCAL | VT_LVAL | pt, p); | |
| 1403 | + /* XXX: size will be different someday */ | |
| 1404 | + a = a + 4; | |
| 1405 | + s = sym_push(n | SYM_FIELD, VT_LOCAL | VT_LVAL | pt, a); | |
| 1406 | + s->next = last; | |
| 1407 | + last = s; | |
| 1375 | 1408 | if (tok == ',') { |
| 1376 | 1409 | next(); |
| 1377 | − if (l == 1 && tok == TOK_DOTS) { | |
| 1410 | + if (l == FUNC_NEW && tok == TOK_DOTS) { | |
| 1411 | + l = FUNC_ELLIPSIS; | |
| 1378 | 1412 | next(); |
| 1379 | 1413 | break; |
| 1380 | 1414 | } |
| 1381 | 1415 | } |
| 1382 | 1416 | } |
| 1383 | 1417 | skip(')'); |
| 1384 | − t = post_type(0, t); /* XXX: may be incorrect */ | |
| 1385 | − /* transform function pointer to 'char *' */ | |
| 1386 | − if (u) | |
| 1387 | − t = u + VT_BYTE; | |
| 1388 | − else | |
| 1389 | − t = t | VT_FUNC; | |
| 1418 | + t = post_type(t); | |
| 1419 | + /* we push a anonymous symbol which will contain the function prototype */ | |
| 1420 | + p = anon_sym++; | |
| 1421 | + s = sym_push(p, t, l); | |
| 1422 | + s->next = last; | |
| 1423 | + t = VT_FUNC | (p << VT_STRUCT_SHIFT); | |
| 1390 | 1424 | } else if (tok == '[') { |
| 1391 | 1425 | /* array definition */ |
| 1392 | 1426 | next(); |
| @@ -1398,7 +1432,7 @@ int post_type(u, t) · 5 unmodified lines | ||
| 1398 | 1432 | } |
| 1399 | 1433 | skip(']'); |
| 1400 | 1434 | /* parse next post type */ |
| 1401 | − t = post_type(u, t); | |
| 1435 | + t = post_type(t); | |
| 1402 | 1436 | |
| 1403 | 1437 | /* we push a anonymous symbol which will contain the array |
| 1404 | 1438 | element type */ |
| @@ -1413,7 +1447,8 @@ int post_type(u, t) · 8 unmodified lines | ||
| 1413 | 1447 | type. If v is true, then also put variable name in 'vc' */ |
| 1414 | 1448 | int typ(int *v, int t) |
| 1415 | 1449 | { |
| 1416 | − int u; | |
| 1450 | + int u, p; | |
| 1451 | + Sym *s; | |
| 1417 | 1452 | |
| 1418 | 1453 | t = t & -3; /* suppress the ored '2' */ |
| 1419 | 1454 | while (tok == '*') { |
| @@ -1435,13 +1470,26 @@ int typ(int *v, int t) · 15 unmodified lines | ||
| 1435 | 1470 | next(); |
| 1436 | 1471 | } |
| 1437 | 1472 | } |
| 1438 | − return post_type(u, t); | |
| 1473 | + /* append t at the end of u */ | |
| 1474 | + t = post_type(t); | |
| 1475 | + if (!u) | |
| 1476 | + return t; | |
| 1477 | + p = u; | |
| 1478 | + while(1) { | |
| 1479 | + s = sym_find((unsigned)p >> VT_STRUCT_SHIFT); | |
| 1480 | + p = s->t; | |
| 1481 | + if (!p) { | |
| 1482 | + s->t = t; | |
| 1483 | + break; | |
| 1484 | + } | |
| 1485 | + } | |
| 1486 | + return u; | |
| 1439 | 1487 | } |
| 1440 | 1488 | |
| 1441 | 1489 | /* define a new external reference to a function 'v' of type 'u' */ |
| 1442 | 1490 | Sym *external_func(v, u) |
| 1443 | 1491 | { |
| 1444 | − int t, n; | |
| 1492 | + int t, n, p; | |
| 1445 | 1493 | Sym *s; |
| 1446 | 1494 | s = sym_find(v); |
| 1447 | 1495 | if (!s) { |
| @@ -1589,7 +1637,10 @@ void unary() · 141 unmodified lines | ||
| 1589 | 1637 | error("undefined symbol"); |
| 1590 | 1638 | /* for simple function calls, we tolerate undeclared |
| 1591 | 1639 | external reference */ |
| 1592 | − s = external_func(t, VT_FUNC); /* int() function */ | |
| 1640 | + p = anon_sym++; | |
| 1641 | + sym_push1(&global_stack, p, 0, FUNC_OLD); | |
| 1642 | + /* int() function */ | |
| 1643 | + s = external_func(t, VT_FUNC | (p << VT_STRUCT_SHIFT)); | |
| 1593 | 1644 | } |
| 1594 | 1645 | vset(s->t, s->c); |
| 1595 | 1646 | /* if forward reference, we must point to s->c */ |
| @@ -1678,16 +1729,15 @@ void unary() · 82 unmodified lines | ||
| 1678 | 1729 | *(int *)fc = psym(0xe8, *(int *)fc); |
| 1679 | 1730 | } else |
| 1680 | 1731 | oad(0xe8, fc - ind - 5); |
| 1681 | − /* return value is %eax, and take type from function proto */ | |
| 1682 | − vt = 0 | (ft & VT_TYPE & VT_FUNCN); | |
| 1683 | 1732 | } else { |
| 1684 | 1733 | oad(0x2494ff, t); /* call *xxx(%esp) */ |
| 1685 | 1734 | t = t + 4; |
| 1686 | − /* return value is %eax, integer */ | |
| 1687 | − vt = 0; | |
| 1688 | 1735 | } |
| 1689 | 1736 | if (t) |
| 1690 | 1737 | oad(0xc481, t); |
| 1738 | + /* get return type */ | |
| 1739 | + s = sym_find((unsigned)ft >> VT_STRUCT_SHIFT); | |
| 1740 | + vt = s->t | 0; /* return register is eax */ | |
| 1691 | 1741 | } else { |
| 1692 | 1742 | break; |
| 1693 | 1743 | } |
| @@ -1732,7 +1782,8 @@ void sum(l) · 38 unmodified lines | ||
| 1732 | 1782 | while ((l == 0 & (tok == '*' | tok == '/' | tok == '%')) | |
| 1733 | 1783 | (l == 1 & (tok == '+' | tok == '-')) | |
| 1734 | 1784 | (l == 2 & (tok == TOK_SHL | tok == TOK_SAR)) | |
| 1735 | − (l == 3 & (tok >= TOK_LT & tok <= TOK_GT)) | | |
| 1785 | + (l == 3 & ((tok >= TOK_ULE & tok <= TOK_GT) | | |
| 1786 | + tok == TOK_ULT | tok == TOK_UGE)) | | |
| 1736 | 1787 | (l == 4 & (tok == TOK_EQ | tok == TOK_NE)) | |
| 1737 | 1788 | (l == 5 & tok == '&') | |
| 1738 | 1789 | (l == 6 & tok == '^') | |
| @@ -2037,6 +2088,8 @@ void decl(l) · 298 unmodified lines | ||
| 2037 | 2088 | while (1) { /* iterate thru each declaration */ |
| 2038 | 2089 | t = typ(&v, b); |
| 2039 | 2090 | if (tok == '{') { |
| 2091 | + if (!(t & VT_FUNC)) | |
| 2092 | + expect("function defintion"); | |
| 2040 | 2093 | /* patch forward references */ |
| 2041 | 2094 | if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) { |
| 2042 | 2095 | gsym(sym->c); |
| @@ -2046,6 +2099,13 @@ void decl(l) · 3 unmodified lines | ||
| 2046 | 2099 | /* put function address */ |
| 2047 | 2100 | sym_push1(&global_stack, v, VT_CONST | VT_LVAL | t, ind); |
| 2048 | 2101 | } |
| 2102 | + /* push a dummy symbol to enable local sym storage */ | |
| 2103 | + sym_push1(&local_stack, 0, 0, 0); | |
| 2104 | + /* define parameters */ | |
| 2105 | + sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT); | |
| 2106 | + while (sym = sym->next) { | |
| 2107 | + sym_push(sym->v & ~SYM_FIELD, sym->t, sym->c); | |
| 2108 | + } | |
| 2049 | 2109 | loc = 0; |
| 2050 | 2110 | o(0xe58955); /* push %ebp, mov %esp, %ebp */ |
| 2051 | 2111 | a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */ |