History

correct line numbers when reparsing - added explicit TokenString structure - better logic for search path

bellard committed 24 years ago

Commit 4e5a8521 parent dd35b8c1 file · +287 −108Browse files

Unified · Split

tcc.c+287−108
@@ -57,6 +57,10 @@ · 56 unmodified lines
57#define CONFIG_TCC_BCHECK /* enable bound checking code */57#define CONFIG_TCC_BCHECK /* enable bound checking code */
58#endif58#endif
59 59
60#ifndef CONFIG_TCC_PREFIX
61#define CONFIG_TCC_PREFIX "/usr/local"
62#endif
63
60/* amount of virtual memory associated to a section (currently, we do64/* amount of virtual memory associated to a section (currently, we do
61 not realloc them) */65 not realloc them) */
62#define SECTION_VSIZE (1024 * 1024)66#define SECTION_VSIZE (1024 * 1024)
@@ -184,6 +188,22 @@ typedef struct BufferedFile { · 121 unmodified lines
184#define CH_EOB 0 /* end of buffer or '\0' char in file */188#define CH_EOB 0 /* end of buffer or '\0' char in file */
185#define CH_EOF (-1) /* end of file */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) */
193typedef struct ParseState {
194 int *macro_ptr;
195 int line_num;
196 int tok;
197 CValue tokc;
198} ParseState;
199
200/* used to record tokens */
201typedef struct TokenString {
202 int *str;
203 int len;
204 int last_line_num;
205} TokenString;
206
187/* parser */207/* parser */
188struct BufferedFile *file;208struct BufferedFile *file;
189int ch, ch1, tok, tok1;209int ch, ch1, tok, tok1;
@@ -266,6 +286,9 @@ struct TCCState { · 76 unmodified lines
266 char/short stored in integer registers) */286 char/short stored in integer registers) */
267#define VT_MUSTBOUND 0x0800 /* bound checking must be done before287#define VT_MUSTBOUND 0x0800 /* bound checking must be done before
268 dereferencing value */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/* types */293/* types */
271#define VT_STRUCT_SHIFT 16 /* structure/enum name shift (16 bits left) */294#define VT_STRUCT_SHIFT 16 /* structure/enum name shift (16 bits left) */
@@ -274,7 +297,7 @@ struct TCCState { · 2 unmodified lines
274#define VT_BYTE 1 /* signed byte type */297#define VT_BYTE 1 /* signed byte type */
275#define VT_SHORT 2 /* short type */298#define VT_SHORT 2 /* short type */
276#define VT_VOID 3 /* void type */299#define VT_VOID 3 /* void type */
277#define VT_PTR 4 /* pointer increment */300#define VT_PTR 4 /* pointer */
278#define VT_ENUM 5 /* enum definition */301#define VT_ENUM 5 /* enum definition */
279#define VT_FUNC 6 /* function type */302#define VT_FUNC 6 /* function type */
280#define VT_STRUCT 7 /* struct/union definition */303#define VT_STRUCT 7 /* struct/union definition */
@@ -325,9 +348,10 @@ struct TCCState { · 44 unmodified lines
325#define TOK_CCHAR 0xb4 /* char constant in tokc */348#define TOK_CCHAR 0xb4 /* char constant in tokc */
326#define TOK_STR 0xb5 /* pointer to string in tokc */349#define TOK_STR 0xb5 /* pointer to string in tokc */
327#define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */350#define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
328#define TOK_LCHAR 0xb7351#define TOK_LCHAR 0xb7
329#define TOK_LSTR 0xb8352#define TOK_LSTR 0xb8
330#define TOK_CFLOAT 0xb9 /* float constant */353#define TOK_CFLOAT 0xb9 /* float constant */
354#define TOK_LINENUM 0xba /* line number info */
331#define TOK_CDOUBLE 0xc0 /* double constant */355#define TOK_CDOUBLE 0xc0 /* double constant */
332#define TOK_CLDOUBLE 0xc1 /* long double constant */356#define TOK_CLDOUBLE 0xc1 /* long double constant */
333#define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */357#define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
@@ -504,7 +528,7 @@ void vswap(void); · 170 unmodified lines
504void vdup(void);528void vdup(void);
505int get_reg(int rc);529int get_reg(int rc);
506 530
507void macro_subst(int **tok_str, int *tok_len, 531void macro_subst(TokenString *tok_str,
508 Sym **nested_list, int *macro_str);532 Sym **nested_list, int *macro_str);
509int save_reg_forced(int r);533int save_reg_forced(int r);
510void gen_op(int op);534void 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' */
1304void 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' */
1313void 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/* return the number of additionnal 'ints' necessary to store the1321/* return the number of additionnal 'ints' necessary to store the
1274 token */1322 token */
1275static inline int tok_ext_size(int t)1323static inline int tok_ext_size(int t)
@@ -1283,6 +1331,7 @@ static inline int tok_ext_size(int t) · 7 unmodified lines
1283 case TOK_STR:1331 case TOK_STR:
1284 case TOK_LSTR:1332 case TOK_LSTR:
1285 case TOK_CFLOAT:1333 case TOK_CFLOAT:
1334 case TOK_LINENUM:
1286 return 1;1335 return 1;
1287 case TOK_CDOUBLE:1336 case TOK_CDOUBLE:
1288 case TOK_CLLONG: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
1298void tok_add(int **tok_str, int *tok_len, int t)1347/* token string handling */
1348
1349static inline void tok_str_new(TokenString *s)
1350{
1351 s->str = NULL;
1352 s->len = 0;
1353 s->last_line_num = -1;
1354}
1355
1356static void tok_str_add(TokenString *s, int t)
1299{1357{
1300 int len, *str;1358 int len, *str;
1301 len = *tok_len;1359
1302 str = *tok_str;1360 len = s->len;
1361 str = s->str;
1303 if ((len & 63) == 0) {1362 if ((len & 63) == 0) {
1304 str = realloc(str, (len + 64) * sizeof(int));1363 str = realloc(str, (len + 64) * sizeof(int));
1305 if (!str)1364 if (!str)
1306 return;1365 return;
1307 *tok_str = str;1366 s->str = str;
1308 }1367 }
1309 str[len++] = t;1368 str[len++] = t;
1310 *tok_len = len;1369 s->len = len;
1311}1370}
1312 1371
1313void tok_add2(int **tok_str, int *tok_len, int t, CValue *cv)1372static void tok_str_add2(TokenString *s, int t, CValue *cv)
1314{1373{
1315 int n, i;1374 int n, i;
1316 1375 tok_str_add(s, t);
1317 tok_add(tok_str, tok_len, t);
1318 n = tok_ext_size(t);1376 n = tok_ext_size(t);
1319 for(i=0;i<n;i++)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' */
1382static 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/* get a token from an integer array and increment pointer accordingly */1395/* get a token from an integer array and increment pointer accordingly */
1324int tok_get(int **tok_str, CValue *cv)1396static int tok_get(int **tok_str, CValue *cv)
1325{1397{
1326 int *p, t, n, i;1398 int *p, t, n, i;
1327 1399
@@ -1337,10 +1409,10 @@ int tok_get(int **tok_str, CValue *cv) · 9 unmodified lines
1337/* eval an expression for #if/#elif */1409/* eval an expression for #if/#elif */
1338int expr_preprocess(void)1410int expr_preprocess(void)
1339{1411{
1340 int *str, len, c, t;1412 int c, t;
1413 TokenString str;
1341 1414
1342 str = NULL;1415 tok_str_new(&str);
1343 len = 0;
1344 while (1) {1416 while (1) {
1345 skip_spaces();1417 skip_spaces();
1346 if (ch == '\n')1418 if (ch == '\n')
@@ -1361,16 +1433,16 @@ int expr_preprocess(void) · 14 unmodified lines
1361 tok = TOK_CINT;1433 tok = TOK_CINT;
1362 tokc.i = 0;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 */1438 tok_str_add(&str, -1); /* simulate end of file */
1367 tok_add(&str, &len, 0);1439 tok_str_add(&str, 0);
1368 /* now evaluate C constant expression */1440 /* now evaluate C constant expression */
1369 macro_ptr = str;1441 macro_ptr = str.str;
1370 next();1442 next();
1371 c = expr_const();1443 c = expr_const();
1372 macro_ptr = NULL;1444 macro_ptr = NULL;
1373 free(str);1445 free(str.str);
1374 return c != 0;1446 return c != 0;
1375}1447}
1376 1448
@@ -1394,8 +1466,9 @@ void tok_print(int *str) · 17 unmodified lines
1394void parse_define(void)1466void parse_define(void)
1395{1467{
1396 Sym *s, *first, **ps;1468 Sym *s, *first, **ps;
1397 int v, t, *str, len;1469 int v, t;
1398 1470 TokenString str;
1471
1399 v = tok;1472 v = tok;
1400 /* XXX: should check if same macro (ANSI) */1473 /* XXX: should check if same macro (ANSI) */
1401 first = NULL;1474 first = NULL;
@@ -1418,21 +1491,20 @@ void parse_define(void) · 16 unmodified lines
1418 }1491 }
1419 t = MACRO_FUNC;1492 t = MACRO_FUNC;
1420 }1493 }
1421 str = NULL;1494 tok_str_new(&str);
1422 len = 0;
1423 while (1) {1495 while (1) {
1424 skip_spaces();1496 skip_spaces();
1425 if (ch == '\n' || ch == -1)1497 if (ch == '\n' || ch == -1)
1426 break;1498 break;
1427 next_nomacro();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#ifdef PP_DEBUG1503#ifdef PP_DEBUG
1432 printf("define %s %d: ", get_tok_str(v, NULL), t);1504 printf("define %s %d: ", get_tok_str(v, NULL), t);
1433 tok_print(str);1505 tok_print(str);
1434#endif1506#endif
1435 s = sym_push1(&define_stack, v, t, (int)str);1507 s = sym_push1(&define_stack, v, t, (int)str.str);
1436 s->next = first;1508 s->next = first;
1437}1509}
1438 1510
@@ -1510,7 +1582,7 @@ void preprocess(void) · 71 unmodified lines
1510 if (f)1582 if (f)
1511 goto found;1583 goto found;
1512 }1584 }
1513 error("include file '%s' not found", buf1);1585 error("include file '%s' not found", buf);
1514 f = NULL;1586 f = NULL;
1515 found:1587 found:
1516 /* push current file in stack */1588 /* push current file in stack */
@@ -2065,9 +2137,15 @@ void next_nomacro1(void) · 548 unmodified lines
2065void next_nomacro()2137void next_nomacro()
2066{2138{
2067 if (macro_ptr) {2139 if (macro_ptr) {
2140 redo:
2068 tok = *macro_ptr;2141 tok = *macro_ptr;
2069 if (tok)2142 if (tok) {
2070 tok = tok_get(&macro_ptr, &tokc);2143 tok = tok_get(&macro_ptr, &tokc);
2144 if (tok == TOK_LINENUM) {
2145 file->line_num = tokc.i;
2146 goto redo;
2147 }
2148 }
2071 } else {2149 } else {
2072 next_nomacro1();2150 next_nomacro1();
2073 }2151 }
@@ -2076,13 +2154,13 @@ void next_nomacro() · 2 unmodified lines
2076/* substitute args in macro_str and return allocated string */2154/* substitute args in macro_str and return allocated string */
2077int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)2155int *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 Sym *s;2158 Sym *s;
2081 TokenSym *ts;2159 TokenSym *ts;
2082 CValue cval;2160 CValue cval;
2083 2161 TokenString str;
2084 str = NULL;2162
2085 len = 0;2163 tok_str_new(&str);
2086 last_tok = 0;2164 last_tok = 0;
2087 while(1) {2165 while(1) {
2088 t = tok_get(&macro_str, &cval);2166 t = tok_get(&macro_str, &cval);
@@ -2111,9 +2189,9 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 22 unmodified lines
2111 /* add string */2189 /* add string */
2112 ts = tok_alloc(token_buf, 0);2190 ts = tok_alloc(token_buf, 0);
2113 cval.ts = ts;2191 cval.ts = ts;
2114 tok_add2(&str, &len, TOK_STR, &cval);2192 tok_str_add2(&str, TOK_STR, &cval);
2115 } else {2193 } else {
2116 tok_add2(&str, &len, t, &cval);2194 tok_str_add2(&str, t, &cval);
2117 }2195 }
2118 } else if (t >= TOK_IDENT) {2196 } else if (t >= TOK_IDENT) {
2119 s = sym_find2(args, t);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 /* if '##' is present before or after , no arg substitution */2200 /* if '##' is present before or after , no arg substitution */
2123 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {2201 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2124 while (*st)2202 while (*st)
2125 tok_add(&str, &len, *st++);2203 tok_str_add(&str, *st++);
2126 } else {2204 } else {
2127 macro_subst(&str, &len, nested_list, st);2205 macro_subst(&str, nested_list, st);
2128 }2206 }
2129 } else {2207 } else {
2130 tok_add(&str, &len, t);2208 tok_str_add(&str, t);
2131 }2209 }
2132 } else {2210 } else {
2133 tok_add2(&str, &len, t, &cval);2211 tok_str_add2(&str, t, &cval);
2134 }2212 }
2135 last_tok = t;2213 last_tok = t;
2136 }2214 }
2137 tok_add(&str, &len, 0);2215 tok_str_add(&str, 0);
2138 return str;2216 return str.str;
2139}2217}
2140 2218
2141/* handle the '##' operator */2219/* handle the '##' operator */
2142int *macro_twosharps(int *macro_str)2220int *macro_twosharps(int *macro_str)
2143{2221{
2144 TokenSym *ts;2222 TokenSym *ts;
2145 int *macro_str1, macro_str1_len, *macro_ptr1;2223 int *macro_ptr1;
2146 int t;2224 int t;
2147 char *p;2225 char *p;
2148 CValue cval;2226 CValue cval;
2149 2227 TokenString macro_str1;
2150 macro_str1 = NULL;2228
2151 macro_str1_len = 0;2229 tok_str_new(&macro_str1);
2152 tok = 0;2230 tok = 0;
2153 while (1) {2231 while (1) {
2154 next_nomacro();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(&macro_str1, &macro_str1_len, tok, &tokc);2258 tok_str_add2(&macro_str1, tok, &tokc);
2181 }2259 }
2182 tok_add(&macro_str1, &macro_str1_len, 0);2260 tok_str_add(&macro_str1, 0);
2183 return macro_str1;2261 return macro_str1.str;
2184}2262}
2185 2263
2186
2187
2188/* do macro substitution of macro_str and add result to2264/* do macro substitution of macro_str and add result to
2189 (tok_str,tok_len). If macro_str is NULL, then input stream token is2265 (tok_str,tok_len). If macro_str is NULL, then input stream token is
2190 substituted. 'nested_list' is the list of all macros we got inside2266 substituted. 'nested_list' is the list of all macros we got inside
2191 to avoid recursing. */2267 to avoid recursing. */
2192void macro_subst(int **tok_str, int *tok_len, 2268void macro_subst(TokenString *tok_str,
2193 Sym **nested_list, int *macro_str)2269 Sym **nested_list, int *macro_str)
2194{2270{
2195 Sym *s, *args, *sa, *sa1;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 int mstr_allocated, *macro_str1;2273 int mstr_allocated, *macro_str1;
2198 CValue cval;2274 CValue cval;
2199 2275 TokenString str;
2276
2200 saved_macro_ptr = macro_ptr;2277 saved_macro_ptr = macro_ptr;
2201 macro_ptr = macro_str;2278 macro_ptr = macro_str;
2202 macro_str1 = NULL;2279 macro_str1 = NULL;
@@ -2213,16 +2290,16 @@ void macro_subst(int **tok_str, int *tok_len, · 10 unmodified lines
2213 /* special macros */2290 /* special macros */
2214 if (tok == TOK___LINE__) {2291 if (tok == TOK___LINE__) {
2215 cval.i = file->line_num;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 } else if (tok == TOK___FILE__) {2294 } else if (tok == TOK___FILE__) {
2218 cval.ts = tok_alloc(file->filename, 0);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 } else if (tok == TOK___DATE__) {2297 } else if (tok == TOK___DATE__) {
2221 cval.ts = tok_alloc("Jan 1 1970", 0);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 } else if (tok == TOK___TIME__) {2300 } else if (tok == TOK___TIME__) {
2224 cval.ts = tok_alloc("00:00:00", 0);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 } else if ((s = sym_find1(&define_stack, tok)) != NULL) {2303 } else if ((s = sym_find1(&define_stack, tok)) != NULL) {
2227 /* if symbol is a macro, prepare substitution */2304 /* if symbol is a macro, prepare substitution */
2228 /* if nested substitution, do nothing */2305 /* if nested substitution, do nothing */
@@ -2256,8 +2333,7 @@ void macro_subst(int **tok_str, int *tok_len, · 27 unmodified lines
2256 if (!sa)2333 if (!sa)
2257 error("macro '%s' used with too many args",2334 error("macro '%s' used with too many args",
2258 get_tok_str(s->v, 0));2335 get_tok_str(s->v, 0));
2259 len = 0;2336 tok_str_new(&str);
2260 str = NULL;
2261 parlevel = 0;2337 parlevel = 0;
2262 while ((parlevel > 0 || 2338 while ((parlevel > 0 ||
2263 (tok != ')' && 2339 (tok != ')' &&
@@ -2268,11 +2344,11 @@ void macro_subst(int **tok_str, int *tok_len, · 4 unmodified lines
2268 parlevel++;2344 parlevel++;
2269 else if (tok == ')')2345 else if (tok == ')')
2270 parlevel--;2346 parlevel--;
2271 tok_add2(&str, &len, tok, &tokc);2347 tok_str_add2(&str, tok, &tokc);
2272 next_nomacro();2348 next_nomacro();
2273 }2349 }
2274 tok_add(&str, &len, 0);2350 tok_str_add(&str, 0);
2275 sym_push2(&args, sa->v & ~SYM_FIELD, 0, (int)str);2351 sym_push2(&args, sa->v & ~SYM_FIELD, 0, (int)str.str);
2276 if (tok == ')')2352 if (tok == ')')
2277 break;2353 break;
2278 if (tok != ',')2354 if (tok != ',')
@@ -2297,7 +2373,7 @@ void macro_subst(int **tok_str, int *tok_len, · 18 unmodified lines
2297 mstr_allocated = 1;2373 mstr_allocated = 1;
2298 }2374 }
2299 sym_push2(nested_list, s->v, 0, 0);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 /* pop nested defined symbol */2377 /* pop nested defined symbol */
2302 sa1 = *nested_list;2378 sa1 = *nested_list;
2303 *nested_list = sa1->prev;2379 *nested_list = sa1->prev;
@@ -2309,7 +2385,7 @@ void macro_subst(int **tok_str, int *tok_len, · 5 unmodified lines
2309 /* no need to add if reading input stream */2385 /* no need to add if reading input stream */
2310 if (!macro_str)2386 if (!macro_str)
2311 return;2387 return;
2312 tok_add2(tok_str, tok_len, tok, &tokc);2388 tok_str_add2(tok_str, tok, &tokc);
2313 }2389 }
2314 /* only replace one macro while parsing input stream */2390 /* only replace one macro while parsing input stream */
2315 if (!macro_str)2391 if (!macro_str)
@@ -2323,8 +2399,8 @@ void macro_subst(int **tok_str, int *tok_len, · 7 unmodified lines
2323/* return next token with macro substitution */2399/* return next token with macro substitution */
2324void next(void)2400void next(void)
2325{2401{
2326 int len, *ptr;
2327 Sym *nested_list;2402 Sym *nested_list;
2403 TokenString str;
2328 2404
2329 /* special 'ungettok' case for label parsing */2405 /* special 'ungettok' case for label parsing */
2330 if (tok1) {2406 if (tok1) {
@@ -2334,15 +2410,16 @@ void next(void) · 3 unmodified lines
2334 } else {2410 } else {
2335 redo:2411 redo:
2336 if (!macro_ptr) {2412 if (!macro_ptr) {
2337 /* if not reading from macro substituted string, then try to substitute */2413 /* if not reading from macro substituted string, then try
2338 len = 0;2414 to substitute */
2339 ptr = NULL;2415 /* XXX: optimize non macro case */
2416 tok_str_new(&str);
2340 nested_list = NULL;2417 nested_list = NULL;
2341 macro_subst(&ptr, &len, &nested_list, NULL);2418 macro_subst(&str, &nested_list, NULL);
2342 if (ptr) {2419 if (str.str) {
2343 tok_add(&ptr, &len, 0);2420 tok_str_add(&str, 0);
2344 macro_ptr = ptr;2421 macro_ptr = str.str;
2345 macro_ptr_allocated = ptr;2422 macro_ptr_allocated = str.str;
2346 goto redo;2423 goto redo;
2347 }2424 }
2348 if (tok == 0)2425 if (tok == 0)
@@ -4673,15 +4750,16 @@ void unary(void) · 2324 unmodified lines
4673 sa = s->next; /* first parameter */4750 sa = s->next; /* first parameter */
4674#ifdef INVERT_FUNC_PARAMS4751#ifdef INVERT_FUNC_PARAMS
4675 {4752 {
4676 int *str, len, parlevel, *saved_macro_ptr;4753 int parlevel;
4677 Sym *args, *s1;4754 Sym *args, *s1;
4678 4755 ParseState saved_parse_state;
4756 TokenString str;
4757
4679 /* read each argument and store it on a stack */4758 /* read each argument and store it on a stack */
4680 /* XXX: merge it with macro args ? */4759 /* XXX: merge it with macro args ? */
4681 args = NULL;4760 args = NULL;
4682 while (tok != ')') {4761 while (tok != ')') {
4683 len = 0;4762 tok_str_new(&str);
4684 str = NULL;
4685 parlevel = 0;4763 parlevel = 0;
4686 while ((parlevel > 0 || (tok != ')' && tok != ',')) && 4764 while ((parlevel > 0 || (tok != ')' && tok != ',')) &&
4687 tok != -1) {4765 tok != -1) {
@@ -4689,12 +4767,12 @@ void unary(void) · 1 unmodified line
4689 parlevel++;4767 parlevel++;
4690 else if (tok == ')')4768 else if (tok == ')')
4691 parlevel--;4769 parlevel--;
4692 tok_add2(&str, &len, tok, &tokc);4770 tok_str_add_tok(&str);
4693 next();4771 next();
4694 }4772 }
4695 tok_add(&str, &len, -1); /* end of file added */4773 tok_str_add(&str, -1); /* end of file added */
4696 tok_add(&str, &len, 0);4774 tok_str_add(&str, 0);
4697 s1 = sym_push2(&args, 0, 0, (int)str);4775 s1 = sym_push2(&args, 0, 0, (int)str.str);
4698 s1->next = sa; /* add reference to argument */4776 s1->next = sa; /* add reference to argument */
4699 if (sa)4777 if (sa)
4700 sa = sa->next;4778 sa = sa->next;
@@ -4706,7 +4784,7 @@ void unary(void) · 5 unmodified lines
4706 expect(")");4784 expect(")");
4707 4785
4708 /* now generate code in reverse order by reading the stack */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 while (args) {4788 while (args) {
4711 macro_ptr = (int *)args->c;4789 macro_ptr = (int *)args->c;
4712 next();4790 next();
@@ -4719,9 +4797,7 @@ void unary(void) · 6 unmodified lines
4719 free(args);4797 free(args);
4720 args = s1;4798 args = s1;
4721 }4799 }
4722 macro_ptr = saved_macro_ptr;4800 restore_parse_state(&saved_parse_state);
4723 /* restore token */
4724 tok = ')';
4725 }4801 }
4726#endif4802#endif
4727 /* compute first implicit argument if a structure is returned */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 VT_LOCAL or VT_CONST). The allocated address in returned */5596 VT_LOCAL or VT_CONST). The allocated address in returned */
5521int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init)5597int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init)
5522{5598{
5523 int size, align, addr, tok1, data_offset;5599 int size, align, addr, data_offset;
5524 int *init_str, init_len, level, *saved_macro_ptr;5600 int level;
5601 ParseState saved_parse_state;
5602 TokenString init_str;
5525 Section *sec;5603 Section *sec;
5526 5604
5527 size = type_size(t, &align);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 (e.g. string pointers or ISOC99 compound5609 (e.g. string pointers or ISOC99 compound
5532 literals). It also simplifies local5610 literals). It also simplifies local
5533 initializers handling */5611 initializers handling */
5534 init_len = 0;5612 tok_str_new(&init_str);
5535 init_str = NULL;
5536 saved_macro_ptr = NULL; /* avoid warning */
5537 tok1 = 0;
5538 if (size < 0) {5613 if (size < 0) {
5539 if (!has_init) 5614 if (!has_init)
5540 error("unknown type size");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 while (level > 0 || (tok != ',' && tok != ';')) {5618 while (level > 0 || (tok != ',' && tok != ';')) {
5544 if (tok < 0)5619 if (tok < 0)
5545 error("unexpected end of file in initializer");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 if (tok == '{')5622 if (tok == '{')
5548 level++;5623 level++;
5549 else if (tok == '}') {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 next();5629 next();
5555 }5630 }
5556 tok1 = tok;5631 tok_str_add(&init_str, -1);
5557 tok_add(&init_str, &init_len, -1);5632 tok_str_add(&init_str, 0);
5558 tok_add(&init_str, &init_len, 0);
5559 5633
5560 /* compute size */5634 /* compute size */
5561 saved_macro_ptr = macro_ptr;5635 save_parse_state(&saved_parse_state);
5562 macro_ptr = init_str;5636
5637 macro_ptr = init_str.str;
5563 next();5638 next();
5564 decl_initializer(t, r, 0, 1, 1);5639 decl_initializer(t, r, 0, 1, 1);
5565 /* prepare second initializer parsing */5640 /* prepare second initializer parsing */
5566 macro_ptr = init_str;5641 macro_ptr = init_str.str;
5567 next();5642 next();
5568 5643
5569 /* if still unknown size, error */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 if (has_init) {5707 if (has_init) {
5633 decl_initializer(t, r, addr, 1, 0);5708 decl_initializer(t, r, addr, 1, 0);
5634 /* restore parse state if needed */5709 /* restore parse state if needed */
5635 if (init_str) {5710 if (init_str.str) {
5636 free(init_str);5711 free(init_str.str);
5637 macro_ptr = saved_macro_ptr;5712 restore_parse_state(&saved_parse_state);
5638 tok = tok1;
5639 }5713 }
5640 }5714 }
5641 return addr;5715 return addr;
@@ -5663,13 +5737,72 @@ void put_func_debug(int t) · 21 unmodified lines
5663 last_line_num = 0;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
5744void 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 */
5753void 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/* 'l' is VT_LOCAL or VT_CONST to define default storage type */5799/* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5667void decl(int l)5800void decl(int l)
5668{5801{
5669 int t, b, v, addr, has_init, r;5802 int t, b, v, addr, has_init, r;
5670 Sym *sym;5803 Sym *sym;
5671 AttributeDef ad;5804 AttributeDef ad;
5672 5805
5673 while (1) {5806 while (1) {
5674 if (!parse_btype(&b, &ad)) {5807 if (!parse_btype(&b, &ad)) {
5675 /* skip redundant ';' */5808 /* skip redundant ';' */
@@ -5701,10 +5834,48 @@ void decl(int l) · 25 unmodified lines
5701 }5834 }
5702#endif5835#endif
5703 if (tok == '{') {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 if (l == VT_LOCAL)5843 if (l == VT_LOCAL)
5705 error("cannot use local functions");5844 error("cannot use local functions");
5706 if (!(t & VT_FUNC))5845 if (!(t & VT_FUNC))
5707 expect("function definition");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 /* compute text section */5879 /* compute text section */
5709 cur_text_section = ad.section;5880 cur_text_section = ad.section;
5710 if (!cur_text_section)5881 if (!cur_text_section)
@@ -5728,6 +5899,10 @@ void decl(int l) · 17 unmodified lines
5728 gfunc_prolog(t);5899 gfunc_prolog(t);
5729 loc = 0;5900 loc = 0;
5730 rsym = 0;5901 rsym = 0;
5902#ifdef CONFIG_REG_VARS
5903 macro_ptr = func_str.str;
5904 next();
5905#endif
5731 block(NULL, NULL, NULL, NULL, 0);5906 block(NULL, NULL, NULL, NULL, 0);
5732 gsym(rsym);5907 gsym(rsym);
5733 gfunc_epilog();5908 gfunc_epilog();
@@ -5741,6 +5916,11 @@ void decl(int l) · 7 unmodified lines
5741 funcname = ""; /* for safety */5916 funcname = ""; /* for safety */
5742 func_vt = VT_VOID; /* for safety */5917 func_vt = VT_VOID; /* for safety */
5743 ind = 0; /* for safety */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 break;5924 break;
5745 } else {5925 } else {
5746 if (b & VT_TYPEDEF) {5926 if (b & VT_TYPEDEF) {
@@ -6450,8 +6630,7 @@ TCCState *tcc_new(void) · 703 unmodified lines
6450 /* default include paths */6630 /* default include paths */
6451 nb_include_paths = 0;6631 nb_include_paths = 0;
6452 tcc_add_include_path(s, "/usr/include");6632 tcc_add_include_path(s, "/usr/include");
6453 tcc_add_include_path(s, "/usr/lib/tcc");6633 tcc_add_include_path(s, CONFIG_TCC_PREFIX "/lib/tcc/include");
6454 tcc_add_include_path(s, "/usr/local/lib/tcc");
6455 6634
6456 /* add all tokens */6635 /* add all tokens */
6457 tok_ident = TOK_IDENT;6636 tok_ident = TOK_IDENT;
@@ -6502,7 +6681,7 @@ int tcc_add_include_path(TCCState *s, const char *pathname) · 44 unmodified lines
6502 6681
6503void help(void)6682void 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 "usage: tcc [-Idir] [-Dsym[=val]] [-Usym] [-llib] [-g] [-b]\n"6685 "usage: tcc [-Idir] [-Dsym[=val]] [-Usym] [-llib] [-g] [-b]\n"
6507 " [-i infile] infile [infile_args...]\n"6686 " [-i infile] infile [infile_args...]\n"
6508 "\n"6687 "\n"

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t