9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
15 #include "adt/array.h"
20 #include "lang_features.h"
21 #include "driver/firm_opt.h"
22 #include "driver/firm_cmdline.h"
24 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
26 static ir_type *ir_type_const_char;
27 static ir_type *ir_type_wchar_t;
28 static ir_type *ir_type_void;
29 static ir_type *ir_type_int;
31 static type_t *type_const_char;
32 static type_t *type_void;
33 static type_t *type_int;
35 static int next_value_number_function;
36 static ir_node *continue_label;
37 static ir_node *break_label;
38 static ir_node *current_switch_cond;
39 static bool saw_default_label;
40 static ir_node **imature_blocks;
42 static const declaration_t *current_function_decl;
43 static ir_node *current_function_name;
45 static struct obstack asm_obst;
47 typedef enum declaration_kind_t {
48 DECLARATION_KIND_UNKNOWN,
49 DECLARATION_KIND_FUNCTION,
50 DECLARATION_KIND_GLOBAL_VARIABLE,
51 DECLARATION_KIND_LOCAL_VARIABLE,
52 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
53 DECLARATION_KIND_COMPOUND_MEMBER,
54 DECLARATION_KIND_LABEL_BLOCK,
55 DECLARATION_KIND_ENUM_ENTRY
58 static ir_type *get_ir_type(type_t *type);
59 static int count_decls_in_stmts(const statement_t *stmt);
61 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
65 const declaration_t *declaration = & value_numbers[pos]->declaration;
67 print_warning_prefix(declaration->source_position);
68 fprintf(stderr, "variable '%s' might be used uninitialized\n",
69 declaration->symbol->string);
71 fprintf(stderr, "Some variable might be used uninitialized\n");
72 return new_r_Unknown(irg, mode);
75 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
77 const source_position_t *pos = (const source_position_t*) dbg;
80 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
84 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
86 const source_position_t *pos = (const source_position_t*) dbg;
91 return pos->input_name;
94 static dbg_info *get_dbg_info(const source_position_t *pos)
96 return (dbg_info*) pos;
99 static unsigned unique_id = 0;
101 static ident *unique_ident(const char *tag)
105 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
107 return new_id_from_str(buf);
111 * Return the signed integer mode of size bytes.
113 * @param size the size
115 static ir_mode *get_smode(unsigned size)
117 static ir_mode *s_modes[16 + 1] = {0, };
120 if (size <= 0 || size > 16)
129 snprintf(name, sizeof(name), "i%u", bits);
130 res = new_ir_mode(name, irms_int_number, bits, 1, irma_twos_complement,
131 bits <= machine_size ? machine_size : bits );
139 * Return the unsigned integer mode of size bytes.
141 * @param size the size
143 static ir_mode *get_umode(unsigned size)
145 static ir_mode *u_modes[16 + 1] = {0, };
148 if (size <= 0 || size > 16)
157 snprintf(name, sizeof(name), "u%u", bits);
158 res = new_ir_mode(name, irms_int_number, bits, 0, irma_twos_complement,
159 bits <= machine_size ? machine_size : bits );
167 * Return the pointer mode of size bytes.
169 * @param size the size
171 static ir_mode *get_ptrmode(unsigned size, char *name)
173 static ir_mode *p_modes[16 + 1] = {0, };
176 if (size <= 0 || size > 16)
186 snprintf(buf, sizeof(buf), "p%u", bits);
189 res = new_ir_mode(name, irms_reference, bits, 0, irma_twos_complement,
190 bits <= machine_size ? machine_size : bits);
194 set_reference_mode_signed_eq(res, get_smode(size));
195 set_reference_mode_unsigned_eq(res, get_umode(size));
200 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST];
202 static ir_mode *mode_int, *mode_uint;
205 * Initialises the atomic modes depending on the machine size.
207 static void init_atomic_modes(void) {
208 unsigned int_size = machine_size < 32 ? 2 : 4;
209 unsigned long_size = machine_size < 64 ? 4 : 8;
210 unsigned llong_size = machine_size < 32 ? 4 : 8;
212 /* firm has no real void... */
213 _atomic_modes[ATOMIC_TYPE_VOID] = mode_T;
214 _atomic_modes[ATOMIC_TYPE_CHAR] = char_is_signed ? get_smode(1) : get_umode(1);
215 _atomic_modes[ATOMIC_TYPE_SCHAR] = get_smode(1);
216 _atomic_modes[ATOMIC_TYPE_UCHAR] = get_umode(1);
217 _atomic_modes[ATOMIC_TYPE_SHORT] = get_smode(2);
218 _atomic_modes[ATOMIC_TYPE_USHORT] = get_umode(2);
219 _atomic_modes[ATOMIC_TYPE_INT] = get_smode(int_size);
220 _atomic_modes[ATOMIC_TYPE_UINT] = get_umode(int_size);
221 _atomic_modes[ATOMIC_TYPE_LONG] = get_smode(long_size);
222 _atomic_modes[ATOMIC_TYPE_ULONG] = get_umode(long_size);
223 _atomic_modes[ATOMIC_TYPE_LONGLONG] = get_smode(llong_size);
224 _atomic_modes[ATOMIC_TYPE_ULONGLONG] = get_umode(llong_size);
225 _atomic_modes[ATOMIC_TYPE_FLOAT] = mode_F;
226 _atomic_modes[ATOMIC_TYPE_DOUBLE] = mode_D;
227 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = mode_E;
228 _atomic_modes[ATOMIC_TYPE_BOOL] = get_umode(int_size);
230 #ifdef PROVIDE_COMPLEX
231 _atomic_modes[ATOMIC_TYPE_BOOL] = _atomic_modes[ATOMIC_TYPE_INT];
232 _atomic_modes[ATOMIC_TYPE_FLOAT_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_FLOAT];
233 _atomic_modes[ATOMIC_TYPE_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_DOUBLE];
234 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE];
237 /* Hmm, pointers should be machine size */
238 set_modeP_data(get_ptrmode(machine_size >> 3, NULL));
239 set_modeP_code(get_ptrmode(machine_size >> 3, NULL));
241 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
242 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
245 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
248 if ((unsigned)atomic_type->atype < (unsigned)ATOMIC_TYPE_LAST)
249 res = _atomic_modes[(unsigned)atomic_type->atype];
251 panic("Encountered unknown atomic type");
255 static unsigned get_type_size(type_t *type);
257 static unsigned get_atomic_type_size(const atomic_type_t *type)
259 switch(type->atype) {
260 case ATOMIC_TYPE_CHAR:
261 case ATOMIC_TYPE_SCHAR:
262 case ATOMIC_TYPE_UCHAR:
265 case ATOMIC_TYPE_SHORT:
266 case ATOMIC_TYPE_USHORT:
269 case ATOMIC_TYPE_BOOL:
270 case ATOMIC_TYPE_INT:
271 case ATOMIC_TYPE_UINT:
272 return machine_size >> 3;
274 case ATOMIC_TYPE_LONG:
275 case ATOMIC_TYPE_ULONG:
276 return machine_size > 16 ? machine_size >> 3 : 4;
278 case ATOMIC_TYPE_LONGLONG:
279 case ATOMIC_TYPE_ULONGLONG:
280 return machine_size > 16 ? 8 : 4;
282 case ATOMIC_TYPE_FLOAT:
285 case ATOMIC_TYPE_DOUBLE:
288 case ATOMIC_TYPE_LONG_DOUBLE:
291 case ATOMIC_TYPE_VOID:
294 case ATOMIC_TYPE_INVALID:
295 case ATOMIC_TYPE_LAST:
298 panic("Trying to determine size of invalid atomic type");
301 static unsigned get_compound_type_size(compound_type_t *type)
303 ir_type *irtype = get_ir_type((type_t*) type);
304 return get_type_size_bytes(irtype);
307 static unsigned get_array_type_size(array_type_t *type)
309 ir_type *irtype = get_ir_type((type_t*) type);
310 return get_type_size_bytes(irtype);
314 static unsigned get_type_size(type_t *type)
316 type = skip_typeref(type);
320 return get_atomic_type_size(&type->atomic);
322 return get_mode_size_bytes(mode_int);
323 case TYPE_COMPOUND_UNION:
324 case TYPE_COMPOUND_STRUCT:
325 return get_compound_type_size(&type->compound);
327 /* just a pointer to the function */
328 return get_mode_size_bytes(mode_P_code);
330 return get_mode_size_bytes(mode_P_data);
332 return get_array_type_size(&type->array);
334 return get_type_size(type->builtin.real_type);
336 panic("type size of bitfield request");
342 panic("Trying to determine size of invalid type");
345 static unsigned count_parameters(const function_type_t *function_type)
349 function_parameter_t *parameter = function_type->parameters;
350 for ( ; parameter != NULL; parameter = parameter->next) {
360 static long fold_constant(const expression_t *expression);
362 static ir_type *create_atomic_type(const atomic_type_t *type)
364 ir_mode *mode = get_atomic_mode(type);
365 ident *id = get_mode_ident(mode);
366 ir_type *irtype = new_type_primitive(id, mode);
368 if(type->atype == ATOMIC_TYPE_LONG_DOUBLE) {
369 set_type_alignment_bytes(irtype, 4);
375 static ir_type *create_method_type(const function_type_t *function_type)
377 type_t *return_type = function_type->return_type;
379 ident *id = unique_ident("functiontype");
380 int n_parameters = count_parameters(function_type);
381 int n_results = return_type == type_void ? 0 : 1;
382 ir_type *irtype = new_type_method(id, n_parameters, n_results);
384 if(return_type != type_void) {
385 ir_type *restype = get_ir_type(return_type);
386 set_method_res_type(irtype, 0, restype);
389 function_parameter_t *parameter = function_type->parameters;
391 for( ; parameter != NULL; parameter = parameter->next) {
392 ir_type *p_irtype = get_ir_type(parameter->type);
393 set_method_param_type(irtype, n, p_irtype);
397 if(function_type->variadic || function_type->unspecified_parameters) {
398 set_method_variadicity(irtype, variadicity_variadic);
404 static ir_type *create_pointer_type(pointer_type_t *type)
406 type_t *points_to = type->points_to;
407 ir_type *ir_points_to;
408 /* Avoid endless recursion if the points_to type contains this poiner type
409 * again (might be a struct). We therefore first create a void* pointer
410 * and then set the real points_to type
412 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
413 ir_type_void, mode_P_data);
414 type->type.firm_type = ir_type;
416 ir_points_to = get_ir_type(points_to);
417 set_pointer_points_to_type(ir_type, ir_points_to);
422 static ir_type *create_array_type(array_type_t *type)
424 type_t *element_type = type->element_type;
425 ir_type *ir_element_type = get_ir_type(element_type);
427 ident *id = unique_ident("array");
428 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
430 if(type->size != NULL) {
431 int n_elements = fold_constant(type->size);
433 set_array_bounds_int(ir_type, 0, 0, n_elements);
435 size_t elemsize = get_type_size_bytes(ir_element_type);
436 int align = get_type_alignment_bytes(ir_element_type);
437 if(elemsize % align > 0) {
438 elemsize += align - (elemsize % align);
440 set_type_size_bytes(ir_type, n_elements * elemsize);
441 set_type_alignment_bytes(ir_type, align);
442 set_type_state(ir_type, layout_fixed);
444 set_array_lower_bound_int(ir_type, 0, 0);
450 #define INVALID_TYPE ((ir_type_ptr)-1)
452 static ir_type *create_struct_type(compound_type_t *type)
454 symbol_t *symbol = type->declaration->symbol;
457 id = unique_ident(symbol->string);
459 id = unique_ident("__anonymous_struct");
461 ir_type *irtype = new_type_struct(id);
463 type->type.firm_type = irtype;
465 size_t align_all = 1;
467 size_t bit_offset = 0;
468 declaration_t *entry = type->declaration->context.declarations;
469 for( ; entry != NULL; entry = entry->next) {
470 if(entry->namespc != NAMESPACE_NORMAL)
473 type_t *entry_type = skip_typeref(entry->type);
475 ir_type *base_irtype;
476 if(entry_type->kind == TYPE_BITFIELD) {
477 base_irtype = get_ir_type(entry_type->bitfield.base);
479 base_irtype = get_ir_type(entry_type);
482 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
483 size_t misalign = offset % entry_alignment;
485 dbg_info *dbgi = get_dbg_info(&entry->source_position);
486 ir_entity *entity = NULL;
487 if(entry->symbol != NULL) {
488 ident *ident = new_id_from_str(entry->symbol->string);
489 ir_type *entry_irtype = get_ir_type(entry_type);
491 entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
493 /* only bitfields are allowed to be anonymous */
494 assert(entry_type->kind == TYPE_BITFIELD);
498 size_t bits_remainder;
499 if(entry_type->kind == TYPE_BITFIELD) {
500 size_t size_bits = fold_constant(entry_type->bitfield.size);
501 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
503 if(size_bits > rest_size_bits) {
504 /* start a new bucket */
505 offset += entry_alignment - misalign;
511 /* put into current bucket */
512 base = offset - misalign;
513 bits_remainder = misalign * 8 + bit_offset;
516 offset += size_bits / 8;
517 bit_offset = bit_offset + (size_bits % 8);
519 size_t entry_size = get_type_size_bytes(base_irtype);
521 offset += entry_size - misalign;
525 offset += entry_size;
529 if(entry_alignment > align_all) {
530 if(entry_alignment % align_all != 0) {
531 panic("uneven alignments not supported yet");
533 align_all = entry_alignment;
537 set_entity_offset(entity, base);
538 set_entity_offset_bits_remainder(entity,
539 (unsigned char) bits_remainder);
540 add_struct_member(irtype, entity);
541 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
542 entry->v.entity = entity;
546 size_t misalign = offset % align_all;
548 offset += align_all - misalign;
550 set_type_alignment_bytes(irtype, align_all);
551 set_type_size_bytes(irtype, offset);
552 set_type_state(irtype, layout_fixed);
557 static ir_type *create_union_type(compound_type_t *type)
559 declaration_t *declaration = type->declaration;
560 symbol_t *symbol = declaration->symbol;
563 id = unique_ident(symbol->string);
565 id = unique_ident("__anonymous_union");
567 ir_type *irtype = new_type_union(id);
569 type->type.firm_type = irtype;
573 declaration_t *entry = declaration->context.declarations;
574 for( ; entry != NULL; entry = entry->next) {
575 if(entry->namespc != NAMESPACE_NORMAL)
578 ident *ident = new_id_from_str(entry->symbol->string);
579 ir_type *entry_ir_type = get_ir_type(entry->type);
581 int entry_size = get_type_size_bytes(entry_ir_type);
582 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
584 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
585 ir_entity *const entity = new_d_entity(irtype, ident, entry_ir_type,
587 add_union_member(irtype, entity);
588 set_entity_offset(entity, 0);
589 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
590 entry->v.entity = entity;
592 if(entry_size > size) {
595 if(entry_alignment > align_all) {
596 if(entry_alignment % align_all != 0) {
597 panic("Uneven alignments not supported yet");
599 align_all = entry_alignment;
603 set_type_alignment_bytes(irtype, align_all);
604 set_type_size_bytes(irtype, size);
605 set_type_state(irtype, layout_fixed);
610 static ir_node *expression_to_firm(const expression_t *expression);
611 static inline ir_mode *get_ir_mode(type_t *type);
613 static ir_type *create_enum_type(enum_type_t *const type)
615 type->type.firm_type = ir_type_int;
617 ir_mode *const mode = get_ir_mode((type_t*) type);
618 tarval *const one = get_mode_one(mode);
619 tarval * tv_next = get_tarval_null(mode);
621 declaration_t *declaration = type->declaration->next;
622 for (; declaration != NULL; declaration = declaration->next) {
623 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
626 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
628 expression_t *const init = declaration->init.enum_value;
630 ir_node *const cnst = expression_to_firm(init);
631 if (!is_Const(cnst)) {
632 panic("couldn't fold constant");
634 tv_next = get_Const_tarval(cnst);
636 declaration->v.enum_val = tv_next;
637 tv_next = tarval_add(tv_next, one);
644 * Return the signed integer type of size bits.
646 * @param size the size
648 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
651 static ir_mode *s_modes[64 + 1] = {NULL, };
655 if (size <= 0 || size > 64)
658 mode = s_modes[size];
662 snprintf(name, sizeof(name), "bf_I%u", size);
663 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
664 size <= 32 ? 32 : size );
665 s_modes[size] = mode;
669 snprintf(name, sizeof(name), "I%u", size);
670 ident *id = new_id_from_str(name);
671 res = new_type_primitive(mangle_u(get_type_ident(base_tp), id), mode);
672 set_primitive_base_type(res, base_tp);
678 * Return the unsigned integer type of size bits.
680 * @param size the size
682 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
685 static ir_mode *u_modes[64 + 1] = {NULL, };
689 if (size <= 0 || size > 64)
692 mode = u_modes[size];
696 snprintf(name, sizeof(name), "bf_U%u", size);
697 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
698 size <= 32 ? 32 : size );
699 u_modes[size] = mode;
704 snprintf(name, sizeof(name), "U%u", size);
705 ident *id = new_id_from_str(name);
706 res = new_type_primitive(mangle_u(get_type_ident(base_tp), id), mode);
707 set_primitive_base_type(res, base_tp);
712 static ir_type *create_bitfield_type(bitfield_type_t *const type)
714 type_t *base = skip_typeref(type->base);
715 assert(base->kind == TYPE_ATOMIC);
717 ir_type *irbase = get_ir_type(base);
719 unsigned size = fold_constant(type->size);
721 assert(!is_type_floating(base));
722 if(is_type_signed(base)) {
723 return get_signed_int_type_for_bit_size(irbase, size);
725 return get_unsigned_int_type_for_bit_size(irbase, size);
729 static ir_type *get_ir_type(type_t *type)
731 assert(type != NULL);
733 type = skip_typeref(type);
735 if(type->base.firm_type != NULL) {
736 assert(type->base.firm_type != INVALID_TYPE);
737 return type->base.firm_type;
740 ir_type *firm_type = NULL;
743 firm_type = create_atomic_type(&type->atomic);
746 firm_type = create_method_type(&type->function);
749 firm_type = create_pointer_type(&type->pointer);
752 firm_type = create_array_type(&type->array);
754 case TYPE_COMPOUND_STRUCT:
755 firm_type = create_struct_type(&type->compound);
757 case TYPE_COMPOUND_UNION:
758 firm_type = create_union_type(&type->compound);
761 firm_type = create_enum_type(&type->enumt);
764 firm_type = get_ir_type(type->builtin.real_type);
767 firm_type = create_bitfield_type(&type->bitfield);
775 if(firm_type == NULL)
776 panic("unknown type found");
778 type->base.firm_type = firm_type;
782 static inline ir_mode *get_ir_mode(type_t *type)
784 ir_type *irtype = get_ir_type(type);
786 /* firm doesn't report a mode for arrays somehow... */
787 if(is_Array_type(irtype)) {
791 ir_mode *mode = get_type_mode(irtype);
792 assert(mode != NULL);
796 static ident *predef_idents[rts_max];
798 /** Names of the runtime functions. */
799 static const struct {
800 int id; /**< the rts id */
801 int n_res; /**< number of return values */
802 const char *name; /**< the name of the rts function */
803 int n_params; /**< number of parameters */
804 unsigned flags; /**< language flags */
806 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
807 { rts_abort, 0, "abort", 0, _C89 },
808 { rts_abs, 1, "abs", 1, _C89 },
809 { rts_labs, 1, "labs", 1, _C89 },
810 { rts_llabs, 1, "llabs", 1, _C99 },
811 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
813 { rts_fabs, 1, "fabs", 1, _C89 },
814 { rts_sqrt, 1, "sqrt", 1, _C89 },
815 { rts_cbrt, 1, "cbrt", 1, _C99 },
816 { rts_exp, 1, "exp", 1, _C89 },
817 { rts_exp2, 1, "exp2", 1, _C89 },
818 { rts_exp10, 1, "exp10", 1, _GNUC },
819 { rts_log, 1, "log", 1, _C89 },
820 { rts_log2, 1, "log2", 1, _C89 },
821 { rts_log10, 1, "log10", 1, _C89 },
822 { rts_pow, 1, "pow", 2, _C89 },
823 { rts_sin, 1, "sin", 1, _C89 },
824 { rts_cos, 1, "cos", 1, _C89 },
825 { rts_tan, 1, "tan", 1, _C89 },
826 { rts_asin, 1, "asin", 1, _C89 },
827 { rts_acos, 1, "acos", 1, _C89 },
828 { rts_atan, 1, "atan", 1, _C89 },
829 { rts_sinh, 1, "sinh", 1, _C89 },
830 { rts_cosh, 1, "cosh", 1, _C89 },
831 { rts_tanh, 1, "tanh", 1, _C89 },
833 { rts_fabsf, 1, "fabsf", 1, _C99 },
834 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
835 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
836 { rts_expf, 1, "expf", 1, _C99 },
837 { rts_exp2f, 1, "exp2f", 1, _C99 },
838 { rts_exp10f, 1, "exp10f", 1, _GNUC },
839 { rts_logf, 1, "logf", 1, _C99 },
840 { rts_log2f, 1, "log2f", 1, _C99 },
841 { rts_log10f, 1, "log10f", 1, _C99 },
842 { rts_powf, 1, "powf", 2, _C99 },
843 { rts_sinf, 1, "sinf", 1, _C99 },
844 { rts_cosf, 1, "cosf", 1, _C99 },
845 { rts_tanf, 1, "tanf", 1, _C99 },
846 { rts_asinf, 1, "asinf", 1, _C99 },
847 { rts_acosf, 1, "acosf", 1, _C99 },
848 { rts_atanf, 1, "atanf", 1, _C99 },
849 { rts_sinhf, 1, "sinhf", 1, _C99 },
850 { rts_coshf, 1, "coshf", 1, _C99 },
851 { rts_tanhf, 1, "tanhf", 1, _C99 },
853 { rts_fabsl, 1, "fabsl", 1, _C99 },
854 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
855 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
856 { rts_expl, 1, "expl", 1, _C99 },
857 { rts_exp2l, 1, "exp2l", 1, _C99 },
858 { rts_exp10l, 1, "exp10l", 1, _GNUC },
859 { rts_logl, 1, "logl", 1, _C99 },
860 { rts_log2l, 1, "log2l", 1, _C99 },
861 { rts_log10l, 1, "log10l", 1, _C99 },
862 { rts_powl, 1, "powl", 2, _C99 },
863 { rts_sinl, 1, "sinl", 1, _C99 },
864 { rts_cosl, 1, "cosl", 1, _C99 },
865 { rts_tanl, 1, "tanl", 1, _C99 },
866 { rts_asinl, 1, "asinl", 1, _C99 },
867 { rts_acosl, 1, "acosl", 1, _C99 },
868 { rts_atanl, 1, "atanl", 1, _C99 },
869 { rts_sinhl, 1, "sinhl", 1, _C99 },
870 { rts_coshl, 1, "coshl", 1, _C99 },
871 { rts_tanhl, 1, "tanhl", 1, _C99 },
873 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
874 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
875 { rts_strcmp, 1, "strcmp", 2, _C89 },
876 { rts_strncmp, 1, "strncmp", 3, _C89 }
879 static ir_entity* get_function_entity(declaration_t *declaration)
881 if(declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
882 return declaration->v.entity;
883 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
885 symbol_t *symbol = declaration->symbol;
886 ident *id = new_id_from_str(symbol->string);
888 ir_type *global_type = get_glob_type();
889 ir_type *ir_type_method = get_ir_type(declaration->type);
890 assert(is_Method_type(ir_type_method));
892 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
893 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
894 set_entity_ld_ident(entity, id);
895 if(declaration->storage_class == STORAGE_CLASS_STATIC
896 || declaration->is_inline) {
897 set_entity_visibility(entity, visibility_local);
898 } else if(declaration->init.statement != NULL) {
899 set_entity_visibility(entity, visibility_external_visible);
901 set_entity_visibility(entity, visibility_external_allocated);
903 /* We should check for file scope here, but as long as we compile C only
904 this is not needed. */
905 int n_params = get_method_n_params(ir_type_method);
906 int n_res = get_method_n_ress(ir_type_method);
909 if (n_params == 0 && n_res == 0 && id == predef_idents[rts_abort]) {
910 /* found abort(), store for later */
914 if (! firm_opt.freestanding) {
915 /* check for a known runtime function */
916 for (i = 0; i < rts_max; ++i) {
917 /* ignore those rts functions not necessary needed for current mode */
918 if ((c_mode & rts_data[i].flags) == 0)
920 if (n_params == rts_data[i].n_params && n_res == rts_data[i].n_res &&
921 id == predef_idents[rts_data[i].id])
922 rts_entities[rts_data[i].id] = entity;
927 set_entity_allocation(entity, allocation_static);
929 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
930 declaration->v.entity = entity;
935 static ir_node *const_to_firm(const const_expression_t *cnst)
937 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
938 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
943 if(mode_is_float(mode)) {
944 tv = new_tarval_from_double(cnst->v.float_value, mode);
946 if(mode_is_signed(mode)) {
947 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
949 len = snprintf(buf, sizeof(buf), "%llu",
950 (unsigned long long) cnst->v.int_value);
952 tv = new_tarval_from_str(buf, len, mode);
955 return new_d_Const(dbgi, mode, tv);
958 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
961 assert(entity != NULL);
962 union symconst_symbol sym;
963 sym.entity_p = entity;
964 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
967 static ir_node *string_to_firm(const source_position_t *const src_pos,
968 const char *const id_prefix,
969 const char *const string)
971 ir_type *const global_type = get_glob_type();
972 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
975 ident *const id = unique_ident(id_prefix);
976 dbg_info *const dbgi = get_dbg_info(src_pos);
977 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
978 set_entity_ld_ident(entity, id);
979 set_entity_variability(entity, variability_constant);
980 set_entity_allocation(entity, allocation_static);
982 ir_type *const elem_type = ir_type_const_char;
983 ir_mode *const mode = get_type_mode(elem_type);
985 const size_t slen = strlen(string) + 1;
987 set_array_lower_bound_int(type, 0, 0);
988 set_array_upper_bound_int(type, 0, slen);
989 set_type_size_bytes(type, slen);
990 set_type_state(type, layout_fixed);
992 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
993 for(size_t i = 0; i < slen; ++i) {
994 tvs[i] = new_tarval_from_long(string[i], mode);
997 set_array_entity_values(entity, tvs, slen);
1000 return create_symconst(dbgi, mode_P_data, entity);
1003 static ir_node *string_literal_to_firm(
1004 const string_literal_expression_t* literal)
1006 return string_to_firm(&literal->expression.source_position, "Lstr",
1010 static ir_node *wide_string_literal_to_firm(
1011 const wide_string_literal_expression_t* const literal)
1013 ir_type *const global_type = get_glob_type();
1014 ir_type *const elem_type = ir_type_wchar_t;
1015 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
1018 ident *const id = unique_ident("Lstr");
1019 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
1020 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1021 set_entity_ld_ident(entity, id);
1022 set_entity_variability(entity, variability_constant);
1023 set_entity_allocation(entity, allocation_static);
1025 ir_mode *const mode = get_type_mode(elem_type);
1027 const wchar_rep_t *const string = literal->value.begin;
1028 const size_t slen = literal->value.size;
1030 set_array_lower_bound_int(type, 0, 0);
1031 set_array_upper_bound_int(type, 0, slen);
1032 set_type_size_bytes(type, slen);
1033 set_type_state(type, layout_fixed);
1035 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1036 for(size_t i = 0; i < slen; ++i) {
1037 tvs[i] = new_tarval_from_long(string[i], mode);
1040 set_array_entity_values(entity, tvs, slen);
1043 return create_symconst(dbgi, mode_P_data, entity);
1046 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
1047 dbg_info *const dbgi)
1049 if(is_compound_type(irtype) || is_Array_type(irtype)) {
1053 ir_mode *const mode = get_type_mode(irtype);
1054 ir_node *const memory = get_store();
1055 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1056 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1057 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1058 set_store(load_mem);
1062 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1064 ir_mode *mode = get_irn_mode(node);
1066 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1068 if(!mode_is_float(mode))
1071 /* check if there is already a Conv */
1072 if (get_irn_op(node) == op_Conv) {
1073 /* convert it into a strict Conv */
1074 set_Conv_strict(node, 1);
1078 /* otherwise create a new one */
1079 return new_d_strictConv(dbgi, node, mode);
1082 static ir_node *get_global_var_address(dbg_info *const dbgi,
1083 const declaration_t *const decl)
1085 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1087 ir_entity *const entity = decl->v.entity;
1088 switch ((storage_class_tag_t)decl->storage_class) {
1089 case STORAGE_CLASS_THREAD:
1090 case STORAGE_CLASS_THREAD_EXTERN:
1091 case STORAGE_CLASS_THREAD_STATIC: {
1092 ir_node *const no_mem = new_NoMem();
1093 ir_node *const tls = get_irg_tls(current_ir_graph);
1094 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1098 return create_symconst(dbgi, mode_P_data, entity);
1102 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1104 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
1105 declaration_t *declaration = ref->declaration;
1106 type_t *type = skip_typeref(declaration->type);
1108 switch((declaration_kind_t) declaration->declaration_kind) {
1109 case DECLARATION_KIND_UNKNOWN:
1110 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
1116 case DECLARATION_KIND_ENUM_ENTRY: {
1117 ir_mode *const mode = get_ir_mode(type);
1118 return new_Const(mode, declaration->v.enum_val);
1121 case DECLARATION_KIND_LOCAL_VARIABLE: {
1122 ir_mode *const mode = get_ir_mode(type);
1123 return get_value(declaration->v.value_number, mode);
1125 case DECLARATION_KIND_FUNCTION: {
1126 ir_mode *const mode = get_ir_mode(type);
1127 return create_symconst(dbgi, mode, declaration->v.entity);
1129 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1130 ir_node *const addr = get_global_var_address(dbgi, declaration);
1131 ir_type *const irtype = get_entity_type(declaration->v.entity);
1132 return deref_address(irtype, addr, dbgi);
1135 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1136 ir_entity *entity = declaration->v.entity;
1137 ir_node *frame = get_irg_frame(current_ir_graph);
1138 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1139 ir_type *irtype = get_entity_type(entity);
1140 return deref_address(irtype, sel, dbgi);
1143 case DECLARATION_KIND_COMPOUND_MEMBER:
1144 case DECLARATION_KIND_LABEL_BLOCK:
1145 panic("not implemented reference type");
1148 panic("reference to declaration with unknown type found");
1151 static ir_node *reference_addr(const reference_expression_t *ref)
1153 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
1154 declaration_t *declaration = ref->declaration;
1156 switch((declaration_kind_t) declaration->declaration_kind) {
1157 case DECLARATION_KIND_UNKNOWN:
1159 case DECLARATION_KIND_LOCAL_VARIABLE:
1160 panic("local variable without entity has no address");
1161 case DECLARATION_KIND_FUNCTION: {
1162 type_t *const type = skip_typeref(ref->expression.datatype);
1163 ir_mode *const mode = get_ir_mode(type);
1164 return create_symconst(dbgi, mode, declaration->v.entity);
1166 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1167 ir_node *const addr = get_global_var_address(dbgi, declaration);
1170 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1171 ir_entity *entity = declaration->v.entity;
1172 ir_node *frame = get_irg_frame(current_ir_graph);
1173 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1178 case DECLARATION_KIND_ENUM_ENTRY:
1179 panic("trying to reference enum entry");
1181 case DECLARATION_KIND_COMPOUND_MEMBER:
1182 case DECLARATION_KIND_LABEL_BLOCK:
1183 panic("not implemented reference type");
1186 panic("reference to declaration with unknown type found");
1189 static ir_node *process_builtin_call(const call_expression_t *call)
1191 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1193 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1194 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1196 type_t *type = skip_typeref(builtin->expression.datatype);
1197 assert(is_type_pointer(type));
1199 type_t *function_type = skip_typeref(type->pointer.points_to);
1200 symbol_t *symbol = builtin->symbol;
1202 switch(symbol->ID) {
1203 case T___builtin_alloca: {
1204 if(call->arguments == NULL || call->arguments->next != NULL) {
1205 panic("invalid number of parameters on __builtin_alloca");
1207 expression_t *argument = call->arguments->expression;
1208 ir_node *size = expression_to_firm(argument);
1210 ir_node *store = get_store();
1211 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1213 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1215 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1219 case T___builtin_nan:
1220 case T___builtin_nanf:
1221 case T___builtin_nand: {
1222 /* Ignore string for now... */
1223 assert(is_type_function(function_type));
1224 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1225 tarval *tv = get_mode_NAN(mode);
1226 ir_node *res = new_d_Const(dbgi, mode, tv);
1229 case T___builtin_va_end:
1232 panic("Unsupported builtin found\n");
1236 static ir_node *call_expression_to_firm(const call_expression_t *call)
1238 assert(get_cur_block() != NULL);
1240 expression_t *function = call->function;
1241 if(function->kind == EXPR_BUILTIN_SYMBOL) {
1242 return process_builtin_call(call);
1244 ir_node *callee = expression_to_firm(function);
1246 type_t *type = skip_typeref(function->base.datatype);
1247 assert(is_type_pointer(type));
1248 pointer_type_t *pointer_type = &type->pointer;
1249 type_t *points_to = skip_typeref(pointer_type->points_to);
1250 assert(is_type_function(points_to));
1251 function_type_t *function_type = &points_to->function;
1253 int n_parameters = 0;
1254 call_argument_t *argument = call->arguments;
1255 for( ; argument != NULL; argument = argument->next) {
1259 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1261 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1262 ir_type *new_method_type = NULL;
1263 if(function_type->variadic || function_type->unspecified_parameters) {
1264 /* we need to construct a new method type matching the call
1266 int n_res = get_method_n_ress(ir_method_type);
1267 new_method_type = new_type_method(unique_ident("calltype"),
1268 n_parameters, n_res);
1269 set_method_calling_convention(new_method_type,
1270 get_method_calling_convention(ir_method_type));
1271 set_method_additional_properties(new_method_type,
1272 get_method_additional_properties(ir_method_type));
1274 for(int i = 0; i < n_res; ++i) {
1275 set_method_res_type(new_method_type, i,
1276 get_method_res_type(ir_method_type, i));
1279 ir_node *in[n_parameters];
1281 argument = call->arguments;
1283 for( ; argument != NULL; argument = argument->next) {
1284 expression_t *expression = argument->expression;
1285 ir_node *arg_node = expression_to_firm(expression);
1287 arg_node = do_strict_conv(dbgi, arg_node);
1290 if(new_method_type != NULL) {
1291 ir_type *irtype = get_ir_type(expression->base.datatype);
1292 set_method_param_type(new_method_type, n, irtype);
1297 assert(n == n_parameters);
1299 if(new_method_type != NULL)
1300 ir_method_type = new_method_type;
1302 ir_node *store = get_store();
1303 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1305 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1308 type_t *return_type = skip_typeref(function_type->return_type);
1309 ir_node *result = NULL;
1311 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1313 if(is_type_scalar(return_type)) {
1314 mode = get_ir_mode(return_type);
1318 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1319 result = new_d_Proj(dbgi, resproj, mode, 0);
1325 static void statement_to_firm(statement_t *statement);
1326 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1328 static ir_node *expression_to_addr(const expression_t *expression);
1329 static void create_condition_evaluation(const expression_t *expression,
1330 ir_node *true_block,
1331 ir_node *false_block);
1333 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1336 value = do_strict_conv(dbgi, value);
1338 ir_node *memory = get_store();
1340 if(is_type_scalar(type)) {
1341 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1342 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1343 set_store(store_mem);
1345 ir_type *irtype = get_ir_type(type);
1346 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1347 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1348 set_store(copyb_mem);
1352 static void set_value_for_expression(const expression_t *expression,
1355 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1356 value = do_strict_conv(dbgi, value);
1358 if(expression->kind == EXPR_REFERENCE) {
1359 const reference_expression_t *ref = &expression->reference;
1361 declaration_t *declaration = ref->declaration;
1362 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1363 if(declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1364 set_value(declaration->v.value_number, value);
1369 ir_node *addr = expression_to_addr(expression);
1370 type_t *type = skip_typeref(expression->base.datatype);
1371 assign_value(dbgi, addr, type, value);
1374 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1376 ir_mode *value_mode = get_irn_mode(value);
1378 if (value_mode == dest_mode || is_Bad(value))
1381 if(dest_mode == mode_b) {
1382 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1383 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1384 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1388 return new_d_Conv(dbgi, value, dest_mode);
1391 static ir_node *create_incdec(const unary_expression_t *expression)
1393 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1394 type_t *type = skip_typeref(expression->expression.datatype);
1395 ir_mode *mode = get_ir_mode(type);
1396 expression_t *value = expression->value;
1398 ir_node *value_node = expression_to_firm(value);
1401 if(is_type_pointer(type)) {
1402 pointer_type_t *pointer_type = &type->pointer;
1403 unsigned elem_size = get_type_size(pointer_type->points_to);
1404 offset = new_Const_long(mode_int, elem_size);
1406 assert(is_type_arithmetic(type));
1407 offset = new_Const(mode, get_mode_one(mode));
1410 switch(expression->expression.kind) {
1411 case EXPR_UNARY_POSTFIX_INCREMENT: {
1412 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1413 set_value_for_expression(value, new_value);
1416 case EXPR_UNARY_POSTFIX_DECREMENT: {
1417 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1418 set_value_for_expression(value, new_value);
1421 case EXPR_UNARY_PREFIX_INCREMENT: {
1422 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1423 set_value_for_expression(value, new_value);
1426 case EXPR_UNARY_PREFIX_DECREMENT: {
1427 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1428 set_value_for_expression(value, new_value);
1432 panic("no incdec expr in create_incdec");
1437 static bool is_local_variable(expression_t *expression)
1439 if (expression->kind != EXPR_REFERENCE)
1441 reference_expression_t *ref_expr = &expression->reference;
1442 declaration_t *declaration = ref_expr->declaration;
1443 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1446 static pn_Cmp get_pnc(const expression_kind_t kind)
1449 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1450 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1451 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Ne;
1452 case EXPR_BINARY_ISLESS:
1453 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1454 case EXPR_BINARY_ISLESSEQUAL:
1455 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1456 case EXPR_BINARY_ISGREATER:
1457 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1458 case EXPR_BINARY_ISGREATEREQUAL:
1459 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1460 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1465 panic("trying to get pn_Cmp from non-comparison binexpr type");
1469 * Handle the assume optimizer hint: check if a Confirm
1470 * node can be created.
1472 * @param dbi debug info
1473 * @param expr the IL assume expression
1475 * we support here only some simple cases:
1480 static ir_node *handle_assume_compare(dbg_info *dbi,
1481 const binary_expression_t *expression)
1483 expression_t *op1 = expression->left;
1484 expression_t *op2 = expression->right;
1485 declaration_t *var2, *var = NULL;
1486 ir_node *res = NULL;
1489 cmp_val = get_pnc(expression->expression.kind);
1491 if (is_local_variable(op1) && is_local_variable(op2)) {
1492 var = op1->reference.declaration;
1493 var2 = op2->reference.declaration;
1495 type_t *const type = skip_typeref(var->type);
1496 ir_mode *const mode = get_ir_mode(type);
1498 ir_node *const irn1 = get_value(var->v.value_number, mode);
1499 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1501 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1502 set_value(var2->v.value_number, res);
1504 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1505 set_value(var->v.value_number, res);
1511 if (is_local_variable(op1) && is_constant_expression(op2)) {
1512 var = op1->reference.declaration;
1514 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1515 cmp_val = get_inversed_pnc(cmp_val);
1516 var = op2->reference.declaration;
1521 type_t *const type = skip_typeref(var->type);
1522 ir_mode *const mode = get_ir_mode(type);
1524 res = get_value(var->v.value_number, mode);
1525 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1526 set_value(var->v.value_number, res);
1532 * Handle the assume optimizer hint.
1534 * @param dbi debug info
1535 * @param expr the IL assume expression
1537 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1538 switch(expression->kind) {
1539 case EXPR_BINARY_EQUAL:
1540 case EXPR_BINARY_NOTEQUAL:
1541 case EXPR_BINARY_LESS:
1542 case EXPR_BINARY_LESSEQUAL:
1543 case EXPR_BINARY_GREATER:
1544 case EXPR_BINARY_GREATEREQUAL:
1545 return handle_assume_compare(dbi, &expression->binary);
1551 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1553 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1554 type_t *type = skip_typeref(expression->expression.datatype);
1556 if(expression->expression.kind == EXPR_UNARY_TAKE_ADDRESS)
1557 return expression_to_addr(expression->value);
1559 const expression_t *value = expression->value;
1561 switch(expression->expression.kind) {
1562 case EXPR_UNARY_NEGATE: {
1563 ir_node *value_node = expression_to_firm(value);
1564 ir_mode *mode = get_ir_mode(type);
1565 return new_d_Minus(dbgi, value_node, mode);
1567 case EXPR_UNARY_PLUS:
1568 return expression_to_firm(value);
1569 case EXPR_UNARY_BITWISE_NEGATE: {
1570 ir_node *value_node = expression_to_firm(value);
1571 ir_mode *mode = get_ir_mode(type);
1572 return new_d_Not(dbgi, value_node, mode);
1574 case EXPR_UNARY_NOT: {
1575 ir_node *value_node = expression_to_firm(value);
1576 ir_mode *mode = get_ir_mode(type);
1577 if(get_irn_mode(value_node) != mode_b) {
1578 value_node = create_conv(dbgi, value_node, mode_b);
1580 value_node = new_d_Not(dbgi, value_node, mode_b);
1581 if(mode != mode_b) {
1582 value_node = create_conv(dbgi, value_node, mode);
1586 case EXPR_UNARY_DEREFERENCE: {
1587 ir_node *value_node = expression_to_firm(value);
1588 type_t *value_type = skip_typeref(value->base.datatype);
1589 ir_type *irtype = get_ir_type(value_type);
1590 assert(is_Pointer_type(irtype));
1591 ir_type *points_to = get_pointer_points_to_type(irtype);
1592 return deref_address(points_to, value_node, dbgi);
1594 case EXPR_UNARY_POSTFIX_INCREMENT:
1595 case EXPR_UNARY_POSTFIX_DECREMENT:
1596 case EXPR_UNARY_PREFIX_INCREMENT:
1597 case EXPR_UNARY_PREFIX_DECREMENT:
1598 return create_incdec(expression);
1599 case EXPR_UNARY_CAST: {
1600 ir_node *value_node = expression_to_firm(value);
1601 ir_mode *mode = get_ir_mode(type);
1602 ir_node *node = create_conv(dbgi, value_node, mode);
1603 node = do_strict_conv(dbgi, node);
1606 case EXPR_UNARY_CAST_IMPLICIT: {
1607 ir_node *value_node = expression_to_firm(value);
1608 ir_mode *mode = get_ir_mode(type);
1609 return create_conv(dbgi, value_node, mode);
1611 case EXPR_UNARY_ASSUME:
1612 if(firm_opt.confirm)
1613 return handle_assume(dbgi, value);
1620 panic("invalid UNEXPR type found");
1623 static ir_node *create_lazy_op(const binary_expression_t *expression)
1625 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1626 type_t *type = expression->expression.datatype;
1627 ir_mode *mode = get_ir_mode(type);
1629 if(is_constant_expression(expression->left)) {
1630 long val = fold_constant(expression->left);
1631 expression_kind_t ekind = expression->expression.kind;
1632 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
1633 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1634 return expression_to_firm(expression->right);
1636 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
1637 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
1638 return new_Const(mode, get_mode_one(mode));
1642 ir_node *cur_block = get_cur_block();
1644 ir_node *one_block = new_immBlock();
1645 ir_node *one = new_Const(mode, get_mode_one(mode));
1646 ir_node *jmp_one = new_d_Jmp(dbgi);
1648 ir_node *zero_block = new_immBlock();
1649 ir_node *zero = new_Const(mode, get_mode_null(mode));
1650 ir_node *jmp_zero = new_d_Jmp(dbgi);
1652 set_cur_block(cur_block);
1653 create_condition_evaluation((const expression_t*) expression,
1654 one_block, zero_block);
1655 mature_immBlock(one_block);
1656 mature_immBlock(zero_block);
1658 ir_node *common_block = new_immBlock();
1659 add_immBlock_pred(common_block, jmp_one);
1660 add_immBlock_pred(common_block, jmp_zero);
1661 mature_immBlock(common_block);
1663 ir_node *in[2] = { one, zero };
1664 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1669 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1670 ir_node *right, ir_mode *mode);
1672 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1673 create_arithmetic_func func)
1675 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1676 ir_node *left = expression_to_firm(expression->left);
1677 ir_node *right = expression_to_firm(expression->right);
1678 type_t *type = expression->right->base.datatype;
1679 /* be careful with the modes, because in arithmetic assign nodes only
1680 * the right operand has the mode of the arithmetic already */
1681 ir_mode *mode = get_ir_mode(type);
1682 left = create_conv(dbgi, left, mode);
1683 ir_node *res = func(dbgi, left, right, mode);
1688 static ir_node *pointer_arithmetic(ir_node *const pointer,
1691 dbg_info *const dbgi,
1692 const create_arithmetic_func func)
1694 pointer_type_t *const pointer_type = &type->pointer;
1695 type_t *const points_to = pointer_type->points_to;
1696 const unsigned elem_size = get_type_size(points_to);
1698 assert(elem_size >= 1);
1699 if (elem_size > 1) {
1700 integer = create_conv(dbgi, integer, mode_int);
1701 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1702 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1706 ir_mode *const mode = get_ir_mode(type);
1707 return func(dbgi, pointer, integer, mode);
1710 static ir_node *create_arithmetic_assign_binop(
1711 const binary_expression_t *expression, create_arithmetic_func func)
1713 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1714 type_t *const type = skip_typeref(expression->expression.datatype);
1717 if (is_type_pointer(type)) {
1718 ir_node *const pointer = expression_to_firm(expression->left);
1719 ir_node * integer = expression_to_firm(expression->right);
1720 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1722 value = create_arithmetic_binop(expression, func);
1725 ir_mode *const mode = get_ir_mode(type);
1726 value = create_conv(dbgi, value, mode);
1727 set_value_for_expression(expression->left, value);
1732 static ir_node *create_add(const binary_expression_t *expression)
1734 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1735 ir_node *left = expression_to_firm(expression->left);
1736 ir_node *right = expression_to_firm(expression->right);
1737 type_t *type = expression->expression.datatype;
1739 expression_t *expr_left = expression->left;
1740 expression_t *expr_right = expression->right;
1741 type_t *type_left = skip_typeref(expr_left->base.datatype);
1742 type_t *type_right = skip_typeref(expr_right->base.datatype);
1744 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1745 ir_mode *const mode = get_ir_mode(type);
1746 return new_d_Add(dbgi, left, right, mode);
1749 if (is_type_pointer(type_left)) {
1750 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1752 assert(is_type_pointer(type_right));
1753 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1757 static ir_node *create_sub(const binary_expression_t *expression)
1759 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1760 expression_t *const expr_left = expression->left;
1761 expression_t *const expr_right = expression->right;
1762 ir_node *const left = expression_to_firm(expr_left);
1763 ir_node *const right = expression_to_firm(expr_right);
1764 type_t *const type = expression->expression.datatype;
1765 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1766 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1768 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1769 ir_mode *const mode = get_ir_mode(type);
1770 return new_d_Sub(dbgi, left, right, mode);
1771 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1772 const pointer_type_t *const ptr_type = &type_left->pointer;
1773 const unsigned elem_size = get_type_size(ptr_type->points_to);
1774 ir_mode *const mode = get_ir_mode(type);
1775 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1776 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1777 ir_node *const no_mem = new_NoMem();
1778 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1779 op_pin_state_floats);
1780 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1783 assert(is_type_pointer(type_left));
1784 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1787 static ir_node *create_shift(const binary_expression_t *expression)
1789 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1790 ir_node *left = expression_to_firm(expression->left);
1791 ir_node *right = expression_to_firm(expression->right);
1792 type_t *type = expression->expression.datatype;
1793 ir_mode *mode = get_ir_mode(type);
1795 /* firm always wants the shift count to be unsigned */
1796 right = create_conv(dbgi, right, mode_uint);
1800 switch(expression->expression.kind) {
1801 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1802 case EXPR_BINARY_SHIFTLEFT:
1803 res = new_d_Shl(dbgi, left, right, mode);
1805 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1806 case EXPR_BINARY_SHIFTRIGHT: {
1807 expression_t *expr_left = expression->left;
1808 type_t *type_left = skip_typeref(expr_left->base.datatype);
1810 if(is_type_signed(type_left)) {
1811 res = new_d_Shrs(dbgi, left, right, mode);
1813 res = new_d_Shr(dbgi, left, right, mode);
1818 panic("create shift op called for non-shift op");
1825 static ir_node *create_divmod(const binary_expression_t *expression)
1827 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1828 ir_node *left = expression_to_firm(expression->left);
1829 ir_node *right = expression_to_firm(expression->right);
1830 ir_node *pin = new_Pin(new_NoMem());
1831 /* be careful with the modes, because in arithmetic assign nodes only
1832 * the right operand has the mode of the arithmetic already */
1833 type_t *type = expression->right->base.datatype;
1834 ir_mode *mode = get_ir_mode(type);
1835 left = create_conv(dbgi, left, mode);
1839 switch (expression->expression.kind) {
1840 case EXPR_BINARY_DIV:
1841 case EXPR_BINARY_DIV_ASSIGN:
1842 if(mode_is_float(mode)) {
1843 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1844 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1846 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1847 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1851 case EXPR_BINARY_MOD:
1852 case EXPR_BINARY_MOD_ASSIGN:
1853 assert(!mode_is_float(mode));
1854 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1855 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1858 default: panic("unexpected binary expression type in create_divmod()");
1864 static ir_node *create_arithmetic_assign_divmod(
1865 const binary_expression_t *expression)
1867 ir_node * value = create_divmod(expression);
1868 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1869 type_t *const type = expression->expression.datatype;
1870 ir_mode *const mode = get_ir_mode(type);
1872 assert(type->kind != TYPE_POINTER);
1874 value = create_conv(dbgi, value, mode);
1875 set_value_for_expression(expression->left, value);
1880 static ir_node *create_arithmetic_assign_shift(
1881 const binary_expression_t *expression)
1883 ir_node * value = create_shift(expression);
1884 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1885 type_t *const type = expression->expression.datatype;
1886 ir_mode *const mode = get_ir_mode(type);
1888 value = create_conv(dbgi, value, mode);
1889 set_value_for_expression(expression->left, value);
1894 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1896 expression_kind_t kind = expression->expression.kind;
1899 case EXPR_BINARY_EQUAL:
1900 case EXPR_BINARY_NOTEQUAL:
1901 case EXPR_BINARY_LESS:
1902 case EXPR_BINARY_LESSEQUAL:
1903 case EXPR_BINARY_GREATER:
1904 case EXPR_BINARY_GREATEREQUAL:
1905 case EXPR_BINARY_ISGREATER:
1906 case EXPR_BINARY_ISGREATEREQUAL:
1907 case EXPR_BINARY_ISLESS:
1908 case EXPR_BINARY_ISLESSEQUAL:
1909 case EXPR_BINARY_ISLESSGREATER:
1910 case EXPR_BINARY_ISUNORDERED: {
1911 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1912 ir_node *left = expression_to_firm(expression->left);
1913 ir_node *right = expression_to_firm(expression->right);
1914 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1915 long pnc = get_pnc(kind);
1916 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1919 case EXPR_BINARY_ASSIGN: {
1920 ir_node *right = expression_to_firm(expression->right);
1921 set_value_for_expression(expression->left, right);
1925 case EXPR_BINARY_ADD:
1926 return create_add(expression);
1927 case EXPR_BINARY_SUB:
1928 return create_sub(expression);
1929 case EXPR_BINARY_MUL:
1930 return create_arithmetic_binop(expression, new_d_Mul);
1931 case EXPR_BINARY_BITWISE_AND:
1932 return create_arithmetic_binop(expression, new_d_And);
1933 case EXPR_BINARY_BITWISE_OR:
1934 return create_arithmetic_binop(expression, new_d_Or);
1935 case EXPR_BINARY_BITWISE_XOR:
1936 return create_arithmetic_binop(expression, new_d_Eor);
1937 case EXPR_BINARY_SHIFTLEFT:
1938 case EXPR_BINARY_SHIFTRIGHT:
1939 return create_shift(expression);
1940 case EXPR_BINARY_DIV:
1941 case EXPR_BINARY_MOD:
1942 return create_divmod(expression);
1943 case EXPR_BINARY_LOGICAL_AND:
1944 case EXPR_BINARY_LOGICAL_OR:
1945 return create_lazy_op(expression);
1946 case EXPR_BINARY_COMMA:
1947 expression_to_firm(expression->left);
1948 return expression_to_firm(expression->right);
1949 case EXPR_BINARY_ADD_ASSIGN:
1950 return create_arithmetic_assign_binop(expression, new_d_Add);
1951 case EXPR_BINARY_SUB_ASSIGN:
1952 return create_arithmetic_assign_binop(expression, new_d_Sub);
1953 case EXPR_BINARY_MUL_ASSIGN:
1954 return create_arithmetic_assign_binop(expression, new_d_Mul);
1955 case EXPR_BINARY_DIV_ASSIGN:
1956 return create_arithmetic_assign_divmod(expression);
1957 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1958 return create_arithmetic_assign_binop(expression, new_d_And);
1959 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1960 return create_arithmetic_assign_binop(expression, new_d_Or);
1961 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1962 return create_arithmetic_assign_binop(expression, new_d_Eor);
1963 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1964 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1965 return create_arithmetic_assign_shift(expression);
1966 case EXPR_BINARY_BUILTIN_EXPECT:
1967 return expression_to_firm(expression->left);
1969 panic("TODO binexpr type");
1973 static ir_node *array_access_addr(const array_access_expression_t *expression)
1975 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1976 ir_node *base_addr = expression_to_firm(expression->array_ref);
1977 ir_node *offset = expression_to_firm(expression->index);
1978 offset = create_conv(dbgi, offset, mode_uint);
1980 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1981 assert(is_type_pointer(ref_type));
1982 pointer_type_t *pointer_type = &ref_type->pointer;
1984 unsigned elem_size = get_type_size(pointer_type->points_to);
1985 ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
1986 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1988 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
1993 static ir_node *array_access_to_firm(
1994 const array_access_expression_t *expression)
1996 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1997 ir_node *addr = array_access_addr(expression);
1998 type_t *type = revert_automatic_type_conversion(
1999 (const expression_t*) expression);
2000 type = skip_typeref(type);
2001 ir_type *irtype = get_ir_type(type);
2003 return deref_address(irtype, addr, dbgi);
2006 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
2008 type_t *type = expression->type;
2010 type = expression->size_expression->base.datatype;
2011 assert(type != NULL);
2014 ir_mode *const mode = get_ir_mode(expression->expression.datatype);
2015 symconst_symbol sym;
2016 sym.type_p = get_ir_type(type);
2017 return new_SymConst(mode, sym, symconst_type_size);
2020 static ir_node *alignof_to_firm(const alignof_expression_t *expression)
2022 type_t *const type = expression->type;
2023 ir_mode *const mode = get_ir_mode(expression->expression.datatype);
2024 symconst_symbol sym;
2025 sym.type_p = get_ir_type(type);
2026 return new_SymConst(mode, sym, symconst_type_align);
2029 static long fold_constant(const expression_t *expression)
2031 assert(is_constant_expression(expression));
2033 ir_graph *old_current_ir_graph = current_ir_graph;
2034 if(current_ir_graph == NULL) {
2035 current_ir_graph = get_const_code_irg();
2038 ir_node *cnst = expression_to_firm(expression);
2039 current_ir_graph = old_current_ir_graph;
2041 if(!is_Const(cnst)) {
2042 panic("couldn't fold constant\n");
2045 tarval *tv = get_Const_tarval(cnst);
2046 if(!tarval_is_long(tv)) {
2047 panic("result of constant folding is not integer\n");
2050 return get_tarval_long(tv);
2053 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2055 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
2057 /* first try to fold a constant condition */
2058 if(is_constant_expression(expression->condition)) {
2059 long val = fold_constant(expression->condition);
2061 return expression_to_firm(expression->true_expression);
2063 return expression_to_firm(expression->false_expression);
2067 ir_node *cur_block = get_cur_block();
2069 /* create the true block */
2070 ir_node *true_block = new_immBlock();
2072 ir_node *true_val = expression_to_firm(expression->true_expression);
2073 ir_node *true_jmp = new_Jmp();
2075 /* create the false block */
2076 ir_node *false_block = new_immBlock();
2078 ir_node *false_val = expression_to_firm(expression->false_expression);
2079 ir_node *false_jmp = new_Jmp();
2081 /* create the condition evaluation */
2082 set_cur_block(cur_block);
2083 create_condition_evaluation(expression->condition, true_block, false_block);
2084 mature_immBlock(true_block);
2085 mature_immBlock(false_block);
2087 /* create the common block */
2088 ir_node *common_block = new_immBlock();
2089 add_immBlock_pred(common_block, true_jmp);
2090 add_immBlock_pred(common_block, false_jmp);
2091 mature_immBlock(common_block);
2093 /* TODO improve static semantics, so either both or no values are NULL */
2094 if (true_val == NULL || false_val == NULL)
2097 ir_node *in[2] = { true_val, false_val };
2098 ir_mode *mode = get_irn_mode(true_val);
2099 assert(get_irn_mode(false_val) == mode);
2100 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2105 static ir_node *select_addr(const select_expression_t *expression)
2107 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2109 ir_node *compound_addr = expression_to_firm(expression->compound);
2111 declaration_t *entry = expression->compound_entry;
2112 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2113 ir_entity *entity = entry->v.entity;
2115 assert(entity != NULL);
2117 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2122 static ir_node *select_to_firm(const select_expression_t *expression)
2124 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2125 ir_node *addr = select_addr(expression);
2126 type_t *type = revert_automatic_type_conversion(
2127 (const expression_t*) expression);
2128 type = skip_typeref(type);
2129 ir_type *irtype = get_ir_type(type);
2131 return deref_address(irtype, addr, dbgi);
2134 /* Values returned by __builtin_classify_type. */
2135 typedef enum gcc_type_class
2141 enumeral_type_class,
2144 reference_type_class,
2148 function_type_class,
2159 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2161 const type_t *const type = expr->type_expression->base.datatype;
2167 const atomic_type_t *const atomic_type = &type->atomic;
2168 switch (atomic_type->atype) {
2169 /* should not be reached */
2170 case ATOMIC_TYPE_INVALID:
2174 /* gcc cannot do that */
2175 case ATOMIC_TYPE_VOID:
2176 tc = void_type_class;
2179 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2180 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2181 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2182 case ATOMIC_TYPE_SHORT:
2183 case ATOMIC_TYPE_USHORT:
2184 case ATOMIC_TYPE_INT:
2185 case ATOMIC_TYPE_UINT:
2186 case ATOMIC_TYPE_LONG:
2187 case ATOMIC_TYPE_ULONG:
2188 case ATOMIC_TYPE_LONGLONG:
2189 case ATOMIC_TYPE_ULONGLONG:
2190 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2191 tc = integer_type_class;
2194 case ATOMIC_TYPE_FLOAT:
2195 case ATOMIC_TYPE_DOUBLE:
2196 case ATOMIC_TYPE_LONG_DOUBLE:
2197 tc = real_type_class;
2200 #ifdef PROVIDE_COMPLEX
2201 case ATOMIC_TYPE_FLOAT_COMPLEX:
2202 case ATOMIC_TYPE_DOUBLE_COMPLEX:
2203 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
2204 tc = complex_type_class;
2206 case ATOMIC_TYPE_FLOAT_IMAGINARY:
2207 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
2208 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
2209 tc = complex_type_class;
2214 panic("Unimplemented case in classify_type_to_firm().");
2219 case TYPE_ARRAY: /* gcc handles this as pointer */
2220 case TYPE_FUNCTION: /* gcc handles this as pointer */
2221 case TYPE_POINTER: tc = pointer_type_class; break;
2222 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
2223 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
2225 /* gcc handles this as integer */
2226 case TYPE_ENUM: tc = integer_type_class; break;
2229 panic("Unimplemented case in classify_type_to_firm().");
2232 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
2233 ir_mode *const mode = mode_int;
2234 tarval *const tv = new_tarval_from_long(tc, mode);
2235 return new_d_Const(dbgi, mode, tv);
2238 static ir_node *function_name_to_firm(
2239 const string_literal_expression_t *const expr)
2241 if (current_function_name == NULL) {
2242 const source_position_t *const src_pos =
2243 &expr->expression.source_position;
2244 const char *const name = current_function_decl->symbol->string;
2245 current_function_name = string_to_firm(src_pos, "__func__", name);
2248 return current_function_name;
2251 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2253 statement_t *statement = expr->statement;
2255 assert(statement->kind == STATEMENT_COMPOUND);
2256 return compound_statement_to_firm(&statement->compound);
2259 static ir_node *va_start_expression_to_firm(
2260 const va_start_expression_t *const expr)
2262 ir_type *const method_type = get_ir_type(current_function_decl->type);
2263 int const n = get_method_n_params(method_type) - 1;
2264 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2265 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2266 dbg_info *const dbgi =
2267 get_dbg_info(&expr->expression.source_position);
2268 ir_node *const no_mem = new_NoMem();
2269 ir_node *const arg_sel =
2270 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2272 size_t const parm_size = get_type_size(expr->parameter->type);
2273 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2274 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2275 set_value_for_expression(expr->ap, add);
2280 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2282 ir_type *const irtype = get_ir_type(expr->expression.datatype);
2283 ir_node *const ap = expression_to_firm(expr->ap);
2284 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
2285 ir_node *const res = deref_address(irtype, ap, dbgi);
2287 size_t const parm_size = get_type_size(expr->expression.datatype);
2288 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2289 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2290 set_value_for_expression(expr->ap, add);
2295 static ir_node *dereference_addr(const unary_expression_t *const expression)
2297 assert(expression->expression.kind == EXPR_UNARY_DEREFERENCE);
2298 return expression_to_firm(expression->value);
2301 static ir_node *expression_to_addr(const expression_t *expression)
2303 switch(expression->kind) {
2304 case EXPR_REFERENCE:
2305 return reference_addr(&expression->reference);
2306 case EXPR_ARRAY_ACCESS:
2307 return array_access_addr(&expression->array_access);
2309 return select_addr(&expression->select);
2311 return call_expression_to_firm(&expression->call);
2312 case EXPR_UNARY_DEREFERENCE: {
2313 return dereference_addr(&expression->unary);
2318 panic("trying to get address of non-lvalue");
2321 static ir_node *builtin_constant_to_firm(const builtin_constant_expression_t *expression)
2323 ir_mode *mode = get_ir_mode(expression->expression.datatype);
2326 if (is_constant_expression(expression->value)) {
2331 return new_Const_long(mode, v);
2334 static ir_node *builtin_prefetch_to_firm(const builtin_prefetch_expression_t *expression)
2336 ir_node *adr = expression_to_firm(expression->adr);
2337 /* no Firm support for prefetch yet */
2342 static ir_node *_expression_to_firm(const expression_t *expression)
2344 switch(expression->kind) {
2346 return const_to_firm(&expression->conste);
2347 case EXPR_STRING_LITERAL:
2348 return string_literal_to_firm(&expression->string);
2349 case EXPR_WIDE_STRING_LITERAL:
2350 return wide_string_literal_to_firm(&expression->wide_string);
2351 case EXPR_REFERENCE:
2352 return reference_expression_to_firm(&expression->reference);
2354 return call_expression_to_firm(&expression->call);
2356 return unary_expression_to_firm(&expression->unary);
2358 return binary_expression_to_firm(&expression->binary);
2359 case EXPR_ARRAY_ACCESS:
2360 return array_access_to_firm(&expression->array_access);
2362 return sizeof_to_firm(&expression->sizeofe);
2364 return alignof_to_firm(&expression->alignofe);
2365 case EXPR_CONDITIONAL:
2366 return conditional_to_firm(&expression->conditional);
2368 return select_to_firm(&expression->select);
2369 case EXPR_CLASSIFY_TYPE:
2370 return classify_type_to_firm(&expression->classify_type);
2372 case EXPR_PRETTY_FUNCTION:
2373 return function_name_to_firm(&expression->string);
2374 case EXPR_STATEMENT:
2375 return statement_expression_to_firm(&expression->statement);
2377 return va_start_expression_to_firm(&expression->va_starte);
2379 return va_arg_expression_to_firm(&expression->va_arge);
2381 case EXPR_BUILTIN_SYMBOL:
2382 panic("unimplemented expression found");
2383 case EXPR_BUILTIN_CONSTANT_P:
2384 return builtin_constant_to_firm(&expression->builtin_constant);
2385 case EXPR_BUILTIN_PREFETCH:
2386 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2392 panic("invalid expression found");
2395 static ir_node *expression_to_firm(const expression_t *expression)
2397 ir_node *res = _expression_to_firm(expression);
2399 if(res != NULL && get_irn_mode(res) == mode_b) {
2400 ir_mode *mode = get_ir_mode(expression->base.datatype);
2401 res = create_conv(NULL, res, mode);
2407 static ir_node *expression_to_modeb(const expression_t *expression)
2409 ir_node *res = _expression_to_firm(expression);
2410 res = create_conv(NULL, res, mode_b);
2416 * create a short-circuit expression evaluation that tries to construct
2417 * efficient control flow structures for &&, || and ! expressions
2419 static void create_condition_evaluation(const expression_t *expression,
2420 ir_node *true_block,
2421 ir_node *false_block)
2423 switch(expression->kind) {
2424 case EXPR_UNARY_NOT: {
2425 const unary_expression_t *unary_expression = &expression->unary;
2426 create_condition_evaluation(unary_expression->value, false_block,
2430 case EXPR_BINARY_LOGICAL_AND: {
2431 const binary_expression_t *binary_expression = &expression->binary;
2433 ir_node *cur_block = get_cur_block();
2434 ir_node *extra_block = new_immBlock();
2435 set_cur_block(cur_block);
2436 create_condition_evaluation(binary_expression->left, extra_block,
2438 mature_immBlock(extra_block);
2439 set_cur_block(extra_block);
2440 create_condition_evaluation(binary_expression->right, true_block,
2444 case EXPR_BINARY_LOGICAL_OR: {
2445 const binary_expression_t *binary_expression = &expression->binary;
2447 ir_node *cur_block = get_cur_block();
2448 ir_node *extra_block = new_immBlock();
2449 set_cur_block(cur_block);
2450 create_condition_evaluation(binary_expression->left, true_block,
2452 mature_immBlock(extra_block);
2453 set_cur_block(extra_block);
2454 create_condition_evaluation(binary_expression->right, true_block,
2462 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2463 ir_node *condition = expression_to_modeb(expression);
2464 ir_node *cond = new_d_Cond(dbgi, condition);
2465 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2466 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2468 /* set branch prediction info based on __builtin_expect */
2469 if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
2470 long cnst = fold_constant(expression->binary.right);
2471 cond_jmp_predicate pred;
2474 pred = COND_JMP_PRED_FALSE;
2476 pred = COND_JMP_PRED_TRUE;
2478 set_Cond_jmp_pred(cond, pred);
2481 add_immBlock_pred(true_block, true_proj);
2482 add_immBlock_pred(false_block, false_proj);
2484 set_cur_block(NULL);
2489 static void create_declaration_entity(declaration_t *declaration,
2490 declaration_kind_t declaration_kind,
2491 ir_type *parent_type)
2493 ident *const id = new_id_from_str(declaration->symbol->string);
2494 ir_type *const irtype = get_ir_type(declaration->type);
2495 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2496 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2497 set_entity_ld_ident(entity, id);
2499 declaration->declaration_kind = (unsigned char) declaration_kind;
2500 declaration->v.entity = entity;
2501 set_entity_variability(entity, variability_uninitialized);
2502 if(parent_type == get_tls_type())
2503 set_entity_allocation(entity, allocation_automatic);
2504 else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
2505 set_entity_allocation(entity, allocation_static);
2506 /* TODO: visibility? */
2509 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2511 enum compound_graph_entry_type_t {
2512 COMPOUND_GRAPH_ENTRY_ARRAY,
2513 COMPOUND_GRAPH_ENTRY_COMPOUND
2516 struct compound_graph_path_entry_t {
2522 compound_graph_path_entry_t *prev;
2525 static void create_initializer_object(initializer_t *initializer, type_t *type,
2526 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2528 static compound_graph_path *create_compound_path(ir_type *type,
2529 compound_graph_path_entry_t *entry, int len)
2531 compound_graph_path *path = new_compound_graph_path(type, len);
2534 for( ; entry != NULL; entry = entry->prev, --i) {
2536 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2537 set_compound_graph_path_node(path, i, entry->v.entity);
2539 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2540 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2548 static void create_initializer_value(initializer_value_t *initializer,
2550 compound_graph_path_entry_t *entry,
2553 ir_node *node = expression_to_firm(initializer->value);
2554 ir_type *type = get_entity_type(entity);
2555 compound_graph_path *path = create_compound_path(type, entry, len);
2556 add_compound_ent_value_w_path(entity, node, path);
2559 static void create_initializer_compound(initializer_list_t *initializer,
2560 compound_type_t *type,
2562 compound_graph_path_entry_t *last_entry,
2565 declaration_t *compound_declaration = type->declaration;
2567 declaration_t *compound_entry = compound_declaration->context.declarations;
2569 compound_graph_path_entry_t entry;
2570 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2571 entry.prev = last_entry;
2575 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2576 if(compound_entry->symbol == NULL)
2578 if(compound_entry->namespc != NAMESPACE_NORMAL)
2581 if(i >= initializer->len)
2584 entry.v.entity = compound_entry->v.entity;
2586 initializer_t *sub_initializer = initializer->initializers[i];
2588 assert(compound_entry != NULL);
2589 assert(compound_entry->declaration_kind
2590 == DECLARATION_KIND_COMPOUND_MEMBER);
2592 if(sub_initializer->kind == INITIALIZER_VALUE) {
2593 create_initializer_value(&sub_initializer->value,
2594 entity, &entry, len);
2596 type_t *entry_type = skip_typeref(compound_entry->type);
2597 create_initializer_object(sub_initializer, entry_type, entity,
2605 static void create_initializer_array(initializer_list_t *initializer,
2606 array_type_t *type, ir_entity *entity,
2607 compound_graph_path_entry_t *last_entry,
2610 type_t *element_type = type->element_type;
2611 element_type = skip_typeref(element_type);
2613 compound_graph_path_entry_t entry;
2614 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2615 entry.prev = last_entry;
2619 for(i = 0; i < initializer->len; ++i) {
2620 entry.v.array_index = i;
2622 initializer_t *sub_initializer = initializer->initializers[i];
2624 if(sub_initializer->kind == INITIALIZER_VALUE) {
2625 create_initializer_value(&sub_initializer->value,
2626 entity, &entry, len);
2628 create_initializer_object(sub_initializer, element_type, entity,
2634 /* TODO: initialize rest... */
2635 if(type->size_expression != NULL) {
2636 size_t array_len = fold_constant(type->size_expression);
2637 for( ; i < array_len; ++i) {
2644 static void create_initializer_string(initializer_string_t *initializer,
2645 array_type_t *type, ir_entity *entity,
2646 compound_graph_path_entry_t *last_entry,
2649 type_t *element_type = type->element_type;
2650 element_type = skip_typeref(element_type);
2652 compound_graph_path_entry_t entry;
2653 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2654 entry.prev = last_entry;
2657 ir_type *irtype = get_entity_type(entity);
2658 size_t arr_len = get_array_type_size(type);
2659 const char *p = initializer->string;
2661 for(i = 0; i < arr_len; ++i, ++p) {
2662 entry.v.array_index = i;
2664 ir_node *node = new_Const_long(mode_Bs, *p);
2665 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2666 add_compound_ent_value_w_path(entity, node, path);
2673 static void create_initializer_wide_string(
2674 const initializer_wide_string_t *const initializer, array_type_t *const type,
2675 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2678 type_t *element_type = type->element_type;
2679 element_type = skip_typeref(element_type);
2681 compound_graph_path_entry_t entry;
2682 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2683 entry.prev = last_entry;
2686 ir_type *const irtype = get_entity_type(entity);
2687 const size_t arr_len = get_array_type_size(type);
2688 const wchar_rep_t * p = initializer->string.begin;
2689 const wchar_rep_t *const end = p + initializer->string.size;
2690 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2691 entry.v.array_index = i;
2693 ir_node *node = new_Const_long(mode_int, *p);
2694 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2695 add_compound_ent_value_w_path(entity, node, path);
2699 static void create_initializer_object(initializer_t *initializer, type_t *type,
2700 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2702 if(is_type_array(type)) {
2703 array_type_t *array_type = &type->array;
2705 switch (initializer->kind) {
2706 case INITIALIZER_STRING: {
2707 initializer_string_t *const string = &initializer->string;
2708 create_initializer_string(string, array_type, entity, entry, len);
2712 case INITIALIZER_WIDE_STRING: {
2713 initializer_wide_string_t *const string = &initializer->wide_string;
2714 create_initializer_wide_string(string, array_type, entity, entry, len);
2718 case INITIALIZER_LIST: {
2719 initializer_list_t *const list = &initializer->list;
2720 create_initializer_array(list, array_type, entity, entry, len);
2724 case INITIALIZER_VALUE:
2727 panic("Unhandled initializer");
2729 assert(initializer->kind == INITIALIZER_LIST);
2730 initializer_list_t *list = &initializer->list;
2732 assert(is_type_compound(type));
2733 compound_type_t *compound_type = &type->compound;
2734 create_initializer_compound(list, compound_type, entity, entry, len);
2738 static void create_initializer_local_variable_entity(declaration_t *declaration)
2740 initializer_t *initializer = declaration->init.initializer;
2741 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2742 ir_entity *entity = declaration->v.entity;
2743 ir_node *memory = get_store();
2744 ir_node *nomem = new_NoMem();
2745 ir_node *frame = get_irg_frame(current_ir_graph);
2746 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2748 if(initializer->kind == INITIALIZER_VALUE) {
2749 initializer_value_t *initializer_value = &initializer->value;
2751 ir_node *value = expression_to_firm(initializer_value->value);
2752 type_t *type = skip_typeref(declaration->type);
2753 assign_value(dbgi, addr, type, value);
2757 /* create a "template" entity which is copied to the entity on the stack */
2758 ident *const id = unique_ident("initializer");
2759 ir_type *const irtype = get_ir_type(declaration->type);
2760 ir_type *const global_type = get_glob_type();
2761 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
2762 set_entity_ld_ident(init_entity, id);
2764 set_entity_variability(init_entity, variability_initialized);
2765 set_entity_visibility(init_entity, visibility_local);
2766 set_entity_allocation(init_entity, allocation_static);
2768 ir_graph *const old_current_ir_graph = current_ir_graph;
2769 current_ir_graph = get_const_code_irg();
2771 type_t *const type = skip_typeref(declaration->type);
2772 create_initializer_object(initializer, type, init_entity, NULL, 0);
2774 assert(current_ir_graph == get_const_code_irg());
2775 current_ir_graph = old_current_ir_graph;
2777 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
2778 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2780 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2781 set_store(copyb_mem);
2784 static void create_initializer(declaration_t *declaration)
2786 initializer_t *initializer = declaration->init.initializer;
2787 if(initializer == NULL)
2790 declaration_kind_t declaration_kind
2791 = (declaration_kind_t) declaration->declaration_kind;
2792 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
2793 create_initializer_local_variable_entity(declaration);
2797 if(initializer->kind == INITIALIZER_VALUE) {
2798 initializer_value_t *initializer_value = &initializer->value;
2800 ir_node *value = expression_to_firm(initializer_value->value);
2802 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2803 set_value(declaration->v.value_number, value);
2805 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
2807 ir_entity *entity = declaration->v.entity;
2809 set_entity_variability(entity, variability_initialized);
2810 set_atomic_ent_value(entity, value);
2813 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
2814 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
2816 ir_entity *entity = declaration->v.entity;
2817 set_entity_variability(entity, variability_initialized);
2819 type_t *type = skip_typeref(declaration->type);
2820 create_initializer_object(initializer, type, entity, NULL, 0);
2824 static void create_local_variable(declaration_t *declaration)
2826 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
2828 bool needs_entity = declaration->address_taken;
2829 type_t *type = skip_typeref(declaration->type);
2831 if(is_type_array(type) || is_type_compound(type)) {
2832 needs_entity = true;
2836 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2837 create_declaration_entity(declaration,
2838 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
2841 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
2842 declaration->v.value_number = next_value_number_function;
2843 ++next_value_number_function;
2846 create_initializer(declaration);
2849 static void create_local_static_variable(declaration_t *declaration)
2851 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
2853 type_t *const type = skip_typeref(declaration->type);
2854 ir_type *const global_type = get_glob_type();
2855 ident *const id = unique_ident(declaration->symbol->string);
2856 ir_type *const irtype = get_ir_type(type);
2857 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2858 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
2859 set_entity_ld_ident(entity, id);
2861 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
2862 declaration->v.entity = entity;
2863 set_entity_variability(entity, variability_uninitialized);
2864 set_entity_visibility(entity, visibility_local);
2865 set_entity_allocation(entity, allocation_static);
2867 ir_graph *const old_current_ir_graph = current_ir_graph;
2868 current_ir_graph = get_const_code_irg();
2870 create_initializer(declaration);
2872 assert(current_ir_graph == get_const_code_irg());
2873 current_ir_graph = old_current_ir_graph;
2878 static void return_statement_to_firm(return_statement_t *statement)
2880 if(get_cur_block() == NULL)
2883 ir_type *func_irtype = get_ir_type(current_function_decl->type);
2885 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2889 if(get_method_n_ress(func_irtype) > 0) {
2890 ir_type *res_type = get_method_res_type(func_irtype, 0);
2892 if(statement->return_value != NULL) {
2893 ir_node *node = expression_to_firm(statement->return_value);
2894 node = do_strict_conv(dbgi, node);
2898 if(is_compound_type(res_type)) {
2901 mode = get_type_mode(res_type);
2903 in[0] = new_Unknown(mode);
2907 /* build return_value for its side effects */
2908 if(statement->return_value != NULL) {
2909 expression_to_firm(statement->return_value);
2914 ir_node *store = get_store();
2915 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
2917 ir_node *end_block = get_irg_end_block(current_ir_graph);
2918 add_immBlock_pred(end_block, ret);
2920 set_cur_block(NULL);
2923 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
2925 if(get_cur_block() == NULL)
2928 return expression_to_firm(statement->expression);
2931 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
2933 ir_node *result = NULL;
2934 statement_t *statement = compound->statements;
2935 for( ; statement != NULL; statement = statement->base.next) {
2936 //context2firm(&statement->context);
2938 if(statement->base.next == NULL
2939 && statement->kind == STATEMENT_EXPRESSION) {
2940 result = expression_statement_to_firm(
2941 &statement->expression);
2944 statement_to_firm(statement);
2950 static void create_local_declaration(declaration_t *declaration)
2952 type_t *type = skip_typeref(declaration->type);
2954 switch ((storage_class_tag_t) declaration->storage_class) {
2955 case STORAGE_CLASS_STATIC:
2956 create_local_static_variable(declaration);
2958 case STORAGE_CLASS_ENUM_ENTRY:
2959 panic("enum entry declaration in local block found");
2960 case STORAGE_CLASS_EXTERN:
2961 panic("extern declaration in local block found");
2962 case STORAGE_CLASS_NONE:
2963 case STORAGE_CLASS_AUTO:
2964 case STORAGE_CLASS_REGISTER:
2965 if(is_type_function(type)) {
2966 panic("nested functions not supported yet");
2968 create_local_variable(declaration);
2971 case STORAGE_CLASS_TYPEDEF:
2972 case STORAGE_CLASS_THREAD:
2973 case STORAGE_CLASS_THREAD_EXTERN:
2974 case STORAGE_CLASS_THREAD_STATIC:
2977 panic("invalid storage class found");
2980 static void declaration_statement_to_firm(declaration_statement_t *statement)
2982 declaration_t *declaration = statement->declarations_begin;
2983 declaration_t *end = statement->declarations_end->next;
2984 for( ; declaration != end; declaration = declaration->next) {
2985 create_local_variable(declaration);
2989 static void if_statement_to_firm(if_statement_t *statement)
2991 ir_node *cur_block = get_cur_block();
2993 ir_node *fallthrough_block = new_immBlock();
2995 /* the true (blocks) */
2996 ir_node *true_block;
2997 if (statement->true_statement != NULL) {
2998 true_block = new_immBlock();
2999 statement_to_firm(statement->true_statement);
3000 if(get_cur_block() != NULL) {
3001 ir_node *jmp = new_Jmp();
3002 add_immBlock_pred(fallthrough_block, jmp);
3005 true_block = fallthrough_block;
3008 /* the false (blocks) */
3009 ir_node *false_block;
3010 if(statement->false_statement != NULL) {
3011 false_block = new_immBlock();
3013 statement_to_firm(statement->false_statement);
3014 if(get_cur_block() != NULL) {
3015 ir_node *jmp = new_Jmp();
3016 add_immBlock_pred(fallthrough_block, jmp);
3019 false_block = fallthrough_block;
3022 /* create the condition */
3023 if(cur_block != NULL) {
3024 set_cur_block(cur_block);
3025 create_condition_evaluation(statement->condition, true_block,
3029 mature_immBlock(true_block);
3030 if(false_block != fallthrough_block) {
3031 mature_immBlock(false_block);
3033 mature_immBlock(fallthrough_block);
3035 set_cur_block(fallthrough_block);
3038 static void while_statement_to_firm(while_statement_t *statement)
3040 ir_node *jmp = NULL;
3041 if(get_cur_block() != NULL) {
3045 /* create the header block */
3046 ir_node *header_block = new_immBlock();
3048 add_immBlock_pred(header_block, jmp);
3051 /* the false block */
3052 ir_node *false_block = new_immBlock();
3055 ir_node *body_block;
3056 if (statement->body != NULL) {
3057 ir_node *old_continue_label = continue_label;
3058 ir_node *old_break_label = break_label;
3059 continue_label = header_block;
3060 break_label = false_block;
3062 body_block = new_immBlock();
3063 statement_to_firm(statement->body);
3065 assert(continue_label == header_block);
3066 assert(break_label == false_block);
3067 continue_label = old_continue_label;
3068 break_label = old_break_label;
3070 if(get_cur_block() != NULL) {
3072 add_immBlock_pred(header_block, jmp);
3075 body_block = header_block;
3078 /* create the condition */
3079 set_cur_block(header_block);
3081 create_condition_evaluation(statement->condition, body_block, false_block);
3082 mature_immBlock(body_block);
3083 mature_immBlock(false_block);
3084 mature_immBlock(header_block);
3086 set_cur_block(false_block);
3089 static void do_while_statement_to_firm(do_while_statement_t *statement)
3091 ir_node *jmp = NULL;
3092 if(get_cur_block() != NULL) {
3096 /* create the header block */
3097 ir_node *header_block = new_immBlock();
3099 /* the false block */
3100 ir_node *false_block = new_immBlock();
3103 ir_node *body_block = new_immBlock();
3105 add_immBlock_pred(body_block, jmp);
3108 if (statement->body != NULL) {
3109 ir_node *old_continue_label = continue_label;
3110 ir_node *old_break_label = break_label;
3111 continue_label = header_block;
3112 break_label = false_block;
3114 statement_to_firm(statement->body);
3116 assert(continue_label == header_block);
3117 assert(break_label == false_block);
3118 continue_label = old_continue_label;
3119 break_label = old_break_label;
3121 if (get_cur_block() == NULL) {
3122 mature_immBlock(header_block);
3123 mature_immBlock(body_block);
3124 mature_immBlock(false_block);
3129 ir_node *body_jmp = new_Jmp();
3130 add_immBlock_pred(header_block, body_jmp);
3131 mature_immBlock(header_block);
3133 /* create the condition */
3134 set_cur_block(header_block);
3136 create_condition_evaluation(statement->condition, body_block, false_block);
3137 mature_immBlock(body_block);
3138 mature_immBlock(false_block);
3139 mature_immBlock(header_block);
3141 set_cur_block(false_block);
3144 static void for_statement_to_firm(for_statement_t *statement)
3146 ir_node *jmp = NULL;
3147 if (get_cur_block() != NULL) {
3148 if(statement->initialisation != NULL) {
3149 expression_to_firm(statement->initialisation);
3152 /* create declarations */
3153 declaration_t *declaration = statement->context.declarations;
3154 for( ; declaration != NULL; declaration = declaration->next) {
3155 create_local_declaration(declaration);
3162 /* create the step block */
3163 ir_node *const step_block = new_immBlock();
3164 if (statement->step != NULL) {
3165 expression_to_firm(statement->step);
3167 ir_node *const step_jmp = new_Jmp();
3169 /* create the header block */
3170 ir_node *const header_block = new_immBlock();
3172 add_immBlock_pred(header_block, jmp);
3174 add_immBlock_pred(header_block, step_jmp);
3176 /* the false block */
3177 ir_node *const false_block = new_immBlock();
3180 ir_node * body_block;
3181 if (statement->body != NULL) {
3182 ir_node *const old_continue_label = continue_label;
3183 ir_node *const old_break_label = break_label;
3184 continue_label = step_block;
3185 break_label = false_block;
3187 body_block = new_immBlock();
3188 statement_to_firm(statement->body);
3190 assert(continue_label == step_block);
3191 assert(break_label == false_block);
3192 continue_label = old_continue_label;
3193 break_label = old_break_label;
3195 if (get_cur_block() != NULL) {
3197 add_immBlock_pred(step_block, jmp);
3200 body_block = step_block;
3203 /* create the condition */
3204 set_cur_block(header_block);
3205 if (statement->condition != NULL) {
3206 create_condition_evaluation(statement->condition, body_block,
3209 keep_alive(header_block);
3211 add_immBlock_pred(body_block, jmp);
3214 mature_immBlock(body_block);
3215 mature_immBlock(false_block);
3216 mature_immBlock(step_block);
3217 mature_immBlock(header_block);
3218 mature_immBlock(false_block);
3220 set_cur_block(false_block);
3223 static void create_jump_statement(const statement_t *statement,
3224 ir_node *target_block)
3226 if(get_cur_block() == NULL)
3229 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3230 ir_node *jump = new_d_Jmp(dbgi);
3231 add_immBlock_pred(target_block, jump);
3233 set_cur_block(NULL);
3236 static void switch_statement_to_firm(const switch_statement_t *statement)
3238 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
3240 ir_node *expression = expression_to_firm(statement->expression);
3241 ir_node *cond = new_d_Cond(dbgi, expression);
3242 ir_node *break_block = new_immBlock();
3244 set_cur_block(NULL);
3246 ir_node *const old_switch_cond = current_switch_cond;
3247 ir_node *const old_break_label = break_label;
3248 const bool old_saw_default_label = saw_default_label;
3249 current_switch_cond = cond;
3250 break_label = break_block;
3252 statement_to_firm(statement->body);
3254 if(get_cur_block() != NULL) {
3255 ir_node *jmp = new_Jmp();
3256 add_immBlock_pred(break_block, jmp);
3259 if (!saw_default_label) {
3260 set_cur_block(get_nodes_block(cond));
3261 ir_node *const proj = new_d_defaultProj(dbgi, cond,
3262 MAGIC_DEFAULT_PN_NUMBER);
3263 add_immBlock_pred(break_block, proj);
3266 assert(current_switch_cond == cond);
3267 assert(break_label == break_block);
3268 current_switch_cond = old_switch_cond;
3269 break_label = old_break_label;
3270 saw_default_label = old_saw_default_label;
3272 mature_immBlock(break_block);
3273 set_cur_block(break_block);
3276 static void case_label_to_firm(const case_label_statement_t *statement)
3278 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
3280 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
3282 /* let's create a node and hope firm constant folding creates a Const
3285 set_cur_block(get_nodes_block(current_switch_cond));
3286 if(statement->expression) {
3287 long pn = fold_constant(statement->expression);
3288 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
3289 /* oops someone detected our cheating... */
3290 panic("magic default pn used");
3292 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
3294 saw_default_label = true;
3295 proj = new_d_defaultProj(dbgi, current_switch_cond,
3296 MAGIC_DEFAULT_PN_NUMBER);
3299 ir_node *block = new_immBlock();
3300 if (fallthrough != NULL) {
3301 add_immBlock_pred(block, fallthrough);
3303 add_immBlock_pred(block, proj);
3304 mature_immBlock(block);
3306 if(statement->label_statement != NULL) {
3307 statement_to_firm(statement->label_statement);
3311 static ir_node *get_label_block(declaration_t *label)
3313 assert(label->namespc == NAMESPACE_LABEL);
3315 if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
3316 return label->v.block;
3318 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
3320 ir_node *old_cur_block = get_cur_block();
3321 ir_node *block = new_immBlock();
3322 set_cur_block(old_cur_block);
3324 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
3325 label->v.block = block;
3327 ARR_APP1(ir_node *, imature_blocks, block);
3332 static void label_to_firm(const label_statement_t *statement)
3334 ir_node *block = get_label_block(statement->label);
3336 if(get_cur_block() != NULL) {
3337 ir_node *jmp = new_Jmp();
3338 add_immBlock_pred(block, jmp);
3341 set_cur_block(block);
3344 if(statement->label_statement != NULL) {
3345 statement_to_firm(statement->label_statement);
3349 static void goto_to_firm(const goto_statement_t *statement)
3351 if(get_cur_block() == NULL)
3354 ir_node *block = get_label_block(statement->label);
3355 ir_node *jmp = new_Jmp();
3356 add_immBlock_pred(block, jmp);
3358 set_cur_block(NULL);
3361 typedef enum modifier_t {
3362 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
3363 ASM_MODIFIER_READ_WRITE = 1 << 1,
3364 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
3365 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
3369 static void asm_statement_to_firm(const asm_statement_t *statement)
3371 bool needs_memory = false;
3373 size_t n_clobbers = 0;
3374 asm_clobber_t *clobber = statement->clobbers;
3375 for( ; clobber != NULL; clobber = clobber->next) {
3376 if(strcmp(clobber->clobber, "memory") == 0) {
3377 needs_memory = true;
3381 ident *id = new_id_from_str(clobber->clobber);
3382 obstack_ptr_grow(&asm_obst, id);
3385 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
3386 ident **clobbers = NULL;
3387 if(n_clobbers > 0) {
3388 clobbers = obstack_finish(&asm_obst);
3391 /* find and count input and output constraints */
3392 asm_constraint_t *constraint = statement->inputs;
3393 for( ; constraint != NULL; constraint = constraint->next) {
3395 bool supports_memop = false;
3396 for(const char *c = constraint->constraints; *c != 0; ++c) {
3397 /* TODO: improve error messages */
3401 panic("multiple alternative assembler constraints not "
3409 supports_memop = true;
3410 obstack_1grow(&asm_obst, *c);
3413 if(modifiers & ASM_MODIFIER_READ_WRITE)
3414 panic("inconsistent register constraints");
3415 modifiers |= ASM_MODIFIER_WRITE_ONLY;
3418 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
3419 panic("inconsistent register constraints");
3420 modifiers |= ASM_MODIFIER_READ_WRITE;
3423 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
3424 panic("early clobber assembler constraint not supported yet");
3427 modifiers |= ASM_MODIFIER_COMMUTATIVE;
3428 panic("commutative assembler constraint not supported yet");
3431 /* skip register preferences stuff... */
3432 while(*c != 0 && *c != ',')
3436 /* skip register preferences stuff... */
3440 obstack_1grow(&asm_obst, *c);
3444 obstack_1grow(&asm_obst, '\0');
3445 const char *constraint_string = obstack_finish(&asm_obst);
3447 needs_memory |= supports_memop;
3448 if(supports_memop) {
3456 static void statement_to_firm(statement_t *statement)
3458 switch(statement->kind) {
3459 case STATEMENT_INVALID:
3460 panic("invalid statement found");
3461 case STATEMENT_COMPOUND:
3462 compound_statement_to_firm(&statement->compound);
3464 case STATEMENT_RETURN:
3465 return_statement_to_firm(&statement->returns);
3467 case STATEMENT_EXPRESSION:
3468 expression_statement_to_firm(&statement->expression);
3471 if_statement_to_firm(&statement->ifs);
3473 case STATEMENT_WHILE:
3474 while_statement_to_firm(&statement->whiles);
3476 case STATEMENT_DO_WHILE:
3477 do_while_statement_to_firm(&statement->do_while);
3479 case STATEMENT_DECLARATION:
3480 declaration_statement_to_firm(&statement->declaration);
3482 case STATEMENT_BREAK:
3483 create_jump_statement(statement, break_label);
3485 case STATEMENT_CONTINUE:
3486 create_jump_statement(statement, continue_label);
3488 case STATEMENT_SWITCH:
3489 switch_statement_to_firm(&statement->switchs);
3491 case STATEMENT_CASE_LABEL:
3492 case_label_to_firm(&statement->case_label);
3495 for_statement_to_firm(&statement->fors);
3497 case STATEMENT_LABEL:
3498 label_to_firm(&statement->label);
3500 case STATEMENT_GOTO:
3501 goto_to_firm(&statement->gotos);
3504 //asm_statement_to_firm(&statement->asms);
3508 panic("Statement not implemented\n");
3511 static int count_decls_in_expression(const expression_t *expression);
3513 static int count_local_declarations(const declaration_t * decl,
3514 const declaration_t *const end)
3517 for (; decl != end; decl = decl->next) {
3518 const type_t *type = skip_typeref(decl->type);
3519 switch (type->kind) {
3523 if (!decl->address_taken)
3529 const initializer_t *initializer = decl->init.initializer;
3530 /* FIXME: should walk initializer hierarchies... */
3531 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
3532 count += count_decls_in_expression(initializer->value.value);
3538 static int count_decls_in_expression(const expression_t *expression) {
3539 if(expression == NULL)
3542 switch(expression->base.kind) {
3543 case EXPR_STATEMENT:
3544 return count_decls_in_stmts(expression->statement.statement);
3546 int count_left = count_decls_in_expression(expression->binary.left);
3547 int count_right = count_decls_in_expression(expression->binary.right);
3548 return count_left + count_right;
3551 return count_decls_in_expression(expression->unary.value);
3557 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
3558 * (or implement all the missing expressions here/implement a walker)
3564 static int count_decls_in_stmts(const statement_t *stmt)
3567 for (; stmt != NULL; stmt = stmt->base.next) {
3568 switch (stmt->kind) {
3569 case STATEMENT_DECLARATION: {
3570 const declaration_statement_t *const decl_stmt = &stmt->declaration;
3571 count += count_local_declarations(decl_stmt->declarations_begin,
3572 decl_stmt->declarations_end->next);
3576 case STATEMENT_COMPOUND: {
3577 const compound_statement_t *const comp =
3579 count += count_decls_in_stmts(comp->statements);
3583 case STATEMENT_IF: {
3584 const if_statement_t *const if_stmt = &stmt->ifs;
3585 count += count_decls_in_expression(if_stmt->condition);
3586 count += count_decls_in_stmts(if_stmt->true_statement);
3587 count += count_decls_in_stmts(if_stmt->false_statement);
3591 case STATEMENT_SWITCH: {
3592 const switch_statement_t *const switch_stmt = &stmt->switchs;
3593 count += count_decls_in_expression(switch_stmt->expression);
3594 count += count_decls_in_stmts(switch_stmt->body);
3598 case STATEMENT_LABEL: {
3599 const label_statement_t *const label_stmt = &stmt->label;
3600 count += count_decls_in_stmts(label_stmt->label_statement);
3604 case STATEMENT_WHILE: {
3605 const while_statement_t *const while_stmt = &stmt->whiles;
3606 count += count_decls_in_expression(while_stmt->condition);
3607 count += count_decls_in_stmts(while_stmt->body);
3611 case STATEMENT_DO_WHILE: {
3612 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
3613 count += count_decls_in_expression(do_while_stmt->condition);
3614 count += count_decls_in_stmts(do_while_stmt->body);
3618 case STATEMENT_FOR: {
3619 const for_statement_t *const for_stmt = &stmt->fors;
3620 count += count_local_declarations(for_stmt->context.declarations, NULL);
3621 count += count_decls_in_expression(for_stmt->initialisation);
3622 count += count_decls_in_expression(for_stmt->condition);
3623 count += count_decls_in_expression(for_stmt->step);
3624 count += count_decls_in_stmts(for_stmt->body);
3628 case STATEMENT_CASE_LABEL: {
3629 const case_label_statement_t *label = &stmt->case_label;
3630 count += count_decls_in_expression(label->expression);
3631 count += count_decls_in_stmts(label->label_statement);
3636 case STATEMENT_BREAK:
3637 case STATEMENT_CONTINUE:
3640 case STATEMENT_EXPRESSION: {
3641 const expression_statement_t *expr_stmt = &stmt->expression;
3642 count += count_decls_in_expression(expr_stmt->expression);
3646 case STATEMENT_GOTO:
3647 case STATEMENT_INVALID:
3650 case STATEMENT_RETURN: {
3651 const return_statement_t *ret_stmt = &stmt->returns;
3652 count += count_decls_in_expression(ret_stmt->return_value);
3660 static int get_function_n_local_vars(declaration_t *declaration)
3664 /* count parameters */
3665 count += count_local_declarations(declaration->context.declarations, NULL);
3667 /* count local variables declared in body */
3668 count += count_decls_in_stmts(declaration->init.statement);
3673 static void initialize_function_parameters(declaration_t *declaration)
3675 ir_graph *irg = current_ir_graph;
3676 ir_node *args = get_irg_args(irg);
3677 ir_node *start_block = get_irg_start_block(irg);
3678 ir_type *function_irtype = get_ir_type(declaration->type);
3681 declaration_t *parameter = declaration->context.declarations;
3682 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3683 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
3684 type_t *type = skip_typeref(parameter->type);
3686 bool needs_entity = parameter->address_taken;
3687 assert(!is_type_array(type));
3688 if(is_type_compound(type)) {
3689 needs_entity = true;
3693 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3694 ident *id = new_id_from_str(parameter->symbol->string);
3695 set_entity_ident(entity, id);
3697 parameter->declaration_kind
3698 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
3699 parameter->v.entity = entity;
3703 ir_mode *mode = get_ir_mode(parameter->type);
3705 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3707 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3708 parameter->v.value_number = next_value_number_function;
3709 ++next_value_number_function;
3711 set_value(parameter->v.value_number, proj);
3716 * Handle additional decl modifiers for IR-graphs
3718 * @param irg the IR-graph
3719 * @param dec_modifiers additional modifiers
3721 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
3723 if (decl_modifiers & DM_NORETURN) {
3724 /* TRUE if the declaration includes the Microsoft
3725 __declspec(noreturn) specifier. */
3726 set_irg_additional_property(irg, mtp_property_noreturn);
3728 if (decl_modifiers & DM_NOTHROW) {
3729 /* TRUE if the declaration includes the Microsoft
3730 __declspec(nothrow) specifier. */
3731 set_irg_additional_property(irg, mtp_property_nothrow);
3733 if (decl_modifiers & DM_NAKED) {
3734 /* TRUE if the declaration includes the Microsoft
3735 __declspec(naked) specifier. */
3736 set_irg_additional_property(irg, mtp_property_naked);
3738 if (decl_modifiers & DM_FORCEINLINE) {
3739 /* TRUE if the declaration includes the
3740 Microsoft __forceinline specifier. */
3741 set_irg_inline_property(irg, irg_inline_forced);
3743 if (decl_modifiers & DM_NOINLINE) {
3744 /* TRUE if the declaration includes the Microsoft
3745 __declspec(noinline) specifier. */
3746 set_irg_inline_property(irg, irg_inline_forbidden);
3750 static void create_function(declaration_t *declaration)
3752 ir_entity *function_entity = get_function_entity(declaration);
3754 if(declaration->init.statement == NULL)
3757 current_function_decl = declaration;
3758 current_function_name = NULL;
3760 assert(imature_blocks == NULL);
3761 imature_blocks = NEW_ARR_F(ir_node*, 0);
3763 int n_local_vars = get_function_n_local_vars(declaration);
3764 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3765 ir_node *first_block = get_cur_block();
3767 /* set inline flags */
3768 if (declaration->is_inline)
3769 set_irg_inline_property(irg, irg_inline_recomended);
3770 handle_decl_modifier_irg(irg, declaration->modifiers);
3772 next_value_number_function = 0;
3773 initialize_function_parameters(declaration);
3775 statement_to_firm(declaration->init.statement);
3777 ir_node *end_block = get_irg_end_block(irg);
3779 /* do we have a return statement yet? */
3780 if(get_cur_block() != NULL) {
3781 type_t *type = skip_typeref(declaration->type);
3782 assert(is_type_function(type));
3783 const function_type_t *func_type = &type->function;
3784 const type_t *return_type
3785 = skip_typeref(func_type->return_type);
3788 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3789 ret = new_Return(get_store(), 0, NULL);
3792 if(is_type_scalar(return_type)) {
3793 mode = get_ir_mode(func_type->return_type);
3799 /* ยง5.1.2.2.3 main implicitly returns 0 */
3800 if (strcmp(declaration->symbol->string, "main") == 0) {
3801 in[0] = new_Const(mode, get_mode_null(mode));
3803 in[0] = new_Unknown(mode);
3805 ret = new_Return(get_store(), 1, in);
3807 add_immBlock_pred(end_block, ret);
3810 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3811 mature_immBlock(imature_blocks[i]);
3813 DEL_ARR_F(imature_blocks);
3814 imature_blocks = NULL;
3816 mature_immBlock(first_block);
3817 mature_immBlock(end_block);
3819 irg_finalize_cons(irg);
3821 /* finalize the frame type */
3822 ir_type *frame_type = get_irg_frame_type(irg);
3823 int n = get_compound_n_members(frame_type);
3826 for(int i = 0; i < n; ++i) {
3827 ir_entity *entity = get_compound_member(frame_type, i);
3828 ir_type *entity_type = get_entity_type(entity);
3830 int align = get_type_alignment_bytes(entity_type);
3831 if(align > align_all)
3835 misalign = offset % align;
3837 offset += align - misalign;
3841 set_entity_offset(entity, offset);
3842 offset += get_type_size_bytes(entity_type);
3844 set_type_size_bytes(frame_type, offset);
3845 set_type_alignment_bytes(frame_type, align_all);
3846 set_type_state(frame_type, layout_fixed);
3851 static void create_global_variable(declaration_t *declaration)
3855 switch ((storage_class_tag_t)declaration->storage_class) {
3856 case STORAGE_CLASS_STATIC:
3857 vis = visibility_local;
3860 case STORAGE_CLASS_EXTERN:
3861 vis = visibility_external_allocated;
3864 case STORAGE_CLASS_NONE:
3865 vis = visibility_external_visible;
3868 case STORAGE_CLASS_THREAD:
3869 vis = visibility_external_visible;
3872 case STORAGE_CLASS_THREAD_EXTERN:
3873 vis = visibility_external_allocated;
3876 case STORAGE_CLASS_THREAD_STATIC:
3877 vis = visibility_local;
3881 var_type = get_tls_type();
3885 var_type = get_glob_type();
3889 create_declaration_entity(declaration,
3890 DECLARATION_KIND_GLOBAL_VARIABLE,
3892 set_entity_visibility(declaration->v.entity, vis);
3894 current_ir_graph = get_const_code_irg();
3895 create_initializer(declaration);
3898 case STORAGE_CLASS_TYPEDEF:
3899 case STORAGE_CLASS_AUTO:
3900 case STORAGE_CLASS_REGISTER:
3901 case STORAGE_CLASS_ENUM_ENTRY:
3904 panic("Invalid storage class for global variable");
3907 static void context_to_firm(context_t *context)
3909 /* first pass: create declarations */
3910 declaration_t *declaration = context->declarations;
3911 for( ; declaration != NULL; declaration = declaration->next) {
3912 if(declaration->namespc != NAMESPACE_NORMAL)
3914 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3915 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3917 if(declaration->symbol == NULL)
3920 type_t *type = skip_typeref(declaration->type);
3921 if(is_type_function(type)) {
3922 get_function_entity(declaration);
3924 create_global_variable(declaration);
3928 /* second pass: create code */
3929 declaration = context->declarations;
3930 for( ; declaration != NULL; declaration = declaration->next) {
3931 if(declaration->namespc != NAMESPACE_NORMAL)
3933 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3934 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3936 if(declaration->symbol == NULL)
3939 type_t *type = declaration->type;
3940 if(type->kind != TYPE_FUNCTION)
3943 create_function(declaration);
3947 void init_ast2firm(void)
3949 obstack_init(&asm_obst);
3950 init_atomic_modes();
3952 /* create idents for all known runtime functions */
3953 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
3954 predef_idents[rts_data[i].id] = new_id_from_str(rts_data[i].name);
3958 void exit_ast2firm(void)
3960 obstack_free(&asm_obst, NULL);
3963 void translation_unit_to_firm(translation_unit_t *unit)
3965 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3966 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3967 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3969 ir_type_int = get_ir_type(type_int);
3970 ir_type_const_char = get_ir_type(type_const_char);
3971 ir_type_wchar_t = get_ir_type(type_wchar_t);
3972 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3975 type_void->base.firm_type = ir_type_void;
3977 /* just to be sure */
3978 continue_label = NULL;
3980 current_switch_cond = NULL;
3982 context_to_firm(&unit->context);