| @@ -60,14 +60,18 @@ typedef union CValue { · 59 unmodified lines |
| 60 | 60 | float f; |
| 61 | 61 | int i; |
| 62 | 62 | unsigned int ui; |
| 63 | + unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */ |
| 64 | + long long ll; |
| 65 | + unsigned long long ull; |
| 63 | 66 | struct TokenSym *ts; |
| 64 | 67 | int tab[1]; |
| 68 | + struct Sym *sym; |
| 65 | 69 | } CValue; |
| 66 | 70 | |
| 67 | 71 | /* value on stack */ |
| 68 | 72 | typedef struct SValue { |
| 69 | 73 | int t; |
| 70 | | − int c; |
| 74 | + CValue c; |
| 71 | 75 | } SValue; |
| 72 | 76 | |
| 73 | 77 | /* symbol management */ |
| @@ -332,7 +336,7 @@ void next(void); · 258 unmodified lines |
| 332 | 336 | void next_nomacro(void); |
| 333 | 337 | int expr_const(void); |
| 334 | 338 | void expr_eq(void); |
| 335 | | −void expr(void); |
| 339 | +void gexpr(void); |
| 336 | 340 | void decl(int l); |
| 337 | 341 | void decl_initializer(int t, int c, int first, int size_only); |
| 338 | 342 | int decl_initializer_alloc(int t, int has_init); |
| @@ -358,6 +362,14 @@ int type_decl(int *v, int t, int td); · 19 unmodified lines |
| 358 | 362 | void error(const char *fmt, ...); |
| 359 | 363 | void vset(int t, int v); |
| 360 | 364 | |
| 365 | +/* true if float/double/long double type */ |
| 366 | +static inline int is_float(int t) |
| 367 | +{ |
| 368 | + int bt; |
| 369 | + bt = t & VT_BTYPE; |
| 370 | + return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT; |
| 371 | +} |
| 372 | + |
| 361 | 373 | #include "i386-gen.c" |
| 362 | 374 | |
| 363 | 375 | #ifdef CONFIG_TCC_STATIC |
| @@ -983,7 +995,7 @@ void preprocess(void) · 619 unmodified lines |
| 983 | 995 | } |
| 984 | 996 | tok_add(&str, &len, 0); |
| 985 | 997 | #ifdef PP_DEBUG |
| 986 | | − printf("define %s %d: ", get_tok_str(v, 0), t); |
| 998 | + printf("define %s %d: ", get_tok_str(v, NULL), t); |
| 987 | 999 | tok_print(str); |
| 988 | 1000 | #endif |
| 989 | 1001 | s = sym_push1(&define_stack, v, t, (int)str); |
| @@ -1220,7 +1232,6 @@ void parse_number(void) · 230 unmodified lines |
| 1220 | 1232 | char *q; |
| 1221 | 1233 | unsigned int n, n1; |
| 1222 | 1234 | unsigned int bn[BN_SIZE]; |
| 1223 | | − CValue cval; |
| 1224 | 1235 | double d; |
| 1225 | 1236 | |
| 1226 | 1237 | /* number */ |
| @@ -1343,7 +1354,6 @@ void parse_number(void) · 116 unmodified lines |
| 1343 | 1354 | cinp(); |
| 1344 | 1355 | } |
| 1345 | 1356 | exp_val = exp_val * s; |
| 1346 | | − printf("num=%08x %08x %d %d\n", bn[1], bn[0], frac_bits, exp_val); |
| 1347 | 1357 | |
| 1348 | 1358 | /* now we can generate the number */ |
| 1349 | 1359 | /* XXX: should patch directly float number */ |
| @@ -1354,15 +1364,15 @@ void parse_number(void) · 4 unmodified lines |
| 1354 | 1364 | cinp(); |
| 1355 | 1365 | tok = TOK_CFLOAT; |
| 1356 | 1366 | /* float : should handle overflow */ |
| 1357 | | − cval.f = (float)d; |
| 1367 | + tokc.f = (float)d; |
| 1358 | 1368 | } else if (t == 'L') { |
| 1359 | 1369 | cinp(); |
| 1360 | 1370 | tok = TOK_CLDOUBLE; |
| 1361 | 1371 | /* XXX: not large enough */ |
| 1362 | | − cval.ld = (long double)d; |
| 1372 | + tokc.ld = (long double)d; |
| 1363 | 1373 | } else { |
| 1364 | 1374 | tok = TOK_CDOUBLE; |
| 1365 | | − cval.d = d; |
| 1375 | + tokc.d = d; |
| 1366 | 1376 | } |
| 1367 | 1377 | } else { |
| 1368 | 1378 | /* decimal floats */ |
| @@ -1405,14 +1415,14 @@ void parse_number(void) · 36 unmodified lines |
| 1405 | 1415 | if (t == 'F') { |
| 1406 | 1416 | cinp(); |
| 1407 | 1417 | tok = TOK_CFLOAT; |
| 1408 | | − cval.f = strtof(token_buf, NULL); |
| 1418 | + tokc.f = strtof(token_buf, NULL); |
| 1409 | 1419 | } else if (t == 'L') { |
| 1410 | 1420 | cinp(); |
| 1411 | 1421 | tok = TOK_CLDOUBLE; |
| 1412 | | − cval.ld = strtold(token_buf, NULL); |
| 1422 | + tokc.ld = strtold(token_buf, NULL); |
| 1413 | 1423 | } else { |
| 1414 | 1424 | tok = TOK_CDOUBLE; |
| 1415 | | − cval.d = strtod(token_buf, NULL); |
| 1425 | + tokc.d = strtod(token_buf, NULL); |
| 1416 | 1426 | } |
| 1417 | 1427 | } |
| 1418 | 1428 | } else { |
| @@ -1858,7 +1868,7 @@ void swap(int *p, int *q) · 439 unmodified lines |
| 1858 | 1868 | *q = t; |
| 1859 | 1869 | } |
| 1860 | 1870 | |
| 1861 | | −void vset(int t, int v) |
| 1871 | +void vsetc(int t, CValue *vc) |
| 1862 | 1872 | { |
| 1863 | 1873 | if (vtop >= vstack + VSTACK_SIZE) |
| 1864 | 1874 | error("memory full"); |
| @@ -1868,12 +1878,15 @@ void vset(int t, int v) · 3 unmodified lines |
| 1868 | 1878 | gv(); |
| 1869 | 1879 | vtop++; |
| 1870 | 1880 | vtop->t = t; |
| 1871 | | − vtop->c = v; |
| 1881 | + vtop->c = *vc; |
| 1872 | 1882 | } |
| 1873 | 1883 | |
| 1874 | | −void vpop(void) |
| 1884 | +void vset(int t, int v) |
| 1875 | 1885 | { |
| 1876 | | − vtop--; |
| 1886 | + CValue cval; |
| 1887 | + |
| 1888 | + cval.i = v; |
| 1889 | + vsetc(t, &cval); |
| 1877 | 1890 | } |
| 1878 | 1891 | |
| 1879 | 1892 | void vswap(void) |
| @@ -1912,7 +1925,7 @@ int save_reg_forced(int r) · 32 unmodified lines |
| 1912 | 1925 | else |
| 1913 | 1926 | t = VT_LOCAL; |
| 1914 | 1927 | p->t = (p->t & VT_TYPE) | VT_LVAL | t; |
| 1915 | | − p->c = l; |
| 1928 | + p->c.ul = l; |
| 1916 | 1929 | } |
| 1917 | 1930 | } |
| 1918 | 1931 | return l; |
| @@ -1994,7 +2007,7 @@ void move_reg(int r, int s) · 75 unmodified lines |
| 1994 | 2007 | (such as structures). */ |
| 1995 | 2008 | int gv(void) |
| 1996 | 2009 | { |
| 1997 | | − int r, bit_pos, bit_size, rc; |
| 2010 | + int r, bit_pos, bit_size, rc, size, align, i; |
| 1998 | 2011 | |
| 1999 | 2012 | /* NOTE: get_reg can modify vstack[] */ |
| 2000 | 2013 | if (vtop->t & VT_BITFIELD) { |
| @@ -2010,16 +2023,28 @@ int gv(void) · 9 unmodified lines |
| 2010 | 2023 | gen_op(TOK_SAR); |
| 2011 | 2024 | r = gv(); |
| 2012 | 2025 | } else { |
| 2026 | + if (is_float(vtop->t) && (vtop->t & (VT_CONST | VT_LVAL)) == VT_CONST) { |
| 2027 | + /* CPUs usually cannot use float constants, so we store them |
| 2028 | + generically in data segment */ |
| 2029 | + size = type_size(vtop->t, &align); |
| 2030 | + glo = (glo + align - 1) & -align; |
| 2031 | + /* XXX: not portable yet */ |
| 2032 | + size = size >> 2; |
| 2033 | + for(i=0;i<size;i++) |
| 2034 | + ((int *)glo)[i] = vtop->c.tab[i]; |
| 2035 | + vtop->t |= VT_LVAL; |
| 2036 | + vtop->c.ul = glo; |
| 2037 | + glo += size << 2; |
| 2038 | + } |
| 2013 | 2039 | r = vtop->t & VT_VALMASK; |
| 2014 | 2040 | if (r >= VT_CONST || (vtop->t & VT_LVAL)) { |
| 2015 | | − if ((vtop->t & VT_BTYPE) == VT_FLOAT || |
| 2016 | | − (vtop->t & VT_BTYPE) == VT_DOUBLE) |
| 2041 | + if (is_float(vtop->t)) |
| 2017 | 2042 | rc = REG_CLASS_FLOAT; |
| 2018 | 2043 | else |
| 2019 | 2044 | rc = REG_CLASS_INT; |
| 2020 | 2045 | r = get_reg(rc); |
| 2021 | 2046 | } |
| 2022 | | − load(r, vtop->t, vtop->c); |
| 2047 | + load(r, vtop->t, vtop->c.ul); |
| 2023 | 2048 | vtop->t = (vtop->t & VT_TYPE) | r; |
| 2024 | 2049 | } |
| 2025 | 2050 | return r; |
| @@ -2037,36 +2062,36 @@ void gen_opc(int op) · 11 unmodified lines |
| 2037 | 2062 | c1 = (v1->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST; |
| 2038 | 2063 | c2 = (v2->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST; |
| 2039 | 2064 | if (c1 && c2) { |
| 2040 | | − fc = v2->c; |
| 2065 | + fc = v2->c.i; |
| 2041 | 2066 | switch(op) { |
| 2042 | | − case '+': v1->c += fc; break; |
| 2043 | | − case '-': v1->c -= fc; break; |
| 2044 | | − case '&': v1->c &= fc; break; |
| 2045 | | − case '^': v1->c ^= fc; break; |
| 2046 | | − case '|': v1->c |= fc; break; |
| 2047 | | − case '*': v1->c *= fc; break; |
| 2067 | + case '+': v1->c.i += fc; break; |
| 2068 | + case '-': v1->c.i -= fc; break; |
| 2069 | + case '&': v1->c.i &= fc; break; |
| 2070 | + case '^': v1->c.i ^= fc; break; |
| 2071 | + case '|': v1->c.i |= fc; break; |
| 2072 | + case '*': v1->c.i *= fc; break; |
| 2048 | 2073 | case TOK_PDIV: |
| 2049 | | − case '/': v1->c /= fc; break; /* XXX: zero case ? */ |
| 2050 | | − case '%': v1->c %= fc; break; /* XXX: zero case ? */ |
| 2051 | | − case TOK_UDIV: v1->c = (unsigned)v1->c / fc; break; /* XXX: zero case ? */ |
| 2052 | | − case TOK_UMOD: v1->c = (unsigned)v1->c % fc; break; /* XXX: zero case ? */ |
| 2053 | | − case TOK_SHL: v1->c <<= fc; break; |
| 2054 | | − case TOK_SHR: v1->c = (unsigned)v1->c >> fc; break; |
| 2055 | | − case TOK_SAR: v1->c >>= fc; break; |
| 2074 | + case '/': v1->c.i /= fc; break; /* XXX: zero case ? */ |
| 2075 | + case '%': v1->c.i %= fc; break; /* XXX: zero case ? */ |
| 2076 | + case TOK_UDIV: v1->c.i = (unsigned)v1->c.i / fc; break; /* XXX: zero case ? */ |
| 2077 | + case TOK_UMOD: v1->c.i = (unsigned)v1->c.i % fc; break; /* XXX: zero case ? */ |
| 2078 | + case TOK_SHL: v1->c.i <<= fc; break; |
| 2079 | + case TOK_SHR: v1->c.i = (unsigned)v1->c.i >> fc; break; |
| 2080 | + case TOK_SAR: v1->c.i >>= fc; break; |
| 2056 | 2081 | /* tests */ |
| 2057 | | − case TOK_ULT: v1->c = (unsigned)v1->c < (unsigned)fc; break; |
| 2058 | | − case TOK_UGE: v1->c = (unsigned)v1->c >= (unsigned)fc; break; |
| 2059 | | − case TOK_EQ: v1->c = v1->c == fc; break; |
| 2060 | | − case TOK_NE: v1->c = v1->c != fc; break; |
| 2061 | | − case TOK_ULE: v1->c = (unsigned)v1->c <= (unsigned)fc; break; |
| 2062 | | − case TOK_UGT: v1->c = (unsigned)v1->c > (unsigned)fc; break; |
| 2063 | | − case TOK_LT: v1->c = v1->c < fc; break; |
| 2064 | | − case TOK_GE: v1->c = v1->c >= fc; break; |
| 2065 | | − case TOK_LE: v1->c = v1->c <= fc; break; |
| 2066 | | − case TOK_GT: v1->c = v1->c > fc; break; |
| 2082 | + case TOK_ULT: v1->c.i = (unsigned)v1->c.i < (unsigned)fc; break; |
| 2083 | + case TOK_UGE: v1->c.i = (unsigned)v1->c.i >= (unsigned)fc; break; |
| 2084 | + case TOK_EQ: v1->c.i = v1->c.i == fc; break; |
| 2085 | + case TOK_NE: v1->c.i = v1->c.i != fc; break; |
| 2086 | + case TOK_ULE: v1->c.i = (unsigned)v1->c.i <= (unsigned)fc; break; |
| 2087 | + case TOK_UGT: v1->c.i = (unsigned)v1->c.i > (unsigned)fc; break; |
| 2088 | + case TOK_LT: v1->c.i = v1->c.i < fc; break; |
| 2089 | + case TOK_GE: v1->c.i = v1->c.i >= fc; break; |
| 2090 | + case TOK_LE: v1->c.i = v1->c.i <= fc; break; |
| 2091 | + case TOK_GT: v1->c.i = v1->c.i > fc; break; |
| 2067 | 2092 | /* logical */ |
| 2068 | | − case TOK_LAND: v1->c = v1->c && fc; break; |
| 2069 | | − case TOK_LOR: v1->c = v1->c || fc; break; |
| 2093 | + case TOK_LAND: v1->c.i = v1->c.i && fc; break; |
| 2094 | + case TOK_LOR: v1->c.i = v1->c.i || fc; break; |
| 2070 | 2095 | default: |
| 2071 | 2096 | goto general_case; |
| 2072 | 2097 | } |
| @@ -2078,7 +2103,7 @@ void gen_opc(int op) · 5 unmodified lines |
| 2078 | 2103 | vswap(); |
| 2079 | 2104 | swap(&c1, &c2); |
| 2080 | 2105 | } |
| 2081 | | − fc = vtop->c; |
| 2106 | + fc = vtop->c.i; |
| 2082 | 2107 | if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV || |
| 2083 | 2108 | op == TOK_PDIV) && |
| 2084 | 2109 | fc == 1) || |
| @@ -2097,7 +2122,7 @@ void gen_opc(int op) · 12 unmodified lines |
| 2097 | 2122 | fc >>= 1; |
| 2098 | 2123 | n++; |
| 2099 | 2124 | } |
| 2100 | | − vtop->c = n; |
| 2125 | + vtop->c.i = n; |
| 2101 | 2126 | if (op == '*') |
| 2102 | 2127 | op = TOK_SHL; |
| 2103 | 2128 | else if (op == TOK_PDIV) |
| @@ -2120,14 +2145,6 @@ int pointed_size(int t) · 16 unmodified lines |
| 2120 | 2145 | return type_size(pointed_type(t), &t); |
| 2121 | 2146 | } |
| 2122 | 2147 | |
| 2123 | | −/* true if float/double/long double type */ |
| 2124 | | −static inline int is_float(int t) |
| 2125 | | −{ |
| 2126 | | − int bt; |
| 2127 | | − bt = t & VT_BTYPE; |
| 2128 | | − return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT; |
| 2129 | | −} |
| 2130 | | − |
| 2131 | 2148 | /* generic gen_op: handles types problems */ |
| 2132 | 2149 | void gen_op(int op) |
| 2133 | 2150 | { |
| @@ -2219,23 +2236,111 @@ void gen_op(int op) · 85 unmodified lines |
| 2219 | 2236 | /* cast 'vtop' to 't' type */ |
| 2220 | 2237 | void gen_cast(int t) |
| 2221 | 2238 | { |
| 2222 | | − int r, bits, bt, vbt; |
| 2239 | + int r, bits, sbt, dbt, sf, df, c, st1, dt1; |
| 2223 | 2240 | |
| 2224 | 2241 | r = vtop->t & VT_VALMASK; |
| 2225 | 2242 | if (!(t & VT_LVAL)) { |
| 2226 | 2243 | /* if not lvalue, then we convert now */ |
| 2227 | | − bt = t & VT_BTYPE; |
| 2228 | | − vbt = vtop->t & VT_BTYPE; |
| 2229 | | − if (bt != vbt) { |
| 2230 | | − if (is_float(bt)) { |
| 2231 | | − /* need to generate value and do explicit cast */ |
| 2232 | | − gv(); |
| 2233 | | − gen_cvtf(bt); |
| 2234 | | − } else if (bt == VT_BOOL) { |
| 2244 | + dbt = t & VT_BTYPE; |
| 2245 | + sbt = vtop->t & VT_BTYPE; |
| 2246 | + if (sbt != dbt) { |
| 2247 | + sf = is_float(sbt); |
| 2248 | + df = is_float(dbt); |
| 2249 | + c = (vtop->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST; |
| 2250 | + if (sf && df) { |
| 2251 | + /* convert from fp to fp */ |
| 2252 | + if (c) { |
| 2253 | + /* constant case: we can do it now */ |
| 2254 | + /* XXX: in ISOC, cannot do it if error in convert */ |
| 2255 | + if (dbt == VT_FLOAT && sbt == VT_DOUBLE) |
| 2256 | + vtop->c.f = (float)vtop->c.d; |
| 2257 | + else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE) |
| 2258 | + vtop->c.f = (float)vtop->c.ld; |
| 2259 | + else if (dbt == VT_DOUBLE && sbt == VT_FLOAT) |
| 2260 | + vtop->c.d = (double)vtop->c.f; |
| 2261 | + else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE) |
| 2262 | + vtop->c.d = (double)vtop->c.ld; |
| 2263 | + else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT) |
| 2264 | + vtop->c.ld = (long double)vtop->c.f; |
| 2265 | + else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE) |
| 2266 | + vtop->c.ld = (long double)vtop->c.d; |
| 2267 | + } else { |
| 2268 | + /* non constant case: generate code */ |
| 2269 | + gen_cvt_ftof(dbt); |
| 2270 | + } |
| 2271 | + } else if (df) { |
| 2272 | + /* convert int to fp */ |
| 2273 | + /* XXX: add const cases */ |
| 2274 | + st1 = vtop->t & (VT_BTYPE | VT_UNSIGNED); |
| 2275 | + if (c) { |
| 2276 | + switch(st1) { |
| 2277 | + case VT_LLONG | VT_UNSIGNED: |
| 2278 | + case VT_LLONG: |
| 2279 | + /* well, currently not needed */ |
| 2280 | + goto do_itof; |
| 2281 | + case VT_INT | VT_UNSIGNED: |
| 2282 | + switch(dbt) { |
| 2283 | + case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break; |
| 2284 | + case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break; |
| 2285 | + case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break; |
| 2286 | + } |
| 2287 | + break; |
| 2288 | + default: |
| 2289 | + switch(dbt) { |
| 2290 | + case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break; |
| 2291 | + case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break; |
| 2292 | + case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break; |
| 2293 | + } |
| 2294 | + break; |
| 2295 | + } |
| 2296 | + } else { |
| 2297 | + do_itof: |
| 2298 | + gen_cvt_itof(dbt); |
| 2299 | + } |
| 2300 | + } else if (sf) { |
| 2301 | + /* convert fp to int */ |
| 2302 | + dt1 = t & (VT_BTYPE | VT_UNSIGNED); |
| 2303 | + /* we handle char/short/etc... with generic code */ |
| 2304 | + if (dt1 != VT_INT | VT_UNSIGNED && |
| 2305 | + dt1 != VT_LLONG | VT_UNSIGNED && |
| 2306 | + dt1 != VT_LLONG) |
| 2307 | + dt1 = VT_INT; |
| 2308 | + if (c) { |
| 2309 | + switch(dt1) { |
| 2310 | + case VT_LLONG | VT_UNSIGNED: |
| 2311 | + case VT_LLONG: |
| 2312 | + /* well, currently not needed */ |
| 2313 | + goto do_ftoi; |
| 2314 | + case VT_INT | VT_UNSIGNED: |
| 2315 | + switch(sbt) { |
| 2316 | + case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break; |
| 2317 | + case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break; |
| 2318 | + case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break; |
| 2319 | + } |
| 2320 | + break; |
| 2321 | + default: |
| 2322 | + /* int case */ |
| 2323 | + switch(sbt) { |
| 2324 | + case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break; |
| 2325 | + case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break; |
| 2326 | + case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break; |
| 2327 | + } |
| 2328 | + break; |
| 2329 | + } |
| 2330 | + } else { |
| 2331 | + do_ftoi: |
| 2332 | + gen_cvt_ftoi(dt1); |
| 2333 | + } |
| 2334 | + if (dt1 == VT_INT && (t & (VT_TYPE | VT_UNSIGNED)) != dt1) { |
| 2335 | + /* additionnal cast for char/short/bool... */ |
| 2336 | + vtop->t = (vtop->t & ~VT_TYPE) | dt1; |
| 2337 | + gen_cast(t); |
| 2338 | + } |
| 2339 | + } else if (dbt == VT_BOOL) { |
| 2235 | 2340 | vset(VT_CONST, 0); |
| 2236 | 2341 | gen_op(TOK_NE); |
| 2237 | | − } else if (bt == VT_BYTE || bt == VT_SHORT) { |
| 2238 | | − if (bt == VT_BYTE) |
| 2342 | + } else if (dbt == VT_BYTE || dbt == VT_SHORT) { |
| 2343 | + if (dbt == VT_BYTE) |
| 2239 | 2344 | bits = 8; |
| 2240 | 2345 | else |
| 2241 | 2346 | bits = 16; |
| @@ -2310,6 +2415,181 @@ int mk_pointer(int t) · 68 unmodified lines |
| 2310 | 2415 | return VT_PTR | (p << VT_STRUCT_SHIFT) | (t & ~VT_TYPE); |
| 2311 | 2416 | } |
| 2312 | 2417 | |
| 2418 | +int is_compatible_types(int t1, int t2) |
| 2419 | +{ |
| 2420 | + Sym *s1, *s2; |
| 2421 | + int bt1, bt2; |
| 2422 | + |
| 2423 | + t1 &= VT_TYPE; |
| 2424 | + t2 &= VT_TYPE; |
| 2425 | + bt1 = t1 & VT_BTYPE; |
| 2426 | + bt2 = t2 & VT_BTYPE; |
| 2427 | + if (bt1 == VT_PTR) { |
| 2428 | + t1 = pointed_type(t1); |
| 2429 | + /* if function, then convert implicitely to function pointer */ |
| 2430 | + if (bt2 != VT_FUNC) { |
| 2431 | + if (bt2 != VT_PTR) |
| 2432 | + return 0; |
| 2433 | + t2 = pointed_type(t2); |
| 2434 | + } |
| 2435 | + /* void matches everything */ |
| 2436 | + t1 &= VT_TYPE; |
| 2437 | + t2 &= VT_TYPE; |
| 2438 | + if (t1 == VT_VOID || t2 == VT_VOID) |
| 2439 | + return 1; |
| 2440 | + return is_compatible_types(t1, t2); |
| 2441 | + } else if (bt1 == VT_STRUCT) { |
| 2442 | + return (t2 == t1); |
| 2443 | + } else if (bt1 == VT_FUNC) { |
| 2444 | + if (bt2 != VT_FUNC) |
| 2445 | + return 0; |
| 2446 | + s1 = sym_find(((unsigned)t1 >> VT_STRUCT_SHIFT)); |
| 2447 | + s2 = sym_find(((unsigned)t2 >> VT_STRUCT_SHIFT)); |
| 2448 | + if (!is_compatible_types(s1->t, s2->t)) |
| 2449 | + return 0; |
| 2450 | + /* XXX: not complete */ |
| 2451 | + if (s1->c == FUNC_OLD || s2->c == FUNC_OLD) |
| 2452 | + return 1; |
| 2453 | + if (s1->c != s2->c) |
| 2454 | + return 0; |
| 2455 | + while (s1 != NULL) { |
| 2456 | + if (s2 == NULL) |
| 2457 | + return 0; |
| 2458 | + if (!is_compatible_types(s1->t, s2->t)) |
| 2459 | + return 0; |
| 2460 | + s1 = s1->next; |
| 2461 | + s2 = s2->next; |
| 2462 | + } |
| 2463 | + if (s2) |
| 2464 | + return 0; |
| 2465 | + return 1; |
| 2466 | + } else { |
| 2467 | + /* XXX: not complete */ |
| 2468 | + return 1; |
| 2469 | + } |
| 2470 | +} |
| 2471 | + |
| 2472 | +int check_assign_types(int t1, int t2) |
| 2473 | +{ |
| 2474 | + t1 &= VT_TYPE; |
| 2475 | + t2 &= VT_TYPE; |
| 2476 | + if ((t1 & VT_BTYPE) == VT_PTR && |
| 2477 | + (t2 & VT_BTYPE) == VT_FUNC) { |
| 2478 | + return is_compatible_types(pointed_type(t1), t2); |
| 2479 | + } else { |
| 2480 | + return is_compatible_types(t1, t2); |
| 2481 | + } |
| 2482 | +} |
| 2483 | + |
| 2484 | +/* print a type. If 'varstr' is not NULL, then the variable is also |
| 2485 | + printed in the type */ |
| 2486 | +/* XXX: add array and function pointers */ |
| 2487 | +/* XXX: buffer overflows */ |
| 2488 | +void type_to_str(char *buf, int buf_size, |
| 2489 | + int t, const char *varstr) |
| 2490 | +{ |
| 2491 | + int bt, v; |
| 2492 | + Sym *s, *sa; |
| 2493 | + char buf1[256]; |
| 2494 | + |
| 2495 | + t = t & VT_TYPE; |
| 2496 | + bt = t & VT_BTYPE; |
| 2497 | + buf[0] = '\0'; |
| 2498 | + if (t & VT_UNSIGNED) |
| 2499 | + strcat(buf, "unsigned "); |
| 2500 | + switch(bt) { |
| 2501 | + case VT_VOID: |
| 2502 | + strcat(buf, "void"); |
| 2503 | + break; |
| 2504 | + case VT_BOOL: |
| 2505 | + strcat(buf, "_Bool"); |
| 2506 | + break; |
| 2507 | + case VT_BYTE: |
| 2508 | + strcat(buf, "char"); |
| 2509 | + break; |
| 2510 | + case VT_SHORT: |
| 2511 | + strcat(buf, "short"); |
| 2512 | + break; |
| 2513 | + case VT_INT: |
| 2514 | + strcat(buf, "int"); |
| 2515 | + break; |
| 2516 | + case VT_LONG: |
| 2517 | + strcat(buf, "long"); |
| 2518 | + break; |
| 2519 | + case VT_LLONG: |
| 2520 | + strcat(buf, "long long"); |
| 2521 | + break; |
| 2522 | + case VT_FLOAT: |
| 2523 | + strcat(buf, "float"); |
| 2524 | + break; |
| 2525 | + case VT_DOUBLE: |
| 2526 | + strcat(buf, "double"); |
| 2527 | + break; |
| 2528 | + case VT_LDOUBLE: |
| 2529 | + strcat(buf, "long double"); |
| 2530 | + break; |
| 2531 | + case VT_ENUM: |
| 2532 | + case VT_STRUCT: |
| 2533 | + if (bt == VT_STRUCT) |
| 2534 | + strcat(buf, "struct "); |
| 2535 | + else |
| 2536 | + strcat(buf, "enum "); |
| 2537 | + v = (unsigned)t >> VT_STRUCT_SHIFT; |
| 2538 | + if (v >= SYM_FIRST_ANOM) |
| 2539 | + strcat(buf, "<anonymous>"); |
| 2540 | + else |
| 2541 | + strcat(buf, get_tok_str(v, NULL)); |
| 2542 | + break; |
| 2543 | + case VT_FUNC: |
| 2544 | + s = sym_find((unsigned)t >> VT_STRUCT_SHIFT); |
| 2545 | + type_to_str(buf, buf_size, s->t, varstr); |
| 2546 | + strcat(buf, "("); |
| 2547 | + sa = s->next; |
| 2548 | + while (sa != NULL) { |
| 2549 | + type_to_str(buf1, sizeof(buf1), sa->t, NULL); |
| 2550 | + strcat(buf, buf1); |
| 2551 | + sa = sa->next; |
| 2552 | + if (sa) |
| 2553 | + strcat(buf, ", "); |
| 2554 | + } |
| 2555 | + strcat(buf, ")"); |
| 2556 | + goto no_var; |
| 2557 | + case VT_PTR: |
| 2558 | + s = sym_find((unsigned)t >> VT_STRUCT_SHIFT); |
| 2559 | + strcpy(buf1, "*"); |
| 2560 | + if (varstr) |
| 2561 | + strcat(buf1, varstr); |
| 2562 | + type_to_str(buf, buf_size, s->t, buf1); |
| 2563 | + goto no_var; |
| 2564 | + } |
| 2565 | + if (varstr) { |
| 2566 | + strcat(buf, " "); |
| 2567 | + strcat(buf, varstr); |
| 2568 | + } |
| 2569 | + no_var: ; |
| 2570 | +} |
| 2571 | + |
| 2572 | + |
| 2573 | + |
| 2574 | +/* verify type compatibility to store vtop in 'st' type, and generate |
| 2575 | + casts if needed */ |
| 2576 | +void gen_assign_cast(int dt) |
| 2577 | +{ |
| 2578 | + int st; |
| 2579 | + char buf1[256], buf2[256]; |
| 2580 | + |
| 2581 | + st = vtop->t; /* destination type */ |
| 2582 | + if (!check_assign_types(dt, st)) { |
| 2583 | + type_to_str(buf1, sizeof(buf1), st, NULL); |
| 2584 | + type_to_str(buf2, sizeof(buf2), dt, NULL); |
| 2585 | + error("cannot cast '%s' to '%s'", buf1, buf2); |
| 2586 | + } |
| 2587 | + if ((dt & VT_BTYPE) == VT_BOOL || is_float(dt)) { |
| 2588 | + gen_cast(dt & VT_BTYPE); |
| 2589 | + } |
| 2590 | +} |
| 2591 | + |
| 2592 | + |
| 2313 | 2593 | /* store vtop in lvalue pushed on stack */ |
| 2314 | 2594 | void vstore(void) |
| 2315 | 2595 | { |
| @@ -2317,6 +2597,8 @@ void vstore(void) · 1 unmodified line |
| 2317 | 2597 | GFuncContext gf; |
| 2318 | 2598 | |
| 2319 | 2599 | ft = vtop[-1].t; |
| 2600 | + gen_assign_cast(ft & VT_TYPE); |
| 2601 | + |
| 2320 | 2602 | if ((vtop->t & VT_BTYPE) == VT_STRUCT) { |
| 2321 | 2603 | /* if structure, only generate pointer */ |
| 2322 | 2604 | /* structure assignment : generate memcpy */ |
| @@ -2364,13 +2646,9 @@ void vstore(void) · 41 unmodified lines |
| 2364 | 2646 | /* store result */ |
| 2365 | 2647 | vstore(); |
| 2366 | 2648 | } else { |
| 2367 | | − /* generate cast if needed implicit cast for bool */ |
| 2368 | | − if ((ft & VT_BTYPE) == VT_BOOL) { |
| 2369 | | − gen_cast(VT_BOOL); |
| 2370 | | − } |
| 2371 | 2649 | r = gv(); /* generate value */ |
| 2372 | 2650 | ft = vtop[-1].t; |
| 2373 | | − fc = vtop[-1].c; |
| 2651 | + fc = vtop[-1].c.i; |
| 2374 | 2652 | /* if lvalue was saved on stack, must read it */ |
| 2375 | 2653 | if ((ft & VT_VALMASK) == VT_LLOCAL) { |
| 2376 | 2654 | t = get_reg(REG_CLASS_INT); |
| @@ -2380,7 +2658,7 @@ void vstore(void) · 3 unmodified lines |
| 2380 | 2658 | store(r, ft, fc); |
| 2381 | 2659 | vtop--; |
| 2382 | 2660 | vtop->t = (ft & VT_TYPE) | r; |
| 2383 | | − vtop->c = 0; |
| 2661 | + vtop->c.i = 0; |
| 2384 | 2662 | } |
| 2385 | 2663 | } |
| 2386 | 2664 | |
| @@ -2401,7 +2679,7 @@ void inc(int post, int c) · 14 unmodified lines |
| 2401 | 2679 | load(r1, r, 0); /* move r to r1 */ |
| 2402 | 2680 | /* duplicates value */ |
| 2403 | 2681 | vtop[-2].t = (vtop->t & VT_TYPE) | r1; |
| 2404 | | − vtop[-2].c = 0; |
| 2682 | + vtop[-2].c.i = 0; |
| 2405 | 2683 | } |
| 2406 | 2684 | /* add constant */ |
| 2407 | 2685 | vset(VT_CONST, c - TOK_MID); |
| @@ -2470,7 +2748,8 @@ int struct_decl(int u) · 62 unmodified lines |
| 2470 | 2748 | t = type_decl(&v, b, TYPE_DIRECT); |
| 2471 | 2749 | if ((t & VT_BTYPE) == VT_FUNC || |
| 2472 | 2750 | (t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN))) |
| 2473 | | − error("invalid type for '%s'", get_tok_str(v, 0)); |
| 2751 | + error("invalid type for '%s'", |
| 2752 | + get_tok_str(v, NULL)); |
| 2474 | 2753 | } else { |
| 2475 | 2754 | t = b; |
| 2476 | 2755 | } |
| @@ -2480,10 +2759,10 @@ int struct_decl(int u) · 3 unmodified lines |
| 2480 | 2759 | /* XXX: handle v = 0 case for messages */ |
| 2481 | 2760 | if (bit_size < 0) |
| 2482 | 2761 | error("negative width in bit-field '%s'", |
| 2483 | | − get_tok_str(v, 0)); |
| 2762 | + get_tok_str(v, NULL)); |
| 2484 | 2763 | if (v && bit_size == 0) |
| 2485 | 2764 | error("zero width for bit-field '%s'", |
| 2486 | | − get_tok_str(v, 0)); |
| 2765 | + get_tok_str(v, NULL)); |
| 2487 | 2766 | } |
| 2488 | 2767 | size = type_size(t, &align); |
| 2489 | 2768 | lbit_pos = 0; |
| @@ -2496,7 +2775,7 @@ int struct_decl(int u) · 6 unmodified lines |
| 2496 | 2775 | bsize = size * 8; |
| 2497 | 2776 | if (bit_size > bsize) { |
| 2498 | 2777 | error("width of '%s' exceeds its type", |
| 2499 | | − get_tok_str(v, 0)); |
| 2778 | + get_tok_str(v, NULL)); |
| 2500 | 2779 | } else if (bit_size == bsize) { |
| 2501 | 2780 | /* no need for bit fields */ |
| 2502 | 2781 | bit_pos = 0; |
| @@ -2538,7 +2817,7 @@ int struct_decl(int u) · 35 unmodified lines |
| 2538 | 2817 | } |
| 2539 | 2818 | #if 0 |
| 2540 | 2819 | printf("add field %s offset=%d", |
| 2541 | | − get_tok_str(v, 0), offset); |
| 2820 | + get_tok_str(v, NULL), offset); |
| 2542 | 2821 | if (t & VT_BITFIELD) { |
| 2543 | 2822 | printf(" pos=%d size=%d", |
| 2544 | 2823 | (t >> VT_STRUCT_SHIFT) & 0x3f, |
| @@ -2698,10 +2977,12 @@ int post_type(int t) · 153 unmodified lines |
| 2698 | 2977 | goto old_proto; |
| 2699 | 2978 | } |
| 2700 | 2979 | } |
| 2980 | + l = FUNC_NEW; |
| 2701 | 2981 | if ((pt & VT_BTYPE) == VT_VOID && tok == ')') |
| 2702 | 2982 | break; |
| 2703 | | − l = FUNC_NEW; |
| 2704 | 2983 | pt = type_decl(&n, pt, TYPE_DIRECT | TYPE_ABSTRACT); |
| 2984 | + if ((pt & VT_BTYPE) == VT_VOID) |
| 2985 | + error("parameter declared as void"); |
| 2705 | 2986 | } else { |
| 2706 | 2987 | old_proto: |
| 2707 | 2988 | n = tok; |
| @@ -2722,6 +3003,9 @@ int post_type(int t) · 14 unmodified lines |
| 2722 | 3003 | } |
| 2723 | 3004 | } |
| 2724 | 3005 | } |
| 3006 | + /* if no parameters, then old type prototype */ |
| 3007 | + if (l == 0) |
| 3008 | + l = FUNC_OLD; |
| 2725 | 3009 | skip(')'); |
| 2726 | 3010 | t1 = t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN); |
| 2727 | 3011 | t = post_type(t & ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN)); |
| @@ -2826,6 +3110,24 @@ void indir(void) · 98 unmodified lines |
| 2826 | 3110 | vtop->t |= VT_LVAL; |
| 2827 | 3111 | } |
| 2828 | 3112 | |
| 3113 | +/* pass a parameter to a function and do type checking and casting */ |
| 3114 | +void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg) |
| 3115 | +{ |
| 3116 | + int func_type; |
| 3117 | + func_type = func->c; |
| 3118 | + if (func_type == FUNC_OLD || |
| 3119 | + (func_type == FUNC_ELLIPSIS && arg == NULL)) { |
| 3120 | + /* default casting : only need to convert float to double */ |
| 3121 | + if ((vtop->t & VT_BTYPE) == VT_FLOAT) |
| 3122 | + gen_cast(VT_DOUBLE); |
| 3123 | + } else if (arg == NULL) { |
| 3124 | + error("too many arguments to function"); |
| 3125 | + } else { |
| 3126 | + gen_assign_cast(arg->t); |
| 3127 | + } |
| 3128 | + gfunc_param(gf); |
| 3129 | +} |
| 3130 | + |
| 2829 | 3131 | void unary(void) |
| 2830 | 3132 | { |
| 2831 | 3133 | int n, t, ft, fc, p, align, size; |
| @@ -2836,16 +3138,13 @@ void unary(void) · 4 unmodified lines |
| 2836 | 3138 | vset(VT_CONST | VT_INT, tokc.i); |
| 2837 | 3139 | next(); |
| 2838 | 3140 | } else if (tok == TOK_CFLOAT) { |
| 2839 | | − /* currently, cannot do more */ |
| 2840 | | − vset(VT_CONST | VT_FLOAT, 0); |
| 3141 | + vsetc(VT_CONST | VT_FLOAT, &tokc); |
| 2841 | 3142 | next(); |
| 2842 | 3143 | } else if (tok == TOK_CDOUBLE) { |
| 2843 | | − /* currently, cannot do more */ |
| 2844 | | − vset(VT_CONST | VT_DOUBLE, 0); |
| 3144 | + vsetc(VT_CONST | VT_DOUBLE, &tokc); |
| 2845 | 3145 | next(); |
| 2846 | 3146 | } else if (tok == TOK_CLDOUBLE) { |
| 2847 | | − /* currently, cannot do more */ |
| 2848 | | − vset(VT_CONST | VT_LDOUBLE, 0); |
| 3147 | + vsetc(VT_CONST | VT_LDOUBLE, &tokc); |
| 2849 | 3148 | next(); |
| 2850 | 3149 | } else if (tok == TOK___FUNC__) { |
| 2851 | 3150 | /* special function name identifier */ |
| @@ -2895,7 +3194,7 @@ void unary(void) · 43 unmodified lines |
| 2895 | 3194 | gen_cast(ft); |
| 2896 | 3195 | } |
| 2897 | 3196 | } else { |
| 2898 | | − expr(); |
| 3197 | + gexpr(); |
| 2899 | 3198 | skip(')'); |
| 2900 | 3199 | } |
| 2901 | 3200 | } else if (t == '*') { |
| @@ -2914,9 +3213,9 @@ void unary(void) · 12 unmodified lines |
| 2914 | 3213 | if (t == '!') { |
| 2915 | 3214 | unary(); |
| 2916 | 3215 | if ((vtop->t & (VT_VALMASK | VT_LVAL)) == VT_CONST) |
| 2917 | | − vtop->c = !vtop->c; |
| 3216 | + vtop->c.i = !vtop->c.i; |
| 2918 | 3217 | else if ((vtop->t & VT_VALMASK) == VT_CMP) |
| 2919 | | − vtop->c = vtop->c ^ 1; |
| 3218 | + vtop->c.i = vtop->c.i ^ 1; |
| 2920 | 3219 | else |
| 2921 | 3220 | vset(VT_JMP, gtst(1, 0)); |
| 2922 | 3221 | } else |
| @@ -2936,7 +3235,7 @@ void unary(void) · 13 unmodified lines |
| 2936 | 3235 | else { |
| 2937 | 3236 | /* XXX: some code could be generated: add eval |
| 2938 | 3237 | flag */ |
| 2939 | | − expr(); |
| 3238 | + gexpr(); |
| 2940 | 3239 | t = vtop->t; |
| 2941 | 3240 | vpop(); |
| 2942 | 3241 | } |
| @@ -2960,7 +3259,7 @@ void unary(void) · 17 unmodified lines |
| 2960 | 3259 | s = sym_find(t); |
| 2961 | 3260 | if (!s) { |
| 2962 | 3261 | if (tok != '(') |
| 2963 | | − error("'%s' undeclared", get_tok_str(t, 0)); |
| 3262 | + error("'%s' undeclared", get_tok_str(t, NULL)); |
| 2964 | 3263 | /* for simple function calls, we tolerate undeclared |
| 2965 | 3264 | external reference */ |
| 2966 | 3265 | p = anon_sym++; |
| @@ -2971,7 +3270,7 @@ void unary(void) · 4 unmodified lines |
| 2971 | 3270 | vset(s->t, s->c); |
| 2972 | 3271 | /* if forward reference, we must point to s */ |
| 2973 | 3272 | if (vtop->t & VT_FORWARD) |
| 2974 | | − vtop->c = (int)s; |
| 3273 | + vtop->c.sym = s; |
| 2975 | 3274 | } |
| 2976 | 3275 | } |
| 2977 | 3276 | |
| @@ -3011,12 +3310,14 @@ void unary(void) · 33 unmodified lines |
| 3011 | 3310 | next(); |
| 3012 | 3311 | } else if (tok == '[') { |
| 3013 | 3312 | next(); |
| 3014 | | − expr(); |
| 3313 | + gexpr(); |
| 3015 | 3314 | gen_op('+'); |
| 3016 | 3315 | indir(); |
| 3017 | 3316 | skip(']'); |
| 3018 | 3317 | } else if (tok == '(') { |
| 3019 | | − int rett, retc; |
| 3318 | + int rett; |
| 3319 | + CValue retc; |
| 3320 | + Sym *sa; |
| 3020 | 3321 | |
| 3021 | 3322 | /* function call */ |
| 3022 | 3323 | if ((vtop->t & VT_BTYPE) != VT_FUNC) { |
| @@ -3032,12 +3333,12 @@ void unary(void) · 9 unmodified lines |
| 3032 | 3333 | } else { |
| 3033 | 3334 | vtop->t &= ~VT_LVAL; /* no lvalue */ |
| 3034 | 3335 | } |
| 3035 | | − |
| 3036 | 3336 | /* get return type */ |
| 3037 | 3337 | s = sym_find((unsigned)vtop->t >> VT_STRUCT_SHIFT); |
| 3038 | 3338 | save_regs(); /* save used temporary registers */ |
| 3039 | 3339 | gfunc_start(&gf); |
| 3040 | 3340 | next(); |
| 3341 | + sa = s->next; /* first parameter */ |
| 3041 | 3342 | #ifdef INVERT_FUNC_PARAMS |
| 3042 | 3343 | { |
| 3043 | 3344 | int *str, len, parlevel, *saved_macro_ptr; |
| @@ -3061,7 +3362,10 @@ void unary(void) · 17 unmodified lines |
| 3061 | 3362 | } |
| 3062 | 3363 | tok_add(&str, &len, -1); /* end of file added */ |
| 3063 | 3364 | tok_add(&str, &len, 0); |
| 3064 | | − sym_push2(&args, 0, 0, (int)str); |
| 3365 | + s1 = sym_push2(&args, 0, 0, (int)str); |
| 3366 | + s1->next = sa; /* add reference to argument */ |
| 3367 | + if (sa) |
| 3368 | + sa = sa->next; |
| 3065 | 3369 | if (tok != ',') |
| 3066 | 3370 | break; |
| 3067 | 3371 | next(); |
| @@ -3077,7 +3381,7 @@ void unary(void) · 9 unmodified lines |
| 3077 | 3381 | expr_eq(); |
| 3078 | 3382 | if (tok != -1) |
| 3079 | 3383 | expect("',' or ')'"); |
| 3080 | | − gfunc_param(&gf); |
| 3384 | + gfunc_param_typed(&gf, s, args->next); |
| 3081 | 3385 | s1 = args->prev; |
| 3082 | 3386 | free((int *)args->c); |
| 3083 | 3387 | free(args); |
| @@ -3101,93 +3405,30 @@ void unary(void) · 17 unmodified lines |
| 3101 | 3405 | gfunc_param(&gf); |
| 3102 | 3406 | } else { |
| 3103 | 3407 | rett = s->t | FUNC_RET_REG; /* return in register */ |
| 3104 | | − retc = 0; |
| 3408 | + retc.i = 0; |
| 3105 | 3409 | } |
| 3106 | 3410 | #ifndef INVERT_FUNC_PARAMS |
| 3107 | 3411 | while (tok != ')') { |
| 3108 | 3412 | expr_eq(); |
| 3109 | | − gfunc_param(&gf); |
| 3413 | + gfunc_param_typed(&gf, s, sa); |
| 3414 | + if (sa) |
| 3415 | + sa = sa->next; |
| 3110 | 3416 | if (tok == ',') |
| 3111 | 3417 | next(); |
| 3112 | 3418 | } |
| 3113 | 3419 | #endif |
| 3420 | + if (sa) |
| 3421 | + error("too few arguments to function %x", sa->t); |
| 3114 | 3422 | skip(')'); |
| 3115 | 3423 | gfunc_call(&gf); |
| 3116 | 3424 | /* return value */ |
| 3117 | | − vset(rett, retc); |
| 3425 | + vsetc(rett, &retc); |
| 3118 | 3426 | } else { |
| 3119 | 3427 | break; |
| 3120 | 3428 | } |
| 3121 | 3429 | } |
| 3122 | 3430 | } |
| 3123 | 3431 | |
| 3124 | | −int is_compatible_types(int t1, int t2) |
| 3125 | | −{ |
| 3126 | | − Sym *s1, *s2; |
| 3127 | | − int bt1, bt2; |
| 3128 | | − |
| 3129 | | − t1 &= VT_TYPE; |
| 3130 | | − t2 &= VT_TYPE; |
| 3131 | | − bt1 = t1 & VT_BTYPE; |
| 3132 | | − bt2 = t2 & VT_BTYPE; |
| 3133 | | − if (bt1 == VT_PTR) { |
| 3134 | | − t1 = pointed_type(t1); |
| 3135 | | − /* if function, then convert implicitely to function pointer */ |
| 3136 | | − if (bt2 != VT_FUNC) { |
| 3137 | | − if (bt2 != VT_PTR) |
| 3138 | | − return 0; |
| 3139 | | − t2 = pointed_type(t2); |
| 3140 | | − } |
| 3141 | | − /* void matches everything */ |
| 3142 | | − t1 &= VT_TYPE; |
| 3143 | | − t2 &= VT_TYPE; |
| 3144 | | − if (t1 == VT_VOID || t2 == VT_VOID) |
| 3145 | | − return 1; |
| 3146 | | − return is_compatible_types(t1, t2); |
| 3147 | | − } else if (bt1 == VT_STRUCT) { |
| 3148 | | − return (t2 == t1); |
| 3149 | | − } else if (bt1 == VT_FUNC) { |
| 3150 | | − if (bt2 != VT_FUNC) |
| 3151 | | − return 0; |
| 3152 | | − s1 = sym_find(((unsigned)t1 >> VT_STRUCT_SHIFT)); |
| 3153 | | − s2 = sym_find(((unsigned)t2 >> VT_STRUCT_SHIFT)); |
| 3154 | | − if (!is_compatible_types(s1->t, s2->t)) |
| 3155 | | − return 0; |
| 3156 | | − /* XXX: not complete */ |
| 3157 | | − if (s1->c == FUNC_OLD || s2->c == FUNC_OLD) |
| 3158 | | − return 1; |
| 3159 | | − if (s1->c != s2->c) |
| 3160 | | − return 0; |
| 3161 | | − while (s1 != NULL) { |
| 3162 | | − if (s2 == NULL) |
| 3163 | | − return 0; |
| 3164 | | − if (!is_compatible_types(s1->t, s2->t)) |
| 3165 | | − return 0; |
| 3166 | | − s1 = s1->next; |
| 3167 | | − s2 = s2->next; |
| 3168 | | − } |
| 3169 | | − if (s2) |
| 3170 | | − return 0; |
| 3171 | | − return 1; |
| 3172 | | − } else { |
| 3173 | | − /* XXX: not complete */ |
| 3174 | | − return 1; |
| 3175 | | − } |
| 3176 | | −} |
| 3177 | | − |
| 3178 | | −int check_assign_types(int t1, int t2) |
| 3179 | | −{ |
| 3180 | | − t1 &= VT_TYPE; |
| 3181 | | − t2 &= VT_TYPE; |
| 3182 | | − if ((t1 & VT_BTYPE) == VT_PTR && |
| 3183 | | − (t2 & VT_BTYPE) == VT_FUNC) { |
| 3184 | | − return is_compatible_types(pointed_type(t1), t2); |
| 3185 | | − } else { |
| 3186 | | − return is_compatible_types(t1, t2); |
| 3187 | | − } |
| 3188 | | −} |
| 3189 | | − |
| 3190 | | − |
| 3191 | 3432 | void uneq(void) |
| 3192 | 3433 | { |
| 3193 | 3434 | int t; |
| @@ -3202,8 +3443,6 @@ void uneq(void) · 8 unmodified lines |
| 3202 | 3443 | next(); |
| 3203 | 3444 | if (t == '=') { |
| 3204 | 3445 | expr_eq(); |
| 3205 | | − if (!check_assign_types(vtop[-1].t, vtop[0].t)) |
| 3206 | | − warning("incompatible types"); |
| 3207 | 3446 | } else { |
| 3208 | 3447 | vdup(); |
| 3209 | 3448 | expr_eq(); |
| @@ -3289,16 +3528,16 @@ void expr_eq(void) · 79 unmodified lines |
| 3289 | 3528 | if (const_wanted) { |
| 3290 | 3529 | sum(10); |
| 3291 | 3530 | if (tok == '?') { |
| 3292 | | − c = vtop->c; |
| 3531 | + c = vtop->c.i; |
| 3293 | 3532 | vpop(); |
| 3294 | 3533 | next(); |
| 3295 | | − expr(); |
| 3296 | | − t = vtop->c; |
| 3534 | + gexpr(); |
| 3535 | + t = vtop->c.i; |
| 3297 | 3536 | vpop(); |
| 3298 | 3537 | skip(':'); |
| 3299 | 3538 | expr_eq(); |
| 3300 | 3539 | if (c) |
| 3301 | | − vtop->c = t; |
| 3540 | + vtop->c.i = t; |
| 3302 | 3541 | } |
| 3303 | 3542 | } else { |
| 3304 | 3543 | eor(); |
| @@ -3306,7 +3545,7 @@ void expr_eq(void) · 1 unmodified line |
| 3306 | 3545 | next(); |
| 3307 | 3546 | t = gtst(1, 0); |
| 3308 | 3547 | |
| 3309 | | − expr(); |
| 3548 | + gexpr(); |
| 3310 | 3549 | r1 = gv(); |
| 3311 | 3550 | vpop(); |
| 3312 | 3551 | skip(':'); |
| @@ -3322,7 +3561,7 @@ void expr_eq(void) · 9 unmodified lines |
| 3322 | 3561 | } |
| 3323 | 3562 | } |
| 3324 | 3563 | |
| 3325 | | −void expr(void) |
| 3564 | +void gexpr(void) |
| 3326 | 3565 | { |
| 3327 | 3566 | while (1) { |
| 3328 | 3567 | expr_eq(); |
| @@ -3333,16 +3572,24 @@ void expr(void) · 4 unmodified lines |
| 3333 | 3572 | } |
| 3334 | 3573 | } |
| 3335 | 3574 | |
| 3336 | | −int expr_const(void) |
| 3575 | +/* parse a constant expression and return value in vtop */ |
| 3576 | +void expr_const1(void) |
| 3337 | 3577 | { |
| 3338 | | − int a, c; |
| 3578 | + int a; |
| 3339 | 3579 | a = const_wanted; |
| 3340 | 3580 | const_wanted = 1; |
| 3341 | 3581 | expr_eq(); |
| 3342 | 3582 | if ((vtop->t & (VT_CONST | VT_LVAL)) != VT_CONST) |
| 3343 | 3583 | expect("constant"); |
| 3344 | 3584 | const_wanted = a; |
| 3345 | | − c = vtop->c; |
| 3585 | +} |
| 3586 | + |
| 3587 | +/* parse an integer constant and return its value */ |
| 3588 | +int expr_const(void) |
| 3589 | +{ |
| 3590 | + int c; |
| 3591 | + expr_const1(); |
| 3592 | + c = vtop->c.i; |
| 3346 | 3593 | vpop(); |
| 3347 | 3594 | return c; |
| 3348 | 3595 | } |
| @@ -3383,7 +3630,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 34 unmodified lines |
| 3383 | 3630 | /* if test */ |
| 3384 | 3631 | next(); |
| 3385 | 3632 | skip('('); |
| 3386 | | − expr(); |
| 3633 | + gexpr(); |
| 3387 | 3634 | skip(')'); |
| 3388 | 3635 | a = gtst(1, 0); |
| 3389 | 3636 | block(bsym, csym, case_sym, def_sym, case_reg); |
| @@ -3400,7 +3647,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 10 unmodified lines |
| 3400 | 3647 | next(); |
| 3401 | 3648 | d = ind; |
| 3402 | 3649 | skip('('); |
| 3403 | | − expr(); |
| 3650 | + gexpr(); |
| 3404 | 3651 | skip(')'); |
| 3405 | 3652 | a = gtst(1, 0); |
| 3406 | 3653 | b = 0; |
| @@ -3423,7 +3670,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 16 unmodified lines |
| 3423 | 3670 | } else if (tok == TOK_RETURN) { |
| 3424 | 3671 | next(); |
| 3425 | 3672 | if (tok != ';') { |
| 3426 | | − expr(); |
| 3673 | + gexpr(); |
| 3427 | 3674 | if ((func_vt & VT_BTYPE) == VT_STRUCT) { |
| 3428 | 3675 | /* if returning structure, must copy it to implicit |
| 3429 | 3676 | first pointer arg location */ |
| @@ -3432,6 +3679,9 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 2 unmodified lines |
| 3432 | 3679 | vswap(); |
| 3433 | 3680 | /* copy structure value to pointer */ |
| 3434 | 3681 | vstore(); |
| 3682 | + } else if (is_float(func_vt)) { |
| 3683 | + /* move return value to float return register */ |
| 3684 | + move_reg(FUNC_RET_FREG, gv()); |
| 3435 | 3685 | } else { |
| 3436 | 3686 | /* move return value to standard return register */ |
| 3437 | 3687 | move_reg(FUNC_RET_REG, gv()); |
| @@ -3459,7 +3709,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 21 unmodified lines |
| 3459 | 3709 | next(); |
| 3460 | 3710 | skip('('); |
| 3461 | 3711 | if (tok != ';') { |
| 3462 | | − expr(); |
| 3712 | + gexpr(); |
| 3463 | 3713 | vpop(); |
| 3464 | 3714 | } |
| 3465 | 3715 | skip(';'); |
| @@ -3468,14 +3718,14 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 2 unmodified lines |
| 3468 | 3718 | a = 0; |
| 3469 | 3719 | b = 0; |
| 3470 | 3720 | if (tok != ';') { |
| 3471 | | − expr(); |
| 3721 | + gexpr(); |
| 3472 | 3722 | a = gtst(1, 0); |
| 3473 | 3723 | } |
| 3474 | 3724 | skip(';'); |
| 3475 | 3725 | if (tok != ')') { |
| 3476 | 3726 | e = gjmp(0); |
| 3477 | 3727 | c = ind; |
| 3478 | | − expr(); |
| 3728 | + gexpr(); |
| 3479 | 3729 | vpop(); |
| 3480 | 3730 | oad(0xe9, d - ind - 5); /* jmp */ |
| 3481 | 3731 | gsym(e); |
| @@ -3495,7 +3745,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 13 unmodified lines |
| 3495 | 3745 | skip(TOK_WHILE); |
| 3496 | 3746 | skip('('); |
| 3497 | 3747 | gsym(b); |
| 3498 | | − expr(); |
| 3748 | + gexpr(); |
| 3499 | 3749 | c = gtst(0, 0); |
| 3500 | 3750 | gsym_addr(c, d); |
| 3501 | 3751 | skip(')'); |
| @@ -3505,7 +3755,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 3 unmodified lines |
| 3505 | 3755 | if (tok == TOK_SWITCH) { |
| 3506 | 3756 | next(); |
| 3507 | 3757 | skip('('); |
| 3508 | | − expr(); |
| 3758 | + gexpr(); |
| 3509 | 3759 | case_reg = gv(); |
| 3510 | 3760 | vpop(); |
| 3511 | 3761 | skip(')'); |
| @@ -3582,7 +3832,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 70 unmodified lines |
| 3582 | 3832 | } else { |
| 3583 | 3833 | /* expression case */ |
| 3584 | 3834 | if (tok != ';') { |
| 3585 | | − expr(); |
| 3835 | + gexpr(); |
| 3586 | 3836 | vpop(); |
| 3587 | 3837 | } |
| 3588 | 3838 | skip(';'); |
| @@ -3670,28 +3920,50 @@ void decl_designator(int t, int c, · 81 unmodified lines |
| 3670 | 3920 | |
| 3671 | 3921 | void init_putv(int t, int c, int v, int is_expr) |
| 3672 | 3922 | { |
| 3673 | | − int saved_global_expr; |
| 3923 | + int saved_global_expr, bt; |
| 3674 | 3924 | |
| 3675 | 3925 | if ((t & VT_VALMASK) == VT_CONST) { |
| 3676 | 3926 | if (is_expr) { |
| 3677 | 3927 | /* compound literals must be allocated globally in this case */ |
| 3678 | 3928 | saved_global_expr = global_expr; |
| 3679 | 3929 | global_expr = 1; |
| 3680 | | − v = expr_const(); |
| 3930 | + expr_const1(); |
| 3681 | 3931 | global_expr = saved_global_expr; |
| 3932 | + } else { |
| 3933 | + vset(VT_CONST | VT_INT, v); |
| 3682 | 3934 | } |
| 3683 | | − if ((t & VT_BTYPE) == VT_BYTE) |
| 3684 | | − *(char *)c = v; |
| 3685 | | − else if ((t & VT_BTYPE) == VT_SHORT) |
| 3686 | | − *(short *)c = v; |
| 3687 | | − else |
| 3688 | | − *(int *)c = v; |
| 3935 | + /* XXX: do casting */ |
| 3936 | + /* XXX: not portable */ |
| 3937 | + bt = vtop->t & VT_BTYPE; |
| 3938 | + switch(bt) { |
| 3939 | + case VT_BYTE: |
| 3940 | + *(char *)c = vtop->c.i; |
| 3941 | + break; |
| 3942 | + case VT_SHORT: |
| 3943 | + *(short *)c = vtop->c.i; |
| 3944 | + break; |
| 3945 | + case VT_DOUBLE: |
| 3946 | + *(double *)c = vtop->c.d; |
| 3947 | + break; |
| 3948 | + case VT_LDOUBLE: |
| 3949 | + *(long double *)c = vtop->c.ld; |
| 3950 | + break; |
| 3951 | +#if 0 |
| 3952 | + case VT_LLONG: |
| 3953 | + *(long long *)c = vtop->c.ll; |
| 3954 | + break; |
| 3955 | +#endif |
| 3956 | + default: |
| 3957 | + *(int *)c = vtop->c.i; |
| 3958 | + break; |
| 3959 | + } |
| 3960 | + vpop(); |
| 3689 | 3961 | } else { |
| 3690 | 3962 | vset(t, c); |
| 3691 | 3963 | if (is_expr) |
| 3692 | 3964 | expr_eq(); |
| 3693 | 3965 | else |
| 3694 | | − vset(VT_CONST, v); |
| 3966 | + vset(VT_CONST | VT_INT, v); |
| 3695 | 3967 | vstore(); |
| 3696 | 3968 | vpop(); |
| 3697 | 3969 | } |
| @@ -3969,6 +4241,13 @@ void decl(int l) · 271 unmodified lines |
| 3969 | 4241 | } |
| 3970 | 4242 | while (1) { /* iterate thru each declaration */ |
| 3971 | 4243 | t = type_decl(&v, b, TYPE_DIRECT); |
| 4244 | +#if 0 |
| 4245 | + { |
| 4246 | + char buf[500]; |
| 4247 | + type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL)); |
| 4248 | + printf("type = '%s'\n", buf); |
| 4249 | + } |
| 4250 | +#endif |
| 3972 | 4251 | if (tok == '{') { |
| 3973 | 4252 | if (l == VT_LOCAL) |
| 3974 | 4253 | error("cannot use local functions"); |
| @@ -3982,7 +4261,7 @@ void decl(int l) · 7 unmodified lines |
| 3982 | 4261 | /* put function address */ |
| 3983 | 4262 | sym_push1(&global_stack, v, VT_CONST | t, ind); |
| 3984 | 4263 | } |
| 3985 | | − funcname = get_tok_str(v, 0); |
| 4264 | + funcname = get_tok_str(v, NULL); |
| 3986 | 4265 | /* push a dummy symbol to enable local sym storage */ |
| 3987 | 4266 | sym_push1(&local_stack, 0, 0, 0); |
| 3988 | 4267 | /* define parameters */ |
| @@ -4002,27 +4281,21 @@ void decl(int l) · 13 unmodified lines |
| 4002 | 4281 | sym_push(sym->v & ~SYM_FIELD, |
| 4003 | 4282 | u | VT_LOCAL | VT_LVAL, |
| 4004 | 4283 | addr); |
| 4005 | | − if ((u & VT_BTYPE) == VT_STRUCT) { |
| 4284 | + size = type_size(u, &align); |
| 4285 | + size = (size + 3) & ~3; |
| 4006 | 4286 | #ifdef FUNC_STRUCT_PARAM_AS_PTR |
| 4007 | | − /* structs are passed as pointer */ |
| 4008 | | − size = 4; |
| 4009 | | −#else |
| 4010 | | − /* structs are directly put on stack (x86 |
| 4011 | | − like) */ |
| 4012 | | − size = type_size(u, &align); |
| 4013 | | − size = (size + 3) & ~3; |
| 4014 | | −#endif |
| 4015 | | − } else { |
| 4016 | | − /* XXX: size will be different someday */ |
| 4287 | + /* structs are passed as pointer */ |
| 4288 | + if ((u & VT_BTYPE) == VT_STRUCT) { |
| 4017 | 4289 | size = 4; |
| 4018 | 4290 | } |
| 4291 | +#endif |
| 4019 | 4292 | addr += size; |
| 4020 | 4293 | } |
| 4021 | 4294 | loc = 0; |
| 4022 | 4295 | o(0xe58955); /* push %ebp, mov %esp, %ebp */ |
| 4023 | 4296 | a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */ |
| 4024 | 4297 | rsym = 0; |
| 4025 | | − block(0, 0, 0, 0, 0); |
| 4298 | + block(NULL, NULL, NULL, NULL, 0); |
| 4026 | 4299 | gsym(rsym); |
| 4027 | 4300 | o(0xc3c9); /* leave, ret */ |
| 4028 | 4301 | *a = (-loc + 3) & -4; /* align local size to word & |
| @@ -4063,9 +4336,9 @@ void decl(int l) · 34 unmodified lines |
| 4063 | 4336 | goto do_def; |
| 4064 | 4337 | if (!is_compatible_types(sym->t, u)) |
| 4065 | 4338 | error("incompatible types for redefinition of '%s'", |
| 4066 | | − get_tok_str(v, 0)); |
| 4339 | + get_tok_str(v, NULL)); |
| 4067 | 4340 | if (!(sym->t & VT_FORWARD)) |
| 4068 | | − error("redefinition of '%s'", get_tok_str(v, 0)); |
| 4341 | + error("redefinition of '%s'", get_tok_str(v, NULL)); |
| 4069 | 4342 | greloc_patch(sym, addr); |
| 4070 | 4343 | } else { |
| 4071 | 4344 | do_def: |
| @@ -4120,7 +4393,7 @@ void resolve_global_syms(void) · 48 unmodified lines |
| 4120 | 4393 | /* external symbol already exists and is defined : |
| 4121 | 4394 | patch all references to it */ |
| 4122 | 4395 | if (!(s->t & VT_FORWARD)) |
| 4123 | | − error("'%s' defined twice", get_tok_str(s->v, 0)); |
| 4396 | + error("'%s' defined twice", get_tok_str(s->v, NULL)); |
| 4124 | 4397 | greloc_patch(s, ext_sym->c); |
| 4125 | 4398 | } |
| 4126 | 4399 | } |
| @@ -4192,7 +4465,7 @@ void resolve_extern_syms(void) · 65 unmodified lines |
| 4192 | 4465 | /* if there is at least one relocation to do, then find it |
| 4193 | 4466 | and patch it */ |
| 4194 | 4467 | if (s->c) { |
| 4195 | | − str = get_tok_str(s->v, 0); |
| 4468 | + str = get_tok_str(s->v, NULL); |
| 4196 | 4469 | addr = (int)dlsym(NULL, str); |
| 4197 | 4470 | if (!addr) |
| 4198 | 4471 | error("unresolved external reference '%s'", str); |