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 type_t *type = declaration->type;
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 || type->qualifiers & TYPE_QUALIFIER_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);
626 ir_mode *mode = get_ir_mode(type);
628 switch((declaration_type_t) declaration->declaration_type) {
629 case DECLARATION_TYPE_UNKNOWN:
631 case DECLARATION_TYPE_LOCAL_VARIABLE:
632 return get_value(declaration->v.value_number, mode);
633 case DECLARATION_TYPE_FUNCTION: {
634 return create_symconst(dbgi, declaration->v.entity);
636 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
637 ir_entity *entity = declaration->v.entity;
638 ir_node *symconst = create_symconst(dbgi, entity);
640 if(type->type == TYPE_ARRAY) {
643 return load_from_expression_addr(type, symconst, dbgi);
646 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
647 case DECLARATION_TYPE_COMPOUND_MEMBER:
648 case DECLARATION_TYPE_LABEL_BLOCK:
649 panic("not implemented reference type");
652 panic("reference to declaration with unknown type found");
655 static ir_node *reference_addr(const reference_expression_t *ref)
657 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
658 declaration_t *declaration = ref->declaration;
660 switch((declaration_type_t) declaration->declaration_type) {
661 case DECLARATION_TYPE_UNKNOWN:
663 case DECLARATION_TYPE_LOCAL_VARIABLE:
664 panic("local variable without entity has no address");
665 case DECLARATION_TYPE_FUNCTION: {
666 return create_symconst(dbgi, declaration->v.entity);
668 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
669 ir_entity *entity = declaration->v.entity;
670 ir_node *symconst = create_symconst(dbgi, entity);
673 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
674 case DECLARATION_TYPE_COMPOUND_MEMBER:
675 case DECLARATION_TYPE_LABEL_BLOCK:
676 panic("not implemented reference type");
679 panic("reference to declaration with unknown type found");
682 static ir_node *call_expression_to_firm(const call_expression_t *call)
684 assert(get_cur_block() != NULL);
686 expression_t *function = call->function;
687 ir_node *callee = expression_to_firm(function);
689 assert(function->datatype->type == TYPE_FUNCTION);
690 function_type_t *function_type = (function_type_t*) function->datatype;
692 int n_parameters = 0;
693 call_argument_t *argument = call->arguments;
694 for( ; argument != NULL; argument = argument->next) {
698 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
699 ir_type *new_method_type = NULL;
700 if(function_type->variadic || function_type->unspecified_parameters) {
701 /* we need to construct a new method type matching the call
703 int n_res = get_method_n_ress(ir_method_type);
704 new_method_type = new_type_method(unique_ident("calltype"),
705 n_parameters, n_res);
706 set_method_calling_convention(new_method_type,
707 get_method_calling_convention(ir_method_type));
708 set_method_additional_properties(new_method_type,
709 get_method_additional_properties(ir_method_type));
711 for(int i = 0; i < n_res; ++i) {
712 set_method_res_type(new_method_type, i,
713 get_method_res_type(ir_method_type, i));
716 ir_node *in[n_parameters];
718 argument = call->arguments;
720 for( ; argument != NULL; argument = argument->next) {
721 expression_t *expression = argument->expression;
722 ir_node *arg_node = expression_to_firm(expression);
725 if(new_method_type != NULL) {
726 ir_type *irtype = get_ir_type(expression->datatype);
727 set_method_param_type(new_method_type, n, irtype);
732 assert(n == n_parameters);
734 if(new_method_type != NULL)
735 ir_method_type = new_method_type;
737 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
738 ir_node *store = get_store();
739 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
741 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
744 type_t *result_type = function_type->result_type;
745 ir_node *result = NULL;
746 if(result_type != type_void) {
747 ir_mode *mode = get_ir_mode(result_type);
748 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
749 result = new_d_Proj(dbgi, resproj, mode, 0);
755 static ir_node *expression_to_addr(const expression_t *expression);
757 static void set_value_for_expression(const expression_t *expression,
760 if(expression->type == EXPR_REFERENCE) {
761 reference_expression_t *ref = (reference_expression_t*) expression;
763 declaration_t *declaration = ref->declaration;
764 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
765 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
766 set_value(declaration->v.value_number, value);
771 dbg_info *dbgi = get_dbg_info(&expression->source_position);
772 ir_node *addr = expression_to_addr(expression);
773 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
774 ir_node *memory = get_store();
775 ir_node *store = new_d_Store(dbgi, memory, addr, value);
776 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
777 set_store(store_mem);
780 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
782 ir_mode *value_mode = get_irn_mode(value);
784 if(value_mode == dest_mode)
787 if(dest_mode == mode_b) {
788 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
789 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
790 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
794 return new_d_Conv(dbgi, value, dest_mode);
797 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
799 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
800 type_t *type = expression->expression.datatype;
801 ir_mode *mode = get_ir_mode(type);
803 if(expression->type == UNEXPR_TAKE_ADDRESS)
804 return expression_to_addr(expression->value);
806 const expression_t *value = expression->value;
807 ir_node *value_node = expression_to_firm(value);
809 switch(expression->type) {
811 return new_d_Minus(dbgi, value_node, mode);
814 case UNEXPR_BITWISE_NEGATE:
815 return new_d_Not(dbgi, value_node, mode);
817 if(get_irn_mode(value_node) != mode_b) {
818 value_node = create_conv(dbgi, value_node, mode_b);
820 value_node = new_d_Not(dbgi, value_node, mode_b);
822 value_node = create_conv(dbgi, value_node, mode);
825 case UNEXPR_DEREFERENCE:
826 return load_from_expression_addr(type, value_node, dbgi);
827 case UNEXPR_POSTFIX_INCREMENT: {
828 ir_node *one = new_Const(mode, get_mode_one(mode));
829 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
830 set_value_for_expression(value, new_value);
833 case UNEXPR_POSTFIX_DECREMENT: {
834 ir_node *one = new_Const(mode, get_mode_one(mode));
835 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
836 set_value_for_expression(value, new_value);
839 case UNEXPR_PREFIX_INCREMENT: {
840 ir_node *one = new_Const(mode, get_mode_one(mode));
841 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
842 set_value_for_expression(value, new_value);
845 case UNEXPR_PREFIX_DECREMENT: {
846 ir_node *one = new_Const(mode, get_mode_one(mode));
847 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
848 set_value_for_expression(value, new_value);
852 return create_conv(dbgi, value_node, mode);
854 case UNEXPR_TAKE_ADDRESS:
858 panic("invalid UNEXPR type found");
861 static long get_pnc(binary_expression_type_t type)
864 case BINEXPR_EQUAL: return pn_Cmp_Eq;
865 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
866 case BINEXPR_LESS: return pn_Cmp_Lt;
867 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
868 case BINEXPR_GREATER: return pn_Cmp_Gt;
869 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
873 panic("trying to get pn_Cmp from non-comparison binexpr type");
876 static ir_node *create_lazy_op(const binary_expression_t *expression)
878 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
880 bool is_or = (expression->type == BINEXPR_LOGICAL_OR);
881 assert(is_or || expression->type == BINEXPR_LOGICAL_AND);
883 ir_node *val1 = expression_to_modeb(expression->left);
884 ir_node *cond = new_d_Cond(dbgi, val1);
885 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
886 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
888 ir_node *fallthrough_block = new_immBlock();
891 ir_node *calc_val2_block = new_immBlock();
893 add_immBlock_pred(calc_val2_block, false_proj);
895 add_immBlock_pred(calc_val2_block, true_proj);
898 mature_immBlock(calc_val2_block);
900 ir_node *val2 = expression_to_modeb(expression->right);
901 if(get_cur_block() != NULL) {
902 ir_node *jmp = new_d_Jmp(dbgi);
903 add_immBlock_pred(fallthrough_block, jmp);
909 constb = new_d_Const(dbgi, mode_b, get_tarval_b_true());
910 add_immBlock_pred(fallthrough_block, true_proj);
912 constb = new_d_Const(dbgi, mode_b, get_tarval_b_false());
913 add_immBlock_pred(fallthrough_block, false_proj);
915 mature_immBlock(fallthrough_block);
917 set_cur_block(fallthrough_block);
919 ir_node *in[2] = { val2, constb };
920 ir_node *val = new_d_Phi(dbgi, 2, in, mode_b);
925 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
926 ir_node *right, ir_mode *mode);
928 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
929 create_arithmetic_func func)
931 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
932 ir_node *left = expression_to_firm(expression->left);
933 ir_node *right = expression_to_firm(expression->right);
934 type_t *type = expression->right->datatype;
935 /* be careful with the modes, because in asithmetic assign nodes only
936 * the right operand has the mode of the arithmetic alread */
937 ir_mode *mode = get_ir_mode(type);
938 left = create_conv(dbgi, left, mode);
939 ir_node *res = func(dbgi, left, right, mode);
944 static ir_node *create_arithmetic_assign_binop(
945 const binary_expression_t *expression, create_arithmetic_func func)
947 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
948 ir_node *value = create_arithmetic_binop(expression, func);
949 type_t *type = expression->expression.datatype;
950 ir_mode *mode = get_ir_mode(type);
952 assert(type->type != TYPE_POINTER);
954 value = create_conv(dbgi, value, mode);
955 set_value_for_expression(expression->left, value);
960 static ir_node *create_add(const binary_expression_t *expression)
962 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
963 ir_node *left = expression_to_firm(expression->left);
964 ir_node *right = expression_to_firm(expression->right);
965 type_t *type = expression->expression.datatype;
966 ir_mode *mode = get_ir_mode(type);
968 expression_t *expr_left = expression->left;
969 expression_t *expr_right = expression->right;
970 type_t *type_left = skip_typeref(expr_left->datatype);
971 type_t *type_right = skip_typeref(expr_right->datatype);
973 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
974 return new_d_Add(dbgi, left, right, mode);
979 pointer_type_t *pointer_type;
980 if(type_left->type == TYPE_POINTER) {
983 pointer_type = (pointer_type_t*) type_left;
985 assert(type_right->type == TYPE_POINTER);
988 pointer_type = (pointer_type_t*) type_right;
991 type_t *points_to = pointer_type->points_to;
992 unsigned elem_size = get_type_size(points_to);
994 assert(elem_size >= 1);
996 integer = create_conv(dbgi, integer, mode_Is);
997 ir_node *cnst = new_Const_long(mode_Is, (int) elem_size);
998 ir_node *mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1002 ir_node *res = new_d_Add(dbgi, pointer, integer, mode);
1007 static ir_node *create_sub(const binary_expression_t *expression)
1009 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1010 ir_node *left = expression_to_firm(expression->left);
1011 ir_node *right = expression_to_firm(expression->right);
1012 type_t *type = expression->expression.datatype;
1013 ir_mode *mode = get_ir_mode(type);
1015 expression_t *expr_left = expression->left;
1016 expression_t *expr_right = expression->right;
1017 type_t *type_left = skip_typeref(expr_left->datatype);
1018 type_t *type_right = skip_typeref(expr_right->datatype);
1020 if((is_type_arithmetic(type_left) && is_type_arithmetic(type_right))
1021 || (type_left->type == TYPE_POINTER
1022 && type_right->type == TYPE_POINTER)) {
1023 return new_d_Sub(dbgi, left, right, mode);
1026 assert(type_right->type == TYPE_POINTER);
1027 ir_node *pointer = left;
1028 ir_node *integer = right;
1029 pointer_type_t *pointer_type = (pointer_type_t*) type_right;
1031 type_t *points_to = pointer_type->points_to;
1032 unsigned elem_size = get_type_size(points_to);
1034 assert(elem_size >= 1);
1036 ir_node *cnst = new_Const_long(mode_Iu, elem_size);
1037 ir_node *mul = new_d_Mul(dbgi, integer, cnst, mode_Iu);
1041 ir_node *res = new_d_Sub(dbgi, pointer, integer, mode);
1046 static ir_node *create_divmod(const binary_expression_t *expression)
1048 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1049 ir_node *left = expression_to_firm(expression->left);
1050 ir_node *right = expression_to_firm(expression->right);
1051 ir_node *pin = new_Pin(new_NoMem());
1052 type_t *type = expression->expression.datatype;
1053 ir_mode *mode = get_ir_mode(type);
1057 if(expression->type == BINEXPR_DIV) {
1058 if(mode_is_float(mode)) {
1059 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1060 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1062 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1063 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1066 assert(expression->type == BINEXPR_MOD);
1067 assert(!mode_is_float(mode));
1068 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1069 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1077 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1079 binary_expression_type_t type = expression->type;
1082 case BINEXPR_NOTEQUAL:
1084 case BINEXPR_LESSEQUAL:
1085 case BINEXPR_GREATER:
1086 case BINEXPR_GREATEREQUAL: {
1087 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1088 ir_node *left = expression_to_firm(expression->left);
1089 ir_node *right = expression_to_firm(expression->right);
1090 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1091 long pnc = get_pnc(type);
1092 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1095 case BINEXPR_ASSIGN: {
1096 ir_node *right = expression_to_firm(expression->right);
1097 set_value_for_expression(expression->left, right);
1101 return create_add(expression);
1103 return create_sub(expression);
1105 return create_arithmetic_binop(expression, new_d_Mul);
1106 case BINEXPR_BITWISE_AND:
1107 return create_arithmetic_binop(expression, new_d_And);
1108 case BINEXPR_BITWISE_OR:
1109 return create_arithmetic_binop(expression, new_d_Or);
1110 case BINEXPR_BITWISE_XOR:
1111 return create_arithmetic_binop(expression, new_d_Eor);
1112 case BINEXPR_SHIFTLEFT:
1113 return create_arithmetic_binop(expression, new_d_Shl);
1114 case BINEXPR_SHIFTRIGHT:
1115 return create_arithmetic_binop(expression, new_d_Shr);
1118 return create_divmod(expression);
1119 case BINEXPR_LOGICAL_AND:
1120 case BINEXPR_LOGICAL_OR:
1121 return create_lazy_op(expression);
1123 expression_to_firm(expression->left);
1124 return expression_to_firm(expression->right);
1125 case BINEXPR_ADD_ASSIGN:
1126 return create_arithmetic_assign_binop(expression, new_d_Add);
1127 case BINEXPR_SUB_ASSIGN:
1128 return create_arithmetic_assign_binop(expression, new_d_Sub);
1129 case BINEXPR_MUL_ASSIGN:
1130 return create_arithmetic_assign_binop(expression, new_d_Mul);
1131 case BINEXPR_BITWISE_AND_ASSIGN:
1132 return create_arithmetic_assign_binop(expression, new_d_And);
1133 case BINEXPR_BITWISE_OR_ASSIGN:
1134 return create_arithmetic_assign_binop(expression, new_d_Or);
1135 case BINEXPR_BITWISE_XOR_ASSIGN:
1136 return create_arithmetic_assign_binop(expression, new_d_Eor);
1137 case BINEXPR_SHIFTLEFT_ASSIGN:
1138 return create_arithmetic_assign_binop(expression, new_d_Shl);
1139 case BINEXPR_SHIFTRIGHT_ASSIGN:
1140 return create_arithmetic_assign_binop(expression, new_d_Shr);
1142 panic("TODO binexpr type");
1146 static ir_node *array_access_addr(const array_access_expression_t *expression)
1148 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1150 ir_node *base_addr = expression_to_firm(expression->array_ref);
1151 ir_node *offset = expression_to_firm(expression->index);
1152 offset = create_conv(dbgi, offset, mode_Iu);
1154 unsigned elem_size = get_type_size(expression->expression.datatype);
1155 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1156 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1158 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1163 static ir_node *array_access_to_firm(
1164 const array_access_expression_t *expression)
1166 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1167 ir_node *addr = array_access_addr(expression);
1168 type_t *type = expression->expression.datatype;
1170 return load_from_expression_addr(type, addr, dbgi);
1173 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1175 type_t *type = expression->type;
1177 type = expression->size_expression->datatype;
1178 assert(type != NULL);
1181 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1182 unsigned size = get_type_size(type);
1183 ir_node *size_node = new_Const_long(mode, size);
1188 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1190 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1192 ir_node *condition = expression_to_modeb(expression->condition);
1193 ir_node *cond = new_d_Cond(dbgi, condition);
1194 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1195 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1197 /* create the true block */
1198 ir_node *true_block = new_immBlock();
1199 add_immBlock_pred(true_block, true_proj);
1200 mature_immBlock(true_block);
1202 ir_node *true_val = expression_to_firm(expression->true_expression);
1203 ir_node *true_jmp = new_Jmp();
1205 /* create the false block */
1206 ir_node *false_block = new_immBlock();
1207 add_immBlock_pred(false_block, false_proj);
1208 mature_immBlock(false_block);
1210 ir_node *false_val = expression_to_firm(expression->false_expression);
1211 ir_node *false_jmp = new_Jmp();
1213 /* create the common block */
1214 ir_node *common_block = new_immBlock();
1215 add_immBlock_pred(common_block, true_jmp);
1216 add_immBlock_pred(common_block, false_jmp);
1217 mature_immBlock(common_block);
1219 ir_node *in[2] = { true_val, false_val };
1220 ir_mode *mode = get_irn_mode(true_val);
1221 assert(get_irn_mode(false_val) == mode);
1222 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1227 static ir_node *expression_to_addr(const expression_t *expression)
1229 switch(expression->type) {
1230 case EXPR_REFERENCE:
1231 return reference_addr((const reference_expression_t*) expression);
1232 case EXPR_ARRAY_ACCESS:
1233 return array_access_addr((const array_access_expression_t*) expression);
1237 panic("trying to get address of non-lvalue");
1240 static ir_node *_expression_to_firm(const expression_t *expression)
1242 switch(expression->type) {
1244 return const_to_firm((const const_t*) expression);
1245 case EXPR_STRING_LITERAL:
1246 return string_literal_to_firm((const string_literal_t*) expression);
1247 case EXPR_REFERENCE:
1248 return reference_expression_to_firm(
1249 (const reference_expression_t*) expression);
1251 return call_expression_to_firm((const call_expression_t*) expression);
1253 return unary_expression_to_firm((const unary_expression_t*) expression);
1255 return binary_expression_to_firm(
1256 (const binary_expression_t*) expression);
1257 case EXPR_ARRAY_ACCESS:
1258 return array_access_to_firm(
1259 (const array_access_expression_t*) expression);
1261 return sizeof_to_firm((const sizeof_expression_t*) expression);
1262 case EXPR_CONDITIONAL:
1263 return conditional_to_firm((const conditional_expression_t*)expression);
1267 panic("unsupported expression found");
1270 static ir_node *expression_to_firm(const expression_t *expression)
1272 ir_node *res = _expression_to_firm(expression);
1274 if(expression->datatype == type_void)
1277 ir_mode *mode = get_ir_mode(expression->datatype);
1278 res = create_conv(NULL, res, mode);
1282 static ir_node *expression_to_modeb(const expression_t *expression)
1284 ir_node *res = _expression_to_firm(expression);
1285 res = create_conv(NULL, res, mode_b);
1290 static void statement_to_firm(statement_t *statement);
1292 static void return_statement_to_firm(return_statement_t *statement)
1294 if(get_cur_block() == NULL)
1297 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1300 if(statement->return_value != NULL) {
1301 ir_node *retval = expression_to_firm(statement->return_value);
1305 ret = new_d_Return(dbgi, get_store(), 1, in);
1307 ret = new_d_Return(dbgi, get_store(), 0, NULL);
1309 ir_node *end_block = get_irg_end_block(current_ir_graph);
1310 add_immBlock_pred(end_block, ret);
1312 set_cur_block(NULL);
1315 static void compound_statement_to_firm(compound_statement_t *compound)
1317 statement_t *statement = compound->statements;
1318 for( ; statement != NULL; statement = statement->next) {
1319 //context2firm(&statement->context);
1320 statement_to_firm(statement);
1324 static void expression_statement_to_firm(expression_statement_t *statement)
1326 if(get_cur_block() == NULL)
1329 expression_to_firm(statement->expression);
1332 static void if_statement_to_firm(if_statement_t *statement)
1334 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1335 ir_node *condition = expression_to_modeb(statement->condition);
1337 /* make sure we have a mode_b condition */
1338 ir_node *cond = new_d_Cond(dbgi, condition);
1339 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1340 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1342 ir_node *fallthrough_block = new_immBlock();
1344 /* the true (blocks) */
1345 ir_node *true_block = new_immBlock();
1346 add_immBlock_pred(true_block, true_proj);
1347 mature_immBlock(true_block);
1349 statement_to_firm(statement->true_statement);
1350 if(get_cur_block() != NULL) {
1351 ir_node *jmp = new_Jmp();
1352 add_immBlock_pred(fallthrough_block, jmp);
1355 /* the false (blocks) */
1356 if(statement->false_statement != NULL) {
1357 ir_node *false_block = new_immBlock();
1358 add_immBlock_pred(false_block, false_proj);
1359 mature_immBlock(false_block);
1361 statement_to_firm(statement->false_statement);
1362 if(get_cur_block() != NULL) {
1363 ir_node *jmp = new_Jmp();
1364 add_immBlock_pred(fallthrough_block, jmp);
1367 add_immBlock_pred(fallthrough_block, false_proj);
1369 mature_immBlock(fallthrough_block);
1371 set_cur_block(fallthrough_block);
1374 static void while_statement_to_firm(while_statement_t *statement)
1376 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1378 ir_node *jmp = NULL;
1379 if(get_cur_block() != NULL) {
1383 /* create the header block */
1384 ir_node *header_block = new_immBlock();
1386 add_immBlock_pred(header_block, jmp);
1389 /* create the condition */
1390 ir_node *condition = expression_to_modeb(statement->condition);
1391 ir_node *cond = new_d_Cond(dbgi, condition);
1392 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1393 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1395 /* the false block */
1396 ir_node *false_block = new_immBlock();
1397 add_immBlock_pred(false_block, false_proj);
1400 ir_node *body_block = new_immBlock();
1401 add_immBlock_pred(body_block, true_proj);
1402 mature_immBlock(body_block);
1404 ir_node *old_continue_label = continue_label;
1405 ir_node *old_break_label = break_label;
1406 continue_label = header_block;
1407 break_label = false_block;
1409 statement_to_firm(statement->body);
1411 assert(continue_label == header_block);
1412 assert(break_label == false_block);
1413 continue_label = old_continue_label;
1414 break_label = old_break_label;
1416 if(get_cur_block() != NULL) {
1417 ir_node *jmp = new_Jmp();
1418 add_immBlock_pred(header_block, jmp);
1421 mature_immBlock(header_block);
1422 mature_immBlock(false_block);
1424 set_cur_block(false_block);
1427 static void do_while_statement_to_firm(do_while_statement_t *statement)
1429 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1431 ir_node *jmp = NULL;
1432 if(get_cur_block() != NULL) {
1436 /* create the header block */
1437 ir_node *header_block = new_immBlock();
1439 /* the false block */
1440 ir_node *false_block = new_immBlock();
1443 ir_node *body_block = new_immBlock();
1445 add_immBlock_pred(body_block, jmp);
1448 ir_node *old_continue_label = continue_label;
1449 ir_node *old_break_label = break_label;
1450 continue_label = header_block;
1451 break_label = false_block;
1453 statement_to_firm(statement->body);
1455 assert(continue_label == header_block);
1456 assert(break_label == false_block);
1457 continue_label = old_continue_label;
1458 break_label = old_break_label;
1460 if(get_cur_block() == NULL) {
1461 mature_immBlock(header_block);
1462 mature_immBlock(body_block);
1466 ir_node *body_jmp = new_Jmp();
1467 add_immBlock_pred(header_block, body_jmp);
1468 mature_immBlock(header_block);
1470 /* create the condition */
1471 ir_node *condition = expression_to_modeb(statement->condition);
1472 ir_node *cond = new_d_Cond(dbgi, condition);
1473 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1474 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1476 add_immBlock_pred(body_block, true_proj);
1477 mature_immBlock(body_block);
1479 add_immBlock_pred(false_block, false_proj);
1480 mature_immBlock(false_block);
1482 set_cur_block(false_block);
1485 static void for_statement_to_firm(for_statement_t *statement)
1487 dbg_info *const dbgi = get_dbg_info(&statement->statement.source_position);
1489 ir_node *jmp = NULL;
1490 if (get_cur_block() != NULL) {
1491 if(statement->initialisation != NULL) {
1492 expression_to_firm(statement->initialisation);
1497 /* create the step block */
1498 ir_node *const step_block = new_immBlock();
1499 if (statement->step != NULL) {
1500 expression_to_firm(statement->step);
1502 ir_node *const step_jmp = new_Jmp();
1504 /* create the header block */
1505 ir_node *const header_block = new_immBlock();
1507 add_immBlock_pred(header_block, jmp);
1509 add_immBlock_pred(header_block, step_jmp);
1511 /* create the condition */
1513 ir_node *false_proj;
1514 if (statement->condition != NULL) {
1515 ir_node *const condition = expression_to_modeb(statement->condition);
1516 ir_node *const cond = new_d_Cond(dbgi, condition);
1517 true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1518 false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1520 keep_alive(header_block);
1521 true_proj = new_Jmp();
1525 /* the false block */
1526 ir_node *const false_block = new_immBlock();
1527 if (false_proj != NULL) {
1528 add_immBlock_pred(false_block, false_proj);
1532 ir_node *const body_block = new_immBlock();
1533 add_immBlock_pred(body_block, true_proj);
1534 mature_immBlock(body_block);
1536 ir_node *const old_continue_label = continue_label;
1537 ir_node *const old_break_label = break_label;
1538 continue_label = step_block;
1539 break_label = false_block;
1541 statement_to_firm(statement->body);
1543 assert(continue_label == step_block);
1544 assert(break_label == false_block);
1545 continue_label = old_continue_label;
1546 break_label = old_break_label;
1548 if (get_cur_block() != NULL) {
1549 ir_node *const jmp = new_Jmp();
1550 add_immBlock_pred(step_block, jmp);
1553 mature_immBlock(step_block);
1554 mature_immBlock(header_block);
1555 mature_immBlock(false_block);
1557 set_cur_block(false_block);
1560 static void create_declaration_entity(declaration_t *declaration,
1561 declaration_type_t declaration_type,
1562 ir_type *parent_type)
1564 ident *id = new_id_from_str(declaration->symbol->string);
1565 ir_type *irtype = get_ir_type(declaration->type);
1566 ir_entity *entity = new_entity(parent_type, id, irtype);
1567 set_entity_ld_ident(entity, id);
1569 declaration->declaration_type = declaration_type;
1570 declaration->v.entity = entity;
1571 set_entity_variability(entity, variability_uninitialized);
1572 /* TODO: visibility? */
1575 static void create_initializer(declaration_t *declaration)
1577 initializer_t *initializer = declaration->init.initializer;
1578 if(initializer == NULL)
1581 if(initializer->type == INITIALIZER_VALUE) {
1582 assert(initializer->designator == NULL);
1583 assert(initializer->next == NULL);
1584 ir_node *init_node = expression_to_firm(initializer->v.value);
1586 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1587 set_value(declaration->v.value_number, init_node);
1589 ir_entity *entity = declaration->v.entity;
1591 set_entity_variability(entity, variability_initialized);
1592 set_atomic_ent_value(entity, init_node);
1595 assert(initializer->type == INITIALIZER_LIST);
1596 panic("list initializer not supported yet");
1600 static void create_local_variable(declaration_t *declaration)
1602 bool needs_entity = declaration->address_taken;
1604 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1607 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1608 create_declaration_entity(declaration,
1609 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1612 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1613 declaration->v.value_number = next_value_number_function;
1614 ++next_value_number_function;
1617 create_initializer(declaration);
1620 static void declaration_statement_to_firm(declaration_statement_t *statement)
1622 declaration_t *declaration = statement->declarations_begin;
1623 declaration_t *end = statement->declarations_end->next;
1624 for( ; declaration != end; declaration = declaration->next) {
1625 type_t *type = declaration->type;
1627 switch(declaration->storage_class) {
1628 case STORAGE_CLASS_TYPEDEF:
1630 case STORAGE_CLASS_STATIC:
1631 panic("static local vars not implemented yet");
1632 case STORAGE_CLASS_ENUM_ENTRY:
1633 panic("enum entry declaration in local block found");
1634 case STORAGE_CLASS_EXTERN:
1635 panic("extern declaration in local block found");
1636 case STORAGE_CLASS_NONE:
1637 case STORAGE_CLASS_AUTO:
1638 case STORAGE_CLASS_REGISTER:
1639 if(type->type == TYPE_FUNCTION) {
1640 panic("nested functions not supported yet");
1642 create_local_variable(declaration);
1646 panic("invalid storage class found");
1650 static void create_jump_statement(const statement_t *statement,
1651 ir_node *target_block)
1653 dbg_info *dbgi = get_dbg_info(&statement->source_position);
1654 ir_node *jump = new_d_Jmp(dbgi);
1655 add_immBlock_pred(target_block, jump);
1657 set_cur_block(NULL);
1660 static void switch_statement_to_firm(const switch_statement_t *statement)
1662 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1664 ir_node *expression = expression_to_firm(statement->expression);
1665 ir_node *cond = new_d_Cond(dbgi, expression);
1666 ir_node *break_block = new_immBlock();
1668 set_cur_block(NULL);
1670 ir_node *old_switch_cond = current_switch_cond;
1671 ir_node *old_break_label = break_label;
1672 current_switch_cond = cond;
1673 break_label = break_block;
1675 statement_to_firm(statement->body);
1677 if(get_cur_block() != NULL) {
1678 ir_node *jmp = new_Jmp();
1679 add_immBlock_pred(break_block, jmp);
1682 assert(current_switch_cond == cond);
1683 assert(break_label == break_block);
1684 current_switch_cond = old_switch_cond;
1685 break_label = old_break_label;
1687 mature_immBlock(break_block);
1688 set_cur_block(break_block);
1691 static long fold_constant(const expression_t *expression)
1693 ir_graph *old_current_ir_graph = current_ir_graph;
1694 current_ir_graph = get_const_code_irg();
1696 ir_node *cnst = expression_to_firm(expression);
1697 if(!is_Const(cnst)) {
1698 panic("couldn't fold constantl");
1700 tarval *tv = get_Const_tarval(cnst);
1701 if(!tarval_is_long(tv)) {
1702 panic("folded constant not an integer");
1705 long res = get_tarval_long(tv);
1707 current_ir_graph = old_current_ir_graph;
1711 static void case_label_to_firm(const case_label_statement_t *statement)
1713 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1715 /* let's create a node and hope firm constant folding creates a Const
1718 set_cur_block(get_nodes_block(current_switch_cond));
1719 if(statement->expression) {
1720 long pn = fold_constant(statement->expression);
1721 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
1722 /* oops someone detected our cheating... */
1723 panic("magic default pn used");
1725 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
1727 proj = new_d_defaultProj(dbgi, current_switch_cond,
1728 MAGIC_DEFAULT_PN_NUMBER);
1731 ir_node *block = new_immBlock();
1732 add_immBlock_pred(block, proj);
1733 mature_immBlock(block);
1736 static ir_node *get_label_block(declaration_t *label)
1738 assert(label->namespace == NAMESPACE_LABEL);
1740 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
1741 return label->v.block;
1743 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
1745 ir_node *old_cur_block = get_cur_block();
1746 ir_node *block = new_immBlock();
1747 set_cur_block(old_cur_block);
1749 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
1750 label->v.block = block;
1752 ARR_APP1(imature_blocks, block);
1757 static void label_to_firm(const label_statement_t *statement)
1759 ir_node *block = get_label_block(statement->label);
1761 if(get_cur_block() != NULL) {
1762 ir_node *jmp = new_Jmp();
1763 add_immBlock_pred(block, jmp);
1766 set_cur_block(block);
1769 statement_to_firm(statement->label_statement);
1772 static void goto_to_firm(const goto_statement_t *statement)
1774 if(get_cur_block() == NULL)
1777 ir_node *block = get_label_block(statement->label);
1778 ir_node *jmp = new_Jmp();
1779 add_immBlock_pred(block, jmp);
1781 set_cur_block(NULL);
1784 static void statement_to_firm(statement_t *statement)
1786 switch(statement->type) {
1787 case STATEMENT_COMPOUND:
1788 compound_statement_to_firm((compound_statement_t*) statement);
1790 case STATEMENT_RETURN:
1791 return_statement_to_firm((return_statement_t*) statement);
1793 case STATEMENT_EXPRESSION:
1794 expression_statement_to_firm((expression_statement_t*) statement);
1797 if_statement_to_firm((if_statement_t*) statement);
1799 case STATEMENT_WHILE:
1800 while_statement_to_firm((while_statement_t*) statement);
1802 case STATEMENT_DO_WHILE:
1803 do_while_statement_to_firm((do_while_statement_t*) statement);
1805 case STATEMENT_DECLARATION:
1806 declaration_statement_to_firm((declaration_statement_t*) statement);
1808 case STATEMENT_BREAK:
1809 create_jump_statement(statement, break_label);
1811 case STATEMENT_CONTINUE:
1812 create_jump_statement(statement, continue_label);
1814 case STATEMENT_SWITCH:
1815 switch_statement_to_firm((switch_statement_t*) statement);
1817 case STATEMENT_CASE_LABEL:
1818 case_label_to_firm((case_label_statement_t*) statement);
1821 for_statement_to_firm((for_statement_t*) statement);
1823 case STATEMENT_LABEL:
1824 label_to_firm((label_statement_t*) statement);
1826 case STATEMENT_GOTO:
1827 goto_to_firm((goto_statement_t*) statement);
1832 panic("Statement not implemented\n");
1835 static int get_function_n_local_vars(declaration_t *declaration)
1842 static void initialize_function_parameters(declaration_t *declaration)
1844 ir_graph *irg = current_ir_graph;
1845 ir_node *args = get_irg_args(irg);
1846 ir_node *start_block = get_irg_start_block(irg);
1849 declaration_t *parameter = declaration->context.declarations;
1850 for( ; parameter != NULL; parameter = parameter->next) {
1851 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
1853 if(parameter->address_taken) {
1854 panic("address take from parameter not implemented yet");
1857 ir_mode *mode = get_ir_mode(parameter->type);
1859 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
1862 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1863 parameter->v.value_number = next_value_number_function;
1864 ++next_value_number_function;
1866 set_value(parameter->v.value_number, proj);
1870 static void create_function(declaration_t *declaration)
1872 ir_entity *entity = get_function_entity(declaration);
1874 if(declaration->init.statement == NULL)
1877 assert(imature_blocks == NULL);
1878 imature_blocks = NEW_ARR_F(ir_node*, 0);
1880 int n_local_vars = get_function_n_local_vars(declaration);
1881 ir_graph *irg = new_ir_graph(entity, n_local_vars);
1882 ir_node *first_block = get_cur_block();
1884 next_value_number_function = 0;
1885 initialize_function_parameters(declaration);
1887 statement_to_firm(declaration->init.statement);
1889 ir_node *end_block = get_irg_end_block(irg);
1891 /* do we have a return statement yet? */
1892 if(get_cur_block() != NULL) {
1893 assert(declaration->type->type == TYPE_FUNCTION);
1894 const function_type_t* const func_type
1895 = (const function_type_t*) declaration->type;
1897 if (func_type->result_type == type_void) {
1898 ret = new_Return(get_store(), 0, NULL);
1900 ir_mode *const mode = get_ir_mode(func_type->result_type);
1902 // ยง5.1.2.2.3 main implicitly returns 0
1903 if (strcmp(declaration->symbol->string, "main") == 0) {
1904 in[0] = new_Const(mode, get_mode_null(mode));
1906 in[0] = new_Unknown(mode);
1908 ret = new_Return(get_store(), 1, in);
1910 add_immBlock_pred(end_block, ret);
1913 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
1914 mature_immBlock(imature_blocks[i]);
1916 DEL_ARR_F(imature_blocks);
1917 imature_blocks = NULL;
1919 mature_immBlock(first_block);
1920 mature_immBlock(end_block);
1922 irg_finalize_cons(irg);
1924 /* finalize the frame type */
1925 ir_type *frame_type = get_irg_frame_type(irg);
1926 int n = get_compound_n_members(frame_type);
1929 for(int i = 0; i < n; ++i) {
1930 ir_entity *entity = get_compound_member(frame_type, i);
1931 ir_type *entity_type = get_entity_type(entity);
1933 int align = get_type_alignment_bytes(entity_type);
1934 if(align > align_all)
1938 misalign = offset % align;
1942 set_entity_offset(entity, offset);
1943 offset += get_type_size_bytes(entity_type);
1945 set_type_size_bytes(frame_type, offset);
1946 set_type_alignment_bytes(frame_type, align_all);
1947 set_type_state(frame_type, layout_fixed);
1952 static void create_global_variable(declaration_t *declaration)
1954 ir_type *global_type = get_glob_type();
1955 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
1958 ir_entity *entity = declaration->v.entity;
1959 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
1960 set_entity_visibility(entity, visibility_local);
1961 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
1962 set_entity_visibility(entity, visibility_external_allocated);
1964 set_entity_visibility(entity, visibility_external_visible);
1966 current_ir_graph = get_const_code_irg();
1967 create_initializer(declaration);
1970 static void context_to_firm(context_t *context)
1972 declaration_t *declaration = context->declarations;
1973 for( ; declaration != NULL; declaration = declaration->next) {
1974 if(declaration->namespace != NAMESPACE_NORMAL)
1976 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
1977 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
1979 if(declaration->symbol == NULL)
1982 type_t *type = declaration->type;
1983 if(type->type == TYPE_FUNCTION) {
1984 create_function(declaration);
1986 create_global_variable(declaration);
1991 void translation_unit_to_firm(translation_unit_t *unit)
1993 /* remove me later TODO FIXME */
1994 (void) get_type_size;
1996 /* just to be sure */
1997 continue_label = NULL;
1999 current_switch_cond = NULL;
2001 context_to_firm(& unit->context);