Unified · Split
| @@ -57,6 +57,10 @@ · 56 unmodified lines | ||
| 57 | 57 | #define CONFIG_TCC_BCHECK /* enable bound checking code */ |
| 58 | 58 | #endif |
| 59 | 59 | |
| 60 | +#ifndef CONFIG_TCC_PREFIX | |
| 61 | +#define CONFIG_TCC_PREFIX "/usr/local" | |
| 62 | +#endif | |
| 63 | + | |
| 60 | 64 | /* amount of virtual memory associated to a section (currently, we do |
| 61 | 65 | not realloc them) */ |
| 62 | 66 | #define SECTION_VSIZE (1024 * 1024) |
| @@ -184,6 +188,22 @@ typedef struct BufferedFile { · 121 unmodified lines | ||
| 184 | 188 | #define CH_EOB 0 /* end of buffer or '\0' char in file */ |
| 185 | 189 | #define CH_EOF (-1) /* end of file */ |
| 186 | 190 | |
| 191 | +/* parsing state (used to save parser state to reparse part of the | |
| 192 | + source several times) */ | |
| 193 | +typedef struct ParseState { | |
| 194 | + int *macro_ptr; | |
| 195 | + int line_num; | |
| 196 | + int tok; | |
| 197 | + CValue tokc; | |
| 198 | +} ParseState; | |
| 199 | + | |
| 200 | +/* used to record tokens */ | |
| 201 | +typedef struct TokenString { | |
| 202 | + int *str; | |
| 203 | + int len; | |
| 204 | + int last_line_num; | |
| 205 | +} TokenString; | |
| 206 | + | |
| 187 | 207 | /* parser */ |
| 188 | 208 | struct BufferedFile *file; |
| 189 | 209 | int ch, ch1, tok, tok1; |
| @@ -266,6 +286,9 @@ struct TCCState { · 76 unmodified lines | ||
| 266 | 286 | char/short stored in integer registers) */ |
| 267 | 287 | #define VT_MUSTBOUND 0x0800 /* bound checking must be done before |
| 268 | 288 | dereferencing value */ |
| 289 | +#define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */ | |
| 290 | +#define VT_LVAL_SHORT 0x2000 /* lvalue is a short */ | |
| 291 | +#define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */ | |
| 269 | 292 | |
| 270 | 293 | /* types */ |
| 271 | 294 | #define VT_STRUCT_SHIFT 16 /* structure/enum name shift (16 bits left) */ |
| @@ -274,7 +297,7 @@ struct TCCState { · 2 unmodified lines | ||
| 274 | 297 | #define VT_BYTE 1 /* signed byte type */ |
| 275 | 298 | #define VT_SHORT 2 /* short type */ |
| 276 | 299 | #define VT_VOID 3 /* void type */ |
| 277 | −#define VT_PTR 4 /* pointer increment */ | |
| 300 | +#define VT_PTR 4 /* pointer */ | |
| 278 | 301 | #define VT_ENUM 5 /* enum definition */ |
| 279 | 302 | #define VT_FUNC 6 /* function type */ |
| 280 | 303 | #define VT_STRUCT 7 /* struct/union definition */ |
| @@ -325,9 +348,10 @@ struct TCCState { · 44 unmodified lines | ||
| 325 | 348 | #define TOK_CCHAR 0xb4 /* char constant in tokc */ |
| 326 | 349 | #define TOK_STR 0xb5 /* pointer to string in tokc */ |
| 327 | 350 | #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */ |
| 328 | −#define TOK_LCHAR 0xb7 | |
| 329 | −#define TOK_LSTR 0xb8 | |
| 351 | +#define TOK_LCHAR 0xb7 | |
| 352 | +#define TOK_LSTR 0xb8 | |
| 330 | 353 | #define TOK_CFLOAT 0xb9 /* float constant */ |
| 354 | +#define TOK_LINENUM 0xba /* line number info */ | |
| 331 | 355 | #define TOK_CDOUBLE 0xc0 /* double constant */ |
| 332 | 356 | #define TOK_CLDOUBLE 0xc1 /* long double constant */ |
| 333 | 357 | #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */ |
| @@ -504,7 +528,7 @@ void vswap(void); · 170 unmodified lines | ||
| 504 | 528 | void vdup(void); |
| 505 | 529 | int get_reg(int rc); |
| 506 | 530 | |
| 507 | −void macro_subst(int **tok_str, int *tok_len, | |
| 531 | +void macro_subst(TokenString *tok_str, | |
| 508 | 532 | Sym **nested_list, int *macro_str); |
| 509 | 533 | int save_reg_forced(int r); |
| 510 | 534 | void gen_op(int op); |
| @@ -1270,6 +1294,30 @@ void preprocess_skip(void) · 759 unmodified lines | ||
| 1270 | 1294 | } |
| 1271 | 1295 | } |
| 1272 | 1296 | |
| 1297 | +/* ParseState handling */ | |
| 1298 | + | |
| 1299 | +/* XXX: currently, no include file info is stored. Thus, we cannot display | |
| 1300 | + accurate messages if the function or data definition spans multiple | |
| 1301 | + files */ | |
| 1302 | + | |
| 1303 | +/* save current parse state in 's' */ | |
| 1304 | +void save_parse_state(ParseState *s) | |
| 1305 | +{ | |
| 1306 | + s->line_num = file->line_num; | |
| 1307 | + s->macro_ptr = macro_ptr; | |
| 1308 | + s->tok = tok; | |
| 1309 | + s->tokc = tokc; | |
| 1310 | +} | |
| 1311 | + | |
| 1312 | +/* restore parse state from 's' */ | |
| 1313 | +void restore_parse_state(ParseState *s) | |
| 1314 | +{ | |
| 1315 | + file->line_num = s->line_num; | |
| 1316 | + macro_ptr = s->macro_ptr; | |
| 1317 | + tok = s->tok; | |
| 1318 | + tokc = s->tokc; | |
| 1319 | +} | |
| 1320 | + | |
| 1273 | 1321 | /* return the number of additionnal 'ints' necessary to store the |
| 1274 | 1322 | token */ |
| 1275 | 1323 | static inline int tok_ext_size(int t) |
| @@ -1283,6 +1331,7 @@ static inline int tok_ext_size(int t) · 7 unmodified lines | ||
| 1283 | 1331 | case TOK_STR: |
| 1284 | 1332 | case TOK_LSTR: |
| 1285 | 1333 | case TOK_CFLOAT: |
| 1334 | + case TOK_LINENUM: | |
| 1286 | 1335 | return 1; |
| 1287 | 1336 | case TOK_CDOUBLE: |
| 1288 | 1337 | case TOK_CLLONG: |
| @@ -1295,33 +1344,56 @@ static inline int tok_ext_size(int t) · 6 unmodified lines | ||
| 1295 | 1344 | } |
| 1296 | 1345 | } |
| 1297 | 1346 | |
| 1298 | −void tok_add(int **tok_str, int *tok_len, int t) | |
| 1347 | +/* token string handling */ | |
| 1348 | + | |
| 1349 | +static inline void tok_str_new(TokenString *s) | |
| 1350 | +{ | |
| 1351 | + s->str = NULL; | |
| 1352 | + s->len = 0; | |
| 1353 | + s->last_line_num = -1; | |
| 1354 | +} | |
| 1355 | + | |
| 1356 | +static void tok_str_add(TokenString *s, int t) | |
| 1299 | 1357 | { |
| 1300 | 1358 | int len, *str; |
| 1301 | − len = *tok_len; | |
| 1302 | − str = *tok_str; | |
| 1359 | + | |
| 1360 | + len = s->len; | |
| 1361 | + str = s->str; | |
| 1303 | 1362 | if ((len & 63) == 0) { |
| 1304 | 1363 | str = realloc(str, (len + 64) * sizeof(int)); |
| 1305 | 1364 | if (!str) |
| 1306 | 1365 | return; |
| 1307 | − *tok_str = str; | |
| 1366 | + s->str = str; | |
| 1308 | 1367 | } |
| 1309 | 1368 | str[len++] = t; |
| 1310 | − *tok_len = len; | |
| 1369 | + s->len = len; | |
| 1311 | 1370 | } |
| 1312 | 1371 | |
| 1313 | −void tok_add2(int **tok_str, int *tok_len, int t, CValue *cv) | |
| 1372 | +static void tok_str_add2(TokenString *s, int t, CValue *cv) | |
| 1314 | 1373 | { |
| 1315 | 1374 | int n, i; |
| 1316 | − | |
| 1317 | − tok_add(tok_str, tok_len, t); | |
| 1375 | + tok_str_add(s, t); | |
| 1318 | 1376 | n = tok_ext_size(t); |
| 1319 | 1377 | for(i=0;i<n;i++) |
| 1320 | − tok_add(tok_str, tok_len, cv->tab[i]); | |
| 1378 | + tok_str_add(s, cv->tab[i]); | |
| 1379 | +} | |
| 1380 | + | |
| 1381 | +/* add the current parse token in token string 's' */ | |
| 1382 | +static void tok_str_add_tok(TokenString *s) | |
| 1383 | +{ | |
| 1384 | + CValue cval; | |
| 1385 | + | |
| 1386 | + /* save line number info */ | |
| 1387 | + if (file->line_num != s->last_line_num) { | |
| 1388 | + s->last_line_num = file->line_num; | |
| 1389 | + cval.i = s->last_line_num; | |
| 1390 | + tok_str_add2(s, TOK_LINENUM, &cval); | |
| 1391 | + } | |
| 1392 | + tok_str_add2(s, tok, &tokc); | |
| 1321 | 1393 | } |
| 1322 | 1394 | |
| 1323 | 1395 | /* get a token from an integer array and increment pointer accordingly */ |
| 1324 | −int tok_get(int **tok_str, CValue *cv) | |
| 1396 | +static int tok_get(int **tok_str, CValue *cv) | |
| 1325 | 1397 | { |
| 1326 | 1398 | int *p, t, n, i; |
| 1327 | 1399 | |
| @@ -1337,10 +1409,10 @@ int tok_get(int **tok_str, CValue *cv) · 9 unmodified lines | ||
| 1337 | 1409 | /* eval an expression for #if/#elif */ |
| 1338 | 1410 | int expr_preprocess(void) |
| 1339 | 1411 | { |
| 1340 | − int *str, len, c, t; | |
| 1412 | + int c, t; | |
| 1413 | + TokenString str; | |
| 1341 | 1414 | |
| 1342 | − str = NULL; | |
| 1343 | − len = 0; | |
| 1415 | + tok_str_new(&str); | |
| 1344 | 1416 | while (1) { |
| 1345 | 1417 | skip_spaces(); |
| 1346 | 1418 | if (ch == '\n') |
| @@ -1361,16 +1433,16 @@ int expr_preprocess(void) · 14 unmodified lines | ||
| 1361 | 1433 | tok = TOK_CINT; |
| 1362 | 1434 | tokc.i = 0; |
| 1363 | 1435 | } |
| 1364 | − tok_add2(&str, &len, tok, &tokc); | |
| 1436 | + tok_str_add_tok(&str); | |
| 1365 | 1437 | } |
| 1366 | − tok_add(&str, &len, -1); /* simulate end of file */ | |
| 1367 | − tok_add(&str, &len, 0); | |
| 1438 | + tok_str_add(&str, -1); /* simulate end of file */ | |
| 1439 | + tok_str_add(&str, 0); | |
| 1368 | 1440 | /* now evaluate C constant expression */ |
| 1369 | − macro_ptr = str; | |
| 1441 | + macro_ptr = str.str; | |
| 1370 | 1442 | next(); |
| 1371 | 1443 | c = expr_const(); |
| 1372 | 1444 | macro_ptr = NULL; |
| 1373 | − free(str); | |
| 1445 | + free(str.str); | |
| 1374 | 1446 | return c != 0; |
| 1375 | 1447 | } |
| 1376 | 1448 | |
| @@ -1394,8 +1466,9 @@ void tok_print(int *str) · 17 unmodified lines | ||
| 1394 | 1466 | void parse_define(void) |
| 1395 | 1467 | { |
| 1396 | 1468 | Sym *s, *first, **ps; |
| 1397 | − int v, t, *str, len; | |
| 1398 | − | |
| 1469 | + int v, t; | |
| 1470 | + TokenString str; | |
| 1471 | + | |
| 1399 | 1472 | v = tok; |
| 1400 | 1473 | /* XXX: should check if same macro (ANSI) */ |
| 1401 | 1474 | first = NULL; |
| @@ -1418,21 +1491,20 @@ void parse_define(void) · 16 unmodified lines | ||
| 1418 | 1491 | } |
| 1419 | 1492 | t = MACRO_FUNC; |
| 1420 | 1493 | } |
| 1421 | − str = NULL; | |
| 1422 | − len = 0; | |
| 1494 | + tok_str_new(&str); | |
| 1423 | 1495 | while (1) { |
| 1424 | 1496 | skip_spaces(); |
| 1425 | 1497 | if (ch == '\n' || ch == -1) |
| 1426 | 1498 | break; |
| 1427 | 1499 | next_nomacro(); |
| 1428 | − tok_add2(&str, &len, tok, &tokc); | |
| 1500 | + tok_str_add2(&str, tok, &tokc); | |
| 1429 | 1501 | } |
| 1430 | − tok_add(&str, &len, 0); | |
| 1502 | + tok_str_add(&str, 0); | |
| 1431 | 1503 | #ifdef PP_DEBUG |
| 1432 | 1504 | printf("define %s %d: ", get_tok_str(v, NULL), t); |
| 1433 | 1505 | tok_print(str); |
| 1434 | 1506 | #endif |
| 1435 | − s = sym_push1(&define_stack, v, t, (int)str); | |
| 1507 | + s = sym_push1(&define_stack, v, t, (int)str.str); | |
| 1436 | 1508 | s->next = first; |
| 1437 | 1509 | } |
| 1438 | 1510 | |
| @@ -1510,7 +1582,7 @@ void preprocess(void) · 71 unmodified lines | ||
| 1510 | 1582 | if (f) |
| 1511 | 1583 | goto found; |
| 1512 | 1584 | } |
| 1513 | − error("include file '%s' not found", buf1); | |
| 1585 | + error("include file '%s' not found", buf); | |
| 1514 | 1586 | f = NULL; |
| 1515 | 1587 | found: |
| 1516 | 1588 | /* push current file in stack */ |
| @@ -2065,9 +2137,15 @@ void next_nomacro1(void) · 548 unmodified lines | ||
| 2065 | 2137 | void next_nomacro() |
| 2066 | 2138 | { |
| 2067 | 2139 | if (macro_ptr) { |
| 2140 | + redo: | |
| 2068 | 2141 | tok = *macro_ptr; |
| 2069 | − if (tok) | |
| 2142 | + if (tok) { | |
| 2070 | 2143 | tok = tok_get(¯o_ptr, &tokc); |
| 2144 | + if (tok == TOK_LINENUM) { | |
| 2145 | + file->line_num = tokc.i; | |
| 2146 | + goto redo; | |
| 2147 | + } | |
| 2148 | + } | |
| 2071 | 2149 | } else { |
| 2072 | 2150 | next_nomacro1(); |
| 2073 | 2151 | } |
| @@ -2076,13 +2154,13 @@ void next_nomacro() · 2 unmodified lines | ||
| 2076 | 2154 | /* substitute args in macro_str and return allocated string */ |
| 2077 | 2155 | int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) |
| 2078 | 2156 | { |
| 2079 | − int *st, last_tok, t, notfirst, *str, len; | |
| 2157 | + int *st, last_tok, t, notfirst; | |
| 2080 | 2158 | Sym *s; |
| 2081 | 2159 | TokenSym *ts; |
| 2082 | 2160 | CValue cval; |
| 2083 | − | |
| 2084 | − str = NULL; | |
| 2085 | − len = 0; | |
| 2161 | + TokenString str; | |
| 2162 | + | |
| 2163 | + tok_str_new(&str); | |
| 2086 | 2164 | last_tok = 0; |
| 2087 | 2165 | while(1) { |
| 2088 | 2166 | t = tok_get(¯o_str, &cval); |
| @@ -2111,9 +2189,9 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 22 unmodified lines | ||
| 2111 | 2189 | /* add string */ |
| 2112 | 2190 | ts = tok_alloc(token_buf, 0); |
| 2113 | 2191 | cval.ts = ts; |
| 2114 | − tok_add2(&str, &len, TOK_STR, &cval); | |
| 2192 | + tok_str_add2(&str, TOK_STR, &cval); | |
| 2115 | 2193 | } else { |
| 2116 | − tok_add2(&str, &len, t, &cval); | |
| 2194 | + tok_str_add2(&str, t, &cval); | |
| 2117 | 2195 | } |
| 2118 | 2196 | } else if (t >= TOK_IDENT) { |
| 2119 | 2197 | s = sym_find2(args, t); |
| @@ -2122,33 +2200,33 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 2 unmodified lines | ||
| 2122 | 2200 | /* if '##' is present before or after , no arg substitution */ |
| 2123 | 2201 | if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) { |
| 2124 | 2202 | while (*st) |
| 2125 | − tok_add(&str, &len, *st++); | |
| 2203 | + tok_str_add(&str, *st++); | |
| 2126 | 2204 | } else { |
| 2127 | − macro_subst(&str, &len, nested_list, st); | |
| 2205 | + macro_subst(&str, nested_list, st); | |
| 2128 | 2206 | } |
| 2129 | 2207 | } else { |
| 2130 | − tok_add(&str, &len, t); | |
| 2208 | + tok_str_add(&str, t); | |
| 2131 | 2209 | } |
| 2132 | 2210 | } else { |
| 2133 | − tok_add2(&str, &len, t, &cval); | |
| 2211 | + tok_str_add2(&str, t, &cval); | |
| 2134 | 2212 | } |
| 2135 | 2213 | last_tok = t; |
| 2136 | 2214 | } |
| 2137 | − tok_add(&str, &len, 0); | |
| 2138 | − return str; | |
| 2215 | + tok_str_add(&str, 0); | |
| 2216 | + return str.str; | |
| 2139 | 2217 | } |
| 2140 | 2218 | |
| 2141 | 2219 | /* handle the '##' operator */ |
| 2142 | 2220 | int *macro_twosharps(int *macro_str) |
| 2143 | 2221 | { |
| 2144 | 2222 | TokenSym *ts; |
| 2145 | − int *macro_str1, macro_str1_len, *macro_ptr1; | |
| 2223 | + int *macro_ptr1; | |
| 2146 | 2224 | int t; |
| 2147 | 2225 | char *p; |
| 2148 | 2226 | CValue cval; |
| 2149 | − | |
| 2150 | − macro_str1 = NULL; | |
| 2151 | − macro_str1_len = 0; | |
| 2227 | + TokenString macro_str1; | |
| 2228 | + | |
| 2229 | + tok_str_new(¯o_str1); | |
| 2152 | 2230 | tok = 0; |
| 2153 | 2231 | while (1) { |
| 2154 | 2232 | next_nomacro(); |
| @@ -2177,26 +2255,25 @@ int *macro_twosharps(int *macro_str) · 22 unmodified lines | ||
| 2177 | 2255 | } |
| 2178 | 2256 | } |
| 2179 | 2257 | } |
| 2180 | − tok_add2(¯o_str1, ¯o_str1_len, tok, &tokc); | |
| 2258 | + tok_str_add2(¯o_str1, tok, &tokc); | |
| 2181 | 2259 | } |
| 2182 | − tok_add(¯o_str1, ¯o_str1_len, 0); | |
| 2183 | − return macro_str1; | |
| 2260 | + tok_str_add(¯o_str1, 0); | |
| 2261 | + return macro_str1.str; | |
| 2184 | 2262 | } |
| 2185 | 2263 | |
| 2186 | − | |
| 2187 | − | |
| 2188 | 2264 | /* do macro substitution of macro_str and add result to |
| 2189 | 2265 | (tok_str,tok_len). If macro_str is NULL, then input stream token is |
| 2190 | 2266 | substituted. 'nested_list' is the list of all macros we got inside |
| 2191 | 2267 | to avoid recursing. */ |
| 2192 | −void macro_subst(int **tok_str, int *tok_len, | |
| 2268 | +void macro_subst(TokenString *tok_str, | |
| 2193 | 2269 | Sym **nested_list, int *macro_str) |
| 2194 | 2270 | { |
| 2195 | 2271 | Sym *s, *args, *sa, *sa1; |
| 2196 | − int *str, parlevel, len, *mstr, t, *saved_macro_ptr; | |
| 2272 | + int parlevel, *mstr, t, *saved_macro_ptr; | |
| 2197 | 2273 | int mstr_allocated, *macro_str1; |
| 2198 | 2274 | CValue cval; |
| 2199 | − | |
| 2275 | + TokenString str; | |
| 2276 | + | |
| 2200 | 2277 | saved_macro_ptr = macro_ptr; |
| 2201 | 2278 | macro_ptr = macro_str; |
| 2202 | 2279 | macro_str1 = NULL; |
| @@ -2213,16 +2290,16 @@ void macro_subst(int **tok_str, int *tok_len, · 10 unmodified lines | ||
| 2213 | 2290 | /* special macros */ |
| 2214 | 2291 | if (tok == TOK___LINE__) { |
| 2215 | 2292 | cval.i = file->line_num; |
| 2216 | − tok_add2(tok_str, tok_len, TOK_CINT, &cval); | |
| 2293 | + tok_str_add2(tok_str, TOK_CINT, &cval); | |
| 2217 | 2294 | } else if (tok == TOK___FILE__) { |
| 2218 | 2295 | cval.ts = tok_alloc(file->filename, 0); |
| 2219 | − tok_add2(tok_str, tok_len, TOK_STR, &cval); | |
| 2296 | + tok_str_add2(tok_str, TOK_STR, &cval); | |
| 2220 | 2297 | } else if (tok == TOK___DATE__) { |
| 2221 | 2298 | cval.ts = tok_alloc("Jan 1 1970", 0); |
| 2222 | − tok_add2(tok_str, tok_len, TOK_STR, &cval); | |
| 2299 | + tok_str_add2(tok_str, TOK_STR, &cval); | |
| 2223 | 2300 | } else if (tok == TOK___TIME__) { |
| 2224 | 2301 | cval.ts = tok_alloc("00:00:00", 0); |
| 2225 | − tok_add2(tok_str, tok_len, TOK_STR, &cval); | |
| 2302 | + tok_str_add2(tok_str, TOK_STR, &cval); | |
| 2226 | 2303 | } else if ((s = sym_find1(&define_stack, tok)) != NULL) { |
| 2227 | 2304 | /* if symbol is a macro, prepare substitution */ |
| 2228 | 2305 | /* if nested substitution, do nothing */ |
| @@ -2256,8 +2333,7 @@ void macro_subst(int **tok_str, int *tok_len, · 27 unmodified lines | ||
| 2256 | 2333 | if (!sa) |
| 2257 | 2334 | error("macro '%s' used with too many args", |
| 2258 | 2335 | get_tok_str(s->v, 0)); |
| 2259 | − len = 0; | |
| 2260 | − str = NULL; | |
| 2336 | + tok_str_new(&str); | |
| 2261 | 2337 | parlevel = 0; |
| 2262 | 2338 | while ((parlevel > 0 || |
| 2263 | 2339 | (tok != ')' && |
| @@ -2268,11 +2344,11 @@ void macro_subst(int **tok_str, int *tok_len, · 4 unmodified lines | ||
| 2268 | 2344 | parlevel++; |
| 2269 | 2345 | else if (tok == ')') |
| 2270 | 2346 | parlevel--; |
| 2271 | − tok_add2(&str, &len, tok, &tokc); | |
| 2347 | + tok_str_add2(&str, tok, &tokc); | |
| 2272 | 2348 | next_nomacro(); |
| 2273 | 2349 | } |
| 2274 | − tok_add(&str, &len, 0); | |
| 2275 | − sym_push2(&args, sa->v & ~SYM_FIELD, 0, (int)str); | |
| 2350 | + tok_str_add(&str, 0); | |
| 2351 | + sym_push2(&args, sa->v & ~SYM_FIELD, 0, (int)str.str); | |
| 2276 | 2352 | if (tok == ')') |
| 2277 | 2353 | break; |
| 2278 | 2354 | if (tok != ',') |
| @@ -2297,7 +2373,7 @@ void macro_subst(int **tok_str, int *tok_len, · 18 unmodified lines | ||
| 2297 | 2373 | mstr_allocated = 1; |
| 2298 | 2374 | } |
| 2299 | 2375 | sym_push2(nested_list, s->v, 0, 0); |
| 2300 | − macro_subst(tok_str, tok_len, nested_list, mstr); | |
| 2376 | + macro_subst(tok_str, nested_list, mstr); | |
| 2301 | 2377 | /* pop nested defined symbol */ |
| 2302 | 2378 | sa1 = *nested_list; |
| 2303 | 2379 | *nested_list = sa1->prev; |
| @@ -2309,7 +2385,7 @@ void macro_subst(int **tok_str, int *tok_len, · 5 unmodified lines | ||
| 2309 | 2385 | /* no need to add if reading input stream */ |
| 2310 | 2386 | if (!macro_str) |
| 2311 | 2387 | return; |
| 2312 | − tok_add2(tok_str, tok_len, tok, &tokc); | |
| 2388 | + tok_str_add2(tok_str, tok, &tokc); | |
| 2313 | 2389 | } |
| 2314 | 2390 | /* only replace one macro while parsing input stream */ |
| 2315 | 2391 | if (!macro_str) |
| @@ -2323,8 +2399,8 @@ void macro_subst(int **tok_str, int *tok_len, · 7 unmodified lines | ||
| 2323 | 2399 | /* return next token with macro substitution */ |
| 2324 | 2400 | void next(void) |
| 2325 | 2401 | { |
| 2326 | − int len, *ptr; | |
| 2327 | 2402 | Sym *nested_list; |
| 2403 | + TokenString str; | |
| 2328 | 2404 | |
| 2329 | 2405 | /* special 'ungettok' case for label parsing */ |
| 2330 | 2406 | if (tok1) { |
| @@ -2334,15 +2410,16 @@ void next(void) · 3 unmodified lines | ||
| 2334 | 2410 | } else { |
| 2335 | 2411 | redo: |
| 2336 | 2412 | if (!macro_ptr) { |
| 2337 | − /* if not reading from macro substituted string, then try to substitute */ | |
| 2338 | − len = 0; | |
| 2339 | − ptr = NULL; | |
| 2413 | + /* if not reading from macro substituted string, then try | |
| 2414 | + to substitute */ | |
| 2415 | + /* XXX: optimize non macro case */ | |
| 2416 | + tok_str_new(&str); | |
| 2340 | 2417 | nested_list = NULL; |
| 2341 | − macro_subst(&ptr, &len, &nested_list, NULL); | |
| 2342 | − if (ptr) { | |
| 2343 | − tok_add(&ptr, &len, 0); | |
| 2344 | − macro_ptr = ptr; | |
| 2345 | − macro_ptr_allocated = ptr; | |
| 2418 | + macro_subst(&str, &nested_list, NULL); | |
| 2419 | + if (str.str) { | |
| 2420 | + tok_str_add(&str, 0); | |
| 2421 | + macro_ptr = str.str; | |
| 2422 | + macro_ptr_allocated = str.str; | |
| 2346 | 2423 | goto redo; |
| 2347 | 2424 | } |
| 2348 | 2425 | if (tok == 0) |
| @@ -4673,15 +4750,16 @@ void unary(void) · 2324 unmodified lines | ||
| 4673 | 4750 | sa = s->next; /* first parameter */ |
| 4674 | 4751 | #ifdef INVERT_FUNC_PARAMS |
| 4675 | 4752 | { |
| 4676 | − int *str, len, parlevel, *saved_macro_ptr; | |
| 4753 | + int parlevel; | |
| 4677 | 4754 | Sym *args, *s1; |
| 4678 | − | |
| 4755 | + ParseState saved_parse_state; | |
| 4756 | + TokenString str; | |
| 4757 | + | |
| 4679 | 4758 | /* read each argument and store it on a stack */ |
| 4680 | 4759 | /* XXX: merge it with macro args ? */ |
| 4681 | 4760 | args = NULL; |
| 4682 | 4761 | while (tok != ')') { |
| 4683 | − len = 0; | |
| 4684 | − str = NULL; | |
| 4762 | + tok_str_new(&str); | |
| 4685 | 4763 | parlevel = 0; |
| 4686 | 4764 | while ((parlevel > 0 || (tok != ')' && tok != ',')) && |
| 4687 | 4765 | tok != -1) { |
| @@ -4689,12 +4767,12 @@ void unary(void) · 1 unmodified line | ||
| 4689 | 4767 | parlevel++; |
| 4690 | 4768 | else if (tok == ')') |
| 4691 | 4769 | parlevel--; |
| 4692 | − tok_add2(&str, &len, tok, &tokc); | |
| 4770 | + tok_str_add_tok(&str); | |
| 4693 | 4771 | next(); |
| 4694 | 4772 | } |
| 4695 | − tok_add(&str, &len, -1); /* end of file added */ | |
| 4696 | − tok_add(&str, &len, 0); | |
| 4697 | − s1 = sym_push2(&args, 0, 0, (int)str); | |
| 4773 | + tok_str_add(&str, -1); /* end of file added */ | |
| 4774 | + tok_str_add(&str, 0); | |
| 4775 | + s1 = sym_push2(&args, 0, 0, (int)str.str); | |
| 4698 | 4776 | s1->next = sa; /* add reference to argument */ |
| 4699 | 4777 | if (sa) |
| 4700 | 4778 | sa = sa->next; |
| @@ -4706,7 +4784,7 @@ void unary(void) · 5 unmodified lines | ||
| 4706 | 4784 | expect(")"); |
| 4707 | 4785 | |
| 4708 | 4786 | /* now generate code in reverse order by reading the stack */ |
| 4709 | − saved_macro_ptr = macro_ptr; | |
| 4787 | + save_parse_state(&saved_parse_state); | |
| 4710 | 4788 | while (args) { |
| 4711 | 4789 | macro_ptr = (int *)args->c; |
| 4712 | 4790 | next(); |
| @@ -4719,9 +4797,7 @@ void unary(void) · 6 unmodified lines | ||
| 4719 | 4797 | free(args); |
| 4720 | 4798 | args = s1; |
| 4721 | 4799 | } |
| 4722 | − macro_ptr = saved_macro_ptr; | |
| 4723 | − /* restore token */ | |
| 4724 | − tok = ')'; | |
| 4800 | + restore_parse_state(&saved_parse_state); | |
| 4725 | 4801 | } |
| 4726 | 4802 | #endif |
| 4727 | 4803 | /* compute first implicit argument if a structure is returned */ |
| @@ -5520,8 +5596,10 @@ void decl_initializer(int t, int r, int c, int first, int size_only) · 792 unmodified lines | ||
| 5520 | 5596 | VT_LOCAL or VT_CONST). The allocated address in returned */ |
| 5521 | 5597 | int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) |
| 5522 | 5598 | { |
| 5523 | − int size, align, addr, tok1, data_offset; | |
| 5524 | − int *init_str, init_len, level, *saved_macro_ptr; | |
| 5599 | + int size, align, addr, data_offset; | |
| 5600 | + int level; | |
| 5601 | + ParseState saved_parse_state; | |
| 5602 | + TokenString init_str; | |
| 5525 | 5603 | Section *sec; |
| 5526 | 5604 | |
| 5527 | 5605 | size = type_size(t, &align); |
| @@ -5531,10 +5609,7 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 3 unmodified lines | ||
| 5531 | 5609 | (e.g. string pointers or ISOC99 compound |
| 5532 | 5610 | literals). It also simplifies local |
| 5533 | 5611 | initializers handling */ |
| 5534 | − init_len = 0; | |
| 5535 | − init_str = NULL; | |
| 5536 | − saved_macro_ptr = NULL; /* avoid warning */ | |
| 5537 | − tok1 = 0; | |
| 5612 | + tok_str_new(&init_str); | |
| 5538 | 5613 | if (size < 0) { |
| 5539 | 5614 | if (!has_init) |
| 5540 | 5615 | error("unknown type size"); |
| @@ -5543,7 +5618,7 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 2 unmodified lines | ||
| 5543 | 5618 | while (level > 0 || (tok != ',' && tok != ';')) { |
| 5544 | 5619 | if (tok < 0) |
| 5545 | 5620 | error("unexpected end of file in initializer"); |
| 5546 | − tok_add2(&init_str, &init_len, tok, &tokc); | |
| 5621 | + tok_str_add_tok(&init_str); | |
| 5547 | 5622 | if (tok == '{') |
| 5548 | 5623 | level++; |
| 5549 | 5624 | else if (tok == '}') { |
| @@ -5553,17 +5628,17 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 3 unmodified lines | ||
| 5553 | 5628 | } |
| 5554 | 5629 | next(); |
| 5555 | 5630 | } |
| 5556 | − tok1 = tok; | |
| 5557 | − tok_add(&init_str, &init_len, -1); | |
| 5558 | − tok_add(&init_str, &init_len, 0); | |
| 5631 | + tok_str_add(&init_str, -1); | |
| 5632 | + tok_str_add(&init_str, 0); | |
| 5559 | 5633 | |
| 5560 | 5634 | /* compute size */ |
| 5561 | − saved_macro_ptr = macro_ptr; | |
| 5562 | − macro_ptr = init_str; | |
| 5635 | + save_parse_state(&saved_parse_state); | |
| 5636 | + | |
| 5637 | + macro_ptr = init_str.str; | |
| 5563 | 5638 | next(); |
| 5564 | 5639 | decl_initializer(t, r, 0, 1, 1); |
| 5565 | 5640 | /* prepare second initializer parsing */ |
| 5566 | − macro_ptr = init_str; | |
| 5641 | + macro_ptr = init_str.str; | |
| 5567 | 5642 | next(); |
| 5568 | 5643 | |
| 5569 | 5644 | /* if still unknown size, error */ |
| @@ -5632,10 +5707,9 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 62 unmodified lines | ||
| 5632 | 5707 | if (has_init) { |
| 5633 | 5708 | decl_initializer(t, r, addr, 1, 0); |
| 5634 | 5709 | /* restore parse state if needed */ |
| 5635 | − if (init_str) { | |
| 5636 | − free(init_str); | |
| 5637 | − macro_ptr = saved_macro_ptr; | |
| 5638 | − tok = tok1; | |
| 5710 | + if (init_str.str) { | |
| 5711 | + free(init_str.str); | |
| 5712 | + restore_parse_state(&saved_parse_state); | |
| 5639 | 5713 | } |
| 5640 | 5714 | } |
| 5641 | 5715 | return addr; |
| @@ -5663,13 +5737,72 @@ void put_func_debug(int t) · 21 unmodified lines | ||
| 5663 | 5737 | last_line_num = 0; |
| 5664 | 5738 | } |
| 5665 | 5739 | |
| 5740 | +/* not finished : try to put some local vars in registers */ | |
| 5741 | +//#define CONFIG_REG_VARS | |
| 5742 | + | |
| 5743 | +#ifdef CONFIG_REG_VARS | |
| 5744 | +void add_var_ref(int t) | |
| 5745 | +{ | |
| 5746 | + printf("%s:%d: &%s\n", | |
| 5747 | + file->filename, file->line_num, | |
| 5748 | + get_tok_str(t, NULL)); | |
| 5749 | +} | |
| 5750 | + | |
| 5751 | +/* first pass on a function with heuristic to extract variable usage | |
| 5752 | + and pointer references to local variables for register allocation */ | |
| 5753 | +void analyse_function(void) | |
| 5754 | +{ | |
| 5755 | + int level, t; | |
| 5756 | + | |
| 5757 | + for(;;) { | |
| 5758 | + if (tok == -1) | |
| 5759 | + break; | |
| 5760 | + /* any symbol coming after '&' is considered as being a | |
| 5761 | + variable whose reference is taken. It is highly unaccurate | |
| 5762 | + but it is difficult to do better without a complete parse */ | |
| 5763 | + if (tok == '&') { | |
| 5764 | + next(); | |
| 5765 | + /* if '& number', then no need to examine next tokens */ | |
| 5766 | + if (tok == TOK_CINT || | |
| 5767 | + tok == TOK_CUINT || | |
| 5768 | + tok == TOK_CLLONG || | |
| 5769 | + tok == TOK_CULLONG) { | |
| 5770 | + continue; | |
| 5771 | + } else if (tok >= TOK_UIDENT) { | |
| 5772 | + /* if '& ident [' or '& ident ->', then ident address | |
| 5773 | + is not needed */ | |
| 5774 | + t = tok; | |
| 5775 | + next(); | |
| 5776 | + if (tok != '[' && tok != TOK_ARROW) | |
| 5777 | + add_var_ref(t); | |
| 5778 | + } else { | |
| 5779 | + level = 0; | |
| 5780 | + while (tok != '}' && tok != ';' && | |
| 5781 | + !((tok == ',' || tok == ')') && level == 0)) { | |
| 5782 | + if (tok >= TOK_UIDENT) { | |
| 5783 | + add_var_ref(tok); | |
| 5784 | + } else if (tok == '(') { | |
| 5785 | + level++; | |
| 5786 | + } else if (tok == ')') { | |
| 5787 | + level--; | |
| 5788 | + } | |
| 5789 | + next(); | |
| 5790 | + } | |
| 5791 | + } | |
| 5792 | + } else { | |
| 5793 | + next(); | |
| 5794 | + } | |
| 5795 | + } | |
| 5796 | +} | |
| 5797 | +#endif | |
| 5798 | + | |
| 5666 | 5799 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ |
| 5667 | 5800 | void decl(int l) |
| 5668 | 5801 | { |
| 5669 | 5802 | int t, b, v, addr, has_init, r; |
| 5670 | 5803 | Sym *sym; |
| 5671 | 5804 | AttributeDef ad; |
| 5672 | − | |
| 5805 | + | |
| 5673 | 5806 | while (1) { |
| 5674 | 5807 | if (!parse_btype(&b, &ad)) { |
| 5675 | 5808 | /* skip redundant ';' */ |
| @@ -5701,10 +5834,48 @@ void decl(int l) · 25 unmodified lines | ||
| 5701 | 5834 | } |
| 5702 | 5835 | #endif |
| 5703 | 5836 | if (tok == '{') { |
| 5837 | +#ifdef CONFIG_REG_VARS | |
| 5838 | + TokenString func_str; | |
| 5839 | + ParseState saved_parse_state; | |
| 5840 | + int block_level; | |
| 5841 | +#endif | |
| 5842 | + | |
| 5704 | 5843 | if (l == VT_LOCAL) |
| 5705 | 5844 | error("cannot use local functions"); |
| 5706 | 5845 | if (!(t & VT_FUNC)) |
| 5707 | 5846 | expect("function definition"); |
| 5847 | + | |
| 5848 | +#ifdef CONFIG_REG_VARS | |
| 5849 | + /* parse all function code and record it */ | |
| 5850 | + | |
| 5851 | + tok_str_new(&func_str); | |
| 5852 | + | |
| 5853 | + block_level = 0; | |
| 5854 | + for(;;) { | |
| 5855 | + int t; | |
| 5856 | + if (tok == -1) | |
| 5857 | + error("unexpected end of file"); | |
| 5858 | + tok_str_add_tok(&func_str); | |
| 5859 | + t = tok; | |
| 5860 | + next(); | |
| 5861 | + if (t == '{') { | |
| 5862 | + block_level++; | |
| 5863 | + } else if (t == '}') { | |
| 5864 | + block_level--; | |
| 5865 | + if (block_level == 0) | |
| 5866 | + break; | |
| 5867 | + } | |
| 5868 | + } | |
| 5869 | + tok_str_add(&func_str, -1); | |
| 5870 | + tok_str_add(&func_str, 0); | |
| 5871 | + | |
| 5872 | + save_parse_state(&saved_parse_state); | |
| 5873 | + | |
| 5874 | + macro_ptr = func_str.str; | |
| 5875 | + next(); | |
| 5876 | + analyse_function(); | |
| 5877 | +#endif | |
| 5878 | + | |
| 5708 | 5879 | /* compute text section */ |
| 5709 | 5880 | cur_text_section = ad.section; |
| 5710 | 5881 | if (!cur_text_section) |
| @@ -5728,6 +5899,10 @@ void decl(int l) · 17 unmodified lines | ||
| 5728 | 5899 | gfunc_prolog(t); |
| 5729 | 5900 | loc = 0; |
| 5730 | 5901 | rsym = 0; |
| 5902 | +#ifdef CONFIG_REG_VARS | |
| 5903 | + macro_ptr = func_str.str; | |
| 5904 | + next(); | |
| 5905 | +#endif | |
| 5731 | 5906 | block(NULL, NULL, NULL, NULL, 0); |
| 5732 | 5907 | gsym(rsym); |
| 5733 | 5908 | gfunc_epilog(); |
| @@ -5741,6 +5916,11 @@ void decl(int l) · 7 unmodified lines | ||
| 5741 | 5916 | funcname = ""; /* for safety */ |
| 5742 | 5917 | func_vt = VT_VOID; /* for safety */ |
| 5743 | 5918 | ind = 0; /* for safety */ |
| 5919 | + | |
| 5920 | +#ifdef CONFIG_REG_VARS | |
| 5921 | + free(func_str.str); | |
| 5922 | + restore_parse_state(&saved_parse_state); | |
| 5923 | +#endif | |
| 5744 | 5924 | break; |
| 5745 | 5925 | } else { |
| 5746 | 5926 | if (b & VT_TYPEDEF) { |
| @@ -6450,8 +6630,7 @@ TCCState *tcc_new(void) · 703 unmodified lines | ||
| 6450 | 6630 | /* default include paths */ |
| 6451 | 6631 | nb_include_paths = 0; |
| 6452 | 6632 | tcc_add_include_path(s, "/usr/include"); |
| 6453 | − tcc_add_include_path(s, "/usr/lib/tcc"); | |
| 6454 | − tcc_add_include_path(s, "/usr/local/lib/tcc"); | |
| 6633 | + tcc_add_include_path(s, CONFIG_TCC_PREFIX "/lib/tcc/include"); | |
| 6455 | 6634 | |
| 6456 | 6635 | /* add all tokens */ |
| 6457 | 6636 | tok_ident = TOK_IDENT; |
| @@ -6502,7 +6681,7 @@ int tcc_add_include_path(TCCState *s, const char *pathname) · 44 unmodified lines | ||
| 6502 | 6681 | |
| 6503 | 6682 | void help(void) |
| 6504 | 6683 | { |
| 6505 | − printf("tcc version 0.9.7 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n" | |
| 6684 | + printf("tcc version 0.9.8 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n" | |
| 6506 | 6685 | "usage: tcc [-Idir] [-Dsym[=val]] [-Usym] [-llib] [-g] [-b]\n" |
| 6507 | 6686 | " [-i infile] infile [infile_args...]\n" |
| 6508 | 6687 | "\n" |