OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [lcc/] [src/] [sym.c] - Blame information for rev 292

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 hellwig
#include "c.h"
2
#include <stdio.h>
3
 
4
static char rcsid[] = "$Id: sym.c,v 1.1 2002/08/28 23:12:47 drh Exp $";
5
 
6
#define equalp(x) v.x == p->sym.u.c.v.x
7
 
8
struct table {
9
        int level;
10
        Table previous;
11
        struct entry {
12
                struct symbol sym;
13
                struct entry *link;
14
        } *buckets[256];
15
        Symbol all;
16
};
17
#define HASHSIZE NELEMS(((Table)0)->buckets)
18
static struct table
19
        cns = { CONSTANTS },
20
        ext = { GLOBAL },
21
        ids = { GLOBAL },
22
        tys = { GLOBAL };
23
Table constants   = &cns;
24
Table externals   = &ext;
25
Table identifiers = &ids;
26
Table globals     = &ids;
27
Table types       = &tys;
28
Table labels;
29
int level = GLOBAL;
30
static int tempid;
31
List loci, symbols;
32
 
33
Table newtable(int arena) {
34
        Table new;
35
 
36
        NEW0(new, arena);
37
        return new;
38
}
39
 
40
Table table(Table tp, int level) {
41
        Table new = newtable(FUNC);
42
        new->previous = tp;
43
        new->level = level;
44
        if (tp)
45
                new->all = tp->all;
46
        return new;
47
}
48
void foreach(Table tp, int lev, void (*apply)(Symbol, void *), void *cl) {
49
        assert(tp);
50
        while (tp && tp->level > lev)
51
                tp = tp->previous;
52
        if (tp && tp->level == lev) {
53
                Symbol p;
54
                Coordinate sav;
55
                sav = src;
56
                for (p = tp->all; p && p->scope == lev; p = p->up) {
57
                        src = p->src;
58
                        (*apply)(p, cl);
59
                }
60
                src = sav;
61
        }
62
}
63
void enterscope(void) {
64
        if (++level == LOCAL)
65
                tempid = 0;
66
}
67
void exitscope(void) {
68
        rmtypes(level);
69
        if (types->level == level)
70
                types = types->previous;
71
        if (identifiers->level == level) {
72
                if (Aflag >= 2) {
73
                        int n = 0;
74
                        Symbol p;
75
                        for (p = identifiers->all; p && p->scope == level; p = p->up)
76
                                if (++n > 127) {
77
                                        warning("more than 127 identifiers declared in a block\n");
78
                                        break;
79
                                }
80
                }
81
                identifiers = identifiers->previous;
82
        }
83
        assert(level >= GLOBAL);
84
        --level;
85
}
86
Symbol install(const char *name, Table *tpp, int level, int arena) {
87
        Table tp = *tpp;
88
        struct entry *p;
89
        unsigned h = (unsigned long)name&(HASHSIZE-1);
90
 
91
        assert(level == 0 || level >= tp->level);
92
        if (level > 0 && tp->level < level)
93
                tp = *tpp = table(tp, level);
94
        NEW0(p, arena);
95
        p->sym.name = (char *)name;
96
        p->sym.scope = level;
97
        p->sym.up = tp->all;
98
        tp->all = &p->sym;
99
        p->link = tp->buckets[h];
100
        tp->buckets[h] = p;
101
        return &p->sym;
102
}
103
Symbol relocate(const char *name, Table src, Table dst) {
104
        struct entry *p, **q;
105
        Symbol *r;
106
        unsigned h = (unsigned long)name&(HASHSIZE-1);
107
 
108
        for (q = &src->buckets[h]; *q; q = &(*q)->link)
109
                if (name == (*q)->sym.name)
110
                        break;
111
        assert(*q);
112
        /*
113
         Remove the entry from src's hash chain
114
          and from its list of all symbols.
115
        */
116
        p = *q;
117
        *q = (*q)->link;
118
        for (r = &src->all; *r && *r != &p->sym; r = &(*r)->up)
119
                ;
120
        assert(*r == &p->sym);
121
        *r = p->sym.up;
122
        /*
123
         Insert the entry into dst's hash chain
124
          and into its list of all symbols.
125
          Return the symbol-table entry.
126
        */
127
        p->link = dst->buckets[h];
128
        dst->buckets[h] = p;
129
        p->sym.up = dst->all;
130
        dst->all = &p->sym;
131
        return &p->sym;
132
}
133
Symbol lookup(const char *name, Table tp) {
134
        struct entry *p;
135
        unsigned h = (unsigned long)name&(HASHSIZE-1);
136
 
137
        assert(tp);
138
        do
139
                for (p = tp->buckets[h]; p; p = p->link)
140
                        if (name == p->sym.name)
141
                                return &p->sym;
142
        while ((tp = tp->previous) != NULL);
143
        return NULL;
144
}
145
int genlabel(int n) {
146
        static int label = 1;
147
 
148
        label += n;
149
        return label - n;
150
}
151
Symbol findlabel(int lab) {
152
        struct entry *p;
153
        unsigned h = lab&(HASHSIZE-1);
154
 
155
        for (p = labels->buckets[h]; p; p = p->link)
156
                if (lab == p->sym.u.l.label)
157
                        return &p->sym;
158
        NEW0(p, FUNC);
159
        p->sym.name = stringd(lab);
160
        p->sym.scope = LABELS;
161
        p->sym.up = labels->all;
162
        labels->all = &p->sym;
163
        p->link = labels->buckets[h];
164
        labels->buckets[h] = p;
165
        p->sym.generated = 1;
166
        p->sym.u.l.label = lab;
167
        (*IR->defsymbol)(&p->sym);
168
        return &p->sym;
169
}
170
Symbol constant(Type ty, Value v) {
171
        struct entry *p;
172
        unsigned h = v.u&(HASHSIZE-1);
173
        static union { int x; char endian; } little = { 1 };
174
 
175
        ty = unqual(ty);
176
        for (p = constants->buckets[h]; p; p = p->link)
177
                if (eqtype(ty, p->sym.type, 1))
178
                        switch (ty->op) {
179
                        case INT:      if (equalp(i)) return &p->sym; break;
180
                        case UNSIGNED: if (equalp(u)) return &p->sym; break;
181
                        case FLOAT:
182
                                if (v.d == 0.0) {
183
                                        float z1 = v.d, z2 = p->sym.u.c.v.d;
184
                                        char *b1 = (char *)&z1, *b2 = (char *)&z2;
185
                                        if (z1 == z2
186
                                        && (!little.endian && b1[0] == b2[0]
187
                                        ||   little.endian && b1[sizeof (z1)-1] == b2[sizeof (z2)-1]))
188
                                                return &p->sym;
189
                                } else if (equalp(d))
190
                                        return &p->sym;
191
                                break;
192
                        case FUNCTION: if (equalp(g)) return &p->sym; break;
193
                        case ARRAY:
194
                        case POINTER:  if (equalp(p)) return &p->sym; break;
195
                        default: assert(0);
196
                        }
197
        NEW0(p, PERM);
198
        p->sym.name = vtoa(ty, v);
199
        p->sym.scope = CONSTANTS;
200
        p->sym.type = ty;
201
        p->sym.sclass = STATIC;
202
        p->sym.u.c.v = v;
203
        p->link = constants->buckets[h];
204
        p->sym.up = constants->all;
205
        constants->all = &p->sym;
206
        constants->buckets[h] = p;
207
        if (ty->u.sym && !ty->u.sym->addressed)
208
                (*IR->defsymbol)(&p->sym);
209
        p->sym.defined = 1;
210
        return &p->sym;
211
}
212
Symbol intconst(int n) {
213
        Value v;
214
 
215
        v.i = n;
216
        return constant(inttype, v);
217
}
218
Symbol genident(int scls, Type ty, int lev) {
219
        Symbol p;
220
 
221
        NEW0(p, lev >= LOCAL ? FUNC : PERM);
222
        p->name = stringd(genlabel(1));
223
        p->scope = lev;
224
        p->sclass = scls;
225
        p->type = ty;
226
        p->generated = 1;
227
        if (lev == GLOBAL)
228
                (*IR->defsymbol)(p);
229
        return p;
230
}
231
 
232
Symbol temporary(int scls, Type ty) {
233
        Symbol p;
234
 
235
        NEW0(p, FUNC);
236
        p->name = stringd(++tempid);
237
        p->scope = level < LOCAL ? LOCAL : level;
238
        p->sclass = scls;
239
        p->type = ty;
240
        p->temporary = 1;
241
        p->generated = 1;
242
        return p;
243
}
244
Symbol newtemp(int sclass, int tc, int size) {
245
        Symbol p = temporary(sclass, btot(tc, size));
246
 
247
        (*IR->local)(p);
248
        p->defined = 1;
249
        return p;
250
}
251
 
252
Symbol allsymbols(Table tp) {
253
        return tp->all;
254
}
255
 
256
void locus(Table tp, Coordinate *cp) {
257
        loci    = append(cp, loci);
258
        symbols = append(allsymbols(tp), symbols);
259
}
260
 
261
void use(Symbol p, Coordinate src) {
262
        Coordinate *cp;
263
 
264
        NEW(cp, PERM);
265
        *cp = src;
266
        p->uses = append(cp, p->uses);
267
}
268
/* findtype - find type ty in identifiers */
269
Symbol findtype(Type ty) {
270
        Table tp = identifiers;
271
        int i;
272
        struct entry *p;
273
 
274
        assert(tp);
275
        do
276
                for (i = 0; i < HASHSIZE; i++)
277
                        for (p = tp->buckets[i]; p; p = p->link)
278
                                if (p->sym.type == ty && p->sym.sclass == TYPEDEF)
279
                                        return &p->sym;
280
        while ((tp = tp->previous) != NULL);
281
        return NULL;
282
}
283
 
284
/* mkstr - make a string constant */
285
Symbol mkstr(char *str) {
286
        Value v;
287
        Symbol p;
288
 
289
        v.p = str;
290
        p = constant(array(chartype, strlen(v.p) + 1, 0), v);
291
        if (p->u.c.loc == NULL)
292
                p->u.c.loc = genident(STATIC, p->type, GLOBAL);
293
        return p;
294
}
295
 
296
/* mksymbol - make a symbol for name, install in &globals if sclass==EXTERN */
297
Symbol mksymbol(int sclass, const char *name, Type ty) {
298
        Symbol p;
299
 
300
        if (sclass == EXTERN)
301
                p = install(string(name), &globals, GLOBAL, PERM);
302
        else {
303
                NEW0(p, PERM);
304
                p->name = string(name);
305
                p->scope = GLOBAL;
306
        }
307
        p->sclass = sclass;
308
        p->type = ty;
309
        (*IR->defsymbol)(p);
310
        p->defined = 1;
311
        return p;
312
}
313
 
314
/* vtoa - return string for the constant v of type ty */
315
char *vtoa(Type ty, Value v) {
316
        char buf[50];
317
 
318
        ty = unqual(ty);
319
        switch (ty->op) {
320
        case INT:      return stringd(v.i);
321
        case UNSIGNED: return stringf((v.u&~0x7FFF) ? "0x%X" : "%U", v.u);
322
        case FLOAT:    return stringf("%g", (double)v.d);
323
        case ARRAY:
324
                if (ty->type == chartype || ty->type == signedchar
325
                ||  ty->type == unsignedchar)
326
                        return v.p;
327
                return stringf("%p", v.p);
328
        case POINTER:  return stringf("%p", v.p);
329
        case FUNCTION: return stringf("%p", v.g);
330
        }
331
        assert(0); return NULL;
332
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.