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
289289 inclusion if the include file is protected by #ifndef ... #endif */
290290 typedef struct CachedInclude {
291291 int ifndef_macro;
292+ int hash_next; /* -1 if none */
292293 char type; /* '"' or '>' to give include type */
293294 char filename[1]; /* path specified in #include */
294295 } CachedInclude;
295296
297+#define CACHED_INCLUDES_HASH_SIZE 512
298+
296299 /* parser */
297300 static struct BufferedFile *file;
298301 static int ch, tok;
@@ -319,7 +322,9 @@ static int parse_flags; · 20 unmodified lines
319322 static Section *text_section, *data_section, *bss_section; /* predefined sections */
320323 static Section *cur_text_section; /* current section where function code is
321324 generated */
325+#ifdef CONFIG_TCC_ASM
322326 static Section *last_text_section; /* to handle .previous asm directive */
327+#endif
323328 /* bound check related sections */
324329 static Section *bounds_section; /* contains global data bound description */
325330 static Section *lbounds_section; /* contains local data bound description */
@@ -352,6 +357,9 @@ static char *funcname; · 26 unmodified lines
352357 static Sym *global_stack, *local_stack;
353358 static Sym *define_stack;
354359 static Sym *global_label_stack, *local_label_stack;
360+/* symbol allocator */
361+#define SYM_POOL_NB (8192 / sizeof(Sym))
362+static Sym *sym_free_first;
355363
356364 static SValue vstack[VSTACK_SIZE], *vtop;
357365 /* some predefined types */
@@ -473,6 +481,9 @@ struct TCCState { · 115 unmodified lines
473481
474482 /* see ifdef_stack_ptr */
475483 int ifdef_stack[IFDEF_STACK_SIZE];
484+
485+ /* see cached_includes */
486+ int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
476487 };
477488
478489 /* The current value can be: */
@@ -1048,6 +1059,41 @@ static void dynarray_add(void ***ptab, int *nb_ptr, void *data) · 569 unmodified lines
10481059 *nb_ptr = nb;
10491060 }
10501061
1062+/* symbol allocator */
1063+static 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+
1081+static 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+
1091+static inline void sym_free(Sym *sym)
1092+{
1093+ sym->next = sym_free_first;
1094+ sym_free_first = sym;
1095+}
1096+
10511097 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
10521098 {
10531099 Section *sec;
@@ -1433,7 +1479,7 @@ static void cstr_realloc(CString *cstr, int new_size) · 379 unmodified lines
14331479 }
14341480
14351481 /* add a byte */
1436−static void cstr_ccat(CString *cstr, int ch)
1482+static inline void cstr_ccat(CString *cstr, int ch)
14371483 {
14381484 int size;
14391485 size = cstr->size + 1;
@@ -1480,21 +1526,6 @@ static void cstr_free(CString *cstr) · 40 unmodified lines
14801526
14811527 #define cstr_reset(cstr) cstr_free(cstr)
14821528
1483−static 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−
14981529 /* XXX: unicode ? */
14991530 static void add_char(CString *cstr, int c)
15001531 {
@@ -1618,7 +1649,7 @@ char *get_tok_str(int v, CValue *cv) · 117 unmodified lines
16181649 static Sym *sym_push2(Sym **ps, int v, int t, int c)
16191650 {
16201651 Sym *s;
1621− s = tcc_malloc(sizeof(Sym));
1652+ s = sym_malloc();
16221653 s->v = v;
16231654 s->type.t = t;
16241655 s->c = c;
@@ -1726,7 +1757,7 @@ static void sym_pop(Sym **ptop, Sym *b) · 101 unmodified lines
17261757 ps = &ts->sym_identifier;
17271758 *ps = s->prev_tok;
17281759 }
1729− tcc_free(s);
1760+ sym_free(s);
17301761 s = ss;
17311762 }
17321763 *ptop = b;
@@ -2194,11 +2225,13 @@ static inline int tok_ext_size(int t) · 461 unmodified lines
21942225 case TOK_CUINT:
21952226 case TOK_CCHAR:
21962227 case TOK_LCHAR:
2197− case TOK_STR:
2198− case TOK_LSTR:
21992228 case TOK_CFLOAT:
22002229 case TOK_LINENUM:
2230+ return 1;
2231+ case TOK_STR:
2232+ case TOK_LSTR:
22012233 case TOK_PPNUM:
2234+ error("unsupported token");
22022235 return 1;
22032236 case TOK_CDOUBLE:
22042237 case TOK_CLLONG:
@@ -2223,48 +2256,6 @@ static inline void tok_str_new(TokenString *s) · 18 unmodified lines
22232256
22242257 static void tok_str_free(int *str)
22252258 {
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− }
22682259 tcc_free(str);
22692260 }
22702261
@@ -2320,7 +2311,22 @@ static void tok_str_add2(TokenString *s, int t, CValue *cv) · 49 unmodified lines
23202311 case TOK_PPNUM:
23212312 case TOK_STR:
23222313 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+ }
23242330 break;
23252331 case TOK_CDOUBLE:
23262332 case TOK_CLLONG:
@@ -2386,10 +2392,14 @@ static void tok_str_add_tok(TokenString *s) · 59 unmodified lines
23862392 case TOK_LCHAR: \
23872393 case TOK_CFLOAT: \
23882394 case TOK_LINENUM: \
2395+ cv.tab[0] = *p++; \
2396+ break; \
23892397 case TOK_STR: \
23902398 case TOK_LSTR: \
23912399 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;\
23932403 break; \
23942404 case TOK_CDOUBLE: \
23952405 case TOK_CLLONG: \
@@ -2450,7 +2460,7 @@ static void free_defines(Sym *b) · 54 unmodified lines
24502460 v = top->v;
24512461 if (v >= TOK_IDENT && v < tok_ident)
24522462 table_ident[v - TOK_IDENT]->sym_define = NULL;
2453− tcc_free(top);
2463+ sym_free(top);
24542464 top = top1;
24552465 }
24562466 define_stack = b;
@@ -2503,7 +2513,7 @@ static void label_pop(Sym **ptop, Sym *slast) · 46 unmodified lines
25032513 }
25042514 /* remove label */
25052515 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2506− tcc_free(s);
2516+ sym_free(s);
25072517 }
25082518 *ptop = slast;
25092519 }
@@ -2619,17 +2629,37 @@ static void parse_define(void) · 109 unmodified lines
26192629 define_push(v, t, str.str, first);
26202630 }
26212631
2632+static 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+
26222648 /* XXX: use a token or a hash table to accelerate matching ? */
26232649 static CachedInclude *search_cached_include(TCCState *s1,
26242650 int type, const char *filename)
26252651 {
26262652 CachedInclude *e;
2627− int i;
2628−
2629− for(i = 0;i < s1->nb_cached_includes; i++) {
2630− e = s1->cached_includes[i];
2653+ int i, h;
2654+ h = hash_cached_include(type, filename);
2655+ i = s1->cached_includes_hash[h];
2656+ for(;;) {
2657+ if (i == 0)
2658+ break;
2659+ e = s1->cached_includes[i - 1];
26312660 if (e->type == type && !strcmp(e->filename, filename))
26322661 return e;
2662+ i = e->hash_next;
26332663 }
26342664 return NULL;
26352665 }
@@ -2638,6 +2668,7 @@ static inline void add_cached_include(TCCState *s1, int type, · 2 unmodified lines
26382668 const char *filename, int ifndef_macro)
26392669 {
26402670 CachedInclude *e;
2671+ int h;
26412672
26422673 if (search_cached_include(s1, type, filename))
26432674 return;
@@ -2651,6 +2682,10 @@ static inline void add_cached_include(TCCState *s1, int type, · 7 unmodified lines
26512682 strcpy(e->filename, filename);
26522683 e->ifndef_macro = ifndef_macro;
26532684 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;
26542689 }
26552690
26562691 /* 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
39894024 while (sa) {
39904025 sa1 = sa->prev;
39914026 tok_str_free((int *)sa->c);
3992− tcc_free(sa);
4027+ sym_free(sa);
39934028 sa = sa1;
39944029 }
39954030 mstr_allocated = 1;
@@ -3999,7 +4034,7 @@ static int macro_subst_tok(TokenString *tok_str, · 3 unmodified lines
39994034 /* pop nested defined symbol */
40004035 sa1 = *nested_list;
40014036 *nested_list = sa1->prev;
4002− tcc_free(sa1);
4037+ sym_free(sa1);
40034038 if (mstr_allocated)
40044039 tok_str_free(mstr);
40054040 }
@@ -9409,10 +9444,12 @@ int tcc_relocate(TCCState *s1) · 5403 unmodified lines
94099444
94109445 tcc_add_runtime(s1);
94119446
9412− build_got_entries(s1);
9413−
94149447 relocate_common_syms();
94159448
9449+ tcc_add_linker_symbols(s1);
9450+
9451+ build_got_entries(s1);
9452+
94169453 /* compute relocation address : section are relocated in place. We
94179454 also alloc the bss space */
94189455 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