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);
248 panic("Trying to determine size of invalid type");
251 static unsigned count_parameters(const function_type_t *function_type)
255 function_parameter_t *parameter = function_type->parameters;
256 for ( ; parameter != NULL; parameter = parameter->next) {
266 static long fold_constant(const expression_t *expression);
268 static ir_type *create_atomic_type(const atomic_type_t *type)
270 ir_mode *mode = get_atomic_mode(type);
271 ident *id = get_mode_ident(mode);
272 ir_type *irtype = new_type_primitive(id, mode);
277 static ir_type *create_method_type(const function_type_t *function_type)
279 type_t *result_type = function_type->result_type;
281 ident *id = unique_ident("functiontype");
282 int n_parameters = count_parameters(function_type);
283 int n_results = result_type == type_void ? 0 : 1;
284 ir_type *irtype = new_type_method(id, n_parameters, n_results);
286 if(result_type != type_void) {
287 ir_type *restype = get_ir_type(result_type);
288 set_method_res_type(irtype, 0, restype);
291 function_parameter_t *parameter = function_type->parameters;
293 for( ; parameter != NULL; parameter = parameter->next) {
294 ir_type *p_irtype = get_ir_type(parameter->type);
295 set_method_param_type(irtype, n, p_irtype);
299 if(function_type->variadic || function_type->unspecified_parameters) {
300 set_method_variadicity(irtype, variadicity_variadic);
306 static ir_type *create_pointer_type(pointer_type_t *type)
308 type_t *points_to = type->points_to;
309 ir_type *ir_points_to;
310 /* Avoid endless recursion if the points_to type contains this poiner type
311 * again (might be a struct). We therefore first create a void* pointer
312 * and then set the real points_to type
314 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
315 ir_type_void, mode_P_data);
316 type->type.firm_type = ir_type;
318 ir_points_to = get_ir_type(points_to);
319 set_pointer_points_to_type(ir_type, ir_points_to);
324 static ir_type *create_array_type(array_type_t *type)
326 type_t *element_type = type->element_type;
327 ir_type *ir_element_type = get_ir_type(element_type);
329 ident *id = unique_ident("array");
330 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
332 if(type->size != NULL) {
333 int n_elements = fold_constant(type->size);
335 set_array_bounds_int(ir_type, 0, 0, n_elements);
337 size_t elemsize = get_type_size_bytes(ir_element_type);
338 int align = get_type_alignment_bytes(ir_element_type);
339 if(elemsize % align > 0) {
340 elemsize += align - (elemsize % align);
342 set_type_size_bytes(ir_type, n_elements * elemsize);
343 set_type_alignment_bytes(ir_type, align);
344 set_type_state(ir_type, layout_fixed);
350 #define INVALID_TYPE ((ir_type_ptr)-1)
352 static ir_type *create_struct_type(compound_type_t *type)
354 symbol_t *symbol = type->declaration->symbol;
357 id = unique_ident(symbol->string);
359 id = unique_ident("__anonymous_struct");
361 ir_type *ir_type = new_type_struct(id);
363 type->type.firm_type = ir_type;
367 declaration_t *entry = type->declaration->context.declarations;
368 for( ; entry != NULL; entry = entry->next) {
369 if(entry->namespc != NAMESPACE_NORMAL)
372 ident *ident = new_id_from_str(entry->symbol->string);
373 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
375 int entry_size = get_type_size_bytes(entry_ir_type);
376 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
377 int misalign = offset % entry_alignment;
379 offset += entry_alignment - misalign;
381 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
382 set_entity_offset(entity, offset);
383 add_struct_member(ir_type, entity);
384 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
385 entry->v.entity = entity;
387 offset += entry_size;
388 if(entry_alignment > align_all) {
389 if(entry_alignment % align_all != 0) {
390 panic("Uneven alignments not supported yet");
392 align_all = entry_alignment;
396 int misalign = offset % align_all;
398 set_type_alignment_bytes(ir_type, align_all);
399 set_type_size_bytes(ir_type, offset);
400 set_type_state(ir_type, layout_fixed);
405 static ir_type *create_union_type(compound_type_t *type)
407 declaration_t *declaration = type->declaration;
408 symbol_t *symbol = declaration->symbol;
411 id = unique_ident(symbol->string);
413 id = unique_ident("__anonymous_union");
415 ir_type *ir_type = new_type_union(id);
417 type->type.firm_type = ir_type;
421 declaration_t *entry = declaration->context.declarations;
422 for( ; entry != NULL; entry = entry->next) {
423 if(entry->namespc != NAMESPACE_NORMAL)
426 ident *ident = new_id_from_str(entry->symbol->string);
427 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
429 int entry_size = get_type_size_bytes(entry_ir_type);
430 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
432 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
433 add_union_member(ir_type, entity);
434 set_entity_offset(entity, 0);
435 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
436 entry->v.entity = entity;
438 if(entry_size > size) {
441 if(entry_alignment > align_all) {
442 if(entry_alignment % align_all != 0) {
443 panic("Uneven alignments not supported yet");
445 align_all = entry_alignment;
449 set_type_alignment_bytes(ir_type, align_all);
450 set_type_size_bytes(ir_type, size);
451 set_type_state(ir_type, layout_fixed);
456 static ir_node *expression_to_firm(const expression_t *expression);
457 static inline ir_mode *get_ir_mode(type_t *type);
459 static ir_type *create_enum_type(enum_type_t *const type)
461 type->type.firm_type = ir_type_int;
463 ir_mode *const mode = get_ir_mode((type_t*) type);
464 tarval *const one = get_mode_one(mode);
465 tarval * tv_next = get_tarval_null(mode);
467 declaration_t *declaration = type->declaration->next;
468 for (; declaration != NULL; declaration = declaration->next) {
469 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
472 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
474 expression_t *const init = declaration->init.enum_value;
476 ir_node *const cnst = expression_to_firm(init);
477 if (!is_Const(cnst)) {
478 panic("couldn't fold constant");
480 tv_next = get_Const_tarval(cnst);
482 declaration->v.enum_val = tv_next;
483 tv_next = tarval_add(tv_next, one);
489 static ir_type *get_ir_type(type_t *type)
491 assert(type != NULL);
493 type = skip_typeref(type);
495 if(type->base.firm_type != NULL) {
496 assert(type->base.firm_type != INVALID_TYPE);
497 return type->base.firm_type;
500 ir_type *firm_type = NULL;
503 firm_type = create_atomic_type(&type->atomic);
506 firm_type = create_method_type(&type->function);
509 firm_type = create_pointer_type(&type->pointer);
512 firm_type = create_array_type(&type->array);
514 case TYPE_COMPOUND_STRUCT:
515 firm_type = create_struct_type(&type->compound);
517 case TYPE_COMPOUND_UNION:
518 firm_type = create_union_type(&type->compound);
521 firm_type = create_enum_type(&type->enumt);
529 if(firm_type == NULL)
530 panic("unknown type found");
532 type->base.firm_type = firm_type;
536 static inline ir_mode *get_ir_mode(type_t *type)
538 ir_type *irtype = get_ir_type(type);
540 /* firm doesn't report a mode for arrays somehow... */
541 if(is_Array_type(irtype)) {
545 ir_mode *mode = get_type_mode(irtype);
546 assert(mode != NULL);
550 static ir_entity* get_function_entity(declaration_t *declaration)
552 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
553 return declaration->v.entity;
554 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
556 symbol_t *symbol = declaration->symbol;
557 ident *id = new_id_from_str(symbol->string);
559 ir_type *global_type = get_glob_type();
560 ir_type *ir_type_method = get_ir_type(declaration->type);
561 assert(is_Method_type(ir_type_method));
563 ir_entity *entity = new_entity(global_type, id, ir_type_method);
564 set_entity_ld_ident(entity, id);
565 if(declaration->storage_class == STORAGE_CLASS_STATIC
566 || declaration->is_inline) {
567 set_entity_visibility(entity, visibility_local);
568 } else if(declaration->init.statement != NULL) {
569 set_entity_visibility(entity, visibility_external_visible);
571 set_entity_visibility(entity, visibility_external_allocated);
574 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
575 declaration->v.entity = entity;
580 static dbg_info *get_dbg_info(const source_position_t *pos)
582 return (dbg_info*) pos;
585 static ir_node *const_to_firm(const const_expression_t *cnst)
587 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
588 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
593 if(mode_is_float(mode)) {
594 tv = new_tarval_from_double(cnst->v.float_value, mode);
596 if(mode_is_signed(mode)) {
597 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
599 len = snprintf(buf, sizeof(buf), "%llu",
600 (unsigned long long) cnst->v.int_value);
602 tv = new_tarval_from_str(buf, len, mode);
605 return new_d_Const(dbgi, mode, tv);
608 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
610 assert(entity != NULL);
611 union symconst_symbol sym;
612 sym.entity_p = entity;
613 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
616 static ir_node *string_to_firm(const source_position_t *const src_pos,
617 const char *const id_prefix,
618 const char *const string)
620 ir_type *const global_type = get_glob_type();
621 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
624 ident *const id = unique_ident(id_prefix);
625 ir_entity *const entity = new_entity(global_type, id, type);
626 set_entity_ld_ident(entity, id);
627 set_entity_variability(entity, variability_constant);
629 ir_type *const elem_type = ir_type_const_char;
630 ir_mode *const mode = get_type_mode(elem_type);
632 const size_t slen = strlen(string) + 1;
634 set_array_lower_bound_int(type, 0, 0);
635 set_array_upper_bound_int(type, 0, slen);
636 set_type_size_bytes(type, slen);
637 set_type_state(type, layout_fixed);
639 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
640 for(size_t i = 0; i < slen; ++i) {
641 tvs[i] = new_tarval_from_long(string[i], mode);
644 set_array_entity_values(entity, tvs, slen);
647 dbg_info *const dbgi = get_dbg_info(src_pos);
649 return create_symconst(dbgi, entity);
652 static ir_node *string_literal_to_firm(
653 const string_literal_expression_t* literal)
655 return string_to_firm(&literal->expression.source_position, "Lstr",
659 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
660 dbg_info *const dbgi)
662 if(is_compound_type(irtype) || is_Array_type(irtype)) {
666 ir_mode *const mode = get_type_mode(irtype);
667 ir_node *const memory = get_store();
668 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
669 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
670 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
675 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
677 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
678 declaration_t *declaration = ref->declaration;
679 type_t *type = skip_typeref(declaration->type);
681 switch((declaration_type_t) declaration->declaration_type) {
682 case DECLARATION_TYPE_UNKNOWN:
683 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
689 case DECLARATION_TYPE_ENUM_ENTRY: {
690 ir_mode *const mode = get_ir_mode(type);
691 return new_Const(mode, declaration->v.enum_val);
694 case DECLARATION_TYPE_LOCAL_VARIABLE: {
695 ir_mode *mode = get_ir_mode(type);
696 return get_value(declaration->v.value_number, mode);
698 case DECLARATION_TYPE_FUNCTION: {
699 return create_symconst(dbgi, declaration->v.entity);
701 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
702 ir_entity *entity = declaration->v.entity;
703 ir_node *symconst = create_symconst(dbgi, entity);
704 ir_type *irtype = get_entity_type(entity);
705 return deref_address(irtype, symconst, dbgi);
707 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
708 ir_entity *entity = declaration->v.entity;
709 ir_node *frame = get_irg_frame(current_ir_graph);
710 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
711 ir_type *irtype = get_entity_type(entity);
712 return deref_address(irtype, sel, dbgi);
715 case DECLARATION_TYPE_COMPOUND_MEMBER:
716 case DECLARATION_TYPE_LABEL_BLOCK:
717 panic("not implemented reference type");
720 panic("reference to declaration with unknown type found");
723 static ir_node *reference_addr(const reference_expression_t *ref)
725 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
726 declaration_t *declaration = ref->declaration;
728 switch((declaration_type_t) declaration->declaration_type) {
729 case DECLARATION_TYPE_UNKNOWN:
731 case DECLARATION_TYPE_LOCAL_VARIABLE:
732 panic("local variable without entity has no address");
733 case DECLARATION_TYPE_FUNCTION: {
734 return create_symconst(dbgi, declaration->v.entity);
736 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
737 ir_entity *entity = declaration->v.entity;
738 ir_node *symconst = create_symconst(dbgi, entity);
741 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
742 ir_entity *entity = declaration->v.entity;
743 ir_node *frame = get_irg_frame(current_ir_graph);
744 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
749 case DECLARATION_TYPE_ENUM_ENTRY:
750 panic("trying to reference enum entry");
752 case DECLARATION_TYPE_COMPOUND_MEMBER:
753 case DECLARATION_TYPE_LABEL_BLOCK:
754 panic("not implemented reference type");
757 panic("reference to declaration with unknown type found");
760 static ir_node *process_builtin_call(const call_expression_t *call)
762 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
764 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
765 builtin_symbol_expression_t *builtin
766 = (builtin_symbol_expression_t*) call->function;
767 symbol_t *symbol = builtin->symbol;
769 if(symbol == symbol_alloca) {
770 if(call->arguments == NULL || call->arguments->next != NULL) {
771 panic("invalid number of parameters on __builtin_alloca");
773 expression_t *argument = call->arguments->expression;
774 ir_node *size = expression_to_firm(argument);
776 ir_node *store = get_store();
777 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
779 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
781 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
785 panic("Unsupported builtin found\n");
789 static ir_node *call_expression_to_firm(const call_expression_t *call)
791 assert(get_cur_block() != NULL);
793 expression_t *function = call->function;
794 if(function->type == EXPR_BUILTIN_SYMBOL) {
795 return process_builtin_call(call);
797 ir_node *callee = expression_to_firm(function);
799 type_t *type = function->base.datatype;
800 assert(type->type == TYPE_POINTER);
801 pointer_type_t *const ptr_type = &type->pointer;
802 assert(ptr_type->points_to->type == TYPE_FUNCTION);
803 function_type_t *function_type = &ptr_type->points_to->function;
805 int n_parameters = 0;
806 call_argument_t *argument = call->arguments;
807 for( ; argument != NULL; argument = argument->next) {
811 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
812 ir_type *new_method_type = NULL;
813 if(function_type->variadic || function_type->unspecified_parameters) {
814 /* we need to construct a new method type matching the call
816 int n_res = get_method_n_ress(ir_method_type);
817 new_method_type = new_type_method(unique_ident("calltype"),
818 n_parameters, n_res);
819 set_method_calling_convention(new_method_type,
820 get_method_calling_convention(ir_method_type));
821 set_method_additional_properties(new_method_type,
822 get_method_additional_properties(ir_method_type));
824 for(int i = 0; i < n_res; ++i) {
825 set_method_res_type(new_method_type, i,
826 get_method_res_type(ir_method_type, i));
829 ir_node *in[n_parameters];
831 argument = call->arguments;
833 for( ; argument != NULL; argument = argument->next) {
834 expression_t *expression = argument->expression;
835 ir_node *arg_node = expression_to_firm(expression);
838 if(new_method_type != NULL) {
839 ir_type *irtype = get_ir_type(expression->base.datatype);
840 set_method_param_type(new_method_type, n, irtype);
845 assert(n == n_parameters);
847 if(new_method_type != NULL)
848 ir_method_type = new_method_type;
850 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
851 ir_node *store = get_store();
852 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
854 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
857 type_t *result_type = skip_typeref(function_type->result_type);
858 ir_node *result = NULL;
860 if(!is_type_atomic(result_type, ATOMIC_TYPE_VOID)) {
862 if(is_type_scalar(result_type)) {
863 mode = get_ir_mode(result_type);
867 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
868 result = new_d_Proj(dbgi, resproj, mode, 0);
874 static void statement_to_firm(statement_t *statement);
875 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
877 static ir_node *expression_to_addr(const expression_t *expression);
878 static void create_condition_evaluation(const expression_t *expression,
880 ir_node *false_block);
882 static void set_value_for_expression(const expression_t *expression,
885 if(expression->type == EXPR_REFERENCE) {
886 reference_expression_t *ref = (reference_expression_t*) expression;
888 declaration_t *declaration = ref->declaration;
889 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
890 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
891 set_value(declaration->v.value_number, value);
896 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
897 ir_node *addr = expression_to_addr(expression);
898 ir_node *memory = get_store();
900 type_t *type = skip_typeref(expression->base.datatype);
901 if(is_type_scalar(type)) {
902 ir_node *store = new_d_Store(dbgi, memory, addr, value);
903 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
904 set_store(store_mem);
906 ir_type *irtype = get_ir_type(type);
907 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
908 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
909 set_store(copyb_mem);
913 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
915 ir_mode *value_mode = get_irn_mode(value);
917 if (value_mode == dest_mode || is_Bad(value))
920 if(dest_mode == mode_b) {
921 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
922 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
923 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
927 return new_d_Conv(dbgi, value, dest_mode);
930 static ir_node *create_incdec(const unary_expression_t *expression)
932 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
933 type_t *type = expression->expression.datatype;
934 ir_mode *mode = get_ir_mode(type);
935 expression_t *value = expression->value;
937 ir_node *value_node = expression_to_firm(value);
940 if(type->type == TYPE_POINTER) {
941 pointer_type_t *pointer_type = &type->pointer;
942 unsigned elem_size = get_type_size(pointer_type->points_to);
943 offset = new_Const_long(mode_Is, elem_size);
945 assert(is_type_arithmetic(type));
946 offset = new_Const(mode, get_mode_one(mode));
949 switch(expression->type) {
950 case UNEXPR_POSTFIX_INCREMENT: {
951 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
952 set_value_for_expression(value, new_value);
955 case UNEXPR_POSTFIX_DECREMENT: {
956 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
957 set_value_for_expression(value, new_value);
960 case UNEXPR_PREFIX_INCREMENT: {
961 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
962 set_value_for_expression(value, new_value);
965 case UNEXPR_PREFIX_DECREMENT: {
966 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
967 set_value_for_expression(value, new_value);
971 panic("no incdec expr in create_incdec");
976 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
978 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
979 type_t *type = skip_typeref(expression->expression.datatype);
981 if(expression->type == UNEXPR_TAKE_ADDRESS)
982 return expression_to_addr(expression->value);
984 const expression_t *value = expression->value;
985 ir_node *value_node = expression_to_firm(value);
987 switch(expression->type) {
989 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
992 case UNEXPR_BITWISE_NEGATE:
993 return new_d_Not(dbgi, value_node, get_ir_mode(type));
995 if(get_irn_mode(value_node) != mode_b) {
996 value_node = create_conv(dbgi, value_node, mode_b);
998 value_node = new_d_Not(dbgi, value_node, mode_b);
999 ir_mode *const mode = get_ir_mode(type);
1000 if(mode != mode_b) {
1001 value_node = create_conv(dbgi, value_node, mode);
1005 case UNEXPR_DEREFERENCE: {
1006 ir_type *irtype = get_ir_type(type);
1007 return deref_address(irtype, value_node, dbgi);
1009 case UNEXPR_POSTFIX_INCREMENT:
1010 case UNEXPR_POSTFIX_DECREMENT:
1011 case UNEXPR_PREFIX_INCREMENT:
1012 case UNEXPR_PREFIX_DECREMENT:
1013 return create_incdec(expression);
1015 return create_conv(dbgi, value_node, get_ir_mode(type));
1017 case UNEXPR_TAKE_ADDRESS:
1018 case UNEXPR_INVALID:
1021 panic("invalid UNEXPR type found");
1024 static long get_pnc(binary_expression_type_t type)
1027 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1028 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1029 case BINEXPR_LESS: return pn_Cmp_Lt;
1030 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1031 case BINEXPR_GREATER: return pn_Cmp_Gt;
1032 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1036 panic("trying to get pn_Cmp from non-comparison binexpr type");
1039 static ir_node *create_lazy_op(const binary_expression_t *expression)
1041 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1042 type_t *type = expression->expression.datatype;
1043 ir_mode *mode = get_ir_mode(type);
1045 ir_node *cur_block = get_cur_block();
1047 ir_node *one_block = new_immBlock();
1048 ir_node *one = new_Const(mode, get_mode_one(mode));
1049 ir_node *jmp_one = new_d_Jmp(dbgi);
1051 ir_node *zero_block = new_immBlock();
1052 ir_node *zero = new_Const(mode, get_mode_null(mode));
1053 ir_node *jmp_zero = new_d_Jmp(dbgi);
1055 set_cur_block(cur_block);
1056 create_condition_evaluation((const expression_t*) expression,
1057 one_block, zero_block);
1058 mature_immBlock(one_block);
1059 mature_immBlock(zero_block);
1061 ir_node *common_block = new_immBlock();
1062 add_immBlock_pred(common_block, jmp_one);
1063 add_immBlock_pred(common_block, jmp_zero);
1064 mature_immBlock(common_block);
1066 ir_node *in[2] = { one, zero };
1067 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1072 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1073 ir_node *right, ir_mode *mode);
1075 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1076 create_arithmetic_func func)
1078 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1079 ir_node *left = expression_to_firm(expression->left);
1080 ir_node *right = expression_to_firm(expression->right);
1081 type_t *type = expression->right->base.datatype;
1082 /* be careful with the modes, because in arithmetic assign nodes only
1083 * the right operand has the mode of the arithmetic already */
1084 ir_mode *mode = get_ir_mode(type);
1085 left = create_conv(dbgi, left, mode);
1086 ir_node *res = func(dbgi, left, right, mode);
1091 static ir_node *pointer_arithmetic(ir_node *const pointer,
1094 dbg_info *const dbgi,
1095 const create_arithmetic_func func)
1097 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1098 type_t *const points_to = pointer_type->points_to;
1099 const unsigned elem_size = get_type_size(points_to);
1101 assert(elem_size >= 1);
1102 if (elem_size > 1) {
1103 integer = create_conv(dbgi, integer, mode_Is);
1104 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1105 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1109 ir_mode *const mode = get_ir_mode(type);
1110 return func(dbgi, pointer, integer, mode);
1113 static ir_node *create_arithmetic_assign_binop(
1114 const binary_expression_t *expression, create_arithmetic_func func)
1116 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1117 type_t *const type = expression->expression.datatype;
1120 if (type->type == TYPE_POINTER) {
1121 ir_node *const pointer = expression_to_firm(expression->left);
1122 ir_node * integer = expression_to_firm(expression->right);
1123 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1125 value = create_arithmetic_binop(expression, func);
1128 ir_mode *const mode = get_ir_mode(type);
1129 value = create_conv(dbgi, value, mode);
1130 set_value_for_expression(expression->left, value);
1135 static ir_node *create_add(const binary_expression_t *expression)
1137 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1138 ir_node *left = expression_to_firm(expression->left);
1139 ir_node *right = expression_to_firm(expression->right);
1140 type_t *type = expression->expression.datatype;
1142 expression_t *expr_left = expression->left;
1143 expression_t *expr_right = expression->right;
1144 type_t *type_left = skip_typeref(expr_left->base.datatype);
1145 type_t *type_right = skip_typeref(expr_right->base.datatype);
1147 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1148 ir_mode *const mode = get_ir_mode(type);
1149 return new_d_Add(dbgi, left, right, mode);
1152 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1153 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1155 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1156 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1160 static ir_node *create_sub(const binary_expression_t *expression)
1162 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1163 expression_t *const expr_left = expression->left;
1164 expression_t *const expr_right = expression->right;
1165 ir_node *const left = expression_to_firm(expr_left);
1166 ir_node *const right = expression_to_firm(expr_right);
1167 type_t *const type = expression->expression.datatype;
1168 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1169 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1171 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1172 ir_mode *const mode = get_ir_mode(type);
1173 return new_d_Sub(dbgi, left, right, mode);
1174 } else if (type_left->type == TYPE_POINTER
1175 && type_right->type == TYPE_POINTER) {
1176 const pointer_type_t *const ptr_type = &type_left->pointer;
1177 const unsigned elem_size = get_type_size(ptr_type->points_to);
1178 ir_mode *const mode = get_ir_mode(type);
1179 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1180 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1181 ir_node *const no_mem = new_NoMem();
1182 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1183 op_pin_state_floats);
1184 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1187 assert(type_left->type == TYPE_POINTER);
1188 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1191 static ir_node *create_shift(const binary_expression_t *expression)
1193 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1194 ir_node *left = expression_to_firm(expression->left);
1195 ir_node *right = expression_to_firm(expression->right);
1196 type_t *type = expression->expression.datatype;
1197 ir_mode *mode = get_ir_mode(type);
1199 /* firm always wants the shift count to be unsigned */
1200 right = create_conv(dbgi, right, mode_Iu);
1204 switch(expression->type) {
1205 case BINEXPR_SHIFTLEFT_ASSIGN:
1206 case BINEXPR_SHIFTLEFT:
1207 res = new_d_Shl(dbgi, left, right, mode);
1209 case BINEXPR_SHIFTRIGHT_ASSIGN:
1210 case BINEXPR_SHIFTRIGHT: {
1211 expression_t *expr_left = expression->left;
1212 type_t *type_left = skip_typeref(expr_left->base.datatype);
1214 if(is_type_signed(type_left)) {
1215 res = new_d_Shrs(dbgi, left, right, mode);
1217 res = new_d_Shr(dbgi, left, right, mode);
1222 panic("create shift op called for non-shift op");
1229 static ir_node *create_divmod(const binary_expression_t *expression)
1231 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1232 ir_node *left = expression_to_firm(expression->left);
1233 ir_node *right = expression_to_firm(expression->right);
1234 ir_node *pin = new_Pin(new_NoMem());
1235 /* be careful with the modes, because in arithmetic assign nodes only
1236 * the right operand has the mode of the arithmetic already */
1237 type_t *type = expression->right->base.datatype;
1238 ir_mode *mode = get_ir_mode(type);
1239 left = create_conv(dbgi, left, mode);
1243 switch (expression->type) {
1245 case BINEXPR_DIV_ASSIGN:
1246 if(mode_is_float(mode)) {
1247 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1248 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1250 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1251 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1256 case BINEXPR_MOD_ASSIGN:
1257 assert(!mode_is_float(mode));
1258 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1259 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1262 default: panic("unexpected binary expression type in create_divmod()");
1268 static ir_node *create_arithmetic_assign_divmod(
1269 const binary_expression_t *expression)
1271 ir_node * value = create_divmod(expression);
1272 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1273 type_t *const type = expression->expression.datatype;
1274 ir_mode *const mode = get_ir_mode(type);
1276 assert(type->type != TYPE_POINTER);
1278 value = create_conv(dbgi, value, mode);
1279 set_value_for_expression(expression->left, value);
1284 static ir_node *create_arithmetic_assign_shift(
1285 const binary_expression_t *expression)
1287 ir_node * value = create_shift(expression);
1288 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1289 type_t *const type = expression->expression.datatype;
1290 ir_mode *const mode = get_ir_mode(type);
1292 value = create_conv(dbgi, value, mode);
1293 set_value_for_expression(expression->left, value);
1298 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1300 binary_expression_type_t type = expression->type;
1303 case BINEXPR_NOTEQUAL:
1305 case BINEXPR_LESSEQUAL:
1306 case BINEXPR_GREATER:
1307 case BINEXPR_GREATEREQUAL: {
1308 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1309 ir_node *left = expression_to_firm(expression->left);
1310 ir_node *right = expression_to_firm(expression->right);
1311 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1312 long pnc = get_pnc(type);
1313 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1316 case BINEXPR_ASSIGN: {
1317 ir_node *right = expression_to_firm(expression->right);
1318 set_value_for_expression(expression->left, right);
1323 return create_add(expression);
1325 return create_sub(expression);
1327 return create_arithmetic_binop(expression, new_d_Mul);
1328 case BINEXPR_BITWISE_AND:
1329 return create_arithmetic_binop(expression, new_d_And);
1330 case BINEXPR_BITWISE_OR:
1331 return create_arithmetic_binop(expression, new_d_Or);
1332 case BINEXPR_BITWISE_XOR:
1333 return create_arithmetic_binop(expression, new_d_Eor);
1334 case BINEXPR_SHIFTLEFT:
1335 case BINEXPR_SHIFTRIGHT:
1336 return create_shift(expression);
1339 return create_divmod(expression);
1340 case BINEXPR_LOGICAL_AND:
1341 case BINEXPR_LOGICAL_OR:
1342 return create_lazy_op(expression);
1344 expression_to_firm(expression->left);
1345 return expression_to_firm(expression->right);
1346 case BINEXPR_ADD_ASSIGN:
1347 return create_arithmetic_assign_binop(expression, new_d_Add);
1348 case BINEXPR_SUB_ASSIGN:
1349 return create_arithmetic_assign_binop(expression, new_d_Sub);
1350 case BINEXPR_MUL_ASSIGN:
1351 return create_arithmetic_assign_binop(expression, new_d_Mul);
1352 case BINEXPR_DIV_ASSIGN:
1353 return create_arithmetic_assign_divmod(expression);
1354 case BINEXPR_BITWISE_AND_ASSIGN:
1355 return create_arithmetic_assign_binop(expression, new_d_And);
1356 case BINEXPR_BITWISE_OR_ASSIGN:
1357 return create_arithmetic_assign_binop(expression, new_d_Or);
1358 case BINEXPR_BITWISE_XOR_ASSIGN:
1359 return create_arithmetic_assign_binop(expression, new_d_Eor);
1360 case BINEXPR_SHIFTLEFT_ASSIGN:
1361 case BINEXPR_SHIFTRIGHT_ASSIGN:
1362 return create_arithmetic_assign_shift(expression);
1364 panic("TODO binexpr type");
1368 static ir_node *array_access_addr(const array_access_expression_t *expression)
1370 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1371 ir_node *base_addr = expression_to_firm(expression->array_ref);
1372 ir_node *offset = expression_to_firm(expression->index);
1373 offset = create_conv(dbgi, offset, mode_Iu);
1375 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1376 assert(is_type_pointer(ref_type));
1377 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1379 unsigned elem_size = get_type_size(pointer_type->points_to);
1380 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1381 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1383 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1388 static ir_node *array_access_to_firm(
1389 const array_access_expression_t *expression)
1391 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1392 ir_node *addr = array_access_addr(expression);
1393 type_t *type = revert_automatic_type_conversion(
1394 (const expression_t*) expression);
1395 type = skip_typeref(type);
1396 ir_type *irtype = get_ir_type(type);
1398 return deref_address(irtype, addr, dbgi);
1401 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1403 type_t *type = expression->type;
1405 type = expression->size_expression->base.datatype;
1406 assert(type != NULL);
1409 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1410 unsigned size = get_type_size(type);
1411 ir_node *size_node = new_Const_long(mode, size);
1416 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1418 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1420 ir_node *cur_block = get_cur_block();
1422 /* create the true block */
1423 ir_node *true_block = new_immBlock();
1425 ir_node *true_val = expression_to_firm(expression->true_expression);
1426 ir_node *true_jmp = new_Jmp();
1428 /* create the false block */
1429 ir_node *false_block = new_immBlock();
1431 ir_node *false_val = expression_to_firm(expression->false_expression);
1432 ir_node *false_jmp = new_Jmp();
1434 /* create the condition evaluation */
1435 set_cur_block(cur_block);
1436 create_condition_evaluation(expression->condition, true_block, false_block);
1437 mature_immBlock(true_block);
1438 mature_immBlock(false_block);
1440 /* create the common block */
1441 ir_node *common_block = new_immBlock();
1442 add_immBlock_pred(common_block, true_jmp);
1443 add_immBlock_pred(common_block, false_jmp);
1444 mature_immBlock(common_block);
1446 /* TODO improve static semantics, so either both or no values are NULL */
1447 if (true_val == NULL || false_val == NULL) return NULL;
1449 ir_node *in[2] = { true_val, false_val };
1450 ir_mode *mode = get_irn_mode(true_val);
1451 assert(get_irn_mode(false_val) == mode);
1452 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1457 static ir_node *select_addr(const select_expression_t *expression)
1459 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1461 ir_node *compound_addr = expression_to_firm(expression->compound);
1463 declaration_t *entry = expression->compound_entry;
1464 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1465 ir_entity *entity = entry->v.entity;
1467 assert(entity != NULL);
1469 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1474 static ir_node *select_to_firm(const select_expression_t *expression)
1476 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1477 ir_node *addr = select_addr(expression);
1478 type_t *type = revert_automatic_type_conversion(
1479 (const expression_t*) expression);
1480 type = skip_typeref(type);
1481 ir_type *irtype = get_ir_type(type);
1483 return deref_address(irtype, addr, dbgi);
1486 /* Values returned by __builtin_classify_type. */
1487 typedef enum gcc_type_class
1493 enumeral_type_class,
1496 reference_type_class,
1500 function_type_class,
1511 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1513 const type_t *const type = expr->type_expression->base.datatype;
1519 const atomic_type_t *const atomic_type = &type->atomic;
1520 switch (atomic_type->atype) {
1521 // should not be reached
1522 case ATOMIC_TYPE_INVALID:
1526 // gcc cannot do that
1527 case ATOMIC_TYPE_VOID:
1528 tc = void_type_class;
1531 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1532 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1533 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1534 case ATOMIC_TYPE_SHORT:
1535 case ATOMIC_TYPE_USHORT:
1536 case ATOMIC_TYPE_INT:
1537 case ATOMIC_TYPE_UINT:
1538 case ATOMIC_TYPE_LONG:
1539 case ATOMIC_TYPE_ULONG:
1540 case ATOMIC_TYPE_LONGLONG:
1541 case ATOMIC_TYPE_ULONGLONG:
1542 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1543 tc = integer_type_class;
1546 case ATOMIC_TYPE_FLOAT:
1547 case ATOMIC_TYPE_DOUBLE:
1548 case ATOMIC_TYPE_LONG_DOUBLE:
1549 tc = real_type_class;
1552 #ifdef PROVIDE_COMPLEX
1553 case ATOMIC_TYPE_FLOAT_COMPLEX:
1554 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1555 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1556 tc = complex_type_class;
1558 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1559 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1560 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1561 tc = complex_type_class;
1566 panic("Unimplemented case in classify_type_to_firm().");
1571 case TYPE_ARRAY: // gcc handles this as pointer
1572 case TYPE_FUNCTION: // gcc handles this as pointer
1573 case TYPE_POINTER: tc = pointer_type_class; break;
1574 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1575 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1577 // gcc handles this as integer
1578 case TYPE_ENUM: tc = integer_type_class; break;
1581 panic("Unimplemented case in classify_type_to_firm().");
1584 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1585 ir_mode *const mode = mode_Is;
1586 tarval *const tv = new_tarval_from_long(tc, mode);
1587 return new_d_Const(dbgi, mode, tv);
1590 static ir_node *function_name_to_firm(
1591 const string_literal_expression_t *const expr)
1593 if (current_function_name == NULL) {
1594 const source_position_t *const src_pos =
1595 &expr->expression.source_position;
1596 const char *const name = current_function_decl->symbol->string;
1597 current_function_name = string_to_firm(src_pos, "__func__", name);
1600 return current_function_name;
1603 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1605 statement_t *statement = expr->statement;
1607 assert(statement->type == STATEMENT_COMPOUND);
1608 return compound_statement_to_firm((compound_statement_t*) statement);
1611 static ir_node *dereference_addr(const unary_expression_t *const expression)
1613 assert(expression->type == UNEXPR_DEREFERENCE);
1614 return expression_to_firm(expression->value);
1617 static ir_node *expression_to_addr(const expression_t *expression)
1619 switch(expression->type) {
1620 case EXPR_REFERENCE:
1621 return reference_addr(&expression->reference);
1622 case EXPR_ARRAY_ACCESS:
1623 return array_access_addr(&expression->array_access);
1625 return select_addr(&expression->select);
1627 return call_expression_to_firm(&expression->call);
1629 const unary_expression_t *const unary_expr = &expression->unary;
1630 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1631 return dereference_addr(unary_expr);
1638 panic("trying to get address of non-lvalue");
1641 static ir_node *_expression_to_firm(const expression_t *expression)
1643 switch(expression->type) {
1645 return const_to_firm(&expression->conste);
1646 case EXPR_STRING_LITERAL:
1647 return string_literal_to_firm(&expression->string);
1648 case EXPR_REFERENCE:
1649 return reference_expression_to_firm(&expression->reference);
1651 return call_expression_to_firm(&expression->call);
1653 return unary_expression_to_firm(&expression->unary);
1655 return binary_expression_to_firm(&expression->binary);
1656 case EXPR_ARRAY_ACCESS:
1657 return array_access_to_firm(&expression->array_access);
1659 return sizeof_to_firm(&expression->sizeofe);
1660 case EXPR_CONDITIONAL:
1661 return conditional_to_firm(&expression->conditional);
1663 return select_to_firm(&expression->select);
1664 case EXPR_CLASSIFY_TYPE:
1665 return classify_type_to_firm(&expression->classify_type);
1667 case EXPR_PRETTY_FUNCTION:
1668 return function_name_to_firm(&expression->string);
1669 case EXPR_STATEMENT:
1670 return statement_expression_to_firm(&expression->statement);
1673 case EXPR_BUILTIN_SYMBOL:
1674 panic("unimplemented expression found");
1680 panic("invalid expression found");
1683 static ir_node *expression_to_firm(const expression_t *expression)
1685 ir_node *res = _expression_to_firm(expression);
1687 if(res != NULL && get_irn_mode(res) == mode_b) {
1688 ir_mode *mode = get_ir_mode(expression->base.datatype);
1689 res = create_conv(NULL, res, mode);
1695 static ir_node *expression_to_modeb(const expression_t *expression)
1697 ir_node *res = _expression_to_firm(expression);
1698 res = create_conv(NULL, res, mode_b);
1704 * create a short-circuit expression evaluation that tries to construct
1705 * efficient control flow structures for &&, || and ! expressions
1707 static void create_condition_evaluation(const expression_t *expression,
1708 ir_node *true_block,
1709 ir_node *false_block)
1711 switch(expression->type) {
1713 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1714 if(unary_expression->type == UNEXPR_NOT) {
1715 create_condition_evaluation(unary_expression->value, false_block,
1722 binary_expression_t *binary_expression
1723 = (binary_expression_t*) expression;
1724 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
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, extra_block,
1730 mature_immBlock(extra_block);
1731 set_cur_block(extra_block);
1732 create_condition_evaluation(binary_expression->right, true_block,
1736 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1737 ir_node *cur_block = get_cur_block();
1738 ir_node *extra_block = new_immBlock();
1739 set_cur_block(cur_block);
1740 create_condition_evaluation(binary_expression->left, true_block,
1742 mature_immBlock(extra_block);
1743 set_cur_block(extra_block);
1744 create_condition_evaluation(binary_expression->right, true_block,
1754 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1755 ir_node *condition = expression_to_modeb(expression);
1756 ir_node *cond = new_d_Cond(dbgi, condition);
1757 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1758 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1760 add_immBlock_pred(true_block, true_proj);
1761 add_immBlock_pred(false_block, false_proj);
1763 set_cur_block(NULL);
1767 static void return_statement_to_firm(return_statement_t *statement)
1769 if(get_cur_block() == NULL)
1772 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1774 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1778 if(get_method_n_ress(func_irtype) > 0) {
1779 ir_type *res_type = get_method_res_type(func_irtype, 0);
1781 if(is_compound_type(res_type)) {
1782 ir_entity *entity = get_method_value_res_ent(func_irtype, 0);
1784 ir_node *frame = get_irg_frame(current_ir_graph);
1785 ir_node *nomem = new_NoMem();
1786 ir_node *source_addr = expression_to_addr(statement->return_value);
1787 ir_node *dest_addr = new_simpleSel(nomem, frame, entity);
1789 ir_node *store = get_store();
1790 ir_node *copyb = new_d_CopyB(dbgi, store, dest_addr, source_addr,
1793 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1794 set_store(copyb_mem);
1800 if(statement->return_value != NULL) {
1801 in[0] = expression_to_firm(statement->return_value);
1803 ir_mode *mode = get_type_mode(res_type);
1804 in[0] = new_Unknown(mode);
1808 /* build return_value for its side effects */
1809 if(statement->return_value != NULL) {
1810 expression_to_firm(statement->return_value);
1815 ir_node *store = get_store();
1816 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1818 ir_node *end_block = get_irg_end_block(current_ir_graph);
1819 add_immBlock_pred(end_block, ret);
1821 set_cur_block(NULL);
1824 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1826 if(get_cur_block() == NULL)
1829 return expression_to_firm(statement->expression);
1832 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1834 ir_node *result = NULL;
1835 statement_t *statement = compound->statements;
1836 for( ; statement != NULL; statement = statement->base.next) {
1837 //context2firm(&statement->context);
1839 if(statement->base.next == NULL
1840 && statement->type == STATEMENT_EXPRESSION) {
1841 result = expression_statement_to_firm(
1842 (expression_statement_t*) statement);
1845 statement_to_firm(statement);
1851 static void if_statement_to_firm(if_statement_t *statement)
1853 ir_node *cur_block = get_cur_block();
1855 ir_node *fallthrough_block = new_immBlock();
1857 /* the true (blocks) */
1858 ir_node *true_block;
1859 if (statement->true_statement != NULL) {
1860 true_block = new_immBlock();
1861 statement_to_firm(statement->true_statement);
1862 if(get_cur_block() != NULL) {
1863 ir_node *jmp = new_Jmp();
1864 add_immBlock_pred(fallthrough_block, jmp);
1867 true_block = fallthrough_block;
1870 /* the false (blocks) */
1871 ir_node *false_block;
1872 if(statement->false_statement != NULL) {
1873 false_block = new_immBlock();
1875 statement_to_firm(statement->false_statement);
1876 if(get_cur_block() != NULL) {
1877 ir_node *jmp = new_Jmp();
1878 add_immBlock_pred(fallthrough_block, jmp);
1881 false_block = fallthrough_block;
1884 /* create the condition */
1885 if(cur_block != NULL) {
1886 set_cur_block(cur_block);
1887 create_condition_evaluation(statement->condition, true_block,
1891 mature_immBlock(true_block);
1892 if(false_block != fallthrough_block) {
1893 mature_immBlock(false_block);
1895 mature_immBlock(fallthrough_block);
1897 set_cur_block(fallthrough_block);
1900 static void while_statement_to_firm(while_statement_t *statement)
1902 ir_node *jmp = NULL;
1903 if(get_cur_block() != NULL) {
1907 /* create the header block */
1908 ir_node *header_block = new_immBlock();
1910 add_immBlock_pred(header_block, jmp);
1913 /* the false block */
1914 ir_node *false_block = new_immBlock();
1917 ir_node *body_block;
1918 if (statement->body != NULL) {
1919 ir_node *old_continue_label = continue_label;
1920 ir_node *old_break_label = break_label;
1921 continue_label = header_block;
1922 break_label = false_block;
1924 body_block = new_immBlock();
1925 statement_to_firm(statement->body);
1927 assert(continue_label == header_block);
1928 assert(break_label == false_block);
1929 continue_label = old_continue_label;
1930 break_label = old_break_label;
1932 if(get_cur_block() != NULL) {
1934 add_immBlock_pred(header_block, jmp);
1937 body_block = header_block;
1940 /* create the condition */
1941 set_cur_block(header_block);
1943 create_condition_evaluation(statement->condition, body_block, false_block);
1944 mature_immBlock(body_block);
1945 mature_immBlock(false_block);
1946 mature_immBlock(header_block);
1948 set_cur_block(false_block);
1951 static void do_while_statement_to_firm(do_while_statement_t *statement)
1953 ir_node *jmp = NULL;
1954 if(get_cur_block() != NULL) {
1958 /* create the header block */
1959 ir_node *header_block = new_immBlock();
1961 /* the false block */
1962 ir_node *false_block = new_immBlock();
1965 ir_node *body_block = new_immBlock();
1967 add_immBlock_pred(body_block, jmp);
1970 if (statement->body != NULL) {
1971 ir_node *old_continue_label = continue_label;
1972 ir_node *old_break_label = break_label;
1973 continue_label = header_block;
1974 break_label = false_block;
1976 statement_to_firm(statement->body);
1978 assert(continue_label == header_block);
1979 assert(break_label == false_block);
1980 continue_label = old_continue_label;
1981 break_label = old_break_label;
1983 if (get_cur_block() == NULL) {
1984 mature_immBlock(header_block);
1985 mature_immBlock(body_block);
1986 mature_immBlock(false_block);
1991 ir_node *body_jmp = new_Jmp();
1992 add_immBlock_pred(header_block, body_jmp);
1993 mature_immBlock(header_block);
1995 /* create the condition */
1996 set_cur_block(header_block);
1998 create_condition_evaluation(statement->condition, body_block, false_block);
1999 mature_immBlock(body_block);
2000 mature_immBlock(false_block);
2001 mature_immBlock(header_block);
2003 set_cur_block(false_block);
2006 static void for_statement_to_firm(for_statement_t *statement)
2008 ir_node *jmp = NULL;
2009 if (get_cur_block() != NULL) {
2010 if(statement->initialisation != NULL) {
2011 expression_to_firm(statement->initialisation);
2016 /* create the step block */
2017 ir_node *const step_block = new_immBlock();
2018 if (statement->step != NULL) {
2019 expression_to_firm(statement->step);
2021 ir_node *const step_jmp = new_Jmp();
2023 /* create the header block */
2024 ir_node *const header_block = new_immBlock();
2026 add_immBlock_pred(header_block, jmp);
2028 add_immBlock_pred(header_block, step_jmp);
2030 /* the false block */
2031 ir_node *const false_block = new_immBlock();
2034 ir_node * body_block;
2035 if (statement->body != NULL) {
2036 ir_node *const old_continue_label = continue_label;
2037 ir_node *const old_break_label = break_label;
2038 continue_label = step_block;
2039 break_label = false_block;
2041 body_block = new_immBlock();
2042 statement_to_firm(statement->body);
2044 assert(continue_label == step_block);
2045 assert(break_label == false_block);
2046 continue_label = old_continue_label;
2047 break_label = old_break_label;
2049 if (get_cur_block() != NULL) {
2051 add_immBlock_pred(step_block, jmp);
2054 body_block = step_block;
2057 /* create the condition */
2058 set_cur_block(header_block);
2059 if (statement->condition != NULL) {
2060 create_condition_evaluation(statement->condition, body_block,
2063 keep_alive(header_block);
2065 add_immBlock_pred(body_block, jmp);
2068 mature_immBlock(body_block);
2069 mature_immBlock(false_block);
2070 mature_immBlock(step_block);
2071 mature_immBlock(header_block);
2072 mature_immBlock(false_block);
2074 set_cur_block(false_block);
2077 static void create_declaration_entity(declaration_t *declaration,
2078 declaration_type_t declaration_type,
2079 ir_type *parent_type)
2081 ident *id = new_id_from_str(declaration->symbol->string);
2082 ir_type *irtype = get_ir_type(declaration->type);
2083 ir_entity *entity = new_entity(parent_type, id, irtype);
2084 set_entity_ld_ident(entity, id);
2086 declaration->declaration_type = (unsigned char) declaration_type;
2087 declaration->v.entity = entity;
2088 set_entity_variability(entity, variability_uninitialized);
2089 /* TODO: visibility? */
2092 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2094 enum compound_graph_entry_type_t {
2095 COMPOUND_GRAPH_ENTRY_ARRAY,
2096 COMPOUND_GRAPH_ENTRY_COMPOUND
2099 struct compound_graph_path_entry_t {
2105 compound_graph_path_entry_t *prev;
2108 static void create_initializer_object(initializer_t *initializer, type_t *type,
2109 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2111 static compound_graph_path *create_compound_path(ir_type *type,
2112 compound_graph_path_entry_t *entry, int len)
2114 compound_graph_path *path = new_compound_graph_path(type, len);
2117 for( ; entry != NULL; entry = entry->prev, --i) {
2119 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2120 set_compound_graph_path_node(path, i, entry->v.entity);
2122 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2123 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2131 static void create_initializer_value(initializer_value_t *initializer,
2133 compound_graph_path_entry_t *entry,
2136 ir_node *node = expression_to_firm(initializer->value);
2137 ir_type *type = get_entity_type(entity);
2138 compound_graph_path *path = create_compound_path(type, entry, len);
2139 add_compound_ent_value_w_path(entity, node, path);
2142 static void create_initializer_compound(initializer_list_t *initializer,
2143 compound_type_t *type,
2145 compound_graph_path_entry_t *last_entry,
2148 declaration_t *compound_declaration = type->declaration;
2150 declaration_t *compound_entry = compound_declaration->context.declarations;
2152 compound_graph_path_entry_t entry;
2153 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2154 entry.prev = last_entry;
2158 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2159 if(compound_entry->symbol == NULL)
2161 if(compound_entry->namespc != NAMESPACE_NORMAL)
2164 if(i >= initializer->len)
2167 entry.v.entity = compound_entry->v.entity;
2169 initializer_t *sub_initializer = initializer->initializers[i];
2171 assert(compound_entry != NULL);
2172 assert(compound_entry->declaration_type
2173 == DECLARATION_TYPE_COMPOUND_MEMBER);
2175 if(sub_initializer->type == INITIALIZER_VALUE) {
2176 create_initializer_value(&sub_initializer->value,
2177 entity, &entry, len);
2179 type_t *entry_type = skip_typeref(compound_entry->type);
2180 create_initializer_object(sub_initializer, entry_type, entity,
2188 static void create_initializer_array(initializer_list_t *initializer,
2189 array_type_t *type, ir_entity *entity,
2190 compound_graph_path_entry_t *last_entry,
2193 type_t *element_type = type->element_type;
2194 element_type = skip_typeref(element_type);
2196 compound_graph_path_entry_t entry;
2197 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2198 entry.prev = last_entry;
2202 for(i = 0; i < initializer->len; ++i) {
2203 entry.v.array_index = i;
2205 initializer_t *sub_initializer = initializer->initializers[i];
2207 if(sub_initializer->type == INITIALIZER_VALUE) {
2208 create_initializer_value(&sub_initializer->value,
2209 entity, &entry, len);
2211 create_initializer_object(sub_initializer, element_type, entity,
2217 /* TODO: initialize rest... */
2218 if(type->size_expression != NULL) {
2219 size_t array_len = fold_constant(type->size_expression);
2220 for( ; i < array_len; ++i) {
2227 static void create_initializer_string(initializer_string_t *initializer,
2228 array_type_t *type, ir_entity *entity,
2229 compound_graph_path_entry_t *last_entry,
2232 type_t *element_type = type->element_type;
2233 element_type = skip_typeref(element_type);
2235 compound_graph_path_entry_t entry;
2236 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2237 entry.prev = last_entry;
2240 ir_type *irtype = get_entity_type(entity);
2241 size_t arr_len = get_array_type_size(type);
2242 const char *p = initializer->string;
2244 for(i = 0; i < arr_len; ++i, ++p) {
2245 entry.v.array_index = i;
2247 ir_node *node = new_Const_long(mode_Bs, *p);
2248 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2249 add_compound_ent_value_w_path(entity, node, path);
2256 static void create_initializer_object(initializer_t *initializer, type_t *type,
2257 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2259 if(type->type == TYPE_ARRAY) {
2260 array_type_t *array_type = &type->array;
2262 if(initializer->type == INITIALIZER_STRING) {
2263 initializer_string_t *string = &initializer->string;
2264 create_initializer_string(string, array_type, entity, entry, len);
2266 assert(initializer->type == INITIALIZER_LIST);
2267 initializer_list_t *list = &initializer->list;
2268 create_initializer_array(list, array_type, entity, entry, len);
2271 assert(initializer->type == INITIALIZER_LIST);
2272 initializer_list_t *list = &initializer->list;
2274 assert(type->type == TYPE_COMPOUND_STRUCT
2275 || type->type == TYPE_COMPOUND_UNION);
2276 compound_type_t *compound_type = &type->compound;
2277 create_initializer_compound(list, compound_type, entity, entry, len);
2281 static void create_initializer_local_variable_entity(declaration_t *declaration)
2283 initializer_t *initializer = declaration->init.initializer;
2284 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2285 ir_entity *entity = declaration->v.entity;
2286 ir_node *memory = get_store();
2287 ir_node *nomem = new_NoMem();
2288 ir_node *frame = get_irg_frame(current_ir_graph);
2289 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2291 if(is_atomic_entity(entity)) {
2292 assert(initializer->type == INITIALIZER_VALUE);
2293 initializer_value_t *initializer_value = &initializer->value;
2295 ir_node *value = expression_to_firm(initializer_value->value);
2296 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2297 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2298 set_store(store_mem);
2302 /* create a "template" entity which is copied to the entity on the stack */
2303 ident *id = unique_ident("initializer");
2304 ir_type *irtype = get_ir_type(declaration->type);
2305 ir_type *global_type = get_glob_type();
2306 ir_entity *init_entity = new_entity(global_type, id, irtype);
2307 set_entity_ld_ident(init_entity, id);
2309 set_entity_variability(init_entity, variability_initialized);
2310 set_entity_visibility(init_entity, visibility_local);
2312 ir_graph *old_current_ir_graph = current_ir_graph;
2313 current_ir_graph = get_const_code_irg();
2315 type_t *type = skip_typeref(declaration->type);
2316 create_initializer_object(initializer, type, init_entity, NULL, 0);
2318 assert(current_ir_graph == get_const_code_irg());
2319 current_ir_graph = old_current_ir_graph;
2321 ir_node *src_addr = create_symconst(dbgi, init_entity);
2322 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2324 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2325 set_store(copyb_mem);
2328 static void create_initializer(declaration_t *declaration)
2330 initializer_t *initializer = declaration->init.initializer;
2331 if(initializer == NULL)
2334 declaration_type_t declaration_type
2335 = (declaration_type_t) declaration->declaration_type;
2336 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2337 create_initializer_local_variable_entity(declaration);
2341 if(initializer->type == INITIALIZER_VALUE) {
2342 initializer_value_t *initializer_value = &initializer->value;
2344 ir_node *value = expression_to_firm(initializer_value->value);
2346 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2347 set_value(declaration->v.value_number, value);
2349 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2351 ir_entity *entity = declaration->v.entity;
2353 set_entity_variability(entity, variability_initialized);
2354 set_atomic_ent_value(entity, value);
2357 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2358 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2360 ir_entity *entity = declaration->v.entity;
2361 set_entity_variability(entity, variability_initialized);
2363 type_t *type = skip_typeref(declaration->type);
2364 create_initializer_object(initializer, type, entity, NULL, 0);
2368 static void create_local_variable(declaration_t *declaration)
2370 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2372 bool needs_entity = declaration->address_taken;
2373 type_t *type = skip_typeref(declaration->type);
2375 if(type->type == TYPE_ARRAY
2376 || type->type == TYPE_COMPOUND_STRUCT
2377 || type->type == TYPE_COMPOUND_UNION) {
2378 needs_entity = true;
2382 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2383 create_declaration_entity(declaration,
2384 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2387 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2388 declaration->v.value_number = next_value_number_function;
2389 ++next_value_number_function;
2392 create_initializer(declaration);
2395 static void create_local_static_variable(declaration_t *declaration)
2397 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2399 type_t *type = skip_typeref(declaration->type);
2400 ir_type *global_type = get_glob_type();
2401 ident *id = unique_ident(declaration->symbol->string);
2402 ir_type *irtype = get_ir_type(type);
2403 ir_entity *entity = new_entity(global_type, id, irtype);
2404 set_entity_ld_ident(entity, id);
2406 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2407 declaration->v.entity = entity;
2408 set_entity_variability(entity, variability_uninitialized);
2409 set_entity_visibility(entity, visibility_local);
2411 ir_graph *old_current_ir_graph = current_ir_graph;
2412 current_ir_graph = get_const_code_irg();
2414 create_initializer(declaration);
2416 assert(current_ir_graph == get_const_code_irg());
2417 current_ir_graph = old_current_ir_graph;
2420 static void declaration_statement_to_firm(declaration_statement_t *statement)
2422 declaration_t *declaration = statement->declarations_begin;
2423 declaration_t *end = statement->declarations_end->next;
2424 for( ; declaration != end; declaration = declaration->next) {
2425 type_t *type = declaration->type;
2427 switch(declaration->storage_class) {
2428 case STORAGE_CLASS_TYPEDEF:
2430 case STORAGE_CLASS_STATIC:
2431 create_local_static_variable(declaration);
2433 case STORAGE_CLASS_ENUM_ENTRY:
2434 panic("enum entry declaration in local block found");
2435 case STORAGE_CLASS_EXTERN:
2436 panic("extern declaration in local block found");
2437 case STORAGE_CLASS_NONE:
2438 case STORAGE_CLASS_AUTO:
2439 case STORAGE_CLASS_REGISTER:
2440 if(type->type == TYPE_FUNCTION) {
2441 panic("nested functions not supported yet");
2443 create_local_variable(declaration);
2447 panic("invalid storage class found");
2451 static void create_jump_statement(const statement_t *statement,
2452 ir_node *target_block)
2454 if(get_cur_block() == NULL)
2457 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2458 ir_node *jump = new_d_Jmp(dbgi);
2459 add_immBlock_pred(target_block, jump);
2461 set_cur_block(NULL);
2464 static void switch_statement_to_firm(const switch_statement_t *statement)
2466 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2468 ir_node *expression = expression_to_firm(statement->expression);
2469 ir_node *cond = new_d_Cond(dbgi, expression);
2470 ir_node *break_block = new_immBlock();
2472 set_cur_block(NULL);
2474 ir_node *const old_switch_cond = current_switch_cond;
2475 ir_node *const old_break_label = break_label;
2476 const bool old_saw_default_label = saw_default_label;
2477 current_switch_cond = cond;
2478 break_label = break_block;
2480 statement_to_firm(statement->body);
2482 if(get_cur_block() != NULL) {
2483 ir_node *jmp = new_Jmp();
2484 add_immBlock_pred(break_block, jmp);
2487 if (!saw_default_label) {
2488 set_cur_block(get_nodes_block(cond));
2489 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2490 MAGIC_DEFAULT_PN_NUMBER);
2491 add_immBlock_pred(break_block, proj);
2494 assert(current_switch_cond == cond);
2495 assert(break_label == break_block);
2496 current_switch_cond = old_switch_cond;
2497 break_label = old_break_label;
2498 saw_default_label = old_saw_default_label;
2500 mature_immBlock(break_block);
2501 set_cur_block(break_block);
2504 static long fold_constant(const expression_t *expression)
2506 ir_graph *old_current_ir_graph = current_ir_graph;
2507 current_ir_graph = get_const_code_irg();
2509 ir_node *cnst = expression_to_firm(expression);
2510 if(!is_Const(cnst)) {
2511 panic("couldn't fold constantl");
2513 tarval *tv = get_Const_tarval(cnst);
2514 if(!tarval_is_long(tv)) {
2515 panic("folded constant not an integer");
2518 long res = get_tarval_long(tv);
2520 current_ir_graph = old_current_ir_graph;
2524 static void case_label_to_firm(const case_label_statement_t *statement)
2526 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2528 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2530 /* let's create a node and hope firm constant folding creates a Const
2533 set_cur_block(get_nodes_block(current_switch_cond));
2534 if(statement->expression) {
2535 long pn = fold_constant(statement->expression);
2536 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2537 /* oops someone detected our cheating... */
2538 panic("magic default pn used");
2540 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2542 saw_default_label = true;
2543 proj = new_d_defaultProj(dbgi, current_switch_cond,
2544 MAGIC_DEFAULT_PN_NUMBER);
2547 ir_node *block = new_immBlock();
2548 if (fallthrough != NULL) {
2549 add_immBlock_pred(block, fallthrough);
2551 add_immBlock_pred(block, proj);
2552 mature_immBlock(block);
2554 statement_to_firm(statement->label_statement);
2557 static ir_node *get_label_block(declaration_t *label)
2559 assert(label->namespc == NAMESPACE_LABEL);
2561 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2562 return label->v.block;
2564 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2566 ir_node *old_cur_block = get_cur_block();
2567 ir_node *block = new_immBlock();
2568 set_cur_block(old_cur_block);
2570 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2571 label->v.block = block;
2573 ARR_APP1(ir_node *, imature_blocks, block);
2578 static void label_to_firm(const label_statement_t *statement)
2580 ir_node *block = get_label_block(statement->label);
2582 if(get_cur_block() != NULL) {
2583 ir_node *jmp = new_Jmp();
2584 add_immBlock_pred(block, jmp);
2587 set_cur_block(block);
2590 statement_to_firm(statement->label_statement);
2593 static void goto_to_firm(const goto_statement_t *statement)
2595 if(get_cur_block() == NULL)
2598 ir_node *block = get_label_block(statement->label);
2599 ir_node *jmp = new_Jmp();
2600 add_immBlock_pred(block, jmp);
2602 set_cur_block(NULL);
2605 static void statement_to_firm(statement_t *statement)
2607 switch(statement->type) {
2608 case STATEMENT_COMPOUND:
2609 compound_statement_to_firm((compound_statement_t*) statement);
2611 case STATEMENT_RETURN:
2612 return_statement_to_firm((return_statement_t*) statement);
2614 case STATEMENT_EXPRESSION:
2615 expression_statement_to_firm((expression_statement_t*) statement);
2618 if_statement_to_firm((if_statement_t*) statement);
2620 case STATEMENT_WHILE:
2621 while_statement_to_firm((while_statement_t*) statement);
2623 case STATEMENT_DO_WHILE:
2624 do_while_statement_to_firm((do_while_statement_t*) statement);
2626 case STATEMENT_DECLARATION:
2627 declaration_statement_to_firm((declaration_statement_t*) statement);
2629 case STATEMENT_BREAK:
2630 create_jump_statement(statement, break_label);
2632 case STATEMENT_CONTINUE:
2633 create_jump_statement(statement, continue_label);
2635 case STATEMENT_SWITCH:
2636 switch_statement_to_firm((switch_statement_t*) statement);
2638 case STATEMENT_CASE_LABEL:
2639 case_label_to_firm((case_label_statement_t*) statement);
2642 for_statement_to_firm((for_statement_t*) statement);
2644 case STATEMENT_LABEL:
2645 label_to_firm((label_statement_t*) statement);
2647 case STATEMENT_GOTO:
2648 goto_to_firm((goto_statement_t*) statement);
2653 panic("Statement not implemented\n");
2656 static int count_local_declarations(const declaration_t * decl,
2657 const declaration_t *const end)
2660 for (; decl != end; decl = decl->next) {
2661 const type_t *type = skip_typeref(decl->type);
2662 switch (type->type) {
2666 if (!decl->address_taken)
2676 static int count_decls_in_stmts(const statement_t *stmt)
2679 for (; stmt != NULL; stmt = stmt->base.next) {
2680 switch (stmt->type) {
2681 case STATEMENT_DECLARATION: {
2682 const declaration_statement_t *const decl_stmt =
2683 (const declaration_statement_t*)stmt;
2684 count += count_local_declarations(decl_stmt->declarations_begin,
2685 decl_stmt->declarations_end->next);
2689 case STATEMENT_COMPOUND: {
2690 const compound_statement_t *const comp =
2691 (const compound_statement_t*)stmt;
2692 count += count_decls_in_stmts(comp->statements);
2696 case STATEMENT_IF: {
2697 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2698 count += count_decls_in_stmts(if_stmt->true_statement);
2699 count += count_decls_in_stmts(if_stmt->false_statement);
2703 case STATEMENT_SWITCH: {
2704 const switch_statement_t *const switch_stmt =
2705 (const switch_statement_t*)stmt;
2706 count += count_decls_in_stmts(switch_stmt->body);
2710 case STATEMENT_LABEL: {
2711 const label_statement_t *const label_stmt =
2712 (const label_statement_t*)stmt;
2713 count += count_decls_in_stmts(label_stmt->label_statement);
2717 case STATEMENT_WHILE: {
2718 const while_statement_t *const while_stmt =
2719 (const while_statement_t*)stmt;
2720 count += count_decls_in_stmts(while_stmt->body);
2724 case STATEMENT_DO_WHILE: {
2725 const do_while_statement_t *const do_while_stmt =
2726 (const do_while_statement_t*)stmt;
2727 count += count_decls_in_stmts(do_while_stmt->body);
2731 case STATEMENT_FOR: {
2732 const for_statement_t *const for_stmt =
2733 (const for_statement_t*)stmt;
2734 /* TODO initialisation */
2735 count += count_decls_in_stmts(for_stmt->body);
2739 case STATEMENT_BREAK:
2740 case STATEMENT_CASE_LABEL:
2741 case STATEMENT_CONTINUE:
2742 case STATEMENT_EXPRESSION:
2743 case STATEMENT_GOTO:
2744 case STATEMENT_INVALID:
2745 case STATEMENT_RETURN:
2752 static int get_function_n_local_vars(declaration_t *declaration)
2756 /* count parameters */
2757 count += count_local_declarations(declaration->context.declarations, NULL);
2759 /* count local variables declared in body */
2760 count += count_decls_in_stmts(declaration->init.statement);
2762 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2763 * for expression statements... */
2769 static void initialize_function_parameters(declaration_t *declaration)
2771 ir_graph *irg = current_ir_graph;
2772 ir_node *args = get_irg_args(irg);
2773 ir_node *start_block = get_irg_start_block(irg);
2774 ir_type *function_irtype = get_ir_type(declaration->type);
2777 declaration_t *parameter = declaration->context.declarations;
2778 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2779 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2780 type_t *type = skip_typeref(parameter->type);
2782 bool needs_entity = parameter->address_taken;
2783 if(type->type == TYPE_COMPOUND_STRUCT
2784 || type->type == TYPE_COMPOUND_UNION) {
2785 needs_entity = true;
2789 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2790 ident *id = new_id_from_str(parameter->symbol->string);
2791 set_entity_ident(entity, id);
2793 parameter->declaration_type
2794 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2795 parameter->v.entity = entity;
2799 ir_mode *mode = get_ir_mode(parameter->type);
2801 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2803 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2804 parameter->v.value_number = next_value_number_function;
2805 ++next_value_number_function;
2807 set_value(parameter->v.value_number, proj);
2811 static void create_function(declaration_t *declaration)
2813 ir_entity *function_entity = get_function_entity(declaration);
2815 if(declaration->init.statement == NULL)
2818 current_function_decl = declaration;
2819 current_function_name = NULL;
2821 assert(imature_blocks == NULL);
2822 imature_blocks = NEW_ARR_F(ir_node*, 0);
2824 int n_local_vars = get_function_n_local_vars(declaration);
2825 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
2826 ir_node *first_block = get_cur_block();
2828 next_value_number_function = 0;
2829 initialize_function_parameters(declaration);
2831 statement_to_firm(declaration->init.statement);
2833 ir_node *end_block = get_irg_end_block(irg);
2835 /* do we have a return statement yet? */
2836 if(get_cur_block() != NULL) {
2837 assert(declaration->type->type == TYPE_FUNCTION);
2838 const function_type_t* const func_type = &declaration->type->function;
2841 if (func_type->result_type == type_void) {
2842 ret = new_Return(get_store(), 0, NULL);
2844 ir_mode *const mode = get_ir_mode(func_type->result_type);
2846 // ยง5.1.2.2.3 main implicitly returns 0
2847 if (strcmp(declaration->symbol->string, "main") == 0) {
2848 in[0] = new_Const(mode, get_mode_null(mode));
2850 in[0] = new_Unknown(mode);
2852 ret = new_Return(get_store(), 1, in);
2854 add_immBlock_pred(end_block, ret);
2857 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2858 mature_immBlock(imature_blocks[i]);
2860 DEL_ARR_F(imature_blocks);
2861 imature_blocks = NULL;
2863 mature_immBlock(first_block);
2864 mature_immBlock(end_block);
2866 irg_finalize_cons(irg);
2868 /* finalize the frame type */
2869 ir_type *frame_type = get_irg_frame_type(irg);
2870 int n = get_compound_n_members(frame_type);
2873 for(int i = 0; i < n; ++i) {
2874 ir_entity *entity = get_compound_member(frame_type, i);
2875 ir_type *entity_type = get_entity_type(entity);
2877 int align = get_type_alignment_bytes(entity_type);
2878 if(align > align_all)
2882 misalign = offset % align;
2884 offset += align - misalign;
2888 set_entity_offset(entity, offset);
2889 offset += get_type_size_bytes(entity_type);
2891 set_type_size_bytes(frame_type, offset);
2892 set_type_alignment_bytes(frame_type, align_all);
2893 set_type_state(frame_type, layout_fixed);
2898 static void create_global_variable(declaration_t *declaration)
2900 ir_type *global_type = get_glob_type();
2901 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2904 ir_entity *entity = declaration->v.entity;
2905 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2906 set_entity_visibility(entity, visibility_local);
2907 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2908 set_entity_visibility(entity, visibility_external_allocated);
2910 set_entity_visibility(entity, visibility_external_visible);
2912 current_ir_graph = get_const_code_irg();
2913 create_initializer(declaration);
2916 static void context_to_firm(context_t *context)
2918 /* first pass: create declarations */
2919 declaration_t *declaration = context->declarations;
2920 for( ; declaration != NULL; declaration = declaration->next) {
2921 if(declaration->namespc != NAMESPACE_NORMAL)
2923 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2924 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2926 if(declaration->symbol == NULL)
2929 type_t *type = declaration->type;
2930 if(type->type == TYPE_FUNCTION) {
2931 get_function_entity(declaration);
2933 create_global_variable(declaration);
2937 /* second pass: create code */
2938 declaration = context->declarations;
2939 for( ; declaration != NULL; declaration = declaration->next) {
2940 if(declaration->namespc != NAMESPACE_NORMAL)
2942 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2943 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2945 if(declaration->symbol == NULL)
2948 type_t *type = declaration->type;
2949 if(type->type != TYPE_FUNCTION)
2952 create_function(declaration);
2956 void translation_unit_to_firm(translation_unit_t *unit)
2958 /* just to be sure */
2959 continue_label = NULL;
2961 current_switch_cond = NULL;
2963 context_to_firm(& unit->context);