/
symtab.c
176 lines (135 loc) · 3.06 KB
/
symtab.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* symtab.c - Symbol table
*
* Copyright 2011-2012 by Werner Almesberger
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "compiler.h"
#include "symtab.h"
#define INITIAL_ALLOC 64
struct key_n {
const char *s;
int n;
};
struct sym well_known[] = {
#include "fnp.inc"
};
int num_well_known;
struct sym *user_syms = NULL;
int num_user_syms = 0;
static int allocated = 0;
/*
* "a" is not NUL-terminated and its length "a" is determined by "n".
* "b" is NUL-terminated.
*/
static int strcmp_n(const char *a, const char *b, int n)
{
int diff;
diff = strncmp(a, b, n);
if (diff)
return diff;
/* handle implicit NUL in string "a" */
return -b[n];
}
static char *strdup_n(const char *s, int n)
{
char *new;
new = malloc(n+1);
memcpy(new, s, n);
new[n] = 0;
return new;
}
static void grow_table(void)
{
if(num_user_syms != allocated)
return;
allocated = allocated ? allocated*2 : INITIAL_ALLOC;
user_syms = realloc(user_syms, allocated*sizeof(*user_syms));
}
static int cmp(const void *a, const void *b)
{
const struct sym *sym = b;
return strcmp(a, sym->fpvm_sym.name);
}
static int cmp_n(const void *a, const void *b)
{
const struct key_n *key = a;
const struct sym *sym = b;
return strcmp_n(key->s, sym->fpvm_sym.name, key->n);
}
struct sym *unique(const char *s)
{
struct sym *res, *walk, *new;
res = bsearch(s, well_known, num_well_known, sizeof(*well_known), cmp);
if(res)
return res;
for(walk = user_syms; walk != user_syms+num_user_syms; walk++)
if(!strcmp(walk->fpvm_sym.name, s))
return walk;
grow_table();
new = user_syms+num_user_syms++;
new->fpvm_sym.name = strdup(s);
new->pfv_idx = new->pvv_idx = -1;
new->flags = 0;
new->stim = NULL;
return new;
}
struct sym *unique_n(const char *s, int n)
{
struct key_n key = {
.s = s,
.n = n,
};
struct sym *res, *walk, *new;
assert(n);
res = bsearch(&key, well_known, num_well_known, sizeof(*well_known),
cmp_n);
if(res)
return res;
for(walk = user_syms; walk != user_syms+num_user_syms; walk++)
if(!strcmp_n(s, walk->fpvm_sym.name, n))
return walk;
grow_table();
new = user_syms+num_user_syms++;
new->fpvm_sym.name = strdup_n(s, n);
new->pfv_idx = new->pvv_idx = -1;
new->flags = 0;
new->stim = NULL;
return new;
}
void symtab_init(void)
{
int i;
num_well_known = sizeof(well_known)/sizeof(*well_known);
for(i = 0; i != num_well_known; i++)
well_known[i].flags &= SF_FIXED;
}
static void free_stim(struct sym *sym)
{
struct sym_stim *next;
while(sym->stim) {
next = sym->stim->next;
free(sym->stim);
sym->stim = next;
}
}
void symtab_free(void)
{
int i;
for(i = 0; i != num_well_known; i++)
free_stim(well_known+i);
for(i = 0; i != num_user_syms; i++) {
free((void *) user_syms[i].fpvm_sym.name);
free_stim(user_syms+i);
}
free(user_syms);
user_syms = NULL;
num_user_syms = allocated = 0;
}