9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
15 #include "adt/array.h"
20 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
22 static ir_type *ir_type_const_char;
23 static ir_type *ir_type_void;
24 static ir_type *ir_type_int;
25 static ir_type *ir_type_void_ptr;
27 static type_t *type_const_char;
28 static type_t *type_void;
29 static type_t *type_int;
31 static int next_value_number_function;
32 static ir_node *continue_label;
33 static ir_node *break_label;
34 static ir_node *current_switch_cond;
35 static bool saw_default_label;
36 static ir_node **imature_blocks;
38 static const declaration_t *current_function_decl;
39 static ir_node *current_function_name;
41 typedef enum declaration_type_t {
42 DECLARATION_TYPE_UNKNOWN,
43 DECLARATION_TYPE_FUNCTION,
44 DECLARATION_TYPE_GLOBAL_VARIABLE,
45 DECLARATION_TYPE_LOCAL_VARIABLE,
46 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
47 DECLARATION_TYPE_COMPOUND_MEMBER,
48 DECLARATION_TYPE_LABEL_BLOCK,
51 static ir_type *get_ir_type(type_t *type);
53 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
57 const declaration_t *declaration = & value_numbers[pos]->declaration;
59 print_warning_prefix(declaration->source_position);
60 fprintf(stderr, "variable '%s' might be used uninitialized\n",
61 declaration->symbol->string);
63 fprintf(stderr, "Some variable might be used uninitialized\n");
64 return new_r_Unknown(irg, mode);
67 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
69 const source_position_t *pos = (const source_position_t*) dbg;
72 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
76 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
78 const source_position_t *pos = (const source_position_t*) dbg;
83 return pos->input_name;
86 void init_ast2firm(void)
88 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
89 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
90 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
92 ir_type_int = get_ir_type(type_int);
93 ir_type_const_char = get_ir_type(type_const_char);
94 ir_type_void = get_ir_type(type_int); /* we don't have a real void
96 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
97 ir_type_void, mode_P_data);
99 type_void->firm_type = ir_type_void;
102 void exit_ast2firm(void)
106 static unsigned unique_id = 0;
108 static ident *unique_ident(const char *tag)
112 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
114 return new_id_from_str(buf);
117 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
119 switch(atomic_type->atype) {
120 case ATOMIC_TYPE_SCHAR:
121 case ATOMIC_TYPE_CHAR:
123 case ATOMIC_TYPE_UCHAR:
125 case ATOMIC_TYPE_SHORT:
127 case ATOMIC_TYPE_USHORT:
129 case ATOMIC_TYPE_LONG:
130 case ATOMIC_TYPE_INT:
132 case ATOMIC_TYPE_ULONG:
133 case ATOMIC_TYPE_UINT:
135 case ATOMIC_TYPE_LONGLONG:
137 case ATOMIC_TYPE_ULONGLONG:
139 case ATOMIC_TYPE_FLOAT:
141 case ATOMIC_TYPE_DOUBLE:
143 case ATOMIC_TYPE_LONG_DOUBLE:
145 case ATOMIC_TYPE_BOOL:
147 #ifdef PROVIDE_COMPLEX
148 case ATOMIC_TYPE_FLOAT_COMPLEX:
149 case ATOMIC_TYPE_DOUBLE_COMPLEX:
150 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
151 panic("complex lowering not implemented yet");
153 case ATOMIC_TYPE_FLOAT_IMAGINARY:
154 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
155 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
156 panic("imaginary lowering not implemented yet");
159 case ATOMIC_TYPE_VOID:
160 /* firm has no real void... */
162 case ATOMIC_TYPE_INVALID:
165 panic("Encountered unknown atomic type");
169 static unsigned get_type_size(type_t *type);
171 static unsigned get_atomic_type_size(const atomic_type_t *type)
173 switch(type->atype) {
174 case ATOMIC_TYPE_CHAR:
175 case ATOMIC_TYPE_SCHAR:
176 case ATOMIC_TYPE_UCHAR:
179 case ATOMIC_TYPE_SHORT:
180 case ATOMIC_TYPE_USHORT:
183 case ATOMIC_TYPE_BOOL:
184 case ATOMIC_TYPE_INT:
185 case ATOMIC_TYPE_UINT:
186 case ATOMIC_TYPE_LONG:
187 case ATOMIC_TYPE_ULONG:
188 case ATOMIC_TYPE_FLOAT:
191 case ATOMIC_TYPE_LONGLONG:
192 case ATOMIC_TYPE_ULONGLONG:
193 case ATOMIC_TYPE_DOUBLE:
196 case ATOMIC_TYPE_LONG_DOUBLE:
199 case ATOMIC_TYPE_VOID:
202 case ATOMIC_TYPE_INVALID:
205 panic("Trying to determine size of invalid atomic type");
208 static unsigned get_compound_type_size(compound_type_t *type)
210 ir_type *irtype = get_ir_type(&type->type);
211 return get_type_size_bytes(irtype);
214 static unsigned get_array_type_size(array_type_t *type)
216 ir_type *irtype = get_ir_type(&type->type);
217 return get_type_size_bytes(irtype);
220 static unsigned get_type_size(type_t *type)
222 type = skip_typeref(type);
226 return get_atomic_type_size((const atomic_type_t*) type);
228 return get_mode_size_bytes(mode_Is);
229 case TYPE_COMPOUND_UNION:
230 case TYPE_COMPOUND_STRUCT:
231 return get_compound_type_size((compound_type_t*) type);
233 /* just a pointer to the function */
234 return get_mode_size_bytes(mode_P_code);
236 return get_mode_size_bytes(mode_P_data);
238 return get_array_type_size((array_type_t*) type);
245 panic("Trying to determine size of invalid type");
248 static unsigned count_parameters(const function_type_t *function_type)
252 function_parameter_t *parameter = function_type->parameters;
253 for ( ; parameter != NULL; parameter = parameter->next) {
263 static long fold_constant(const expression_t *expression);
265 static ir_type *create_atomic_type(const atomic_type_t *type)
267 ir_mode *mode = get_atomic_mode(type);
268 ident *id = get_mode_ident(mode);
269 ir_type *irtype = new_type_primitive(id, mode);
274 static ir_type *create_method_type(const function_type_t *function_type)
276 type_t *result_type = function_type->result_type;
278 ident *id = unique_ident("functiontype");
279 int n_parameters = count_parameters(function_type);
280 int n_results = result_type == type_void ? 0 : 1;
281 ir_type *irtype = new_type_method(id, n_parameters, n_results);
283 if(result_type != type_void) {
284 ir_type *restype = get_ir_type(result_type);
285 set_method_res_type(irtype, 0, restype);
288 function_parameter_t *parameter = function_type->parameters;
290 for( ; parameter != NULL; parameter = parameter->next) {
291 ir_type *p_irtype = get_ir_type(parameter->type);
292 set_method_param_type(irtype, n, p_irtype);
296 if(function_type->variadic || function_type->unspecified_parameters) {
297 set_method_variadicity(irtype, variadicity_variadic);
303 static ir_type *create_pointer_type(pointer_type_t *type)
305 type_t *points_to = type->points_to;
306 ir_type *ir_points_to;
307 /* Avoid endless recursion if the points_to type contains this poiner type
308 * again (might be a struct). We therefore first create a void* pointer
309 * and then set the real points_to type
311 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
312 ir_type_void, mode_P_data);
313 type->type.firm_type = ir_type;
315 ir_points_to = get_ir_type(points_to);
316 set_pointer_points_to_type(ir_type, ir_points_to);
321 static ir_type *create_array_type(array_type_t *type)
323 type_t *element_type = type->element_type;
324 ir_type *ir_element_type = get_ir_type(element_type);
326 ident *id = unique_ident("array");
327 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
329 if(type->size != NULL) {
330 int n_elements = fold_constant(type->size);
332 set_array_bounds_int(ir_type, 0, 0, n_elements);
334 size_t elemsize = get_type_size_bytes(ir_element_type);
335 int align = get_type_alignment_bytes(ir_element_type);
336 if(elemsize % align > 0) {
337 elemsize += align - (elemsize % align);
339 set_type_size_bytes(ir_type, n_elements * elemsize);
340 set_type_alignment_bytes(ir_type, align);
341 set_type_state(ir_type, layout_fixed);
347 #define INVALID_TYPE ((ir_type_ptr)-1)
349 static ir_type *create_struct_type(compound_type_t *type)
351 symbol_t *symbol = type->declaration->symbol;
354 id = unique_ident(symbol->string);
356 id = unique_ident("__anonymous_struct");
358 ir_type *ir_type = new_type_struct(id);
360 type->type.firm_type = ir_type;
364 declaration_t *entry = type->declaration->context.declarations;
365 for( ; entry != NULL; entry = entry->next) {
366 if(entry->namespc != NAMESPACE_NORMAL)
369 ident *ident = new_id_from_str(entry->symbol->string);
370 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
372 int entry_size = get_type_size_bytes(entry_ir_type);
373 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
374 int misalign = offset % entry_alignment;
376 offset += entry_alignment - misalign;
378 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
379 set_entity_offset(entity, offset);
380 add_struct_member(ir_type, entity);
381 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
382 entry->v.entity = entity;
384 offset += entry_size;
385 if(entry_alignment > align_all) {
386 if(entry_alignment % align_all != 0) {
387 panic("Uneven alignments not supported yet");
389 align_all = entry_alignment;
393 int misalign = offset % align_all;
395 set_type_alignment_bytes(ir_type, align_all);
396 set_type_size_bytes(ir_type, offset);
397 set_type_state(ir_type, layout_fixed);
402 static ir_type *create_union_type(compound_type_t *type)
404 declaration_t *declaration = type->declaration;
405 symbol_t *symbol = declaration->symbol;
408 id = unique_ident(symbol->string);
410 id = unique_ident("__anonymous_union");
412 ir_type *ir_type = new_type_union(id);
414 type->type.firm_type = ir_type;
418 declaration_t *entry = declaration->context.declarations;
419 for( ; entry != NULL; entry = entry->next) {
420 if(entry->namespc != NAMESPACE_NORMAL)
423 ident *ident = new_id_from_str(entry->symbol->string);
424 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
426 int entry_size = get_type_size_bytes(entry_ir_type);
427 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
429 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
430 add_union_member(ir_type, entity);
431 set_entity_offset(entity, 0);
432 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
433 entry->v.entity = entity;
435 if(entry_size > size) {
438 if(entry_alignment > align_all) {
439 if(entry_alignment % align_all != 0) {
440 panic("Uneven alignments not supported yet");
442 align_all = entry_alignment;
446 set_type_alignment_bytes(ir_type, align_all);
447 set_type_size_bytes(ir_type, size);
448 set_type_state(ir_type, layout_fixed);
453 static ir_type *get_ir_type(type_t *type)
455 assert(type != NULL);
457 type = skip_typeref(type);
459 if(type->firm_type != NULL) {
460 assert(type->firm_type != INVALID_TYPE);
461 return type->firm_type;
464 ir_type *firm_type = NULL;
467 firm_type = create_atomic_type((atomic_type_t*) type);
470 firm_type = create_method_type((function_type_t*) type);
473 firm_type = create_pointer_type((pointer_type_t*) type);
476 firm_type = create_array_type((array_type_t*) type);
478 case TYPE_COMPOUND_STRUCT:
479 firm_type = create_struct_type((compound_type_t*) type);
481 case TYPE_COMPOUND_UNION:
482 firm_type = create_union_type((compound_type_t*) type);
485 firm_type = ir_type_int;
493 if(firm_type == NULL)
494 panic("unknown type found");
496 type->firm_type = firm_type;
500 static inline ir_mode *get_ir_mode(type_t *type)
502 ir_type *irtype = get_ir_type(type);
504 /* firm doesn't report a mode for arrays somehow... */
505 if(is_Array_type(irtype)) {
509 ir_mode *mode = get_type_mode(irtype);
510 assert(mode != NULL);
514 static ir_entity* get_function_entity(declaration_t *declaration)
516 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
517 return declaration->v.entity;
518 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
520 symbol_t *symbol = declaration->symbol;
521 ident *id = new_id_from_str(symbol->string);
523 ir_type *global_type = get_glob_type();
524 ir_type *ir_type_method = get_ir_type(declaration->type);
525 assert(is_Method_type(ir_type_method));
527 ir_entity *entity = new_entity(global_type, id, ir_type_method);
528 set_entity_ld_ident(entity, id);
529 if(declaration->storage_class == STORAGE_CLASS_STATIC
530 || declaration->is_inline) {
531 set_entity_visibility(entity, visibility_local);
532 } else if(declaration->init.statement != NULL) {
533 set_entity_visibility(entity, visibility_external_visible);
535 set_entity_visibility(entity, visibility_external_allocated);
538 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
539 declaration->v.entity = entity;
546 static ir_node *expression_to_firm(const expression_t *expression);
548 static dbg_info *get_dbg_info(const source_position_t *pos)
550 return (dbg_info*) pos;
553 static ir_node *const_to_firm(const const_t *cnst)
555 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
556 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
561 if(mode_is_float(mode)) {
562 tv = new_tarval_from_double(cnst->v.float_value, mode);
564 if(mode_is_signed(mode)) {
565 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
567 len = snprintf(buf, sizeof(buf), "%llu", cnst->v.int_value);
569 tv = new_tarval_from_str(buf, len, mode);
572 return new_d_Const(dbgi, mode, tv);
575 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
577 assert(entity != NULL);
578 union symconst_symbol sym;
579 sym.entity_p = entity;
580 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
583 static ir_node *string_to_firm(const source_position_t *const src_pos,
584 const char *const id_prefix,
585 const char *const string)
587 ir_type *const global_type = get_glob_type();
588 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
591 ident *const id = unique_ident(id_prefix);
592 ir_entity *const entity = new_entity(global_type, id, type);
593 set_entity_ld_ident(entity, id);
594 set_entity_variability(entity, variability_constant);
596 ir_type *const elem_type = ir_type_const_char;
597 ir_mode *const mode = get_type_mode(elem_type);
599 const size_t slen = strlen(string) + 1;
601 set_array_lower_bound_int(type, 0, 0);
602 set_array_upper_bound_int(type, 0, slen);
603 set_type_size_bytes(type, slen);
604 set_type_state(type, layout_fixed);
606 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
607 for(size_t i = 0; i < slen; ++i) {
608 tvs[i] = new_tarval_from_long(string[i], mode);
611 set_array_entity_values(entity, tvs, slen);
614 dbg_info *const dbgi = get_dbg_info(src_pos);
616 return create_symconst(dbgi, entity);
619 static ir_node *string_literal_to_firm(const string_literal_t* literal)
621 return string_to_firm(&literal->expression.source_position, "Lstr",
625 static ir_node *deref_address(type_t *const type, ir_node *const addr,
626 dbg_info *const dbgi)
628 switch (type->type) {
630 case TYPE_COMPOUND_STRUCT:
631 case TYPE_COMPOUND_UNION:
635 ir_mode *const mode = get_ir_mode(type);
636 ir_node *const memory = get_store();
637 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
638 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
639 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
646 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
648 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
649 declaration_t *declaration = ref->declaration;
650 type_t *type = skip_typeref(declaration->type);
652 switch((declaration_type_t) declaration->declaration_type) {
653 case DECLARATION_TYPE_UNKNOWN:
655 case DECLARATION_TYPE_LOCAL_VARIABLE: {
656 ir_mode *mode = get_ir_mode(type);
657 return get_value(declaration->v.value_number, mode);
659 case DECLARATION_TYPE_FUNCTION: {
660 return create_symconst(dbgi, declaration->v.entity);
662 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
663 ir_entity *entity = declaration->v.entity;
664 ir_node *symconst = create_symconst(dbgi, entity);
665 return deref_address(type, symconst, dbgi);
667 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
668 ir_entity *entity = declaration->v.entity;
669 ir_node *frame = get_irg_frame(current_ir_graph);
670 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
671 return deref_address(type, sel, dbgi);
674 case DECLARATION_TYPE_COMPOUND_MEMBER:
675 case DECLARATION_TYPE_LABEL_BLOCK:
676 panic("not implemented reference type");
679 panic("reference to declaration with unknown type found");
682 static ir_node *reference_addr(const reference_expression_t *ref)
684 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
685 declaration_t *declaration = ref->declaration;
687 switch((declaration_type_t) declaration->declaration_type) {
688 case DECLARATION_TYPE_UNKNOWN:
690 case DECLARATION_TYPE_LOCAL_VARIABLE:
691 panic("local variable without entity has no address");
692 case DECLARATION_TYPE_FUNCTION: {
693 return create_symconst(dbgi, declaration->v.entity);
695 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
696 ir_entity *entity = declaration->v.entity;
697 ir_node *symconst = create_symconst(dbgi, entity);
700 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
701 ir_entity *entity = declaration->v.entity;
702 ir_node *frame = get_irg_frame(current_ir_graph);
703 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
707 case DECLARATION_TYPE_COMPOUND_MEMBER:
708 case DECLARATION_TYPE_LABEL_BLOCK:
709 panic("not implemented reference type");
712 panic("reference to declaration with unknown type found");
715 static ir_node *call_expression_to_firm(const call_expression_t *call)
717 assert(get_cur_block() != NULL);
719 expression_t *function = call->function;
720 ir_node *callee = expression_to_firm(function);
722 function_type_t *function_type;
723 if (function->datatype->type == TYPE_POINTER) {
724 pointer_type_t *const ptr_type = (pointer_type_t*)function->datatype;
725 assert(ptr_type->points_to->type == TYPE_FUNCTION);
726 function_type = (function_type_t*)ptr_type->points_to;
728 assert(function->datatype->type == TYPE_FUNCTION);
729 function_type = (function_type_t*)function->datatype;
732 int n_parameters = 0;
733 call_argument_t *argument = call->arguments;
734 for( ; argument != NULL; argument = argument->next) {
738 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
739 ir_type *new_method_type = NULL;
740 if(function_type->variadic || function_type->unspecified_parameters) {
741 /* we need to construct a new method type matching the call
743 int n_res = get_method_n_ress(ir_method_type);
744 new_method_type = new_type_method(unique_ident("calltype"),
745 n_parameters, n_res);
746 set_method_calling_convention(new_method_type,
747 get_method_calling_convention(ir_method_type));
748 set_method_additional_properties(new_method_type,
749 get_method_additional_properties(ir_method_type));
751 for(int i = 0; i < n_res; ++i) {
752 set_method_res_type(new_method_type, i,
753 get_method_res_type(ir_method_type, i));
756 ir_node *in[n_parameters];
758 argument = call->arguments;
760 for( ; argument != NULL; argument = argument->next) {
761 expression_t *expression = argument->expression;
762 ir_node *arg_node = expression_to_firm(expression);
765 if(new_method_type != NULL) {
766 ir_type *irtype = get_ir_type(expression->datatype);
767 set_method_param_type(new_method_type, n, irtype);
772 assert(n == n_parameters);
774 if(new_method_type != NULL)
775 ir_method_type = new_method_type;
777 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
778 ir_node *store = get_store();
779 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
781 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
784 type_t *result_type = function_type->result_type;
785 ir_node *result = NULL;
786 if(result_type != type_void) {
787 ir_mode *mode = get_ir_mode(result_type);
788 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
789 result = new_d_Proj(dbgi, resproj, mode, 0);
795 static ir_node *expression_to_addr(const expression_t *expression);
796 static void create_condition_evaluation(const expression_t *expression,
798 ir_node *false_block);
800 static void set_value_for_expression(const expression_t *expression,
803 if(expression->type == EXPR_REFERENCE) {
804 reference_expression_t *ref = (reference_expression_t*) expression;
806 declaration_t *declaration = ref->declaration;
807 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
808 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
809 set_value(declaration->v.value_number, value);
814 dbg_info *dbgi = get_dbg_info(&expression->source_position);
815 ir_node *addr = expression_to_addr(expression);
816 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
817 ir_node *memory = get_store();
818 ir_node *store = new_d_Store(dbgi, memory, addr, value);
819 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
820 set_store(store_mem);
823 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
825 ir_mode *value_mode = get_irn_mode(value);
827 if (value_mode == dest_mode || is_Bad(value))
830 if(dest_mode == mode_b) {
831 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
832 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
833 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
837 return new_d_Conv(dbgi, value, dest_mode);
840 static ir_node *create_incdec(const unary_expression_t *expression)
842 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
843 type_t *type = expression->expression.datatype;
844 ir_mode *mode = get_ir_mode(type);
845 expression_t *value = expression->value;
847 ir_node *value_node = expression_to_firm(value);
850 if(type->type == TYPE_POINTER) {
851 pointer_type_t *pointer_type = (pointer_type_t*) type;
852 unsigned elem_size = get_type_size(pointer_type->points_to);
853 offset = new_Const_long(mode_Is, elem_size);
855 assert(is_type_arithmetic(type));
856 offset = new_Const(mode, get_mode_one(mode));
860 switch(expression->type) {
861 case UNEXPR_POSTFIX_INCREMENT: {
862 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
863 set_value_for_expression(value, new_value);
866 case UNEXPR_POSTFIX_DECREMENT: {
867 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
868 set_value_for_expression(value, new_value);
871 case UNEXPR_PREFIX_INCREMENT: {
872 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
873 set_value_for_expression(value, new_value);
876 case UNEXPR_PREFIX_DECREMENT: {
877 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
878 set_value_for_expression(value, new_value);
882 panic("no incdec expr in create_incdec");
888 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
890 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
891 type_t *type = expression->expression.datatype;
893 if(expression->type == UNEXPR_TAKE_ADDRESS)
894 return expression_to_addr(expression->value);
896 const expression_t *value = expression->value;
897 ir_node *value_node = expression_to_firm(value);
899 switch(expression->type) {
901 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
904 case UNEXPR_BITWISE_NEGATE:
905 return new_d_Not(dbgi, value_node, get_ir_mode(type));
907 if(get_irn_mode(value_node) != mode_b) {
908 value_node = create_conv(dbgi, value_node, mode_b);
910 value_node = new_d_Not(dbgi, value_node, mode_b);
911 ir_mode *const mode = get_ir_mode(type);
913 value_node = create_conv(dbgi, value_node, mode);
917 case UNEXPR_DEREFERENCE:
918 return deref_address(type, value_node, dbgi);
919 case UNEXPR_POSTFIX_INCREMENT:
920 case UNEXPR_POSTFIX_DECREMENT:
921 case UNEXPR_PREFIX_INCREMENT:
922 case UNEXPR_PREFIX_DECREMENT:
923 return create_incdec(expression);
925 return create_conv(dbgi, value_node, get_ir_mode(type));
927 case UNEXPR_TAKE_ADDRESS:
931 panic("invalid UNEXPR type found");
934 static long get_pnc(binary_expression_type_t type)
937 case BINEXPR_EQUAL: return pn_Cmp_Eq;
938 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
939 case BINEXPR_LESS: return pn_Cmp_Lt;
940 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
941 case BINEXPR_GREATER: return pn_Cmp_Gt;
942 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
946 panic("trying to get pn_Cmp from non-comparison binexpr type");
949 static ir_node *create_lazy_op(const binary_expression_t *expression)
951 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
952 type_t *type = expression->expression.datatype;
953 ir_mode *mode = get_ir_mode(type);
955 ir_node *cur_block = get_cur_block();
957 ir_node *one_block = new_immBlock();
958 ir_node *one = new_Const(mode, get_mode_one(mode));
959 ir_node *jmp_one = new_d_Jmp(dbgi);
961 ir_node *zero_block = new_immBlock();
962 ir_node *zero = new_Const(mode, get_mode_null(mode));
963 ir_node *jmp_zero = new_d_Jmp(dbgi);
965 set_cur_block(cur_block);
966 create_condition_evaluation((const expression_t*) expression,
967 one_block, zero_block);
968 mature_immBlock(one_block);
969 mature_immBlock(zero_block);
971 ir_node *common_block = new_immBlock();
972 add_immBlock_pred(common_block, jmp_one);
973 add_immBlock_pred(common_block, jmp_zero);
974 mature_immBlock(common_block);
976 ir_node *in[2] = { one, zero };
977 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
982 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
983 ir_node *right, ir_mode *mode);
985 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
986 create_arithmetic_func func)
988 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
989 ir_node *left = expression_to_firm(expression->left);
990 ir_node *right = expression_to_firm(expression->right);
991 type_t *type = expression->right->datatype;
992 /* be careful with the modes, because in arithmetic assign nodes only
993 * the right operand has the mode of the arithmetic already */
994 ir_mode *mode = get_ir_mode(type);
995 left = create_conv(dbgi, left, mode);
996 ir_node *res = func(dbgi, left, right, mode);
1001 static ir_node *pointer_arithmetic(ir_node *const pointer,
1004 dbg_info *const dbgi,
1005 const create_arithmetic_func func)
1007 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1008 type_t *const points_to = pointer_type->points_to;
1009 const unsigned elem_size = get_type_size(points_to);
1011 assert(elem_size >= 1);
1012 if (elem_size > 1) {
1013 integer = create_conv(dbgi, integer, mode_Is);
1014 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1015 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1019 ir_mode *const mode = get_ir_mode(type);
1020 return func(dbgi, pointer, integer, mode);
1023 static ir_node *create_arithmetic_assign_binop(
1024 const binary_expression_t *expression, create_arithmetic_func func)
1026 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1027 type_t *const type = expression->expression.datatype;
1030 if (type->type == TYPE_POINTER) {
1031 ir_node *const pointer = expression_to_firm(expression->left);
1032 ir_node * integer = expression_to_firm(expression->right);
1033 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1035 value = create_arithmetic_binop(expression, func);
1038 ir_mode *const mode = get_ir_mode(type);
1039 value = create_conv(dbgi, value, mode);
1040 set_value_for_expression(expression->left, value);
1045 static ir_node *create_add(const binary_expression_t *expression)
1047 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1048 ir_node *left = expression_to_firm(expression->left);
1049 ir_node *right = expression_to_firm(expression->right);
1050 type_t *type = expression->expression.datatype;
1052 expression_t *expr_left = expression->left;
1053 expression_t *expr_right = expression->right;
1054 type_t *type_left = skip_typeref(expr_left->datatype);
1055 type_t *type_right = skip_typeref(expr_right->datatype);
1057 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1058 ir_mode *const mode = get_ir_mode(type);
1059 return new_d_Add(dbgi, left, right, mode);
1062 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1063 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1065 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1066 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1070 static ir_node *create_sub(const binary_expression_t *expression)
1072 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1073 expression_t *const expr_left = expression->left;
1074 expression_t *const expr_right = expression->right;
1075 ir_node *const left = expression_to_firm(expr_left);
1076 ir_node *const right = expression_to_firm(expr_right);
1077 type_t *const type = expression->expression.datatype;
1078 type_t *const type_left = skip_typeref(expr_left->datatype);
1079 type_t *const type_right = skip_typeref(expr_right->datatype);
1081 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1082 ir_mode *const mode = get_ir_mode(type);
1083 return new_d_Sub(dbgi, left, right, mode);
1084 } else if (type_left->type == TYPE_POINTER && type_right->type == TYPE_POINTER) {
1085 const pointer_type_t *const ptr_type = (const pointer_type_t*)type_left;
1086 const unsigned elem_size = get_type_size(ptr_type->points_to);
1087 ir_mode *const mode = get_ir_mode(type);
1088 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1089 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1090 ir_node *const no_mem = new_NoMem();
1091 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1092 op_pin_state_floats);
1093 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1096 assert(type_left->type == TYPE_POINTER);
1097 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1100 static ir_node *create_shift(const binary_expression_t *expression)
1102 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1103 ir_node *left = expression_to_firm(expression->left);
1104 ir_node *right = expression_to_firm(expression->right);
1105 type_t *type = expression->expression.datatype;
1106 ir_mode *mode = get_ir_mode(type);
1108 /* firm always wants the shift count to be unsigned */
1109 right = create_conv(dbgi, right, mode_Iu);
1113 switch(expression->type) {
1114 case BINEXPR_SHIFTLEFT:
1115 res = new_d_Shl(dbgi, left, right, mode);
1117 case BINEXPR_SHIFTRIGHT: {
1118 expression_t *expr_left = expression->left;
1119 type_t *type_left = skip_typeref(expr_left->datatype);
1121 if(is_type_signed(type_left)) {
1122 res = new_d_Shrs(dbgi, left, right, mode);
1124 res = new_d_Shr(dbgi, left, right, mode);
1129 panic("create shift op called for non-shift op");
1136 static ir_node *create_divmod(const binary_expression_t *expression)
1138 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1139 ir_node *left = expression_to_firm(expression->left);
1140 ir_node *right = expression_to_firm(expression->right);
1141 ir_node *pin = new_Pin(new_NoMem());
1142 type_t *type = expression->expression.datatype;
1143 ir_mode *mode = get_ir_mode(type);
1147 switch (expression->type) {
1149 case BINEXPR_DIV_ASSIGN:
1150 if(mode_is_float(mode)) {
1151 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1152 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1154 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1155 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1160 case BINEXPR_MOD_ASSIGN:
1161 assert(!mode_is_float(mode));
1162 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1163 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1166 default: panic("unexpected binary expression type in create_divmod()");
1172 static ir_node *create_arithmetic_assign_divmod(
1173 const binary_expression_t *expression)
1175 ir_node * value = create_divmod(expression);
1176 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1177 type_t *const type = expression->expression.datatype;
1178 ir_mode *const mode = get_ir_mode(type);
1180 assert(type->type != TYPE_POINTER);
1182 value = create_conv(dbgi, value, mode);
1183 set_value_for_expression(expression->left, value);
1189 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1191 binary_expression_type_t type = expression->type;
1194 case BINEXPR_NOTEQUAL:
1196 case BINEXPR_LESSEQUAL:
1197 case BINEXPR_GREATER:
1198 case BINEXPR_GREATEREQUAL: {
1199 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1200 ir_node *left = expression_to_firm(expression->left);
1201 ir_node *right = expression_to_firm(expression->right);
1202 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1203 long pnc = get_pnc(type);
1204 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1207 case BINEXPR_ASSIGN: {
1208 ir_node *right = expression_to_firm(expression->right);
1209 set_value_for_expression(expression->left, right);
1213 return create_add(expression);
1215 return create_sub(expression);
1217 return create_arithmetic_binop(expression, new_d_Mul);
1218 case BINEXPR_BITWISE_AND:
1219 return create_arithmetic_binop(expression, new_d_And);
1220 case BINEXPR_BITWISE_OR:
1221 return create_arithmetic_binop(expression, new_d_Or);
1222 case BINEXPR_BITWISE_XOR:
1223 return create_arithmetic_binop(expression, new_d_Eor);
1224 case BINEXPR_SHIFTLEFT:
1225 case BINEXPR_SHIFTRIGHT:
1226 return create_shift(expression);
1229 return create_divmod(expression);
1230 case BINEXPR_LOGICAL_AND:
1231 case BINEXPR_LOGICAL_OR:
1232 return create_lazy_op(expression);
1234 expression_to_firm(expression->left);
1235 return expression_to_firm(expression->right);
1236 case BINEXPR_ADD_ASSIGN:
1237 return create_arithmetic_assign_binop(expression, new_d_Add);
1238 case BINEXPR_SUB_ASSIGN:
1239 return create_arithmetic_assign_binop(expression, new_d_Sub);
1240 case BINEXPR_MUL_ASSIGN:
1241 return create_arithmetic_assign_binop(expression, new_d_Mul);
1242 case BINEXPR_DIV_ASSIGN:
1243 return create_arithmetic_assign_divmod(expression);
1244 case BINEXPR_BITWISE_AND_ASSIGN:
1245 return create_arithmetic_assign_binop(expression, new_d_And);
1246 case BINEXPR_BITWISE_OR_ASSIGN:
1247 return create_arithmetic_assign_binop(expression, new_d_Or);
1248 case BINEXPR_BITWISE_XOR_ASSIGN:
1249 return create_arithmetic_assign_binop(expression, new_d_Eor);
1250 case BINEXPR_SHIFTLEFT_ASSIGN:
1251 return create_arithmetic_assign_binop(expression, new_d_Shl);
1252 case BINEXPR_SHIFTRIGHT_ASSIGN:
1253 return create_arithmetic_assign_binop(expression, new_d_Shr);
1255 panic("TODO binexpr type");
1259 static ir_node *array_access_addr(const array_access_expression_t *expression)
1261 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1265 type_t *type_left = skip_typeref(expression->array_ref->datatype);
1266 type_t *type_right = skip_typeref(expression->index->datatype);
1268 if(type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1269 base_addr = expression_to_firm(expression->array_ref);
1270 offset = expression_to_firm(expression->index);
1272 assert(type_right->type == TYPE_POINTER
1273 || type_right->type == TYPE_ARRAY);
1274 base_addr = expression_to_firm(expression->index);
1275 offset = expression_to_firm(expression->array_ref);
1277 offset = create_conv(dbgi, offset, mode_Iu);
1279 unsigned elem_size = get_type_size(expression->expression.datatype);
1280 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1281 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1283 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1288 static ir_node *array_access_to_firm(
1289 const array_access_expression_t *expression)
1291 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1292 ir_node *addr = array_access_addr(expression);
1293 type_t *type = skip_typeref(expression->expression.datatype);
1294 return deref_address(type, addr, dbgi);
1297 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1299 type_t *type = expression->type;
1301 type = expression->size_expression->datatype;
1302 assert(type != NULL);
1305 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1306 unsigned size = get_type_size(type);
1307 ir_node *size_node = new_Const_long(mode, size);
1312 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1314 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1316 ir_node *cur_block = get_cur_block();
1318 /* create the true block */
1319 ir_node *true_block = new_immBlock();
1321 ir_node *true_val = expression_to_firm(expression->true_expression);
1322 ir_node *true_jmp = new_Jmp();
1324 /* create the false block */
1325 ir_node *false_block = new_immBlock();
1327 ir_node *false_val = expression_to_firm(expression->false_expression);
1328 ir_node *false_jmp = new_Jmp();
1330 /* create the condition evaluation */
1331 set_cur_block(cur_block);
1332 create_condition_evaluation(expression->condition, true_block, false_block);
1333 mature_immBlock(true_block);
1334 mature_immBlock(false_block);
1336 /* create the common block */
1337 ir_node *common_block = new_immBlock();
1338 add_immBlock_pred(common_block, true_jmp);
1339 add_immBlock_pred(common_block, false_jmp);
1340 mature_immBlock(common_block);
1342 /* TODO improve static semantics, so either both or no values are NULL */
1343 if (true_val == NULL || false_val == NULL) return NULL;
1345 ir_node *in[2] = { true_val, false_val };
1346 ir_mode *mode = get_irn_mode(true_val);
1347 assert(get_irn_mode(false_val) == mode);
1348 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1353 static ir_node *select_addr(const select_expression_t *expression)
1355 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1357 ir_node *compound_addr = expression_to_firm(expression->compound);
1359 declaration_t *entry = expression->compound_entry;
1360 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1361 ir_entity *entity = entry->v.entity;
1363 assert(entity != NULL);
1365 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1370 static ir_node *select_to_firm(const select_expression_t *expression)
1372 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1373 ir_node *addr = select_addr(expression);
1374 type_t *type = skip_typeref(expression->expression.datatype);
1375 return deref_address(type, addr, dbgi);
1378 /* Values returned by __builtin_classify_type. */
1379 typedef enum gcc_type_class
1385 enumeral_type_class,
1388 reference_type_class,
1392 function_type_class,
1403 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1405 const type_t *const type = expr->type_expression->datatype;
1411 const atomic_type_t *const atomic_type = (const atomic_type_t*)type;
1412 switch (atomic_type->atype) {
1413 // should not be reached
1414 case ATOMIC_TYPE_INVALID:
1418 // gcc cannot do that
1419 case ATOMIC_TYPE_VOID:
1420 tc = void_type_class;
1423 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1424 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1425 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1426 case ATOMIC_TYPE_SHORT:
1427 case ATOMIC_TYPE_USHORT:
1428 case ATOMIC_TYPE_INT:
1429 case ATOMIC_TYPE_UINT:
1430 case ATOMIC_TYPE_LONG:
1431 case ATOMIC_TYPE_ULONG:
1432 case ATOMIC_TYPE_LONGLONG:
1433 case ATOMIC_TYPE_ULONGLONG:
1434 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1435 tc = integer_type_class;
1438 case ATOMIC_TYPE_FLOAT:
1439 case ATOMIC_TYPE_DOUBLE:
1440 case ATOMIC_TYPE_LONG_DOUBLE:
1441 tc = real_type_class;
1444 #ifdef PROVIDE_COMPLEX
1445 case ATOMIC_TYPE_FLOAT_COMPLEX:
1446 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1447 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1448 tc = complex_type_class;
1450 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1451 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1452 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1453 tc = complex_type_class;
1458 panic("Unimplemented case in classify_type_to_firm().");
1463 case TYPE_ARRAY: // gcc handles this as pointer
1464 case TYPE_FUNCTION: // gcc handles this as pointer
1465 case TYPE_POINTER: tc = pointer_type_class; break;
1466 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1467 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1469 // gcc handles this as integer
1470 case TYPE_ENUM: tc = integer_type_class; break;
1473 panic("Unimplemented case in classify_type_to_firm().");
1476 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1477 ir_mode *const mode = mode_Is;
1478 tarval *const tv = new_tarval_from_long(tc, mode);
1479 return new_d_Const(dbgi, mode, tv);
1482 static ir_node *function_name_to_firm(const string_literal_t *const expr)
1484 if (current_function_name == NULL) {
1485 const source_position_t *const src_pos =
1486 &expr->expression.source_position;
1487 const char *const name = current_function_decl->symbol->string;
1488 current_function_name = string_to_firm(src_pos, "__func__", name);
1491 return current_function_name;
1494 static ir_node *dereference_addr(const unary_expression_t *const expression)
1496 assert(expression->type == UNEXPR_DEREFERENCE);
1497 return expression_to_firm(expression->value);
1500 static ir_node *expression_to_addr(const expression_t *expression)
1502 switch(expression->type) {
1503 case EXPR_REFERENCE:
1504 return reference_addr((const reference_expression_t*) expression);
1505 case EXPR_ARRAY_ACCESS:
1506 return array_access_addr((const array_access_expression_t*) expression);
1508 return select_addr((const select_expression_t*) expression);
1510 const unary_expression_t *const unary_expr =
1511 (const unary_expression_t*)expression;
1512 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1513 return dereference_addr(unary_expr);
1520 panic("trying to get address of non-lvalue");
1523 static ir_node *_expression_to_firm(const expression_t *expression)
1525 switch(expression->type) {
1527 return const_to_firm((const const_t*) expression);
1528 case EXPR_STRING_LITERAL:
1529 return string_literal_to_firm((const string_literal_t*) expression);
1530 case EXPR_REFERENCE:
1531 return reference_expression_to_firm(
1532 (const reference_expression_t*) expression);
1534 return call_expression_to_firm((const call_expression_t*) expression);
1536 return unary_expression_to_firm((const unary_expression_t*) expression);
1538 return binary_expression_to_firm(
1539 (const binary_expression_t*) expression);
1540 case EXPR_ARRAY_ACCESS:
1541 return array_access_to_firm(
1542 (const array_access_expression_t*) expression);
1544 return sizeof_to_firm((const sizeof_expression_t*) expression);
1545 case EXPR_CONDITIONAL:
1546 return conditional_to_firm((const conditional_expression_t*)expression);
1548 return select_to_firm((const select_expression_t*) expression);
1549 case EXPR_CLASSIFY_TYPE:
1550 return classify_type_to_firm((const classify_type_expression_t*)expression);
1552 case EXPR_PRETTY_FUNCTION:
1553 return function_name_to_firm((const string_literal_t*)expression);
1556 case EXPR_STATEMENT:
1557 case EXPR_BUILTIN_SYMBOL:
1558 panic("unimplemented expression found");
1564 panic("invalid expression found");
1567 static ir_node *expression_to_firm(const expression_t *expression)
1569 ir_node *res = _expression_to_firm(expression);
1571 if(res != NULL && get_irn_mode(res) == mode_b) {
1572 ir_mode *mode = get_ir_mode(expression->datatype);
1573 res = create_conv(NULL, res, mode);
1579 static ir_node *expression_to_modeb(const expression_t *expression)
1581 ir_node *res = _expression_to_firm(expression);
1582 res = create_conv(NULL, res, mode_b);
1588 * create a short-circuit expression evaluation that tries to construct
1589 * efficient control flow structures for &&, || and ! expressions
1591 static void create_condition_evaluation(const expression_t *expression,
1592 ir_node *true_block,
1593 ir_node *false_block)
1595 switch(expression->type) {
1597 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1598 if(unary_expression->type == UNEXPR_NOT) {
1599 create_condition_evaluation(unary_expression->value, false_block,
1606 binary_expression_t *binary_expression
1607 = (binary_expression_t*) expression;
1608 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1609 ir_node *cur_block = get_cur_block();
1610 ir_node *extra_block = new_immBlock();
1611 set_cur_block(cur_block);
1612 create_condition_evaluation(binary_expression->left, extra_block,
1614 mature_immBlock(extra_block);
1615 set_cur_block(extra_block);
1616 create_condition_evaluation(binary_expression->right, true_block,
1620 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1621 ir_node *cur_block = get_cur_block();
1622 ir_node *extra_block = new_immBlock();
1623 set_cur_block(cur_block);
1624 create_condition_evaluation(binary_expression->left, true_block,
1626 mature_immBlock(extra_block);
1627 set_cur_block(extra_block);
1628 create_condition_evaluation(binary_expression->right, true_block,
1638 dbg_info *dbgi = get_dbg_info(&expression->source_position);
1639 ir_node *condition = expression_to_modeb(expression);
1640 ir_node *cond = new_d_Cond(dbgi, condition);
1641 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1642 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1644 add_immBlock_pred(true_block, true_proj);
1645 add_immBlock_pred(false_block, false_proj);
1647 set_cur_block(NULL);
1650 static void statement_to_firm(statement_t *statement);
1652 static void return_statement_to_firm(return_statement_t *statement)
1654 if(get_cur_block() == NULL)
1657 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1660 if(statement->return_value != NULL) {
1661 ir_node *retval = expression_to_firm(statement->return_value);
1665 ret = new_d_Return(dbgi, get_store(), 1, in);
1667 ret = new_d_Return(dbgi, get_store(), 0, NULL);
1669 ir_node *end_block = get_irg_end_block(current_ir_graph);
1670 add_immBlock_pred(end_block, ret);
1672 set_cur_block(NULL);
1675 static void compound_statement_to_firm(compound_statement_t *compound)
1677 statement_t *statement = compound->statements;
1678 for( ; statement != NULL; statement = statement->next) {
1679 //context2firm(&statement->context);
1680 statement_to_firm(statement);
1684 static void expression_statement_to_firm(expression_statement_t *statement)
1686 if(get_cur_block() == NULL)
1689 expression_to_firm(statement->expression);
1692 static void if_statement_to_firm(if_statement_t *statement)
1694 ir_node *cur_block = get_cur_block();
1696 ir_node *fallthrough_block = new_immBlock();
1698 /* the true (blocks) */
1699 ir_node *true_block;
1700 if (statement->true_statement != NULL) {
1701 true_block = new_immBlock();
1702 statement_to_firm(statement->true_statement);
1703 if(get_cur_block() != NULL) {
1704 ir_node *jmp = new_Jmp();
1705 add_immBlock_pred(fallthrough_block, jmp);
1708 true_block = fallthrough_block;
1711 /* the false (blocks) */
1712 ir_node *false_block;
1713 if(statement->false_statement != NULL) {
1714 false_block = new_immBlock();
1716 statement_to_firm(statement->false_statement);
1717 if(get_cur_block() != NULL) {
1718 ir_node *jmp = new_Jmp();
1719 add_immBlock_pred(fallthrough_block, jmp);
1722 false_block = fallthrough_block;
1725 /* create the condition */
1726 if(cur_block != NULL) {
1727 set_cur_block(cur_block);
1728 create_condition_evaluation(statement->condition, true_block,
1732 mature_immBlock(true_block);
1733 if(false_block != fallthrough_block) {
1734 mature_immBlock(false_block);
1736 mature_immBlock(fallthrough_block);
1738 set_cur_block(fallthrough_block);
1741 static void while_statement_to_firm(while_statement_t *statement)
1743 ir_node *jmp = NULL;
1744 if(get_cur_block() != NULL) {
1748 /* create the header block */
1749 ir_node *header_block = new_immBlock();
1751 add_immBlock_pred(header_block, jmp);
1754 /* the false block */
1755 ir_node *false_block = new_immBlock();
1758 ir_node *body_block;
1759 if (statement->body != NULL) {
1760 ir_node *old_continue_label = continue_label;
1761 ir_node *old_break_label = break_label;
1762 continue_label = header_block;
1763 break_label = false_block;
1765 body_block = new_immBlock();
1766 statement_to_firm(statement->body);
1768 assert(continue_label == header_block);
1769 assert(break_label == false_block);
1770 continue_label = old_continue_label;
1771 break_label = old_break_label;
1773 if(get_cur_block() != NULL) {
1774 ir_node *jmp = new_Jmp();
1775 add_immBlock_pred(header_block, jmp);
1778 body_block = header_block;
1781 /* create the condition */
1782 set_cur_block(header_block);
1784 create_condition_evaluation(statement->condition, body_block, false_block);
1785 mature_immBlock(body_block);
1786 mature_immBlock(false_block);
1787 mature_immBlock(header_block);
1789 set_cur_block(false_block);
1792 static void do_while_statement_to_firm(do_while_statement_t *statement)
1794 ir_node *jmp = NULL;
1795 if(get_cur_block() != NULL) {
1799 /* create the header block */
1800 ir_node *header_block = new_immBlock();
1802 /* the false block */
1803 ir_node *false_block = new_immBlock();
1806 ir_node *body_block = new_immBlock();
1808 add_immBlock_pred(body_block, jmp);
1811 if (statement->body != NULL) {
1812 ir_node *old_continue_label = continue_label;
1813 ir_node *old_break_label = break_label;
1814 continue_label = header_block;
1815 break_label = false_block;
1817 statement_to_firm(statement->body);
1819 assert(continue_label == header_block);
1820 assert(break_label == false_block);
1821 continue_label = old_continue_label;
1822 break_label = old_break_label;
1824 if (get_cur_block() == NULL) {
1825 mature_immBlock(header_block);
1826 mature_immBlock(body_block);
1827 mature_immBlock(false_block);
1832 ir_node *body_jmp = new_Jmp();
1833 add_immBlock_pred(header_block, body_jmp);
1834 mature_immBlock(header_block);
1836 /* create the condition */
1837 set_cur_block(header_block);
1839 create_condition_evaluation(statement->condition, body_block, false_block);
1840 mature_immBlock(body_block);
1841 mature_immBlock(false_block);
1842 mature_immBlock(header_block);
1844 set_cur_block(false_block);
1847 static void for_statement_to_firm(for_statement_t *statement)
1849 ir_node *jmp = NULL;
1850 if (get_cur_block() != NULL) {
1851 if(statement->initialisation != NULL) {
1852 expression_to_firm(statement->initialisation);
1857 /* create the step block */
1858 ir_node *const step_block = new_immBlock();
1859 if (statement->step != NULL) {
1860 expression_to_firm(statement->step);
1862 ir_node *const step_jmp = new_Jmp();
1864 /* create the header block */
1865 ir_node *const header_block = new_immBlock();
1867 add_immBlock_pred(header_block, jmp);
1869 add_immBlock_pred(header_block, step_jmp);
1871 /* the false block */
1872 ir_node *const false_block = new_immBlock();
1875 ir_node * body_block;
1876 if (statement->body != NULL) {
1877 ir_node *const old_continue_label = continue_label;
1878 ir_node *const old_break_label = break_label;
1879 continue_label = step_block;
1880 break_label = false_block;
1882 body_block = new_immBlock();
1883 statement_to_firm(statement->body);
1885 assert(continue_label == step_block);
1886 assert(break_label == false_block);
1887 continue_label = old_continue_label;
1888 break_label = old_break_label;
1890 if (get_cur_block() != NULL) {
1891 ir_node *const jmp = new_Jmp();
1892 add_immBlock_pred(step_block, jmp);
1895 body_block = step_block;
1898 /* create the condition */
1899 set_cur_block(header_block);
1900 if (statement->condition != NULL) {
1901 create_condition_evaluation(statement->condition, body_block,
1904 keep_alive(header_block);
1905 ir_node *jmp = new_Jmp();
1906 add_immBlock_pred(body_block, jmp);
1909 mature_immBlock(body_block);
1910 mature_immBlock(false_block);
1911 mature_immBlock(step_block);
1912 mature_immBlock(header_block);
1913 mature_immBlock(false_block);
1915 set_cur_block(false_block);
1918 static void create_declaration_entity(declaration_t *declaration,
1919 declaration_type_t declaration_type,
1920 ir_type *parent_type)
1922 ident *id = new_id_from_str(declaration->symbol->string);
1923 ir_type *irtype = get_ir_type(declaration->type);
1924 ir_entity *entity = new_entity(parent_type, id, irtype);
1925 set_entity_ld_ident(entity, id);
1927 declaration->declaration_type = declaration_type;
1928 declaration->v.entity = entity;
1929 set_entity_variability(entity, variability_uninitialized);
1930 /* TODO: visibility? */
1933 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
1935 enum compound_graph_entry_type_t {
1936 COMPOUND_GRAPH_ENTRY_ARRAY,
1937 COMPOUND_GRAPH_ENTRY_COMPOUND
1940 struct compound_graph_path_entry_t {
1946 compound_graph_path_entry_t *prev;
1949 static void create_initializer_list(initializer_list_t *initializer,
1950 type_t *type, ir_entity *entity,
1951 compound_graph_path_entry_t *entry,
1954 static void create_initializer_value(initializer_value_t *initializer,
1956 compound_graph_path_entry_t *entry,
1959 ir_node *node = expression_to_firm(initializer->value);
1961 ir_type *type = get_entity_type(entity);
1962 compound_graph_path *path = new_compound_graph_path(type, len);
1965 for( ; entry != NULL; entry = entry->prev, --i) {
1967 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
1968 set_compound_graph_path_node(path, i, entry->v.entity);
1970 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
1971 set_compound_graph_path_array_index(path, i, entry->v.array_index);
1976 add_compound_ent_value_w_path(entity, node, path);
1979 static void create_initializer_compound(initializer_list_t *initializer,
1980 compound_type_t *type,
1982 compound_graph_path_entry_t *last_entry,
1985 declaration_t *compound_declaration = type->declaration;
1987 declaration_t *compound_entry = compound_declaration->context.declarations;
1989 compound_graph_path_entry_t entry;
1990 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
1991 entry.prev = last_entry;
1995 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
1996 if(compound_entry->symbol == NULL)
1998 if(compound_entry->namespc != NAMESPACE_NORMAL)
2001 if(i >= initializer->len)
2004 entry.v.entity = compound_entry->v.entity;
2006 initializer_t *sub_initializer = initializer->initializers[i];
2008 assert(compound_entry != NULL);
2009 assert(compound_entry->declaration_type
2010 == DECLARATION_TYPE_COMPOUND_MEMBER);
2012 if(sub_initializer->type == INITIALIZER_VALUE) {
2013 create_initializer_value((initializer_value_t*) sub_initializer,
2014 entity, &entry, len);
2016 assert(sub_initializer->type == INITIALIZER_LIST);
2017 create_initializer_list((initializer_list_t*) sub_initializer,
2018 compound_entry->type, entity, &entry, len);
2025 static void create_initializer_array(initializer_list_t *initializer,
2026 array_type_t *type, ir_entity *entity,
2027 compound_graph_path_entry_t *last_entry,
2030 type_t *element_type = type->element_type;
2032 compound_graph_path_entry_t entry;
2033 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2034 entry.prev = last_entry;
2037 for(size_t i = 0; i < initializer->len; ++i) {
2038 entry.v.array_index = i;
2040 initializer_t *sub_initializer = initializer->initializers[i];
2042 if(sub_initializer->type == INITIALIZER_VALUE) {
2043 create_initializer_value((initializer_value_t*) sub_initializer,
2044 entity, &entry, len);
2046 assert(sub_initializer->type == INITIALIZER_LIST);
2047 create_initializer_list((initializer_list_t*) sub_initializer,
2048 element_type, entity, &entry, len);
2053 static void create_initializer_list(initializer_list_t *initializer,
2054 type_t *type, ir_entity *entity,
2055 compound_graph_path_entry_t *entry, int len)
2057 if(type->type == TYPE_ARRAY) {
2058 create_initializer_array(initializer, (array_type_t*) type,
2059 entity, entry, len);
2061 assert(type->type == TYPE_COMPOUND_STRUCT
2062 || type->type == TYPE_COMPOUND_UNION);
2063 create_initializer_compound(initializer, (compound_type_t*) type,
2064 entity, entry, len);
2068 static void create_initializer(declaration_t *declaration)
2070 initializer_t *initializer = declaration->init.initializer;
2071 if(initializer == NULL)
2074 if(initializer->type == INITIALIZER_VALUE) {
2075 initializer_value_t *initializer_value
2076 = (initializer_value_t*) initializer;
2077 ir_node *value = expression_to_firm(initializer_value->value);
2079 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2080 set_value(declaration->v.value_number, value);
2082 ir_entity *entity = declaration->v.entity;
2084 set_entity_variability(entity, variability_initialized);
2085 set_atomic_ent_value(entity, value);
2088 assert(initializer->type == INITIALIZER_LIST);
2089 initializer_list_t *list = (initializer_list_t*) initializer;
2091 declaration_type_t declaration_type = declaration->declaration_type;
2092 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2093 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2095 ir_entity *entity = declaration->v.entity;
2096 set_entity_variability(entity, variability_initialized);
2098 create_initializer_list(list, declaration->type, entity, NULL, 0);
2102 static void create_local_variable(declaration_t *declaration)
2104 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2106 bool needs_entity = declaration->address_taken;
2107 type_t *type = skip_typeref(declaration->type);
2109 if(type->type == TYPE_ARRAY
2110 || type->type == TYPE_COMPOUND_STRUCT
2111 || type->type == TYPE_COMPOUND_UNION) {
2112 needs_entity = true;
2116 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2117 create_declaration_entity(declaration,
2118 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2121 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2122 declaration->v.value_number = next_value_number_function;
2123 ++next_value_number_function;
2126 create_initializer(declaration);
2129 static void declaration_statement_to_firm(declaration_statement_t *statement)
2131 declaration_t *declaration = statement->declarations_begin;
2132 declaration_t *end = statement->declarations_end->next;
2133 for( ; declaration != end; declaration = declaration->next) {
2134 type_t *type = declaration->type;
2136 switch(declaration->storage_class) {
2137 case STORAGE_CLASS_TYPEDEF:
2139 case STORAGE_CLASS_STATIC:
2140 panic("static local vars not implemented yet");
2141 case STORAGE_CLASS_ENUM_ENTRY:
2142 panic("enum entry declaration in local block found");
2143 case STORAGE_CLASS_EXTERN:
2144 panic("extern declaration in local block found");
2145 case STORAGE_CLASS_NONE:
2146 case STORAGE_CLASS_AUTO:
2147 case STORAGE_CLASS_REGISTER:
2148 if(type->type == TYPE_FUNCTION) {
2149 panic("nested functions not supported yet");
2151 create_local_variable(declaration);
2155 panic("invalid storage class found");
2159 static void create_jump_statement(const statement_t *statement,
2160 ir_node *target_block)
2162 if(get_cur_block() == NULL)
2165 dbg_info *dbgi = get_dbg_info(&statement->source_position);
2166 ir_node *jump = new_d_Jmp(dbgi);
2167 add_immBlock_pred(target_block, jump);
2169 set_cur_block(NULL);
2172 static void switch_statement_to_firm(const switch_statement_t *statement)
2174 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2176 ir_node *expression = expression_to_firm(statement->expression);
2177 ir_node *cond = new_d_Cond(dbgi, expression);
2178 ir_node *break_block = new_immBlock();
2180 set_cur_block(NULL);
2182 ir_node *const old_switch_cond = current_switch_cond;
2183 ir_node *const old_break_label = break_label;
2184 const bool old_saw_default_label = saw_default_label;
2185 current_switch_cond = cond;
2186 break_label = break_block;
2188 statement_to_firm(statement->body);
2190 if(get_cur_block() != NULL) {
2191 ir_node *jmp = new_Jmp();
2192 add_immBlock_pred(break_block, jmp);
2195 if (!saw_default_label) {
2196 set_cur_block(get_nodes_block(cond));
2197 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2198 MAGIC_DEFAULT_PN_NUMBER);
2199 add_immBlock_pred(break_block, proj);
2202 assert(current_switch_cond == cond);
2203 assert(break_label == break_block);
2204 current_switch_cond = old_switch_cond;
2205 break_label = old_break_label;
2206 saw_default_label = old_saw_default_label;
2208 mature_immBlock(break_block);
2209 set_cur_block(break_block);
2212 static long fold_constant(const expression_t *expression)
2214 ir_graph *old_current_ir_graph = current_ir_graph;
2215 current_ir_graph = get_const_code_irg();
2217 ir_node *cnst = expression_to_firm(expression);
2218 if(!is_Const(cnst)) {
2219 panic("couldn't fold constantl");
2221 tarval *tv = get_Const_tarval(cnst);
2222 if(!tarval_is_long(tv)) {
2223 panic("folded constant not an integer");
2226 long res = get_tarval_long(tv);
2228 current_ir_graph = old_current_ir_graph;
2232 static void case_label_to_firm(const case_label_statement_t *statement)
2234 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2236 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2238 /* let's create a node and hope firm constant folding creates a Const
2241 set_cur_block(get_nodes_block(current_switch_cond));
2242 if(statement->expression) {
2243 long pn = fold_constant(statement->expression);
2244 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2245 /* oops someone detected our cheating... */
2246 panic("magic default pn used");
2248 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2250 saw_default_label = true;
2251 proj = new_d_defaultProj(dbgi, current_switch_cond,
2252 MAGIC_DEFAULT_PN_NUMBER);
2255 ir_node *block = new_immBlock();
2256 if (fallthrough != NULL) {
2257 add_immBlock_pred(block, fallthrough);
2259 add_immBlock_pred(block, proj);
2260 mature_immBlock(block);
2262 statement_to_firm(statement->label_statement);
2265 static ir_node *get_label_block(declaration_t *label)
2267 assert(label->namespc == NAMESPACE_LABEL);
2269 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2270 return label->v.block;
2272 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2274 ir_node *old_cur_block = get_cur_block();
2275 ir_node *block = new_immBlock();
2276 set_cur_block(old_cur_block);
2278 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2279 label->v.block = block;
2281 ARR_APP1(ir_node *, imature_blocks, block);
2286 static void label_to_firm(const label_statement_t *statement)
2288 ir_node *block = get_label_block(statement->label);
2290 if(get_cur_block() != NULL) {
2291 ir_node *jmp = new_Jmp();
2292 add_immBlock_pred(block, jmp);
2295 set_cur_block(block);
2298 statement_to_firm(statement->label_statement);
2301 static void goto_to_firm(const goto_statement_t *statement)
2303 if(get_cur_block() == NULL)
2306 ir_node *block = get_label_block(statement->label);
2307 ir_node *jmp = new_Jmp();
2308 add_immBlock_pred(block, jmp);
2310 set_cur_block(NULL);
2313 static void statement_to_firm(statement_t *statement)
2315 switch(statement->type) {
2316 case STATEMENT_COMPOUND:
2317 compound_statement_to_firm((compound_statement_t*) statement);
2319 case STATEMENT_RETURN:
2320 return_statement_to_firm((return_statement_t*) statement);
2322 case STATEMENT_EXPRESSION:
2323 expression_statement_to_firm((expression_statement_t*) statement);
2326 if_statement_to_firm((if_statement_t*) statement);
2328 case STATEMENT_WHILE:
2329 while_statement_to_firm((while_statement_t*) statement);
2331 case STATEMENT_DO_WHILE:
2332 do_while_statement_to_firm((do_while_statement_t*) statement);
2334 case STATEMENT_DECLARATION:
2335 declaration_statement_to_firm((declaration_statement_t*) statement);
2337 case STATEMENT_BREAK:
2338 create_jump_statement(statement, break_label);
2340 case STATEMENT_CONTINUE:
2341 create_jump_statement(statement, continue_label);
2343 case STATEMENT_SWITCH:
2344 switch_statement_to_firm((switch_statement_t*) statement);
2346 case STATEMENT_CASE_LABEL:
2347 case_label_to_firm((case_label_statement_t*) statement);
2350 for_statement_to_firm((for_statement_t*) statement);
2352 case STATEMENT_LABEL:
2353 label_to_firm((label_statement_t*) statement);
2355 case STATEMENT_GOTO:
2356 goto_to_firm((goto_statement_t*) statement);
2361 panic("Statement not implemented\n");
2364 static int count_local_declarations(const declaration_t * decl,
2365 const declaration_t *const end)
2368 for (; decl != end; decl = decl->next) {
2369 const type_t *type = skip_typeref(decl->type);
2370 switch (type->type) {
2374 if (!decl->address_taken) ++count;
2383 static int count_decls_in_stmts(const statement_t *stmt)
2386 for (; stmt != NULL; stmt = stmt->next) {
2387 switch (stmt->type) {
2388 case STATEMENT_DECLARATION: {
2389 const declaration_statement_t *const decl_stmt =
2390 (const declaration_statement_t*)stmt;
2391 count += count_local_declarations(decl_stmt->declarations_begin,
2392 decl_stmt->declarations_end->next);
2396 case STATEMENT_COMPOUND: {
2397 const compound_statement_t *const comp =
2398 (const compound_statement_t*)stmt;
2399 count += count_decls_in_stmts(comp->statements);
2403 case STATEMENT_IF: {
2404 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2405 count += count_decls_in_stmts(if_stmt->true_statement);
2406 count += count_decls_in_stmts(if_stmt->false_statement);
2410 case STATEMENT_SWITCH: {
2411 const switch_statement_t *const switch_stmt =
2412 (const switch_statement_t*)stmt;
2413 count += count_decls_in_stmts(switch_stmt->body);
2417 case STATEMENT_LABEL: {
2418 const label_statement_t *const label_stmt =
2419 (const label_statement_t*)stmt;
2420 count += count_decls_in_stmts(label_stmt->label_statement);
2424 case STATEMENT_WHILE: {
2425 const while_statement_t *const while_stmt =
2426 (const while_statement_t*)stmt;
2427 count += count_decls_in_stmts(while_stmt->body);
2431 case STATEMENT_DO_WHILE: {
2432 const do_while_statement_t *const do_while_stmt =
2433 (const do_while_statement_t*)stmt;
2434 count += count_decls_in_stmts(do_while_stmt->body);
2438 case STATEMENT_FOR: {
2439 const for_statement_t *const for_stmt =
2440 (const for_statement_t*)stmt;
2441 /* TODO initialisation */
2442 count += count_decls_in_stmts(for_stmt->body);
2446 case STATEMENT_BREAK:
2447 case STATEMENT_CASE_LABEL:
2448 case STATEMENT_CONTINUE:
2449 case STATEMENT_EXPRESSION:
2450 case STATEMENT_GOTO:
2451 case STATEMENT_INVALID:
2452 case STATEMENT_RETURN:
2459 static int get_function_n_local_vars(declaration_t *declaration)
2463 /* count parameters */
2464 count += count_local_declarations(declaration->context.declarations, NULL);
2466 /* count local variables declared in body */
2467 count += count_decls_in_stmts(declaration->init.statement);
2472 static void initialize_function_parameters(declaration_t *declaration)
2474 ir_graph *irg = current_ir_graph;
2475 ir_node *args = get_irg_args(irg);
2476 ir_node *start_block = get_irg_start_block(irg);
2477 ir_type *function_irtype = get_ir_type(declaration->type);
2480 declaration_t *parameter = declaration->context.declarations;
2481 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2482 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2483 type_t *type = parameter->type;
2485 bool needs_entity = parameter->address_taken;
2486 if(type->type == TYPE_COMPOUND_STRUCT
2487 || type->type == TYPE_COMPOUND_UNION) {
2488 needs_entity = true;
2492 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2493 ident *id = new_id_from_str(parameter->symbol->string);
2494 set_entity_ident(entity, id);
2496 parameter->declaration_type
2497 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2498 parameter->v.entity = entity;
2502 ir_mode *mode = get_ir_mode(parameter->type);
2504 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2506 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2507 parameter->v.value_number = next_value_number_function;
2508 ++next_value_number_function;
2510 set_value(parameter->v.value_number, proj);
2514 static void create_function(declaration_t *declaration)
2516 ir_entity *entity = get_function_entity(declaration);
2518 if(declaration->init.statement == NULL)
2521 current_function_decl = declaration;
2522 current_function_name = NULL;
2524 assert(imature_blocks == NULL);
2525 imature_blocks = NEW_ARR_F(ir_node*, 0);
2527 int n_local_vars = get_function_n_local_vars(declaration);
2528 ir_graph *irg = new_ir_graph(entity, n_local_vars);
2529 ir_node *first_block = get_cur_block();
2531 next_value_number_function = 0;
2532 initialize_function_parameters(declaration);
2534 statement_to_firm(declaration->init.statement);
2536 ir_node *end_block = get_irg_end_block(irg);
2538 /* do we have a return statement yet? */
2539 if(get_cur_block() != NULL) {
2540 assert(declaration->type->type == TYPE_FUNCTION);
2541 const function_type_t* const func_type
2542 = (const function_type_t*) declaration->type;
2544 if (func_type->result_type == type_void) {
2545 ret = new_Return(get_store(), 0, NULL);
2547 ir_mode *const mode = get_ir_mode(func_type->result_type);
2549 // ยง5.1.2.2.3 main implicitly returns 0
2550 if (strcmp(declaration->symbol->string, "main") == 0) {
2551 in[0] = new_Const(mode, get_mode_null(mode));
2553 in[0] = new_Unknown(mode);
2555 ret = new_Return(get_store(), 1, in);
2557 add_immBlock_pred(end_block, ret);
2560 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2561 mature_immBlock(imature_blocks[i]);
2563 DEL_ARR_F(imature_blocks);
2564 imature_blocks = NULL;
2566 mature_immBlock(first_block);
2567 mature_immBlock(end_block);
2569 irg_finalize_cons(irg);
2571 /* finalize the frame type */
2572 ir_type *frame_type = get_irg_frame_type(irg);
2573 int n = get_compound_n_members(frame_type);
2576 for(int i = 0; i < n; ++i) {
2577 ir_entity *entity = get_compound_member(frame_type, i);
2578 ir_type *entity_type = get_entity_type(entity);
2580 int align = get_type_alignment_bytes(entity_type);
2581 if(align > align_all)
2585 misalign = offset % align;
2587 offset += align - misalign;
2591 set_entity_offset(entity, offset);
2592 offset += get_type_size_bytes(entity_type);
2594 set_type_size_bytes(frame_type, offset);
2595 set_type_alignment_bytes(frame_type, align_all);
2596 set_type_state(frame_type, layout_fixed);
2601 static void create_global_variable(declaration_t *declaration)
2603 ir_type *global_type = get_glob_type();
2604 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2607 ir_entity *entity = declaration->v.entity;
2608 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2609 set_entity_visibility(entity, visibility_local);
2610 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2611 set_entity_visibility(entity, visibility_external_allocated);
2613 set_entity_visibility(entity, visibility_external_visible);
2615 current_ir_graph = get_const_code_irg();
2616 create_initializer(declaration);
2619 static void context_to_firm(context_t *context)
2621 declaration_t *declaration = context->declarations;
2622 for( ; declaration != NULL; declaration = declaration->next) {
2623 if(declaration->namespc != NAMESPACE_NORMAL)
2625 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2626 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2628 if(declaration->symbol == NULL)
2631 type_t *type = declaration->type;
2632 if(type->type == TYPE_FUNCTION) {
2633 create_function(declaration);
2635 create_global_variable(declaration);
2640 void translation_unit_to_firm(translation_unit_t *unit)
2642 /* just to be sure */
2643 continue_label = NULL;
2645 current_switch_cond = NULL;
2647 context_to_firm(& unit->context);