History

-bench fix

Put total_lines etc. into TCCState. Also, initialize the predefined compiler types for the preprocessor too. tccpe.c: fix BaseOfCode if .init section present (with tcc -b)

grischka committed 6 years ago

Commit ff3b5ee1 parent b476a5f8 files · +83 −75Browse files

Unified · Split

8 files
  1. libtcc.c +15 −20
  2. tcc.h +16 −8
  3. tccelf.c +9 −4
  4. tccgen.c +29 −27
  5. tccpe.c +4 −2
  6. tccpp.c +6 −11
  7. tccrun.c +1 −1
  8. tests/libtcc_test_mt.c +3 −2
libtcc.c+15−20
@@ -64,7 +64,9 @@ · 63 unmodified lines
6464 /* XXX: get rid of this ASAP (or maybe not) */
6565 ST_DATA struct TCCState *tcc_state;
6666
67+#ifdef MEM_DEBUG
6768 static int nb_states;
69+#endif
6870
6971 /********************************************************/
7072 #ifdef _WIN32
@@ -245,10 +247,6 @@ PUB_FUNC char *tcc_strdup(const char *str) · 174 unmodified lines
245247 return ptr;
246248 }
247249
248−PUB_FUNC void tcc_memcheck(void)
249−{
250−}
251−
252250 #else
253251
254252 #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
@@ -631,6 +629,7 @@ ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen) · 376 unmodified lines
631629
632630 ST_FUNC void tcc_close(void)
633631 {
632+ TCCState *s1 = tcc_state;
634633 BufferedFile *bf = file;
635634 if (bf->fd > 0) {
636635 close(bf->fd);
@@ -668,15 +667,10 @@ ST_FUNC int tcc_open(TCCState *s1, const char *filename) · 31 unmodified lines
668667 /* compile the file opened in 'file'. Return non zero if errors. */
669668 static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd)
670669 {
671− tccelf_begin_file(s1);
672−
673670 /* Here we enter the code section where we use the global variables for
674671 parsing and code generation (tccpp.c, tccgen.c, <target>-gen.c).
675672 Other threads need to wait until we're done.
676673
677− Alternatively we could of course pass TCCState *s1 everwhere
678− except that it would look extremly ugly.
679−
680674 Alternatively we could use thread local storage for those global
681675 variables, which may or may not have advantages */
682676
@@ -698,7 +692,9 @@ static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd) · 15 unmodified lines
698692 }
699693
700694 is_asm = !!(filetype & (AFF_TYPE_ASM|AFF_TYPE_ASMPP));
695+ tccelf_begin_file(s1);
701696 preprocess_start(s1, is_asm);
697+ tccgen_init(s1);
702698 if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
703699 tcc_preprocess(s1);
704700 } else if (is_asm) {
@@ -712,13 +708,12 @@ static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd) · 7 unmodified lines
712708 }
713709 }
714710 s1->error_set_jmp_enabled = 0;
715− tccgen_finish(s1, 1);
711+ tccgen_finish(s1);
716712 preprocess_end(s1);
717− tccgen_finish(s1, 2);
713+ tccelf_end_file(s1);
718714
719715 tcc_state = NULL;
720716 POST_SEM();
721− tccelf_end_file(s1);
722717 return s1->nb_errors != 0 ? -1 : 0;
723718 }
724719
@@ -749,9 +744,9 @@ LIBTCCAPI TCCState *tcc_new(void) · 24 unmodified lines
749744 s = tcc_mallocz(sizeof(TCCState));
750745 if (!s)
751746 return NULL;
752− WAIT_SEM();
747+#ifdef MEM_DEBUG
753748 ++nb_states;
754− POST_SEM();
749+#endif
755750
756751 #undef gnu_ext
757752
@@ -960,10 +955,10 @@ LIBTCCAPI void tcc_delete(TCCState *s1) · 202 unmodified lines
960955 #endif
961956
962957 tcc_free(s1);
963− WAIT_SEM();
958+#ifdef MEM_DEBUG
964959 if (0 == --nb_states)
965960 tcc_memcheck();
966− POST_SEM();
961+#endif
967962 }
968963
969964 LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
@@ -1824,7 +1819,7 @@ reparse: · 854 unmodified lines
18241819 else if (*optarg == 't')
18251820 s->dflag = 16;
18261821 else if (isnum(*optarg))
1827− g_debug = atoi(optarg);
1822+ s->g_debug |= atoi(optarg);
18281823 else
18291824 goto unsupported_option;
18301825 break;
@@ -1925,7 +1920,7 @@ reparse: · 94 unmodified lines
19251920 tcc_add_sysinclude_path(s, optarg);
19261921 break;
19271922 case TCC_OPTION_include:
1928− cstr_printf(&s->cmdline_defs, "#include \"%s\"\n", optarg);
1923+ cstr_printf(&s->cmdline_incl, "#include \"%s\"\n", optarg);
19291924 break;
19301925 case TCC_OPTION_nostdinc:
19311926 s->nostdinc = 1;
@@ -2077,7 +2072,7 @@ LIBTCCAPI void tcc_set_options(TCCState *s, const char *r) · 145 unmodified lines
20772072 dynarray_reset(&argv, &argc);
20782073 }
20792074
2080−PUB_FUNC void tcc_print_stats(TCCState *s, unsigned total_time)
2075+PUB_FUNC void tcc_print_stats(TCCState *s1, unsigned total_time)
20812076 {
20822077 if (total_time < 1)
20832078 total_time = 1;
@@ -2085,7 +2080,7 @@ PUB_FUNC void tcc_print_stats(TCCState *s, unsigned total_time) · 1 unmodified line
20852080 total_bytes = 1;
20862081 fprintf(stderr, "* %d idents, %d lines, %d bytes\n"
20872082 "* %0.3f s, %u lines/s, %0.1f MB/s\n",
2088− tok_ident - TOK_IDENT, total_lines, total_bytes,
2083+ total_idents, total_lines, total_bytes,
20892084 (double)total_time/1000,
20902085 (unsigned)total_lines*1000/total_time,
20912086 (double)total_bytes/1000/total_time);
tcc.h+16−8
@@ -128,7 +128,7 @@ extern long double strtold (const char *__nptr, char **__endptr); · 127 unmodified lines
128128 /* #define PP_DEBUG */
129129 /* include file debug */
130130 /* #define INC_DEBUG */
131−/* memory leak debug */
131+/* memory leak debug (only for single threaded usage) */
132132 /* #define MEM_DEBUG */
133133 /* assembler debug */
134134 /* #define ASM_DEBUG */
@@ -889,6 +889,14 @@ struct TCCState { · 754 unmodified lines
889889
890890 int fd, cc; /* used by tcc_load_ldscript */
891891
892+ /* benchmark info */
893+ int total_idents;
894+ int total_lines;
895+ int total_bytes;
896+
897+ /* option -dnum (for general development purposes) */
898+ int g_debug;
899+
892900 /* used by main and tcc_parse_args only */
893901 struct filespec **files; /* files seen on command line */
894902 int nb_files; /* number thereof */
@@ -1192,7 +1200,6 @@ PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line); · 297 unmodified lines
11921200 #define realloc(p, s) use_tcc_realloc(p, s)
11931201 #undef strdup
11941202 #define strdup(s) use_tcc_strdup(s)
1195−PUB_FUNC void tcc_memcheck(void);
11961203 PUB_FUNC void _tcc_error_noabort(const char *fmt, ...);
11971204 PUB_FUNC NORETURN void _tcc_error(const char *fmt, ...);
11981205 PUB_FUNC void _tcc_warning(const char *fmt, ...);
@@ -1277,8 +1284,6 @@ ST_DATA int tok_flags; · 78 unmodified lines
12771284 ST_DATA CString tokcstr; /* current parsed string, if any */
12781285
12791286 /* display benchmark infos */
1280−ST_DATA int total_lines;
1281−ST_DATA int total_bytes;
12821287 ST_DATA int tok_ident;
12831288 ST_DATA TokenSym **table_ident;
12841289
@@ -1360,8 +1365,7 @@ ST_DATA Sym *local_label_stack; · 75 unmodified lines
13601365 ST_DATA Sym *global_label_stack;
13611366 ST_DATA Sym *define_stack;
13621367 ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
1363−ST_DATA SValue __vstack[1+/*to make bcheck happy*/ VSTACK_SIZE], *vtop, *pvtop;
1364−#define vstack (__vstack + 1)
1368+ST_DATA SValue *vtop;
13651369 ST_DATA int rsym, anon_sym, ind, loc;
13661370
13671371 ST_DATA int const_wanted; /* true if constant wanted */
@@ -1371,7 +1375,6 @@ ST_DATA CType func_vt; /* current function return type (used by return instructi · 3 unmodified lines
13711375 ST_DATA int func_var; /* true if current function is variadic */
13721376 ST_DATA int func_vc;
13731377 ST_DATA const char *funcname;
1374−ST_DATA int g_debug;
13751378
13761379 ST_FUNC void tcc_debug_start(TCCState *s1);
13771380 ST_FUNC void tcc_debug_end(TCCState *s1);
@@ -1382,8 +1385,9 @@ ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym); · 4 unmodified lines
13821385 ST_FUNC void tcc_debug_funcend(TCCState *s1, int size);
13831386 ST_FUNC void tcc_debug_line(TCCState *s1);
13841387
1388+ST_FUNC void tccgen_init(TCCState *s1);
13851389 ST_FUNC int tccgen_compile(TCCState *s1);
1386−ST_FUNC void tccgen_finish(TCCState *s1, int f);
1390+ST_FUNC void tccgen_finish(TCCState *s1);
13871391 ST_FUNC void check_vstack(void);
13881392
13891393 ST_INLN int is_float(int t);
@@ -1751,6 +1755,10 @@ ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename) · 361 unmodified lines
17511755 #define tcc_error TCC_SET_STATE(_tcc_error)
17521756 #define tcc_warning TCC_SET_STATE(_tcc_warning)
17531757
1758+#define total_idents TCC_STATE_VAR(total_idents)
1759+#define total_lines TCC_STATE_VAR(total_lines)
1760+#define total_bytes TCC_STATE_VAR(total_bytes)
1761+
17541762 PUB_FUNC void tcc_enter_state(TCCState *s1);
17551763
17561764 /********************************************************/
tccelf.c+9−4
@@ -3318,8 +3318,11 @@ static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed) · 3317 unmodified lines
33183318 if (!as_needed)
33193319 s1->new_undef_sym = 0;
33203320 t = ld_next(s1, filename, sizeof(filename));
3321− if (t != '(')
3322− expect("(");
3321+ if (t != '(') {
3322+ tcc_error_noabort("( expected");
3323+ ret = -1;
3324+ goto lib_parse_error;
3325+ }
33233326 t = ld_next(s1, filename, sizeof(filename));
33243327 for(;;) {
33253328 libname[0] = '\0';
@@ -3408,8 +3411,10 @@ ST_FUNC int tcc_load_ldscript(TCCState *s1, int fd) · 82 unmodified lines
34083411 !strcmp(cmd, "TARGET")) {
34093412 /* ignore some commands */
34103413 t = ld_next(s1, cmd, sizeof(cmd));
3411− if (t != '(')
3412− expect("(");
3414+ if (t != '(') {
3415+ tcc_error_noabort("( expected");
3416+ return -1;
3417+ }
34133418 for(;;) {
34143419 t = ld_next(s1, filename, sizeof(filename));
34153420 if (t == LD_TOK_EOF) {
tccgen.c+29−27
@@ -47,7 +47,9 @@ static int in_sizeof; · 46 unmodified lines
4747 static int in_generic;
4848 static int section_sym;
4949
50−ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop, *pvtop;
50+ST_DATA SValue *vtop;
51+static SValue _vstack[1 + VSTACK_SIZE];
52+#define vstack (_vstack + 1)
5153
5254 ST_DATA int const_wanted; /* true if constant wanted */
5355 ST_DATA int nocode_wanted; /* no code generation wanted */
@@ -77,7 +79,6 @@ ST_DATA int func_var; /* true if current function is variadic (used by return in · 23 unmodified lines
7779 ST_DATA int func_vc;
7880 static int last_line_num, new_file, func_ind; /* debug info control */
7981 ST_DATA const char *funcname;
80−ST_DATA int g_debug;
8182
8283 ST_DATA CType char_pointer_type, func_old_type, int_type, size_type, ptrdiff_type;
8384
@@ -180,8 +181,8 @@ ST_FUNC void test_lvalue(void) · 96 unmodified lines
180181
181182 ST_FUNC void check_vstack(void)
182183 {
183− if (pvtop != vtop)
184− tcc_error("internal compiler error: vstack leak (%d)", vtop - pvtop);
184+ if (vtop != vstack - 1)
185+ tcc_error("internal compiler error: vstack leak (%d)", vtop - vstack + 1);
185186 }
186187
187188 /* ------------------------------------------------------------------------- */
@@ -325,15 +326,11 @@ ST_FUNC void tcc_debug_eincl(TCCState *s1) · 137 unmodified lines
325326 }
326327
327328 /* ------------------------------------------------------------------------- */
328−ST_FUNC int tccgen_compile(TCCState *s1)
329+/* initialize vstack and types. This must be done also for tcc -E */
330+ST_FUNC void tccgen_init(TCCState *s1)
329331 {
330− cur_text_section = NULL;
331− funcname = "";
332− anon_sym = SYM_FIRST_ANOM;
333− section_sym = 0;
334− const_wanted = 0;
335− nocode_wanted = 0x80000000;
336− local_scope = 0;
332+ vtop = vstack - 1;
333+ memset(vtop, 0, sizeof *vtop);
337334
338335 /* define some often used types */
339336 int_type.t = VT_INT;
@@ -353,17 +350,25 @@ ST_FUNC int tccgen_compile(TCCState *s1) · 13 unmodified lines
353350 func_old_type.ref = sym_push(SYM_FIELD, &int_type, 0, 0);
354351 func_old_type.ref->f.func_call = FUNC_CDECL;
355352 func_old_type.ref->f.func_type = FUNC_OLD;
353+}
356354
357− tcc_debug_start(s1);
355+ST_FUNC int tccgen_compile(TCCState *s1)
356+{
357+ cur_text_section = NULL;
358+ funcname = "";
359+ anon_sym = SYM_FIRST_ANOM;
360+ section_sym = 0;
361+ const_wanted = 0;
362+ nocode_wanted = 0x80000000;
363+ local_scope = 0;
358364
365+ tcc_debug_start(s1);
359366 #ifdef TCC_TARGET_ARM
360367 arm_init(s1);
361368 #endif
362−
363369 #ifdef INC_DEBUG
364370 printf("%s: **** new file\n", file->filename);
365371 #endif
366−
367372 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_TOK_STR;
368373 next();
369374 decl(VT_CONST);
@@ -374,19 +379,16 @@ ST_FUNC int tccgen_compile(TCCState *s1) · 4 unmodified lines
374379 return 0;
375380 }
376381
377−ST_FUNC void tccgen_finish(TCCState *s1, int f)
382+ST_FUNC void tccgen_finish(TCCState *s1)
378383 {
379− if (f == 1) {
380− free_inline_functions(s1);
381− sym_pop(&global_stack, NULL, 0);
382− sym_pop(&local_stack, NULL, 0);
383− }
384−
385− if (f == 2) {
386− /* free sym_pools */
387− dynarray_reset(&sym_pools, &nb_sym_pools);
388− sym_free_first = NULL;
389− }
384+ free_inline_functions(s1);
385+ sym_pop(&global_stack, NULL, 0);
386+ sym_pop(&local_stack, NULL, 0);
387+ /* free preprocessor macros */
388+ free_defines(NULL);
389+ /* free sym_pools */
390+ dynarray_reset(&sym_pools, &nb_sym_pools);
391+ sym_free_first = NULL;
390392 }
391393
392394 /* ------------------------------------------------------------------------- */
tccpe.c+4−2
@@ -636,12 +636,14 @@ static int pe_write(struct pe_info *pe) · 635 unmodified lines
636636
637637 switch (si->cls) {
638638 case sec_text:
639− pe_header.opthdr.BaseOfCode = addr;
639+ if (!pe_header.opthdr.BaseOfCode)
640+ pe_header.opthdr.BaseOfCode = addr;
640641 break;
641642
642643 case sec_data:
643644 #ifndef TCC_TARGET_X86_64
644− pe_header.opthdr.BaseOfData = addr;
645+ if (!pe_header.opthdr.BaseOfData)
646+ pe_header.opthdr.BaseOfData = addr;
645647 #endif
646648 break;
647649
tccpp.c+6−11
@@ -34,8 +34,6 @@ ST_DATA const int *macro_ptr; · 33 unmodified lines
3434 ST_DATA CString tokcstr; /* current parsed string, if any */
3535
3636 /* display benchmark infos */
37−ST_DATA int total_lines;
38−ST_DATA int total_bytes;
3937 ST_DATA int tok_ident;
4038 ST_DATA TokenSym **table_ident;
4139
@@ -604,7 +602,7 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv) · 562 unmodified lines
604602
605603 /* return the current character, handling end of block if necessary
606604 (but not stray) */
607−ST_FUNC int handle_eob(void)
605+static int handle_eob(void)
608606 {
609607 BufferedFile *bf = file;
610608 int len;
@@ -637,7 +635,7 @@ ST_FUNC int handle_eob(void) · 26 unmodified lines
637635 }
638636
639637 /* read next char from current input file and handle end of input buffer */
640−ST_INLN void inp(void)
638+static inline void inp(void)
641639 {
642640 ch = *(++(file->buf_ptr));
643641 /* end of buffer/file handling */
@@ -723,7 +721,7 @@ static int handle_stray1(uint8_t *p) · 79 unmodified lines
723721 /* input with '\[\r]\n' handling. Note that this function cannot
724722 handle other characters after '\', so you cannot call it inside
725723 strings or comments */
726−ST_FUNC void minp(void)
724+static void minp(void)
727725 {
728726 inp();
729727 if (ch == '\\')
@@ -768,7 +766,7 @@ static uint8_t *parse_line_comment(uint8_t *p) · 38 unmodified lines
768766 }
769767
770768 /* C comments */
771−ST_FUNC uint8_t *parse_comment(uint8_t *p)
769+static uint8_t *parse_comment(uint8_t *p)
772770 {
773771 int c;
774772
@@ -3597,8 +3595,6 @@ ST_FUNC void preprocess_start(TCCState *s1, int is_asm) · 2822 unmodified lines
35973595 pp_counter = 0;
35983596 pp_debug_tok = pp_debug_symv = 0;
35993597 pp_once++;
3600− pvtop = vtop = vstack - 1;
3601− memset(vtop, 0, sizeof *vtop);
36023598 s1->pack_stack[0] = 0;
36033599 s1->pack_stack_ptr = s1->pack_stack;
36043600
@@ -3629,10 +3625,8 @@ ST_FUNC void preprocess_end(TCCState *s1) · 24 unmodified lines
36293625 while (macro_stack)
36303626 end_macro();
36313627 macro_ptr = NULL;
3632−
36333628 while (file)
36343629 tcc_close();
3635−
36363630 tccpp_delete(s1);
36373631 }
36383632
@@ -3690,11 +3684,12 @@ ST_FUNC void tccpp_delete(TCCState *s) · 51 unmodified lines
36903684 {
36913685 int i, n;
36923686
3693− free_defines(NULL);
36943687 dynarray_reset(&s->cached_includes, &s->nb_cached_includes);
36953688
36963689 /* free tokens */
36973690 n = tok_ident - TOK_IDENT;
3691+ if (n > total_idents)
3692+ total_idents = n;
36983693 for(i = 0; i < n; i++)
36993694 tal_free(toksym_alloc, table_ident[i]);
37003695 tcc_free(table_ident);
tccrun.c+1−1
@@ -373,7 +373,7 @@ static addr_t rt_printline(TCCState *s1, addr_t wanted_pc, const char *msg) · 372 unmodified lines
373373 if (symtab_section) {
374374 esym_start = (ElfW(Sym) *)(symtab_section->data);
375375 esym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
376− elf_str = symtab_section->link->data;
376+ elf_str = (char *) symtab_section->link->data;
377377 }
378378
379379 for (sym = stab_sym + 1; sym < stab_sym_end; ++sym) {
tests/libtcc_test_mt.c+3−2
@@ -218,12 +218,13 @@ TF_TYPE(thread_test_complex, vn) · 217 unmodified lines
218218 argv[argc] = NULL;
219219
220220 s = new_state(1);
221− sleep_ms(1);
221+ sleep_ms(2);
222222 ret = tcc_add_file(s, argv[0]);
223− sleep_ms(1);
223+ sleep_ms(3);
224224 if (ret >= 0)
225225 tcc_run(s, argc, argv);
226226 tcc_delete(s);
227+ fflush(stdout);
227228 return 0;
228229 }
229230