move some global variables into TCCState

tcc-xref
grischka 2009-05-11 18:45:44 +02:00
parent 5c6509578e
commit f9181416f6
9 changed files with 91 additions and 86 deletions

View File

@ -480,7 +480,7 @@ void gfunc_prolog(CType *func_type)
func_ret_sub = addr - 8;
/* leave some room for bound checking code */
if (do_bounds_check) {
if (tcc_state->do_bounds_check) {
oad(0xb8, 0); /* lbound section pointer */
oad(0xb8, 0); /* call to function */
func_bound_offset = lbounds_section->data_offset;
@ -493,7 +493,8 @@ void gfunc_epilog(void)
int v, saved_ind;
#ifdef CONFIG_TCC_BCHECK
if (do_bounds_check && func_bound_offset != lbounds_section->data_offset) {
if (tcc_state->do_bounds_check
&& func_bound_offset != lbounds_section->data_offset) {
int saved_ind;
int *bounds_ptr;
Sym *sym, *sym_data;

View File

@ -20,6 +20,10 @@
#include "tcc.h"
/* display benchmark infos */
int total_lines;
int total_bytes;
/* parser */
static struct BufferedFile *file;
static int ch, tok;
@ -93,19 +97,6 @@ static SValue vstack[VSTACK_SIZE], *vtop;
/* some predefined types */
static CType char_pointer_type, func_old_type, int_type;
/* display some information during compilation */
static int verbose = 0;
/* compile with debug symbol (and use them if error during execution) */
static int do_debug = 0;
/* compile with built-in memory and bounds checker */
static int do_bounds_check = 0;
/* display benchmark infos */
static int total_lines;
static int total_bytes;
/* use GNU C extensions */
static int gnu_ext = 1;
@ -114,17 +105,13 @@ static int tcc_ext = 1;
/* max number of callers shown if error */
#ifdef CONFIG_TCC_BACKTRACE
static int num_callers = 6;
static const char **rt_bound_error_msg;
int num_callers = 6;
const char **rt_bound_error_msg;
#endif
/* XXX: get rid of this ASAP */
static struct TCCState *tcc_state;
/* give the path of the tcc libraries */
static const char *tcc_lib_path = CONFIG_TCCDIR;
#ifdef TCC_TARGET_I386
#include "i386-gen.c"
#endif
@ -564,7 +551,7 @@ static void put_extern_sym2(Sym *sym, Section *section,
if (!sym->c) {
name = get_tok_str(sym->v, NULL);
#ifdef CONFIG_TCC_BCHECK
if (do_bounds_check) {
if (tcc_state->do_bounds_check) {
char buf[32];
/* XXX: avoid doing that for statics ? */
@ -989,7 +976,7 @@ BufferedFile *tcc_open(TCCState *s1, const char *filename)
fd = 0, filename = "stdin";
else
fd = open(filename, O_RDONLY | O_BINARY);
if ((verbose == 2 && fd >= 0) || verbose == 3)
if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
printf("%s %*s%s\n", fd < 0 ? "nf":"->",
(s1->include_stack_ptr - s1->include_stack), "", filename);
if (fd < 0)
@ -1039,7 +1026,7 @@ static int tcc_compile(TCCState *s1)
/* file info: full path + filename */
section_sym = 0; /* avoid warning */
if (do_debug) {
if (s1->do_debug) {
section_sym = put_elf_sym(symtab_section, 0, 0,
ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
text_section->sh_num, NULL);
@ -1108,7 +1095,7 @@ static int tcc_compile(TCCState *s1)
expect("declaration");
/* end of translation unit info */
if (do_debug) {
if (s1->do_debug) {
put_stabs_r(NULL, N_SO, 0, 0,
text_section->data_offset, text_section, section_sym);
}
@ -1579,7 +1566,7 @@ int tcc_run(TCCState *s1, int argc, char **argv)
prog_main = tcc_get_symbol_err(s1, "main");
if (do_debug) {
if (s1->do_debug) {
#ifdef CONFIG_TCC_BACKTRACE
struct sigaction sigact;
/* install TCC signal handlers to print debug info on fatal
@ -1598,7 +1585,7 @@ int tcc_run(TCCState *s1, int argc, char **argv)
}
#ifdef CONFIG_TCC_BCHECK
if (do_bounds_check) {
if (s1->do_bounds_check) {
void (*bound_init)(void);
/* set error function */
@ -1659,6 +1646,7 @@ TCCState *tcc_new(void)
return NULL;
tcc_state = s;
s->output_type = TCC_OUTPUT_MEMORY;
s->tcc_lib_path = CONFIG_TCCDIR;
preprocess_new();
@ -1995,17 +1983,17 @@ int tcc_set_output_type(TCCState *s, int output_type)
tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/local/include");
tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/include");
#endif
snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
snprintf(buf, sizeof(buf), "%s/include", s->tcc_lib_path);
tcc_add_sysinclude_path(s, buf);
#ifdef TCC_TARGET_PE
snprintf(buf, sizeof(buf), "%s/include/winapi", tcc_lib_path);
snprintf(buf, sizeof(buf), "%s/include/winapi", s->tcc_lib_path);
tcc_add_sysinclude_path(s, buf);
#endif
}
/* if bound checking, then add corresponding sections */
#ifdef CONFIG_TCC_BCHECK
if (do_bounds_check) {
if (s->do_bounds_check) {
/* define symbol */
tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
/* create bounds sections */
@ -2021,7 +2009,7 @@ int tcc_set_output_type(TCCState *s, int output_type)
}
/* add debug sections */
if (do_debug) {
if (s->do_debug) {
/* stab symbols */
stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
stab_section->sh_entsize = sizeof(Stab_Sym);
@ -2043,7 +2031,7 @@ int tcc_set_output_type(TCCState *s, int output_type)
#endif
#ifdef TCC_TARGET_PE
snprintf(buf, sizeof(buf), "%s/lib", tcc_lib_path);
snprintf(buf, sizeof(buf), "%s/lib", s->tcc_lib_path);
tcc_add_library_path(s, buf);
#endif
@ -2127,6 +2115,19 @@ int tcc_set_flag(TCCState *s, const char *flag_name, int value)
/* set CONFIG_TCCDIR at runtime */
void tcc_set_lib_path(TCCState *s, const char *path)
{
tcc_lib_path = tcc_strdup(path);
s->tcc_lib_path = tcc_strdup(path);
}
LIBTCCAPI void print_stats(TCCState *s, int64_t total_time)
{
double tt;
tt = (double)total_time / 1000000.0;
if (tt < 0.001)
tt = 0.001;
if (total_bytes < 1)
total_bytes = 1;
printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
tok_ident - TOK_IDENT, total_lines, total_bytes,
tt, (int)(total_lines / tt),
total_bytes / tt / 1000000.0);
}

30
tcc.c
View File

@ -303,12 +303,12 @@ int parse_args(TCCState *s, int argc, char **argv)
#endif
#ifdef CONFIG_TCC_BCHECK
case TCC_OPTION_b:
do_bounds_check = 1;
do_debug = 1;
s->do_bounds_check = 1;
s->do_debug = 1;
break;
#endif
case TCC_OPTION_g:
do_debug = 1;
s->do_debug = 1;
break;
case TCC_OPTION_c:
multiple_files = 1;
@ -355,7 +355,7 @@ int parse_args(TCCState *s, int argc, char **argv)
break;
case TCC_OPTION_v:
do {
if (0 == verbose++)
if (0 == s->verbose++)
printf("tcc version %s\n", TCC_VERSION);
} while (*optarg++ == 'v');
break;
@ -439,11 +439,11 @@ int main(int argc, char **argv)
optind = parse_args(s, argc - 1, argv + 1);
if (print_search_dirs) {
/* enough for Linux kernel */
printf("install: %s/\n", tcc_lib_path);
printf("install: %s/\n", s->tcc_lib_path);
return 0;
}
if (optind == 0 || nb_files == 0) {
if (optind && verbose)
if (optind && s->verbose)
return 0;
help();
return 1;
@ -517,7 +517,7 @@ int main(int argc, char **argv)
if (tcc_add_library(s, filename + 2) < 0)
error("cannot find %s", filename);
} else {
if (1 == verbose)
if (1 == s->verbose)
printf("-> %s\n", filename);
if (tcc_add_file(s, filename) < 0)
ret = 1;
@ -530,18 +530,8 @@ int main(int argc, char **argv)
if (ret)
goto the_end;
if (do_bench) {
double total_time;
total_time = (double)(getclock_us() - start_time) / 1000000.0;
if (total_time < 0.001)
total_time = 0.001;
if (total_bytes < 1)
total_bytes = 1;
printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
tok_ident - TOK_IDENT, total_lines, total_bytes,
total_time, (int)(total_lines / total_time),
total_bytes / total_time / 1000000.0);
}
if (do_bench)
print_stats(s, getclock_us() - start_time);
if (s->output_type == TCC_OUTPUT_PREPROCESS) {
if (outfile)
@ -552,7 +542,7 @@ int main(int argc, char **argv)
ret = tcc_output_file(s, outfile) ? 1 : 0;
the_end:
/* XXX: cannot do it with bound checking because of the malloc hooks */
if (!do_bounds_check)
if (!s->do_bounds_check)
tcc_delete(s);
#ifdef MEM_DEBUG

15
tcc.h
View File

@ -385,7 +385,6 @@ struct TCCState {
int nostdinc; /* if true, no standard headers are added */
int nostdlib; /* if true, no standard libraries are added */
int nocommon; /* if true, do not use common symbols for .bss data */
/* if true, static linking is performed */
@ -418,6 +417,15 @@ struct TCCState {
int warn_none;
int warn_implicit_function_declaration;
/* display some information during compilation */
int verbose;
/* compile with debug symbol (and use them if error during execution) */
int do_debug;
/* compile with built-in memory and bounds checker */
int do_bounds_check;
/* give the path of the tcc libraries */
const char *tcc_lib_path;
/* error handling */
void *error_opaque;
void (*error_func)(void *opaque, const char *msg);
@ -842,6 +850,11 @@ static int tcc_assemble(TCCState *s1, int do_preprocess);
static void asm_instr(void);
static void asm_global_instr(void);
#ifdef CONFIG_TCC_BACKTRACE
extern int num_callers;
extern const char **rt_bound_error_msg;
#endif
/* true if float/double/long double type */
static inline int is_float(int t)
{

View File

@ -183,7 +183,7 @@ int tcc_output_coff(TCCState *s1, FILE *f)
coff_sec->s_nlnno = 0;
coff_sec->s_lnnoptr = 0;
if (do_debug && tcc_sect == stext) {
if (s1->do_debug && tcc_sect == stext) {
// count how many line nos data
// also find association between source file name and function
@ -311,7 +311,7 @@ int tcc_output_coff(TCCState *s1, FILE *f)
file_hdr.f_symptr = file_pointer; /* file pointer to symtab */
if (do_debug)
if (s1->do_debug)
file_hdr.f_nsyms = coff_nb_syms; /* number of symtab entries */
else
file_hdr.f_nsyms = 0;
@ -362,7 +362,7 @@ int tcc_output_coff(TCCState *s1, FILE *f)
// group the symbols in order of filename, func1, func2, etc
// finally global symbols
if (do_debug)
if (s1->do_debug)
SortSymbolTable();
// write line no data
@ -371,7 +371,7 @@ int tcc_output_coff(TCCState *s1, FILE *f)
coff_sec = &section_header[i];
tcc_sect = s1->sections[i];
if (do_debug && tcc_sect == stext) {
if (s1->do_debug && tcc_sect == stext) {
// count how many line nos data
@ -499,7 +499,7 @@ int tcc_output_coff(TCCState *s1, FILE *f)
}
// write symbol table
if (do_debug) {
if (s1->do_debug) {
int k;
struct syment csym;
AUXFUNC auxfunc;
@ -670,7 +670,7 @@ int tcc_output_coff(TCCState *s1, FILE *f)
}
}
if (do_debug) {
if (s1->do_debug) {
// write string table
// first write the size

View File

@ -1163,7 +1163,7 @@ static void tcc_add_runtime(TCCState *s1)
#endif
#ifdef CONFIG_TCC_BCHECK
if (do_bounds_check) {
if (s1->do_bounds_check) {
unsigned long *ptr;
Section *init_section;
unsigned char *pinit;
@ -1176,7 +1176,7 @@ static void tcc_add_runtime(TCCState *s1)
ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
bounds_section->sh_num, "__bounds_start");
/* add bound check code */
snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "bcheck.o");
snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, "bcheck.o");
tcc_add_file(s1, buf);
#ifdef TCC_TARGET_I386
if (s1->output_type != TCC_OUTPUT_MEMORY) {
@ -1199,7 +1199,7 @@ static void tcc_add_runtime(TCCState *s1)
#ifdef CONFIG_USE_LIBGCC
tcc_add_file(s1, CONFIG_SYSROOT "/lib/libgcc_s.so.1");
#else
snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "libtcc1.a");
snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, "libtcc1.a");
tcc_add_file(s1, buf);
#endif
}
@ -1574,9 +1574,9 @@ int elf_output_file(TCCState *s1, const char *filename)
/* //gr: avoid bogus relocs for empty (debug) sections */
if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC)
prepare_dynamic_rel(s1, s);
else if (do_debug)
else if (s1->do_debug)
s->sh_size = s->data_offset;
} else if (do_debug ||
} else if (s1->do_debug ||
file_type == TCC_OUTPUT_OBJ ||
(s->sh_flags & SHF_ALLOC) ||
i == (s1->nb_sections - 1)) {
@ -1817,7 +1817,7 @@ int elf_output_file(TCCState *s1, const char *filename)
put_dt(dynamic, DT_RELSZ, rel_size);
put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
#endif
if (do_debug)
if (s1->do_debug)
put_dt(dynamic, DT_DEBUG, 0);
put_dt(dynamic, DT_NULL, 0);
}
@ -1888,7 +1888,7 @@ int elf_output_file(TCCState *s1, const char *filename)
goto fail;
}
f = fdopen(fd, "wb");
if (verbose)
if (s1->verbose)
printf("<- %s\n", filename);
#ifdef TCC_TARGET_COFF

View File

@ -1311,7 +1311,7 @@ void gen_op(int op)
#ifdef CONFIG_TCC_BCHECK
/* if evaluating constant expression, no code should be
generated, so no bound check */
if (do_bounds_check && !const_wanted) {
if (tcc_state->do_bounds_check && !const_wanted) {
/* if bounded pointers, we generate a special code to
test bounds */
if (op == '-') {
@ -2880,7 +2880,7 @@ static void indir(void)
&& (vtop->type.t & VT_BTYPE) != VT_FUNC) {
vtop->r |= lvalue_type(vtop->type.t);
/* if bound checking, the referenced pointer must be checked */
if (do_bounds_check)
if (tcc_state->do_bounds_check)
vtop->r |= VT_MUSTBOUND;
}
}
@ -3294,7 +3294,7 @@ static void unary(void)
if (!(vtop->type.t & VT_ARRAY)) {
vtop->r |= lvalue_type(vtop->type.t);
/* if bound checking, the referenced pointer must be checked */
if (do_bounds_check)
if (tcc_state->do_bounds_check)
vtop->r |= VT_MUSTBOUND;
}
next();
@ -3794,7 +3794,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
Sym *s;
/* generate line number info */
if (do_debug &&
if (tcc_state->do_debug &&
(last_line_num != file->line_num || last_ind != ind)) {
put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
last_ind = ind;
@ -4649,14 +4649,14 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
}
if ((r & VT_VALMASK) == VT_LOCAL) {
sec = NULL;
if (do_bounds_check && (type->t & VT_ARRAY))
if (tcc_state->do_bounds_check && (type->t & VT_ARRAY))
loc--;
loc = (loc - size) & -align;
addr = loc;
/* handles bounds */
/* XXX: currently, since we do only one pass, we cannot track
'&' operators, so we add only arrays */
if (do_bounds_check && (type->t & VT_ARRAY)) {
if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
unsigned long *bounds_ptr;
/* add padding between regions */
loc--;
@ -4722,7 +4722,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
because initializers themselves can create new initializers */
data_offset += size;
/* add padding if bound check */
if (do_bounds_check)
if (tcc_state->do_bounds_check)
data_offset++;
sec->data_offset = data_offset;
/* allocate section space to put the data */
@ -4763,7 +4763,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
/* handles bounds now because the symbol must be defined
before for the relocation */
if (do_bounds_check) {
if (tcc_state->do_bounds_check) {
unsigned long *bounds_ptr;
greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
@ -4860,7 +4860,7 @@ static void gen_function(Sym *sym)
funcname = get_tok_str(sym->v, NULL);
func_ind = ind;
/* put debug symbol */
if (do_debug)
if (tcc_state->do_debug)
put_func_debug(sym);
/* push a dummy symbol to enable local sym storage */
sym_push2(&local_stack, SYM_FIELD, 0, 0);
@ -4876,7 +4876,7 @@ static void gen_function(Sym *sym)
/* patch symbol size */
((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
ind - func_ind;
if (do_debug) {
if (tcc_state->do_debug) {
put_stabn(N_FUN, 0, 0, ind - func_ind);
}
/* It's better to crash than to generate wrong code */

12
tccpe.c
View File

@ -577,7 +577,7 @@ ST_FN int pe_write(struct pe_info *pe)
file_offset = pe->sizeofheaders;
pe_fpad(op, file_offset);
if (2 == verbose)
if (2 == pe->s1->verbose)
printf("-------------------------------"
"\n virt file size section" "\n");
@ -588,7 +588,7 @@ ST_FN int pe_write(struct pe_info *pe)
unsigned long size = si->sh_size;
IMAGE_SECTION_HEADER *psh = &si->ish;
if (2 == verbose)
if (2 == pe->s1->verbose)
printf("%6lx %6lx %6lx %s\n",
addr, file_offset, size, sh_name);
@ -662,9 +662,9 @@ ST_FN int pe_write(struct pe_info *pe)
fwrite(&pe->sec_info[i].ish, 1, sizeof(IMAGE_SECTION_HEADER), op);
fclose (op);
if (2 == verbose)
if (2 == pe->s1->verbose)
printf("-------------------------------\n");
if (verbose)
if (pe->s1->verbose)
printf("<- %s (%lu bytes)\n", pe->filename, file_offset);
return 0;
@ -859,7 +859,7 @@ ST_FN void pe_build_exports(struct pe_info *pe)
error_noabort("could not create '%s': %s", buf, strerror(errno));
} else {
fprintf(op, "LIBRARY %s\n\nEXPORTS\n", dllname);
if (verbose)
if (pe->s1->verbose)
printf("<- %s (%d symbols)\n", buf, sym_count);
}
#endif
@ -1065,7 +1065,7 @@ ST_FN int pe_assign_addresses (struct pe_info *pe)
flags & SHF_EXECINSTR ? "exec" : ""
);
}
verbose = 2;
pe->s1->verbose = 2;
#endif
tcc_free(section_order);

View File

@ -440,7 +440,7 @@ static uint8_t *parse_comment(uint8_t *p)
#define cinp minp
/* space exlcuding newline */
static inline int is_space(int ch)
LIBTCCAPI static inline int is_space(int ch)
{
return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
}
@ -1336,7 +1336,7 @@ static void preprocess(int is_bof)
pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
file = f;
/* add include file debug info */
if (do_debug) {
if (tcc_state->do_debug) {
put_stabs(file->filename, N_BINCL, 0, 0, 0);
}
tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
@ -1946,7 +1946,7 @@ static inline void next_nomacro1(void)
}
/* add end of include file debug info */
if (do_debug) {
if (tcc_state->do_debug) {
put_stabd(N_EINCL, 0, 0);
}
/* pop include stack */