Skip to content

Commit

Permalink
cleanup: stop abuse of sym->r for inline functions
Browse files Browse the repository at this point in the history
  • Loading branch information
grischka authored and unknown committed Jul 18, 2009
1 parent 5e83b64 commit d0b432a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
12 changes: 10 additions & 2 deletions tcc.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ typedef struct {
#define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call) #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
#define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export) #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
#define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args) #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
#define INLINE_DEF(r) (*(int **)&(r))
/* -------------------------------------------------- */ /* -------------------------------------------------- */


#define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */ #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
Expand Down Expand Up @@ -331,6 +330,13 @@ typedef struct TokenString {
int last_line_num; int last_line_num;
} TokenString; } TokenString;


/* inline functions */
typedef struct InlineFunc {
int *token_str;
Sym *sym;
char filename[1];
} InlineFunc;

/* include file cache, used to find files faster and also to eliminate /* include file cache, used to find files faster and also to eliminate
inclusion if the include file is protected by #ifndef ... #endif */ inclusion if the include file is protected by #ifndef ... #endif */
typedef struct CachedInclude { typedef struct CachedInclude {
Expand Down Expand Up @@ -362,7 +368,6 @@ typedef struct ASMOperand {
int is_memory; /* true if memory operand */ int is_memory; /* true if memory operand */
int is_rw; /* for '+' modifier */ int is_rw; /* for '+' modifier */
} ASMOperand; } ASMOperand;

#endif #endif


struct TCCState { struct TCCState {
Expand Down Expand Up @@ -479,6 +484,9 @@ struct TCCState {
/* for tcc_relocate */ /* for tcc_relocate */
int runtime_added; int runtime_added;


struct InlineFunc **inline_fns;
int nb_inline_fns;

#ifdef TCC_TARGET_X86_64 #ifdef TCC_TARGET_X86_64
/* write PLT and GOT here */ /* write PLT and GOT here */
char *runtime_plt_and_got; char *runtime_plt_and_got;
Expand Down
45 changes: 20 additions & 25 deletions tccgen.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4914,21 +4914,22 @@ static void gen_function(Sym *sym)
static void gen_inline_functions(void) static void gen_inline_functions(void)
{ {
Sym *sym; Sym *sym;
CType *type; int *str, inline_generated, i;
int *str, inline_generated; struct InlineFunc *fn;


/* iterate while inline function are referenced */ /* iterate while inline function are referenced */
for(;;) { for(;;) {
inline_generated = 0; inline_generated = 0;
for(sym = global_stack; sym != NULL; sym = sym->prev) { for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
type = &sym->type; fn = tcc_state->inline_fns[i];
if (((type->t & VT_BTYPE) == VT_FUNC) && sym = fn->sym;
(type->t & (VT_STATIC | VT_INLINE)) == if (sym && sym->c) {
(VT_STATIC | VT_INLINE) &&
sym->c != 0) {
/* the function was used: generate its code and /* the function was used: generate its code and
convert it to a normal function */ convert it to a normal function */
str = INLINE_DEF(sym->r); str = fn->token_str;
fn->sym = NULL;
if (file)
strcpy(file->filename, fn->filename);
sym->r = VT_SYM | VT_CONST; sym->r = VT_SYM | VT_CONST;
sym->type.t &= ~VT_INLINE; sym->type.t &= ~VT_INLINE;


Expand All @@ -4945,21 +4946,7 @@ static void gen_inline_functions(void)
if (!inline_generated) if (!inline_generated)
break; break;
} }

dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
/* free all remaining inline function tokens */
for(sym = global_stack; sym != NULL; sym = sym->prev) {
type = &sym->type;
if (((type->t & VT_BTYPE) == VT_FUNC) &&
(type->t & (VT_STATIC | VT_INLINE)) ==
(VT_STATIC | VT_INLINE)) {
//gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
continue;
str = INLINE_DEF(sym->r);
tok_str_free(str);
sym->r = 0; /* fail safe */
}
}
} }


/* 'l' is VT_LOCAL or VT_CONST to define default storage type */ /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
Expand Down Expand Up @@ -5070,6 +5057,8 @@ static void decl(int l)
(VT_INLINE | VT_STATIC)) { (VT_INLINE | VT_STATIC)) {
TokenString func_str; TokenString func_str;
int block_level; int block_level;
struct InlineFunc *fn;
const char *filename;


tok_str_new(&func_str); tok_str_new(&func_str);


Expand All @@ -5091,7 +5080,13 @@ static void decl(int l)
} }
tok_str_add(&func_str, -1); tok_str_add(&func_str, -1);
tok_str_add(&func_str, 0); tok_str_add(&func_str, 0);
INLINE_DEF(sym->r) = func_str.str; filename = file ? file->filename : "";
fn = tcc_malloc(sizeof *fn + strlen(filename));
strcpy(fn->filename, filename);
fn->sym = sym;
fn->token_str = func_str.str;
dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);

} else { } else {
/* compute text section */ /* compute text section */
cur_text_section = ad.section; cur_text_section = ad.section;
Expand Down

0 comments on commit d0b432a

Please sign in to comment.