History

restored include file optimization - added static on all globals

bellard committed 23 years ago

Commit cdcfed91 parent 2956bd81 file · +135 −135Browse files

Unified · Split

tcc.c+135−135
@@ -213,7 +213,8 @@ typedef struct BufferedFile { · 212 unmodified lines
213 unsigned char *buf_end;213 unsigned char *buf_end;
214 int fd;214 int fd;
215 int line_num; /* current line number - here to simply code */215 int line_num; /* current line number - here to simply code */
216 int ifndef_macro; /*'#ifndef macro \n #define macro' search */216 int ifndef_macro; /* #ifndef macro / #endif search */
217 int ifndef_macro_saved; /* saved ifndef_macro */
217 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */218 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
218 char inc_type; /* type of include */219 char inc_type; /* type of include */
219 char inc_filename[512]; /* filename specified by the user */220 char inc_filename[512]; /* filename specified by the user */
@@ -249,73 +250,76 @@ typedef struct CachedInclude { · 29 unmodified lines
249} CachedInclude;250} CachedInclude;
250 251
251/* parser */252/* parser */
252struct BufferedFile *file;253static struct BufferedFile *file;
253int ch, tok, tok1;254static int ch, tok, tok1;
254CValue tokc, tok1c;255static CValue tokc, tok1c;
255CString tokcstr; /* current parsed string, if any */256static CString tokcstr; /* current parsed string, if any */
257/* additionnal informations about token */
258static int tok_flags;
259#define TOK_FLAG_BOL 0x0001 /* beginning of line before */
260#define TOK_FLAG_BOF 0x0002 /* beginning of file before */
261#define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
262
256/* if true, line feed is returned as a token. line feed is also263/* if true, line feed is returned as a token. line feed is also
257 returned at eof */264 returned at eof */
258int return_linefeed; 265static int return_linefeed;
259/* set to TRUE if eof was reached */266static Section *text_section, *data_section, *bss_section; /* predefined sections */
260int eof_seen;267static Section *cur_text_section; /* current section where function code is
261Section *text_section, *data_section, *bss_section; /* predefined sections */
262Section *cur_text_section; /* current section where function code is
263 generated */268 generated */
264/* bound check related sections */269/* bound check related sections */
265Section *bounds_section; /* contains global data bound description */270static Section *bounds_section; /* contains global data bound description */
266Section *lbounds_section; /* contains local data bound description */271static Section *lbounds_section; /* contains local data bound description */
267/* symbol sections */272/* symbol sections */
268Section *symtab_section, *strtab_section;273static Section *symtab_section, *strtab_section;
269 274
270/* debug sections */275/* debug sections */
271Section *stab_section, *stabstr_section;276static Section *stab_section, *stabstr_section;
272 277
273/* loc : local variable index278/* loc : local variable index
274 ind : output code index279 ind : output code index
275 rsym: return symbol280 rsym: return symbol
276 anon_sym: anonymous symbol index281 anon_sym: anonymous symbol index
277*/282*/
278int rsym, anon_sym,283static int rsym, anon_sym, ind, loc;
279 prog, ind, loc;
280/* expression generation modifiers */284/* expression generation modifiers */
281int const_wanted; /* true if constant wanted */285static int const_wanted; /* true if constant wanted */
282int nocode_wanted; /* true if no code generation wanted for an expression */286static int nocode_wanted; /* true if no code generation wanted for an expression */
283int global_expr; /* true if compound literals must be allocated287static int global_expr; /* true if compound literals must be allocated
284 globally (used during initializers parsing */288 globally (used during initializers parsing */
285CType func_vt; /* current function return type (used by return289static CType func_vt; /* current function return type (used by return
286 instruction) */290 instruction) */
287int func_vc;291static int func_vc;
288int last_line_num, last_ind, func_ind; /* debug last line number and pc */292static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
289int tok_ident;293static int tok_ident;
290TokenSym **table_ident;294static TokenSym **table_ident;
291TokenSym *hash_ident[TOK_HASH_SIZE];295static TokenSym *hash_ident[TOK_HASH_SIZE];
292char token_buf[STRING_MAX_SIZE + 1];296static char token_buf[STRING_MAX_SIZE + 1];
293char *funcname;297static char *funcname;
294Sym *global_stack, *local_stack;298static Sym *global_stack, *local_stack;
295Sym *define_stack;299static Sym *define_stack;
296Sym *label_stack;300static Sym *label_stack;
297 301
298SValue vstack[VSTACK_SIZE], *vtop;302static SValue vstack[VSTACK_SIZE], *vtop;
299int *macro_ptr, *macro_ptr_allocated;303static int *macro_ptr, *macro_ptr_allocated;
300/* some predefined types */304/* some predefined types */
301CType char_pointer_type, func_old_type, int_type;305static CType char_pointer_type, func_old_type, int_type;
302 306
303/* compile with debug symbol (and use them if error during execution) */307/* compile with debug symbol (and use them if error during execution) */
304int do_debug = 0;308static int do_debug = 0;
305 309
306/* compile with built-in memory and bounds checker */310/* compile with built-in memory and bounds checker */
307int do_bounds_check = 0;311static int do_bounds_check = 0;
308 312
309/* display benchmark infos */313/* display benchmark infos */
310int do_bench = 0;314static int do_bench = 0;
311int total_lines;315static int total_lines;
312int total_bytes;316static int total_bytes;
313 317
314/* use GNU C extensions */318/* use GNU C extensions */
315int gnu_ext = 1;319static int gnu_ext = 1;
316 320
317/* use Tiny C extensions */321/* use Tiny C extensions */
318int tcc_ext = 1;322static int tcc_ext = 1;
319 323
320/* max number of callers shown if error */324/* max number of callers shown if error */
321static int num_callers = 6;325static int num_callers = 6;
@@ -1225,6 +1229,21 @@ static void cstr_free(CString *cstr) · 903 unmodified lines
1225 1229
1226#define cstr_reset(cstr) cstr_free(cstr)1230#define cstr_reset(cstr) cstr_free(cstr)
1227 1231
1232static CString *cstr_dup(CString *cstr1)
1233{
1234 CString *cstr;
1235 int size;
1236
1237 cstr = tcc_malloc(sizeof(CString));
1238 size = cstr1->size;
1239 cstr->size = size;
1240 cstr->size_allocated = size;
1241 cstr->data_allocated = tcc_malloc(size);
1242 cstr->data = cstr->data_allocated;
1243 memcpy(cstr->data_allocated, cstr1->data_allocated, size);
1244 return cstr;
1245}
1246
1228/* XXX: unicode ? */1247/* XXX: unicode ? */
1229static void add_char(CString *cstr, int c)1248static void add_char(CString *cstr, int c)
1230{1249{
@@ -1525,32 +1544,10 @@ int tcc_getc_slow(BufferedFile *bf) · 294 unmodified lines
1525/* no need to put that inline */1544/* no need to put that inline */
1526void handle_eob(void)1545void handle_eob(void)
1527{1546{
1528 TCCState *s1 = tcc_state;
1529
1530 /* no need to do anything if not at EOB */1547 /* no need to do anything if not at EOB */
1531 if (file->buf_ptr <= file->buf_end)1548 if (file->buf_ptr <= file->buf_end)
1532 return;1549 return;
1533 1550 ch = tcc_getc_slow(file);
1534 for(;;) {
1535 ch = tcc_getc_slow(file);
1536 if (ch != CH_EOF)
1537 return;
1538 eof_seen = 1;
1539 if (return_linefeed) {
1540 ch = '\n';
1541 return;
1542 }
1543 if (s1->include_stack_ptr == s1->include_stack)
1544 return;
1545 /* add end of include file debug info */
1546 if (do_debug) {
1547 put_stabd(N_EINCL, 0, 0);
1548 }
1549 /* pop include stack */
1550 tcc_close(file);
1551 s1->include_stack_ptr--;
1552 file = *s1->include_stack_ptr;
1553 }
1554}1551}
1555 1552
1556/* read next char from current input file and handle end of input buffer */1553/* read next char from current input file and handle end of input buffer */
@@ -1668,7 +1665,8 @@ void preprocess_skip(void) · 111 unmodified lines
1668 1665
1669 start_of_line = 1;1666 start_of_line = 1;
1670 a = 0;1667 a = 0;
1671 while (1) {1668 for(;;) {
1669 redo_no_start:
1672 switch(ch) {1670 switch(ch) {
1673 case ' ':1671 case ' ':
1674 case '\t':1672 case '\t':
@@ -1676,19 +1674,18 @@ void preprocess_skip(void) · 1 unmodified line
1676 case '\v':1674 case '\v':
1677 case '\r':1675 case '\r':
1678 inp();1676 inp();
1679 break;1677 goto redo_no_start;
1680 case '\n':1678 case '\n':
1681 start_of_line = 1;1679 start_of_line = 1;
1682 file->line_num++;1680 file->line_num++;
1683 inp();1681 inp();
1684 break;1682 goto redo_no_start;
1685 case '\\':1683 case '\\':
1686 handle_stray();1684 handle_stray();
1687 break;1685 goto redo_no_start;
1688 /* skip strings */1686 /* skip strings */
1689 case '\"':1687 case '\"':
1690 case '\'':1688 case '\'':
1691 start_of_line = 0;
1692 sep = ch;1689 sep = ch;
1693 inp();1690 inp();
1694 while (ch != sep) {1691 while (ch != sep) {
@@ -1714,8 +1711,6 @@ void preprocess_skip(void) · 19 unmodified lines
1714 parse_comment();1711 parse_comment();
1715 } else if (ch == '/') {1712 } else if (ch == '/') {
1716 parse_line_comment();1713 parse_line_comment();
1717 } else {
1718 start_of_line = 0;
1719 }1714 }
1720 break;1715 break;
1721 1716
@@ -1731,16 +1726,15 @@ void preprocess_skip(void) · 9 unmodified lines
1731 else if (tok == TOK_ENDIF)1726 else if (tok == TOK_ENDIF)
1732 a--;1727 a--;
1733 }1728 }
1734 start_of_line = 0;
1735 break;1729 break;
1736 case CH_EOF:1730 case CH_EOF:
1737 expect("#endif");1731 expect("#endif");
1738 break;1732 break;
1739 default:1733 default:
1740 inp();1734 inp();
1741 start_of_line = 0;
1742 break;1735 break;
1743 }1736 }
1737 start_of_line = 0;
1744 }1738 }
1745 the_end: ;1739 the_end: ;
1746}1740}
@@ -1846,22 +1840,13 @@ static void tok_str_add(TokenString *s, int t) · 99 unmodified lines
1846 1840
1847static void tok_str_add2(TokenString *s, int t, CValue *cv)1841static void tok_str_add2(TokenString *s, int t, CValue *cv)
1848{1842{
1849 int n, i, size;1843 int n, i;
1850 CString *cstr, *cstr1;
1851 CValue cv1;1844 CValue cv1;
1852 1845
1853 tok_str_add(s, t);1846 tok_str_add(s, t);
1854 if (t == TOK_STR || t == TOK_LSTR || t == TOK_PPNUM) {1847 if (t == TOK_STR || t == TOK_LSTR || t == TOK_PPNUM) {
1855 /* special case: need to duplicate string */1848 /* special case: need to duplicate string */
1856 cstr1 = cv->cstr;1849 cv1.cstr = cstr_dup(cv->cstr);
1857 cstr = tcc_malloc(sizeof(CString));
1858 size = cstr1->size;
1859 cstr->size = size;
1860 cstr->size_allocated = size;
1861 cstr->data_allocated = tcc_malloc(size);
1862 cstr->data = cstr->data_allocated;
1863 memcpy(cstr->data_allocated, cstr1->data_allocated, size);
1864 cv1.cstr = cstr;
1865 tok_str_add(s, cv1.tab[0]);1850 tok_str_add(s, cv1.tab[0]);
1866 } else {1851 } else {
1867 n = tok_ext_size(t);1852 n = tok_ext_size(t);
@@ -2107,17 +2092,11 @@ static inline void add_cached_include(TCCState *s1, int type, · 239 unmodified lines
2107 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);2092 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2108}2093}
2109 2094
2110 2095/* is_bof is true if first non space token at beginning of file */
2111enum IncludeState {2096static void preprocess(int is_bof)
2112 INCLUDE_STATE_NONE = 0,
2113 INCLUDE_STATE_SEEK_IFNDEF,
2114};
2115
2116void preprocess(void)
2117{2097{
2118 TCCState *s1 = tcc_state;2098 TCCState *s1 = tcc_state;
2119 int size, i, c, n, line_num;2099 int size, i, c, n, line_num;
2120 enum IncludeState state;
2121 char buf[1024], *q, *p;2100 char buf[1024], *q, *p;
2122 char buf1[1024];2101 char buf1[1024];
2123 BufferedFile *f;2102 BufferedFile *f;
@@ -2126,9 +2105,6 @@ void preprocess(void) · 2 unmodified lines
2126 2105
2127 return_linefeed = 1; /* linefeed will be returned as a2106 return_linefeed = 1; /* linefeed will be returned as a
2128 token. EOF is also returned as line feed */2107 token. EOF is also returned as line feed */
2129 state = INCLUDE_STATE_NONE;
2130 eof_seen = 0;
2131 redo1:
2132 next_nomacro();2108 next_nomacro();
2133 redo:2109 redo:
2134 switch(tok) {2110 switch(tok) {
@@ -2253,16 +2229,8 @@ void preprocess(void) · 118 unmodified lines
2253 if (do_debug) {2229 if (do_debug) {
2254 put_stabs(file->filename, N_BINCL, 0, 0, 0);2230 put_stabs(file->filename, N_BINCL, 0, 0, 0);
2255 }2231 }
2256 /* we check for the construct: #ifndef IDENT \n #define IDENT */2232 tok_flags |= TOK_FLAG_BOF;
2257 inp();2233 goto the_end;
2258 /* get first non space char */
2259 while (is_space(ch) || ch == '\n')
2260 cinp();
2261 if (ch != '#')
2262 goto the_end;
2263 state = INCLUDE_STATE_SEEK_IFNDEF;
2264 cinp();
2265 goto redo1;
2266 }2234 }
2267 break;2235 break;
2268 case TOK_IFNDEF:2236 case TOK_IFNDEF:
@@ -2277,11 +2245,13 @@ void preprocess(void) · 8 unmodified lines
2277 next_nomacro();2245 next_nomacro();
2278 if (tok < TOK_IDENT)2246 if (tok < TOK_IDENT)
2279 error("invalid argument for '#if%sdef'", c ? "n" : "");2247 error("invalid argument for '#if%sdef'", c ? "n" : "");
2280 if (state == INCLUDE_STATE_SEEK_IFNDEF) {2248 if (is_bof) {
2281 if (c) {2249 if (c) {
2250#ifdef INC_DEBUG
2251 printf("#ifndef %s\n", get_tok_str(tok, NULL));
2252#endif
2282 file->ifndef_macro = tok;2253 file->ifndef_macro = tok;
2283 }2254 }
2284 state = INCLUDE_STATE_NONE;
2285 }2255 }
2286 c = (define_find(tok) != 0) ^ c;2256 c = (define_find(tok) != 0) ^ c;
2287 do_if:2257 do_if:
@@ -2311,31 +2281,27 @@ void preprocess(void) · 23 unmodified lines
2311 if (!(c & 1)) {2281 if (!(c & 1)) {
2312 skip:2282 skip:
2313 preprocess_skip();2283 preprocess_skip();
2314 state = INCLUDE_STATE_NONE;2284 is_bof = 0;
2315 goto redo;2285 goto redo;
2316 }2286 }
2317 break;2287 break;
2318 case TOK_ENDIF:2288 case TOK_ENDIF:
2319 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)2289 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
2320 error("#endif without matching #if");2290 error("#endif without matching #if");
2291 s1->ifdef_stack_ptr--;
2292 /* '#ifndef macro' was at the start of file. Now we check if
2293 an '#endif' is exactly at the end of file */
2321 if (file->ifndef_macro &&2294 if (file->ifndef_macro &&
2322 s1->ifdef_stack_ptr == (file->ifdef_stack_ptr + 1)) {2295 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
2323 /* '#ifndef macro \n #define macro' was at the start of2296 file->ifndef_macro_saved = file->ifndef_macro;
2324 file. Now we check if an '#endif' is exactly at the end2297 /* need to set to zero to avoid false matches if another
2325 of file */2298 #ifndef at middle of file */
2299 file->ifndef_macro = 0;
2326 while (tok != TOK_LINEFEED)2300 while (tok != TOK_LINEFEED)
2327 next_nomacro();2301 next_nomacro();
2328 /* XXX: should also skip comments, but it is more complicated */2302 tok_flags |= TOK_FLAG_ENDIF;
2329 if (eof_seen) {2303 goto the_end;
2330 add_cached_include(s1, file->inc_type, file->inc_filename,
2331 file->ifndef_macro);
2332 } else {
2333 /* if not end of file, we must desactivate the ifndef
2334 macro search */
2335 file->ifndef_macro = 0;
2336 }
2337 }2304 }
2338 s1->ifdef_stack_ptr--;
2339 break;2305 break;
2340 case TOK_LINE:2306 case TOK_LINE:
2341 next();2307 next();
@@ -2378,7 +2344,7 @@ void preprocess(void) · 36 unmodified lines
2378 break;2344 break;
2379 }2345 }
2380 /* ignore other preprocess commands or #! for C scripts */2346 /* ignore other preprocess commands or #! for C scripts */
2381 while (tok != TOK_LINEFEED && tok != TOK_EOF)2347 while (tok != TOK_LINEFEED)
2382 next_nomacro();2348 next_nomacro();
2383 the_end:2349 the_end:
2384 return_linefeed = 0;2350 return_linefeed = 0;
@@ -2780,11 +2746,10 @@ void parse_number(const char *p) · 395 unmodified lines
2780/* return next token without macro substitution */2746/* return next token without macro substitution */
2781static inline void next_nomacro1(void)2747static inline void next_nomacro1(void)
2782{2748{
2783 int b, t, start_of_line;2749 int b, t;
2784 char *q;2750 char *q;
2785 TokenSym *ts;2751 TokenSym *ts;
2786 2752
2787 start_of_line = 0;
2788 redo_no_start:2753 redo_no_start:
2789 switch(ch) {2754 switch(ch) {
2790 case ' ':2755 case ' ':
@@ -2799,13 +2764,48 @@ static inline void next_nomacro1(void) · 8 unmodified lines
2799 handle_stray();2764 handle_stray();
2800 goto redo_no_start;2765 goto redo_no_start;
2801 2766
2767 case CH_EOF:
2768 {
2769 TCCState *s1 = tcc_state;
2770
2771 if (return_linefeed) {
2772 tok = TOK_LINEFEED;
2773 } else if (s1->include_stack_ptr == s1->include_stack) {
2774 /* no include left : end of file */
2775 tok = TOK_EOF;
2776 } else {
2777 /* pop include file */
2778
2779 /* test if previous '#endif' was after a #ifdef at
2780 start of file */
2781 if (tok_flags & TOK_FLAG_ENDIF) {
2782#ifdef INC_DEBUG
2783 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2784#endif
2785 add_cached_include(s1, file->inc_type, file->inc_filename,
2786 file->ifndef_macro_saved);
2787 }
2788
2789 /* add end of include file debug info */
2790 if (do_debug) {
2791 put_stabd(N_EINCL, 0, 0);
2792 }
2793 /* pop include stack */
2794 tcc_close(file);
2795 s1->include_stack_ptr--;
2796 file = *s1->include_stack_ptr;
2797 inp();
2798 goto redo_no_start;
2799 }
2800 }
2801 break;
2802
2802 case '\n':2803 case '\n':
2803 file->line_num++;2804 file->line_num++;
2804 if (return_linefeed) {2805 if (return_linefeed) {
2805 /* XXX: should eat token ? */
2806 tok = TOK_LINEFEED;2806 tok = TOK_LINEFEED;
2807 } else {2807 } else {
2808 start_of_line = 1;2808 tok_flags |= TOK_FLAG_BOL;
2809 inp();2809 inp();
2810 goto redo_no_start;2810 goto redo_no_start;
2811 }2811 }
@@ -2813,8 +2813,8 @@ static inline void next_nomacro1(void) · 1 unmodified line
2813 2813
2814 case '#':2814 case '#':
2815 minp();2815 minp();
2816 if (start_of_line) {2816 if (tok_flags & TOK_FLAG_BOL) {
2817 preprocess();2817 preprocess(tok_flags & TOK_FLAG_BOF);
2818 goto redo_no_start;2818 goto redo_no_start;
2819 } else {2819 } else {
2820 if (ch == '#') {2820 if (ch == '#') {
@@ -3108,13 +3108,11 @@ static inline void next_nomacro1(void) · 287 unmodified lines
3108 tok = ch;3108 tok = ch;
3109 cinp();3109 cinp();
3110 break;3110 break;
3111 case CH_EOF:
3112 tok = TOK_EOF;
3113 break;
3114 default:3111 default:
3115 error("unrecognized character \\x%02x", ch);3112 error("unrecognized character \\x%02x", ch);
3116 break;3113 break;
3117 }3114 }
3115 tok_flags = 0;
3118#if defined(PARSE_DEBUG)3116#if defined(PARSE_DEBUG)
3119 printf("token = %s\n", get_tok_str(tok, &tokc));3117 printf("token = %s\n", get_tok_str(tok, &tokc));
3120#endif3118#endif
@@ -3386,6 +3384,7 @@ static int macro_subst_tok(TokenString *tok_str, · 265 unmodified lines
3386 if (macro_ptr) {3384 if (macro_ptr) {
3387 t = *macro_ptr;3385 t = *macro_ptr;
3388 } else {3386 } else {
3387 /* XXX: incorrect with comments */
3389 while (is_space(ch) || ch == '\n')3388 while (is_space(ch) || ch == '\n')
3390 cinp();3389 cinp();
3391 t = ch;3390 t = ch;
@@ -7642,7 +7641,8 @@ static int tcc_compile(TCCState *s1) · 4250 unmodified lines
7642 s1->nb_errors = 0;7641 s1->nb_errors = 0;
7643 s1->error_set_jmp_enabled = 1;7642 s1->error_set_jmp_enabled = 1;
7644 7643
7645 ch = '\n'; /* needed to parse correctly first preprocessor command */7644 ch = '\n'; /* XXX: suppress that*/
7645 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
7646 next();7646 next();
7647 decl(VT_CONST);7647 decl(VT_CONST);
7648 if (tok != -1)7648 if (tok != -1)

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