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
5757 #define CONFIG_TCC_BCHECK /* enable bound checking code */
5858 #endif
5959
60+#ifndef CONFIG_TCC_PREFIX
61+#define CONFIG_TCC_PREFIX "/usr/local"
62+#endif
63+
6064 /* amount of virtual memory associated to a section (currently, we do
6165 not realloc them) */
6266 #define SECTION_VSIZE (1024 * 1024)
@@ -184,6 +188,22 @@ typedef struct BufferedFile { · 121 unmodified lines
184188 #define CH_EOB 0 /* end of buffer or '\0' char in file */
185189 #define CH_EOF (-1) /* end of file */
186190
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+
187207 /* parser */
188208 struct BufferedFile *file;
189209 int ch, ch1, tok, tok1;
@@ -266,6 +286,9 @@ struct TCCState { · 76 unmodified lines
266286 char/short stored in integer registers) */
267287 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
268288 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 */
269292
270293 /* types */
271294 #define VT_STRUCT_SHIFT 16 /* structure/enum name shift (16 bits left) */
@@ -274,7 +297,7 @@ struct TCCState { · 2 unmodified lines
274297 #define VT_BYTE 1 /* signed byte type */
275298 #define VT_SHORT 2 /* short type */
276299 #define VT_VOID 3 /* void type */
277−#define VT_PTR 4 /* pointer increment */
300+#define VT_PTR 4 /* pointer */
278301 #define VT_ENUM 5 /* enum definition */
279302 #define VT_FUNC 6 /* function type */
280303 #define VT_STRUCT 7 /* struct/union definition */
@@ -325,9 +348,10 @@ struct TCCState { · 44 unmodified lines
325348 #define TOK_CCHAR 0xb4 /* char constant in tokc */
326349 #define TOK_STR 0xb5 /* pointer to string in tokc */
327350 #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
330353 #define TOK_CFLOAT 0xb9 /* float constant */
354+#define TOK_LINENUM 0xba /* line number info */
331355 #define TOK_CDOUBLE 0xc0 /* double constant */
332356 #define TOK_CLDOUBLE 0xc1 /* long double constant */
333357 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
@@ -504,7 +528,7 @@ void vswap(void); · 170 unmodified lines
504528 void vdup(void);
505529 int get_reg(int rc);
506530
507−void macro_subst(int **tok_str, int *tok_len,
531+void macro_subst(TokenString *tok_str,
508532 Sym **nested_list, int *macro_str);
509533 int save_reg_forced(int r);
510534 void gen_op(int op);
@@ -1270,6 +1294,30 @@ void preprocess_skip(void) · 759 unmodified lines
12701294 }
12711295 }
12721296
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+
12731321 /* return the number of additionnal 'ints' necessary to store the
12741322 token */
12751323 static inline int tok_ext_size(int t)
@@ -1283,6 +1331,7 @@ static inline int tok_ext_size(int t) · 7 unmodified lines
12831331 case TOK_STR:
12841332 case TOK_LSTR:
12851333 case TOK_CFLOAT:
1334+ case TOK_LINENUM:
12861335 return 1;
12871336 case TOK_CDOUBLE:
12881337 case TOK_CLLONG:
@@ -1295,33 +1344,56 @@ static inline int tok_ext_size(int t) · 6 unmodified lines
12951344 }
12961345 }
12971346
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)
12991357 {
13001358 int len, *str;
1301− len = *tok_len;
1302− str = *tok_str;
1359+
1360+ len = s->len;
1361+ str = s->str;
13031362 if ((len & 63) == 0) {
13041363 str = realloc(str, (len + 64) * sizeof(int));
13051364 if (!str)
13061365 return;
1307− *tok_str = str;
1366+ s->str = str;
13081367 }
13091368 str[len++] = t;
1310− *tok_len = len;
1369+ s->len = len;
13111370 }
13121371
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)
13141373 {
13151374 int n, i;
1316−
1317− tok_add(tok_str, tok_len, t);
1375+ tok_str_add(s, t);
13181376 n = tok_ext_size(t);
13191377 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);
13211393 }
13221394
13231395 /* 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)
13251397 {
13261398 int *p, t, n, i;
13271399
@@ -1337,10 +1409,10 @@ int tok_get(int **tok_str, CValue *cv) · 9 unmodified lines
13371409 /* eval an expression for #if/#elif */
13381410 int expr_preprocess(void)
13391411 {
1340− int *str, len, c, t;
1412+ int c, t;
1413+ TokenString str;
13411414
1342− str = NULL;
1343− len = 0;
1415+ tok_str_new(&str);
13441416 while (1) {
13451417 skip_spaces();
13461418 if (ch == '\n')
@@ -1361,16 +1433,16 @@ int expr_preprocess(void) · 14 unmodified lines
13611433 tok = TOK_CINT;
13621434 tokc.i = 0;
13631435 }
1364− tok_add2(&str, &len, tok, &tokc);
1436+ tok_str_add_tok(&str);
13651437 }
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);
13681440 /* now evaluate C constant expression */
1369− macro_ptr = str;
1441+ macro_ptr = str.str;
13701442 next();
13711443 c = expr_const();
13721444 macro_ptr = NULL;
1373− free(str);
1445+ free(str.str);
13741446 return c != 0;
13751447 }
13761448
@@ -1394,8 +1466,9 @@ void tok_print(int *str) · 17 unmodified lines
13941466 void parse_define(void)
13951467 {
13961468 Sym *s, *first, **ps;
1397− int v, t, *str, len;
1398−
1469+ int v, t;
1470+ TokenString str;
1471+
13991472 v = tok;
14001473 /* XXX: should check if same macro (ANSI) */
14011474 first = NULL;
@@ -1418,21 +1491,20 @@ void parse_define(void) · 16 unmodified lines
14181491 }
14191492 t = MACRO_FUNC;
14201493 }
1421− str = NULL;
1422− len = 0;
1494+ tok_str_new(&str);
14231495 while (1) {
14241496 skip_spaces();
14251497 if (ch == '\n' || ch == -1)
14261498 break;
14271499 next_nomacro();
1428− tok_add2(&str, &len, tok, &tokc);
1500+ tok_str_add2(&str, tok, &tokc);
14291501 }
1430− tok_add(&str, &len, 0);
1502+ tok_str_add(&str, 0);
14311503 #ifdef PP_DEBUG
14321504 printf("define %s %d: ", get_tok_str(v, NULL), t);
14331505 tok_print(str);
14341506 #endif
1435− s = sym_push1(&define_stack, v, t, (int)str);
1507+ s = sym_push1(&define_stack, v, t, (int)str.str);
14361508 s->next = first;
14371509 }
14381510
@@ -1510,7 +1582,7 @@ void preprocess(void) · 71 unmodified lines
15101582 if (f)
15111583 goto found;
15121584 }
1513− error("include file '%s' not found", buf1);
1585+ error("include file '%s' not found", buf);
15141586 f = NULL;
15151587 found:
15161588 /* push current file in stack */
@@ -2065,9 +2137,15 @@ void next_nomacro1(void) · 548 unmodified lines
20652137 void next_nomacro()
20662138 {
20672139 if (macro_ptr) {
2140+ redo:
20682141 tok = *macro_ptr;
2069− if (tok)
2142+ if (tok) {
20702143 tok = tok_get(&macro_ptr, &tokc);
2144+ if (tok == TOK_LINENUM) {
2145+ file->line_num = tokc.i;
2146+ goto redo;
2147+ }
2148+ }
20712149 } else {
20722150 next_nomacro1();
20732151 }
@@ -2076,13 +2154,13 @@ void next_nomacro() · 2 unmodified lines
20762154 /* substitute args in macro_str and return allocated string */
20772155 int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
20782156 {
2079− int *st, last_tok, t, notfirst, *str, len;
2157+ int *st, last_tok, t, notfirst;
20802158 Sym *s;
20812159 TokenSym *ts;
20822160 CValue cval;
2083−
2084− str = NULL;
2085− len = 0;
2161+ TokenString str;
2162+
2163+ tok_str_new(&str);
20862164 last_tok = 0;
20872165 while(1) {
20882166 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
21112189 /* add string */
21122190 ts = tok_alloc(token_buf, 0);
21132191 cval.ts = ts;
2114− tok_add2(&str, &len, TOK_STR, &cval);
2192+ tok_str_add2(&str, TOK_STR, &cval);
21152193 } else {
2116− tok_add2(&str, &len, t, &cval);
2194+ tok_str_add2(&str, t, &cval);
21172195 }
21182196 } else if (t >= TOK_IDENT) {
21192197 s = sym_find2(args, t);
@@ -2122,33 +2200,33 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 2 unmodified lines
21222200 /* if '##' is present before or after , no arg substitution */
21232201 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
21242202 while (*st)
2125− tok_add(&str, &len, *st++);
2203+ tok_str_add(&str, *st++);
21262204 } else {
2127− macro_subst(&str, &len, nested_list, st);
2205+ macro_subst(&str, nested_list, st);
21282206 }
21292207 } else {
2130− tok_add(&str, &len, t);
2208+ tok_str_add(&str, t);
21312209 }
21322210 } else {
2133− tok_add2(&str, &len, t, &cval);
2211+ tok_str_add2(&str, t, &cval);
21342212 }
21352213 last_tok = t;
21362214 }
2137− tok_add(&str, &len, 0);
2138− return str;
2215+ tok_str_add(&str, 0);
2216+ return str.str;
21392217 }
21402218
21412219 /* handle the '##' operator */
21422220 int *macro_twosharps(int *macro_str)
21432221 {
21442222 TokenSym *ts;
2145− int *macro_str1, macro_str1_len, *macro_ptr1;
2223+ int *macro_ptr1;
21462224 int t;
21472225 char *p;
21482226 CValue cval;
2149−
2150− macro_str1 = NULL;
2151− macro_str1_len = 0;
2227+ TokenString macro_str1;
2228+
2229+ tok_str_new(&macro_str1);
21522230 tok = 0;
21532231 while (1) {
21542232 next_nomacro();
@@ -2177,26 +2255,25 @@ int *macro_twosharps(int *macro_str) · 22 unmodified lines
21772255 }
21782256 }
21792257 }
2180− tok_add2(&macro_str1, &macro_str1_len, tok, &tokc);
2258+ tok_str_add2(&macro_str1, tok, &tokc);
21812259 }
2182− tok_add(&macro_str1, &macro_str1_len, 0);
2183− return macro_str1;
2260+ tok_str_add(&macro_str1, 0);
2261+ return macro_str1.str;
21842262 }
21852263
2186−
2187−
21882264 /* do macro substitution of macro_str and add result to
21892265 (tok_str,tok_len). If macro_str is NULL, then input stream token is
21902266 substituted. 'nested_list' is the list of all macros we got inside
21912267 to avoid recursing. */
2192−void macro_subst(int **tok_str, int *tok_len,
2268+void macro_subst(TokenString *tok_str,
21932269 Sym **nested_list, int *macro_str)
21942270 {
21952271 Sym *s, *args, *sa, *sa1;
2196− int *str, parlevel, len, *mstr, t, *saved_macro_ptr;
2272+ int parlevel, *mstr, t, *saved_macro_ptr;
21972273 int mstr_allocated, *macro_str1;
21982274 CValue cval;
2199−
2275+ TokenString str;
2276+
22002277 saved_macro_ptr = macro_ptr;
22012278 macro_ptr = macro_str;
22022279 macro_str1 = NULL;
@@ -2213,16 +2290,16 @@ void macro_subst(int **tok_str, int *tok_len, · 10 unmodified lines
22132290 /* special macros */
22142291 if (tok == TOK___LINE__) {
22152292 cval.i = file->line_num;
2216− tok_add2(tok_str, tok_len, TOK_CINT, &cval);
2293+ tok_str_add2(tok_str, TOK_CINT, &cval);
22172294 } else if (tok == TOK___FILE__) {
22182295 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);
22202297 } else if (tok == TOK___DATE__) {
22212298 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);
22232300 } else if (tok == TOK___TIME__) {
22242301 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);
22262303 } else if ((s = sym_find1(&define_stack, tok)) != NULL) {
22272304 /* if symbol is a macro, prepare substitution */
22282305 /* if nested substitution, do nothing */
@@ -2256,8 +2333,7 @@ void macro_subst(int **tok_str, int *tok_len, · 27 unmodified lines
22562333 if (!sa)
22572334 error("macro '%s' used with too many args",
22582335 get_tok_str(s->v, 0));
2259− len = 0;
2260− str = NULL;
2336+ tok_str_new(&str);
22612337 parlevel = 0;
22622338 while ((parlevel > 0 ||
22632339 (tok != ')' &&
@@ -2268,11 +2344,11 @@ void macro_subst(int **tok_str, int *tok_len, · 4 unmodified lines
22682344 parlevel++;
22692345 else if (tok == ')')
22702346 parlevel--;
2271− tok_add2(&str, &len, tok, &tokc);
2347+ tok_str_add2(&str, tok, &tokc);
22722348 next_nomacro();
22732349 }
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);
22762352 if (tok == ')')
22772353 break;
22782354 if (tok != ',')
@@ -2297,7 +2373,7 @@ void macro_subst(int **tok_str, int *tok_len, · 18 unmodified lines
22972373 mstr_allocated = 1;
22982374 }
22992375 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);
23012377 /* pop nested defined symbol */
23022378 sa1 = *nested_list;
23032379 *nested_list = sa1->prev;
@@ -2309,7 +2385,7 @@ void macro_subst(int **tok_str, int *tok_len, · 5 unmodified lines
23092385 /* no need to add if reading input stream */
23102386 if (!macro_str)
23112387 return;
2312− tok_add2(tok_str, tok_len, tok, &tokc);
2388+ tok_str_add2(tok_str, tok, &tokc);
23132389 }
23142390 /* only replace one macro while parsing input stream */
23152391 if (!macro_str)
@@ -2323,8 +2399,8 @@ void macro_subst(int **tok_str, int *tok_len, · 7 unmodified lines
23232399 /* return next token with macro substitution */
23242400 void next(void)
23252401 {
2326− int len, *ptr;
23272402 Sym *nested_list;
2403+ TokenString str;
23282404
23292405 /* special 'ungettok' case for label parsing */
23302406 if (tok1) {
@@ -2334,15 +2410,16 @@ void next(void) · 3 unmodified lines
23342410 } else {
23352411 redo:
23362412 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);
23402417 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;
23462423 goto redo;
23472424 }
23482425 if (tok == 0)
@@ -4673,15 +4750,16 @@ void unary(void) · 2324 unmodified lines
46734750 sa = s->next; /* first parameter */
46744751 #ifdef INVERT_FUNC_PARAMS
46754752 {
4676− int *str, len, parlevel, *saved_macro_ptr;
4753+ int parlevel;
46774754 Sym *args, *s1;
4678−
4755+ ParseState saved_parse_state;
4756+ TokenString str;
4757+
46794758 /* read each argument and store it on a stack */
46804759 /* XXX: merge it with macro args ? */
46814760 args = NULL;
46824761 while (tok != ')') {
4683− len = 0;
4684− str = NULL;
4762+ tok_str_new(&str);
46854763 parlevel = 0;
46864764 while ((parlevel > 0 || (tok != ')' && tok != ',')) &&
46874765 tok != -1) {
@@ -4689,12 +4767,12 @@ void unary(void) · 1 unmodified line
46894767 parlevel++;
46904768 else if (tok == ')')
46914769 parlevel--;
4692− tok_add2(&str, &len, tok, &tokc);
4770+ tok_str_add_tok(&str);
46934771 next();
46944772 }
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);
46984776 s1->next = sa; /* add reference to argument */
46994777 if (sa)
47004778 sa = sa->next;
@@ -4706,7 +4784,7 @@ void unary(void) · 5 unmodified lines
47064784 expect(")");
47074785
47084786 /* now generate code in reverse order by reading the stack */
4709− saved_macro_ptr = macro_ptr;
4787+ save_parse_state(&saved_parse_state);
47104788 while (args) {
47114789 macro_ptr = (int *)args->c;
47124790 next();
@@ -4719,9 +4797,7 @@ void unary(void) · 6 unmodified lines
47194797 free(args);
47204798 args = s1;
47214799 }
4722− macro_ptr = saved_macro_ptr;
4723− /* restore token */
4724− tok = ')';
4800+ restore_parse_state(&saved_parse_state);
47254801 }
47264802 #endif
47274803 /* 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
55205596 VT_LOCAL or VT_CONST). The allocated address in returned */
55215597 int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init)
55225598 {
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;
55255603 Section *sec;
55265604
55275605 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
55315609 (e.g. string pointers or ISOC99 compound
55325610 literals). It also simplifies local
55335611 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);
55385613 if (size < 0) {
55395614 if (!has_init)
55405615 error("unknown type size");
@@ -5543,7 +5618,7 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 2 unmodified lines
55435618 while (level > 0 || (tok != ',' && tok != ';')) {
55445619 if (tok < 0)
55455620 error("unexpected end of file in initializer");
5546− tok_add2(&init_str, &init_len, tok, &tokc);
5621+ tok_str_add_tok(&init_str);
55475622 if (tok == '{')
55485623 level++;
55495624 else if (tok == '}') {
@@ -5553,17 +5628,17 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 3 unmodified lines
55535628 }
55545629 next();
55555630 }
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);
55595633
55605634 /* 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;
55635638 next();
55645639 decl_initializer(t, r, 0, 1, 1);
55655640 /* prepare second initializer parsing */
5566− macro_ptr = init_str;
5641+ macro_ptr = init_str.str;
55675642 next();
55685643
55695644 /* 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
56325707 if (has_init) {
56335708 decl_initializer(t, r, addr, 1, 0);
56345709 /* 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);
56395713 }
56405714 }
56415715 return addr;
@@ -5663,13 +5737,72 @@ void put_func_debug(int t) · 21 unmodified lines
56635737 last_line_num = 0;
56645738 }
56655739
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+
56665799 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
56675800 void decl(int l)
56685801 {
56695802 int t, b, v, addr, has_init, r;
56705803 Sym *sym;
56715804 AttributeDef ad;
5672−
5805+
56735806 while (1) {
56745807 if (!parse_btype(&b, &ad)) {
56755808 /* skip redundant ';' */
@@ -5701,10 +5834,48 @@ void decl(int l) · 25 unmodified lines
57015834 }
57025835 #endif
57035836 if (tok == '{') {
5837+#ifdef CONFIG_REG_VARS
5838+ TokenString func_str;
5839+ ParseState saved_parse_state;
5840+ int block_level;
5841+#endif
5842+
57045843 if (l == VT_LOCAL)
57055844 error("cannot use local functions");
57065845 if (!(t & VT_FUNC))
57075846 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+
57085879 /* compute text section */
57095880 cur_text_section = ad.section;
57105881 if (!cur_text_section)
@@ -5728,6 +5899,10 @@ void decl(int l) · 17 unmodified lines
57285899 gfunc_prolog(t);
57295900 loc = 0;
57305901 rsym = 0;
5902+#ifdef CONFIG_REG_VARS
5903+ macro_ptr = func_str.str;
5904+ next();
5905+#endif
57315906 block(NULL, NULL, NULL, NULL, 0);
57325907 gsym(rsym);
57335908 gfunc_epilog();
@@ -5741,6 +5916,11 @@ void decl(int l) · 7 unmodified lines
57415916 funcname = ""; /* for safety */
57425917 func_vt = VT_VOID; /* for safety */
57435918 ind = 0; /* for safety */
5919+
5920+#ifdef CONFIG_REG_VARS
5921+ free(func_str.str);
5922+ restore_parse_state(&saved_parse_state);
5923+#endif
57445924 break;
57455925 } else {
57465926 if (b & VT_TYPEDEF) {
@@ -6450,8 +6630,7 @@ TCCState *tcc_new(void) · 703 unmodified lines
64506630 /* default include paths */
64516631 nb_include_paths = 0;
64526632 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");
64556634
64566635 /* add all tokens */
64576636 tok_ident = TOK_IDENT;
@@ -6502,7 +6681,7 @@ int tcc_add_include_path(TCCState *s, const char *pathname) · 44 unmodified lines
65026681
65036682 void help(void)
65046683 {
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"
65066685 "usage: tcc [-Idir] [-Dsym[=val]] [-Usym] [-llib] [-g] [-b]\n"
65076686 " [-i infile] infile [infile_args...]\n"
65086687 "\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