7 #include <libfirm/firm.h>
8 #include <libfirm/adt/obst.h>
12 #include "adt/error.h"
13 #include "adt/array.h"
18 #include "diagnostic.h"
19 #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;
33 static int next_value_number_function;
34 static ir_node *continue_label;
35 static ir_node *break_label;
36 static ir_node *current_switch_cond;
37 static bool saw_default_label;
38 static ir_node **imature_blocks;
40 static const declaration_t *current_function_decl;
41 static ir_node *current_function_name;
43 static struct obstack asm_obst;
45 typedef enum declaration_kind_t {
46 DECLARATION_KIND_UNKNOWN,
47 DECLARATION_KIND_FUNCTION,
48 DECLARATION_KIND_GLOBAL_VARIABLE,
49 DECLARATION_KIND_LOCAL_VARIABLE,
50 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
51 DECLARATION_KIND_COMPOUND_MEMBER,
52 DECLARATION_KIND_LABEL_BLOCK,
53 DECLARATION_KIND_ENUM_ENTRY
56 static ir_type *get_ir_type(type_t *type);
57 static int count_decls_in_stmts(const statement_t *stmt);
59 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
61 const declaration_t *declaration = get_irg_loc_description(irg, pos);
63 warningf(declaration->source_position,
64 "variable '%#T' might be used uninitialized",
65 declaration->type, declaration->symbol);
66 return new_r_Unknown(irg, mode);
69 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
71 const source_position_t *pos = (const source_position_t*) dbg;
74 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
78 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
80 const source_position_t *pos = (const source_position_t*) dbg;
85 return pos->input_name;
88 static dbg_info *get_dbg_info(const source_position_t *pos)
90 return (dbg_info*) pos;
93 static unsigned unique_id = 0;
95 static ident *unique_ident(const char *tag)
99 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
101 return new_id_from_str(buf);
105 * Return the signed integer mode of size bytes.
107 * @param size the size
109 static ir_mode *get_smode(unsigned size)
111 static ir_mode *s_modes[16 + 1] = {0, };
114 if (size <= 0 || size > 16)
123 snprintf(name, sizeof(name), "i%u", bits);
124 res = new_ir_mode(name, irms_int_number, bits, 1, irma_twos_complement,
125 bits <= machine_size ? machine_size : bits );
133 * Return the unsigned integer mode of size bytes.
135 * @param size the size
137 static ir_mode *get_umode(unsigned size)
139 static ir_mode *u_modes[16 + 1] = {0, };
142 if (size <= 0 || size > 16)
151 snprintf(name, sizeof(name), "u%u", bits);
152 res = new_ir_mode(name, irms_int_number, bits, 0, irma_twos_complement,
153 bits <= machine_size ? machine_size : bits );
161 * Return the pointer mode of size bytes.
163 * @param size the size
165 static ir_mode *get_ptrmode(unsigned size, char *name)
167 static ir_mode *p_modes[16 + 1] = {0, };
170 if (size <= 0 || size > 16)
180 snprintf(buf, sizeof(buf), "p%u", bits);
183 res = new_ir_mode(name, irms_reference, bits, 0, irma_twos_complement,
184 bits <= machine_size ? machine_size : bits);
188 set_reference_mode_signed_eq(res, get_smode(size));
189 set_reference_mode_unsigned_eq(res, get_umode(size));
194 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST];
196 static ir_mode *mode_int, *mode_uint;
199 * Initialises the atomic modes depending on the machine size.
201 static void init_atomic_modes(void) {
202 unsigned int_size = machine_size < 32 ? 2 : 4;
203 unsigned long_size = machine_size < 64 ? 4 : 8;
204 unsigned llong_size = machine_size < 32 ? 4 : 8;
206 /* firm has no real void... */
207 _atomic_modes[ATOMIC_TYPE_VOID] = mode_T;
208 _atomic_modes[ATOMIC_TYPE_CHAR] = char_is_signed ? get_smode(1) : get_umode(1);
209 _atomic_modes[ATOMIC_TYPE_SCHAR] = get_smode(1);
210 _atomic_modes[ATOMIC_TYPE_UCHAR] = get_umode(1);
211 _atomic_modes[ATOMIC_TYPE_SHORT] = get_smode(2);
212 _atomic_modes[ATOMIC_TYPE_USHORT] = get_umode(2);
213 _atomic_modes[ATOMIC_TYPE_INT] = get_smode(int_size);
214 _atomic_modes[ATOMIC_TYPE_UINT] = get_umode(int_size);
215 _atomic_modes[ATOMIC_TYPE_LONG] = get_smode(long_size);
216 _atomic_modes[ATOMIC_TYPE_ULONG] = get_umode(long_size);
217 _atomic_modes[ATOMIC_TYPE_LONGLONG] = get_smode(llong_size);
218 _atomic_modes[ATOMIC_TYPE_ULONGLONG] = get_umode(llong_size);
219 _atomic_modes[ATOMIC_TYPE_FLOAT] = mode_F;
220 _atomic_modes[ATOMIC_TYPE_DOUBLE] = mode_D;
221 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = mode_E;
222 _atomic_modes[ATOMIC_TYPE_BOOL] = get_umode(int_size);
224 #ifdef PROVIDE_COMPLEX
225 _atomic_modes[ATOMIC_TYPE_BOOL] = _atomic_modes[ATOMIC_TYPE_INT];
226 _atomic_modes[ATOMIC_TYPE_FLOAT_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_FLOAT];
227 _atomic_modes[ATOMIC_TYPE_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_DOUBLE];
228 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE];
231 /* Hmm, pointers should be machine size */
232 set_modeP_data(get_ptrmode(machine_size >> 3, NULL));
233 set_modeP_code(get_ptrmode(machine_size >> 3, NULL));
235 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
236 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
239 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
242 if ((unsigned)atomic_type->akind < (unsigned)ATOMIC_TYPE_LAST)
243 res = _atomic_modes[(unsigned)atomic_type->akind];
245 panic("Encountered unknown atomic type");
249 static unsigned get_type_size(type_t *type);
251 static unsigned get_atomic_type_size(const atomic_type_t *type)
253 switch(type->akind) {
254 case ATOMIC_TYPE_CHAR:
255 case ATOMIC_TYPE_SCHAR:
256 case ATOMIC_TYPE_UCHAR:
259 case ATOMIC_TYPE_SHORT:
260 case ATOMIC_TYPE_USHORT:
263 case ATOMIC_TYPE_BOOL:
264 case ATOMIC_TYPE_INT:
265 case ATOMIC_TYPE_UINT:
266 return machine_size >> 3;
268 case ATOMIC_TYPE_LONG:
269 case ATOMIC_TYPE_ULONG:
270 return machine_size > 16 ? machine_size >> 3 : 4;
272 case ATOMIC_TYPE_LONGLONG:
273 case ATOMIC_TYPE_ULONGLONG:
274 return machine_size > 16 ? 8 : 4;
276 case ATOMIC_TYPE_FLOAT:
279 case ATOMIC_TYPE_DOUBLE:
282 case ATOMIC_TYPE_LONG_DOUBLE:
285 case ATOMIC_TYPE_VOID:
288 case ATOMIC_TYPE_INVALID:
289 case ATOMIC_TYPE_LAST:
292 panic("Trying to determine size of invalid atomic type");
295 static unsigned get_compound_type_size(compound_type_t *type)
297 ir_type *irtype = get_ir_type((type_t*) type);
298 return get_type_size_bytes(irtype);
301 static unsigned get_array_type_size(array_type_t *type)
303 ir_type *irtype = get_ir_type((type_t*) type);
304 return get_type_size_bytes(irtype);
308 static unsigned get_type_size(type_t *type)
310 type = skip_typeref(type);
314 panic("error type occured");
316 return get_atomic_type_size(&type->atomic);
318 return get_mode_size_bytes(mode_int);
319 case TYPE_COMPOUND_UNION:
320 case TYPE_COMPOUND_STRUCT:
321 return get_compound_type_size(&type->compound);
323 /* just a pointer to the function */
324 return get_mode_size_bytes(mode_P_code);
326 return get_mode_size_bytes(mode_P_data);
328 return get_array_type_size(&type->array);
330 return get_type_size(type->builtin.real_type);
332 panic("type size of bitfield request");
338 panic("Trying to determine size of invalid type");
341 static unsigned count_parameters(const function_type_t *function_type)
345 function_parameter_t *parameter = function_type->parameters;
346 for ( ; parameter != NULL; parameter = parameter->next) {
354 static ir_type *create_atomic_type(const atomic_type_t *type)
356 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
357 ir_mode *mode = get_atomic_mode(type);
358 ident *id = get_mode_ident(mode);
359 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
361 if(type->akind == ATOMIC_TYPE_LONG_DOUBLE
362 || type->akind == ATOMIC_TYPE_DOUBLE) {
363 set_type_alignment_bytes(irtype, 4);
369 static ir_type *create_method_type(const function_type_t *function_type)
371 type_t *return_type = function_type->return_type;
373 ident *id = unique_ident("functiontype");
374 int n_parameters = count_parameters(function_type);
375 int n_results = return_type == type_void ? 0 : 1;
376 dbg_info *dbgi = get_dbg_info(&function_type->type.source_position);
377 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
379 if(return_type != type_void) {
380 ir_type *restype = get_ir_type(return_type);
381 set_method_res_type(irtype, 0, restype);
384 function_parameter_t *parameter = function_type->parameters;
386 for( ; parameter != NULL; parameter = parameter->next) {
387 ir_type *p_irtype = get_ir_type(parameter->type);
388 set_method_param_type(irtype, n, p_irtype);
392 if(function_type->variadic || function_type->unspecified_parameters) {
393 set_method_variadicity(irtype, variadicity_variadic);
399 static ir_type *create_pointer_type(pointer_type_t *type)
401 type_t *points_to = type->points_to;
402 ir_type *ir_points_to;
403 /* Avoid endless recursion if the points_to type contains this poiner type
404 * again (might be a struct). We therefore first create a void* pointer
405 * and then set the real points_to type
407 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
408 ir_type *ir_type = new_d_type_pointer(unique_ident("pointer"),
409 ir_type_void, mode_P_data, dbgi);
410 type->type.firm_type = ir_type;
412 ir_points_to = get_ir_type(points_to);
413 set_pointer_points_to_type(ir_type, ir_points_to);
418 static ir_type *create_array_type(array_type_t *type)
420 type_t *element_type = type->element_type;
421 ir_type *ir_element_type = get_ir_type(element_type);
423 ident *id = unique_ident("array");
424 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
425 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
427 const int align = get_type_alignment_bytes(ir_element_type);
428 set_type_alignment_bytes(ir_type, align);
430 if(type->size_constant) {
431 int n_elements = type->size;
433 set_array_bounds_int(ir_type, 0, 0, n_elements);
435 size_t elemsize = get_type_size_bytes(ir_element_type);
436 if(elemsize % align > 0) {
437 elemsize += align - (elemsize % align);
439 set_type_size_bytes(ir_type, n_elements * elemsize);
441 set_array_lower_bound_int(ir_type, 0, 0);
443 set_type_state(ir_type, layout_fixed);
449 * Return the signed integer type of size bits.
451 * @param size the size
453 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
456 static ir_mode *s_modes[64 + 1] = {NULL, };
460 if (size <= 0 || size > 64)
463 mode = s_modes[size];
467 snprintf(name, sizeof(name), "bf_I%u", size);
468 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
469 size <= 32 ? 32 : size );
470 s_modes[size] = mode;
474 snprintf(name, sizeof(name), "I%u", size);
475 ident *id = new_id_from_str(name);
476 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
477 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
478 set_primitive_base_type(res, base_tp);
484 * Return the unsigned integer type of size bits.
486 * @param size the size
488 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
491 static ir_mode *u_modes[64 + 1] = {NULL, };
495 if (size <= 0 || size > 64)
498 mode = u_modes[size];
502 snprintf(name, sizeof(name), "bf_U%u", size);
503 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
504 size <= 32 ? 32 : size );
505 u_modes[size] = mode;
510 snprintf(name, sizeof(name), "U%u", size);
511 ident *id = new_id_from_str(name);
512 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
513 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
514 set_primitive_base_type(res, base_tp);
519 static ir_type *create_bitfield_type(bitfield_type_t *const type)
521 type_t *base = skip_typeref(type->base);
522 assert(base->kind == TYPE_ATOMIC);
523 ir_type *irbase = get_ir_type(base);
525 unsigned size = fold_constant(type->size);
527 assert(!is_type_float(base));
528 if(is_type_signed(base)) {
529 return get_signed_int_type_for_bit_size(irbase, size);
531 return get_unsigned_int_type_for_bit_size(irbase, size);
535 #define INVALID_TYPE ((ir_type_ptr)-1)
537 static ir_type *create_union_type(compound_type_t *type, ir_type *irtype,
538 size_t *outer_offset, size_t *outer_align);
540 static ir_type *create_struct_type(compound_type_t *type, ir_type *irtype,
541 size_t *outer_offset, size_t *outer_align)
543 declaration_t *declaration = type->declaration;
544 if(declaration->v.irtype != NULL) {
545 return declaration->v.irtype;
548 size_t align_all = 1;
550 size_t bit_offset = 0;
552 symbol_t *symbol = declaration->symbol;
555 id = unique_ident(symbol->string);
557 id = unique_ident("__anonymous_struct");
559 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
561 irtype = new_d_type_struct(id, dbgi);
563 declaration->v.irtype = irtype;
564 type->type.firm_type = irtype;
566 offset = *outer_offset;
567 align_all = *outer_align;
570 declaration_t *entry = declaration->scope.declarations;
571 for( ; entry != NULL; entry = entry->next) {
572 if(entry->namespc != NAMESPACE_NORMAL)
575 symbol_t *symbol = entry->symbol;
576 type_t *entry_type = skip_typeref(entry->type);
577 dbg_info *dbgi = get_dbg_info(&entry->source_position);
580 ident = new_id_from_str(symbol->string);
582 if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
583 create_struct_type(&entry_type->compound, irtype, &offset,
586 } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
587 create_union_type(&entry_type->compound, irtype, &offset,
591 assert(entry_type->kind == TYPE_BITFIELD);
593 ident = unique_ident("anon");
596 ir_type *base_irtype;
597 if(entry_type->kind == TYPE_BITFIELD) {
598 base_irtype = get_ir_type(entry_type->bitfield.base);
600 base_irtype = get_ir_type(entry_type);
603 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
604 size_t misalign = offset % entry_alignment;
606 ir_type *entry_irtype = get_ir_type(entry_type);
607 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
610 size_t bits_remainder;
611 if(entry_type->kind == TYPE_BITFIELD) {
612 size_t size_bits = fold_constant(entry_type->bitfield.size);
613 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
615 if(size_bits > rest_size_bits) {
616 /* start a new bucket */
617 offset += entry_alignment - misalign;
623 /* put into current bucket */
624 base = offset - misalign;
625 bits_remainder = misalign * 8 + bit_offset;
628 offset += size_bits / 8;
629 bit_offset = bit_offset + (size_bits % 8);
631 size_t entry_size = get_type_size_bytes(base_irtype);
632 if(misalign > 0 || bit_offset > 0)
633 offset += entry_alignment - misalign;
637 offset += entry_size;
641 if(entry_alignment > align_all) {
642 if(entry_alignment % align_all != 0) {
643 panic("uneven alignments not supported yet");
645 align_all = entry_alignment;
648 set_entity_offset(entity, base);
649 set_entity_offset_bits_remainder(entity,
650 (unsigned char) bits_remainder);
651 //add_struct_member(irtype, entity);
652 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
653 assert(entry->v.entity == NULL);
654 entry->v.entity = entity;
657 size_t misalign = offset % align_all;
658 if(misalign > 0 || bit_offset > 0) {
659 offset += align_all - misalign;
662 if(outer_offset != NULL) {
663 *outer_offset = offset;
664 *outer_align = align_all;
666 set_type_alignment_bytes(irtype, align_all);
667 set_type_size_bytes(irtype, offset);
668 set_type_state(irtype, layout_fixed);
674 static ir_type *create_union_type(compound_type_t *type, ir_type *irtype,
675 size_t *outer_offset, size_t *outer_align)
677 declaration_t *declaration = type->declaration;
678 if(declaration->v.irtype != NULL) {
679 return declaration->v.irtype;
682 size_t align_all = 1;
687 symbol_t *symbol = declaration->symbol;
690 id = unique_ident(symbol->string);
692 id = unique_ident("__anonymous_union");
694 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
696 irtype = new_d_type_union(id, dbgi);
698 declaration->v.irtype = irtype;
699 type->type.firm_type = irtype;
701 offset = *outer_offset;
702 align_all = *outer_align;
705 type->type.firm_type = irtype;
707 declaration_t *entry = declaration->scope.declarations;
708 for( ; entry != NULL; entry = entry->next) {
709 if(entry->namespc != NAMESPACE_NORMAL)
712 symbol_t *symbol = entry->symbol;
713 type_t *entry_type = skip_typeref(entry->type);
714 ir_type *entry_ir_type = get_ir_type(entry_type);
718 ident = new_id_from_str(entry->symbol->string);
720 size_t offs = offset;
721 if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
722 create_struct_type(&entry_type->compound, irtype, &offs,
725 } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
726 create_union_type(&entry_type->compound, irtype, &offs,
730 panic("anonymous union member must be struct or union");
732 size_t entry_size = offs - offset;
733 if(entry_size > size) {
736 ident = unique_ident("anon");
739 size_t entry_size = get_type_size_bytes(entry_ir_type);
740 size_t entry_alignment = get_type_alignment_bytes(entry_ir_type);
742 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
743 ir_entity *const entity = new_d_entity(irtype, ident, entry_ir_type,
745 //add_union_member(irtype, entity);
746 set_entity_offset(entity, 0);
747 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
748 assert(entry->v.entity == NULL);
749 entry->v.entity = entity;
751 if(entry_size > size) {
754 if(entry_alignment > align_all) {
755 if(entry_alignment % align_all != 0) {
756 panic("Uneven alignments not supported yet");
758 align_all = entry_alignment;
762 if(outer_offset != NULL) {
763 assert(*outer_offset == offset);
765 size_t misalign = offset % align_all;
767 size += align_all - misalign;
768 *outer_offset += size;
770 if(align_all > *outer_align) {
771 if(align_all % *outer_align != 0) {
772 panic("uneven alignments not supported yet");
774 *outer_align = align_all;
777 set_type_alignment_bytes(irtype, align_all);
778 set_type_size_bytes(irtype, size);
779 set_type_state(irtype, layout_fixed);
780 declaration->v.irtype = irtype;
786 static ir_node *expression_to_firm(const expression_t *expression);
787 static inline ir_mode *get_ir_mode(type_t *type);
789 static ir_type *create_enum_type(enum_type_t *const type)
791 type->type.firm_type = ir_type_int;
793 ir_mode *const mode = mode_int;
794 tarval *const one = get_mode_one(mode);
795 tarval * tv_next = get_tarval_null(mode);
797 declaration_t *declaration = type->declaration->next;
798 for (; declaration != NULL; declaration = declaration->next) {
799 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
802 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
804 expression_t *const init = declaration->init.enum_value;
806 ir_node *const cnst = expression_to_firm(init);
807 if (!is_Const(cnst)) {
808 panic("couldn't fold constant");
810 tv_next = get_Const_tarval(cnst);
812 declaration->v.enum_val = tv_next;
813 tv_next = tarval_add(tv_next, one);
819 static ir_type *get_ir_type(type_t *type)
821 assert(type != NULL);
823 type = skip_typeref(type);
825 if(type->base.firm_type != NULL) {
826 assert(type->base.firm_type != INVALID_TYPE);
827 return type->base.firm_type;
830 ir_type *firm_type = NULL;
833 panic("error type occured");
835 firm_type = create_atomic_type(&type->atomic);
838 firm_type = create_method_type(&type->function);
841 firm_type = create_pointer_type(&type->pointer);
844 firm_type = create_array_type(&type->array);
846 case TYPE_COMPOUND_STRUCT:
847 firm_type = create_struct_type(&type->compound, NULL, NULL, NULL);
849 case TYPE_COMPOUND_UNION:
850 firm_type = create_union_type(&type->compound, NULL, NULL, NULL);
853 firm_type = create_enum_type(&type->enumt);
856 firm_type = get_ir_type(type->builtin.real_type);
859 firm_type = create_bitfield_type(&type->bitfield);
867 if(firm_type == NULL)
868 panic("unknown type found");
870 type->base.firm_type = firm_type;
874 static inline ir_mode *get_ir_mode(type_t *type)
876 ir_type *irtype = get_ir_type(type);
878 /* firm doesn't report a mode for arrays somehow... */
879 if(is_Array_type(irtype)) {
883 ir_mode *mode = get_type_mode(irtype);
884 assert(mode != NULL);
888 static ident *predef_idents[rts_max];
890 /** Names of the runtime functions. */
891 static const struct {
892 int id; /**< the rts id */
893 int n_res; /**< number of return values */
894 const char *name; /**< the name of the rts function */
895 int n_params; /**< number of parameters */
896 unsigned flags; /**< language flags */
898 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
899 { rts_abort, 0, "abort", 0, _C89 },
900 { rts_abs, 1, "abs", 1, _C89 },
901 { rts_labs, 1, "labs", 1, _C89 },
902 { rts_llabs, 1, "llabs", 1, _C99 },
903 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
905 { rts_fabs, 1, "fabs", 1, _C89 },
906 { rts_sqrt, 1, "sqrt", 1, _C89 },
907 { rts_cbrt, 1, "cbrt", 1, _C99 },
908 { rts_exp, 1, "exp", 1, _C89 },
909 { rts_exp2, 1, "exp2", 1, _C89 },
910 { rts_exp10, 1, "exp10", 1, _GNUC },
911 { rts_log, 1, "log", 1, _C89 },
912 { rts_log2, 1, "log2", 1, _C89 },
913 { rts_log10, 1, "log10", 1, _C89 },
914 { rts_pow, 1, "pow", 2, _C89 },
915 { rts_sin, 1, "sin", 1, _C89 },
916 { rts_cos, 1, "cos", 1, _C89 },
917 { rts_tan, 1, "tan", 1, _C89 },
918 { rts_asin, 1, "asin", 1, _C89 },
919 { rts_acos, 1, "acos", 1, _C89 },
920 { rts_atan, 1, "atan", 1, _C89 },
921 { rts_sinh, 1, "sinh", 1, _C89 },
922 { rts_cosh, 1, "cosh", 1, _C89 },
923 { rts_tanh, 1, "tanh", 1, _C89 },
925 { rts_fabsf, 1, "fabsf", 1, _C99 },
926 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
927 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
928 { rts_expf, 1, "expf", 1, _C99 },
929 { rts_exp2f, 1, "exp2f", 1, _C99 },
930 { rts_exp10f, 1, "exp10f", 1, _GNUC },
931 { rts_logf, 1, "logf", 1, _C99 },
932 { rts_log2f, 1, "log2f", 1, _C99 },
933 { rts_log10f, 1, "log10f", 1, _C99 },
934 { rts_powf, 1, "powf", 2, _C99 },
935 { rts_sinf, 1, "sinf", 1, _C99 },
936 { rts_cosf, 1, "cosf", 1, _C99 },
937 { rts_tanf, 1, "tanf", 1, _C99 },
938 { rts_asinf, 1, "asinf", 1, _C99 },
939 { rts_acosf, 1, "acosf", 1, _C99 },
940 { rts_atanf, 1, "atanf", 1, _C99 },
941 { rts_sinhf, 1, "sinhf", 1, _C99 },
942 { rts_coshf, 1, "coshf", 1, _C99 },
943 { rts_tanhf, 1, "tanhf", 1, _C99 },
945 { rts_fabsl, 1, "fabsl", 1, _C99 },
946 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
947 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
948 { rts_expl, 1, "expl", 1, _C99 },
949 { rts_exp2l, 1, "exp2l", 1, _C99 },
950 { rts_exp10l, 1, "exp10l", 1, _GNUC },
951 { rts_logl, 1, "logl", 1, _C99 },
952 { rts_log2l, 1, "log2l", 1, _C99 },
953 { rts_log10l, 1, "log10l", 1, _C99 },
954 { rts_powl, 1, "powl", 2, _C99 },
955 { rts_sinl, 1, "sinl", 1, _C99 },
956 { rts_cosl, 1, "cosl", 1, _C99 },
957 { rts_tanl, 1, "tanl", 1, _C99 },
958 { rts_asinl, 1, "asinl", 1, _C99 },
959 { rts_acosl, 1, "acosl", 1, _C99 },
960 { rts_atanl, 1, "atanl", 1, _C99 },
961 { rts_sinhl, 1, "sinhl", 1, _C99 },
962 { rts_coshl, 1, "coshl", 1, _C99 },
963 { rts_tanhl, 1, "tanhl", 1, _C99 },
965 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
966 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
967 { rts_strcmp, 1, "strcmp", 2, _C89 },
968 { rts_strncmp, 1, "strncmp", 3, _C89 }
971 static ir_entity* get_function_entity(declaration_t *declaration)
973 if(declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
974 return declaration->v.entity;
975 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
977 symbol_t *symbol = declaration->symbol;
978 ident *id = new_id_from_str(symbol->string);
980 ir_type *global_type = get_glob_type();
981 ir_type *ir_type_method = get_ir_type(declaration->type);
982 assert(is_Method_type(ir_type_method));
984 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
985 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
986 set_entity_ld_ident(entity, id);
987 if(declaration->storage_class == STORAGE_CLASS_STATIC
988 || declaration->is_inline) {
989 set_entity_visibility(entity, visibility_local);
990 } else if(declaration->init.statement != NULL) {
991 set_entity_visibility(entity, visibility_external_visible);
993 set_entity_visibility(entity, visibility_external_allocated);
995 /* We should check for file scope here, but as long as we compile C only
996 this is not needed. */
997 int n_params = get_method_n_params(ir_type_method);
998 int n_res = get_method_n_ress(ir_type_method);
1001 if (n_params == 0 && n_res == 0 && id == predef_idents[rts_abort]) {
1002 /* found abort(), store for later */
1006 if (! firm_opt.freestanding) {
1007 /* check for a known runtime function */
1008 for (i = 0; i < rts_max; ++i) {
1009 /* ignore those rts functions not necessary needed for current mode */
1010 if ((c_mode & rts_data[i].flags) == 0)
1012 if (n_params == rts_data[i].n_params && n_res == rts_data[i].n_res &&
1013 id == predef_idents[rts_data[i].id])
1014 rts_entities[rts_data[i].id] = entity;
1019 set_entity_allocation(entity, allocation_static);
1021 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1022 declaration->v.entity = entity;
1027 static ir_node *const_to_firm(const const_expression_t *cnst)
1029 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1030 ir_mode *mode = get_ir_mode(cnst->base.type);
1035 if(mode_is_float(mode)) {
1036 tv = new_tarval_from_double(cnst->v.float_value, mode);
1038 if(mode_is_signed(mode)) {
1039 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1041 len = snprintf(buf, sizeof(buf), "%llu",
1042 (unsigned long long) cnst->v.int_value);
1044 tv = new_tarval_from_str(buf, len, mode);
1047 return new_d_Const(dbgi, mode, tv);
1050 static ir_node *char_const_to_firm(const const_expression_t *cnst)
1052 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1053 ir_mode *mode = get_ir_mode(cnst->base.type);
1055 long long int v = 0;
1056 for (size_t i = 0; i < cnst->v.chars.size; ++i) {
1057 v = (v << 8) | ((unsigned char)cnst->v.chars.begin[i]);
1060 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1061 tarval *tv = new_tarval_from_str(buf, len, mode);
1063 return new_d_Const(dbgi, mode, tv);
1066 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1069 assert(entity != NULL);
1070 union symconst_symbol sym;
1071 sym.entity_p = entity;
1072 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1075 static ir_node *string_to_firm(const source_position_t *const src_pos,
1076 const char *const id_prefix,
1077 const string_t *const value)
1079 ir_type *const global_type = get_glob_type();
1080 dbg_info *const dbgi = get_dbg_info(src_pos);
1081 ir_type *const type = new_d_type_array(unique_ident("strtype"), 1,
1082 ir_type_const_char, dbgi);
1084 ident *const id = unique_ident(id_prefix);
1085 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1086 set_entity_ld_ident(entity, id);
1087 set_entity_variability(entity, variability_constant);
1088 set_entity_allocation(entity, allocation_static);
1090 ir_type *const elem_type = ir_type_const_char;
1091 ir_mode *const mode = get_type_mode(elem_type);
1093 const char* const string = value->begin;
1094 const size_t slen = value->size;
1096 set_array_lower_bound_int(type, 0, 0);
1097 set_array_upper_bound_int(type, 0, slen);
1098 set_type_size_bytes(type, slen);
1099 set_type_state(type, layout_fixed);
1101 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1102 for(size_t i = 0; i < slen; ++i) {
1103 tvs[i] = new_tarval_from_long(string[i], mode);
1106 set_array_entity_values(entity, tvs, slen);
1109 return create_symconst(dbgi, mode_P_data, entity);
1112 static ir_node *string_literal_to_firm(
1113 const string_literal_expression_t* literal)
1115 return string_to_firm(&literal->base.source_position, "Lstr",
1119 static ir_node *wide_string_literal_to_firm(
1120 const wide_string_literal_expression_t* const literal)
1122 ir_type *const global_type = get_glob_type();
1123 ir_type *const elem_type = ir_type_wchar_t;
1124 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1125 ir_type *const type = new_d_type_array(unique_ident("strtype"), 1,
1128 ident *const id = unique_ident("Lstr");
1129 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1130 set_entity_ld_ident(entity, id);
1131 set_entity_variability(entity, variability_constant);
1132 set_entity_allocation(entity, allocation_static);
1134 ir_mode *const mode = get_type_mode(elem_type);
1136 const wchar_rep_t *const string = literal->value.begin;
1137 const size_t slen = literal->value.size;
1139 set_array_lower_bound_int(type, 0, 0);
1140 set_array_upper_bound_int(type, 0, slen);
1141 set_type_size_bytes(type, slen);
1142 set_type_state(type, layout_fixed);
1144 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1145 for(size_t i = 0; i < slen; ++i) {
1146 tvs[i] = new_tarval_from_long(string[i], mode);
1149 set_array_entity_values(entity, tvs, slen);
1152 return create_symconst(dbgi, mode_P_data, entity);
1155 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
1156 dbg_info *const dbgi)
1158 if (is_compound_type(irtype) ||
1159 is_Method_type(irtype) ||
1160 is_Array_type(irtype)) {
1164 ir_mode *const mode = get_type_mode(irtype);
1165 ir_node *const memory = get_store();
1166 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1167 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1168 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1169 set_store(load_mem);
1173 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1175 ir_mode *mode = get_irn_mode(node);
1177 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1179 if(!mode_is_float(mode))
1182 /* check if there is already a Conv */
1183 if (get_irn_op(node) == op_Conv) {
1184 /* convert it into a strict Conv */
1185 set_Conv_strict(node, 1);
1189 /* otherwise create a new one */
1190 return new_d_strictConv(dbgi, node, mode);
1193 static ir_node *get_global_var_address(dbg_info *const dbgi,
1194 const declaration_t *const decl)
1196 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1198 ir_entity *const entity = decl->v.entity;
1199 switch ((storage_class_tag_t)decl->storage_class) {
1200 case STORAGE_CLASS_THREAD:
1201 case STORAGE_CLASS_THREAD_EXTERN:
1202 case STORAGE_CLASS_THREAD_STATIC: {
1203 ir_node *const no_mem = new_NoMem();
1204 ir_node *const tls = get_irg_tls(current_ir_graph);
1205 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1209 return create_symconst(dbgi, mode_P_data, entity);
1213 /* Returns the correct base address depending on whether it is a parameter or a
1214 * normal local variable */
1215 static ir_node *get_local_frame(ir_entity *const ent)
1217 ir_graph *const irg = current_ir_graph;
1218 const ir_type *const owner = get_entity_owner(ent);
1219 if (owner == get_irg_frame_type(irg)) {
1220 return get_irg_frame(irg);
1222 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1223 return get_irg_value_param_base(irg);
1227 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1229 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1230 declaration_t *declaration = ref->declaration;
1231 type_t *type = skip_typeref(declaration->type);
1233 switch((declaration_kind_t) declaration->declaration_kind) {
1234 case DECLARATION_KIND_UNKNOWN:
1235 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
1241 case DECLARATION_KIND_ENUM_ENTRY: {
1242 ir_mode *const mode = get_ir_mode(type);
1243 return new_Const(mode, declaration->v.enum_val);
1246 case DECLARATION_KIND_LOCAL_VARIABLE: {
1247 ir_mode *const mode = get_ir_mode(type);
1248 return get_value(declaration->v.value_number, mode);
1250 case DECLARATION_KIND_FUNCTION: {
1251 ir_mode *const mode = get_ir_mode(type);
1252 return create_symconst(dbgi, mode, declaration->v.entity);
1254 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1255 ir_node *const addr = get_global_var_address(dbgi, declaration);
1256 ir_type *const irtype = get_entity_type(declaration->v.entity);
1257 return deref_address(irtype, addr, dbgi);
1260 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1261 ir_entity *entity = declaration->v.entity;
1262 ir_node *frame = get_local_frame(entity);
1263 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1264 ir_type *irtype = get_entity_type(entity);
1265 return deref_address(irtype, sel, dbgi);
1268 case DECLARATION_KIND_COMPOUND_MEMBER:
1269 case DECLARATION_KIND_LABEL_BLOCK:
1270 panic("not implemented reference type");
1273 panic("reference to declaration with unknown type found");
1276 static ir_node *reference_addr(const reference_expression_t *ref)
1278 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1279 declaration_t *declaration = ref->declaration;
1281 switch((declaration_kind_t) declaration->declaration_kind) {
1282 case DECLARATION_KIND_UNKNOWN:
1284 case DECLARATION_KIND_LOCAL_VARIABLE:
1285 panic("local variable without entity has no address");
1286 case DECLARATION_KIND_FUNCTION: {
1287 type_t *const type = skip_typeref(ref->base.type);
1288 ir_mode *const mode = get_ir_mode(type);
1289 return create_symconst(dbgi, mode, declaration->v.entity);
1291 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1292 ir_node *const addr = get_global_var_address(dbgi, declaration);
1295 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1296 ir_entity *entity = declaration->v.entity;
1297 ir_node *frame = get_local_frame(entity);
1298 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1303 case DECLARATION_KIND_ENUM_ENTRY:
1304 panic("trying to reference enum entry");
1306 case DECLARATION_KIND_COMPOUND_MEMBER:
1307 case DECLARATION_KIND_LABEL_BLOCK:
1308 panic("not implemented reference type");
1311 panic("reference to declaration with unknown type found");
1314 static ir_node *process_builtin_call(const call_expression_t *call)
1316 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1318 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1319 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1321 type_t *type = skip_typeref(builtin->base.type);
1322 assert(is_type_pointer(type));
1324 type_t *function_type = skip_typeref(type->pointer.points_to);
1325 symbol_t *symbol = builtin->symbol;
1327 switch(symbol->ID) {
1328 case T___builtin_alloca: {
1329 if(call->arguments == NULL || call->arguments->next != NULL) {
1330 panic("invalid number of parameters on __builtin_alloca");
1332 expression_t *argument = call->arguments->expression;
1333 ir_node *size = expression_to_firm(argument);
1335 ir_node *store = get_store();
1336 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1338 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1340 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1344 case T___builtin_nan:
1345 case T___builtin_nanf:
1346 case T___builtin_nand: {
1347 /* Ignore string for now... */
1348 assert(is_type_function(function_type));
1349 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1350 tarval *tv = get_mode_NAN(mode);
1351 ir_node *res = new_d_Const(dbgi, mode, tv);
1354 case T___builtin_va_end:
1357 panic("Unsupported builtin found\n");
1361 static ir_node *call_expression_to_firm(const call_expression_t *call)
1363 assert(get_cur_block() != NULL);
1365 expression_t *function = call->function;
1366 if(function->kind == EXPR_BUILTIN_SYMBOL) {
1367 return process_builtin_call(call);
1369 ir_node *callee = expression_to_firm(function);
1371 type_t *type = skip_typeref(function->base.type);
1372 assert(is_type_pointer(type));
1373 pointer_type_t *pointer_type = &type->pointer;
1374 type_t *points_to = skip_typeref(pointer_type->points_to);
1375 assert(is_type_function(points_to));
1376 function_type_t *function_type = &points_to->function;
1378 int n_parameters = 0;
1379 call_argument_t *argument = call->arguments;
1380 for( ; argument != NULL; argument = argument->next) {
1384 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1386 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1387 ir_type *new_method_type = NULL;
1388 if(function_type->variadic || function_type->unspecified_parameters) {
1389 /* we need to construct a new method type matching the call
1391 int n_res = get_method_n_ress(ir_method_type);
1392 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1393 new_method_type = new_d_type_method(unique_ident("calltype"),
1394 n_parameters, n_res, dbgi);
1395 set_method_calling_convention(new_method_type,
1396 get_method_calling_convention(ir_method_type));
1397 set_method_additional_properties(new_method_type,
1398 get_method_additional_properties(ir_method_type));
1400 for(int i = 0; i < n_res; ++i) {
1401 set_method_res_type(new_method_type, i,
1402 get_method_res_type(ir_method_type, i));
1405 ir_node *in[n_parameters];
1407 argument = call->arguments;
1409 for( ; argument != NULL; argument = argument->next) {
1410 expression_t *expression = argument->expression;
1411 ir_node *arg_node = expression_to_firm(expression);
1413 arg_node = do_strict_conv(dbgi, arg_node);
1416 if(new_method_type != NULL) {
1417 ir_type *irtype = get_ir_type(expression->base.type);
1418 set_method_param_type(new_method_type, n, irtype);
1423 assert(n == n_parameters);
1425 if(new_method_type != NULL)
1426 ir_method_type = new_method_type;
1428 ir_node *store = get_store();
1429 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1431 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1434 type_t *return_type = skip_typeref(function_type->return_type);
1435 ir_node *result = NULL;
1437 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1439 if(is_type_scalar(return_type)) {
1440 mode = get_ir_mode(return_type);
1444 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1445 result = new_d_Proj(dbgi, resproj, mode, 0);
1451 static void statement_to_firm(statement_t *statement);
1452 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1454 static ir_node *expression_to_addr(const expression_t *expression);
1455 static void create_condition_evaluation(const expression_t *expression,
1456 ir_node *true_block,
1457 ir_node *false_block);
1459 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1462 value = do_strict_conv(dbgi, value);
1464 ir_node *memory = get_store();
1466 if(is_type_scalar(type)) {
1467 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1468 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1469 set_store(store_mem);
1471 ir_type *irtype = get_ir_type(type);
1472 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1473 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1474 set_store(copyb_mem);
1478 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1480 tarval *all_one = get_mode_all_one(mode);
1481 int mode_size = get_mode_size_bits(mode);
1483 assert(offset >= 0 && size >= 0);
1484 assert(offset + size <= mode_size);
1485 if(size == mode_size) {
1489 long shiftr = get_mode_size_bits(mode) - size;
1490 long shiftl = offset;
1491 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1492 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1493 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1494 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1499 static void bitfield_store_to_firm(const unary_expression_t *expression,
1502 expression_t *select = expression->value;
1503 assert(select->kind == EXPR_SELECT);
1504 type_t *type = select->base.type;
1505 assert(type->kind == TYPE_BITFIELD);
1506 ir_mode *mode = get_ir_mode(type->bitfield.base);
1507 ir_node *addr = expression_to_addr(select);
1509 assert(get_irn_mode(value) == mode);
1511 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1513 /* kill upper bits of value and shift to right position */
1514 ir_entity *entity = select->select.compound_entry->v.entity;
1515 int bitoffset = get_entity_offset_bits_remainder(entity);
1516 ir_type *entity_type = get_entity_type(entity);
1517 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1519 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1520 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1521 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1522 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1523 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1524 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1526 /* load current value */
1527 ir_node *mem = get_store();
1528 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1529 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1530 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1531 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1532 tarval *inv_mask = tarval_not(shift_mask);
1533 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1534 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1536 /* construct new value and store */
1537 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1538 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1539 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1540 set_store(store_mem);
1543 static void set_value_for_expression(const expression_t *expression,
1546 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1547 value = do_strict_conv(dbgi, value);
1549 if(expression->kind == EXPR_REFERENCE) {
1550 const reference_expression_t *ref = &expression->reference;
1552 declaration_t *declaration = ref->declaration;
1553 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1554 if(declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1555 set_value(declaration->v.value_number, value);
1560 if(expression->kind == EXPR_UNARY_BITFIELD_EXTRACT) {
1561 bitfield_store_to_firm(&expression->unary, value);
1565 ir_node *addr = expression_to_addr(expression);
1566 type_t *type = skip_typeref(expression->base.type);
1567 assign_value(dbgi, addr, type, value);
1570 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1572 ir_mode *value_mode = get_irn_mode(value);
1574 if (value_mode == dest_mode || is_Bad(value))
1577 if(dest_mode == mode_b) {
1578 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1579 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1580 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1584 return new_d_Conv(dbgi, value, dest_mode);
1587 static ir_node *create_incdec(const unary_expression_t *expression)
1589 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1590 type_t *type = skip_typeref(expression->base.type);
1591 ir_mode *mode = get_ir_mode(type);
1592 expression_t *value = expression->value;
1594 ir_node *value_node = expression_to_firm(value);
1597 if(is_type_pointer(type)) {
1598 pointer_type_t *pointer_type = &type->pointer;
1599 unsigned elem_size = get_type_size(pointer_type->points_to);
1600 offset = new_Const_long(mode_int, elem_size);
1602 assert(is_type_arithmetic(type));
1603 offset = new_Const(mode, get_mode_one(mode));
1606 switch(expression->base.kind) {
1607 case EXPR_UNARY_POSTFIX_INCREMENT: {
1608 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1609 set_value_for_expression(value, new_value);
1612 case EXPR_UNARY_POSTFIX_DECREMENT: {
1613 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1614 set_value_for_expression(value, new_value);
1617 case EXPR_UNARY_PREFIX_INCREMENT: {
1618 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1619 set_value_for_expression(value, new_value);
1622 case EXPR_UNARY_PREFIX_DECREMENT: {
1623 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1624 set_value_for_expression(value, new_value);
1628 panic("no incdec expr in create_incdec");
1633 static bool is_local_variable(expression_t *expression)
1635 if (expression->kind != EXPR_REFERENCE)
1637 reference_expression_t *ref_expr = &expression->reference;
1638 declaration_t *declaration = ref_expr->declaration;
1639 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1642 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1645 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1646 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1647 case EXPR_BINARY_NOTEQUAL:
1648 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1649 case EXPR_BINARY_ISLESS:
1650 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1651 case EXPR_BINARY_ISLESSEQUAL:
1652 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1653 case EXPR_BINARY_ISGREATER:
1654 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1655 case EXPR_BINARY_ISGREATEREQUAL:
1656 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1657 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1662 panic("trying to get pn_Cmp from non-comparison binexpr type");
1666 * Handle the assume optimizer hint: check if a Confirm
1667 * node can be created.
1669 * @param dbi debug info
1670 * @param expr the IL assume expression
1672 * we support here only some simple cases:
1677 static ir_node *handle_assume_compare(dbg_info *dbi,
1678 const binary_expression_t *expression)
1680 expression_t *op1 = expression->left;
1681 expression_t *op2 = expression->right;
1682 declaration_t *var2, *var = NULL;
1683 ir_node *res = NULL;
1686 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1688 if (is_local_variable(op1) && is_local_variable(op2)) {
1689 var = op1->reference.declaration;
1690 var2 = op2->reference.declaration;
1692 type_t *const type = skip_typeref(var->type);
1693 ir_mode *const mode = get_ir_mode(type);
1695 ir_node *const irn1 = get_value(var->v.value_number, mode);
1696 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1698 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1699 set_value(var2->v.value_number, res);
1701 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1702 set_value(var->v.value_number, res);
1708 if (is_local_variable(op1) && is_constant_expression(op2)) {
1709 var = op1->reference.declaration;
1711 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1712 cmp_val = get_inversed_pnc(cmp_val);
1713 var = op2->reference.declaration;
1718 type_t *const type = skip_typeref(var->type);
1719 ir_mode *const mode = get_ir_mode(type);
1721 res = get_value(var->v.value_number, mode);
1722 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1723 set_value(var->v.value_number, res);
1729 * Handle the assume optimizer hint.
1731 * @param dbi debug info
1732 * @param expr the IL assume expression
1734 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1735 switch(expression->kind) {
1736 case EXPR_BINARY_EQUAL:
1737 case EXPR_BINARY_NOTEQUAL:
1738 case EXPR_BINARY_LESS:
1739 case EXPR_BINARY_LESSEQUAL:
1740 case EXPR_BINARY_GREATER:
1741 case EXPR_BINARY_GREATEREQUAL:
1742 return handle_assume_compare(dbi, &expression->binary);
1748 static ir_node *bitfield_extract_to_firm(const unary_expression_t *expression)
1750 expression_t *select = expression->value;
1751 assert(select->kind == EXPR_SELECT);
1753 type_t *type = select->base.type;
1754 assert(type->kind == TYPE_BITFIELD);
1755 ir_mode *mode = get_ir_mode(type->bitfield.base);
1756 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1757 ir_node *addr = expression_to_addr(select);
1758 ir_node *mem = get_store();
1759 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1760 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1761 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1763 load_res = create_conv(dbgi, load_res, mode_int);
1765 set_store(load_mem);
1767 /* kill upper bits */
1768 ir_entity *entity = select->select.compound_entry->v.entity;
1769 int bitoffset = get_entity_offset_bits_remainder(entity);
1770 ir_type *entity_type = get_entity_type(entity);
1771 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1772 long shift_bitsl = machine_size - bitoffset - bitsize;
1773 assert(shift_bitsl >= 0);
1774 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1775 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1776 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1778 long shift_bitsr = bitoffset + shift_bitsl;
1779 assert(shift_bitsr <= (long) machine_size);
1780 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1781 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1783 if(mode_is_signed(mode)) {
1784 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1786 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1789 return create_conv(dbgi, shiftr, mode);
1792 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1794 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1795 type_t *type = skip_typeref(expression->base.type);
1797 if(expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
1798 return expression_to_addr(expression->value);
1800 const expression_t *value = expression->value;
1802 switch(expression->base.kind) {
1803 case EXPR_UNARY_NEGATE: {
1804 ir_node *value_node = expression_to_firm(value);
1805 ir_mode *mode = get_ir_mode(type);
1806 return new_d_Minus(dbgi, value_node, mode);
1808 case EXPR_UNARY_PLUS:
1809 return expression_to_firm(value);
1810 case EXPR_UNARY_BITWISE_NEGATE: {
1811 ir_node *value_node = expression_to_firm(value);
1812 ir_mode *mode = get_ir_mode(type);
1813 return new_d_Not(dbgi, value_node, mode);
1815 case EXPR_UNARY_NOT: {
1816 ir_node *value_node = expression_to_firm(value);
1817 ir_mode *mode = get_ir_mode(type);
1818 if(get_irn_mode(value_node) != mode_b) {
1819 value_node = create_conv(dbgi, value_node, mode_b);
1821 value_node = new_d_Not(dbgi, value_node, mode_b);
1822 if(mode != mode_b) {
1823 value_node = create_conv(dbgi, value_node, mode);
1827 case EXPR_UNARY_DEREFERENCE: {
1828 ir_node *value_node = expression_to_firm(value);
1829 type_t *value_type = skip_typeref(value->base.type);
1830 ir_type *irtype = get_ir_type(value_type);
1831 assert(is_Pointer_type(irtype));
1832 ir_type *points_to = get_pointer_points_to_type(irtype);
1833 return deref_address(points_to, value_node, dbgi);
1835 case EXPR_UNARY_POSTFIX_INCREMENT:
1836 case EXPR_UNARY_POSTFIX_DECREMENT:
1837 case EXPR_UNARY_PREFIX_INCREMENT:
1838 case EXPR_UNARY_PREFIX_DECREMENT:
1839 return create_incdec(expression);
1840 case EXPR_UNARY_CAST: {
1841 ir_node *value_node = expression_to_firm(value);
1842 if(is_type_scalar(type)) {
1843 ir_mode *mode = get_ir_mode(type);
1844 ir_node *node = create_conv(dbgi, value_node, mode);
1845 node = do_strict_conv(dbgi, node);
1851 case EXPR_UNARY_CAST_IMPLICIT: {
1852 ir_node *value_node = expression_to_firm(value);
1853 if(is_type_scalar(type)) {
1854 ir_mode *mode = get_ir_mode(type);
1855 return create_conv(dbgi, value_node, mode);
1860 case EXPR_UNARY_ASSUME:
1861 if(firm_opt.confirm)
1862 return handle_assume(dbgi, value);
1865 case EXPR_UNARY_BITFIELD_EXTRACT:
1866 return bitfield_extract_to_firm(expression);
1871 panic("invalid UNEXPR type found");
1874 static ir_node *create_lazy_op(const binary_expression_t *expression)
1876 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1877 type_t *type = expression->base.type;
1878 ir_mode *mode = get_ir_mode(type);
1880 if(is_constant_expression(expression->left)) {
1881 long val = fold_constant(expression->left);
1882 expression_kind_t ekind = expression->base.kind;
1883 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
1884 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1885 return expression_to_firm(expression->right);
1887 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
1888 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
1889 return new_Const(mode, get_mode_one(mode));
1893 ir_node *cur_block = get_cur_block();
1895 ir_node *one_block = new_immBlock();
1896 ir_node *one = new_Const(mode, get_mode_one(mode));
1897 ir_node *jmp_one = new_d_Jmp(dbgi);
1899 ir_node *zero_block = new_immBlock();
1900 ir_node *zero = new_Const(mode, get_mode_null(mode));
1901 ir_node *jmp_zero = new_d_Jmp(dbgi);
1903 set_cur_block(cur_block);
1904 create_condition_evaluation((const expression_t*) expression,
1905 one_block, zero_block);
1906 mature_immBlock(one_block);
1907 mature_immBlock(zero_block);
1909 ir_node *common_block = new_immBlock();
1910 add_immBlock_pred(common_block, jmp_one);
1911 add_immBlock_pred(common_block, jmp_zero);
1912 mature_immBlock(common_block);
1914 ir_node *in[2] = { one, zero };
1915 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1920 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1921 ir_node *right, ir_mode *mode);
1923 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1924 create_arithmetic_func func)
1926 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1927 ir_node *left = expression_to_firm(expression->left);
1928 ir_node *right = expression_to_firm(expression->right);
1929 type_t *type = expression->right->base.type;
1930 /* be careful with the modes, because in arithmetic assign nodes only
1931 * the right operand has the mode of the arithmetic already */
1932 ir_mode *mode = get_ir_mode(type);
1933 left = create_conv(dbgi, left, mode);
1934 ir_node *res = func(dbgi, left, right, mode);
1939 static ir_node *pointer_arithmetic(ir_node *const pointer,
1942 dbg_info *const dbgi,
1943 const create_arithmetic_func func)
1945 pointer_type_t *const pointer_type = &type->pointer;
1946 type_t *const points_to = pointer_type->points_to;
1947 const unsigned elem_size = get_type_size(points_to);
1949 assert(elem_size >= 1);
1950 if (elem_size > 1) {
1951 integer = create_conv(dbgi, integer, mode_int);
1952 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1953 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1957 ir_mode *const mode = get_ir_mode(type);
1958 return func(dbgi, pointer, integer, mode);
1961 static ir_node *create_arithmetic_assign_binop(
1962 const binary_expression_t *expression, create_arithmetic_func func)
1964 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1965 type_t *const type = skip_typeref(expression->base.type);
1968 if (is_type_pointer(type)) {
1969 ir_node *const pointer = expression_to_firm(expression->left);
1970 ir_node * integer = expression_to_firm(expression->right);
1971 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1973 value = create_arithmetic_binop(expression, func);
1976 ir_mode *const mode = get_ir_mode(type);
1977 value = create_conv(dbgi, value, mode);
1978 set_value_for_expression(expression->left, value);
1983 static ir_node *create_add(const binary_expression_t *expression)
1985 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1986 ir_node *left = expression_to_firm(expression->left);
1987 ir_node *right = expression_to_firm(expression->right);
1988 type_t *type = expression->base.type;
1990 expression_t *expr_left = expression->left;
1991 expression_t *expr_right = expression->right;
1992 type_t *type_left = skip_typeref(expr_left->base.type);
1993 type_t *type_right = skip_typeref(expr_right->base.type);
1995 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1996 ir_mode *const mode = get_ir_mode(type);
1997 return new_d_Add(dbgi, left, right, mode);
2000 if (is_type_pointer(type_left)) {
2001 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
2003 assert(is_type_pointer(type_right));
2004 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
2008 static ir_node *create_sub(const binary_expression_t *expression)
2010 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2011 expression_t *const expr_left = expression->left;
2012 expression_t *const expr_right = expression->right;
2013 ir_node *const left = expression_to_firm(expr_left);
2014 ir_node *const right = expression_to_firm(expr_right);
2015 type_t *const type = expression->base.type;
2016 type_t *const type_left = skip_typeref(expr_left->base.type);
2017 type_t *const type_right = skip_typeref(expr_right->base.type);
2019 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2020 ir_mode *const mode = get_ir_mode(type);
2021 return new_d_Sub(dbgi, left, right, mode);
2022 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2023 const pointer_type_t *const ptr_type = &type_left->pointer;
2024 const unsigned elem_size = get_type_size(ptr_type->points_to);
2025 ir_mode *const mode = get_ir_mode(type);
2026 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2027 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2028 ir_node *const no_mem = new_NoMem();
2029 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
2030 op_pin_state_floats);
2031 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2034 assert(is_type_pointer(type_left));
2035 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
2038 static ir_node *create_shift(const binary_expression_t *expression)
2040 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2041 ir_node *left = expression_to_firm(expression->left);
2042 ir_node *right = expression_to_firm(expression->right);
2043 type_t *type = expression->base.type;
2044 ir_mode *mode = get_ir_mode(type);
2046 /* firm always wants the shift count to be unsigned */
2047 right = create_conv(dbgi, right, mode_uint);
2051 switch(expression->base.kind) {
2052 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2053 case EXPR_BINARY_SHIFTLEFT:
2054 res = new_d_Shl(dbgi, left, right, mode);
2056 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2057 case EXPR_BINARY_SHIFTRIGHT: {
2058 expression_t *expr_left = expression->left;
2059 type_t *type_left = skip_typeref(expr_left->base.type);
2061 if(is_type_signed(type_left)) {
2062 res = new_d_Shrs(dbgi, left, right, mode);
2064 res = new_d_Shr(dbgi, left, right, mode);
2069 panic("create shift op called for non-shift op");
2076 static ir_node *create_divmod(const binary_expression_t *expression)
2078 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2079 ir_node *left = expression_to_firm(expression->left);
2080 ir_node *right = expression_to_firm(expression->right);
2081 ir_node *pin = new_Pin(new_NoMem());
2082 /* be careful with the modes, because in arithmetic assign nodes only
2083 * the right operand has the mode of the arithmetic already */
2084 type_t *type = expression->right->base.type;
2085 ir_mode *mode = get_ir_mode(type);
2086 left = create_conv(dbgi, left, mode);
2090 switch (expression->base.kind) {
2091 case EXPR_BINARY_DIV:
2092 case EXPR_BINARY_DIV_ASSIGN:
2093 if(mode_is_float(mode)) {
2094 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2095 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2097 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2098 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2102 case EXPR_BINARY_MOD:
2103 case EXPR_BINARY_MOD_ASSIGN:
2104 assert(!mode_is_float(mode));
2105 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
2106 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2109 default: panic("unexpected binary expression type in create_divmod()");
2115 static ir_node *create_arithmetic_assign_divmod(
2116 const binary_expression_t *expression)
2118 ir_node * value = create_divmod(expression);
2119 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2120 type_t *const type = expression->base.type;
2121 ir_mode *const mode = get_ir_mode(type);
2123 assert(type->kind != TYPE_POINTER);
2125 value = create_conv(dbgi, value, mode);
2126 set_value_for_expression(expression->left, value);
2131 static ir_node *create_arithmetic_assign_shift(
2132 const binary_expression_t *expression)
2134 ir_node * value = create_shift(expression);
2135 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2136 type_t *const type = expression->base.type;
2137 ir_mode *const mode = get_ir_mode(type);
2139 value = create_conv(dbgi, value, mode);
2140 set_value_for_expression(expression->left, value);
2145 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2147 expression_kind_t kind = expression->base.kind;
2150 case EXPR_BINARY_EQUAL:
2151 case EXPR_BINARY_NOTEQUAL:
2152 case EXPR_BINARY_LESS:
2153 case EXPR_BINARY_LESSEQUAL:
2154 case EXPR_BINARY_GREATER:
2155 case EXPR_BINARY_GREATEREQUAL:
2156 case EXPR_BINARY_ISGREATER:
2157 case EXPR_BINARY_ISGREATEREQUAL:
2158 case EXPR_BINARY_ISLESS:
2159 case EXPR_BINARY_ISLESSEQUAL:
2160 case EXPR_BINARY_ISLESSGREATER:
2161 case EXPR_BINARY_ISUNORDERED: {
2162 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2163 ir_node *left = expression_to_firm(expression->left);
2164 ir_node *right = expression_to_firm(expression->right);
2165 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2166 long pnc = get_pnc(kind, expression->left->base.type);
2167 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2170 case EXPR_BINARY_ASSIGN: {
2171 ir_node *right = expression_to_firm(expression->right);
2172 set_value_for_expression(expression->left, right);
2176 case EXPR_BINARY_ADD:
2177 return create_add(expression);
2178 case EXPR_BINARY_SUB:
2179 return create_sub(expression);
2180 case EXPR_BINARY_MUL:
2181 return create_arithmetic_binop(expression, new_d_Mul);
2182 case EXPR_BINARY_BITWISE_AND:
2183 return create_arithmetic_binop(expression, new_d_And);
2184 case EXPR_BINARY_BITWISE_OR:
2185 return create_arithmetic_binop(expression, new_d_Or);
2186 case EXPR_BINARY_BITWISE_XOR:
2187 return create_arithmetic_binop(expression, new_d_Eor);
2188 case EXPR_BINARY_SHIFTLEFT:
2189 case EXPR_BINARY_SHIFTRIGHT:
2190 return create_shift(expression);
2191 case EXPR_BINARY_DIV:
2192 case EXPR_BINARY_MOD:
2193 return create_divmod(expression);
2194 case EXPR_BINARY_LOGICAL_AND:
2195 case EXPR_BINARY_LOGICAL_OR:
2196 return create_lazy_op(expression);
2197 case EXPR_BINARY_COMMA:
2198 expression_to_firm(expression->left);
2199 return expression_to_firm(expression->right);
2200 case EXPR_BINARY_ADD_ASSIGN:
2201 return create_arithmetic_assign_binop(expression, new_d_Add);
2202 case EXPR_BINARY_SUB_ASSIGN:
2203 return create_arithmetic_assign_binop(expression, new_d_Sub);
2204 case EXPR_BINARY_MUL_ASSIGN:
2205 return create_arithmetic_assign_binop(expression, new_d_Mul);
2206 case EXPR_BINARY_MOD_ASSIGN:
2207 case EXPR_BINARY_DIV_ASSIGN:
2208 return create_arithmetic_assign_divmod(expression);
2209 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2210 return create_arithmetic_assign_binop(expression, new_d_And);
2211 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2212 return create_arithmetic_assign_binop(expression, new_d_Or);
2213 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2214 return create_arithmetic_assign_binop(expression, new_d_Eor);
2215 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2216 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2217 return create_arithmetic_assign_shift(expression);
2218 case EXPR_BINARY_BUILTIN_EXPECT:
2219 return expression_to_firm(expression->left);
2221 panic("TODO binexpr type");
2225 static ir_node *array_access_addr(const array_access_expression_t *expression)
2227 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2228 ir_node *base_addr = expression_to_firm(expression->array_ref);
2229 ir_node *offset = expression_to_firm(expression->index);
2230 offset = create_conv(dbgi, offset, mode_uint);
2232 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2233 assert(is_type_pointer(ref_type));
2234 pointer_type_t *pointer_type = &ref_type->pointer;
2236 unsigned elem_size = get_type_size(pointer_type->points_to);
2237 ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
2238 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2240 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2245 static ir_node *array_access_to_firm(
2246 const array_access_expression_t *expression)
2248 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2249 ir_node *addr = array_access_addr(expression);
2250 type_t *type = revert_automatic_type_conversion(
2251 (const expression_t*) expression);
2252 type = skip_typeref(type);
2253 ir_type *irtype = get_ir_type(type);
2255 return deref_address(irtype, addr, dbgi);
2258 static long get_offsetof_offset(const offsetof_expression_t *expression)
2260 type_t *orig_type = expression->type;
2263 designator_t *designator = expression->designator;
2264 for( ; designator != NULL; designator = designator->next) {
2265 type_t *type = skip_typeref(orig_type);
2266 /* be sure the type is constructed */
2267 (void) get_ir_type(type);
2269 if(designator->symbol != NULL) {
2270 assert(is_type_compound(type));
2271 symbol_t *symbol = designator->symbol;
2273 declaration_t *declaration = type->compound.declaration;
2274 declaration_t *iter = declaration->scope.declarations;
2275 for( ; iter != NULL; iter = iter->next) {
2276 if(iter->symbol == symbol) {
2280 assert(iter != NULL);
2282 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2283 offset += get_entity_offset(iter->v.entity);
2285 orig_type = iter->type;
2287 expression_t *array_index = designator->array_index;
2288 assert(designator->array_index != NULL);
2289 assert(is_type_array(type));
2290 assert(is_type_valid(array_index->base.type));
2292 long index = fold_constant(array_index);
2293 ir_type *arr_type = get_ir_type(type);
2294 ir_type *elem_type = get_array_element_type(arr_type);
2295 long elem_size = get_type_size_bytes(elem_type);
2297 offset += index * elem_size;
2299 orig_type = type->array.element_type;
2306 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2308 ir_mode *mode = get_ir_mode(expression->base.type);
2309 long offset = get_offsetof_offset(expression);
2310 tarval *tv = new_tarval_from_long(offset, mode);
2311 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2313 return new_d_Const(dbgi, mode, tv);
2316 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2317 ir_entity *entity, type_t *type);
2319 static ir_node *compound_literal_to_firm(
2320 const compound_literal_expression_t *expression)
2322 type_t *type = expression->type;
2324 /* create an entity on the stack */
2325 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2327 ident *const id = unique_ident("CompLit");
2328 ir_type *const irtype = get_ir_type(type);
2329 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2330 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2331 set_entity_ld_ident(entity, id);
2333 set_entity_variability(entity, variability_uninitialized);
2335 /* create initialisation code */
2336 initializer_t *initializer = expression->initializer;
2337 create_local_initializer(initializer, dbgi, entity, type);
2339 /* create a sel for the compound literal address */
2340 ir_node *frame = get_local_frame(entity);
2341 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2346 * Transform a sizeof expression into Firm code.
2348 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2350 type_t *type = expression->type;
2352 type = expression->tp_expression->base.type;
2353 assert(type != NULL);
2356 ir_mode *const mode = get_ir_mode(expression->base.type);
2357 symconst_symbol sym;
2358 sym.type_p = get_ir_type(type);
2359 return new_SymConst(mode, sym, symconst_type_size);
2363 * Transform an alignof expression into Firm code.
2365 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2367 type_t *type = expression->type;
2369 /* beware: if expression is a variable reference, return the
2370 alignment of the variable. */
2371 const expression_t *tp_expression = expression->tp_expression;
2372 const declaration_t *declaration = expr_is_variable(tp_expression);
2373 if (declaration != NULL) {
2374 /* TODO: get the alignment of this variable. */
2376 type = tp_expression->base.type;
2377 assert(type != NULL);
2380 ir_mode *const mode = get_ir_mode(expression->base.type);
2381 symconst_symbol sym;
2382 sym.type_p = get_ir_type(type);
2383 return new_SymConst(mode, sym, symconst_type_align);
2386 static void init_ir_types(void);
2387 long fold_constant(const expression_t *expression)
2391 assert(is_constant_expression(expression));
2393 ir_graph *old_current_ir_graph = current_ir_graph;
2394 if(current_ir_graph == NULL) {
2395 current_ir_graph = get_const_code_irg();
2398 ir_node *cnst = expression_to_firm(expression);
2399 current_ir_graph = old_current_ir_graph;
2401 if(!is_Const(cnst)) {
2402 panic("couldn't fold constant\n");
2405 tarval *tv = get_Const_tarval(cnst);
2406 if(!tarval_is_long(tv)) {
2407 panic("result of constant folding is not integer\n");
2410 return get_tarval_long(tv);
2413 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2415 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2417 /* first try to fold a constant condition */
2418 if(is_constant_expression(expression->condition)) {
2419 long val = fold_constant(expression->condition);
2421 return expression_to_firm(expression->true_expression);
2423 return expression_to_firm(expression->false_expression);
2427 ir_node *cur_block = get_cur_block();
2429 /* create the true block */
2430 ir_node *true_block = new_immBlock();
2432 ir_node *true_val = expression_to_firm(expression->true_expression);
2433 ir_node *true_jmp = new_Jmp();
2435 /* create the false block */
2436 ir_node *false_block = new_immBlock();
2438 ir_node *false_val = expression_to_firm(expression->false_expression);
2439 ir_node *false_jmp = new_Jmp();
2441 /* create the condition evaluation */
2442 set_cur_block(cur_block);
2443 create_condition_evaluation(expression->condition, true_block, false_block);
2444 mature_immBlock(true_block);
2445 mature_immBlock(false_block);
2447 /* create the common block */
2448 ir_node *common_block = new_immBlock();
2449 add_immBlock_pred(common_block, true_jmp);
2450 add_immBlock_pred(common_block, false_jmp);
2451 mature_immBlock(common_block);
2453 /* TODO improve static semantics, so either both or no values are NULL */
2454 if (true_val == NULL || false_val == NULL)
2457 ir_node *in[2] = { true_val, false_val };
2458 ir_mode *mode = get_irn_mode(true_val);
2459 assert(get_irn_mode(false_val) == mode);
2460 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2465 static ir_node *select_addr(const select_expression_t *expression)
2467 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2469 ir_node *compound_addr = expression_to_firm(expression->compound);
2471 declaration_t *entry = expression->compound_entry;
2472 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2473 ir_entity *entity = entry->v.entity;
2475 assert(entity != NULL);
2477 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2482 static ir_node *select_to_firm(const select_expression_t *expression)
2484 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2485 ir_node *addr = select_addr(expression);
2486 type_t *type = revert_automatic_type_conversion(
2487 (const expression_t*) expression);
2488 type = skip_typeref(type);
2489 ir_type *irtype = get_ir_type(type);
2491 return deref_address(irtype, addr, dbgi);
2494 /* Values returned by __builtin_classify_type. */
2495 typedef enum gcc_type_class
2501 enumeral_type_class,
2504 reference_type_class,
2508 function_type_class,
2519 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2521 const type_t *const type = expr->type_expression->base.type;
2527 const atomic_type_t *const atomic_type = &type->atomic;
2528 switch (atomic_type->akind) {
2529 /* should not be reached */
2530 case ATOMIC_TYPE_INVALID:
2534 /* gcc cannot do that */
2535 case ATOMIC_TYPE_VOID:
2536 tc = void_type_class;
2539 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2540 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2541 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2542 case ATOMIC_TYPE_SHORT:
2543 case ATOMIC_TYPE_USHORT:
2544 case ATOMIC_TYPE_INT:
2545 case ATOMIC_TYPE_UINT:
2546 case ATOMIC_TYPE_LONG:
2547 case ATOMIC_TYPE_ULONG:
2548 case ATOMIC_TYPE_LONGLONG:
2549 case ATOMIC_TYPE_ULONGLONG:
2550 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2551 tc = integer_type_class;
2554 case ATOMIC_TYPE_FLOAT:
2555 case ATOMIC_TYPE_DOUBLE:
2556 case ATOMIC_TYPE_LONG_DOUBLE:
2557 tc = real_type_class;
2560 #ifdef PROVIDE_COMPLEX
2561 case ATOMIC_TYPE_FLOAT_COMPLEX:
2562 case ATOMIC_TYPE_DOUBLE_COMPLEX:
2563 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
2564 tc = complex_type_class;
2566 case ATOMIC_TYPE_FLOAT_IMAGINARY:
2567 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
2568 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
2569 tc = complex_type_class;
2574 panic("Unimplemented case in classify_type_to_firm().");
2579 case TYPE_ARRAY: /* gcc handles this as pointer */
2580 case TYPE_FUNCTION: /* gcc handles this as pointer */
2581 case TYPE_POINTER: tc = pointer_type_class; break;
2582 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
2583 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
2585 /* gcc handles this as integer */
2586 case TYPE_ENUM: tc = integer_type_class; break;
2589 panic("Unimplemented case in classify_type_to_firm().");
2592 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2593 ir_mode *const mode = mode_int;
2594 tarval *const tv = new_tarval_from_long(tc, mode);
2595 return new_d_Const(dbgi, mode, tv);
2598 static ir_node *function_name_to_firm(
2599 const string_literal_expression_t *const expr)
2601 if (current_function_name == NULL) {
2602 const source_position_t *const src_pos = &expr->base.source_position;
2603 const char *const name = current_function_decl->symbol->string;
2604 const string_t string = { name, strlen(name) + 1 };
2605 current_function_name = string_to_firm(src_pos, "__func__", &string);
2608 return current_function_name;
2611 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2613 statement_t *statement = expr->statement;
2615 assert(statement->kind == STATEMENT_COMPOUND);
2616 return compound_statement_to_firm(&statement->compound);
2619 static ir_node *va_start_expression_to_firm(
2620 const va_start_expression_t *const expr)
2622 ir_type *const method_type = get_ir_type(current_function_decl->type);
2623 int const n = get_method_n_params(method_type) - 1;
2624 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2625 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2626 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2627 ir_node *const no_mem = new_NoMem();
2628 ir_node *const arg_sel =
2629 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2631 size_t const parm_size = get_type_size(expr->parameter->type);
2632 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2633 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2634 set_value_for_expression(expr->ap, add);
2639 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2641 ir_type *const irtype = get_ir_type(expr->base.type);
2642 ir_node *const ap = expression_to_firm(expr->ap);
2643 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2644 ir_node *const res = deref_address(irtype, ap, dbgi);
2646 size_t const parm_size = get_type_size(expr->base.type);
2647 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2648 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2649 set_value_for_expression(expr->ap, add);
2654 static ir_node *dereference_addr(const unary_expression_t *const expression)
2656 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2657 return expression_to_firm(expression->value);
2660 static ir_node *expression_to_addr(const expression_t *expression)
2662 switch(expression->kind) {
2663 case EXPR_REFERENCE:
2664 return reference_addr(&expression->reference);
2665 case EXPR_ARRAY_ACCESS:
2666 return array_access_addr(&expression->array_access);
2668 return select_addr(&expression->select);
2670 return call_expression_to_firm(&expression->call);
2671 case EXPR_UNARY_DEREFERENCE: {
2672 return dereference_addr(&expression->unary);
2677 panic("trying to get address of non-lvalue");
2680 static ir_node *builtin_constant_to_firm(
2681 const builtin_constant_expression_t *expression)
2683 ir_mode *mode = get_ir_mode(expression->base.type);
2686 if (is_constant_expression(expression->value)) {
2691 return new_Const_long(mode, v);
2694 static ir_node *builtin_prefetch_to_firm(
2695 const builtin_prefetch_expression_t *expression)
2697 ir_node *adr = expression_to_firm(expression->adr);
2698 /* no Firm support for prefetch yet */
2703 static ir_node *_expression_to_firm(const expression_t *expression)
2705 switch(expression->kind) {
2706 case EXPR_CHAR_CONST:
2707 return char_const_to_firm(&expression->conste);
2709 return const_to_firm(&expression->conste);
2710 case EXPR_STRING_LITERAL:
2711 return string_literal_to_firm(&expression->string);
2712 case EXPR_WIDE_STRING_LITERAL:
2713 return wide_string_literal_to_firm(&expression->wide_string);
2714 case EXPR_REFERENCE:
2715 return reference_expression_to_firm(&expression->reference);
2717 return call_expression_to_firm(&expression->call);
2719 return unary_expression_to_firm(&expression->unary);
2721 return binary_expression_to_firm(&expression->binary);
2722 case EXPR_ARRAY_ACCESS:
2723 return array_access_to_firm(&expression->array_access);
2725 return sizeof_to_firm(&expression->typeprop);
2727 return alignof_to_firm(&expression->typeprop);
2728 case EXPR_CONDITIONAL:
2729 return conditional_to_firm(&expression->conditional);
2731 return select_to_firm(&expression->select);
2732 case EXPR_CLASSIFY_TYPE:
2733 return classify_type_to_firm(&expression->classify_type);
2735 case EXPR_PRETTY_FUNCTION:
2736 return function_name_to_firm(&expression->string);
2737 case EXPR_STATEMENT:
2738 return statement_expression_to_firm(&expression->statement);
2740 return va_start_expression_to_firm(&expression->va_starte);
2742 return va_arg_expression_to_firm(&expression->va_arge);
2743 case EXPR_BUILTIN_SYMBOL:
2744 panic("unimplemented expression found");
2745 case EXPR_BUILTIN_CONSTANT_P:
2746 return builtin_constant_to_firm(&expression->builtin_constant);
2747 case EXPR_BUILTIN_PREFETCH:
2748 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2750 return offsetof_to_firm(&expression->offsetofe);
2751 case EXPR_COMPOUND_LITERAL:
2752 return compound_literal_to_firm(&expression->compound_literal);
2758 panic("invalid expression found");
2761 static ir_node *expression_to_firm(const expression_t *expression)
2763 ir_node *res = _expression_to_firm(expression);
2765 if(res != NULL && get_irn_mode(res) == mode_b) {
2766 ir_mode *mode = get_ir_mode(expression->base.type);
2767 res = create_conv(NULL, res, mode);
2773 static ir_node *expression_to_modeb(const expression_t *expression)
2775 ir_node *res = _expression_to_firm(expression);
2776 res = create_conv(NULL, res, mode_b);
2782 * create a short-circuit expression evaluation that tries to construct
2783 * efficient control flow structures for &&, || and ! expressions
2785 static void create_condition_evaluation(const expression_t *expression,
2786 ir_node *true_block,
2787 ir_node *false_block)
2789 switch(expression->kind) {
2790 case EXPR_UNARY_NOT: {
2791 const unary_expression_t *unary_expression = &expression->unary;
2792 create_condition_evaluation(unary_expression->value, false_block,
2796 case EXPR_BINARY_LOGICAL_AND: {
2797 const binary_expression_t *binary_expression = &expression->binary;
2799 ir_node *cur_block = get_cur_block();
2800 ir_node *extra_block = new_immBlock();
2801 set_cur_block(cur_block);
2802 create_condition_evaluation(binary_expression->left, extra_block,
2804 mature_immBlock(extra_block);
2805 set_cur_block(extra_block);
2806 create_condition_evaluation(binary_expression->right, true_block,
2810 case EXPR_BINARY_LOGICAL_OR: {
2811 const binary_expression_t *binary_expression = &expression->binary;
2813 ir_node *cur_block = get_cur_block();
2814 ir_node *extra_block = new_immBlock();
2815 set_cur_block(cur_block);
2816 create_condition_evaluation(binary_expression->left, true_block,
2818 mature_immBlock(extra_block);
2819 set_cur_block(extra_block);
2820 create_condition_evaluation(binary_expression->right, true_block,
2828 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2829 ir_node *condition = expression_to_modeb(expression);
2830 ir_node *cond = new_d_Cond(dbgi, condition);
2831 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2832 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2834 /* set branch prediction info based on __builtin_expect */
2835 if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
2836 long cnst = fold_constant(expression->binary.right);
2837 cond_jmp_predicate pred;
2840 pred = COND_JMP_PRED_FALSE;
2842 pred = COND_JMP_PRED_TRUE;
2844 set_Cond_jmp_pred(cond, pred);
2847 add_immBlock_pred(true_block, true_proj);
2848 add_immBlock_pred(false_block, false_proj);
2850 set_cur_block(NULL);
2855 static void create_declaration_entity(declaration_t *declaration,
2856 declaration_kind_t declaration_kind,
2857 ir_type *parent_type)
2859 ident *const id = new_id_from_str(declaration->symbol->string);
2860 ir_type *const irtype = get_ir_type(declaration->type);
2861 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2862 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2863 set_entity_ld_ident(entity, id);
2865 declaration->declaration_kind = (unsigned char) declaration_kind;
2866 declaration->v.entity = entity;
2867 set_entity_variability(entity, variability_uninitialized);
2868 if(parent_type == get_tls_type())
2869 set_entity_allocation(entity, allocation_automatic);
2870 else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
2871 set_entity_allocation(entity, allocation_static);
2872 /* TODO: visibility? */
2876 typedef struct type_path_entry_t type_path_entry_t;
2877 struct type_path_entry_t {
2879 ir_initializer_t *initializer;
2881 declaration_t *compound_entry;
2884 typedef struct type_path_t type_path_t;
2885 struct type_path_t {
2886 type_path_entry_t *path;
2891 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
2893 size_t len = ARR_LEN(path->path);
2895 for(size_t i = 0; i < len; ++i) {
2896 const type_path_entry_t *entry = & path->path[i];
2898 type_t *type = skip_typeref(entry->type);
2899 if(is_type_compound(type)) {
2900 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
2901 } else if(is_type_array(type)) {
2902 fprintf(stderr, "[%u]", entry->index);
2904 fprintf(stderr, "-INVALID-");
2907 fprintf(stderr, " (");
2908 print_type(path->top_type);
2909 fprintf(stderr, ")");
2912 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2914 size_t len = ARR_LEN(path->path);
2916 return & path->path[len-1];
2919 static type_path_entry_t *append_to_type_path(type_path_t *path)
2921 size_t len = ARR_LEN(path->path);
2922 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2924 type_path_entry_t *result = & path->path[len];
2925 memset(result, 0, sizeof(result[0]));
2929 static size_t get_compound_size(const compound_type_t *type)
2931 declaration_t *declaration = type->declaration;
2932 declaration_t *member = declaration->scope.declarations;
2934 for( ; member != NULL; member = member->next) {
2937 /* TODO: cache results? */
2942 static ir_initializer_t *get_initializer_entry(type_path_t *path)
2944 type_t *orig_top_type = path->top_type;
2945 type_t *top_type = skip_typeref(orig_top_type);
2947 assert(is_type_compound(top_type) || is_type_array(top_type));
2949 if(ARR_LEN(path->path) == 0) {
2952 type_path_entry_t *top = get_type_path_top(path);
2953 ir_initializer_t *initializer = top->initializer;
2954 return get_initializer_compound_value(initializer, top->index);
2958 static void descend_into_subtype(type_path_t *path)
2960 type_t *orig_top_type = path->top_type;
2961 type_t *top_type = skip_typeref(orig_top_type);
2963 assert(is_type_compound(top_type) || is_type_array(top_type));
2965 ir_initializer_t *initializer = get_initializer_entry(path);
2967 type_path_entry_t *top = append_to_type_path(path);
2968 top->type = top_type;
2972 if(is_type_compound(top_type)) {
2973 declaration_t *declaration = top_type->compound.declaration;
2974 declaration_t *entry = declaration->scope.declarations;
2976 top->compound_entry = entry;
2978 path->top_type = entry->type;
2979 len = get_compound_size(&top_type->compound);
2981 assert(is_type_array(top_type));
2982 assert(top_type->array.size > 0);
2985 path->top_type = top_type->array.element_type;
2986 len = top_type->array.size;
2988 if(initializer == NULL
2989 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
2990 initializer = create_initializer_compound(len);
2991 /* we have to set the entry at the 2nd latest path entry... */
2992 size_t path_len = ARR_LEN(path->path);
2993 assert(path_len >= 1);
2995 type_path_entry_t *entry = & path->path[path_len-2];
2996 ir_initializer_t *tinitializer = entry->initializer;
2997 set_initializer_compound_value(tinitializer, entry->index,
3001 top->initializer = initializer;
3004 static void ascend_from_subtype(type_path_t *path)
3006 type_path_entry_t *top = get_type_path_top(path);
3008 path->top_type = top->type;
3010 size_t len = ARR_LEN(path->path);
3011 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3014 static void walk_designator(type_path_t *path, const designator_t *designator)
3016 /* designators start at current object type */
3017 ARR_RESIZE(type_path_entry_t, path->path, 1);
3019 for( ; designator != NULL; designator = designator->next) {
3020 type_path_entry_t *top = get_type_path_top(path);
3021 type_t *orig_type = top->type;
3022 type_t *type = skip_typeref(orig_type);
3024 if(designator->symbol != NULL) {
3025 assert(is_type_compound(type));
3027 symbol_t *symbol = designator->symbol;
3029 declaration_t *declaration = type->compound.declaration;
3030 declaration_t *iter = declaration->scope.declarations;
3031 for( ; iter != NULL; iter = iter->next, ++index) {
3032 if(iter->symbol == symbol) {
3036 assert(iter != NULL);
3038 top->type = orig_type;
3039 top->compound_entry = iter;
3041 orig_type = iter->type;
3043 expression_t *array_index = designator->array_index;
3044 assert(designator->array_index != NULL);
3045 assert(is_type_array(type));
3046 assert(is_type_valid(array_index->base.type));
3048 long index = fold_constant(array_index);
3051 if(type->array.size_constant == 1) {
3052 long array_size = type->array.size;
3053 assert(index < array_size);
3057 top->type = orig_type;
3058 top->index = (size_t) index;
3059 orig_type = type->array.element_type;
3061 path->top_type = orig_type;
3063 if(designator->next != NULL) {
3064 descend_into_subtype(path);
3068 path->invalid = false;
3071 static void advance_current_object(type_path_t *path)
3074 /* TODO: handle this... */
3075 panic("invalid initializer in ast2firm (excessive elements)");
3079 type_path_entry_t *top = get_type_path_top(path);
3081 type_t *type = skip_typeref(top->type);
3082 if(is_type_union(type)) {
3083 top->compound_entry = NULL;
3084 } else if(is_type_struct(type)) {
3085 declaration_t *entry = top->compound_entry;
3088 entry = entry->next;
3089 top->compound_entry = entry;
3091 path->top_type = entry->type;
3095 assert(is_type_array(type));
3098 if(!type->array.size_constant || top->index < type->array.size) {
3103 /* we're past the last member of the current sub-aggregate, try if we
3104 * can ascend in the type hierarchy and continue with another subobject */
3105 size_t len = ARR_LEN(path->path);
3108 ascend_from_subtype(path);
3109 advance_current_object(path);
3111 path->invalid = true;
3116 static ir_initializer_t *create_ir_initializer(
3117 const initializer_t *initializer, type_t *type);
3119 static ir_initializer_t *create_ir_initializer_value(
3120 const initializer_value_t *initializer)
3122 ir_node *value = expression_to_firm(initializer->value);
3123 return create_initializer_const(value);
3126 static ir_initializer_t *create_ir_initializer_list(
3127 const initializer_list_t *initializer, type_t *type)
3130 memset(&path, 0, sizeof(path));
3131 path.top_type = type;
3132 path.path = NEW_ARR_F(type_path_entry_t, 0);
3134 descend_into_subtype(&path);
3136 for(size_t i = 0; i < initializer->len; ++i) {
3137 const initializer_t *sub_initializer = initializer->initializers[i];
3139 if(sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3140 walk_designator(&path, sub_initializer->designator.designator);
3144 if(sub_initializer->kind == INITIALIZER_VALUE) {
3145 /* we might have to descend into types until we're at a scalar
3148 type_t *orig_top_type = path.top_type;
3149 type_t *top_type = skip_typeref(orig_top_type);
3151 if(is_type_scalar(top_type))
3153 descend_into_subtype(&path);
3157 ir_initializer_t *sub_irinitializer
3158 = create_ir_initializer(sub_initializer, path.top_type);
3160 size_t path_len = ARR_LEN(path.path);
3161 assert(path_len >= 1);
3162 type_path_entry_t *entry = & path.path[path_len-1];
3163 ir_initializer_t *tinitializer = entry->initializer;
3164 set_initializer_compound_value(tinitializer, entry->index,
3167 advance_current_object(&path);
3170 assert(ARR_LEN(path.path) >= 1);
3171 ir_initializer_t *result = path.path[0].initializer;
3172 DEL_ARR_F(path.path);
3177 static ir_initializer_t *create_ir_initializer_string(
3178 const initializer_string_t *initializer, type_t *type)
3180 size_t string_len = initializer->string.size;
3181 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3182 size_t len = type->array.size;
3183 ir_initializer_t *irinitializer = create_initializer_compound(len);
3185 const char *string = initializer->string.begin;
3186 ir_mode *mode = get_type_mode(ir_type_const_char);
3188 for(size_t i = 0; i < len; ++i) {
3193 tarval *tv = new_tarval_from_long(string[i], mode);
3194 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3196 set_initializer_compound_value(irinitializer, i, char_initializer);
3199 return irinitializer;
3202 static ir_initializer_t *create_ir_initializer_wide_string(
3203 const initializer_wide_string_t *initializer, type_t *type)
3205 size_t string_len = initializer->string.size;
3206 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3207 size_t len = type->array.size;
3208 ir_initializer_t *irinitializer = create_initializer_compound(len);
3210 const wchar_rep_t *string = initializer->string.begin;
3211 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3213 for(size_t i = 0; i < len; ++i) {
3215 if(i < string_len) {
3218 tarval *tv = new_tarval_from_long(string[i], mode);
3219 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3221 set_initializer_compound_value(irinitializer, i, char_initializer);
3224 return irinitializer;
3227 static ir_initializer_t *create_ir_initializer(
3228 const initializer_t *initializer, type_t *type)
3230 switch(initializer->kind) {
3231 case INITIALIZER_STRING:
3232 return create_ir_initializer_string(&initializer->string, type);
3234 case INITIALIZER_WIDE_STRING:
3235 return create_ir_initializer_wide_string(&initializer->wide_string,
3238 case INITIALIZER_LIST:
3239 return create_ir_initializer_list(&initializer->list, type);
3241 case INITIALIZER_VALUE:
3242 return create_ir_initializer_value(&initializer->value);
3244 case INITIALIZER_DESIGNATOR:
3245 panic("unexpected designator initializer found");
3247 panic("unknown initializer");
3250 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3251 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3253 switch(get_initializer_kind(initializer)) {
3254 case IR_INITIALIZER_NULL: {
3255 ir_mode *mode = get_type_mode(type);
3256 /* TODO: implement this for compound types... */
3257 assert(type != NULL);
3258 tarval *zero = get_mode_null(mode);
3259 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3261 /* TODO: bitfields */
3262 ir_node *mem = get_store();
3263 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3264 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3268 case IR_INITIALIZER_CONST: {
3269 ir_node *node = get_initializer_const_value(initializer);
3270 ir_mode *mode = get_irn_mode(node);
3271 assert(get_type_mode(type) == mode);
3273 /* TODO: bitfields... */
3274 ir_node *mem = get_store();
3275 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3276 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3280 case IR_INITIALIZER_TARVAL: {
3281 tarval *tv = get_initializer_tarval_value(initializer);
3282 ir_mode *mode = get_tarval_mode(tv);
3283 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3284 assert(get_type_mode(type) == mode);
3286 /* TODO: bitfields... */
3287 ir_node *mem = get_store();
3288 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3289 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3293 case IR_INITIALIZER_COMPOUND: {
3294 assert(is_compound_type(type));
3296 if(is_Array_type(type)) {
3297 assert(has_array_upper_bound(type, 0));
3298 n_members = get_array_upper_bound_int(type, 0);
3300 n_members = get_compound_n_members(type);
3303 if(get_initializer_compound_n_entries(initializer)
3304 != (unsigned) n_members)
3305 panic("initializer doesn't match compound type");
3307 for(int i = 0; i < n_members; ++i) {
3310 if(is_Array_type(type)) {
3311 ir_entity *entity = get_array_element_entity(type);
3312 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3313 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3314 ir_node *in[1] = { cnst };
3315 irtype = get_array_element_type(type);
3316 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3318 ir_entity *member = get_compound_member(type, i);
3320 irtype = get_entity_type(member);
3321 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3324 ir_initializer_t *sub_init
3325 = get_initializer_compound_value(initializer, i);
3327 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3333 panic("invalid IR_INITIALIZER found");
3336 static void create_dynamic_initializer(ir_initializer_t *initializer,
3337 dbg_info *dbgi, ir_entity *entity)
3339 ir_node *frame = get_local_frame(entity);
3340 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3341 ir_type *type = get_entity_type(entity);
3343 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3346 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3347 ir_entity *entity, type_t *type)
3349 ir_node *memory = get_store();
3350 ir_node *nomem = new_NoMem();
3351 ir_node *frame = get_irg_frame(current_ir_graph);
3352 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3354 if(initializer->kind == INITIALIZER_VALUE) {
3355 initializer_value_t *initializer_value = &initializer->value;
3357 ir_node *value = expression_to_firm(initializer_value->value);
3358 type = skip_typeref(type);
3359 assign_value(dbgi, addr, type, value);
3363 if(!is_constant_initializer(initializer)) {
3364 ir_initializer_t *irinitializer
3365 = create_ir_initializer(initializer, type);
3367 create_dynamic_initializer(irinitializer, dbgi, entity);
3371 /* create the ir_initializer */
3372 ir_graph *const old_current_ir_graph = current_ir_graph;
3373 current_ir_graph = get_const_code_irg();
3375 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3377 assert(current_ir_graph == get_const_code_irg());
3378 current_ir_graph = old_current_ir_graph;
3380 /* create a "template" entity which is copied to the entity on the stack */
3381 ident *const id = unique_ident("initializer");
3382 ir_type *const irtype = get_ir_type(type);
3383 ir_type *const global_type = get_glob_type();
3384 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3385 set_entity_ld_ident(init_entity, id);
3387 set_entity_variability(init_entity, variability_initialized);
3388 set_entity_visibility(init_entity, visibility_local);
3389 set_entity_allocation(init_entity, allocation_static);
3391 set_entity_initializer(init_entity, irinitializer);
3393 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3394 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3396 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3397 set_store(copyb_mem);
3400 static void create_initializer_local_variable_entity(declaration_t *declaration)
3402 initializer_t *initializer = declaration->init.initializer;
3403 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3404 ir_entity *entity = declaration->v.entity;
3405 type_t *type = declaration->type;
3406 create_local_initializer(initializer, dbgi, entity, type);
3409 static void create_declaration_initializer(declaration_t *declaration)
3411 initializer_t *initializer = declaration->init.initializer;
3412 if(initializer == NULL)
3415 declaration_kind_t declaration_kind
3416 = (declaration_kind_t) declaration->declaration_kind;
3417 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3418 create_initializer_local_variable_entity(declaration);
3422 if(initializer->kind == INITIALIZER_VALUE) {
3423 initializer_value_t *initializer_value = &initializer->value;
3425 ir_node *value = expression_to_firm(initializer_value->value);
3427 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3428 set_value(declaration->v.value_number, value);
3430 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3432 ir_entity *entity = declaration->v.entity;
3434 set_entity_variability(entity, variability_initialized);
3435 set_atomic_ent_value(entity, value);
3438 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3439 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3441 ir_entity *entity = declaration->v.entity;
3442 ir_initializer_t *irinitializer
3443 = create_ir_initializer(initializer, declaration->type);
3445 set_entity_variability(entity, variability_initialized);
3446 set_entity_initializer(entity, irinitializer);
3451 * Creates a Firm local variable from a declaration.
3453 static void create_local_variable(declaration_t *declaration)
3455 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3457 bool needs_entity = declaration->address_taken;
3458 type_t *type = skip_typeref(declaration->type);
3460 if(is_type_array(type) || is_type_compound(type)) {
3461 needs_entity = true;
3465 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3466 create_declaration_entity(declaration,
3467 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3470 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3471 declaration->v.value_number = next_value_number_function;
3472 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3473 ++next_value_number_function;
3476 create_declaration_initializer(declaration);
3479 static void create_local_static_variable(declaration_t *declaration)
3481 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3483 type_t *const type = skip_typeref(declaration->type);
3484 ir_type *const global_type = get_glob_type();
3485 ident *const id = unique_ident(declaration->symbol->string);
3486 ir_type *const irtype = get_ir_type(type);
3487 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3488 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3489 set_entity_ld_ident(entity, id);
3491 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3492 declaration->v.entity = entity;
3493 set_entity_variability(entity, variability_uninitialized);
3494 set_entity_visibility(entity, visibility_local);
3495 set_entity_allocation(entity, allocation_static);
3497 ir_graph *const old_current_ir_graph = current_ir_graph;
3498 current_ir_graph = get_const_code_irg();
3500 create_declaration_initializer(declaration);
3502 assert(current_ir_graph == get_const_code_irg());
3503 current_ir_graph = old_current_ir_graph;
3508 static void return_statement_to_firm(return_statement_t *statement)
3510 if(get_cur_block() == NULL)
3513 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3514 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3519 if(get_method_n_ress(func_irtype) > 0) {
3520 ir_type *res_type = get_method_res_type(func_irtype, 0);
3522 if(statement->value != NULL) {
3523 ir_node *node = expression_to_firm(statement->value);
3524 node = do_strict_conv(dbgi, node);
3528 if(is_compound_type(res_type)) {
3531 mode = get_type_mode(res_type);
3533 in[0] = new_Unknown(mode);
3537 /* build return_value for its side effects */
3538 if(statement->value != NULL) {
3539 expression_to_firm(statement->value);
3544 ir_node *store = get_store();
3545 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3547 ir_node *end_block = get_irg_end_block(current_ir_graph);
3548 add_immBlock_pred(end_block, ret);
3550 set_cur_block(NULL);
3553 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3555 if(get_cur_block() == NULL)
3558 return expression_to_firm(statement->expression);
3561 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3563 ir_node *result = NULL;
3564 statement_t *statement = compound->statements;
3565 for( ; statement != NULL; statement = statement->base.next) {
3566 //context2firm(&statement->scope);
3568 if(statement->base.next == NULL
3569 && statement->kind == STATEMENT_EXPRESSION) {
3570 result = expression_statement_to_firm(
3571 &statement->expression);
3574 statement_to_firm(statement);
3580 static void create_global_variable(declaration_t *declaration)
3584 switch ((storage_class_tag_t)declaration->storage_class) {
3585 case STORAGE_CLASS_STATIC:
3586 vis = visibility_local;
3589 case STORAGE_CLASS_EXTERN:
3590 vis = visibility_external_allocated;
3593 case STORAGE_CLASS_NONE:
3594 vis = visibility_external_visible;
3597 case STORAGE_CLASS_THREAD:
3598 vis = visibility_external_visible;
3601 case STORAGE_CLASS_THREAD_EXTERN:
3602 vis = visibility_external_allocated;
3605 case STORAGE_CLASS_THREAD_STATIC:
3606 vis = visibility_local;
3610 var_type = get_tls_type();
3614 var_type = get_glob_type();
3618 create_declaration_entity(declaration,
3619 DECLARATION_KIND_GLOBAL_VARIABLE,
3621 set_entity_visibility(declaration->v.entity, vis);
3625 case STORAGE_CLASS_TYPEDEF:
3626 case STORAGE_CLASS_AUTO:
3627 case STORAGE_CLASS_REGISTER:
3628 case STORAGE_CLASS_ENUM_ENTRY:
3631 panic("Invalid storage class for global variable");
3634 static void create_local_declaration(declaration_t *declaration)
3636 if(declaration->symbol == NULL)
3639 type_t *type = skip_typeref(declaration->type);
3641 switch ((storage_class_tag_t) declaration->storage_class) {
3642 case STORAGE_CLASS_STATIC:
3643 create_local_static_variable(declaration);
3645 case STORAGE_CLASS_EXTERN:
3646 create_global_variable(declaration);
3647 create_declaration_initializer(declaration);
3649 case STORAGE_CLASS_NONE:
3650 case STORAGE_CLASS_AUTO:
3651 case STORAGE_CLASS_REGISTER:
3652 if(is_type_function(type)) {
3653 if(declaration->init.statement != NULL) {
3654 panic("nested functions not supported yet");
3656 get_function_entity(declaration);
3659 create_local_variable(declaration);
3662 case STORAGE_CLASS_ENUM_ENTRY:
3663 case STORAGE_CLASS_TYPEDEF:
3664 case STORAGE_CLASS_THREAD:
3665 case STORAGE_CLASS_THREAD_EXTERN:
3666 case STORAGE_CLASS_THREAD_STATIC:
3669 panic("invalid storage class found");
3672 static void declaration_statement_to_firm(declaration_statement_t *statement)
3674 declaration_t *declaration = statement->declarations_begin;
3675 declaration_t *end = statement->declarations_end->next;
3676 for( ; declaration != end; declaration = declaration->next) {
3677 if(declaration->namespc != NAMESPACE_NORMAL)
3679 create_local_declaration(declaration);
3683 static void if_statement_to_firm(if_statement_t *statement)
3685 ir_node *cur_block = get_cur_block();
3687 ir_node *fallthrough_block = new_immBlock();
3689 /* the true (blocks) */
3690 ir_node *true_block;
3691 if (statement->true_statement != NULL) {
3692 true_block = new_immBlock();
3693 statement_to_firm(statement->true_statement);
3694 if(get_cur_block() != NULL) {
3695 ir_node *jmp = new_Jmp();
3696 add_immBlock_pred(fallthrough_block, jmp);
3699 true_block = fallthrough_block;
3702 /* the false (blocks) */
3703 ir_node *false_block;
3704 if(statement->false_statement != NULL) {
3705 false_block = new_immBlock();
3707 statement_to_firm(statement->false_statement);
3708 if(get_cur_block() != NULL) {
3709 ir_node *jmp = new_Jmp();
3710 add_immBlock_pred(fallthrough_block, jmp);
3713 false_block = fallthrough_block;
3716 /* create the condition */
3717 if(cur_block != NULL) {
3718 set_cur_block(cur_block);
3719 create_condition_evaluation(statement->condition, true_block,
3723 mature_immBlock(true_block);
3724 if(false_block != fallthrough_block) {
3725 mature_immBlock(false_block);
3727 mature_immBlock(fallthrough_block);
3729 set_cur_block(fallthrough_block);
3732 static void while_statement_to_firm(while_statement_t *statement)
3734 ir_node *jmp = NULL;
3735 if(get_cur_block() != NULL) {
3739 /* create the header block */
3740 ir_node *header_block = new_immBlock();
3742 add_immBlock_pred(header_block, jmp);
3745 /* the false block */
3746 ir_node *false_block = new_immBlock();
3749 ir_node *body_block;
3750 if (statement->body != NULL) {
3751 ir_node *old_continue_label = continue_label;
3752 ir_node *old_break_label = break_label;
3753 continue_label = header_block;
3754 break_label = false_block;
3756 body_block = new_immBlock();
3757 statement_to_firm(statement->body);
3759 assert(continue_label == header_block);
3760 assert(break_label == false_block);
3761 continue_label = old_continue_label;
3762 break_label = old_break_label;
3764 if(get_cur_block() != NULL) {
3766 add_immBlock_pred(header_block, jmp);
3769 body_block = header_block;
3772 /* create the condition */
3773 set_cur_block(header_block);
3775 create_condition_evaluation(statement->condition, body_block, false_block);
3776 mature_immBlock(body_block);
3777 mature_immBlock(false_block);
3778 mature_immBlock(header_block);
3780 set_cur_block(false_block);
3783 static void do_while_statement_to_firm(do_while_statement_t *statement)
3785 ir_node *jmp = NULL;
3786 if(get_cur_block() != NULL) {
3790 /* create the header block */
3791 ir_node *header_block = new_immBlock();
3793 /* the false block */
3794 ir_node *false_block = new_immBlock();
3797 ir_node *body_block = new_immBlock();
3799 add_immBlock_pred(body_block, jmp);
3802 if (statement->body != NULL) {
3803 ir_node *old_continue_label = continue_label;
3804 ir_node *old_break_label = break_label;
3805 continue_label = header_block;
3806 break_label = false_block;
3808 statement_to_firm(statement->body);
3810 assert(continue_label == header_block);
3811 assert(break_label == false_block);
3812 continue_label = old_continue_label;
3813 break_label = old_break_label;
3815 if (get_cur_block() == NULL) {
3816 mature_immBlock(header_block);
3817 mature_immBlock(body_block);
3818 mature_immBlock(false_block);
3823 ir_node *body_jmp = new_Jmp();
3824 add_immBlock_pred(header_block, body_jmp);
3825 mature_immBlock(header_block);
3827 /* create the condition */
3828 set_cur_block(header_block);
3830 create_condition_evaluation(statement->condition, body_block, false_block);
3831 mature_immBlock(body_block);
3832 mature_immBlock(false_block);
3833 mature_immBlock(header_block);
3835 set_cur_block(false_block);
3838 static void for_statement_to_firm(for_statement_t *statement)
3840 ir_node *jmp = NULL;
3841 if (get_cur_block() != NULL) {
3842 if(statement->initialisation != NULL) {
3843 expression_to_firm(statement->initialisation);
3846 /* create declarations */
3847 declaration_t *declaration = statement->scope.declarations;
3848 for( ; declaration != NULL; declaration = declaration->next) {
3849 create_local_declaration(declaration);
3856 /* create the step block */
3857 ir_node *const step_block = new_immBlock();
3858 if (statement->step != NULL) {
3859 expression_to_firm(statement->step);
3861 ir_node *const step_jmp = new_Jmp();
3863 /* create the header block */
3864 ir_node *const header_block = new_immBlock();
3866 add_immBlock_pred(header_block, jmp);
3868 add_immBlock_pred(header_block, step_jmp);
3870 /* the false block */
3871 ir_node *const false_block = new_immBlock();
3874 ir_node * body_block;
3875 if (statement->body != NULL) {
3876 ir_node *const old_continue_label = continue_label;
3877 ir_node *const old_break_label = break_label;
3878 continue_label = step_block;
3879 break_label = false_block;
3881 body_block = new_immBlock();
3882 statement_to_firm(statement->body);
3884 assert(continue_label == step_block);
3885 assert(break_label == false_block);
3886 continue_label = old_continue_label;
3887 break_label = old_break_label;
3889 if (get_cur_block() != NULL) {
3891 add_immBlock_pred(step_block, jmp);
3894 body_block = step_block;
3897 /* create the condition */
3898 set_cur_block(header_block);
3899 if (statement->condition != NULL) {
3900 create_condition_evaluation(statement->condition, body_block,
3903 keep_alive(header_block);
3905 add_immBlock_pred(body_block, jmp);
3908 mature_immBlock(body_block);
3909 mature_immBlock(false_block);
3910 mature_immBlock(step_block);
3911 mature_immBlock(header_block);
3912 mature_immBlock(false_block);
3914 set_cur_block(false_block);
3917 static void create_jump_statement(const statement_t *statement,
3918 ir_node *target_block)
3920 if(get_cur_block() == NULL)
3923 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3924 ir_node *jump = new_d_Jmp(dbgi);
3925 add_immBlock_pred(target_block, jump);
3927 set_cur_block(NULL);
3930 static void switch_statement_to_firm(const switch_statement_t *statement)
3932 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3934 ir_node *expression = expression_to_firm(statement->expression);
3935 ir_node *cond = new_d_Cond(dbgi, expression);
3936 ir_node *break_block = new_immBlock();
3938 set_cur_block(NULL);
3940 ir_node *const old_switch_cond = current_switch_cond;
3941 ir_node *const old_break_label = break_label;
3942 const bool old_saw_default_label = saw_default_label;
3943 current_switch_cond = cond;
3944 break_label = break_block;
3946 if (statement->body != NULL) {
3947 statement_to_firm(statement->body);
3950 if(get_cur_block() != NULL) {
3951 ir_node *jmp = new_Jmp();
3952 add_immBlock_pred(break_block, jmp);
3955 if (!saw_default_label) {
3956 set_cur_block(get_nodes_block(cond));
3957 ir_node *const proj = new_d_defaultProj(dbgi, cond,
3958 MAGIC_DEFAULT_PN_NUMBER);
3959 add_immBlock_pred(break_block, proj);
3962 assert(current_switch_cond == cond);
3963 assert(break_label == break_block);
3964 current_switch_cond = old_switch_cond;
3965 break_label = old_break_label;
3966 saw_default_label = old_saw_default_label;
3968 mature_immBlock(break_block);
3969 set_cur_block(break_block);
3972 static void case_label_to_firm(const case_label_statement_t *statement)
3974 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3976 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
3978 /* let's create a node and hope firm constant folding creates a Const
3981 ir_node *old_block = get_nodes_block(current_switch_cond);
3982 ir_node *block = new_immBlock();
3984 set_cur_block(old_block);
3985 if(statement->expression != NULL) {
3986 long start_pn = fold_constant(statement->expression);
3987 long end_pn = start_pn;
3988 if (statement->end_range != NULL) {
3989 end_pn = fold_constant(statement->end_range);
3991 assert(start_pn <= end_pn);
3992 /* create jumps for all cases in the given range */
3993 for (long pn = start_pn; pn <= end_pn; ++pn) {
3994 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
3995 /* oops someone detected our cheating... */
3996 panic("magic default pn used");
3998 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
3999 add_immBlock_pred(block, proj);
4002 saw_default_label = true;
4003 proj = new_d_defaultProj(dbgi, current_switch_cond,
4004 MAGIC_DEFAULT_PN_NUMBER);
4006 add_immBlock_pred(block, proj);
4009 if (fallthrough != NULL) {
4010 add_immBlock_pred(block, fallthrough);
4012 mature_immBlock(block);
4013 set_cur_block(block);
4015 if(statement->statement != NULL) {
4016 statement_to_firm(statement->statement);
4020 static ir_node *get_label_block(declaration_t *label)
4022 assert(label->namespc == NAMESPACE_LABEL);
4024 if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4025 return label->v.block;
4027 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4029 ir_node *old_cur_block = get_cur_block();
4030 ir_node *block = new_immBlock();
4031 set_cur_block(old_cur_block);
4033 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4034 label->v.block = block;
4036 ARR_APP1(ir_node *, imature_blocks, block);
4041 static void label_to_firm(const label_statement_t *statement)
4043 ir_node *block = get_label_block(statement->label);
4045 if(get_cur_block() != NULL) {
4046 ir_node *jmp = new_Jmp();
4047 add_immBlock_pred(block, jmp);
4050 set_cur_block(block);
4053 if(statement->statement != NULL) {
4054 statement_to_firm(statement->statement);
4058 static void goto_to_firm(const goto_statement_t *statement)
4060 if(get_cur_block() == NULL)
4063 ir_node *block = get_label_block(statement->label);
4064 ir_node *jmp = new_Jmp();
4065 add_immBlock_pred(block, jmp);
4067 set_cur_block(NULL);
4070 typedef enum modifier_t {
4071 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
4072 ASM_MODIFIER_READ_WRITE = 1 << 1,
4073 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
4074 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
4077 static void asm_statement_to_firm(const asm_statement_t *statement)
4080 fprintf(stderr, "WARNING asm not implemented yet!\n");
4082 bool needs_memory = false;
4084 size_t n_clobbers = 0;
4085 asm_clobber_t *clobber = statement->clobbers;
4086 for( ; clobber != NULL; clobber = clobber->next) {
4087 if(strcmp(clobber->clobber, "memory") == 0) {
4088 needs_memory = true;
4092 ident *id = new_id_from_str(clobber->clobber);
4093 obstack_ptr_grow(&asm_obst, id);
4096 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4097 ident **clobbers = NULL;
4098 if(n_clobbers > 0) {
4099 clobbers = obstack_finish(&asm_obst);
4102 /* find and count input and output constraints */
4103 asm_constraint_t *constraint = statement->inputs;
4104 for( ; constraint != NULL; constraint = constraint->next) {
4106 bool supports_memop = false;
4107 for(const char *c = constraint->constraints; *c != 0; ++c) {
4108 /* TODO: improve error messages */
4112 panic("multiple alternative assembler constraints not "
4120 supports_memop = true;
4121 obstack_1grow(&asm_obst, *c);
4124 if(modifiers & ASM_MODIFIER_READ_WRITE)
4125 panic("inconsistent register constraints");
4126 modifiers |= ASM_MODIFIER_WRITE_ONLY;
4129 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
4130 panic("inconsistent register constraints");
4131 modifiers |= ASM_MODIFIER_READ_WRITE;
4134 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
4135 panic("early clobber assembler constraint not supported yet");
4138 modifiers |= ASM_MODIFIER_COMMUTATIVE;
4139 panic("commutative assembler constraint not supported yet");
4142 /* skip register preferences stuff... */
4143 while(*c != 0 && *c != ',')
4147 /* skip register preferences stuff... */
4151 obstack_1grow(&asm_obst, *c);
4155 obstack_1grow(&asm_obst, '\0');
4156 const char *constraint_string = obstack_finish(&asm_obst);
4158 needs_memory |= supports_memop;
4159 if(supports_memop) {
4166 static void statement_to_firm(statement_t *statement)
4168 switch(statement->kind) {
4169 case STATEMENT_INVALID:
4170 panic("invalid statement found");
4171 case STATEMENT_COMPOUND:
4172 compound_statement_to_firm(&statement->compound);
4174 case STATEMENT_RETURN:
4175 return_statement_to_firm(&statement->returns);
4177 case STATEMENT_EXPRESSION:
4178 expression_statement_to_firm(&statement->expression);
4181 if_statement_to_firm(&statement->ifs);
4183 case STATEMENT_WHILE:
4184 while_statement_to_firm(&statement->whiles);
4186 case STATEMENT_DO_WHILE:
4187 do_while_statement_to_firm(&statement->do_while);
4189 case STATEMENT_DECLARATION:
4190 declaration_statement_to_firm(&statement->declaration);
4192 case STATEMENT_BREAK:
4193 create_jump_statement(statement, break_label);
4195 case STATEMENT_CONTINUE:
4196 create_jump_statement(statement, continue_label);
4198 case STATEMENT_SWITCH:
4199 switch_statement_to_firm(&statement->switchs);
4201 case STATEMENT_CASE_LABEL:
4202 case_label_to_firm(&statement->case_label);
4205 for_statement_to_firm(&statement->fors);
4207 case STATEMENT_LABEL:
4208 label_to_firm(&statement->label);
4210 case STATEMENT_GOTO:
4211 goto_to_firm(&statement->gotos);
4214 asm_statement_to_firm(&statement->asms);
4217 panic("Statement not implemented\n");
4220 static int count_decls_in_expression(const expression_t *expression);
4222 static int count_local_declarations(const declaration_t * decl,
4223 const declaration_t *const end)
4226 for (; decl != end; decl = decl->next) {
4227 if(decl->namespc != NAMESPACE_NORMAL)
4229 const type_t *type = skip_typeref(decl->type);
4230 if (!decl->address_taken && is_type_scalar(type))
4232 const initializer_t *initializer = decl->init.initializer;
4233 /* FIXME: should walk initializer hierarchies... */
4234 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4235 count += count_decls_in_expression(initializer->value.value);
4241 static int count_decls_in_expression(const expression_t *expression) {
4242 if(expression == NULL)
4245 switch(expression->base.kind) {
4246 case EXPR_STATEMENT:
4247 return count_decls_in_stmts(expression->statement.statement);
4249 int count_left = count_decls_in_expression(expression->binary.left);
4250 int count_right = count_decls_in_expression(expression->binary.right);
4251 return count_left + count_right;
4254 return count_decls_in_expression(expression->unary.value);
4257 call_argument_t *argument = expression->call.arguments;
4258 for( ; argument != NULL; argument = argument->next) {
4259 count += count_decls_in_expression(argument->expression);
4268 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
4269 * (or implement all the missing expressions here/implement a walker)
4275 static int count_decls_in_stmts(const statement_t *stmt)
4278 for (; stmt != NULL; stmt = stmt->base.next) {
4279 switch (stmt->kind) {
4280 case STATEMENT_DECLARATION: {
4281 const declaration_statement_t *const decl_stmt = &stmt->declaration;
4282 count += count_local_declarations(decl_stmt->declarations_begin,
4283 decl_stmt->declarations_end->next);
4287 case STATEMENT_COMPOUND: {
4288 const compound_statement_t *const comp =
4290 count += count_decls_in_stmts(comp->statements);
4294 case STATEMENT_IF: {
4295 const if_statement_t *const if_stmt = &stmt->ifs;
4296 count += count_decls_in_expression(if_stmt->condition);
4297 count += count_decls_in_stmts(if_stmt->true_statement);
4298 count += count_decls_in_stmts(if_stmt->false_statement);
4302 case STATEMENT_SWITCH: {
4303 const switch_statement_t *const switch_stmt = &stmt->switchs;
4304 count += count_decls_in_expression(switch_stmt->expression);
4305 count += count_decls_in_stmts(switch_stmt->body);
4309 case STATEMENT_LABEL: {
4310 const label_statement_t *const label_stmt = &stmt->label;
4311 if(label_stmt->statement != NULL) {
4312 count += count_decls_in_stmts(label_stmt->statement);
4317 case STATEMENT_WHILE: {
4318 const while_statement_t *const while_stmt = &stmt->whiles;
4319 count += count_decls_in_expression(while_stmt->condition);
4320 count += count_decls_in_stmts(while_stmt->body);
4324 case STATEMENT_DO_WHILE: {
4325 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
4326 count += count_decls_in_expression(do_while_stmt->condition);
4327 count += count_decls_in_stmts(do_while_stmt->body);
4331 case STATEMENT_FOR: {
4332 const for_statement_t *const for_stmt = &stmt->fors;
4333 count += count_local_declarations(for_stmt->scope.declarations, NULL);
4334 count += count_decls_in_expression(for_stmt->initialisation);
4335 count += count_decls_in_expression(for_stmt->condition);
4336 count += count_decls_in_expression(for_stmt->step);
4337 count += count_decls_in_stmts(for_stmt->body);
4341 case STATEMENT_CASE_LABEL: {
4342 const case_label_statement_t *label = &stmt->case_label;
4343 count += count_decls_in_expression(label->expression);
4344 if(label->statement != NULL) {
4345 count += count_decls_in_stmts(label->statement);
4351 case STATEMENT_BREAK:
4352 case STATEMENT_CONTINUE:
4355 case STATEMENT_EXPRESSION: {
4356 const expression_statement_t *expr_stmt = &stmt->expression;
4357 count += count_decls_in_expression(expr_stmt->expression);
4361 case STATEMENT_GOTO:
4362 case STATEMENT_INVALID:
4365 case STATEMENT_RETURN: {
4366 const return_statement_t *ret_stmt = &stmt->returns;
4367 count += count_decls_in_expression(ret_stmt->value);
4375 static int get_function_n_local_vars(declaration_t *declaration)
4379 /* count parameters */
4380 count += count_local_declarations(declaration->scope.declarations, NULL);
4382 /* count local variables declared in body */
4383 count += count_decls_in_stmts(declaration->init.statement);
4388 static void initialize_function_parameters(declaration_t *declaration)
4390 ir_graph *irg = current_ir_graph;
4391 ir_node *args = get_irg_args(irg);
4392 ir_node *start_block = get_irg_start_block(irg);
4393 ir_type *function_irtype = get_ir_type(declaration->type);
4396 declaration_t *parameter = declaration->scope.declarations;
4397 for( ; parameter != NULL; parameter = parameter->next, ++n) {
4398 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
4399 type_t *type = skip_typeref(parameter->type);
4401 bool needs_entity = parameter->address_taken;
4402 assert(!is_type_array(type));
4403 if(is_type_compound(type)) {
4404 needs_entity = true;
4408 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
4409 ident *id = new_id_from_str(parameter->symbol->string);
4410 set_entity_ident(entity, id);
4412 parameter->declaration_kind
4413 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
4414 parameter->v.entity = entity;
4418 ir_mode *mode = get_ir_mode(parameter->type);
4420 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
4422 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
4423 parameter->v.value_number = next_value_number_function;
4424 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
4425 ++next_value_number_function;
4427 set_value(parameter->v.value_number, proj);
4432 * Handle additional decl modifiers for IR-graphs
4434 * @param irg the IR-graph
4435 * @param dec_modifiers additional modifiers
4437 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
4439 if (decl_modifiers & DM_NORETURN) {
4440 /* TRUE if the declaration includes the Microsoft
4441 __declspec(noreturn) specifier. */
4442 set_irg_additional_property(irg, mtp_property_noreturn);
4444 if (decl_modifiers & DM_NOTHROW) {
4445 /* TRUE if the declaration includes the Microsoft
4446 __declspec(nothrow) specifier. */
4447 set_irg_additional_property(irg, mtp_property_nothrow);
4449 if (decl_modifiers & DM_NAKED) {
4450 /* TRUE if the declaration includes the Microsoft
4451 __declspec(naked) specifier. */
4452 set_irg_additional_property(irg, mtp_property_naked);
4454 if (decl_modifiers & DM_FORCEINLINE) {
4455 /* TRUE if the declaration includes the
4456 Microsoft __forceinline specifier. */
4457 set_irg_inline_property(irg, irg_inline_forced);
4459 if (decl_modifiers & DM_NOINLINE) {
4460 /* TRUE if the declaration includes the Microsoft
4461 __declspec(noinline) specifier. */
4462 set_irg_inline_property(irg, irg_inline_forbidden);
4466 static void create_function(declaration_t *declaration)
4468 ir_entity *function_entity = get_function_entity(declaration);
4470 if(declaration->init.statement == NULL)
4473 current_function_decl = declaration;
4474 current_function_name = NULL;
4476 assert(imature_blocks == NULL);
4477 imature_blocks = NEW_ARR_F(ir_node*, 0);
4479 int n_local_vars = get_function_n_local_vars(declaration);
4480 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
4481 ir_node *first_block = get_cur_block();
4483 /* set inline flags */
4484 if (declaration->is_inline)
4485 set_irg_inline_property(irg, irg_inline_recomended);
4486 handle_decl_modifier_irg(irg, declaration->modifiers);
4488 next_value_number_function = 0;
4489 initialize_function_parameters(declaration);
4491 statement_to_firm(declaration->init.statement);
4493 ir_node *end_block = get_irg_end_block(irg);
4495 /* do we have a return statement yet? */
4496 if(get_cur_block() != NULL) {
4497 type_t *type = skip_typeref(declaration->type);
4498 assert(is_type_function(type));
4499 const function_type_t *func_type = &type->function;
4500 const type_t *return_type
4501 = skip_typeref(func_type->return_type);
4504 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4505 ret = new_Return(get_store(), 0, NULL);
4508 if(is_type_scalar(return_type)) {
4509 mode = get_ir_mode(func_type->return_type);
4515 /* ยง5.1.2.2.3 main implicitly returns 0 */
4516 if (strcmp(declaration->symbol->string, "main") == 0) {
4517 in[0] = new_Const(mode, get_mode_null(mode));
4519 in[0] = new_Unknown(mode);
4521 ret = new_Return(get_store(), 1, in);
4523 add_immBlock_pred(end_block, ret);
4526 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
4527 mature_immBlock(imature_blocks[i]);
4529 DEL_ARR_F(imature_blocks);
4530 imature_blocks = NULL;
4532 mature_immBlock(first_block);
4533 mature_immBlock(end_block);
4535 irg_finalize_cons(irg);
4537 /* finalize the frame type */
4538 ir_type *frame_type = get_irg_frame_type(irg);
4539 int n = get_compound_n_members(frame_type);
4542 for(int i = 0; i < n; ++i) {
4543 ir_entity *entity = get_compound_member(frame_type, i);
4544 ir_type *entity_type = get_entity_type(entity);
4546 int align = get_type_alignment_bytes(entity_type);
4547 if(align > align_all)
4551 misalign = offset % align;
4553 offset += align - misalign;
4557 set_entity_offset(entity, offset);
4558 offset += get_type_size_bytes(entity_type);
4560 set_type_size_bytes(frame_type, offset);
4561 set_type_alignment_bytes(frame_type, align_all);
4562 set_type_state(frame_type, layout_fixed);
4567 static void scope_to_firm(scope_t *scope)
4569 /* first pass: create declarations */
4570 declaration_t *declaration = scope->declarations;
4571 for( ; declaration != NULL; declaration = declaration->next) {
4572 if(declaration->namespc != NAMESPACE_NORMAL)
4574 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4575 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4577 if(declaration->symbol == NULL)
4580 type_t *type = skip_typeref(declaration->type);
4581 if(is_type_function(type)) {
4582 get_function_entity(declaration);
4584 create_global_variable(declaration);
4588 /* second pass: create code/initializers */
4589 declaration = scope->declarations;
4590 for( ; declaration != NULL; declaration = declaration->next) {
4591 if(declaration->namespc != NAMESPACE_NORMAL)
4593 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4594 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4596 if(declaration->symbol == NULL)
4599 type_t *type = declaration->type;
4600 if(type->kind == TYPE_FUNCTION) {
4601 create_function(declaration);
4603 assert(declaration->declaration_kind
4604 == DECLARATION_KIND_GLOBAL_VARIABLE);
4605 current_ir_graph = get_const_code_irg();
4606 create_declaration_initializer(declaration);
4611 void init_ast2firm(void)
4613 obstack_init(&asm_obst);
4614 init_atomic_modes();
4616 /* create idents for all known runtime functions */
4617 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
4618 predef_idents[rts_data[i].id] = new_id_from_str(rts_data[i].name);
4622 static void init_ir_types(void)
4624 static int ir_types_initialized = 0;
4625 if(ir_types_initialized)
4627 ir_types_initialized = 1;
4629 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4630 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4631 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4633 ir_type_int = get_ir_type(type_int);
4634 ir_type_const_char = get_ir_type(type_const_char);
4635 ir_type_wchar_t = get_ir_type(type_wchar_t);
4636 ir_type_void = get_ir_type(type_int); /* we don't have a real void
4639 type_void->base.firm_type = ir_type_void;
4642 void exit_ast2firm(void)
4644 obstack_free(&asm_obst, NULL);
4647 void translation_unit_to_firm(translation_unit_t *unit)
4649 /* just to be sure */
4650 continue_label = NULL;
4652 current_switch_cond = NULL;
4656 scope_to_firm(&unit->scope);