1 |
|
|
/* $OpenBSD: symtab.c,v 1.17 2014/03/13 00:56:39 tedu Exp $ */ |
2 |
|
|
/* $NetBSD: symtab.c,v 1.4 1996/03/19 03:21:48 jtc Exp $ */ |
3 |
|
|
|
4 |
|
|
/* |
5 |
|
|
* Copyright (c) 1989 The Regents of the University of California. |
6 |
|
|
* All rights reserved. |
7 |
|
|
* |
8 |
|
|
* This code is derived from software contributed to Berkeley by |
9 |
|
|
* Robert Paul Corbett. |
10 |
|
|
* |
11 |
|
|
* Redistribution and use in source and binary forms, with or without |
12 |
|
|
* modification, are permitted provided that the following conditions |
13 |
|
|
* are met: |
14 |
|
|
* 1. Redistributions of source code must retain the above copyright |
15 |
|
|
* notice, this list of conditions and the following disclaimer. |
16 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
17 |
|
|
* notice, this list of conditions and the following disclaimer in the |
18 |
|
|
* documentation and/or other materials provided with the distribution. |
19 |
|
|
* 3. Neither the name of the University nor the names of its contributors |
20 |
|
|
* may be used to endorse or promote products derived from this software |
21 |
|
|
* without specific prior written permission. |
22 |
|
|
* |
23 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
24 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
26 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
27 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
29 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
30 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
31 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
32 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
33 |
|
|
* SUCH DAMAGE. |
34 |
|
|
*/ |
35 |
|
|
|
36 |
|
|
#include "defs.h" |
37 |
|
|
|
38 |
|
|
/* TABLE_SIZE is the number of entries in the symbol table. */ |
39 |
|
|
/* TABLE_SIZE must be a power of two. */ |
40 |
|
|
|
41 |
|
|
#define TABLE_SIZE 1024 |
42 |
|
|
|
43 |
|
|
|
44 |
|
|
bucket **symbol_table; |
45 |
|
|
bucket *first_symbol; |
46 |
|
|
bucket *last_symbol; |
47 |
|
|
|
48 |
|
|
int hash(char *); |
49 |
|
|
|
50 |
|
|
|
51 |
|
|
int |
52 |
|
|
hash(char *name) |
53 |
|
6657 |
{ |
54 |
|
|
char *s; |
55 |
|
|
int c, k; |
56 |
|
|
|
57 |
✓✗✗✓
|
6657 |
assert(name && *name); |
58 |
|
6657 |
s = name; |
59 |
|
6657 |
k = *s; |
60 |
✓✓ |
54766 |
while ((c = *++s)) |
61 |
|
41452 |
k = (31 * k + c) & (TABLE_SIZE - 1); |
62 |
|
|
|
63 |
|
6657 |
return (k); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
|
67 |
|
|
bucket * |
68 |
|
|
make_bucket(char *name) |
69 |
|
2020 |
{ |
70 |
|
|
bucket *bp; |
71 |
|
|
|
72 |
✗✓ |
2020 |
assert(name); |
73 |
|
2020 |
bp = malloc(sizeof(bucket)); |
74 |
✗✓ |
2020 |
if (bp == NULL) |
75 |
|
|
no_space(); |
76 |
|
2020 |
bp->link = 0; |
77 |
|
2020 |
bp->next = 0; |
78 |
|
2020 |
bp->name = strdup(name); |
79 |
✗✓ |
2020 |
if (bp->name == NULL) |
80 |
|
|
no_space(); |
81 |
|
2020 |
bp->tag = 0; |
82 |
|
2020 |
bp->value = UNDEFINED; |
83 |
|
2020 |
bp->index = 0; |
84 |
|
2020 |
bp->prec = 0; |
85 |
|
2020 |
bp->class = UNKNOWN; |
86 |
|
2020 |
bp->assoc = TOKEN; |
87 |
|
|
|
88 |
|
2020 |
return (bp); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
|
92 |
|
|
bucket * |
93 |
|
|
lookup(char *name) |
94 |
|
6636 |
{ |
95 |
|
|
bucket *bp, **bpp; |
96 |
|
|
|
97 |
|
6636 |
bpp = symbol_table + hash(name); |
98 |
|
6636 |
bp = *bpp; |
99 |
|
|
|
100 |
✓✓ |
14141 |
while (bp) { |
101 |
✓✓ |
5561 |
if (strcmp(name, bp->name) == 0) |
102 |
|
4692 |
return (bp); |
103 |
|
869 |
bpp = &bp->link; |
104 |
|
869 |
bp = *bpp; |
105 |
|
|
} |
106 |
|
|
|
107 |
|
1944 |
*bpp = bp = make_bucket(name); |
108 |
|
1944 |
last_symbol->next = bp; |
109 |
|
1944 |
last_symbol = bp; |
110 |
|
|
|
111 |
|
1944 |
return (bp); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
|
115 |
|
|
void |
116 |
|
|
create_symbol_table(void) |
117 |
|
21 |
{ |
118 |
|
|
bucket *bp; |
119 |
|
|
|
120 |
|
21 |
symbol_table = calloc(TABLE_SIZE, sizeof(bucket *)); |
121 |
✗✓ |
21 |
if (symbol_table == NULL) |
122 |
|
|
no_space(); |
123 |
|
|
|
124 |
|
21 |
bp = make_bucket("error"); |
125 |
|
21 |
bp->index = 1; |
126 |
|
21 |
bp->class = TERM; |
127 |
|
|
|
128 |
|
21 |
first_symbol = bp; |
129 |
|
21 |
last_symbol = bp; |
130 |
|
21 |
symbol_table[hash("error")] = bp; |
131 |
|
21 |
} |
132 |
|
|
|
133 |
|
|
|
134 |
|
|
void |
135 |
|
|
free_symbol_table(void) |
136 |
|
21 |
{ |
137 |
|
21 |
free(symbol_table); |
138 |
|
21 |
symbol_table = 0; |
139 |
|
21 |
} |
140 |
|
|
|
141 |
|
|
|
142 |
|
|
void |
143 |
|
|
free_symbols(void) |
144 |
|
21 |
{ |
145 |
|
|
bucket *p, *q; |
146 |
|
|
|
147 |
✓✓ |
2041 |
for (p = first_symbol; p; p = q) { |
148 |
|
2020 |
q = p->next; |
149 |
|
2020 |
free(p); |
150 |
|
|
} |
151 |
|
21 |
} |