Debian Codegraph @

src/ikarus-symbol-table.c

raw
1 /*
2 * Ikarus Scheme -- A compiler for R6RS Scheme.
3 * Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18
19
20 #include "ikarus-data.h"
21 #include <strings.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 static ikptr
26 make_symbol_table(ikpcb* pcb){
27 #define NUM_OF_BUCKETS 4096 /* power of 2 */
28 int size = align_to_next_page(disp_vector_data + NUM_OF_BUCKETS * wordsize);
29 ikptr st = ik_mmap_ptr(size, 0, pcb) + vector_tag;
30 bzero((char*)(long)st-vector_tag, size);
31 ref(st, off_vector_length) = fix(NUM_OF_BUCKETS);
32 return st;
33 }
34
35
36 /* one-at-a-time from http://burtleburtle.net/bob/hash/doobs.html */
37 static long int
38 compute_hash(ikptr str){
39 long int len = unfix(ref(str, off_string_length));
40 int* data = (int*)(str + off_string_data);
41 int h = len;
42 int* last = data + len;
43
44 /* one-at-a-time */
45 while(data < last){
46 int c = (*data >> 8);
47 h = h + c;
48 h = h + (h << 10);
49 h = h ^ (h >> 6);
50 data++;
51 }
52 h = h + (h << 3);
53 h = h ^ (h >> 11);
54 h = h + (h << 15);
55 return (h >= 0) ? h : (1 - h);
56 }
57
58 ikptr
59 ikrt_string_hash(ikptr str){
60 return (ikptr)(compute_hash(str) & (~ fx_mask));
61 }
62
63 static int strings_eqp(ikptr str1, ikptr str2){
64 ikptr len = ref(str1, off_string_length);
65 if(len == ref(str2, off_string_length)){
66 return
67 (memcmp((char*)(long)str1+off_string_data,
68 (char*)(long)str2+off_string_data,
69 unfix(len) * string_char_size)
70 == 0);
71 }
72 return 0;
73 }
74
75 static ikptr
76 ik_make_symbol(ikptr str, ikptr ustr, ikpcb* pcb){
77 ikptr sym = ik_unsafe_alloc(pcb, symbol_record_size) + record_tag;
78 ref(sym, -record_tag) = symbol_record_tag;
79 ref(sym, off_symbol_record_string) = str;
80 ref(sym, off_symbol_record_ustring) = ustr;
81 ref(sym, off_symbol_record_value) = unbound_object;
82 ref(sym, off_symbol_record_proc) = str;
83 ref(sym, off_symbol_record_plist) = null_object;
84 return sym;
85 }
86
87 static ikptr
88 intern_string(ikptr str, ikptr st, ikpcb* pcb){
89 int h = compute_hash(str);
90 int idx = h & (unfix(ref(st, off_vector_length)) - 1);
91 ikptr bckt = ref(st, off_vector_data + idx*wordsize);
92 ikptr b = bckt;
93 while(b){
94 ikptr sym = ref(b, off_car);
95 ikptr sym_str = ref(sym, off_symbol_record_string);
96 if(strings_eqp(sym_str, str)){
97 return sym;
98 }
99 b = ref(b, off_cdr);
100 }
101 ikptr sym = ik_make_symbol(str, false_object, pcb);
102 b = ik_unsafe_alloc(pcb, pair_size) + pair_tag;
103 ref(b, off_car) = sym;
104 ref(b, off_cdr) = bckt;
105 ref(st, off_vector_data + idx*wordsize) = b;
106 ((int*)(long)pcb->dirty_vector)[page_index(st+off_vector_data+idx*wordsize)] = -1;
107 return sym;
108 }
109
110 static ikptr
111 intern_unique_string(ikptr str, ikptr ustr, ikptr st, ikpcb* pcb){
112 int h = compute_hash(ustr);
113 int idx = h & (unfix(ref(st, off_vector_length)) - 1);
114 ikptr bckt = ref(st, off_vector_data + idx*wordsize);
115 ikptr b = bckt;
116 while(b){
117 ikptr sym = ref(b, off_car);
118 ikptr sym_ustr = ref(sym, off_symbol_record_ustring);
119 if(strings_eqp(sym_ustr, ustr)){
120 return sym;
121 }
122 b = ref(b, off_cdr);
123 }
124 ikptr sym = ik_make_symbol(str, ustr, pcb);
125 b = ik_unsafe_alloc(pcb, pair_size) + pair_tag;
126 ref(b, off_car) = sym;
127 ref(b, off_cdr) = bckt;
128 ref(st, off_vector_data + idx*wordsize) = b;
129 ((int*)(long)pcb->dirty_vector)[page_index(st+off_vector_data+idx*wordsize)] = -1;
130 return sym;
131 }
132
133 ikptr
134 ikrt_intern_gensym(ikptr sym, ikpcb* pcb){
135 ikptr st = pcb->gensym_table;
136 if(st == 0){
137 st = make_symbol_table(pcb);
138 pcb->gensym_table = st;
139 }
140 ikptr ustr = ref(sym, off_symbol_record_ustring);
141 int h = compute_hash(ustr);
142 int idx = h & (unfix(ref(st, off_vector_length)) - 1);
143 ikptr bckt = ref(st, off_vector_data + idx*wordsize);
144 ikptr b = bckt;
145 while(b){
146 ikptr sym = ref(b, off_car);
147 ikptr sym_ustr = ref(sym, off_symbol_record_ustring);
148 if(strings_eqp(sym_ustr, ustr)){
149 return false_object;
150 }
151 b = ref(b, off_cdr);
152 }
153 b = ik_unsafe_alloc(pcb, pair_size) + pair_tag;
154 ref(b, off_car) = sym;
155 ref(b, off_cdr) = bckt;
156 ref(st, off_vector_data + idx*wordsize) = b;
157 ((int*)(long)pcb->dirty_vector)[page_index(st+off_vector_data+idx*wordsize)] = -1;
158 return true_object;
159 }
160
161
162 ikptr
163 ikrt_unintern_gensym(ikptr sym, ikpcb* pcb){
164 ikptr st = pcb->gensym_table;
165 if(st == 0){
166 /* no symbol table */
167 return false_object;
168 }
169 ikptr ustr = ref(sym, off_symbol_record_ustring);
170 if (tagof(ustr) != string_tag) {
171 return false_object;
172 }
173 int h = compute_hash(ustr);
174 int idx = h & (unfix(ref(st, off_vector_length)) - 1);
175 ikptr loc = (ikptr)(st+off_vector_data+idx*wordsize);
176 ikptr bckt = ref(loc, 0);
177 while(bckt){
178 if (ref(bckt, off_car) == sym) {
179 /* found it */
180 ref(sym, off_symbol_record_ustring) = true_object;
181 ref(loc, 0) = ref(bckt, off_cdr);
182 return true_object;
183 } else {
184 loc = (ikptr)(bckt + off_cdr);
185 bckt = ref(loc, 0);
186 }
187 }
188 return false_object;
189 }
190
191 ikptr
192 ikrt_get_symbol_table(ikpcb* pcb){
193 ikptr st = pcb->symbol_table;
194 pcb->symbol_table = false_object;
195 if(st == false_object) {
196 fprintf(stderr, "bug in ikarus, attempt to access dead symbol table\n");
197 exit(-1);
198 }
199 return st;
200 }
201
202
203 ikptr
204 ikrt_string_to_symbol(ikptr str, ikpcb* pcb){
205 ikptr st = pcb->symbol_table;
206 if(st == false_object) {
207 fprintf(stderr, "bug in ikarus, attempt to access dead symbol table\n");
208 exit(-1);
209 }
210 if(st == 0){
211 st = make_symbol_table(pcb);
212 pcb->symbol_table = st;
213 }
214 return intern_string(str, st, pcb);
215 }
216
217 ikptr
218 ik_intern_string(ikptr str, ikpcb* pcb){
219 return ikrt_string_to_symbol(str, pcb);
220 }
221
222 ikptr
223 ikrt_strings_to_gensym(ikptr str, ikptr ustr, ikpcb* pcb){
224 ikptr st = pcb->gensym_table;
225 if(st == 0){
226 st = make_symbol_table(pcb);
227 pcb->gensym_table = st;
228 }
229 return intern_unique_string(str, ustr, st, pcb);
230 }
231
232