2 * Symbol handling for TCC
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* symbol allocator */
23 Sym
*__sym_malloc(void)
25 Sym
*sym_pool
, *sym
, *last_sym
;
28 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
29 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
31 last_sym
= sym_free_first
;
33 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
38 sym_free_first
= last_sym
;
43 /* push, without hashing */
44 Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
61 /* find a symbol and return its associated structure. 's' is the top
62 of the symbol stack */
63 Sym
*sym_find2(Sym
*s
, int v
)
73 /* push a given symbol on the symbol stack */
74 Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
83 s
= sym_push2(ps
, v
, type
->t
, c
);
84 s
->type
.ref
= type
->ref
;
86 /* don't record fields or anonymous symbols */
88 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
89 /* record symbol in token array */
90 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
94 ps
= &ts
->sym_identifier
;
101 /* push a global identifier */
102 Sym
*global_identifier_push(int v
, int t
, int c
)
105 s
= sym_push2(&global_stack
, v
, t
, c
);
106 /* don't record anonymous symbol */
107 if (v
< SYM_FIRST_ANOM
) {
108 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
109 /* modify the top most local identifier, so that
110 sym_identifier will point to 's' when popped */
112 ps
= &(*ps
)->prev_tok
;
119 /* pop symbols until top reaches 'b' */
120 void sym_pop(Sym
**ptop
, Sym
*b
)
130 /* remove symbol in token array */
132 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
133 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
135 ps
= &ts
->sym_struct
;
137 ps
= &ts
->sym_identifier
;
146 #ifdef CONFIG_TCC_STATIC
148 /* dummy function for profiling */
149 void *dlopen(const char *filename
, int flag
)
154 void dlclose(void *p
)
158 const char *dlerror(void)
163 typedef struct TCCSyms
{
168 #define TCCSYM(a) { #a, &a, },
170 /* add the symbol you want here if no dynamic linking is done */
171 static TCCSyms tcc_syms
[] = {
179 void *resolve_sym(TCCState
*s1
, const char *symbol
)
183 while (p
->str
!= NULL
) {
184 if (!strcmp(p
->str
, symbol
))
191 #elif defined(_WIN32)
192 #define dlclose FreeLibrary
197 void *resolve_sym(TCCState
*s1
, const char *sym
)
199 return dlsym(RTLD_DEFAULT
, sym
);