9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
15 #include "adt/array.h"
20 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
22 static ir_type *ir_type_const_char;
23 static ir_type *ir_type_void;
24 static ir_type *ir_type_int;
25 static ir_type *ir_type_void_ptr;
27 static type_t *type_const_char;
28 static type_t *type_void;
29 static type_t *type_int;
31 static int next_value_number_function;
32 static ir_node *continue_label;
33 static ir_node *break_label;
34 static ir_node *current_switch_cond;
35 static bool saw_default_label;
36 static ir_node **imature_blocks;
38 typedef enum declaration_type_t {
39 DECLARATION_TYPE_UNKNOWN,
40 DECLARATION_TYPE_FUNCTION,
41 DECLARATION_TYPE_GLOBAL_VARIABLE,
42 DECLARATION_TYPE_LOCAL_VARIABLE,
43 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
44 DECLARATION_TYPE_COMPOUND_MEMBER,
45 DECLARATION_TYPE_LABEL_BLOCK,
48 static ir_type *get_ir_type(type_t *type);
50 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
54 const declaration_t *declaration = & value_numbers[pos]->declaration;
56 print_warning_prefix(declaration->source_position);
57 fprintf(stderr, "variable '%s' might be used uninitialized\n",
58 declaration->symbol->string);
60 fprintf(stderr, "Some variable might be used uninitialized\n");
61 return new_r_Unknown(irg, mode);
64 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
66 const source_position_t *pos = (const source_position_t*) dbg;
69 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
73 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
75 const source_position_t *pos = (const source_position_t*) dbg;
80 return pos->input_name;
83 void init_ast2firm(void)
85 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
86 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
87 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
89 ir_type_int = get_ir_type(type_int);
90 ir_type_const_char = get_ir_type(type_const_char);
91 ir_type_void = get_ir_type(type_int); /* we don't have a real void
93 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
94 ir_type_void, mode_P_data);
96 type_void->firm_type = ir_type_void;
99 void exit_ast2firm(void)
103 static unsigned unique_id = 0;
105 static ident *unique_ident(const char *tag)
109 snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
111 return new_id_from_str(buf);
114 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
116 switch(atomic_type->atype) {
117 case ATOMIC_TYPE_SCHAR:
118 case ATOMIC_TYPE_CHAR:
120 case ATOMIC_TYPE_UCHAR:
122 case ATOMIC_TYPE_SHORT:
124 case ATOMIC_TYPE_USHORT:
126 case ATOMIC_TYPE_LONG:
127 case ATOMIC_TYPE_INT:
129 case ATOMIC_TYPE_ULONG:
130 case ATOMIC_TYPE_UINT:
132 case ATOMIC_TYPE_LONGLONG:
134 case ATOMIC_TYPE_ULONGLONG:
136 case ATOMIC_TYPE_FLOAT:
138 case ATOMIC_TYPE_DOUBLE:
140 case ATOMIC_TYPE_LONG_DOUBLE:
142 case ATOMIC_TYPE_BOOL:
144 #ifdef PROVIDE_COMPLEX
145 case ATOMIC_TYPE_FLOAT_COMPLEX:
146 case ATOMIC_TYPE_DOUBLE_COMPLEX:
147 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
148 panic("complex lowering not implemented yet");
150 case ATOMIC_TYPE_FLOAT_IMAGINARY:
151 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
152 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
153 panic("imaginary lowering not implemented yet");
156 case ATOMIC_TYPE_VOID:
157 /* firm has no real void... */
159 case ATOMIC_TYPE_INVALID:
162 panic("Encountered unknown atomic type");
166 static unsigned get_type_size(type_t *type);
168 static unsigned get_atomic_type_size(const atomic_type_t *type)
170 switch(type->atype) {
171 case ATOMIC_TYPE_CHAR:
172 case ATOMIC_TYPE_SCHAR:
173 case ATOMIC_TYPE_UCHAR:
176 case ATOMIC_TYPE_SHORT:
177 case ATOMIC_TYPE_USHORT:
180 case ATOMIC_TYPE_BOOL:
181 case ATOMIC_TYPE_INT:
182 case ATOMIC_TYPE_UINT:
183 case ATOMIC_TYPE_LONG:
184 case ATOMIC_TYPE_ULONG:
185 case ATOMIC_TYPE_FLOAT:
188 case ATOMIC_TYPE_LONGLONG:
189 case ATOMIC_TYPE_ULONGLONG:
190 case ATOMIC_TYPE_DOUBLE:
193 case ATOMIC_TYPE_LONG_DOUBLE:
196 case ATOMIC_TYPE_VOID:
199 case ATOMIC_TYPE_INVALID:
202 panic("Trying to determine size of invalid atomic type");
205 static unsigned get_compound_type_size(compound_type_t *type)
207 ir_type *irtype = get_ir_type(&type->type);
208 return get_type_size_bytes(irtype);
211 static unsigned get_array_type_size(array_type_t *type)
213 ir_type *irtype = get_ir_type(&type->type);
214 return get_type_size_bytes(irtype);
217 static unsigned get_type_size(type_t *type)
219 type = skip_typeref(type);
223 return get_atomic_type_size((const atomic_type_t*) type);
225 return get_mode_size_bytes(mode_Is);
226 case TYPE_COMPOUND_UNION:
227 case TYPE_COMPOUND_STRUCT:
228 return get_compound_type_size((compound_type_t*) type);
230 /* just a pointer to the function */
231 return get_mode_size_bytes(mode_P_code);
233 return get_mode_size_bytes(mode_P_data);
235 return get_array_type_size((array_type_t*) type);
242 panic("Trying to determine size of invalid type");
245 static unsigned count_parameters(const function_type_t *function_type)
249 function_parameter_t *parameter = function_type->parameters;
250 for ( ; parameter != NULL; parameter = parameter->next) {
260 static long fold_constant(const expression_t *expression);
262 static ir_type *create_atomic_type(const atomic_type_t *type)
264 ir_mode *mode = get_atomic_mode(type);
265 ident *id = get_mode_ident(mode);
266 ir_type *irtype = new_type_primitive(id, mode);
271 static ir_type *create_method_type(const function_type_t *function_type)
273 type_t *result_type = function_type->result_type;
275 ident *id = unique_ident("functiontype");
276 int n_parameters = count_parameters(function_type);
277 int n_results = result_type == type_void ? 0 : 1;
278 ir_type *irtype = new_type_method(id, n_parameters, n_results);
280 if(result_type != type_void) {
281 ir_type *restype = get_ir_type(result_type);
282 set_method_res_type(irtype, 0, restype);
285 function_parameter_t *parameter = function_type->parameters;
287 for( ; parameter != NULL; parameter = parameter->next) {
288 ir_type *p_irtype = get_ir_type(parameter->type);
289 set_method_param_type(irtype, n, p_irtype);
293 if(function_type->variadic || function_type->unspecified_parameters) {
294 set_method_variadicity(irtype, variadicity_variadic);
300 static ir_type *create_pointer_type(pointer_type_t *type)
302 type_t *points_to = type->points_to;
303 ir_type *ir_points_to;
304 /* Avoid endless recursion if the points_to type contains this poiner type
305 * again (might be a struct). We therefore first create a void* pointer
306 * and then set the real points_to type
308 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
309 ir_type_void, mode_P_data);
310 type->type.firm_type = ir_type;
312 ir_points_to = get_ir_type(points_to);
313 set_pointer_points_to_type(ir_type, ir_points_to);
318 static ir_type *create_array_type(array_type_t *type)
320 type_t *element_type = type->element_type;
321 ir_type *ir_element_type = get_ir_type(element_type);
323 ident *id = unique_ident("array");
324 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
326 if(type->size != NULL) {
327 int n_elements = fold_constant(type->size);
329 set_array_bounds_int(ir_type, 0, 0, n_elements);
331 size_t elemsize = get_type_size_bytes(ir_element_type);
332 int align = get_type_alignment_bytes(ir_element_type);
333 if(elemsize % align > 0) {
334 elemsize += align - (elemsize % align);
336 set_type_size_bytes(ir_type, n_elements * elemsize);
337 set_type_alignment_bytes(ir_type, align);
338 set_type_state(ir_type, layout_fixed);
344 #define INVALID_TYPE ((ir_type_ptr)-1)
346 static ir_type *create_struct_type(compound_type_t *type)
348 symbol_t *symbol = type->declaration->symbol;
351 id = unique_ident(symbol->string);
353 id = unique_ident("__anonymous_struct");
355 ir_type *ir_type = new_type_struct(id);
357 type->type.firm_type = ir_type;
361 declaration_t *entry = type->declaration->context.declarations;
362 for( ; entry != NULL; entry = entry->next) {
363 if(entry->namespace != NAMESPACE_NORMAL)
366 ident *ident = new_id_from_str(entry->symbol->string);
367 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
369 int entry_size = get_type_size_bytes(entry_ir_type);
370 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
371 int misalign = offset % entry_alignment;
374 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
375 set_entity_offset(entity, offset);
376 add_struct_member(ir_type, entity);
377 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
378 entry->v.entity = entity;
380 offset += entry_size;
381 if(entry_alignment > align_all) {
382 if(entry_alignment % align_all != 0) {
383 panic("Uneven alignments not supported yet");
385 align_all = entry_alignment;
389 int misalign = offset % align_all;
391 set_type_alignment_bytes(ir_type, align_all);
392 set_type_size_bytes(ir_type, offset);
393 set_type_state(ir_type, layout_fixed);
398 static ir_type *create_union_type(compound_type_t *type)
400 declaration_t *declaration = type->declaration;
401 symbol_t *symbol = declaration->symbol;
404 id = unique_ident(symbol->string);
406 id = unique_ident("__anonymous_union");
408 ir_type *ir_type = new_type_union(id);
410 type->type.firm_type = ir_type;
414 declaration_t *entry = declaration->context.declarations;
415 for( ; entry != NULL; entry = entry->next) {
416 if(entry->namespace != NAMESPACE_NORMAL)
419 ident *ident = new_id_from_str(entry->symbol->string);
420 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
422 int entry_size = get_type_size_bytes(entry_ir_type);
423 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
425 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
426 add_union_member(ir_type, entity);
427 set_entity_offset(entity, 0);
428 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
429 entry->v.entity = entity;
431 if(entry_size > size) {
434 if(entry_alignment > align_all) {
435 if(entry_alignment % align_all != 0) {
436 panic("Uneven alignments not supported yet");
438 align_all = entry_alignment;
442 set_type_alignment_bytes(ir_type, align_all);
443 set_type_size_bytes(ir_type, size);
444 set_type_state(ir_type, layout_fixed);
449 static ir_type *get_ir_type(type_t *type)
451 assert(type != NULL);
453 type = skip_typeref(type);
455 if(type->firm_type != NULL) {
456 assert(type->firm_type != INVALID_TYPE);
457 return type->firm_type;
460 ir_type *firm_type = NULL;
463 firm_type = create_atomic_type((atomic_type_t*) type);
466 firm_type = create_method_type((function_type_t*) type);
469 firm_type = create_pointer_type((pointer_type_t*) type);
472 firm_type = create_array_type((array_type_t*) type);
474 case TYPE_COMPOUND_STRUCT:
475 firm_type = create_struct_type((compound_type_t*) type);
477 case TYPE_COMPOUND_UNION:
478 firm_type = create_union_type((compound_type_t*) type);
481 firm_type = ir_type_int;
489 if(firm_type == NULL)
490 panic("unknown type found");
492 type->firm_type = firm_type;
496 static inline ir_mode *get_ir_mode(type_t *type)
498 ir_type *irtype = get_ir_type(type);
500 /* firm doesn't report a mode for arrays somehow... */
501 if(is_Array_type(irtype)) {
505 ir_mode *mode = get_type_mode(irtype);
506 assert(mode != NULL);
510 static ir_entity* get_function_entity(declaration_t *declaration)
512 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
513 return declaration->v.entity;
514 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
516 symbol_t *symbol = declaration->symbol;
517 ident *id = new_id_from_str(symbol->string);
519 ir_type *global_type = get_glob_type();
520 ir_type *ir_type_method = get_ir_type(declaration->type);
521 assert(is_Method_type(ir_type_method));
523 ir_entity *entity = new_entity(global_type, id, ir_type_method);
524 set_entity_ld_ident(entity, id);
525 if(declaration->storage_class == STORAGE_CLASS_STATIC
526 || declaration->is_inline) {
527 set_entity_visibility(entity, visibility_local);
528 } else if(declaration->init.statement != NULL) {
529 set_entity_visibility(entity, visibility_external_visible);
531 set_entity_visibility(entity, visibility_external_allocated);
534 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
535 declaration->v.entity = entity;
542 static ir_node *expression_to_firm(const expression_t *expression);
543 static ir_node *expression_to_modeb(const expression_t *expression);
545 static dbg_info *get_dbg_info(const source_position_t *pos)
547 return (dbg_info*) pos;
550 static ir_node *const_to_firm(const const_t *cnst)
552 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
553 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
556 if(mode_is_float(mode)) {
557 tv = new_tarval_from_double(cnst->v.float_value, mode);
559 tv = new_tarval_from_long(cnst->v.int_value, mode);
562 return new_d_Const(dbgi, mode, tv);
565 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
567 assert(entity != NULL);
568 union symconst_symbol sym;
569 sym.entity_p = entity;
570 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
573 static ir_node *string_literal_to_firm(const string_literal_t* literal)
575 ir_type *global_type = get_glob_type();
576 ir_type *type = new_type_array(unique_ident("strtype"), 1,
579 ident *id = unique_ident("Lstr");
580 ir_entity *entity = new_entity(global_type, id, type);
581 set_entity_ld_ident(entity, id);
582 set_entity_variability(entity, variability_constant);
584 ir_type *elem_type = ir_type_const_char;
585 ir_mode *mode = get_type_mode(elem_type);
587 const char *string = literal->value;
588 size_t slen = strlen(string) + 1;
590 set_array_lower_bound_int(type, 0, 0);
591 set_array_upper_bound_int(type, 0, slen);
592 set_type_size_bytes(type, slen);
593 set_type_state(type, layout_fixed);
595 tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
596 for(size_t i = 0; i < slen; ++i) {
597 tvs[i] = new_tarval_from_long(string[i], mode);
600 set_array_entity_values(entity, tvs, slen);
603 dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
605 return create_symconst(dbgi, entity);
608 static ir_node *load_from_expression_addr(type_t *type, ir_node *addr,
611 ir_mode *mode = get_ir_mode(type);
612 ir_node *memory = get_store();
613 ir_node *load = new_d_Load(dbgi, memory, addr, mode);
614 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
615 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
621 static ir_node *deref_address(type_t *const type, ir_node *const addr,
622 dbg_info *const dbgi)
624 switch (type->type) {
626 case TYPE_COMPOUND_STRUCT:
627 case TYPE_COMPOUND_UNION:
631 return load_from_expression_addr(type, addr, dbgi);
635 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
637 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
638 declaration_t *declaration = ref->declaration;
639 type_t *type = skip_typeref(declaration->type);
641 switch((declaration_type_t) declaration->declaration_type) {
642 case DECLARATION_TYPE_UNKNOWN:
644 case DECLARATION_TYPE_LOCAL_VARIABLE: {
645 ir_mode *mode = get_ir_mode(type);
646 return get_value(declaration->v.value_number, mode);
648 case DECLARATION_TYPE_FUNCTION: {
649 return create_symconst(dbgi, declaration->v.entity);
651 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
652 ir_entity *entity = declaration->v.entity;
653 ir_node *symconst = create_symconst(dbgi, entity);
655 if(type->type == TYPE_ARRAY) {
658 return load_from_expression_addr(type, symconst, dbgi);
661 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
662 ir_entity *entity = declaration->v.entity;
663 ir_node *frame = get_irg_frame(current_ir_graph);
664 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
665 return deref_address(type, sel, dbgi);
668 case DECLARATION_TYPE_COMPOUND_MEMBER:
669 case DECLARATION_TYPE_LABEL_BLOCK:
670 panic("not implemented reference type");
673 panic("reference to declaration with unknown type found");
676 static ir_node *reference_addr(const reference_expression_t *ref)
678 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
679 declaration_t *declaration = ref->declaration;
681 switch((declaration_type_t) declaration->declaration_type) {
682 case DECLARATION_TYPE_UNKNOWN:
684 case DECLARATION_TYPE_LOCAL_VARIABLE:
685 panic("local variable without entity has no address");
686 case DECLARATION_TYPE_FUNCTION: {
687 return create_symconst(dbgi, declaration->v.entity);
689 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
690 ir_entity *entity = declaration->v.entity;
691 ir_node *symconst = create_symconst(dbgi, entity);
694 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
695 ir_entity *entity = declaration->v.entity;
696 ir_node *frame = get_irg_frame(current_ir_graph);
697 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
701 case DECLARATION_TYPE_COMPOUND_MEMBER:
702 case DECLARATION_TYPE_LABEL_BLOCK:
703 panic("not implemented reference type");
706 panic("reference to declaration with unknown type found");
709 static ir_node *call_expression_to_firm(const call_expression_t *call)
711 assert(get_cur_block() != NULL);
713 expression_t *function = call->function;
714 ir_node *callee = expression_to_firm(function);
716 assert(function->datatype->type == TYPE_FUNCTION);
717 function_type_t *function_type = (function_type_t*) function->datatype;
719 int n_parameters = 0;
720 call_argument_t *argument = call->arguments;
721 for( ; argument != NULL; argument = argument->next) {
725 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
726 ir_type *new_method_type = NULL;
727 if(function_type->variadic || function_type->unspecified_parameters) {
728 /* we need to construct a new method type matching the call
730 int n_res = get_method_n_ress(ir_method_type);
731 new_method_type = new_type_method(unique_ident("calltype"),
732 n_parameters, n_res);
733 set_method_calling_convention(new_method_type,
734 get_method_calling_convention(ir_method_type));
735 set_method_additional_properties(new_method_type,
736 get_method_additional_properties(ir_method_type));
738 for(int i = 0; i < n_res; ++i) {
739 set_method_res_type(new_method_type, i,
740 get_method_res_type(ir_method_type, i));
743 ir_node *in[n_parameters];
745 argument = call->arguments;
747 for( ; argument != NULL; argument = argument->next) {
748 expression_t *expression = argument->expression;
749 ir_node *arg_node = expression_to_firm(expression);
752 if(new_method_type != NULL) {
753 ir_type *irtype = get_ir_type(expression->datatype);
754 set_method_param_type(new_method_type, n, irtype);
759 assert(n == n_parameters);
761 if(new_method_type != NULL)
762 ir_method_type = new_method_type;
764 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
765 ir_node *store = get_store();
766 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
768 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
771 type_t *result_type = function_type->result_type;
772 ir_node *result = NULL;
773 if(result_type != type_void) {
774 ir_mode *mode = get_ir_mode(result_type);
775 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
776 result = new_d_Proj(dbgi, resproj, mode, 0);
782 static ir_node *expression_to_addr(const expression_t *expression);
784 static void set_value_for_expression(const expression_t *expression,
787 if(expression->type == EXPR_REFERENCE) {
788 reference_expression_t *ref = (reference_expression_t*) expression;
790 declaration_t *declaration = ref->declaration;
791 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
792 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
793 set_value(declaration->v.value_number, value);
798 dbg_info *dbgi = get_dbg_info(&expression->source_position);
799 ir_node *addr = expression_to_addr(expression);
800 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
801 ir_node *memory = get_store();
802 ir_node *store = new_d_Store(dbgi, memory, addr, value);
803 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
804 set_store(store_mem);
807 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
809 ir_mode *value_mode = get_irn_mode(value);
811 if(value_mode == dest_mode)
814 if(dest_mode == mode_b) {
815 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
816 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
817 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
821 return new_d_Conv(dbgi, value, dest_mode);
824 static ir_node *create_incdec(const unary_expression_t *expression)
826 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
827 type_t *type = expression->expression.datatype;
828 ir_mode *mode = get_ir_mode(type);
829 expression_t *value = expression->value;
831 ir_node *value_node = expression_to_firm(value);
834 if(type->type == TYPE_POINTER) {
835 pointer_type_t *pointer_type = (pointer_type_t*) type;
836 unsigned elem_size = get_type_size(pointer_type->points_to);
837 offset = new_Const_long(mode_Is, elem_size);
839 assert(is_type_arithmetic(type));
840 offset = new_Const(mode, get_mode_one(mode));
844 switch(expression->type) {
845 case UNEXPR_POSTFIX_INCREMENT: {
846 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
847 set_value_for_expression(value, new_value);
850 case UNEXPR_POSTFIX_DECREMENT: {
851 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
852 set_value_for_expression(value, new_value);
855 case UNEXPR_PREFIX_INCREMENT: {
856 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
857 set_value_for_expression(value, new_value);
860 case UNEXPR_PREFIX_DECREMENT: {
861 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
862 set_value_for_expression(value, new_value);
866 panic("no incdec expr in create_incdec");
872 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
874 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
875 type_t *type = expression->expression.datatype;
876 ir_mode *mode = get_ir_mode(type);
878 if(expression->type == UNEXPR_TAKE_ADDRESS)
879 return expression_to_addr(expression->value);
881 const expression_t *value = expression->value;
882 ir_node *value_node = expression_to_firm(value);
884 switch(expression->type) {
886 return new_d_Minus(dbgi, value_node, mode);
889 case UNEXPR_BITWISE_NEGATE:
890 return new_d_Not(dbgi, value_node, mode);
892 if(get_irn_mode(value_node) != mode_b) {
893 value_node = create_conv(dbgi, value_node, mode_b);
895 value_node = new_d_Not(dbgi, value_node, mode_b);
897 value_node = create_conv(dbgi, value_node, mode);
900 case UNEXPR_DEREFERENCE:
901 return load_from_expression_addr(type, value_node, dbgi);
902 case UNEXPR_POSTFIX_INCREMENT:
903 case UNEXPR_POSTFIX_DECREMENT:
904 case UNEXPR_PREFIX_INCREMENT:
905 case UNEXPR_PREFIX_DECREMENT:
906 return create_incdec(expression);
908 return create_conv(dbgi, value_node, mode);
910 case UNEXPR_TAKE_ADDRESS:
914 panic("invalid UNEXPR type found");
917 static long get_pnc(binary_expression_type_t type)
920 case BINEXPR_EQUAL: return pn_Cmp_Eq;
921 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
922 case BINEXPR_LESS: return pn_Cmp_Lt;
923 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
924 case BINEXPR_GREATER: return pn_Cmp_Gt;
925 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
929 panic("trying to get pn_Cmp from non-comparison binexpr type");
932 static ir_node *create_lazy_op(const binary_expression_t *expression)
934 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
936 bool is_or = (expression->type == BINEXPR_LOGICAL_OR);
937 assert(is_or || expression->type == BINEXPR_LOGICAL_AND);
939 ir_node *val1 = expression_to_modeb(expression->left);
940 ir_node *cond = new_d_Cond(dbgi, val1);
941 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
942 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
944 ir_node *fallthrough_block = new_immBlock();
947 ir_node *calc_val2_block = new_immBlock();
949 add_immBlock_pred(calc_val2_block, false_proj);
951 add_immBlock_pred(calc_val2_block, true_proj);
954 mature_immBlock(calc_val2_block);
956 ir_node *val2 = expression_to_modeb(expression->right);
957 if(get_cur_block() != NULL) {
958 ir_node *jmp = new_d_Jmp(dbgi);
959 add_immBlock_pred(fallthrough_block, jmp);
965 constb = new_d_Const(dbgi, mode_b, get_tarval_b_true());
966 add_immBlock_pred(fallthrough_block, true_proj);
968 constb = new_d_Const(dbgi, mode_b, get_tarval_b_false());
969 add_immBlock_pred(fallthrough_block, false_proj);
971 mature_immBlock(fallthrough_block);
973 set_cur_block(fallthrough_block);
975 ir_node *in[2] = { val2, constb };
976 ir_node *val = new_d_Phi(dbgi, 2, in, mode_b);
981 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
982 ir_node *right, ir_mode *mode);
984 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
985 create_arithmetic_func func)
987 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
988 ir_node *left = expression_to_firm(expression->left);
989 ir_node *right = expression_to_firm(expression->right);
990 type_t *type = expression->right->datatype;
991 /* be careful with the modes, because in asithmetic assign nodes only
992 * the right operand has the mode of the arithmetic alread */
993 ir_mode *mode = get_ir_mode(type);
994 left = create_conv(dbgi, left, mode);
995 ir_node *res = func(dbgi, left, right, mode);
1000 static ir_node *create_arithmetic_assign_binop(
1001 const binary_expression_t *expression, create_arithmetic_func func)
1003 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1004 ir_node *value = create_arithmetic_binop(expression, func);
1005 type_t *type = expression->expression.datatype;
1006 ir_mode *mode = get_ir_mode(type);
1008 assert(type->type != TYPE_POINTER);
1010 value = create_conv(dbgi, value, mode);
1011 set_value_for_expression(expression->left, value);
1016 static ir_node *create_add(const binary_expression_t *expression)
1018 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1019 ir_node *left = expression_to_firm(expression->left);
1020 ir_node *right = expression_to_firm(expression->right);
1021 type_t *type = expression->expression.datatype;
1022 ir_mode *mode = get_ir_mode(type);
1024 expression_t *expr_left = expression->left;
1025 expression_t *expr_right = expression->right;
1026 type_t *type_left = skip_typeref(expr_left->datatype);
1027 type_t *type_right = skip_typeref(expr_right->datatype);
1029 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1030 return new_d_Add(dbgi, left, right, mode);
1035 pointer_type_t *pointer_type;
1036 if(type_left->type == TYPE_POINTER) {
1039 pointer_type = (pointer_type_t*) type_left;
1041 assert(type_right->type == TYPE_POINTER);
1044 pointer_type = (pointer_type_t*) type_right;
1047 type_t *points_to = pointer_type->points_to;
1048 unsigned elem_size = get_type_size(points_to);
1050 assert(elem_size >= 1);
1052 integer = create_conv(dbgi, integer, mode_Is);
1053 ir_node *cnst = new_Const_long(mode_Is, (int) elem_size);
1054 ir_node *mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1058 ir_node *res = new_d_Add(dbgi, pointer, integer, mode);
1063 static ir_node *create_sub(const binary_expression_t *expression)
1065 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1066 ir_node *left = expression_to_firm(expression->left);
1067 ir_node *right = expression_to_firm(expression->right);
1068 type_t *type = expression->expression.datatype;
1069 ir_mode *mode = get_ir_mode(type);
1071 expression_t *expr_left = expression->left;
1072 expression_t *expr_right = expression->right;
1073 type_t *type_left = skip_typeref(expr_left->datatype);
1074 type_t *type_right = skip_typeref(expr_right->datatype);
1076 if((is_type_arithmetic(type_left) && is_type_arithmetic(type_right))
1077 || (type_left->type == TYPE_POINTER
1078 && type_right->type == TYPE_POINTER)) {
1079 return new_d_Sub(dbgi, left, right, mode);
1082 assert(type_right->type == TYPE_POINTER);
1083 ir_node *pointer = left;
1084 ir_node *integer = right;
1085 pointer_type_t *pointer_type = (pointer_type_t*) type_right;
1087 type_t *points_to = pointer_type->points_to;
1088 unsigned elem_size = get_type_size(points_to);
1090 assert(elem_size >= 1);
1092 ir_node *cnst = new_Const_long(mode_Iu, elem_size);
1093 ir_node *mul = new_d_Mul(dbgi, integer, cnst, mode_Iu);
1097 ir_node *res = new_d_Sub(dbgi, pointer, integer, mode);
1102 static ir_node *create_shift(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 type_t *type = expression->expression.datatype;
1108 ir_mode *mode = get_ir_mode(type);
1110 /* firm always wants the shift count to be unsigned */
1111 right = create_conv(dbgi, right, mode_Iu);
1115 switch(expression->type) {
1116 case BINEXPR_SHIFTLEFT:
1117 res = new_d_Shl(dbgi, left, right, mode);
1119 case BINEXPR_SHIFTRIGHT: {
1120 expression_t *expr_left = expression->left;
1121 type_t *type_left = skip_typeref(expr_left->datatype);
1123 if(is_type_signed(type_left)) {
1124 res = new_d_Shrs(dbgi, left, right, mode);
1126 res = new_d_Shr(dbgi, left, right, mode);
1131 panic("create shift op called for non-shift op");
1138 static ir_node *create_divmod(const binary_expression_t *expression)
1140 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1141 ir_node *left = expression_to_firm(expression->left);
1142 ir_node *right = expression_to_firm(expression->right);
1143 ir_node *pin = new_Pin(new_NoMem());
1144 type_t *type = expression->expression.datatype;
1145 ir_mode *mode = get_ir_mode(type);
1149 switch (expression->type) {
1151 case BINEXPR_DIV_ASSIGN:
1152 if(mode_is_float(mode)) {
1153 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1154 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1156 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1157 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1162 case BINEXPR_MOD_ASSIGN:
1163 assert(!mode_is_float(mode));
1164 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1165 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1168 default: panic("unexpected binary expression type in create_divmod()");
1174 static ir_node *create_arithmetic_assign_divmod(
1175 const binary_expression_t *expression)
1177 ir_node * value = create_divmod(expression);
1178 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1179 type_t *const type = expression->expression.datatype;
1180 ir_mode *const mode = get_ir_mode(type);
1182 assert(type->type != TYPE_POINTER);
1184 value = create_conv(dbgi, value, mode);
1185 set_value_for_expression(expression->left, value);
1191 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1193 binary_expression_type_t type = expression->type;
1196 case BINEXPR_NOTEQUAL:
1198 case BINEXPR_LESSEQUAL:
1199 case BINEXPR_GREATER:
1200 case BINEXPR_GREATEREQUAL: {
1201 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1202 ir_node *left = expression_to_firm(expression->left);
1203 ir_node *right = expression_to_firm(expression->right);
1204 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1205 long pnc = get_pnc(type);
1206 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1209 case BINEXPR_ASSIGN: {
1210 ir_node *right = expression_to_firm(expression->right);
1211 set_value_for_expression(expression->left, right);
1215 return create_add(expression);
1217 return create_sub(expression);
1219 return create_arithmetic_binop(expression, new_d_Mul);
1220 case BINEXPR_BITWISE_AND:
1221 return create_arithmetic_binop(expression, new_d_And);
1222 case BINEXPR_BITWISE_OR:
1223 return create_arithmetic_binop(expression, new_d_Or);
1224 case BINEXPR_BITWISE_XOR:
1225 return create_arithmetic_binop(expression, new_d_Eor);
1226 case BINEXPR_SHIFTLEFT:
1227 case BINEXPR_SHIFTRIGHT:
1228 return create_shift(expression);
1231 return create_divmod(expression);
1232 case BINEXPR_LOGICAL_AND:
1233 case BINEXPR_LOGICAL_OR:
1234 return create_lazy_op(expression);
1236 expression_to_firm(expression->left);
1237 return expression_to_firm(expression->right);
1238 case BINEXPR_ADD_ASSIGN:
1239 return create_arithmetic_assign_binop(expression, new_d_Add);
1240 case BINEXPR_SUB_ASSIGN:
1241 return create_arithmetic_assign_binop(expression, new_d_Sub);
1242 case BINEXPR_MUL_ASSIGN:
1243 return create_arithmetic_assign_binop(expression, new_d_Mul);
1244 case BINEXPR_DIV_ASSIGN:
1245 return create_arithmetic_assign_divmod(expression);
1246 case BINEXPR_BITWISE_AND_ASSIGN:
1247 return create_arithmetic_assign_binop(expression, new_d_And);
1248 case BINEXPR_BITWISE_OR_ASSIGN:
1249 return create_arithmetic_assign_binop(expression, new_d_Or);
1250 case BINEXPR_BITWISE_XOR_ASSIGN:
1251 return create_arithmetic_assign_binop(expression, new_d_Eor);
1252 case BINEXPR_SHIFTLEFT_ASSIGN:
1253 return create_arithmetic_assign_binop(expression, new_d_Shl);
1254 case BINEXPR_SHIFTRIGHT_ASSIGN:
1255 return create_arithmetic_assign_binop(expression, new_d_Shr);
1257 panic("TODO binexpr type");
1261 static ir_node *array_access_addr(const array_access_expression_t *expression)
1263 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1265 ir_node *base_addr = expression_to_firm(expression->array_ref);
1266 ir_node *offset = expression_to_firm(expression->index);
1267 offset = create_conv(dbgi, offset, mode_Iu);
1269 unsigned elem_size = get_type_size(expression->expression.datatype);
1270 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1271 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1273 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1278 static ir_node *array_access_to_firm(
1279 const array_access_expression_t *expression)
1281 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1282 ir_node *addr = array_access_addr(expression);
1283 type_t *type = expression->expression.datatype;
1284 return deref_address(type, addr, dbgi);
1287 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1289 type_t *type = expression->type;
1291 type = expression->size_expression->datatype;
1292 assert(type != NULL);
1295 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1296 unsigned size = get_type_size(type);
1297 ir_node *size_node = new_Const_long(mode, size);
1302 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1304 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1306 ir_node *condition = expression_to_modeb(expression->condition);
1307 ir_node *cond = new_d_Cond(dbgi, condition);
1308 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1309 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1311 /* create the true block */
1312 ir_node *true_block = new_immBlock();
1313 add_immBlock_pred(true_block, true_proj);
1314 mature_immBlock(true_block);
1316 ir_node *true_val = expression_to_firm(expression->true_expression);
1317 ir_node *true_jmp = new_Jmp();
1319 /* create the false block */
1320 ir_node *false_block = new_immBlock();
1321 add_immBlock_pred(false_block, false_proj);
1322 mature_immBlock(false_block);
1324 ir_node *false_val = expression_to_firm(expression->false_expression);
1325 ir_node *false_jmp = new_Jmp();
1327 /* create the common block */
1328 ir_node *common_block = new_immBlock();
1329 add_immBlock_pred(common_block, true_jmp);
1330 add_immBlock_pred(common_block, false_jmp);
1331 mature_immBlock(common_block);
1333 ir_node *in[2] = { true_val, false_val };
1334 ir_mode *mode = get_irn_mode(true_val);
1335 assert(get_irn_mode(false_val) == mode);
1336 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1341 static ir_node *select_addr(const select_expression_t *expression)
1343 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1345 ir_node *compound_addr = expression_to_firm(expression->compound);
1347 declaration_t *entry = expression->compound_entry;
1348 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1349 ir_entity *entity = entry->v.entity;
1351 assert(entity != NULL);
1353 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1358 static ir_node *select_to_firm(const select_expression_t *expression)
1360 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1361 ir_node *addr = select_addr(expression);
1362 type_t *type = expression->expression.datatype;
1364 return load_from_expression_addr(type, addr, dbgi);
1367 static ir_node *dereference_addr(const unary_expression_t *const expression)
1369 assert(expression->type == UNEXPR_DEREFERENCE);
1370 return expression_to_firm(expression->value);
1373 static ir_node *expression_to_addr(const expression_t *expression)
1375 switch(expression->type) {
1376 case EXPR_REFERENCE:
1377 return reference_addr((const reference_expression_t*) expression);
1378 case EXPR_ARRAY_ACCESS:
1379 return array_access_addr((const array_access_expression_t*) expression);
1381 return select_addr((const select_expression_t*) expression);
1383 const unary_expression_t *const unary_expr =
1384 (const unary_expression_t*)expression;
1385 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1386 return dereference_addr(unary_expr);
1393 panic("trying to get address of non-lvalue");
1396 static ir_node *_expression_to_firm(const expression_t *expression)
1398 switch(expression->type) {
1400 return const_to_firm((const const_t*) expression);
1401 case EXPR_STRING_LITERAL:
1402 return string_literal_to_firm((const string_literal_t*) expression);
1403 case EXPR_REFERENCE:
1404 return reference_expression_to_firm(
1405 (const reference_expression_t*) expression);
1407 return call_expression_to_firm((const call_expression_t*) expression);
1409 return unary_expression_to_firm((const unary_expression_t*) expression);
1411 return binary_expression_to_firm(
1412 (const binary_expression_t*) expression);
1413 case EXPR_ARRAY_ACCESS:
1414 return array_access_to_firm(
1415 (const array_access_expression_t*) expression);
1417 return sizeof_to_firm((const sizeof_expression_t*) expression);
1418 case EXPR_CONDITIONAL:
1419 return conditional_to_firm((const conditional_expression_t*)expression);
1421 return select_to_firm((const select_expression_t*) expression);
1424 case EXPR_PRETTY_FUNCTION:
1426 case EXPR_STATEMENT:
1427 case EXPR_BUILTIN_SYMBOL:
1428 panic("unimplemented expression found");
1434 panic("invalid expression found");
1437 static ir_node *expression_to_firm(const expression_t *expression)
1439 ir_node *res = _expression_to_firm(expression);
1441 if(res != NULL && get_irn_mode(res) == mode_b) {
1442 ir_mode *mode = get_ir_mode(expression->datatype);
1443 res = create_conv(NULL, res, mode);
1449 static ir_node *expression_to_modeb(const expression_t *expression)
1451 ir_node *res = _expression_to_firm(expression);
1452 res = create_conv(NULL, res, mode_b);
1457 static void statement_to_firm(statement_t *statement);
1459 static void return_statement_to_firm(return_statement_t *statement)
1461 if(get_cur_block() == NULL)
1464 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1467 if(statement->return_value != NULL) {
1468 ir_node *retval = expression_to_firm(statement->return_value);
1472 ret = new_d_Return(dbgi, get_store(), 1, in);
1474 ret = new_d_Return(dbgi, get_store(), 0, NULL);
1476 ir_node *end_block = get_irg_end_block(current_ir_graph);
1477 add_immBlock_pred(end_block, ret);
1479 set_cur_block(NULL);
1482 static void compound_statement_to_firm(compound_statement_t *compound)
1484 statement_t *statement = compound->statements;
1485 for( ; statement != NULL; statement = statement->next) {
1486 //context2firm(&statement->context);
1487 statement_to_firm(statement);
1491 static void expression_statement_to_firm(expression_statement_t *statement)
1493 if(get_cur_block() == NULL)
1496 expression_to_firm(statement->expression);
1499 static void if_statement_to_firm(if_statement_t *statement)
1501 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1502 ir_node *condition = expression_to_modeb(statement->condition);
1504 /* make sure we have a mode_b condition */
1505 ir_node *cond = new_d_Cond(dbgi, condition);
1506 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1507 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1509 ir_node *fallthrough_block = new_immBlock();
1511 /* the true (blocks) */
1512 ir_node *true_block = new_immBlock();
1513 add_immBlock_pred(true_block, true_proj);
1514 mature_immBlock(true_block);
1516 statement_to_firm(statement->true_statement);
1517 if(get_cur_block() != NULL) {
1518 ir_node *jmp = new_Jmp();
1519 add_immBlock_pred(fallthrough_block, jmp);
1522 /* the false (blocks) */
1523 if(statement->false_statement != NULL) {
1524 ir_node *false_block = new_immBlock();
1525 add_immBlock_pred(false_block, false_proj);
1526 mature_immBlock(false_block);
1528 statement_to_firm(statement->false_statement);
1529 if(get_cur_block() != NULL) {
1530 ir_node *jmp = new_Jmp();
1531 add_immBlock_pred(fallthrough_block, jmp);
1534 add_immBlock_pred(fallthrough_block, false_proj);
1536 mature_immBlock(fallthrough_block);
1538 set_cur_block(fallthrough_block);
1541 static void while_statement_to_firm(while_statement_t *statement)
1543 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1545 ir_node *jmp = NULL;
1546 if(get_cur_block() != NULL) {
1550 /* create the header block */
1551 ir_node *header_block = new_immBlock();
1553 add_immBlock_pred(header_block, jmp);
1556 /* create the condition */
1557 ir_node *condition = expression_to_modeb(statement->condition);
1558 ir_node *cond = new_d_Cond(dbgi, condition);
1559 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1560 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1562 /* the false block */
1563 ir_node *false_block = new_immBlock();
1564 add_immBlock_pred(false_block, false_proj);
1567 ir_node *body_block = new_immBlock();
1568 add_immBlock_pred(body_block, true_proj);
1569 mature_immBlock(body_block);
1571 ir_node *old_continue_label = continue_label;
1572 ir_node *old_break_label = break_label;
1573 continue_label = header_block;
1574 break_label = false_block;
1576 statement_to_firm(statement->body);
1578 assert(continue_label == header_block);
1579 assert(break_label == false_block);
1580 continue_label = old_continue_label;
1581 break_label = old_break_label;
1583 if(get_cur_block() != NULL) {
1584 ir_node *jmp = new_Jmp();
1585 add_immBlock_pred(header_block, jmp);
1588 mature_immBlock(header_block);
1589 mature_immBlock(false_block);
1591 set_cur_block(false_block);
1594 static void do_while_statement_to_firm(do_while_statement_t *statement)
1596 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1598 ir_node *jmp = NULL;
1599 if(get_cur_block() != NULL) {
1603 /* create the header block */
1604 ir_node *header_block = new_immBlock();
1606 /* the false block */
1607 ir_node *false_block = new_immBlock();
1610 ir_node *body_block = new_immBlock();
1612 add_immBlock_pred(body_block, jmp);
1615 ir_node *old_continue_label = continue_label;
1616 ir_node *old_break_label = break_label;
1617 continue_label = header_block;
1618 break_label = false_block;
1620 statement_to_firm(statement->body);
1622 assert(continue_label == header_block);
1623 assert(break_label == false_block);
1624 continue_label = old_continue_label;
1625 break_label = old_break_label;
1627 if(get_cur_block() == NULL) {
1628 mature_immBlock(header_block);
1629 mature_immBlock(body_block);
1630 mature_immBlock(false_block);
1634 ir_node *body_jmp = new_Jmp();
1635 add_immBlock_pred(header_block, body_jmp);
1636 mature_immBlock(header_block);
1638 /* create the condition */
1639 set_cur_block(header_block);
1640 ir_node *condition = expression_to_modeb(statement->condition);
1641 ir_node *cond = new_d_Cond(dbgi, condition);
1642 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1643 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1645 add_immBlock_pred(body_block, true_proj);
1646 mature_immBlock(body_block);
1648 add_immBlock_pred(false_block, false_proj);
1649 mature_immBlock(false_block);
1651 set_cur_block(false_block);
1654 static void for_statement_to_firm(for_statement_t *statement)
1656 dbg_info *const dbgi = get_dbg_info(&statement->statement.source_position);
1658 ir_node *jmp = NULL;
1659 if (get_cur_block() != NULL) {
1660 if(statement->initialisation != NULL) {
1661 expression_to_firm(statement->initialisation);
1666 /* create the step block */
1667 ir_node *const step_block = new_immBlock();
1668 if (statement->step != NULL) {
1669 expression_to_firm(statement->step);
1671 ir_node *const step_jmp = new_Jmp();
1673 /* create the header block */
1674 ir_node *const header_block = new_immBlock();
1676 add_immBlock_pred(header_block, jmp);
1678 add_immBlock_pred(header_block, step_jmp);
1680 /* create the condition */
1682 ir_node *false_proj;
1683 if (statement->condition != NULL) {
1684 ir_node *const condition = expression_to_modeb(statement->condition);
1685 ir_node *const cond = new_d_Cond(dbgi, condition);
1686 true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1687 false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1689 keep_alive(header_block);
1690 true_proj = new_Jmp();
1694 /* the false block */
1695 ir_node *const false_block = new_immBlock();
1696 if (false_proj != NULL) {
1697 add_immBlock_pred(false_block, false_proj);
1701 ir_node *const body_block = new_immBlock();
1702 add_immBlock_pred(body_block, true_proj);
1703 mature_immBlock(body_block);
1705 ir_node *const old_continue_label = continue_label;
1706 ir_node *const old_break_label = break_label;
1707 continue_label = step_block;
1708 break_label = false_block;
1710 statement_to_firm(statement->body);
1712 assert(continue_label == step_block);
1713 assert(break_label == false_block);
1714 continue_label = old_continue_label;
1715 break_label = old_break_label;
1717 if (get_cur_block() != NULL) {
1718 ir_node *const jmp = new_Jmp();
1719 add_immBlock_pred(step_block, jmp);
1722 mature_immBlock(step_block);
1723 mature_immBlock(header_block);
1724 mature_immBlock(false_block);
1726 set_cur_block(false_block);
1729 static void create_declaration_entity(declaration_t *declaration,
1730 declaration_type_t declaration_type,
1731 ir_type *parent_type)
1733 ident *id = new_id_from_str(declaration->symbol->string);
1734 ir_type *irtype = get_ir_type(declaration->type);
1735 ir_entity *entity = new_entity(parent_type, id, irtype);
1736 set_entity_ld_ident(entity, id);
1738 declaration->declaration_type = declaration_type;
1739 declaration->v.entity = entity;
1740 set_entity_variability(entity, variability_uninitialized);
1741 /* TODO: visibility? */
1744 static void create_initializer(declaration_t *declaration)
1746 initializer_t *initializer = declaration->init.initializer;
1747 if(initializer == NULL)
1750 if(initializer->type == INITIALIZER_VALUE) {
1751 assert(initializer->designator == NULL);
1752 assert(initializer->next == NULL);
1753 ir_node *init_node = expression_to_firm(initializer->v.value);
1755 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1756 set_value(declaration->v.value_number, init_node);
1758 ir_entity *entity = declaration->v.entity;
1760 set_entity_variability(entity, variability_initialized);
1761 set_atomic_ent_value(entity, init_node);
1764 assert(initializer->type == INITIALIZER_LIST);
1765 panic("list initializer not supported yet");
1769 static void create_local_variable(declaration_t *declaration)
1771 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1773 bool needs_entity = declaration->address_taken;
1774 type_t *type = skip_typeref(declaration->type);
1776 if(type->type == TYPE_ARRAY
1777 || type->type == TYPE_COMPOUND_STRUCT
1778 || type->type == TYPE_COMPOUND_UNION) {
1779 needs_entity = true;
1783 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1784 create_declaration_entity(declaration,
1785 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1788 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1789 declaration->v.value_number = next_value_number_function;
1790 ++next_value_number_function;
1793 create_initializer(declaration);
1796 static void declaration_statement_to_firm(declaration_statement_t *statement)
1798 declaration_t *declaration = statement->declarations_begin;
1799 declaration_t *end = statement->declarations_end->next;
1800 for( ; declaration != end; declaration = declaration->next) {
1801 type_t *type = declaration->type;
1803 switch(declaration->storage_class) {
1804 case STORAGE_CLASS_TYPEDEF:
1806 case STORAGE_CLASS_STATIC:
1807 panic("static local vars not implemented yet");
1808 case STORAGE_CLASS_ENUM_ENTRY:
1809 panic("enum entry declaration in local block found");
1810 case STORAGE_CLASS_EXTERN:
1811 panic("extern declaration in local block found");
1812 case STORAGE_CLASS_NONE:
1813 case STORAGE_CLASS_AUTO:
1814 case STORAGE_CLASS_REGISTER:
1815 if(type->type == TYPE_FUNCTION) {
1816 panic("nested functions not supported yet");
1818 create_local_variable(declaration);
1822 panic("invalid storage class found");
1826 static void create_jump_statement(const statement_t *statement,
1827 ir_node *target_block)
1829 if(get_cur_block() == NULL)
1832 dbg_info *dbgi = get_dbg_info(&statement->source_position);
1833 ir_node *jump = new_d_Jmp(dbgi);
1834 add_immBlock_pred(target_block, jump);
1836 set_cur_block(NULL);
1839 static void switch_statement_to_firm(const switch_statement_t *statement)
1841 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1843 ir_node *expression = expression_to_firm(statement->expression);
1844 ir_node *cond = new_d_Cond(dbgi, expression);
1845 ir_node *break_block = new_immBlock();
1847 set_cur_block(NULL);
1849 ir_node *const old_switch_cond = current_switch_cond;
1850 ir_node *const old_break_label = break_label;
1851 const bool old_saw_default_label = saw_default_label;
1852 current_switch_cond = cond;
1853 break_label = break_block;
1855 statement_to_firm(statement->body);
1857 if(get_cur_block() != NULL) {
1858 ir_node *jmp = new_Jmp();
1859 add_immBlock_pred(break_block, jmp);
1862 if (!saw_default_label) {
1863 set_cur_block(get_nodes_block(cond));
1864 ir_node *const proj = new_d_defaultProj(dbgi, cond,
1865 MAGIC_DEFAULT_PN_NUMBER);
1866 add_immBlock_pred(break_block, proj);
1869 assert(current_switch_cond == cond);
1870 assert(break_label == break_block);
1871 current_switch_cond = old_switch_cond;
1872 break_label = old_break_label;
1873 saw_default_label = old_saw_default_label;
1875 mature_immBlock(break_block);
1876 set_cur_block(break_block);
1879 static long fold_constant(const expression_t *expression)
1881 ir_graph *old_current_ir_graph = current_ir_graph;
1882 current_ir_graph = get_const_code_irg();
1884 ir_node *cnst = expression_to_firm(expression);
1885 if(!is_Const(cnst)) {
1886 panic("couldn't fold constantl");
1888 tarval *tv = get_Const_tarval(cnst);
1889 if(!tarval_is_long(tv)) {
1890 panic("folded constant not an integer");
1893 long res = get_tarval_long(tv);
1895 current_ir_graph = old_current_ir_graph;
1899 static void case_label_to_firm(const case_label_statement_t *statement)
1901 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1903 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
1905 /* let's create a node and hope firm constant folding creates a Const
1908 set_cur_block(get_nodes_block(current_switch_cond));
1909 if(statement->expression) {
1910 long pn = fold_constant(statement->expression);
1911 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
1912 /* oops someone detected our cheating... */
1913 panic("magic default pn used");
1915 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
1917 saw_default_label = true;
1918 proj = new_d_defaultProj(dbgi, current_switch_cond,
1919 MAGIC_DEFAULT_PN_NUMBER);
1922 ir_node *block = new_immBlock();
1923 if (fallthrough != NULL) {
1924 add_immBlock_pred(block, fallthrough);
1926 add_immBlock_pred(block, proj);
1927 mature_immBlock(block);
1930 static ir_node *get_label_block(declaration_t *label)
1932 assert(label->namespace == NAMESPACE_LABEL);
1934 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
1935 return label->v.block;
1937 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
1939 ir_node *old_cur_block = get_cur_block();
1940 ir_node *block = new_immBlock();
1941 set_cur_block(old_cur_block);
1943 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
1944 label->v.block = block;
1946 ARR_APP1(ir_node *, imature_blocks, block);
1951 static void label_to_firm(const label_statement_t *statement)
1953 ir_node *block = get_label_block(statement->label);
1955 if(get_cur_block() != NULL) {
1956 ir_node *jmp = new_Jmp();
1957 add_immBlock_pred(block, jmp);
1960 set_cur_block(block);
1963 statement_to_firm(statement->label_statement);
1966 static void goto_to_firm(const goto_statement_t *statement)
1968 if(get_cur_block() == NULL)
1971 ir_node *block = get_label_block(statement->label);
1972 ir_node *jmp = new_Jmp();
1973 add_immBlock_pred(block, jmp);
1975 set_cur_block(NULL);
1978 static void statement_to_firm(statement_t *statement)
1980 switch(statement->type) {
1981 case STATEMENT_COMPOUND:
1982 compound_statement_to_firm((compound_statement_t*) statement);
1984 case STATEMENT_RETURN:
1985 return_statement_to_firm((return_statement_t*) statement);
1987 case STATEMENT_EXPRESSION:
1988 expression_statement_to_firm((expression_statement_t*) statement);
1991 if_statement_to_firm((if_statement_t*) statement);
1993 case STATEMENT_WHILE:
1994 while_statement_to_firm((while_statement_t*) statement);
1996 case STATEMENT_DO_WHILE:
1997 do_while_statement_to_firm((do_while_statement_t*) statement);
1999 case STATEMENT_DECLARATION:
2000 declaration_statement_to_firm((declaration_statement_t*) statement);
2002 case STATEMENT_BREAK:
2003 create_jump_statement(statement, break_label);
2005 case STATEMENT_CONTINUE:
2006 create_jump_statement(statement, continue_label);
2008 case STATEMENT_SWITCH:
2009 switch_statement_to_firm((switch_statement_t*) statement);
2011 case STATEMENT_CASE_LABEL:
2012 case_label_to_firm((case_label_statement_t*) statement);
2015 for_statement_to_firm((for_statement_t*) statement);
2017 case STATEMENT_LABEL:
2018 label_to_firm((label_statement_t*) statement);
2020 case STATEMENT_GOTO:
2021 goto_to_firm((goto_statement_t*) statement);
2026 panic("Statement not implemented\n");
2029 static int get_function_n_local_vars(declaration_t *declaration)
2036 static void initialize_function_parameters(declaration_t *declaration)
2038 ir_graph *irg = current_ir_graph;
2039 ir_node *args = get_irg_args(irg);
2040 ir_node *start_block = get_irg_start_block(irg);
2043 declaration_t *parameter = declaration->context.declarations;
2044 for( ; parameter != NULL; parameter = parameter->next) {
2045 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2046 type_t *type = parameter->type;
2048 bool needs_entity = parameter->address_taken;
2049 if(type->type == TYPE_COMPOUND_STRUCT
2050 || type->type == TYPE_COMPOUND_UNION) {
2051 needs_entity = true;
2055 panic("entities for function parameters not implemented yet");
2058 ir_mode *mode = get_ir_mode(parameter->type);
2060 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2063 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2064 parameter->v.value_number = next_value_number_function;
2065 ++next_value_number_function;
2067 set_value(parameter->v.value_number, proj);
2071 static void create_function(declaration_t *declaration)
2073 ir_entity *entity = get_function_entity(declaration);
2075 if(declaration->init.statement == NULL)
2078 assert(imature_blocks == NULL);
2079 imature_blocks = NEW_ARR_F(ir_node*, 0);
2081 int n_local_vars = get_function_n_local_vars(declaration);
2082 ir_graph *irg = new_ir_graph(entity, n_local_vars);
2083 ir_node *first_block = get_cur_block();
2085 next_value_number_function = 0;
2086 initialize_function_parameters(declaration);
2088 statement_to_firm(declaration->init.statement);
2090 ir_node *end_block = get_irg_end_block(irg);
2092 /* do we have a return statement yet? */
2093 if(get_cur_block() != NULL) {
2094 assert(declaration->type->type == TYPE_FUNCTION);
2095 const function_type_t* const func_type
2096 = (const function_type_t*) declaration->type;
2098 if (func_type->result_type == type_void) {
2099 ret = new_Return(get_store(), 0, NULL);
2101 ir_mode *const mode = get_ir_mode(func_type->result_type);
2103 // ยง5.1.2.2.3 main implicitly returns 0
2104 if (strcmp(declaration->symbol->string, "main") == 0) {
2105 in[0] = new_Const(mode, get_mode_null(mode));
2107 in[0] = new_Unknown(mode);
2109 ret = new_Return(get_store(), 1, in);
2111 add_immBlock_pred(end_block, ret);
2114 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2115 mature_immBlock(imature_blocks[i]);
2117 DEL_ARR_F(imature_blocks);
2118 imature_blocks = NULL;
2120 mature_immBlock(first_block);
2121 mature_immBlock(end_block);
2123 irg_finalize_cons(irg);
2125 /* finalize the frame type */
2126 ir_type *frame_type = get_irg_frame_type(irg);
2127 int n = get_compound_n_members(frame_type);
2130 for(int i = 0; i < n; ++i) {
2131 ir_entity *entity = get_compound_member(frame_type, i);
2132 ir_type *entity_type = get_entity_type(entity);
2134 int align = get_type_alignment_bytes(entity_type);
2135 if(align > align_all)
2139 misalign = offset % align;
2143 set_entity_offset(entity, offset);
2144 offset += get_type_size_bytes(entity_type);
2146 set_type_size_bytes(frame_type, offset);
2147 set_type_alignment_bytes(frame_type, align_all);
2148 set_type_state(frame_type, layout_fixed);
2153 static void create_global_variable(declaration_t *declaration)
2155 ir_type *global_type = get_glob_type();
2156 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2159 ir_entity *entity = declaration->v.entity;
2160 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2161 set_entity_visibility(entity, visibility_local);
2162 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2163 set_entity_visibility(entity, visibility_external_allocated);
2165 set_entity_visibility(entity, visibility_external_visible);
2167 current_ir_graph = get_const_code_irg();
2168 create_initializer(declaration);
2171 static void context_to_firm(context_t *context)
2173 declaration_t *declaration = context->declarations;
2174 for( ; declaration != NULL; declaration = declaration->next) {
2175 if(declaration->namespace != NAMESPACE_NORMAL)
2177 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2178 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2180 if(declaration->symbol == NULL)
2183 type_t *type = declaration->type;
2184 if(type->type == TYPE_FUNCTION) {
2185 create_function(declaration);
2187 create_global_variable(declaration);
2192 void translation_unit_to_firm(translation_unit_t *unit)
2194 /* remove me later TODO FIXME */
2195 (void) get_type_size;
2197 /* just to be sure */
2198 continue_label = NULL;
2200 current_switch_cond = NULL;
2202 context_to_firm(& unit->context);