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 || type->atype == ATOMIC_TYPE_DOUBLE) {
370 set_type_alignment_bytes(irtype, 4);
376 static ir_type *create_method_type(const function_type_t *function_type)
378 type_t *return_type = function_type->return_type;
380 ident *id = unique_ident("functiontype");
381 int n_parameters = count_parameters(function_type);
382 int n_results = return_type == type_void ? 0 : 1;
383 ir_type *irtype = new_type_method(id, n_parameters, n_results);
385 if(return_type != type_void) {
386 ir_type *restype = get_ir_type(return_type);
387 set_method_res_type(irtype, 0, restype);
390 function_parameter_t *parameter = function_type->parameters;
392 for( ; parameter != NULL; parameter = parameter->next) {
393 ir_type *p_irtype = get_ir_type(parameter->type);
394 set_method_param_type(irtype, n, p_irtype);
398 if(function_type->variadic || function_type->unspecified_parameters) {
399 set_method_variadicity(irtype, variadicity_variadic);
405 static ir_type *create_pointer_type(pointer_type_t *type)
407 type_t *points_to = type->points_to;
408 ir_type *ir_points_to;
409 /* Avoid endless recursion if the points_to type contains this poiner type
410 * again (might be a struct). We therefore first create a void* pointer
411 * and then set the real points_to type
413 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
414 ir_type_void, mode_P_data);
415 type->type.firm_type = ir_type;
417 ir_points_to = get_ir_type(points_to);
418 set_pointer_points_to_type(ir_type, ir_points_to);
423 static ir_type *create_array_type(array_type_t *type)
425 type_t *element_type = type->element_type;
426 ir_type *ir_element_type = get_ir_type(element_type);
428 ident *id = unique_ident("array");
429 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
431 if(type->size != NULL) {
432 int n_elements = fold_constant(type->size);
434 set_array_bounds_int(ir_type, 0, 0, n_elements);
436 size_t elemsize = get_type_size_bytes(ir_element_type);
437 int align = get_type_alignment_bytes(ir_element_type);
438 if(elemsize % align > 0) {
439 elemsize += align - (elemsize % align);
441 set_type_size_bytes(ir_type, n_elements * elemsize);
442 set_type_alignment_bytes(ir_type, align);
443 set_type_state(ir_type, layout_fixed);
445 set_array_lower_bound_int(ir_type, 0, 0);
452 * Return the signed integer type of size bits.
454 * @param size the size
456 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
459 static ir_mode *s_modes[64 + 1] = {NULL, };
463 if (size <= 0 || size > 64)
466 mode = s_modes[size];
470 snprintf(name, sizeof(name), "bf_I%u", size);
471 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
472 size <= 32 ? 32 : size );
473 s_modes[size] = mode;
477 snprintf(name, sizeof(name), "I%u", size);
478 ident *id = new_id_from_str(name);
479 res = new_type_primitive(mangle_u(get_type_ident(base_tp), id), mode);
480 set_primitive_base_type(res, base_tp);
486 * Return the unsigned integer type of size bits.
488 * @param size the size
490 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
493 static ir_mode *u_modes[64 + 1] = {NULL, };
497 if (size <= 0 || size > 64)
500 mode = u_modes[size];
504 snprintf(name, sizeof(name), "bf_U%u", size);
505 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
506 size <= 32 ? 32 : size );
507 u_modes[size] = mode;
512 snprintf(name, sizeof(name), "U%u", size);
513 ident *id = new_id_from_str(name);
514 res = new_type_primitive(mangle_u(get_type_ident(base_tp), id), mode);
515 set_primitive_base_type(res, base_tp);
520 static ir_type *create_bitfield_type(bitfield_type_t *const type)
522 type_t *base = skip_typeref(type->base);
523 assert(base->kind == TYPE_ATOMIC);
524 ir_type *irbase = get_ir_type(base);
526 unsigned size = fold_constant(type->size);
528 assert(!is_type_floating(base));
529 if(is_type_signed(base)) {
530 return get_signed_int_type_for_bit_size(irbase, size);
532 return get_unsigned_int_type_for_bit_size(irbase, size);
536 #define INVALID_TYPE ((ir_type_ptr)-1)
538 static ir_type *create_struct_type(compound_type_t *type)
540 symbol_t *symbol = type->declaration->symbol;
543 id = unique_ident(symbol->string);
545 id = unique_ident("__anonymous_struct");
547 ir_type *irtype = new_type_struct(id);
549 type->type.firm_type = irtype;
551 size_t align_all = 1;
553 size_t bit_offset = 0;
554 declaration_t *entry = type->declaration->context.declarations;
555 for( ; entry != NULL; entry = entry->next) {
556 if(entry->namespc != NAMESPACE_NORMAL)
559 type_t *entry_type = skip_typeref(entry->type);
560 ir_type *base_irtype;
561 if(entry_type->kind == TYPE_BITFIELD) {
562 base_irtype = get_ir_type(entry_type->bitfield.base);
564 base_irtype = get_ir_type(entry_type);
567 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
568 size_t misalign = offset % entry_alignment;
570 dbg_info *dbgi = get_dbg_info(&entry->source_position);
571 ir_entity *entity = NULL;
572 if(entry->symbol != NULL) {
573 ident *ident = new_id_from_str(entry->symbol->string);
574 ir_type *entry_irtype = get_ir_type(entry_type);
575 entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
577 /* only bitfields are allowed to be anonymous */
578 assert(entry_type->kind == TYPE_BITFIELD);
582 size_t bits_remainder;
583 if(entry_type->kind == TYPE_BITFIELD) {
584 size_t size_bits = fold_constant(entry_type->bitfield.size);
585 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
587 if(size_bits > rest_size_bits) {
588 /* start a new bucket */
589 offset += entry_alignment - misalign;
595 /* put into current bucket */
596 base = offset - misalign;
597 bits_remainder = misalign * 8 + bit_offset;
600 offset += size_bits / 8;
601 bit_offset = bit_offset + (size_bits % 8);
603 size_t entry_size = get_type_size_bytes(base_irtype);
604 if(misalign > 0 || bit_offset > 0)
605 offset += entry_alignment - misalign;
609 offset += entry_size;
613 if(entry_alignment > align_all) {
614 if(entry_alignment % align_all != 0) {
615 panic("uneven alignments not supported yet");
617 align_all = entry_alignment;
621 set_entity_offset(entity, base);
622 set_entity_offset_bits_remainder(entity,
623 (unsigned char) bits_remainder);
624 add_struct_member(irtype, entity);
625 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
626 entry->v.entity = entity;
630 size_t misalign = offset % align_all;
632 offset += align_all - misalign;
634 set_type_alignment_bytes(irtype, align_all);
635 set_type_size_bytes(irtype, offset);
636 set_type_state(irtype, layout_fixed);
641 static ir_type *create_union_type(compound_type_t *type)
643 declaration_t *declaration = type->declaration;
644 symbol_t *symbol = declaration->symbol;
647 id = unique_ident(symbol->string);
649 id = unique_ident("__anonymous_union");
651 ir_type *irtype = new_type_union(id);
653 type->type.firm_type = irtype;
657 declaration_t *entry = declaration->context.declarations;
658 for( ; entry != NULL; entry = entry->next) {
659 if(entry->namespc != NAMESPACE_NORMAL)
662 ident *ident = new_id_from_str(entry->symbol->string);
663 ir_type *entry_ir_type = get_ir_type(entry->type);
665 int entry_size = get_type_size_bytes(entry_ir_type);
666 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
668 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
669 ir_entity *const entity = new_d_entity(irtype, ident, entry_ir_type,
671 add_union_member(irtype, entity);
672 set_entity_offset(entity, 0);
673 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
674 entry->v.entity = entity;
676 if(entry_size > size) {
679 if(entry_alignment > align_all) {
680 if(entry_alignment % align_all != 0) {
681 panic("Uneven alignments not supported yet");
683 align_all = entry_alignment;
687 set_type_alignment_bytes(irtype, align_all);
688 set_type_size_bytes(irtype, size);
689 set_type_state(irtype, layout_fixed);
694 static ir_node *expression_to_firm(const expression_t *expression);
695 static inline ir_mode *get_ir_mode(type_t *type);
697 static ir_type *create_enum_type(enum_type_t *const type)
699 type->type.firm_type = ir_type_int;
701 ir_mode *const mode = get_ir_mode((type_t*) type);
702 tarval *const one = get_mode_one(mode);
703 tarval * tv_next = get_tarval_null(mode);
705 declaration_t *declaration = type->declaration->next;
706 for (; declaration != NULL; declaration = declaration->next) {
707 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
710 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
712 expression_t *const init = declaration->init.enum_value;
714 ir_node *const cnst = expression_to_firm(init);
715 if (!is_Const(cnst)) {
716 panic("couldn't fold constant");
718 tv_next = get_Const_tarval(cnst);
720 declaration->v.enum_val = tv_next;
721 tv_next = tarval_add(tv_next, one);
727 static ir_type *get_ir_type(type_t *type)
729 assert(type != NULL);
731 type = skip_typeref(type);
733 if(type->base.firm_type != NULL) {
734 assert(type->base.firm_type != INVALID_TYPE);
735 return type->base.firm_type;
738 ir_type *firm_type = NULL;
741 firm_type = create_atomic_type(&type->atomic);
744 firm_type = create_method_type(&type->function);
747 firm_type = create_pointer_type(&type->pointer);
750 firm_type = create_array_type(&type->array);
752 case TYPE_COMPOUND_STRUCT:
753 firm_type = create_struct_type(&type->compound);
755 case TYPE_COMPOUND_UNION:
756 firm_type = create_union_type(&type->compound);
759 firm_type = create_enum_type(&type->enumt);
762 firm_type = get_ir_type(type->builtin.real_type);
765 firm_type = create_bitfield_type(&type->bitfield);
773 if(firm_type == NULL)
774 panic("unknown type found");
776 type->base.firm_type = firm_type;
780 static inline ir_mode *get_ir_mode(type_t *type)
782 ir_type *irtype = get_ir_type(type);
784 /* firm doesn't report a mode for arrays somehow... */
785 if(is_Array_type(irtype)) {
789 ir_mode *mode = get_type_mode(irtype);
790 assert(mode != NULL);
794 static ident *predef_idents[rts_max];
796 /** Names of the runtime functions. */
797 static const struct {
798 int id; /**< the rts id */
799 int n_res; /**< number of return values */
800 const char *name; /**< the name of the rts function */
801 int n_params; /**< number of parameters */
802 unsigned flags; /**< language flags */
804 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
805 { rts_abort, 0, "abort", 0, _C89 },
806 { rts_abs, 1, "abs", 1, _C89 },
807 { rts_labs, 1, "labs", 1, _C89 },
808 { rts_llabs, 1, "llabs", 1, _C99 },
809 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
811 { rts_fabs, 1, "fabs", 1, _C89 },
812 { rts_sqrt, 1, "sqrt", 1, _C89 },
813 { rts_cbrt, 1, "cbrt", 1, _C99 },
814 { rts_exp, 1, "exp", 1, _C89 },
815 { rts_exp2, 1, "exp2", 1, _C89 },
816 { rts_exp10, 1, "exp10", 1, _GNUC },
817 { rts_log, 1, "log", 1, _C89 },
818 { rts_log2, 1, "log2", 1, _C89 },
819 { rts_log10, 1, "log10", 1, _C89 },
820 { rts_pow, 1, "pow", 2, _C89 },
821 { rts_sin, 1, "sin", 1, _C89 },
822 { rts_cos, 1, "cos", 1, _C89 },
823 { rts_tan, 1, "tan", 1, _C89 },
824 { rts_asin, 1, "asin", 1, _C89 },
825 { rts_acos, 1, "acos", 1, _C89 },
826 { rts_atan, 1, "atan", 1, _C89 },
827 { rts_sinh, 1, "sinh", 1, _C89 },
828 { rts_cosh, 1, "cosh", 1, _C89 },
829 { rts_tanh, 1, "tanh", 1, _C89 },
831 { rts_fabsf, 1, "fabsf", 1, _C99 },
832 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
833 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
834 { rts_expf, 1, "expf", 1, _C99 },
835 { rts_exp2f, 1, "exp2f", 1, _C99 },
836 { rts_exp10f, 1, "exp10f", 1, _GNUC },
837 { rts_logf, 1, "logf", 1, _C99 },
838 { rts_log2f, 1, "log2f", 1, _C99 },
839 { rts_log10f, 1, "log10f", 1, _C99 },
840 { rts_powf, 1, "powf", 2, _C99 },
841 { rts_sinf, 1, "sinf", 1, _C99 },
842 { rts_cosf, 1, "cosf", 1, _C99 },
843 { rts_tanf, 1, "tanf", 1, _C99 },
844 { rts_asinf, 1, "asinf", 1, _C99 },
845 { rts_acosf, 1, "acosf", 1, _C99 },
846 { rts_atanf, 1, "atanf", 1, _C99 },
847 { rts_sinhf, 1, "sinhf", 1, _C99 },
848 { rts_coshf, 1, "coshf", 1, _C99 },
849 { rts_tanhf, 1, "tanhf", 1, _C99 },
851 { rts_fabsl, 1, "fabsl", 1, _C99 },
852 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
853 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
854 { rts_expl, 1, "expl", 1, _C99 },
855 { rts_exp2l, 1, "exp2l", 1, _C99 },
856 { rts_exp10l, 1, "exp10l", 1, _GNUC },
857 { rts_logl, 1, "logl", 1, _C99 },
858 { rts_log2l, 1, "log2l", 1, _C99 },
859 { rts_log10l, 1, "log10l", 1, _C99 },
860 { rts_powl, 1, "powl", 2, _C99 },
861 { rts_sinl, 1, "sinl", 1, _C99 },
862 { rts_cosl, 1, "cosl", 1, _C99 },
863 { rts_tanl, 1, "tanl", 1, _C99 },
864 { rts_asinl, 1, "asinl", 1, _C99 },
865 { rts_acosl, 1, "acosl", 1, _C99 },
866 { rts_atanl, 1, "atanl", 1, _C99 },
867 { rts_sinhl, 1, "sinhl", 1, _C99 },
868 { rts_coshl, 1, "coshl", 1, _C99 },
869 { rts_tanhl, 1, "tanhl", 1, _C99 },
871 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
872 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
873 { rts_strcmp, 1, "strcmp", 2, _C89 },
874 { rts_strncmp, 1, "strncmp", 3, _C89 }
877 static ir_entity* get_function_entity(declaration_t *declaration)
879 if(declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
880 return declaration->v.entity;
881 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
883 symbol_t *symbol = declaration->symbol;
884 ident *id = new_id_from_str(symbol->string);
886 ir_type *global_type = get_glob_type();
887 ir_type *ir_type_method = get_ir_type(declaration->type);
888 assert(is_Method_type(ir_type_method));
890 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
891 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
892 set_entity_ld_ident(entity, id);
893 if(declaration->storage_class == STORAGE_CLASS_STATIC
894 || declaration->is_inline) {
895 set_entity_visibility(entity, visibility_local);
896 } else if(declaration->init.statement != NULL) {
897 set_entity_visibility(entity, visibility_external_visible);
899 set_entity_visibility(entity, visibility_external_allocated);
901 /* We should check for file scope here, but as long as we compile C only
902 this is not needed. */
903 int n_params = get_method_n_params(ir_type_method);
904 int n_res = get_method_n_ress(ir_type_method);
907 if (n_params == 0 && n_res == 0 && id == predef_idents[rts_abort]) {
908 /* found abort(), store for later */
912 if (! firm_opt.freestanding) {
913 /* check for a known runtime function */
914 for (i = 0; i < rts_max; ++i) {
915 /* ignore those rts functions not necessary needed for current mode */
916 if ((c_mode & rts_data[i].flags) == 0)
918 if (n_params == rts_data[i].n_params && n_res == rts_data[i].n_res &&
919 id == predef_idents[rts_data[i].id])
920 rts_entities[rts_data[i].id] = entity;
925 set_entity_allocation(entity, allocation_static);
927 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
928 declaration->v.entity = entity;
933 static ir_node *const_to_firm(const const_expression_t *cnst)
935 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
936 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
941 if(mode_is_float(mode)) {
942 tv = new_tarval_from_double(cnst->v.float_value, mode);
944 if(mode_is_signed(mode)) {
945 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
947 len = snprintf(buf, sizeof(buf), "%llu",
948 (unsigned long long) cnst->v.int_value);
950 tv = new_tarval_from_str(buf, len, mode);
953 return new_d_Const(dbgi, mode, tv);
956 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
959 assert(entity != NULL);
960 union symconst_symbol sym;
961 sym.entity_p = entity;
962 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
965 static ir_node *string_to_firm(const source_position_t *const src_pos,
966 const char *const id_prefix,
967 const char *const string)
969 ir_type *const global_type = get_glob_type();
970 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
973 ident *const id = unique_ident(id_prefix);
974 dbg_info *const dbgi = get_dbg_info(src_pos);
975 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
976 set_entity_ld_ident(entity, id);
977 set_entity_variability(entity, variability_constant);
978 set_entity_allocation(entity, allocation_static);
980 ir_type *const elem_type = ir_type_const_char;
981 ir_mode *const mode = get_type_mode(elem_type);
983 const size_t slen = strlen(string) + 1;
985 set_array_lower_bound_int(type, 0, 0);
986 set_array_upper_bound_int(type, 0, slen);
987 set_type_size_bytes(type, slen);
988 set_type_state(type, layout_fixed);
990 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
991 for(size_t i = 0; i < slen; ++i) {
992 tvs[i] = new_tarval_from_long(string[i], mode);
995 set_array_entity_values(entity, tvs, slen);
998 return create_symconst(dbgi, mode_P_data, entity);
1001 static ir_node *string_literal_to_firm(
1002 const string_literal_expression_t* literal)
1004 return string_to_firm(&literal->expression.source_position, "Lstr",
1008 static ir_node *wide_string_literal_to_firm(
1009 const wide_string_literal_expression_t* const literal)
1011 ir_type *const global_type = get_glob_type();
1012 ir_type *const elem_type = ir_type_wchar_t;
1013 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
1016 ident *const id = unique_ident("Lstr");
1017 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
1018 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1019 set_entity_ld_ident(entity, id);
1020 set_entity_variability(entity, variability_constant);
1021 set_entity_allocation(entity, allocation_static);
1023 ir_mode *const mode = get_type_mode(elem_type);
1025 const wchar_rep_t *const string = literal->value.begin;
1026 const size_t slen = literal->value.size;
1028 set_array_lower_bound_int(type, 0, 0);
1029 set_array_upper_bound_int(type, 0, slen);
1030 set_type_size_bytes(type, slen);
1031 set_type_state(type, layout_fixed);
1033 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1034 for(size_t i = 0; i < slen; ++i) {
1035 tvs[i] = new_tarval_from_long(string[i], mode);
1038 set_array_entity_values(entity, tvs, slen);
1041 return create_symconst(dbgi, mode_P_data, entity);
1044 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
1045 dbg_info *const dbgi)
1047 if(is_compound_type(irtype) || is_Array_type(irtype)) {
1051 ir_mode *const mode = get_type_mode(irtype);
1052 ir_node *const memory = get_store();
1053 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1054 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1055 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1056 set_store(load_mem);
1060 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1062 ir_mode *mode = get_irn_mode(node);
1064 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1066 if(!mode_is_float(mode))
1069 /* check if there is already a Conv */
1070 if (get_irn_op(node) == op_Conv) {
1071 /* convert it into a strict Conv */
1072 set_Conv_strict(node, 1);
1076 /* otherwise create a new one */
1077 return new_d_strictConv(dbgi, node, mode);
1080 static ir_node *get_global_var_address(dbg_info *const dbgi,
1081 const declaration_t *const decl)
1083 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1085 ir_entity *const entity = decl->v.entity;
1086 switch ((storage_class_tag_t)decl->storage_class) {
1087 case STORAGE_CLASS_THREAD:
1088 case STORAGE_CLASS_THREAD_EXTERN:
1089 case STORAGE_CLASS_THREAD_STATIC: {
1090 ir_node *const no_mem = new_NoMem();
1091 ir_node *const tls = get_irg_tls(current_ir_graph);
1092 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1096 return create_symconst(dbgi, mode_P_data, entity);
1100 /* Returns the correct base address depending on whether it is a parameter or a
1101 * normal local variable */
1102 static ir_node *get_local_frame(ir_entity *const ent)
1104 ir_graph *const irg = current_ir_graph;
1105 const ir_type *const owner = get_entity_owner(ent);
1106 if (owner == get_irg_frame_type(irg)) {
1107 return get_irg_frame(irg);
1109 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1110 return get_irg_value_param_base(irg);
1114 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1116 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
1117 declaration_t *declaration = ref->declaration;
1118 type_t *type = skip_typeref(declaration->type);
1120 switch((declaration_kind_t) declaration->declaration_kind) {
1121 case DECLARATION_KIND_UNKNOWN:
1122 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
1128 case DECLARATION_KIND_ENUM_ENTRY: {
1129 ir_mode *const mode = get_ir_mode(type);
1130 return new_Const(mode, declaration->v.enum_val);
1133 case DECLARATION_KIND_LOCAL_VARIABLE: {
1134 ir_mode *const mode = get_ir_mode(type);
1135 return get_value(declaration->v.value_number, mode);
1137 case DECLARATION_KIND_FUNCTION: {
1138 ir_mode *const mode = get_ir_mode(type);
1139 return create_symconst(dbgi, mode, declaration->v.entity);
1141 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1142 ir_node *const addr = get_global_var_address(dbgi, declaration);
1143 ir_type *const irtype = get_entity_type(declaration->v.entity);
1144 return deref_address(irtype, addr, dbgi);
1147 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1148 ir_entity *entity = declaration->v.entity;
1149 ir_node *frame = get_local_frame(entity);
1150 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1151 ir_type *irtype = get_entity_type(entity);
1152 return deref_address(irtype, sel, dbgi);
1155 case DECLARATION_KIND_COMPOUND_MEMBER:
1156 case DECLARATION_KIND_LABEL_BLOCK:
1157 panic("not implemented reference type");
1160 panic("reference to declaration with unknown type found");
1163 static ir_node *reference_addr(const reference_expression_t *ref)
1165 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
1166 declaration_t *declaration = ref->declaration;
1168 switch((declaration_kind_t) declaration->declaration_kind) {
1169 case DECLARATION_KIND_UNKNOWN:
1171 case DECLARATION_KIND_LOCAL_VARIABLE:
1172 panic("local variable without entity has no address");
1173 case DECLARATION_KIND_FUNCTION: {
1174 type_t *const type = skip_typeref(ref->expression.datatype);
1175 ir_mode *const mode = get_ir_mode(type);
1176 return create_symconst(dbgi, mode, declaration->v.entity);
1178 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1179 ir_node *const addr = get_global_var_address(dbgi, declaration);
1182 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1183 ir_entity *entity = declaration->v.entity;
1184 ir_node *frame = get_local_frame(entity);
1185 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1190 case DECLARATION_KIND_ENUM_ENTRY:
1191 panic("trying to reference enum entry");
1193 case DECLARATION_KIND_COMPOUND_MEMBER:
1194 case DECLARATION_KIND_LABEL_BLOCK:
1195 panic("not implemented reference type");
1198 panic("reference to declaration with unknown type found");
1201 static ir_node *process_builtin_call(const call_expression_t *call)
1203 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1205 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1206 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1208 type_t *type = skip_typeref(builtin->expression.datatype);
1209 assert(is_type_pointer(type));
1211 type_t *function_type = skip_typeref(type->pointer.points_to);
1212 symbol_t *symbol = builtin->symbol;
1214 switch(symbol->ID) {
1215 case T___builtin_alloca: {
1216 if(call->arguments == NULL || call->arguments->next != NULL) {
1217 panic("invalid number of parameters on __builtin_alloca");
1219 expression_t *argument = call->arguments->expression;
1220 ir_node *size = expression_to_firm(argument);
1222 ir_node *store = get_store();
1223 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1225 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1227 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1231 case T___builtin_nan:
1232 case T___builtin_nanf:
1233 case T___builtin_nand: {
1234 /* Ignore string for now... */
1235 assert(is_type_function(function_type));
1236 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1237 tarval *tv = get_mode_NAN(mode);
1238 ir_node *res = new_d_Const(dbgi, mode, tv);
1241 case T___builtin_va_end:
1244 panic("Unsupported builtin found\n");
1248 static ir_node *call_expression_to_firm(const call_expression_t *call)
1250 assert(get_cur_block() != NULL);
1252 expression_t *function = call->function;
1253 if(function->kind == EXPR_BUILTIN_SYMBOL) {
1254 return process_builtin_call(call);
1256 ir_node *callee = expression_to_firm(function);
1258 type_t *type = skip_typeref(function->base.datatype);
1259 assert(is_type_pointer(type));
1260 pointer_type_t *pointer_type = &type->pointer;
1261 type_t *points_to = skip_typeref(pointer_type->points_to);
1262 assert(is_type_function(points_to));
1263 function_type_t *function_type = &points_to->function;
1265 int n_parameters = 0;
1266 call_argument_t *argument = call->arguments;
1267 for( ; argument != NULL; argument = argument->next) {
1271 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1273 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1274 ir_type *new_method_type = NULL;
1275 if(function_type->variadic || function_type->unspecified_parameters) {
1276 /* we need to construct a new method type matching the call
1278 int n_res = get_method_n_ress(ir_method_type);
1279 new_method_type = new_type_method(unique_ident("calltype"),
1280 n_parameters, n_res);
1281 set_method_calling_convention(new_method_type,
1282 get_method_calling_convention(ir_method_type));
1283 set_method_additional_properties(new_method_type,
1284 get_method_additional_properties(ir_method_type));
1286 for(int i = 0; i < n_res; ++i) {
1287 set_method_res_type(new_method_type, i,
1288 get_method_res_type(ir_method_type, i));
1291 ir_node *in[n_parameters];
1293 argument = call->arguments;
1295 for( ; argument != NULL; argument = argument->next) {
1296 expression_t *expression = argument->expression;
1297 ir_node *arg_node = expression_to_firm(expression);
1299 arg_node = do_strict_conv(dbgi, arg_node);
1302 if(new_method_type != NULL) {
1303 ir_type *irtype = get_ir_type(expression->base.datatype);
1304 set_method_param_type(new_method_type, n, irtype);
1309 assert(n == n_parameters);
1311 if(new_method_type != NULL)
1312 ir_method_type = new_method_type;
1314 ir_node *store = get_store();
1315 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1317 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1320 type_t *return_type = skip_typeref(function_type->return_type);
1321 ir_node *result = NULL;
1323 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1325 if(is_type_scalar(return_type)) {
1326 mode = get_ir_mode(return_type);
1330 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1331 result = new_d_Proj(dbgi, resproj, mode, 0);
1337 static void statement_to_firm(statement_t *statement);
1338 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1340 static ir_node *expression_to_addr(const expression_t *expression);
1341 static void create_condition_evaluation(const expression_t *expression,
1342 ir_node *true_block,
1343 ir_node *false_block);
1345 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1348 value = do_strict_conv(dbgi, value);
1350 ir_node *memory = get_store();
1352 if(is_type_scalar(type)) {
1353 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1354 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1355 set_store(store_mem);
1357 ir_type *irtype = get_ir_type(type);
1358 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1359 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1360 set_store(copyb_mem);
1364 static void set_value_for_expression(const expression_t *expression,
1367 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1368 value = do_strict_conv(dbgi, value);
1370 if(expression->kind == EXPR_REFERENCE) {
1371 const reference_expression_t *ref = &expression->reference;
1373 declaration_t *declaration = ref->declaration;
1374 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1375 if(declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1376 set_value(declaration->v.value_number, value);
1381 ir_node *addr = expression_to_addr(expression);
1382 type_t *type = skip_typeref(expression->base.datatype);
1383 assign_value(dbgi, addr, type, value);
1386 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1388 ir_mode *value_mode = get_irn_mode(value);
1390 if (value_mode == dest_mode || is_Bad(value))
1393 if(dest_mode == mode_b) {
1394 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1395 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1396 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1400 return new_d_Conv(dbgi, value, dest_mode);
1403 static ir_node *create_incdec(const unary_expression_t *expression)
1405 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1406 type_t *type = skip_typeref(expression->expression.datatype);
1407 ir_mode *mode = get_ir_mode(type);
1408 expression_t *value = expression->value;
1410 ir_node *value_node = expression_to_firm(value);
1413 if(is_type_pointer(type)) {
1414 pointer_type_t *pointer_type = &type->pointer;
1415 unsigned elem_size = get_type_size(pointer_type->points_to);
1416 offset = new_Const_long(mode_int, elem_size);
1418 assert(is_type_arithmetic(type));
1419 offset = new_Const(mode, get_mode_one(mode));
1422 switch(expression->expression.kind) {
1423 case EXPR_UNARY_POSTFIX_INCREMENT: {
1424 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1425 set_value_for_expression(value, new_value);
1428 case EXPR_UNARY_POSTFIX_DECREMENT: {
1429 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1430 set_value_for_expression(value, new_value);
1433 case EXPR_UNARY_PREFIX_INCREMENT: {
1434 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1435 set_value_for_expression(value, new_value);
1438 case EXPR_UNARY_PREFIX_DECREMENT: {
1439 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1440 set_value_for_expression(value, new_value);
1444 panic("no incdec expr in create_incdec");
1449 static bool is_local_variable(expression_t *expression)
1451 if (expression->kind != EXPR_REFERENCE)
1453 reference_expression_t *ref_expr = &expression->reference;
1454 declaration_t *declaration = ref_expr->declaration;
1455 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1458 static pn_Cmp get_pnc(const expression_kind_t kind)
1461 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1462 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1463 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Ne;
1464 case EXPR_BINARY_ISLESS:
1465 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1466 case EXPR_BINARY_ISLESSEQUAL:
1467 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1468 case EXPR_BINARY_ISGREATER:
1469 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1470 case EXPR_BINARY_ISGREATEREQUAL:
1471 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1472 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1477 panic("trying to get pn_Cmp from non-comparison binexpr type");
1481 * Handle the assume optimizer hint: check if a Confirm
1482 * node can be created.
1484 * @param dbi debug info
1485 * @param expr the IL assume expression
1487 * we support here only some simple cases:
1492 static ir_node *handle_assume_compare(dbg_info *dbi,
1493 const binary_expression_t *expression)
1495 expression_t *op1 = expression->left;
1496 expression_t *op2 = expression->right;
1497 declaration_t *var2, *var = NULL;
1498 ir_node *res = NULL;
1501 cmp_val = get_pnc(expression->expression.kind);
1503 if (is_local_variable(op1) && is_local_variable(op2)) {
1504 var = op1->reference.declaration;
1505 var2 = op2->reference.declaration;
1507 type_t *const type = skip_typeref(var->type);
1508 ir_mode *const mode = get_ir_mode(type);
1510 ir_node *const irn1 = get_value(var->v.value_number, mode);
1511 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1513 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1514 set_value(var2->v.value_number, res);
1516 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1517 set_value(var->v.value_number, res);
1523 if (is_local_variable(op1) && is_constant_expression(op2)) {
1524 var = op1->reference.declaration;
1526 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1527 cmp_val = get_inversed_pnc(cmp_val);
1528 var = op2->reference.declaration;
1533 type_t *const type = skip_typeref(var->type);
1534 ir_mode *const mode = get_ir_mode(type);
1536 res = get_value(var->v.value_number, mode);
1537 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1538 set_value(var->v.value_number, res);
1544 * Handle the assume optimizer hint.
1546 * @param dbi debug info
1547 * @param expr the IL assume expression
1549 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1550 switch(expression->kind) {
1551 case EXPR_BINARY_EQUAL:
1552 case EXPR_BINARY_NOTEQUAL:
1553 case EXPR_BINARY_LESS:
1554 case EXPR_BINARY_LESSEQUAL:
1555 case EXPR_BINARY_GREATER:
1556 case EXPR_BINARY_GREATEREQUAL:
1557 return handle_assume_compare(dbi, &expression->binary);
1563 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1565 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1566 type_t *type = skip_typeref(expression->expression.datatype);
1568 if(expression->expression.kind == EXPR_UNARY_TAKE_ADDRESS)
1569 return expression_to_addr(expression->value);
1571 const expression_t *value = expression->value;
1573 switch(expression->expression.kind) {
1574 case EXPR_UNARY_NEGATE: {
1575 ir_node *value_node = expression_to_firm(value);
1576 ir_mode *mode = get_ir_mode(type);
1577 return new_d_Minus(dbgi, value_node, mode);
1579 case EXPR_UNARY_PLUS:
1580 return expression_to_firm(value);
1581 case EXPR_UNARY_BITWISE_NEGATE: {
1582 ir_node *value_node = expression_to_firm(value);
1583 ir_mode *mode = get_ir_mode(type);
1584 return new_d_Not(dbgi, value_node, mode);
1586 case EXPR_UNARY_NOT: {
1587 ir_node *value_node = expression_to_firm(value);
1588 ir_mode *mode = get_ir_mode(type);
1589 if(get_irn_mode(value_node) != mode_b) {
1590 value_node = create_conv(dbgi, value_node, mode_b);
1592 value_node = new_d_Not(dbgi, value_node, mode_b);
1593 if(mode != mode_b) {
1594 value_node = create_conv(dbgi, value_node, mode);
1598 case EXPR_UNARY_DEREFERENCE: {
1599 ir_node *value_node = expression_to_firm(value);
1600 type_t *value_type = skip_typeref(value->base.datatype);
1601 ir_type *irtype = get_ir_type(value_type);
1602 assert(is_Pointer_type(irtype));
1603 ir_type *points_to = get_pointer_points_to_type(irtype);
1604 return deref_address(points_to, value_node, dbgi);
1606 case EXPR_UNARY_POSTFIX_INCREMENT:
1607 case EXPR_UNARY_POSTFIX_DECREMENT:
1608 case EXPR_UNARY_PREFIX_INCREMENT:
1609 case EXPR_UNARY_PREFIX_DECREMENT:
1610 return create_incdec(expression);
1611 case EXPR_UNARY_CAST: {
1612 ir_node *value_node = expression_to_firm(value);
1613 ir_mode *mode = get_ir_mode(type);
1614 ir_node *node = create_conv(dbgi, value_node, mode);
1615 node = do_strict_conv(dbgi, node);
1618 case EXPR_UNARY_CAST_IMPLICIT: {
1619 ir_node *value_node = expression_to_firm(value);
1620 ir_mode *mode = get_ir_mode(type);
1621 return create_conv(dbgi, value_node, mode);
1623 case EXPR_UNARY_ASSUME:
1624 if(firm_opt.confirm)
1625 return handle_assume(dbgi, value);
1632 panic("invalid UNEXPR type found");
1635 static ir_node *create_lazy_op(const binary_expression_t *expression)
1637 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1638 type_t *type = expression->expression.datatype;
1639 ir_mode *mode = get_ir_mode(type);
1641 if(is_constant_expression(expression->left)) {
1642 long val = fold_constant(expression->left);
1643 expression_kind_t ekind = expression->expression.kind;
1644 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
1645 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1646 return expression_to_firm(expression->right);
1648 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
1649 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
1650 return new_Const(mode, get_mode_one(mode));
1654 ir_node *cur_block = get_cur_block();
1656 ir_node *one_block = new_immBlock();
1657 ir_node *one = new_Const(mode, get_mode_one(mode));
1658 ir_node *jmp_one = new_d_Jmp(dbgi);
1660 ir_node *zero_block = new_immBlock();
1661 ir_node *zero = new_Const(mode, get_mode_null(mode));
1662 ir_node *jmp_zero = new_d_Jmp(dbgi);
1664 set_cur_block(cur_block);
1665 create_condition_evaluation((const expression_t*) expression,
1666 one_block, zero_block);
1667 mature_immBlock(one_block);
1668 mature_immBlock(zero_block);
1670 ir_node *common_block = new_immBlock();
1671 add_immBlock_pred(common_block, jmp_one);
1672 add_immBlock_pred(common_block, jmp_zero);
1673 mature_immBlock(common_block);
1675 ir_node *in[2] = { one, zero };
1676 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1681 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1682 ir_node *right, ir_mode *mode);
1684 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1685 create_arithmetic_func func)
1687 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1688 ir_node *left = expression_to_firm(expression->left);
1689 ir_node *right = expression_to_firm(expression->right);
1690 type_t *type = expression->right->base.datatype;
1691 /* be careful with the modes, because in arithmetic assign nodes only
1692 * the right operand has the mode of the arithmetic already */
1693 ir_mode *mode = get_ir_mode(type);
1694 left = create_conv(dbgi, left, mode);
1695 ir_node *res = func(dbgi, left, right, mode);
1700 static ir_node *pointer_arithmetic(ir_node *const pointer,
1703 dbg_info *const dbgi,
1704 const create_arithmetic_func func)
1706 pointer_type_t *const pointer_type = &type->pointer;
1707 type_t *const points_to = pointer_type->points_to;
1708 const unsigned elem_size = get_type_size(points_to);
1710 assert(elem_size >= 1);
1711 if (elem_size > 1) {
1712 integer = create_conv(dbgi, integer, mode_int);
1713 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1714 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1718 ir_mode *const mode = get_ir_mode(type);
1719 return func(dbgi, pointer, integer, mode);
1722 static ir_node *create_arithmetic_assign_binop(
1723 const binary_expression_t *expression, create_arithmetic_func func)
1725 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1726 type_t *const type = skip_typeref(expression->expression.datatype);
1729 if (is_type_pointer(type)) {
1730 ir_node *const pointer = expression_to_firm(expression->left);
1731 ir_node * integer = expression_to_firm(expression->right);
1732 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1734 value = create_arithmetic_binop(expression, func);
1737 ir_mode *const mode = get_ir_mode(type);
1738 value = create_conv(dbgi, value, mode);
1739 set_value_for_expression(expression->left, value);
1744 static ir_node *create_add(const binary_expression_t *expression)
1746 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1747 ir_node *left = expression_to_firm(expression->left);
1748 ir_node *right = expression_to_firm(expression->right);
1749 type_t *type = expression->expression.datatype;
1751 expression_t *expr_left = expression->left;
1752 expression_t *expr_right = expression->right;
1753 type_t *type_left = skip_typeref(expr_left->base.datatype);
1754 type_t *type_right = skip_typeref(expr_right->base.datatype);
1756 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1757 ir_mode *const mode = get_ir_mode(type);
1758 return new_d_Add(dbgi, left, right, mode);
1761 if (is_type_pointer(type_left)) {
1762 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1764 assert(is_type_pointer(type_right));
1765 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1769 static ir_node *create_sub(const binary_expression_t *expression)
1771 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1772 expression_t *const expr_left = expression->left;
1773 expression_t *const expr_right = expression->right;
1774 ir_node *const left = expression_to_firm(expr_left);
1775 ir_node *const right = expression_to_firm(expr_right);
1776 type_t *const type = expression->expression.datatype;
1777 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1778 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1780 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1781 ir_mode *const mode = get_ir_mode(type);
1782 return new_d_Sub(dbgi, left, right, mode);
1783 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1784 const pointer_type_t *const ptr_type = &type_left->pointer;
1785 const unsigned elem_size = get_type_size(ptr_type->points_to);
1786 ir_mode *const mode = get_ir_mode(type);
1787 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1788 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1789 ir_node *const no_mem = new_NoMem();
1790 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1791 op_pin_state_floats);
1792 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1795 assert(is_type_pointer(type_left));
1796 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1799 static ir_node *create_shift(const binary_expression_t *expression)
1801 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1802 ir_node *left = expression_to_firm(expression->left);
1803 ir_node *right = expression_to_firm(expression->right);
1804 type_t *type = expression->expression.datatype;
1805 ir_mode *mode = get_ir_mode(type);
1807 /* firm always wants the shift count to be unsigned */
1808 right = create_conv(dbgi, right, mode_uint);
1812 switch(expression->expression.kind) {
1813 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1814 case EXPR_BINARY_SHIFTLEFT:
1815 res = new_d_Shl(dbgi, left, right, mode);
1817 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1818 case EXPR_BINARY_SHIFTRIGHT: {
1819 expression_t *expr_left = expression->left;
1820 type_t *type_left = skip_typeref(expr_left->base.datatype);
1822 if(is_type_signed(type_left)) {
1823 res = new_d_Shrs(dbgi, left, right, mode);
1825 res = new_d_Shr(dbgi, left, right, mode);
1830 panic("create shift op called for non-shift op");
1837 static ir_node *create_divmod(const binary_expression_t *expression)
1839 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1840 ir_node *left = expression_to_firm(expression->left);
1841 ir_node *right = expression_to_firm(expression->right);
1842 ir_node *pin = new_Pin(new_NoMem());
1843 /* be careful with the modes, because in arithmetic assign nodes only
1844 * the right operand has the mode of the arithmetic already */
1845 type_t *type = expression->right->base.datatype;
1846 ir_mode *mode = get_ir_mode(type);
1847 left = create_conv(dbgi, left, mode);
1851 switch (expression->expression.kind) {
1852 case EXPR_BINARY_DIV:
1853 case EXPR_BINARY_DIV_ASSIGN:
1854 if(mode_is_float(mode)) {
1855 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1856 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1858 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1859 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1863 case EXPR_BINARY_MOD:
1864 case EXPR_BINARY_MOD_ASSIGN:
1865 assert(!mode_is_float(mode));
1866 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1867 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1870 default: panic("unexpected binary expression type in create_divmod()");
1876 static ir_node *create_arithmetic_assign_divmod(
1877 const binary_expression_t *expression)
1879 ir_node * value = create_divmod(expression);
1880 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1881 type_t *const type = expression->expression.datatype;
1882 ir_mode *const mode = get_ir_mode(type);
1884 assert(type->kind != TYPE_POINTER);
1886 value = create_conv(dbgi, value, mode);
1887 set_value_for_expression(expression->left, value);
1892 static ir_node *create_arithmetic_assign_shift(
1893 const binary_expression_t *expression)
1895 ir_node * value = create_shift(expression);
1896 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1897 type_t *const type = expression->expression.datatype;
1898 ir_mode *const mode = get_ir_mode(type);
1900 value = create_conv(dbgi, value, mode);
1901 set_value_for_expression(expression->left, value);
1906 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1908 expression_kind_t kind = expression->expression.kind;
1911 case EXPR_BINARY_EQUAL:
1912 case EXPR_BINARY_NOTEQUAL:
1913 case EXPR_BINARY_LESS:
1914 case EXPR_BINARY_LESSEQUAL:
1915 case EXPR_BINARY_GREATER:
1916 case EXPR_BINARY_GREATEREQUAL:
1917 case EXPR_BINARY_ISGREATER:
1918 case EXPR_BINARY_ISGREATEREQUAL:
1919 case EXPR_BINARY_ISLESS:
1920 case EXPR_BINARY_ISLESSEQUAL:
1921 case EXPR_BINARY_ISLESSGREATER:
1922 case EXPR_BINARY_ISUNORDERED: {
1923 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1924 ir_node *left = expression_to_firm(expression->left);
1925 ir_node *right = expression_to_firm(expression->right);
1926 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1927 long pnc = get_pnc(kind);
1928 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1931 case EXPR_BINARY_ASSIGN: {
1932 ir_node *right = expression_to_firm(expression->right);
1933 set_value_for_expression(expression->left, right);
1937 case EXPR_BINARY_ADD:
1938 return create_add(expression);
1939 case EXPR_BINARY_SUB:
1940 return create_sub(expression);
1941 case EXPR_BINARY_MUL:
1942 return create_arithmetic_binop(expression, new_d_Mul);
1943 case EXPR_BINARY_BITWISE_AND:
1944 return create_arithmetic_binop(expression, new_d_And);
1945 case EXPR_BINARY_BITWISE_OR:
1946 return create_arithmetic_binop(expression, new_d_Or);
1947 case EXPR_BINARY_BITWISE_XOR:
1948 return create_arithmetic_binop(expression, new_d_Eor);
1949 case EXPR_BINARY_SHIFTLEFT:
1950 case EXPR_BINARY_SHIFTRIGHT:
1951 return create_shift(expression);
1952 case EXPR_BINARY_DIV:
1953 case EXPR_BINARY_MOD:
1954 return create_divmod(expression);
1955 case EXPR_BINARY_LOGICAL_AND:
1956 case EXPR_BINARY_LOGICAL_OR:
1957 return create_lazy_op(expression);
1958 case EXPR_BINARY_COMMA:
1959 expression_to_firm(expression->left);
1960 return expression_to_firm(expression->right);
1961 case EXPR_BINARY_ADD_ASSIGN:
1962 return create_arithmetic_assign_binop(expression, new_d_Add);
1963 case EXPR_BINARY_SUB_ASSIGN:
1964 return create_arithmetic_assign_binop(expression, new_d_Sub);
1965 case EXPR_BINARY_MUL_ASSIGN:
1966 return create_arithmetic_assign_binop(expression, new_d_Mul);
1967 case EXPR_BINARY_DIV_ASSIGN:
1968 return create_arithmetic_assign_divmod(expression);
1969 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1970 return create_arithmetic_assign_binop(expression, new_d_And);
1971 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1972 return create_arithmetic_assign_binop(expression, new_d_Or);
1973 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1974 return create_arithmetic_assign_binop(expression, new_d_Eor);
1975 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1976 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1977 return create_arithmetic_assign_shift(expression);
1978 case EXPR_BINARY_BUILTIN_EXPECT:
1979 return expression_to_firm(expression->left);
1981 panic("TODO binexpr type");
1985 static ir_node *array_access_addr(const array_access_expression_t *expression)
1987 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1988 ir_node *base_addr = expression_to_firm(expression->array_ref);
1989 ir_node *offset = expression_to_firm(expression->index);
1990 offset = create_conv(dbgi, offset, mode_uint);
1992 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1993 assert(is_type_pointer(ref_type));
1994 pointer_type_t *pointer_type = &ref_type->pointer;
1996 unsigned elem_size = get_type_size(pointer_type->points_to);
1997 ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
1998 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2000 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2005 static ir_node *array_access_to_firm(
2006 const array_access_expression_t *expression)
2008 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2009 ir_node *addr = array_access_addr(expression);
2010 type_t *type = revert_automatic_type_conversion(
2011 (const expression_t*) expression);
2012 type = skip_typeref(type);
2013 ir_type *irtype = get_ir_type(type);
2015 return deref_address(irtype, addr, dbgi);
2018 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
2020 type_t *type = expression->type;
2022 type = expression->size_expression->base.datatype;
2023 assert(type != NULL);
2026 ir_mode *const mode = get_ir_mode(expression->expression.datatype);
2027 symconst_symbol sym;
2028 sym.type_p = get_ir_type(type);
2029 return new_SymConst(mode, sym, symconst_type_size);
2032 static ir_node *alignof_to_firm(const alignof_expression_t *expression)
2034 type_t *const type = expression->type;
2035 ir_mode *const mode = get_ir_mode(expression->expression.datatype);
2036 symconst_symbol sym;
2037 sym.type_p = get_ir_type(type);
2038 return new_SymConst(mode, sym, symconst_type_align);
2041 static long fold_constant(const expression_t *expression)
2043 assert(is_constant_expression(expression));
2045 ir_graph *old_current_ir_graph = current_ir_graph;
2046 if(current_ir_graph == NULL) {
2047 current_ir_graph = get_const_code_irg();
2050 ir_node *cnst = expression_to_firm(expression);
2051 current_ir_graph = old_current_ir_graph;
2053 if(!is_Const(cnst)) {
2054 panic("couldn't fold constant\n");
2057 tarval *tv = get_Const_tarval(cnst);
2058 if(!tarval_is_long(tv)) {
2059 panic("result of constant folding is not integer\n");
2062 return get_tarval_long(tv);
2065 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2067 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
2069 /* first try to fold a constant condition */
2070 if(is_constant_expression(expression->condition)) {
2071 long val = fold_constant(expression->condition);
2073 return expression_to_firm(expression->true_expression);
2075 return expression_to_firm(expression->false_expression);
2079 ir_node *cur_block = get_cur_block();
2081 /* create the true block */
2082 ir_node *true_block = new_immBlock();
2084 ir_node *true_val = expression_to_firm(expression->true_expression);
2085 ir_node *true_jmp = new_Jmp();
2087 /* create the false block */
2088 ir_node *false_block = new_immBlock();
2090 ir_node *false_val = expression_to_firm(expression->false_expression);
2091 ir_node *false_jmp = new_Jmp();
2093 /* create the condition evaluation */
2094 set_cur_block(cur_block);
2095 create_condition_evaluation(expression->condition, true_block, false_block);
2096 mature_immBlock(true_block);
2097 mature_immBlock(false_block);
2099 /* create the common block */
2100 ir_node *common_block = new_immBlock();
2101 add_immBlock_pred(common_block, true_jmp);
2102 add_immBlock_pred(common_block, false_jmp);
2103 mature_immBlock(common_block);
2105 /* TODO improve static semantics, so either both or no values are NULL */
2106 if (true_val == NULL || false_val == NULL)
2109 ir_node *in[2] = { true_val, false_val };
2110 ir_mode *mode = get_irn_mode(true_val);
2111 assert(get_irn_mode(false_val) == mode);
2112 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2117 static ir_node *select_addr(const select_expression_t *expression)
2119 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2121 ir_node *compound_addr = expression_to_firm(expression->compound);
2123 declaration_t *entry = expression->compound_entry;
2124 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2125 ir_entity *entity = entry->v.entity;
2127 assert(entity != NULL);
2129 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2134 static ir_node *select_to_firm(const select_expression_t *expression)
2136 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2137 ir_node *addr = select_addr(expression);
2138 type_t *type = revert_automatic_type_conversion(
2139 (const expression_t*) expression);
2140 type = skip_typeref(type);
2141 ir_type *irtype = get_ir_type(type);
2143 return deref_address(irtype, addr, dbgi);
2146 /* Values returned by __builtin_classify_type. */
2147 typedef enum gcc_type_class
2153 enumeral_type_class,
2156 reference_type_class,
2160 function_type_class,
2171 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2173 const type_t *const type = expr->type_expression->base.datatype;
2179 const atomic_type_t *const atomic_type = &type->atomic;
2180 switch (atomic_type->atype) {
2181 /* should not be reached */
2182 case ATOMIC_TYPE_INVALID:
2186 /* gcc cannot do that */
2187 case ATOMIC_TYPE_VOID:
2188 tc = void_type_class;
2191 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2192 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2193 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2194 case ATOMIC_TYPE_SHORT:
2195 case ATOMIC_TYPE_USHORT:
2196 case ATOMIC_TYPE_INT:
2197 case ATOMIC_TYPE_UINT:
2198 case ATOMIC_TYPE_LONG:
2199 case ATOMIC_TYPE_ULONG:
2200 case ATOMIC_TYPE_LONGLONG:
2201 case ATOMIC_TYPE_ULONGLONG:
2202 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2203 tc = integer_type_class;
2206 case ATOMIC_TYPE_FLOAT:
2207 case ATOMIC_TYPE_DOUBLE:
2208 case ATOMIC_TYPE_LONG_DOUBLE:
2209 tc = real_type_class;
2212 #ifdef PROVIDE_COMPLEX
2213 case ATOMIC_TYPE_FLOAT_COMPLEX:
2214 case ATOMIC_TYPE_DOUBLE_COMPLEX:
2215 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
2216 tc = complex_type_class;
2218 case ATOMIC_TYPE_FLOAT_IMAGINARY:
2219 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
2220 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
2221 tc = complex_type_class;
2226 panic("Unimplemented case in classify_type_to_firm().");
2231 case TYPE_ARRAY: /* gcc handles this as pointer */
2232 case TYPE_FUNCTION: /* gcc handles this as pointer */
2233 case TYPE_POINTER: tc = pointer_type_class; break;
2234 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
2235 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
2237 /* gcc handles this as integer */
2238 case TYPE_ENUM: tc = integer_type_class; break;
2241 panic("Unimplemented case in classify_type_to_firm().");
2244 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
2245 ir_mode *const mode = mode_int;
2246 tarval *const tv = new_tarval_from_long(tc, mode);
2247 return new_d_Const(dbgi, mode, tv);
2250 static ir_node *function_name_to_firm(
2251 const string_literal_expression_t *const expr)
2253 if (current_function_name == NULL) {
2254 const source_position_t *const src_pos =
2255 &expr->expression.source_position;
2256 const char *const name = current_function_decl->symbol->string;
2257 current_function_name = string_to_firm(src_pos, "__func__", name);
2260 return current_function_name;
2263 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2265 statement_t *statement = expr->statement;
2267 assert(statement->kind == STATEMENT_COMPOUND);
2268 return compound_statement_to_firm(&statement->compound);
2271 static ir_node *va_start_expression_to_firm(
2272 const va_start_expression_t *const expr)
2274 ir_type *const method_type = get_ir_type(current_function_decl->type);
2275 int const n = get_method_n_params(method_type) - 1;
2276 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2277 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2278 dbg_info *const dbgi =
2279 get_dbg_info(&expr->expression.source_position);
2280 ir_node *const no_mem = new_NoMem();
2281 ir_node *const arg_sel =
2282 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2284 size_t const parm_size = get_type_size(expr->parameter->type);
2285 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2286 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2287 set_value_for_expression(expr->ap, add);
2292 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2294 ir_type *const irtype = get_ir_type(expr->expression.datatype);
2295 ir_node *const ap = expression_to_firm(expr->ap);
2296 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
2297 ir_node *const res = deref_address(irtype, ap, dbgi);
2299 size_t const parm_size = get_type_size(expr->expression.datatype);
2300 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2301 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2302 set_value_for_expression(expr->ap, add);
2307 static ir_node *dereference_addr(const unary_expression_t *const expression)
2309 assert(expression->expression.kind == EXPR_UNARY_DEREFERENCE);
2310 return expression_to_firm(expression->value);
2313 static ir_node *expression_to_addr(const expression_t *expression)
2315 switch(expression->kind) {
2316 case EXPR_REFERENCE:
2317 return reference_addr(&expression->reference);
2318 case EXPR_ARRAY_ACCESS:
2319 return array_access_addr(&expression->array_access);
2321 return select_addr(&expression->select);
2323 return call_expression_to_firm(&expression->call);
2324 case EXPR_UNARY_DEREFERENCE: {
2325 return dereference_addr(&expression->unary);
2330 panic("trying to get address of non-lvalue");
2333 static ir_node *builtin_constant_to_firm(const builtin_constant_expression_t *expression)
2335 ir_mode *mode = get_ir_mode(expression->expression.datatype);
2338 if (is_constant_expression(expression->value)) {
2343 return new_Const_long(mode, v);
2346 static ir_node *builtin_prefetch_to_firm(const builtin_prefetch_expression_t *expression)
2348 ir_node *adr = expression_to_firm(expression->adr);
2349 /* no Firm support for prefetch yet */
2354 static ir_node *_expression_to_firm(const expression_t *expression)
2356 switch(expression->kind) {
2358 return const_to_firm(&expression->conste);
2359 case EXPR_STRING_LITERAL:
2360 return string_literal_to_firm(&expression->string);
2361 case EXPR_WIDE_STRING_LITERAL:
2362 return wide_string_literal_to_firm(&expression->wide_string);
2363 case EXPR_REFERENCE:
2364 return reference_expression_to_firm(&expression->reference);
2366 return call_expression_to_firm(&expression->call);
2368 return unary_expression_to_firm(&expression->unary);
2370 return binary_expression_to_firm(&expression->binary);
2371 case EXPR_ARRAY_ACCESS:
2372 return array_access_to_firm(&expression->array_access);
2374 return sizeof_to_firm(&expression->sizeofe);
2376 return alignof_to_firm(&expression->alignofe);
2377 case EXPR_CONDITIONAL:
2378 return conditional_to_firm(&expression->conditional);
2380 return select_to_firm(&expression->select);
2381 case EXPR_CLASSIFY_TYPE:
2382 return classify_type_to_firm(&expression->classify_type);
2384 case EXPR_PRETTY_FUNCTION:
2385 return function_name_to_firm(&expression->string);
2386 case EXPR_STATEMENT:
2387 return statement_expression_to_firm(&expression->statement);
2389 return va_start_expression_to_firm(&expression->va_starte);
2391 return va_arg_expression_to_firm(&expression->va_arge);
2393 case EXPR_BUILTIN_SYMBOL:
2394 panic("unimplemented expression found");
2395 case EXPR_BUILTIN_CONSTANT_P:
2396 return builtin_constant_to_firm(&expression->builtin_constant);
2397 case EXPR_BUILTIN_PREFETCH:
2398 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2404 panic("invalid expression found");
2407 static ir_node *expression_to_firm(const expression_t *expression)
2409 ir_node *res = _expression_to_firm(expression);
2411 if(res != NULL && get_irn_mode(res) == mode_b) {
2412 ir_mode *mode = get_ir_mode(expression->base.datatype);
2413 res = create_conv(NULL, res, mode);
2419 static ir_node *expression_to_modeb(const expression_t *expression)
2421 ir_node *res = _expression_to_firm(expression);
2422 res = create_conv(NULL, res, mode_b);
2428 * create a short-circuit expression evaluation that tries to construct
2429 * efficient control flow structures for &&, || and ! expressions
2431 static void create_condition_evaluation(const expression_t *expression,
2432 ir_node *true_block,
2433 ir_node *false_block)
2435 switch(expression->kind) {
2436 case EXPR_UNARY_NOT: {
2437 const unary_expression_t *unary_expression = &expression->unary;
2438 create_condition_evaluation(unary_expression->value, false_block,
2442 case EXPR_BINARY_LOGICAL_AND: {
2443 const binary_expression_t *binary_expression = &expression->binary;
2445 ir_node *cur_block = get_cur_block();
2446 ir_node *extra_block = new_immBlock();
2447 set_cur_block(cur_block);
2448 create_condition_evaluation(binary_expression->left, extra_block,
2450 mature_immBlock(extra_block);
2451 set_cur_block(extra_block);
2452 create_condition_evaluation(binary_expression->right, true_block,
2456 case EXPR_BINARY_LOGICAL_OR: {
2457 const binary_expression_t *binary_expression = &expression->binary;
2459 ir_node *cur_block = get_cur_block();
2460 ir_node *extra_block = new_immBlock();
2461 set_cur_block(cur_block);
2462 create_condition_evaluation(binary_expression->left, true_block,
2464 mature_immBlock(extra_block);
2465 set_cur_block(extra_block);
2466 create_condition_evaluation(binary_expression->right, true_block,
2474 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2475 ir_node *condition = expression_to_modeb(expression);
2476 ir_node *cond = new_d_Cond(dbgi, condition);
2477 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2478 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2480 /* set branch prediction info based on __builtin_expect */
2481 if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
2482 long cnst = fold_constant(expression->binary.right);
2483 cond_jmp_predicate pred;
2486 pred = COND_JMP_PRED_FALSE;
2488 pred = COND_JMP_PRED_TRUE;
2490 set_Cond_jmp_pred(cond, pred);
2493 add_immBlock_pred(true_block, true_proj);
2494 add_immBlock_pred(false_block, false_proj);
2496 set_cur_block(NULL);
2501 static void create_declaration_entity(declaration_t *declaration,
2502 declaration_kind_t declaration_kind,
2503 ir_type *parent_type)
2505 ident *const id = new_id_from_str(declaration->symbol->string);
2506 ir_type *const irtype = get_ir_type(declaration->type);
2507 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2508 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2509 set_entity_ld_ident(entity, id);
2511 declaration->declaration_kind = (unsigned char) declaration_kind;
2512 declaration->v.entity = entity;
2513 set_entity_variability(entity, variability_uninitialized);
2514 if(parent_type == get_tls_type())
2515 set_entity_allocation(entity, allocation_automatic);
2516 else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
2517 set_entity_allocation(entity, allocation_static);
2518 /* TODO: visibility? */
2521 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2523 enum compound_graph_entry_type_t {
2524 COMPOUND_GRAPH_ENTRY_ARRAY,
2525 COMPOUND_GRAPH_ENTRY_COMPOUND
2528 struct compound_graph_path_entry_t {
2534 compound_graph_path_entry_t *prev;
2537 static void create_initializer_object(initializer_t *initializer, type_t *type,
2538 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2540 static compound_graph_path *create_compound_path(ir_type *type,
2541 compound_graph_path_entry_t *entry, int len)
2543 compound_graph_path *path = new_compound_graph_path(type, len);
2546 for( ; entry != NULL; entry = entry->prev, --i) {
2548 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2549 set_compound_graph_path_node(path, i, entry->v.entity);
2551 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2552 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2560 static void create_initializer_value(initializer_value_t *initializer,
2562 compound_graph_path_entry_t *entry,
2565 ir_node *node = expression_to_firm(initializer->value);
2566 ir_type *type = get_entity_type(entity);
2567 compound_graph_path *path = create_compound_path(type, entry, len);
2568 add_compound_ent_value_w_path(entity, node, path);
2571 static void create_initializer_compound(initializer_list_t *initializer,
2572 compound_type_t *type,
2574 compound_graph_path_entry_t *last_entry,
2577 declaration_t *compound_declaration = type->declaration;
2579 declaration_t *compound_entry = compound_declaration->context.declarations;
2581 compound_graph_path_entry_t entry;
2582 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2583 entry.prev = last_entry;
2587 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2588 if(compound_entry->symbol == NULL)
2590 if(compound_entry->namespc != NAMESPACE_NORMAL)
2593 if(i >= initializer->len)
2596 entry.v.entity = compound_entry->v.entity;
2598 initializer_t *sub_initializer = initializer->initializers[i];
2600 assert(compound_entry != NULL);
2601 assert(compound_entry->declaration_kind
2602 == DECLARATION_KIND_COMPOUND_MEMBER);
2604 if(sub_initializer->kind == INITIALIZER_VALUE) {
2605 create_initializer_value(&sub_initializer->value,
2606 entity, &entry, len);
2608 type_t *entry_type = skip_typeref(compound_entry->type);
2609 create_initializer_object(sub_initializer, entry_type, entity,
2617 static void create_initializer_array(initializer_list_t *initializer,
2618 array_type_t *type, ir_entity *entity,
2619 compound_graph_path_entry_t *last_entry,
2622 type_t *element_type = type->element_type;
2623 element_type = skip_typeref(element_type);
2625 compound_graph_path_entry_t entry;
2626 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2627 entry.prev = last_entry;
2631 for(i = 0; i < initializer->len; ++i) {
2632 entry.v.array_index = i;
2634 initializer_t *sub_initializer = initializer->initializers[i];
2636 if(sub_initializer->kind == INITIALIZER_VALUE) {
2637 create_initializer_value(&sub_initializer->value,
2638 entity, &entry, len);
2640 create_initializer_object(sub_initializer, element_type, entity,
2646 /* TODO: initialize rest... */
2647 if(type->size_expression != NULL) {
2648 size_t array_len = fold_constant(type->size_expression);
2649 for( ; i < array_len; ++i) {
2656 static void create_initializer_string(initializer_string_t *initializer,
2657 array_type_t *type, ir_entity *entity,
2658 compound_graph_path_entry_t *last_entry,
2661 type_t *element_type = type->element_type;
2662 element_type = skip_typeref(element_type);
2664 compound_graph_path_entry_t entry;
2665 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2666 entry.prev = last_entry;
2669 ir_type *irtype = get_entity_type(entity);
2670 size_t arr_len = get_array_type_size(type);
2671 const char *p = initializer->string;
2673 for(i = 0; i < arr_len; ++i, ++p) {
2674 entry.v.array_index = i;
2676 ir_node *node = new_Const_long(mode_Bs, *p);
2677 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2678 add_compound_ent_value_w_path(entity, node, path);
2685 static void create_initializer_wide_string(
2686 const initializer_wide_string_t *const initializer, array_type_t *const type,
2687 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2690 type_t *element_type = type->element_type;
2691 element_type = skip_typeref(element_type);
2693 compound_graph_path_entry_t entry;
2694 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2695 entry.prev = last_entry;
2698 ir_type *const irtype = get_entity_type(entity);
2699 const size_t arr_len = get_array_type_size(type);
2700 const wchar_rep_t * p = initializer->string.begin;
2701 const wchar_rep_t *const end = p + initializer->string.size;
2702 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2703 entry.v.array_index = i;
2705 ir_node *node = new_Const_long(mode_int, *p);
2706 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2707 add_compound_ent_value_w_path(entity, node, path);
2711 static void create_initializer_object(initializer_t *initializer, type_t *type,
2712 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2714 if(is_type_array(type)) {
2715 array_type_t *array_type = &type->array;
2717 switch (initializer->kind) {
2718 case INITIALIZER_STRING: {
2719 initializer_string_t *const string = &initializer->string;
2720 create_initializer_string(string, array_type, entity, entry, len);
2724 case INITIALIZER_WIDE_STRING: {
2725 initializer_wide_string_t *const string = &initializer->wide_string;
2726 create_initializer_wide_string(string, array_type, entity, entry, len);
2730 case INITIALIZER_LIST: {
2731 initializer_list_t *const list = &initializer->list;
2732 create_initializer_array(list, array_type, entity, entry, len);
2736 case INITIALIZER_VALUE:
2739 panic("Unhandled initializer");
2741 assert(initializer->kind == INITIALIZER_LIST);
2742 initializer_list_t *list = &initializer->list;
2744 assert(is_type_compound(type));
2745 compound_type_t *compound_type = &type->compound;
2746 create_initializer_compound(list, compound_type, entity, entry, len);
2750 static void create_initializer_local_variable_entity(declaration_t *declaration)
2752 initializer_t *initializer = declaration->init.initializer;
2753 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2754 ir_entity *entity = declaration->v.entity;
2755 ir_node *memory = get_store();
2756 ir_node *nomem = new_NoMem();
2757 ir_node *frame = get_irg_frame(current_ir_graph);
2758 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2760 if(initializer->kind == INITIALIZER_VALUE) {
2761 initializer_value_t *initializer_value = &initializer->value;
2763 ir_node *value = expression_to_firm(initializer_value->value);
2764 type_t *type = skip_typeref(declaration->type);
2765 assign_value(dbgi, addr, type, value);
2769 /* create a "template" entity which is copied to the entity on the stack */
2770 ident *const id = unique_ident("initializer");
2771 ir_type *const irtype = get_ir_type(declaration->type);
2772 ir_type *const global_type = get_glob_type();
2773 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
2774 set_entity_ld_ident(init_entity, id);
2776 set_entity_variability(init_entity, variability_initialized);
2777 set_entity_visibility(init_entity, visibility_local);
2778 set_entity_allocation(init_entity, allocation_static);
2780 ir_graph *const old_current_ir_graph = current_ir_graph;
2781 current_ir_graph = get_const_code_irg();
2783 type_t *const type = skip_typeref(declaration->type);
2784 create_initializer_object(initializer, type, init_entity, NULL, 0);
2786 assert(current_ir_graph == get_const_code_irg());
2787 current_ir_graph = old_current_ir_graph;
2789 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
2790 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2792 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2793 set_store(copyb_mem);
2796 static void create_initializer(declaration_t *declaration)
2798 initializer_t *initializer = declaration->init.initializer;
2799 if(initializer == NULL)
2802 declaration_kind_t declaration_kind
2803 = (declaration_kind_t) declaration->declaration_kind;
2804 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
2805 create_initializer_local_variable_entity(declaration);
2809 if(initializer->kind == INITIALIZER_VALUE) {
2810 initializer_value_t *initializer_value = &initializer->value;
2812 ir_node *value = expression_to_firm(initializer_value->value);
2814 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2815 set_value(declaration->v.value_number, value);
2817 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
2819 ir_entity *entity = declaration->v.entity;
2821 set_entity_variability(entity, variability_initialized);
2822 set_atomic_ent_value(entity, value);
2825 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
2826 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
2828 ir_entity *entity = declaration->v.entity;
2829 set_entity_variability(entity, variability_initialized);
2831 type_t *type = skip_typeref(declaration->type);
2832 create_initializer_object(initializer, type, entity, NULL, 0);
2836 static void create_local_variable(declaration_t *declaration)
2838 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
2840 bool needs_entity = declaration->address_taken;
2841 type_t *type = skip_typeref(declaration->type);
2843 if(is_type_array(type) || is_type_compound(type)) {
2844 needs_entity = true;
2848 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2849 create_declaration_entity(declaration,
2850 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
2853 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
2854 declaration->v.value_number = next_value_number_function;
2855 ++next_value_number_function;
2858 create_initializer(declaration);
2861 static void create_local_static_variable(declaration_t *declaration)
2863 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
2865 type_t *const type = skip_typeref(declaration->type);
2866 ir_type *const global_type = get_glob_type();
2867 ident *const id = unique_ident(declaration->symbol->string);
2868 ir_type *const irtype = get_ir_type(type);
2869 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2870 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
2871 set_entity_ld_ident(entity, id);
2873 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
2874 declaration->v.entity = entity;
2875 set_entity_variability(entity, variability_uninitialized);
2876 set_entity_visibility(entity, visibility_local);
2877 set_entity_allocation(entity, allocation_static);
2879 ir_graph *const old_current_ir_graph = current_ir_graph;
2880 current_ir_graph = get_const_code_irg();
2882 create_initializer(declaration);
2884 assert(current_ir_graph == get_const_code_irg());
2885 current_ir_graph = old_current_ir_graph;
2890 static void return_statement_to_firm(return_statement_t *statement)
2892 if(get_cur_block() == NULL)
2895 ir_type *func_irtype = get_ir_type(current_function_decl->type);
2897 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2901 if(get_method_n_ress(func_irtype) > 0) {
2902 ir_type *res_type = get_method_res_type(func_irtype, 0);
2904 if(statement->return_value != NULL) {
2905 ir_node *node = expression_to_firm(statement->return_value);
2906 node = do_strict_conv(dbgi, node);
2910 if(is_compound_type(res_type)) {
2913 mode = get_type_mode(res_type);
2915 in[0] = new_Unknown(mode);
2919 /* build return_value for its side effects */
2920 if(statement->return_value != NULL) {
2921 expression_to_firm(statement->return_value);
2926 ir_node *store = get_store();
2927 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
2929 ir_node *end_block = get_irg_end_block(current_ir_graph);
2930 add_immBlock_pred(end_block, ret);
2932 set_cur_block(NULL);
2935 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
2937 if(get_cur_block() == NULL)
2940 return expression_to_firm(statement->expression);
2943 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
2945 ir_node *result = NULL;
2946 statement_t *statement = compound->statements;
2947 for( ; statement != NULL; statement = statement->base.next) {
2948 //context2firm(&statement->context);
2950 if(statement->base.next == NULL
2951 && statement->kind == STATEMENT_EXPRESSION) {
2952 result = expression_statement_to_firm(
2953 &statement->expression);
2956 statement_to_firm(statement);
2962 static void create_local_declaration(declaration_t *declaration)
2964 type_t *type = skip_typeref(declaration->type);
2966 switch ((storage_class_tag_t) declaration->storage_class) {
2967 case STORAGE_CLASS_STATIC:
2968 create_local_static_variable(declaration);
2970 case STORAGE_CLASS_ENUM_ENTRY:
2971 panic("enum entry declaration in local block found");
2972 case STORAGE_CLASS_EXTERN:
2973 panic("extern declaration in local block found");
2974 case STORAGE_CLASS_NONE:
2975 case STORAGE_CLASS_AUTO:
2976 case STORAGE_CLASS_REGISTER:
2977 if(is_type_function(type)) {
2978 panic("nested functions not supported yet");
2980 create_local_variable(declaration);
2983 case STORAGE_CLASS_TYPEDEF:
2984 case STORAGE_CLASS_THREAD:
2985 case STORAGE_CLASS_THREAD_EXTERN:
2986 case STORAGE_CLASS_THREAD_STATIC:
2989 panic("invalid storage class found");
2992 static void declaration_statement_to_firm(declaration_statement_t *statement)
2994 declaration_t *declaration = statement->declarations_begin;
2995 declaration_t *end = statement->declarations_end->next;
2996 for( ; declaration != end; declaration = declaration->next) {
2997 create_local_variable(declaration);
3001 static void if_statement_to_firm(if_statement_t *statement)
3003 ir_node *cur_block = get_cur_block();
3005 ir_node *fallthrough_block = new_immBlock();
3007 /* the true (blocks) */
3008 ir_node *true_block;
3009 if (statement->true_statement != NULL) {
3010 true_block = new_immBlock();
3011 statement_to_firm(statement->true_statement);
3012 if(get_cur_block() != NULL) {
3013 ir_node *jmp = new_Jmp();
3014 add_immBlock_pred(fallthrough_block, jmp);
3017 true_block = fallthrough_block;
3020 /* the false (blocks) */
3021 ir_node *false_block;
3022 if(statement->false_statement != NULL) {
3023 false_block = new_immBlock();
3025 statement_to_firm(statement->false_statement);
3026 if(get_cur_block() != NULL) {
3027 ir_node *jmp = new_Jmp();
3028 add_immBlock_pred(fallthrough_block, jmp);
3031 false_block = fallthrough_block;
3034 /* create the condition */
3035 if(cur_block != NULL) {
3036 set_cur_block(cur_block);
3037 create_condition_evaluation(statement->condition, true_block,
3041 mature_immBlock(true_block);
3042 if(false_block != fallthrough_block) {
3043 mature_immBlock(false_block);
3045 mature_immBlock(fallthrough_block);
3047 set_cur_block(fallthrough_block);
3050 static void while_statement_to_firm(while_statement_t *statement)
3052 ir_node *jmp = NULL;
3053 if(get_cur_block() != NULL) {
3057 /* create the header block */
3058 ir_node *header_block = new_immBlock();
3060 add_immBlock_pred(header_block, jmp);
3063 /* the false block */
3064 ir_node *false_block = new_immBlock();
3067 ir_node *body_block;
3068 if (statement->body != NULL) {
3069 ir_node *old_continue_label = continue_label;
3070 ir_node *old_break_label = break_label;
3071 continue_label = header_block;
3072 break_label = false_block;
3074 body_block = new_immBlock();
3075 statement_to_firm(statement->body);
3077 assert(continue_label == header_block);
3078 assert(break_label == false_block);
3079 continue_label = old_continue_label;
3080 break_label = old_break_label;
3082 if(get_cur_block() != NULL) {
3084 add_immBlock_pred(header_block, jmp);
3087 body_block = header_block;
3090 /* create the condition */
3091 set_cur_block(header_block);
3093 create_condition_evaluation(statement->condition, body_block, false_block);
3094 mature_immBlock(body_block);
3095 mature_immBlock(false_block);
3096 mature_immBlock(header_block);
3098 set_cur_block(false_block);
3101 static void do_while_statement_to_firm(do_while_statement_t *statement)
3103 ir_node *jmp = NULL;
3104 if(get_cur_block() != NULL) {
3108 /* create the header block */
3109 ir_node *header_block = new_immBlock();
3111 /* the false block */
3112 ir_node *false_block = new_immBlock();
3115 ir_node *body_block = new_immBlock();
3117 add_immBlock_pred(body_block, jmp);
3120 if (statement->body != NULL) {
3121 ir_node *old_continue_label = continue_label;
3122 ir_node *old_break_label = break_label;
3123 continue_label = header_block;
3124 break_label = false_block;
3126 statement_to_firm(statement->body);
3128 assert(continue_label == header_block);
3129 assert(break_label == false_block);
3130 continue_label = old_continue_label;
3131 break_label = old_break_label;
3133 if (get_cur_block() == NULL) {
3134 mature_immBlock(header_block);
3135 mature_immBlock(body_block);
3136 mature_immBlock(false_block);
3141 ir_node *body_jmp = new_Jmp();
3142 add_immBlock_pred(header_block, body_jmp);
3143 mature_immBlock(header_block);
3145 /* create the condition */
3146 set_cur_block(header_block);
3148 create_condition_evaluation(statement->condition, body_block, false_block);
3149 mature_immBlock(body_block);
3150 mature_immBlock(false_block);
3151 mature_immBlock(header_block);
3153 set_cur_block(false_block);
3156 static void for_statement_to_firm(for_statement_t *statement)
3158 ir_node *jmp = NULL;
3159 if (get_cur_block() != NULL) {
3160 if(statement->initialisation != NULL) {
3161 expression_to_firm(statement->initialisation);
3164 /* create declarations */
3165 declaration_t *declaration = statement->context.declarations;
3166 for( ; declaration != NULL; declaration = declaration->next) {
3167 create_local_declaration(declaration);
3174 /* create the step block */
3175 ir_node *const step_block = new_immBlock();
3176 if (statement->step != NULL) {
3177 expression_to_firm(statement->step);
3179 ir_node *const step_jmp = new_Jmp();
3181 /* create the header block */
3182 ir_node *const header_block = new_immBlock();
3184 add_immBlock_pred(header_block, jmp);
3186 add_immBlock_pred(header_block, step_jmp);
3188 /* the false block */
3189 ir_node *const false_block = new_immBlock();
3192 ir_node * body_block;
3193 if (statement->body != NULL) {
3194 ir_node *const old_continue_label = continue_label;
3195 ir_node *const old_break_label = break_label;
3196 continue_label = step_block;
3197 break_label = false_block;
3199 body_block = new_immBlock();
3200 statement_to_firm(statement->body);
3202 assert(continue_label == step_block);
3203 assert(break_label == false_block);
3204 continue_label = old_continue_label;
3205 break_label = old_break_label;
3207 if (get_cur_block() != NULL) {
3209 add_immBlock_pred(step_block, jmp);
3212 body_block = step_block;
3215 /* create the condition */
3216 set_cur_block(header_block);
3217 if (statement->condition != NULL) {
3218 create_condition_evaluation(statement->condition, body_block,
3221 keep_alive(header_block);
3223 add_immBlock_pred(body_block, jmp);
3226 mature_immBlock(body_block);
3227 mature_immBlock(false_block);
3228 mature_immBlock(step_block);
3229 mature_immBlock(header_block);
3230 mature_immBlock(false_block);
3232 set_cur_block(false_block);
3235 static void create_jump_statement(const statement_t *statement,
3236 ir_node *target_block)
3238 if(get_cur_block() == NULL)
3241 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3242 ir_node *jump = new_d_Jmp(dbgi);
3243 add_immBlock_pred(target_block, jump);
3245 set_cur_block(NULL);
3248 static void switch_statement_to_firm(const switch_statement_t *statement)
3250 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
3252 ir_node *expression = expression_to_firm(statement->expression);
3253 ir_node *cond = new_d_Cond(dbgi, expression);
3254 ir_node *break_block = new_immBlock();
3256 set_cur_block(NULL);
3258 ir_node *const old_switch_cond = current_switch_cond;
3259 ir_node *const old_break_label = break_label;
3260 const bool old_saw_default_label = saw_default_label;
3261 current_switch_cond = cond;
3262 break_label = break_block;
3264 statement_to_firm(statement->body);
3266 if(get_cur_block() != NULL) {
3267 ir_node *jmp = new_Jmp();
3268 add_immBlock_pred(break_block, jmp);
3271 if (!saw_default_label) {
3272 set_cur_block(get_nodes_block(cond));
3273 ir_node *const proj = new_d_defaultProj(dbgi, cond,
3274 MAGIC_DEFAULT_PN_NUMBER);
3275 add_immBlock_pred(break_block, proj);
3278 assert(current_switch_cond == cond);
3279 assert(break_label == break_block);
3280 current_switch_cond = old_switch_cond;
3281 break_label = old_break_label;
3282 saw_default_label = old_saw_default_label;
3284 mature_immBlock(break_block);
3285 set_cur_block(break_block);
3288 static void case_label_to_firm(const case_label_statement_t *statement)
3290 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
3292 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
3294 /* let's create a node and hope firm constant folding creates a Const
3297 set_cur_block(get_nodes_block(current_switch_cond));
3298 if(statement->expression) {
3299 long pn = fold_constant(statement->expression);
3300 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
3301 /* oops someone detected our cheating... */
3302 panic("magic default pn used");
3304 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
3306 saw_default_label = true;
3307 proj = new_d_defaultProj(dbgi, current_switch_cond,
3308 MAGIC_DEFAULT_PN_NUMBER);
3311 ir_node *block = new_immBlock();
3312 if (fallthrough != NULL) {
3313 add_immBlock_pred(block, fallthrough);
3315 add_immBlock_pred(block, proj);
3316 mature_immBlock(block);
3318 if(statement->label_statement != NULL) {
3319 statement_to_firm(statement->label_statement);
3323 static ir_node *get_label_block(declaration_t *label)
3325 assert(label->namespc == NAMESPACE_LABEL);
3327 if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
3328 return label->v.block;
3330 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
3332 ir_node *old_cur_block = get_cur_block();
3333 ir_node *block = new_immBlock();
3334 set_cur_block(old_cur_block);
3336 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
3337 label->v.block = block;
3339 ARR_APP1(ir_node *, imature_blocks, block);
3344 static void label_to_firm(const label_statement_t *statement)
3346 ir_node *block = get_label_block(statement->label);
3348 if(get_cur_block() != NULL) {
3349 ir_node *jmp = new_Jmp();
3350 add_immBlock_pred(block, jmp);
3353 set_cur_block(block);
3356 if(statement->label_statement != NULL) {
3357 statement_to_firm(statement->label_statement);
3361 static void goto_to_firm(const goto_statement_t *statement)
3363 if(get_cur_block() == NULL)
3366 ir_node *block = get_label_block(statement->label);
3367 ir_node *jmp = new_Jmp();
3368 add_immBlock_pred(block, jmp);
3370 set_cur_block(NULL);
3373 typedef enum modifier_t {
3374 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
3375 ASM_MODIFIER_READ_WRITE = 1 << 1,
3376 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
3377 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
3381 static void asm_statement_to_firm(const asm_statement_t *statement)
3383 bool needs_memory = false;
3385 size_t n_clobbers = 0;
3386 asm_clobber_t *clobber = statement->clobbers;
3387 for( ; clobber != NULL; clobber = clobber->next) {
3388 if(strcmp(clobber->clobber, "memory") == 0) {
3389 needs_memory = true;
3393 ident *id = new_id_from_str(clobber->clobber);
3394 obstack_ptr_grow(&asm_obst, id);
3397 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
3398 ident **clobbers = NULL;
3399 if(n_clobbers > 0) {
3400 clobbers = obstack_finish(&asm_obst);
3403 /* find and count input and output constraints */
3404 asm_constraint_t *constraint = statement->inputs;
3405 for( ; constraint != NULL; constraint = constraint->next) {
3407 bool supports_memop = false;
3408 for(const char *c = constraint->constraints; *c != 0; ++c) {
3409 /* TODO: improve error messages */
3413 panic("multiple alternative assembler constraints not "
3421 supports_memop = true;
3422 obstack_1grow(&asm_obst, *c);
3425 if(modifiers & ASM_MODIFIER_READ_WRITE)
3426 panic("inconsistent register constraints");
3427 modifiers |= ASM_MODIFIER_WRITE_ONLY;
3430 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
3431 panic("inconsistent register constraints");
3432 modifiers |= ASM_MODIFIER_READ_WRITE;
3435 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
3436 panic("early clobber assembler constraint not supported yet");
3439 modifiers |= ASM_MODIFIER_COMMUTATIVE;
3440 panic("commutative assembler constraint not supported yet");
3443 /* skip register preferences stuff... */
3444 while(*c != 0 && *c != ',')
3448 /* skip register preferences stuff... */
3452 obstack_1grow(&asm_obst, *c);
3456 obstack_1grow(&asm_obst, '\0');
3457 const char *constraint_string = obstack_finish(&asm_obst);
3459 needs_memory |= supports_memop;
3460 if(supports_memop) {
3468 static void statement_to_firm(statement_t *statement)
3470 switch(statement->kind) {
3471 case STATEMENT_INVALID:
3472 panic("invalid statement found");
3473 case STATEMENT_COMPOUND:
3474 compound_statement_to_firm(&statement->compound);
3476 case STATEMENT_RETURN:
3477 return_statement_to_firm(&statement->returns);
3479 case STATEMENT_EXPRESSION:
3480 expression_statement_to_firm(&statement->expression);
3483 if_statement_to_firm(&statement->ifs);
3485 case STATEMENT_WHILE:
3486 while_statement_to_firm(&statement->whiles);
3488 case STATEMENT_DO_WHILE:
3489 do_while_statement_to_firm(&statement->do_while);
3491 case STATEMENT_DECLARATION:
3492 declaration_statement_to_firm(&statement->declaration);
3494 case STATEMENT_BREAK:
3495 create_jump_statement(statement, break_label);
3497 case STATEMENT_CONTINUE:
3498 create_jump_statement(statement, continue_label);
3500 case STATEMENT_SWITCH:
3501 switch_statement_to_firm(&statement->switchs);
3503 case STATEMENT_CASE_LABEL:
3504 case_label_to_firm(&statement->case_label);
3507 for_statement_to_firm(&statement->fors);
3509 case STATEMENT_LABEL:
3510 label_to_firm(&statement->label);
3512 case STATEMENT_GOTO:
3513 goto_to_firm(&statement->gotos);
3516 //asm_statement_to_firm(&statement->asms);
3520 panic("Statement not implemented\n");
3523 static int count_decls_in_expression(const expression_t *expression);
3525 static int count_local_declarations(const declaration_t * decl,
3526 const declaration_t *const end)
3529 for (; decl != end; decl = decl->next) {
3530 const type_t *type = skip_typeref(decl->type);
3531 switch (type->kind) {
3535 if (!decl->address_taken)
3541 const initializer_t *initializer = decl->init.initializer;
3542 /* FIXME: should walk initializer hierarchies... */
3543 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
3544 count += count_decls_in_expression(initializer->value.value);
3550 static int count_decls_in_expression(const expression_t *expression) {
3551 if(expression == NULL)
3554 switch(expression->base.kind) {
3555 case EXPR_STATEMENT:
3556 return count_decls_in_stmts(expression->statement.statement);
3558 int count_left = count_decls_in_expression(expression->binary.left);
3559 int count_right = count_decls_in_expression(expression->binary.right);
3560 return count_left + count_right;
3563 return count_decls_in_expression(expression->unary.value);
3569 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
3570 * (or implement all the missing expressions here/implement a walker)
3576 static int count_decls_in_stmts(const statement_t *stmt)
3579 for (; stmt != NULL; stmt = stmt->base.next) {
3580 switch (stmt->kind) {
3581 case STATEMENT_DECLARATION: {
3582 const declaration_statement_t *const decl_stmt = &stmt->declaration;
3583 count += count_local_declarations(decl_stmt->declarations_begin,
3584 decl_stmt->declarations_end->next);
3588 case STATEMENT_COMPOUND: {
3589 const compound_statement_t *const comp =
3591 count += count_decls_in_stmts(comp->statements);
3595 case STATEMENT_IF: {
3596 const if_statement_t *const if_stmt = &stmt->ifs;
3597 count += count_decls_in_expression(if_stmt->condition);
3598 count += count_decls_in_stmts(if_stmt->true_statement);
3599 count += count_decls_in_stmts(if_stmt->false_statement);
3603 case STATEMENT_SWITCH: {
3604 const switch_statement_t *const switch_stmt = &stmt->switchs;
3605 count += count_decls_in_expression(switch_stmt->expression);
3606 count += count_decls_in_stmts(switch_stmt->body);
3610 case STATEMENT_LABEL: {
3611 const label_statement_t *const label_stmt = &stmt->label;
3612 count += count_decls_in_stmts(label_stmt->label_statement);
3616 case STATEMENT_WHILE: {
3617 const while_statement_t *const while_stmt = &stmt->whiles;
3618 count += count_decls_in_expression(while_stmt->condition);
3619 count += count_decls_in_stmts(while_stmt->body);
3623 case STATEMENT_DO_WHILE: {
3624 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
3625 count += count_decls_in_expression(do_while_stmt->condition);
3626 count += count_decls_in_stmts(do_while_stmt->body);
3630 case STATEMENT_FOR: {
3631 const for_statement_t *const for_stmt = &stmt->fors;
3632 count += count_local_declarations(for_stmt->context.declarations, NULL);
3633 count += count_decls_in_expression(for_stmt->initialisation);
3634 count += count_decls_in_expression(for_stmt->condition);
3635 count += count_decls_in_expression(for_stmt->step);
3636 count += count_decls_in_stmts(for_stmt->body);
3640 case STATEMENT_CASE_LABEL: {
3641 const case_label_statement_t *label = &stmt->case_label;
3642 count += count_decls_in_expression(label->expression);
3643 count += count_decls_in_stmts(label->label_statement);
3648 case STATEMENT_BREAK:
3649 case STATEMENT_CONTINUE:
3652 case STATEMENT_EXPRESSION: {
3653 const expression_statement_t *expr_stmt = &stmt->expression;
3654 count += count_decls_in_expression(expr_stmt->expression);
3658 case STATEMENT_GOTO:
3659 case STATEMENT_INVALID:
3662 case STATEMENT_RETURN: {
3663 const return_statement_t *ret_stmt = &stmt->returns;
3664 count += count_decls_in_expression(ret_stmt->return_value);
3672 static int get_function_n_local_vars(declaration_t *declaration)
3676 /* count parameters */
3677 count += count_local_declarations(declaration->context.declarations, NULL);
3679 /* count local variables declared in body */
3680 count += count_decls_in_stmts(declaration->init.statement);
3685 static void initialize_function_parameters(declaration_t *declaration)
3687 ir_graph *irg = current_ir_graph;
3688 ir_node *args = get_irg_args(irg);
3689 ir_node *start_block = get_irg_start_block(irg);
3690 ir_type *function_irtype = get_ir_type(declaration->type);
3693 declaration_t *parameter = declaration->context.declarations;
3694 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3695 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
3696 type_t *type = skip_typeref(parameter->type);
3698 bool needs_entity = parameter->address_taken;
3699 assert(!is_type_array(type));
3700 if(is_type_compound(type)) {
3701 needs_entity = true;
3705 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3706 ident *id = new_id_from_str(parameter->symbol->string);
3707 set_entity_ident(entity, id);
3709 parameter->declaration_kind
3710 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
3711 parameter->v.entity = entity;
3715 ir_mode *mode = get_ir_mode(parameter->type);
3717 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3719 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3720 parameter->v.value_number = next_value_number_function;
3721 ++next_value_number_function;
3723 set_value(parameter->v.value_number, proj);
3728 * Handle additional decl modifiers for IR-graphs
3730 * @param irg the IR-graph
3731 * @param dec_modifiers additional modifiers
3733 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
3735 if (decl_modifiers & DM_NORETURN) {
3736 /* TRUE if the declaration includes the Microsoft
3737 __declspec(noreturn) specifier. */
3738 set_irg_additional_property(irg, mtp_property_noreturn);
3740 if (decl_modifiers & DM_NOTHROW) {
3741 /* TRUE if the declaration includes the Microsoft
3742 __declspec(nothrow) specifier. */
3743 set_irg_additional_property(irg, mtp_property_nothrow);
3745 if (decl_modifiers & DM_NAKED) {
3746 /* TRUE if the declaration includes the Microsoft
3747 __declspec(naked) specifier. */
3748 set_irg_additional_property(irg, mtp_property_naked);
3750 if (decl_modifiers & DM_FORCEINLINE) {
3751 /* TRUE if the declaration includes the
3752 Microsoft __forceinline specifier. */
3753 set_irg_inline_property(irg, irg_inline_forced);
3755 if (decl_modifiers & DM_NOINLINE) {
3756 /* TRUE if the declaration includes the Microsoft
3757 __declspec(noinline) specifier. */
3758 set_irg_inline_property(irg, irg_inline_forbidden);
3762 static void create_function(declaration_t *declaration)
3764 ir_entity *function_entity = get_function_entity(declaration);
3766 if(declaration->init.statement == NULL)
3769 current_function_decl = declaration;
3770 current_function_name = NULL;
3772 assert(imature_blocks == NULL);
3773 imature_blocks = NEW_ARR_F(ir_node*, 0);
3775 int n_local_vars = get_function_n_local_vars(declaration);
3776 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3777 ir_node *first_block = get_cur_block();
3779 /* set inline flags */
3780 if (declaration->is_inline)
3781 set_irg_inline_property(irg, irg_inline_recomended);
3782 handle_decl_modifier_irg(irg, declaration->modifiers);
3784 next_value_number_function = 0;
3785 initialize_function_parameters(declaration);
3787 statement_to_firm(declaration->init.statement);
3789 ir_node *end_block = get_irg_end_block(irg);
3791 /* do we have a return statement yet? */
3792 if(get_cur_block() != NULL) {
3793 type_t *type = skip_typeref(declaration->type);
3794 assert(is_type_function(type));
3795 const function_type_t *func_type = &type->function;
3796 const type_t *return_type
3797 = skip_typeref(func_type->return_type);
3800 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3801 ret = new_Return(get_store(), 0, NULL);
3804 if(is_type_scalar(return_type)) {
3805 mode = get_ir_mode(func_type->return_type);
3811 /* ยง5.1.2.2.3 main implicitly returns 0 */
3812 if (strcmp(declaration->symbol->string, "main") == 0) {
3813 in[0] = new_Const(mode, get_mode_null(mode));
3815 in[0] = new_Unknown(mode);
3817 ret = new_Return(get_store(), 1, in);
3819 add_immBlock_pred(end_block, ret);
3822 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3823 mature_immBlock(imature_blocks[i]);
3825 DEL_ARR_F(imature_blocks);
3826 imature_blocks = NULL;
3828 mature_immBlock(first_block);
3829 mature_immBlock(end_block);
3831 irg_finalize_cons(irg);
3833 /* finalize the frame type */
3834 ir_type *frame_type = get_irg_frame_type(irg);
3835 int n = get_compound_n_members(frame_type);
3838 for(int i = 0; i < n; ++i) {
3839 ir_entity *entity = get_compound_member(frame_type, i);
3840 ir_type *entity_type = get_entity_type(entity);
3842 int align = get_type_alignment_bytes(entity_type);
3843 if(align > align_all)
3847 misalign = offset % align;
3849 offset += align - misalign;
3853 set_entity_offset(entity, offset);
3854 offset += get_type_size_bytes(entity_type);
3856 set_type_size_bytes(frame_type, offset);
3857 set_type_alignment_bytes(frame_type, align_all);
3858 set_type_state(frame_type, layout_fixed);
3863 static void create_global_variable(declaration_t *declaration)
3867 switch ((storage_class_tag_t)declaration->storage_class) {
3868 case STORAGE_CLASS_STATIC:
3869 vis = visibility_local;
3872 case STORAGE_CLASS_EXTERN:
3873 vis = visibility_external_allocated;
3876 case STORAGE_CLASS_NONE:
3877 vis = visibility_external_visible;
3880 case STORAGE_CLASS_THREAD:
3881 vis = visibility_external_visible;
3884 case STORAGE_CLASS_THREAD_EXTERN:
3885 vis = visibility_external_allocated;
3888 case STORAGE_CLASS_THREAD_STATIC:
3889 vis = visibility_local;
3893 var_type = get_tls_type();
3897 var_type = get_glob_type();
3901 create_declaration_entity(declaration,
3902 DECLARATION_KIND_GLOBAL_VARIABLE,
3904 set_entity_visibility(declaration->v.entity, vis);
3906 current_ir_graph = get_const_code_irg();
3907 create_initializer(declaration);
3910 case STORAGE_CLASS_TYPEDEF:
3911 case STORAGE_CLASS_AUTO:
3912 case STORAGE_CLASS_REGISTER:
3913 case STORAGE_CLASS_ENUM_ENTRY:
3916 panic("Invalid storage class for global variable");
3919 static void context_to_firm(context_t *context)
3921 /* first pass: create declarations */
3922 declaration_t *declaration = context->declarations;
3923 for( ; declaration != NULL; declaration = declaration->next) {
3924 if(declaration->namespc != NAMESPACE_NORMAL)
3926 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3927 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3929 if(declaration->symbol == NULL)
3932 type_t *type = skip_typeref(declaration->type);
3933 if(is_type_function(type)) {
3934 get_function_entity(declaration);
3936 create_global_variable(declaration);
3940 /* second pass: create code */
3941 declaration = context->declarations;
3942 for( ; declaration != NULL; declaration = declaration->next) {
3943 if(declaration->namespc != NAMESPACE_NORMAL)
3945 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3946 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3948 if(declaration->symbol == NULL)
3951 type_t *type = declaration->type;
3952 if(type->kind != TYPE_FUNCTION)
3955 create_function(declaration);
3959 void init_ast2firm(void)
3961 obstack_init(&asm_obst);
3962 init_atomic_modes();
3964 /* create idents for all known runtime functions */
3965 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
3966 predef_idents[rts_data[i].id] = new_id_from_str(rts_data[i].name);
3970 void exit_ast2firm(void)
3972 obstack_free(&asm_obst, NULL);
3975 void translation_unit_to_firm(translation_unit_t *unit)
3977 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3978 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3979 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3981 ir_type_int = get_ir_type(type_int);
3982 ir_type_const_char = get_ir_type(type_const_char);
3983 ir_type_wchar_t = get_ir_type(type_wchar_t);
3984 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3987 type_void->base.firm_type = ir_type_void;
3989 /* just to be sure */
3990 continue_label = NULL;
3992 current_switch_cond = NULL;
3994 context_to_firm(&unit->context);