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 symbol_t *symbol_alloca;
33 static int next_value_number_function;
34 static ir_node *continue_label;
35 static ir_node *break_label;
36 static ir_node *current_switch_cond;
37 static bool saw_default_label;
38 static ir_node **imature_blocks;
40 static const declaration_t *current_function_decl;
41 static ir_node *current_function_name;
43 typedef enum declaration_type_t {
44 DECLARATION_TYPE_UNKNOWN,
45 DECLARATION_TYPE_FUNCTION,
46 DECLARATION_TYPE_GLOBAL_VARIABLE,
47 DECLARATION_TYPE_LOCAL_VARIABLE,
48 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
49 DECLARATION_TYPE_COMPOUND_MEMBER,
50 DECLARATION_TYPE_LABEL_BLOCK,
51 DECLARATION_TYPE_ENUM_ENTRY
54 static ir_type *get_ir_type(type_t *type);
56 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
60 const declaration_t *declaration = & value_numbers[pos]->declaration;
62 print_warning_prefix(declaration->source_position);
63 fprintf(stderr, "variable '%s' might be used uninitialized\n",
64 declaration->symbol->string);
66 fprintf(stderr, "Some variable might be used uninitialized\n");
67 return new_r_Unknown(irg, mode);
70 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
72 const source_position_t *pos = (const source_position_t*) dbg;
75 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
79 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
81 const source_position_t *pos = (const source_position_t*) dbg;
86 return pos->input_name;
89 void init_ast2firm(void)
91 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
92 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
93 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
95 ir_type_int = get_ir_type(type_int);
96 ir_type_const_char = get_ir_type(type_const_char);
97 ir_type_void = get_ir_type(type_int); /* we don't have a real void
99 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
100 ir_type_void, mode_P_data);
102 type_void->firm_type = ir_type_void;
104 symbol_alloca = symbol_table_insert("__builtin_alloca");
107 void exit_ast2firm(void)
111 static unsigned unique_id = 0;
113 static ident *unique_ident(const char *tag)
117 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
119 return new_id_from_str(buf);
122 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
124 switch(atomic_type->atype) {
125 case ATOMIC_TYPE_SCHAR:
126 case ATOMIC_TYPE_CHAR:
128 case ATOMIC_TYPE_UCHAR:
130 case ATOMIC_TYPE_SHORT:
132 case ATOMIC_TYPE_USHORT:
134 case ATOMIC_TYPE_LONG:
135 case ATOMIC_TYPE_INT:
137 case ATOMIC_TYPE_ULONG:
138 case ATOMIC_TYPE_UINT:
140 case ATOMIC_TYPE_LONGLONG:
142 case ATOMIC_TYPE_ULONGLONG:
144 case ATOMIC_TYPE_FLOAT:
146 case ATOMIC_TYPE_DOUBLE:
148 case ATOMIC_TYPE_LONG_DOUBLE:
150 case ATOMIC_TYPE_BOOL:
152 #ifdef PROVIDE_COMPLEX
153 case ATOMIC_TYPE_FLOAT_COMPLEX:
154 case ATOMIC_TYPE_DOUBLE_COMPLEX:
155 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
156 panic("complex lowering not implemented yet");
158 case ATOMIC_TYPE_FLOAT_IMAGINARY:
159 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
160 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
161 panic("imaginary lowering not implemented yet");
164 case ATOMIC_TYPE_VOID:
165 /* firm has no real void... */
167 case ATOMIC_TYPE_INVALID:
170 panic("Encountered unknown atomic type");
174 static unsigned get_type_size(type_t *type);
176 static unsigned get_atomic_type_size(const atomic_type_t *type)
178 switch(type->atype) {
179 case ATOMIC_TYPE_CHAR:
180 case ATOMIC_TYPE_SCHAR:
181 case ATOMIC_TYPE_UCHAR:
184 case ATOMIC_TYPE_SHORT:
185 case ATOMIC_TYPE_USHORT:
188 case ATOMIC_TYPE_BOOL:
189 case ATOMIC_TYPE_INT:
190 case ATOMIC_TYPE_UINT:
191 case ATOMIC_TYPE_LONG:
192 case ATOMIC_TYPE_ULONG:
193 case ATOMIC_TYPE_FLOAT:
196 case ATOMIC_TYPE_LONGLONG:
197 case ATOMIC_TYPE_ULONGLONG:
198 case ATOMIC_TYPE_DOUBLE:
201 case ATOMIC_TYPE_LONG_DOUBLE:
204 case ATOMIC_TYPE_VOID:
207 case ATOMIC_TYPE_INVALID:
210 panic("Trying to determine size of invalid atomic type");
213 static unsigned get_compound_type_size(compound_type_t *type)
215 ir_type *irtype = get_ir_type(&type->type);
216 return get_type_size_bytes(irtype);
219 static unsigned get_array_type_size(array_type_t *type)
221 ir_type *irtype = get_ir_type(&type->type);
222 return get_type_size_bytes(irtype);
225 static unsigned get_type_size(type_t *type)
227 type = skip_typeref(type);
231 return get_atomic_type_size((const atomic_type_t*) type);
233 return get_mode_size_bytes(mode_Is);
234 case TYPE_COMPOUND_UNION:
235 case TYPE_COMPOUND_STRUCT:
236 return get_compound_type_size((compound_type_t*) type);
238 /* just a pointer to the function */
239 return get_mode_size_bytes(mode_P_code);
241 return get_mode_size_bytes(mode_P_data);
243 return get_array_type_size((array_type_t*) type);
250 panic("Trying to determine size of invalid type");
253 static unsigned count_parameters(const function_type_t *function_type)
257 function_parameter_t *parameter = function_type->parameters;
258 for ( ; parameter != NULL; parameter = parameter->next) {
268 static long fold_constant(const expression_t *expression);
270 static ir_type *create_atomic_type(const atomic_type_t *type)
272 ir_mode *mode = get_atomic_mode(type);
273 ident *id = get_mode_ident(mode);
274 ir_type *irtype = new_type_primitive(id, mode);
279 static ir_type *create_method_type(const function_type_t *function_type)
281 type_t *result_type = function_type->result_type;
283 ident *id = unique_ident("functiontype");
284 int n_parameters = count_parameters(function_type);
285 int n_results = result_type == type_void ? 0 : 1;
286 ir_type *irtype = new_type_method(id, n_parameters, n_results);
288 if(result_type != type_void) {
289 ir_type *restype = get_ir_type(result_type);
290 set_method_res_type(irtype, 0, restype);
293 function_parameter_t *parameter = function_type->parameters;
295 for( ; parameter != NULL; parameter = parameter->next) {
296 ir_type *p_irtype = get_ir_type(parameter->type);
297 set_method_param_type(irtype, n, p_irtype);
301 if(function_type->variadic || function_type->unspecified_parameters) {
302 set_method_variadicity(irtype, variadicity_variadic);
308 static ir_type *create_pointer_type(pointer_type_t *type)
310 type_t *points_to = type->points_to;
311 ir_type *ir_points_to;
312 /* Avoid endless recursion if the points_to type contains this poiner type
313 * again (might be a struct). We therefore first create a void* pointer
314 * and then set the real points_to type
316 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
317 ir_type_void, mode_P_data);
318 type->type.firm_type = ir_type;
320 ir_points_to = get_ir_type(points_to);
321 set_pointer_points_to_type(ir_type, ir_points_to);
326 static ir_type *create_array_type(array_type_t *type)
328 type_t *element_type = type->element_type;
329 ir_type *ir_element_type = get_ir_type(element_type);
331 ident *id = unique_ident("array");
332 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
334 if(type->size != NULL) {
335 int n_elements = fold_constant(type->size);
337 set_array_bounds_int(ir_type, 0, 0, n_elements);
339 size_t elemsize = get_type_size_bytes(ir_element_type);
340 int align = get_type_alignment_bytes(ir_element_type);
341 if(elemsize % align > 0) {
342 elemsize += align - (elemsize % align);
344 set_type_size_bytes(ir_type, n_elements * elemsize);
345 set_type_alignment_bytes(ir_type, align);
346 set_type_state(ir_type, layout_fixed);
352 #define INVALID_TYPE ((ir_type_ptr)-1)
354 static ir_type *create_struct_type(compound_type_t *type)
356 symbol_t *symbol = type->declaration->symbol;
359 id = unique_ident(symbol->string);
361 id = unique_ident("__anonymous_struct");
363 ir_type *ir_type = new_type_struct(id);
365 type->type.firm_type = ir_type;
369 declaration_t *entry = type->declaration->context.declarations;
370 for( ; entry != NULL; entry = entry->next) {
371 if(entry->namespc != NAMESPACE_NORMAL)
374 ident *ident = new_id_from_str(entry->symbol->string);
375 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
377 int entry_size = get_type_size_bytes(entry_ir_type);
378 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
379 int misalign = offset % entry_alignment;
381 offset += entry_alignment - misalign;
383 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
384 set_entity_offset(entity, offset);
385 add_struct_member(ir_type, entity);
386 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
387 entry->v.entity = entity;
389 offset += entry_size;
390 if(entry_alignment > align_all) {
391 if(entry_alignment % align_all != 0) {
392 panic("Uneven alignments not supported yet");
394 align_all = entry_alignment;
398 int misalign = offset % align_all;
400 set_type_alignment_bytes(ir_type, align_all);
401 set_type_size_bytes(ir_type, offset);
402 set_type_state(ir_type, layout_fixed);
407 static ir_type *create_union_type(compound_type_t *type)
409 declaration_t *declaration = type->declaration;
410 symbol_t *symbol = declaration->symbol;
413 id = unique_ident(symbol->string);
415 id = unique_ident("__anonymous_union");
417 ir_type *ir_type = new_type_union(id);
419 type->type.firm_type = ir_type;
423 declaration_t *entry = declaration->context.declarations;
424 for( ; entry != NULL; entry = entry->next) {
425 if(entry->namespc != NAMESPACE_NORMAL)
428 ident *ident = new_id_from_str(entry->symbol->string);
429 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
431 int entry_size = get_type_size_bytes(entry_ir_type);
432 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
434 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
435 add_union_member(ir_type, entity);
436 set_entity_offset(entity, 0);
437 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
438 entry->v.entity = entity;
440 if(entry_size > size) {
443 if(entry_alignment > align_all) {
444 if(entry_alignment % align_all != 0) {
445 panic("Uneven alignments not supported yet");
447 align_all = entry_alignment;
451 set_type_alignment_bytes(ir_type, align_all);
452 set_type_size_bytes(ir_type, size);
453 set_type_state(ir_type, layout_fixed);
458 static ir_node *expression_to_firm(const expression_t *expression);
459 static inline ir_mode *get_ir_mode(type_t *type);
461 static ir_type *create_enum_type(enum_type_t *const type)
463 type->type.firm_type = ir_type_int;
465 ir_mode *const mode = get_ir_mode((type_t*) type);
466 tarval *const one = get_mode_one(mode);
467 tarval * tv_next = get_tarval_null(mode);
469 declaration_t *declaration = type->declaration->next;
470 for (; declaration != NULL; declaration = declaration->next) {
471 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
474 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
476 expression_t *const init = declaration->init.enum_value;
478 ir_node *const cnst = expression_to_firm(init);
479 if (!is_Const(cnst)) {
480 panic("couldn't fold constant");
482 tv_next = get_Const_tarval(cnst);
484 declaration->v.enum_val = tv_next;
485 tv_next = tarval_add(tv_next, one);
491 static ir_type *get_ir_type(type_t *type)
493 assert(type != NULL);
495 type = skip_typeref(type);
497 if(type->firm_type != NULL) {
498 assert(type->firm_type != INVALID_TYPE);
499 return type->firm_type;
502 ir_type *firm_type = NULL;
505 firm_type = create_atomic_type((atomic_type_t*) type);
508 firm_type = create_method_type((function_type_t*) type);
511 firm_type = create_pointer_type((pointer_type_t*) type);
514 firm_type = create_array_type((array_type_t*) type);
516 case TYPE_COMPOUND_STRUCT:
517 firm_type = create_struct_type((compound_type_t*) type);
519 case TYPE_COMPOUND_UNION:
520 firm_type = create_union_type((compound_type_t*) type);
523 firm_type = create_enum_type((enum_type_t*) type);
531 if(firm_type == NULL)
532 panic("unknown type found");
534 type->firm_type = firm_type;
538 static inline ir_mode *get_ir_mode(type_t *type)
540 ir_type *irtype = get_ir_type(type);
542 /* firm doesn't report a mode for arrays somehow... */
543 if(is_Array_type(irtype)) {
547 ir_mode *mode = get_type_mode(irtype);
548 assert(mode != NULL);
552 static ir_entity* get_function_entity(declaration_t *declaration)
554 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
555 return declaration->v.entity;
556 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
558 symbol_t *symbol = declaration->symbol;
559 ident *id = new_id_from_str(symbol->string);
561 ir_type *global_type = get_glob_type();
562 ir_type *ir_type_method = get_ir_type(declaration->type);
563 assert(is_Method_type(ir_type_method));
565 ir_entity *entity = new_entity(global_type, id, ir_type_method);
566 set_entity_ld_ident(entity, id);
567 if(declaration->storage_class == STORAGE_CLASS_STATIC
568 || declaration->is_inline) {
569 set_entity_visibility(entity, visibility_local);
570 } else if(declaration->init.statement != NULL) {
571 set_entity_visibility(entity, visibility_external_visible);
573 set_entity_visibility(entity, visibility_external_allocated);
576 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
577 declaration->v.entity = entity;
582 static dbg_info *get_dbg_info(const source_position_t *pos)
584 return (dbg_info*) pos;
587 static ir_node *const_to_firm(const const_t *cnst)
589 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
590 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
595 if(mode_is_float(mode)) {
596 tv = new_tarval_from_double(cnst->v.float_value, mode);
598 if(mode_is_signed(mode)) {
599 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
601 len = snprintf(buf, sizeof(buf), "%llu", cnst->v.int_value);
603 tv = new_tarval_from_str(buf, len, mode);
606 return new_d_Const(dbgi, mode, tv);
609 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
611 assert(entity != NULL);
612 union symconst_symbol sym;
613 sym.entity_p = entity;
614 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
617 static ir_node *string_to_firm(const source_position_t *const src_pos,
618 const char *const id_prefix,
619 const char *const string)
621 ir_type *const global_type = get_glob_type();
622 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
625 ident *const id = unique_ident(id_prefix);
626 ir_entity *const entity = new_entity(global_type, id, type);
627 set_entity_ld_ident(entity, id);
628 set_entity_variability(entity, variability_constant);
630 ir_type *const elem_type = ir_type_const_char;
631 ir_mode *const mode = get_type_mode(elem_type);
633 const size_t slen = strlen(string) + 1;
635 set_array_lower_bound_int(type, 0, 0);
636 set_array_upper_bound_int(type, 0, slen);
637 set_type_size_bytes(type, slen);
638 set_type_state(type, layout_fixed);
640 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
641 for(size_t i = 0; i < slen; ++i) {
642 tvs[i] = new_tarval_from_long(string[i], mode);
645 set_array_entity_values(entity, tvs, slen);
648 dbg_info *const dbgi = get_dbg_info(src_pos);
650 return create_symconst(dbgi, entity);
653 static ir_node *string_literal_to_firm(const string_literal_t* literal)
655 return string_to_firm(&literal->expression.source_position, "Lstr",
659 static ir_node *deref_address(type_t *const type, ir_node *const addr,
660 dbg_info *const dbgi)
662 switch (type->type) {
664 case TYPE_COMPOUND_STRUCT:
665 case TYPE_COMPOUND_UNION:
669 ir_mode *const mode = get_ir_mode(type);
670 ir_node *const memory = get_store();
671 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
672 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
673 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
680 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
682 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
683 declaration_t *declaration = ref->declaration;
684 type_t *type = skip_typeref(declaration->type);
686 switch((declaration_type_t) declaration->declaration_type) {
687 case DECLARATION_TYPE_UNKNOWN:
688 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
694 case DECLARATION_TYPE_ENUM_ENTRY: {
695 ir_mode *const mode = get_ir_mode(type);
696 return new_Const(mode, declaration->v.enum_val);
699 case DECLARATION_TYPE_LOCAL_VARIABLE: {
700 ir_mode *mode = get_ir_mode(type);
701 return get_value(declaration->v.value_number, mode);
703 case DECLARATION_TYPE_FUNCTION: {
704 return create_symconst(dbgi, declaration->v.entity);
706 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
707 ir_entity *entity = declaration->v.entity;
708 ir_node *symconst = create_symconst(dbgi, entity);
709 return deref_address(type, symconst, dbgi);
711 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
712 ir_entity *entity = declaration->v.entity;
713 ir_node *frame = get_irg_frame(current_ir_graph);
714 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
715 return deref_address(type, sel, dbgi);
718 case DECLARATION_TYPE_COMPOUND_MEMBER:
719 case DECLARATION_TYPE_LABEL_BLOCK:
720 panic("not implemented reference type");
723 panic("reference to declaration with unknown type found");
726 static ir_node *reference_addr(const reference_expression_t *ref)
728 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
729 declaration_t *declaration = ref->declaration;
731 switch((declaration_type_t) declaration->declaration_type) {
732 case DECLARATION_TYPE_UNKNOWN:
734 case DECLARATION_TYPE_LOCAL_VARIABLE:
735 panic("local variable without entity has no address");
736 case DECLARATION_TYPE_FUNCTION: {
737 return create_symconst(dbgi, declaration->v.entity);
739 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
740 ir_entity *entity = declaration->v.entity;
741 ir_node *symconst = create_symconst(dbgi, entity);
744 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
745 ir_entity *entity = declaration->v.entity;
746 ir_node *frame = get_irg_frame(current_ir_graph);
747 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
752 case DECLARATION_TYPE_ENUM_ENTRY:
753 panic("trying to reference enum entry");
755 case DECLARATION_TYPE_COMPOUND_MEMBER:
756 case DECLARATION_TYPE_LABEL_BLOCK:
757 panic("not implemented reference type");
760 panic("reference to declaration with unknown type found");
763 static ir_node *process_builtin_call(const call_expression_t *call)
765 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
767 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
768 builtin_symbol_expression_t *builtin
769 = (builtin_symbol_expression_t*) call->function;
770 symbol_t *symbol = builtin->symbol;
772 if(symbol == symbol_alloca) {
773 if(call->arguments == NULL || call->arguments->next != NULL) {
774 panic("invalid number of parameters on __builtin_alloca");
776 expression_t *argument = call->arguments->expression;
777 ir_node *size = expression_to_firm(argument);
779 ir_node *store = get_store();
780 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
782 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
784 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
788 panic("Unsupported builtin found\n");
792 static ir_node *call_expression_to_firm(const call_expression_t *call)
794 assert(get_cur_block() != NULL);
796 expression_t *function = call->function;
797 if(function->type == EXPR_BUILTIN_SYMBOL) {
798 return process_builtin_call(call);
800 ir_node *callee = expression_to_firm(function);
802 function_type_t *function_type;
803 if (function->datatype->type == TYPE_POINTER) {
804 pointer_type_t *const ptr_type = (pointer_type_t*)function->datatype;
805 assert(ptr_type->points_to->type == TYPE_FUNCTION);
806 function_type = (function_type_t*)ptr_type->points_to;
808 assert(function->datatype->type == TYPE_FUNCTION);
809 function_type = (function_type_t*)function->datatype;
812 int n_parameters = 0;
813 call_argument_t *argument = call->arguments;
814 for( ; argument != NULL; argument = argument->next) {
818 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
819 ir_type *new_method_type = NULL;
820 if(function_type->variadic || function_type->unspecified_parameters) {
821 /* we need to construct a new method type matching the call
823 int n_res = get_method_n_ress(ir_method_type);
824 new_method_type = new_type_method(unique_ident("calltype"),
825 n_parameters, n_res);
826 set_method_calling_convention(new_method_type,
827 get_method_calling_convention(ir_method_type));
828 set_method_additional_properties(new_method_type,
829 get_method_additional_properties(ir_method_type));
831 for(int i = 0; i < n_res; ++i) {
832 set_method_res_type(new_method_type, i,
833 get_method_res_type(ir_method_type, i));
836 ir_node *in[n_parameters];
838 argument = call->arguments;
840 for( ; argument != NULL; argument = argument->next) {
841 expression_t *expression = argument->expression;
842 ir_node *arg_node = expression_to_firm(expression);
845 if(new_method_type != NULL) {
846 ir_type *irtype = get_ir_type(expression->datatype);
847 set_method_param_type(new_method_type, n, irtype);
852 assert(n == n_parameters);
854 if(new_method_type != NULL)
855 ir_method_type = new_method_type;
857 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
858 ir_node *store = get_store();
859 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
861 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
864 type_t *result_type = function_type->result_type;
865 ir_node *result = NULL;
866 if(result_type != type_void) {
867 ir_mode *mode = get_ir_mode(result_type);
868 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
869 result = new_d_Proj(dbgi, resproj, mode, 0);
875 static void statement_to_firm(statement_t *statement);
876 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
878 static ir_node *expression_to_addr(const expression_t *expression);
879 static void create_condition_evaluation(const expression_t *expression,
881 ir_node *false_block);
883 static void set_value_for_expression(const expression_t *expression,
886 if(expression->type == EXPR_REFERENCE) {
887 reference_expression_t *ref = (reference_expression_t*) expression;
889 declaration_t *declaration = ref->declaration;
890 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
891 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
892 set_value(declaration->v.value_number, value);
897 dbg_info *dbgi = get_dbg_info(&expression->source_position);
898 ir_node *addr = expression_to_addr(expression);
899 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
900 ir_node *memory = get_store();
901 ir_node *store = new_d_Store(dbgi, memory, addr, value);
902 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
903 set_store(store_mem);
906 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
908 ir_mode *value_mode = get_irn_mode(value);
910 if (value_mode == dest_mode || is_Bad(value))
913 if(dest_mode == mode_b) {
914 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
915 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
916 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
920 return new_d_Conv(dbgi, value, dest_mode);
923 static ir_node *create_incdec(const unary_expression_t *expression)
925 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
926 type_t *type = expression->expression.datatype;
927 ir_mode *mode = get_ir_mode(type);
928 expression_t *value = expression->value;
930 ir_node *value_node = expression_to_firm(value);
933 if(type->type == TYPE_POINTER) {
934 pointer_type_t *pointer_type = (pointer_type_t*) type;
935 unsigned elem_size = get_type_size(pointer_type->points_to);
936 offset = new_Const_long(mode_Is, elem_size);
938 assert(is_type_arithmetic(type));
939 offset = new_Const(mode, get_mode_one(mode));
942 switch(expression->type) {
943 case UNEXPR_POSTFIX_INCREMENT: {
944 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
945 set_value_for_expression(value, new_value);
948 case UNEXPR_POSTFIX_DECREMENT: {
949 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
950 set_value_for_expression(value, new_value);
953 case UNEXPR_PREFIX_INCREMENT: {
954 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
955 set_value_for_expression(value, new_value);
958 case UNEXPR_PREFIX_DECREMENT: {
959 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
960 set_value_for_expression(value, new_value);
964 panic("no incdec expr in create_incdec");
969 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
971 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
972 type_t *type = skip_typeref(expression->expression.datatype);
974 if(expression->type == UNEXPR_TAKE_ADDRESS)
975 return expression_to_addr(expression->value);
977 const expression_t *value = expression->value;
978 ir_node *value_node = expression_to_firm(value);
980 switch(expression->type) {
982 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
985 case UNEXPR_BITWISE_NEGATE:
986 return new_d_Not(dbgi, value_node, get_ir_mode(type));
988 if(get_irn_mode(value_node) != mode_b) {
989 value_node = create_conv(dbgi, value_node, mode_b);
991 value_node = new_d_Not(dbgi, value_node, mode_b);
992 ir_mode *const mode = get_ir_mode(type);
994 value_node = create_conv(dbgi, value_node, mode);
998 case UNEXPR_DEREFERENCE:
999 return deref_address(type, value_node, dbgi);
1000 case UNEXPR_POSTFIX_INCREMENT:
1001 case UNEXPR_POSTFIX_DECREMENT:
1002 case UNEXPR_PREFIX_INCREMENT:
1003 case UNEXPR_PREFIX_DECREMENT:
1004 return create_incdec(expression);
1006 return create_conv(dbgi, value_node, get_ir_mode(type));
1008 case UNEXPR_TAKE_ADDRESS:
1009 case UNEXPR_INVALID:
1012 panic("invalid UNEXPR type found");
1015 static long get_pnc(binary_expression_type_t type)
1018 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1019 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1020 case BINEXPR_LESS: return pn_Cmp_Lt;
1021 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1022 case BINEXPR_GREATER: return pn_Cmp_Gt;
1023 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1027 panic("trying to get pn_Cmp from non-comparison binexpr type");
1030 static ir_node *create_lazy_op(const binary_expression_t *expression)
1032 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1033 type_t *type = expression->expression.datatype;
1034 ir_mode *mode = get_ir_mode(type);
1036 ir_node *cur_block = get_cur_block();
1038 ir_node *one_block = new_immBlock();
1039 ir_node *one = new_Const(mode, get_mode_one(mode));
1040 ir_node *jmp_one = new_d_Jmp(dbgi);
1042 ir_node *zero_block = new_immBlock();
1043 ir_node *zero = new_Const(mode, get_mode_null(mode));
1044 ir_node *jmp_zero = new_d_Jmp(dbgi);
1046 set_cur_block(cur_block);
1047 create_condition_evaluation((const expression_t*) expression,
1048 one_block, zero_block);
1049 mature_immBlock(one_block);
1050 mature_immBlock(zero_block);
1052 ir_node *common_block = new_immBlock();
1053 add_immBlock_pred(common_block, jmp_one);
1054 add_immBlock_pred(common_block, jmp_zero);
1055 mature_immBlock(common_block);
1057 ir_node *in[2] = { one, zero };
1058 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1063 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1064 ir_node *right, ir_mode *mode);
1066 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1067 create_arithmetic_func func)
1069 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1070 ir_node *left = expression_to_firm(expression->left);
1071 ir_node *right = expression_to_firm(expression->right);
1072 type_t *type = expression->right->datatype;
1073 /* be careful with the modes, because in arithmetic assign nodes only
1074 * the right operand has the mode of the arithmetic already */
1075 ir_mode *mode = get_ir_mode(type);
1076 left = create_conv(dbgi, left, mode);
1077 ir_node *res = func(dbgi, left, right, mode);
1082 static ir_node *pointer_arithmetic(ir_node *const pointer,
1085 dbg_info *const dbgi,
1086 const create_arithmetic_func func)
1088 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1089 type_t *const points_to = pointer_type->points_to;
1090 const unsigned elem_size = get_type_size(points_to);
1092 assert(elem_size >= 1);
1093 if (elem_size > 1) {
1094 integer = create_conv(dbgi, integer, mode_Is);
1095 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1096 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1100 ir_mode *const mode = get_ir_mode(type);
1101 return func(dbgi, pointer, integer, mode);
1104 static ir_node *create_arithmetic_assign_binop(
1105 const binary_expression_t *expression, create_arithmetic_func func)
1107 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1108 type_t *const type = expression->expression.datatype;
1111 if (type->type == TYPE_POINTER) {
1112 ir_node *const pointer = expression_to_firm(expression->left);
1113 ir_node * integer = expression_to_firm(expression->right);
1114 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1116 value = create_arithmetic_binop(expression, func);
1119 ir_mode *const mode = get_ir_mode(type);
1120 value = create_conv(dbgi, value, mode);
1121 set_value_for_expression(expression->left, value);
1126 static ir_node *create_add(const binary_expression_t *expression)
1128 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1129 ir_node *left = expression_to_firm(expression->left);
1130 ir_node *right = expression_to_firm(expression->right);
1131 type_t *type = expression->expression.datatype;
1133 expression_t *expr_left = expression->left;
1134 expression_t *expr_right = expression->right;
1135 type_t *type_left = skip_typeref(expr_left->datatype);
1136 type_t *type_right = skip_typeref(expr_right->datatype);
1138 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1139 ir_mode *const mode = get_ir_mode(type);
1140 return new_d_Add(dbgi, left, right, mode);
1143 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1144 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1146 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1147 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1151 static ir_node *create_sub(const binary_expression_t *expression)
1153 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1154 expression_t *const expr_left = expression->left;
1155 expression_t *const expr_right = expression->right;
1156 ir_node *const left = expression_to_firm(expr_left);
1157 ir_node *const right = expression_to_firm(expr_right);
1158 type_t *const type = expression->expression.datatype;
1159 type_t *const type_left = skip_typeref(expr_left->datatype);
1160 type_t *const type_right = skip_typeref(expr_right->datatype);
1162 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1163 ir_mode *const mode = get_ir_mode(type);
1164 return new_d_Sub(dbgi, left, right, mode);
1165 } else if (type_left->type == TYPE_POINTER && type_right->type == TYPE_POINTER) {
1166 const pointer_type_t *const ptr_type = (const pointer_type_t*)type_left;
1167 const unsigned elem_size = get_type_size(ptr_type->points_to);
1168 ir_mode *const mode = get_ir_mode(type);
1169 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1170 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1171 ir_node *const no_mem = new_NoMem();
1172 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1173 op_pin_state_floats);
1174 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1177 assert(type_left->type == TYPE_POINTER);
1178 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1181 static ir_node *create_shift(const binary_expression_t *expression)
1183 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1184 ir_node *left = expression_to_firm(expression->left);
1185 ir_node *right = expression_to_firm(expression->right);
1186 type_t *type = expression->expression.datatype;
1187 ir_mode *mode = get_ir_mode(type);
1189 /* firm always wants the shift count to be unsigned */
1190 right = create_conv(dbgi, right, mode_Iu);
1194 switch(expression->type) {
1195 case BINEXPR_SHIFTLEFT_ASSIGN:
1196 case BINEXPR_SHIFTLEFT:
1197 res = new_d_Shl(dbgi, left, right, mode);
1199 case BINEXPR_SHIFTRIGHT_ASSIGN:
1200 case BINEXPR_SHIFTRIGHT: {
1201 expression_t *expr_left = expression->left;
1202 type_t *type_left = skip_typeref(expr_left->datatype);
1204 if(is_type_signed(type_left)) {
1205 res = new_d_Shrs(dbgi, left, right, mode);
1207 res = new_d_Shr(dbgi, left, right, mode);
1212 panic("create shift op called for non-shift op");
1219 static ir_node *create_divmod(const binary_expression_t *expression)
1221 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1222 ir_node *left = expression_to_firm(expression->left);
1223 ir_node *right = expression_to_firm(expression->right);
1224 ir_node *pin = new_Pin(new_NoMem());
1225 /* be careful with the modes, because in arithmetic assign nodes only
1226 * the right operand has the mode of the arithmetic already */
1227 type_t *type = expression->right->datatype;
1228 ir_mode *mode = get_ir_mode(type);
1229 left = create_conv(dbgi, left, mode);
1233 switch (expression->type) {
1235 case BINEXPR_DIV_ASSIGN:
1236 if(mode_is_float(mode)) {
1237 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1238 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1240 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1241 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1246 case BINEXPR_MOD_ASSIGN:
1247 assert(!mode_is_float(mode));
1248 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1249 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1252 default: panic("unexpected binary expression type in create_divmod()");
1258 static ir_node *create_arithmetic_assign_divmod(
1259 const binary_expression_t *expression)
1261 ir_node * value = create_divmod(expression);
1262 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1263 type_t *const type = expression->expression.datatype;
1264 ir_mode *const mode = get_ir_mode(type);
1266 assert(type->type != TYPE_POINTER);
1268 value = create_conv(dbgi, value, mode);
1269 set_value_for_expression(expression->left, value);
1274 static ir_node *create_arithmetic_assign_shift(
1275 const binary_expression_t *expression)
1277 ir_node * value = create_shift(expression);
1278 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1279 type_t *const type = expression->expression.datatype;
1280 ir_mode *const mode = get_ir_mode(type);
1282 value = create_conv(dbgi, value, mode);
1283 set_value_for_expression(expression->left, value);
1288 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1290 binary_expression_type_t type = expression->type;
1293 case BINEXPR_NOTEQUAL:
1295 case BINEXPR_LESSEQUAL:
1296 case BINEXPR_GREATER:
1297 case BINEXPR_GREATEREQUAL: {
1298 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1299 ir_node *left = expression_to_firm(expression->left);
1300 ir_node *right = expression_to_firm(expression->right);
1301 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1302 long pnc = get_pnc(type);
1303 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1306 case BINEXPR_ASSIGN: {
1307 ir_node *right = expression_to_firm(expression->right);
1308 set_value_for_expression(expression->left, right);
1312 return create_add(expression);
1314 return create_sub(expression);
1316 return create_arithmetic_binop(expression, new_d_Mul);
1317 case BINEXPR_BITWISE_AND:
1318 return create_arithmetic_binop(expression, new_d_And);
1319 case BINEXPR_BITWISE_OR:
1320 return create_arithmetic_binop(expression, new_d_Or);
1321 case BINEXPR_BITWISE_XOR:
1322 return create_arithmetic_binop(expression, new_d_Eor);
1323 case BINEXPR_SHIFTLEFT:
1324 case BINEXPR_SHIFTRIGHT:
1325 return create_shift(expression);
1328 return create_divmod(expression);
1329 case BINEXPR_LOGICAL_AND:
1330 case BINEXPR_LOGICAL_OR:
1331 return create_lazy_op(expression);
1333 expression_to_firm(expression->left);
1334 return expression_to_firm(expression->right);
1335 case BINEXPR_ADD_ASSIGN:
1336 return create_arithmetic_assign_binop(expression, new_d_Add);
1337 case BINEXPR_SUB_ASSIGN:
1338 return create_arithmetic_assign_binop(expression, new_d_Sub);
1339 case BINEXPR_MUL_ASSIGN:
1340 return create_arithmetic_assign_binop(expression, new_d_Mul);
1341 case BINEXPR_DIV_ASSIGN:
1342 return create_arithmetic_assign_divmod(expression);
1343 case BINEXPR_BITWISE_AND_ASSIGN:
1344 return create_arithmetic_assign_binop(expression, new_d_And);
1345 case BINEXPR_BITWISE_OR_ASSIGN:
1346 return create_arithmetic_assign_binop(expression, new_d_Or);
1347 case BINEXPR_BITWISE_XOR_ASSIGN:
1348 return create_arithmetic_assign_binop(expression, new_d_Eor);
1349 case BINEXPR_SHIFTLEFT_ASSIGN:
1350 case BINEXPR_SHIFTRIGHT_ASSIGN:
1351 return create_arithmetic_assign_shift(expression);
1353 panic("TODO binexpr type");
1357 static ir_node *array_access_addr(const array_access_expression_t *expression)
1359 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1363 type_t *type_left = skip_typeref(expression->array_ref->datatype);
1364 type_t *type_right = skip_typeref(expression->index->datatype);
1366 if(type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1367 base_addr = expression_to_firm(expression->array_ref);
1368 offset = expression_to_firm(expression->index);
1370 assert(type_right->type == TYPE_POINTER
1371 || type_right->type == TYPE_ARRAY);
1372 base_addr = expression_to_firm(expression->index);
1373 offset = expression_to_firm(expression->array_ref);
1375 offset = create_conv(dbgi, offset, mode_Iu);
1377 unsigned elem_size = get_type_size(expression->expression.datatype);
1378 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1379 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1381 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1386 static ir_node *array_access_to_firm(
1387 const array_access_expression_t *expression)
1389 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1390 ir_node *addr = array_access_addr(expression);
1391 type_t *type = skip_typeref(expression->expression.datatype);
1392 return deref_address(type, addr, dbgi);
1395 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1397 type_t *type = expression->type;
1399 type = expression->size_expression->datatype;
1400 assert(type != NULL);
1403 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1404 unsigned size = get_type_size(type);
1405 ir_node *size_node = new_Const_long(mode, size);
1410 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1412 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1414 ir_node *cur_block = get_cur_block();
1416 /* create the true block */
1417 ir_node *true_block = new_immBlock();
1419 ir_node *true_val = expression_to_firm(expression->true_expression);
1420 ir_node *true_jmp = new_Jmp();
1422 /* create the false block */
1423 ir_node *false_block = new_immBlock();
1425 ir_node *false_val = expression_to_firm(expression->false_expression);
1426 ir_node *false_jmp = new_Jmp();
1428 /* create the condition evaluation */
1429 set_cur_block(cur_block);
1430 create_condition_evaluation(expression->condition, true_block, false_block);
1431 mature_immBlock(true_block);
1432 mature_immBlock(false_block);
1434 /* create the common block */
1435 ir_node *common_block = new_immBlock();
1436 add_immBlock_pred(common_block, true_jmp);
1437 add_immBlock_pred(common_block, false_jmp);
1438 mature_immBlock(common_block);
1440 /* TODO improve static semantics, so either both or no values are NULL */
1441 if (true_val == NULL || false_val == NULL) return NULL;
1443 ir_node *in[2] = { true_val, false_val };
1444 ir_mode *mode = get_irn_mode(true_val);
1445 assert(get_irn_mode(false_val) == mode);
1446 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1451 static ir_node *select_addr(const select_expression_t *expression)
1453 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1455 ir_node *compound_addr = expression_to_firm(expression->compound);
1457 declaration_t *entry = expression->compound_entry;
1458 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1459 ir_entity *entity = entry->v.entity;
1461 assert(entity != NULL);
1463 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1468 static ir_node *select_to_firm(const select_expression_t *expression)
1470 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1471 ir_node *addr = select_addr(expression);
1472 type_t *type = skip_typeref(expression->expression.datatype);
1473 return deref_address(type, addr, dbgi);
1476 /* Values returned by __builtin_classify_type. */
1477 typedef enum gcc_type_class
1483 enumeral_type_class,
1486 reference_type_class,
1490 function_type_class,
1501 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1503 const type_t *const type = expr->type_expression->datatype;
1509 const atomic_type_t *const atomic_type = (const atomic_type_t*)type;
1510 switch (atomic_type->atype) {
1511 // should not be reached
1512 case ATOMIC_TYPE_INVALID:
1516 // gcc cannot do that
1517 case ATOMIC_TYPE_VOID:
1518 tc = void_type_class;
1521 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1522 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1523 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1524 case ATOMIC_TYPE_SHORT:
1525 case ATOMIC_TYPE_USHORT:
1526 case ATOMIC_TYPE_INT:
1527 case ATOMIC_TYPE_UINT:
1528 case ATOMIC_TYPE_LONG:
1529 case ATOMIC_TYPE_ULONG:
1530 case ATOMIC_TYPE_LONGLONG:
1531 case ATOMIC_TYPE_ULONGLONG:
1532 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1533 tc = integer_type_class;
1536 case ATOMIC_TYPE_FLOAT:
1537 case ATOMIC_TYPE_DOUBLE:
1538 case ATOMIC_TYPE_LONG_DOUBLE:
1539 tc = real_type_class;
1542 #ifdef PROVIDE_COMPLEX
1543 case ATOMIC_TYPE_FLOAT_COMPLEX:
1544 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1545 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1546 tc = complex_type_class;
1548 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1549 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1550 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1551 tc = complex_type_class;
1556 panic("Unimplemented case in classify_type_to_firm().");
1561 case TYPE_ARRAY: // gcc handles this as pointer
1562 case TYPE_FUNCTION: // gcc handles this as pointer
1563 case TYPE_POINTER: tc = pointer_type_class; break;
1564 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1565 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1567 // gcc handles this as integer
1568 case TYPE_ENUM: tc = integer_type_class; break;
1571 panic("Unimplemented case in classify_type_to_firm().");
1574 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1575 ir_mode *const mode = mode_Is;
1576 tarval *const tv = new_tarval_from_long(tc, mode);
1577 return new_d_Const(dbgi, mode, tv);
1580 static ir_node *function_name_to_firm(const string_literal_t *const expr)
1582 if (current_function_name == NULL) {
1583 const source_position_t *const src_pos =
1584 &expr->expression.source_position;
1585 const char *const name = current_function_decl->symbol->string;
1586 current_function_name = string_to_firm(src_pos, "__func__", name);
1589 return current_function_name;
1592 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1594 statement_t *statement = expr->statement;
1596 assert(statement->type == STATEMENT_COMPOUND);
1597 return compound_statement_to_firm((compound_statement_t*) statement);
1600 static ir_node *dereference_addr(const unary_expression_t *const expression)
1602 assert(expression->type == UNEXPR_DEREFERENCE);
1603 return expression_to_firm(expression->value);
1606 static ir_node *expression_to_addr(const expression_t *expression)
1608 switch(expression->type) {
1609 case EXPR_REFERENCE:
1610 return reference_addr((const reference_expression_t*) expression);
1611 case EXPR_ARRAY_ACCESS:
1612 return array_access_addr((const array_access_expression_t*) expression);
1614 return select_addr((const select_expression_t*) expression);
1616 const unary_expression_t *const unary_expr =
1617 (const unary_expression_t*)expression;
1618 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1619 return dereference_addr(unary_expr);
1626 panic("trying to get address of non-lvalue");
1629 static ir_node *_expression_to_firm(const expression_t *expression)
1631 switch(expression->type) {
1633 return const_to_firm((const const_t*) expression);
1634 case EXPR_STRING_LITERAL:
1635 return string_literal_to_firm((const string_literal_t*) expression);
1636 case EXPR_REFERENCE:
1637 return reference_expression_to_firm(
1638 (const reference_expression_t*) expression);
1640 return call_expression_to_firm((const call_expression_t*) expression);
1642 return unary_expression_to_firm((const unary_expression_t*) expression);
1644 return binary_expression_to_firm(
1645 (const binary_expression_t*) expression);
1646 case EXPR_ARRAY_ACCESS:
1647 return array_access_to_firm(
1648 (const array_access_expression_t*) expression);
1650 return sizeof_to_firm((const sizeof_expression_t*) expression);
1651 case EXPR_CONDITIONAL:
1652 return conditional_to_firm((const conditional_expression_t*)expression);
1654 return select_to_firm((const select_expression_t*) expression);
1655 case EXPR_CLASSIFY_TYPE:
1656 return classify_type_to_firm((const classify_type_expression_t*)expression);
1658 case EXPR_PRETTY_FUNCTION:
1659 return function_name_to_firm((const string_literal_t*)expression);
1660 case EXPR_STATEMENT:
1661 return statement_expression_to_firm(
1662 (const statement_expression_t*) expression);
1665 case EXPR_BUILTIN_SYMBOL:
1666 panic("unimplemented expression found");
1672 panic("invalid expression found");
1675 static ir_node *expression_to_firm(const expression_t *expression)
1677 ir_node *res = _expression_to_firm(expression);
1679 if(res != NULL && get_irn_mode(res) == mode_b) {
1680 ir_mode *mode = get_ir_mode(expression->datatype);
1681 res = create_conv(NULL, res, mode);
1687 static ir_node *expression_to_modeb(const expression_t *expression)
1689 ir_node *res = _expression_to_firm(expression);
1690 res = create_conv(NULL, res, mode_b);
1696 * create a short-circuit expression evaluation that tries to construct
1697 * efficient control flow structures for &&, || and ! expressions
1699 static void create_condition_evaluation(const expression_t *expression,
1700 ir_node *true_block,
1701 ir_node *false_block)
1703 switch(expression->type) {
1705 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1706 if(unary_expression->type == UNEXPR_NOT) {
1707 create_condition_evaluation(unary_expression->value, false_block,
1714 binary_expression_t *binary_expression
1715 = (binary_expression_t*) expression;
1716 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1717 ir_node *cur_block = get_cur_block();
1718 ir_node *extra_block = new_immBlock();
1719 set_cur_block(cur_block);
1720 create_condition_evaluation(binary_expression->left, extra_block,
1722 mature_immBlock(extra_block);
1723 set_cur_block(extra_block);
1724 create_condition_evaluation(binary_expression->right, true_block,
1728 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1729 ir_node *cur_block = get_cur_block();
1730 ir_node *extra_block = new_immBlock();
1731 set_cur_block(cur_block);
1732 create_condition_evaluation(binary_expression->left, true_block,
1734 mature_immBlock(extra_block);
1735 set_cur_block(extra_block);
1736 create_condition_evaluation(binary_expression->right, true_block,
1746 dbg_info *dbgi = get_dbg_info(&expression->source_position);
1747 ir_node *condition = expression_to_modeb(expression);
1748 ir_node *cond = new_d_Cond(dbgi, condition);
1749 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1750 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1752 add_immBlock_pred(true_block, true_proj);
1753 add_immBlock_pred(false_block, false_proj);
1755 set_cur_block(NULL);
1759 static void return_statement_to_firm(return_statement_t *statement)
1761 if(get_cur_block() == NULL)
1764 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1767 if(statement->return_value != NULL) {
1768 ir_node *retval = expression_to_firm(statement->return_value);
1772 ret = new_d_Return(dbgi, get_store(), 1, in);
1774 ret = new_d_Return(dbgi, get_store(), 0, NULL);
1776 ir_node *end_block = get_irg_end_block(current_ir_graph);
1777 add_immBlock_pred(end_block, ret);
1779 set_cur_block(NULL);
1782 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1784 if(get_cur_block() == NULL)
1787 return expression_to_firm(statement->expression);
1790 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1792 ir_node *result = NULL;
1793 statement_t *statement = compound->statements;
1794 for( ; statement != NULL; statement = statement->next) {
1795 //context2firm(&statement->context);
1797 if(statement->next == NULL && statement->type == STATEMENT_EXPRESSION) {
1798 result = expression_statement_to_firm(
1799 (expression_statement_t*) statement);
1802 statement_to_firm(statement);
1808 static void if_statement_to_firm(if_statement_t *statement)
1810 ir_node *cur_block = get_cur_block();
1812 ir_node *fallthrough_block = new_immBlock();
1814 /* the true (blocks) */
1815 ir_node *true_block;
1816 if (statement->true_statement != NULL) {
1817 true_block = new_immBlock();
1818 statement_to_firm(statement->true_statement);
1819 if(get_cur_block() != NULL) {
1820 ir_node *jmp = new_Jmp();
1821 add_immBlock_pred(fallthrough_block, jmp);
1824 true_block = fallthrough_block;
1827 /* the false (blocks) */
1828 ir_node *false_block;
1829 if(statement->false_statement != NULL) {
1830 false_block = new_immBlock();
1832 statement_to_firm(statement->false_statement);
1833 if(get_cur_block() != NULL) {
1834 ir_node *jmp = new_Jmp();
1835 add_immBlock_pred(fallthrough_block, jmp);
1838 false_block = fallthrough_block;
1841 /* create the condition */
1842 if(cur_block != NULL) {
1843 set_cur_block(cur_block);
1844 create_condition_evaluation(statement->condition, true_block,
1848 mature_immBlock(true_block);
1849 if(false_block != fallthrough_block) {
1850 mature_immBlock(false_block);
1852 mature_immBlock(fallthrough_block);
1854 set_cur_block(fallthrough_block);
1857 static void while_statement_to_firm(while_statement_t *statement)
1859 ir_node *jmp = NULL;
1860 if(get_cur_block() != NULL) {
1864 /* create the header block */
1865 ir_node *header_block = new_immBlock();
1867 add_immBlock_pred(header_block, jmp);
1870 /* the false block */
1871 ir_node *false_block = new_immBlock();
1874 ir_node *body_block;
1875 if (statement->body != NULL) {
1876 ir_node *old_continue_label = continue_label;
1877 ir_node *old_break_label = break_label;
1878 continue_label = header_block;
1879 break_label = false_block;
1881 body_block = new_immBlock();
1882 statement_to_firm(statement->body);
1884 assert(continue_label == header_block);
1885 assert(break_label == false_block);
1886 continue_label = old_continue_label;
1887 break_label = old_break_label;
1889 if(get_cur_block() != NULL) {
1890 ir_node *jmp = new_Jmp();
1891 add_immBlock_pred(header_block, jmp);
1894 body_block = header_block;
1897 /* create the condition */
1898 set_cur_block(header_block);
1900 create_condition_evaluation(statement->condition, body_block, false_block);
1901 mature_immBlock(body_block);
1902 mature_immBlock(false_block);
1903 mature_immBlock(header_block);
1905 set_cur_block(false_block);
1908 static void do_while_statement_to_firm(do_while_statement_t *statement)
1910 ir_node *jmp = NULL;
1911 if(get_cur_block() != NULL) {
1915 /* create the header block */
1916 ir_node *header_block = new_immBlock();
1918 /* the false block */
1919 ir_node *false_block = new_immBlock();
1922 ir_node *body_block = new_immBlock();
1924 add_immBlock_pred(body_block, jmp);
1927 if (statement->body != NULL) {
1928 ir_node *old_continue_label = continue_label;
1929 ir_node *old_break_label = break_label;
1930 continue_label = header_block;
1931 break_label = false_block;
1933 statement_to_firm(statement->body);
1935 assert(continue_label == header_block);
1936 assert(break_label == false_block);
1937 continue_label = old_continue_label;
1938 break_label = old_break_label;
1940 if (get_cur_block() == NULL) {
1941 mature_immBlock(header_block);
1942 mature_immBlock(body_block);
1943 mature_immBlock(false_block);
1948 ir_node *body_jmp = new_Jmp();
1949 add_immBlock_pred(header_block, body_jmp);
1950 mature_immBlock(header_block);
1952 /* create the condition */
1953 set_cur_block(header_block);
1955 create_condition_evaluation(statement->condition, body_block, false_block);
1956 mature_immBlock(body_block);
1957 mature_immBlock(false_block);
1958 mature_immBlock(header_block);
1960 set_cur_block(false_block);
1963 static void for_statement_to_firm(for_statement_t *statement)
1965 ir_node *jmp = NULL;
1966 if (get_cur_block() != NULL) {
1967 if(statement->initialisation != NULL) {
1968 expression_to_firm(statement->initialisation);
1973 /* create the step block */
1974 ir_node *const step_block = new_immBlock();
1975 if (statement->step != NULL) {
1976 expression_to_firm(statement->step);
1978 ir_node *const step_jmp = new_Jmp();
1980 /* create the header block */
1981 ir_node *const header_block = new_immBlock();
1983 add_immBlock_pred(header_block, jmp);
1985 add_immBlock_pred(header_block, step_jmp);
1987 /* the false block */
1988 ir_node *const false_block = new_immBlock();
1991 ir_node * body_block;
1992 if (statement->body != NULL) {
1993 ir_node *const old_continue_label = continue_label;
1994 ir_node *const old_break_label = break_label;
1995 continue_label = step_block;
1996 break_label = false_block;
1998 body_block = new_immBlock();
1999 statement_to_firm(statement->body);
2001 assert(continue_label == step_block);
2002 assert(break_label == false_block);
2003 continue_label = old_continue_label;
2004 break_label = old_break_label;
2006 if (get_cur_block() != NULL) {
2007 ir_node *const jmp = new_Jmp();
2008 add_immBlock_pred(step_block, jmp);
2011 body_block = step_block;
2014 /* create the condition */
2015 set_cur_block(header_block);
2016 if (statement->condition != NULL) {
2017 create_condition_evaluation(statement->condition, body_block,
2020 keep_alive(header_block);
2021 ir_node *jmp = new_Jmp();
2022 add_immBlock_pred(body_block, jmp);
2025 mature_immBlock(body_block);
2026 mature_immBlock(false_block);
2027 mature_immBlock(step_block);
2028 mature_immBlock(header_block);
2029 mature_immBlock(false_block);
2031 set_cur_block(false_block);
2034 static void create_declaration_entity(declaration_t *declaration,
2035 declaration_type_t declaration_type,
2036 ir_type *parent_type)
2038 ident *id = new_id_from_str(declaration->symbol->string);
2039 ir_type *irtype = get_ir_type(declaration->type);
2040 ir_entity *entity = new_entity(parent_type, id, irtype);
2041 set_entity_ld_ident(entity, id);
2043 declaration->declaration_type = declaration_type;
2044 declaration->v.entity = entity;
2045 set_entity_variability(entity, variability_uninitialized);
2046 /* TODO: visibility? */
2049 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2051 enum compound_graph_entry_type_t {
2052 COMPOUND_GRAPH_ENTRY_ARRAY,
2053 COMPOUND_GRAPH_ENTRY_COMPOUND
2056 struct compound_graph_path_entry_t {
2062 compound_graph_path_entry_t *prev;
2065 static void create_initializer_object(initializer_t *initializer, type_t *type,
2066 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2068 static compound_graph_path *create_compound_path(ir_type *type,
2069 compound_graph_path_entry_t *entry, int len)
2071 compound_graph_path *path = new_compound_graph_path(type, len);
2074 for( ; entry != NULL; entry = entry->prev, --i) {
2076 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2077 set_compound_graph_path_node(path, i, entry->v.entity);
2079 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2080 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2088 static void create_initializer_value(initializer_value_t *initializer,
2090 compound_graph_path_entry_t *entry,
2093 ir_node *node = expression_to_firm(initializer->value);
2094 ir_type *type = get_entity_type(entity);
2095 compound_graph_path *path = create_compound_path(type, entry, len);
2096 add_compound_ent_value_w_path(entity, node, path);
2099 static void create_initializer_compound(initializer_list_t *initializer,
2100 compound_type_t *type,
2102 compound_graph_path_entry_t *last_entry,
2105 declaration_t *compound_declaration = type->declaration;
2107 declaration_t *compound_entry = compound_declaration->context.declarations;
2109 compound_graph_path_entry_t entry;
2110 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2111 entry.prev = last_entry;
2115 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2116 if(compound_entry->symbol == NULL)
2118 if(compound_entry->namespc != NAMESPACE_NORMAL)
2121 if(i >= initializer->len)
2124 entry.v.entity = compound_entry->v.entity;
2126 initializer_t *sub_initializer = initializer->initializers[i];
2128 assert(compound_entry != NULL);
2129 assert(compound_entry->declaration_type
2130 == DECLARATION_TYPE_COMPOUND_MEMBER);
2132 if(sub_initializer->type == INITIALIZER_VALUE) {
2133 create_initializer_value(&sub_initializer->value,
2134 entity, &entry, len);
2136 type_t *type = skip_typeref(compound_entry->type);
2137 create_initializer_object(sub_initializer, type, entity, &entry,
2145 static void create_initializer_array(initializer_list_t *initializer,
2146 array_type_t *type, ir_entity *entity,
2147 compound_graph_path_entry_t *last_entry,
2150 type_t *element_type = type->element_type;
2151 element_type = skip_typeref(element_type);
2153 compound_graph_path_entry_t entry;
2154 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2155 entry.prev = last_entry;
2158 for(size_t i = 0; i < initializer->len; ++i) {
2159 entry.v.array_index = i;
2161 initializer_t *sub_initializer = initializer->initializers[i];
2163 if(sub_initializer->type == INITIALIZER_VALUE) {
2164 create_initializer_value(&sub_initializer->value,
2165 entity, &entry, len);
2167 create_initializer_object(sub_initializer, element_type, entity,
2173 static void create_initializer_string(initializer_string_t *initializer,
2174 array_type_t *type, ir_entity *entity,
2175 compound_graph_path_entry_t *last_entry,
2178 type_t *element_type = type->element_type;
2179 element_type = skip_typeref(element_type);
2181 compound_graph_path_entry_t entry;
2182 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2183 entry.prev = last_entry;
2186 ir_type *irtype = get_entity_type(entity);
2187 size_t arr_len = get_array_type_size(type);
2188 const char *p = initializer->string;
2190 for(i = 0; i < arr_len; ++i, ++p) {
2191 entry.v.array_index = i;
2193 ir_node *node = new_Const_long(mode_Bs, *p);
2194 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2195 add_compound_ent_value_w_path(entity, node, path);
2202 static void create_initializer_object(initializer_t *initializer, type_t *type,
2203 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2205 if(type->type == TYPE_ARRAY) {
2206 array_type_t *array_type = (array_type_t*) type;
2208 if(initializer->type == INITIALIZER_STRING) {
2209 initializer_string_t *string = &initializer->string;
2210 create_initializer_string(string, array_type, entity, entry, len);
2212 assert(initializer->type == INITIALIZER_LIST);
2213 initializer_list_t *list = &initializer->list;
2214 create_initializer_array(list, array_type, entity, entry, len);
2217 assert(initializer->type == INITIALIZER_LIST);
2218 initializer_list_t *list = &initializer->list;
2220 assert(type->type == TYPE_COMPOUND_STRUCT
2221 || type->type == TYPE_COMPOUND_UNION);
2222 compound_type_t *compound_type = (compound_type_t*) type;
2223 create_initializer_compound(list, compound_type, entity, entry, len);
2227 static void create_initializer_local_variable_entity(declaration_t *declaration)
2229 initializer_t *initializer = declaration->init.initializer;
2230 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2231 ir_entity *entity = declaration->v.entity;
2232 ir_node *memory = get_store();
2233 ir_node *nomem = new_NoMem();
2234 ir_node *frame = get_irg_frame(current_ir_graph);
2235 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2237 if(is_atomic_entity(entity)) {
2238 assert(initializer->type == INITIALIZER_VALUE);
2239 initializer_value_t *initializer_value = &initializer->value;
2241 ir_node *value = expression_to_firm(initializer_value->value);
2242 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2243 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2244 set_store(store_mem);
2248 /* create a "template" entity which is copied to the entity on the stack */
2249 ident *id = unique_ident("initializer");
2250 ir_type *irtype = get_ir_type(declaration->type);
2251 ir_type *global_type = get_glob_type();
2252 ir_entity *init_entity = new_entity(global_type, id, irtype);
2253 set_entity_ld_ident(init_entity, id);
2255 set_entity_variability(init_entity, variability_initialized);
2256 set_entity_visibility(init_entity, visibility_local);
2258 ir_graph *old_current_ir_graph = current_ir_graph;
2259 current_ir_graph = get_const_code_irg();
2261 type_t *type = skip_typeref(declaration->type);
2262 create_initializer_object(initializer, type, init_entity, NULL, 0);
2264 assert(current_ir_graph == get_const_code_irg());
2265 current_ir_graph = old_current_ir_graph;
2267 ir_node *src_addr = create_symconst(dbgi, init_entity);
2268 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2270 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2271 set_store(copyb_mem);
2274 static void create_initializer(declaration_t *declaration)
2276 initializer_t *initializer = declaration->init.initializer;
2277 if(initializer == NULL)
2280 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2281 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2282 create_initializer_local_variable_entity(declaration);
2286 if(initializer->type == INITIALIZER_VALUE) {
2287 initializer_value_t *initializer_value = &initializer->value;
2289 ir_node *value = expression_to_firm(initializer_value->value);
2291 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2292 set_value(declaration->v.value_number, value);
2294 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2296 ir_entity *entity = declaration->v.entity;
2298 set_entity_variability(entity, variability_initialized);
2299 set_atomic_ent_value(entity, value);
2302 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2303 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2304 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2306 ir_entity *entity = declaration->v.entity;
2307 set_entity_variability(entity, variability_initialized);
2309 type_t *type = skip_typeref(declaration->type);
2310 create_initializer_object(initializer, type, entity, NULL, 0);
2314 static void create_local_variable(declaration_t *declaration)
2316 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2318 bool needs_entity = declaration->address_taken;
2319 type_t *type = skip_typeref(declaration->type);
2321 if(type->type == TYPE_ARRAY
2322 || type->type == TYPE_COMPOUND_STRUCT
2323 || type->type == TYPE_COMPOUND_UNION) {
2324 needs_entity = true;
2328 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2329 create_declaration_entity(declaration,
2330 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2333 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2334 declaration->v.value_number = next_value_number_function;
2335 ++next_value_number_function;
2338 create_initializer(declaration);
2341 static void create_local_static_variable(declaration_t *declaration)
2343 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2345 type_t *type = skip_typeref(declaration->type);
2346 ir_type *global_type = get_glob_type();
2347 ident *id = unique_ident(declaration->symbol->string);
2348 ir_type *irtype = get_ir_type(type);
2349 ir_entity *entity = new_entity(global_type, id, irtype);
2350 set_entity_ld_ident(entity, id);
2352 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2353 declaration->v.entity = entity;
2354 set_entity_variability(entity, variability_uninitialized);
2355 set_entity_visibility(entity, visibility_local);
2357 ir_graph *old_current_ir_graph = current_ir_graph;
2358 current_ir_graph = get_const_code_irg();
2360 create_initializer(declaration);
2362 assert(current_ir_graph == get_const_code_irg());
2363 current_ir_graph = old_current_ir_graph;
2366 static void declaration_statement_to_firm(declaration_statement_t *statement)
2368 declaration_t *declaration = statement->declarations_begin;
2369 declaration_t *end = statement->declarations_end->next;
2370 for( ; declaration != end; declaration = declaration->next) {
2371 type_t *type = declaration->type;
2373 switch(declaration->storage_class) {
2374 case STORAGE_CLASS_TYPEDEF:
2376 case STORAGE_CLASS_STATIC:
2377 create_local_static_variable(declaration);
2379 case STORAGE_CLASS_ENUM_ENTRY:
2380 panic("enum entry declaration in local block found");
2381 case STORAGE_CLASS_EXTERN:
2382 panic("extern declaration in local block found");
2383 case STORAGE_CLASS_NONE:
2384 case STORAGE_CLASS_AUTO:
2385 case STORAGE_CLASS_REGISTER:
2386 if(type->type == TYPE_FUNCTION) {
2387 panic("nested functions not supported yet");
2389 create_local_variable(declaration);
2393 panic("invalid storage class found");
2397 static void create_jump_statement(const statement_t *statement,
2398 ir_node *target_block)
2400 if(get_cur_block() == NULL)
2403 dbg_info *dbgi = get_dbg_info(&statement->source_position);
2404 ir_node *jump = new_d_Jmp(dbgi);
2405 add_immBlock_pred(target_block, jump);
2407 set_cur_block(NULL);
2410 static void switch_statement_to_firm(const switch_statement_t *statement)
2412 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2414 ir_node *expression = expression_to_firm(statement->expression);
2415 ir_node *cond = new_d_Cond(dbgi, expression);
2416 ir_node *break_block = new_immBlock();
2418 set_cur_block(NULL);
2420 ir_node *const old_switch_cond = current_switch_cond;
2421 ir_node *const old_break_label = break_label;
2422 const bool old_saw_default_label = saw_default_label;
2423 current_switch_cond = cond;
2424 break_label = break_block;
2426 statement_to_firm(statement->body);
2428 if(get_cur_block() != NULL) {
2429 ir_node *jmp = new_Jmp();
2430 add_immBlock_pred(break_block, jmp);
2433 if (!saw_default_label) {
2434 set_cur_block(get_nodes_block(cond));
2435 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2436 MAGIC_DEFAULT_PN_NUMBER);
2437 add_immBlock_pred(break_block, proj);
2440 assert(current_switch_cond == cond);
2441 assert(break_label == break_block);
2442 current_switch_cond = old_switch_cond;
2443 break_label = old_break_label;
2444 saw_default_label = old_saw_default_label;
2446 mature_immBlock(break_block);
2447 set_cur_block(break_block);
2450 static long fold_constant(const expression_t *expression)
2452 ir_graph *old_current_ir_graph = current_ir_graph;
2453 current_ir_graph = get_const_code_irg();
2455 ir_node *cnst = expression_to_firm(expression);
2456 if(!is_Const(cnst)) {
2457 panic("couldn't fold constantl");
2459 tarval *tv = get_Const_tarval(cnst);
2460 if(!tarval_is_long(tv)) {
2461 panic("folded constant not an integer");
2464 long res = get_tarval_long(tv);
2466 current_ir_graph = old_current_ir_graph;
2470 static void case_label_to_firm(const case_label_statement_t *statement)
2472 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2474 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2476 /* let's create a node and hope firm constant folding creates a Const
2479 set_cur_block(get_nodes_block(current_switch_cond));
2480 if(statement->expression) {
2481 long pn = fold_constant(statement->expression);
2482 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2483 /* oops someone detected our cheating... */
2484 panic("magic default pn used");
2486 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2488 saw_default_label = true;
2489 proj = new_d_defaultProj(dbgi, current_switch_cond,
2490 MAGIC_DEFAULT_PN_NUMBER);
2493 ir_node *block = new_immBlock();
2494 if (fallthrough != NULL) {
2495 add_immBlock_pred(block, fallthrough);
2497 add_immBlock_pred(block, proj);
2498 mature_immBlock(block);
2500 statement_to_firm(statement->label_statement);
2503 static ir_node *get_label_block(declaration_t *label)
2505 assert(label->namespc == NAMESPACE_LABEL);
2507 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2508 return label->v.block;
2510 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2512 ir_node *old_cur_block = get_cur_block();
2513 ir_node *block = new_immBlock();
2514 set_cur_block(old_cur_block);
2516 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2517 label->v.block = block;
2519 ARR_APP1(ir_node *, imature_blocks, block);
2524 static void label_to_firm(const label_statement_t *statement)
2526 ir_node *block = get_label_block(statement->label);
2528 if(get_cur_block() != NULL) {
2529 ir_node *jmp = new_Jmp();
2530 add_immBlock_pred(block, jmp);
2533 set_cur_block(block);
2536 statement_to_firm(statement->label_statement);
2539 static void goto_to_firm(const goto_statement_t *statement)
2541 if(get_cur_block() == NULL)
2544 ir_node *block = get_label_block(statement->label);
2545 ir_node *jmp = new_Jmp();
2546 add_immBlock_pred(block, jmp);
2548 set_cur_block(NULL);
2551 static void statement_to_firm(statement_t *statement)
2553 switch(statement->type) {
2554 case STATEMENT_COMPOUND:
2555 compound_statement_to_firm((compound_statement_t*) statement);
2557 case STATEMENT_RETURN:
2558 return_statement_to_firm((return_statement_t*) statement);
2560 case STATEMENT_EXPRESSION:
2561 expression_statement_to_firm((expression_statement_t*) statement);
2564 if_statement_to_firm((if_statement_t*) statement);
2566 case STATEMENT_WHILE:
2567 while_statement_to_firm((while_statement_t*) statement);
2569 case STATEMENT_DO_WHILE:
2570 do_while_statement_to_firm((do_while_statement_t*) statement);
2572 case STATEMENT_DECLARATION:
2573 declaration_statement_to_firm((declaration_statement_t*) statement);
2575 case STATEMENT_BREAK:
2576 create_jump_statement(statement, break_label);
2578 case STATEMENT_CONTINUE:
2579 create_jump_statement(statement, continue_label);
2581 case STATEMENT_SWITCH:
2582 switch_statement_to_firm((switch_statement_t*) statement);
2584 case STATEMENT_CASE_LABEL:
2585 case_label_to_firm((case_label_statement_t*) statement);
2588 for_statement_to_firm((for_statement_t*) statement);
2590 case STATEMENT_LABEL:
2591 label_to_firm((label_statement_t*) statement);
2593 case STATEMENT_GOTO:
2594 goto_to_firm((goto_statement_t*) statement);
2599 panic("Statement not implemented\n");
2602 static int count_local_declarations(const declaration_t * decl,
2603 const declaration_t *const end)
2606 for (; decl != end; decl = decl->next) {
2607 const type_t *type = skip_typeref(decl->type);
2608 switch (type->type) {
2612 if (!decl->address_taken) ++count;
2621 static int count_decls_in_stmts(const statement_t *stmt)
2624 for (; stmt != NULL; stmt = stmt->next) {
2625 switch (stmt->type) {
2626 case STATEMENT_DECLARATION: {
2627 const declaration_statement_t *const decl_stmt =
2628 (const declaration_statement_t*)stmt;
2629 count += count_local_declarations(decl_stmt->declarations_begin,
2630 decl_stmt->declarations_end->next);
2634 case STATEMENT_COMPOUND: {
2635 const compound_statement_t *const comp =
2636 (const compound_statement_t*)stmt;
2637 count += count_decls_in_stmts(comp->statements);
2641 case STATEMENT_IF: {
2642 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2643 count += count_decls_in_stmts(if_stmt->true_statement);
2644 count += count_decls_in_stmts(if_stmt->false_statement);
2648 case STATEMENT_SWITCH: {
2649 const switch_statement_t *const switch_stmt =
2650 (const switch_statement_t*)stmt;
2651 count += count_decls_in_stmts(switch_stmt->body);
2655 case STATEMENT_LABEL: {
2656 const label_statement_t *const label_stmt =
2657 (const label_statement_t*)stmt;
2658 count += count_decls_in_stmts(label_stmt->label_statement);
2662 case STATEMENT_WHILE: {
2663 const while_statement_t *const while_stmt =
2664 (const while_statement_t*)stmt;
2665 count += count_decls_in_stmts(while_stmt->body);
2669 case STATEMENT_DO_WHILE: {
2670 const do_while_statement_t *const do_while_stmt =
2671 (const do_while_statement_t*)stmt;
2672 count += count_decls_in_stmts(do_while_stmt->body);
2676 case STATEMENT_FOR: {
2677 const for_statement_t *const for_stmt =
2678 (const for_statement_t*)stmt;
2679 /* TODO initialisation */
2680 count += count_decls_in_stmts(for_stmt->body);
2684 case STATEMENT_BREAK:
2685 case STATEMENT_CASE_LABEL:
2686 case STATEMENT_CONTINUE:
2687 case STATEMENT_EXPRESSION:
2688 case STATEMENT_GOTO:
2689 case STATEMENT_INVALID:
2690 case STATEMENT_RETURN:
2697 static int get_function_n_local_vars(declaration_t *declaration)
2701 /* count parameters */
2702 count += count_local_declarations(declaration->context.declarations, NULL);
2704 /* count local variables declared in body */
2705 count += count_decls_in_stmts(declaration->init.statement);
2707 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2708 * for expression statements... */
2714 static void initialize_function_parameters(declaration_t *declaration)
2716 ir_graph *irg = current_ir_graph;
2717 ir_node *args = get_irg_args(irg);
2718 ir_node *start_block = get_irg_start_block(irg);
2719 ir_type *function_irtype = get_ir_type(declaration->type);
2722 declaration_t *parameter = declaration->context.declarations;
2723 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2724 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2725 type_t *type = skip_typeref(parameter->type);
2727 bool needs_entity = parameter->address_taken;
2728 if(type->type == TYPE_COMPOUND_STRUCT
2729 || type->type == TYPE_COMPOUND_UNION) {
2730 needs_entity = true;
2734 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2735 ident *id = new_id_from_str(parameter->symbol->string);
2736 set_entity_ident(entity, id);
2738 parameter->declaration_type
2739 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2740 parameter->v.entity = entity;
2744 ir_mode *mode = get_ir_mode(parameter->type);
2746 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2748 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2749 parameter->v.value_number = next_value_number_function;
2750 ++next_value_number_function;
2752 set_value(parameter->v.value_number, proj);
2756 static void create_function(declaration_t *declaration)
2758 ir_entity *entity = get_function_entity(declaration);
2760 if(declaration->init.statement == NULL)
2763 current_function_decl = declaration;
2764 current_function_name = NULL;
2766 assert(imature_blocks == NULL);
2767 imature_blocks = NEW_ARR_F(ir_node*, 0);
2769 int n_local_vars = get_function_n_local_vars(declaration);
2770 ir_graph *irg = new_ir_graph(entity, n_local_vars);
2771 ir_node *first_block = get_cur_block();
2773 next_value_number_function = 0;
2774 initialize_function_parameters(declaration);
2776 statement_to_firm(declaration->init.statement);
2778 ir_node *end_block = get_irg_end_block(irg);
2780 /* do we have a return statement yet? */
2781 if(get_cur_block() != NULL) {
2782 assert(declaration->type->type == TYPE_FUNCTION);
2783 const function_type_t* const func_type
2784 = (const function_type_t*) declaration->type;
2786 if (func_type->result_type == type_void) {
2787 ret = new_Return(get_store(), 0, NULL);
2789 ir_mode *const mode = get_ir_mode(func_type->result_type);
2791 // ยง5.1.2.2.3 main implicitly returns 0
2792 if (strcmp(declaration->symbol->string, "main") == 0) {
2793 in[0] = new_Const(mode, get_mode_null(mode));
2795 in[0] = new_Unknown(mode);
2797 ret = new_Return(get_store(), 1, in);
2799 add_immBlock_pred(end_block, ret);
2802 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2803 mature_immBlock(imature_blocks[i]);
2805 DEL_ARR_F(imature_blocks);
2806 imature_blocks = NULL;
2808 mature_immBlock(first_block);
2809 mature_immBlock(end_block);
2811 irg_finalize_cons(irg);
2813 /* finalize the frame type */
2814 ir_type *frame_type = get_irg_frame_type(irg);
2815 int n = get_compound_n_members(frame_type);
2818 for(int i = 0; i < n; ++i) {
2819 ir_entity *entity = get_compound_member(frame_type, i);
2820 ir_type *entity_type = get_entity_type(entity);
2822 int align = get_type_alignment_bytes(entity_type);
2823 if(align > align_all)
2827 misalign = offset % align;
2829 offset += align - misalign;
2833 set_entity_offset(entity, offset);
2834 offset += get_type_size_bytes(entity_type);
2836 set_type_size_bytes(frame_type, offset);
2837 set_type_alignment_bytes(frame_type, align_all);
2838 set_type_state(frame_type, layout_fixed);
2843 static void create_global_variable(declaration_t *declaration)
2845 ir_type *global_type = get_glob_type();
2846 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2849 ir_entity *entity = declaration->v.entity;
2850 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2851 set_entity_visibility(entity, visibility_local);
2852 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2853 set_entity_visibility(entity, visibility_external_allocated);
2855 set_entity_visibility(entity, visibility_external_visible);
2857 current_ir_graph = get_const_code_irg();
2858 create_initializer(declaration);
2861 static void context_to_firm(context_t *context)
2863 /* first pass: create declarations */
2864 declaration_t *declaration = context->declarations;
2865 for( ; declaration != NULL; declaration = declaration->next) {
2866 if(declaration->namespc != NAMESPACE_NORMAL)
2868 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2869 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2871 if(declaration->symbol == NULL)
2874 type_t *type = declaration->type;
2875 if(type->type == TYPE_FUNCTION) {
2876 get_function_entity(declaration);
2878 create_global_variable(declaration);
2882 /* second pass: create code */
2883 declaration = context->declarations;
2884 for( ; declaration != NULL; declaration = declaration->next) {
2885 if(declaration->namespc != NAMESPACE_NORMAL)
2887 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2888 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2890 if(declaration->symbol == NULL)
2893 type_t *type = declaration->type;
2894 if(type->type != TYPE_FUNCTION)
2897 create_function(declaration);
2901 void translation_unit_to_firm(translation_unit_t *unit)
2903 /* just to be sure */
2904 continue_label = NULL;
2906 current_switch_cond = NULL;
2908 context_to_firm(& unit->context);