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 type_t* atomic_type)
124 switch(atomic_type->v.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 type_t *type)
178 switch(type->v.atomic_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(type_t *type)
215 ir_type *irtype = get_ir_type(type);
216 return get_type_size_bytes(irtype);
219 static unsigned get_array_type_size(type_t *type)
221 ir_type *irtype = get_ir_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(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(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(type);
250 panic("Trying to determine size of invalid type");
253 static unsigned count_parameters(const type_t *type)
257 function_parameter_t *parameter = type->v.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 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 type_t *type)
281 type_t *result_type = type->v.function_type.result_type;
283 ident *id = unique_ident("functiontype");
284 int n_parameters = count_parameters(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 = type->v.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(type->v.function_type.variadic || type->v.function_type.unspecified_parameters) {
302 set_method_variadicity(irtype, variadicity_variadic);
308 static ir_type *create_pointer_type(type_t *type)
310 type_t *points_to = type->v.pointer_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->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(type_t *type)
328 type_t *element_type = type->v.array_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->v.array_type.size != NULL) {
335 int n_elements = fold_constant(type->v.array_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(type_t *type)
356 symbol_t *symbol = type->v.compound_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->firm_type = ir_type;
369 declaration_t *entry = type->v.compound_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(type_t *type)
409 declaration_t *declaration = type->v.compound_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->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(type_t *const type)
463 type->firm_type = ir_type_int;
465 ir_mode *const mode = get_ir_mode(type);
466 tarval *const one = get_mode_one(mode);
467 tarval * tv_next = get_tarval_null(mode);
469 for (declaration_t *decl = type->v.enum_type.declaration;;) {
471 if (decl == NULL || decl->storage_class != STORAGE_CLASS_ENUM_ENTRY)
474 decl->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
476 expression_t *const init = decl->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 decl->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(type);
508 firm_type = create_method_type(type);
511 firm_type = create_pointer_type(type);
514 firm_type = create_array_type(type);
516 case TYPE_COMPOUND_STRUCT:
517 firm_type = create_struct_type(type);
519 case TYPE_COMPOUND_UNION:
520 firm_type = create_union_type(type);
523 firm_type = create_enum_type(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);
803 if (function->datatype->type == TYPE_POINTER) {
804 type_t *const ptr_type = function->datatype;
805 assert(ptr_type->v.pointer_type.points_to->type == TYPE_FUNCTION);
806 type = ptr_type->v.pointer_type.points_to;
808 assert(function->datatype->type == TYPE_FUNCTION);
809 type = 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*) type);
819 ir_type *new_method_type = NULL;
820 if(type->v.function_type.variadic || type->v.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 = type->v.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(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 unsigned elem_size = get_type_size(type->v.pointer_type.points_to);
935 offset = new_Const_long(mode_Is, elem_size);
937 assert(is_type_arithmetic(type));
938 offset = new_Const(mode, get_mode_one(mode));
941 switch(expression->type) {
942 case UNEXPR_POSTFIX_INCREMENT: {
943 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
944 set_value_for_expression(value, new_value);
947 case UNEXPR_POSTFIX_DECREMENT: {
948 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
949 set_value_for_expression(value, new_value);
952 case UNEXPR_PREFIX_INCREMENT: {
953 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
954 set_value_for_expression(value, new_value);
957 case UNEXPR_PREFIX_DECREMENT: {
958 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
959 set_value_for_expression(value, new_value);
963 panic("no incdec expr in create_incdec");
968 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
970 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
971 type_t *type = skip_typeref(expression->expression.datatype);
973 if(expression->type == UNEXPR_TAKE_ADDRESS)
974 return expression_to_addr(expression->value);
976 const expression_t *value = expression->value;
977 ir_node *value_node = expression_to_firm(value);
979 switch(expression->type) {
981 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
984 case UNEXPR_BITWISE_NEGATE:
985 return new_d_Not(dbgi, value_node, get_ir_mode(type));
987 if(get_irn_mode(value_node) != mode_b) {
988 value_node = create_conv(dbgi, value_node, mode_b);
990 value_node = new_d_Not(dbgi, value_node, mode_b);
991 ir_mode *const mode = get_ir_mode(type);
993 value_node = create_conv(dbgi, value_node, mode);
997 case UNEXPR_DEREFERENCE:
998 return deref_address(type, value_node, dbgi);
999 case UNEXPR_POSTFIX_INCREMENT:
1000 case UNEXPR_POSTFIX_DECREMENT:
1001 case UNEXPR_PREFIX_INCREMENT:
1002 case UNEXPR_PREFIX_DECREMENT:
1003 return create_incdec(expression);
1005 return create_conv(dbgi, value_node, get_ir_mode(type));
1007 case UNEXPR_TAKE_ADDRESS:
1008 case UNEXPR_INVALID:
1011 panic("invalid UNEXPR type found");
1014 static long get_pnc(binary_expression_type_t type)
1017 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1018 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1019 case BINEXPR_LESS: return pn_Cmp_Lt;
1020 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1021 case BINEXPR_GREATER: return pn_Cmp_Gt;
1022 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1026 panic("trying to get pn_Cmp from non-comparison binexpr type");
1029 static ir_node *create_lazy_op(const binary_expression_t *expression)
1031 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1032 type_t *type = expression->expression.datatype;
1033 ir_mode *mode = get_ir_mode(type);
1035 ir_node *cur_block = get_cur_block();
1037 ir_node *one_block = new_immBlock();
1038 ir_node *one = new_Const(mode, get_mode_one(mode));
1039 ir_node *jmp_one = new_d_Jmp(dbgi);
1041 ir_node *zero_block = new_immBlock();
1042 ir_node *zero = new_Const(mode, get_mode_null(mode));
1043 ir_node *jmp_zero = new_d_Jmp(dbgi);
1045 set_cur_block(cur_block);
1046 create_condition_evaluation((const expression_t*) expression,
1047 one_block, zero_block);
1048 mature_immBlock(one_block);
1049 mature_immBlock(zero_block);
1051 ir_node *common_block = new_immBlock();
1052 add_immBlock_pred(common_block, jmp_one);
1053 add_immBlock_pred(common_block, jmp_zero);
1054 mature_immBlock(common_block);
1056 ir_node *in[2] = { one, zero };
1057 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1062 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1063 ir_node *right, ir_mode *mode);
1065 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1066 create_arithmetic_func func)
1068 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1069 ir_node *left = expression_to_firm(expression->left);
1070 ir_node *right = expression_to_firm(expression->right);
1071 type_t *type = expression->right->datatype;
1072 /* be careful with the modes, because in arithmetic assign nodes only
1073 * the right operand has the mode of the arithmetic already */
1074 ir_mode *mode = get_ir_mode(type);
1075 left = create_conv(dbgi, left, mode);
1076 ir_node *res = func(dbgi, left, right, mode);
1081 static ir_node *pointer_arithmetic(ir_node *const pointer,
1084 dbg_info *const dbgi,
1085 const create_arithmetic_func func)
1087 type_t *const points_to = type->v.pointer_type.points_to;
1088 const unsigned elem_size = get_type_size(points_to);
1090 assert(elem_size >= 1);
1091 if (elem_size > 1) {
1092 integer = create_conv(dbgi, integer, mode_Is);
1093 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1094 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1098 ir_mode *const mode = get_ir_mode(type);
1099 return func(dbgi, pointer, integer, mode);
1102 static ir_node *create_arithmetic_assign_binop(
1103 const binary_expression_t *expression, create_arithmetic_func func)
1105 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1106 type_t *const type = expression->expression.datatype;
1109 if (type->type == TYPE_POINTER) {
1110 ir_node *const pointer = expression_to_firm(expression->left);
1111 ir_node * integer = expression_to_firm(expression->right);
1112 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1114 value = create_arithmetic_binop(expression, func);
1117 ir_mode *const mode = get_ir_mode(type);
1118 value = create_conv(dbgi, value, mode);
1119 set_value_for_expression(expression->left, value);
1124 static ir_node *create_add(const binary_expression_t *expression)
1126 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1127 ir_node *left = expression_to_firm(expression->left);
1128 ir_node *right = expression_to_firm(expression->right);
1129 type_t *type = expression->expression.datatype;
1131 expression_t *expr_left = expression->left;
1132 expression_t *expr_right = expression->right;
1133 type_t *type_left = skip_typeref(expr_left->datatype);
1134 type_t *type_right = skip_typeref(expr_right->datatype);
1136 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1137 ir_mode *const mode = get_ir_mode(type);
1138 return new_d_Add(dbgi, left, right, mode);
1141 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1142 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1144 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1145 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1149 static ir_node *create_sub(const binary_expression_t *expression)
1151 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1152 expression_t *const expr_left = expression->left;
1153 expression_t *const expr_right = expression->right;
1154 ir_node *const left = expression_to_firm(expr_left);
1155 ir_node *const right = expression_to_firm(expr_right);
1156 type_t *const type = expression->expression.datatype;
1157 type_t *const type_left = skip_typeref(expr_left->datatype);
1158 type_t *const type_right = skip_typeref(expr_right->datatype);
1160 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1161 ir_mode *const mode = get_ir_mode(type);
1162 return new_d_Sub(dbgi, left, right, mode);
1163 } else if (type_left->type == TYPE_POINTER && type_right->type == TYPE_POINTER) {
1164 const type_t *const ptr_type = type_left;
1165 const unsigned elem_size = get_type_size(ptr_type->v.pointer_type.points_to);
1166 ir_mode *const mode = get_ir_mode(type);
1167 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1168 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1169 ir_node *const no_mem = new_NoMem();
1170 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1171 op_pin_state_floats);
1172 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1175 assert(type_left->type == TYPE_POINTER);
1176 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1179 static ir_node *create_shift(const binary_expression_t *expression)
1181 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1182 ir_node *left = expression_to_firm(expression->left);
1183 ir_node *right = expression_to_firm(expression->right);
1184 type_t *type = expression->expression.datatype;
1185 ir_mode *mode = get_ir_mode(type);
1187 /* firm always wants the shift count to be unsigned */
1188 right = create_conv(dbgi, right, mode_Iu);
1192 switch(expression->type) {
1193 case BINEXPR_SHIFTLEFT_ASSIGN:
1194 case BINEXPR_SHIFTLEFT:
1195 res = new_d_Shl(dbgi, left, right, mode);
1197 case BINEXPR_SHIFTRIGHT_ASSIGN:
1198 case BINEXPR_SHIFTRIGHT: {
1199 expression_t *expr_left = expression->left;
1200 type_t *type_left = skip_typeref(expr_left->datatype);
1202 if(is_type_signed(type_left)) {
1203 res = new_d_Shrs(dbgi, left, right, mode);
1205 res = new_d_Shr(dbgi, left, right, mode);
1210 panic("create shift op called for non-shift op");
1217 static ir_node *create_divmod(const binary_expression_t *expression)
1219 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1220 ir_node *left = expression_to_firm(expression->left);
1221 ir_node *right = expression_to_firm(expression->right);
1222 ir_node *pin = new_Pin(new_NoMem());
1223 /* be careful with the modes, because in arithmetic assign nodes only
1224 * the right operand has the mode of the arithmetic already */
1225 type_t *type = expression->right->datatype;
1226 ir_mode *mode = get_ir_mode(type);
1227 left = create_conv(dbgi, left, mode);
1231 switch (expression->type) {
1233 case BINEXPR_DIV_ASSIGN:
1234 if(mode_is_float(mode)) {
1235 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1236 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1238 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1239 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1244 case BINEXPR_MOD_ASSIGN:
1245 assert(!mode_is_float(mode));
1246 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1247 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1250 default: panic("unexpected binary expression type in create_divmod()");
1256 static ir_node *create_arithmetic_assign_divmod(
1257 const binary_expression_t *expression)
1259 ir_node * value = create_divmod(expression);
1260 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1261 type_t *const type = expression->expression.datatype;
1262 ir_mode *const mode = get_ir_mode(type);
1264 assert(type->type != TYPE_POINTER);
1266 value = create_conv(dbgi, value, mode);
1267 set_value_for_expression(expression->left, value);
1272 static ir_node *create_arithmetic_assign_shift(
1273 const binary_expression_t *expression)
1275 ir_node * value = create_shift(expression);
1276 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1277 type_t *const type = expression->expression.datatype;
1278 ir_mode *const mode = get_ir_mode(type);
1280 value = create_conv(dbgi, value, mode);
1281 set_value_for_expression(expression->left, value);
1286 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1288 binary_expression_type_t type = expression->type;
1291 case BINEXPR_NOTEQUAL:
1293 case BINEXPR_LESSEQUAL:
1294 case BINEXPR_GREATER:
1295 case BINEXPR_GREATEREQUAL: {
1296 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1297 ir_node *left = expression_to_firm(expression->left);
1298 ir_node *right = expression_to_firm(expression->right);
1299 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1300 long pnc = get_pnc(type);
1301 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1304 case BINEXPR_ASSIGN: {
1305 ir_node *right = expression_to_firm(expression->right);
1306 set_value_for_expression(expression->left, right);
1310 return create_add(expression);
1312 return create_sub(expression);
1314 return create_arithmetic_binop(expression, new_d_Mul);
1315 case BINEXPR_BITWISE_AND:
1316 return create_arithmetic_binop(expression, new_d_And);
1317 case BINEXPR_BITWISE_OR:
1318 return create_arithmetic_binop(expression, new_d_Or);
1319 case BINEXPR_BITWISE_XOR:
1320 return create_arithmetic_binop(expression, new_d_Eor);
1321 case BINEXPR_SHIFTLEFT:
1322 case BINEXPR_SHIFTRIGHT:
1323 return create_shift(expression);
1326 return create_divmod(expression);
1327 case BINEXPR_LOGICAL_AND:
1328 case BINEXPR_LOGICAL_OR:
1329 return create_lazy_op(expression);
1331 expression_to_firm(expression->left);
1332 return expression_to_firm(expression->right);
1333 case BINEXPR_ADD_ASSIGN:
1334 return create_arithmetic_assign_binop(expression, new_d_Add);
1335 case BINEXPR_SUB_ASSIGN:
1336 return create_arithmetic_assign_binop(expression, new_d_Sub);
1337 case BINEXPR_MUL_ASSIGN:
1338 return create_arithmetic_assign_binop(expression, new_d_Mul);
1339 case BINEXPR_DIV_ASSIGN:
1340 return create_arithmetic_assign_divmod(expression);
1341 case BINEXPR_BITWISE_AND_ASSIGN:
1342 return create_arithmetic_assign_binop(expression, new_d_And);
1343 case BINEXPR_BITWISE_OR_ASSIGN:
1344 return create_arithmetic_assign_binop(expression, new_d_Or);
1345 case BINEXPR_BITWISE_XOR_ASSIGN:
1346 return create_arithmetic_assign_binop(expression, new_d_Eor);
1347 case BINEXPR_SHIFTLEFT_ASSIGN:
1348 case BINEXPR_SHIFTRIGHT_ASSIGN:
1349 return create_arithmetic_assign_shift(expression);
1351 panic("TODO binexpr type");
1355 static ir_node *array_access_addr(const array_access_expression_t *expression)
1357 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1361 type_t *type_left = skip_typeref(expression->array_ref->datatype);
1362 type_t *type_right = skip_typeref(expression->index->datatype);
1364 if(type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1365 base_addr = expression_to_firm(expression->array_ref);
1366 offset = expression_to_firm(expression->index);
1368 assert(type_right->type == TYPE_POINTER
1369 || type_right->type == TYPE_ARRAY);
1370 base_addr = expression_to_firm(expression->index);
1371 offset = expression_to_firm(expression->array_ref);
1373 offset = create_conv(dbgi, offset, mode_Iu);
1375 unsigned elem_size = get_type_size(expression->expression.datatype);
1376 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1377 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1379 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1384 static ir_node *array_access_to_firm(
1385 const array_access_expression_t *expression)
1387 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1388 ir_node *addr = array_access_addr(expression);
1389 type_t *type = skip_typeref(expression->expression.datatype);
1390 return deref_address(type, addr, dbgi);
1393 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1395 type_t *type = expression->type;
1397 type = expression->size_expression->datatype;
1398 assert(type != NULL);
1401 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1402 unsigned size = get_type_size(type);
1403 ir_node *size_node = new_Const_long(mode, size);
1408 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1410 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1412 ir_node *cur_block = get_cur_block();
1414 /* create the true block */
1415 ir_node *true_block = new_immBlock();
1417 ir_node *true_val = expression_to_firm(expression->true_expression);
1418 ir_node *true_jmp = new_Jmp();
1420 /* create the false block */
1421 ir_node *false_block = new_immBlock();
1423 ir_node *false_val = expression_to_firm(expression->false_expression);
1424 ir_node *false_jmp = new_Jmp();
1426 /* create the condition evaluation */
1427 set_cur_block(cur_block);
1428 create_condition_evaluation(expression->condition, true_block, false_block);
1429 mature_immBlock(true_block);
1430 mature_immBlock(false_block);
1432 /* create the common block */
1433 ir_node *common_block = new_immBlock();
1434 add_immBlock_pred(common_block, true_jmp);
1435 add_immBlock_pred(common_block, false_jmp);
1436 mature_immBlock(common_block);
1438 /* TODO improve static semantics, so either both or no values are NULL */
1439 if (true_val == NULL || false_val == NULL) return NULL;
1441 ir_node *in[2] = { true_val, false_val };
1442 ir_mode *mode = get_irn_mode(true_val);
1443 assert(get_irn_mode(false_val) == mode);
1444 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1449 static ir_node *select_addr(const select_expression_t *expression)
1451 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1453 ir_node *compound_addr = expression_to_firm(expression->compound);
1455 declaration_t *entry = expression->compound_entry;
1456 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1457 ir_entity *entity = entry->v.entity;
1459 assert(entity != NULL);
1461 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1466 static ir_node *select_to_firm(const select_expression_t *expression)
1468 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1469 ir_node *addr = select_addr(expression);
1470 type_t *type = skip_typeref(expression->expression.datatype);
1471 return deref_address(type, addr, dbgi);
1474 /* Values returned by __builtin_classify_type. */
1475 typedef enum gcc_type_class
1481 enumeral_type_class,
1484 reference_type_class,
1488 function_type_class,
1499 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1501 const type_t *const type = expr->type_expression->datatype;
1507 switch (type->v.atomic_type.atype) {
1508 // should not be reached
1509 case ATOMIC_TYPE_INVALID:
1513 // gcc cannot do that
1514 case ATOMIC_TYPE_VOID:
1515 tc = void_type_class;
1518 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1519 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1520 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1521 case ATOMIC_TYPE_SHORT:
1522 case ATOMIC_TYPE_USHORT:
1523 case ATOMIC_TYPE_INT:
1524 case ATOMIC_TYPE_UINT:
1525 case ATOMIC_TYPE_LONG:
1526 case ATOMIC_TYPE_ULONG:
1527 case ATOMIC_TYPE_LONGLONG:
1528 case ATOMIC_TYPE_ULONGLONG:
1529 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1530 tc = integer_type_class;
1533 case ATOMIC_TYPE_FLOAT:
1534 case ATOMIC_TYPE_DOUBLE:
1535 case ATOMIC_TYPE_LONG_DOUBLE:
1536 tc = real_type_class;
1539 #ifdef PROVIDE_COMPLEX
1540 case ATOMIC_TYPE_FLOAT_COMPLEX:
1541 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1542 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1543 tc = complex_type_class;
1545 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1546 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1547 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1548 tc = complex_type_class;
1553 panic("Unimplemented case in classify_type_to_firm().");
1557 case TYPE_ARRAY: // gcc handles this as pointer
1558 case TYPE_FUNCTION: // gcc handles this as pointer
1559 case TYPE_POINTER: tc = pointer_type_class; break;
1560 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1561 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1563 // gcc handles this as integer
1564 case TYPE_ENUM: tc = integer_type_class; break;
1567 panic("Unimplemented case in classify_type_to_firm().");
1570 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1571 ir_mode *const mode = mode_Is;
1572 tarval *const tv = new_tarval_from_long(tc, mode);
1573 return new_d_Const(dbgi, mode, tv);
1576 static ir_node *function_name_to_firm(const string_literal_t *const expr)
1578 if (current_function_name == NULL) {
1579 const source_position_t *const src_pos =
1580 &expr->expression.source_position;
1581 const char *const name = current_function_decl->symbol->string;
1582 current_function_name = string_to_firm(src_pos, "__func__", name);
1585 return current_function_name;
1588 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1590 statement_t *statement = expr->statement;
1592 assert(statement->type == STATEMENT_COMPOUND);
1593 return compound_statement_to_firm(statement);
1596 static ir_node *dereference_addr(const unary_expression_t *const expression)
1598 assert(expression->type == UNEXPR_DEREFERENCE);
1599 return expression_to_firm(expression->value);
1602 static ir_node *expression_to_addr(const expression_t *expression)
1604 switch(expression->type) {
1605 case EXPR_REFERENCE:
1606 return reference_addr((const reference_expression_t*) expression);
1607 case EXPR_ARRAY_ACCESS:
1608 return array_access_addr((const array_access_expression_t*) expression);
1610 return select_addr((const select_expression_t*) expression);
1612 const unary_expression_t *const unary_expr =
1613 (const unary_expression_t*)expression;
1614 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1615 return dereference_addr(unary_expr);
1622 panic("trying to get address of non-lvalue");
1625 static ir_node *_expression_to_firm(const expression_t *expression)
1627 switch(expression->type) {
1629 return const_to_firm((const const_t*) expression);
1630 case EXPR_STRING_LITERAL:
1631 return string_literal_to_firm((const string_literal_t*) expression);
1632 case EXPR_REFERENCE:
1633 return reference_expression_to_firm(
1634 (const reference_expression_t*) expression);
1636 return call_expression_to_firm((const call_expression_t*) expression);
1638 return unary_expression_to_firm((const unary_expression_t*) expression);
1640 return binary_expression_to_firm(
1641 (const binary_expression_t*) expression);
1642 case EXPR_ARRAY_ACCESS:
1643 return array_access_to_firm(
1644 (const array_access_expression_t*) expression);
1646 return sizeof_to_firm((const sizeof_expression_t*) expression);
1647 case EXPR_CONDITIONAL:
1648 return conditional_to_firm((const conditional_expression_t*)expression);
1650 return select_to_firm((const select_expression_t*) expression);
1651 case EXPR_CLASSIFY_TYPE:
1652 return classify_type_to_firm((const classify_type_expression_t*)expression);
1654 case EXPR_PRETTY_FUNCTION:
1655 return function_name_to_firm((const string_literal_t*)expression);
1656 case EXPR_STATEMENT:
1657 return statement_expression_to_firm(
1658 (const statement_expression_t*) expression);
1661 case EXPR_BUILTIN_SYMBOL:
1662 panic("unimplemented expression found");
1668 panic("invalid expression found");
1671 static ir_node *expression_to_firm(const expression_t *expression)
1673 ir_node *res = _expression_to_firm(expression);
1675 if(res != NULL && get_irn_mode(res) == mode_b) {
1676 ir_mode *mode = get_ir_mode(expression->datatype);
1677 res = create_conv(NULL, res, mode);
1683 static ir_node *expression_to_modeb(const expression_t *expression)
1685 ir_node *res = _expression_to_firm(expression);
1686 res = create_conv(NULL, res, mode_b);
1692 * create a short-circuit expression evaluation that tries to construct
1693 * efficient control flow structures for &&, || and ! expressions
1695 static void create_condition_evaluation(const expression_t *expression,
1696 ir_node *true_block,
1697 ir_node *false_block)
1699 switch(expression->type) {
1701 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1702 if(unary_expression->type == UNEXPR_NOT) {
1703 create_condition_evaluation(unary_expression->value, false_block,
1710 binary_expression_t *binary_expression
1711 = (binary_expression_t*) expression;
1712 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1713 ir_node *cur_block = get_cur_block();
1714 ir_node *extra_block = new_immBlock();
1715 set_cur_block(cur_block);
1716 create_condition_evaluation(binary_expression->left, extra_block,
1718 mature_immBlock(extra_block);
1719 set_cur_block(extra_block);
1720 create_condition_evaluation(binary_expression->right, true_block,
1724 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1725 ir_node *cur_block = get_cur_block();
1726 ir_node *extra_block = new_immBlock();
1727 set_cur_block(cur_block);
1728 create_condition_evaluation(binary_expression->left, true_block,
1730 mature_immBlock(extra_block);
1731 set_cur_block(extra_block);
1732 create_condition_evaluation(binary_expression->right, true_block,
1742 dbg_info *dbgi = get_dbg_info(&expression->source_position);
1743 ir_node *condition = expression_to_modeb(expression);
1744 ir_node *cond = new_d_Cond(dbgi, condition);
1745 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1746 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1748 add_immBlock_pred(true_block, true_proj);
1749 add_immBlock_pred(false_block, false_proj);
1751 set_cur_block(NULL);
1755 static void return_statement_to_firm(statement_t *statement)
1757 if(get_cur_block() == NULL)
1760 dbg_info *dbgi = get_dbg_info(&statement->source_position);
1763 if(statement->v.return_value != NULL) {
1764 ir_node *retval = expression_to_firm(statement->v.return_value);
1768 ret = new_d_Return(dbgi, get_store(), 1, in);
1770 ret = new_d_Return(dbgi, get_store(), 0, NULL);
1772 ir_node *end_block = get_irg_end_block(current_ir_graph);
1773 add_immBlock_pred(end_block, ret);
1775 set_cur_block(NULL);
1778 static ir_node *expression_statement_to_firm(statement_t *statement)
1780 if(get_cur_block() == NULL)
1783 return expression_to_firm(statement->v.expression);
1786 static ir_node *compound_statement_to_firm(statement_t *compound)
1788 ir_node *result = NULL;
1789 statement_t *statement = compound->v.compound_stmt.statements;
1790 for( ; statement != NULL; statement = statement->next) {
1791 //context2firm(&statement->context);
1793 if(statement->next == NULL && statement->type == STATEMENT_EXPRESSION) {
1794 result = expression_statement_to_firm(statement);
1797 statement_to_firm(statement);
1803 static void if_statement_to_firm(statement_t *statement)
1805 ir_node *cur_block = get_cur_block();
1807 ir_node *fallthrough_block = new_immBlock();
1809 /* the true (blocks) */
1810 ir_node *true_block;
1811 if (statement->v.if_stmt.true_statement != NULL) {
1812 true_block = new_immBlock();
1813 statement_to_firm(statement->v.if_stmt.true_statement);
1814 if(get_cur_block() != NULL) {
1815 ir_node *jmp = new_Jmp();
1816 add_immBlock_pred(fallthrough_block, jmp);
1819 true_block = fallthrough_block;
1822 /* the false (blocks) */
1823 ir_node *false_block;
1824 if(statement->v.if_stmt.false_statement != NULL) {
1825 false_block = new_immBlock();
1827 statement_to_firm(statement->v.if_stmt.false_statement);
1828 if(get_cur_block() != NULL) {
1829 ir_node *jmp = new_Jmp();
1830 add_immBlock_pred(fallthrough_block, jmp);
1833 false_block = fallthrough_block;
1836 /* create the condition */
1837 if(cur_block != NULL) {
1838 set_cur_block(cur_block);
1839 create_condition_evaluation(statement->v.if_stmt.condition, true_block,
1843 mature_immBlock(true_block);
1844 if(false_block != fallthrough_block) {
1845 mature_immBlock(false_block);
1847 mature_immBlock(fallthrough_block);
1849 set_cur_block(fallthrough_block);
1852 static void while_statement_to_firm(statement_t *statement)
1854 ir_node *jmp = NULL;
1855 if(get_cur_block() != NULL) {
1859 /* create the header block */
1860 ir_node *header_block = new_immBlock();
1862 add_immBlock_pred(header_block, jmp);
1865 /* the false block */
1866 ir_node *false_block = new_immBlock();
1869 ir_node *body_block;
1870 if (statement->v.while_stmt.body != NULL) {
1871 ir_node *old_continue_label = continue_label;
1872 ir_node *old_break_label = break_label;
1873 continue_label = header_block;
1874 break_label = false_block;
1876 body_block = new_immBlock();
1877 statement_to_firm(statement->v.while_stmt.body);
1879 assert(continue_label == header_block);
1880 assert(break_label == false_block);
1881 continue_label = old_continue_label;
1882 break_label = old_break_label;
1884 if(get_cur_block() != NULL) {
1885 ir_node *jmp = new_Jmp();
1886 add_immBlock_pred(header_block, jmp);
1889 body_block = header_block;
1892 /* create the condition */
1893 set_cur_block(header_block);
1895 create_condition_evaluation(statement->v.while_stmt.condition, body_block, false_block);
1896 mature_immBlock(body_block);
1897 mature_immBlock(false_block);
1898 mature_immBlock(header_block);
1900 set_cur_block(false_block);
1903 static void do_while_statement_to_firm(statement_t *statement)
1905 ir_node *jmp = NULL;
1906 if(get_cur_block() != NULL) {
1910 /* create the header block */
1911 ir_node *header_block = new_immBlock();
1913 /* the false block */
1914 ir_node *false_block = new_immBlock();
1917 ir_node *body_block = new_immBlock();
1919 add_immBlock_pred(body_block, jmp);
1922 if (statement->v.while_stmt.body != NULL) {
1923 ir_node *old_continue_label = continue_label;
1924 ir_node *old_break_label = break_label;
1925 continue_label = header_block;
1926 break_label = false_block;
1928 statement_to_firm(statement->v.while_stmt.body);
1930 assert(continue_label == header_block);
1931 assert(break_label == false_block);
1932 continue_label = old_continue_label;
1933 break_label = old_break_label;
1935 if (get_cur_block() == NULL) {
1936 mature_immBlock(header_block);
1937 mature_immBlock(body_block);
1938 mature_immBlock(false_block);
1943 ir_node *body_jmp = new_Jmp();
1944 add_immBlock_pred(header_block, body_jmp);
1945 mature_immBlock(header_block);
1947 /* create the condition */
1948 set_cur_block(header_block);
1950 create_condition_evaluation(statement->v.while_stmt.condition, body_block, false_block);
1951 mature_immBlock(body_block);
1952 mature_immBlock(false_block);
1953 mature_immBlock(header_block);
1955 set_cur_block(false_block);
1958 static void for_statement_to_firm(statement_t *statement)
1960 ir_node *jmp = NULL;
1961 if (get_cur_block() != NULL) {
1962 if(statement->v.for_stmt.initialisation != NULL) {
1963 expression_to_firm(statement->v.for_stmt.initialisation);
1968 /* create the step block */
1969 ir_node *const step_block = new_immBlock();
1970 if (statement->v.for_stmt.step != NULL) {
1971 expression_to_firm(statement->v.for_stmt.step);
1973 ir_node *const step_jmp = new_Jmp();
1975 /* create the header block */
1976 ir_node *const header_block = new_immBlock();
1978 add_immBlock_pred(header_block, jmp);
1980 add_immBlock_pred(header_block, step_jmp);
1982 /* the false block */
1983 ir_node *const false_block = new_immBlock();
1986 ir_node * body_block;
1987 if (statement->v.for_stmt.body != NULL) {
1988 ir_node *const old_continue_label = continue_label;
1989 ir_node *const old_break_label = break_label;
1990 continue_label = step_block;
1991 break_label = false_block;
1993 body_block = new_immBlock();
1994 statement_to_firm(statement->v.for_stmt.body);
1996 assert(continue_label == step_block);
1997 assert(break_label == false_block);
1998 continue_label = old_continue_label;
1999 break_label = old_break_label;
2001 if (get_cur_block() != NULL) {
2002 ir_node *const jmp = new_Jmp();
2003 add_immBlock_pred(step_block, jmp);
2006 body_block = step_block;
2009 /* create the condition */
2010 set_cur_block(header_block);
2011 if (statement->v.for_stmt.condition != NULL) {
2012 create_condition_evaluation(statement->v.for_stmt.condition, body_block,
2015 keep_alive(header_block);
2016 ir_node *jmp = new_Jmp();
2017 add_immBlock_pred(body_block, jmp);
2020 mature_immBlock(body_block);
2021 mature_immBlock(false_block);
2022 mature_immBlock(step_block);
2023 mature_immBlock(header_block);
2024 mature_immBlock(false_block);
2026 set_cur_block(false_block);
2029 static void create_declaration_entity(declaration_t *declaration,
2030 declaration_type_t declaration_type,
2031 ir_type *parent_type)
2033 ident *id = new_id_from_str(declaration->symbol->string);
2034 ir_type *irtype = get_ir_type(declaration->type);
2035 ir_entity *entity = new_entity(parent_type, id, irtype);
2036 set_entity_ld_ident(entity, id);
2038 declaration->declaration_type = declaration_type;
2039 declaration->v.entity = entity;
2040 set_entity_variability(entity, variability_uninitialized);
2041 /* TODO: visibility? */
2044 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2046 enum compound_graph_entry_type_t {
2047 COMPOUND_GRAPH_ENTRY_ARRAY,
2048 COMPOUND_GRAPH_ENTRY_COMPOUND
2051 struct compound_graph_path_entry_t {
2057 compound_graph_path_entry_t *prev;
2060 static void create_initializer_object(initializer_t *initializer, type_t *type,
2061 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2063 static compound_graph_path *create_compound_path(ir_type *type,
2064 compound_graph_path_entry_t *entry, int len)
2066 compound_graph_path *path = new_compound_graph_path(type, len);
2069 for( ; entry != NULL; entry = entry->prev, --i) {
2071 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2072 set_compound_graph_path_node(path, i, entry->v.entity);
2074 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2075 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2083 static void create_initializer_value(initializer_t *initializer,
2085 compound_graph_path_entry_t *entry,
2088 ir_node *node = expression_to_firm(initializer->v.value);
2089 ir_type *type = get_entity_type(entity);
2090 compound_graph_path *path = create_compound_path(type, entry, len);
2091 add_compound_ent_value_w_path(entity, node, path);
2094 static void create_initializer_compound(initializer_t *initializer,
2097 compound_graph_path_entry_t *last_entry,
2100 declaration_t *compound_declaration = type->v.compound_type.declaration;
2102 declaration_t *compound_entry = compound_declaration->context.declarations;
2104 compound_graph_path_entry_t entry;
2105 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2106 entry.prev = last_entry;
2110 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2111 if(compound_entry->symbol == NULL)
2113 if(compound_entry->namespc != NAMESPACE_NORMAL)
2116 if(i >= initializer->v.list.len)
2119 entry.v.entity = compound_entry->v.entity;
2121 initializer_t *sub_initializer = initializer->v.list.initializers[i];
2123 assert(compound_entry != NULL);
2124 assert(compound_entry->declaration_type
2125 == DECLARATION_TYPE_COMPOUND_MEMBER);
2127 if(sub_initializer->type == INITIALIZER_VALUE) {
2128 create_initializer_value(sub_initializer, entity, &entry, len);
2130 type_t *type = skip_typeref(compound_entry->type);
2131 create_initializer_object(sub_initializer, type, entity, &entry,
2139 static void create_initializer_array(initializer_t *initializer,
2140 type_t *type, ir_entity *entity,
2141 compound_graph_path_entry_t *last_entry,
2144 type_t *element_type = type->v.array_type.element_type;
2145 element_type = skip_typeref(element_type);
2147 compound_graph_path_entry_t entry;
2148 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2149 entry.prev = last_entry;
2152 for(size_t i = 0; i < initializer->v.list.len; ++i) {
2153 entry.v.array_index = i;
2155 initializer_t *sub_initializer = initializer->v.list.initializers[i];
2157 if(sub_initializer->type == INITIALIZER_VALUE) {
2158 create_initializer_value(sub_initializer, entity, &entry, len);
2160 assert(sub_initializer->type == INITIALIZER_LIST);
2161 create_initializer_object(sub_initializer, element_type, entity,
2167 static void create_initializer_string(initializer_t *initializer,
2168 type_t *type, ir_entity *entity,
2169 compound_graph_path_entry_t *last_entry,
2172 type_t *element_type = type->v.array_type.element_type;
2173 element_type = skip_typeref(element_type);
2175 compound_graph_path_entry_t entry;
2176 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2177 entry.prev = last_entry;
2180 ir_type *irtype = get_entity_type(entity);
2181 size_t arr_len = get_array_type_size(type);
2182 const char *p = initializer->v.string;
2184 for(i = 0; i < arr_len; ++i, ++p) {
2185 entry.v.array_index = i;
2187 ir_node *node = new_Const_long(mode_Bs, *p);
2188 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2189 add_compound_ent_value_w_path(entity, node, path);
2196 static void create_initializer_object(initializer_t *initializer, type_t *type,
2197 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2199 if(type->type == TYPE_ARRAY) {
2200 if(initializer->type == INITIALIZER_STRING) {
2201 create_initializer_string(initializer, type, entity, entry, len);
2203 assert(initializer->type == INITIALIZER_LIST);
2204 create_initializer_array(initializer, type, entity, entry, len);
2207 assert(initializer->type == INITIALIZER_LIST);
2209 assert(type->type == TYPE_COMPOUND_STRUCT
2210 || type->type == TYPE_COMPOUND_UNION);
2211 create_initializer_compound(initializer, type, entity, entry, len);
2215 static void create_initializer_local_variable_entity(declaration_t *declaration)
2217 initializer_t *initializer = declaration->init.initializer;
2218 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2219 ir_entity *entity = declaration->v.entity;
2220 ir_node *memory = get_store();
2221 ir_node *nomem = new_NoMem();
2222 ir_node *frame = get_irg_frame(current_ir_graph);
2223 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2225 if(is_atomic_entity(entity)) {
2226 assert(initializer->type == INITIALIZER_VALUE);
2228 ir_node *value = expression_to_firm(initializer->v.value);
2229 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2230 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2231 set_store(store_mem);
2235 /* create a "template" entity which is copied to the entity on the stack */
2236 ident *id = unique_ident("initializer");
2237 ir_type *irtype = get_ir_type(declaration->type);
2238 ir_type *global_type = get_glob_type();
2239 ir_entity *init_entity = new_entity(global_type, id, irtype);
2240 set_entity_ld_ident(init_entity, id);
2242 set_entity_variability(init_entity, variability_initialized);
2243 set_entity_visibility(init_entity, visibility_local);
2245 ir_graph *old_current_ir_graph = current_ir_graph;
2246 current_ir_graph = get_const_code_irg();
2248 type_t *type = skip_typeref(declaration->type);
2249 create_initializer_object(initializer, type, init_entity, NULL, 0);
2251 assert(current_ir_graph == get_const_code_irg());
2252 current_ir_graph = old_current_ir_graph;
2254 ir_node *src_addr = create_symconst(dbgi, init_entity);
2255 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2257 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2258 set_store(copyb_mem);
2261 static void create_initializer(declaration_t *declaration)
2263 initializer_t *initializer = declaration->init.initializer;
2264 if(initializer == NULL)
2267 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2268 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2269 create_initializer_local_variable_entity(declaration);
2273 if(initializer->type == INITIALIZER_VALUE) {
2274 ir_node *value = expression_to_firm(initializer->v.value);
2276 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2277 set_value(declaration->v.value_number, value);
2279 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2281 ir_entity *entity = declaration->v.entity;
2283 set_entity_variability(entity, variability_initialized);
2284 set_atomic_ent_value(entity, value);
2287 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2288 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2289 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2291 ir_entity *entity = declaration->v.entity;
2292 set_entity_variability(entity, variability_initialized);
2294 type_t *type = skip_typeref(declaration->type);
2295 create_initializer_object(initializer, type, entity, NULL, 0);
2299 static void create_local_variable(declaration_t *declaration)
2301 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2303 bool needs_entity = declaration->address_taken;
2304 type_t *type = skip_typeref(declaration->type);
2306 if(type->type == TYPE_ARRAY
2307 || type->type == TYPE_COMPOUND_STRUCT
2308 || type->type == TYPE_COMPOUND_UNION) {
2309 needs_entity = true;
2313 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2314 create_declaration_entity(declaration,
2315 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2318 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2319 declaration->v.value_number = next_value_number_function;
2320 ++next_value_number_function;
2323 create_initializer(declaration);
2326 static void create_local_static_variable(declaration_t *declaration)
2328 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2330 type_t *type = skip_typeref(declaration->type);
2331 ir_type *global_type = get_glob_type();
2332 ident *id = unique_ident(declaration->symbol->string);
2333 ir_type *irtype = get_ir_type(type);
2334 ir_entity *entity = new_entity(global_type, id, irtype);
2335 set_entity_ld_ident(entity, id);
2337 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2338 declaration->v.entity = entity;
2339 set_entity_variability(entity, variability_uninitialized);
2340 set_entity_visibility(entity, visibility_local);
2342 ir_graph *old_current_ir_graph = current_ir_graph;
2343 current_ir_graph = get_const_code_irg();
2345 create_initializer(declaration);
2347 assert(current_ir_graph == get_const_code_irg());
2348 current_ir_graph = old_current_ir_graph;
2351 static void declaration_statement_to_firm(statement_t *statement)
2353 declaration_t *declaration = statement->v.declaration_stmt.begin;
2354 declaration_t *end = statement->v.declaration_stmt.end->next;
2355 for( ; declaration != end; declaration = declaration->next) {
2356 type_t *type = declaration->type;
2358 switch(declaration->storage_class) {
2359 case STORAGE_CLASS_TYPEDEF:
2361 case STORAGE_CLASS_STATIC:
2362 create_local_static_variable(declaration);
2364 case STORAGE_CLASS_ENUM_ENTRY:
2365 panic("enum entry declaration in local block found");
2366 case STORAGE_CLASS_EXTERN:
2367 panic("extern declaration in local block found");
2368 case STORAGE_CLASS_NONE:
2369 case STORAGE_CLASS_AUTO:
2370 case STORAGE_CLASS_REGISTER:
2371 if(type->type == TYPE_FUNCTION) {
2372 panic("nested functions not supported yet");
2374 create_local_variable(declaration);
2378 panic("invalid storage class found");
2382 static void create_jump_statement(const statement_t *statement,
2383 ir_node *target_block)
2385 if(get_cur_block() == NULL)
2388 dbg_info *dbgi = get_dbg_info(&statement->source_position);
2389 ir_node *jump = new_d_Jmp(dbgi);
2390 add_immBlock_pred(target_block, jump);
2392 set_cur_block(NULL);
2395 static void switch_statement_to_firm(const statement_t *statement)
2397 dbg_info *dbgi = get_dbg_info(&statement->source_position);
2399 ir_node *expression = expression_to_firm(statement->v.switch_stmt.expression);
2400 ir_node *cond = new_d_Cond(dbgi, expression);
2401 ir_node *break_block = new_immBlock();
2403 set_cur_block(NULL);
2405 ir_node *const old_switch_cond = current_switch_cond;
2406 ir_node *const old_break_label = break_label;
2407 const bool old_saw_default_label = saw_default_label;
2408 current_switch_cond = cond;
2409 break_label = break_block;
2411 statement_to_firm(statement->v.switch_stmt.body);
2413 if(get_cur_block() != NULL) {
2414 ir_node *jmp = new_Jmp();
2415 add_immBlock_pred(break_block, jmp);
2418 if (!saw_default_label) {
2419 set_cur_block(get_nodes_block(cond));
2420 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2421 MAGIC_DEFAULT_PN_NUMBER);
2422 add_immBlock_pred(break_block, proj);
2425 assert(current_switch_cond == cond);
2426 assert(break_label == break_block);
2427 current_switch_cond = old_switch_cond;
2428 break_label = old_break_label;
2429 saw_default_label = old_saw_default_label;
2431 mature_immBlock(break_block);
2432 set_cur_block(break_block);
2435 static long fold_constant(const expression_t *expression)
2437 ir_graph *old_current_ir_graph = current_ir_graph;
2438 current_ir_graph = get_const_code_irg();
2440 ir_node *cnst = expression_to_firm(expression);
2441 if(!is_Const(cnst)) {
2442 panic("couldn't fold constant");
2444 tarval *tv = get_Const_tarval(cnst);
2445 if(!tarval_is_long(tv)) {
2446 panic("folded constant not an integer");
2449 long res = get_tarval_long(tv);
2451 current_ir_graph = old_current_ir_graph;
2455 static void case_label_to_firm(const statement_t *statement)
2457 dbg_info *dbgi = get_dbg_info(&statement->source_position);
2459 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2461 /* let's create a node and hope firm constant folding creates a Const
2464 set_cur_block(get_nodes_block(current_switch_cond));
2465 if(statement->v.case_label_stmt.expression) {
2466 long pn = fold_constant(statement->v.case_label_stmt.expression);
2467 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2468 /* oops someone detected our cheating... */
2469 panic("magic default pn used");
2471 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2473 saw_default_label = true;
2474 proj = new_d_defaultProj(dbgi, current_switch_cond,
2475 MAGIC_DEFAULT_PN_NUMBER);
2478 ir_node *block = new_immBlock();
2479 if (fallthrough != NULL) {
2480 add_immBlock_pred(block, fallthrough);
2482 add_immBlock_pred(block, proj);
2483 mature_immBlock(block);
2485 statement_to_firm(statement->v.case_label_stmt.label_statement);
2488 static ir_node *get_label_block(declaration_t *label)
2490 assert(label->namespc == NAMESPACE_LABEL);
2492 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2493 return label->v.block;
2495 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2497 ir_node *old_cur_block = get_cur_block();
2498 ir_node *block = new_immBlock();
2499 set_cur_block(old_cur_block);
2501 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2502 label->v.block = block;
2504 ARR_APP1(ir_node *, imature_blocks, block);
2509 static void label_to_firm(const statement_t *statement)
2511 ir_node *block = get_label_block(statement->v.label_stmt.label);
2513 if(get_cur_block() != NULL) {
2514 ir_node *jmp = new_Jmp();
2515 add_immBlock_pred(block, jmp);
2518 set_cur_block(block);
2521 statement_to_firm(statement->v.label_stmt.label_statement);
2524 static void goto_to_firm(const statement_t *statement)
2526 if(get_cur_block() == NULL)
2529 ir_node *block = get_label_block(statement->v.goto_label);
2530 ir_node *jmp = new_Jmp();
2531 add_immBlock_pred(block, jmp);
2533 set_cur_block(NULL);
2536 static void statement_to_firm(statement_t *statement)
2538 switch(statement->type) {
2539 case STATEMENT_COMPOUND:
2540 compound_statement_to_firm(statement);
2542 case STATEMENT_RETURN:
2543 return_statement_to_firm(statement);
2545 case STATEMENT_EXPRESSION:
2546 expression_statement_to_firm(statement);
2549 if_statement_to_firm(statement);
2551 case STATEMENT_WHILE:
2552 while_statement_to_firm(statement);
2554 case STATEMENT_DO_WHILE:
2555 do_while_statement_to_firm(statement);
2557 case STATEMENT_DECLARATION:
2558 declaration_statement_to_firm(statement);
2560 case STATEMENT_BREAK:
2561 create_jump_statement(statement, break_label);
2563 case STATEMENT_CONTINUE:
2564 create_jump_statement(statement, continue_label);
2566 case STATEMENT_SWITCH:
2567 switch_statement_to_firm(statement);
2569 case STATEMENT_CASE_LABEL:
2570 case_label_to_firm(statement);
2573 for_statement_to_firm(statement);
2575 case STATEMENT_LABEL:
2576 label_to_firm(statement);
2578 case STATEMENT_GOTO:
2579 goto_to_firm(statement);
2584 panic("Statement not implemented\n");
2587 static int count_local_declarations(const declaration_t * decl,
2588 const declaration_t *const end)
2591 for (; decl != end; decl = decl->next) {
2592 const type_t *type = skip_typeref(decl->type);
2593 switch (type->type) {
2597 if (!decl->address_taken) ++count;
2606 static int count_decls_in_stmts(const statement_t *stmt)
2609 for (; stmt != NULL; stmt = stmt->next) {
2610 switch (stmt->type) {
2611 case STATEMENT_DECLARATION:
2612 count += count_local_declarations(stmt->v.declaration_stmt.begin,
2613 stmt->v.declaration_stmt.end->next);
2616 case STATEMENT_COMPOUND:
2617 count += count_decls_in_stmts(stmt->v.compound_stmt.statements);
2621 count += count_decls_in_stmts(stmt->v.if_stmt.true_statement);
2622 count += count_decls_in_stmts(stmt->v.if_stmt.false_statement);
2625 case STATEMENT_SWITCH:
2626 count += count_decls_in_stmts(stmt->v.switch_stmt.body);
2629 case STATEMENT_LABEL:
2630 count += count_decls_in_stmts(stmt->v.label_stmt.label_statement);
2633 case STATEMENT_WHILE:
2634 count += count_decls_in_stmts(stmt->v.while_stmt.body);
2637 case STATEMENT_DO_WHILE:
2638 count += count_decls_in_stmts(stmt->v.while_stmt.body);
2641 case STATEMENT_FOR: {
2642 /* TODO initialisation */
2643 count += count_decls_in_stmts(stmt->v.for_stmt.body);
2647 case STATEMENT_BREAK:
2648 case STATEMENT_CASE_LABEL:
2649 case STATEMENT_CONTINUE:
2650 case STATEMENT_EXPRESSION:
2651 case STATEMENT_GOTO:
2652 case STATEMENT_INVALID:
2653 case STATEMENT_RETURN:
2660 static int get_function_n_local_vars(declaration_t *declaration)
2664 /* count parameters */
2665 count += count_local_declarations(declaration->context.declarations, NULL);
2667 /* count local variables declared in body */
2668 count += count_decls_in_stmts(declaration->init.statement);
2670 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2671 * for expression statements... */
2677 static void initialize_function_parameters(declaration_t *declaration)
2679 ir_graph *irg = current_ir_graph;
2680 ir_node *args = get_irg_args(irg);
2681 ir_node *start_block = get_irg_start_block(irg);
2682 ir_type *function_irtype = get_ir_type(declaration->type);
2685 declaration_t *parameter = declaration->context.declarations;
2686 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2687 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2688 type_t *type = skip_typeref(parameter->type);
2690 bool needs_entity = parameter->address_taken;
2691 if(type->type == TYPE_COMPOUND_STRUCT
2692 || type->type == TYPE_COMPOUND_UNION) {
2693 needs_entity = true;
2697 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2698 ident *id = new_id_from_str(parameter->symbol->string);
2699 set_entity_ident(entity, id);
2701 parameter->declaration_type
2702 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2703 parameter->v.entity = entity;
2707 ir_mode *mode = get_ir_mode(parameter->type);
2709 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2711 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2712 parameter->v.value_number = next_value_number_function;
2713 ++next_value_number_function;
2715 set_value(parameter->v.value_number, proj);
2719 static void create_function(declaration_t *declaration)
2721 ir_entity *entity = get_function_entity(declaration);
2723 if(declaration->init.statement == NULL)
2726 current_function_decl = declaration;
2727 current_function_name = NULL;
2729 assert(imature_blocks == NULL);
2730 imature_blocks = NEW_ARR_F(ir_node*, 0);
2732 int n_local_vars = get_function_n_local_vars(declaration);
2733 ir_graph *irg = new_ir_graph(entity, n_local_vars);
2734 ir_node *first_block = get_cur_block();
2736 next_value_number_function = 0;
2737 initialize_function_parameters(declaration);
2739 statement_to_firm(declaration->init.statement);
2741 ir_node *end_block = get_irg_end_block(irg);
2743 /* do we have a return statement yet? */
2744 if(get_cur_block() != NULL) {
2745 assert(declaration->type->type == TYPE_FUNCTION);
2746 const type_t* const type = declaration->type;
2748 if (type->v.function_type.result_type == type_void) {
2749 ret = new_Return(get_store(), 0, NULL);
2751 ir_mode *const mode = get_ir_mode(type->v.function_type.result_type);
2753 // ยง5.1.2.2.3 main implicitly returns 0
2754 if (strcmp(declaration->symbol->string, "main") == 0) {
2755 in[0] = new_Const(mode, get_mode_null(mode));
2757 in[0] = new_Unknown(mode);
2759 ret = new_Return(get_store(), 1, in);
2761 add_immBlock_pred(end_block, ret);
2764 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2765 mature_immBlock(imature_blocks[i]);
2767 DEL_ARR_F(imature_blocks);
2768 imature_blocks = NULL;
2770 mature_immBlock(first_block);
2771 mature_immBlock(end_block);
2773 irg_finalize_cons(irg);
2775 /* finalize the frame type */
2776 ir_type *frame_type = get_irg_frame_type(irg);
2777 int n = get_compound_n_members(frame_type);
2780 for(int i = 0; i < n; ++i) {
2781 ir_entity *entity = get_compound_member(frame_type, i);
2782 ir_type *entity_type = get_entity_type(entity);
2784 int align = get_type_alignment_bytes(entity_type);
2785 if(align > align_all)
2789 misalign = offset % align;
2791 offset += align - misalign;
2795 set_entity_offset(entity, offset);
2796 offset += get_type_size_bytes(entity_type);
2798 set_type_size_bytes(frame_type, offset);
2799 set_type_alignment_bytes(frame_type, align_all);
2800 set_type_state(frame_type, layout_fixed);
2805 static void create_global_variable(declaration_t *declaration)
2807 ir_type *global_type = get_glob_type();
2808 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2811 ir_entity *entity = declaration->v.entity;
2812 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2813 set_entity_visibility(entity, visibility_local);
2814 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2815 set_entity_visibility(entity, visibility_external_allocated);
2817 set_entity_visibility(entity, visibility_external_visible);
2819 current_ir_graph = get_const_code_irg();
2820 create_initializer(declaration);
2823 static void context_to_firm(context_t *context)
2825 /* first pass: create declarations */
2826 declaration_t *declaration = context->declarations;
2827 for( ; declaration != NULL; declaration = declaration->next) {
2828 if(declaration->namespc != NAMESPACE_NORMAL)
2830 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2831 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2833 if(declaration->symbol == NULL)
2836 type_t *type = declaration->type;
2837 if(type->type == TYPE_FUNCTION) {
2838 get_function_entity(declaration);
2840 create_global_variable(declaration);
2844 /* second pass: create code */
2845 declaration = context->declarations;
2846 for( ; declaration != NULL; declaration = declaration->next) {
2847 if(declaration->namespc != NAMESPACE_NORMAL)
2849 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2850 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2852 if(declaration->symbol == NULL)
2855 type_t *type = declaration->type;
2856 if(type->type != TYPE_FUNCTION)
2859 create_function(declaration);
2863 void translation_unit_to_firm(translation_unit_t *unit)
2865 /* just to be sure */
2866 continue_label = NULL;
2868 current_switch_cond = NULL;
2870 context_to_firm(& unit->context);