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 typedef enum declaration_type_t {
39 DECLARATION_TYPE_UNKNOWN,
40 DECLARATION_TYPE_FUNCTION,
41 DECLARATION_TYPE_GLOBAL_VARIABLE,
42 DECLARATION_TYPE_LOCAL_VARIABLE,
43 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
44 DECLARATION_TYPE_COMPOUND_MEMBER,
45 DECLARATION_TYPE_LABEL_BLOCK,
48 static ir_type *get_ir_type(type_t *type);
50 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
54 const declaration_t *declaration = & value_numbers[pos]->declaration;
56 print_warning_prefix(declaration->source_position);
57 fprintf(stderr, "variable '%s' might be used uninitialized\n",
58 declaration->symbol->string);
60 fprintf(stderr, "Some variable might be used uninitialized\n");
61 return new_r_Unknown(irg, mode);
64 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
66 const source_position_t *pos = (const source_position_t*) dbg;
69 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
73 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
75 const source_position_t *pos = (const source_position_t*) dbg;
80 return pos->input_name;
83 void init_ast2firm(void)
85 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
86 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
87 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
89 ir_type_int = get_ir_type(type_int);
90 ir_type_const_char = get_ir_type(type_const_char);
91 ir_type_void = get_ir_type(type_int); /* we don't have a real void
93 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
94 ir_type_void, mode_P_data);
96 type_void->firm_type = ir_type_void;
99 void exit_ast2firm(void)
103 static unsigned unique_id = 0;
105 static ident *unique_ident(const char *tag)
109 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
111 return new_id_from_str(buf);
114 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
116 switch(atomic_type->atype) {
117 case ATOMIC_TYPE_SCHAR:
118 case ATOMIC_TYPE_CHAR:
120 case ATOMIC_TYPE_UCHAR:
122 case ATOMIC_TYPE_SHORT:
124 case ATOMIC_TYPE_USHORT:
126 case ATOMIC_TYPE_LONG:
127 case ATOMIC_TYPE_INT:
129 case ATOMIC_TYPE_ULONG:
130 case ATOMIC_TYPE_UINT:
132 case ATOMIC_TYPE_LONGLONG:
134 case ATOMIC_TYPE_ULONGLONG:
136 case ATOMIC_TYPE_FLOAT:
138 case ATOMIC_TYPE_DOUBLE:
140 case ATOMIC_TYPE_LONG_DOUBLE:
142 case ATOMIC_TYPE_BOOL:
144 #ifdef PROVIDE_COMPLEX
145 case ATOMIC_TYPE_FLOAT_COMPLEX:
146 case ATOMIC_TYPE_DOUBLE_COMPLEX:
147 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
148 panic("complex lowering not implemented yet");
150 case ATOMIC_TYPE_FLOAT_IMAGINARY:
151 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
152 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
153 panic("imaginary lowering not implemented yet");
156 case ATOMIC_TYPE_VOID:
157 /* firm has no real void... */
159 case ATOMIC_TYPE_INVALID:
162 panic("Encountered unknown atomic type");
166 static unsigned get_type_size(type_t *type);
168 static unsigned get_atomic_type_size(const atomic_type_t *type)
170 switch(type->atype) {
171 case ATOMIC_TYPE_CHAR:
172 case ATOMIC_TYPE_SCHAR:
173 case ATOMIC_TYPE_UCHAR:
176 case ATOMIC_TYPE_SHORT:
177 case ATOMIC_TYPE_USHORT:
180 case ATOMIC_TYPE_BOOL:
181 case ATOMIC_TYPE_INT:
182 case ATOMIC_TYPE_UINT:
183 case ATOMIC_TYPE_LONG:
184 case ATOMIC_TYPE_ULONG:
185 case ATOMIC_TYPE_FLOAT:
188 case ATOMIC_TYPE_LONGLONG:
189 case ATOMIC_TYPE_ULONGLONG:
190 case ATOMIC_TYPE_DOUBLE:
193 case ATOMIC_TYPE_LONG_DOUBLE:
196 case ATOMIC_TYPE_VOID:
199 case ATOMIC_TYPE_INVALID:
202 panic("Trying to determine size of invalid atomic type");
205 static unsigned get_compound_type_size(compound_type_t *type)
207 ir_type *irtype = get_ir_type(&type->type);
208 return get_type_size_bytes(irtype);
211 static unsigned get_array_type_size(array_type_t *type)
213 ir_type *irtype = get_ir_type(&type->type);
214 return get_type_size_bytes(irtype);
217 static unsigned get_type_size(type_t *type)
219 type = skip_typeref(type);
223 return get_atomic_type_size((const atomic_type_t*) type);
225 return get_mode_size_bytes(mode_Is);
226 case TYPE_COMPOUND_UNION:
227 case TYPE_COMPOUND_STRUCT:
228 return get_compound_type_size((compound_type_t*) type);
230 /* just a pointer to the function */
231 return get_mode_size_bytes(mode_P_code);
233 return get_mode_size_bytes(mode_P_data);
235 return get_array_type_size((array_type_t*) type);
242 panic("Trying to determine size of invalid type");
245 static unsigned count_parameters(const function_type_t *function_type)
249 function_parameter_t *parameter = function_type->parameters;
250 for ( ; parameter != NULL; parameter = parameter->next) {
260 static long fold_constant(const expression_t *expression);
262 static ir_type *create_atomic_type(const atomic_type_t *type)
264 ir_mode *mode = get_atomic_mode(type);
265 ident *id = get_mode_ident(mode);
266 ir_type *irtype = new_type_primitive(id, mode);
271 static ir_type *create_method_type(const function_type_t *function_type)
273 type_t *result_type = function_type->result_type;
275 ident *id = unique_ident("functiontype");
276 int n_parameters = count_parameters(function_type);
277 int n_results = result_type == type_void ? 0 : 1;
278 ir_type *irtype = new_type_method(id, n_parameters, n_results);
280 if(result_type != type_void) {
281 ir_type *restype = get_ir_type(result_type);
282 set_method_res_type(irtype, 0, restype);
285 function_parameter_t *parameter = function_type->parameters;
287 for( ; parameter != NULL; parameter = parameter->next) {
288 ir_type *p_irtype = get_ir_type(parameter->type);
289 set_method_param_type(irtype, n, p_irtype);
293 if(function_type->variadic || function_type->unspecified_parameters) {
294 set_method_variadicity(irtype, variadicity_variadic);
300 static ir_type *create_pointer_type(pointer_type_t *type)
302 type_t *points_to = type->points_to;
303 ir_type *ir_points_to;
304 /* Avoid endless recursion if the points_to type contains this poiner type
305 * again (might be a struct). We therefore first create a void* pointer
306 * and then set the real points_to type
308 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
309 ir_type_void, mode_P_data);
310 type->type.firm_type = ir_type;
312 ir_points_to = get_ir_type(points_to);
313 set_pointer_points_to_type(ir_type, ir_points_to);
318 static ir_type *create_array_type(array_type_t *type)
320 type_t *element_type = type->element_type;
321 ir_type *ir_element_type = get_ir_type(element_type);
323 ident *id = unique_ident("array");
324 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
326 if(type->size != NULL) {
327 int n_elements = fold_constant(type->size);
329 set_array_bounds_int(ir_type, 0, 0, n_elements);
331 size_t elemsize = get_type_size_bytes(ir_element_type);
332 int align = get_type_alignment_bytes(ir_element_type);
333 if(elemsize % align > 0) {
334 elemsize += align - (elemsize % align);
336 set_type_size_bytes(ir_type, n_elements * elemsize);
337 set_type_alignment_bytes(ir_type, align);
338 set_type_state(ir_type, layout_fixed);
344 #define INVALID_TYPE ((ir_type_ptr)-1)
346 static ir_type *create_struct_type(compound_type_t *type)
348 symbol_t *symbol = type->declaration->symbol;
351 id = unique_ident(symbol->string);
353 id = unique_ident("__anonymous_struct");
355 ir_type *ir_type = new_type_struct(id);
357 type->type.firm_type = ir_type;
361 declaration_t *entry = type->declaration->context.declarations;
362 for( ; entry != NULL; entry = entry->next) {
363 if(entry->namespc != NAMESPACE_NORMAL)
366 ident *ident = new_id_from_str(entry->symbol->string);
367 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
369 int entry_size = get_type_size_bytes(entry_ir_type);
370 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
371 int misalign = offset % entry_alignment;
373 offset += entry_alignment - misalign;
375 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
376 set_entity_offset(entity, offset);
377 add_struct_member(ir_type, entity);
378 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
379 entry->v.entity = entity;
381 offset += entry_size;
382 if(entry_alignment > align_all) {
383 if(entry_alignment % align_all != 0) {
384 panic("Uneven alignments not supported yet");
386 align_all = entry_alignment;
390 int misalign = offset % align_all;
392 set_type_alignment_bytes(ir_type, align_all);
393 set_type_size_bytes(ir_type, offset);
394 set_type_state(ir_type, layout_fixed);
399 static ir_type *create_union_type(compound_type_t *type)
401 declaration_t *declaration = type->declaration;
402 symbol_t *symbol = declaration->symbol;
405 id = unique_ident(symbol->string);
407 id = unique_ident("__anonymous_union");
409 ir_type *ir_type = new_type_union(id);
411 type->type.firm_type = ir_type;
415 declaration_t *entry = declaration->context.declarations;
416 for( ; entry != NULL; entry = entry->next) {
417 if(entry->namespc != NAMESPACE_NORMAL)
420 ident *ident = new_id_from_str(entry->symbol->string);
421 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
423 int entry_size = get_type_size_bytes(entry_ir_type);
424 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
426 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
427 add_union_member(ir_type, entity);
428 set_entity_offset(entity, 0);
429 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
430 entry->v.entity = entity;
432 if(entry_size > size) {
435 if(entry_alignment > align_all) {
436 if(entry_alignment % align_all != 0) {
437 panic("Uneven alignments not supported yet");
439 align_all = entry_alignment;
443 set_type_alignment_bytes(ir_type, align_all);
444 set_type_size_bytes(ir_type, size);
445 set_type_state(ir_type, layout_fixed);
450 static ir_type *get_ir_type(type_t *type)
452 assert(type != NULL);
454 type = skip_typeref(type);
456 if(type->firm_type != NULL) {
457 assert(type->firm_type != INVALID_TYPE);
458 return type->firm_type;
461 ir_type *firm_type = NULL;
464 firm_type = create_atomic_type((atomic_type_t*) type);
467 firm_type = create_method_type((function_type_t*) type);
470 firm_type = create_pointer_type((pointer_type_t*) type);
473 firm_type = create_array_type((array_type_t*) type);
475 case TYPE_COMPOUND_STRUCT:
476 firm_type = create_struct_type((compound_type_t*) type);
478 case TYPE_COMPOUND_UNION:
479 firm_type = create_union_type((compound_type_t*) type);
482 firm_type = ir_type_int;
490 if(firm_type == NULL)
491 panic("unknown type found");
493 type->firm_type = firm_type;
497 static inline ir_mode *get_ir_mode(type_t *type)
499 ir_type *irtype = get_ir_type(type);
501 /* firm doesn't report a mode for arrays somehow... */
502 if(is_Array_type(irtype)) {
506 ir_mode *mode = get_type_mode(irtype);
507 assert(mode != NULL);
511 static ir_entity* get_function_entity(declaration_t *declaration)
513 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
514 return declaration->v.entity;
515 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
517 symbol_t *symbol = declaration->symbol;
518 ident *id = new_id_from_str(symbol->string);
520 ir_type *global_type = get_glob_type();
521 ir_type *ir_type_method = get_ir_type(declaration->type);
522 assert(is_Method_type(ir_type_method));
524 ir_entity *entity = new_entity(global_type, id, ir_type_method);
525 set_entity_ld_ident(entity, id);
526 if(declaration->storage_class == STORAGE_CLASS_STATIC
527 || declaration->is_inline) {
528 set_entity_visibility(entity, visibility_local);
529 } else if(declaration->init.statement != NULL) {
530 set_entity_visibility(entity, visibility_external_visible);
532 set_entity_visibility(entity, visibility_external_allocated);
535 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
536 declaration->v.entity = entity;
543 static ir_node *expression_to_firm(const expression_t *expression);
545 static dbg_info *get_dbg_info(const source_position_t *pos)
547 return (dbg_info*) pos;
550 static ir_node *const_to_firm(const const_t *cnst)
552 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
553 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
556 if(mode_is_float(mode)) {
557 tv = new_tarval_from_double(cnst->v.float_value, mode);
559 tv = new_tarval_from_long(cnst->v.int_value, mode);
562 return new_d_Const(dbgi, mode, tv);
565 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
567 assert(entity != NULL);
568 union symconst_symbol sym;
569 sym.entity_p = entity;
570 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
573 static ir_node *string_literal_to_firm(const string_literal_t* literal)
575 ir_type *global_type = get_glob_type();
576 ir_type *type = new_type_array(unique_ident("strtype"), 1,
579 ident *id = unique_ident("Lstr");
580 ir_entity *entity = new_entity(global_type, id, type);
581 set_entity_ld_ident(entity, id);
582 set_entity_variability(entity, variability_constant);
584 ir_type *elem_type = ir_type_const_char;
585 ir_mode *mode = get_type_mode(elem_type);
587 const char *string = literal->value;
588 size_t slen = strlen(string) + 1;
590 set_array_lower_bound_int(type, 0, 0);
591 set_array_upper_bound_int(type, 0, slen);
592 set_type_size_bytes(type, slen);
593 set_type_state(type, layout_fixed);
595 tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
596 for(size_t i = 0; i < slen; ++i) {
597 tvs[i] = new_tarval_from_long(string[i], mode);
600 set_array_entity_values(entity, tvs, slen);
603 dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
605 return create_symconst(dbgi, entity);
608 static ir_node *deref_address(type_t *const type, ir_node *const addr,
609 dbg_info *const dbgi)
611 switch (type->type) {
613 case TYPE_COMPOUND_STRUCT:
614 case TYPE_COMPOUND_UNION:
618 ir_mode *const mode = get_ir_mode(type);
619 ir_node *const memory = get_store();
620 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
621 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
622 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
629 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
631 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
632 declaration_t *declaration = ref->declaration;
633 type_t *type = skip_typeref(declaration->type);
635 switch((declaration_type_t) declaration->declaration_type) {
636 case DECLARATION_TYPE_UNKNOWN:
638 case DECLARATION_TYPE_LOCAL_VARIABLE: {
639 ir_mode *mode = get_ir_mode(type);
640 return get_value(declaration->v.value_number, mode);
642 case DECLARATION_TYPE_FUNCTION: {
643 return create_symconst(dbgi, declaration->v.entity);
645 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
646 ir_entity *entity = declaration->v.entity;
647 ir_node *symconst = create_symconst(dbgi, entity);
648 return deref_address(type, symconst, dbgi);
650 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
651 ir_entity *entity = declaration->v.entity;
652 ir_node *frame = get_irg_frame(current_ir_graph);
653 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
654 return deref_address(type, sel, dbgi);
657 case DECLARATION_TYPE_COMPOUND_MEMBER:
658 case DECLARATION_TYPE_LABEL_BLOCK:
659 panic("not implemented reference type");
662 panic("reference to declaration with unknown type found");
665 static ir_node *reference_addr(const reference_expression_t *ref)
667 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
668 declaration_t *declaration = ref->declaration;
670 switch((declaration_type_t) declaration->declaration_type) {
671 case DECLARATION_TYPE_UNKNOWN:
673 case DECLARATION_TYPE_LOCAL_VARIABLE:
674 panic("local variable without entity has no address");
675 case DECLARATION_TYPE_FUNCTION: {
676 return create_symconst(dbgi, declaration->v.entity);
678 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
679 ir_entity *entity = declaration->v.entity;
680 ir_node *symconst = create_symconst(dbgi, entity);
683 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
684 ir_entity *entity = declaration->v.entity;
685 ir_node *frame = get_irg_frame(current_ir_graph);
686 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
690 case DECLARATION_TYPE_COMPOUND_MEMBER:
691 case DECLARATION_TYPE_LABEL_BLOCK:
692 panic("not implemented reference type");
695 panic("reference to declaration with unknown type found");
698 static ir_node *call_expression_to_firm(const call_expression_t *call)
700 assert(get_cur_block() != NULL);
702 expression_t *function = call->function;
703 ir_node *callee = expression_to_firm(function);
705 function_type_t *function_type;
706 if (function->datatype->type == TYPE_POINTER) {
707 pointer_type_t *const ptr_type = (pointer_type_t*)function->datatype;
708 assert(ptr_type->points_to->type == TYPE_FUNCTION);
709 function_type = (function_type_t*)ptr_type->points_to;
711 assert(function->datatype->type == TYPE_FUNCTION);
712 function_type = (function_type_t*)function->datatype;
715 int n_parameters = 0;
716 call_argument_t *argument = call->arguments;
717 for( ; argument != NULL; argument = argument->next) {
721 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
722 ir_type *new_method_type = NULL;
723 if(function_type->variadic || function_type->unspecified_parameters) {
724 /* we need to construct a new method type matching the call
726 int n_res = get_method_n_ress(ir_method_type);
727 new_method_type = new_type_method(unique_ident("calltype"),
728 n_parameters, n_res);
729 set_method_calling_convention(new_method_type,
730 get_method_calling_convention(ir_method_type));
731 set_method_additional_properties(new_method_type,
732 get_method_additional_properties(ir_method_type));
734 for(int i = 0; i < n_res; ++i) {
735 set_method_res_type(new_method_type, i,
736 get_method_res_type(ir_method_type, i));
739 ir_node *in[n_parameters];
741 argument = call->arguments;
743 for( ; argument != NULL; argument = argument->next) {
744 expression_t *expression = argument->expression;
745 ir_node *arg_node = expression_to_firm(expression);
748 if(new_method_type != NULL) {
749 ir_type *irtype = get_ir_type(expression->datatype);
750 set_method_param_type(new_method_type, n, irtype);
755 assert(n == n_parameters);
757 if(new_method_type != NULL)
758 ir_method_type = new_method_type;
760 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
761 ir_node *store = get_store();
762 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
764 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
767 type_t *result_type = function_type->result_type;
768 ir_node *result = NULL;
769 if(result_type != type_void) {
770 ir_mode *mode = get_ir_mode(result_type);
771 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
772 result = new_d_Proj(dbgi, resproj, mode, 0);
778 static ir_node *expression_to_addr(const expression_t *expression);
779 static void create_condition_evaluation(const expression_t *expression,
781 ir_node *false_block);
783 static void set_value_for_expression(const expression_t *expression,
786 if(expression->type == EXPR_REFERENCE) {
787 reference_expression_t *ref = (reference_expression_t*) expression;
789 declaration_t *declaration = ref->declaration;
790 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
791 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
792 set_value(declaration->v.value_number, value);
797 dbg_info *dbgi = get_dbg_info(&expression->source_position);
798 ir_node *addr = expression_to_addr(expression);
799 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
800 ir_node *memory = get_store();
801 ir_node *store = new_d_Store(dbgi, memory, addr, value);
802 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
803 set_store(store_mem);
806 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
808 ir_mode *value_mode = get_irn_mode(value);
810 if (value_mode == dest_mode || is_Bad(value))
813 if(dest_mode == mode_b) {
814 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
815 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
816 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
820 return new_d_Conv(dbgi, value, dest_mode);
823 static ir_node *create_incdec(const unary_expression_t *expression)
825 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
826 type_t *type = expression->expression.datatype;
827 ir_mode *mode = get_ir_mode(type);
828 expression_t *value = expression->value;
830 ir_node *value_node = expression_to_firm(value);
833 if(type->type == TYPE_POINTER) {
834 pointer_type_t *pointer_type = (pointer_type_t*) type;
835 unsigned elem_size = get_type_size(pointer_type->points_to);
836 offset = new_Const_long(mode_Is, elem_size);
838 assert(is_type_arithmetic(type));
839 offset = new_Const(mode, get_mode_one(mode));
843 switch(expression->type) {
844 case UNEXPR_POSTFIX_INCREMENT: {
845 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
846 set_value_for_expression(value, new_value);
849 case UNEXPR_POSTFIX_DECREMENT: {
850 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
851 set_value_for_expression(value, new_value);
854 case UNEXPR_PREFIX_INCREMENT: {
855 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
856 set_value_for_expression(value, new_value);
859 case UNEXPR_PREFIX_DECREMENT: {
860 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
861 set_value_for_expression(value, new_value);
865 panic("no incdec expr in create_incdec");
871 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
873 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
874 type_t *type = expression->expression.datatype;
876 if(expression->type == UNEXPR_TAKE_ADDRESS)
877 return expression_to_addr(expression->value);
879 const expression_t *value = expression->value;
880 ir_node *value_node = expression_to_firm(value);
882 switch(expression->type) {
884 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
887 case UNEXPR_BITWISE_NEGATE:
888 return new_d_Not(dbgi, value_node, get_ir_mode(type));
890 if(get_irn_mode(value_node) != mode_b) {
891 value_node = create_conv(dbgi, value_node, mode_b);
893 value_node = new_d_Not(dbgi, value_node, mode_b);
894 ir_mode *const mode = get_ir_mode(type);
896 value_node = create_conv(dbgi, value_node, mode);
900 case UNEXPR_DEREFERENCE:
901 return deref_address(type, value_node, dbgi);
902 case UNEXPR_POSTFIX_INCREMENT:
903 case UNEXPR_POSTFIX_DECREMENT:
904 case UNEXPR_PREFIX_INCREMENT:
905 case UNEXPR_PREFIX_DECREMENT:
906 return create_incdec(expression);
908 return create_conv(dbgi, value_node, get_ir_mode(type));
910 case UNEXPR_TAKE_ADDRESS:
914 panic("invalid UNEXPR type found");
917 static long get_pnc(binary_expression_type_t type)
920 case BINEXPR_EQUAL: return pn_Cmp_Eq;
921 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
922 case BINEXPR_LESS: return pn_Cmp_Lt;
923 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
924 case BINEXPR_GREATER: return pn_Cmp_Gt;
925 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
929 panic("trying to get pn_Cmp from non-comparison binexpr type");
932 static ir_node *create_lazy_op(const binary_expression_t *expression)
934 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
935 type_t *type = expression->expression.datatype;
936 ir_mode *mode = get_ir_mode(type);
938 ir_node *cur_block = get_cur_block();
940 ir_node *one_block = new_immBlock();
941 ir_node *one = new_Const(mode, get_mode_one(mode));
942 ir_node *jmp_one = new_d_Jmp(dbgi);
944 ir_node *zero_block = new_immBlock();
945 ir_node *zero = new_Const(mode, get_mode_null(mode));
946 ir_node *jmp_zero = new_d_Jmp(dbgi);
948 set_cur_block(cur_block);
949 create_condition_evaluation((const expression_t*) expression,
950 one_block, zero_block);
951 mature_immBlock(one_block);
952 mature_immBlock(zero_block);
954 ir_node *common_block = new_immBlock();
955 add_immBlock_pred(common_block, jmp_one);
956 add_immBlock_pred(common_block, jmp_zero);
957 mature_immBlock(common_block);
959 ir_node *in[2] = { one, zero };
960 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
965 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
966 ir_node *right, ir_mode *mode);
968 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
969 create_arithmetic_func func)
971 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
972 ir_node *left = expression_to_firm(expression->left);
973 ir_node *right = expression_to_firm(expression->right);
974 type_t *type = expression->right->datatype;
975 /* be careful with the modes, because in arithmetic assign nodes only
976 * the right operand has the mode of the arithmetic already */
977 ir_mode *mode = get_ir_mode(type);
978 left = create_conv(dbgi, left, mode);
979 ir_node *res = func(dbgi, left, right, mode);
984 static ir_node *pointer_arithmetic(ir_node *const pointer,
987 dbg_info *const dbgi,
988 const create_arithmetic_func func)
990 pointer_type_t *const pointer_type = (pointer_type_t*)type;
991 type_t *const points_to = pointer_type->points_to;
992 const unsigned elem_size = get_type_size(points_to);
994 assert(elem_size >= 1);
996 integer = create_conv(dbgi, integer, mode_Is);
997 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
998 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1002 ir_mode *const mode = get_ir_mode(type);
1003 return func(dbgi, pointer, integer, mode);
1006 static ir_node *create_arithmetic_assign_binop(
1007 const binary_expression_t *expression, create_arithmetic_func func)
1009 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1010 type_t *const type = expression->expression.datatype;
1013 if (type->type == TYPE_POINTER) {
1014 ir_node *const pointer = expression_to_firm(expression->left);
1015 ir_node * integer = expression_to_firm(expression->right);
1016 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1018 value = create_arithmetic_binop(expression, func);
1021 ir_mode *const mode = get_ir_mode(type);
1022 value = create_conv(dbgi, value, mode);
1023 set_value_for_expression(expression->left, value);
1028 static ir_node *create_add(const binary_expression_t *expression)
1030 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1031 ir_node *left = expression_to_firm(expression->left);
1032 ir_node *right = expression_to_firm(expression->right);
1033 type_t *type = expression->expression.datatype;
1035 expression_t *expr_left = expression->left;
1036 expression_t *expr_right = expression->right;
1037 type_t *type_left = skip_typeref(expr_left->datatype);
1038 type_t *type_right = skip_typeref(expr_right->datatype);
1040 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1041 ir_mode *const mode = get_ir_mode(type);
1042 return new_d_Add(dbgi, left, right, mode);
1045 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1046 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1048 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1049 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1053 static ir_node *create_sub(const binary_expression_t *expression)
1055 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1056 expression_t *const expr_left = expression->left;
1057 expression_t *const expr_right = expression->right;
1058 ir_node *const left = expression_to_firm(expr_left);
1059 ir_node *const right = expression_to_firm(expr_right);
1060 type_t *const type = expression->expression.datatype;
1061 type_t *const type_left = skip_typeref(expr_left->datatype);
1062 type_t *const type_right = skip_typeref(expr_right->datatype);
1064 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
1065 (type_left->type == TYPE_POINTER && type_right->type == TYPE_POINTER)) {
1066 ir_mode *const mode = get_ir_mode(type);
1067 return new_d_Sub(dbgi, left, right, mode);
1070 assert(type_left->type == TYPE_POINTER);
1071 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1074 static ir_node *create_shift(const binary_expression_t *expression)
1076 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1077 ir_node *left = expression_to_firm(expression->left);
1078 ir_node *right = expression_to_firm(expression->right);
1079 type_t *type = expression->expression.datatype;
1080 ir_mode *mode = get_ir_mode(type);
1082 /* firm always wants the shift count to be unsigned */
1083 right = create_conv(dbgi, right, mode_Iu);
1087 switch(expression->type) {
1088 case BINEXPR_SHIFTLEFT:
1089 res = new_d_Shl(dbgi, left, right, mode);
1091 case BINEXPR_SHIFTRIGHT: {
1092 expression_t *expr_left = expression->left;
1093 type_t *type_left = skip_typeref(expr_left->datatype);
1095 if(is_type_signed(type_left)) {
1096 res = new_d_Shrs(dbgi, left, right, mode);
1098 res = new_d_Shr(dbgi, left, right, mode);
1103 panic("create shift op called for non-shift op");
1110 static ir_node *create_divmod(const binary_expression_t *expression)
1112 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1113 ir_node *left = expression_to_firm(expression->left);
1114 ir_node *right = expression_to_firm(expression->right);
1115 ir_node *pin = new_Pin(new_NoMem());
1116 type_t *type = expression->expression.datatype;
1117 ir_mode *mode = get_ir_mode(type);
1121 switch (expression->type) {
1123 case BINEXPR_DIV_ASSIGN:
1124 if(mode_is_float(mode)) {
1125 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1126 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1128 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1129 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1134 case BINEXPR_MOD_ASSIGN:
1135 assert(!mode_is_float(mode));
1136 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1137 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1140 default: panic("unexpected binary expression type in create_divmod()");
1146 static ir_node *create_arithmetic_assign_divmod(
1147 const binary_expression_t *expression)
1149 ir_node * value = create_divmod(expression);
1150 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1151 type_t *const type = expression->expression.datatype;
1152 ir_mode *const mode = get_ir_mode(type);
1154 assert(type->type != TYPE_POINTER);
1156 value = create_conv(dbgi, value, mode);
1157 set_value_for_expression(expression->left, value);
1163 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1165 binary_expression_type_t type = expression->type;
1168 case BINEXPR_NOTEQUAL:
1170 case BINEXPR_LESSEQUAL:
1171 case BINEXPR_GREATER:
1172 case BINEXPR_GREATEREQUAL: {
1173 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1174 ir_node *left = expression_to_firm(expression->left);
1175 ir_node *right = expression_to_firm(expression->right);
1176 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1177 long pnc = get_pnc(type);
1178 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1181 case BINEXPR_ASSIGN: {
1182 ir_node *right = expression_to_firm(expression->right);
1183 set_value_for_expression(expression->left, right);
1187 return create_add(expression);
1189 return create_sub(expression);
1191 return create_arithmetic_binop(expression, new_d_Mul);
1192 case BINEXPR_BITWISE_AND:
1193 return create_arithmetic_binop(expression, new_d_And);
1194 case BINEXPR_BITWISE_OR:
1195 return create_arithmetic_binop(expression, new_d_Or);
1196 case BINEXPR_BITWISE_XOR:
1197 return create_arithmetic_binop(expression, new_d_Eor);
1198 case BINEXPR_SHIFTLEFT:
1199 case BINEXPR_SHIFTRIGHT:
1200 return create_shift(expression);
1203 return create_divmod(expression);
1204 case BINEXPR_LOGICAL_AND:
1205 case BINEXPR_LOGICAL_OR:
1206 return create_lazy_op(expression);
1208 expression_to_firm(expression->left);
1209 return expression_to_firm(expression->right);
1210 case BINEXPR_ADD_ASSIGN:
1211 return create_arithmetic_assign_binop(expression, new_d_Add);
1212 case BINEXPR_SUB_ASSIGN:
1213 return create_arithmetic_assign_binop(expression, new_d_Sub);
1214 case BINEXPR_MUL_ASSIGN:
1215 return create_arithmetic_assign_binop(expression, new_d_Mul);
1216 case BINEXPR_DIV_ASSIGN:
1217 return create_arithmetic_assign_divmod(expression);
1218 case BINEXPR_BITWISE_AND_ASSIGN:
1219 return create_arithmetic_assign_binop(expression, new_d_And);
1220 case BINEXPR_BITWISE_OR_ASSIGN:
1221 return create_arithmetic_assign_binop(expression, new_d_Or);
1222 case BINEXPR_BITWISE_XOR_ASSIGN:
1223 return create_arithmetic_assign_binop(expression, new_d_Eor);
1224 case BINEXPR_SHIFTLEFT_ASSIGN:
1225 return create_arithmetic_assign_binop(expression, new_d_Shl);
1226 case BINEXPR_SHIFTRIGHT_ASSIGN:
1227 return create_arithmetic_assign_binop(expression, new_d_Shr);
1229 panic("TODO binexpr type");
1233 static ir_node *array_access_addr(const array_access_expression_t *expression)
1235 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1237 ir_node *base_addr = expression_to_firm(expression->array_ref);
1238 ir_node *offset = expression_to_firm(expression->index);
1239 offset = create_conv(dbgi, offset, mode_Iu);
1241 unsigned elem_size = get_type_size(expression->expression.datatype);
1242 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1243 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1245 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1250 static ir_node *array_access_to_firm(
1251 const array_access_expression_t *expression)
1253 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1254 ir_node *addr = array_access_addr(expression);
1255 type_t *type = skip_typeref(expression->expression.datatype);
1256 return deref_address(type, addr, dbgi);
1259 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1261 type_t *type = expression->type;
1263 type = expression->size_expression->datatype;
1264 assert(type != NULL);
1267 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1268 unsigned size = get_type_size(type);
1269 ir_node *size_node = new_Const_long(mode, size);
1274 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1276 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1278 ir_node *cur_block = get_cur_block();
1280 /* create the true block */
1281 ir_node *true_block = new_immBlock();
1283 ir_node *true_val = expression_to_firm(expression->true_expression);
1284 ir_node *true_jmp = new_Jmp();
1286 /* create the false block */
1287 ir_node *false_block = new_immBlock();
1289 ir_node *false_val = expression_to_firm(expression->false_expression);
1290 ir_node *false_jmp = new_Jmp();
1292 /* create the condition evaluation */
1293 set_cur_block(cur_block);
1294 create_condition_evaluation(expression->condition, true_block, false_block);
1295 mature_immBlock(true_block);
1296 mature_immBlock(false_block);
1298 /* create the common block */
1299 ir_node *common_block = new_immBlock();
1300 add_immBlock_pred(common_block, true_jmp);
1301 add_immBlock_pred(common_block, false_jmp);
1302 mature_immBlock(common_block);
1304 ir_node *in[2] = { true_val, false_val };
1305 ir_mode *mode = get_irn_mode(true_val);
1306 assert(get_irn_mode(false_val) == mode);
1307 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1312 static ir_node *select_addr(const select_expression_t *expression)
1314 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1316 ir_node *compound_addr = expression_to_firm(expression->compound);
1318 declaration_t *entry = expression->compound_entry;
1319 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1320 ir_entity *entity = entry->v.entity;
1322 assert(entity != NULL);
1324 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1329 static ir_node *select_to_firm(const select_expression_t *expression)
1331 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1332 ir_node *addr = select_addr(expression);
1333 type_t *type = skip_typeref(expression->expression.datatype);
1334 return deref_address(type, addr, dbgi);
1337 /* Values returned by __builtin_classify_type. */
1338 typedef enum gcc_type_class
1344 enumeral_type_class,
1347 reference_type_class,
1351 function_type_class,
1362 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1364 const type_t *const type = expr->type_expression->datatype;
1370 const atomic_type_t *const atomic_type = (const atomic_type_t*)type;
1371 switch (atomic_type->atype) {
1372 // should not be reached
1373 case ATOMIC_TYPE_INVALID:
1377 // gcc cannot do that
1378 case ATOMIC_TYPE_VOID:
1379 tc = void_type_class;
1382 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1383 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1384 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1385 case ATOMIC_TYPE_SHORT:
1386 case ATOMIC_TYPE_USHORT:
1387 case ATOMIC_TYPE_INT:
1388 case ATOMIC_TYPE_UINT:
1389 case ATOMIC_TYPE_LONG:
1390 case ATOMIC_TYPE_ULONG:
1391 case ATOMIC_TYPE_LONGLONG:
1392 case ATOMIC_TYPE_ULONGLONG:
1393 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1394 tc = integer_type_class;
1397 case ATOMIC_TYPE_FLOAT:
1398 case ATOMIC_TYPE_DOUBLE:
1399 case ATOMIC_TYPE_LONG_DOUBLE:
1400 tc = real_type_class;
1403 #ifdef PROVIDE_COMPLEX
1404 case ATOMIC_TYPE_FLOAT_COMPLEX:
1405 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1406 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1407 tc = complex_type_class;
1411 #ifdef PROVIDE_IMAGINARY
1412 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1413 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1414 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1415 tc = complex_type_class;
1420 panic("Unimplemented case in classify_type_to_firm().");
1425 case TYPE_ARRAY: // gcc handles this as pointer
1426 case TYPE_FUNCTION: // gcc handles this as pointer
1427 case TYPE_POINTER: tc = pointer_type_class; break;
1428 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1429 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1431 // gcc handles this as integer
1432 case TYPE_ENUM: tc = integer_type_class; break;
1435 panic("Unimplemented case in classify_type_to_firm().");
1438 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1439 ir_mode *const mode = mode_Is;
1440 tarval *const tv = new_tarval_from_long(tc, mode);
1441 return new_d_Const(dbgi, mode, tv);
1444 static ir_node *dereference_addr(const unary_expression_t *const expression)
1446 assert(expression->type == UNEXPR_DEREFERENCE);
1447 return expression_to_firm(expression->value);
1450 static ir_node *expression_to_addr(const expression_t *expression)
1452 switch(expression->type) {
1453 case EXPR_REFERENCE:
1454 return reference_addr((const reference_expression_t*) expression);
1455 case EXPR_ARRAY_ACCESS:
1456 return array_access_addr((const array_access_expression_t*) expression);
1458 return select_addr((const select_expression_t*) expression);
1460 const unary_expression_t *const unary_expr =
1461 (const unary_expression_t*)expression;
1462 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1463 return dereference_addr(unary_expr);
1470 panic("trying to get address of non-lvalue");
1473 static ir_node *_expression_to_firm(const expression_t *expression)
1475 switch(expression->type) {
1477 return const_to_firm((const const_t*) expression);
1478 case EXPR_STRING_LITERAL:
1479 return string_literal_to_firm((const string_literal_t*) expression);
1480 case EXPR_REFERENCE:
1481 return reference_expression_to_firm(
1482 (const reference_expression_t*) expression);
1484 return call_expression_to_firm((const call_expression_t*) expression);
1486 return unary_expression_to_firm((const unary_expression_t*) expression);
1488 return binary_expression_to_firm(
1489 (const binary_expression_t*) expression);
1490 case EXPR_ARRAY_ACCESS:
1491 return array_access_to_firm(
1492 (const array_access_expression_t*) expression);
1494 return sizeof_to_firm((const sizeof_expression_t*) expression);
1495 case EXPR_CONDITIONAL:
1496 return conditional_to_firm((const conditional_expression_t*)expression);
1498 return select_to_firm((const select_expression_t*) expression);
1499 case EXPR_CLASSIFY_TYPE:
1500 return classify_type_to_firm((const classify_type_expression_t*)expression);
1503 case EXPR_PRETTY_FUNCTION:
1505 case EXPR_STATEMENT:
1506 case EXPR_BUILTIN_SYMBOL:
1507 panic("unimplemented expression found");
1513 panic("invalid expression found");
1516 static ir_node *expression_to_firm(const expression_t *expression)
1518 ir_node *res = _expression_to_firm(expression);
1520 if(res != NULL && get_irn_mode(res) == mode_b) {
1521 ir_mode *mode = get_ir_mode(expression->datatype);
1522 res = create_conv(NULL, res, mode);
1528 static ir_node *expression_to_modeb(const expression_t *expression)
1530 ir_node *res = _expression_to_firm(expression);
1531 res = create_conv(NULL, res, mode_b);
1537 * create a short-circuit expression evaluation that tries to construct
1538 * efficient control flow structures for &&, || and ! expressions
1540 static void create_condition_evaluation(const expression_t *expression,
1541 ir_node *true_block,
1542 ir_node *false_block)
1544 switch(expression->type) {
1546 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1547 if(unary_expression->type == UNEXPR_NOT) {
1548 create_condition_evaluation(unary_expression->value, false_block,
1555 binary_expression_t *binary_expression
1556 = (binary_expression_t*) expression;
1557 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1558 ir_node *cur_block = get_cur_block();
1559 ir_node *extra_block = new_immBlock();
1560 set_cur_block(cur_block);
1561 create_condition_evaluation(binary_expression->left, extra_block,
1563 mature_immBlock(extra_block);
1564 set_cur_block(extra_block);
1565 create_condition_evaluation(binary_expression->right, true_block,
1569 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1570 ir_node *cur_block = get_cur_block();
1571 ir_node *extra_block = new_immBlock();
1572 set_cur_block(cur_block);
1573 create_condition_evaluation(binary_expression->left, true_block,
1575 mature_immBlock(extra_block);
1576 set_cur_block(extra_block);
1577 create_condition_evaluation(binary_expression->right, true_block,
1587 dbg_info *dbgi = get_dbg_info(&expression->source_position);
1588 ir_node *condition = expression_to_modeb(expression);
1589 ir_node *cond = new_d_Cond(dbgi, condition);
1590 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1591 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1593 add_immBlock_pred(true_block, true_proj);
1594 add_immBlock_pred(false_block, false_proj);
1596 set_cur_block(NULL);
1599 static void statement_to_firm(statement_t *statement);
1601 static void return_statement_to_firm(return_statement_t *statement)
1603 if(get_cur_block() == NULL)
1606 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1609 if(statement->return_value != NULL) {
1610 ir_node *retval = expression_to_firm(statement->return_value);
1614 ret = new_d_Return(dbgi, get_store(), 1, in);
1616 ret = new_d_Return(dbgi, get_store(), 0, NULL);
1618 ir_node *end_block = get_irg_end_block(current_ir_graph);
1619 add_immBlock_pred(end_block, ret);
1621 set_cur_block(NULL);
1624 static void compound_statement_to_firm(compound_statement_t *compound)
1626 statement_t *statement = compound->statements;
1627 for( ; statement != NULL; statement = statement->next) {
1628 //context2firm(&statement->context);
1629 statement_to_firm(statement);
1633 static void expression_statement_to_firm(expression_statement_t *statement)
1635 if(get_cur_block() == NULL)
1638 expression_to_firm(statement->expression);
1641 static void if_statement_to_firm(if_statement_t *statement)
1643 ir_node *cur_block = get_cur_block();
1645 ir_node *fallthrough_block = new_immBlock();
1647 /* the true (blocks) */
1648 ir_node *true_block;
1649 if (statement->true_statement != NULL) {
1650 true_block = new_immBlock();
1651 statement_to_firm(statement->true_statement);
1652 if(get_cur_block() != NULL) {
1653 ir_node *jmp = new_Jmp();
1654 add_immBlock_pred(fallthrough_block, jmp);
1657 true_block = fallthrough_block;
1660 /* the false (blocks) */
1661 ir_node *false_block;
1662 if(statement->false_statement != NULL) {
1663 false_block = new_immBlock();
1665 statement_to_firm(statement->false_statement);
1666 if(get_cur_block() != NULL) {
1667 ir_node *jmp = new_Jmp();
1668 add_immBlock_pred(fallthrough_block, jmp);
1671 false_block = fallthrough_block;
1674 /* create the condition */
1675 if(cur_block != NULL) {
1676 set_cur_block(cur_block);
1677 create_condition_evaluation(statement->condition, true_block,
1681 mature_immBlock(true_block);
1682 if(false_block != fallthrough_block) {
1683 mature_immBlock(false_block);
1685 mature_immBlock(fallthrough_block);
1687 set_cur_block(fallthrough_block);
1690 static void while_statement_to_firm(while_statement_t *statement)
1692 ir_node *jmp = NULL;
1693 if(get_cur_block() != NULL) {
1697 /* create the header block */
1698 ir_node *header_block = new_immBlock();
1700 add_immBlock_pred(header_block, jmp);
1703 /* the false block */
1704 ir_node *false_block = new_immBlock();
1707 ir_node *body_block;
1708 if (statement->body != NULL) {
1709 ir_node *old_continue_label = continue_label;
1710 ir_node *old_break_label = break_label;
1711 continue_label = header_block;
1712 break_label = false_block;
1714 body_block = new_immBlock();
1715 statement_to_firm(statement->body);
1717 assert(continue_label == header_block);
1718 assert(break_label == false_block);
1719 continue_label = old_continue_label;
1720 break_label = old_break_label;
1722 if(get_cur_block() != NULL) {
1723 ir_node *jmp = new_Jmp();
1724 add_immBlock_pred(header_block, jmp);
1727 body_block = header_block;
1730 /* create the condition */
1731 set_cur_block(header_block);
1733 create_condition_evaluation(statement->condition, body_block, false_block);
1734 mature_immBlock(body_block);
1735 mature_immBlock(false_block);
1736 mature_immBlock(header_block);
1738 set_cur_block(false_block);
1741 static void do_while_statement_to_firm(do_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 /* the false block */
1752 ir_node *false_block = new_immBlock();
1755 ir_node *body_block = new_immBlock();
1757 add_immBlock_pred(body_block, jmp);
1760 if (statement->body != NULL) {
1761 ir_node *old_continue_label = continue_label;
1762 ir_node *old_break_label = break_label;
1763 continue_label = header_block;
1764 break_label = false_block;
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 mature_immBlock(header_block);
1775 mature_immBlock(body_block);
1776 mature_immBlock(false_block);
1781 ir_node *body_jmp = new_Jmp();
1782 add_immBlock_pred(header_block, body_jmp);
1783 mature_immBlock(header_block);
1785 /* create the condition */
1786 set_cur_block(header_block);
1788 create_condition_evaluation(statement->condition, body_block, false_block);
1789 mature_immBlock(body_block);
1790 mature_immBlock(false_block);
1791 mature_immBlock(header_block);
1793 set_cur_block(false_block);
1796 static void for_statement_to_firm(for_statement_t *statement)
1798 ir_node *jmp = NULL;
1799 if (get_cur_block() != NULL) {
1800 if(statement->initialisation != NULL) {
1801 expression_to_firm(statement->initialisation);
1806 /* create the step block */
1807 ir_node *const step_block = new_immBlock();
1808 if (statement->step != NULL) {
1809 expression_to_firm(statement->step);
1811 ir_node *const step_jmp = new_Jmp();
1813 /* create the header block */
1814 ir_node *const header_block = new_immBlock();
1816 add_immBlock_pred(header_block, jmp);
1818 add_immBlock_pred(header_block, step_jmp);
1820 /* the false block */
1821 ir_node *const false_block = new_immBlock();
1824 ir_node * body_block;
1825 if (statement->body != NULL) {
1826 ir_node *const old_continue_label = continue_label;
1827 ir_node *const old_break_label = break_label;
1828 continue_label = step_block;
1829 break_label = false_block;
1831 body_block = new_immBlock();
1832 statement_to_firm(statement->body);
1834 assert(continue_label == step_block);
1835 assert(break_label == false_block);
1836 continue_label = old_continue_label;
1837 break_label = old_break_label;
1839 if (get_cur_block() != NULL) {
1840 ir_node *const jmp = new_Jmp();
1841 add_immBlock_pred(step_block, jmp);
1844 body_block = step_block;
1847 /* create the condition */
1848 set_cur_block(header_block);
1849 if (statement->condition != NULL) {
1850 create_condition_evaluation(statement->condition, body_block,
1853 keep_alive(header_block);
1854 ir_node *jmp = new_Jmp();
1855 add_immBlock_pred(body_block, jmp);
1858 mature_immBlock(body_block);
1859 mature_immBlock(false_block);
1860 mature_immBlock(step_block);
1861 mature_immBlock(header_block);
1862 mature_immBlock(false_block);
1864 set_cur_block(false_block);
1867 static void create_declaration_entity(declaration_t *declaration,
1868 declaration_type_t declaration_type,
1869 ir_type *parent_type)
1871 ident *id = new_id_from_str(declaration->symbol->string);
1872 ir_type *irtype = get_ir_type(declaration->type);
1873 ir_entity *entity = new_entity(parent_type, id, irtype);
1874 set_entity_ld_ident(entity, id);
1876 declaration->declaration_type = declaration_type;
1877 declaration->v.entity = entity;
1878 set_entity_variability(entity, variability_uninitialized);
1879 /* TODO: visibility? */
1882 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
1884 enum compound_graph_entry_type_t {
1885 COMPOUND_GRAPH_ENTRY_ARRAY,
1886 COMPOUND_GRAPH_ENTRY_COMPOUND
1889 struct compound_graph_path_entry_t {
1895 compound_graph_path_entry_t *prev;
1898 static void create_initializer_list(initializer_list_t *initializer,
1899 type_t *type, ir_entity *entity,
1900 compound_graph_path_entry_t *entry,
1903 static void create_initializer_value(initializer_value_t *initializer,
1905 compound_graph_path_entry_t *entry,
1908 ir_node *node = expression_to_firm(initializer->value);
1910 ir_type *type = get_entity_type(entity);
1911 compound_graph_path *path = new_compound_graph_path(type, len);
1914 for( ; entry != NULL; entry = entry->prev, --i) {
1916 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
1917 set_compound_graph_path_node(path, i, entry->v.entity);
1919 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
1920 set_compound_graph_path_array_index(path, i, entry->v.array_index);
1925 add_compound_ent_value_w_path(entity, node, path);
1928 static void create_initializer_compound(initializer_list_t *initializer,
1929 compound_type_t *type,
1931 compound_graph_path_entry_t *last_entry,
1934 declaration_t *compound_declaration = type->declaration;
1936 declaration_t *compound_entry = compound_declaration->context.declarations;
1938 compound_graph_path_entry_t entry;
1939 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
1940 entry.prev = last_entry;
1944 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
1945 if(compound_entry->symbol == NULL)
1947 if(compound_entry->namespc != NAMESPACE_NORMAL)
1950 if(i >= initializer->len)
1953 entry.v.entity = compound_entry->v.entity;
1955 initializer_t *sub_initializer = initializer->initializers[i];
1957 assert(compound_entry != NULL);
1958 assert(compound_entry->declaration_type
1959 == DECLARATION_TYPE_COMPOUND_MEMBER);
1961 if(sub_initializer->type == INITIALIZER_VALUE) {
1962 create_initializer_value((initializer_value_t*) sub_initializer,
1963 entity, &entry, len);
1965 assert(sub_initializer->type == INITIALIZER_LIST);
1966 create_initializer_list((initializer_list_t*) sub_initializer,
1967 compound_entry->type, entity, &entry, len);
1974 static void create_initializer_list(initializer_list_t *initializer,
1975 type_t *type, ir_entity *entity,
1976 compound_graph_path_entry_t *entry, int len)
1978 if(type->type == TYPE_ARRAY) {
1981 assert(type->type == TYPE_COMPOUND_STRUCT
1982 || type->type == TYPE_COMPOUND_UNION);
1983 create_initializer_compound(initializer, (compound_type_t*) type,
1984 entity, entry, len);
1988 static void create_initializer(declaration_t *declaration)
1990 initializer_t *initializer = declaration->init.initializer;
1991 if(initializer == NULL)
1994 if(initializer->type == INITIALIZER_VALUE) {
1995 initializer_value_t *initializer_value
1996 = (initializer_value_t*) initializer;
1997 ir_node *value = expression_to_firm(initializer_value->value);
1999 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2000 set_value(declaration->v.value_number, value);
2002 ir_entity *entity = declaration->v.entity;
2004 set_entity_variability(entity, variability_initialized);
2005 set_atomic_ent_value(entity, value);
2008 assert(initializer->type == INITIALIZER_LIST);
2009 initializer_list_t *list = (initializer_list_t*) initializer;
2011 declaration_type_t declaration_type = declaration->declaration_type;
2012 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2013 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2015 ir_entity *entity = declaration->v.entity;
2016 set_entity_variability(entity, variability_initialized);
2018 create_initializer_list(list, declaration->type, entity, NULL, 0);
2022 static void create_local_variable(declaration_t *declaration)
2024 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2026 bool needs_entity = declaration->address_taken;
2027 type_t *type = skip_typeref(declaration->type);
2029 if(type->type == TYPE_ARRAY
2030 || type->type == TYPE_COMPOUND_STRUCT
2031 || type->type == TYPE_COMPOUND_UNION) {
2032 needs_entity = true;
2036 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2037 create_declaration_entity(declaration,
2038 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2041 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2042 declaration->v.value_number = next_value_number_function;
2043 ++next_value_number_function;
2046 create_initializer(declaration);
2049 static void declaration_statement_to_firm(declaration_statement_t *statement)
2051 declaration_t *declaration = statement->declarations_begin;
2052 declaration_t *end = statement->declarations_end->next;
2053 for( ; declaration != end; declaration = declaration->next) {
2054 type_t *type = declaration->type;
2056 switch(declaration->storage_class) {
2057 case STORAGE_CLASS_TYPEDEF:
2059 case STORAGE_CLASS_STATIC:
2060 panic("static local vars not implemented yet");
2061 case STORAGE_CLASS_ENUM_ENTRY:
2062 panic("enum entry declaration in local block found");
2063 case STORAGE_CLASS_EXTERN:
2064 panic("extern declaration in local block found");
2065 case STORAGE_CLASS_NONE:
2066 case STORAGE_CLASS_AUTO:
2067 case STORAGE_CLASS_REGISTER:
2068 if(type->type == TYPE_FUNCTION) {
2069 panic("nested functions not supported yet");
2071 create_local_variable(declaration);
2075 panic("invalid storage class found");
2079 static void create_jump_statement(const statement_t *statement,
2080 ir_node *target_block)
2082 if(get_cur_block() == NULL)
2085 dbg_info *dbgi = get_dbg_info(&statement->source_position);
2086 ir_node *jump = new_d_Jmp(dbgi);
2087 add_immBlock_pred(target_block, jump);
2089 set_cur_block(NULL);
2092 static void switch_statement_to_firm(const switch_statement_t *statement)
2094 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2096 ir_node *expression = expression_to_firm(statement->expression);
2097 ir_node *cond = new_d_Cond(dbgi, expression);
2098 ir_node *break_block = new_immBlock();
2100 set_cur_block(NULL);
2102 ir_node *const old_switch_cond = current_switch_cond;
2103 ir_node *const old_break_label = break_label;
2104 const bool old_saw_default_label = saw_default_label;
2105 current_switch_cond = cond;
2106 break_label = break_block;
2108 statement_to_firm(statement->body);
2110 if(get_cur_block() != NULL) {
2111 ir_node *jmp = new_Jmp();
2112 add_immBlock_pred(break_block, jmp);
2115 if (!saw_default_label) {
2116 set_cur_block(get_nodes_block(cond));
2117 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2118 MAGIC_DEFAULT_PN_NUMBER);
2119 add_immBlock_pred(break_block, proj);
2122 assert(current_switch_cond == cond);
2123 assert(break_label == break_block);
2124 current_switch_cond = old_switch_cond;
2125 break_label = old_break_label;
2126 saw_default_label = old_saw_default_label;
2128 mature_immBlock(break_block);
2129 set_cur_block(break_block);
2132 static long fold_constant(const expression_t *expression)
2134 ir_graph *old_current_ir_graph = current_ir_graph;
2135 current_ir_graph = get_const_code_irg();
2137 ir_node *cnst = expression_to_firm(expression);
2138 if(!is_Const(cnst)) {
2139 panic("couldn't fold constantl");
2141 tarval *tv = get_Const_tarval(cnst);
2142 if(!tarval_is_long(tv)) {
2143 panic("folded constant not an integer");
2146 long res = get_tarval_long(tv);
2148 current_ir_graph = old_current_ir_graph;
2152 static void case_label_to_firm(const case_label_statement_t *statement)
2154 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2156 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2158 /* let's create a node and hope firm constant folding creates a Const
2161 set_cur_block(get_nodes_block(current_switch_cond));
2162 if(statement->expression) {
2163 long pn = fold_constant(statement->expression);
2164 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2165 /* oops someone detected our cheating... */
2166 panic("magic default pn used");
2168 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2170 saw_default_label = true;
2171 proj = new_d_defaultProj(dbgi, current_switch_cond,
2172 MAGIC_DEFAULT_PN_NUMBER);
2175 ir_node *block = new_immBlock();
2176 if (fallthrough != NULL) {
2177 add_immBlock_pred(block, fallthrough);
2179 add_immBlock_pred(block, proj);
2180 mature_immBlock(block);
2183 static ir_node *get_label_block(declaration_t *label)
2185 assert(label->namespc == NAMESPACE_LABEL);
2187 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2188 return label->v.block;
2190 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2192 ir_node *old_cur_block = get_cur_block();
2193 ir_node *block = new_immBlock();
2194 set_cur_block(old_cur_block);
2196 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2197 label->v.block = block;
2199 ARR_APP1(ir_node *, imature_blocks, block);
2204 static void label_to_firm(const label_statement_t *statement)
2206 ir_node *block = get_label_block(statement->label);
2208 if(get_cur_block() != NULL) {
2209 ir_node *jmp = new_Jmp();
2210 add_immBlock_pred(block, jmp);
2213 set_cur_block(block);
2216 statement_to_firm(statement->label_statement);
2219 static void goto_to_firm(const goto_statement_t *statement)
2221 if(get_cur_block() == NULL)
2224 ir_node *block = get_label_block(statement->label);
2225 ir_node *jmp = new_Jmp();
2226 add_immBlock_pred(block, jmp);
2228 set_cur_block(NULL);
2231 static void statement_to_firm(statement_t *statement)
2233 switch(statement->type) {
2234 case STATEMENT_COMPOUND:
2235 compound_statement_to_firm((compound_statement_t*) statement);
2237 case STATEMENT_RETURN:
2238 return_statement_to_firm((return_statement_t*) statement);
2240 case STATEMENT_EXPRESSION:
2241 expression_statement_to_firm((expression_statement_t*) statement);
2244 if_statement_to_firm((if_statement_t*) statement);
2246 case STATEMENT_WHILE:
2247 while_statement_to_firm((while_statement_t*) statement);
2249 case STATEMENT_DO_WHILE:
2250 do_while_statement_to_firm((do_while_statement_t*) statement);
2252 case STATEMENT_DECLARATION:
2253 declaration_statement_to_firm((declaration_statement_t*) statement);
2255 case STATEMENT_BREAK:
2256 create_jump_statement(statement, break_label);
2258 case STATEMENT_CONTINUE:
2259 create_jump_statement(statement, continue_label);
2261 case STATEMENT_SWITCH:
2262 switch_statement_to_firm((switch_statement_t*) statement);
2264 case STATEMENT_CASE_LABEL:
2265 case_label_to_firm((case_label_statement_t*) statement);
2268 for_statement_to_firm((for_statement_t*) statement);
2270 case STATEMENT_LABEL:
2271 label_to_firm((label_statement_t*) statement);
2273 case STATEMENT_GOTO:
2274 goto_to_firm((goto_statement_t*) statement);
2279 panic("Statement not implemented\n");
2282 static int count_local_declarations(const declaration_t * decl,
2283 const declaration_t *const end)
2286 for (; decl != end; decl = decl->next) {
2287 const type_t *type = skip_typeref(decl->type);
2288 switch (type->type) {
2292 if (!decl->address_taken) ++count;
2301 static int count_decls_in_stmts(const statement_t *stmt)
2304 for (; stmt != NULL; stmt = stmt->next) {
2305 switch (stmt->type) {
2306 case STATEMENT_DECLARATION: {
2307 const declaration_statement_t *const decl_stmt =
2308 (const declaration_statement_t*)stmt;
2309 count += count_local_declarations(decl_stmt->declarations_begin,
2310 decl_stmt->declarations_end->next);
2314 case STATEMENT_COMPOUND: {
2315 const compound_statement_t *const comp =
2316 (const compound_statement_t*)stmt;
2317 count += count_decls_in_stmts(comp->statements);
2321 case STATEMENT_IF: {
2322 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2323 count += count_decls_in_stmts(if_stmt->true_statement);
2324 count += count_decls_in_stmts(if_stmt->false_statement);
2328 case STATEMENT_SWITCH: {
2329 const switch_statement_t *const switch_stmt =
2330 (const switch_statement_t*)stmt;
2331 count += count_decls_in_stmts(switch_stmt->body);
2335 case STATEMENT_LABEL: {
2336 const label_statement_t *const label_stmt =
2337 (const label_statement_t*)stmt;
2338 count += count_decls_in_stmts(label_stmt->label_statement);
2342 case STATEMENT_WHILE: {
2343 const while_statement_t *const while_stmt =
2344 (const while_statement_t*)stmt;
2345 count += count_decls_in_stmts(while_stmt->body);
2349 case STATEMENT_DO_WHILE: {
2350 const do_while_statement_t *const do_while_stmt =
2351 (const do_while_statement_t*)stmt;
2352 count += count_decls_in_stmts(do_while_stmt->body);
2356 case STATEMENT_FOR: {
2357 const for_statement_t *const for_stmt =
2358 (const for_statement_t*)stmt;
2359 /* TODO initialisation */
2360 count += count_decls_in_stmts(for_stmt->body);
2364 case STATEMENT_BREAK:
2365 case STATEMENT_CASE_LABEL:
2366 case STATEMENT_CONTINUE:
2367 case STATEMENT_EXPRESSION:
2368 case STATEMENT_GOTO:
2369 case STATEMENT_INVALID:
2370 case STATEMENT_RETURN:
2377 static int get_function_n_local_vars(declaration_t *declaration)
2381 /* count parameters */
2382 count += count_local_declarations(declaration->context.declarations, NULL);
2384 /* count local variables declared in body */
2385 count += count_decls_in_stmts(declaration->init.statement);
2390 static void initialize_function_parameters(declaration_t *declaration)
2392 ir_graph *irg = current_ir_graph;
2393 ir_node *args = get_irg_args(irg);
2394 ir_node *start_block = get_irg_start_block(irg);
2397 declaration_t *parameter = declaration->context.declarations;
2398 for( ; parameter != NULL; parameter = parameter->next) {
2399 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2400 type_t *type = parameter->type;
2402 bool needs_entity = parameter->address_taken;
2403 if(type->type == TYPE_COMPOUND_STRUCT
2404 || type->type == TYPE_COMPOUND_UNION) {
2405 needs_entity = true;
2409 panic("entities for function parameters not implemented yet");
2412 ir_mode *mode = get_ir_mode(parameter->type);
2414 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2417 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2418 parameter->v.value_number = next_value_number_function;
2419 ++next_value_number_function;
2421 set_value(parameter->v.value_number, proj);
2425 static void create_function(declaration_t *declaration)
2427 ir_entity *entity = get_function_entity(declaration);
2429 if(declaration->init.statement == NULL)
2432 assert(imature_blocks == NULL);
2433 imature_blocks = NEW_ARR_F(ir_node*, 0);
2435 int n_local_vars = get_function_n_local_vars(declaration);
2436 ir_graph *irg = new_ir_graph(entity, n_local_vars);
2437 ir_node *first_block = get_cur_block();
2439 next_value_number_function = 0;
2440 initialize_function_parameters(declaration);
2442 statement_to_firm(declaration->init.statement);
2444 ir_node *end_block = get_irg_end_block(irg);
2446 /* do we have a return statement yet? */
2447 if(get_cur_block() != NULL) {
2448 assert(declaration->type->type == TYPE_FUNCTION);
2449 const function_type_t* const func_type
2450 = (const function_type_t*) declaration->type;
2452 if (func_type->result_type == type_void) {
2453 ret = new_Return(get_store(), 0, NULL);
2455 ir_mode *const mode = get_ir_mode(func_type->result_type);
2457 // ยง5.1.2.2.3 main implicitly returns 0
2458 if (strcmp(declaration->symbol->string, "main") == 0) {
2459 in[0] = new_Const(mode, get_mode_null(mode));
2461 in[0] = new_Unknown(mode);
2463 ret = new_Return(get_store(), 1, in);
2465 add_immBlock_pred(end_block, ret);
2468 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2469 mature_immBlock(imature_blocks[i]);
2471 DEL_ARR_F(imature_blocks);
2472 imature_blocks = NULL;
2474 mature_immBlock(first_block);
2475 mature_immBlock(end_block);
2477 irg_finalize_cons(irg);
2479 /* finalize the frame type */
2480 ir_type *frame_type = get_irg_frame_type(irg);
2481 int n = get_compound_n_members(frame_type);
2484 for(int i = 0; i < n; ++i) {
2485 ir_entity *entity = get_compound_member(frame_type, i);
2486 ir_type *entity_type = get_entity_type(entity);
2488 int align = get_type_alignment_bytes(entity_type);
2489 if(align > align_all)
2493 misalign = offset % align;
2495 offset += align - misalign;
2499 set_entity_offset(entity, offset);
2500 offset += get_type_size_bytes(entity_type);
2502 set_type_size_bytes(frame_type, offset);
2503 set_type_alignment_bytes(frame_type, align_all);
2504 set_type_state(frame_type, layout_fixed);
2509 static void create_global_variable(declaration_t *declaration)
2511 ir_type *global_type = get_glob_type();
2512 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2515 ir_entity *entity = declaration->v.entity;
2516 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2517 set_entity_visibility(entity, visibility_local);
2518 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2519 set_entity_visibility(entity, visibility_external_allocated);
2521 set_entity_visibility(entity, visibility_external_visible);
2523 current_ir_graph = get_const_code_irg();
2524 create_initializer(declaration);
2527 static void context_to_firm(context_t *context)
2529 declaration_t *declaration = context->declarations;
2530 for( ; declaration != NULL; declaration = declaration->next) {
2531 if(declaration->namespc != NAMESPACE_NORMAL)
2533 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2534 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2536 if(declaration->symbol == NULL)
2539 type_t *type = declaration->type;
2540 if(type->type == TYPE_FUNCTION) {
2541 create_function(declaration);
2543 create_global_variable(declaration);
2548 void translation_unit_to_firm(translation_unit_t *unit)
2550 /* just to be sure */
2551 continue_label = NULL;
2553 current_switch_cond = NULL;
2555 context_to_firm(& unit->context);