Print this page
11972 resync smatch
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/symbol.c
+++ new/usr/src/tools/smatch/src/symbol.c
1 1 /*
2 2 * Symbol lookup and handling.
3 3 *
4 4 * Copyright (C) 2003 Transmeta Corp.
5 5 * 2003-2004 Linus Torvalds
6 6 *
7 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 8 * of this software and associated documentation files (the "Software"), to deal
9 9 * in the Software without restriction, including without limitation the rights
10 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 11 * copies of the Software, and to permit persons to whom the Software is
12 12 * furnished to do so, subject to the following conditions:
13 13 *
14 14 * The above copyright notice and this permission notice shall be included in
15 15 * all copies or substantial portions of the Software.
16 16 *
17 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 23 * THE SOFTWARE.
24 24 */
25 25 #include <stdlib.h>
↓ open down ↓ |
25 lines elided |
↑ open up ↑ |
26 26 #include <stdio.h>
27 27 #include <string.h>
28 28
29 29 #include "lib.h"
30 30 #include "allocate.h"
31 31 #include "token.h"
32 32 #include "parse.h"
33 33 #include "symbol.h"
34 34 #include "scope.h"
35 35 #include "expression.h"
36 +#include "evaluate.h"
36 37
37 38 #include "target.h"
38 39
39 40 /*
40 41 * Secondary symbol list for stuff that needs to be output because it
41 42 * was used.
42 43 */
43 44 struct symbol_list *translation_unit_used_list = NULL;
44 45
45 46 /*
46 47 * If the symbol is an inline symbol, add it to the list of symbols to parse
47 48 */
48 49 void access_symbol(struct symbol *sym)
49 50 {
50 51 if (sym->ctype.modifiers & MOD_INLINE) {
51 - if (!(sym->ctype.modifiers & MOD_ACCESSED)) {
52 + if (!sym->accessed) {
52 53 add_symbol(&translation_unit_used_list, sym);
53 - sym->ctype.modifiers |= MOD_ACCESSED;
54 + sym->accessed = 1;
54 55 }
55 56 }
56 57 }
57 58
58 59 struct symbol *lookup_symbol(struct ident *ident, enum namespace ns)
59 60 {
60 61 struct symbol *sym;
61 62
62 63 for (sym = ident->symbols; sym; sym = sym->next_id) {
63 64 if (sym->namespace & ns) {
64 65 sym->used = 1;
65 66 return sym;
66 67 }
67 68 }
68 69 return NULL;
69 70 }
70 71
71 72 struct context *alloc_context(void)
72 73 {
73 74 return __alloc_context(0);
74 75 }
75 76
76 77 struct symbol *alloc_symbol(struct position pos, int type)
77 78 {
78 79 struct symbol *sym = __alloc_symbol(0);
79 80 sym->type = type;
80 81 sym->pos = pos;
81 82 sym->endpos.type = 0;
82 83 return sym;
83 84 }
84 85
85 86 struct struct_union_info {
86 87 unsigned long max_align;
87 88 unsigned long bit_size;
88 89 int align_size;
89 90 };
90 91
91 92 /*
92 93 * Unions are fairly easy to lay out ;)
93 94 */
94 95 static void lay_out_union(struct symbol *sym, struct struct_union_info *info)
95 96 {
96 97 examine_symbol_type(sym);
97 98
98 99 // Unnamed bitfields do not affect alignment.
99 100 if (sym->ident || !is_bitfield_type(sym)) {
100 101 if (sym->ctype.alignment > info->max_align)
101 102 info->max_align = sym->ctype.alignment;
102 103 }
103 104
104 105 if (sym->bit_size > info->bit_size)
105 106 info->bit_size = sym->bit_size;
106 107
107 108 sym->offset = 0;
108 109 }
109 110
110 111 static int bitfield_base_size(struct symbol *sym)
111 112 {
112 113 if (sym->type == SYM_NODE)
113 114 sym = sym->ctype.base_type;
114 115 if (sym->type == SYM_BITFIELD)
115 116 sym = sym->ctype.base_type;
116 117 return sym->bit_size;
117 118 }
118 119
119 120 /*
120 121 * Structures are a bit more interesting to lay out
121 122 */
122 123 static void lay_out_struct(struct symbol *sym, struct struct_union_info *info)
123 124 {
124 125 unsigned long bit_size, align_bit_mask;
125 126 int base_size;
126 127
127 128 examine_symbol_type(sym);
128 129
129 130 // Unnamed bitfields do not affect alignment.
130 131 if (sym->ident || !is_bitfield_type(sym)) {
131 132 if (sym->ctype.alignment > info->max_align)
132 133 info->max_align = sym->ctype.alignment;
133 134 }
134 135
135 136 bit_size = info->bit_size;
136 137 base_size = sym->bit_size;
137 138
138 139 /*
139 140 * Unsized arrays cause us to not align the resulting
140 141 * structure size
141 142 */
142 143 if (base_size < 0) {
143 144 info->align_size = 0;
144 145 base_size = 0;
145 146 }
146 147
147 148 align_bit_mask = bytes_to_bits(sym->ctype.alignment) - 1;
148 149
149 150 /*
150 151 * Bitfields have some very special rules..
151 152 */
152 153 if (is_bitfield_type (sym)) {
153 154 unsigned long bit_offset = bit_size & align_bit_mask;
154 155 int room = bitfield_base_size(sym) - bit_offset;
155 156 // Zero-width fields just fill up the unit.
156 157 int width = base_size ? : (bit_offset ? room : 0);
157 158
158 159 if (width > room) {
159 160 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
160 161 bit_offset = 0;
161 162 }
162 163 sym->offset = bits_to_bytes(bit_size - bit_offset);
163 164 sym->bit_offset = bit_offset;
164 165 sym->ctype.base_type->bit_offset = bit_offset;
165 166 info->bit_size = bit_size + width;
166 167 // warning (sym->pos, "bitfield: offset=%d:%d size=:%d", sym->offset, sym->bit_offset, width);
167 168
168 169 return;
169 170 }
170 171
171 172 /*
172 173 * Otherwise, just align it right and add it up..
173 174 */
174 175 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
175 176 sym->offset = bits_to_bytes(bit_size);
176 177
177 178 info->bit_size = bit_size + base_size;
178 179 // warning (sym->pos, "regular: offset=%d", sym->offset);
179 180 }
180 181
181 182 static struct symbol * examine_struct_union_type(struct symbol *sym, int advance)
182 183 {
183 184 struct struct_union_info info = {
184 185 .max_align = 1,
185 186 .bit_size = 0,
186 187 .align_size = 1
187 188 };
188 189 unsigned long bit_size, bit_align;
189 190 void (*fn)(struct symbol *, struct struct_union_info *);
190 191 struct symbol *member;
191 192
192 193 fn = advance ? lay_out_struct : lay_out_union;
193 194 FOR_EACH_PTR(sym->symbol_list, member) {
194 195 fn(member, &info);
195 196 } END_FOR_EACH_PTR(member);
196 197
197 198 if (!sym->ctype.alignment)
198 199 sym->ctype.alignment = info.max_align;
199 200 bit_size = info.bit_size;
200 201 if (info.align_size) {
201 202 bit_align = bytes_to_bits(sym->ctype.alignment)-1;
202 203 bit_size = (bit_size + bit_align) & ~bit_align;
203 204 }
204 205 sym->bit_size = bit_size;
205 206 return sym;
↓ open down ↓ |
142 lines elided |
↑ open up ↑ |
206 207 }
207 208
208 209 static struct symbol *examine_base_type(struct symbol *sym)
209 210 {
210 211 struct symbol *base_type;
211 212
212 213 /* Check the base type */
213 214 base_type = examine_symbol_type(sym->ctype.base_type);
214 215 if (!base_type || base_type->type == SYM_PTR)
215 216 return base_type;
216 - sym->ctype.as |= base_type->ctype.as;
217 + combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
217 218 sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
218 219 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
219 220 (struct ptr_list **)&sym->ctype.contexts);
220 221 if (base_type->type == SYM_NODE) {
221 222 base_type = base_type->ctype.base_type;
222 223 sym->ctype.base_type = base_type;
223 224 }
224 225 return base_type;
225 226 }
226 227
227 228 static struct symbol * examine_array_type(struct symbol *sym)
228 229 {
229 230 struct symbol *base_type = examine_base_type(sym);
230 231 unsigned long bit_size = -1, alignment;
231 232 struct expression *array_size = sym->array_size;
232 233
233 234 if (!base_type)
234 235 return sym;
235 236
236 237 if (array_size) {
237 238 bit_size = array_element_offset(base_type->bit_size,
238 239 get_expression_value_silent(array_size));
239 240 if (array_size->type != EXPR_VALUE) {
240 241 if (Wvla)
241 242 warning(array_size->pos, "Variable length array is used.");
242 243 bit_size = -1;
243 244 }
244 245 }
245 246 alignment = base_type->ctype.alignment;
246 247 if (!sym->ctype.alignment)
247 248 sym->ctype.alignment = alignment;
248 249 sym->bit_size = bit_size;
249 250 return sym;
250 251 }
251 252
252 253 static struct symbol *examine_bitfield_type(struct symbol *sym)
253 254 {
254 255 struct symbol *base_type = examine_base_type(sym);
255 256 unsigned long bit_size, alignment, modifiers;
256 257
257 258 if (!base_type)
258 259 return sym;
259 260 bit_size = base_type->bit_size;
260 261 if (sym->bit_size > bit_size)
261 262 warning(sym->pos, "impossible field-width, %d, for this type", sym->bit_size);
262 263
263 264 alignment = base_type->ctype.alignment;
264 265 if (!sym->ctype.alignment)
265 266 sym->ctype.alignment = alignment;
266 267 modifiers = base_type->ctype.modifiers;
267 268
268 269 /* Bitfields are unsigned, unless the base type was explicitly signed */
269 270 if (!(modifiers & MOD_EXPLICITLY_SIGNED))
↓ open down ↓ |
43 lines elided |
↑ open up ↑ |
270 271 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
271 272 sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
272 273 return sym;
273 274 }
274 275
275 276 /*
276 277 * "typeof" will have to merge the types together
277 278 */
278 279 void merge_type(struct symbol *sym, struct symbol *base_type)
279 280 {
280 - sym->ctype.as |= base_type->ctype.as;
281 + combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
281 282 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
282 283 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
283 284 (struct ptr_list **)&sym->ctype.contexts);
284 285 sym->ctype.base_type = base_type->ctype.base_type;
285 286 if (sym->ctype.base_type->type == SYM_NODE)
286 287 merge_type(sym, sym->ctype.base_type);
287 288 }
288 289
289 290 static int count_array_initializer(struct symbol *t, struct expression *expr)
290 291 {
291 292 int nr = 0;
292 293 int is_char = 0;
293 294
294 295 /*
295 296 * Arrays of character types are special; they can be initialized by
296 297 * string literal _or_ by string literal in braces. The latter means
297 298 * that with T x[] = {<string literal>} number of elements in x depends
298 299 * on T - if it's a character type, we get the length of string literal
299 300 * (including NUL), otherwise we have one element here.
300 301 */
301 302 if (t->ctype.base_type == &int_type && t->ctype.modifiers & MOD_CHAR)
302 303 is_char = 1;
303 304
304 305 switch (expr->type) {
305 306 case EXPR_INITIALIZER: {
306 307 struct expression *entry;
307 308 int count = 0;
308 309 int str_len = 0;
309 310 FOR_EACH_PTR(expr->expr_list, entry) {
310 311 count++;
311 312 switch (entry->type) {
312 313 case EXPR_INDEX:
313 314 if (entry->idx_to >= nr)
314 315 nr = entry->idx_to+1;
315 316 break;
316 317 case EXPR_PREOP: {
317 318 struct expression *e = entry;
318 319 if (is_char) {
319 320 while (e && e->type == EXPR_PREOP && e->op == '(')
320 321 e = e->unop;
321 322 if (e && e->type == EXPR_STRING) {
322 323 entry = e;
323 324 case EXPR_STRING:
324 325 if (is_char)
325 326 str_len = entry->string->length;
326 327 }
327 328
328 329
329 330 }
330 331 }
331 332 default:
332 333 nr++;
333 334 }
334 335 } END_FOR_EACH_PTR(entry);
335 336 if (count == 1 && str_len)
336 337 nr = str_len;
337 338 break;
338 339 }
339 340 case EXPR_PREOP:
340 341 if (is_char) {
341 342 struct expression *e = expr;
342 343 while (e && e->type == EXPR_PREOP && e->op == '(')
343 344 e = e->unop;
344 345 if (e && e->type == EXPR_STRING) {
345 346 expr = e;
346 347 case EXPR_STRING:
347 348 if (is_char)
348 349 nr = expr->string->length;
349 350 }
350 351 }
351 352 break;
352 353 default:
353 354 break;
354 355 }
355 356 return nr;
356 357 }
↓ open down ↓ |
66 lines elided |
↑ open up ↑ |
357 358
358 359 static struct expression *get_symbol_initializer(struct symbol *sym)
359 360 {
360 361 do {
361 362 if (sym->initializer)
362 363 return sym->initializer;
363 364 } while ((sym = sym->same_symbol) != NULL);
364 365 return NULL;
365 366 }
366 367
368 +static unsigned int implicit_array_size(struct symbol *node, unsigned int count)
369 +{
370 + struct symbol *arr_ori = node->ctype.base_type;
371 + struct symbol *arr_new = alloc_symbol(node->pos, SYM_ARRAY);
372 + struct symbol *elem_type = arr_ori->ctype.base_type;
373 + struct expression *size = alloc_const_expression(node->pos, count);
374 + unsigned int bit_size = array_element_offset(elem_type->bit_size, count);
375 +
376 + *arr_new = *arr_ori;
377 + arr_new->bit_size = bit_size;
378 + arr_new->array_size = size;
379 + node->array_size = size;
380 + node->ctype.base_type = arr_new;
381 +
382 + return bit_size;
383 +}
384 +
367 385 static struct symbol * examine_node_type(struct symbol *sym)
368 386 {
369 387 struct symbol *base_type = examine_base_type(sym);
370 388 int bit_size;
371 389 unsigned long alignment;
372 390
373 391 /* SYM_NODE - figure out what the type of the node was.. */
374 392 bit_size = 0;
375 393 alignment = 0;
376 394 if (!base_type)
377 395 return sym;
378 396
379 397 bit_size = base_type->bit_size;
380 398 alignment = base_type->ctype.alignment;
381 399
382 400 /* Pick up signedness information into the node */
383 401 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
384 402
385 403 if (!sym->ctype.alignment)
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
386 404 sym->ctype.alignment = alignment;
387 405
388 406 /* Unsized array? The size might come from the initializer.. */
389 407 if (bit_size < 0 && base_type->type == SYM_ARRAY) {
390 408 struct expression *initializer = get_symbol_initializer(sym);
391 409 if (initializer) {
392 410 struct symbol *node_type = base_type->ctype.base_type;
393 411 int count = count_array_initializer(node_type, initializer);
394 412
395 413 if (node_type && node_type->bit_size >= 0)
396 - bit_size = array_element_offset(node_type->bit_size, count);
414 + bit_size = implicit_array_size(sym, count);
397 415 }
398 416 }
399 417
400 418 sym->bit_size = bit_size;
401 419 return sym;
402 420 }
403 421
404 422 static struct symbol *examine_enum_type(struct symbol *sym)
405 423 {
406 424 struct symbol *base_type = examine_base_type(sym);
407 425
408 426 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
409 427 sym->bit_size = bits_in_enum;
410 428 if (base_type->bit_size > sym->bit_size)
411 429 sym->bit_size = base_type->bit_size;
412 430 sym->ctype.alignment = enum_alignment;
413 431 if (base_type->ctype.alignment > sym->ctype.alignment)
414 432 sym->ctype.alignment = base_type->ctype.alignment;
415 433 return sym;
416 434 }
417 435
418 436 static struct symbol *examine_pointer_type(struct symbol *sym)
419 437 {
420 438 /*
421 439 * We need to set the pointer size first, and
422 440 * examine the thing we point to only afterwards.
423 441 * That's because this pointer type may end up
424 442 * being needed for the base type size evaluation.
425 443 */
426 444 if (!sym->bit_size)
427 445 sym->bit_size = bits_in_pointer;
428 446 if (!sym->ctype.alignment)
429 447 sym->ctype.alignment = pointer_alignment;
430 448 return sym;
431 449 }
432 450
433 451 /*
434 452 * Fill in type size and alignment information for
435 453 * regular SYM_TYPE things.
436 454 */
437 455 struct symbol *examine_symbol_type(struct symbol * sym)
438 456 {
439 457 if (!sym)
440 458 return sym;
441 459
442 460 /* Already done? */
443 461 if (sym->examined)
444 462 return sym;
445 463 sym->examined = 1;
446 464
447 465 switch (sym->type) {
448 466 case SYM_FN:
449 467 case SYM_NODE:
450 468 return examine_node_type(sym);
451 469 case SYM_ARRAY:
452 470 return examine_array_type(sym);
453 471 case SYM_STRUCT:
454 472 return examine_struct_union_type(sym, 1);
455 473 case SYM_UNION:
456 474 return examine_struct_union_type(sym, 0);
457 475 case SYM_PTR:
458 476 return examine_pointer_type(sym);
459 477 case SYM_ENUM:
460 478 return examine_enum_type(sym);
461 479 case SYM_BITFIELD:
462 480 return examine_bitfield_type(sym);
463 481 case SYM_BASETYPE:
464 482 /* Size and alignment had better already be set up */
465 483 return sym;
466 484 case SYM_TYPEOF: {
467 485 struct symbol *base = evaluate_expression(sym->initializer);
468 486 if (base) {
469 487 unsigned long mod = 0;
470 488
471 489 if (is_bitfield_type(base))
↓ open down ↓ |
65 lines elided |
↑ open up ↑ |
472 490 warning(base->pos, "typeof applied to bitfield type");
473 491 if (base->type == SYM_NODE) {
474 492 mod |= base->ctype.modifiers & MOD_TYPEOF;
475 493 base = base->ctype.base_type;
476 494 }
477 495 sym->type = SYM_NODE;
478 496 sym->ctype.modifiers = mod;
479 497 sym->ctype.base_type = base;
480 498 return examine_node_type(sym);
481 499 }
482 - break;
500 + sym->type = SYM_NODE;
501 + sym->ctype.base_type = &bad_ctype;
502 + return sym;
483 503 }
484 504 case SYM_PREPROCESSOR:
485 505 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
486 506 return NULL;
487 507 case SYM_UNINITIALIZED:
488 -// sparse_error(sym->pos, "ctype on uninitialized symbol %p", sym);
508 +// sparse_error(sym->pos, "ctype on uninitialized symbol '%s'", show_typename(sym));
489 509 return NULL;
490 510 case SYM_RESTRICT:
491 511 examine_base_type(sym);
492 512 return sym;
493 513 case SYM_FOULED:
494 514 examine_base_type(sym);
495 515 return sym;
496 516 default:
497 517 // sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
498 518 break;
499 519 }
500 520 return sym;
501 521 }
502 522
503 523 const char* get_type_name(enum type type)
504 524 {
505 525 const char *type_lookup[] = {
506 526 [SYM_UNINITIALIZED] = "uninitialized",
507 527 [SYM_PREPROCESSOR] = "preprocessor",
508 528 [SYM_BASETYPE] = "basetype",
509 529 [SYM_NODE] = "node",
510 530 [SYM_PTR] = "pointer",
511 531 [SYM_FN] = "function",
512 532 [SYM_ARRAY] = "array",
513 533 [SYM_STRUCT] = "struct",
514 534 [SYM_UNION] = "union",
515 535 [SYM_ENUM] = "enum",
516 536 [SYM_TYPEDEF] = "typedef",
517 537 [SYM_TYPEOF] = "typeof",
518 538 [SYM_MEMBER] = "member",
519 539 [SYM_BITFIELD] = "bitfield",
520 540 [SYM_LABEL] = "label",
521 541 [SYM_RESTRICT] = "restrict",
522 542 [SYM_FOULED] = "fouled",
523 543 [SYM_KEYWORD] = "keyword",
524 544 [SYM_BAD] = "bad"};
525 545
526 546 if (type <= SYM_BAD)
527 547 return type_lookup[type];
528 548 else
529 549 return NULL;
530 550 }
531 551
532 552 struct symbol *examine_pointer_target(struct symbol *sym)
533 553 {
534 554 return examine_base_type(sym);
535 555 }
536 556
537 557 static struct symbol_list *restr, *fouled;
538 558
539 559 void create_fouled(struct symbol *type)
540 560 {
541 561 if (type->bit_size < bits_in_int) {
542 562 struct symbol *new = alloc_symbol(type->pos, type->type);
543 563 *new = *type;
544 564 new->bit_size = bits_in_int;
545 565 new->type = SYM_FOULED;
546 566 new->ctype.base_type = type;
547 567 add_symbol(&restr, type);
548 568 add_symbol(&fouled, new);
549 569 }
550 570 }
551 571
552 572 struct symbol *befoul(struct symbol *type)
553 573 {
554 574 struct symbol *t1, *t2;
555 575 while (type->type == SYM_NODE)
556 576 type = type->ctype.base_type;
557 577 PREPARE_PTR_LIST(restr, t1);
558 578 PREPARE_PTR_LIST(fouled, t2);
559 579 for (;;) {
560 580 if (t1 == type)
561 581 return t2;
562 582 if (!t1)
563 583 break;
564 584 NEXT_PTR_LIST(t1);
565 585 NEXT_PTR_LIST(t2);
566 586 }
567 587 FINISH_PTR_LIST(t2);
568 588 FINISH_PTR_LIST(t1);
569 589 return NULL;
570 590 }
571 591
572 592 void check_declaration(struct symbol *sym)
573 593 {
574 594 int warned = 0;
575 595 struct symbol *next = sym;
576 596
577 597 while ((next = next->next_id) != NULL) {
578 598 if (next->namespace != sym->namespace)
579 599 continue;
580 600 if (sym->scope == next->scope) {
581 601 sym->same_symbol = next;
582 602 return;
583 603 }
584 604 /* Extern in block level matches a TOPLEVEL non-static symbol */
585 605 if (sym->ctype.modifiers & MOD_EXTERN) {
586 606 if ((next->ctype.modifiers & (MOD_TOPLEVEL|MOD_STATIC)) == MOD_TOPLEVEL) {
587 607 sym->same_symbol = next;
588 608 return;
589 609 }
590 610 }
591 611
592 612 if (!Wshadow || warned)
593 613 continue;
594 614 if (get_sym_type(next) == SYM_FN)
595 615 continue;
596 616 warned = 1;
597 617 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
598 618 info(next->pos, "originally declared here");
599 619 }
600 620 }
601 621
602 622 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
603 623 {
604 624 struct scope *scope;
605 625 if (sym->bound) {
606 626 sparse_error(sym->pos, "internal error: symbol type already bound");
607 627 return;
608 628 }
609 629 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
610 630 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
611 631 return;
612 632 }
613 633 sym->namespace = ns;
614 634 sym->next_id = ident->symbols;
615 635 ident->symbols = sym;
616 636 if (sym->ident && sym->ident != ident)
617 637 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
618 638 sym->ident = ident;
619 639 sym->bound = 1;
620 640
621 641 scope = block_scope;
622 642 if (ns == NS_SYMBOL && toplevel(scope)) {
623 643 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
624 644
625 645 scope = global_scope;
626 646 if (sym->ctype.modifiers & MOD_STATIC ||
627 647 is_extern_inline(sym)) {
628 648 scope = file_scope;
629 649 mod = MOD_TOPLEVEL;
630 650 }
631 651 sym->ctype.modifiers |= mod;
632 652 }
633 653 if (ns == NS_MACRO)
634 654 scope = file_scope;
635 655 if (ns == NS_LABEL)
636 656 scope = function_scope;
637 657 bind_scope(sym, scope);
638 658 }
639 659
640 660 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
641 661 {
642 662 struct ident *ident = built_in_ident(name);
643 663 struct symbol *sym = lookup_symbol(ident, namespace);
644 664
645 665 if (sym && sym->type != type)
646 666 die("symbol %s created with different types: %d old %d", name,
647 667 type, sym->type);
648 668
649 669 if (!sym) {
650 670 struct token *token = built_in_token(stream, ident);
651 671
652 672 sym = alloc_symbol(token->pos, type);
653 673 bind_symbol(sym, token->ident, namespace);
654 674 }
655 675 return sym;
656 676 }
657 677
658 678
659 679 /*
660 680 * Abstract types
661 681 */
662 682 struct symbol int_type,
663 683 fp_type;
664 684
665 685 /*
666 686 * C types (i.e. actual instances that the abstract types
667 687 * can map onto)
668 688 */
669 689 struct symbol bool_ctype, void_ctype, type_ctype,
↓ open down ↓ |
171 lines elided |
↑ open up ↑ |
670 690 char_ctype, schar_ctype, uchar_ctype,
671 691 short_ctype, sshort_ctype, ushort_ctype,
672 692 int_ctype, sint_ctype, uint_ctype,
673 693 long_ctype, slong_ctype, ulong_ctype,
674 694 llong_ctype, sllong_ctype, ullong_ctype,
675 695 lllong_ctype, slllong_ctype, ulllong_ctype,
676 696 float_ctype, double_ctype, ldouble_ctype,
677 697 string_ctype, ptr_ctype, lazy_ptr_ctype,
678 698 incomplete_ctype, label_ctype, bad_ctype,
679 699 null_ctype;
700 +struct symbol int_ptr_ctype, uint_ptr_ctype;
701 +struct symbol long_ptr_ctype, ulong_ptr_ctype;
702 +struct symbol llong_ptr_ctype, ullong_ptr_ctype;
703 +struct symbol float32_ctype, float32x_ctype;
704 +struct symbol float64_ctype, float64x_ctype;
705 +struct symbol float128_ctype;
706 +struct symbol const_void_ctype, const_char_ctype;
707 +struct symbol const_ptr_ctype, const_string_ctype;
680 708
681 709 struct symbol zero_int;
682 710
683 711 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
684 712 #define __IDENT(n,str,res) \
685 713 struct ident n = __INIT_IDENT(str,res)
686 714
687 715 #include "ident-list.h"
688 716
689 717 void init_symbols(void)
690 718 {
691 719 int stream = init_stream("builtin", -1, includepath);
692 720
693 721 #define __IDENT(n,str,res) \
694 722 hash_ident(&n)
695 723 #include "ident-list.h"
↓ open down ↓ |
6 lines elided |
↑ open up ↑ |
696 724
697 725 init_parser(stream);
698 726 init_builtins(stream);
699 727 }
700 728
701 729 #ifdef __CHAR_UNSIGNED__
702 730 #define CHAR_SIGNEDNESS MOD_UNSIGNED
703 731 #else
704 732 #define CHAR_SIGNEDNESS MOD_SIGNED
705 733 #endif
734 +// For fix-sized types
735 +static int bits_in_type32 = 32;
736 +static int bits_in_type64 = 64;
737 +static int bits_in_type128 = 128;
706 738
707 739 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
708 740 #define MOD_LL (MOD_LONG | MOD_LONGLONG)
709 741 #define MOD_LLL MOD_LONGLONGLONG
710 742 static const struct ctype_declare {
711 743 struct symbol *ptr;
712 744 enum type type;
713 745 unsigned long modifiers;
714 746 int *bit_size;
715 747 int *maxalign;
716 748 struct symbol *base_type;
717 749 } ctype_declaration[] = {
718 750 { &bool_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_bool, &max_int_alignment, &int_type },
719 751 { &void_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
720 752 { &type_ctype, SYM_BASETYPE, MOD_TYPE, NULL, NULL, NULL },
721 753 { &incomplete_ctype,SYM_BASETYPE, 0, NULL, NULL, NULL },
722 754 { &bad_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
723 755
724 756 { &char_ctype, SYM_BASETYPE, CHAR_SIGNEDNESS | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
725 757 { &schar_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
726 758 { &uchar_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
727 759 { &short_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
728 760 { &sshort_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
729 761 { &ushort_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
730 762 { &int_ctype, SYM_BASETYPE, MOD_SIGNED, &bits_in_int, &max_int_alignment, &int_type },
731 763 { &sint_ctype, SYM_BASETYPE, MOD_ESIGNED, &bits_in_int, &max_int_alignment, &int_type },
732 764 { &uint_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_int, &max_int_alignment, &int_type },
733 765 { &long_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
734 766 { &slong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
735 767 { &ulong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
736 768 { &llong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
↓ open down ↓ |
21 lines elided |
↑ open up ↑ |
737 769 { &sllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
738 770 { &ullong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
739 771 { &lllong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
740 772 { &slllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
741 773 { &ulllong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
742 774
743 775 { &float_ctype, SYM_BASETYPE, 0, &bits_in_float, &max_fp_alignment, &fp_type },
744 776 { &double_ctype, SYM_BASETYPE, MOD_LONG, &bits_in_double, &max_fp_alignment, &fp_type },
745 777 { &ldouble_ctype, SYM_BASETYPE, MOD_LONG | MOD_LONGLONG, &bits_in_longdouble, &max_fp_alignment, &fp_type },
746 778
779 + { &float32_ctype, SYM_BASETYPE, 0, &bits_in_type32, &max_fp_alignment, &fp_type },
780 + { &float32x_ctype, SYM_BASETYPE, MOD_LONG, &bits_in_double, &max_fp_alignment, &fp_type },
781 + { &float64_ctype, SYM_BASETYPE, 0, &bits_in_type64, &max_fp_alignment, &fp_type },
782 + { &float64x_ctype, SYM_BASETYPE, MOD_LONG | MOD_LONGLONG, &bits_in_longdouble, &max_fp_alignment, &fp_type },
783 + { &float128_ctype, SYM_BASETYPE, 0, &bits_in_type128, &max_alignment, &fp_type },
784 +
747 785 { &string_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &char_ctype },
748 786 { &ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
749 787 { &null_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
750 788 { &label_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
751 789 { &lazy_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
790 + { &int_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &int_ctype },
791 + { &uint_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &uint_ctype },
792 + { &long_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &long_ctype },
793 + { &ulong_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &ulong_ctype },
794 + { &llong_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &llong_ctype },
795 + { &ullong_ptr_ctype,SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &ullong_ctype },
796 +
797 + { &const_void_ctype, SYM_NODE, MOD_CONST, NULL, NULL, &void_ctype },
798 + { &const_char_ctype, SYM_NODE, MOD_CONST, &bits_in_char, &max_int_alignment, &char_ctype },
799 + { &const_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &const_void_ctype },
800 + { &const_string_ctype,SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &const_char_ctype },
752 801 { NULL, }
753 802 };
754 803 #undef MOD_LLL
755 804 #undef MOD_LL
756 805 #undef MOD_ESIGNED
757 806
758 807 void init_ctype(void)
759 808 {
760 809 const struct ctype_declare *ctype;
761 810
762 811 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
763 812 struct symbol *sym = ctype->ptr;
764 813 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
765 814 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
766 815 unsigned long alignment = bits_to_bytes(bit_size);
767 816
768 817 if (alignment > maxalign)
769 818 alignment = maxalign;
770 819 sym->type = ctype->type;
771 820 sym->bit_size = bit_size;
772 821 sym->ctype.alignment = alignment;
773 822 sym->ctype.base_type = ctype->base_type;
774 823 sym->ctype.modifiers = ctype->modifiers;
775 824 }
825 +
826 + // and now some adjustments
827 + if (funsigned_char) {
828 + char_ctype.ctype.modifiers |= MOD_UNSIGNED;
829 + char_ctype.ctype.modifiers &= ~MOD_SIGNED;
830 + }
776 831 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX