Debian Codegraph @

cc/ccom/symtabs.c

raw symbols SCIP
1 /* $Id: symtabs.c,v 1.40 2019/04/08 13:30:17 ragge Exp $ */
2 /*
3 * Copyright (c) 2003 Anders Magnusson ([email protected]).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29
30 #include "pass1.h"
31 #include "unicode.h"
32 #include <stdlib.h>
33
34 #define NODE P1ND
35 #define fwalk p1fwalk
36
37 /*
38 * These definitions are used in the patricia tree that stores
39 * the strings.
40 */
41 #define LEFT_IS_LEAF 0x80000000
42 #define RIGHT_IS_LEAF 0x40000000
43 #define IS_LEFT_LEAF(x) (((x) & LEFT_IS_LEAF) != 0)
44 #define IS_RIGHT_LEAF(x) (((x) & RIGHT_IS_LEAF) != 0)
45 #define BITNO(x) ((x) & ~(LEFT_IS_LEAF|RIGHT_IS_LEAF))
46 #define CHECKBITS 8
47
48 struct tree {
49 int bitno;
50 struct tree *lr[2];
51 };
52
53 extern int dimfuncnt;
54 static struct tree *firstname;
55 int nametabs, namestrlen;
56 static struct tree *firststr;
57 int strtabs, strstrlen, symtreecnt;
58 static char *symtab_add(char *key, struct tree **, int *, int *);
59 int lastloc = NOSEG;
60 int treestrsz = sizeof(struct tree);
61
62 #define P_BIT(key, bit) (key[bit >> 3] >> (bit & 7)) & 1
63 #define getree() permalloc(sizeof(struct tree))
64
65 char *
66 addname(char *key)
67 {
68 return symtab_add(key, &firstname, &nametabs, &namestrlen);
69 }
70
71 char *
72 addstring(char *key)
73 {
74 return symtab_add(key, &firststr, &strtabs, &strstrlen);
75 }
76
77 /*
78 * Add a name to the name stack (if its non-existing),
79 * return its address.
80 * This is a simple patricia implementation.
81 */
82 static char *
83 symtab_add(char *key, struct tree **first, int *tabs, int *stlen)
84 {
85 struct tree *w, *new, *last;
86 int cix, bit, fbit, svbit, ix, bitno, len;
87 char *m, *k, *sm;
88
89 /* Count full string length */
90 for (k = key, len = 0; *k; k++, len++)
91 ;
92
93 switch (*tabs) {
94 case 0:
95 *first = (struct tree *)newstring(key, len);
96 *stlen += (len + 1);
97 (*tabs)++;
98 return (char *)*first;
99
100 case 1:
101 m = (char *)*first;
102 svbit = 0; /* XXX why? */
103 break;
104
105 default:
106 w = *first;
107 bitno = len * CHECKBITS;
108 for (;;) {
109 bit = BITNO(w->bitno);
110 fbit = bit > bitno ? 0 : P_BIT(key, bit);
111 svbit = fbit ? IS_RIGHT_LEAF(w->bitno) :
112 IS_LEFT_LEAF(w->bitno);
113 w = w->lr[fbit];
114 if (svbit) {
115 m = (char *)w;
116 break;
117 }
118 }
119 }
120
121 sm = m;
122 k = key;
123
124 /* Check for correct string and return */
125 for (cix = 0; *m && *k && *m == *k; m++, k++, cix += CHECKBITS)
126 ;
127 if (*m == 0 && *k == 0)
128 return sm;
129
130 ix = *m ^ *k;
131 while ((ix & 1) == 0)
132 ix >>= 1, cix++;
133
134 /* Create new node */
135 new = getree();
136 bit = P_BIT(key, cix);
137 new->bitno = cix | (bit ? RIGHT_IS_LEAF : LEFT_IS_LEAF);
138 new->lr[bit] = (struct tree *)newstring(key, len);
139 *stlen += (len + 1);
140
141 if ((*tabs)++ == 1) {
142 new->lr[!bit] = *first;
143 new->bitno |= (bit ? LEFT_IS_LEAF : RIGHT_IS_LEAF);
144 *first = new;
145 return (char *)new->lr[bit];
146 }
147
148
149 w = *first;
150 last = NULL;
151 for (;;) {
152 fbit = w->bitno;
153 bitno = BITNO(w->bitno);
154 if (bitno == cix)
155 cerror("bitno == cix");
156 if (bitno > cix)
157 break;
158 svbit = P_BIT(key, bitno);
159 last = w;
160 w = w->lr[svbit];
161 if (fbit & (svbit ? RIGHT_IS_LEAF : LEFT_IS_LEAF))
162 break;
163 }
164
165 new->lr[!bit] = w;
166 if (last == NULL) {
167 *first = new;
168 } else {
169 last->lr[svbit] = new;
170 last->bitno &= ~(svbit ? RIGHT_IS_LEAF : LEFT_IS_LEAF);
171 }
172 if (bitno < cix)
173 new->bitno |= (bit ? LEFT_IS_LEAF : RIGHT_IS_LEAF);
174 return (char *)new->lr[bit];
175 }
176
177 static struct tree *sympole[NSTYPES];
178 static struct symtab *tmpsyms[NSTYPES];
179 int numsyms[NSTYPES];
180
181 /*
182 * Inserts a symbol into the symbol tree.
183 * Returns a struct symtab.
184 */
185 struct symtab *
186 lookup(char *key, int stype)
187 {
188 struct symtab *sym;
189 struct tree *w, *new, *last;
190 int cix, bit, fbit, svbit, bitno;
191 int type, uselvl;
192 intptr_t ix, match, code = (intptr_t)key;
193
194 type = stype & SMASK;
195 uselvl = (blevel > 0 && type != SSTRING);
196
197 /*
198 * The local symbols are kept in a simple linked list.
199 * Check this list first.
200 */
201 if (blevel > 0)
202 for (sym = tmpsyms[type]; sym; sym = sym->snext)
203 if (sym->sname == key)
204 return sym;
205
206 switch (numsyms[type]) {
207 case 0:
208 if (stype & SNOCREAT)
209 return NULL;
210 if (uselvl) {
211 if (type == SNORMAL)
212 stype |= SBLK;
213 sym = getsymtab(key, stype);
214 sym->snext = tmpsyms[type];
215 tmpsyms[type] = sym;
216 return sym;
217 }
218 sympole[type] = (struct tree *)getsymtab(key, stype);
219 numsyms[type]++;
220 return (struct symtab *)sympole[type];
221
222 case 1:
223 w = (struct tree *)sympole[type];
224 svbit = 0; /* XXX why? */
225 break;
226
227 default:
228 w = sympole[type];
229 for (;;) {
230 bit = BITNO(w->bitno);
231 fbit = (int)(code >> bit) & 1;
232 svbit = fbit ? IS_RIGHT_LEAF(w->bitno) :
233 IS_LEFT_LEAF(w->bitno);
234 w = w->lr[fbit];
235 if (svbit)
236 break;
237 }
238 }
239
240 sym = (struct symtab *)w;
241 match = (intptr_t)sym->sname;
242
243 ix = code ^ match;
244 if (ix == 0)
245 return sym;
246 else if (stype & SNOCREAT)
247 return NULL;
248
249 #ifdef PCC_DEBUG
250 if (ddebug)
251 printf(" adding %s as %s at level %d\n",
252 key, uselvl ? "temp" : "perm", blevel);
253 #endif
254
255 /*
256 * Insert into the linked list, if feasible.
257 */
258 if (uselvl) {
259 sym = getsymtab(key, stype|STEMP);
260 sym->snext = tmpsyms[type];
261 tmpsyms[type] = sym;
262 return sym;
263 }
264
265 /*
266 * Need a new node. If type is SNORMAL and inside a function
267 * the node must be allocated as permanent anyway.
268 * This could be optimized by adding a remove routine, but it
269 * may be more trouble than it is worth.
270 */
271 if (stype == (STEMP|SNORMAL))
272 stype = SNORMAL;
273
274 for (cix = 0; (ix & 1) == 0; ix >>= 1, cix++)
275 ;
276
277 new = (symtreecnt++, permalloc(sizeof(struct tree)));
278 bit = (int)(code >> cix) & 1;
279 new->bitno = cix | (bit ? RIGHT_IS_LEAF : LEFT_IS_LEAF);
280 new->lr[bit] = (struct tree *)getsymtab(key, stype);
281 if (numsyms[type]++ == 1) {
282 new->lr[!bit] = sympole[type];
283 new->bitno |= (bit ? LEFT_IS_LEAF : RIGHT_IS_LEAF);
284 sympole[type] = new;
285 return (struct symtab *)new->lr[bit];
286 }
287
288
289 w = sympole[type];
290 last = NULL;
291 for (;;) {
292 fbit = w->bitno;
293 bitno = BITNO(w->bitno);
294 if (bitno == cix)
295 cerror("bitno == cix");
296 if (bitno > cix)
297 break;
298 svbit = (int)(code >> bitno) & 1;
299 last = w;
300 w = w->lr[svbit];
301 if (fbit & (svbit ? RIGHT_IS_LEAF : LEFT_IS_LEAF))
302 break;
303 }
304
305 new->lr[!bit] = w;
306 if (last == NULL) {
307 sympole[type] = new;
308 } else {
309 last->lr[svbit] = new;
310 last->bitno &= ~(svbit ? RIGHT_IS_LEAF : LEFT_IS_LEAF);
311 }
312 if (bitno < cix)
313 new->bitno |= (bit ? LEFT_IS_LEAF : RIGHT_IS_LEAF);
314 return (struct symtab *)new->lr[bit];
315 }
316
317 void
318 symclear(int level)
319 {
320 struct symtab *s;
321 int i;
322
323 #ifdef PCC_DEBUG
324 if (ddebug)
325 printf("symclear(%d)\n", level);
326 #endif
327 if (level < 1) {
328 for (i = 0; i < NSTYPES; i++) {
329 s = tmpsyms[i];
330 tmpsyms[i] = 0;
331 if (i != SLBLNAME)
332 continue;
333 while (s != NULL) {
334 if (s->soffset < 0)
335 uerror("label '%s' undefined",s->sname);
336 s = s->snext;
337 }
338 }
339 } else {
340 for (i = 0; i < NSTYPES; i++) {
341 if (i == SLBLNAME)
342 continue; /* function scope */
343 while (tmpsyms[i] != NULL &&
344 tmpsyms[i]->slevel > level) {
345 tmpsyms[i] = tmpsyms[i]->snext;
346 }
347 }
348 }
349 }
350
351 struct symtab *
352 hide(struct symtab *sym)
353 {
354 struct symtab *new;
355 int typ = sym->sflags & SMASK;
356
357 new = getsymtab(sym->sname, typ|STEMP);
358 new->snext = tmpsyms[typ];
359 tmpsyms[typ] = new;
360
361 #ifdef PCC_DEBUG
362 if (ddebug)
363 printf("\t%s hidden at level %d (%p -> %p)\n",
364 sym->sname, blevel, sym, new);
365 #endif
366 return new;
367 }
368
369 /*
370 * Extract correct segment for the specified symbol and call
371 * target routines to print it out.
372 * If symtab entry is specified, output alignment as well.
373 */
374 void
375 locctr(int seg, struct symtab *sp)
376 {
377 #ifdef GCC_COMPAT
378 struct attr *ga;
379 #endif
380
381 if (seg == NOSEG) {
382 ;
383 } else if (sp == NULL) {
384 if (lastloc != seg)
385 setseg(seg, NULL);
386 #ifdef GCC_COMPAT
387 } else if ((ga = attr_find(sp->sap, GCC_ATYP_SECTION)) != NULL) {
388 setseg(NMSEG, ga->sarg(0));
389 seg = NOSEG;
390 #endif
391 } else {
392 if (seg == DATA) {
393 if (ISCON(cqual(sp->stype, sp->squal)))
394 seg = RDATA;
395 else if (sp->sclass == STATIC)
396 seg = LDATA;
397 }
398 if (sp->sflags & STLS) {
399 if (seg == DATA || seg == LDATA)
400 seg = TLSDATA;
401 if (seg == UDATA) seg = TLSUDATA;
402 } else if (kflag) {
403 if (seg == DATA) seg = PICDATA;
404 if (seg == RDATA) seg = PICRDATA;
405 if (seg == LDATA) seg = PICLDATA;
406 }
407 if (lastloc != seg)
408 setseg(seg, NULL);
409 }
410 lastloc = seg;
411
412 /* setup alignment */
413 #ifndef ALFTN
414 #define ALFTN ALINT
415 #endif
416 if (sp) {
417 int al;
418
419 if (ISFTN(sp->stype)) {
420 al = ALFTN;
421 } else
422 al = talign(sp->stype, sp->sap);
423 defalign(al);
424 symdirec(sp);
425 }
426 }
427
428 #ifndef MYALIGN
429 void
430 defalign(int al)
431 {
432 #ifdef HASP2ALIGN
433 #define P2ALIGN(x) ispow2(x)
434 #else
435 #define P2ALIGN(x) (x)
436 #endif
437 if (al != ALCHAR)
438 printf(PRTPREF "\t.align %d\n", P2ALIGN(al/ALCHAR));
439 }
440 #endif
441
442 #ifndef MYDIREC
443 /*
444 * Directives given as attributes to symbols.
445 */
446 void
447 symdirec(struct symtab *sp)
448 {
449 #ifdef GCC_COMPAT
450 struct attr *ga;
451 char *name;
452
453 name = getexname(sp);
454 if ((ga = attr_find(sp->sap, GCC_ATYP_WEAK)) != NULL)
455 printf(PRTPREF "\t.weak %s\n", name);
456 if ((ga = attr_find(sp->sap, GCC_ATYP_VISIBILITY)) &&
457 strcmp(ga->sarg(0), "default"))
458 printf(PRTPREF "\t.%s %s\n", ga->sarg(0), name);
459 if ((ga = attr_find(sp->sap, GCC_ATYP_ALIASWEAK))) {
460 printf(PRTPREF "\t.weak %s\n", ga->sarg(0));
461 printf(PRTPREF "\t.set %s,%s\n", ga->sarg(0), name);
462 }
463 #endif
464 }
465 #endif
466
467 char *
468 getexname(struct symtab *sp)
469 {
470 struct attr *ap = attr_find(sp->sap, ATTR_SONAME);
471
472 return (ap ? ap->sarg(0) : addname(exname(sp->sname)));
473 }
474
475 static char *csbuf;
476 static int csbufp, cssz, strtype;
477 #ifndef NO_STRING_SAVE
478 static struct symtab *strpole;
479 #endif
480 #define STCHNK 128
481
482 static void
483 savch(int ch)
484 {
485 if (csbufp == cssz) {
486 cssz += STCHNK;
487 csbuf = realloc(csbuf, cssz);
488 }
489 csbuf[csbufp++] = ch;
490 }
491
492 /*
493 * save value as 3-digit octal escape sequence
494 */
495 static void
496 voct(unsigned int v)
497 {
498 savch('\\');
499 savch(((v & 0700) >> 6) + '0');
500 savch(((v & 0070) >> 3) + '0');
501 savch((v & 0007) + '0');
502 }
503
504
505 /*
506 * Add string new to string old.
507 * String new must come directly after old.
508 * new is expected to be utf-8. Will be cleaned slightly here.
509 */
510 char *
511 stradd(char *old, char *new)
512 {
513 if (old == NULL) {
514 strtype = 0;
515 csbufp = 0;
516 } else if (old != csbuf)
517 cerror("string synk error");
518
519 /* special hack for function names */
520 for (old = new; *old; old++)
521 ;
522 if (old[-1] != '\"') {
523 do {
524 savch(*new);
525 } while (*new++);
526 return csbuf;
527 }
528
529 if (*new != '\"') {
530 int ny = *new++;
531 if (ny == 'u' && *new == '8')
532 ny = '8', new++;
533 if (strtype && ny != strtype)
534 uerror("clash in string types");
535 strtype = ny;
536 }
537 if (*new++ != '\"')
538 cerror("snuff synk error");
539
540 while (*new != '\"') {
541 if (*new == '\\') {
542 voct(esccon(&new));
543 } else if (*new < ' ' || *new > '~') {
544 voct(*(unsigned char *)new++);
545 } else {
546 savch(*new++);
547 }
548 }
549 savch(0);
550 csbufp--;
551 return csbuf;
552 }
553
554 TWORD
555 styp(void)
556 {
557 TWORD t;
558
559 if (strtype == 0 || strtype == '8')
560 t = xuchar ? UCHAR+ARY : CHAR+ARY;
561 else if (strtype == 'u')
562 t = ctype(USHORT)+ARY;
563 else if (strtype == 'L')
564 t = ctype(WCHAR_TYPE)+ARY;
565 else
566 t = ctype(SZINT < 32 ? ULONG : UNSIGNED)+ARY;
567 return t;
568 }
569
570 /*
571 * Create a string struct.
572 */
573 static void
574 strst(struct symtab *sp, TWORD t)
575 {
576 char *wr;
577 int i;
578
579 sp->sclass = STATIC;
580 sp->slevel = 1;
581 sp->soffset = getlab();
582 sp->squal = (CON >> TSHIFT);
583 #ifndef NO_STRING_SAVE
584 sp->sdf = permalloc(sizeof(union dimfun));
585 #else
586 sp->sdf = stmtalloc(sizeof(union dimfun));
587 #endif
588 dimfuncnt++;
589 sp->stype = t;
590
591 for (wr = sp->sname, i = 1; *wr; i++) {
592 if (strtype == 'L' || strtype == 'U' || strtype == 'u')
593 (void)u82cp(&wr);
594 else if (*wr == '\\')
595 (void)esccon(&wr);
596 else
597 wr++;
598 }
599 sp->sdf->ddim = i;
600 #ifndef NO_STRING_SAVE
601 sp->snext = strpole;
602 strpole = sp;
603 #endif
604 }
605
606 /*
607 * Save string (if needed) and return NODE for it.
608 * String is already in utf-8 format.
609 */
610 NODE *
611 strend(char *s, TWORD t)
612 {
613 struct symtab *sp, *sp2;
614 NODE *p;
615
616 #ifdef NO_STRING_SAVE
617 sp = getsymtab(s, SSTRING|SSTMT);
618 #else
619 s = addstring(s);
620 sp = lookup(s, SSTRING);
621 #endif
622
623 if (sp->soffset && sp->stype != t) {
624 /* same string stored but different type */
625 /* This is uncommon, create a new symtab struct for it */
626 sp2 = permalloc(sizeof(*sp));
627 *sp2 = *sp;
628 strst(sp2, t);
629 sp = sp2;
630 } else if (sp->soffset == 0) { /* No string */
631 strst(sp, t);
632 }
633 if (cssz > STCHNK) {
634 cssz = STCHNK;
635 csbuf = realloc(csbuf, cssz);
636 }
637 #ifdef NO_STRING_SAVE
638 instring(sp);
639 #endif
640 p = block(NAME, NIL, NIL, sp->stype, sp->sdf, sp->sap);
641 p->n_sp = sp;
642 return(clocal(p));
643 }
644
645 #ifndef NO_STRING_SAVE
646 /*
647 * Print out strings that have been referenced.
648 */
649 void
650 strprint(void)
651 {
652 struct symtab *sp;
653
654 for (sp = strpole; sp; sp = sp->snext) {
655 if ((sp->sflags & SASG) == 0)
656 continue; /* not referenced */
657 instring(sp);
658 }
659 }
660 #endif