Added missing file.
[tinycc/k1w1.git] / tccsym.c
blobd9258ef9284bdd4762548d8d83edf706a275c199
1 /*
2 * Symbol handling for TCC
3 *
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
20 #include "tcc.h"
22 /* symbol allocator */
23 Sym *__sym_malloc(void)
25 Sym *sym_pool, *sym, *last_sym;
26 int i;
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;
32 sym = sym_pool;
33 for(i = 0; i < SYM_POOL_NB; i++) {
34 sym->next = last_sym;
35 last_sym = sym;
36 sym++;
38 sym_free_first = last_sym;
39 return last_sym;
43 /* push, without hashing */
44 Sym *sym_push2(Sym **ps, int v, int t, long c)
46 Sym *s;
47 s = sym_malloc();
48 s->v = v;
49 s->type.t = t;
50 #ifdef _WIN64
51 s->d = NULL;
52 #endif
53 s->c = c;
54 s->next = NULL;
55 /* add in stack */
56 s->prev = *ps;
57 *ps = s;
58 return s;
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)
65 while (s) {
66 if (s->v == v)
67 return s;
68 s = s->prev;
70 return NULL;
73 /* push a given symbol on the symbol stack */
74 Sym *sym_push(int v, CType *type, int r, int c)
76 Sym *s, **ps;
77 TokenSym *ts;
79 if (local_stack)
80 ps = &local_stack;
81 else
82 ps = &global_stack;
83 s = sym_push2(ps, v, type->t, c);
84 s->type.ref = type->ref;
85 s->r = r;
86 /* don't record fields or anonymous symbols */
87 /* XXX: simplify */
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];
91 if (v & SYM_STRUCT)
92 ps = &ts->sym_struct;
93 else
94 ps = &ts->sym_identifier;
95 s->prev_tok = *ps;
96 *ps = s;
98 return s;
101 /* push a global identifier */
102 Sym *global_identifier_push(int v, int t, int c)
104 Sym *s, **ps;
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 */
111 while (*ps != NULL)
112 ps = &(*ps)->prev_tok;
113 s->prev_tok = NULL;
114 *ps = s;
116 return s;
119 /* pop symbols until top reaches 'b' */
120 void sym_pop(Sym **ptop, Sym *b)
122 Sym *s, *ss, **ps;
123 TokenSym *ts;
124 int v;
126 s = *ptop;
127 while(s != b) {
128 ss = s->prev;
129 v = s->v;
130 /* remove symbol in token array */
131 /* XXX: simplify */
132 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
133 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
134 if (v & SYM_STRUCT)
135 ps = &ts->sym_struct;
136 else
137 ps = &ts->sym_identifier;
138 *ps = s->prev_tok;
140 sym_free(s);
141 s = ss;
143 *ptop = b;
146 #ifdef CONFIG_TCC_STATIC
148 /* dummy function for profiling */
149 void *dlopen(const char *filename, int flag)
151 return NULL;
154 void dlclose(void *p)
158 const char *dlerror(void)
160 return "error";
163 typedef struct TCCSyms {
164 char *str;
165 void *ptr;
166 } 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[] = {
172 TCCSYM(printf)
173 TCCSYM(fprintf)
174 TCCSYM(fopen)
175 TCCSYM(fclose)
176 { NULL, NULL },
179 void *resolve_sym(TCCState *s1, const char *symbol)
181 TCCSyms *p;
182 p = tcc_syms;
183 while (p->str != NULL) {
184 if (!strcmp(p->str, symbol))
185 return p->ptr;
186 p++;
188 return NULL;
191 #elif defined(_WIN32)
192 #define dlclose FreeLibrary
194 #else
195 #include <dlfcn.h>
197 void *resolve_sym(TCCState *s1, const char *sym)
199 return dlsym(RTLD_DEFAULT, sym);
202 #endif