Alien-TinyCCx
view release on metacpan or search on metacpan
src/tccexsymtab.c view on Meta::CPAN
}
Sym * get_new_deftab_pointer (Sym * old, ram_hash * rh)
{
void ** Sym_ref;
Sym * to_return;
/* Handle the null case up-front */
if (old == NULL) return NULL;
/* Does this exist in the ram hash? */
Sym_ref = ram_hash_get_ref(rh, old);
to_return = *Sym_ref;
if (to_return != NULL) return to_return;
/* Create a new define object. See symtab pointer copy above for
* descriptions of some of the fields. */
to_return = *Sym_ref = tcc_mallocz(sizeof(Sym));
/* Convert the symbol's token index. */
to_return->v = old->v & ~SYM_EXTENDED;
/* As far as I can tell, the 'r' field is not used by
* preprocessor macros. Just copy it in the off-chance I'm wrong. */
to_return->r = old->r;
/* Copy the tokenstream if it exists */
if (old->d != NULL) {
int * str = old->d;
int len = tokenstream_len(str);
to_return->d = tcc_malloc(sizeof(int) * len);
/* The extended symbol table's token ids are identical to the
* originals, so we can just copy the token stream verbatim! */
memcpy(to_return->d, old->d, sizeof(int) * len);
}
/* Set the type. define_push and parse_define indicate that this
* will be either MACRO_OBJ or MACRO_FUNC. */
to_return->type.t = old->type.t;
/* Copy the macro arguments. */
to_return->next = get_new_deftab_pointer(old->next, rh);
return to_return;
}
int should_copy_TokenSym(TokenSym * to_check, int tok_start)
{
/* Copy all tokens that come after tok_start */
if (to_check->tok >= tok_start) return 1;
/* Always ignore these, no matter what */
if (to_check->tok == TOK___LINE__
|| to_check->tok == TOK___FILE__
|| to_check->tok == TOK___DATE__
|| to_check->tok == TOK___TIME__
) return 0;
/* For the handful of specially declared tokens (like push, pop, etc),
* decide based on contents. */
if ((to_check->sym_define != NULL && to_check->sym_define->d != NULL)
|| to_check->sym_struct != NULL
|| to_check->sym_identifier != NULL
) return 1;
return 0;
}
/* Make a complete copy of the TokenSym and Sym tables, using a ram_hash
* for the latter. */
void copy_extended_symtab (TCCState * s, Sym * define_start, int tok_start)
{
int i, N_tokens, tok_start_offset;
extended_symtab * to_return;
int curr_tok_idx;
ram_hash * sym_rh;
ram_hash * def_rh;
/* Do nothing if we have an empty TCCState. */
if (NULL == s) return;
/* Count the number of tokens that we'll store whose token ids come
* before tok_start. (We know we'll at least have everything after
* and including tok_start.) */
tok_start_offset = 0;
for (i = 0; i < tok_start - TOK_IDENT; i++) {
if (should_copy_TokenSym(table_ident[i], tok_start)) tok_start_offset++;
}
N_tokens = tok_ident - tok_start + tok_start_offset;
/* Room for the first TokenSym is included in the struct definition, so I
* need to allocate room for the extended symtab plus N_tokens - 1. */
to_return = tcc_malloc(sizeof(extended_symtab) + sizeof(void*) * (N_tokens - 1));
to_return->tok_start = tok_start;
to_return->tok_start_offset = tok_start_offset;
/* Allocate the token string and ram hashes */
to_return->tsh = token_string_hash_new();
sym_rh = to_return->sym_rh = ram_hash_new();
def_rh = to_return->def_rh = ram_hash_new();
to_return->N_syms = 0; /* Setting to zero indicates that the data */
to_return->N_defs = 0; /* are in hashes, not arrays */
/* Copy all TokenSyms and their dependent Syms */
curr_tok_idx = 0;
for (i = 0; i < tok_ident - TOK_IDENT; i++)
{
TokenSym * tok_copy = table_ident[i];
int tokensym_size;
TokenSym * tok_sym;
if (!should_copy_TokenSym(tok_copy, tok_start)) continue;
tokensym_size = sizeof(TokenSym) + tok_copy->len;
tok_sym = to_return->tokenSym_list[curr_tok_idx++]
= tcc_malloc(tokensym_size);
/* Follow the code from tok_alloc_new in tccpp.c */
tok_sym->tok = tok_copy->tok;
( run in 0.754 second using v1.01-cache-2.11-cpan-13bb782fe5a )