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.%d", 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->namespace != 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;
374 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
375 set_entity_offset(entity, offset);
376 add_struct_member(ir_type, entity);
377 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
378 entry->v.entity = entity;
380 offset += entry_size;
381 if(entry_alignment > align_all) {
382 if(entry_alignment % align_all != 0) {
383 panic("Uneven alignments not supported yet");
385 align_all = entry_alignment;
389 int misalign = offset % align_all;
391 set_type_alignment_bytes(ir_type, align_all);
392 set_type_size_bytes(ir_type, offset);
393 set_type_state(ir_type, layout_fixed);
398 static ir_type *create_union_type(compound_type_t *type)
400 declaration_t *declaration = type->declaration;
401 symbol_t *symbol = declaration->symbol;
404 id = unique_ident(symbol->string);
406 id = unique_ident("__anonymous_union");
408 ir_type *ir_type = new_type_union(id);
410 type->type.firm_type = ir_type;
414 declaration_t *entry = declaration->context.declarations;
415 for( ; entry != NULL; entry = entry->next) {
416 if(entry->namespace != NAMESPACE_NORMAL)
419 ident *ident = new_id_from_str(entry->symbol->string);
420 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
422 int entry_size = get_type_size_bytes(entry_ir_type);
423 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
425 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
426 add_union_member(ir_type, entity);
427 set_entity_offset(entity, 0);
428 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
429 entry->v.entity = entity;
431 if(entry_size > size) {
434 if(entry_alignment > align_all) {
435 if(entry_alignment % align_all != 0) {
436 panic("Uneven alignments not supported yet");
438 align_all = entry_alignment;
442 set_type_alignment_bytes(ir_type, align_all);
443 set_type_size_bytes(ir_type, size);
444 set_type_state(ir_type, layout_fixed);
449 static ir_type *get_ir_type(type_t *type)
451 assert(type != NULL);
453 type = skip_typeref(type);
455 if(type->firm_type != NULL) {
456 assert(type->firm_type != INVALID_TYPE);
457 return type->firm_type;
460 ir_type *firm_type = NULL;
463 firm_type = create_atomic_type((atomic_type_t*) type);
466 firm_type = create_method_type((function_type_t*) type);
469 firm_type = create_pointer_type((pointer_type_t*) type);
472 firm_type = create_array_type((array_type_t*) type);
474 case TYPE_COMPOUND_STRUCT:
475 firm_type = create_struct_type((compound_type_t*) type);
477 case TYPE_COMPOUND_UNION:
478 firm_type = create_union_type((compound_type_t*) type);
481 firm_type = ir_type_int;
489 if(firm_type == NULL)
490 panic("unknown type found");
492 type->firm_type = firm_type;
496 static inline ir_mode *get_ir_mode(type_t *type)
498 ir_type *irtype = get_ir_type(type);
500 /* firm doesn't report a mode for arrays somehow... */
501 if(is_Array_type(irtype)) {
505 ir_mode *mode = get_type_mode(irtype);
506 assert(mode != NULL);
510 static ir_entity* get_function_entity(declaration_t *declaration)
512 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
513 return declaration->v.entity;
514 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
516 symbol_t *symbol = declaration->symbol;
517 ident *id = new_id_from_str(symbol->string);
519 ir_type *global_type = get_glob_type();
520 ir_type *ir_type_method = get_ir_type(declaration->type);
521 assert(is_Method_type(ir_type_method));
523 ir_entity *entity = new_entity(global_type, id, ir_type_method);
524 set_entity_ld_ident(entity, id);
525 if(declaration->storage_class == STORAGE_CLASS_STATIC
526 || declaration->is_inline) {
527 set_entity_visibility(entity, visibility_local);
528 } else if(declaration->init.statement != NULL) {
529 set_entity_visibility(entity, visibility_external_visible);
531 set_entity_visibility(entity, visibility_external_allocated);
534 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
535 declaration->v.entity = entity;
542 static ir_node *expression_to_firm(const expression_t *expression);
543 static ir_node *expression_to_modeb(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 *load_from_expression_addr(type_t *type, ir_node *addr,
611 ir_mode *mode = get_ir_mode(type);
612 ir_node *memory = get_store();
613 ir_node *load = new_d_Load(dbgi, memory, addr, mode);
614 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
615 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
621 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
623 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
624 declaration_t *declaration = ref->declaration;
625 type_t *type = skip_typeref(declaration->type);
627 switch((declaration_type_t) declaration->declaration_type) {
628 case DECLARATION_TYPE_UNKNOWN:
630 case DECLARATION_TYPE_LOCAL_VARIABLE: {
631 ir_mode *mode = get_ir_mode(type);
632 return get_value(declaration->v.value_number, mode);
634 case DECLARATION_TYPE_FUNCTION: {
635 return create_symconst(dbgi, declaration->v.entity);
637 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
638 ir_entity *entity = declaration->v.entity;
639 ir_node *symconst = create_symconst(dbgi, entity);
641 if(type->type == TYPE_ARRAY) {
644 return load_from_expression_addr(type, symconst, dbgi);
647 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
648 ir_entity *entity = declaration->v.entity;
649 ir_node *frame = get_irg_frame(current_ir_graph);
650 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
652 if(type->type == TYPE_ARRAY || type->type == TYPE_COMPOUND_STRUCT
653 || type->type == TYPE_COMPOUND_UNION) {
656 return load_from_expression_addr(type, sel, dbgi);
660 case DECLARATION_TYPE_COMPOUND_MEMBER:
661 case DECLARATION_TYPE_LABEL_BLOCK:
662 panic("not implemented reference type");
665 panic("reference to declaration with unknown type found");
668 static ir_node *reference_addr(const reference_expression_t *ref)
670 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
671 declaration_t *declaration = ref->declaration;
673 switch((declaration_type_t) declaration->declaration_type) {
674 case DECLARATION_TYPE_UNKNOWN:
676 case DECLARATION_TYPE_LOCAL_VARIABLE:
677 panic("local variable without entity has no address");
678 case DECLARATION_TYPE_FUNCTION: {
679 return create_symconst(dbgi, declaration->v.entity);
681 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
682 ir_entity *entity = declaration->v.entity;
683 ir_node *symconst = create_symconst(dbgi, entity);
686 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
687 ir_entity *entity = declaration->v.entity;
688 ir_node *frame = get_irg_frame(current_ir_graph);
689 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
693 case DECLARATION_TYPE_COMPOUND_MEMBER:
694 case DECLARATION_TYPE_LABEL_BLOCK:
695 panic("not implemented reference type");
698 panic("reference to declaration with unknown type found");
701 static ir_node *call_expression_to_firm(const call_expression_t *call)
703 assert(get_cur_block() != NULL);
705 expression_t *function = call->function;
706 ir_node *callee = expression_to_firm(function);
708 assert(function->datatype->type == TYPE_FUNCTION);
709 function_type_t *function_type = (function_type_t*) function->datatype;
711 int n_parameters = 0;
712 call_argument_t *argument = call->arguments;
713 for( ; argument != NULL; argument = argument->next) {
717 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
718 ir_type *new_method_type = NULL;
719 if(function_type->variadic || function_type->unspecified_parameters) {
720 /* we need to construct a new method type matching the call
722 int n_res = get_method_n_ress(ir_method_type);
723 new_method_type = new_type_method(unique_ident("calltype"),
724 n_parameters, n_res);
725 set_method_calling_convention(new_method_type,
726 get_method_calling_convention(ir_method_type));
727 set_method_additional_properties(new_method_type,
728 get_method_additional_properties(ir_method_type));
730 for(int i = 0; i < n_res; ++i) {
731 set_method_res_type(new_method_type, i,
732 get_method_res_type(ir_method_type, i));
735 ir_node *in[n_parameters];
737 argument = call->arguments;
739 for( ; argument != NULL; argument = argument->next) {
740 expression_t *expression = argument->expression;
741 ir_node *arg_node = expression_to_firm(expression);
744 if(new_method_type != NULL) {
745 ir_type *irtype = get_ir_type(expression->datatype);
746 set_method_param_type(new_method_type, n, irtype);
751 assert(n == n_parameters);
753 if(new_method_type != NULL)
754 ir_method_type = new_method_type;
756 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
757 ir_node *store = get_store();
758 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
760 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
763 type_t *result_type = function_type->result_type;
764 ir_node *result = NULL;
765 if(result_type != type_void) {
766 ir_mode *mode = get_ir_mode(result_type);
767 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
768 result = new_d_Proj(dbgi, resproj, mode, 0);
774 static ir_node *expression_to_addr(const expression_t *expression);
776 static void set_value_for_expression(const expression_t *expression,
779 if(expression->type == EXPR_REFERENCE) {
780 reference_expression_t *ref = (reference_expression_t*) expression;
782 declaration_t *declaration = ref->declaration;
783 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
784 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
785 set_value(declaration->v.value_number, value);
790 dbg_info *dbgi = get_dbg_info(&expression->source_position);
791 ir_node *addr = expression_to_addr(expression);
792 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
793 ir_node *memory = get_store();
794 ir_node *store = new_d_Store(dbgi, memory, addr, value);
795 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
796 set_store(store_mem);
799 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
801 ir_mode *value_mode = get_irn_mode(value);
803 if(value_mode == dest_mode)
806 if(dest_mode == mode_b) {
807 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
808 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
809 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
813 return new_d_Conv(dbgi, value, dest_mode);
816 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
818 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
819 type_t *type = expression->expression.datatype;
820 ir_mode *mode = get_ir_mode(type);
822 if(expression->type == UNEXPR_TAKE_ADDRESS)
823 return expression_to_addr(expression->value);
825 const expression_t *value = expression->value;
826 ir_node *value_node = expression_to_firm(value);
828 switch(expression->type) {
830 return new_d_Minus(dbgi, value_node, mode);
833 case UNEXPR_BITWISE_NEGATE:
834 return new_d_Not(dbgi, value_node, mode);
836 if(get_irn_mode(value_node) != mode_b) {
837 value_node = create_conv(dbgi, value_node, mode_b);
839 value_node = new_d_Not(dbgi, value_node, mode_b);
841 value_node = create_conv(dbgi, value_node, mode);
844 case UNEXPR_DEREFERENCE:
845 return load_from_expression_addr(type, value_node, dbgi);
846 case UNEXPR_POSTFIX_INCREMENT: {
847 ir_node *one = new_Const(mode, get_mode_one(mode));
848 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
849 set_value_for_expression(value, new_value);
852 case UNEXPR_POSTFIX_DECREMENT: {
853 ir_node *one = new_Const(mode, get_mode_one(mode));
854 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
855 set_value_for_expression(value, new_value);
858 case UNEXPR_PREFIX_INCREMENT: {
859 ir_node *one = new_Const(mode, get_mode_one(mode));
860 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
861 set_value_for_expression(value, new_value);
864 case UNEXPR_PREFIX_DECREMENT: {
865 ir_node *one = new_Const(mode, get_mode_one(mode));
866 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
867 set_value_for_expression(value, new_value);
871 return create_conv(dbgi, value_node, mode);
873 case UNEXPR_TAKE_ADDRESS:
877 panic("invalid UNEXPR type found");
880 static long get_pnc(binary_expression_type_t type)
883 case BINEXPR_EQUAL: return pn_Cmp_Eq;
884 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
885 case BINEXPR_LESS: return pn_Cmp_Lt;
886 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
887 case BINEXPR_GREATER: return pn_Cmp_Gt;
888 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
892 panic("trying to get pn_Cmp from non-comparison binexpr type");
895 static ir_node *create_lazy_op(const binary_expression_t *expression)
897 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
899 bool is_or = (expression->type == BINEXPR_LOGICAL_OR);
900 assert(is_or || expression->type == BINEXPR_LOGICAL_AND);
902 ir_node *val1 = expression_to_modeb(expression->left);
903 ir_node *cond = new_d_Cond(dbgi, val1);
904 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
905 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
907 ir_node *fallthrough_block = new_immBlock();
910 ir_node *calc_val2_block = new_immBlock();
912 add_immBlock_pred(calc_val2_block, false_proj);
914 add_immBlock_pred(calc_val2_block, true_proj);
917 mature_immBlock(calc_val2_block);
919 ir_node *val2 = expression_to_modeb(expression->right);
920 if(get_cur_block() != NULL) {
921 ir_node *jmp = new_d_Jmp(dbgi);
922 add_immBlock_pred(fallthrough_block, jmp);
928 constb = new_d_Const(dbgi, mode_b, get_tarval_b_true());
929 add_immBlock_pred(fallthrough_block, true_proj);
931 constb = new_d_Const(dbgi, mode_b, get_tarval_b_false());
932 add_immBlock_pred(fallthrough_block, false_proj);
934 mature_immBlock(fallthrough_block);
936 set_cur_block(fallthrough_block);
938 ir_node *in[2] = { val2, constb };
939 ir_node *val = new_d_Phi(dbgi, 2, in, mode_b);
944 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
945 ir_node *right, ir_mode *mode);
947 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
948 create_arithmetic_func func)
950 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
951 ir_node *left = expression_to_firm(expression->left);
952 ir_node *right = expression_to_firm(expression->right);
953 type_t *type = expression->right->datatype;
954 /* be careful with the modes, because in asithmetic assign nodes only
955 * the right operand has the mode of the arithmetic alread */
956 ir_mode *mode = get_ir_mode(type);
957 left = create_conv(dbgi, left, mode);
958 ir_node *res = func(dbgi, left, right, mode);
963 static ir_node *create_arithmetic_assign_binop(
964 const binary_expression_t *expression, create_arithmetic_func func)
966 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
967 ir_node *value = create_arithmetic_binop(expression, func);
968 type_t *type = expression->expression.datatype;
969 ir_mode *mode = get_ir_mode(type);
971 assert(type->type != TYPE_POINTER);
973 value = create_conv(dbgi, value, mode);
974 set_value_for_expression(expression->left, value);
979 static ir_node *create_add(const binary_expression_t *expression)
981 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
982 ir_node *left = expression_to_firm(expression->left);
983 ir_node *right = expression_to_firm(expression->right);
984 type_t *type = expression->expression.datatype;
985 ir_mode *mode = get_ir_mode(type);
987 expression_t *expr_left = expression->left;
988 expression_t *expr_right = expression->right;
989 type_t *type_left = skip_typeref(expr_left->datatype);
990 type_t *type_right = skip_typeref(expr_right->datatype);
992 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
993 return new_d_Add(dbgi, left, right, mode);
998 pointer_type_t *pointer_type;
999 if(type_left->type == TYPE_POINTER) {
1002 pointer_type = (pointer_type_t*) type_left;
1004 assert(type_right->type == TYPE_POINTER);
1007 pointer_type = (pointer_type_t*) type_right;
1010 type_t *points_to = pointer_type->points_to;
1011 unsigned elem_size = get_type_size(points_to);
1013 assert(elem_size >= 1);
1015 integer = create_conv(dbgi, integer, mode_Is);
1016 ir_node *cnst = new_Const_long(mode_Is, (int) elem_size);
1017 ir_node *mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1021 ir_node *res = new_d_Add(dbgi, pointer, integer, mode);
1026 static ir_node *create_sub(const binary_expression_t *expression)
1028 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1029 ir_node *left = expression_to_firm(expression->left);
1030 ir_node *right = expression_to_firm(expression->right);
1031 type_t *type = expression->expression.datatype;
1032 ir_mode *mode = get_ir_mode(type);
1034 expression_t *expr_left = expression->left;
1035 expression_t *expr_right = expression->right;
1036 type_t *type_left = skip_typeref(expr_left->datatype);
1037 type_t *type_right = skip_typeref(expr_right->datatype);
1039 if((is_type_arithmetic(type_left) && is_type_arithmetic(type_right))
1040 || (type_left->type == TYPE_POINTER
1041 && type_right->type == TYPE_POINTER)) {
1042 return new_d_Sub(dbgi, left, right, mode);
1045 assert(type_right->type == TYPE_POINTER);
1046 ir_node *pointer = left;
1047 ir_node *integer = right;
1048 pointer_type_t *pointer_type = (pointer_type_t*) type_right;
1050 type_t *points_to = pointer_type->points_to;
1051 unsigned elem_size = get_type_size(points_to);
1053 assert(elem_size >= 1);
1055 ir_node *cnst = new_Const_long(mode_Iu, elem_size);
1056 ir_node *mul = new_d_Mul(dbgi, integer, cnst, mode_Iu);
1060 ir_node *res = new_d_Sub(dbgi, pointer, integer, mode);
1065 static ir_node *create_shift(const binary_expression_t *expression)
1067 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1068 ir_node *left = expression_to_firm(expression->left);
1069 ir_node *right = expression_to_firm(expression->right);
1070 type_t *type = expression->expression.datatype;
1071 ir_mode *mode = get_ir_mode(type);
1073 /* firm always wants the shift count to be unsigned */
1074 right = create_conv(dbgi, right, mode_Iu);
1078 switch(expression->type) {
1079 case BINEXPR_SHIFTLEFT:
1080 res = new_d_Shl(dbgi, left, right, mode);
1082 case BINEXPR_SHIFTRIGHT: {
1083 expression_t *expr_left = expression->left;
1084 type_t *type_left = skip_typeref(expr_left->datatype);
1086 if(is_type_signed(type_left)) {
1087 res = new_d_Shrs(dbgi, left, right, mode);
1089 res = new_d_Shr(dbgi, left, right, mode);
1094 panic("create shift op called for non-shift op");
1101 static ir_node *create_divmod(const binary_expression_t *expression)
1103 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1104 ir_node *left = expression_to_firm(expression->left);
1105 ir_node *right = expression_to_firm(expression->right);
1106 ir_node *pin = new_Pin(new_NoMem());
1107 type_t *type = expression->expression.datatype;
1108 ir_mode *mode = get_ir_mode(type);
1112 switch (expression->type) {
1114 case BINEXPR_DIV_ASSIGN:
1115 if(mode_is_float(mode)) {
1116 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1117 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1119 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1120 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1125 case BINEXPR_MOD_ASSIGN:
1126 assert(!mode_is_float(mode));
1127 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1128 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1131 default: panic("unexpected binary expression type in create_divmod()");
1137 static ir_node *create_arithmetic_assign_divmod(
1138 const binary_expression_t *expression)
1140 ir_node * value = create_divmod(expression);
1141 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1142 type_t *const type = expression->expression.datatype;
1143 ir_mode *const mode = get_ir_mode(type);
1145 assert(type->type != TYPE_POINTER);
1147 value = create_conv(dbgi, value, mode);
1148 set_value_for_expression(expression->left, value);
1154 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1156 binary_expression_type_t type = expression->type;
1159 case BINEXPR_NOTEQUAL:
1161 case BINEXPR_LESSEQUAL:
1162 case BINEXPR_GREATER:
1163 case BINEXPR_GREATEREQUAL: {
1164 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1165 ir_node *left = expression_to_firm(expression->left);
1166 ir_node *right = expression_to_firm(expression->right);
1167 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1168 long pnc = get_pnc(type);
1169 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1172 case BINEXPR_ASSIGN: {
1173 ir_node *right = expression_to_firm(expression->right);
1174 set_value_for_expression(expression->left, right);
1178 return create_add(expression);
1180 return create_sub(expression);
1182 return create_arithmetic_binop(expression, new_d_Mul);
1183 case BINEXPR_BITWISE_AND:
1184 return create_arithmetic_binop(expression, new_d_And);
1185 case BINEXPR_BITWISE_OR:
1186 return create_arithmetic_binop(expression, new_d_Or);
1187 case BINEXPR_BITWISE_XOR:
1188 return create_arithmetic_binop(expression, new_d_Eor);
1189 case BINEXPR_SHIFTLEFT:
1190 case BINEXPR_SHIFTRIGHT:
1191 return create_shift(expression);
1194 return create_divmod(expression);
1195 case BINEXPR_LOGICAL_AND:
1196 case BINEXPR_LOGICAL_OR:
1197 return create_lazy_op(expression);
1199 expression_to_firm(expression->left);
1200 return expression_to_firm(expression->right);
1201 case BINEXPR_ADD_ASSIGN:
1202 return create_arithmetic_assign_binop(expression, new_d_Add);
1203 case BINEXPR_SUB_ASSIGN:
1204 return create_arithmetic_assign_binop(expression, new_d_Sub);
1205 case BINEXPR_MUL_ASSIGN:
1206 return create_arithmetic_assign_binop(expression, new_d_Mul);
1207 case BINEXPR_DIV_ASSIGN:
1208 return create_arithmetic_assign_divmod(expression);
1209 case BINEXPR_BITWISE_AND_ASSIGN:
1210 return create_arithmetic_assign_binop(expression, new_d_And);
1211 case BINEXPR_BITWISE_OR_ASSIGN:
1212 return create_arithmetic_assign_binop(expression, new_d_Or);
1213 case BINEXPR_BITWISE_XOR_ASSIGN:
1214 return create_arithmetic_assign_binop(expression, new_d_Eor);
1215 case BINEXPR_SHIFTLEFT_ASSIGN:
1216 return create_arithmetic_assign_binop(expression, new_d_Shl);
1217 case BINEXPR_SHIFTRIGHT_ASSIGN:
1218 return create_arithmetic_assign_binop(expression, new_d_Shr);
1220 panic("TODO binexpr type");
1224 static ir_node *array_access_addr(const array_access_expression_t *expression)
1226 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1228 ir_node *base_addr = expression_to_firm(expression->array_ref);
1229 ir_node *offset = expression_to_firm(expression->index);
1230 offset = create_conv(dbgi, offset, mode_Iu);
1232 unsigned elem_size = get_type_size(expression->expression.datatype);
1233 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1234 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1236 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1241 static ir_node *array_access_to_firm(
1242 const array_access_expression_t *expression)
1244 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1245 ir_node *addr = array_access_addr(expression);
1246 type_t *type = expression->expression.datatype;
1248 return load_from_expression_addr(type, addr, dbgi);
1251 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1253 type_t *type = expression->type;
1255 type = expression->size_expression->datatype;
1256 assert(type != NULL);
1259 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1260 unsigned size = get_type_size(type);
1261 ir_node *size_node = new_Const_long(mode, size);
1266 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1268 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1270 ir_node *condition = expression_to_modeb(expression->condition);
1271 ir_node *cond = new_d_Cond(dbgi, condition);
1272 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1273 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1275 /* create the true block */
1276 ir_node *true_block = new_immBlock();
1277 add_immBlock_pred(true_block, true_proj);
1278 mature_immBlock(true_block);
1280 ir_node *true_val = expression_to_firm(expression->true_expression);
1281 ir_node *true_jmp = new_Jmp();
1283 /* create the false block */
1284 ir_node *false_block = new_immBlock();
1285 add_immBlock_pred(false_block, false_proj);
1286 mature_immBlock(false_block);
1288 ir_node *false_val = expression_to_firm(expression->false_expression);
1289 ir_node *false_jmp = new_Jmp();
1291 /* create the common block */
1292 ir_node *common_block = new_immBlock();
1293 add_immBlock_pred(common_block, true_jmp);
1294 add_immBlock_pred(common_block, false_jmp);
1295 mature_immBlock(common_block);
1297 ir_node *in[2] = { true_val, false_val };
1298 ir_mode *mode = get_irn_mode(true_val);
1299 assert(get_irn_mode(false_val) == mode);
1300 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1305 static ir_node *select_addr(const select_expression_t *expression)
1307 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1309 ir_node *compound_addr = expression_to_firm(expression->compound);
1311 declaration_t *entry = expression->compound_entry;
1312 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1313 ir_entity *entity = entry->v.entity;
1315 assert(entity != NULL);
1317 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1322 static ir_node *select_to_firm(const select_expression_t *expression)
1324 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1325 ir_node *addr = select_addr(expression);
1326 type_t *type = expression->expression.datatype;
1328 return load_from_expression_addr(type, addr, dbgi);
1331 static ir_node *dereference_addr(const unary_expression_t *const expression)
1333 assert(expression->type == UNEXPR_DEREFERENCE);
1334 return expression_to_firm(expression->value);
1337 static ir_node *expression_to_addr(const expression_t *expression)
1339 switch(expression->type) {
1340 case EXPR_REFERENCE:
1341 return reference_addr((const reference_expression_t*) expression);
1342 case EXPR_ARRAY_ACCESS:
1343 return array_access_addr((const array_access_expression_t*) expression);
1345 return select_addr((const select_expression_t*) expression);
1347 const unary_expression_t *const unary_expr =
1348 (const unary_expression_t*)expression;
1349 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1350 return dereference_addr(unary_expr);
1357 panic("trying to get address of non-lvalue");
1360 static ir_node *_expression_to_firm(const expression_t *expression)
1362 switch(expression->type) {
1364 return const_to_firm((const const_t*) expression);
1365 case EXPR_STRING_LITERAL:
1366 return string_literal_to_firm((const string_literal_t*) expression);
1367 case EXPR_REFERENCE:
1368 return reference_expression_to_firm(
1369 (const reference_expression_t*) expression);
1371 return call_expression_to_firm((const call_expression_t*) expression);
1373 return unary_expression_to_firm((const unary_expression_t*) expression);
1375 return binary_expression_to_firm(
1376 (const binary_expression_t*) expression);
1377 case EXPR_ARRAY_ACCESS:
1378 return array_access_to_firm(
1379 (const array_access_expression_t*) expression);
1381 return sizeof_to_firm((const sizeof_expression_t*) expression);
1382 case EXPR_CONDITIONAL:
1383 return conditional_to_firm((const conditional_expression_t*)expression);
1385 return select_to_firm((const select_expression_t*) expression);
1388 case EXPR_PRETTY_FUNCTION:
1390 case EXPR_STATEMENT:
1391 case EXPR_BUILTIN_SYMBOL:
1392 panic("unimplemented expression found");
1398 panic("invalid expression found");
1401 static ir_node *expression_to_firm(const expression_t *expression)
1403 ir_node *res = _expression_to_firm(expression);
1405 if(res != NULL && get_irn_mode(res) == mode_b) {
1406 ir_mode *mode = get_ir_mode(expression->datatype);
1407 res = create_conv(NULL, res, mode);
1413 static ir_node *expression_to_modeb(const expression_t *expression)
1415 ir_node *res = _expression_to_firm(expression);
1416 res = create_conv(NULL, res, mode_b);
1421 static void statement_to_firm(statement_t *statement);
1423 static void return_statement_to_firm(return_statement_t *statement)
1425 if(get_cur_block() == NULL)
1428 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1431 if(statement->return_value != NULL) {
1432 ir_node *retval = expression_to_firm(statement->return_value);
1436 ret = new_d_Return(dbgi, get_store(), 1, in);
1438 ret = new_d_Return(dbgi, get_store(), 0, NULL);
1440 ir_node *end_block = get_irg_end_block(current_ir_graph);
1441 add_immBlock_pred(end_block, ret);
1443 set_cur_block(NULL);
1446 static void compound_statement_to_firm(compound_statement_t *compound)
1448 statement_t *statement = compound->statements;
1449 for( ; statement != NULL; statement = statement->next) {
1450 //context2firm(&statement->context);
1451 statement_to_firm(statement);
1455 static void expression_statement_to_firm(expression_statement_t *statement)
1457 if(get_cur_block() == NULL)
1460 expression_to_firm(statement->expression);
1463 static void if_statement_to_firm(if_statement_t *statement)
1465 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1466 ir_node *condition = expression_to_modeb(statement->condition);
1468 /* make sure we have a mode_b condition */
1469 ir_node *cond = new_d_Cond(dbgi, condition);
1470 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1471 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1473 ir_node *fallthrough_block = new_immBlock();
1475 /* the true (blocks) */
1476 ir_node *true_block = new_immBlock();
1477 add_immBlock_pred(true_block, true_proj);
1478 mature_immBlock(true_block);
1480 statement_to_firm(statement->true_statement);
1481 if(get_cur_block() != NULL) {
1482 ir_node *jmp = new_Jmp();
1483 add_immBlock_pred(fallthrough_block, jmp);
1486 /* the false (blocks) */
1487 if(statement->false_statement != NULL) {
1488 ir_node *false_block = new_immBlock();
1489 add_immBlock_pred(false_block, false_proj);
1490 mature_immBlock(false_block);
1492 statement_to_firm(statement->false_statement);
1493 if(get_cur_block() != NULL) {
1494 ir_node *jmp = new_Jmp();
1495 add_immBlock_pred(fallthrough_block, jmp);
1498 add_immBlock_pred(fallthrough_block, false_proj);
1500 mature_immBlock(fallthrough_block);
1502 set_cur_block(fallthrough_block);
1505 static void while_statement_to_firm(while_statement_t *statement)
1507 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1509 ir_node *jmp = NULL;
1510 if(get_cur_block() != NULL) {
1514 /* create the header block */
1515 ir_node *header_block = new_immBlock();
1517 add_immBlock_pred(header_block, jmp);
1520 /* create the condition */
1521 ir_node *condition = expression_to_modeb(statement->condition);
1522 ir_node *cond = new_d_Cond(dbgi, condition);
1523 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1524 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1526 /* the false block */
1527 ir_node *false_block = new_immBlock();
1528 add_immBlock_pred(false_block, false_proj);
1531 ir_node *body_block = new_immBlock();
1532 add_immBlock_pred(body_block, true_proj);
1533 mature_immBlock(body_block);
1535 ir_node *old_continue_label = continue_label;
1536 ir_node *old_break_label = break_label;
1537 continue_label = header_block;
1538 break_label = false_block;
1540 statement_to_firm(statement->body);
1542 assert(continue_label == header_block);
1543 assert(break_label == false_block);
1544 continue_label = old_continue_label;
1545 break_label = old_break_label;
1547 if(get_cur_block() != NULL) {
1548 ir_node *jmp = new_Jmp();
1549 add_immBlock_pred(header_block, jmp);
1552 mature_immBlock(header_block);
1553 mature_immBlock(false_block);
1555 set_cur_block(false_block);
1558 static void do_while_statement_to_firm(do_while_statement_t *statement)
1560 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1562 ir_node *jmp = NULL;
1563 if(get_cur_block() != NULL) {
1567 /* create the header block */
1568 ir_node *header_block = new_immBlock();
1570 /* the false block */
1571 ir_node *false_block = new_immBlock();
1574 ir_node *body_block = new_immBlock();
1576 add_immBlock_pred(body_block, jmp);
1579 ir_node *old_continue_label = continue_label;
1580 ir_node *old_break_label = break_label;
1581 continue_label = header_block;
1582 break_label = false_block;
1584 statement_to_firm(statement->body);
1586 assert(continue_label == header_block);
1587 assert(break_label == false_block);
1588 continue_label = old_continue_label;
1589 break_label = old_break_label;
1591 if(get_cur_block() == NULL) {
1592 mature_immBlock(header_block);
1593 mature_immBlock(body_block);
1594 mature_immBlock(false_block);
1598 ir_node *body_jmp = new_Jmp();
1599 add_immBlock_pred(header_block, body_jmp);
1600 mature_immBlock(header_block);
1602 /* create the condition */
1603 set_cur_block(header_block);
1604 ir_node *condition = expression_to_modeb(statement->condition);
1605 ir_node *cond = new_d_Cond(dbgi, condition);
1606 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1607 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1609 add_immBlock_pred(body_block, true_proj);
1610 mature_immBlock(body_block);
1612 add_immBlock_pred(false_block, false_proj);
1613 mature_immBlock(false_block);
1615 set_cur_block(false_block);
1618 static void for_statement_to_firm(for_statement_t *statement)
1620 dbg_info *const dbgi = get_dbg_info(&statement->statement.source_position);
1622 ir_node *jmp = NULL;
1623 if (get_cur_block() != NULL) {
1624 if(statement->initialisation != NULL) {
1625 expression_to_firm(statement->initialisation);
1630 /* create the step block */
1631 ir_node *const step_block = new_immBlock();
1632 if (statement->step != NULL) {
1633 expression_to_firm(statement->step);
1635 ir_node *const step_jmp = new_Jmp();
1637 /* create the header block */
1638 ir_node *const header_block = new_immBlock();
1640 add_immBlock_pred(header_block, jmp);
1642 add_immBlock_pred(header_block, step_jmp);
1644 /* create the condition */
1646 ir_node *false_proj;
1647 if (statement->condition != NULL) {
1648 ir_node *const condition = expression_to_modeb(statement->condition);
1649 ir_node *const cond = new_d_Cond(dbgi, condition);
1650 true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1651 false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1653 keep_alive(header_block);
1654 true_proj = new_Jmp();
1658 /* the false block */
1659 ir_node *const false_block = new_immBlock();
1660 if (false_proj != NULL) {
1661 add_immBlock_pred(false_block, false_proj);
1665 ir_node *const body_block = new_immBlock();
1666 add_immBlock_pred(body_block, true_proj);
1667 mature_immBlock(body_block);
1669 ir_node *const old_continue_label = continue_label;
1670 ir_node *const old_break_label = break_label;
1671 continue_label = step_block;
1672 break_label = false_block;
1674 statement_to_firm(statement->body);
1676 assert(continue_label == step_block);
1677 assert(break_label == false_block);
1678 continue_label = old_continue_label;
1679 break_label = old_break_label;
1681 if (get_cur_block() != NULL) {
1682 ir_node *const jmp = new_Jmp();
1683 add_immBlock_pred(step_block, jmp);
1686 mature_immBlock(step_block);
1687 mature_immBlock(header_block);
1688 mature_immBlock(false_block);
1690 set_cur_block(false_block);
1693 static void create_declaration_entity(declaration_t *declaration,
1694 declaration_type_t declaration_type,
1695 ir_type *parent_type)
1697 ident *id = new_id_from_str(declaration->symbol->string);
1698 ir_type *irtype = get_ir_type(declaration->type);
1699 ir_entity *entity = new_entity(parent_type, id, irtype);
1700 set_entity_ld_ident(entity, id);
1702 declaration->declaration_type = declaration_type;
1703 declaration->v.entity = entity;
1704 set_entity_variability(entity, variability_uninitialized);
1705 /* TODO: visibility? */
1708 static void create_initializer(declaration_t *declaration)
1710 initializer_t *initializer = declaration->init.initializer;
1711 if(initializer == NULL)
1714 if(initializer->type == INITIALIZER_VALUE) {
1715 assert(initializer->designator == NULL);
1716 assert(initializer->next == NULL);
1717 ir_node *init_node = expression_to_firm(initializer->v.value);
1719 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1720 set_value(declaration->v.value_number, init_node);
1722 ir_entity *entity = declaration->v.entity;
1724 set_entity_variability(entity, variability_initialized);
1725 set_atomic_ent_value(entity, init_node);
1728 assert(initializer->type == INITIALIZER_LIST);
1729 panic("list initializer not supported yet");
1733 static void create_local_variable(declaration_t *declaration)
1735 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1737 bool needs_entity = declaration->address_taken;
1738 type_t *type = skip_typeref(declaration->type);
1740 if(type->type == TYPE_ARRAY
1741 || type->type == TYPE_COMPOUND_STRUCT
1742 || type->type == TYPE_COMPOUND_UNION) {
1743 needs_entity = true;
1747 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1748 create_declaration_entity(declaration,
1749 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1752 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1753 declaration->v.value_number = next_value_number_function;
1754 ++next_value_number_function;
1757 create_initializer(declaration);
1760 static void declaration_statement_to_firm(declaration_statement_t *statement)
1762 declaration_t *declaration = statement->declarations_begin;
1763 declaration_t *end = statement->declarations_end->next;
1764 for( ; declaration != end; declaration = declaration->next) {
1765 type_t *type = declaration->type;
1767 switch(declaration->storage_class) {
1768 case STORAGE_CLASS_TYPEDEF:
1770 case STORAGE_CLASS_STATIC:
1771 panic("static local vars not implemented yet");
1772 case STORAGE_CLASS_ENUM_ENTRY:
1773 panic("enum entry declaration in local block found");
1774 case STORAGE_CLASS_EXTERN:
1775 panic("extern declaration in local block found");
1776 case STORAGE_CLASS_NONE:
1777 case STORAGE_CLASS_AUTO:
1778 case STORAGE_CLASS_REGISTER:
1779 if(type->type == TYPE_FUNCTION) {
1780 panic("nested functions not supported yet");
1782 create_local_variable(declaration);
1786 panic("invalid storage class found");
1790 static void create_jump_statement(const statement_t *statement,
1791 ir_node *target_block)
1793 if(get_cur_block() == NULL)
1796 dbg_info *dbgi = get_dbg_info(&statement->source_position);
1797 ir_node *jump = new_d_Jmp(dbgi);
1798 add_immBlock_pred(target_block, jump);
1800 set_cur_block(NULL);
1803 static void switch_statement_to_firm(const switch_statement_t *statement)
1805 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1807 ir_node *expression = expression_to_firm(statement->expression);
1808 ir_node *cond = new_d_Cond(dbgi, expression);
1809 ir_node *break_block = new_immBlock();
1811 set_cur_block(NULL);
1813 ir_node *const old_switch_cond = current_switch_cond;
1814 ir_node *const old_break_label = break_label;
1815 const bool old_saw_default_label = saw_default_label;
1816 current_switch_cond = cond;
1817 break_label = break_block;
1819 statement_to_firm(statement->body);
1821 if(get_cur_block() != NULL) {
1822 ir_node *jmp = new_Jmp();
1823 add_immBlock_pred(break_block, jmp);
1826 if (!saw_default_label) {
1827 set_cur_block(get_nodes_block(cond));
1828 ir_node *const proj = new_d_defaultProj(dbgi, cond,
1829 MAGIC_DEFAULT_PN_NUMBER);
1830 add_immBlock_pred(break_block, proj);
1833 assert(current_switch_cond == cond);
1834 assert(break_label == break_block);
1835 current_switch_cond = old_switch_cond;
1836 break_label = old_break_label;
1837 saw_default_label = old_saw_default_label;
1839 mature_immBlock(break_block);
1840 set_cur_block(break_block);
1843 static long fold_constant(const expression_t *expression)
1845 ir_graph *old_current_ir_graph = current_ir_graph;
1846 current_ir_graph = get_const_code_irg();
1848 ir_node *cnst = expression_to_firm(expression);
1849 if(!is_Const(cnst)) {
1850 panic("couldn't fold constantl");
1852 tarval *tv = get_Const_tarval(cnst);
1853 if(!tarval_is_long(tv)) {
1854 panic("folded constant not an integer");
1857 long res = get_tarval_long(tv);
1859 current_ir_graph = old_current_ir_graph;
1863 static void case_label_to_firm(const case_label_statement_t *statement)
1865 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1867 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
1869 /* let's create a node and hope firm constant folding creates a Const
1872 set_cur_block(get_nodes_block(current_switch_cond));
1873 if(statement->expression) {
1874 long pn = fold_constant(statement->expression);
1875 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
1876 /* oops someone detected our cheating... */
1877 panic("magic default pn used");
1879 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
1881 saw_default_label = true;
1882 proj = new_d_defaultProj(dbgi, current_switch_cond,
1883 MAGIC_DEFAULT_PN_NUMBER);
1886 ir_node *block = new_immBlock();
1887 if (fallthrough != NULL) {
1888 add_immBlock_pred(block, fallthrough);
1890 add_immBlock_pred(block, proj);
1891 mature_immBlock(block);
1894 static ir_node *get_label_block(declaration_t *label)
1896 assert(label->namespace == NAMESPACE_LABEL);
1898 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
1899 return label->v.block;
1901 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
1903 ir_node *old_cur_block = get_cur_block();
1904 ir_node *block = new_immBlock();
1905 set_cur_block(old_cur_block);
1907 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
1908 label->v.block = block;
1910 ARR_APP1(ir_node *, imature_blocks, block);
1915 static void label_to_firm(const label_statement_t *statement)
1917 ir_node *block = get_label_block(statement->label);
1919 if(get_cur_block() != NULL) {
1920 ir_node *jmp = new_Jmp();
1921 add_immBlock_pred(block, jmp);
1924 set_cur_block(block);
1927 statement_to_firm(statement->label_statement);
1930 static void goto_to_firm(const goto_statement_t *statement)
1932 if(get_cur_block() == NULL)
1935 ir_node *block = get_label_block(statement->label);
1936 ir_node *jmp = new_Jmp();
1937 add_immBlock_pred(block, jmp);
1939 set_cur_block(NULL);
1942 static void statement_to_firm(statement_t *statement)
1944 switch(statement->type) {
1945 case STATEMENT_COMPOUND:
1946 compound_statement_to_firm((compound_statement_t*) statement);
1948 case STATEMENT_RETURN:
1949 return_statement_to_firm((return_statement_t*) statement);
1951 case STATEMENT_EXPRESSION:
1952 expression_statement_to_firm((expression_statement_t*) statement);
1955 if_statement_to_firm((if_statement_t*) statement);
1957 case STATEMENT_WHILE:
1958 while_statement_to_firm((while_statement_t*) statement);
1960 case STATEMENT_DO_WHILE:
1961 do_while_statement_to_firm((do_while_statement_t*) statement);
1963 case STATEMENT_DECLARATION:
1964 declaration_statement_to_firm((declaration_statement_t*) statement);
1966 case STATEMENT_BREAK:
1967 create_jump_statement(statement, break_label);
1969 case STATEMENT_CONTINUE:
1970 create_jump_statement(statement, continue_label);
1972 case STATEMENT_SWITCH:
1973 switch_statement_to_firm((switch_statement_t*) statement);
1975 case STATEMENT_CASE_LABEL:
1976 case_label_to_firm((case_label_statement_t*) statement);
1979 for_statement_to_firm((for_statement_t*) statement);
1981 case STATEMENT_LABEL:
1982 label_to_firm((label_statement_t*) statement);
1984 case STATEMENT_GOTO:
1985 goto_to_firm((goto_statement_t*) statement);
1990 panic("Statement not implemented\n");
1993 static int get_function_n_local_vars(declaration_t *declaration)
2000 static void initialize_function_parameters(declaration_t *declaration)
2002 ir_graph *irg = current_ir_graph;
2003 ir_node *args = get_irg_args(irg);
2004 ir_node *start_block = get_irg_start_block(irg);
2007 declaration_t *parameter = declaration->context.declarations;
2008 for( ; parameter != NULL; parameter = parameter->next) {
2009 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2010 type_t *type = parameter->type;
2012 bool needs_entity = parameter->address_taken;
2013 if(type->type == TYPE_COMPOUND_STRUCT
2014 || type->type == TYPE_COMPOUND_UNION) {
2015 needs_entity = true;
2019 panic("entities for function parameters not implemented yet");
2022 ir_mode *mode = get_ir_mode(parameter->type);
2024 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2027 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2028 parameter->v.value_number = next_value_number_function;
2029 ++next_value_number_function;
2031 set_value(parameter->v.value_number, proj);
2035 static void create_function(declaration_t *declaration)
2037 ir_entity *entity = get_function_entity(declaration);
2039 if(declaration->init.statement == NULL)
2042 assert(imature_blocks == NULL);
2043 imature_blocks = NEW_ARR_F(ir_node*, 0);
2045 int n_local_vars = get_function_n_local_vars(declaration);
2046 ir_graph *irg = new_ir_graph(entity, n_local_vars);
2047 ir_node *first_block = get_cur_block();
2049 next_value_number_function = 0;
2050 initialize_function_parameters(declaration);
2052 statement_to_firm(declaration->init.statement);
2054 ir_node *end_block = get_irg_end_block(irg);
2056 /* do we have a return statement yet? */
2057 if(get_cur_block() != NULL) {
2058 assert(declaration->type->type == TYPE_FUNCTION);
2059 const function_type_t* const func_type
2060 = (const function_type_t*) declaration->type;
2062 if (func_type->result_type == type_void) {
2063 ret = new_Return(get_store(), 0, NULL);
2065 ir_mode *const mode = get_ir_mode(func_type->result_type);
2067 // ยง5.1.2.2.3 main implicitly returns 0
2068 if (strcmp(declaration->symbol->string, "main") == 0) {
2069 in[0] = new_Const(mode, get_mode_null(mode));
2071 in[0] = new_Unknown(mode);
2073 ret = new_Return(get_store(), 1, in);
2075 add_immBlock_pred(end_block, ret);
2078 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2079 mature_immBlock(imature_blocks[i]);
2081 DEL_ARR_F(imature_blocks);
2082 imature_blocks = NULL;
2084 mature_immBlock(first_block);
2085 mature_immBlock(end_block);
2087 irg_finalize_cons(irg);
2089 /* finalize the frame type */
2090 ir_type *frame_type = get_irg_frame_type(irg);
2091 int n = get_compound_n_members(frame_type);
2094 for(int i = 0; i < n; ++i) {
2095 ir_entity *entity = get_compound_member(frame_type, i);
2096 ir_type *entity_type = get_entity_type(entity);
2098 int align = get_type_alignment_bytes(entity_type);
2099 if(align > align_all)
2103 misalign = offset % align;
2107 set_entity_offset(entity, offset);
2108 offset += get_type_size_bytes(entity_type);
2110 set_type_size_bytes(frame_type, offset);
2111 set_type_alignment_bytes(frame_type, align_all);
2112 set_type_state(frame_type, layout_fixed);
2117 static void create_global_variable(declaration_t *declaration)
2119 ir_type *global_type = get_glob_type();
2120 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2123 ir_entity *entity = declaration->v.entity;
2124 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2125 set_entity_visibility(entity, visibility_local);
2126 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2127 set_entity_visibility(entity, visibility_external_allocated);
2129 set_entity_visibility(entity, visibility_external_visible);
2131 current_ir_graph = get_const_code_irg();
2132 create_initializer(declaration);
2135 static void context_to_firm(context_t *context)
2137 declaration_t *declaration = context->declarations;
2138 for( ; declaration != NULL; declaration = declaration->next) {
2139 if(declaration->namespace != NAMESPACE_NORMAL)
2141 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2142 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2144 if(declaration->symbol == NULL)
2147 type_t *type = declaration->type;
2148 if(type->type == TYPE_FUNCTION) {
2149 create_function(declaration);
2151 create_global_variable(declaration);
2156 void translation_unit_to_firm(translation_unit_t *unit)
2158 /* remove me later TODO FIXME */
2159 (void) get_type_size;
2161 /* just to be sure */
2162 continue_label = NULL;
2164 current_switch_cond = NULL;
2166 context_to_firm(& unit->context);