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);
544 static dbg_info *get_dbg_info(const source_position_t *pos)
546 return (dbg_info*) pos;
549 static ir_node *const_to_firm(const const_t *cnst)
551 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
552 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
555 if(mode_is_float(mode)) {
556 tv = new_tarval_from_double(cnst->v.float_value, mode);
558 tv = new_tarval_from_long(cnst->v.int_value, mode);
561 return new_d_Const(dbgi, mode, tv);
564 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
566 assert(entity != NULL);
567 union symconst_symbol sym;
568 sym.entity_p = entity;
569 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
572 static ir_node *string_literal_to_firm(const string_literal_t* literal)
574 ir_type *global_type = get_glob_type();
575 ir_type *type = new_type_array(unique_ident("strtype"), 1,
578 ident *id = unique_ident("Lstr");
579 ir_entity *entity = new_entity(global_type, id, type);
580 set_entity_ld_ident(entity, id);
581 set_entity_variability(entity, variability_constant);
583 ir_type *elem_type = ir_type_const_char;
584 ir_mode *mode = get_type_mode(elem_type);
586 const char *string = literal->value;
587 size_t slen = strlen(string) + 1;
589 set_array_lower_bound_int(type, 0, 0);
590 set_array_upper_bound_int(type, 0, slen);
591 set_type_size_bytes(type, slen);
592 set_type_state(type, layout_fixed);
594 tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
595 for(size_t i = 0; i < slen; ++i) {
596 tvs[i] = new_tarval_from_long(string[i], mode);
599 set_array_entity_values(entity, tvs, slen);
602 dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
604 return create_symconst(dbgi, entity);
607 static ir_node *deref_address(type_t *const type, ir_node *const addr,
608 dbg_info *const dbgi)
610 switch (type->type) {
612 case TYPE_COMPOUND_STRUCT:
613 case TYPE_COMPOUND_UNION:
617 ir_mode *const mode = get_ir_mode(type);
618 ir_node *const memory = get_store();
619 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
620 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
621 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
628 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
630 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
631 declaration_t *declaration = ref->declaration;
632 type_t *type = skip_typeref(declaration->type);
634 switch((declaration_type_t) declaration->declaration_type) {
635 case DECLARATION_TYPE_UNKNOWN:
637 case DECLARATION_TYPE_LOCAL_VARIABLE: {
638 ir_mode *mode = get_ir_mode(type);
639 return get_value(declaration->v.value_number, mode);
641 case DECLARATION_TYPE_FUNCTION: {
642 return create_symconst(dbgi, declaration->v.entity);
644 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
645 ir_entity *entity = declaration->v.entity;
646 ir_node *symconst = create_symconst(dbgi, entity);
647 return deref_address(type, symconst, dbgi);
649 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
650 ir_entity *entity = declaration->v.entity;
651 ir_node *frame = get_irg_frame(current_ir_graph);
652 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
653 return deref_address(type, sel, dbgi);
656 case DECLARATION_TYPE_COMPOUND_MEMBER:
657 case DECLARATION_TYPE_LABEL_BLOCK:
658 panic("not implemented reference type");
661 panic("reference to declaration with unknown type found");
664 static ir_node *reference_addr(const reference_expression_t *ref)
666 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
667 declaration_t *declaration = ref->declaration;
669 switch((declaration_type_t) declaration->declaration_type) {
670 case DECLARATION_TYPE_UNKNOWN:
672 case DECLARATION_TYPE_LOCAL_VARIABLE:
673 panic("local variable without entity has no address");
674 case DECLARATION_TYPE_FUNCTION: {
675 return create_symconst(dbgi, declaration->v.entity);
677 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
678 ir_entity *entity = declaration->v.entity;
679 ir_node *symconst = create_symconst(dbgi, entity);
682 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
683 ir_entity *entity = declaration->v.entity;
684 ir_node *frame = get_irg_frame(current_ir_graph);
685 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
689 case DECLARATION_TYPE_COMPOUND_MEMBER:
690 case DECLARATION_TYPE_LABEL_BLOCK:
691 panic("not implemented reference type");
694 panic("reference to declaration with unknown type found");
697 static ir_node *call_expression_to_firm(const call_expression_t *call)
699 assert(get_cur_block() != NULL);
701 expression_t *function = call->function;
702 ir_node *callee = expression_to_firm(function);
704 assert(function->datatype->type == TYPE_FUNCTION);
705 function_type_t *function_type = (function_type_t*) function->datatype;
707 int n_parameters = 0;
708 call_argument_t *argument = call->arguments;
709 for( ; argument != NULL; argument = argument->next) {
713 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
714 ir_type *new_method_type = NULL;
715 if(function_type->variadic || function_type->unspecified_parameters) {
716 /* we need to construct a new method type matching the call
718 int n_res = get_method_n_ress(ir_method_type);
719 new_method_type = new_type_method(unique_ident("calltype"),
720 n_parameters, n_res);
721 set_method_calling_convention(new_method_type,
722 get_method_calling_convention(ir_method_type));
723 set_method_additional_properties(new_method_type,
724 get_method_additional_properties(ir_method_type));
726 for(int i = 0; i < n_res; ++i) {
727 set_method_res_type(new_method_type, i,
728 get_method_res_type(ir_method_type, i));
731 ir_node *in[n_parameters];
733 argument = call->arguments;
735 for( ; argument != NULL; argument = argument->next) {
736 expression_t *expression = argument->expression;
737 ir_node *arg_node = expression_to_firm(expression);
740 if(new_method_type != NULL) {
741 ir_type *irtype = get_ir_type(expression->datatype);
742 set_method_param_type(new_method_type, n, irtype);
747 assert(n == n_parameters);
749 if(new_method_type != NULL)
750 ir_method_type = new_method_type;
752 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
753 ir_node *store = get_store();
754 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
756 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
759 type_t *result_type = function_type->result_type;
760 ir_node *result = NULL;
761 if(result_type != type_void) {
762 ir_mode *mode = get_ir_mode(result_type);
763 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
764 result = new_d_Proj(dbgi, resproj, mode, 0);
770 static ir_node *expression_to_addr(const expression_t *expression);
771 static void create_condition_evaluation(const expression_t *expression,
773 ir_node *false_block);
775 static void set_value_for_expression(const expression_t *expression,
778 if(expression->type == EXPR_REFERENCE) {
779 reference_expression_t *ref = (reference_expression_t*) expression;
781 declaration_t *declaration = ref->declaration;
782 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
783 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
784 set_value(declaration->v.value_number, value);
789 dbg_info *dbgi = get_dbg_info(&expression->source_position);
790 ir_node *addr = expression_to_addr(expression);
791 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
792 ir_node *memory = get_store();
793 ir_node *store = new_d_Store(dbgi, memory, addr, value);
794 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
795 set_store(store_mem);
798 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
800 ir_mode *value_mode = get_irn_mode(value);
802 if(value_mode == dest_mode)
805 if(dest_mode == mode_b) {
806 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
807 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
808 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
812 return new_d_Conv(dbgi, value, dest_mode);
815 static ir_node *create_incdec(const unary_expression_t *expression)
817 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
818 type_t *type = expression->expression.datatype;
819 ir_mode *mode = get_ir_mode(type);
820 expression_t *value = expression->value;
822 ir_node *value_node = expression_to_firm(value);
825 if(type->type == TYPE_POINTER) {
826 pointer_type_t *pointer_type = (pointer_type_t*) type;
827 unsigned elem_size = get_type_size(pointer_type->points_to);
828 offset = new_Const_long(mode_Is, elem_size);
830 assert(is_type_arithmetic(type));
831 offset = new_Const(mode, get_mode_one(mode));
835 switch(expression->type) {
836 case UNEXPR_POSTFIX_INCREMENT: {
837 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
838 set_value_for_expression(value, new_value);
841 case UNEXPR_POSTFIX_DECREMENT: {
842 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
843 set_value_for_expression(value, new_value);
846 case UNEXPR_PREFIX_INCREMENT: {
847 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
848 set_value_for_expression(value, new_value);
851 case UNEXPR_PREFIX_DECREMENT: {
852 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
853 set_value_for_expression(value, new_value);
857 panic("no incdec expr in create_incdec");
863 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
865 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
866 type_t *type = expression->expression.datatype;
868 if(expression->type == UNEXPR_TAKE_ADDRESS)
869 return expression_to_addr(expression->value);
871 const expression_t *value = expression->value;
872 ir_node *value_node = expression_to_firm(value);
874 switch(expression->type) {
876 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
879 case UNEXPR_BITWISE_NEGATE:
880 return new_d_Not(dbgi, value_node, get_ir_mode(type));
882 if(get_irn_mode(value_node) != mode_b) {
883 value_node = create_conv(dbgi, value_node, mode_b);
885 value_node = new_d_Not(dbgi, value_node, mode_b);
886 ir_mode *const mode = get_ir_mode(type);
888 value_node = create_conv(dbgi, value_node, mode);
892 case UNEXPR_DEREFERENCE:
893 return deref_address(type, value_node, dbgi);
894 case UNEXPR_POSTFIX_INCREMENT:
895 case UNEXPR_POSTFIX_DECREMENT:
896 case UNEXPR_PREFIX_INCREMENT:
897 case UNEXPR_PREFIX_DECREMENT:
898 return create_incdec(expression);
900 return create_conv(dbgi, value_node, get_ir_mode(type));
902 case UNEXPR_TAKE_ADDRESS:
906 panic("invalid UNEXPR type found");
909 static long get_pnc(binary_expression_type_t type)
912 case BINEXPR_EQUAL: return pn_Cmp_Eq;
913 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
914 case BINEXPR_LESS: return pn_Cmp_Lt;
915 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
916 case BINEXPR_GREATER: return pn_Cmp_Gt;
917 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
921 panic("trying to get pn_Cmp from non-comparison binexpr type");
924 static ir_node *create_lazy_op(const binary_expression_t *expression)
926 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
927 type_t *type = expression->expression.datatype;
928 ir_mode *mode = get_ir_mode(type);
930 ir_node *cur_block = get_cur_block();
932 ir_node *one_block = new_immBlock();
933 ir_node *one = new_Const(mode, get_mode_one(mode));
934 ir_node *jmp_one = new_d_Jmp(dbgi);
936 ir_node *zero_block = new_immBlock();
937 ir_node *zero = new_Const(mode, get_mode_null(mode));
938 ir_node *jmp_zero = new_d_Jmp(dbgi);
940 set_cur_block(cur_block);
941 create_condition_evaluation((const expression_t*) expression,
942 one_block, zero_block);
943 mature_immBlock(one_block);
944 mature_immBlock(zero_block);
946 ir_node *common_block = new_immBlock();
947 add_immBlock_pred(common_block, jmp_one);
948 add_immBlock_pred(common_block, jmp_zero);
949 mature_immBlock(common_block);
951 ir_node *in[2] = { one, zero };
952 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
957 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
958 ir_node *right, ir_mode *mode);
960 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
961 create_arithmetic_func func)
963 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
964 ir_node *left = expression_to_firm(expression->left);
965 ir_node *right = expression_to_firm(expression->right);
966 type_t *type = expression->right->datatype;
967 /* be careful with the modes, because in asithmetic assign nodes only
968 * the right operand has the mode of the arithmetic alread */
969 ir_mode *mode = get_ir_mode(type);
970 left = create_conv(dbgi, left, mode);
971 ir_node *res = func(dbgi, left, right, mode);
976 static ir_node *pointer_arithmetic(ir_node *const pointer,
979 dbg_info *const dbgi,
980 const create_arithmetic_func func)
982 pointer_type_t *const pointer_type = (pointer_type_t*)type;
983 type_t *const points_to = pointer_type->points_to;
984 const unsigned elem_size = get_type_size(points_to);
986 assert(elem_size >= 1);
988 integer = create_conv(dbgi, integer, mode_Is);
989 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
990 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
994 ir_mode *const mode = get_ir_mode(type);
995 return func(dbgi, pointer, integer, mode);
998 static ir_node *create_arithmetic_assign_binop(
999 const binary_expression_t *expression, create_arithmetic_func func)
1001 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1002 type_t *const type = expression->expression.datatype;
1005 if (type->type == TYPE_POINTER) {
1006 ir_node *const pointer = expression_to_firm(expression->left);
1007 ir_node * integer = expression_to_firm(expression->right);
1008 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1010 value = create_arithmetic_binop(expression, func);
1013 ir_mode *const mode = get_ir_mode(type);
1014 value = create_conv(dbgi, value, mode);
1015 set_value_for_expression(expression->left, value);
1020 static ir_node *create_add(const binary_expression_t *expression)
1022 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1023 ir_node *left = expression_to_firm(expression->left);
1024 ir_node *right = expression_to_firm(expression->right);
1025 type_t *type = expression->expression.datatype;
1027 expression_t *expr_left = expression->left;
1028 expression_t *expr_right = expression->right;
1029 type_t *type_left = skip_typeref(expr_left->datatype);
1030 type_t *type_right = skip_typeref(expr_right->datatype);
1032 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1033 ir_mode *const mode = get_ir_mode(type);
1034 return new_d_Add(dbgi, left, right, mode);
1037 if(type_left->type == TYPE_POINTER) {
1038 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Add);
1040 assert(type_right->type == TYPE_POINTER);
1041 return pointer_arithmetic(right, left, type_right, dbgi, new_d_Add);
1045 static ir_node *create_sub(const binary_expression_t *expression)
1047 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1048 expression_t *const expr_left = expression->left;
1049 expression_t *const expr_right = expression->right;
1050 ir_node *const left = expression_to_firm(expr_left);
1051 ir_node *const right = expression_to_firm(expr_right);
1052 type_t *const type = expression->expression.datatype;
1053 type_t *const type_left = skip_typeref(expr_left->datatype);
1054 type_t *const type_right = skip_typeref(expr_right->datatype);
1056 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
1057 (type_left->type == TYPE_POINTER && type_right->type == TYPE_POINTER)) {
1058 ir_mode *const mode = get_ir_mode(type);
1059 return new_d_Sub(dbgi, left, right, mode);
1062 assert(type_left->type == TYPE_POINTER);
1063 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1066 static ir_node *create_shift(const binary_expression_t *expression)
1068 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1069 ir_node *left = expression_to_firm(expression->left);
1070 ir_node *right = expression_to_firm(expression->right);
1071 type_t *type = expression->expression.datatype;
1072 ir_mode *mode = get_ir_mode(type);
1074 /* firm always wants the shift count to be unsigned */
1075 right = create_conv(dbgi, right, mode_Iu);
1079 switch(expression->type) {
1080 case BINEXPR_SHIFTLEFT:
1081 res = new_d_Shl(dbgi, left, right, mode);
1083 case BINEXPR_SHIFTRIGHT: {
1084 expression_t *expr_left = expression->left;
1085 type_t *type_left = skip_typeref(expr_left->datatype);
1087 if(is_type_signed(type_left)) {
1088 res = new_d_Shrs(dbgi, left, right, mode);
1090 res = new_d_Shr(dbgi, left, right, mode);
1095 panic("create shift op called for non-shift op");
1102 static ir_node *create_divmod(const binary_expression_t *expression)
1104 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1105 ir_node *left = expression_to_firm(expression->left);
1106 ir_node *right = expression_to_firm(expression->right);
1107 ir_node *pin = new_Pin(new_NoMem());
1108 type_t *type = expression->expression.datatype;
1109 ir_mode *mode = get_ir_mode(type);
1113 switch (expression->type) {
1115 case BINEXPR_DIV_ASSIGN:
1116 if(mode_is_float(mode)) {
1117 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1118 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1120 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1121 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1126 case BINEXPR_MOD_ASSIGN:
1127 assert(!mode_is_float(mode));
1128 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1129 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1132 default: panic("unexpected binary expression type in create_divmod()");
1138 static ir_node *create_arithmetic_assign_divmod(
1139 const binary_expression_t *expression)
1141 ir_node * value = create_divmod(expression);
1142 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1143 type_t *const type = expression->expression.datatype;
1144 ir_mode *const mode = get_ir_mode(type);
1146 assert(type->type != TYPE_POINTER);
1148 value = create_conv(dbgi, value, mode);
1149 set_value_for_expression(expression->left, value);
1155 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1157 binary_expression_type_t type = expression->type;
1160 case BINEXPR_NOTEQUAL:
1162 case BINEXPR_LESSEQUAL:
1163 case BINEXPR_GREATER:
1164 case BINEXPR_GREATEREQUAL: {
1165 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1166 ir_node *left = expression_to_firm(expression->left);
1167 ir_node *right = expression_to_firm(expression->right);
1168 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1169 long pnc = get_pnc(type);
1170 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1173 case BINEXPR_ASSIGN: {
1174 ir_node *right = expression_to_firm(expression->right);
1175 set_value_for_expression(expression->left, right);
1179 return create_add(expression);
1181 return create_sub(expression);
1183 return create_arithmetic_binop(expression, new_d_Mul);
1184 case BINEXPR_BITWISE_AND:
1185 return create_arithmetic_binop(expression, new_d_And);
1186 case BINEXPR_BITWISE_OR:
1187 return create_arithmetic_binop(expression, new_d_Or);
1188 case BINEXPR_BITWISE_XOR:
1189 return create_arithmetic_binop(expression, new_d_Eor);
1190 case BINEXPR_SHIFTLEFT:
1191 case BINEXPR_SHIFTRIGHT:
1192 return create_shift(expression);
1195 return create_divmod(expression);
1196 case BINEXPR_LOGICAL_AND:
1197 case BINEXPR_LOGICAL_OR:
1198 return create_lazy_op(expression);
1200 expression_to_firm(expression->left);
1201 return expression_to_firm(expression->right);
1202 case BINEXPR_ADD_ASSIGN:
1203 return create_arithmetic_assign_binop(expression, new_d_Add);
1204 case BINEXPR_SUB_ASSIGN:
1205 return create_arithmetic_assign_binop(expression, new_d_Sub);
1206 case BINEXPR_MUL_ASSIGN:
1207 return create_arithmetic_assign_binop(expression, new_d_Mul);
1208 case BINEXPR_DIV_ASSIGN:
1209 return create_arithmetic_assign_divmod(expression);
1210 case BINEXPR_BITWISE_AND_ASSIGN:
1211 return create_arithmetic_assign_binop(expression, new_d_And);
1212 case BINEXPR_BITWISE_OR_ASSIGN:
1213 return create_arithmetic_assign_binop(expression, new_d_Or);
1214 case BINEXPR_BITWISE_XOR_ASSIGN:
1215 return create_arithmetic_assign_binop(expression, new_d_Eor);
1216 case BINEXPR_SHIFTLEFT_ASSIGN:
1217 return create_arithmetic_assign_binop(expression, new_d_Shl);
1218 case BINEXPR_SHIFTRIGHT_ASSIGN:
1219 return create_arithmetic_assign_binop(expression, new_d_Shr);
1221 panic("TODO binexpr type");
1225 static ir_node *array_access_addr(const array_access_expression_t *expression)
1227 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1229 ir_node *base_addr = expression_to_firm(expression->array_ref);
1230 ir_node *offset = expression_to_firm(expression->index);
1231 offset = create_conv(dbgi, offset, mode_Iu);
1233 unsigned elem_size = get_type_size(expression->expression.datatype);
1234 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1235 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1237 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1242 static ir_node *array_access_to_firm(
1243 const array_access_expression_t *expression)
1245 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1246 ir_node *addr = array_access_addr(expression);
1247 type_t *type = expression->expression.datatype;
1248 return deref_address(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 *cur_block = get_cur_block();
1272 /* create the true block */
1273 ir_node *true_block = new_immBlock();
1275 ir_node *true_val = expression_to_firm(expression->true_expression);
1276 ir_node *true_jmp = new_Jmp();
1278 /* create the false block */
1279 ir_node *false_block = new_immBlock();
1281 ir_node *false_val = expression_to_firm(expression->false_expression);
1282 ir_node *false_jmp = new_Jmp();
1284 /* create the condition evaluation */
1285 set_cur_block(cur_block);
1286 create_condition_evaluation(expression->condition, true_block, false_block);
1287 mature_immBlock(true_block);
1288 mature_immBlock(false_block);
1290 /* create the common block */
1291 ir_node *common_block = new_immBlock();
1292 add_immBlock_pred(common_block, true_jmp);
1293 add_immBlock_pred(common_block, false_jmp);
1294 mature_immBlock(common_block);
1296 ir_node *in[2] = { true_val, false_val };
1297 ir_mode *mode = get_irn_mode(true_val);
1298 assert(get_irn_mode(false_val) == mode);
1299 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1304 static ir_node *select_addr(const select_expression_t *expression)
1306 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1308 ir_node *compound_addr = expression_to_firm(expression->compound);
1310 declaration_t *entry = expression->compound_entry;
1311 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1312 ir_entity *entity = entry->v.entity;
1314 assert(entity != NULL);
1316 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1321 static ir_node *select_to_firm(const select_expression_t *expression)
1323 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1324 ir_node *addr = select_addr(expression);
1325 type_t *type = expression->expression.datatype;
1326 return deref_address(type, addr, dbgi);
1329 static ir_node *dereference_addr(const unary_expression_t *const expression)
1331 assert(expression->type == UNEXPR_DEREFERENCE);
1332 return expression_to_firm(expression->value);
1335 static ir_node *expression_to_addr(const expression_t *expression)
1337 switch(expression->type) {
1338 case EXPR_REFERENCE:
1339 return reference_addr((const reference_expression_t*) expression);
1340 case EXPR_ARRAY_ACCESS:
1341 return array_access_addr((const array_access_expression_t*) expression);
1343 return select_addr((const select_expression_t*) expression);
1345 const unary_expression_t *const unary_expr =
1346 (const unary_expression_t*)expression;
1347 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1348 return dereference_addr(unary_expr);
1355 panic("trying to get address of non-lvalue");
1358 static ir_node *_expression_to_firm(const expression_t *expression)
1360 switch(expression->type) {
1362 return const_to_firm((const const_t*) expression);
1363 case EXPR_STRING_LITERAL:
1364 return string_literal_to_firm((const string_literal_t*) expression);
1365 case EXPR_REFERENCE:
1366 return reference_expression_to_firm(
1367 (const reference_expression_t*) expression);
1369 return call_expression_to_firm((const call_expression_t*) expression);
1371 return unary_expression_to_firm((const unary_expression_t*) expression);
1373 return binary_expression_to_firm(
1374 (const binary_expression_t*) expression);
1375 case EXPR_ARRAY_ACCESS:
1376 return array_access_to_firm(
1377 (const array_access_expression_t*) expression);
1379 return sizeof_to_firm((const sizeof_expression_t*) expression);
1380 case EXPR_CONDITIONAL:
1381 return conditional_to_firm((const conditional_expression_t*)expression);
1383 return select_to_firm((const select_expression_t*) expression);
1386 case EXPR_PRETTY_FUNCTION:
1388 case EXPR_STATEMENT:
1389 case EXPR_BUILTIN_SYMBOL:
1390 panic("unimplemented expression found");
1396 panic("invalid expression found");
1399 static ir_node *expression_to_firm(const expression_t *expression)
1401 ir_node *res = _expression_to_firm(expression);
1403 if(res != NULL && get_irn_mode(res) == mode_b) {
1404 ir_mode *mode = get_ir_mode(expression->datatype);
1405 res = create_conv(NULL, res, mode);
1411 static ir_node *expression_to_modeb(const expression_t *expression)
1413 ir_node *res = _expression_to_firm(expression);
1414 res = create_conv(NULL, res, mode_b);
1420 * create a short-circuit expression evaluation that tries to construct
1421 * efficient control flow structures for &&, || and ! expressions
1423 static void create_condition_evaluation(const expression_t *expression,
1424 ir_node *true_block,
1425 ir_node *false_block)
1427 switch(expression->type) {
1429 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1430 if(unary_expression->type == UNEXPR_NOT) {
1431 create_condition_evaluation(unary_expression->value, false_block,
1438 binary_expression_t *binary_expression
1439 = (binary_expression_t*) expression;
1440 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1441 ir_node *cur_block = get_cur_block();
1442 ir_node *extra_block = new_immBlock();
1443 set_cur_block(cur_block);
1444 create_condition_evaluation(binary_expression->left, extra_block,
1446 mature_immBlock(extra_block);
1447 set_cur_block(extra_block);
1448 create_condition_evaluation(binary_expression->right, true_block,
1452 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1453 ir_node *cur_block = get_cur_block();
1454 ir_node *extra_block = new_immBlock();
1455 set_cur_block(cur_block);
1456 create_condition_evaluation(binary_expression->left, true_block,
1458 mature_immBlock(extra_block);
1459 set_cur_block(extra_block);
1460 create_condition_evaluation(binary_expression->right, true_block,
1470 dbg_info *dbgi = get_dbg_info(&expression->source_position);
1471 ir_node *condition = expression_to_modeb(expression);
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(true_block, true_proj);
1477 add_immBlock_pred(false_block, false_proj);
1479 set_cur_block(NULL);
1482 static void statement_to_firm(statement_t *statement);
1484 static void return_statement_to_firm(return_statement_t *statement)
1486 if(get_cur_block() == NULL)
1489 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1492 if(statement->return_value != NULL) {
1493 ir_node *retval = expression_to_firm(statement->return_value);
1497 ret = new_d_Return(dbgi, get_store(), 1, in);
1499 ret = new_d_Return(dbgi, get_store(), 0, NULL);
1501 ir_node *end_block = get_irg_end_block(current_ir_graph);
1502 add_immBlock_pred(end_block, ret);
1504 set_cur_block(NULL);
1507 static void compound_statement_to_firm(compound_statement_t *compound)
1509 statement_t *statement = compound->statements;
1510 for( ; statement != NULL; statement = statement->next) {
1511 //context2firm(&statement->context);
1512 statement_to_firm(statement);
1516 static void expression_statement_to_firm(expression_statement_t *statement)
1518 if(get_cur_block() == NULL)
1521 expression_to_firm(statement->expression);
1524 static void if_statement_to_firm(if_statement_t *statement)
1526 ir_node *cur_block = get_cur_block();
1528 ir_node *fallthrough_block = new_immBlock();
1530 /* the true (blocks) */
1531 ir_node *true_block = new_immBlock();
1533 statement_to_firm(statement->true_statement);
1534 if(get_cur_block() != NULL) {
1535 ir_node *jmp = new_Jmp();
1536 add_immBlock_pred(fallthrough_block, jmp);
1539 /* the false (blocks) */
1540 ir_node *false_block;
1541 if(statement->false_statement != NULL) {
1542 false_block = new_immBlock();
1544 statement_to_firm(statement->false_statement);
1545 if(get_cur_block() != NULL) {
1546 ir_node *jmp = new_Jmp();
1547 add_immBlock_pred(fallthrough_block, jmp);
1550 false_block = fallthrough_block;
1553 /* create the condition */
1554 if(cur_block != NULL) {
1555 set_cur_block(cur_block);
1556 create_condition_evaluation(statement->condition, true_block,
1560 mature_immBlock(true_block);
1561 if(false_block != fallthrough_block) {
1562 mature_immBlock(false_block);
1564 mature_immBlock(fallthrough_block);
1566 set_cur_block(fallthrough_block);
1569 static void while_statement_to_firm(while_statement_t *statement)
1571 ir_node *jmp = NULL;
1572 if(get_cur_block() != NULL) {
1576 /* create the header block */
1577 ir_node *header_block = new_immBlock();
1579 add_immBlock_pred(header_block, jmp);
1582 /* the false block */
1583 ir_node *false_block = new_immBlock();
1586 ir_node *body_block = new_immBlock();
1588 ir_node *old_continue_label = continue_label;
1589 ir_node *old_break_label = break_label;
1590 continue_label = header_block;
1591 break_label = false_block;
1593 statement_to_firm(statement->body);
1595 assert(continue_label == header_block);
1596 assert(break_label == false_block);
1597 continue_label = old_continue_label;
1598 break_label = old_break_label;
1600 if(get_cur_block() != NULL) {
1601 ir_node *jmp = new_Jmp();
1602 add_immBlock_pred(header_block, jmp);
1605 /* create the condition */
1606 set_cur_block(header_block);
1608 create_condition_evaluation(statement->condition, body_block, false_block);
1609 mature_immBlock(body_block);
1610 mature_immBlock(false_block);
1611 mature_immBlock(header_block);
1613 set_cur_block(false_block);
1616 static void do_while_statement_to_firm(do_while_statement_t *statement)
1618 ir_node *jmp = NULL;
1619 if(get_cur_block() != NULL) {
1623 /* create the header block */
1624 ir_node *header_block = new_immBlock();
1626 /* the false block */
1627 ir_node *false_block = new_immBlock();
1630 ir_node *body_block = new_immBlock();
1632 add_immBlock_pred(body_block, jmp);
1635 ir_node *old_continue_label = continue_label;
1636 ir_node *old_break_label = break_label;
1637 continue_label = header_block;
1638 break_label = false_block;
1640 statement_to_firm(statement->body);
1642 assert(continue_label == header_block);
1643 assert(break_label == false_block);
1644 continue_label = old_continue_label;
1645 break_label = old_break_label;
1647 if(get_cur_block() == NULL) {
1648 mature_immBlock(header_block);
1649 mature_immBlock(body_block);
1650 mature_immBlock(false_block);
1654 ir_node *body_jmp = new_Jmp();
1655 add_immBlock_pred(header_block, body_jmp);
1656 mature_immBlock(header_block);
1658 /* create the condition */
1659 set_cur_block(header_block);
1661 create_condition_evaluation(statement->condition, body_block, false_block);
1662 mature_immBlock(body_block);
1663 mature_immBlock(false_block);
1664 mature_immBlock(header_block);
1666 set_cur_block(false_block);
1669 static void for_statement_to_firm(for_statement_t *statement)
1671 ir_node *jmp = NULL;
1672 if (get_cur_block() != NULL) {
1673 if(statement->initialisation != NULL) {
1674 expression_to_firm(statement->initialisation);
1679 /* create the step block */
1680 ir_node *const step_block = new_immBlock();
1681 if (statement->step != NULL) {
1682 expression_to_firm(statement->step);
1684 ir_node *const step_jmp = new_Jmp();
1686 /* create the header block */
1687 ir_node *const header_block = new_immBlock();
1689 add_immBlock_pred(header_block, jmp);
1691 add_immBlock_pred(header_block, step_jmp);
1693 /* the false block */
1694 ir_node *const false_block = new_immBlock();
1697 ir_node *const body_block = new_immBlock();
1699 ir_node *const old_continue_label = continue_label;
1700 ir_node *const old_break_label = break_label;
1701 continue_label = step_block;
1702 break_label = false_block;
1704 statement_to_firm(statement->body);
1706 assert(continue_label == step_block);
1707 assert(break_label == false_block);
1708 continue_label = old_continue_label;
1709 break_label = old_break_label;
1711 if (get_cur_block() != NULL) {
1712 ir_node *const jmp = new_Jmp();
1713 add_immBlock_pred(step_block, jmp);
1716 /* create the condition */
1717 set_cur_block(header_block);
1718 if (statement->condition != NULL) {
1719 create_condition_evaluation(statement->condition, body_block,
1722 keep_alive(header_block);
1723 ir_node *jmp = new_Jmp();
1724 add_immBlock_pred(body_block, jmp);
1727 mature_immBlock(body_block);
1728 mature_immBlock(false_block);
1729 mature_immBlock(step_block);
1730 mature_immBlock(header_block);
1731 mature_immBlock(false_block);
1733 set_cur_block(false_block);
1736 static void create_declaration_entity(declaration_t *declaration,
1737 declaration_type_t declaration_type,
1738 ir_type *parent_type)
1740 ident *id = new_id_from_str(declaration->symbol->string);
1741 ir_type *irtype = get_ir_type(declaration->type);
1742 ir_entity *entity = new_entity(parent_type, id, irtype);
1743 set_entity_ld_ident(entity, id);
1745 declaration->declaration_type = declaration_type;
1746 declaration->v.entity = entity;
1747 set_entity_variability(entity, variability_uninitialized);
1748 /* TODO: visibility? */
1751 static void create_initializer(declaration_t *declaration)
1753 initializer_t *initializer = declaration->init.initializer;
1754 if(initializer == NULL)
1757 if(initializer->type == INITIALIZER_VALUE) {
1758 assert(initializer->designator == NULL);
1759 assert(initializer->next == NULL);
1760 ir_node *init_node = expression_to_firm(initializer->v.value);
1762 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1763 set_value(declaration->v.value_number, init_node);
1765 ir_entity *entity = declaration->v.entity;
1767 set_entity_variability(entity, variability_initialized);
1768 set_atomic_ent_value(entity, init_node);
1771 assert(initializer->type == INITIALIZER_LIST);
1772 panic("list initializer not supported yet");
1776 static void create_local_variable(declaration_t *declaration)
1778 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1780 bool needs_entity = declaration->address_taken;
1781 type_t *type = skip_typeref(declaration->type);
1783 if(type->type == TYPE_ARRAY
1784 || type->type == TYPE_COMPOUND_STRUCT
1785 || type->type == TYPE_COMPOUND_UNION) {
1786 needs_entity = true;
1790 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1791 create_declaration_entity(declaration,
1792 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1795 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1796 declaration->v.value_number = next_value_number_function;
1797 ++next_value_number_function;
1800 create_initializer(declaration);
1803 static void declaration_statement_to_firm(declaration_statement_t *statement)
1805 declaration_t *declaration = statement->declarations_begin;
1806 declaration_t *end = statement->declarations_end->next;
1807 for( ; declaration != end; declaration = declaration->next) {
1808 type_t *type = declaration->type;
1810 switch(declaration->storage_class) {
1811 case STORAGE_CLASS_TYPEDEF:
1813 case STORAGE_CLASS_STATIC:
1814 panic("static local vars not implemented yet");
1815 case STORAGE_CLASS_ENUM_ENTRY:
1816 panic("enum entry declaration in local block found");
1817 case STORAGE_CLASS_EXTERN:
1818 panic("extern declaration in local block found");
1819 case STORAGE_CLASS_NONE:
1820 case STORAGE_CLASS_AUTO:
1821 case STORAGE_CLASS_REGISTER:
1822 if(type->type == TYPE_FUNCTION) {
1823 panic("nested functions not supported yet");
1825 create_local_variable(declaration);
1829 panic("invalid storage class found");
1833 static void create_jump_statement(const statement_t *statement,
1834 ir_node *target_block)
1836 if(get_cur_block() == NULL)
1839 dbg_info *dbgi = get_dbg_info(&statement->source_position);
1840 ir_node *jump = new_d_Jmp(dbgi);
1841 add_immBlock_pred(target_block, jump);
1843 set_cur_block(NULL);
1846 static void switch_statement_to_firm(const switch_statement_t *statement)
1848 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1850 ir_node *expression = expression_to_firm(statement->expression);
1851 ir_node *cond = new_d_Cond(dbgi, expression);
1852 ir_node *break_block = new_immBlock();
1854 set_cur_block(NULL);
1856 ir_node *const old_switch_cond = current_switch_cond;
1857 ir_node *const old_break_label = break_label;
1858 const bool old_saw_default_label = saw_default_label;
1859 current_switch_cond = cond;
1860 break_label = break_block;
1862 statement_to_firm(statement->body);
1864 if(get_cur_block() != NULL) {
1865 ir_node *jmp = new_Jmp();
1866 add_immBlock_pred(break_block, jmp);
1869 if (!saw_default_label) {
1870 set_cur_block(get_nodes_block(cond));
1871 ir_node *const proj = new_d_defaultProj(dbgi, cond,
1872 MAGIC_DEFAULT_PN_NUMBER);
1873 add_immBlock_pred(break_block, proj);
1876 assert(current_switch_cond == cond);
1877 assert(break_label == break_block);
1878 current_switch_cond = old_switch_cond;
1879 break_label = old_break_label;
1880 saw_default_label = old_saw_default_label;
1882 mature_immBlock(break_block);
1883 set_cur_block(break_block);
1886 static long fold_constant(const expression_t *expression)
1888 ir_graph *old_current_ir_graph = current_ir_graph;
1889 current_ir_graph = get_const_code_irg();
1891 ir_node *cnst = expression_to_firm(expression);
1892 if(!is_Const(cnst)) {
1893 panic("couldn't fold constantl");
1895 tarval *tv = get_Const_tarval(cnst);
1896 if(!tarval_is_long(tv)) {
1897 panic("folded constant not an integer");
1900 long res = get_tarval_long(tv);
1902 current_ir_graph = old_current_ir_graph;
1906 static void case_label_to_firm(const case_label_statement_t *statement)
1908 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1910 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
1912 /* let's create a node and hope firm constant folding creates a Const
1915 set_cur_block(get_nodes_block(current_switch_cond));
1916 if(statement->expression) {
1917 long pn = fold_constant(statement->expression);
1918 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
1919 /* oops someone detected our cheating... */
1920 panic("magic default pn used");
1922 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
1924 saw_default_label = true;
1925 proj = new_d_defaultProj(dbgi, current_switch_cond,
1926 MAGIC_DEFAULT_PN_NUMBER);
1929 ir_node *block = new_immBlock();
1930 if (fallthrough != NULL) {
1931 add_immBlock_pred(block, fallthrough);
1933 add_immBlock_pred(block, proj);
1934 mature_immBlock(block);
1937 static ir_node *get_label_block(declaration_t *label)
1939 assert(label->namespace == NAMESPACE_LABEL);
1941 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
1942 return label->v.block;
1944 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
1946 ir_node *old_cur_block = get_cur_block();
1947 ir_node *block = new_immBlock();
1948 set_cur_block(old_cur_block);
1950 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
1951 label->v.block = block;
1953 ARR_APP1(ir_node *, imature_blocks, block);
1958 static void label_to_firm(const label_statement_t *statement)
1960 ir_node *block = get_label_block(statement->label);
1962 if(get_cur_block() != NULL) {
1963 ir_node *jmp = new_Jmp();
1964 add_immBlock_pred(block, jmp);
1967 set_cur_block(block);
1970 statement_to_firm(statement->label_statement);
1973 static void goto_to_firm(const goto_statement_t *statement)
1975 if(get_cur_block() == NULL)
1978 ir_node *block = get_label_block(statement->label);
1979 ir_node *jmp = new_Jmp();
1980 add_immBlock_pred(block, jmp);
1982 set_cur_block(NULL);
1985 static void statement_to_firm(statement_t *statement)
1987 switch(statement->type) {
1988 case STATEMENT_COMPOUND:
1989 compound_statement_to_firm((compound_statement_t*) statement);
1991 case STATEMENT_RETURN:
1992 return_statement_to_firm((return_statement_t*) statement);
1994 case STATEMENT_EXPRESSION:
1995 expression_statement_to_firm((expression_statement_t*) statement);
1998 if_statement_to_firm((if_statement_t*) statement);
2000 case STATEMENT_WHILE:
2001 while_statement_to_firm((while_statement_t*) statement);
2003 case STATEMENT_DO_WHILE:
2004 do_while_statement_to_firm((do_while_statement_t*) statement);
2006 case STATEMENT_DECLARATION:
2007 declaration_statement_to_firm((declaration_statement_t*) statement);
2009 case STATEMENT_BREAK:
2010 create_jump_statement(statement, break_label);
2012 case STATEMENT_CONTINUE:
2013 create_jump_statement(statement, continue_label);
2015 case STATEMENT_SWITCH:
2016 switch_statement_to_firm((switch_statement_t*) statement);
2018 case STATEMENT_CASE_LABEL:
2019 case_label_to_firm((case_label_statement_t*) statement);
2022 for_statement_to_firm((for_statement_t*) statement);
2024 case STATEMENT_LABEL:
2025 label_to_firm((label_statement_t*) statement);
2027 case STATEMENT_GOTO:
2028 goto_to_firm((goto_statement_t*) statement);
2033 panic("Statement not implemented\n");
2036 static int get_function_n_local_vars(declaration_t *declaration)
2043 static void initialize_function_parameters(declaration_t *declaration)
2045 ir_graph *irg = current_ir_graph;
2046 ir_node *args = get_irg_args(irg);
2047 ir_node *start_block = get_irg_start_block(irg);
2050 declaration_t *parameter = declaration->context.declarations;
2051 for( ; parameter != NULL; parameter = parameter->next) {
2052 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2053 type_t *type = parameter->type;
2055 bool needs_entity = parameter->address_taken;
2056 if(type->type == TYPE_COMPOUND_STRUCT
2057 || type->type == TYPE_COMPOUND_UNION) {
2058 needs_entity = true;
2062 panic("entities for function parameters not implemented yet");
2065 ir_mode *mode = get_ir_mode(parameter->type);
2067 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2070 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2071 parameter->v.value_number = next_value_number_function;
2072 ++next_value_number_function;
2074 set_value(parameter->v.value_number, proj);
2078 static void create_function(declaration_t *declaration)
2080 ir_entity *entity = get_function_entity(declaration);
2082 if(declaration->init.statement == NULL)
2085 assert(imature_blocks == NULL);
2086 imature_blocks = NEW_ARR_F(ir_node*, 0);
2088 int n_local_vars = get_function_n_local_vars(declaration);
2089 ir_graph *irg = new_ir_graph(entity, n_local_vars);
2090 ir_node *first_block = get_cur_block();
2092 next_value_number_function = 0;
2093 initialize_function_parameters(declaration);
2095 statement_to_firm(declaration->init.statement);
2097 ir_node *end_block = get_irg_end_block(irg);
2099 /* do we have a return statement yet? */
2100 if(get_cur_block() != NULL) {
2101 assert(declaration->type->type == TYPE_FUNCTION);
2102 const function_type_t* const func_type
2103 = (const function_type_t*) declaration->type;
2105 if (func_type->result_type == type_void) {
2106 ret = new_Return(get_store(), 0, NULL);
2108 ir_mode *const mode = get_ir_mode(func_type->result_type);
2110 // ยง5.1.2.2.3 main implicitly returns 0
2111 if (strcmp(declaration->symbol->string, "main") == 0) {
2112 in[0] = new_Const(mode, get_mode_null(mode));
2114 in[0] = new_Unknown(mode);
2116 ret = new_Return(get_store(), 1, in);
2118 add_immBlock_pred(end_block, ret);
2121 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2122 mature_immBlock(imature_blocks[i]);
2124 DEL_ARR_F(imature_blocks);
2125 imature_blocks = NULL;
2127 mature_immBlock(first_block);
2128 mature_immBlock(end_block);
2130 irg_finalize_cons(irg);
2132 /* finalize the frame type */
2133 ir_type *frame_type = get_irg_frame_type(irg);
2134 int n = get_compound_n_members(frame_type);
2137 for(int i = 0; i < n; ++i) {
2138 ir_entity *entity = get_compound_member(frame_type, i);
2139 ir_type *entity_type = get_entity_type(entity);
2141 int align = get_type_alignment_bytes(entity_type);
2142 if(align > align_all)
2146 misalign = offset % align;
2150 set_entity_offset(entity, offset);
2151 offset += get_type_size_bytes(entity_type);
2153 set_type_size_bytes(frame_type, offset);
2154 set_type_alignment_bytes(frame_type, align_all);
2155 set_type_state(frame_type, layout_fixed);
2160 static void create_global_variable(declaration_t *declaration)
2162 ir_type *global_type = get_glob_type();
2163 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2166 ir_entity *entity = declaration->v.entity;
2167 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2168 set_entity_visibility(entity, visibility_local);
2169 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2170 set_entity_visibility(entity, visibility_external_allocated);
2172 set_entity_visibility(entity, visibility_external_visible);
2174 current_ir_graph = get_const_code_irg();
2175 create_initializer(declaration);
2178 static void context_to_firm(context_t *context)
2180 declaration_t *declaration = context->declarations;
2181 for( ; declaration != NULL; declaration = declaration->next) {
2182 if(declaration->namespace != NAMESPACE_NORMAL)
2184 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2185 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2187 if(declaration->symbol == NULL)
2190 type_t *type = declaration->type;
2191 if(type->type == TYPE_FUNCTION) {
2192 create_function(declaration);
2194 create_global_variable(declaration);
2199 void translation_unit_to_firm(translation_unit_t *unit)
2201 /* remove me later TODO FIXME */
2202 (void) get_type_size;
2204 /* just to be sure */
2205 continue_label = NULL;
2207 current_switch_cond = NULL;
2209 context_to_firm(& unit->context);