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
213213 unsigned char *buf_end;
214214 int fd;
215215 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 */
217218 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
218219 char inc_type; /* type of include */
219220 char inc_filename[512]; /* filename specified by the user */
@@ -249,73 +250,76 @@ typedef struct CachedInclude { · 29 unmodified lines
249250 } CachedInclude;
250251
251252 /* parser */
252−struct BufferedFile *file;
253−int ch, tok, tok1;
254−CValue tokc, tok1c;
255−CString tokcstr; /* current parsed string, if any */
253+static struct BufferedFile *file;
254+static int ch, tok, tok1;
255+static CValue tokc, tok1c;
256+static CString tokcstr; /* current parsed string, if any */
257+/* additionnal informations about token */
258+static 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+
256263 /* if true, line feed is returned as a token. line feed is also
257264 returned at eof */
258−int return_linefeed;
259−/* set to TRUE if eof was reached */
260−int eof_seen;
261−Section *text_section, *data_section, *bss_section; /* predefined sections */
262−Section *cur_text_section; /* current section where function code is
265+static int return_linefeed;
266+static Section *text_section, *data_section, *bss_section; /* predefined sections */
267+static Section *cur_text_section; /* current section where function code is
263268 generated */
264269 /* bound check related sections */
265−Section *bounds_section; /* contains global data bound description */
266−Section *lbounds_section; /* contains local data bound description */
270+static Section *bounds_section; /* contains global data bound description */
271+static Section *lbounds_section; /* contains local data bound description */
267272 /* symbol sections */
268−Section *symtab_section, *strtab_section;
273+static Section *symtab_section, *strtab_section;
269274
270275 /* debug sections */
271−Section *stab_section, *stabstr_section;
276+static Section *stab_section, *stabstr_section;
272277
273278 /* loc : local variable index
274279 ind : output code index
275280 rsym: return symbol
276281 anon_sym: anonymous symbol index
277282 */
278−int rsym, anon_sym,
279− prog, ind, loc;
283+static int rsym, anon_sym, ind, loc;
280284 /* expression generation modifiers */
281−int const_wanted; /* true if constant wanted */
282−int nocode_wanted; /* true if no code generation wanted for an expression */
283−int global_expr; /* true if compound literals must be allocated
284− globally (used during initializers parsing */
285−CType func_vt; /* current function return type (used by return
286− instruction) */
287−int func_vc;
288−int last_line_num, last_ind, func_ind; /* debug last line number and pc */
289−int tok_ident;
290−TokenSym **table_ident;
291−TokenSym *hash_ident[TOK_HASH_SIZE];
292−char token_buf[STRING_MAX_SIZE + 1];
293−char *funcname;
294−Sym *global_stack, *local_stack;
295−Sym *define_stack;
296−Sym *label_stack;
297−
298−SValue vstack[VSTACK_SIZE], *vtop;
299−int *macro_ptr, *macro_ptr_allocated;
285+static int const_wanted; /* true if constant wanted */
286+static int nocode_wanted; /* true if no code generation wanted for an expression */
287+static int global_expr; /* true if compound literals must be allocated
288+ globally (used during initializers parsing */
289+static CType func_vt; /* current function return type (used by return
290+ instruction) */
291+static int func_vc;
292+static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
293+static int tok_ident;
294+static TokenSym **table_ident;
295+static TokenSym *hash_ident[TOK_HASH_SIZE];
296+static char token_buf[STRING_MAX_SIZE + 1];
297+static char *funcname;
298+static Sym *global_stack, *local_stack;
299+static Sym *define_stack;
300+static Sym *label_stack;
301+
302+static SValue vstack[VSTACK_SIZE], *vtop;
303+static int *macro_ptr, *macro_ptr_allocated;
300304 /* some predefined types */
301−CType char_pointer_type, func_old_type, int_type;
305+static CType char_pointer_type, func_old_type, int_type;
302306
303307 /* compile with debug symbol (and use them if error during execution) */
304−int do_debug = 0;
308+static int do_debug = 0;
305309
306310 /* compile with built-in memory and bounds checker */
307−int do_bounds_check = 0;
311+static int do_bounds_check = 0;
308312
309313 /* display benchmark infos */
310−int do_bench = 0;
311−int total_lines;
312−int total_bytes;
314+static int do_bench = 0;
315+static int total_lines;
316+static int total_bytes;
313317
314318 /* use GNU C extensions */
315−int gnu_ext = 1;
319+static int gnu_ext = 1;
316320
317321 /* use Tiny C extensions */
318−int tcc_ext = 1;
322+static int tcc_ext = 1;
319323
320324 /* max number of callers shown if error */
321325 static int num_callers = 6;
@@ -1225,6 +1229,21 @@ static void cstr_free(CString *cstr) · 903 unmodified lines
12251229
12261230 #define cstr_reset(cstr) cstr_free(cstr)
12271231
1232+static 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+
12281247 /* XXX: unicode ? */
12291248 static void add_char(CString *cstr, int c)
12301249 {
@@ -1525,32 +1544,10 @@ int tcc_getc_slow(BufferedFile *bf) · 294 unmodified lines
15251544 /* no need to put that inline */
15261545 void handle_eob(void)
15271546 {
1528− TCCState *s1 = tcc_state;
1529−
15301547 /* no need to do anything if not at EOB */
15311548 if (file->buf_ptr <= file->buf_end)
15321549 return;
1533−
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− }
1550+ ch = tcc_getc_slow(file);
15541551 }
15551552
15561553 /* read next char from current input file and handle end of input buffer */
@@ -1668,7 +1665,8 @@ void preprocess_skip(void) · 111 unmodified lines
16681665
16691666 start_of_line = 1;
16701667 a = 0;
1671− while (1) {
1668+ for(;;) {
1669+ redo_no_start:
16721670 switch(ch) {
16731671 case ' ':
16741672 case '\t':
@@ -1676,19 +1674,18 @@ void preprocess_skip(void) · 1 unmodified line
16761674 case '\v':
16771675 case '\r':
16781676 inp();
1679− break;
1677+ goto redo_no_start;
16801678 case '\n':
16811679 start_of_line = 1;
16821680 file->line_num++;
16831681 inp();
1684− break;
1682+ goto redo_no_start;
16851683 case '\\':
16861684 handle_stray();
1687− break;
1685+ goto redo_no_start;
16881686 /* skip strings */
16891687 case '\"':
16901688 case '\'':
1691− start_of_line = 0;
16921689 sep = ch;
16931690 inp();
16941691 while (ch != sep) {
@@ -1714,8 +1711,6 @@ void preprocess_skip(void) · 19 unmodified lines
17141711 parse_comment();
17151712 } else if (ch == '/') {
17161713 parse_line_comment();
1717− } else {
1718− start_of_line = 0;
17191714 }
17201715 break;
17211716
@@ -1731,16 +1726,15 @@ void preprocess_skip(void) · 9 unmodified lines
17311726 else if (tok == TOK_ENDIF)
17321727 a--;
17331728 }
1734− start_of_line = 0;
17351729 break;
17361730 case CH_EOF:
17371731 expect("#endif");
17381732 break;
17391733 default:
17401734 inp();
1741− start_of_line = 0;
17421735 break;
17431736 }
1737+ start_of_line = 0;
17441738 }
17451739 the_end: ;
17461740 }
@@ -1846,22 +1840,13 @@ static void tok_str_add(TokenString *s, int t) · 99 unmodified lines
18461840
18471841 static void tok_str_add2(TokenString *s, int t, CValue *cv)
18481842 {
1849− int n, i, size;
1850− CString *cstr, *cstr1;
1843+ int n, i;
18511844 CValue cv1;
18521845
18531846 tok_str_add(s, t);
18541847 if (t == TOK_STR || t == TOK_LSTR || t == TOK_PPNUM) {
18551848 /* special case: need to duplicate string */
1856− cstr1 = 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;
1849+ cv1.cstr = cstr_dup(cv->cstr);
18651850 tok_str_add(s, cv1.tab[0]);
18661851 } else {
18671852 n = tok_ext_size(t);
@@ -2107,17 +2092,11 @@ static inline void add_cached_include(TCCState *s1, int type, · 239 unmodified lines
21072092 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
21082093 }
21092094
2110−
2111−enum IncludeState {
2112− INCLUDE_STATE_NONE = 0,
2113− INCLUDE_STATE_SEEK_IFNDEF,
2114−};
2115−
2116−void preprocess(void)
2095+/* is_bof is true if first non space token at beginning of file */
2096+static void preprocess(int is_bof)
21172097 {
21182098 TCCState *s1 = tcc_state;
21192099 int size, i, c, n, line_num;
2120− enum IncludeState state;
21212100 char buf[1024], *q, *p;
21222101 char buf1[1024];
21232102 BufferedFile *f;
@@ -2126,9 +2105,6 @@ void preprocess(void) · 2 unmodified lines
21262105
21272106 return_linefeed = 1; /* linefeed will be returned as a
21282107 token. EOF is also returned as line feed */
2129− state = INCLUDE_STATE_NONE;
2130− eof_seen = 0;
2131− redo1:
21322108 next_nomacro();
21332109 redo:
21342110 switch(tok) {
@@ -2253,16 +2229,8 @@ void preprocess(void) · 118 unmodified lines
22532229 if (do_debug) {
22542230 put_stabs(file->filename, N_BINCL, 0, 0, 0);
22552231 }
2256− /* we check for the construct: #ifndef IDENT \n #define IDENT */
2257− inp();
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;
2232+ tok_flags |= TOK_FLAG_BOF;
2233+ goto the_end;
22662234 }
22672235 break;
22682236 case TOK_IFNDEF:
@@ -2277,11 +2245,13 @@ void preprocess(void) · 8 unmodified lines
22772245 next_nomacro();
22782246 if (tok < TOK_IDENT)
22792247 error("invalid argument for '#if%sdef'", c ? "n" : "");
2280− if (state == INCLUDE_STATE_SEEK_IFNDEF) {
2248+ if (is_bof) {
22812249 if (c) {
2250+#ifdef INC_DEBUG
2251+ printf("#ifndef %s\n", get_tok_str(tok, NULL));
2252+#endif
22822253 file->ifndef_macro = tok;
22832254 }
2284− state = INCLUDE_STATE_NONE;
22852255 }
22862256 c = (define_find(tok) != 0) ^ c;
22872257 do_if:
@@ -2311,31 +2281,27 @@ void preprocess(void) · 23 unmodified lines
23112281 if (!(c & 1)) {
23122282 skip:
23132283 preprocess_skip();
2314− state = INCLUDE_STATE_NONE;
2284+ is_bof = 0;
23152285 goto redo;
23162286 }
23172287 break;
23182288 case TOK_ENDIF:
23192289 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
23202290 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 */
23212294 if (file->ifndef_macro &&
2322− s1->ifdef_stack_ptr == (file->ifdef_stack_ptr + 1)) {
2323− /* '#ifndef macro \n #define macro' was at the start of
2324− file. Now we check if an '#endif' is exactly at the end
2325− of file */
2295+ s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
2296+ file->ifndef_macro_saved = file->ifndef_macro;
2297+ /* need to set to zero to avoid false matches if another
2298+ #ifndef at middle of file */
2299+ file->ifndef_macro = 0;
23262300 while (tok != TOK_LINEFEED)
23272301 next_nomacro();
2328− /* XXX: should also skip comments, but it is more complicated */
2329− if (eof_seen) {
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− }
2302+ tok_flags |= TOK_FLAG_ENDIF;
2303+ goto the_end;
23372304 }
2338− s1->ifdef_stack_ptr--;
23392305 break;
23402306 case TOK_LINE:
23412307 next();
@@ -2378,7 +2344,7 @@ void preprocess(void) · 36 unmodified lines
23782344 break;
23792345 }
23802346 /* ignore other preprocess commands or #! for C scripts */
2381− while (tok != TOK_LINEFEED && tok != TOK_EOF)
2347+ while (tok != TOK_LINEFEED)
23822348 next_nomacro();
23832349 the_end:
23842350 return_linefeed = 0;
@@ -2780,11 +2746,10 @@ void parse_number(const char *p) · 395 unmodified lines
27802746 /* return next token without macro substitution */
27812747 static inline void next_nomacro1(void)
27822748 {
2783− int b, t, start_of_line;
2749+ int b, t;
27842750 char *q;
27852751 TokenSym *ts;
27862752
2787− start_of_line = 0;
27882753 redo_no_start:
27892754 switch(ch) {
27902755 case ' ':
@@ -2799,13 +2764,48 @@ static inline void next_nomacro1(void) · 8 unmodified lines
27992764 handle_stray();
28002765 goto redo_no_start;
28012766
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+
28022803 case '\n':
28032804 file->line_num++;
28042805 if (return_linefeed) {
2805− /* XXX: should eat token ? */
28062806 tok = TOK_LINEFEED;
28072807 } else {
2808− start_of_line = 1;
2808+ tok_flags |= TOK_FLAG_BOL;
28092809 inp();
28102810 goto redo_no_start;
28112811 }
@@ -2813,8 +2813,8 @@ static inline void next_nomacro1(void) · 1 unmodified line
28132813
28142814 case '#':
28152815 minp();
2816− if (start_of_line) {
2817− preprocess();
2816+ if (tok_flags & TOK_FLAG_BOL) {
2817+ preprocess(tok_flags & TOK_FLAG_BOF);
28182818 goto redo_no_start;
28192819 } else {
28202820 if (ch == '#') {
@@ -3108,13 +3108,11 @@ static inline void next_nomacro1(void) · 287 unmodified lines
31083108 tok = ch;
31093109 cinp();
31103110 break;
3111− case CH_EOF:
3112− tok = TOK_EOF;
3113− break;
31143111 default:
31153112 error("unrecognized character \\x%02x", ch);
31163113 break;
31173114 }
3115+ tok_flags = 0;
31183116 #if defined(PARSE_DEBUG)
31193117 printf("token = %s\n", get_tok_str(tok, &tokc));
31203118 #endif
@@ -3386,6 +3384,7 @@ static int macro_subst_tok(TokenString *tok_str, · 265 unmodified lines
33863384 if (macro_ptr) {
33873385 t = *macro_ptr;
33883386 } else {
3387+ /* XXX: incorrect with comments */
33893388 while (is_space(ch) || ch == '\n')
33903389 cinp();
33913390 t = ch;
@@ -7642,7 +7641,8 @@ static int tcc_compile(TCCState *s1) · 4250 unmodified lines
76427641 s1->nb_errors = 0;
76437642 s1->error_set_jmp_enabled = 1;
76447643
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;
76467646 next();
76477647 decl(VT_CONST);
76487648 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