2 * Symbol lookup and handling.
4 * Copyright (C) 2003 Transmeta Corp.
6 * Licensed under the Open Software License version 1.1
18 #include "expression.h"
23 * Secondary symbol list for stuff that needs to be output because it
26 struct symbol_list
*used_list
= NULL
;
29 * If the symbol is an inline symbol, add it to the list of symbols to parse
31 void access_symbol(struct symbol
*sym
)
33 if (sym
->ctype
.modifiers
& MOD_INLINE
) {
34 if (!(sym
->ctype
.modifiers
& MOD_ACCESSED
)) {
35 add_symbol(&used_list
, sym
);
36 sym
->ctype
.modifiers
|= MOD_ACCESSED
;
41 struct symbol
*lookup_symbol(struct ident
*ident
, enum namespace ns
)
45 for (sym
= ident
->symbols
; sym
; sym
= sym
->next_id
) {
46 if (sym
->namespace == ns
) {
54 struct symbol
*alloc_symbol(struct position pos
, int type
)
56 struct symbol
*sym
= __alloc_symbol(0);
62 struct struct_union_info
{
63 unsigned long max_align
;
64 unsigned long bit_size
;
68 * Unions are easy to lay out ;)
70 static void lay_out_union(struct symbol
*sym
, void *_info
, int flags
)
72 struct struct_union_info
*info
= _info
;
74 examine_symbol_type(sym
);
75 if (sym
->ctype
.alignment
> info
->max_align
)
76 info
->max_align
= sym
->ctype
.alignment
;
77 if (sym
->bit_size
> info
->bit_size
)
78 info
->bit_size
= sym
->bit_size
;
84 * Structures are a bit more interesting to lay out
86 static void lay_out_struct(struct symbol
*sym
, void *_info
, int flags
)
88 struct struct_union_info
*info
= _info
;
89 unsigned long bit_size
, base_size
;
90 unsigned long align_bit_mask
;
92 examine_symbol_type(sym
);
93 if (sym
->ctype
.alignment
> info
->max_align
)
94 info
->max_align
= sym
->ctype
.alignment
;
96 bit_size
= info
->bit_size
;
97 base_size
= sym
->bit_size
;
98 align_bit_mask
= (sym
->ctype
.alignment
<< 3) - 1;
101 * Bitfields have some very special rules..
103 if (sym
->fieldwidth
) {
104 unsigned long bit_offset
= bit_size
& align_bit_mask
;
106 if (bit_offset
+ sym
->fieldwidth
> base_size
) {
107 bit_size
= (bit_size
+ align_bit_mask
) & ~align_bit_mask
;
110 sym
->offset
= (bit_size
- bit_offset
) >> 3;
111 sym
->bit_offset
= bit_offset
;
112 info
->bit_size
= bit_size
+ sym
->fieldwidth
;
117 * Otherwise, just align it right and add it up..
119 bit_size
= (bit_size
+ align_bit_mask
) & ~align_bit_mask
;
120 sym
->offset
= bit_size
>> 3;
122 info
->bit_size
= bit_size
+ sym
->bit_size
;
125 static void examine_struct_union_type(struct symbol
*sym
, int advance
)
127 struct struct_union_info info
= { 1, 0 };
128 unsigned long bit_size
, bit_align
;
129 void (*fn
)(struct symbol
*, void *, int);
131 fn
= advance
? lay_out_struct
: lay_out_union
;
132 symbol_iterate(sym
->symbol_list
, fn
, &info
);
134 if (!sym
->ctype
.alignment
)
135 sym
->ctype
.alignment
= info
.max_align
;
136 bit_size
= info
.bit_size
;
137 bit_align
= (sym
->ctype
.alignment
<< 3)-1;
138 bit_size
= (bit_size
+ bit_align
) & ~bit_align
;
139 sym
->bit_size
= bit_size
;
142 static void examine_array_type(struct symbol
*sym
)
144 struct symbol
*base_type
= sym
->ctype
.base_type
;
145 unsigned long bit_size
, alignment
;
149 examine_symbol_type(base_type
);
150 bit_size
= base_type
->bit_size
* sym
->array_size
;
151 if (sym
->array_size
< 0)
153 alignment
= base_type
->ctype
.alignment
;
154 if (!sym
->ctype
.alignment
)
155 sym
->ctype
.alignment
= alignment
;
156 sym
->bit_size
= bit_size
;
159 static void examine_bitfield_type(struct symbol
*sym
)
161 struct symbol
*base_type
= sym
->ctype
.base_type
;
162 unsigned long bit_size
, alignment
;
166 examine_symbol_type(base_type
);
167 bit_size
= base_type
->bit_size
;
168 if (sym
->fieldwidth
> bit_size
) {
169 warn(sym
->pos
, "impossible field-width for this type");
170 sym
->fieldwidth
= bit_size
;
172 alignment
= base_type
->ctype
.alignment
;
173 if (!sym
->ctype
.alignment
)
174 sym
->ctype
.alignment
= alignment
;
175 sym
->bit_size
= bit_size
;
179 * "typeof" will have to merge the types together
181 void merge_type(struct symbol
*sym
, struct symbol
*base_type
)
183 sym
->ctype
.as
|= base_type
->ctype
.as
;
184 sym
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& ~MOD_STORAGE
);
185 sym
->ctype
.context
|= base_type
->ctype
.context
;
186 sym
->ctype
.contextmask
|= base_type
->ctype
.contextmask
;
187 sym
->ctype
.base_type
= base_type
->ctype
.base_type
;
191 * Fill in type size and alignment information for
192 * regular SYM_TYPE things.
194 struct symbol
*examine_symbol_type(struct symbol
* sym
)
196 unsigned int bit_size
, alignment
;
197 struct symbol
*base_type
;
198 unsigned long modifiers
;
209 examine_array_type(sym
);
212 examine_struct_union_type(sym
, 1);
215 examine_struct_union_type(sym
, 0);
219 sym
->bit_size
= BITS_IN_POINTER
;
220 if (!sym
->ctype
.alignment
)
221 sym
->ctype
.alignment
= POINTER_ALIGNMENT
;
222 base_type
= sym
->ctype
.base_type
;
223 base_type
= examine_symbol_type(base_type
);
224 if (base_type
&& base_type
->type
== SYM_NODE
)
225 merge_type(sym
, base_type
);
229 sym
->bit_size
= BITS_IN_ENUM
;
230 if (!sym
->ctype
.alignment
)
231 sym
->ctype
.alignment
= ENUM_ALIGNMENT
;
234 examine_bitfield_type(sym
);
237 /* Size and alignment had better already be set up */
240 struct symbol
*base
= evaluate_expression(sym
->initializer
);
249 /* SYM_NODE - figure out what the type of the node was.. */
250 base_type
= sym
->ctype
.base_type
;
251 modifiers
= sym
->ctype
.modifiers
;
254 base_type
= examine_symbol_type(base_type
);
255 sym
->ctype
.base_type
= base_type
;
256 if (base_type
&& base_type
->type
== SYM_NODE
)
257 merge_type(sym
, base_type
);
259 bit_size
= base_type
->bit_size
;
260 alignment
= base_type
->ctype
.alignment
;
261 if (base_type
->fieldwidth
)
262 sym
->fieldwidth
= base_type
->fieldwidth
;
266 if (!sym
->ctype
.alignment
)
267 sym
->ctype
.alignment
= alignment
;
268 sym
->bit_size
= bit_size
;
272 void check_declaration(struct symbol
*sym
)
274 struct symbol
*next
= sym
;
276 while ((next
= next
->next_id
) != NULL
) {
277 if (next
->namespace != sym
->namespace)
279 if (sym
->scope
== next
->scope
) {
280 sym
->same_symbol
= next
;
283 if (sym
->ctype
.modifiers
& next
->ctype
.modifiers
& MOD_EXTERN
) {
284 sym
->same_symbol
= next
;
288 // This may make sense from a warning standpoint:
289 // consider top-level symbols to clash with everything
290 // (but the scoping rules will mean that we actually
291 // _use_ the innermost version)
292 if (toplevel(next
->scope
)) {
293 sym
->same_symbol
= next
;
300 void bind_symbol(struct symbol
*sym
, struct ident
*ident
, enum namespace ns
)
304 warn(sym
->pos
, "internal error: symbol type already bound");
308 sym
->next_id
= ident
->symbols
;
309 ident
->symbols
= sym
;
310 sym
->id_list
= &ident
->symbols
;
313 if (toplevel(scope
)) {
314 sym
->ctype
.modifiers
|= MOD_TOPLEVEL
;
315 if (sym
->ctype
.modifiers
& MOD_STATIC
)
319 scope
= function_scope
;
320 bind_scope(sym
, scope
);
323 static struct symbol
*create_symbol(int stream
, const char *name
, int type
, int namespace)
325 struct token
*token
= built_in_token(stream
, name
);
326 struct symbol
*sym
= alloc_symbol(token
->pos
, type
);
327 bind_symbol(sym
, token
->ident
, namespace);
331 struct symbol
*evaluate_constant_p(struct expression
*expr
)
333 struct expression
*arg
;
334 struct expression_list
*arglist
= expr
->args
;
337 FOR_EACH_PTR (arglist
, arg
) {
338 if (arg
->type
!= EXPR_VALUE
)
342 expr
->ctype
= &int_ctype
;
343 expr
->type
= EXPR_VALUE
;
349 * Type and storage class keywords need to have the symbols
350 * created for them, so that the parser can have enough semantic
351 * information to do parsing.
353 * "double" == "long float", "long double" == "long long float"
357 struct symbol
*base_type
;
358 unsigned int modifiers
;
359 struct symbol
*(*evaluate
)(struct expression
*);
360 } symbol_init_table
[] = {
362 { "auto", NULL
, MOD_AUTO
},
363 { "register", NULL
, MOD_REGISTER
},
364 { "static", NULL
, MOD_STATIC
},
365 { "extern", NULL
, MOD_EXTERN
},
367 /* Type specifiers */
368 { "void", &void_ctype
, 0 },
369 { "char", &int_type
, MOD_CHAR
},
370 { "short", &int_type
, MOD_SHORT
},
371 { "int", &int_type
, 0 },
372 { "long", NULL
, MOD_LONG
},
373 { "float", &fp_type
, 0 },
374 { "double", &fp_type
, MOD_LONG
},
375 { "signed", &int_type
, MOD_SIGNED
},
376 { "__signed", &int_type
, MOD_SIGNED
},
377 { "__signed__", &int_type
, MOD_SIGNED
},
378 { "unsigned", &int_type
, MOD_UNSIGNED
},
380 /* Type qualifiers */
381 { "const", NULL
, MOD_CONST
},
382 { "__const", NULL
, MOD_CONST
},
383 { "__const__", NULL
, MOD_CONST
},
384 { "volatile", NULL
, MOD_VOLATILE
},
385 { "__volatile", NULL
, MOD_VOLATILE
},
386 { "__volatile__", NULL
, MOD_VOLATILE
},
388 /* Predeclared types */
389 { "__builtin_va_list", &int_type
, 0 },
392 { "typedef", NULL
, MOD_TYPEDEF
},
395 { "typeof", NULL
, MOD_TYPEOF
},
396 { "__typeof", NULL
, MOD_TYPEOF
},
397 { "__typeof__", NULL
, MOD_TYPEOF
},
400 { "attribute", NULL
, MOD_ATTRIBUTE
},
401 { "__attribute", NULL
, MOD_ATTRIBUTE
},
403 { "__attribute__", NULL
, MOD_ATTRIBUTE
},
405 { "struct", NULL
, MOD_STRUCTOF
},
406 { "union", NULL
, MOD_UNIONOF
},
407 { "enum", NULL
, MOD_ENUMOF
},
409 { "inline", NULL
, MOD_INLINE
},
410 { "__inline", NULL
, MOD_INLINE
},
411 { "__inline__", NULL
, MOD_INLINE
},
413 /* Ignored for now.. */
414 { "restrict", NULL
, 0 },
415 { "__restrict", NULL
, 0 },
423 struct sym_init eval_init_table
[] = {
424 { "__builtin_constant_p", &int_type
, 0, evaluate_constant_p
},
433 struct symbol int_type
,
439 * C types (ie actual instances that the abstract types
442 struct symbol bool_ctype
, void_ctype
,
443 char_ctype
, uchar_ctype
,
444 short_ctype
, ushort_ctype
,
445 int_ctype
, uint_ctype
,
446 long_ctype
, ulong_ctype
,
447 llong_ctype
, ullong_ctype
,
448 float_ctype
, double_ctype
, ldouble_ctype
,
449 string_ctype
, ptr_ctype
;
451 struct ctype_declare
{
453 unsigned long modifiers
;
454 unsigned long bit_size
;
455 unsigned long maxalign
;
456 struct symbol
*base_type
;
457 } ctype_declaration
[] = {
458 { &bool_ctype
, 0, BITS_IN_INT
, MAX_INT_ALIGNMENT
, &int_type
},
459 { &void_ctype
, 0, -1, 0, NULL
},
461 { &char_ctype
, MOD_SIGNED
| MOD_CHAR
, BITS_IN_CHAR
, MAX_INT_ALIGNMENT
, &int_type
},
462 { &uchar_ctype
, MOD_UNSIGNED
| MOD_CHAR
, BITS_IN_CHAR
, MAX_INT_ALIGNMENT
, &int_type
},
463 { &short_ctype
, MOD_SIGNED
| MOD_SHORT
, BITS_IN_SHORT
, MAX_INT_ALIGNMENT
, &int_type
},
464 { &ushort_ctype
, MOD_UNSIGNED
| MOD_SHORT
, BITS_IN_SHORT
, MAX_INT_ALIGNMENT
, &int_type
},
465 { &int_ctype
, MOD_SIGNED
, BITS_IN_INT
, MAX_INT_ALIGNMENT
, &int_type
},
466 { &uint_ctype
, MOD_UNSIGNED
, BITS_IN_INT
, MAX_INT_ALIGNMENT
, &int_type
},
467 { &long_ctype
, MOD_SIGNED
| MOD_LONG
, BITS_IN_LONG
, MAX_INT_ALIGNMENT
, &int_type
},
468 { &ulong_ctype
, MOD_UNSIGNED
| MOD_LONG
, BITS_IN_LONG
, MAX_INT_ALIGNMENT
, &int_type
},
469 { &llong_ctype
, MOD_SIGNED
| MOD_LONG
| MOD_LONGLONG
, BITS_IN_LONGLONG
, MAX_INT_ALIGNMENT
, &int_type
},
470 { &ullong_ctype
, MOD_UNSIGNED
| MOD_LONG
| MOD_LONGLONG
, BITS_IN_LONGLONG
, MAX_INT_ALIGNMENT
, &int_type
},
472 { &float_ctype
, 0, BITS_IN_FLOAT
, MAX_FP_ALIGNMENT
, &fp_type
},
473 { &double_ctype
, MOD_LONG
, BITS_IN_DOUBLE
, MAX_FP_ALIGNMENT
, &fp_type
},
474 { &ldouble_ctype
,MOD_LONG
| MOD_LONGLONG
, BITS_IN_LONGDOUBLE
,MAX_FP_ALIGNMENT
, &fp_type
},
476 { &string_ctype
, 0, BITS_IN_POINTER
, POINTER_ALIGNMENT
, &char_ctype
},
477 { &ptr_ctype
, 0, BITS_IN_POINTER
, POINTER_ALIGNMENT
, &void_ctype
},
482 #define __IDENT(n,str) \
483 struct ident n ## _ident = { len: sizeof(str)-1, name: str }
484 #define IDENT(n) __IDENT(n, #n)
486 IDENT(struct); IDENT(union); IDENT(enum);
488 IDENT(alignof
); IDENT(__alignof
); IDENT(__alignof__
);
489 IDENT(if); IDENT(else); IDENT(return);
490 IDENT(switch); IDENT(case); IDENT(default);
491 IDENT(break); IDENT(continue);
492 IDENT(for); IDENT(while); IDENT(do); IDENT(goto);
494 IDENT(__asm__
); IDENT(__asm
); IDENT(asm);
495 IDENT(__volatile__
); IDENT(__volatile
); IDENT(volatile);
496 IDENT(__attribute__
); IDENT(__attribute
);
498 void init_symbols(void)
500 int stream
= init_stream("builtin", -1);
501 struct sym_init
*ptr
;
502 struct ctype_declare
*ctype
;
504 hash_ident(&sizeof_ident
);
505 hash_ident(&alignof_ident
);
506 hash_ident(&__alignof_ident
);
507 hash_ident(&__alignof___ident
);
508 hash_ident(&if_ident
);
509 hash_ident(&else_ident
);
510 hash_ident(&return_ident
);
511 hash_ident(&switch_ident
);
512 hash_ident(&case_ident
);
513 hash_ident(&default_ident
);
514 hash_ident(&break_ident
);
515 hash_ident(&continue_ident
);
516 hash_ident(&for_ident
);
517 hash_ident(&while_ident
);
518 hash_ident(&do_ident
);
519 hash_ident(&goto_ident
);
520 hash_ident(&__attribute___ident
);
521 hash_ident(&__attribute_ident
);
522 hash_ident(&__asm___ident
);
523 hash_ident(&__asm_ident
);
524 hash_ident(&asm_ident
);
525 hash_ident(&__volatile___ident
);
526 hash_ident(&__volatile_ident
);
527 hash_ident(&volatile_ident
);
528 for (ptr
= symbol_init_table
; ptr
->name
; ptr
++) {
530 sym
= create_symbol(stream
, ptr
->name
, SYM_NODE
, NS_TYPEDEF
);
531 sym
->ctype
.base_type
= ptr
->base_type
;
532 sym
->ctype
.modifiers
= ptr
->modifiers
;
535 for (ptr
= eval_init_table
; ptr
->name
; ptr
++) {
537 sym
= create_symbol(stream
, ptr
->name
, SYM_NODE
, NS_SYMBOL
);
538 sym
->ctype
.base_type
= ptr
->base_type
;
539 sym
->ctype
.modifiers
= ptr
->modifiers
;
540 sym
->evaluate
= ptr
->evaluate
;
543 ptr_ctype
.type
= SYM_PTR
;
544 string_ctype
.type
= SYM_PTR
;
545 for (ctype
= ctype_declaration
; ctype
->ptr
; ctype
++) {
546 struct symbol
*sym
= ctype
->ptr
;
547 unsigned long bit_size
= ctype
->bit_size
;
548 unsigned long alignment
= bit_size
>> 3;
550 if (alignment
> ctype
->maxalign
)
551 alignment
= ctype
->maxalign
;
552 sym
->bit_size
= bit_size
;
553 sym
->ctype
.alignment
= alignment
;
554 sym
->ctype
.base_type
= ctype
->base_type
;
555 sym
->ctype
.modifiers
= ctype
->modifiers
;