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 ir_node **imature_blocks;
37 typedef enum declaration_type_t {
38 DECLARATION_TYPE_UNKNOWN,
39 DECLARATION_TYPE_FUNCTION,
40 DECLARATION_TYPE_GLOBAL_VARIABLE,
41 DECLARATION_TYPE_LOCAL_VARIABLE,
42 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
43 DECLARATION_TYPE_COMPOUND_MEMBER,
44 DECLARATION_TYPE_LABEL_BLOCK,
47 static ir_type *get_ir_type(type_t *type);
49 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
53 const declaration_t *declaration = & value_numbers[pos]->declaration;
55 print_warning_prefix(declaration->source_position);
56 fprintf(stderr, "variable '%s' might be used uninitialized\n",
57 declaration->symbol->string);
59 fprintf(stderr, "Some variable might be used uninitialized\n");
60 return new_r_Unknown(irg, mode);
63 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
65 const source_position_t *pos = (const source_position_t*) dbg;
68 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
72 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
74 const source_position_t *pos = (const source_position_t*) dbg;
79 return pos->input_name;
82 void init_ast2firm(void)
84 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
85 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
86 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
88 ir_type_int = get_ir_type(type_int);
89 ir_type_const_char = get_ir_type(type_const_char);
90 ir_type_void = get_ir_type(type_int); /* we don't have a real void
92 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
93 ir_type_void, mode_P_data);
95 type_void->firm_type = ir_type_void;
98 void exit_ast2firm(void)
102 static unsigned unique_id = 0;
104 static ident *unique_ident(const char *tag)
108 snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
110 return new_id_from_str(buf);
113 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
115 switch(atomic_type->atype) {
116 case ATOMIC_TYPE_SCHAR:
117 case ATOMIC_TYPE_CHAR:
119 case ATOMIC_TYPE_UCHAR:
121 case ATOMIC_TYPE_SHORT:
123 case ATOMIC_TYPE_USHORT:
125 case ATOMIC_TYPE_LONG:
126 case ATOMIC_TYPE_INT:
128 case ATOMIC_TYPE_ULONG:
129 case ATOMIC_TYPE_UINT:
131 case ATOMIC_TYPE_LONGLONG:
133 case ATOMIC_TYPE_ULONGLONG:
135 case ATOMIC_TYPE_FLOAT:
137 case ATOMIC_TYPE_DOUBLE:
139 case ATOMIC_TYPE_LONG_DOUBLE:
141 case ATOMIC_TYPE_BOOL:
143 #ifdef PROVIDE_COMPLEX
144 case ATOMIC_TYPE_FLOAT_COMPLEX:
145 case ATOMIC_TYPE_DOUBLE_COMPLEX:
146 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
147 panic("complex lowering not implemented yet");
149 case ATOMIC_TYPE_FLOAT_IMAGINARY:
150 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
151 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
152 panic("imaginary lowering not implemented yet");
155 case ATOMIC_TYPE_VOID:
156 /* firm has no real void... */
158 case ATOMIC_TYPE_INVALID:
161 panic("Encountered unknown atomic type");
165 static unsigned get_type_size(type_t *type);
167 static unsigned get_atomic_type_size(const atomic_type_t *type)
169 switch(type->atype) {
170 case ATOMIC_TYPE_CHAR:
171 case ATOMIC_TYPE_SCHAR:
172 case ATOMIC_TYPE_UCHAR:
175 case ATOMIC_TYPE_SHORT:
176 case ATOMIC_TYPE_USHORT:
179 case ATOMIC_TYPE_BOOL:
180 case ATOMIC_TYPE_INT:
181 case ATOMIC_TYPE_UINT:
182 case ATOMIC_TYPE_LONG:
183 case ATOMIC_TYPE_ULONG:
184 case ATOMIC_TYPE_FLOAT:
187 case ATOMIC_TYPE_LONGLONG:
188 case ATOMIC_TYPE_ULONGLONG:
189 case ATOMIC_TYPE_DOUBLE:
192 case ATOMIC_TYPE_LONG_DOUBLE:
195 case ATOMIC_TYPE_VOID:
198 case ATOMIC_TYPE_INVALID:
201 panic("Trying to determine size of invalid atomic type");
204 static unsigned get_compound_type_size(compound_type_t *type)
206 ir_type *irtype = get_ir_type(&type->type);
207 return get_type_size_bytes(irtype);
210 static unsigned get_array_type_size(array_type_t *type)
212 ir_type *irtype = get_ir_type(&type->type);
213 return get_type_size_bytes(irtype);
216 static unsigned get_type_size(type_t *type)
218 type = skip_typeref(type);
222 return get_atomic_type_size((const atomic_type_t*) type);
224 return get_mode_size_bytes(mode_Is);
225 case TYPE_COMPOUND_UNION:
226 case TYPE_COMPOUND_STRUCT:
227 return get_compound_type_size((compound_type_t*) type);
229 /* just a pointer to the function */
230 return get_mode_size_bytes(mode_P_code);
232 return get_mode_size_bytes(mode_P_data);
234 return get_array_type_size((array_type_t*) type);
241 panic("Trying to determine size of invalid type");
244 static unsigned count_parameters(const function_type_t *function_type)
248 function_parameter_t *parameter = function_type->parameters;
249 for ( ; parameter != NULL; parameter = parameter->next) {
259 static long fold_constant(const expression_t *expression);
261 static ir_type *create_atomic_type(const atomic_type_t *type)
263 ir_mode *mode = get_atomic_mode(type);
264 ident *id = get_mode_ident(mode);
265 ir_type *irtype = new_type_primitive(id, mode);
270 static ir_type *create_method_type(const function_type_t *function_type)
272 type_t *result_type = function_type->result_type;
274 ident *id = unique_ident("functiontype");
275 int n_parameters = count_parameters(function_type);
276 int n_results = result_type == type_void ? 0 : 1;
277 ir_type *irtype = new_type_method(id, n_parameters, n_results);
279 if(result_type != type_void) {
280 ir_type *restype = get_ir_type(result_type);
281 set_method_res_type(irtype, 0, restype);
284 function_parameter_t *parameter = function_type->parameters;
286 for( ; parameter != NULL; parameter = parameter->next) {
287 ir_type *p_irtype = get_ir_type(parameter->type);
288 set_method_param_type(irtype, n, p_irtype);
292 if(function_type->variadic || function_type->unspecified_parameters) {
293 set_method_variadicity(irtype, variadicity_variadic);
299 static ir_type *create_pointer_type(pointer_type_t *type)
301 type_t *points_to = type->points_to;
302 ir_type *ir_points_to;
303 /* Avoid endless recursion if the points_to type contains this poiner type
304 * again (might be a struct). We therefore first create a void* pointer
305 * and then set the real points_to type
307 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
308 ir_type_void, mode_P_data);
309 type->type.firm_type = ir_type;
311 ir_points_to = get_ir_type(points_to);
312 set_pointer_points_to_type(ir_type, ir_points_to);
317 static ir_type *create_array_type(array_type_t *type)
319 type_t *element_type = type->element_type;
320 ir_type *ir_element_type = get_ir_type(element_type);
322 ident *id = unique_ident("array");
323 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
325 if(type->size != NULL) {
326 int n_elements = fold_constant(type->size);
328 set_array_bounds_int(ir_type, 0, 0, n_elements);
330 size_t elemsize = get_type_size_bytes(ir_element_type);
331 int align = get_type_alignment_bytes(ir_element_type);
332 if(elemsize % align > 0) {
333 elemsize += align - (elemsize % align);
335 set_type_size_bytes(ir_type, n_elements * elemsize);
336 set_type_alignment_bytes(ir_type, align);
337 set_type_state(ir_type, layout_fixed);
343 #define INVALID_TYPE ((ir_type_ptr)-1)
345 static ir_type *create_struct_type(compound_type_t *type)
347 symbol_t *symbol = type->declaration->symbol;
350 id = unique_ident(symbol->string);
352 id = unique_ident("__anonymous_struct");
354 ir_type *ir_type = new_type_struct(id);
356 type->type.firm_type = ir_type;
360 declaration_t *entry = type->declaration->context.declarations;
361 for( ; entry != NULL; entry = entry->next) {
362 if(entry->namespace != NAMESPACE_NORMAL)
365 ident *ident = new_id_from_str(entry->symbol->string);
366 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
368 int entry_size = get_type_size_bytes(entry_ir_type);
369 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
370 int misalign = offset % entry_alignment;
373 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
374 set_entity_offset(entity, offset);
375 add_struct_member(ir_type, entity);
376 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
377 entry->v.entity = entity;
379 offset += entry_size;
380 if(entry_alignment > align_all) {
381 if(entry_alignment % align_all != 0) {
382 panic("Uneven alignments not supported yet");
384 align_all = entry_alignment;
388 int misalign = offset % align_all;
390 set_type_alignment_bytes(ir_type, align_all);
391 set_type_size_bytes(ir_type, offset);
392 set_type_state(ir_type, layout_fixed);
397 static ir_type *create_union_type(compound_type_t *type)
399 declaration_t *declaration = type->declaration;
400 symbol_t *symbol = declaration->symbol;
403 id = unique_ident(symbol->string);
405 id = unique_ident("__anonymous_union");
407 ir_type *ir_type = new_type_union(id);
409 type->type.firm_type = ir_type;
413 declaration_t *entry = declaration->context.declarations;
414 for( ; entry != NULL; entry = entry->next) {
415 if(entry->namespace != NAMESPACE_NORMAL)
418 ident *ident = new_id_from_str(entry->symbol->string);
419 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
421 int entry_size = get_type_size_bytes(entry_ir_type);
422 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
424 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
425 add_union_member(ir_type, entity);
426 set_entity_offset(entity, 0);
427 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
428 entry->v.entity = entity;
430 if(entry_size > size) {
433 if(entry_alignment > align_all) {
434 if(entry_alignment % align_all != 0) {
435 panic("Uneven alignments not supported yet");
437 align_all = entry_alignment;
441 set_type_alignment_bytes(ir_type, align_all);
442 set_type_size_bytes(ir_type, size);
443 set_type_state(ir_type, layout_fixed);
448 static ir_type *get_ir_type(type_t *type)
450 assert(type != NULL);
452 type = skip_typeref(type);
454 if(type->firm_type != NULL) {
455 assert(type->firm_type != INVALID_TYPE);
456 return type->firm_type;
459 ir_type *firm_type = NULL;
462 firm_type = create_atomic_type((atomic_type_t*) type);
465 firm_type = create_method_type((function_type_t*) type);
468 firm_type = create_pointer_type((pointer_type_t*) type);
471 firm_type = create_array_type((array_type_t*) type);
473 case TYPE_COMPOUND_STRUCT:
474 firm_type = create_struct_type((compound_type_t*) type);
476 case TYPE_COMPOUND_UNION:
477 firm_type = create_union_type((compound_type_t*) type);
480 firm_type = ir_type_int;
488 if(firm_type == NULL)
489 panic("unknown type found");
491 type->firm_type = firm_type;
495 static inline ir_mode *get_ir_mode(type_t *type)
497 ir_type *irtype = get_ir_type(type);
499 /* firm doesn't report a mode for arrays somehow... */
500 if(is_Array_type(irtype)) {
504 ir_mode *mode = get_type_mode(irtype);
505 assert(mode != NULL);
509 static ir_entity* get_function_entity(declaration_t *declaration)
511 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
512 return declaration->v.entity;
513 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
515 symbol_t *symbol = declaration->symbol;
516 ident *id = new_id_from_str(symbol->string);
518 ir_type *global_type = get_glob_type();
519 ir_type *ir_type_method = get_ir_type(declaration->type);
520 assert(is_Method_type(ir_type_method));
522 ir_entity *entity = new_entity(global_type, id, ir_type_method);
523 set_entity_ld_ident(entity, id);
524 if(declaration->storage_class == STORAGE_CLASS_STATIC
525 || declaration->is_inline) {
526 set_entity_visibility(entity, visibility_local);
527 } else if(declaration->init.statement != NULL) {
528 set_entity_visibility(entity, visibility_external_visible);
530 set_entity_visibility(entity, visibility_external_allocated);
533 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
534 declaration->v.entity = entity;
541 static ir_node *expression_to_firm(const expression_t *expression);
542 static ir_node *expression_to_modeb(const expression_t *expression);
544 static dbg_info *get_dbg_info(const source_position_t *pos)
546 return (dbg_info*) pos;
549 static ir_node *const_to_firm(const const_t *cnst)
551 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
552 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
555 if(mode_is_float(mode)) {
556 tv = new_tarval_from_double(cnst->v.float_value, mode);
558 tv = new_tarval_from_long(cnst->v.int_value, mode);
561 return new_d_Const(dbgi, mode, tv);
564 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
566 assert(entity != NULL);
567 union symconst_symbol sym;
568 sym.entity_p = entity;
569 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
572 static ir_node *string_literal_to_firm(const string_literal_t* literal)
574 ir_type *global_type = get_glob_type();
575 ir_type *type = new_type_array(unique_ident("strtype"), 1,
578 ident *id = unique_ident("Lstr");
579 ir_entity *entity = new_entity(global_type, id, type);
580 set_entity_ld_ident(entity, id);
581 set_entity_variability(entity, variability_constant);
583 ir_type *elem_type = ir_type_const_char;
584 ir_mode *mode = get_type_mode(elem_type);
586 const char *string = literal->value;
587 size_t slen = strlen(string) + 1;
589 set_array_lower_bound_int(type, 0, 0);
590 set_array_upper_bound_int(type, 0, slen);
591 set_type_size_bytes(type, slen);
592 set_type_state(type, layout_fixed);
594 tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
595 for(size_t i = 0; i < slen; ++i) {
596 tvs[i] = new_tarval_from_long(string[i], mode);
599 set_array_entity_values(entity, tvs, slen);
602 dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
604 return create_symconst(dbgi, entity);
607 static ir_node *load_from_expression_addr(type_t *type, ir_node *addr,
610 ir_mode *mode = get_ir_mode(type);
611 ir_node *memory = get_store();
612 ir_node *load = new_d_Load(dbgi, memory, addr, mode);
613 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
614 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
620 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
622 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
623 declaration_t *declaration = ref->declaration;
624 type_t *type = skip_typeref(declaration->type);
625 ir_mode *mode = get_ir_mode(type);
627 switch((declaration_type_t) declaration->declaration_type) {
628 case DECLARATION_TYPE_UNKNOWN:
630 case DECLARATION_TYPE_LOCAL_VARIABLE:
631 return get_value(declaration->v.value_number, mode);
632 case DECLARATION_TYPE_FUNCTION: {
633 return create_symconst(dbgi, declaration->v.entity);
635 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
636 ir_entity *entity = declaration->v.entity;
637 ir_node *symconst = create_symconst(dbgi, entity);
639 if(type->type == TYPE_ARRAY) {
642 return load_from_expression_addr(type, symconst, dbgi);
645 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
646 ir_entity *entity = declaration->v.entity;
647 ir_node *frame = get_irg_frame(current_ir_graph);
648 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
650 if(type->type == TYPE_ARRAY) {
653 return load_from_expression_addr(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 assert(function->datatype->type == TYPE_FUNCTION);
706 function_type_t *function_type = (function_type_t*) function->datatype;
708 int n_parameters = 0;
709 call_argument_t *argument = call->arguments;
710 for( ; argument != NULL; argument = argument->next) {
714 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
715 ir_type *new_method_type = NULL;
716 if(function_type->variadic || function_type->unspecified_parameters) {
717 /* we need to construct a new method type matching the call
719 int n_res = get_method_n_ress(ir_method_type);
720 new_method_type = new_type_method(unique_ident("calltype"),
721 n_parameters, n_res);
722 set_method_calling_convention(new_method_type,
723 get_method_calling_convention(ir_method_type));
724 set_method_additional_properties(new_method_type,
725 get_method_additional_properties(ir_method_type));
727 for(int i = 0; i < n_res; ++i) {
728 set_method_res_type(new_method_type, i,
729 get_method_res_type(ir_method_type, i));
732 ir_node *in[n_parameters];
734 argument = call->arguments;
736 for( ; argument != NULL; argument = argument->next) {
737 expression_t *expression = argument->expression;
738 ir_node *arg_node = expression_to_firm(expression);
741 if(new_method_type != NULL) {
742 ir_type *irtype = get_ir_type(expression->datatype);
743 set_method_param_type(new_method_type, n, irtype);
748 assert(n == n_parameters);
750 if(new_method_type != NULL)
751 ir_method_type = new_method_type;
753 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
754 ir_node *store = get_store();
755 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
757 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
760 type_t *result_type = function_type->result_type;
761 ir_node *result = NULL;
762 if(result_type != type_void) {
763 ir_mode *mode = get_ir_mode(result_type);
764 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
765 result = new_d_Proj(dbgi, resproj, mode, 0);
771 static ir_node *expression_to_addr(const expression_t *expression);
773 static void set_value_for_expression(const expression_t *expression,
776 if(expression->type == EXPR_REFERENCE) {
777 reference_expression_t *ref = (reference_expression_t*) expression;
779 declaration_t *declaration = ref->declaration;
780 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
781 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
782 set_value(declaration->v.value_number, value);
787 dbg_info *dbgi = get_dbg_info(&expression->source_position);
788 ir_node *addr = expression_to_addr(expression);
789 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
790 ir_node *memory = get_store();
791 ir_node *store = new_d_Store(dbgi, memory, addr, value);
792 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
793 set_store(store_mem);
796 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
798 ir_mode *value_mode = get_irn_mode(value);
800 if(value_mode == dest_mode)
803 if(dest_mode == mode_b) {
804 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
805 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
806 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
810 return new_d_Conv(dbgi, value, dest_mode);
813 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
815 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
816 type_t *type = expression->expression.datatype;
817 ir_mode *mode = get_ir_mode(type);
819 if(expression->type == UNEXPR_TAKE_ADDRESS)
820 return expression_to_addr(expression->value);
822 const expression_t *value = expression->value;
823 ir_node *value_node = expression_to_firm(value);
825 switch(expression->type) {
827 return new_d_Minus(dbgi, value_node, mode);
830 case UNEXPR_BITWISE_NEGATE:
831 return new_d_Not(dbgi, value_node, mode);
833 if(get_irn_mode(value_node) != mode_b) {
834 value_node = create_conv(dbgi, value_node, mode_b);
836 value_node = new_d_Not(dbgi, value_node, mode_b);
838 value_node = create_conv(dbgi, value_node, mode);
841 case UNEXPR_DEREFERENCE:
842 return load_from_expression_addr(type, value_node, dbgi);
843 case UNEXPR_POSTFIX_INCREMENT: {
844 ir_node *one = new_Const(mode, get_mode_one(mode));
845 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
846 set_value_for_expression(value, new_value);
849 case UNEXPR_POSTFIX_DECREMENT: {
850 ir_node *one = new_Const(mode, get_mode_one(mode));
851 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
852 set_value_for_expression(value, new_value);
855 case UNEXPR_PREFIX_INCREMENT: {
856 ir_node *one = new_Const(mode, get_mode_one(mode));
857 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
858 set_value_for_expression(value, new_value);
861 case UNEXPR_PREFIX_DECREMENT: {
862 ir_node *one = new_Const(mode, get_mode_one(mode));
863 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
864 set_value_for_expression(value, new_value);
868 return create_conv(dbgi, value_node, mode);
870 case UNEXPR_TAKE_ADDRESS:
874 panic("invalid UNEXPR type found");
877 static long get_pnc(binary_expression_type_t type)
880 case BINEXPR_EQUAL: return pn_Cmp_Eq;
881 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
882 case BINEXPR_LESS: return pn_Cmp_Lt;
883 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
884 case BINEXPR_GREATER: return pn_Cmp_Gt;
885 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
889 panic("trying to get pn_Cmp from non-comparison binexpr type");
892 static ir_node *create_lazy_op(const binary_expression_t *expression)
894 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
896 bool is_or = (expression->type == BINEXPR_LOGICAL_OR);
897 assert(is_or || expression->type == BINEXPR_LOGICAL_AND);
899 ir_node *val1 = expression_to_modeb(expression->left);
900 ir_node *cond = new_d_Cond(dbgi, val1);
901 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
902 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
904 ir_node *fallthrough_block = new_immBlock();
907 ir_node *calc_val2_block = new_immBlock();
909 add_immBlock_pred(calc_val2_block, false_proj);
911 add_immBlock_pred(calc_val2_block, true_proj);
914 mature_immBlock(calc_val2_block);
916 ir_node *val2 = expression_to_modeb(expression->right);
917 if(get_cur_block() != NULL) {
918 ir_node *jmp = new_d_Jmp(dbgi);
919 add_immBlock_pred(fallthrough_block, jmp);
925 constb = new_d_Const(dbgi, mode_b, get_tarval_b_true());
926 add_immBlock_pred(fallthrough_block, true_proj);
928 constb = new_d_Const(dbgi, mode_b, get_tarval_b_false());
929 add_immBlock_pred(fallthrough_block, false_proj);
931 mature_immBlock(fallthrough_block);
933 set_cur_block(fallthrough_block);
935 ir_node *in[2] = { val2, constb };
936 ir_node *val = new_d_Phi(dbgi, 2, in, mode_b);
941 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
942 ir_node *right, ir_mode *mode);
944 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
945 create_arithmetic_func func)
947 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
948 ir_node *left = expression_to_firm(expression->left);
949 ir_node *right = expression_to_firm(expression->right);
950 type_t *type = expression->right->datatype;
951 /* be careful with the modes, because in asithmetic assign nodes only
952 * the right operand has the mode of the arithmetic alread */
953 ir_mode *mode = get_ir_mode(type);
954 left = create_conv(dbgi, left, mode);
955 ir_node *res = func(dbgi, left, right, mode);
960 static ir_node *create_arithmetic_assign_binop(
961 const binary_expression_t *expression, create_arithmetic_func func)
963 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
964 ir_node *value = create_arithmetic_binop(expression, func);
965 type_t *type = expression->expression.datatype;
966 ir_mode *mode = get_ir_mode(type);
968 assert(type->type != TYPE_POINTER);
970 value = create_conv(dbgi, value, mode);
971 set_value_for_expression(expression->left, value);
976 static ir_node *create_add(const binary_expression_t *expression)
978 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
979 ir_node *left = expression_to_firm(expression->left);
980 ir_node *right = expression_to_firm(expression->right);
981 type_t *type = expression->expression.datatype;
982 ir_mode *mode = get_ir_mode(type);
984 expression_t *expr_left = expression->left;
985 expression_t *expr_right = expression->right;
986 type_t *type_left = skip_typeref(expr_left->datatype);
987 type_t *type_right = skip_typeref(expr_right->datatype);
989 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
990 return new_d_Add(dbgi, left, right, mode);
995 pointer_type_t *pointer_type;
996 if(type_left->type == TYPE_POINTER) {
999 pointer_type = (pointer_type_t*) type_left;
1001 assert(type_right->type == TYPE_POINTER);
1004 pointer_type = (pointer_type_t*) type_right;
1007 type_t *points_to = pointer_type->points_to;
1008 unsigned elem_size = get_type_size(points_to);
1010 assert(elem_size >= 1);
1012 integer = create_conv(dbgi, integer, mode_Is);
1013 ir_node *cnst = new_Const_long(mode_Is, (int) elem_size);
1014 ir_node *mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1018 ir_node *res = new_d_Add(dbgi, pointer, integer, mode);
1023 static ir_node *create_sub(const binary_expression_t *expression)
1025 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1026 ir_node *left = expression_to_firm(expression->left);
1027 ir_node *right = expression_to_firm(expression->right);
1028 type_t *type = expression->expression.datatype;
1029 ir_mode *mode = get_ir_mode(type);
1031 expression_t *expr_left = expression->left;
1032 expression_t *expr_right = expression->right;
1033 type_t *type_left = skip_typeref(expr_left->datatype);
1034 type_t *type_right = skip_typeref(expr_right->datatype);
1036 if((is_type_arithmetic(type_left) && is_type_arithmetic(type_right))
1037 || (type_left->type == TYPE_POINTER
1038 && type_right->type == TYPE_POINTER)) {
1039 return new_d_Sub(dbgi, left, right, mode);
1042 assert(type_right->type == TYPE_POINTER);
1043 ir_node *pointer = left;
1044 ir_node *integer = right;
1045 pointer_type_t *pointer_type = (pointer_type_t*) type_right;
1047 type_t *points_to = pointer_type->points_to;
1048 unsigned elem_size = get_type_size(points_to);
1050 assert(elem_size >= 1);
1052 ir_node *cnst = new_Const_long(mode_Iu, elem_size);
1053 ir_node *mul = new_d_Mul(dbgi, integer, cnst, mode_Iu);
1057 ir_node *res = new_d_Sub(dbgi, pointer, integer, mode);
1062 static ir_node *create_shift(const binary_expression_t *expression)
1064 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1065 ir_node *left = expression_to_firm(expression->left);
1066 ir_node *right = expression_to_firm(expression->right);
1067 type_t *type = expression->expression.datatype;
1068 ir_mode *mode = get_ir_mode(type);
1070 /* firm always wants the shift count to be unsigned */
1071 right = create_conv(dbgi, right, mode_Iu);
1075 switch(expression->type) {
1076 case BINEXPR_SHIFTLEFT:
1077 res = new_d_Shl(dbgi, left, right, mode);
1079 case BINEXPR_SHIFTRIGHT: {
1080 expression_t *expr_left = expression->left;
1081 type_t *type_left = skip_typeref(expr_left->datatype);
1083 if(is_type_signed(type_left)) {
1084 res = new_d_Shrs(dbgi, left, right, mode);
1086 res = new_d_Shr(dbgi, left, right, mode);
1091 panic("create shift op called for non-shift op");
1098 static ir_node *create_divmod(const binary_expression_t *expression)
1100 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1101 ir_node *left = expression_to_firm(expression->left);
1102 ir_node *right = expression_to_firm(expression->right);
1103 ir_node *pin = new_Pin(new_NoMem());
1104 type_t *type = expression->expression.datatype;
1105 ir_mode *mode = get_ir_mode(type);
1109 if(expression->type == BINEXPR_DIV) {
1110 if(mode_is_float(mode)) {
1111 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1112 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1114 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1115 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1118 assert(expression->type == BINEXPR_MOD);
1119 assert(!mode_is_float(mode));
1120 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1121 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1129 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1131 binary_expression_type_t type = expression->type;
1134 case BINEXPR_NOTEQUAL:
1136 case BINEXPR_LESSEQUAL:
1137 case BINEXPR_GREATER:
1138 case BINEXPR_GREATEREQUAL: {
1139 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1140 ir_node *left = expression_to_firm(expression->left);
1141 ir_node *right = expression_to_firm(expression->right);
1142 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1143 long pnc = get_pnc(type);
1144 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1147 case BINEXPR_ASSIGN: {
1148 ir_node *right = expression_to_firm(expression->right);
1149 set_value_for_expression(expression->left, right);
1153 return create_add(expression);
1155 return create_sub(expression);
1157 return create_arithmetic_binop(expression, new_d_Mul);
1158 case BINEXPR_BITWISE_AND:
1159 return create_arithmetic_binop(expression, new_d_And);
1160 case BINEXPR_BITWISE_OR:
1161 return create_arithmetic_binop(expression, new_d_Or);
1162 case BINEXPR_BITWISE_XOR:
1163 return create_arithmetic_binop(expression, new_d_Eor);
1164 case BINEXPR_SHIFTLEFT:
1165 case BINEXPR_SHIFTRIGHT:
1166 return create_shift(expression);
1169 return create_divmod(expression);
1170 case BINEXPR_LOGICAL_AND:
1171 case BINEXPR_LOGICAL_OR:
1172 return create_lazy_op(expression);
1174 expression_to_firm(expression->left);
1175 return expression_to_firm(expression->right);
1176 case BINEXPR_ADD_ASSIGN:
1177 return create_arithmetic_assign_binop(expression, new_d_Add);
1178 case BINEXPR_SUB_ASSIGN:
1179 return create_arithmetic_assign_binop(expression, new_d_Sub);
1180 case BINEXPR_MUL_ASSIGN:
1181 return create_arithmetic_assign_binop(expression, new_d_Mul);
1182 case BINEXPR_BITWISE_AND_ASSIGN:
1183 return create_arithmetic_assign_binop(expression, new_d_And);
1184 case BINEXPR_BITWISE_OR_ASSIGN:
1185 return create_arithmetic_assign_binop(expression, new_d_Or);
1186 case BINEXPR_BITWISE_XOR_ASSIGN:
1187 return create_arithmetic_assign_binop(expression, new_d_Eor);
1188 case BINEXPR_SHIFTLEFT_ASSIGN:
1189 return create_arithmetic_assign_binop(expression, new_d_Shl);
1190 case BINEXPR_SHIFTRIGHT_ASSIGN:
1191 return create_arithmetic_assign_binop(expression, new_d_Shr);
1193 panic("TODO binexpr type");
1197 static ir_node *array_access_addr(const array_access_expression_t *expression)
1199 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1201 ir_node *base_addr = expression_to_firm(expression->array_ref);
1202 ir_node *offset = expression_to_firm(expression->index);
1203 offset = create_conv(dbgi, offset, mode_Iu);
1205 unsigned elem_size = get_type_size(expression->expression.datatype);
1206 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1207 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1209 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1214 static ir_node *array_access_to_firm(
1215 const array_access_expression_t *expression)
1217 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1218 ir_node *addr = array_access_addr(expression);
1219 type_t *type = expression->expression.datatype;
1221 return load_from_expression_addr(type, addr, dbgi);
1224 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1226 type_t *type = expression->type;
1228 type = expression->size_expression->datatype;
1229 assert(type != NULL);
1232 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1233 unsigned size = get_type_size(type);
1234 ir_node *size_node = new_Const_long(mode, size);
1239 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1241 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1243 ir_node *condition = expression_to_modeb(expression->condition);
1244 ir_node *cond = new_d_Cond(dbgi, condition);
1245 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1246 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1248 /* create the true block */
1249 ir_node *true_block = new_immBlock();
1250 add_immBlock_pred(true_block, true_proj);
1251 mature_immBlock(true_block);
1253 ir_node *true_val = expression_to_firm(expression->true_expression);
1254 ir_node *true_jmp = new_Jmp();
1256 /* create the false block */
1257 ir_node *false_block = new_immBlock();
1258 add_immBlock_pred(false_block, false_proj);
1259 mature_immBlock(false_block);
1261 ir_node *false_val = expression_to_firm(expression->false_expression);
1262 ir_node *false_jmp = new_Jmp();
1264 /* create the common block */
1265 ir_node *common_block = new_immBlock();
1266 add_immBlock_pred(common_block, true_jmp);
1267 add_immBlock_pred(common_block, false_jmp);
1268 mature_immBlock(common_block);
1270 ir_node *in[2] = { true_val, false_val };
1271 ir_mode *mode = get_irn_mode(true_val);
1272 assert(get_irn_mode(false_val) == mode);
1273 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1278 static ir_node *select_addr(const select_expression_t *expression)
1280 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1282 ir_node *compound_addr = expression_to_firm(expression->compound);
1284 declaration_t *entry = expression->compound_entry;
1285 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1286 ir_entity *entity = entry->v.entity;
1288 assert(entity != NULL);
1290 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1295 static ir_node *select_to_firm(const select_expression_t *expression)
1297 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1298 ir_node *addr = select_addr(expression);
1299 type_t *type = expression->expression.datatype;
1301 return load_from_expression_addr(type, addr, dbgi);
1304 static ir_node *dereference_addr(const unary_expression_t *const expression)
1306 assert(expression->type == UNEXPR_DEREFERENCE);
1307 return expression_to_firm(expression->value);
1310 static ir_node *expression_to_addr(const expression_t *expression)
1312 switch(expression->type) {
1313 case EXPR_REFERENCE:
1314 return reference_addr((const reference_expression_t*) expression);
1315 case EXPR_ARRAY_ACCESS:
1316 return array_access_addr((const array_access_expression_t*) expression);
1318 return select_addr((const select_expression_t*) expression);
1320 const unary_expression_t *const unary_expr =
1321 (const unary_expression_t*)expression;
1322 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1323 return dereference_addr(unary_expr);
1330 panic("trying to get address of non-lvalue");
1333 static ir_node *_expression_to_firm(const expression_t *expression)
1335 switch(expression->type) {
1337 return const_to_firm((const const_t*) expression);
1338 case EXPR_STRING_LITERAL:
1339 return string_literal_to_firm((const string_literal_t*) expression);
1340 case EXPR_REFERENCE:
1341 return reference_expression_to_firm(
1342 (const reference_expression_t*) expression);
1344 return call_expression_to_firm((const call_expression_t*) expression);
1346 return unary_expression_to_firm((const unary_expression_t*) expression);
1348 return binary_expression_to_firm(
1349 (const binary_expression_t*) expression);
1350 case EXPR_ARRAY_ACCESS:
1351 return array_access_to_firm(
1352 (const array_access_expression_t*) expression);
1354 return sizeof_to_firm((const sizeof_expression_t*) expression);
1355 case EXPR_CONDITIONAL:
1356 return conditional_to_firm((const conditional_expression_t*)expression);
1358 return select_to_firm((const select_expression_t*) expression);
1361 case EXPR_PRETTY_FUNCTION:
1363 case EXPR_STATEMENT:
1364 case EXPR_BUILTIN_SYMBOL:
1365 panic("unimplemented expression found");
1371 panic("invalid expression found");
1374 static ir_node *expression_to_firm(const expression_t *expression)
1376 ir_node *res = _expression_to_firm(expression);
1378 if(expression->datatype == type_void)
1381 ir_mode *mode = get_ir_mode(expression->datatype);
1382 res = create_conv(NULL, res, mode);
1386 static ir_node *expression_to_modeb(const expression_t *expression)
1388 ir_node *res = _expression_to_firm(expression);
1389 res = create_conv(NULL, res, mode_b);
1394 static void statement_to_firm(statement_t *statement);
1396 static void return_statement_to_firm(return_statement_t *statement)
1398 if(get_cur_block() == NULL)
1401 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1404 if(statement->return_value != NULL) {
1405 ir_node *retval = expression_to_firm(statement->return_value);
1409 ret = new_d_Return(dbgi, get_store(), 1, in);
1411 ret = new_d_Return(dbgi, get_store(), 0, NULL);
1413 ir_node *end_block = get_irg_end_block(current_ir_graph);
1414 add_immBlock_pred(end_block, ret);
1416 set_cur_block(NULL);
1419 static void compound_statement_to_firm(compound_statement_t *compound)
1421 statement_t *statement = compound->statements;
1422 for( ; statement != NULL; statement = statement->next) {
1423 //context2firm(&statement->context);
1424 statement_to_firm(statement);
1428 static void expression_statement_to_firm(expression_statement_t *statement)
1430 if(get_cur_block() == NULL)
1433 expression_to_firm(statement->expression);
1436 static void if_statement_to_firm(if_statement_t *statement)
1438 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1439 ir_node *condition = expression_to_modeb(statement->condition);
1441 /* make sure we have a mode_b condition */
1442 ir_node *cond = new_d_Cond(dbgi, condition);
1443 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1444 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1446 ir_node *fallthrough_block = new_immBlock();
1448 /* the true (blocks) */
1449 ir_node *true_block = new_immBlock();
1450 add_immBlock_pred(true_block, true_proj);
1451 mature_immBlock(true_block);
1453 statement_to_firm(statement->true_statement);
1454 if(get_cur_block() != NULL) {
1455 ir_node *jmp = new_Jmp();
1456 add_immBlock_pred(fallthrough_block, jmp);
1459 /* the false (blocks) */
1460 if(statement->false_statement != NULL) {
1461 ir_node *false_block = new_immBlock();
1462 add_immBlock_pred(false_block, false_proj);
1463 mature_immBlock(false_block);
1465 statement_to_firm(statement->false_statement);
1466 if(get_cur_block() != NULL) {
1467 ir_node *jmp = new_Jmp();
1468 add_immBlock_pred(fallthrough_block, jmp);
1471 add_immBlock_pred(fallthrough_block, false_proj);
1473 mature_immBlock(fallthrough_block);
1475 set_cur_block(fallthrough_block);
1478 static void while_statement_to_firm(while_statement_t *statement)
1480 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1482 ir_node *jmp = NULL;
1483 if(get_cur_block() != NULL) {
1487 /* create the header block */
1488 ir_node *header_block = new_immBlock();
1490 add_immBlock_pred(header_block, jmp);
1493 /* create the condition */
1494 ir_node *condition = expression_to_modeb(statement->condition);
1495 ir_node *cond = new_d_Cond(dbgi, condition);
1496 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1497 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1499 /* the false block */
1500 ir_node *false_block = new_immBlock();
1501 add_immBlock_pred(false_block, false_proj);
1504 ir_node *body_block = new_immBlock();
1505 add_immBlock_pred(body_block, true_proj);
1506 mature_immBlock(body_block);
1508 ir_node *old_continue_label = continue_label;
1509 ir_node *old_break_label = break_label;
1510 continue_label = header_block;
1511 break_label = false_block;
1513 statement_to_firm(statement->body);
1515 assert(continue_label == header_block);
1516 assert(break_label == false_block);
1517 continue_label = old_continue_label;
1518 break_label = old_break_label;
1520 if(get_cur_block() != NULL) {
1521 ir_node *jmp = new_Jmp();
1522 add_immBlock_pred(header_block, jmp);
1525 mature_immBlock(header_block);
1526 mature_immBlock(false_block);
1528 set_cur_block(false_block);
1531 static void do_while_statement_to_firm(do_while_statement_t *statement)
1533 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1535 ir_node *jmp = NULL;
1536 if(get_cur_block() != NULL) {
1540 /* create the header block */
1541 ir_node *header_block = new_immBlock();
1543 /* the false block */
1544 ir_node *false_block = new_immBlock();
1547 ir_node *body_block = new_immBlock();
1549 add_immBlock_pred(body_block, jmp);
1552 ir_node *old_continue_label = continue_label;
1553 ir_node *old_break_label = break_label;
1554 continue_label = header_block;
1555 break_label = false_block;
1557 statement_to_firm(statement->body);
1559 assert(continue_label == header_block);
1560 assert(break_label == false_block);
1561 continue_label = old_continue_label;
1562 break_label = old_break_label;
1564 if(get_cur_block() == NULL) {
1565 mature_immBlock(header_block);
1566 mature_immBlock(body_block);
1567 mature_immBlock(false_block);
1571 ir_node *body_jmp = new_Jmp();
1572 add_immBlock_pred(header_block, body_jmp);
1573 mature_immBlock(header_block);
1575 /* create the condition */
1576 set_cur_block(header_block);
1577 ir_node *condition = expression_to_modeb(statement->condition);
1578 ir_node *cond = new_d_Cond(dbgi, condition);
1579 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1580 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1582 add_immBlock_pred(body_block, true_proj);
1583 mature_immBlock(body_block);
1585 add_immBlock_pred(false_block, false_proj);
1586 mature_immBlock(false_block);
1588 set_cur_block(false_block);
1591 static void for_statement_to_firm(for_statement_t *statement)
1593 dbg_info *const dbgi = get_dbg_info(&statement->statement.source_position);
1595 ir_node *jmp = NULL;
1596 if (get_cur_block() != NULL) {
1597 if(statement->initialisation != NULL) {
1598 expression_to_firm(statement->initialisation);
1603 /* create the step block */
1604 ir_node *const step_block = new_immBlock();
1605 if (statement->step != NULL) {
1606 expression_to_firm(statement->step);
1608 ir_node *const step_jmp = new_Jmp();
1610 /* create the header block */
1611 ir_node *const header_block = new_immBlock();
1613 add_immBlock_pred(header_block, jmp);
1615 add_immBlock_pred(header_block, step_jmp);
1617 /* create the condition */
1619 ir_node *false_proj;
1620 if (statement->condition != NULL) {
1621 ir_node *const condition = expression_to_modeb(statement->condition);
1622 ir_node *const cond = new_d_Cond(dbgi, condition);
1623 true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1624 false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1626 keep_alive(header_block);
1627 true_proj = new_Jmp();
1631 /* the false block */
1632 ir_node *const false_block = new_immBlock();
1633 if (false_proj != NULL) {
1634 add_immBlock_pred(false_block, false_proj);
1638 ir_node *const body_block = new_immBlock();
1639 add_immBlock_pred(body_block, true_proj);
1640 mature_immBlock(body_block);
1642 ir_node *const old_continue_label = continue_label;
1643 ir_node *const old_break_label = break_label;
1644 continue_label = step_block;
1645 break_label = false_block;
1647 statement_to_firm(statement->body);
1649 assert(continue_label == step_block);
1650 assert(break_label == false_block);
1651 continue_label = old_continue_label;
1652 break_label = old_break_label;
1654 if (get_cur_block() != NULL) {
1655 ir_node *const jmp = new_Jmp();
1656 add_immBlock_pred(step_block, jmp);
1659 mature_immBlock(step_block);
1660 mature_immBlock(header_block);
1661 mature_immBlock(false_block);
1663 set_cur_block(false_block);
1666 static void create_declaration_entity(declaration_t *declaration,
1667 declaration_type_t declaration_type,
1668 ir_type *parent_type)
1670 ident *id = new_id_from_str(declaration->symbol->string);
1671 ir_type *irtype = get_ir_type(declaration->type);
1672 ir_entity *entity = new_entity(parent_type, id, irtype);
1673 set_entity_ld_ident(entity, id);
1675 declaration->declaration_type = declaration_type;
1676 declaration->v.entity = entity;
1677 set_entity_variability(entity, variability_uninitialized);
1678 /* TODO: visibility? */
1681 static void create_initializer(declaration_t *declaration)
1683 initializer_t *initializer = declaration->init.initializer;
1684 if(initializer == NULL)
1687 if(initializer->type == INITIALIZER_VALUE) {
1688 assert(initializer->designator == NULL);
1689 assert(initializer->next == NULL);
1690 ir_node *init_node = expression_to_firm(initializer->v.value);
1692 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1693 set_value(declaration->v.value_number, init_node);
1695 ir_entity *entity = declaration->v.entity;
1697 set_entity_variability(entity, variability_initialized);
1698 set_atomic_ent_value(entity, init_node);
1701 assert(initializer->type == INITIALIZER_LIST);
1702 panic("list initializer not supported yet");
1706 static void create_local_variable(declaration_t *declaration)
1708 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1710 bool needs_entity = declaration->address_taken;
1711 type_t *type = declaration->type;
1712 if(type->type == TYPE_ARRAY
1713 || type->type == TYPE_COMPOUND_STRUCT
1714 || type->type == TYPE_COMPOUND_UNION) {
1715 needs_entity = true;
1719 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1720 create_declaration_entity(declaration,
1721 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1724 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1725 declaration->v.value_number = next_value_number_function;
1726 ++next_value_number_function;
1729 create_initializer(declaration);
1732 static void declaration_statement_to_firm(declaration_statement_t *statement)
1734 declaration_t *declaration = statement->declarations_begin;
1735 declaration_t *end = statement->declarations_end->next;
1736 for( ; declaration != end; declaration = declaration->next) {
1737 type_t *type = declaration->type;
1739 switch(declaration->storage_class) {
1740 case STORAGE_CLASS_TYPEDEF:
1742 case STORAGE_CLASS_STATIC:
1743 panic("static local vars not implemented yet");
1744 case STORAGE_CLASS_ENUM_ENTRY:
1745 panic("enum entry declaration in local block found");
1746 case STORAGE_CLASS_EXTERN:
1747 panic("extern declaration in local block found");
1748 case STORAGE_CLASS_NONE:
1749 case STORAGE_CLASS_AUTO:
1750 case STORAGE_CLASS_REGISTER:
1751 if(type->type == TYPE_FUNCTION) {
1752 panic("nested functions not supported yet");
1754 create_local_variable(declaration);
1758 panic("invalid storage class found");
1762 static void create_jump_statement(const statement_t *statement,
1763 ir_node *target_block)
1765 if(get_cur_block() == NULL)
1768 dbg_info *dbgi = get_dbg_info(&statement->source_position);
1769 ir_node *jump = new_d_Jmp(dbgi);
1770 add_immBlock_pred(target_block, jump);
1772 set_cur_block(NULL);
1775 static void switch_statement_to_firm(const switch_statement_t *statement)
1777 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1779 ir_node *expression = expression_to_firm(statement->expression);
1780 ir_node *cond = new_d_Cond(dbgi, expression);
1781 ir_node *break_block = new_immBlock();
1783 set_cur_block(NULL);
1785 ir_node *old_switch_cond = current_switch_cond;
1786 ir_node *old_break_label = break_label;
1787 current_switch_cond = cond;
1788 break_label = break_block;
1790 statement_to_firm(statement->body);
1792 if(get_cur_block() != NULL) {
1793 ir_node *jmp = new_Jmp();
1794 add_immBlock_pred(break_block, jmp);
1797 assert(current_switch_cond == cond);
1798 assert(break_label == break_block);
1799 current_switch_cond = old_switch_cond;
1800 break_label = old_break_label;
1802 mature_immBlock(break_block);
1803 set_cur_block(break_block);
1806 static long fold_constant(const expression_t *expression)
1808 ir_graph *old_current_ir_graph = current_ir_graph;
1809 current_ir_graph = get_const_code_irg();
1811 ir_node *cnst = expression_to_firm(expression);
1812 if(!is_Const(cnst)) {
1813 panic("couldn't fold constantl");
1815 tarval *tv = get_Const_tarval(cnst);
1816 if(!tarval_is_long(tv)) {
1817 panic("folded constant not an integer");
1820 long res = get_tarval_long(tv);
1822 current_ir_graph = old_current_ir_graph;
1826 static void case_label_to_firm(const case_label_statement_t *statement)
1828 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1830 /* let's create a node and hope firm constant folding creates a Const
1833 set_cur_block(get_nodes_block(current_switch_cond));
1834 if(statement->expression) {
1835 long pn = fold_constant(statement->expression);
1836 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
1837 /* oops someone detected our cheating... */
1838 panic("magic default pn used");
1840 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
1842 proj = new_d_defaultProj(dbgi, current_switch_cond,
1843 MAGIC_DEFAULT_PN_NUMBER);
1846 ir_node *block = new_immBlock();
1847 add_immBlock_pred(block, proj);
1848 mature_immBlock(block);
1851 static ir_node *get_label_block(declaration_t *label)
1853 assert(label->namespace == NAMESPACE_LABEL);
1855 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
1856 return label->v.block;
1858 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
1860 ir_node *old_cur_block = get_cur_block();
1861 ir_node *block = new_immBlock();
1862 set_cur_block(old_cur_block);
1864 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
1865 label->v.block = block;
1867 ARR_APP1(imature_blocks, block);
1872 static void label_to_firm(const label_statement_t *statement)
1874 ir_node *block = get_label_block(statement->label);
1876 if(get_cur_block() != NULL) {
1877 ir_node *jmp = new_Jmp();
1878 add_immBlock_pred(block, jmp);
1881 set_cur_block(block);
1884 statement_to_firm(statement->label_statement);
1887 static void goto_to_firm(const goto_statement_t *statement)
1889 if(get_cur_block() == NULL)
1892 ir_node *block = get_label_block(statement->label);
1893 ir_node *jmp = new_Jmp();
1894 add_immBlock_pred(block, jmp);
1896 set_cur_block(NULL);
1899 static void statement_to_firm(statement_t *statement)
1901 switch(statement->type) {
1902 case STATEMENT_COMPOUND:
1903 compound_statement_to_firm((compound_statement_t*) statement);
1905 case STATEMENT_RETURN:
1906 return_statement_to_firm((return_statement_t*) statement);
1908 case STATEMENT_EXPRESSION:
1909 expression_statement_to_firm((expression_statement_t*) statement);
1912 if_statement_to_firm((if_statement_t*) statement);
1914 case STATEMENT_WHILE:
1915 while_statement_to_firm((while_statement_t*) statement);
1917 case STATEMENT_DO_WHILE:
1918 do_while_statement_to_firm((do_while_statement_t*) statement);
1920 case STATEMENT_DECLARATION:
1921 declaration_statement_to_firm((declaration_statement_t*) statement);
1923 case STATEMENT_BREAK:
1924 create_jump_statement(statement, break_label);
1926 case STATEMENT_CONTINUE:
1927 create_jump_statement(statement, continue_label);
1929 case STATEMENT_SWITCH:
1930 switch_statement_to_firm((switch_statement_t*) statement);
1932 case STATEMENT_CASE_LABEL:
1933 case_label_to_firm((case_label_statement_t*) statement);
1936 for_statement_to_firm((for_statement_t*) statement);
1938 case STATEMENT_LABEL:
1939 label_to_firm((label_statement_t*) statement);
1941 case STATEMENT_GOTO:
1942 goto_to_firm((goto_statement_t*) statement);
1947 panic("Statement not implemented\n");
1950 static int get_function_n_local_vars(declaration_t *declaration)
1957 static void initialize_function_parameters(declaration_t *declaration)
1959 ir_graph *irg = current_ir_graph;
1960 ir_node *args = get_irg_args(irg);
1961 ir_node *start_block = get_irg_start_block(irg);
1964 declaration_t *parameter = declaration->context.declarations;
1965 for( ; parameter != NULL; parameter = parameter->next) {
1966 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
1968 if(parameter->address_taken) {
1969 panic("address take from parameter not implemented yet");
1972 ir_mode *mode = get_ir_mode(parameter->type);
1974 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
1977 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1978 parameter->v.value_number = next_value_number_function;
1979 ++next_value_number_function;
1981 set_value(parameter->v.value_number, proj);
1985 static void create_function(declaration_t *declaration)
1987 ir_entity *entity = get_function_entity(declaration);
1989 if(declaration->init.statement == NULL)
1992 assert(imature_blocks == NULL);
1993 imature_blocks = NEW_ARR_F(ir_node*, 0);
1995 int n_local_vars = get_function_n_local_vars(declaration);
1996 ir_graph *irg = new_ir_graph(entity, n_local_vars);
1997 ir_node *first_block = get_cur_block();
1999 next_value_number_function = 0;
2000 initialize_function_parameters(declaration);
2002 statement_to_firm(declaration->init.statement);
2004 ir_node *end_block = get_irg_end_block(irg);
2006 /* do we have a return statement yet? */
2007 if(get_cur_block() != NULL) {
2008 assert(declaration->type->type == TYPE_FUNCTION);
2009 const function_type_t* const func_type
2010 = (const function_type_t*) declaration->type;
2012 if (func_type->result_type == type_void) {
2013 ret = new_Return(get_store(), 0, NULL);
2015 ir_mode *const mode = get_ir_mode(func_type->result_type);
2017 // ยง5.1.2.2.3 main implicitly returns 0
2018 if (strcmp(declaration->symbol->string, "main") == 0) {
2019 in[0] = new_Const(mode, get_mode_null(mode));
2021 in[0] = new_Unknown(mode);
2023 ret = new_Return(get_store(), 1, in);
2025 add_immBlock_pred(end_block, ret);
2028 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2029 mature_immBlock(imature_blocks[i]);
2031 DEL_ARR_F(imature_blocks);
2032 imature_blocks = NULL;
2034 mature_immBlock(first_block);
2035 mature_immBlock(end_block);
2037 irg_finalize_cons(irg);
2039 /* finalize the frame type */
2040 ir_type *frame_type = get_irg_frame_type(irg);
2041 int n = get_compound_n_members(frame_type);
2044 for(int i = 0; i < n; ++i) {
2045 ir_entity *entity = get_compound_member(frame_type, i);
2046 ir_type *entity_type = get_entity_type(entity);
2048 int align = get_type_alignment_bytes(entity_type);
2049 if(align > align_all)
2053 misalign = offset % align;
2057 set_entity_offset(entity, offset);
2058 offset += get_type_size_bytes(entity_type);
2060 set_type_size_bytes(frame_type, offset);
2061 set_type_alignment_bytes(frame_type, align_all);
2062 set_type_state(frame_type, layout_fixed);
2067 static void create_global_variable(declaration_t *declaration)
2069 ir_type *global_type = get_glob_type();
2070 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2073 ir_entity *entity = declaration->v.entity;
2074 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2075 set_entity_visibility(entity, visibility_local);
2076 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2077 set_entity_visibility(entity, visibility_external_allocated);
2079 set_entity_visibility(entity, visibility_external_visible);
2081 current_ir_graph = get_const_code_irg();
2082 create_initializer(declaration);
2085 static void context_to_firm(context_t *context)
2087 declaration_t *declaration = context->declarations;
2088 for( ; declaration != NULL; declaration = declaration->next) {
2089 if(declaration->namespace != NAMESPACE_NORMAL)
2091 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2092 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2094 if(declaration->symbol == NULL)
2097 type_t *type = declaration->type;
2098 if(type->type == TYPE_FUNCTION) {
2099 create_function(declaration);
2101 create_global_variable(declaration);
2106 void translation_unit_to_firm(translation_unit_t *unit)
2108 /* remove me later TODO FIXME */
2109 (void) get_type_size;
2111 /* just to be sure */
2112 continue_label = NULL;
2114 current_switch_cond = NULL;
2116 context_to_firm(& unit->context);