17
|
1 #include <stdlib.h>
|
|
2 #include <string.h>
|
|
3
|
|
4 #include "array.h"
|
|
5 #include "mode.h"
|
|
6 #include "place.h"
|
|
7 #include "macro.h"
|
|
8
|
|
9 struct macro {
|
|
10 struct place defplace;
|
|
11 struct place expansionplace;
|
|
12 unsigned hash;
|
|
13 char *name;
|
18
|
14 bool hasparams;
|
|
15 struct stringarray params;
|
17
|
16 char *expansion;
|
|
17 };
|
|
18 DECLARRAY(macro);
|
|
19 DEFARRAY(macro, );
|
|
20 DECLARRAY(macroarray);
|
|
21 DEFARRAY(macroarray, );
|
|
22
|
|
23 static struct macroarrayarray macros;
|
|
24 static unsigned total_macros;
|
|
25 static unsigned hashmask;
|
|
26
|
|
27 ////////////////////////////////////////////////////////////
|
|
28 // macro structure ops
|
|
29
|
|
30 static
|
|
31 struct macro *
|
18
|
32 macro_create(struct place *p1, const char *name, unsigned hash,
|
|
33 struct place *p2, const char *expansion)
|
17
|
34 {
|
|
35 struct macro *m;
|
|
36
|
|
37 m = domalloc(sizeof(*m));
|
|
38 m->defplace = *p1;
|
|
39 m->expansionplace = *p2;
|
|
40 m->hash = hash;
|
|
41 m->name = dostrdup(name);
|
18
|
42 m->hasparams = false;
|
|
43 stringarray_init(&m->params);
|
17
|
44 m->expansion = dostrdup(expansion);
|
|
45 return m;
|
|
46 }
|
|
47
|
|
48 static
|
|
49 void
|
|
50 macro_destroy(struct macro *m)
|
|
51 {
|
|
52 free(m->name);
|
|
53 free(m->expansion);
|
|
54 free(m);
|
|
55 }
|
|
56
|
18
|
57 static
|
|
58 bool
|
|
59 macro_eq(const struct macro *m1, const struct macro *m2)
|
|
60 {
|
|
61 unsigned num1, num2, i;
|
|
62 const char *p1, *p2;
|
|
63
|
|
64 if (strcmp(m1->name, m2->name) != 0) {
|
|
65 return false;
|
|
66 }
|
|
67
|
|
68 if (m1->hasparams != m2->hasparams) {
|
|
69 return false;
|
|
70 }
|
|
71
|
|
72 if (strcmp(m1->expansion, m2->expansion) != 0) {
|
|
73 return false;
|
|
74 }
|
|
75
|
|
76 num1 = stringarray_num(&m1->params);
|
|
77 num2 = stringarray_num(&m2->params);
|
|
78 if (num1 != num2) {
|
|
79 return false;
|
|
80 }
|
|
81
|
|
82 for (i=0; i<num1; i++) {
|
|
83 p1 = stringarray_get(&m1->params, i);
|
|
84 p2 = stringarray_get(&m2->params, i);
|
|
85 if (strcmp(p1, p2) != 0) {
|
|
86 return false;
|
|
87 }
|
|
88 }
|
|
89 return true;
|
|
90 }
|
|
91
|
17
|
92 ////////////////////////////////////////////////////////////
|
|
93 // macro table
|
|
94
|
|
95 /*
|
|
96 * Unless I've screwed up, this is something called Fletcher's Checksum
|
|
97 * that showed up in Dr. Dobbs in, according to my notes, May 1992. The
|
|
98 * implementation is new.
|
|
99 */
|
|
100 static
|
|
101 unsigned
|
|
102 hashfunc(const char *s)
|
|
103 {
|
|
104 uint16_t x1, x2, a;
|
|
105 size_t i, len;
|
|
106
|
|
107 len = strlen(s);
|
|
108
|
|
109 x1 = (uint16_t) (len >> 16);
|
|
110 x2 = (uint16_t) (len);
|
|
111 if (x1==0) {
|
|
112 x1++;
|
|
113 }
|
|
114 if (x2==0) {
|
|
115 x2++;
|
|
116 }
|
|
117
|
|
118 for (i=0; i<len; i+=2) {
|
|
119 if (i==len-1) {
|
|
120 a = (unsigned char)s[i];
|
|
121 /* don't run off the end of the array */
|
|
122 }
|
|
123 else {
|
|
124 a = (unsigned char)s[i] +
|
|
125 ((uint16_t)(unsigned char)s[i+1] << 8);
|
|
126 }
|
|
127 x1 += a;
|
|
128 if (x1 < a) {
|
|
129 x1++;
|
|
130 }
|
|
131 x2 += x1;
|
|
132 if (x2 < x1) {
|
|
133 x2++;
|
|
134 }
|
|
135 }
|
|
136
|
|
137 x1 ^= 0xffff;
|
|
138 x2 ^= 0xffff;
|
|
139 return ((uint32_t)x2)*65535U + x1;
|
|
140 }
|
|
141
|
|
142 static
|
|
143 void
|
|
144 macrotable_init(void)
|
|
145 {
|
|
146 unsigned i;
|
|
147
|
|
148 macroarrayarray_init(¯os);
|
|
149 macroarrayarray_setsize(¯os, 4);
|
|
150 for (i=0; i<4; i++) {
|
|
151 macroarrayarray_set(¯os, i, NULL);
|
|
152 }
|
|
153 total_macros = 0;
|
|
154 hashmask = 0x3;
|
|
155 }
|
|
156
|
|
157 DESTROYALL_ARRAY(macro, );
|
|
158
|
|
159 static
|
|
160 void
|
|
161 macrotable_cleanup(void)
|
|
162 {
|
|
163 struct macroarray *bucket;
|
|
164 unsigned numbuckets, i;
|
|
165
|
|
166 numbuckets = macroarrayarray_num(¯os);
|
|
167 for (i=0; i<numbuckets; i++) {
|
|
168 bucket = macroarrayarray_get(¯os, i);
|
|
169 macroarray_destroyall(bucket);
|
|
170 macroarray_destroy(bucket);
|
|
171 }
|
|
172 macroarrayarray_setsize(¯os, 0);
|
|
173 macroarrayarray_cleanup(¯os);
|
|
174 }
|
|
175
|
|
176 static
|
|
177 struct macro *
|
|
178 macrotable_find(const char *name, bool remove)
|
|
179 {
|
|
180 unsigned hash;
|
|
181 struct macroarray *bucket;
|
|
182 struct macro *m, *m2;
|
|
183 unsigned i, num;
|
|
184
|
|
185 hash = hashfunc(name);
|
|
186 bucket = macroarrayarray_get(¯os, hash & hashmask);
|
|
187 if (bucket == NULL) {
|
|
188 return NULL;
|
|
189 }
|
|
190 num = macroarray_num(bucket);
|
|
191 for (i=0; i<num; i++) {
|
|
192 m = macroarray_get(bucket, i);
|
|
193 if (hash != m->hash) {
|
|
194 continue;
|
|
195 }
|
|
196 if (!strcmp(name, m->name)) {
|
|
197 if (remove) {
|
|
198 if (i < num-1) {
|
|
199 m2 = macroarray_get(bucket, num-1);
|
|
200 macroarray_set(bucket, i, m2);
|
|
201 }
|
|
202 macroarray_setsize(bucket, num-1);
|
|
203 total_macros--;
|
|
204 }
|
|
205 return m;
|
|
206 }
|
|
207 }
|
|
208 return NULL;
|
|
209 }
|
|
210
|
|
211 static
|
|
212 void
|
|
213 macrotable_rehash(void)
|
|
214 {
|
|
215 struct macroarray *newbucket, *oldbucket;
|
|
216 struct macro *m;
|
|
217 unsigned newmask, tossbit;
|
|
218 unsigned numbuckets, i;
|
|
219 unsigned oldnum, j, k;
|
|
220
|
|
221 numbuckets = macroarrayarray_num(¯os);
|
|
222 macroarrayarray_setsize(¯os, numbuckets*2);
|
|
223
|
|
224 assert(hashmask == numbuckets - 1);
|
|
225 newmask = (hashmask << 1) | 1U;
|
|
226 tossbit = newmask && ~hashmask;
|
|
227 hashmask = newmask;
|
|
228
|
|
229 for (i=0; i<numbuckets; i++) {
|
|
230 newbucket = NULL;
|
|
231 oldbucket = macroarrayarray_get(¯os, i);
|
|
232 oldnum = macroarray_num(oldbucket);
|
|
233 for (j=0; j<oldnum; j++) {
|
|
234 m = macroarray_get(oldbucket, j);
|
|
235 if (m->hash & tossbit) {
|
|
236 if (newbucket == NULL) {
|
|
237 newbucket = macroarray_create();
|
|
238 }
|
|
239 macroarray_set(oldbucket, j, NULL);
|
|
240 macroarray_add(newbucket, m, NULL);
|
|
241 }
|
|
242 }
|
|
243 for (j=k=0; j<oldnum; j++) {
|
|
244 m = macroarray_get(oldbucket, j);
|
|
245 if (m != NULL && k < j) {
|
|
246 macroarray_set(oldbucket, k++, m);
|
|
247 }
|
|
248 }
|
|
249 macroarray_setsize(oldbucket, k);
|
|
250 macroarrayarray_set(¯os, numbuckets + i, newbucket);
|
|
251 }
|
|
252 }
|
|
253
|
|
254 static
|
|
255 void
|
|
256 macrotable_add(struct macro *m)
|
|
257 {
|
|
258 unsigned hash;
|
|
259 struct macroarray *bucket;
|
|
260 unsigned numbuckets;
|
|
261
|
|
262 numbuckets = macroarrayarray_num(¯os);
|
|
263 if (total_macros > 0 && total_macros / numbuckets > 9) {
|
|
264 macrotable_rehash();
|
|
265 }
|
|
266
|
|
267 hash = hashfunc(m->name);
|
|
268 bucket = macroarrayarray_get(¯os, hash & hashmask);
|
|
269 if (bucket == NULL) {
|
|
270 bucket = macroarray_create();
|
|
271 macroarrayarray_set(¯os, hash & hashmask, bucket);
|
|
272 }
|
|
273 macroarray_add(bucket, m, NULL);
|
|
274 total_macros++;
|
|
275 }
|
|
276
|
|
277 ////////////////////////////////////////////////////////////
|
|
278 // external macro definition interface
|
|
279
|
18
|
280 static
|
|
281 struct macro *
|
|
282 macro_define_common_start(struct place *p1, const char *macro,
|
|
283 struct place *p2, const char *expansion)
|
17
|
284 {
|
|
285 struct macro *m;
|
|
286
|
18
|
287 if (!is_identifier(macro)) {
|
|
288 complain(p1, "Invalid macro name %s", macro);
|
|
289 complain_fail();
|
|
290 }
|
|
291
|
|
292 m = macro_create(p1, macro, hashfunc(macro), p2, expansion);
|
|
293 return m;
|
|
294 }
|
|
295
|
|
296 static
|
|
297 void
|
|
298 macro_define_common_end(struct macro *m)
|
|
299 {
|
|
300 struct macro *oldm;
|
|
301 bool ok;
|
|
302
|
|
303 oldm = macrotable_find(m->name, false);
|
|
304 if (oldm != NULL) {
|
|
305 ok = macro_eq(m, oldm);
|
|
306 if (ok) {
|
|
307 complain(&m->defplace,
|
|
308 "Warning: redefinition of %s", m->name);
|
17
|
309 if (mode.werror) {
|
|
310 complain_fail();
|
|
311 }
|
18
|
312 } else {
|
|
313 complain(&m->defplace,
|
|
314 "Redefinition of %s is not identical",
|
|
315 m->name);
|
|
316 complain_fail();
|
17
|
317 }
|
18
|
318 complain(&oldm->defplace, "Previous definition was here");
|
|
319 macro_destroy(m);
|
17
|
320 return;
|
|
321 }
|
18
|
322 macrotable_add(m);
|
|
323 }
|
17
|
324
|
18
|
325 static
|
|
326 void
|
|
327 macro_parse_parameters(struct macro *m, struct place *p, const char *params)
|
|
328 {
|
|
329 size_t len;
|
|
330 const char *s;
|
|
331 char *param;
|
|
332
|
|
333 while (params != NULL) {
|
|
334 len = strspn(params, ws);
|
|
335 params += len;
|
|
336 p->column += len;
|
|
337 s = strchr(params, ',');
|
|
338 if (s) {
|
|
339 len = s-params;
|
|
340 param = dostrndup(params, len);
|
|
341 s++;
|
|
342 } else {
|
|
343 len = strlen(params);
|
|
344 param = dostrndup(params, len);
|
|
345 }
|
|
346 notrailingws(param, strlen(param));
|
|
347 if (!is_identifier(param)) {
|
|
348 complain(p, "Invalid macro parameter name %s", param);
|
|
349 complain_fail();
|
|
350 } else {
|
|
351 stringarray_add(&m->params, param, NULL);
|
|
352 }
|
|
353 params = s;
|
|
354 p->column += len;
|
|
355 }
|
|
356 }
|
|
357
|
|
358 void
|
|
359 macro_define_plain(struct place *p1, const char *macro,
|
|
360 struct place *p2, const char *expansion)
|
|
361 {
|
|
362 struct macro *m;
|
|
363
|
|
364 m = macro_define_common_start(p1, macro, p2, expansion);
|
|
365 macro_define_common_end(m);
|
|
366 }
|
|
367
|
|
368 void
|
|
369 macro_define_params(struct place *p1, const char *macro,
|
|
370 struct place *p2, const char *params,
|
|
371 struct place *p3, const char *expansion)
|
|
372 {
|
|
373 struct macro *m;
|
|
374
|
|
375 m = macro_define_common_start(p1, macro, p3, expansion);
|
|
376 macro_parse_parameters(m, p2, params);
|
|
377 macro_define_common_end(m);
|
17
|
378 }
|
|
379
|
|
380 void
|
|
381 macro_undef(const char *macro)
|
|
382 {
|
|
383 struct macro *m;
|
|
384
|
|
385 m = macrotable_find(macro, true);
|
|
386 if (m) {
|
|
387 macro_destroy(m);
|
|
388 }
|
|
389 }
|
|
390
|
|
391 bool
|
|
392 macro_isdefined(const char *macro)
|
|
393 {
|
|
394 struct macro *m;
|
|
395
|
|
396 m = macrotable_find(macro, false);
|
|
397 return m != NULL;
|
|
398 }
|
|
399
|
|
400 ////////////////////////////////////////////////////////////
|
|
401 // macro expansion
|
|
402
|
|
403 char *macroexpand(struct place *, char *buf, size_t len, bool honordefined);
|
|
404
|
|
405 void macro_sendline(struct place *, char *buf, size_t len);
|
|
406 void macro_sendeof(struct place *);
|
|
407
|
|
408 ////////////////////////////////////////////////////////////
|
|
409 // module initialization
|
|
410
|
|
411 void
|
|
412 macros_init(void)
|
|
413 {
|
|
414 macrotable_init();
|
|
415 }
|
|
416
|
|
417 void
|
|
418 macros_cleanup(void)
|
|
419 {
|
|
420 macrotable_cleanup();
|
|
421 }
|