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 printf("Offs: %u OffsB: %u Misa: %u RestS: %u Size: %u\n",
588 offset, bit_offset, misalign, rest_size_bits, size_bits);
590 if(size_bits > rest_size_bits) {
591 /* start a new bucket */
592 offset += entry_alignment - misalign;
598 /* put into current bucket */
599 base = offset - misalign;
600 bits_remainder = misalign * 8 + bit_offset;
603 offset += size_bits / 8;
604 bit_offset = bit_offset + (size_bits % 8);
606 size_t entry_size = get_type_size_bytes(base_irtype);
607 if(misalign > 0 || bit_offset > 0)
608 offset += entry_alignment - misalign;
610 printf("Alignment: %u Size: %u\n", entry_alignment, entry_size);
613 offset += entry_size;
617 if(entry_alignment > align_all) {
618 if(entry_alignment % align_all != 0) {
619 panic("uneven alignments not supported yet");
621 align_all = entry_alignment;
625 printf("%s.%s %u:%u (O: %u BP: %u)\n",
626 type->declaration->symbol ? type->declaration->symbol->string : "",
627 entry->symbol->string, base, bits_remainder,
629 set_entity_offset(entity, base);
630 set_entity_offset_bits_remainder(entity,
631 (unsigned char) bits_remainder);
632 add_struct_member(irtype, entity);
633 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
634 entry->v.entity = entity;
638 size_t misalign = offset % align_all;
640 offset += align_all - misalign;
642 set_type_alignment_bytes(irtype, align_all);
643 set_type_size_bytes(irtype, offset);
644 set_type_state(irtype, layout_fixed);
649 static ir_type *create_union_type(compound_type_t *type)
651 declaration_t *declaration = type->declaration;
652 symbol_t *symbol = declaration->symbol;
655 id = unique_ident(symbol->string);
657 id = unique_ident("__anonymous_union");
659 ir_type *irtype = new_type_union(id);
661 type->type.firm_type = irtype;
665 declaration_t *entry = declaration->context.declarations;
666 for( ; entry != NULL; entry = entry->next) {
667 if(entry->namespc != NAMESPACE_NORMAL)
670 ident *ident = new_id_from_str(entry->symbol->string);
671 ir_type *entry_ir_type = get_ir_type(entry->type);
673 int entry_size = get_type_size_bytes(entry_ir_type);
674 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
676 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
677 ir_entity *const entity = new_d_entity(irtype, ident, entry_ir_type,
679 add_union_member(irtype, entity);
680 set_entity_offset(entity, 0);
681 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
682 entry->v.entity = entity;
684 if(entry_size > size) {
687 if(entry_alignment > align_all) {
688 if(entry_alignment % align_all != 0) {
689 panic("Uneven alignments not supported yet");
691 align_all = entry_alignment;
695 set_type_alignment_bytes(irtype, align_all);
696 set_type_size_bytes(irtype, size);
697 set_type_state(irtype, layout_fixed);
702 static ir_node *expression_to_firm(const expression_t *expression);
703 static inline ir_mode *get_ir_mode(type_t *type);
705 static ir_type *create_enum_type(enum_type_t *const type)
707 type->type.firm_type = ir_type_int;
709 ir_mode *const mode = get_ir_mode((type_t*) type);
710 tarval *const one = get_mode_one(mode);
711 tarval * tv_next = get_tarval_null(mode);
713 declaration_t *declaration = type->declaration->next;
714 for (; declaration != NULL; declaration = declaration->next) {
715 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
718 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
720 expression_t *const init = declaration->init.enum_value;
722 ir_node *const cnst = expression_to_firm(init);
723 if (!is_Const(cnst)) {
724 panic("couldn't fold constant");
726 tv_next = get_Const_tarval(cnst);
728 declaration->v.enum_val = tv_next;
729 tv_next = tarval_add(tv_next, one);
735 static ir_type *get_ir_type(type_t *type)
737 assert(type != NULL);
739 type = skip_typeref(type);
741 if(type->base.firm_type != NULL) {
742 assert(type->base.firm_type != INVALID_TYPE);
743 return type->base.firm_type;
746 ir_type *firm_type = NULL;
749 firm_type = create_atomic_type(&type->atomic);
752 firm_type = create_method_type(&type->function);
755 firm_type = create_pointer_type(&type->pointer);
758 firm_type = create_array_type(&type->array);
760 case TYPE_COMPOUND_STRUCT:
761 firm_type = create_struct_type(&type->compound);
763 case TYPE_COMPOUND_UNION:
764 firm_type = create_union_type(&type->compound);
767 firm_type = create_enum_type(&type->enumt);
770 firm_type = get_ir_type(type->builtin.real_type);
773 firm_type = create_bitfield_type(&type->bitfield);
781 if(firm_type == NULL)
782 panic("unknown type found");
784 type->base.firm_type = firm_type;
788 static inline ir_mode *get_ir_mode(type_t *type)
790 ir_type *irtype = get_ir_type(type);
792 /* firm doesn't report a mode for arrays somehow... */
793 if(is_Array_type(irtype)) {
797 ir_mode *mode = get_type_mode(irtype);
798 assert(mode != NULL);
802 static ident *predef_idents[rts_max];
804 /** Names of the runtime functions. */
805 static const struct {
806 int id; /**< the rts id */
807 int n_res; /**< number of return values */
808 const char *name; /**< the name of the rts function */
809 int n_params; /**< number of parameters */
810 unsigned flags; /**< language flags */
812 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
813 { rts_abort, 0, "abort", 0, _C89 },
814 { rts_abs, 1, "abs", 1, _C89 },
815 { rts_labs, 1, "labs", 1, _C89 },
816 { rts_llabs, 1, "llabs", 1, _C99 },
817 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
819 { rts_fabs, 1, "fabs", 1, _C89 },
820 { rts_sqrt, 1, "sqrt", 1, _C89 },
821 { rts_cbrt, 1, "cbrt", 1, _C99 },
822 { rts_exp, 1, "exp", 1, _C89 },
823 { rts_exp2, 1, "exp2", 1, _C89 },
824 { rts_exp10, 1, "exp10", 1, _GNUC },
825 { rts_log, 1, "log", 1, _C89 },
826 { rts_log2, 1, "log2", 1, _C89 },
827 { rts_log10, 1, "log10", 1, _C89 },
828 { rts_pow, 1, "pow", 2, _C89 },
829 { rts_sin, 1, "sin", 1, _C89 },
830 { rts_cos, 1, "cos", 1, _C89 },
831 { rts_tan, 1, "tan", 1, _C89 },
832 { rts_asin, 1, "asin", 1, _C89 },
833 { rts_acos, 1, "acos", 1, _C89 },
834 { rts_atan, 1, "atan", 1, _C89 },
835 { rts_sinh, 1, "sinh", 1, _C89 },
836 { rts_cosh, 1, "cosh", 1, _C89 },
837 { rts_tanh, 1, "tanh", 1, _C89 },
839 { rts_fabsf, 1, "fabsf", 1, _C99 },
840 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
841 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
842 { rts_expf, 1, "expf", 1, _C99 },
843 { rts_exp2f, 1, "exp2f", 1, _C99 },
844 { rts_exp10f, 1, "exp10f", 1, _GNUC },
845 { rts_logf, 1, "logf", 1, _C99 },
846 { rts_log2f, 1, "log2f", 1, _C99 },
847 { rts_log10f, 1, "log10f", 1, _C99 },
848 { rts_powf, 1, "powf", 2, _C99 },
849 { rts_sinf, 1, "sinf", 1, _C99 },
850 { rts_cosf, 1, "cosf", 1, _C99 },
851 { rts_tanf, 1, "tanf", 1, _C99 },
852 { rts_asinf, 1, "asinf", 1, _C99 },
853 { rts_acosf, 1, "acosf", 1, _C99 },
854 { rts_atanf, 1, "atanf", 1, _C99 },
855 { rts_sinhf, 1, "sinhf", 1, _C99 },
856 { rts_coshf, 1, "coshf", 1, _C99 },
857 { rts_tanhf, 1, "tanhf", 1, _C99 },
859 { rts_fabsl, 1, "fabsl", 1, _C99 },
860 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
861 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
862 { rts_expl, 1, "expl", 1, _C99 },
863 { rts_exp2l, 1, "exp2l", 1, _C99 },
864 { rts_exp10l, 1, "exp10l", 1, _GNUC },
865 { rts_logl, 1, "logl", 1, _C99 },
866 { rts_log2l, 1, "log2l", 1, _C99 },
867 { rts_log10l, 1, "log10l", 1, _C99 },
868 { rts_powl, 1, "powl", 2, _C99 },
869 { rts_sinl, 1, "sinl", 1, _C99 },
870 { rts_cosl, 1, "cosl", 1, _C99 },
871 { rts_tanl, 1, "tanl", 1, _C99 },
872 { rts_asinl, 1, "asinl", 1, _C99 },
873 { rts_acosl, 1, "acosl", 1, _C99 },
874 { rts_atanl, 1, "atanl", 1, _C99 },
875 { rts_sinhl, 1, "sinhl", 1, _C99 },
876 { rts_coshl, 1, "coshl", 1, _C99 },
877 { rts_tanhl, 1, "tanhl", 1, _C99 },
879 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
880 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
881 { rts_strcmp, 1, "strcmp", 2, _C89 },
882 { rts_strncmp, 1, "strncmp", 3, _C89 }
885 static ir_entity* get_function_entity(declaration_t *declaration)
887 if(declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
888 return declaration->v.entity;
889 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
891 symbol_t *symbol = declaration->symbol;
892 ident *id = new_id_from_str(symbol->string);
894 ir_type *global_type = get_glob_type();
895 ir_type *ir_type_method = get_ir_type(declaration->type);
896 assert(is_Method_type(ir_type_method));
898 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
899 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
900 set_entity_ld_ident(entity, id);
901 if(declaration->storage_class == STORAGE_CLASS_STATIC
902 || declaration->is_inline) {
903 set_entity_visibility(entity, visibility_local);
904 } else if(declaration->init.statement != NULL) {
905 set_entity_visibility(entity, visibility_external_visible);
907 set_entity_visibility(entity, visibility_external_allocated);
909 /* We should check for file scope here, but as long as we compile C only
910 this is not needed. */
911 int n_params = get_method_n_params(ir_type_method);
912 int n_res = get_method_n_ress(ir_type_method);
915 if (n_params == 0 && n_res == 0 && id == predef_idents[rts_abort]) {
916 /* found abort(), store for later */
920 if (! firm_opt.freestanding) {
921 /* check for a known runtime function */
922 for (i = 0; i < rts_max; ++i) {
923 /* ignore those rts functions not necessary needed for current mode */
924 if ((c_mode & rts_data[i].flags) == 0)
926 if (n_params == rts_data[i].n_params && n_res == rts_data[i].n_res &&
927 id == predef_idents[rts_data[i].id])
928 rts_entities[rts_data[i].id] = entity;
933 set_entity_allocation(entity, allocation_static);
935 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
936 declaration->v.entity = entity;
941 static ir_node *const_to_firm(const const_expression_t *cnst)
943 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
944 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
949 if(mode_is_float(mode)) {
950 tv = new_tarval_from_double(cnst->v.float_value, mode);
952 if(mode_is_signed(mode)) {
953 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
955 len = snprintf(buf, sizeof(buf), "%llu",
956 (unsigned long long) cnst->v.int_value);
958 tv = new_tarval_from_str(buf, len, mode);
961 return new_d_Const(dbgi, mode, tv);
964 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
967 assert(entity != NULL);
968 union symconst_symbol sym;
969 sym.entity_p = entity;
970 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
973 static ir_node *string_to_firm(const source_position_t *const src_pos,
974 const char *const id_prefix,
975 const char *const string)
977 ir_type *const global_type = get_glob_type();
978 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
981 ident *const id = unique_ident(id_prefix);
982 dbg_info *const dbgi = get_dbg_info(src_pos);
983 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
984 set_entity_ld_ident(entity, id);
985 set_entity_variability(entity, variability_constant);
986 set_entity_allocation(entity, allocation_static);
988 ir_type *const elem_type = ir_type_const_char;
989 ir_mode *const mode = get_type_mode(elem_type);
991 const size_t slen = strlen(string) + 1;
993 set_array_lower_bound_int(type, 0, 0);
994 set_array_upper_bound_int(type, 0, slen);
995 set_type_size_bytes(type, slen);
996 set_type_state(type, layout_fixed);
998 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
999 for(size_t i = 0; i < slen; ++i) {
1000 tvs[i] = new_tarval_from_long(string[i], mode);
1003 set_array_entity_values(entity, tvs, slen);
1006 return create_symconst(dbgi, mode_P_data, entity);
1009 static ir_node *string_literal_to_firm(
1010 const string_literal_expression_t* literal)
1012 return string_to_firm(&literal->expression.source_position, "Lstr",
1016 static ir_node *wide_string_literal_to_firm(
1017 const wide_string_literal_expression_t* const literal)
1019 ir_type *const global_type = get_glob_type();
1020 ir_type *const elem_type = ir_type_wchar_t;
1021 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
1024 ident *const id = unique_ident("Lstr");
1025 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
1026 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1027 set_entity_ld_ident(entity, id);
1028 set_entity_variability(entity, variability_constant);
1029 set_entity_allocation(entity, allocation_static);
1031 ir_mode *const mode = get_type_mode(elem_type);
1033 const wchar_rep_t *const string = literal->value.begin;
1034 const size_t slen = literal->value.size;
1036 set_array_lower_bound_int(type, 0, 0);
1037 set_array_upper_bound_int(type, 0, slen);
1038 set_type_size_bytes(type, slen);
1039 set_type_state(type, layout_fixed);
1041 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1042 for(size_t i = 0; i < slen; ++i) {
1043 tvs[i] = new_tarval_from_long(string[i], mode);
1046 set_array_entity_values(entity, tvs, slen);
1049 return create_symconst(dbgi, mode_P_data, entity);
1052 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
1053 dbg_info *const dbgi)
1055 if(is_compound_type(irtype) || is_Array_type(irtype)) {
1059 ir_mode *const mode = get_type_mode(irtype);
1060 ir_node *const memory = get_store();
1061 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1062 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1063 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1064 set_store(load_mem);
1068 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1070 ir_mode *mode = get_irn_mode(node);
1072 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1074 if(!mode_is_float(mode))
1077 /* check if there is already a Conv */
1078 if (get_irn_op(node) == op_Conv) {
1079 /* convert it into a strict Conv */
1080 set_Conv_strict(node, 1);
1084 /* otherwise create a new one */
1085 return new_d_strictConv(dbgi, node, mode);
1088 static ir_node *get_global_var_address(dbg_info *const dbgi,
1089 const declaration_t *const decl)
1091 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1093 ir_entity *const entity = decl->v.entity;
1094 switch ((storage_class_tag_t)decl->storage_class) {
1095 case STORAGE_CLASS_THREAD:
1096 case STORAGE_CLASS_THREAD_EXTERN:
1097 case STORAGE_CLASS_THREAD_STATIC: {
1098 ir_node *const no_mem = new_NoMem();
1099 ir_node *const tls = get_irg_tls(current_ir_graph);
1100 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1104 return create_symconst(dbgi, mode_P_data, entity);
1108 /* Returns the correct base address depending on whether it is a parameter or a
1109 * normal local variable */
1110 static ir_node *get_local_frame(ir_entity *const ent)
1112 ir_graph *const irg = current_ir_graph;
1113 const ir_type *const owner = get_entity_owner(ent);
1114 if (owner == get_irg_frame_type(irg)) {
1115 return get_irg_frame(irg);
1117 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1118 return get_irg_value_param_base(irg);
1122 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1124 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
1125 declaration_t *declaration = ref->declaration;
1126 type_t *type = skip_typeref(declaration->type);
1128 switch((declaration_kind_t) declaration->declaration_kind) {
1129 case DECLARATION_KIND_UNKNOWN:
1130 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
1136 case DECLARATION_KIND_ENUM_ENTRY: {
1137 ir_mode *const mode = get_ir_mode(type);
1138 return new_Const(mode, declaration->v.enum_val);
1141 case DECLARATION_KIND_LOCAL_VARIABLE: {
1142 ir_mode *const mode = get_ir_mode(type);
1143 return get_value(declaration->v.value_number, mode);
1145 case DECLARATION_KIND_FUNCTION: {
1146 ir_mode *const mode = get_ir_mode(type);
1147 return create_symconst(dbgi, mode, declaration->v.entity);
1149 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1150 ir_node *const addr = get_global_var_address(dbgi, declaration);
1151 ir_type *const irtype = get_entity_type(declaration->v.entity);
1152 return deref_address(irtype, addr, dbgi);
1155 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1156 ir_entity *entity = declaration->v.entity;
1157 ir_node *frame = get_local_frame(entity);
1158 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1159 ir_type *irtype = get_entity_type(entity);
1160 return deref_address(irtype, sel, dbgi);
1163 case DECLARATION_KIND_COMPOUND_MEMBER:
1164 case DECLARATION_KIND_LABEL_BLOCK:
1165 panic("not implemented reference type");
1168 panic("reference to declaration with unknown type found");
1171 static ir_node *reference_addr(const reference_expression_t *ref)
1173 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
1174 declaration_t *declaration = ref->declaration;
1176 switch((declaration_kind_t) declaration->declaration_kind) {
1177 case DECLARATION_KIND_UNKNOWN:
1179 case DECLARATION_KIND_LOCAL_VARIABLE:
1180 panic("local variable without entity has no address");
1181 case DECLARATION_KIND_FUNCTION: {
1182 type_t *const type = skip_typeref(ref->expression.datatype);
1183 ir_mode *const mode = get_ir_mode(type);
1184 return create_symconst(dbgi, mode, declaration->v.entity);
1186 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1187 ir_node *const addr = get_global_var_address(dbgi, declaration);
1190 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1191 ir_entity *entity = declaration->v.entity;
1192 ir_node *frame = get_local_frame(entity);
1193 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1198 case DECLARATION_KIND_ENUM_ENTRY:
1199 panic("trying to reference enum entry");
1201 case DECLARATION_KIND_COMPOUND_MEMBER:
1202 case DECLARATION_KIND_LABEL_BLOCK:
1203 panic("not implemented reference type");
1206 panic("reference to declaration with unknown type found");
1209 static ir_node *process_builtin_call(const call_expression_t *call)
1211 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1213 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1214 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1216 type_t *type = skip_typeref(builtin->expression.datatype);
1217 assert(is_type_pointer(type));
1219 type_t *function_type = skip_typeref(type->pointer.points_to);
1220 symbol_t *symbol = builtin->symbol;
1222 switch(symbol->ID) {
1223 case T___builtin_alloca: {
1224 if(call->arguments == NULL || call->arguments->next != NULL) {
1225 panic("invalid number of parameters on __builtin_alloca");
1227 expression_t *argument = call->arguments->expression;
1228 ir_node *size = expression_to_firm(argument);
1230 ir_node *store = get_store();
1231 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1233 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1235 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1239 case T___builtin_nan:
1240 case T___builtin_nanf:
1241 case T___builtin_nand: {
1242 /* Ignore string for now... */
1243 assert(is_type_function(function_type));
1244 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1245 tarval *tv = get_mode_NAN(mode);
1246 ir_node *res = new_d_Const(dbgi, mode, tv);
1249 case T___builtin_va_end:
1252 panic("Unsupported builtin found\n");
1256 static ir_node *call_expression_to_firm(const call_expression_t *call)
1258 assert(get_cur_block() != NULL);
1260 expression_t *function = call->function;
1261 if(function->kind == EXPR_BUILTIN_SYMBOL) {
1262 return process_builtin_call(call);
1264 ir_node *callee = expression_to_firm(function);
1266 type_t *type = skip_typeref(function->base.datatype);
1267 assert(is_type_pointer(type));
1268 pointer_type_t *pointer_type = &type->pointer;
1269 type_t *points_to = skip_typeref(pointer_type->points_to);
1270 assert(is_type_function(points_to));
1271 function_type_t *function_type = &points_to->function;
1273 int n_parameters = 0;
1274 call_argument_t *argument = call->arguments;
1275 for( ; argument != NULL; argument = argument->next) {
1279 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1281 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1282 ir_type *new_method_type = NULL;
1283 if(function_type->variadic || function_type->unspecified_parameters) {
1284 /* we need to construct a new method type matching the call
1286 int n_res = get_method_n_ress(ir_method_type);
1287 new_method_type = new_type_method(unique_ident("calltype"),
1288 n_parameters, n_res);
1289 set_method_calling_convention(new_method_type,
1290 get_method_calling_convention(ir_method_type));
1291 set_method_additional_properties(new_method_type,
1292 get_method_additional_properties(ir_method_type));
1294 for(int i = 0; i < n_res; ++i) {
1295 set_method_res_type(new_method_type, i,
1296 get_method_res_type(ir_method_type, i));
1299 ir_node *in[n_parameters];
1301 argument = call->arguments;
1303 for( ; argument != NULL; argument = argument->next) {
1304 expression_t *expression = argument->expression;
1305 ir_node *arg_node = expression_to_firm(expression);
1307 arg_node = do_strict_conv(dbgi, arg_node);
1310 if(new_method_type != NULL) {
1311 ir_type *irtype = get_ir_type(expression->base.datatype);
1312 set_method_param_type(new_method_type, n, irtype);
1317 assert(n == n_parameters);
1319 if(new_method_type != NULL)
1320 ir_method_type = new_method_type;
1322 ir_node *store = get_store();
1323 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1325 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1328 type_t *return_type = skip_typeref(function_type->return_type);
1329 ir_node *result = NULL;
1331 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1333 if(is_type_scalar(return_type)) {
1334 mode = get_ir_mode(return_type);
1338 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1339 result = new_d_Proj(dbgi, resproj, mode, 0);
1345 static void statement_to_firm(statement_t *statement);
1346 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1348 static ir_node *expression_to_addr(const expression_t *expression);
1349 static void create_condition_evaluation(const expression_t *expression,
1350 ir_node *true_block,
1351 ir_node *false_block);
1353 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1356 value = do_strict_conv(dbgi, value);
1358 ir_node *memory = get_store();
1360 if(is_type_scalar(type)) {
1361 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1362 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1363 set_store(store_mem);
1365 ir_type *irtype = get_ir_type(type);
1366 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1367 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1368 set_store(copyb_mem);
1372 static void set_value_for_expression(const expression_t *expression,
1375 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1376 value = do_strict_conv(dbgi, value);
1378 if(expression->kind == EXPR_REFERENCE) {
1379 const reference_expression_t *ref = &expression->reference;
1381 declaration_t *declaration = ref->declaration;
1382 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1383 if(declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1384 set_value(declaration->v.value_number, value);
1389 ir_node *addr = expression_to_addr(expression);
1390 type_t *type = skip_typeref(expression->base.datatype);
1391 assign_value(dbgi, addr, type, value);
1394 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1396 ir_mode *value_mode = get_irn_mode(value);
1398 if (value_mode == dest_mode || is_Bad(value))
1401 if(dest_mode == mode_b) {
1402 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1403 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1404 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1408 return new_d_Conv(dbgi, value, dest_mode);
1411 static ir_node *create_incdec(const unary_expression_t *expression)
1413 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1414 type_t *type = skip_typeref(expression->expression.datatype);
1415 ir_mode *mode = get_ir_mode(type);
1416 expression_t *value = expression->value;
1418 ir_node *value_node = expression_to_firm(value);
1421 if(is_type_pointer(type)) {
1422 pointer_type_t *pointer_type = &type->pointer;
1423 unsigned elem_size = get_type_size(pointer_type->points_to);
1424 offset = new_Const_long(mode_int, elem_size);
1426 assert(is_type_arithmetic(type));
1427 offset = new_Const(mode, get_mode_one(mode));
1430 switch(expression->expression.kind) {
1431 case EXPR_UNARY_POSTFIX_INCREMENT: {
1432 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1433 set_value_for_expression(value, new_value);
1436 case EXPR_UNARY_POSTFIX_DECREMENT: {
1437 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1438 set_value_for_expression(value, new_value);
1441 case EXPR_UNARY_PREFIX_INCREMENT: {
1442 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1443 set_value_for_expression(value, new_value);
1446 case EXPR_UNARY_PREFIX_DECREMENT: {
1447 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1448 set_value_for_expression(value, new_value);
1452 panic("no incdec expr in create_incdec");
1457 static bool is_local_variable(expression_t *expression)
1459 if (expression->kind != EXPR_REFERENCE)
1461 reference_expression_t *ref_expr = &expression->reference;
1462 declaration_t *declaration = ref_expr->declaration;
1463 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1466 static pn_Cmp get_pnc(const expression_kind_t kind)
1469 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1470 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1471 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Ne;
1472 case EXPR_BINARY_ISLESS:
1473 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1474 case EXPR_BINARY_ISLESSEQUAL:
1475 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1476 case EXPR_BINARY_ISGREATER:
1477 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1478 case EXPR_BINARY_ISGREATEREQUAL:
1479 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1480 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1485 panic("trying to get pn_Cmp from non-comparison binexpr type");
1489 * Handle the assume optimizer hint: check if a Confirm
1490 * node can be created.
1492 * @param dbi debug info
1493 * @param expr the IL assume expression
1495 * we support here only some simple cases:
1500 static ir_node *handle_assume_compare(dbg_info *dbi,
1501 const binary_expression_t *expression)
1503 expression_t *op1 = expression->left;
1504 expression_t *op2 = expression->right;
1505 declaration_t *var2, *var = NULL;
1506 ir_node *res = NULL;
1509 cmp_val = get_pnc(expression->expression.kind);
1511 if (is_local_variable(op1) && is_local_variable(op2)) {
1512 var = op1->reference.declaration;
1513 var2 = op2->reference.declaration;
1515 type_t *const type = skip_typeref(var->type);
1516 ir_mode *const mode = get_ir_mode(type);
1518 ir_node *const irn1 = get_value(var->v.value_number, mode);
1519 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1521 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1522 set_value(var2->v.value_number, res);
1524 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1525 set_value(var->v.value_number, res);
1531 if (is_local_variable(op1) && is_constant_expression(op2)) {
1532 var = op1->reference.declaration;
1534 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1535 cmp_val = get_inversed_pnc(cmp_val);
1536 var = op2->reference.declaration;
1541 type_t *const type = skip_typeref(var->type);
1542 ir_mode *const mode = get_ir_mode(type);
1544 res = get_value(var->v.value_number, mode);
1545 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1546 set_value(var->v.value_number, res);
1552 * Handle the assume optimizer hint.
1554 * @param dbi debug info
1555 * @param expr the IL assume expression
1557 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1558 switch(expression->kind) {
1559 case EXPR_BINARY_EQUAL:
1560 case EXPR_BINARY_NOTEQUAL:
1561 case EXPR_BINARY_LESS:
1562 case EXPR_BINARY_LESSEQUAL:
1563 case EXPR_BINARY_GREATER:
1564 case EXPR_BINARY_GREATEREQUAL:
1565 return handle_assume_compare(dbi, &expression->binary);
1571 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1573 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1574 type_t *type = skip_typeref(expression->expression.datatype);
1576 if(expression->expression.kind == EXPR_UNARY_TAKE_ADDRESS)
1577 return expression_to_addr(expression->value);
1579 const expression_t *value = expression->value;
1581 switch(expression->expression.kind) {
1582 case EXPR_UNARY_NEGATE: {
1583 ir_node *value_node = expression_to_firm(value);
1584 ir_mode *mode = get_ir_mode(type);
1585 return new_d_Minus(dbgi, value_node, mode);
1587 case EXPR_UNARY_PLUS:
1588 return expression_to_firm(value);
1589 case EXPR_UNARY_BITWISE_NEGATE: {
1590 ir_node *value_node = expression_to_firm(value);
1591 ir_mode *mode = get_ir_mode(type);
1592 return new_d_Not(dbgi, value_node, mode);
1594 case EXPR_UNARY_NOT: {
1595 ir_node *value_node = expression_to_firm(value);
1596 ir_mode *mode = get_ir_mode(type);
1597 if(get_irn_mode(value_node) != mode_b) {
1598 value_node = create_conv(dbgi, value_node, mode_b);
1600 value_node = new_d_Not(dbgi, value_node, mode_b);
1601 if(mode != mode_b) {
1602 value_node = create_conv(dbgi, value_node, mode);
1606 case EXPR_UNARY_DEREFERENCE: {
1607 ir_node *value_node = expression_to_firm(value);
1608 type_t *value_type = skip_typeref(value->base.datatype);
1609 ir_type *irtype = get_ir_type(value_type);
1610 assert(is_Pointer_type(irtype));
1611 ir_type *points_to = get_pointer_points_to_type(irtype);
1612 return deref_address(points_to, value_node, dbgi);
1614 case EXPR_UNARY_POSTFIX_INCREMENT:
1615 case EXPR_UNARY_POSTFIX_DECREMENT:
1616 case EXPR_UNARY_PREFIX_INCREMENT:
1617 case EXPR_UNARY_PREFIX_DECREMENT:
1618 return create_incdec(expression);
1619 case EXPR_UNARY_CAST: {
1620 ir_node *value_node = expression_to_firm(value);
1621 ir_mode *mode = get_ir_mode(type);
1622 ir_node *node = create_conv(dbgi, value_node, mode);
1623 node = do_strict_conv(dbgi, node);
1626 case EXPR_UNARY_CAST_IMPLICIT: {
1627 ir_node *value_node = expression_to_firm(value);
1628 ir_mode *mode = get_ir_mode(type);
1629 return create_conv(dbgi, value_node, mode);
1631 case EXPR_UNARY_ASSUME:
1632 if(firm_opt.confirm)
1633 return handle_assume(dbgi, value);
1640 panic("invalid UNEXPR type found");
1643 static ir_node *create_lazy_op(const binary_expression_t *expression)
1645 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1646 type_t *type = expression->expression.datatype;
1647 ir_mode *mode = get_ir_mode(type);
1649 if(is_constant_expression(expression->left)) {
1650 long val = fold_constant(expression->left);
1651 expression_kind_t ekind = expression->expression.kind;
1652 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
1653 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1654 return expression_to_firm(expression->right);
1656 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
1657 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
1658 return new_Const(mode, get_mode_one(mode));
1662 ir_node *cur_block = get_cur_block();
1664 ir_node *one_block = new_immBlock();
1665 ir_node *one = new_Const(mode, get_mode_one(mode));
1666 ir_node *jmp_one = new_d_Jmp(dbgi);
1668 ir_node *zero_block = new_immBlock();
1669 ir_node *zero = new_Const(mode, get_mode_null(mode));
1670 ir_node *jmp_zero = new_d_Jmp(dbgi);
1672 set_cur_block(cur_block);
1673 create_condition_evaluation((const expression_t*) expression,
1674 one_block, zero_block);
1675 mature_immBlock(one_block);
1676 mature_immBlock(zero_block);
1678 ir_node *common_block = new_immBlock();
1679 add_immBlock_pred(common_block, jmp_one);
1680 add_immBlock_pred(common_block, jmp_zero);
1681 mature_immBlock(common_block);
1683 ir_node *in[2] = { one, zero };
1684 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1689 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1690 ir_node *right, ir_mode *mode);
1692 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1693 create_arithmetic_func func)
1695 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1696 ir_node *left = expression_to_firm(expression->left);
1697 ir_node *right = expression_to_firm(expression->right);
1698 type_t *type = expression->right->base.datatype;
1699 /* be careful with the modes, because in arithmetic assign nodes only
1700 * the right operand has the mode of the arithmetic already */
1701 ir_mode *mode = get_ir_mode(type);
1702 left = create_conv(dbgi, left, mode);
1703 ir_node *res = func(dbgi, left, right, mode);
1708 static ir_node *pointer_arithmetic(ir_node *const pointer,
1711 dbg_info *const dbgi,
1712 const create_arithmetic_func func)
1714 pointer_type_t *const pointer_type = &type->pointer;
1715 type_t *const points_to = pointer_type->points_to;
1716 const unsigned elem_size = get_type_size(points_to);
1718 assert(elem_size >= 1);
1719 if (elem_size > 1) {
1720 integer = create_conv(dbgi, integer, mode_int);
1721 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1722 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1726 ir_mode *const mode = get_ir_mode(type);
1727 return func(dbgi, pointer, integer, mode);
1730 static ir_node *create_arithmetic_assign_binop(
1731 const binary_expression_t *expression, create_arithmetic_func func)
1733 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1734 type_t *const type = skip_typeref(expression->expression.datatype);
1737 if (is_type_pointer(type)) {
1738 ir_node *const pointer = expression_to_firm(expression->left);
1739 ir_node * integer = expression_to_firm(expression->right);
1740 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1742 value = create_arithmetic_binop(expression, func);
1745 ir_mode *const mode = get_ir_mode(type);
1746 value = create_conv(dbgi, value, mode);
1747 set_value_for_expression(expression->left, value);
1752 static ir_node *create_add(const binary_expression_t *expression)
1754 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1755 ir_node *left = expression_to_firm(expression->left);
1756 ir_node *right = expression_to_firm(expression->right);
1757 type_t *type = expression->expression.datatype;
1759 expression_t *expr_left = expression->left;
1760 expression_t *expr_right = expression->right;
1761 type_t *type_left = skip_typeref(expr_left->base.datatype);
1762 type_t *type_right = skip_typeref(expr_right->base.datatype);
1764 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1765 ir_mode *const mode = get_ir_mode(type);
1766 return new_d_Add(dbgi, left, right, mode);
1769 if (is_type_pointer(type_left)) {
1770 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1772 assert(is_type_pointer(type_right));
1773 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1777 static ir_node *create_sub(const binary_expression_t *expression)
1779 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1780 expression_t *const expr_left = expression->left;
1781 expression_t *const expr_right = expression->right;
1782 ir_node *const left = expression_to_firm(expr_left);
1783 ir_node *const right = expression_to_firm(expr_right);
1784 type_t *const type = expression->expression.datatype;
1785 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1786 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1788 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1789 ir_mode *const mode = get_ir_mode(type);
1790 return new_d_Sub(dbgi, left, right, mode);
1791 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1792 const pointer_type_t *const ptr_type = &type_left->pointer;
1793 const unsigned elem_size = get_type_size(ptr_type->points_to);
1794 ir_mode *const mode = get_ir_mode(type);
1795 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1796 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1797 ir_node *const no_mem = new_NoMem();
1798 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1799 op_pin_state_floats);
1800 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1803 assert(is_type_pointer(type_left));
1804 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1807 static ir_node *create_shift(const binary_expression_t *expression)
1809 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1810 ir_node *left = expression_to_firm(expression->left);
1811 ir_node *right = expression_to_firm(expression->right);
1812 type_t *type = expression->expression.datatype;
1813 ir_mode *mode = get_ir_mode(type);
1815 /* firm always wants the shift count to be unsigned */
1816 right = create_conv(dbgi, right, mode_uint);
1820 switch(expression->expression.kind) {
1821 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1822 case EXPR_BINARY_SHIFTLEFT:
1823 res = new_d_Shl(dbgi, left, right, mode);
1825 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1826 case EXPR_BINARY_SHIFTRIGHT: {
1827 expression_t *expr_left = expression->left;
1828 type_t *type_left = skip_typeref(expr_left->base.datatype);
1830 if(is_type_signed(type_left)) {
1831 res = new_d_Shrs(dbgi, left, right, mode);
1833 res = new_d_Shr(dbgi, left, right, mode);
1838 panic("create shift op called for non-shift op");
1845 static ir_node *create_divmod(const binary_expression_t *expression)
1847 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1848 ir_node *left = expression_to_firm(expression->left);
1849 ir_node *right = expression_to_firm(expression->right);
1850 ir_node *pin = new_Pin(new_NoMem());
1851 /* be careful with the modes, because in arithmetic assign nodes only
1852 * the right operand has the mode of the arithmetic already */
1853 type_t *type = expression->right->base.datatype;
1854 ir_mode *mode = get_ir_mode(type);
1855 left = create_conv(dbgi, left, mode);
1859 switch (expression->expression.kind) {
1860 case EXPR_BINARY_DIV:
1861 case EXPR_BINARY_DIV_ASSIGN:
1862 if(mode_is_float(mode)) {
1863 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1864 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1866 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1867 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1871 case EXPR_BINARY_MOD:
1872 case EXPR_BINARY_MOD_ASSIGN:
1873 assert(!mode_is_float(mode));
1874 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1875 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1878 default: panic("unexpected binary expression type in create_divmod()");
1884 static ir_node *create_arithmetic_assign_divmod(
1885 const binary_expression_t *expression)
1887 ir_node * value = create_divmod(expression);
1888 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1889 type_t *const type = expression->expression.datatype;
1890 ir_mode *const mode = get_ir_mode(type);
1892 assert(type->kind != TYPE_POINTER);
1894 value = create_conv(dbgi, value, mode);
1895 set_value_for_expression(expression->left, value);
1900 static ir_node *create_arithmetic_assign_shift(
1901 const binary_expression_t *expression)
1903 ir_node * value = create_shift(expression);
1904 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1905 type_t *const type = expression->expression.datatype;
1906 ir_mode *const mode = get_ir_mode(type);
1908 value = create_conv(dbgi, value, mode);
1909 set_value_for_expression(expression->left, value);
1914 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1916 expression_kind_t kind = expression->expression.kind;
1919 case EXPR_BINARY_EQUAL:
1920 case EXPR_BINARY_NOTEQUAL:
1921 case EXPR_BINARY_LESS:
1922 case EXPR_BINARY_LESSEQUAL:
1923 case EXPR_BINARY_GREATER:
1924 case EXPR_BINARY_GREATEREQUAL:
1925 case EXPR_BINARY_ISGREATER:
1926 case EXPR_BINARY_ISGREATEREQUAL:
1927 case EXPR_BINARY_ISLESS:
1928 case EXPR_BINARY_ISLESSEQUAL:
1929 case EXPR_BINARY_ISLESSGREATER:
1930 case EXPR_BINARY_ISUNORDERED: {
1931 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1932 ir_node *left = expression_to_firm(expression->left);
1933 ir_node *right = expression_to_firm(expression->right);
1934 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1935 long pnc = get_pnc(kind);
1936 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1939 case EXPR_BINARY_ASSIGN: {
1940 ir_node *right = expression_to_firm(expression->right);
1941 set_value_for_expression(expression->left, right);
1945 case EXPR_BINARY_ADD:
1946 return create_add(expression);
1947 case EXPR_BINARY_SUB:
1948 return create_sub(expression);
1949 case EXPR_BINARY_MUL:
1950 return create_arithmetic_binop(expression, new_d_Mul);
1951 case EXPR_BINARY_BITWISE_AND:
1952 return create_arithmetic_binop(expression, new_d_And);
1953 case EXPR_BINARY_BITWISE_OR:
1954 return create_arithmetic_binop(expression, new_d_Or);
1955 case EXPR_BINARY_BITWISE_XOR:
1956 return create_arithmetic_binop(expression, new_d_Eor);
1957 case EXPR_BINARY_SHIFTLEFT:
1958 case EXPR_BINARY_SHIFTRIGHT:
1959 return create_shift(expression);
1960 case EXPR_BINARY_DIV:
1961 case EXPR_BINARY_MOD:
1962 return create_divmod(expression);
1963 case EXPR_BINARY_LOGICAL_AND:
1964 case EXPR_BINARY_LOGICAL_OR:
1965 return create_lazy_op(expression);
1966 case EXPR_BINARY_COMMA:
1967 expression_to_firm(expression->left);
1968 return expression_to_firm(expression->right);
1969 case EXPR_BINARY_ADD_ASSIGN:
1970 return create_arithmetic_assign_binop(expression, new_d_Add);
1971 case EXPR_BINARY_SUB_ASSIGN:
1972 return create_arithmetic_assign_binop(expression, new_d_Sub);
1973 case EXPR_BINARY_MUL_ASSIGN:
1974 return create_arithmetic_assign_binop(expression, new_d_Mul);
1975 case EXPR_BINARY_DIV_ASSIGN:
1976 return create_arithmetic_assign_divmod(expression);
1977 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1978 return create_arithmetic_assign_binop(expression, new_d_And);
1979 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1980 return create_arithmetic_assign_binop(expression, new_d_Or);
1981 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1982 return create_arithmetic_assign_binop(expression, new_d_Eor);
1983 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1984 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1985 return create_arithmetic_assign_shift(expression);
1986 case EXPR_BINARY_BUILTIN_EXPECT:
1987 return expression_to_firm(expression->left);
1989 panic("TODO binexpr type");
1993 static ir_node *array_access_addr(const array_access_expression_t *expression)
1995 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1996 ir_node *base_addr = expression_to_firm(expression->array_ref);
1997 ir_node *offset = expression_to_firm(expression->index);
1998 offset = create_conv(dbgi, offset, mode_uint);
2000 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
2001 assert(is_type_pointer(ref_type));
2002 pointer_type_t *pointer_type = &ref_type->pointer;
2004 unsigned elem_size = get_type_size(pointer_type->points_to);
2005 ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
2006 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2008 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2013 static ir_node *array_access_to_firm(
2014 const array_access_expression_t *expression)
2016 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2017 ir_node *addr = array_access_addr(expression);
2018 type_t *type = revert_automatic_type_conversion(
2019 (const expression_t*) expression);
2020 type = skip_typeref(type);
2021 ir_type *irtype = get_ir_type(type);
2023 return deref_address(irtype, addr, dbgi);
2026 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
2028 type_t *type = expression->type;
2030 type = expression->size_expression->base.datatype;
2031 assert(type != NULL);
2034 ir_mode *const mode = get_ir_mode(expression->expression.datatype);
2035 symconst_symbol sym;
2036 sym.type_p = get_ir_type(type);
2037 return new_SymConst(mode, sym, symconst_type_size);
2040 static ir_node *alignof_to_firm(const alignof_expression_t *expression)
2042 type_t *const type = expression->type;
2043 ir_mode *const mode = get_ir_mode(expression->expression.datatype);
2044 symconst_symbol sym;
2045 sym.type_p = get_ir_type(type);
2046 return new_SymConst(mode, sym, symconst_type_align);
2049 static long fold_constant(const expression_t *expression)
2051 assert(is_constant_expression(expression));
2053 ir_graph *old_current_ir_graph = current_ir_graph;
2054 if(current_ir_graph == NULL) {
2055 current_ir_graph = get_const_code_irg();
2058 ir_node *cnst = expression_to_firm(expression);
2059 current_ir_graph = old_current_ir_graph;
2061 if(!is_Const(cnst)) {
2062 panic("couldn't fold constant\n");
2065 tarval *tv = get_Const_tarval(cnst);
2066 if(!tarval_is_long(tv)) {
2067 panic("result of constant folding is not integer\n");
2070 return get_tarval_long(tv);
2073 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2075 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
2077 /* first try to fold a constant condition */
2078 if(is_constant_expression(expression->condition)) {
2079 long val = fold_constant(expression->condition);
2081 return expression_to_firm(expression->true_expression);
2083 return expression_to_firm(expression->false_expression);
2087 ir_node *cur_block = get_cur_block();
2089 /* create the true block */
2090 ir_node *true_block = new_immBlock();
2092 ir_node *true_val = expression_to_firm(expression->true_expression);
2093 ir_node *true_jmp = new_Jmp();
2095 /* create the false block */
2096 ir_node *false_block = new_immBlock();
2098 ir_node *false_val = expression_to_firm(expression->false_expression);
2099 ir_node *false_jmp = new_Jmp();
2101 /* create the condition evaluation */
2102 set_cur_block(cur_block);
2103 create_condition_evaluation(expression->condition, true_block, false_block);
2104 mature_immBlock(true_block);
2105 mature_immBlock(false_block);
2107 /* create the common block */
2108 ir_node *common_block = new_immBlock();
2109 add_immBlock_pred(common_block, true_jmp);
2110 add_immBlock_pred(common_block, false_jmp);
2111 mature_immBlock(common_block);
2113 /* TODO improve static semantics, so either both or no values are NULL */
2114 if (true_val == NULL || false_val == NULL)
2117 ir_node *in[2] = { true_val, false_val };
2118 ir_mode *mode = get_irn_mode(true_val);
2119 assert(get_irn_mode(false_val) == mode);
2120 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2125 static ir_node *select_addr(const select_expression_t *expression)
2127 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2129 ir_node *compound_addr = expression_to_firm(expression->compound);
2131 declaration_t *entry = expression->compound_entry;
2132 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2133 ir_entity *entity = entry->v.entity;
2135 assert(entity != NULL);
2137 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2142 static ir_node *select_to_firm(const select_expression_t *expression)
2144 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2145 ir_node *addr = select_addr(expression);
2146 type_t *type = revert_automatic_type_conversion(
2147 (const expression_t*) expression);
2148 type = skip_typeref(type);
2149 ir_type *irtype = get_ir_type(type);
2151 return deref_address(irtype, addr, dbgi);
2154 /* Values returned by __builtin_classify_type. */
2155 typedef enum gcc_type_class
2161 enumeral_type_class,
2164 reference_type_class,
2168 function_type_class,
2179 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2181 const type_t *const type = expr->type_expression->base.datatype;
2187 const atomic_type_t *const atomic_type = &type->atomic;
2188 switch (atomic_type->atype) {
2189 /* should not be reached */
2190 case ATOMIC_TYPE_INVALID:
2194 /* gcc cannot do that */
2195 case ATOMIC_TYPE_VOID:
2196 tc = void_type_class;
2199 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2200 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2201 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2202 case ATOMIC_TYPE_SHORT:
2203 case ATOMIC_TYPE_USHORT:
2204 case ATOMIC_TYPE_INT:
2205 case ATOMIC_TYPE_UINT:
2206 case ATOMIC_TYPE_LONG:
2207 case ATOMIC_TYPE_ULONG:
2208 case ATOMIC_TYPE_LONGLONG:
2209 case ATOMIC_TYPE_ULONGLONG:
2210 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2211 tc = integer_type_class;
2214 case ATOMIC_TYPE_FLOAT:
2215 case ATOMIC_TYPE_DOUBLE:
2216 case ATOMIC_TYPE_LONG_DOUBLE:
2217 tc = real_type_class;
2220 #ifdef PROVIDE_COMPLEX
2221 case ATOMIC_TYPE_FLOAT_COMPLEX:
2222 case ATOMIC_TYPE_DOUBLE_COMPLEX:
2223 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
2224 tc = complex_type_class;
2226 case ATOMIC_TYPE_FLOAT_IMAGINARY:
2227 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
2228 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
2229 tc = complex_type_class;
2234 panic("Unimplemented case in classify_type_to_firm().");
2239 case TYPE_ARRAY: /* gcc handles this as pointer */
2240 case TYPE_FUNCTION: /* gcc handles this as pointer */
2241 case TYPE_POINTER: tc = pointer_type_class; break;
2242 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
2243 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
2245 /* gcc handles this as integer */
2246 case TYPE_ENUM: tc = integer_type_class; break;
2249 panic("Unimplemented case in classify_type_to_firm().");
2252 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
2253 ir_mode *const mode = mode_int;
2254 tarval *const tv = new_tarval_from_long(tc, mode);
2255 return new_d_Const(dbgi, mode, tv);
2258 static ir_node *function_name_to_firm(
2259 const string_literal_expression_t *const expr)
2261 if (current_function_name == NULL) {
2262 const source_position_t *const src_pos =
2263 &expr->expression.source_position;
2264 const char *const name = current_function_decl->symbol->string;
2265 current_function_name = string_to_firm(src_pos, "__func__", name);
2268 return current_function_name;
2271 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2273 statement_t *statement = expr->statement;
2275 assert(statement->kind == STATEMENT_COMPOUND);
2276 return compound_statement_to_firm(&statement->compound);
2279 static ir_node *va_start_expression_to_firm(
2280 const va_start_expression_t *const expr)
2282 ir_type *const method_type = get_ir_type(current_function_decl->type);
2283 int const n = get_method_n_params(method_type) - 1;
2284 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2285 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2286 dbg_info *const dbgi =
2287 get_dbg_info(&expr->expression.source_position);
2288 ir_node *const no_mem = new_NoMem();
2289 ir_node *const arg_sel =
2290 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2292 size_t const parm_size = get_type_size(expr->parameter->type);
2293 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2294 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2295 set_value_for_expression(expr->ap, add);
2300 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2302 ir_type *const irtype = get_ir_type(expr->expression.datatype);
2303 ir_node *const ap = expression_to_firm(expr->ap);
2304 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
2305 ir_node *const res = deref_address(irtype, ap, dbgi);
2307 size_t const parm_size = get_type_size(expr->expression.datatype);
2308 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2309 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2310 set_value_for_expression(expr->ap, add);
2315 static ir_node *dereference_addr(const unary_expression_t *const expression)
2317 assert(expression->expression.kind == EXPR_UNARY_DEREFERENCE);
2318 return expression_to_firm(expression->value);
2321 static ir_node *expression_to_addr(const expression_t *expression)
2323 switch(expression->kind) {
2324 case EXPR_REFERENCE:
2325 return reference_addr(&expression->reference);
2326 case EXPR_ARRAY_ACCESS:
2327 return array_access_addr(&expression->array_access);
2329 return select_addr(&expression->select);
2331 return call_expression_to_firm(&expression->call);
2332 case EXPR_UNARY_DEREFERENCE: {
2333 return dereference_addr(&expression->unary);
2338 panic("trying to get address of non-lvalue");
2341 static ir_node *builtin_constant_to_firm(const builtin_constant_expression_t *expression)
2343 ir_mode *mode = get_ir_mode(expression->expression.datatype);
2346 if (is_constant_expression(expression->value)) {
2351 return new_Const_long(mode, v);
2354 static ir_node *builtin_prefetch_to_firm(const builtin_prefetch_expression_t *expression)
2356 ir_node *adr = expression_to_firm(expression->adr);
2357 /* no Firm support for prefetch yet */
2362 static ir_node *_expression_to_firm(const expression_t *expression)
2364 switch(expression->kind) {
2366 return const_to_firm(&expression->conste);
2367 case EXPR_STRING_LITERAL:
2368 return string_literal_to_firm(&expression->string);
2369 case EXPR_WIDE_STRING_LITERAL:
2370 return wide_string_literal_to_firm(&expression->wide_string);
2371 case EXPR_REFERENCE:
2372 return reference_expression_to_firm(&expression->reference);
2374 return call_expression_to_firm(&expression->call);
2376 return unary_expression_to_firm(&expression->unary);
2378 return binary_expression_to_firm(&expression->binary);
2379 case EXPR_ARRAY_ACCESS:
2380 return array_access_to_firm(&expression->array_access);
2382 return sizeof_to_firm(&expression->sizeofe);
2384 return alignof_to_firm(&expression->alignofe);
2385 case EXPR_CONDITIONAL:
2386 return conditional_to_firm(&expression->conditional);
2388 return select_to_firm(&expression->select);
2389 case EXPR_CLASSIFY_TYPE:
2390 return classify_type_to_firm(&expression->classify_type);
2392 case EXPR_PRETTY_FUNCTION:
2393 return function_name_to_firm(&expression->string);
2394 case EXPR_STATEMENT:
2395 return statement_expression_to_firm(&expression->statement);
2397 return va_start_expression_to_firm(&expression->va_starte);
2399 return va_arg_expression_to_firm(&expression->va_arge);
2401 case EXPR_BUILTIN_SYMBOL:
2402 panic("unimplemented expression found");
2403 case EXPR_BUILTIN_CONSTANT_P:
2404 return builtin_constant_to_firm(&expression->builtin_constant);
2405 case EXPR_BUILTIN_PREFETCH:
2406 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2412 panic("invalid expression found");
2415 static ir_node *expression_to_firm(const expression_t *expression)
2417 ir_node *res = _expression_to_firm(expression);
2419 if(res != NULL && get_irn_mode(res) == mode_b) {
2420 ir_mode *mode = get_ir_mode(expression->base.datatype);
2421 res = create_conv(NULL, res, mode);
2427 static ir_node *expression_to_modeb(const expression_t *expression)
2429 ir_node *res = _expression_to_firm(expression);
2430 res = create_conv(NULL, res, mode_b);
2436 * create a short-circuit expression evaluation that tries to construct
2437 * efficient control flow structures for &&, || and ! expressions
2439 static void create_condition_evaluation(const expression_t *expression,
2440 ir_node *true_block,
2441 ir_node *false_block)
2443 switch(expression->kind) {
2444 case EXPR_UNARY_NOT: {
2445 const unary_expression_t *unary_expression = &expression->unary;
2446 create_condition_evaluation(unary_expression->value, false_block,
2450 case EXPR_BINARY_LOGICAL_AND: {
2451 const binary_expression_t *binary_expression = &expression->binary;
2453 ir_node *cur_block = get_cur_block();
2454 ir_node *extra_block = new_immBlock();
2455 set_cur_block(cur_block);
2456 create_condition_evaluation(binary_expression->left, extra_block,
2458 mature_immBlock(extra_block);
2459 set_cur_block(extra_block);
2460 create_condition_evaluation(binary_expression->right, true_block,
2464 case EXPR_BINARY_LOGICAL_OR: {
2465 const binary_expression_t *binary_expression = &expression->binary;
2467 ir_node *cur_block = get_cur_block();
2468 ir_node *extra_block = new_immBlock();
2469 set_cur_block(cur_block);
2470 create_condition_evaluation(binary_expression->left, true_block,
2472 mature_immBlock(extra_block);
2473 set_cur_block(extra_block);
2474 create_condition_evaluation(binary_expression->right, true_block,
2482 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2483 ir_node *condition = expression_to_modeb(expression);
2484 ir_node *cond = new_d_Cond(dbgi, condition);
2485 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2486 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2488 /* set branch prediction info based on __builtin_expect */
2489 if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
2490 long cnst = fold_constant(expression->binary.right);
2491 cond_jmp_predicate pred;
2494 pred = COND_JMP_PRED_FALSE;
2496 pred = COND_JMP_PRED_TRUE;
2498 set_Cond_jmp_pred(cond, pred);
2501 add_immBlock_pred(true_block, true_proj);
2502 add_immBlock_pred(false_block, false_proj);
2504 set_cur_block(NULL);
2509 static void create_declaration_entity(declaration_t *declaration,
2510 declaration_kind_t declaration_kind,
2511 ir_type *parent_type)
2513 ident *const id = new_id_from_str(declaration->symbol->string);
2514 ir_type *const irtype = get_ir_type(declaration->type);
2515 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2516 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2517 set_entity_ld_ident(entity, id);
2519 declaration->declaration_kind = (unsigned char) declaration_kind;
2520 declaration->v.entity = entity;
2521 set_entity_variability(entity, variability_uninitialized);
2522 if(parent_type == get_tls_type())
2523 set_entity_allocation(entity, allocation_automatic);
2524 else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
2525 set_entity_allocation(entity, allocation_static);
2526 /* TODO: visibility? */
2529 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2531 enum compound_graph_entry_type_t {
2532 COMPOUND_GRAPH_ENTRY_ARRAY,
2533 COMPOUND_GRAPH_ENTRY_COMPOUND
2536 struct compound_graph_path_entry_t {
2542 compound_graph_path_entry_t *prev;
2545 static void create_initializer_object(initializer_t *initializer, type_t *type,
2546 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2548 static compound_graph_path *create_compound_path(ir_type *type,
2549 compound_graph_path_entry_t *entry, int len)
2551 compound_graph_path *path = new_compound_graph_path(type, len);
2554 for( ; entry != NULL; entry = entry->prev, --i) {
2556 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2557 set_compound_graph_path_node(path, i, entry->v.entity);
2559 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2560 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2568 static void create_initializer_value(initializer_value_t *initializer,
2570 compound_graph_path_entry_t *entry,
2573 ir_node *node = expression_to_firm(initializer->value);
2574 ir_type *type = get_entity_type(entity);
2575 compound_graph_path *path = create_compound_path(type, entry, len);
2576 add_compound_ent_value_w_path(entity, node, path);
2579 static void create_initializer_compound(initializer_list_t *initializer,
2580 compound_type_t *type,
2582 compound_graph_path_entry_t *last_entry,
2585 declaration_t *compound_declaration = type->declaration;
2587 declaration_t *compound_entry = compound_declaration->context.declarations;
2589 compound_graph_path_entry_t entry;
2590 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2591 entry.prev = last_entry;
2595 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2596 if(compound_entry->symbol == NULL)
2598 if(compound_entry->namespc != NAMESPACE_NORMAL)
2601 if(i >= initializer->len)
2604 entry.v.entity = compound_entry->v.entity;
2606 initializer_t *sub_initializer = initializer->initializers[i];
2608 assert(compound_entry != NULL);
2609 assert(compound_entry->declaration_kind
2610 == DECLARATION_KIND_COMPOUND_MEMBER);
2612 if(sub_initializer->kind == INITIALIZER_VALUE) {
2613 create_initializer_value(&sub_initializer->value,
2614 entity, &entry, len);
2616 type_t *entry_type = skip_typeref(compound_entry->type);
2617 create_initializer_object(sub_initializer, entry_type, entity,
2625 static void create_initializer_array(initializer_list_t *initializer,
2626 array_type_t *type, ir_entity *entity,
2627 compound_graph_path_entry_t *last_entry,
2630 type_t *element_type = type->element_type;
2631 element_type = skip_typeref(element_type);
2633 compound_graph_path_entry_t entry;
2634 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2635 entry.prev = last_entry;
2639 for(i = 0; i < initializer->len; ++i) {
2640 entry.v.array_index = i;
2642 initializer_t *sub_initializer = initializer->initializers[i];
2644 if(sub_initializer->kind == INITIALIZER_VALUE) {
2645 create_initializer_value(&sub_initializer->value,
2646 entity, &entry, len);
2648 create_initializer_object(sub_initializer, element_type, entity,
2654 /* TODO: initialize rest... */
2655 if(type->size_expression != NULL) {
2656 size_t array_len = fold_constant(type->size_expression);
2657 for( ; i < array_len; ++i) {
2664 static void create_initializer_string(initializer_string_t *initializer,
2665 array_type_t *type, ir_entity *entity,
2666 compound_graph_path_entry_t *last_entry,
2669 type_t *element_type = type->element_type;
2670 element_type = skip_typeref(element_type);
2672 compound_graph_path_entry_t entry;
2673 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2674 entry.prev = last_entry;
2677 ir_type *irtype = get_entity_type(entity);
2678 size_t arr_len = get_array_type_size(type);
2679 const char *p = initializer->string;
2681 for(i = 0; i < arr_len; ++i, ++p) {
2682 entry.v.array_index = i;
2684 ir_node *node = new_Const_long(mode_Bs, *p);
2685 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2686 add_compound_ent_value_w_path(entity, node, path);
2693 static void create_initializer_wide_string(
2694 const initializer_wide_string_t *const initializer, array_type_t *const type,
2695 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2698 type_t *element_type = type->element_type;
2699 element_type = skip_typeref(element_type);
2701 compound_graph_path_entry_t entry;
2702 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2703 entry.prev = last_entry;
2706 ir_type *const irtype = get_entity_type(entity);
2707 const size_t arr_len = get_array_type_size(type);
2708 const wchar_rep_t * p = initializer->string.begin;
2709 const wchar_rep_t *const end = p + initializer->string.size;
2710 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2711 entry.v.array_index = i;
2713 ir_node *node = new_Const_long(mode_int, *p);
2714 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2715 add_compound_ent_value_w_path(entity, node, path);
2719 static void create_initializer_object(initializer_t *initializer, type_t *type,
2720 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2722 if(is_type_array(type)) {
2723 array_type_t *array_type = &type->array;
2725 switch (initializer->kind) {
2726 case INITIALIZER_STRING: {
2727 initializer_string_t *const string = &initializer->string;
2728 create_initializer_string(string, array_type, entity, entry, len);
2732 case INITIALIZER_WIDE_STRING: {
2733 initializer_wide_string_t *const string = &initializer->wide_string;
2734 create_initializer_wide_string(string, array_type, entity, entry, len);
2738 case INITIALIZER_LIST: {
2739 initializer_list_t *const list = &initializer->list;
2740 create_initializer_array(list, array_type, entity, entry, len);
2744 case INITIALIZER_VALUE:
2747 panic("Unhandled initializer");
2749 assert(initializer->kind == INITIALIZER_LIST);
2750 initializer_list_t *list = &initializer->list;
2752 assert(is_type_compound(type));
2753 compound_type_t *compound_type = &type->compound;
2754 create_initializer_compound(list, compound_type, entity, entry, len);
2758 static void create_initializer_local_variable_entity(declaration_t *declaration)
2760 initializer_t *initializer = declaration->init.initializer;
2761 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2762 ir_entity *entity = declaration->v.entity;
2763 ir_node *memory = get_store();
2764 ir_node *nomem = new_NoMem();
2765 ir_node *frame = get_irg_frame(current_ir_graph);
2766 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2768 if(initializer->kind == INITIALIZER_VALUE) {
2769 initializer_value_t *initializer_value = &initializer->value;
2771 ir_node *value = expression_to_firm(initializer_value->value);
2772 type_t *type = skip_typeref(declaration->type);
2773 assign_value(dbgi, addr, type, value);
2777 /* create a "template" entity which is copied to the entity on the stack */
2778 ident *const id = unique_ident("initializer");
2779 ir_type *const irtype = get_ir_type(declaration->type);
2780 ir_type *const global_type = get_glob_type();
2781 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
2782 set_entity_ld_ident(init_entity, id);
2784 set_entity_variability(init_entity, variability_initialized);
2785 set_entity_visibility(init_entity, visibility_local);
2786 set_entity_allocation(init_entity, allocation_static);
2788 ir_graph *const old_current_ir_graph = current_ir_graph;
2789 current_ir_graph = get_const_code_irg();
2791 type_t *const type = skip_typeref(declaration->type);
2792 create_initializer_object(initializer, type, init_entity, NULL, 0);
2794 assert(current_ir_graph == get_const_code_irg());
2795 current_ir_graph = old_current_ir_graph;
2797 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
2798 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2800 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2801 set_store(copyb_mem);
2804 static void create_initializer(declaration_t *declaration)
2806 initializer_t *initializer = declaration->init.initializer;
2807 if(initializer == NULL)
2810 declaration_kind_t declaration_kind
2811 = (declaration_kind_t) declaration->declaration_kind;
2812 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
2813 create_initializer_local_variable_entity(declaration);
2817 if(initializer->kind == INITIALIZER_VALUE) {
2818 initializer_value_t *initializer_value = &initializer->value;
2820 ir_node *value = expression_to_firm(initializer_value->value);
2822 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2823 set_value(declaration->v.value_number, value);
2825 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
2827 ir_entity *entity = declaration->v.entity;
2829 set_entity_variability(entity, variability_initialized);
2830 set_atomic_ent_value(entity, value);
2833 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
2834 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
2836 ir_entity *entity = declaration->v.entity;
2837 set_entity_variability(entity, variability_initialized);
2839 type_t *type = skip_typeref(declaration->type);
2840 create_initializer_object(initializer, type, entity, NULL, 0);
2844 static void create_local_variable(declaration_t *declaration)
2846 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
2848 bool needs_entity = declaration->address_taken;
2849 type_t *type = skip_typeref(declaration->type);
2851 if(is_type_array(type) || is_type_compound(type)) {
2852 needs_entity = true;
2856 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2857 create_declaration_entity(declaration,
2858 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
2861 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
2862 declaration->v.value_number = next_value_number_function;
2863 ++next_value_number_function;
2866 create_initializer(declaration);
2869 static void create_local_static_variable(declaration_t *declaration)
2871 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
2873 type_t *const type = skip_typeref(declaration->type);
2874 ir_type *const global_type = get_glob_type();
2875 ident *const id = unique_ident(declaration->symbol->string);
2876 ir_type *const irtype = get_ir_type(type);
2877 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2878 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
2879 set_entity_ld_ident(entity, id);
2881 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
2882 declaration->v.entity = entity;
2883 set_entity_variability(entity, variability_uninitialized);
2884 set_entity_visibility(entity, visibility_local);
2885 set_entity_allocation(entity, allocation_static);
2887 ir_graph *const old_current_ir_graph = current_ir_graph;
2888 current_ir_graph = get_const_code_irg();
2890 create_initializer(declaration);
2892 assert(current_ir_graph == get_const_code_irg());
2893 current_ir_graph = old_current_ir_graph;
2898 static void return_statement_to_firm(return_statement_t *statement)
2900 if(get_cur_block() == NULL)
2903 ir_type *func_irtype = get_ir_type(current_function_decl->type);
2905 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2909 if(get_method_n_ress(func_irtype) > 0) {
2910 ir_type *res_type = get_method_res_type(func_irtype, 0);
2912 if(statement->return_value != NULL) {
2913 ir_node *node = expression_to_firm(statement->return_value);
2914 node = do_strict_conv(dbgi, node);
2918 if(is_compound_type(res_type)) {
2921 mode = get_type_mode(res_type);
2923 in[0] = new_Unknown(mode);
2927 /* build return_value for its side effects */
2928 if(statement->return_value != NULL) {
2929 expression_to_firm(statement->return_value);
2934 ir_node *store = get_store();
2935 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
2937 ir_node *end_block = get_irg_end_block(current_ir_graph);
2938 add_immBlock_pred(end_block, ret);
2940 set_cur_block(NULL);
2943 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
2945 if(get_cur_block() == NULL)
2948 return expression_to_firm(statement->expression);
2951 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
2953 ir_node *result = NULL;
2954 statement_t *statement = compound->statements;
2955 for( ; statement != NULL; statement = statement->base.next) {
2956 //context2firm(&statement->context);
2958 if(statement->base.next == NULL
2959 && statement->kind == STATEMENT_EXPRESSION) {
2960 result = expression_statement_to_firm(
2961 &statement->expression);
2964 statement_to_firm(statement);
2970 static void create_local_declaration(declaration_t *declaration)
2972 type_t *type = skip_typeref(declaration->type);
2974 switch ((storage_class_tag_t) declaration->storage_class) {
2975 case STORAGE_CLASS_STATIC:
2976 create_local_static_variable(declaration);
2978 case STORAGE_CLASS_ENUM_ENTRY:
2979 panic("enum entry declaration in local block found");
2980 case STORAGE_CLASS_EXTERN:
2981 panic("extern declaration in local block found");
2982 case STORAGE_CLASS_NONE:
2983 case STORAGE_CLASS_AUTO:
2984 case STORAGE_CLASS_REGISTER:
2985 if(is_type_function(type)) {
2986 panic("nested functions not supported yet");
2988 create_local_variable(declaration);
2991 case STORAGE_CLASS_TYPEDEF:
2992 case STORAGE_CLASS_THREAD:
2993 case STORAGE_CLASS_THREAD_EXTERN:
2994 case STORAGE_CLASS_THREAD_STATIC:
2997 panic("invalid storage class found");
3000 static void declaration_statement_to_firm(declaration_statement_t *statement)
3002 declaration_t *declaration = statement->declarations_begin;
3003 declaration_t *end = statement->declarations_end->next;
3004 for( ; declaration != end; declaration = declaration->next) {
3005 create_local_variable(declaration);
3009 static void if_statement_to_firm(if_statement_t *statement)
3011 ir_node *cur_block = get_cur_block();
3013 ir_node *fallthrough_block = new_immBlock();
3015 /* the true (blocks) */
3016 ir_node *true_block;
3017 if (statement->true_statement != NULL) {
3018 true_block = new_immBlock();
3019 statement_to_firm(statement->true_statement);
3020 if(get_cur_block() != NULL) {
3021 ir_node *jmp = new_Jmp();
3022 add_immBlock_pred(fallthrough_block, jmp);
3025 true_block = fallthrough_block;
3028 /* the false (blocks) */
3029 ir_node *false_block;
3030 if(statement->false_statement != NULL) {
3031 false_block = new_immBlock();
3033 statement_to_firm(statement->false_statement);
3034 if(get_cur_block() != NULL) {
3035 ir_node *jmp = new_Jmp();
3036 add_immBlock_pred(fallthrough_block, jmp);
3039 false_block = fallthrough_block;
3042 /* create the condition */
3043 if(cur_block != NULL) {
3044 set_cur_block(cur_block);
3045 create_condition_evaluation(statement->condition, true_block,
3049 mature_immBlock(true_block);
3050 if(false_block != fallthrough_block) {
3051 mature_immBlock(false_block);
3053 mature_immBlock(fallthrough_block);
3055 set_cur_block(fallthrough_block);
3058 static void while_statement_to_firm(while_statement_t *statement)
3060 ir_node *jmp = NULL;
3061 if(get_cur_block() != NULL) {
3065 /* create the header block */
3066 ir_node *header_block = new_immBlock();
3068 add_immBlock_pred(header_block, jmp);
3071 /* the false block */
3072 ir_node *false_block = new_immBlock();
3075 ir_node *body_block;
3076 if (statement->body != NULL) {
3077 ir_node *old_continue_label = continue_label;
3078 ir_node *old_break_label = break_label;
3079 continue_label = header_block;
3080 break_label = false_block;
3082 body_block = new_immBlock();
3083 statement_to_firm(statement->body);
3085 assert(continue_label == header_block);
3086 assert(break_label == false_block);
3087 continue_label = old_continue_label;
3088 break_label = old_break_label;
3090 if(get_cur_block() != NULL) {
3092 add_immBlock_pred(header_block, jmp);
3095 body_block = header_block;
3098 /* create the condition */
3099 set_cur_block(header_block);
3101 create_condition_evaluation(statement->condition, body_block, false_block);
3102 mature_immBlock(body_block);
3103 mature_immBlock(false_block);
3104 mature_immBlock(header_block);
3106 set_cur_block(false_block);
3109 static void do_while_statement_to_firm(do_while_statement_t *statement)
3111 ir_node *jmp = NULL;
3112 if(get_cur_block() != NULL) {
3116 /* create the header block */
3117 ir_node *header_block = new_immBlock();
3119 /* the false block */
3120 ir_node *false_block = new_immBlock();
3123 ir_node *body_block = new_immBlock();
3125 add_immBlock_pred(body_block, jmp);
3128 if (statement->body != NULL) {
3129 ir_node *old_continue_label = continue_label;
3130 ir_node *old_break_label = break_label;
3131 continue_label = header_block;
3132 break_label = false_block;
3134 statement_to_firm(statement->body);
3136 assert(continue_label == header_block);
3137 assert(break_label == false_block);
3138 continue_label = old_continue_label;
3139 break_label = old_break_label;
3141 if (get_cur_block() == NULL) {
3142 mature_immBlock(header_block);
3143 mature_immBlock(body_block);
3144 mature_immBlock(false_block);
3149 ir_node *body_jmp = new_Jmp();
3150 add_immBlock_pred(header_block, body_jmp);
3151 mature_immBlock(header_block);
3153 /* create the condition */
3154 set_cur_block(header_block);
3156 create_condition_evaluation(statement->condition, body_block, false_block);
3157 mature_immBlock(body_block);
3158 mature_immBlock(false_block);
3159 mature_immBlock(header_block);
3161 set_cur_block(false_block);
3164 static void for_statement_to_firm(for_statement_t *statement)
3166 ir_node *jmp = NULL;
3167 if (get_cur_block() != NULL) {
3168 if(statement->initialisation != NULL) {
3169 expression_to_firm(statement->initialisation);
3172 /* create declarations */
3173 declaration_t *declaration = statement->context.declarations;
3174 for( ; declaration != NULL; declaration = declaration->next) {
3175 create_local_declaration(declaration);
3182 /* create the step block */
3183 ir_node *const step_block = new_immBlock();
3184 if (statement->step != NULL) {
3185 expression_to_firm(statement->step);
3187 ir_node *const step_jmp = new_Jmp();
3189 /* create the header block */
3190 ir_node *const header_block = new_immBlock();
3192 add_immBlock_pred(header_block, jmp);
3194 add_immBlock_pred(header_block, step_jmp);
3196 /* the false block */
3197 ir_node *const false_block = new_immBlock();
3200 ir_node * body_block;
3201 if (statement->body != NULL) {
3202 ir_node *const old_continue_label = continue_label;
3203 ir_node *const old_break_label = break_label;
3204 continue_label = step_block;
3205 break_label = false_block;
3207 body_block = new_immBlock();
3208 statement_to_firm(statement->body);
3210 assert(continue_label == step_block);
3211 assert(break_label == false_block);
3212 continue_label = old_continue_label;
3213 break_label = old_break_label;
3215 if (get_cur_block() != NULL) {
3217 add_immBlock_pred(step_block, jmp);
3220 body_block = step_block;
3223 /* create the condition */
3224 set_cur_block(header_block);
3225 if (statement->condition != NULL) {
3226 create_condition_evaluation(statement->condition, body_block,
3229 keep_alive(header_block);
3231 add_immBlock_pred(body_block, jmp);
3234 mature_immBlock(body_block);
3235 mature_immBlock(false_block);
3236 mature_immBlock(step_block);
3237 mature_immBlock(header_block);
3238 mature_immBlock(false_block);
3240 set_cur_block(false_block);
3243 static void create_jump_statement(const statement_t *statement,
3244 ir_node *target_block)
3246 if(get_cur_block() == NULL)
3249 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3250 ir_node *jump = new_d_Jmp(dbgi);
3251 add_immBlock_pred(target_block, jump);
3253 set_cur_block(NULL);
3256 static void switch_statement_to_firm(const switch_statement_t *statement)
3258 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
3260 ir_node *expression = expression_to_firm(statement->expression);
3261 ir_node *cond = new_d_Cond(dbgi, expression);
3262 ir_node *break_block = new_immBlock();
3264 set_cur_block(NULL);
3266 ir_node *const old_switch_cond = current_switch_cond;
3267 ir_node *const old_break_label = break_label;
3268 const bool old_saw_default_label = saw_default_label;
3269 current_switch_cond = cond;
3270 break_label = break_block;
3272 statement_to_firm(statement->body);
3274 if(get_cur_block() != NULL) {
3275 ir_node *jmp = new_Jmp();
3276 add_immBlock_pred(break_block, jmp);
3279 if (!saw_default_label) {
3280 set_cur_block(get_nodes_block(cond));
3281 ir_node *const proj = new_d_defaultProj(dbgi, cond,
3282 MAGIC_DEFAULT_PN_NUMBER);
3283 add_immBlock_pred(break_block, proj);
3286 assert(current_switch_cond == cond);
3287 assert(break_label == break_block);
3288 current_switch_cond = old_switch_cond;
3289 break_label = old_break_label;
3290 saw_default_label = old_saw_default_label;
3292 mature_immBlock(break_block);
3293 set_cur_block(break_block);
3296 static void case_label_to_firm(const case_label_statement_t *statement)
3298 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
3300 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
3302 /* let's create a node and hope firm constant folding creates a Const
3305 set_cur_block(get_nodes_block(current_switch_cond));
3306 if(statement->expression) {
3307 long pn = fold_constant(statement->expression);
3308 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
3309 /* oops someone detected our cheating... */
3310 panic("magic default pn used");
3312 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
3314 saw_default_label = true;
3315 proj = new_d_defaultProj(dbgi, current_switch_cond,
3316 MAGIC_DEFAULT_PN_NUMBER);
3319 ir_node *block = new_immBlock();
3320 if (fallthrough != NULL) {
3321 add_immBlock_pred(block, fallthrough);
3323 add_immBlock_pred(block, proj);
3324 mature_immBlock(block);
3326 if(statement->label_statement != NULL) {
3327 statement_to_firm(statement->label_statement);
3331 static ir_node *get_label_block(declaration_t *label)
3333 assert(label->namespc == NAMESPACE_LABEL);
3335 if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
3336 return label->v.block;
3338 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
3340 ir_node *old_cur_block = get_cur_block();
3341 ir_node *block = new_immBlock();
3342 set_cur_block(old_cur_block);
3344 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
3345 label->v.block = block;
3347 ARR_APP1(ir_node *, imature_blocks, block);
3352 static void label_to_firm(const label_statement_t *statement)
3354 ir_node *block = get_label_block(statement->label);
3356 if(get_cur_block() != NULL) {
3357 ir_node *jmp = new_Jmp();
3358 add_immBlock_pred(block, jmp);
3361 set_cur_block(block);
3364 if(statement->label_statement != NULL) {
3365 statement_to_firm(statement->label_statement);
3369 static void goto_to_firm(const goto_statement_t *statement)
3371 if(get_cur_block() == NULL)
3374 ir_node *block = get_label_block(statement->label);
3375 ir_node *jmp = new_Jmp();
3376 add_immBlock_pred(block, jmp);
3378 set_cur_block(NULL);
3381 typedef enum modifier_t {
3382 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
3383 ASM_MODIFIER_READ_WRITE = 1 << 1,
3384 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
3385 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
3389 static void asm_statement_to_firm(const asm_statement_t *statement)
3391 bool needs_memory = false;
3393 size_t n_clobbers = 0;
3394 asm_clobber_t *clobber = statement->clobbers;
3395 for( ; clobber != NULL; clobber = clobber->next) {
3396 if(strcmp(clobber->clobber, "memory") == 0) {
3397 needs_memory = true;
3401 ident *id = new_id_from_str(clobber->clobber);
3402 obstack_ptr_grow(&asm_obst, id);
3405 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
3406 ident **clobbers = NULL;
3407 if(n_clobbers > 0) {
3408 clobbers = obstack_finish(&asm_obst);
3411 /* find and count input and output constraints */
3412 asm_constraint_t *constraint = statement->inputs;
3413 for( ; constraint != NULL; constraint = constraint->next) {
3415 bool supports_memop = false;
3416 for(const char *c = constraint->constraints; *c != 0; ++c) {
3417 /* TODO: improve error messages */
3421 panic("multiple alternative assembler constraints not "
3429 supports_memop = true;
3430 obstack_1grow(&asm_obst, *c);
3433 if(modifiers & ASM_MODIFIER_READ_WRITE)
3434 panic("inconsistent register constraints");
3435 modifiers |= ASM_MODIFIER_WRITE_ONLY;
3438 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
3439 panic("inconsistent register constraints");
3440 modifiers |= ASM_MODIFIER_READ_WRITE;
3443 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
3444 panic("early clobber assembler constraint not supported yet");
3447 modifiers |= ASM_MODIFIER_COMMUTATIVE;
3448 panic("commutative assembler constraint not supported yet");
3451 /* skip register preferences stuff... */
3452 while(*c != 0 && *c != ',')
3456 /* skip register preferences stuff... */
3460 obstack_1grow(&asm_obst, *c);
3464 obstack_1grow(&asm_obst, '\0');
3465 const char *constraint_string = obstack_finish(&asm_obst);
3467 needs_memory |= supports_memop;
3468 if(supports_memop) {
3476 static void statement_to_firm(statement_t *statement)
3478 switch(statement->kind) {
3479 case STATEMENT_INVALID:
3480 panic("invalid statement found");
3481 case STATEMENT_COMPOUND:
3482 compound_statement_to_firm(&statement->compound);
3484 case STATEMENT_RETURN:
3485 return_statement_to_firm(&statement->returns);
3487 case STATEMENT_EXPRESSION:
3488 expression_statement_to_firm(&statement->expression);
3491 if_statement_to_firm(&statement->ifs);
3493 case STATEMENT_WHILE:
3494 while_statement_to_firm(&statement->whiles);
3496 case STATEMENT_DO_WHILE:
3497 do_while_statement_to_firm(&statement->do_while);
3499 case STATEMENT_DECLARATION:
3500 declaration_statement_to_firm(&statement->declaration);
3502 case STATEMENT_BREAK:
3503 create_jump_statement(statement, break_label);
3505 case STATEMENT_CONTINUE:
3506 create_jump_statement(statement, continue_label);
3508 case STATEMENT_SWITCH:
3509 switch_statement_to_firm(&statement->switchs);
3511 case STATEMENT_CASE_LABEL:
3512 case_label_to_firm(&statement->case_label);
3515 for_statement_to_firm(&statement->fors);
3517 case STATEMENT_LABEL:
3518 label_to_firm(&statement->label);
3520 case STATEMENT_GOTO:
3521 goto_to_firm(&statement->gotos);
3524 //asm_statement_to_firm(&statement->asms);
3528 panic("Statement not implemented\n");
3531 static int count_decls_in_expression(const expression_t *expression);
3533 static int count_local_declarations(const declaration_t * decl,
3534 const declaration_t *const end)
3537 for (; decl != end; decl = decl->next) {
3538 const type_t *type = skip_typeref(decl->type);
3539 switch (type->kind) {
3543 if (!decl->address_taken)
3549 const initializer_t *initializer = decl->init.initializer;
3550 /* FIXME: should walk initializer hierarchies... */
3551 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
3552 count += count_decls_in_expression(initializer->value.value);
3558 static int count_decls_in_expression(const expression_t *expression) {
3559 if(expression == NULL)
3562 switch(expression->base.kind) {
3563 case EXPR_STATEMENT:
3564 return count_decls_in_stmts(expression->statement.statement);
3566 int count_left = count_decls_in_expression(expression->binary.left);
3567 int count_right = count_decls_in_expression(expression->binary.right);
3568 return count_left + count_right;
3571 return count_decls_in_expression(expression->unary.value);
3577 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
3578 * (or implement all the missing expressions here/implement a walker)
3584 static int count_decls_in_stmts(const statement_t *stmt)
3587 for (; stmt != NULL; stmt = stmt->base.next) {
3588 switch (stmt->kind) {
3589 case STATEMENT_DECLARATION: {
3590 const declaration_statement_t *const decl_stmt = &stmt->declaration;
3591 count += count_local_declarations(decl_stmt->declarations_begin,
3592 decl_stmt->declarations_end->next);
3596 case STATEMENT_COMPOUND: {
3597 const compound_statement_t *const comp =
3599 count += count_decls_in_stmts(comp->statements);
3603 case STATEMENT_IF: {
3604 const if_statement_t *const if_stmt = &stmt->ifs;
3605 count += count_decls_in_expression(if_stmt->condition);
3606 count += count_decls_in_stmts(if_stmt->true_statement);
3607 count += count_decls_in_stmts(if_stmt->false_statement);
3611 case STATEMENT_SWITCH: {
3612 const switch_statement_t *const switch_stmt = &stmt->switchs;
3613 count += count_decls_in_expression(switch_stmt->expression);
3614 count += count_decls_in_stmts(switch_stmt->body);
3618 case STATEMENT_LABEL: {
3619 const label_statement_t *const label_stmt = &stmt->label;
3620 count += count_decls_in_stmts(label_stmt->label_statement);
3624 case STATEMENT_WHILE: {
3625 const while_statement_t *const while_stmt = &stmt->whiles;
3626 count += count_decls_in_expression(while_stmt->condition);
3627 count += count_decls_in_stmts(while_stmt->body);
3631 case STATEMENT_DO_WHILE: {
3632 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
3633 count += count_decls_in_expression(do_while_stmt->condition);
3634 count += count_decls_in_stmts(do_while_stmt->body);
3638 case STATEMENT_FOR: {
3639 const for_statement_t *const for_stmt = &stmt->fors;
3640 count += count_local_declarations(for_stmt->context.declarations, NULL);
3641 count += count_decls_in_expression(for_stmt->initialisation);
3642 count += count_decls_in_expression(for_stmt->condition);
3643 count += count_decls_in_expression(for_stmt->step);
3644 count += count_decls_in_stmts(for_stmt->body);
3648 case STATEMENT_CASE_LABEL: {
3649 const case_label_statement_t *label = &stmt->case_label;
3650 count += count_decls_in_expression(label->expression);
3651 count += count_decls_in_stmts(label->label_statement);
3656 case STATEMENT_BREAK:
3657 case STATEMENT_CONTINUE:
3660 case STATEMENT_EXPRESSION: {
3661 const expression_statement_t *expr_stmt = &stmt->expression;
3662 count += count_decls_in_expression(expr_stmt->expression);
3666 case STATEMENT_GOTO:
3667 case STATEMENT_INVALID:
3670 case STATEMENT_RETURN: {
3671 const return_statement_t *ret_stmt = &stmt->returns;
3672 count += count_decls_in_expression(ret_stmt->return_value);
3680 static int get_function_n_local_vars(declaration_t *declaration)
3684 /* count parameters */
3685 count += count_local_declarations(declaration->context.declarations, NULL);
3687 /* count local variables declared in body */
3688 count += count_decls_in_stmts(declaration->init.statement);
3693 static void initialize_function_parameters(declaration_t *declaration)
3695 ir_graph *irg = current_ir_graph;
3696 ir_node *args = get_irg_args(irg);
3697 ir_node *start_block = get_irg_start_block(irg);
3698 ir_type *function_irtype = get_ir_type(declaration->type);
3701 declaration_t *parameter = declaration->context.declarations;
3702 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3703 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
3704 type_t *type = skip_typeref(parameter->type);
3706 bool needs_entity = parameter->address_taken;
3707 assert(!is_type_array(type));
3708 if(is_type_compound(type)) {
3709 needs_entity = true;
3713 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3714 ident *id = new_id_from_str(parameter->symbol->string);
3715 set_entity_ident(entity, id);
3717 parameter->declaration_kind
3718 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
3719 parameter->v.entity = entity;
3723 ir_mode *mode = get_ir_mode(parameter->type);
3725 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3727 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3728 parameter->v.value_number = next_value_number_function;
3729 ++next_value_number_function;
3731 set_value(parameter->v.value_number, proj);
3736 * Handle additional decl modifiers for IR-graphs
3738 * @param irg the IR-graph
3739 * @param dec_modifiers additional modifiers
3741 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
3743 if (decl_modifiers & DM_NORETURN) {
3744 /* TRUE if the declaration includes the Microsoft
3745 __declspec(noreturn) specifier. */
3746 set_irg_additional_property(irg, mtp_property_noreturn);
3748 if (decl_modifiers & DM_NOTHROW) {
3749 /* TRUE if the declaration includes the Microsoft
3750 __declspec(nothrow) specifier. */
3751 set_irg_additional_property(irg, mtp_property_nothrow);
3753 if (decl_modifiers & DM_NAKED) {
3754 /* TRUE if the declaration includes the Microsoft
3755 __declspec(naked) specifier. */
3756 set_irg_additional_property(irg, mtp_property_naked);
3758 if (decl_modifiers & DM_FORCEINLINE) {
3759 /* TRUE if the declaration includes the
3760 Microsoft __forceinline specifier. */
3761 set_irg_inline_property(irg, irg_inline_forced);
3763 if (decl_modifiers & DM_NOINLINE) {
3764 /* TRUE if the declaration includes the Microsoft
3765 __declspec(noinline) specifier. */
3766 set_irg_inline_property(irg, irg_inline_forbidden);
3770 static void create_function(declaration_t *declaration)
3772 ir_entity *function_entity = get_function_entity(declaration);
3774 if(declaration->init.statement == NULL)
3777 current_function_decl = declaration;
3778 current_function_name = NULL;
3780 assert(imature_blocks == NULL);
3781 imature_blocks = NEW_ARR_F(ir_node*, 0);
3783 int n_local_vars = get_function_n_local_vars(declaration);
3784 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3785 ir_node *first_block = get_cur_block();
3787 /* set inline flags */
3788 if (declaration->is_inline)
3789 set_irg_inline_property(irg, irg_inline_recomended);
3790 handle_decl_modifier_irg(irg, declaration->modifiers);
3792 next_value_number_function = 0;
3793 initialize_function_parameters(declaration);
3795 statement_to_firm(declaration->init.statement);
3797 ir_node *end_block = get_irg_end_block(irg);
3799 /* do we have a return statement yet? */
3800 if(get_cur_block() != NULL) {
3801 type_t *type = skip_typeref(declaration->type);
3802 assert(is_type_function(type));
3803 const function_type_t *func_type = &type->function;
3804 const type_t *return_type
3805 = skip_typeref(func_type->return_type);
3808 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3809 ret = new_Return(get_store(), 0, NULL);
3812 if(is_type_scalar(return_type)) {
3813 mode = get_ir_mode(func_type->return_type);
3819 /* ยง5.1.2.2.3 main implicitly returns 0 */
3820 if (strcmp(declaration->symbol->string, "main") == 0) {
3821 in[0] = new_Const(mode, get_mode_null(mode));
3823 in[0] = new_Unknown(mode);
3825 ret = new_Return(get_store(), 1, in);
3827 add_immBlock_pred(end_block, ret);
3830 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3831 mature_immBlock(imature_blocks[i]);
3833 DEL_ARR_F(imature_blocks);
3834 imature_blocks = NULL;
3836 mature_immBlock(first_block);
3837 mature_immBlock(end_block);
3839 irg_finalize_cons(irg);
3841 /* finalize the frame type */
3842 ir_type *frame_type = get_irg_frame_type(irg);
3843 int n = get_compound_n_members(frame_type);
3846 for(int i = 0; i < n; ++i) {
3847 ir_entity *entity = get_compound_member(frame_type, i);
3848 ir_type *entity_type = get_entity_type(entity);
3850 int align = get_type_alignment_bytes(entity_type);
3851 if(align > align_all)
3855 misalign = offset % align;
3857 offset += align - misalign;
3861 set_entity_offset(entity, offset);
3862 offset += get_type_size_bytes(entity_type);
3864 set_type_size_bytes(frame_type, offset);
3865 set_type_alignment_bytes(frame_type, align_all);
3866 set_type_state(frame_type, layout_fixed);
3871 static void create_global_variable(declaration_t *declaration)
3875 switch ((storage_class_tag_t)declaration->storage_class) {
3876 case STORAGE_CLASS_STATIC:
3877 vis = visibility_local;
3880 case STORAGE_CLASS_EXTERN:
3881 vis = visibility_external_allocated;
3884 case STORAGE_CLASS_NONE:
3885 vis = visibility_external_visible;
3888 case STORAGE_CLASS_THREAD:
3889 vis = visibility_external_visible;
3892 case STORAGE_CLASS_THREAD_EXTERN:
3893 vis = visibility_external_allocated;
3896 case STORAGE_CLASS_THREAD_STATIC:
3897 vis = visibility_local;
3901 var_type = get_tls_type();
3905 var_type = get_glob_type();
3909 create_declaration_entity(declaration,
3910 DECLARATION_KIND_GLOBAL_VARIABLE,
3912 set_entity_visibility(declaration->v.entity, vis);
3914 current_ir_graph = get_const_code_irg();
3915 create_initializer(declaration);
3918 case STORAGE_CLASS_TYPEDEF:
3919 case STORAGE_CLASS_AUTO:
3920 case STORAGE_CLASS_REGISTER:
3921 case STORAGE_CLASS_ENUM_ENTRY:
3924 panic("Invalid storage class for global variable");
3927 static void context_to_firm(context_t *context)
3929 /* first pass: create declarations */
3930 declaration_t *declaration = context->declarations;
3931 for( ; declaration != NULL; declaration = declaration->next) {
3932 if(declaration->namespc != NAMESPACE_NORMAL)
3934 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3935 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3937 if(declaration->symbol == NULL)
3940 type_t *type = skip_typeref(declaration->type);
3941 if(is_type_function(type)) {
3942 get_function_entity(declaration);
3944 create_global_variable(declaration);
3948 /* second pass: create code */
3949 declaration = context->declarations;
3950 for( ; declaration != NULL; declaration = declaration->next) {
3951 if(declaration->namespc != NAMESPACE_NORMAL)
3953 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3954 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3956 if(declaration->symbol == NULL)
3959 type_t *type = declaration->type;
3960 if(type->kind != TYPE_FUNCTION)
3963 create_function(declaration);
3967 void init_ast2firm(void)
3969 obstack_init(&asm_obst);
3970 init_atomic_modes();
3972 /* create idents for all known runtime functions */
3973 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
3974 predef_idents[rts_data[i].id] = new_id_from_str(rts_data[i].name);
3978 void exit_ast2firm(void)
3980 obstack_free(&asm_obst, NULL);
3983 void translation_unit_to_firm(translation_unit_t *unit)
3985 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3986 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3987 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3989 ir_type_int = get_ir_type(type_int);
3990 ir_type_const_char = get_ir_type(type_const_char);
3991 ir_type_wchar_t = get_ir_type(type_wchar_t);
3992 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3995 type_void->base.firm_type = ir_type_void;
3997 /* just to be sure */
3998 continue_label = NULL;
4000 current_switch_cond = NULL;
4002 context_to_firm(&unit->context);