History

added hash tables - add -o option for debug output

bellard committed 25 years ago

Commit b9455a71 parent 44b5c971 file · +143 −78Browse files

Unified · Split

tcc.c+143−78
@@ -34,14 +34,18 @@ · 33 unmodified lines
3434 #define STRING_MAX_SIZE 1024
3535 #define INCLUDE_PATHS_MAX 32
3636
37+#define TOK_HASH_SIZE 521
38+#define TOK_ALLOC_INCR 256 /* must be a power of two */
39+#define SYM_HASH_SIZE 263
40+
3741 /* number of available temporary registers */
3842 #define NB_REGS 3
39−/* defined if function parameters must be evaluated in revert order */
43+/* defined if function parameters must be evaluated in reverse order */
4044 #define INVERT_FUNC_PARAMS
4145
4246 /* token symbol management */
4347 typedef struct TokenSym {
44− struct TokenSym *next;
48+ struct TokenSym *hash_next;
4549 int tok; /* token number */
4650 int len;
4751 char str[1];
@@ -54,8 +58,14 @@ typedef struct Sym { · 6 unmodified lines
5458 int c; /* associated number */
5559 struct Sym *next; /* next related symbol */
5660 struct Sym *prev; /* prev symbol in stack */
61+ struct Sym *hash_next; /* next symbol in hash table */
5762 } Sym;
5863
64+typedef struct SymStack {
65+ struct Sym *top;
66+ struct Sym *hash[SYM_HASH_SIZE];
67+} SymStack;
68+
5969 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
6070 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
6171
@@ -87,10 +97,12 @@ typedef struct { · 25 unmodified lines
8797 FILE *file;
8898 int tok, tok1, tokc, rsym, anon_sym,
8999 prog, ind, loc, glo, vt, vc, const_wanted, line_num;
90−TokenSym *first_ident;
100+int tok_ident;
101+TokenSym **table_ident;
102+TokenSym *hash_ident[521];
91103 char token_buf[STRING_MAX_SIZE + 1];
92104 char *filename, *funcname;
93−Sym *define_stack, *global_stack, *local_stack, *label_stack;
105+SymStack define_stack, global_stack, local_stack, label_stack;
94106
95107 int vstack[VSTACK_SIZE], *vstack_ptr;
96108 int *macro_ptr, *macro_ptr_allocated;
@@ -283,14 +295,14 @@ void *dlsym(void *handle, char *symbol) · 186 unmodified lines
283295
284296 #endif
285297
286−int isid(c)
298+inline int isid(c)
287299 {
288− return (c >= 'a' & c <= 'z') |
289− (c >= 'A' & c <= 'Z') |
300+ return (c >= 'a' && c <= 'z') ||
301+ (c >= 'A' && c <= 'Z') ||
290302 c == '_';
291303 }
292304
293−int isnum(c)
305+inline int isnum(c)
294306 {
295307 return c >= '0' & c <= '9';
296308 }
@@ -341,31 +353,40 @@ void test_lvalue() · 44 unmodified lines
341353
342354 TokenSym *tok_alloc(char *str, int len)
343355 {
344− TokenSym *ts, **pts;
345− int t;
356+ TokenSym *ts, **pts, **ptable;
357+ int h, i;
346358
347359 if (len <= 0)
348360 len = strlen(str);
349−
350− t = TOK_IDENT;
351− pts = &first_ident;
361+ h = 1;
362+ for(i=0;i<len;i++)
363+ h = ((h << 8) | (str[i] & 0xff)) % TOK_HASH_SIZE;
364+
365+ pts = &hash_ident[h];
352366 while (1) {
353367 ts = *pts;
354368 if (!ts)
355369 break;
356370 if (ts->len == len && !memcmp(ts->str, str, len))
357371 return ts;
358− t++;
359− pts = &(ts->next);
372+ pts = &(ts->hash_next);
373+ }
374+ /* expand token table if needed */
375+ i = tok_ident - TOK_IDENT;
376+ if ((i % TOK_ALLOC_INCR) == 0) {
377+ ptable = realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
378+ if (!ptable)
379+ error("memory full");
380+ table_ident = ptable;
360381 }
361382 ts = malloc(sizeof(TokenSym) + len);
362383 if (!ts)
363384 error("memory full");
364− ts->tok = t;
385+ table_ident[i] = ts;
386+ ts->tok = tok_ident++;
365387 ts->len = len;
366− ts->next = NULL;
388+ ts->hash_next = NULL;
367389 memcpy(ts->str, str, len + 1);
368−
369390 *pts = ts;
370391 return ts;
371392 }
@@ -425,40 +446,65 @@ char *get_tok_str(int v, int c) · 53 unmodified lines
425446 *p++ = v;
426447 *p = '\0';
427448 return buf;
449+ } else if (v < tok_ident) {
450+ return table_ident[v - TOK_IDENT]->str;
428451 } else {
429− ts = first_ident;
430− while (ts != NULL) {
431− if (ts->tok == v)
432− return ts->str;
433− ts = ts->next;
434− }
452+ /* should never happen */
435453 return NULL;
436454 }
437455 }
438456
457+/* push, without hashing */
458+Sym *sym_push2(Sym **ps, int v, int t, int c)
459+{
460+ Sym *s;
461+ s = malloc(sizeof(Sym));
462+ if (!s)
463+ error("memory full");
464+ s->v = v;
465+ s->t = t;
466+ s->c = c;
467+ s->next = NULL;
468+ /* add in stack */
469+ s->prev = *ps;
470+ *ps = s;
471+ return s;
472+}
473+
439474 /* find a symbol and return its associated structure. 's' is the top
440475 of the symbol stack */
441−Sym *sym_find1(Sym *s, int v)
476+Sym *sym_find2(Sym *s, int v)
442477 {
443478 while (s) {
444479 if (s->v == v)
445480 return s;
446481 s = s->prev;
447482 }
448− return 0;
483+ return NULL;
449484 }
450485
451−Sym *sym_push1(Sym **ps, int v, int t, int c)
486+/* find a symbol and return its associated structure. 'st' is the
487+ symbol stack */
488+Sym *sym_find1(SymStack *st, int v)
452489 {
453490 Sym *s;
454− s = malloc(sizeof(Sym));
455− if (!s)
456− error("memory full");
457− s->v = v;
458− s->t = t;
459− s->c = c;
460− s->next = NULL;
461− s->prev = *ps;
491+
492+ s = st->hash[v % SYM_HASH_SIZE];
493+ while (s) {
494+ if (s->v == v)
495+ return s;
496+ s = s->hash_next;
497+ }
498+ return 0;
499+}
500+
501+Sym *sym_push1(SymStack *st, int v, int t, int c)
502+{
503+ Sym *s, **ps;
504+ s = sym_push2(&st->top, v, t, c);
505+ /* add in hash table */
506+ ps = &st->hash[s->v % SYM_HASH_SIZE];
507+ s->hash_next = *ps;
462508 *ps = s;
463509 return s;
464510 }
@@ -467,33 +513,35 @@ Sym *sym_push1(Sym **ps, int v, int t, int c) · 2 unmodified lines
467513 Sym *sym_find(int v)
468514 {
469515 Sym *s;
470− s = sym_find1(local_stack, v);
516+ s = sym_find1(&local_stack, v);
471517 if (!s)
472− s = sym_find1(global_stack, v);
518+ s = sym_find1(&global_stack, v);
473519 return s;
474520 }
475521
476522 /* push a given symbol on the symbol stack */
477523 Sym *sym_push(int v, int t, int c)
478524 {
479− if (local_stack)
525+ if (local_stack.top)
480526 return sym_push1(&local_stack, v, t, c);
481527 else
482528 return sym_push1(&global_stack, v, t, c);
483529 }
484530
485531 /* pop symbols until top reaches 'b' */
486−void sym_pop(Sym **ps, Sym *b)
532+void sym_pop(SymStack *st, Sym *b)
487533 {
488534 Sym *s, *ss;
489535
490− s = *ps;
536+ s = st->top;
491537 while(s != b) {
492538 ss = s->prev;
539+ /* free hash table entry */
540+ st->hash[s->v % SYM_HASH_SIZE] = s->hash_next;
493541 free(s);
494542 s = ss;
495543 }
496− *ps = b;
544+ st->top = b;
497545 }
498546
499547 int ch, ch1;
@@ -640,7 +688,7 @@ int expr_preprocess() · 140 unmodified lines
640688 t = tok;
641689 if (t == '(')
642690 next_nomacro();
643− c = sym_find1(define_stack, tok) != 0;
691+ c = sym_find1(&define_stack, tok) != 0;
644692 if (t == '(')
645693 next_nomacro();
646694 tok = TOK_NUM;
@@ -748,7 +796,7 @@ void preprocess() · 101 unmodified lines
748796 s->next = first;
749797 } else if (tok == TOK_UNDEF) {
750798 next_nomacro();
751− s = sym_find1(define_stack, tok);
799+ s = sym_find1(&define_stack, tok);
752800 /* undefine symbol by putting an invalid name */
753801 if (s)
754802 s->v = 0;
@@ -823,7 +871,7 @@ void preprocess() · 68 unmodified lines
823871 c = 0;
824872 do_ifdef:
825873 next_nomacro();
826− c = (sym_find1(define_stack, tok) != 0) ^ c;
874+ c = (sym_find1(&define_stack, tok) != 0) ^ c;
827875 do_if:
828876 if (ifdef_stack_ptr >= ifdef_stack + IFDEF_STACK_SIZE)
829877 error("memory full");
@@ -1094,7 +1142,7 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 264 unmodified lines
10941142 t = *macro_str++;
10951143 if (!t)
10961144 break;
1097− s = sym_find1(args, t);
1145+ s = sym_find2(args, t);
10981146 if (s) {
10991147 token_buf[0] = '\0';
11001148 st = (int *)s->c;
@@ -1122,7 +1170,7 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 21 unmodified lines
11221170 } else if (t == TOK_NUM || t == TOK_CCHAR || t == TOK_STR) {
11231171 tok_add2(&str, &len, t, *macro_ptr++);
11241172 } else {
1125− s = sym_find1(args, t);
1173+ s = sym_find2(args, t);
11261174 if (s) {
11271175 st = (int *)s->c;
11281176 /* if '##' is present before or after , no arg substitution */
@@ -1199,7 +1247,7 @@ int *macro_twosharps(int *macro_str) · 70 unmodified lines
11991247 void macro_subst(int **tok_str, int *tok_len,
12001248 Sym **nested_list, int *macro_str)
12011249 {
1202− Sym *s, *args, *sa;
1250+ Sym *s, *args, *sa, *sa1;
12031251 int *str, parlevel, len, *mstr, t, *saved_macro_ptr;
12041252 int mstr_allocated, *macro_str1;
12051253
@@ -1228,10 +1276,10 @@ void macro_subst(int **tok_str, int *tok_len, · 22 unmodified lines
12281276 } else if (tok == TOK___TIME__) {
12291277 tok_add2(tok_str, tok_len, TOK_STR,
12301278 (int)tok_alloc("00:00:00", 0));
1231− } else if ((s = sym_find1(define_stack, tok)) != NULL) {
1279+ } else if ((s = sym_find1(&define_stack, tok)) != NULL) {
12321280 /* if symbol is a macro, prepare substitution */
12331281 /* if nested substitution, do nothing */
1234− if (sym_find1(*nested_list, tok))
1282+ if (sym_find2(*nested_list, tok))
12351283 goto no_subst;
12361284 mstr = (int *)s->c;
12371285 mstr_allocated = 0;
@@ -1270,7 +1318,7 @@ void macro_subst(int **tok_str, int *tok_len, · 32 unmodified lines
12701318 next_nomacro();
12711319 }
12721320 tok_add(&str, &len, 0);
1273− sym_push1(&args, sa->v & ~SYM_FIELD, 0, (int)str);
1321+ sym_push2(&args, sa->v & ~SYM_FIELD, 0, (int)str);
12741322 if (tok != ',')
12751323 break;
12761324 next_nomacro();
@@ -1283,15 +1331,19 @@ void macro_subst(int **tok_str, int *tok_len, · 6 unmodified lines
12831331 /* free memory */
12841332 sa = args;
12851333 while (sa) {
1334+ sa1 = sa->prev;
12861335 free((int *)sa->c);
1287− sa = sa->prev;
1336+ free(sa);
1337+ sa = sa1;
12881338 }
1289− sym_pop(&args, NULL);
12901339 mstr_allocated = 1;
12911340 }
1292− sym_push1(nested_list, s->v, 0, 0);
1341+ sym_push2(nested_list, s->v, 0, 0);
12931342 macro_subst(tok_str, tok_len, nested_list, mstr);
1294− sym_pop(nested_list, (*nested_list)->prev);
1343+ /* pop nested defined symbol */
1344+ sa1 = *nested_list;
1345+ *nested_list = sa1->prev;
1346+ free(sa1);
12951347 if (mstr_allocated)
12961348 free(mstr);
12971349 } else {
@@ -2425,7 +2477,7 @@ void unary() · 1127 unmodified lines
24252477 #ifdef INVERT_FUNC_PARAMS
24262478 {
24272479 int *str, len, parlevel, *saved_macro_ptr;
2428− Sym *args;
2480+ Sym *args, *s1;
24292481
24302482 /* read each argument and store it on a stack */
24312483 /* XXX: merge it with macro args ? */
@@ -2445,7 +2497,7 @@ void unary() · 13 unmodified lines
24452497 }
24462498 tok_add(&str, &len, -1); /* end of file added */
24472499 tok_add(&str, &len, 0);
2448− sym_push1(&args, 0, 0, (int)str);
2500+ sym_push2(&args, 0, 0, (int)str);
24492501 if (tok != ',')
24502502 break;
24512503 next();
@@ -2462,8 +2514,10 @@ void unary() · 10 unmodified lines
24622514 next();
24632515 expr_eq();
24642516 gfunc_param();
2517+ s1 = args->prev;
24652518 free((int *)args->c);
2466− sym_pop(&args, args->prev);
2519+ free(args);
2520+ args = s1;
24672521 }
24682522 macro_ptr = saved_macro_ptr;
24692523 /* restore token */
@@ -2692,7 +2746,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 222 unmodified lines
26922746 } else if (tok == '{') {
26932747 next();
26942748 /* declarations */
2695− s = local_stack;
2749+ s = local_stack.top;
26962750 while (tok != '}') {
26972751 decl(VT_LOCAL);
26982752 block(bsym, csym, case_sym, def_sym, case_reg);
@@ -2810,7 +2864,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 111 unmodified lines
28102864 } else
28112865 if (tok == TOK_GOTO) {
28122866 next();
2813− s = sym_find1(label_stack, tok);
2867+ s = sym_find1(&label_stack, tok);
28142868 /* put forward definition if needed */
28152869 if (!s)
28162870 s = sym_push1(&label_stack, tok, VT_FORWARD, 0);
@@ -2827,7 +2881,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 10 unmodified lines
28272881 if (tok == ':') {
28282882 next();
28292883 /* label case */
2830− s = sym_find1(label_stack, b);
2884+ s = sym_find1(&label_stack, b);
28312885 if (s) {
28322886 if (!(s->t & VT_FORWARD))
28332887 error("multiple defined label");
@@ -2988,7 +3042,6 @@ void decl_assign(int t, int c, int first) · 154 unmodified lines
29883042 } else {
29893043 if ((t & VT_VALMASK) == VT_CONST) {
29903044 v = expr_const();
2991− printf("v=%d\n", v);
29923045 if (t & VT_BYTE)
29933046 *(char *)c = v;
29943047 else if (t & VT_SHORT)
@@ -3135,11 +3188,20 @@ void open_dll(char *libname) · 140 unmodified lines
31353188 error((char *)dlerror());
31363189 }
31373190
3191+/* output a binary file (for testing) */
3192+void build_exe(char *filename)
3193+{
3194+ FILE *f;
3195+ f = fopen(filename, "w");
3196+ fwrite((void *)prog, 1, ind - prog, f);
3197+ fclose(f);
3198+}
3199+
31383200 int main(int argc, char **argv)
31393201 {
31403202 Sym *s;
31413203 int (*t)();
3142− char *p, *r;
3204+ char *p, *r, *outfile;
31433205 int optind;
31443206
31453207 include_paths[0] = "/usr/include";
@@ -3148,6 +3210,7 @@ int main(int argc, char **argv) · 2 unmodified lines
31483210 nb_include_paths = 3;
31493211
31503212 /* add all tokens */
3213+ tok_ident = TOK_IDENT;
31513214 p = "int\0void\0char\0if\0else\0while\0break\0return\0for\0extern\0static\0unsigned\0goto\0do\0continue\0switch\0case\0const\0volatile\0long\0register\0signed\0auto\0inline\0restrict\0float\0double\0short\0struct\0union\0typedef\0default\0enum\0sizeof\0define\0include\0ifdef\0ifndef\0elif\0endif\0defined\0undef\0error\0line\0__LINE__\0__FILE__\0__DATE__\0__TIME__\0__VA_ARGS__\0__func__\0main\0";
31523215 while (*p) {
31533216 r = p;
@@ -3163,8 +3226,10 @@ int main(int argc, char **argv) · 9 unmodified lines
31633226 #endif
31643227
31653228 optind = 1;
3229+ outfile = NULL;
31663230 while (1) {
31673231 if (optind >= argc) {
3232+ show_help:
31683233 printf("tcc version 0.9 - Tiny C Compiler - Copyright (C) 2001 Fabrice Bellard\n"
31693234 "usage: tcc [-Idir] [-Dsym] [-llib] infile [infile_arg...]\n");
31703235 return 1;
@@ -3172,6 +3237,7 @@ int main(int argc, char **argv) · 1 unmodified line
31723237 r = argv[optind];
31733238 if (r[0] != '-')
31743239 break;
3240+ optind++;
31753241 if (r[1] == 'I') {
31763242 if (nb_include_paths >= INCLUDE_PATHS_MAX)
31773243 error("too many include paths");
@@ -3180,11 +3246,15 @@ int main(int argc, char **argv) · 2 unmodified lines
31803246 define_symbol(r + 2);
31813247 } else if (r[1] == 'l') {
31823248 open_dll(r + 2);
3249+ } else if (r[1] == 'o') {
3250+ /* currently, only for testing, so not documented */
3251+ if (optind >= argc)
3252+ goto show_help;
3253+ outfile = argv[optind++];
31833254 } else {
31843255 fprintf(stderr, "invalid option -- '%s'\n", r);
31853256 exit(1);
31863257 }
3187− optind++;
31883258 }
31893259
31903260 filename = argv[optind];
@@ -3210,23 +3280,18 @@ int main(int argc, char **argv) · 19 unmodified lines
32103280 decl(VT_CONST);
32113281 if (tok != -1)
32123282 expect("declaration");
3213−#ifdef TEST
3214− {
3215− FILE *f;
3216− f = fopen(argv[optind + 1], "w");
3217− fwrite((void *)prog, 1, ind - prog, f);
3218− fclose(f);
3283+ if (outfile) {
3284+ build_exe(outfile);
32193285 return 0;
3220− }
3221−#else
3222− s = sym_find(TOK_MAIN);
3223− if (!s)
3224− error("main() not defined");
3225− t = (int (*)())s->c;
3286+ } else {
3287+ s = sym_find(TOK_MAIN);
3288+ if (!s)
3289+ error("main() not defined");
3290+ t = (int (*)())s->c;
32263291 #ifdef PROFILE
3227− return 1;
3292+ return 1;
32283293 #else
3229− return (*t)(argc - optind, argv + optind);
3230−#endif
3294+ return (*t)(argc - optind, argv + optind);
32313295 #endif
3296+ }
32323297 }

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