9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
15 #include "adt/array.h"
21 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
23 static ir_type *ir_type_const_char;
24 static ir_type *ir_type_void;
25 static ir_type *ir_type_int;
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
100 type_void->base.firm_type = ir_type_void;
102 symbol_alloca = symbol_table_insert("__builtin_alloca");
105 void exit_ast2firm(void)
109 static unsigned unique_id = 0;
111 static ident *unique_ident(const char *tag)
115 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
117 return new_id_from_str(buf);
120 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
122 switch(atomic_type->atype) {
123 case ATOMIC_TYPE_SCHAR:
124 case ATOMIC_TYPE_CHAR:
126 case ATOMIC_TYPE_UCHAR:
128 case ATOMIC_TYPE_SHORT:
130 case ATOMIC_TYPE_USHORT:
132 case ATOMIC_TYPE_LONG:
133 case ATOMIC_TYPE_INT:
135 case ATOMIC_TYPE_ULONG:
136 case ATOMIC_TYPE_UINT:
138 case ATOMIC_TYPE_LONGLONG:
140 case ATOMIC_TYPE_ULONGLONG:
142 case ATOMIC_TYPE_FLOAT:
144 case ATOMIC_TYPE_DOUBLE:
146 case ATOMIC_TYPE_LONG_DOUBLE:
148 case ATOMIC_TYPE_BOOL:
150 #ifdef PROVIDE_COMPLEX
151 case ATOMIC_TYPE_FLOAT_COMPLEX:
152 case ATOMIC_TYPE_DOUBLE_COMPLEX:
153 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
154 panic("complex lowering not implemented yet");
156 case ATOMIC_TYPE_FLOAT_IMAGINARY:
157 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
158 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
159 panic("imaginary lowering not implemented yet");
162 case ATOMIC_TYPE_VOID:
163 /* firm has no real void... */
165 case ATOMIC_TYPE_INVALID:
168 panic("Encountered unknown atomic type");
172 static unsigned get_type_size(type_t *type);
174 static unsigned get_atomic_type_size(const atomic_type_t *type)
176 switch(type->atype) {
177 case ATOMIC_TYPE_CHAR:
178 case ATOMIC_TYPE_SCHAR:
179 case ATOMIC_TYPE_UCHAR:
182 case ATOMIC_TYPE_SHORT:
183 case ATOMIC_TYPE_USHORT:
186 case ATOMIC_TYPE_BOOL:
187 case ATOMIC_TYPE_INT:
188 case ATOMIC_TYPE_UINT:
189 case ATOMIC_TYPE_LONG:
190 case ATOMIC_TYPE_ULONG:
191 case ATOMIC_TYPE_FLOAT:
194 case ATOMIC_TYPE_LONGLONG:
195 case ATOMIC_TYPE_ULONGLONG:
196 case ATOMIC_TYPE_DOUBLE:
199 case ATOMIC_TYPE_LONG_DOUBLE:
202 case ATOMIC_TYPE_VOID:
205 case ATOMIC_TYPE_INVALID:
208 panic("Trying to determine size of invalid atomic type");
211 static unsigned get_compound_type_size(compound_type_t *type)
213 ir_type *irtype = get_ir_type((type_t*) type);
214 return get_type_size_bytes(irtype);
217 static unsigned get_array_type_size(array_type_t *type)
219 ir_type *irtype = get_ir_type((type_t*) type);
220 return get_type_size_bytes(irtype);
223 static unsigned get_type_size(type_t *type)
225 type = skip_typeref(type);
229 return get_atomic_type_size(&type->atomic);
231 return get_mode_size_bytes(mode_Is);
232 case TYPE_COMPOUND_UNION:
233 case TYPE_COMPOUND_STRUCT:
234 return get_compound_type_size(&type->compound);
236 /* just a pointer to the function */
237 return get_mode_size_bytes(mode_P_code);
239 return get_mode_size_bytes(mode_P_data);
241 return get_array_type_size(&type->array);
249 panic("Trying to determine size of invalid type");
252 static unsigned count_parameters(const function_type_t *function_type)
256 function_parameter_t *parameter = function_type->parameters;
257 for ( ; parameter != NULL; parameter = parameter->next) {
267 static long fold_constant(const expression_t *expression);
269 static ir_type *create_atomic_type(const atomic_type_t *type)
271 ir_mode *mode = get_atomic_mode(type);
272 ident *id = get_mode_ident(mode);
273 ir_type *irtype = new_type_primitive(id, mode);
278 static ir_type *create_method_type(const function_type_t *function_type)
280 type_t *result_type = function_type->result_type;
282 ident *id = unique_ident("functiontype");
283 int n_parameters = count_parameters(function_type);
284 int n_results = result_type == type_void ? 0 : 1;
285 ir_type *irtype = new_type_method(id, n_parameters, n_results);
287 if(result_type != type_void) {
288 ir_type *restype = get_ir_type(result_type);
289 set_method_res_type(irtype, 0, restype);
292 function_parameter_t *parameter = function_type->parameters;
294 for( ; parameter != NULL; parameter = parameter->next) {
295 ir_type *p_irtype = get_ir_type(parameter->type);
296 set_method_param_type(irtype, n, p_irtype);
300 if(function_type->variadic || function_type->unspecified_parameters) {
301 set_method_variadicity(irtype, variadicity_variadic);
307 static ir_type *create_pointer_type(pointer_type_t *type)
309 type_t *points_to = type->points_to;
310 ir_type *ir_points_to;
311 /* Avoid endless recursion if the points_to type contains this poiner type
312 * again (might be a struct). We therefore first create a void* pointer
313 * and then set the real points_to type
315 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
316 ir_type_void, mode_P_data);
317 type->type.firm_type = ir_type;
319 ir_points_to = get_ir_type(points_to);
320 set_pointer_points_to_type(ir_type, ir_points_to);
325 static ir_type *create_array_type(array_type_t *type)
327 type_t *element_type = type->element_type;
328 ir_type *ir_element_type = get_ir_type(element_type);
330 ident *id = unique_ident("array");
331 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
333 if(type->size != NULL) {
334 int n_elements = fold_constant(type->size);
336 set_array_bounds_int(ir_type, 0, 0, n_elements);
338 size_t elemsize = get_type_size_bytes(ir_element_type);
339 int align = get_type_alignment_bytes(ir_element_type);
340 if(elemsize % align > 0) {
341 elemsize += align - (elemsize % align);
343 set_type_size_bytes(ir_type, n_elements * elemsize);
344 set_type_alignment_bytes(ir_type, align);
345 set_type_state(ir_type, layout_fixed);
351 #define INVALID_TYPE ((ir_type_ptr)-1)
353 static ir_type *create_struct_type(compound_type_t *type)
355 symbol_t *symbol = type->declaration->symbol;
358 id = unique_ident(symbol->string);
360 id = unique_ident("__anonymous_struct");
362 ir_type *ir_type = new_type_struct(id);
364 type->type.firm_type = ir_type;
368 declaration_t *entry = type->declaration->context.declarations;
369 for( ; entry != NULL; entry = entry->next) {
370 if(entry->namespc != NAMESPACE_NORMAL)
373 ident *ident = new_id_from_str(entry->symbol->string);
374 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
376 int entry_size = get_type_size_bytes(entry_ir_type);
377 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
378 int misalign = offset % entry_alignment;
380 offset += entry_alignment - misalign;
382 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
383 set_entity_offset(entity, offset);
384 add_struct_member(ir_type, entity);
385 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
386 entry->v.entity = entity;
388 offset += entry_size;
389 if(entry_alignment > align_all) {
390 if(entry_alignment % align_all != 0) {
391 panic("Uneven alignments not supported yet");
393 align_all = entry_alignment;
397 int misalign = offset % align_all;
399 set_type_alignment_bytes(ir_type, align_all);
400 set_type_size_bytes(ir_type, offset);
401 set_type_state(ir_type, layout_fixed);
406 static ir_type *create_union_type(compound_type_t *type)
408 declaration_t *declaration = type->declaration;
409 symbol_t *symbol = declaration->symbol;
412 id = unique_ident(symbol->string);
414 id = unique_ident("__anonymous_union");
416 ir_type *ir_type = new_type_union(id);
418 type->type.firm_type = ir_type;
422 declaration_t *entry = declaration->context.declarations;
423 for( ; entry != NULL; entry = entry->next) {
424 if(entry->namespc != NAMESPACE_NORMAL)
427 ident *ident = new_id_from_str(entry->symbol->string);
428 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
430 int entry_size = get_type_size_bytes(entry_ir_type);
431 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
433 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
434 add_union_member(ir_type, entity);
435 set_entity_offset(entity, 0);
436 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
437 entry->v.entity = entity;
439 if(entry_size > size) {
442 if(entry_alignment > align_all) {
443 if(entry_alignment % align_all != 0) {
444 panic("Uneven alignments not supported yet");
446 align_all = entry_alignment;
450 set_type_alignment_bytes(ir_type, align_all);
451 set_type_size_bytes(ir_type, size);
452 set_type_state(ir_type, layout_fixed);
457 static ir_node *expression_to_firm(const expression_t *expression);
458 static inline ir_mode *get_ir_mode(type_t *type);
460 static ir_type *create_enum_type(enum_type_t *const type)
462 type->type.firm_type = ir_type_int;
464 ir_mode *const mode = get_ir_mode((type_t*) type);
465 tarval *const one = get_mode_one(mode);
466 tarval * tv_next = get_tarval_null(mode);
468 declaration_t *declaration = type->declaration->next;
469 for (; declaration != NULL; declaration = declaration->next) {
470 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
473 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
475 expression_t *const init = declaration->init.enum_value;
477 ir_node *const cnst = expression_to_firm(init);
478 if (!is_Const(cnst)) {
479 panic("couldn't fold constant");
481 tv_next = get_Const_tarval(cnst);
483 declaration->v.enum_val = tv_next;
484 tv_next = tarval_add(tv_next, one);
490 static ir_type *get_ir_type(type_t *type)
492 assert(type != NULL);
494 type = skip_typeref(type);
496 if(type->base.firm_type != NULL) {
497 assert(type->base.firm_type != INVALID_TYPE);
498 return type->base.firm_type;
501 ir_type *firm_type = NULL;
504 firm_type = create_atomic_type(&type->atomic);
507 firm_type = create_method_type(&type->function);
510 firm_type = create_pointer_type(&type->pointer);
513 firm_type = create_array_type(&type->array);
515 case TYPE_COMPOUND_STRUCT:
516 firm_type = create_struct_type(&type->compound);
518 case TYPE_COMPOUND_UNION:
519 firm_type = create_union_type(&type->compound);
522 firm_type = create_enum_type(&type->enumt);
531 if(firm_type == NULL)
532 panic("unknown type found");
534 type->base.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_expression_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",
602 (unsigned long long) cnst->v.int_value);
604 tv = new_tarval_from_str(buf, len, mode);
607 return new_d_Const(dbgi, mode, tv);
610 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
612 assert(entity != NULL);
613 union symconst_symbol sym;
614 sym.entity_p = entity;
615 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
618 static ir_node *string_to_firm(const source_position_t *const src_pos,
619 const char *const id_prefix,
620 const char *const string)
622 ir_type *const global_type = get_glob_type();
623 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
626 ident *const id = unique_ident(id_prefix);
627 ir_entity *const entity = new_entity(global_type, id, type);
628 set_entity_ld_ident(entity, id);
629 set_entity_variability(entity, variability_constant);
631 ir_type *const elem_type = ir_type_const_char;
632 ir_mode *const mode = get_type_mode(elem_type);
634 const size_t slen = strlen(string) + 1;
636 set_array_lower_bound_int(type, 0, 0);
637 set_array_upper_bound_int(type, 0, slen);
638 set_type_size_bytes(type, slen);
639 set_type_state(type, layout_fixed);
641 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
642 for(size_t i = 0; i < slen; ++i) {
643 tvs[i] = new_tarval_from_long(string[i], mode);
646 set_array_entity_values(entity, tvs, slen);
649 dbg_info *const dbgi = get_dbg_info(src_pos);
651 return create_symconst(dbgi, entity);
654 static ir_node *string_literal_to_firm(
655 const string_literal_expression_t* literal)
657 return string_to_firm(&literal->expression.source_position, "Lstr",
661 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
662 dbg_info *const dbgi)
664 if(is_compound_type(irtype) || is_Array_type(irtype)) {
668 ir_mode *const mode = get_type_mode(irtype);
669 ir_node *const memory = get_store();
670 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
671 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
672 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
677 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
679 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
680 declaration_t *declaration = ref->declaration;
681 type_t *type = skip_typeref(declaration->type);
683 switch((declaration_type_t) declaration->declaration_type) {
684 case DECLARATION_TYPE_UNKNOWN:
685 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
691 case DECLARATION_TYPE_ENUM_ENTRY: {
692 ir_mode *const mode = get_ir_mode(type);
693 return new_Const(mode, declaration->v.enum_val);
696 case DECLARATION_TYPE_LOCAL_VARIABLE: {
697 ir_mode *mode = get_ir_mode(type);
698 return get_value(declaration->v.value_number, mode);
700 case DECLARATION_TYPE_FUNCTION: {
701 return create_symconst(dbgi, declaration->v.entity);
703 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
704 ir_entity *entity = declaration->v.entity;
705 ir_node *symconst = create_symconst(dbgi, entity);
706 ir_type *irtype = get_entity_type(entity);
707 return deref_address(irtype, symconst, dbgi);
709 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
710 ir_entity *entity = declaration->v.entity;
711 ir_node *frame = get_irg_frame(current_ir_graph);
712 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
713 ir_type *irtype = get_entity_type(entity);
714 return deref_address(irtype, sel, dbgi);
717 case DECLARATION_TYPE_COMPOUND_MEMBER:
718 case DECLARATION_TYPE_LABEL_BLOCK:
719 panic("not implemented reference type");
722 panic("reference to declaration with unknown type found");
725 static ir_node *reference_addr(const reference_expression_t *ref)
727 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
728 declaration_t *declaration = ref->declaration;
730 switch((declaration_type_t) declaration->declaration_type) {
731 case DECLARATION_TYPE_UNKNOWN:
733 case DECLARATION_TYPE_LOCAL_VARIABLE:
734 panic("local variable without entity has no address");
735 case DECLARATION_TYPE_FUNCTION: {
736 return create_symconst(dbgi, declaration->v.entity);
738 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
739 ir_entity *entity = declaration->v.entity;
740 ir_node *symconst = create_symconst(dbgi, entity);
743 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
744 ir_entity *entity = declaration->v.entity;
745 ir_node *frame = get_irg_frame(current_ir_graph);
746 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
751 case DECLARATION_TYPE_ENUM_ENTRY:
752 panic("trying to reference enum entry");
754 case DECLARATION_TYPE_COMPOUND_MEMBER:
755 case DECLARATION_TYPE_LABEL_BLOCK:
756 panic("not implemented reference type");
759 panic("reference to declaration with unknown type found");
762 static ir_node *process_builtin_call(const call_expression_t *call)
764 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
766 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
767 builtin_symbol_expression_t *builtin
768 = (builtin_symbol_expression_t*) call->function;
769 symbol_t *symbol = builtin->symbol;
771 if(symbol == symbol_alloca) {
772 if(call->arguments == NULL || call->arguments->next != NULL) {
773 panic("invalid number of parameters on __builtin_alloca");
775 expression_t *argument = call->arguments->expression;
776 ir_node *size = expression_to_firm(argument);
778 ir_node *store = get_store();
779 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
781 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
783 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
787 panic("Unsupported builtin found\n");
791 static ir_node *call_expression_to_firm(const call_expression_t *call)
793 assert(get_cur_block() != NULL);
795 expression_t *function = call->function;
796 if(function->type == EXPR_BUILTIN_SYMBOL) {
797 return process_builtin_call(call);
799 ir_node *callee = expression_to_firm(function);
801 type_t *type = function->base.datatype;
802 assert(type->type == TYPE_POINTER);
803 pointer_type_t *const ptr_type = &type->pointer;
804 assert(ptr_type->points_to->type == TYPE_FUNCTION);
805 function_type_t *function_type = &ptr_type->points_to->function;
807 int n_parameters = 0;
808 call_argument_t *argument = call->arguments;
809 for( ; argument != NULL; argument = argument->next) {
813 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
814 ir_type *new_method_type = NULL;
815 if(function_type->variadic || function_type->unspecified_parameters) {
816 /* we need to construct a new method type matching the call
818 int n_res = get_method_n_ress(ir_method_type);
819 new_method_type = new_type_method(unique_ident("calltype"),
820 n_parameters, n_res);
821 set_method_calling_convention(new_method_type,
822 get_method_calling_convention(ir_method_type));
823 set_method_additional_properties(new_method_type,
824 get_method_additional_properties(ir_method_type));
826 for(int i = 0; i < n_res; ++i) {
827 set_method_res_type(new_method_type, i,
828 get_method_res_type(ir_method_type, i));
831 ir_node *in[n_parameters];
833 argument = call->arguments;
835 for( ; argument != NULL; argument = argument->next) {
836 expression_t *expression = argument->expression;
837 ir_node *arg_node = expression_to_firm(expression);
840 if(new_method_type != NULL) {
841 ir_type *irtype = get_ir_type(expression->base.datatype);
842 set_method_param_type(new_method_type, n, irtype);
847 assert(n == n_parameters);
849 if(new_method_type != NULL)
850 ir_method_type = new_method_type;
852 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
853 ir_node *store = get_store();
854 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
856 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
859 type_t *result_type = skip_typeref(function_type->result_type);
860 ir_node *result = NULL;
862 if(!is_type_atomic(result_type, ATOMIC_TYPE_VOID)) {
864 if(is_type_scalar(result_type)) {
865 mode = get_ir_mode(result_type);
869 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
870 result = new_d_Proj(dbgi, resproj, mode, 0);
876 static void statement_to_firm(statement_t *statement);
877 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
879 static ir_node *expression_to_addr(const expression_t *expression);
880 static void create_condition_evaluation(const expression_t *expression,
882 ir_node *false_block);
884 static void set_value_for_expression(const expression_t *expression,
887 if(expression->type == EXPR_REFERENCE) {
888 reference_expression_t *ref = (reference_expression_t*) expression;
890 declaration_t *declaration = ref->declaration;
891 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
892 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
893 set_value(declaration->v.value_number, value);
898 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
899 ir_node *addr = expression_to_addr(expression);
900 ir_node *memory = get_store();
902 type_t *type = skip_typeref(expression->base.datatype);
903 if(is_type_scalar(type)) {
904 ir_node *store = new_d_Store(dbgi, memory, addr, value);
905 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
906 set_store(store_mem);
908 ir_type *irtype = get_ir_type(type);
909 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
910 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
911 set_store(copyb_mem);
915 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
917 ir_mode *value_mode = get_irn_mode(value);
919 if (value_mode == dest_mode || is_Bad(value))
922 if(dest_mode == mode_b) {
923 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
924 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
925 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
929 return new_d_Conv(dbgi, value, dest_mode);
932 static ir_node *create_incdec(const unary_expression_t *expression)
934 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
935 type_t *type = expression->expression.datatype;
936 ir_mode *mode = get_ir_mode(type);
937 expression_t *value = expression->value;
939 ir_node *value_node = expression_to_firm(value);
942 if(type->type == TYPE_POINTER) {
943 pointer_type_t *pointer_type = &type->pointer;
944 unsigned elem_size = get_type_size(pointer_type->points_to);
945 offset = new_Const_long(mode_Is, elem_size);
947 assert(is_type_arithmetic(type));
948 offset = new_Const(mode, get_mode_one(mode));
951 switch(expression->type) {
952 case UNEXPR_POSTFIX_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_POSTFIX_DECREMENT: {
958 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
959 set_value_for_expression(value, new_value);
962 case UNEXPR_PREFIX_INCREMENT: {
963 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
964 set_value_for_expression(value, new_value);
967 case UNEXPR_PREFIX_DECREMENT: {
968 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
969 set_value_for_expression(value, new_value);
973 panic("no incdec expr in create_incdec");
978 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
980 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
981 type_t *type = skip_typeref(expression->expression.datatype);
983 if(expression->type == UNEXPR_TAKE_ADDRESS)
984 return expression_to_addr(expression->value);
986 const expression_t *value = expression->value;
987 ir_node *value_node = expression_to_firm(value);
989 switch(expression->type) {
991 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
994 case UNEXPR_BITWISE_NEGATE:
995 return new_d_Not(dbgi, value_node, get_ir_mode(type));
997 if(get_irn_mode(value_node) != mode_b) {
998 value_node = create_conv(dbgi, value_node, mode_b);
1000 value_node = new_d_Not(dbgi, value_node, mode_b);
1001 ir_mode *const mode = get_ir_mode(type);
1002 if(mode != mode_b) {
1003 value_node = create_conv(dbgi, value_node, mode);
1007 case UNEXPR_DEREFERENCE: {
1008 ir_type *irtype = get_ir_type(type);
1009 return deref_address(irtype, value_node, dbgi);
1011 case UNEXPR_POSTFIX_INCREMENT:
1012 case UNEXPR_POSTFIX_DECREMENT:
1013 case UNEXPR_PREFIX_INCREMENT:
1014 case UNEXPR_PREFIX_DECREMENT:
1015 return create_incdec(expression);
1017 return create_conv(dbgi, value_node, get_ir_mode(type));
1019 case UNEXPR_TAKE_ADDRESS:
1020 case UNEXPR_INVALID:
1023 panic("invalid UNEXPR type found");
1026 static long get_pnc(binary_expression_type_t type)
1029 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1030 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1031 case BINEXPR_LESS: return pn_Cmp_Lt;
1032 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1033 case BINEXPR_GREATER: return pn_Cmp_Gt;
1034 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1038 panic("trying to get pn_Cmp from non-comparison binexpr type");
1041 static ir_node *create_lazy_op(const binary_expression_t *expression)
1043 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1044 type_t *type = expression->expression.datatype;
1045 ir_mode *mode = get_ir_mode(type);
1047 ir_node *cur_block = get_cur_block();
1049 ir_node *one_block = new_immBlock();
1050 ir_node *one = new_Const(mode, get_mode_one(mode));
1051 ir_node *jmp_one = new_d_Jmp(dbgi);
1053 ir_node *zero_block = new_immBlock();
1054 ir_node *zero = new_Const(mode, get_mode_null(mode));
1055 ir_node *jmp_zero = new_d_Jmp(dbgi);
1057 set_cur_block(cur_block);
1058 create_condition_evaluation((const expression_t*) expression,
1059 one_block, zero_block);
1060 mature_immBlock(one_block);
1061 mature_immBlock(zero_block);
1063 ir_node *common_block = new_immBlock();
1064 add_immBlock_pred(common_block, jmp_one);
1065 add_immBlock_pred(common_block, jmp_zero);
1066 mature_immBlock(common_block);
1068 ir_node *in[2] = { one, zero };
1069 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1074 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1075 ir_node *right, ir_mode *mode);
1077 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1078 create_arithmetic_func func)
1080 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1081 ir_node *left = expression_to_firm(expression->left);
1082 ir_node *right = expression_to_firm(expression->right);
1083 type_t *type = expression->right->base.datatype;
1084 /* be careful with the modes, because in arithmetic assign nodes only
1085 * the right operand has the mode of the arithmetic already */
1086 ir_mode *mode = get_ir_mode(type);
1087 left = create_conv(dbgi, left, mode);
1088 ir_node *res = func(dbgi, left, right, mode);
1093 static ir_node *pointer_arithmetic(ir_node *const pointer,
1096 dbg_info *const dbgi,
1097 const create_arithmetic_func func)
1099 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1100 type_t *const points_to = pointer_type->points_to;
1101 const unsigned elem_size = get_type_size(points_to);
1103 assert(elem_size >= 1);
1104 if (elem_size > 1) {
1105 integer = create_conv(dbgi, integer, mode_Is);
1106 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1107 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1111 ir_mode *const mode = get_ir_mode(type);
1112 return func(dbgi, pointer, integer, mode);
1115 static ir_node *create_arithmetic_assign_binop(
1116 const binary_expression_t *expression, create_arithmetic_func func)
1118 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1119 type_t *const type = expression->expression.datatype;
1122 if (type->type == TYPE_POINTER) {
1123 ir_node *const pointer = expression_to_firm(expression->left);
1124 ir_node * integer = expression_to_firm(expression->right);
1125 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1127 value = create_arithmetic_binop(expression, func);
1130 ir_mode *const mode = get_ir_mode(type);
1131 value = create_conv(dbgi, value, mode);
1132 set_value_for_expression(expression->left, value);
1137 static ir_node *create_add(const binary_expression_t *expression)
1139 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1140 ir_node *left = expression_to_firm(expression->left);
1141 ir_node *right = expression_to_firm(expression->right);
1142 type_t *type = expression->expression.datatype;
1144 expression_t *expr_left = expression->left;
1145 expression_t *expr_right = expression->right;
1146 type_t *type_left = skip_typeref(expr_left->base.datatype);
1147 type_t *type_right = skip_typeref(expr_right->base.datatype);
1149 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1150 ir_mode *const mode = get_ir_mode(type);
1151 return new_d_Add(dbgi, left, right, mode);
1154 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1155 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1157 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1158 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1162 static ir_node *create_sub(const binary_expression_t *expression)
1164 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1165 expression_t *const expr_left = expression->left;
1166 expression_t *const expr_right = expression->right;
1167 ir_node *const left = expression_to_firm(expr_left);
1168 ir_node *const right = expression_to_firm(expr_right);
1169 type_t *const type = expression->expression.datatype;
1170 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1171 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1173 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1174 ir_mode *const mode = get_ir_mode(type);
1175 return new_d_Sub(dbgi, left, right, mode);
1176 } else if (type_left->type == TYPE_POINTER
1177 && type_right->type == TYPE_POINTER) {
1178 const pointer_type_t *const ptr_type = &type_left->pointer;
1179 const unsigned elem_size = get_type_size(ptr_type->points_to);
1180 ir_mode *const mode = get_ir_mode(type);
1181 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1182 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1183 ir_node *const no_mem = new_NoMem();
1184 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1185 op_pin_state_floats);
1186 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1189 assert(type_left->type == TYPE_POINTER);
1190 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1193 static ir_node *create_shift(const binary_expression_t *expression)
1195 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1196 ir_node *left = expression_to_firm(expression->left);
1197 ir_node *right = expression_to_firm(expression->right);
1198 type_t *type = expression->expression.datatype;
1199 ir_mode *mode = get_ir_mode(type);
1201 /* firm always wants the shift count to be unsigned */
1202 right = create_conv(dbgi, right, mode_Iu);
1206 switch(expression->type) {
1207 case BINEXPR_SHIFTLEFT_ASSIGN:
1208 case BINEXPR_SHIFTLEFT:
1209 res = new_d_Shl(dbgi, left, right, mode);
1211 case BINEXPR_SHIFTRIGHT_ASSIGN:
1212 case BINEXPR_SHIFTRIGHT: {
1213 expression_t *expr_left = expression->left;
1214 type_t *type_left = skip_typeref(expr_left->base.datatype);
1216 if(is_type_signed(type_left)) {
1217 res = new_d_Shrs(dbgi, left, right, mode);
1219 res = new_d_Shr(dbgi, left, right, mode);
1224 panic("create shift op called for non-shift op");
1231 static ir_node *create_divmod(const binary_expression_t *expression)
1233 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1234 ir_node *left = expression_to_firm(expression->left);
1235 ir_node *right = expression_to_firm(expression->right);
1236 ir_node *pin = new_Pin(new_NoMem());
1237 /* be careful with the modes, because in arithmetic assign nodes only
1238 * the right operand has the mode of the arithmetic already */
1239 type_t *type = expression->right->base.datatype;
1240 ir_mode *mode = get_ir_mode(type);
1241 left = create_conv(dbgi, left, mode);
1245 switch (expression->type) {
1247 case BINEXPR_DIV_ASSIGN:
1248 if(mode_is_float(mode)) {
1249 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1250 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1252 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1253 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1258 case BINEXPR_MOD_ASSIGN:
1259 assert(!mode_is_float(mode));
1260 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1261 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1264 default: panic("unexpected binary expression type in create_divmod()");
1270 static ir_node *create_arithmetic_assign_divmod(
1271 const binary_expression_t *expression)
1273 ir_node * value = create_divmod(expression);
1274 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1275 type_t *const type = expression->expression.datatype;
1276 ir_mode *const mode = get_ir_mode(type);
1278 assert(type->type != TYPE_POINTER);
1280 value = create_conv(dbgi, value, mode);
1281 set_value_for_expression(expression->left, value);
1286 static ir_node *create_arithmetic_assign_shift(
1287 const binary_expression_t *expression)
1289 ir_node * value = create_shift(expression);
1290 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1291 type_t *const type = expression->expression.datatype;
1292 ir_mode *const mode = get_ir_mode(type);
1294 value = create_conv(dbgi, value, mode);
1295 set_value_for_expression(expression->left, value);
1300 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1302 binary_expression_type_t type = expression->type;
1305 case BINEXPR_NOTEQUAL:
1307 case BINEXPR_LESSEQUAL:
1308 case BINEXPR_GREATER:
1309 case BINEXPR_GREATEREQUAL: {
1310 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1311 ir_node *left = expression_to_firm(expression->left);
1312 ir_node *right = expression_to_firm(expression->right);
1313 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1314 long pnc = get_pnc(type);
1315 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1318 case BINEXPR_ASSIGN: {
1319 ir_node *right = expression_to_firm(expression->right);
1320 set_value_for_expression(expression->left, right);
1325 return create_add(expression);
1327 return create_sub(expression);
1329 return create_arithmetic_binop(expression, new_d_Mul);
1330 case BINEXPR_BITWISE_AND:
1331 return create_arithmetic_binop(expression, new_d_And);
1332 case BINEXPR_BITWISE_OR:
1333 return create_arithmetic_binop(expression, new_d_Or);
1334 case BINEXPR_BITWISE_XOR:
1335 return create_arithmetic_binop(expression, new_d_Eor);
1336 case BINEXPR_SHIFTLEFT:
1337 case BINEXPR_SHIFTRIGHT:
1338 return create_shift(expression);
1341 return create_divmod(expression);
1342 case BINEXPR_LOGICAL_AND:
1343 case BINEXPR_LOGICAL_OR:
1344 return create_lazy_op(expression);
1346 expression_to_firm(expression->left);
1347 return expression_to_firm(expression->right);
1348 case BINEXPR_ADD_ASSIGN:
1349 return create_arithmetic_assign_binop(expression, new_d_Add);
1350 case BINEXPR_SUB_ASSIGN:
1351 return create_arithmetic_assign_binop(expression, new_d_Sub);
1352 case BINEXPR_MUL_ASSIGN:
1353 return create_arithmetic_assign_binop(expression, new_d_Mul);
1354 case BINEXPR_DIV_ASSIGN:
1355 return create_arithmetic_assign_divmod(expression);
1356 case BINEXPR_BITWISE_AND_ASSIGN:
1357 return create_arithmetic_assign_binop(expression, new_d_And);
1358 case BINEXPR_BITWISE_OR_ASSIGN:
1359 return create_arithmetic_assign_binop(expression, new_d_Or);
1360 case BINEXPR_BITWISE_XOR_ASSIGN:
1361 return create_arithmetic_assign_binop(expression, new_d_Eor);
1362 case BINEXPR_SHIFTLEFT_ASSIGN:
1363 case BINEXPR_SHIFTRIGHT_ASSIGN:
1364 return create_arithmetic_assign_shift(expression);
1366 panic("TODO binexpr type");
1370 static ir_node *array_access_addr(const array_access_expression_t *expression)
1372 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1373 ir_node *base_addr = expression_to_firm(expression->array_ref);
1374 ir_node *offset = expression_to_firm(expression->index);
1375 offset = create_conv(dbgi, offset, mode_Iu);
1377 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1378 assert(is_type_pointer(ref_type));
1379 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1381 unsigned elem_size = get_type_size(pointer_type->points_to);
1382 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1383 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1385 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1390 static ir_node *array_access_to_firm(
1391 const array_access_expression_t *expression)
1393 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1394 ir_node *addr = array_access_addr(expression);
1395 type_t *type = revert_automatic_type_conversion(
1396 (const expression_t*) expression);
1397 type = skip_typeref(type);
1398 ir_type *irtype = get_ir_type(type);
1400 return deref_address(irtype, addr, dbgi);
1403 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1405 type_t *type = expression->type;
1407 type = expression->size_expression->base.datatype;
1408 assert(type != NULL);
1411 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1412 unsigned size = get_type_size(type);
1413 ir_node *size_node = new_Const_long(mode, size);
1418 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1420 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1422 ir_node *cur_block = get_cur_block();
1424 /* create the true block */
1425 ir_node *true_block = new_immBlock();
1427 ir_node *true_val = expression_to_firm(expression->true_expression);
1428 ir_node *true_jmp = new_Jmp();
1430 /* create the false block */
1431 ir_node *false_block = new_immBlock();
1433 ir_node *false_val = expression_to_firm(expression->false_expression);
1434 ir_node *false_jmp = new_Jmp();
1436 /* create the condition evaluation */
1437 set_cur_block(cur_block);
1438 create_condition_evaluation(expression->condition, true_block, false_block);
1439 mature_immBlock(true_block);
1440 mature_immBlock(false_block);
1442 /* create the common block */
1443 ir_node *common_block = new_immBlock();
1444 add_immBlock_pred(common_block, true_jmp);
1445 add_immBlock_pred(common_block, false_jmp);
1446 mature_immBlock(common_block);
1448 /* TODO improve static semantics, so either both or no values are NULL */
1449 if (true_val == NULL || false_val == NULL) return NULL;
1451 ir_node *in[2] = { true_val, false_val };
1452 ir_mode *mode = get_irn_mode(true_val);
1453 assert(get_irn_mode(false_val) == mode);
1454 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1459 static ir_node *select_addr(const select_expression_t *expression)
1461 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1463 ir_node *compound_addr = expression_to_firm(expression->compound);
1465 declaration_t *entry = expression->compound_entry;
1466 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1467 ir_entity *entity = entry->v.entity;
1469 assert(entity != NULL);
1471 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1476 static ir_node *select_to_firm(const select_expression_t *expression)
1478 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1479 ir_node *addr = select_addr(expression);
1480 type_t *type = revert_automatic_type_conversion(
1481 (const expression_t*) expression);
1482 type = skip_typeref(type);
1483 ir_type *irtype = get_ir_type(type);
1485 return deref_address(irtype, addr, dbgi);
1488 /* Values returned by __builtin_classify_type. */
1489 typedef enum gcc_type_class
1495 enumeral_type_class,
1498 reference_type_class,
1502 function_type_class,
1513 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1515 const type_t *const type = expr->type_expression->base.datatype;
1521 const atomic_type_t *const atomic_type = &type->atomic;
1522 switch (atomic_type->atype) {
1523 // should not be reached
1524 case ATOMIC_TYPE_INVALID:
1528 // gcc cannot do that
1529 case ATOMIC_TYPE_VOID:
1530 tc = void_type_class;
1533 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1534 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1535 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1536 case ATOMIC_TYPE_SHORT:
1537 case ATOMIC_TYPE_USHORT:
1538 case ATOMIC_TYPE_INT:
1539 case ATOMIC_TYPE_UINT:
1540 case ATOMIC_TYPE_LONG:
1541 case ATOMIC_TYPE_ULONG:
1542 case ATOMIC_TYPE_LONGLONG:
1543 case ATOMIC_TYPE_ULONGLONG:
1544 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1545 tc = integer_type_class;
1548 case ATOMIC_TYPE_FLOAT:
1549 case ATOMIC_TYPE_DOUBLE:
1550 case ATOMIC_TYPE_LONG_DOUBLE:
1551 tc = real_type_class;
1554 #ifdef PROVIDE_COMPLEX
1555 case ATOMIC_TYPE_FLOAT_COMPLEX:
1556 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1557 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1558 tc = complex_type_class;
1560 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1561 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1562 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1563 tc = complex_type_class;
1568 panic("Unimplemented case in classify_type_to_firm().");
1573 case TYPE_ARRAY: // gcc handles this as pointer
1574 case TYPE_FUNCTION: // gcc handles this as pointer
1575 case TYPE_POINTER: tc = pointer_type_class; break;
1576 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1577 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1579 // gcc handles this as integer
1580 case TYPE_ENUM: tc = integer_type_class; break;
1583 panic("Unimplemented case in classify_type_to_firm().");
1586 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1587 ir_mode *const mode = mode_Is;
1588 tarval *const tv = new_tarval_from_long(tc, mode);
1589 return new_d_Const(dbgi, mode, tv);
1592 static ir_node *function_name_to_firm(
1593 const string_literal_expression_t *const expr)
1595 if (current_function_name == NULL) {
1596 const source_position_t *const src_pos =
1597 &expr->expression.source_position;
1598 const char *const name = current_function_decl->symbol->string;
1599 current_function_name = string_to_firm(src_pos, "__func__", name);
1602 return current_function_name;
1605 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1607 statement_t *statement = expr->statement;
1609 assert(statement->type == STATEMENT_COMPOUND);
1610 return compound_statement_to_firm((compound_statement_t*) statement);
1613 static ir_node *dereference_addr(const unary_expression_t *const expression)
1615 assert(expression->type == UNEXPR_DEREFERENCE);
1616 return expression_to_firm(expression->value);
1619 static ir_node *expression_to_addr(const expression_t *expression)
1621 switch(expression->type) {
1622 case EXPR_REFERENCE:
1623 return reference_addr(&expression->reference);
1624 case EXPR_ARRAY_ACCESS:
1625 return array_access_addr(&expression->array_access);
1627 return select_addr(&expression->select);
1629 return call_expression_to_firm(&expression->call);
1631 const unary_expression_t *const unary_expr = &expression->unary;
1632 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1633 return dereference_addr(unary_expr);
1640 panic("trying to get address of non-lvalue");
1643 static ir_node *_expression_to_firm(const expression_t *expression)
1645 switch(expression->type) {
1647 return const_to_firm(&expression->conste);
1648 case EXPR_STRING_LITERAL:
1649 return string_literal_to_firm(&expression->string_literal);
1650 case EXPR_REFERENCE:
1651 return reference_expression_to_firm(&expression->reference);
1653 return call_expression_to_firm(&expression->call);
1655 return unary_expression_to_firm(&expression->unary);
1657 return binary_expression_to_firm(&expression->binary);
1658 case EXPR_ARRAY_ACCESS:
1659 return array_access_to_firm(&expression->array_access);
1661 return sizeof_to_firm(&expression->sizeofe);
1662 case EXPR_CONDITIONAL:
1663 return conditional_to_firm(&expression->conditional);
1665 return select_to_firm(&expression->select);
1666 case EXPR_CLASSIFY_TYPE:
1667 return classify_type_to_firm(&expression->classify_type);
1669 case EXPR_PRETTY_FUNCTION:
1670 return function_name_to_firm(&expression->string_literal);
1671 case EXPR_STATEMENT:
1672 return statement_expression_to_firm(&expression->statement);
1675 case EXPR_BUILTIN_SYMBOL:
1676 panic("unimplemented expression found");
1682 panic("invalid expression found");
1685 static ir_node *expression_to_firm(const expression_t *expression)
1687 ir_node *res = _expression_to_firm(expression);
1689 if(res != NULL && get_irn_mode(res) == mode_b) {
1690 ir_mode *mode = get_ir_mode(expression->base.datatype);
1691 res = create_conv(NULL, res, mode);
1697 static ir_node *expression_to_modeb(const expression_t *expression)
1699 ir_node *res = _expression_to_firm(expression);
1700 res = create_conv(NULL, res, mode_b);
1706 * create a short-circuit expression evaluation that tries to construct
1707 * efficient control flow structures for &&, || and ! expressions
1709 static void create_condition_evaluation(const expression_t *expression,
1710 ir_node *true_block,
1711 ir_node *false_block)
1713 switch(expression->type) {
1715 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1716 if(unary_expression->type == UNEXPR_NOT) {
1717 create_condition_evaluation(unary_expression->value, false_block,
1724 binary_expression_t *binary_expression
1725 = (binary_expression_t*) expression;
1726 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1727 ir_node *cur_block = get_cur_block();
1728 ir_node *extra_block = new_immBlock();
1729 set_cur_block(cur_block);
1730 create_condition_evaluation(binary_expression->left, extra_block,
1732 mature_immBlock(extra_block);
1733 set_cur_block(extra_block);
1734 create_condition_evaluation(binary_expression->right, true_block,
1738 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1739 ir_node *cur_block = get_cur_block();
1740 ir_node *extra_block = new_immBlock();
1741 set_cur_block(cur_block);
1742 create_condition_evaluation(binary_expression->left, true_block,
1744 mature_immBlock(extra_block);
1745 set_cur_block(extra_block);
1746 create_condition_evaluation(binary_expression->right, true_block,
1756 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1757 ir_node *condition = expression_to_modeb(expression);
1758 ir_node *cond = new_d_Cond(dbgi, condition);
1759 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1760 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1762 add_immBlock_pred(true_block, true_proj);
1763 add_immBlock_pred(false_block, false_proj);
1765 set_cur_block(NULL);
1769 static void return_statement_to_firm(return_statement_t *statement)
1771 if(get_cur_block() == NULL)
1774 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1776 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1780 if(get_method_n_ress(func_irtype) > 0) {
1781 ir_type *res_type = get_method_res_type(func_irtype, 0);
1783 if(is_compound_type(res_type)) {
1784 ir_entity *entity = get_method_value_res_ent(func_irtype, 0);
1786 ir_node *frame = get_irg_frame(current_ir_graph);
1787 ir_node *nomem = new_NoMem();
1788 ir_node *source_addr = expression_to_addr(statement->return_value);
1789 ir_node *dest_addr = new_simpleSel(nomem, frame, entity);
1791 ir_node *store = get_store();
1792 ir_node *copyb = new_d_CopyB(dbgi, store, dest_addr, source_addr,
1795 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1796 set_store(copyb_mem);
1802 if(statement->return_value != NULL) {
1803 in[0] = expression_to_firm(statement->return_value);
1805 ir_mode *mode = get_type_mode(res_type);
1806 in[0] = new_Unknown(mode);
1810 /* build return_value for its side effects */
1811 if(statement->return_value != NULL) {
1812 expression_to_firm(statement->return_value);
1817 ir_node *store = get_store();
1818 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1820 ir_node *end_block = get_irg_end_block(current_ir_graph);
1821 add_immBlock_pred(end_block, ret);
1823 set_cur_block(NULL);
1826 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1828 if(get_cur_block() == NULL)
1831 return expression_to_firm(statement->expression);
1834 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1836 ir_node *result = NULL;
1837 statement_t *statement = compound->statements;
1838 for( ; statement != NULL; statement = statement->base.next) {
1839 //context2firm(&statement->context);
1841 if(statement->base.next == NULL
1842 && statement->type == STATEMENT_EXPRESSION) {
1843 result = expression_statement_to_firm(
1844 (expression_statement_t*) statement);
1847 statement_to_firm(statement);
1853 static void if_statement_to_firm(if_statement_t *statement)
1855 ir_node *cur_block = get_cur_block();
1857 ir_node *fallthrough_block = new_immBlock();
1859 /* the true (blocks) */
1860 ir_node *true_block;
1861 if (statement->true_statement != NULL) {
1862 true_block = new_immBlock();
1863 statement_to_firm(statement->true_statement);
1864 if(get_cur_block() != NULL) {
1865 ir_node *jmp = new_Jmp();
1866 add_immBlock_pred(fallthrough_block, jmp);
1869 true_block = fallthrough_block;
1872 /* the false (blocks) */
1873 ir_node *false_block;
1874 if(statement->false_statement != NULL) {
1875 false_block = new_immBlock();
1877 statement_to_firm(statement->false_statement);
1878 if(get_cur_block() != NULL) {
1879 ir_node *jmp = new_Jmp();
1880 add_immBlock_pred(fallthrough_block, jmp);
1883 false_block = fallthrough_block;
1886 /* create the condition */
1887 if(cur_block != NULL) {
1888 set_cur_block(cur_block);
1889 create_condition_evaluation(statement->condition, true_block,
1893 mature_immBlock(true_block);
1894 if(false_block != fallthrough_block) {
1895 mature_immBlock(false_block);
1897 mature_immBlock(fallthrough_block);
1899 set_cur_block(fallthrough_block);
1902 static void while_statement_to_firm(while_statement_t *statement)
1904 ir_node *jmp = NULL;
1905 if(get_cur_block() != NULL) {
1909 /* create the header block */
1910 ir_node *header_block = new_immBlock();
1912 add_immBlock_pred(header_block, jmp);
1915 /* the false block */
1916 ir_node *false_block = new_immBlock();
1919 ir_node *body_block;
1920 if (statement->body != NULL) {
1921 ir_node *old_continue_label = continue_label;
1922 ir_node *old_break_label = break_label;
1923 continue_label = header_block;
1924 break_label = false_block;
1926 body_block = new_immBlock();
1927 statement_to_firm(statement->body);
1929 assert(continue_label == header_block);
1930 assert(break_label == false_block);
1931 continue_label = old_continue_label;
1932 break_label = old_break_label;
1934 if(get_cur_block() != NULL) {
1936 add_immBlock_pred(header_block, jmp);
1939 body_block = header_block;
1942 /* create the condition */
1943 set_cur_block(header_block);
1945 create_condition_evaluation(statement->condition, body_block, false_block);
1946 mature_immBlock(body_block);
1947 mature_immBlock(false_block);
1948 mature_immBlock(header_block);
1950 set_cur_block(false_block);
1953 static void do_while_statement_to_firm(do_while_statement_t *statement)
1955 ir_node *jmp = NULL;
1956 if(get_cur_block() != NULL) {
1960 /* create the header block */
1961 ir_node *header_block = new_immBlock();
1963 /* the false block */
1964 ir_node *false_block = new_immBlock();
1967 ir_node *body_block = new_immBlock();
1969 add_immBlock_pred(body_block, jmp);
1972 if (statement->body != NULL) {
1973 ir_node *old_continue_label = continue_label;
1974 ir_node *old_break_label = break_label;
1975 continue_label = header_block;
1976 break_label = false_block;
1978 statement_to_firm(statement->body);
1980 assert(continue_label == header_block);
1981 assert(break_label == false_block);
1982 continue_label = old_continue_label;
1983 break_label = old_break_label;
1985 if (get_cur_block() == NULL) {
1986 mature_immBlock(header_block);
1987 mature_immBlock(body_block);
1988 mature_immBlock(false_block);
1993 ir_node *body_jmp = new_Jmp();
1994 add_immBlock_pred(header_block, body_jmp);
1995 mature_immBlock(header_block);
1997 /* create the condition */
1998 set_cur_block(header_block);
2000 create_condition_evaluation(statement->condition, body_block, false_block);
2001 mature_immBlock(body_block);
2002 mature_immBlock(false_block);
2003 mature_immBlock(header_block);
2005 set_cur_block(false_block);
2008 static void for_statement_to_firm(for_statement_t *statement)
2010 ir_node *jmp = NULL;
2011 if (get_cur_block() != NULL) {
2012 if(statement->initialisation != NULL) {
2013 expression_to_firm(statement->initialisation);
2018 /* create the step block */
2019 ir_node *const step_block = new_immBlock();
2020 if (statement->step != NULL) {
2021 expression_to_firm(statement->step);
2023 ir_node *const step_jmp = new_Jmp();
2025 /* create the header block */
2026 ir_node *const header_block = new_immBlock();
2028 add_immBlock_pred(header_block, jmp);
2030 add_immBlock_pred(header_block, step_jmp);
2032 /* the false block */
2033 ir_node *const false_block = new_immBlock();
2036 ir_node * body_block;
2037 if (statement->body != NULL) {
2038 ir_node *const old_continue_label = continue_label;
2039 ir_node *const old_break_label = break_label;
2040 continue_label = step_block;
2041 break_label = false_block;
2043 body_block = new_immBlock();
2044 statement_to_firm(statement->body);
2046 assert(continue_label == step_block);
2047 assert(break_label == false_block);
2048 continue_label = old_continue_label;
2049 break_label = old_break_label;
2051 if (get_cur_block() != NULL) {
2053 add_immBlock_pred(step_block, jmp);
2056 body_block = step_block;
2059 /* create the condition */
2060 set_cur_block(header_block);
2061 if (statement->condition != NULL) {
2062 create_condition_evaluation(statement->condition, body_block,
2065 keep_alive(header_block);
2067 add_immBlock_pred(body_block, jmp);
2070 mature_immBlock(body_block);
2071 mature_immBlock(false_block);
2072 mature_immBlock(step_block);
2073 mature_immBlock(header_block);
2074 mature_immBlock(false_block);
2076 set_cur_block(false_block);
2079 static void create_declaration_entity(declaration_t *declaration,
2080 declaration_type_t declaration_type,
2081 ir_type *parent_type)
2083 ident *id = new_id_from_str(declaration->symbol->string);
2084 ir_type *irtype = get_ir_type(declaration->type);
2085 ir_entity *entity = new_entity(parent_type, id, irtype);
2086 set_entity_ld_ident(entity, id);
2088 declaration->declaration_type = (unsigned char) declaration_type;
2089 declaration->v.entity = entity;
2090 set_entity_variability(entity, variability_uninitialized);
2091 /* TODO: visibility? */
2094 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2096 enum compound_graph_entry_type_t {
2097 COMPOUND_GRAPH_ENTRY_ARRAY,
2098 COMPOUND_GRAPH_ENTRY_COMPOUND
2101 struct compound_graph_path_entry_t {
2107 compound_graph_path_entry_t *prev;
2110 static void create_initializer_object(initializer_t *initializer, type_t *type,
2111 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2113 static compound_graph_path *create_compound_path(ir_type *type,
2114 compound_graph_path_entry_t *entry, int len)
2116 compound_graph_path *path = new_compound_graph_path(type, len);
2119 for( ; entry != NULL; entry = entry->prev, --i) {
2121 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2122 set_compound_graph_path_node(path, i, entry->v.entity);
2124 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2125 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2133 static void create_initializer_value(initializer_value_t *initializer,
2135 compound_graph_path_entry_t *entry,
2138 ir_node *node = expression_to_firm(initializer->value);
2139 ir_type *type = get_entity_type(entity);
2140 compound_graph_path *path = create_compound_path(type, entry, len);
2141 add_compound_ent_value_w_path(entity, node, path);
2144 static void create_initializer_compound(initializer_list_t *initializer,
2145 compound_type_t *type,
2147 compound_graph_path_entry_t *last_entry,
2150 declaration_t *compound_declaration = type->declaration;
2152 declaration_t *compound_entry = compound_declaration->context.declarations;
2154 compound_graph_path_entry_t entry;
2155 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2156 entry.prev = last_entry;
2160 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2161 if(compound_entry->symbol == NULL)
2163 if(compound_entry->namespc != NAMESPACE_NORMAL)
2166 if(i >= initializer->len)
2169 entry.v.entity = compound_entry->v.entity;
2171 initializer_t *sub_initializer = initializer->initializers[i];
2173 assert(compound_entry != NULL);
2174 assert(compound_entry->declaration_type
2175 == DECLARATION_TYPE_COMPOUND_MEMBER);
2177 if(sub_initializer->type == INITIALIZER_VALUE) {
2178 create_initializer_value(&sub_initializer->value,
2179 entity, &entry, len);
2181 type_t *entry_type = skip_typeref(compound_entry->type);
2182 create_initializer_object(sub_initializer, entry_type, entity,
2190 static void create_initializer_array(initializer_list_t *initializer,
2191 array_type_t *type, ir_entity *entity,
2192 compound_graph_path_entry_t *last_entry,
2195 type_t *element_type = type->element_type;
2196 element_type = skip_typeref(element_type);
2198 compound_graph_path_entry_t entry;
2199 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2200 entry.prev = last_entry;
2204 for(i = 0; i < initializer->len; ++i) {
2205 entry.v.array_index = i;
2207 initializer_t *sub_initializer = initializer->initializers[i];
2209 if(sub_initializer->type == INITIALIZER_VALUE) {
2210 create_initializer_value(&sub_initializer->value,
2211 entity, &entry, len);
2213 create_initializer_object(sub_initializer, element_type, entity,
2219 /* TODO: initialize rest... */
2220 if(type->size_expression != NULL) {
2221 size_t array_len = fold_constant(type->size_expression);
2222 for( ; i < array_len; ++i) {
2229 static void create_initializer_string(initializer_string_t *initializer,
2230 array_type_t *type, ir_entity *entity,
2231 compound_graph_path_entry_t *last_entry,
2234 type_t *element_type = type->element_type;
2235 element_type = skip_typeref(element_type);
2237 compound_graph_path_entry_t entry;
2238 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2239 entry.prev = last_entry;
2242 ir_type *irtype = get_entity_type(entity);
2243 size_t arr_len = get_array_type_size(type);
2244 const char *p = initializer->string;
2246 for(i = 0; i < arr_len; ++i, ++p) {
2247 entry.v.array_index = i;
2249 ir_node *node = new_Const_long(mode_Bs, *p);
2250 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2251 add_compound_ent_value_w_path(entity, node, path);
2258 static void create_initializer_object(initializer_t *initializer, type_t *type,
2259 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2261 if(type->type == TYPE_ARRAY) {
2262 array_type_t *array_type = &type->array;
2264 if(initializer->type == INITIALIZER_STRING) {
2265 initializer_string_t *string = &initializer->string;
2266 create_initializer_string(string, array_type, entity, entry, len);
2268 assert(initializer->type == INITIALIZER_LIST);
2269 initializer_list_t *list = &initializer->list;
2270 create_initializer_array(list, array_type, entity, entry, len);
2273 assert(initializer->type == INITIALIZER_LIST);
2274 initializer_list_t *list = &initializer->list;
2276 assert(type->type == TYPE_COMPOUND_STRUCT
2277 || type->type == TYPE_COMPOUND_UNION);
2278 compound_type_t *compound_type = &type->compound;
2279 create_initializer_compound(list, compound_type, entity, entry, len);
2283 static void create_initializer_local_variable_entity(declaration_t *declaration)
2285 initializer_t *initializer = declaration->init.initializer;
2286 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2287 ir_entity *entity = declaration->v.entity;
2288 ir_node *memory = get_store();
2289 ir_node *nomem = new_NoMem();
2290 ir_node *frame = get_irg_frame(current_ir_graph);
2291 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2293 if(is_atomic_entity(entity)) {
2294 assert(initializer->type == INITIALIZER_VALUE);
2295 initializer_value_t *initializer_value = &initializer->value;
2297 ir_node *value = expression_to_firm(initializer_value->value);
2298 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2299 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2300 set_store(store_mem);
2304 /* create a "template" entity which is copied to the entity on the stack */
2305 ident *id = unique_ident("initializer");
2306 ir_type *irtype = get_ir_type(declaration->type);
2307 ir_type *global_type = get_glob_type();
2308 ir_entity *init_entity = new_entity(global_type, id, irtype);
2309 set_entity_ld_ident(init_entity, id);
2311 set_entity_variability(init_entity, variability_initialized);
2312 set_entity_visibility(init_entity, visibility_local);
2314 ir_graph *old_current_ir_graph = current_ir_graph;
2315 current_ir_graph = get_const_code_irg();
2317 type_t *type = skip_typeref(declaration->type);
2318 create_initializer_object(initializer, type, init_entity, NULL, 0);
2320 assert(current_ir_graph == get_const_code_irg());
2321 current_ir_graph = old_current_ir_graph;
2323 ir_node *src_addr = create_symconst(dbgi, init_entity);
2324 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2326 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2327 set_store(copyb_mem);
2330 static void create_initializer(declaration_t *declaration)
2332 initializer_t *initializer = declaration->init.initializer;
2333 if(initializer == NULL)
2336 declaration_type_t declaration_type
2337 = (declaration_type_t) declaration->declaration_type;
2338 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2339 create_initializer_local_variable_entity(declaration);
2343 if(initializer->type == INITIALIZER_VALUE) {
2344 initializer_value_t *initializer_value = &initializer->value;
2346 ir_node *value = expression_to_firm(initializer_value->value);
2348 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2349 set_value(declaration->v.value_number, value);
2351 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2353 ir_entity *entity = declaration->v.entity;
2355 set_entity_variability(entity, variability_initialized);
2356 set_atomic_ent_value(entity, value);
2359 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2360 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2362 ir_entity *entity = declaration->v.entity;
2363 set_entity_variability(entity, variability_initialized);
2365 type_t *type = skip_typeref(declaration->type);
2366 create_initializer_object(initializer, type, entity, NULL, 0);
2370 static void create_local_variable(declaration_t *declaration)
2372 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2374 bool needs_entity = declaration->address_taken;
2375 type_t *type = skip_typeref(declaration->type);
2377 if(type->type == TYPE_ARRAY
2378 || type->type == TYPE_COMPOUND_STRUCT
2379 || type->type == TYPE_COMPOUND_UNION) {
2380 needs_entity = true;
2384 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2385 create_declaration_entity(declaration,
2386 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2389 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2390 declaration->v.value_number = next_value_number_function;
2391 ++next_value_number_function;
2394 create_initializer(declaration);
2397 static void create_local_static_variable(declaration_t *declaration)
2399 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2401 type_t *type = skip_typeref(declaration->type);
2402 ir_type *global_type = get_glob_type();
2403 ident *id = unique_ident(declaration->symbol->string);
2404 ir_type *irtype = get_ir_type(type);
2405 ir_entity *entity = new_entity(global_type, id, irtype);
2406 set_entity_ld_ident(entity, id);
2408 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2409 declaration->v.entity = entity;
2410 set_entity_variability(entity, variability_uninitialized);
2411 set_entity_visibility(entity, visibility_local);
2413 ir_graph *old_current_ir_graph = current_ir_graph;
2414 current_ir_graph = get_const_code_irg();
2416 create_initializer(declaration);
2418 assert(current_ir_graph == get_const_code_irg());
2419 current_ir_graph = old_current_ir_graph;
2422 static void declaration_statement_to_firm(declaration_statement_t *statement)
2424 declaration_t *declaration = statement->declarations_begin;
2425 declaration_t *end = statement->declarations_end->next;
2426 for( ; declaration != end; declaration = declaration->next) {
2427 type_t *type = declaration->type;
2429 switch(declaration->storage_class) {
2430 case STORAGE_CLASS_TYPEDEF:
2432 case STORAGE_CLASS_STATIC:
2433 create_local_static_variable(declaration);
2435 case STORAGE_CLASS_ENUM_ENTRY:
2436 panic("enum entry declaration in local block found");
2437 case STORAGE_CLASS_EXTERN:
2438 panic("extern declaration in local block found");
2439 case STORAGE_CLASS_NONE:
2440 case STORAGE_CLASS_AUTO:
2441 case STORAGE_CLASS_REGISTER:
2442 if(type->type == TYPE_FUNCTION) {
2443 panic("nested functions not supported yet");
2445 create_local_variable(declaration);
2449 panic("invalid storage class found");
2453 static void create_jump_statement(const statement_t *statement,
2454 ir_node *target_block)
2456 if(get_cur_block() == NULL)
2459 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2460 ir_node *jump = new_d_Jmp(dbgi);
2461 add_immBlock_pred(target_block, jump);
2463 set_cur_block(NULL);
2466 static void switch_statement_to_firm(const switch_statement_t *statement)
2468 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2470 ir_node *expression = expression_to_firm(statement->expression);
2471 ir_node *cond = new_d_Cond(dbgi, expression);
2472 ir_node *break_block = new_immBlock();
2474 set_cur_block(NULL);
2476 ir_node *const old_switch_cond = current_switch_cond;
2477 ir_node *const old_break_label = break_label;
2478 const bool old_saw_default_label = saw_default_label;
2479 current_switch_cond = cond;
2480 break_label = break_block;
2482 statement_to_firm(statement->body);
2484 if(get_cur_block() != NULL) {
2485 ir_node *jmp = new_Jmp();
2486 add_immBlock_pred(break_block, jmp);
2489 if (!saw_default_label) {
2490 set_cur_block(get_nodes_block(cond));
2491 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2492 MAGIC_DEFAULT_PN_NUMBER);
2493 add_immBlock_pred(break_block, proj);
2496 assert(current_switch_cond == cond);
2497 assert(break_label == break_block);
2498 current_switch_cond = old_switch_cond;
2499 break_label = old_break_label;
2500 saw_default_label = old_saw_default_label;
2502 mature_immBlock(break_block);
2503 set_cur_block(break_block);
2506 static long fold_constant(const expression_t *expression)
2508 ir_graph *old_current_ir_graph = current_ir_graph;
2509 current_ir_graph = get_const_code_irg();
2511 ir_node *cnst = expression_to_firm(expression);
2512 if(!is_Const(cnst)) {
2513 panic("couldn't fold constantl");
2515 tarval *tv = get_Const_tarval(cnst);
2516 if(!tarval_is_long(tv)) {
2517 panic("folded constant not an integer");
2520 long res = get_tarval_long(tv);
2522 current_ir_graph = old_current_ir_graph;
2526 static void case_label_to_firm(const case_label_statement_t *statement)
2528 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2530 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2532 /* let's create a node and hope firm constant folding creates a Const
2535 set_cur_block(get_nodes_block(current_switch_cond));
2536 if(statement->expression) {
2537 long pn = fold_constant(statement->expression);
2538 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2539 /* oops someone detected our cheating... */
2540 panic("magic default pn used");
2542 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2544 saw_default_label = true;
2545 proj = new_d_defaultProj(dbgi, current_switch_cond,
2546 MAGIC_DEFAULT_PN_NUMBER);
2549 ir_node *block = new_immBlock();
2550 if (fallthrough != NULL) {
2551 add_immBlock_pred(block, fallthrough);
2553 add_immBlock_pred(block, proj);
2554 mature_immBlock(block);
2556 statement_to_firm(statement->label_statement);
2559 static ir_node *get_label_block(declaration_t *label)
2561 assert(label->namespc == NAMESPACE_LABEL);
2563 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2564 return label->v.block;
2566 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2568 ir_node *old_cur_block = get_cur_block();
2569 ir_node *block = new_immBlock();
2570 set_cur_block(old_cur_block);
2572 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2573 label->v.block = block;
2575 ARR_APP1(ir_node *, imature_blocks, block);
2580 static void label_to_firm(const label_statement_t *statement)
2582 ir_node *block = get_label_block(statement->label);
2584 if(get_cur_block() != NULL) {
2585 ir_node *jmp = new_Jmp();
2586 add_immBlock_pred(block, jmp);
2589 set_cur_block(block);
2592 statement_to_firm(statement->label_statement);
2595 static void goto_to_firm(const goto_statement_t *statement)
2597 if(get_cur_block() == NULL)
2600 ir_node *block = get_label_block(statement->label);
2601 ir_node *jmp = new_Jmp();
2602 add_immBlock_pred(block, jmp);
2604 set_cur_block(NULL);
2607 static void statement_to_firm(statement_t *statement)
2609 switch(statement->type) {
2610 case STATEMENT_COMPOUND:
2611 compound_statement_to_firm((compound_statement_t*) statement);
2613 case STATEMENT_RETURN:
2614 return_statement_to_firm((return_statement_t*) statement);
2616 case STATEMENT_EXPRESSION:
2617 expression_statement_to_firm((expression_statement_t*) statement);
2620 if_statement_to_firm((if_statement_t*) statement);
2622 case STATEMENT_WHILE:
2623 while_statement_to_firm((while_statement_t*) statement);
2625 case STATEMENT_DO_WHILE:
2626 do_while_statement_to_firm((do_while_statement_t*) statement);
2628 case STATEMENT_DECLARATION:
2629 declaration_statement_to_firm((declaration_statement_t*) statement);
2631 case STATEMENT_BREAK:
2632 create_jump_statement(statement, break_label);
2634 case STATEMENT_CONTINUE:
2635 create_jump_statement(statement, continue_label);
2637 case STATEMENT_SWITCH:
2638 switch_statement_to_firm((switch_statement_t*) statement);
2640 case STATEMENT_CASE_LABEL:
2641 case_label_to_firm((case_label_statement_t*) statement);
2644 for_statement_to_firm((for_statement_t*) statement);
2646 case STATEMENT_LABEL:
2647 label_to_firm((label_statement_t*) statement);
2649 case STATEMENT_GOTO:
2650 goto_to_firm((goto_statement_t*) statement);
2655 panic("Statement not implemented\n");
2658 static int count_local_declarations(const declaration_t * decl,
2659 const declaration_t *const end)
2662 for (; decl != end; decl = decl->next) {
2663 const type_t *type = skip_typeref(decl->type);
2664 switch (type->type) {
2668 if (!decl->address_taken)
2678 static int count_decls_in_stmts(const statement_t *stmt)
2681 for (; stmt != NULL; stmt = stmt->base.next) {
2682 switch (stmt->type) {
2683 case STATEMENT_DECLARATION: {
2684 const declaration_statement_t *const decl_stmt =
2685 (const declaration_statement_t*)stmt;
2686 count += count_local_declarations(decl_stmt->declarations_begin,
2687 decl_stmt->declarations_end->next);
2691 case STATEMENT_COMPOUND: {
2692 const compound_statement_t *const comp =
2693 (const compound_statement_t*)stmt;
2694 count += count_decls_in_stmts(comp->statements);
2698 case STATEMENT_IF: {
2699 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2700 count += count_decls_in_stmts(if_stmt->true_statement);
2701 count += count_decls_in_stmts(if_stmt->false_statement);
2705 case STATEMENT_SWITCH: {
2706 const switch_statement_t *const switch_stmt =
2707 (const switch_statement_t*)stmt;
2708 count += count_decls_in_stmts(switch_stmt->body);
2712 case STATEMENT_LABEL: {
2713 const label_statement_t *const label_stmt =
2714 (const label_statement_t*)stmt;
2715 count += count_decls_in_stmts(label_stmt->label_statement);
2719 case STATEMENT_WHILE: {
2720 const while_statement_t *const while_stmt =
2721 (const while_statement_t*)stmt;
2722 count += count_decls_in_stmts(while_stmt->body);
2726 case STATEMENT_DO_WHILE: {
2727 const do_while_statement_t *const do_while_stmt =
2728 (const do_while_statement_t*)stmt;
2729 count += count_decls_in_stmts(do_while_stmt->body);
2733 case STATEMENT_FOR: {
2734 const for_statement_t *const for_stmt =
2735 (const for_statement_t*)stmt;
2736 /* TODO initialisation */
2737 count += count_decls_in_stmts(for_stmt->body);
2741 case STATEMENT_BREAK:
2742 case STATEMENT_CASE_LABEL:
2743 case STATEMENT_CONTINUE:
2744 case STATEMENT_EXPRESSION:
2745 case STATEMENT_GOTO:
2746 case STATEMENT_INVALID:
2747 case STATEMENT_RETURN:
2754 static int get_function_n_local_vars(declaration_t *declaration)
2758 /* count parameters */
2759 count += count_local_declarations(declaration->context.declarations, NULL);
2761 /* count local variables declared in body */
2762 count += count_decls_in_stmts(declaration->init.statement);
2764 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2765 * for expression statements... */
2771 static void initialize_function_parameters(declaration_t *declaration)
2773 ir_graph *irg = current_ir_graph;
2774 ir_node *args = get_irg_args(irg);
2775 ir_node *start_block = get_irg_start_block(irg);
2776 ir_type *function_irtype = get_ir_type(declaration->type);
2779 declaration_t *parameter = declaration->context.declarations;
2780 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2781 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2782 type_t *type = skip_typeref(parameter->type);
2784 bool needs_entity = parameter->address_taken;
2785 if(type->type == TYPE_COMPOUND_STRUCT
2786 || type->type == TYPE_COMPOUND_UNION) {
2787 needs_entity = true;
2791 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2792 ident *id = new_id_from_str(parameter->symbol->string);
2793 set_entity_ident(entity, id);
2795 parameter->declaration_type
2796 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2797 parameter->v.entity = entity;
2801 ir_mode *mode = get_ir_mode(parameter->type);
2803 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2805 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2806 parameter->v.value_number = next_value_number_function;
2807 ++next_value_number_function;
2809 set_value(parameter->v.value_number, proj);
2813 static void create_function(declaration_t *declaration)
2815 ir_entity *function_entity = get_function_entity(declaration);
2817 if(declaration->init.statement == NULL)
2820 current_function_decl = declaration;
2821 current_function_name = NULL;
2823 assert(imature_blocks == NULL);
2824 imature_blocks = NEW_ARR_F(ir_node*, 0);
2826 int n_local_vars = get_function_n_local_vars(declaration);
2827 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
2828 ir_node *first_block = get_cur_block();
2830 next_value_number_function = 0;
2831 initialize_function_parameters(declaration);
2833 statement_to_firm(declaration->init.statement);
2835 ir_node *end_block = get_irg_end_block(irg);
2837 /* do we have a return statement yet? */
2838 if(get_cur_block() != NULL) {
2839 assert(declaration->type->type == TYPE_FUNCTION);
2840 const function_type_t* const func_type = &declaration->type->function;
2843 if (func_type->result_type == type_void) {
2844 ret = new_Return(get_store(), 0, NULL);
2846 ir_mode *const mode = get_ir_mode(func_type->result_type);
2848 // ยง5.1.2.2.3 main implicitly returns 0
2849 if (strcmp(declaration->symbol->string, "main") == 0) {
2850 in[0] = new_Const(mode, get_mode_null(mode));
2852 in[0] = new_Unknown(mode);
2854 ret = new_Return(get_store(), 1, in);
2856 add_immBlock_pred(end_block, ret);
2859 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2860 mature_immBlock(imature_blocks[i]);
2862 DEL_ARR_F(imature_blocks);
2863 imature_blocks = NULL;
2865 mature_immBlock(first_block);
2866 mature_immBlock(end_block);
2868 irg_finalize_cons(irg);
2870 /* finalize the frame type */
2871 ir_type *frame_type = get_irg_frame_type(irg);
2872 int n = get_compound_n_members(frame_type);
2875 for(int i = 0; i < n; ++i) {
2876 ir_entity *entity = get_compound_member(frame_type, i);
2877 ir_type *entity_type = get_entity_type(entity);
2879 int align = get_type_alignment_bytes(entity_type);
2880 if(align > align_all)
2884 misalign = offset % align;
2886 offset += align - misalign;
2890 set_entity_offset(entity, offset);
2891 offset += get_type_size_bytes(entity_type);
2893 set_type_size_bytes(frame_type, offset);
2894 set_type_alignment_bytes(frame_type, align_all);
2895 set_type_state(frame_type, layout_fixed);
2900 static void create_global_variable(declaration_t *declaration)
2902 ir_type *global_type = get_glob_type();
2903 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2906 ir_entity *entity = declaration->v.entity;
2907 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2908 set_entity_visibility(entity, visibility_local);
2909 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2910 set_entity_visibility(entity, visibility_external_allocated);
2912 set_entity_visibility(entity, visibility_external_visible);
2914 current_ir_graph = get_const_code_irg();
2915 create_initializer(declaration);
2918 static void context_to_firm(context_t *context)
2920 /* first pass: create declarations */
2921 declaration_t *declaration = context->declarations;
2922 for( ; declaration != NULL; declaration = declaration->next) {
2923 if(declaration->namespc != NAMESPACE_NORMAL)
2925 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2926 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2928 if(declaration->symbol == NULL)
2931 type_t *type = declaration->type;
2932 if(type->type == TYPE_FUNCTION) {
2933 get_function_entity(declaration);
2935 create_global_variable(declaration);
2939 /* second pass: create code */
2940 declaration = context->declarations;
2941 for( ; declaration != NULL; declaration = declaration->next) {
2942 if(declaration->namespc != NAMESPACE_NORMAL)
2944 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2945 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2947 if(declaration->symbol == NULL)
2950 type_t *type = declaration->type;
2951 if(type->type != TYPE_FUNCTION)
2954 create_function(declaration);
2958 void translation_unit_to_firm(translation_unit_t *unit)
2960 /* just to be sure */
2961 continue_label = NULL;
2963 current_switch_cond = NULL;
2965 context_to_firm(& unit->context);