Unified · Split
| @@ -71,6 +71,17 @@ typedef struct SymStack { · 70 unmodified lines | |||
| 71 | struct Sym *hash[SYM_HASH_SIZE]; | 71 | struct Sym *hash[SYM_HASH_SIZE]; |
| 72 | } SymStack; | 72 | } SymStack; |
| 73 | 73 | ||
| 74 | /* relocation entry (currently only used for functions or variables */ | ||
| 75 | typedef struct Reloc { | ||
| 76 | int type; /* type of relocation */ | ||
| 77 | int addr; /* address of relocation */ | ||
| 78 | struct Reloc *next; /* next relocation */ | ||
| 79 | } Reloc; | ||
| 80 | |||
| 81 | #define RELOC_ADDR32 1 /* 32 bits relocation */ | ||
| 82 | #define RELOC_REL32 2 /* 32 bits relative relocation */ | ||
| 83 | |||
| 84 | |||
| 74 | #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */ | 85 | #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */ |
| 75 | #define SYM_FIELD 0x20000000 /* struct/union field symbol space */ | 86 | #define SYM_FIELD 0x20000000 /* struct/union field symbol space */ |
| 76 | 87 | ||
| @@ -144,13 +155,15 @@ int nb_include_paths; · 67 unmodified lines | |||
| 144 | #define VT_ENUM 0x00800 /* enum definition */ | 155 | #define VT_ENUM 0x00800 /* enum definition */ |
| 145 | #define VT_FUNC 0x01000 /* function type */ | 156 | #define VT_FUNC 0x01000 /* function type */ |
| 146 | #define VT_STRUCT 0x002000 /* struct/union definition */ | 157 | #define VT_STRUCT 0x002000 /* struct/union definition */ |
| 147 | #define VT_TYPEDEF 0x004000 /* typedef definition */ | 158 | #define VT_SHORT 0x004000 /* short type */ |
| 148 | #define VT_EXTERN 0x008000 /* extern definition */ | ||
| 149 | #define VT_STATIC 0x010000 /* static variable */ | ||
| 150 | #define VT_SHORT 0x020000 /* short type */ | ||
| 151 | #define VT_STRUCT_SHIFT 18 /* structure/enum name shift (14 bits left) */ | 159 | #define VT_STRUCT_SHIFT 18 /* structure/enum name shift (14 bits left) */ |
| 152 | 160 | ||
| 153 | #define VT_TYPE 0xffffffc0 /* type mask */ | 161 | /* storage */ |
| 162 | #define VT_EXTERN 0x00008000 /* extern definition */ | ||
| 163 | #define VT_STATIC 0x00010000 /* static variable */ | ||
| 164 | #define VT_TYPEDEF 0x00020000 /* typedef definition */ | ||
| 165 | |||
| 166 | #define VT_TYPE 0xfffc7fc0 /* type mask */ | ||
| 154 | #define VT_TYPEN 0x0000003f /* ~VT_TYPE */ | 167 | #define VT_TYPEN 0x0000003f /* ~VT_TYPE */ |
| 155 | #define VT_FUNCN -4097 /* ~VT_FUNC */ | 168 | #define VT_FUNCN -4097 /* ~VT_FUNC */ |
| 156 | 169 | ||
| @@ -269,12 +282,12 @@ enum { · 112 unmodified lines | |||
| 269 | }; | 282 | }; |
| 270 | 283 | ||
| 271 | void sum(); | 284 | void sum(); |
| 272 | void next(); | 285 | void next(void); |
| 273 | void next_nomacro(); | 286 | void next_nomacro(); |
| 274 | int expr_const(); | 287 | int expr_const(); |
| 275 | void expr_eq(); | 288 | void expr_eq(); |
| 276 | void expr(); | 289 | void expr(void); |
| 277 | void decl(); | 290 | void decl(int l); |
| 278 | void decl_initializer(int t, int c, int first, int size_only); | 291 | void decl_initializer(int t, int c, int first, int size_only); |
| 279 | int decl_initializer_alloc(int t, int has_init); | 292 | int decl_initializer_alloc(int t, int has_init); |
| 280 | int gv(void); | 293 | int gv(void); |
| @@ -1450,12 +1463,12 @@ typedef struct GFuncContext { · 1169 unmodified lines | |||
| 1450 | int args_size; | 1463 | int args_size; |
| 1451 | } GFuncContext; | 1464 | } GFuncContext; |
| 1452 | 1465 | ||
| 1453 | void g(c) | 1466 | void g(int c) |
| 1454 | { | 1467 | { |
| 1455 | *(char *)ind++ = c; | 1468 | *(char *)ind++ = c; |
| 1456 | } | 1469 | } |
| 1457 | 1470 | ||
| 1458 | void o(c) | 1471 | void o(int c) |
| 1459 | { | 1472 | { |
| 1460 | while (c) { | 1473 | while (c) { |
| 1461 | g(c); | 1474 | g(c); |
| @@ -1463,6 +1476,49 @@ void o(c) · 1 unmodified line | |||
| 1463 | } | 1476 | } |
| 1464 | } | 1477 | } |
| 1465 | 1478 | ||
| 1479 | void gen_le32(int c) | ||
| 1480 | { | ||
| 1481 | g(c); | ||
| 1482 | g(c >> 8); | ||
| 1483 | g(c >> 16); | ||
| 1484 | g(c >> 24); | ||
| 1485 | } | ||
| 1486 | |||
| 1487 | /* add a new relocation entry to symbol 's' */ | ||
| 1488 | void greloc(Sym *s, int addr, int type) | ||
| 1489 | { | ||
| 1490 | Reloc *p; | ||
| 1491 | p = malloc(sizeof(Reloc)); | ||
| 1492 | if (!p) | ||
| 1493 | error("memory full"); | ||
| 1494 | p->type = type; | ||
| 1495 | p->addr = addr; | ||
| 1496 | p->next = (Reloc *)s->c; | ||
| 1497 | s->c = (int)p; | ||
| 1498 | } | ||
| 1499 | |||
| 1500 | /* patch each relocation entry with value 'val' */ | ||
| 1501 | void greloc_patch(Sym *s, int val) | ||
| 1502 | { | ||
| 1503 | Reloc *p, *p1; | ||
| 1504 | |||
| 1505 | p = (Reloc *)s->c; | ||
| 1506 | while (p != NULL) { | ||
| 1507 | p1 = p->next; | ||
| 1508 | switch(p->type) { | ||
| 1509 | case RELOC_ADDR32: | ||
| 1510 | *(int *)p->addr = val; | ||
| 1511 | break; | ||
| 1512 | case RELOC_REL32: | ||
| 1513 | *(int *)p->addr = val - p->addr - 4; | ||
| 1514 | break; | ||
| 1515 | } | ||
| 1516 | free(p); | ||
| 1517 | p = p1; | ||
| 1518 | } | ||
| 1519 | s->c = val; | ||
| 1520 | } | ||
| 1521 | |||
| 1466 | /* output a symbol and patch all calls to it */ | 1522 | /* output a symbol and patch all calls to it */ |
| 1467 | void gsym_addr(t, a) | 1523 | void gsym_addr(t, a) |
| 1468 | { | 1524 | { |
| @@ -1484,7 +1540,7 @@ void gsym(t) · 15 unmodified lines | |||
| 1484 | #define psym oad | 1540 | #define psym oad |
| 1485 | 1541 | ||
| 1486 | /* instruction + 4 bytes data. Return the address of the data */ | 1542 | /* instruction + 4 bytes data. Return the address of the data */ |
| 1487 | int oad(c, s) | 1543 | int oad(int c, int s) |
| 1488 | { | 1544 | { |
| 1489 | o(c); | 1545 | o(c); |
| 1490 | *(int *)ind = s; | 1546 | *(int *)ind = s; |
| @@ -1493,6 +1549,17 @@ int oad(c, s) · 2 unmodified lines | |||
| 1493 | return s; | 1549 | return s; |
| 1494 | } | 1550 | } |
| 1495 | 1551 | ||
| 1552 | /* output constant with relocation if 't & VT_FORWARD' is true */ | ||
| 1553 | void gen_addr32(int c, int t) | ||
| 1554 | { | ||
| 1555 | if (!(t & VT_FORWARD)) { | ||
| 1556 | gen_le32(c); | ||
| 1557 | } else { | ||
| 1558 | greloc((Sym *)c, ind, RELOC_ADDR32); | ||
| 1559 | gen_le32(0); | ||
| 1560 | } | ||
| 1561 | } | ||
| 1562 | |||
| 1496 | /* XXX: generate correct pointer for forward references to functions */ | 1563 | /* XXX: generate correct pointer for forward references to functions */ |
| 1497 | /* r = (ft, fc) */ | 1564 | /* r = (ft, fc) */ |
| 1498 | void load(r, ft, fc) | 1565 | void load(r, ft, fc) |
| @@ -1516,7 +1583,8 @@ void load(r, ft, fc) · 17 unmodified lines | |||
| 1516 | else | 1583 | else |
| 1517 | o(0x8b); /* movl */ | 1584 | o(0x8b); /* movl */ |
| 1518 | if (v == VT_CONST) { | 1585 | if (v == VT_CONST) { |
| 1519 | oad(0x05 + r * 8, fc); /* 0xXX, r */ | 1586 | o(0x05 + r * 8); /* 0xXX, r */ |
| 1587 | gen_addr32(fc, ft); | ||
| 1520 | } else if (v == VT_LOCAL) { | 1588 | } else if (v == VT_LOCAL) { |
| 1521 | oad(0x85 + r * 8, fc); /* xx(%ebp), r */ | 1589 | oad(0x85 + r * 8, fc); /* xx(%ebp), r */ |
| 1522 | } else { | 1590 | } else { |
| @@ -1524,7 +1592,8 @@ void load(r, ft, fc) · 1 unmodified line | |||
| 1524 | } | 1592 | } |
| 1525 | } else { | 1593 | } else { |
| 1526 | if (v == VT_CONST) { | 1594 | if (v == VT_CONST) { |
| 1527 | oad(0xb8 + r, fc); /* mov $xx, r */ | 1595 | o(0xb8 + r); /* mov $xx, r */ |
| 1596 | gen_addr32(fc, ft); | ||
| 1528 | } else if (v == VT_LOCAL) { | 1597 | } else if (v == VT_LOCAL) { |
| 1529 | o(0x8d); | 1598 | o(0x8d); |
| 1530 | oad(0x85 + r * 8, fc); /* lea xxx(%ebp), r */ | 1599 | oad(0x85 + r * 8, fc); /* lea xxx(%ebp), r */ |
| @@ -1559,7 +1628,8 @@ void store(r, ft, fc) · 28 unmodified lines | |||
| 1559 | o(0x66); | 1628 | o(0x66); |
| 1560 | o(0x89 - b); | 1629 | o(0x89 - b); |
| 1561 | if (fr == VT_CONST) { | 1630 | if (fr == VT_CONST) { |
| 1562 | oad(0x05 + r * 8, fc); /* mov r,xxx */ | 1631 | o(0x05 + r * 8); /* mov r,xxx */ |
| 1632 | gen_addr32(fc, ft); | ||
| 1563 | } else if (fr == VT_LOCAL) { | 1633 | } else if (fr == VT_LOCAL) { |
| 1564 | oad(0x85 + r * 8, fc); /* mov r,xxx(%ebp) */ | 1634 | oad(0x85 + r * 8, fc); /* mov r,xxx(%ebp) */ |
| 1565 | } else if (ft & VT_LVAL) { | 1635 | } else if (ft & VT_LVAL) { |
| @@ -1615,9 +1685,11 @@ void gfunc_call(GFuncContext *c, int ft, int fc) · 49 unmodified lines | |||
| 1615 | if (r == VT_CONST) { | 1685 | if (r == VT_CONST) { |
| 1616 | /* forward reference */ | 1686 | /* forward reference */ |
| 1617 | if (ft & VT_FORWARD) { | 1687 | if (ft & VT_FORWARD) { |
| 1618 | *(int *)fc = psym(0xe8, *(int *)fc); | 1688 | greloc((Sym *)fc, ind + 1, RELOC_REL32); |
| 1619 | } else | 1689 | oad(0xe8, 0); |
| 1690 | } else { | ||
| 1620 | oad(0xe8, fc - ind - 5); | 1691 | oad(0xe8, fc - ind - 5); |
| 1692 | } | ||
| 1621 | } else if (r == VT_LOCAL) { | 1693 | } else if (r == VT_LOCAL) { |
| 1622 | oad(0x95ff, fc); /* call *xxx(%ebp) */ | 1694 | oad(0x95ff, fc); /* call *xxx(%ebp) */ |
| 1623 | } else { | 1695 | } else { |
| @@ -2171,7 +2243,7 @@ void inc(post, c) · 547 unmodified lines | |||
| 2171 | } | 2243 | } |
| 2172 | 2244 | ||
| 2173 | /* enum/struct/union declaration */ | 2245 | /* enum/struct/union declaration */ |
| 2174 | int struct_decl(u) | 2246 | int struct_decl(int u) |
| 2175 | { | 2247 | { |
| 2176 | int a, t, b, v, size, align, maxalign, c; | 2248 | int a, t, b, v, size, align, maxalign, c; |
| 2177 | Sym *s, *ss, **ps; | 2249 | Sym *s, *ss, **ps; |
| @@ -2428,27 +2500,19 @@ int type_decl(int *v, int t, int td) · 250 unmodified lines | |||
| 2428 | } | 2500 | } |
| 2429 | 2501 | ||
| 2430 | /* define a new external reference to a function 'v' of type 'u' */ | 2502 | /* define a new external reference to a function 'v' of type 'u' */ |
| 2431 | Sym *external_func(v, u) | 2503 | Sym *external_sym(int v, int u) |
| 2432 | { | 2504 | { |
| 2433 | int n; | ||
| 2434 | Sym *s; | 2505 | Sym *s; |
| 2435 | s = sym_find(v); | 2506 | s = sym_find(v); |
| 2436 | if (!s) { | 2507 | if (!s) { |
| 2437 | n = (int)dlsym(0, get_tok_str(v, 0)); | 2508 | /* push forward reference */ |
| 2438 | if (n == 0) { | 2509 | s = sym_push1(&global_stack, |
| 2439 | /* used to generate symbol list */ | 2510 | v, u | VT_CONST | VT_FORWARD, 0); |
| 2440 | s = sym_push1(&global_stack, | ||
| 2441 | v, u | VT_CONST | VT_LVAL | VT_FORWARD, 0); | ||
| 2442 | } else { | ||
| 2443 | /* int f() */ | ||
| 2444 | s = sym_push1(&global_stack, | ||
| 2445 | v, u | VT_CONST | VT_LVAL, n); | ||
| 2446 | } | ||
| 2447 | } | 2511 | } |
| 2448 | return s; | 2512 | return s; |
| 2449 | } | 2513 | } |
| 2450 | 2514 | ||
| 2451 | void indir() | 2515 | void indir(void) |
| 2452 | { | 2516 | { |
| 2453 | if (vt & VT_LVAL) | 2517 | if (vt & VT_LVAL) |
| 2454 | gv(); | 2518 | gv(); |
| @@ -2459,7 +2523,7 @@ void indir() · 4 unmodified lines | |||
| 2459 | vt |= VT_LVAL; | 2523 | vt |= VT_LVAL; |
| 2460 | } | 2524 | } |
| 2461 | 2525 | ||
| 2462 | void unary() | 2526 | void unary(void) |
| 2463 | { | 2527 | { |
| 2464 | int n, t, ft, fc, p, align, size; | 2528 | int n, t, ft, fc, p, align, size; |
| 2465 | Sym *s; | 2529 | Sym *s; |
| @@ -2523,7 +2587,12 @@ void unary() · 57 unmodified lines | |||
| 2523 | indir(); | 2587 | indir(); |
| 2524 | } else if (t == '&') { | 2588 | } else if (t == '&') { |
| 2525 | unary(); | 2589 | unary(); |
| 2526 | test_lvalue(); | 2590 | /* functions names must be treated as function pointers, |
| 2591 | except for unary '&' and sizeof. Since we consider that | ||
| 2592 | functions are not lvalues, we only have to handle it | ||
| 2593 | there and in function calls. */ | ||
| 2594 | if (!(vt & VT_FUNC)) | ||
| 2595 | test_lvalue(); | ||
| 2527 | vt = mk_pointer(vt & VT_LVALN); | 2596 | vt = mk_pointer(vt & VT_LVALN); |
| 2528 | } else | 2597 | } else |
| 2529 | if (t == '!') { | 2598 | if (t == '!') { |
| @@ -2577,12 +2646,12 @@ void unary() · 47 unmodified lines | |||
| 2577 | p = anon_sym++; | 2646 | p = anon_sym++; |
| 2578 | sym_push1(&global_stack, p, 0, FUNC_OLD); | 2647 | sym_push1(&global_stack, p, 0, FUNC_OLD); |
| 2579 | /* int() function */ | 2648 | /* int() function */ |
| 2580 | s = external_func(t, VT_FUNC | (p << VT_STRUCT_SHIFT)); | 2649 | s = external_sym(t, VT_FUNC | (p << VT_STRUCT_SHIFT)); |
| 2581 | } | 2650 | } |
| 2582 | vset(s->t, s->c); | 2651 | vset(s->t, s->c); |
| 2583 | /* if forward reference, we must point to s->c */ | 2652 | /* if forward reference, we must point to s */ |
| 2584 | if (vt & VT_FORWARD) | 2653 | if (vt & VT_FORWARD) |
| 2585 | vc = (int)&s->c; | 2654 | vc = (int)s; |
| 2586 | } | 2655 | } |
| 2587 | } | 2656 | } |
| 2588 | 2657 | ||
| @@ -2632,8 +2701,16 @@ void unary() · 43 unmodified lines | |||
| 2632 | int rett, retc; | 2701 | int rett, retc; |
| 2633 | 2702 | ||
| 2634 | /* function call */ | 2703 | /* function call */ |
| 2635 | if (!(vt & VT_FUNC)) | 2704 | if (!(vt & VT_FUNC)) { |
| 2636 | expect("function type"); | 2705 | if ((vt & (VT_PTR | VT_ARRAY)) == VT_PTR) { |
| 2706 | vt = pointed_type(vt); | ||
| 2707 | if (!(vt & VT_FUNC)) | ||
| 2708 | goto error_func; | ||
| 2709 | } else { | ||
| 2710 | error_func: | ||
| 2711 | expect("function type"); | ||
| 2712 | } | ||
| 2713 | } | ||
| 2637 | /* get return type */ | 2714 | /* get return type */ |
| 2638 | s = sym_find((unsigned)vt >> VT_STRUCT_SHIFT); | 2715 | s = sym_find((unsigned)vt >> VT_STRUCT_SHIFT); |
| 2639 | 2716 | ||
| @@ -2725,6 +2802,30 @@ void unary() · 85 unmodified lines | |||
| 2725 | } | 2802 | } |
| 2726 | } | 2803 | } |
| 2727 | 2804 | ||
| 2805 | /* check if types are compatible for assignation */ | ||
| 2806 | int same_types(int t1, int t2) | ||
| 2807 | { | ||
| 2808 | t1 &= VT_TYPE; | ||
| 2809 | t2 &= VT_TYPE; | ||
| 2810 | if (t1 & VT_PTR) { | ||
| 2811 | /* XXX: zero test ? */ | ||
| 2812 | if (!(t2 & VT_PTR)) | ||
| 2813 | return 0; | ||
| 2814 | t1 = pointed_type(t1); | ||
| 2815 | t2 = pointed_type(t2); | ||
| 2816 | /* void matches everything */ | ||
| 2817 | if (t1 == VT_VOID || t2 == VT_VOID) | ||
| 2818 | return 1; | ||
| 2819 | return same_types(t1, t2); | ||
| 2820 | } else if (t1 & VT_STRUCT) { | ||
| 2821 | return (t2 == t1); | ||
| 2822 | } else { | ||
| 2823 | /* XXX: not complete */ | ||
| 2824 | return 1; | ||
| 2825 | } | ||
| 2826 | } | ||
| 2827 | |||
| 2828 | |||
| 2728 | void uneq() | 2829 | void uneq() |
| 2729 | { | 2830 | { |
| 2730 | int t; | 2831 | int t; |
| @@ -2740,9 +2841,8 @@ void uneq() · 9 unmodified lines | |||
| 2740 | next(); | 2841 | next(); |
| 2741 | if (t == '=') { | 2842 | if (t == '=') { |
| 2742 | expr_eq(); | 2843 | expr_eq(); |
| 2743 | /* XXX: be more precise */ | 2844 | if (!same_types(vt, vstack_ptr[-2])) |
| 2744 | if ((vt & VT_PTR) != (vstack_ptr[-2] & VT_PTR)) | 2845 | error("incompatible types"); |
| 2745 | warning("incompatible type"); | ||
| 2746 | } else { | 2846 | } else { |
| 2747 | vpush(); | 2847 | vpush(); |
| 2748 | expr_eq(); | 2848 | expr_eq(); |
| @@ -2919,7 +3019,8 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 170 unmodified lines | |||
| 2919 | s = local_stack.top; | 3019 | s = local_stack.top; |
| 2920 | while (tok != '}') { | 3020 | while (tok != '}') { |
| 2921 | decl(VT_LOCAL); | 3021 | decl(VT_LOCAL); |
| 2922 | block(bsym, csym, case_sym, def_sym, case_reg); | 3022 | if (tok != '}') |
| 3023 | block(bsym, csym, case_sym, def_sym, case_reg); | ||
| 2923 | } | 3024 | } |
| 2924 | /* pop locally defined symbols */ | 3025 | /* pop locally defined symbols */ |
| 2925 | sym_pop(&local_stack, s); | 3026 | sym_pop(&local_stack, s); |
| @@ -3002,6 +3103,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 76 unmodified lines | |||
| 3002 | gsym_addr(c, d); | 3103 | gsym_addr(c, d); |
| 3003 | skip(')'); | 3104 | skip(')'); |
| 3004 | gsym(a); | 3105 | gsym(a); |
| 3106 | skip(';'); | ||
| 3005 | } else | 3107 | } else |
| 3006 | if (tok == TOK_SWITCH) { | 3108 | if (tok == TOK_SWITCH) { |
| 3007 | next(); | 3109 | next(); |
| @@ -3297,9 +3399,9 @@ void decl_initializer(int t, int c, int first, int size_only) · 289 unmodified lines | |||
| 3297 | /* patch type size if needed */ | 3399 | /* patch type size if needed */ |
| 3298 | if (n < 0) | 3400 | if (n < 0) |
| 3299 | s->c = array_length; | 3401 | s->c = array_length; |
| 3300 | } else if (t & VT_STRUCT) { | 3402 | } else if ((t & VT_STRUCT) && tok == '{') { |
| 3301 | /* XXX: union needs only one init */ | 3403 | /* XXX: union needs only one init */ |
| 3302 | skip('{'); | 3404 | next(); |
| 3303 | s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT); | 3405 | s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT); |
| 3304 | f = s->next; | 3406 | f = s->next; |
| 3305 | array_length = 0; | 3407 | array_length = 0; |
| @@ -3428,14 +3530,20 @@ int decl_initializer_alloc(int t, int has_init) · 122 unmodified lines | |||
| 3428 | 3530 | ||
| 3429 | 3531 | ||
| 3430 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ | 3532 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ |
| 3431 | void decl(l) | 3533 | void decl(int l) |
| 3432 | { | 3534 | { |
| 3433 | int *a, t, b, v, u, n, addr, has_init, size, align; | 3535 | int *a, t, b, v, u, addr, has_init, size, align; |
| 3434 | Sym *sym; | 3536 | Sym *sym; |
| 3435 | 3537 | ||
| 3436 | while (1) { | 3538 | while (1) { |
| 3437 | b = ist(); | 3539 | b = ist(); |
| 3438 | if (!b) { | 3540 | if (!b) { |
| 3541 | /* skip redundant ';' */ | ||
| 3542 | /* XXX: find more elegant solution */ | ||
| 3543 | if (tok == ';') { | ||
| 3544 | next(); | ||
| 3545 | continue; | ||
| 3546 | } | ||
| 3439 | /* special test for old K&R protos without explicit int | 3547 | /* special test for old K&R protos without explicit int |
| 3440 | type. Only accepted when defining global data */ | 3548 | type. Only accepted when defining global data */ |
| 3441 | if (l == VT_LOCAL || tok < TOK_DEFINE) | 3549 | if (l == VT_LOCAL || tok < TOK_DEFINE) |
| @@ -3450,16 +3558,17 @@ void decl(l) · 8 unmodified lines | |||
| 3450 | while (1) { /* iterate thru each declaration */ | 3558 | while (1) { /* iterate thru each declaration */ |
| 3451 | t = type_decl(&v, b, TYPE_DIRECT); | 3559 | t = type_decl(&v, b, TYPE_DIRECT); |
| 3452 | if (tok == '{') { | 3560 | if (tok == '{') { |
| 3561 | if (l == VT_LOCAL) | ||
| 3562 | error("cannot use local functions"); | ||
| 3453 | if (!(t & VT_FUNC)) | 3563 | if (!(t & VT_FUNC)) |
| 3454 | expect("function definition"); | 3564 | expect("function definition"); |
| 3455 | /* patch forward references */ | 3565 | /* patch forward references */ |
| 3456 | if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) { | 3566 | if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) { |
| 3457 | gsym(sym->c); | 3567 | greloc_patch(sym, ind); |
| 3458 | sym->c = ind; | 3568 | sym->t = VT_CONST | t; |
| 3459 | sym->t = VT_CONST | VT_LVAL | t; | ||
| 3460 | } else { | 3569 | } else { |
| 3461 | /* put function address */ | 3570 | /* put function address */ |
| 3462 | sym_push1(&global_stack, v, VT_CONST | VT_LVAL | t, ind); | 3571 | sym_push1(&global_stack, v, VT_CONST | t, ind); |
| 3463 | } | 3572 | } |
| 3464 | funcname = get_tok_str(v, 0); | 3573 | funcname = get_tok_str(v, 0); |
| 3465 | /* push a dummy symbol to enable local sym storage */ | 3574 | /* push a dummy symbol to enable local sym storage */ |
| @@ -3516,21 +3625,15 @@ void decl(l) · 50 unmodified lines | |||
| 3516 | /* save typedefed type */ | 3625 | /* save typedefed type */ |
| 3517 | sym_push(v, t | VT_TYPEDEF, 0); | 3626 | sym_push(v, t | VT_TYPEDEF, 0); |
| 3518 | } else if (t & VT_FUNC) { | 3627 | } else if (t & VT_FUNC) { |
| 3519 | /* XXX: incorrect to flush, but needed while | ||
| 3520 | waiting for function prototypes */ | ||
| 3521 | /* external function definition */ | 3628 | /* external function definition */ |
| 3522 | external_func(v, t); | 3629 | external_sym(v, t); |
| 3523 | } else { | 3630 | } else { |
| 3524 | /* not lvalue if array */ | 3631 | /* not lvalue if array */ |
| 3525 | if (!(t & VT_ARRAY)) | 3632 | if (!(t & VT_ARRAY)) |
| 3526 | t |= VT_LVAL; | 3633 | t |= VT_LVAL; |
| 3527 | if (b & VT_EXTERN) { | 3634 | if (b & VT_EXTERN) { |
| 3528 | /* external variable */ | 3635 | /* external variable */ |
| 3529 | /* XXX: factorize with external function def */ | 3636 | external_sym(v, t); |
| 3530 | n = (int)dlsym(NULL, get_tok_str(v, 0)); | ||
| 3531 | if (!n) | ||
| 3532 | error("unknown external variable"); | ||
| 3533 | sym_push(v, VT_CONST | t, n); | ||
| 3534 | } else { | 3637 | } else { |
| 3535 | u = l; | 3638 | u = l; |
| 3536 | if (t & VT_STATIC) | 3639 | if (t & VT_STATIC) |
| @@ -3566,6 +3669,30 @@ void open_dll(char *libname) · 29 unmodified lines | |||
| 3566 | error((char *)dlerror()); | 3669 | error((char *)dlerror()); |
| 3567 | } | 3670 | } |
| 3568 | 3671 | ||
| 3672 | void reloc_external_syms(void) | ||
| 3673 | { | ||
| 3674 | Sym *s, *s1; | ||
| 3675 | char *str; | ||
| 3676 | int addr; | ||
| 3677 | |||
| 3678 | s = global_stack.top; | ||
| 3679 | while (s != NULL) { | ||
| 3680 | s1 = s->prev; | ||
| 3681 | if (s->t & VT_FORWARD) { | ||
| 3682 | /* if there is at least one relocation to do, then find it | ||
| 3683 | and patch it */ | ||
| 3684 | if (s->c) { | ||
| 3685 | str = get_tok_str(s->v, 0); | ||
| 3686 | addr = (int)dlsym(NULL, str); | ||
| 3687 | if (!addr) | ||
| 3688 | error("unresolved external reference '%s'", str); | ||
| 3689 | greloc_patch(s, addr); | ||
| 3690 | } | ||
| 3691 | } | ||
| 3692 | s = s1; | ||
| 3693 | } | ||
| 3694 | } | ||
| 3695 | |||
| 3569 | /* output a binary file (for testing) */ | 3696 | /* output a binary file (for testing) */ |
| 3570 | void build_exe(char *filename) | 3697 | void build_exe(char *filename) |
| 3571 | { | 3698 | { |
| @@ -3658,6 +3785,7 @@ int main(int argc, char **argv) · 86 unmodified lines | |||
| 3658 | decl(VT_CONST); | 3785 | decl(VT_CONST); |
| 3659 | if (tok != -1) | 3786 | if (tok != -1) |
| 3660 | expect("declaration"); | 3787 | expect("declaration"); |
| 3788 | reloc_external_syms(); | ||
| 3661 | if (outfile) { | 3789 | if (outfile) { |
| 3662 | build_exe(outfile); | 3790 | build_exe(outfile); |
| 3663 | return 0; | 3791 | return 0; |