History

optimisations: hash table for include file search, pool for symbol allocation - sympler token string allocation - '-b' fix with '-run' option

bellard committed 22 years ago

Commit 48c85f11 parent 398e22c1 file · +111 −74Browse files

Unified · Split

tcc.c+111−74
@@ -289,10 +289,13 @@ typedef struct TokenString { · 288 unmodified lines
289 inclusion if the include file is protected by #ifndef ... #endif */289 inclusion if the include file is protected by #ifndef ... #endif */
290typedef struct CachedInclude {290typedef struct CachedInclude {
291 int ifndef_macro;291 int ifndef_macro;
292 int hash_next; /* -1 if none */
292 char type; /* '"' or '>' to give include type */293 char type; /* '"' or '>' to give include type */
293 char filename[1]; /* path specified in #include */294 char filename[1]; /* path specified in #include */
294} CachedInclude;295} CachedInclude;
295 296
297#define CACHED_INCLUDES_HASH_SIZE 512
298
296/* parser */299/* parser */
297static struct BufferedFile *file;300static struct BufferedFile *file;
298static int ch, tok;301static int ch, tok;
@@ -319,7 +322,9 @@ static int parse_flags; · 20 unmodified lines
319static Section *text_section, *data_section, *bss_section; /* predefined sections */322static Section *text_section, *data_section, *bss_section; /* predefined sections */
320static Section *cur_text_section; /* current section where function code is323static Section *cur_text_section; /* current section where function code is
321 generated */324 generated */
325#ifdef CONFIG_TCC_ASM
322static Section *last_text_section; /* to handle .previous asm directive */326static Section *last_text_section; /* to handle .previous asm directive */
327#endif
323/* bound check related sections */328/* bound check related sections */
324static Section *bounds_section; /* contains global data bound description */329static Section *bounds_section; /* contains global data bound description */
325static Section *lbounds_section; /* contains local data bound description */330static Section *lbounds_section; /* contains local data bound description */
@@ -352,6 +357,9 @@ static char *funcname; · 26 unmodified lines
352static Sym *global_stack, *local_stack;357static Sym *global_stack, *local_stack;
353static Sym *define_stack;358static Sym *define_stack;
354static Sym *global_label_stack, *local_label_stack;359static Sym *global_label_stack, *local_label_stack;
360/* symbol allocator */
361#define SYM_POOL_NB (8192 / sizeof(Sym))
362static Sym *sym_free_first;
355 363
356static SValue vstack[VSTACK_SIZE], *vtop;364static SValue vstack[VSTACK_SIZE], *vtop;
357/* some predefined types */365/* some predefined types */
@@ -473,6 +481,9 @@ struct TCCState { · 115 unmodified lines
473 481
474 /* see ifdef_stack_ptr */482 /* see ifdef_stack_ptr */
475 int ifdef_stack[IFDEF_STACK_SIZE];483 int ifdef_stack[IFDEF_STACK_SIZE];
484
485 /* see cached_includes */
486 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
476};487};
477 488
478/* The current value can be: */489/* The current value can be: */
@@ -1048,6 +1059,41 @@ static void dynarray_add(void ***ptab, int *nb_ptr, void *data) · 569 unmodified lines
1048 *nb_ptr = nb;1059 *nb_ptr = nb;
1049}1060}
1050 1061
1062/* symbol allocator */
1063static Sym *__sym_malloc(void)
1064{
1065 Sym *sym_pool, *sym, *last_sym;
1066 int i;
1067
1068 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
1069
1070 last_sym = sym_free_first;
1071 sym = sym_pool;
1072 for(i = 0; i < SYM_POOL_NB; i++) {
1073 sym->next = last_sym;
1074 last_sym = sym;
1075 sym++;
1076 }
1077 sym_free_first = last_sym;
1078 return last_sym;
1079}
1080
1081static inline Sym *sym_malloc(void)
1082{
1083 Sym *sym;
1084 sym = sym_free_first;
1085 if (!sym)
1086 sym = __sym_malloc();
1087 sym_free_first = sym->next;
1088 return sym;
1089}
1090
1091static inline void sym_free(Sym *sym)
1092{
1093 sym->next = sym_free_first;
1094 sym_free_first = sym;
1095}
1096
1051Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)1097Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
1052{1098{
1053 Section *sec;1099 Section *sec;
@@ -1433,7 +1479,7 @@ static void cstr_realloc(CString *cstr, int new_size) · 379 unmodified lines
1433}1479}
1434 1480
1435/* add a byte */1481/* add a byte */
1436static void cstr_ccat(CString *cstr, int ch)1482static inline void cstr_ccat(CString *cstr, int ch)
1437{1483{
1438 int size;1484 int size;
1439 size = cstr->size + 1;1485 size = cstr->size + 1;
@@ -1480,21 +1526,6 @@ static void cstr_free(CString *cstr) · 40 unmodified lines
1480 1526
1481#define cstr_reset(cstr) cstr_free(cstr)1527#define cstr_reset(cstr) cstr_free(cstr)
1482 1528
1483static CString *cstr_dup(CString *cstr1)
1484{
1485 CString *cstr;
1486 int size;
1487
1488 cstr = tcc_malloc(sizeof(CString));
1489 size = cstr1->size;
1490 cstr->size = size;
1491 cstr->size_allocated = size;
1492 cstr->data_allocated = tcc_malloc(size);
1493 cstr->data = cstr->data_allocated;
1494 memcpy(cstr->data_allocated, cstr1->data_allocated, size);
1495 return cstr;
1496}
1497
1498/* XXX: unicode ? */1529/* XXX: unicode ? */
1499static void add_char(CString *cstr, int c)1530static void add_char(CString *cstr, int c)
1500{1531{
@@ -1618,7 +1649,7 @@ char *get_tok_str(int v, CValue *cv) · 117 unmodified lines
1618static Sym *sym_push2(Sym **ps, int v, int t, int c)1649static Sym *sym_push2(Sym **ps, int v, int t, int c)
1619{1650{
1620 Sym *s;1651 Sym *s;
1621 s = tcc_malloc(sizeof(Sym));1652 s = sym_malloc();
1622 s->v = v;1653 s->v = v;
1623 s->type.t = t;1654 s->type.t = t;
1624 s->c = c;1655 s->c = c;
@@ -1726,7 +1757,7 @@ static void sym_pop(Sym **ptop, Sym *b) · 101 unmodified lines
1726 ps = &ts->sym_identifier;1757 ps = &ts->sym_identifier;
1727 *ps = s->prev_tok;1758 *ps = s->prev_tok;
1728 }1759 }
1729 tcc_free(s);1760 sym_free(s);
1730 s = ss;1761 s = ss;
1731 }1762 }
1732 *ptop = b;1763 *ptop = b;
@@ -2194,11 +2225,13 @@ static inline int tok_ext_size(int t) · 461 unmodified lines
2194 case TOK_CUINT:2225 case TOK_CUINT:
2195 case TOK_CCHAR:2226 case TOK_CCHAR:
2196 case TOK_LCHAR:2227 case TOK_LCHAR:
2197 case TOK_STR:
2198 case TOK_LSTR:
2199 case TOK_CFLOAT:2228 case TOK_CFLOAT:
2200 case TOK_LINENUM:2229 case TOK_LINENUM:
2230 return 1;
2231 case TOK_STR:
2232 case TOK_LSTR:
2201 case TOK_PPNUM:2233 case TOK_PPNUM:
2234 error("unsupported token");
2202 return 1;2235 return 1;
2203 case TOK_CDOUBLE:2236 case TOK_CDOUBLE:
2204 case TOK_CLLONG:2237 case TOK_CLLONG:
@@ -2223,48 +2256,6 @@ static inline void tok_str_new(TokenString *s) · 18 unmodified lines
2223 2256
2224static void tok_str_free(int *str)2257static void tok_str_free(int *str)
2225{2258{
2226 const int *p;
2227 CString *cstr;
2228 int t;
2229
2230 p = str;
2231 for(;;) {
2232 t = *p;
2233 /* NOTE: we test zero separately so that GCC can generate a
2234 table for the following switch */
2235 if (t == 0)
2236 break;
2237 switch(t) {
2238 case TOK_CINT:
2239 case TOK_CUINT:
2240 case TOK_CCHAR:
2241 case TOK_LCHAR:
2242 case TOK_CFLOAT:
2243 case TOK_LINENUM:
2244 p += 2;
2245 break;
2246 case TOK_PPNUM:
2247 case TOK_STR:
2248 case TOK_LSTR:
2249 /* XXX: use a macro to be portable on 64 bit ? */
2250 cstr = (CString *)p[1];
2251 cstr_free(cstr);
2252 tcc_free(cstr);
2253 p += 2;
2254 break;
2255 case TOK_CDOUBLE:
2256 case TOK_CLLONG:
2257 case TOK_CULLONG:
2258 p += 3;
2259 break;
2260 case TOK_CLDOUBLE:
2261 p += 1 + (LDOUBLE_SIZE / 4);
2262 break;
2263 default:
2264 p++;
2265 break;
2266 }
2267 }
2268 tcc_free(str);2259 tcc_free(str);
2269}2260}
2270 2261
@@ -2320,7 +2311,22 @@ static void tok_str_add2(TokenString *s, int t, CValue *cv) · 49 unmodified lines
2320 case TOK_PPNUM:2311 case TOK_PPNUM:
2321 case TOK_STR:2312 case TOK_STR:
2322 case TOK_LSTR:2313 case TOK_LSTR:
2323 str[len++] = (int)cstr_dup(cv->cstr);2314 {
2315 int nb_words;
2316 CString *cstr;
2317
2318 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
2319 while ((len + nb_words) > s->allocated_len)
2320 str = tok_str_realloc(s);
2321 cstr = (CString *)(str + len);
2322 cstr->data = NULL;
2323 cstr->size = cv->cstr->size;
2324 cstr->data_allocated = NULL;
2325 cstr->size_allocated = cstr->size;
2326 memcpy((char *)cstr + sizeof(CString),
2327 cv->cstr->data, cstr->size);
2328 len += nb_words;
2329 }
2324 break;2330 break;
2325 case TOK_CDOUBLE:2331 case TOK_CDOUBLE:
2326 case TOK_CLLONG:2332 case TOK_CLLONG:
@@ -2386,10 +2392,14 @@ static void tok_str_add_tok(TokenString *s) · 59 unmodified lines
2386 case TOK_LCHAR: \2392 case TOK_LCHAR: \
2387 case TOK_CFLOAT: \2393 case TOK_CFLOAT: \
2388 case TOK_LINENUM: \2394 case TOK_LINENUM: \
2395 cv.tab[0] = *p++; \
2396 break; \
2389 case TOK_STR: \2397 case TOK_STR: \
2390 case TOK_LSTR: \2398 case TOK_LSTR: \
2391 case TOK_PPNUM: \2399 case TOK_PPNUM: \
2392 cv.tab[0] = *p++; \2400 cv.cstr = (CString *)p; \
2401 cv.cstr->data = (char *)p + sizeof(CString);\
2402 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2393 break; \2403 break; \
2394 case TOK_CDOUBLE: \2404 case TOK_CDOUBLE: \
2395 case TOK_CLLONG: \2405 case TOK_CLLONG: \
@@ -2450,7 +2460,7 @@ static void free_defines(Sym *b) · 54 unmodified lines
2450 v = top->v;2460 v = top->v;
2451 if (v >= TOK_IDENT && v < tok_ident)2461 if (v >= TOK_IDENT && v < tok_ident)
2452 table_ident[v - TOK_IDENT]->sym_define = NULL;2462 table_ident[v - TOK_IDENT]->sym_define = NULL;
2453 tcc_free(top);2463 sym_free(top);
2454 top = top1;2464 top = top1;
2455 }2465 }
2456 define_stack = b;2466 define_stack = b;
@@ -2503,7 +2513,7 @@ static void label_pop(Sym **ptop, Sym *slast) · 46 unmodified lines
2503 }2513 }
2504 /* remove label */2514 /* remove label */
2505 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;2515 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2506 tcc_free(s);2516 sym_free(s);
2507 }2517 }
2508 *ptop = slast;2518 *ptop = slast;
2509}2519}
@@ -2619,17 +2629,37 @@ static void parse_define(void) · 109 unmodified lines
2619 define_push(v, t, str.str, first);2629 define_push(v, t, str.str, first);
2620}2630}
2621 2631
2632static inline int hash_cached_include(int type, const char *filename)
2633{
2634 const unsigned char *s;
2635 unsigned int h;
2636
2637 h = TOK_HASH_INIT;
2638 h = TOK_HASH_FUNC(h, type);
2639 s = filename;
2640 while (*s) {
2641 h = TOK_HASH_FUNC(h, *s);
2642 s++;
2643 }
2644 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
2645 return h;
2646}
2647
2622/* XXX: use a token or a hash table to accelerate matching ? */2648/* XXX: use a token or a hash table to accelerate matching ? */
2623static CachedInclude *search_cached_include(TCCState *s1,2649static CachedInclude *search_cached_include(TCCState *s1,
2624 int type, const char *filename)2650 int type, const char *filename)
2625{2651{
2626 CachedInclude *e;2652 CachedInclude *e;
2627 int i;2653 int i, h;
2628 2654 h = hash_cached_include(type, filename);
2629 for(i = 0;i < s1->nb_cached_includes; i++) {2655 i = s1->cached_includes_hash[h];
2630 e = s1->cached_includes[i];2656 for(;;) {
2657 if (i == 0)
2658 break;
2659 e = s1->cached_includes[i - 1];
2631 if (e->type == type && !strcmp(e->filename, filename))2660 if (e->type == type && !strcmp(e->filename, filename))
2632 return e;2661 return e;
2662 i = e->hash_next;
2633 }2663 }
2634 return NULL;2664 return NULL;
2635}2665}
@@ -2638,6 +2668,7 @@ static inline void add_cached_include(TCCState *s1, int type, · 2 unmodified lines
2638 const char *filename, int ifndef_macro)2668 const char *filename, int ifndef_macro)
2639{2669{
2640 CachedInclude *e;2670 CachedInclude *e;
2671 int h;
2641 2672
2642 if (search_cached_include(s1, type, filename))2673 if (search_cached_include(s1, type, filename))
2643 return;2674 return;
@@ -2651,6 +2682,10 @@ static inline void add_cached_include(TCCState *s1, int type, · 7 unmodified lines
2651 strcpy(e->filename, filename);2682 strcpy(e->filename, filename);
2652 e->ifndef_macro = ifndef_macro;2683 e->ifndef_macro = ifndef_macro;
2653 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);2684 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2685 /* add in hash table */
2686 h = hash_cached_include(type, filename);
2687 e->hash_next = s1->cached_includes_hash[h];
2688 s1->cached_includes_hash[h] = s1->nb_cached_includes;
2654}2689}
2655 2690
2656/* is_bof is true if first non space token at beginning of file */2691/* is_bof is true if first non space token at beginning of file */
@@ -3989,7 +4024,7 @@ static int macro_subst_tok(TokenString *tok_str, · 1332 unmodified lines
3989 while (sa) {4024 while (sa) {
3990 sa1 = sa->prev;4025 sa1 = sa->prev;
3991 tok_str_free((int *)sa->c);4026 tok_str_free((int *)sa->c);
3992 tcc_free(sa);4027 sym_free(sa);
3993 sa = sa1;4028 sa = sa1;
3994 }4029 }
3995 mstr_allocated = 1;4030 mstr_allocated = 1;
@@ -3999,7 +4034,7 @@ static int macro_subst_tok(TokenString *tok_str, · 3 unmodified lines
3999 /* pop nested defined symbol */4034 /* pop nested defined symbol */
4000 sa1 = *nested_list;4035 sa1 = *nested_list;
4001 *nested_list = sa1->prev;4036 *nested_list = sa1->prev;
4002 tcc_free(sa1);4037 sym_free(sa1);
4003 if (mstr_allocated)4038 if (mstr_allocated)
4004 tok_str_free(mstr);4039 tok_str_free(mstr);
4005 }4040 }
@@ -9409,10 +9444,12 @@ int tcc_relocate(TCCState *s1) · 5403 unmodified lines
9409 9444
9410 tcc_add_runtime(s1);9445 tcc_add_runtime(s1);
9411 9446
9412 build_got_entries(s1);
9413
9414 relocate_common_syms();9447 relocate_common_syms();
9415 9448
9449 tcc_add_linker_symbols(s1);
9450
9451 build_got_entries(s1);
9452
9416 /* compute relocation address : section are relocated in place. We9453 /* compute relocation address : section are relocated in place. We
9417 also alloc the bss space */9454 also alloc the bss space */
9418 for(i = 1; i < s1->nb_sections; i++) {9455 for(i = 1; i < s1->nb_sections; i++) {

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