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;
26 static ir_type *ir_type_void_ptr;
28 static type_t *type_const_char;
29 static type_t *type_void;
30 static type_t *type_int;
32 static symbol_t *symbol_alloca;
34 static int next_value_number_function;
35 static ir_node *continue_label;
36 static ir_node *break_label;
37 static ir_node *current_switch_cond;
38 static bool saw_default_label;
39 static ir_node **imature_blocks;
41 static const declaration_t *current_function_decl;
42 static ir_node *current_function_name;
44 typedef enum declaration_type_t {
45 DECLARATION_TYPE_UNKNOWN,
46 DECLARATION_TYPE_FUNCTION,
47 DECLARATION_TYPE_GLOBAL_VARIABLE,
48 DECLARATION_TYPE_LOCAL_VARIABLE,
49 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
50 DECLARATION_TYPE_COMPOUND_MEMBER,
51 DECLARATION_TYPE_LABEL_BLOCK,
52 DECLARATION_TYPE_ENUM_ENTRY
55 static ir_type *get_ir_type(type_t *type);
57 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
61 const declaration_t *declaration = & value_numbers[pos]->declaration;
63 print_warning_prefix(declaration->source_position);
64 fprintf(stderr, "variable '%s' might be used uninitialized\n",
65 declaration->symbol->string);
67 fprintf(stderr, "Some variable might be used uninitialized\n");
68 return new_r_Unknown(irg, mode);
71 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
73 const source_position_t *pos = (const source_position_t*) dbg;
76 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
80 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
82 const source_position_t *pos = (const source_position_t*) dbg;
87 return pos->input_name;
90 void init_ast2firm(void)
92 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
93 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
94 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
96 ir_type_int = get_ir_type(type_int);
97 ir_type_const_char = get_ir_type(type_const_char);
98 ir_type_void = get_ir_type(type_int); /* we don't have a real void
100 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
101 ir_type_void, mode_P_data);
103 type_void->base.firm_type = ir_type_void;
105 symbol_alloca = symbol_table_insert("__builtin_alloca");
108 void exit_ast2firm(void)
112 static unsigned unique_id = 0;
114 static ident *unique_ident(const char *tag)
118 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
120 return new_id_from_str(buf);
123 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
125 switch(atomic_type->atype) {
126 case ATOMIC_TYPE_SCHAR:
127 case ATOMIC_TYPE_CHAR:
129 case ATOMIC_TYPE_UCHAR:
131 case ATOMIC_TYPE_SHORT:
133 case ATOMIC_TYPE_USHORT:
135 case ATOMIC_TYPE_LONG:
136 case ATOMIC_TYPE_INT:
138 case ATOMIC_TYPE_ULONG:
139 case ATOMIC_TYPE_UINT:
141 case ATOMIC_TYPE_LONGLONG:
143 case ATOMIC_TYPE_ULONGLONG:
145 case ATOMIC_TYPE_FLOAT:
147 case ATOMIC_TYPE_DOUBLE:
149 case ATOMIC_TYPE_LONG_DOUBLE:
151 case ATOMIC_TYPE_BOOL:
153 #ifdef PROVIDE_COMPLEX
154 case ATOMIC_TYPE_FLOAT_COMPLEX:
155 case ATOMIC_TYPE_DOUBLE_COMPLEX:
156 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
157 panic("complex lowering not implemented yet");
159 case ATOMIC_TYPE_FLOAT_IMAGINARY:
160 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
161 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
162 panic("imaginary lowering not implemented yet");
165 case ATOMIC_TYPE_VOID:
166 /* firm has no real void... */
168 case ATOMIC_TYPE_INVALID:
171 panic("Encountered unknown atomic type");
175 static unsigned get_type_size(type_t *type);
177 static unsigned get_atomic_type_size(const atomic_type_t *type)
179 switch(type->atype) {
180 case ATOMIC_TYPE_CHAR:
181 case ATOMIC_TYPE_SCHAR:
182 case ATOMIC_TYPE_UCHAR:
185 case ATOMIC_TYPE_SHORT:
186 case ATOMIC_TYPE_USHORT:
189 case ATOMIC_TYPE_BOOL:
190 case ATOMIC_TYPE_INT:
191 case ATOMIC_TYPE_UINT:
192 case ATOMIC_TYPE_LONG:
193 case ATOMIC_TYPE_ULONG:
194 case ATOMIC_TYPE_FLOAT:
197 case ATOMIC_TYPE_LONGLONG:
198 case ATOMIC_TYPE_ULONGLONG:
199 case ATOMIC_TYPE_DOUBLE:
202 case ATOMIC_TYPE_LONG_DOUBLE:
205 case ATOMIC_TYPE_VOID:
208 case ATOMIC_TYPE_INVALID:
211 panic("Trying to determine size of invalid atomic type");
214 static unsigned get_compound_type_size(compound_type_t *type)
216 ir_type *irtype = get_ir_type((type_t*) type);
217 return get_type_size_bytes(irtype);
220 static unsigned get_array_type_size(array_type_t *type)
222 ir_type *irtype = get_ir_type((type_t*) type);
223 return get_type_size_bytes(irtype);
226 static unsigned get_type_size(type_t *type)
228 type = skip_typeref(type);
232 return get_atomic_type_size((const atomic_type_t*) type);
234 return get_mode_size_bytes(mode_Is);
235 case TYPE_COMPOUND_UNION:
236 case TYPE_COMPOUND_STRUCT:
237 return get_compound_type_size((compound_type_t*) type);
239 /* just a pointer to the function */
240 return get_mode_size_bytes(mode_P_code);
242 return get_mode_size_bytes(mode_P_data);
244 return get_array_type_size((array_type_t*) type);
252 panic("Trying to determine size of invalid type");
255 static unsigned count_parameters(const function_type_t *function_type)
259 function_parameter_t *parameter = function_type->parameters;
260 for ( ; parameter != NULL; parameter = parameter->next) {
270 static long fold_constant(const expression_t *expression);
272 static ir_type *create_atomic_type(const atomic_type_t *type)
274 ir_mode *mode = get_atomic_mode(type);
275 ident *id = get_mode_ident(mode);
276 ir_type *irtype = new_type_primitive(id, mode);
281 static ir_type *create_method_type(const function_type_t *function_type)
283 type_t *result_type = function_type->result_type;
285 ident *id = unique_ident("functiontype");
286 int n_parameters = count_parameters(function_type);
287 int n_results = result_type == type_void ? 0 : 1;
288 ir_type *irtype = new_type_method(id, n_parameters, n_results);
290 if(result_type != type_void) {
291 ir_type *restype = get_ir_type(result_type);
292 set_method_res_type(irtype, 0, restype);
295 function_parameter_t *parameter = function_type->parameters;
297 for( ; parameter != NULL; parameter = parameter->next) {
298 ir_type *p_irtype = get_ir_type(parameter->type);
299 set_method_param_type(irtype, n, p_irtype);
303 if(function_type->variadic || function_type->unspecified_parameters) {
304 set_method_variadicity(irtype, variadicity_variadic);
310 static ir_type *create_pointer_type(pointer_type_t *type)
312 type_t *points_to = type->points_to;
313 ir_type *ir_points_to;
314 /* Avoid endless recursion if the points_to type contains this poiner type
315 * again (might be a struct). We therefore first create a void* pointer
316 * and then set the real points_to type
318 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
319 ir_type_void, mode_P_data);
320 type->type.firm_type = ir_type;
322 ir_points_to = get_ir_type(points_to);
323 set_pointer_points_to_type(ir_type, ir_points_to);
328 static ir_type *create_array_type(array_type_t *type)
330 type_t *element_type = type->element_type;
331 ir_type *ir_element_type = get_ir_type(element_type);
333 ident *id = unique_ident("array");
334 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
336 if(type->size != NULL) {
337 int n_elements = fold_constant(type->size);
339 set_array_bounds_int(ir_type, 0, 0, n_elements);
341 size_t elemsize = get_type_size_bytes(ir_element_type);
342 int align = get_type_alignment_bytes(ir_element_type);
343 if(elemsize % align > 0) {
344 elemsize += align - (elemsize % align);
346 set_type_size_bytes(ir_type, n_elements * elemsize);
347 set_type_alignment_bytes(ir_type, align);
348 set_type_state(ir_type, layout_fixed);
354 #define INVALID_TYPE ((ir_type_ptr)-1)
356 static ir_type *create_struct_type(compound_type_t *type)
358 symbol_t *symbol = type->declaration->symbol;
361 id = unique_ident(symbol->string);
363 id = unique_ident("__anonymous_struct");
365 ir_type *ir_type = new_type_struct(id);
367 type->type.firm_type = ir_type;
371 declaration_t *entry = type->declaration->context.declarations;
372 for( ; entry != NULL; entry = entry->next) {
373 if(entry->namespc != NAMESPACE_NORMAL)
376 ident *ident = new_id_from_str(entry->symbol->string);
377 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
379 int entry_size = get_type_size_bytes(entry_ir_type);
380 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
381 int misalign = offset % entry_alignment;
383 offset += entry_alignment - misalign;
385 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
386 set_entity_offset(entity, offset);
387 add_struct_member(ir_type, entity);
388 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
389 entry->v.entity = entity;
391 offset += entry_size;
392 if(entry_alignment > align_all) {
393 if(entry_alignment % align_all != 0) {
394 panic("Uneven alignments not supported yet");
396 align_all = entry_alignment;
400 int misalign = offset % align_all;
402 set_type_alignment_bytes(ir_type, align_all);
403 set_type_size_bytes(ir_type, offset);
404 set_type_state(ir_type, layout_fixed);
409 static ir_type *create_union_type(compound_type_t *type)
411 declaration_t *declaration = type->declaration;
412 symbol_t *symbol = declaration->symbol;
415 id = unique_ident(symbol->string);
417 id = unique_ident("__anonymous_union");
419 ir_type *ir_type = new_type_union(id);
421 type->type.firm_type = ir_type;
425 declaration_t *entry = declaration->context.declarations;
426 for( ; entry != NULL; entry = entry->next) {
427 if(entry->namespc != NAMESPACE_NORMAL)
430 ident *ident = new_id_from_str(entry->symbol->string);
431 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
433 int entry_size = get_type_size_bytes(entry_ir_type);
434 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
436 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
437 add_union_member(ir_type, entity);
438 set_entity_offset(entity, 0);
439 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
440 entry->v.entity = entity;
442 if(entry_size > size) {
445 if(entry_alignment > align_all) {
446 if(entry_alignment % align_all != 0) {
447 panic("Uneven alignments not supported yet");
449 align_all = entry_alignment;
453 set_type_alignment_bytes(ir_type, align_all);
454 set_type_size_bytes(ir_type, size);
455 set_type_state(ir_type, layout_fixed);
460 static ir_node *expression_to_firm(const expression_t *expression);
461 static inline ir_mode *get_ir_mode(type_t *type);
463 static ir_type *create_enum_type(enum_type_t *const type)
465 type->type.firm_type = ir_type_int;
467 ir_mode *const mode = get_ir_mode((type_t*) type);
468 tarval *const one = get_mode_one(mode);
469 tarval * tv_next = get_tarval_null(mode);
471 declaration_t *declaration = type->declaration->next;
472 for (; declaration != NULL; declaration = declaration->next) {
473 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
476 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
478 expression_t *const init = declaration->init.enum_value;
480 ir_node *const cnst = expression_to_firm(init);
481 if (!is_Const(cnst)) {
482 panic("couldn't fold constant");
484 tv_next = get_Const_tarval(cnst);
486 declaration->v.enum_val = tv_next;
487 tv_next = tarval_add(tv_next, one);
493 static ir_type *get_ir_type(type_t *type)
495 assert(type != NULL);
497 type = skip_typeref(type);
499 if(type->base.firm_type != NULL) {
500 assert(type->base.firm_type != INVALID_TYPE);
501 return type->base.firm_type;
504 ir_type *firm_type = NULL;
507 firm_type = create_atomic_type((atomic_type_t*) type);
510 firm_type = create_method_type((function_type_t*) type);
513 firm_type = create_pointer_type((pointer_type_t*) type);
516 firm_type = create_array_type((array_type_t*) type);
518 case TYPE_COMPOUND_STRUCT:
519 firm_type = create_struct_type((compound_type_t*) type);
521 case TYPE_COMPOUND_UNION:
522 firm_type = create_union_type((compound_type_t*) type);
525 firm_type = create_enum_type((enum_type_t*) type);
534 if(firm_type == NULL)
535 panic("unknown type found");
537 type->base.firm_type = firm_type;
541 static inline ir_mode *get_ir_mode(type_t *type)
543 ir_type *irtype = get_ir_type(type);
545 /* firm doesn't report a mode for arrays somehow... */
546 if(is_Array_type(irtype)) {
550 ir_mode *mode = get_type_mode(irtype);
551 assert(mode != NULL);
555 static ir_entity* get_function_entity(declaration_t *declaration)
557 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
558 return declaration->v.entity;
559 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
561 symbol_t *symbol = declaration->symbol;
562 ident *id = new_id_from_str(symbol->string);
564 ir_type *global_type = get_glob_type();
565 ir_type *ir_type_method = get_ir_type(declaration->type);
566 assert(is_Method_type(ir_type_method));
568 ir_entity *entity = new_entity(global_type, id, ir_type_method);
569 set_entity_ld_ident(entity, id);
570 if(declaration->storage_class == STORAGE_CLASS_STATIC
571 || declaration->is_inline) {
572 set_entity_visibility(entity, visibility_local);
573 } else if(declaration->init.statement != NULL) {
574 set_entity_visibility(entity, visibility_external_visible);
576 set_entity_visibility(entity, visibility_external_allocated);
579 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
580 declaration->v.entity = entity;
585 static dbg_info *get_dbg_info(const source_position_t *pos)
587 return (dbg_info*) pos;
590 static ir_node *const_to_firm(const const_t *cnst)
592 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
593 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
598 if(mode_is_float(mode)) {
599 tv = new_tarval_from_double(cnst->v.float_value, mode);
601 if(mode_is_signed(mode)) {
602 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
604 len = snprintf(buf, sizeof(buf), "%llu", cnst->v.int_value);
606 tv = new_tarval_from_str(buf, len, mode);
609 return new_d_Const(dbgi, mode, tv);
612 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
614 assert(entity != NULL);
615 union symconst_symbol sym;
616 sym.entity_p = entity;
617 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
620 static ir_node *string_to_firm(const source_position_t *const src_pos,
621 const char *const id_prefix,
622 const char *const string)
624 ir_type *const global_type = get_glob_type();
625 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
628 ident *const id = unique_ident(id_prefix);
629 ir_entity *const entity = new_entity(global_type, id, type);
630 set_entity_ld_ident(entity, id);
631 set_entity_variability(entity, variability_constant);
633 ir_type *const elem_type = ir_type_const_char;
634 ir_mode *const mode = get_type_mode(elem_type);
636 const size_t slen = strlen(string) + 1;
638 set_array_lower_bound_int(type, 0, 0);
639 set_array_upper_bound_int(type, 0, slen);
640 set_type_size_bytes(type, slen);
641 set_type_state(type, layout_fixed);
643 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
644 for(size_t i = 0; i < slen; ++i) {
645 tvs[i] = new_tarval_from_long(string[i], mode);
648 set_array_entity_values(entity, tvs, slen);
651 dbg_info *const dbgi = get_dbg_info(src_pos);
653 return create_symconst(dbgi, entity);
656 static ir_node *string_literal_to_firm(const string_literal_t* literal)
658 return string_to_firm(&literal->expression.source_position, "Lstr",
662 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
663 dbg_info *const dbgi)
665 if(is_compound_type(irtype) || is_Array_type(irtype)) {
669 ir_mode *const mode = get_type_mode(irtype);
670 ir_node *const memory = get_store();
671 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
672 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
673 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
678 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
680 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
681 declaration_t *declaration = ref->declaration;
682 type_t *type = skip_typeref(declaration->type);
684 switch((declaration_type_t) declaration->declaration_type) {
685 case DECLARATION_TYPE_UNKNOWN:
686 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
692 case DECLARATION_TYPE_ENUM_ENTRY: {
693 ir_mode *const mode = get_ir_mode(type);
694 return new_Const(mode, declaration->v.enum_val);
697 case DECLARATION_TYPE_LOCAL_VARIABLE: {
698 ir_mode *mode = get_ir_mode(type);
699 return get_value(declaration->v.value_number, mode);
701 case DECLARATION_TYPE_FUNCTION: {
702 return create_symconst(dbgi, declaration->v.entity);
704 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
705 ir_entity *entity = declaration->v.entity;
706 ir_node *symconst = create_symconst(dbgi, entity);
707 ir_type *irtype = get_entity_type(entity);
708 return deref_address(irtype, symconst, dbgi);
710 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
711 ir_entity *entity = declaration->v.entity;
712 ir_node *frame = get_irg_frame(current_ir_graph);
713 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
714 ir_type *irtype = get_entity_type(entity);
715 return deref_address(irtype, sel, dbgi);
718 case DECLARATION_TYPE_COMPOUND_MEMBER:
719 case DECLARATION_TYPE_LABEL_BLOCK:
720 panic("not implemented reference type");
723 panic("reference to declaration with unknown type found");
726 static ir_node *reference_addr(const reference_expression_t *ref)
728 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
729 declaration_t *declaration = ref->declaration;
731 switch((declaration_type_t) declaration->declaration_type) {
732 case DECLARATION_TYPE_UNKNOWN:
734 case DECLARATION_TYPE_LOCAL_VARIABLE:
735 panic("local variable without entity has no address");
736 case DECLARATION_TYPE_FUNCTION: {
737 return create_symconst(dbgi, declaration->v.entity);
739 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
740 ir_entity *entity = declaration->v.entity;
741 ir_node *symconst = create_symconst(dbgi, entity);
744 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
745 ir_entity *entity = declaration->v.entity;
746 ir_node *frame = get_irg_frame(current_ir_graph);
747 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
752 case DECLARATION_TYPE_ENUM_ENTRY:
753 panic("trying to reference enum entry");
755 case DECLARATION_TYPE_COMPOUND_MEMBER:
756 case DECLARATION_TYPE_LABEL_BLOCK:
757 panic("not implemented reference type");
760 panic("reference to declaration with unknown type found");
763 static ir_node *process_builtin_call(const call_expression_t *call)
765 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
767 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
768 builtin_symbol_expression_t *builtin
769 = (builtin_symbol_expression_t*) call->function;
770 symbol_t *symbol = builtin->symbol;
772 if(symbol == symbol_alloca) {
773 if(call->arguments == NULL || call->arguments->next != NULL) {
774 panic("invalid number of parameters on __builtin_alloca");
776 expression_t *argument = call->arguments->expression;
777 ir_node *size = expression_to_firm(argument);
779 ir_node *store = get_store();
780 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
782 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
784 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
788 panic("Unsupported builtin found\n");
792 static ir_node *call_expression_to_firm(const call_expression_t *call)
794 assert(get_cur_block() != NULL);
796 expression_t *function = call->function;
797 if(function->type == EXPR_BUILTIN_SYMBOL) {
798 return process_builtin_call(call);
800 ir_node *callee = expression_to_firm(function);
802 function_type_t *function_type;
803 if (function->datatype->type == TYPE_POINTER) {
804 pointer_type_t *const ptr_type = (pointer_type_t*)function->datatype;
805 assert(ptr_type->points_to->type == TYPE_FUNCTION);
806 function_type = (function_type_t*)ptr_type->points_to;
808 assert(function->datatype->type == TYPE_FUNCTION);
809 function_type = (function_type_t*)function->datatype;
812 int n_parameters = 0;
813 call_argument_t *argument = call->arguments;
814 for( ; argument != NULL; argument = argument->next) {
818 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
819 ir_type *new_method_type = NULL;
820 if(function_type->variadic || function_type->unspecified_parameters) {
821 /* we need to construct a new method type matching the call
823 int n_res = get_method_n_ress(ir_method_type);
824 new_method_type = new_type_method(unique_ident("calltype"),
825 n_parameters, n_res);
826 set_method_calling_convention(new_method_type,
827 get_method_calling_convention(ir_method_type));
828 set_method_additional_properties(new_method_type,
829 get_method_additional_properties(ir_method_type));
831 for(int i = 0; i < n_res; ++i) {
832 set_method_res_type(new_method_type, i,
833 get_method_res_type(ir_method_type, i));
836 ir_node *in[n_parameters];
838 argument = call->arguments;
840 for( ; argument != NULL; argument = argument->next) {
841 expression_t *expression = argument->expression;
842 ir_node *arg_node = expression_to_firm(expression);
845 if(new_method_type != NULL) {
846 ir_type *irtype = get_ir_type(expression->datatype);
847 set_method_param_type(new_method_type, n, irtype);
852 assert(n == n_parameters);
854 if(new_method_type != NULL)
855 ir_method_type = new_method_type;
857 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
858 ir_node *store = get_store();
859 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
861 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
864 type_t *result_type = function_type->result_type;
865 ir_node *result = NULL;
866 if(result_type != type_void) {
867 ir_mode *mode = get_ir_mode(result_type);
868 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
869 result = new_d_Proj(dbgi, resproj, mode, 0);
875 static void statement_to_firm(statement_t *statement);
876 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
878 static ir_node *expression_to_addr(const expression_t *expression);
879 static void create_condition_evaluation(const expression_t *expression,
881 ir_node *false_block);
883 static void set_value_for_expression(const expression_t *expression,
886 if(expression->type == EXPR_REFERENCE) {
887 reference_expression_t *ref = (reference_expression_t*) expression;
889 declaration_t *declaration = ref->declaration;
890 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
891 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
892 set_value(declaration->v.value_number, value);
897 dbg_info *dbgi = get_dbg_info(&expression->source_position);
898 ir_node *addr = expression_to_addr(expression);
899 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
900 ir_node *memory = get_store();
901 ir_node *store = new_d_Store(dbgi, memory, addr, value);
902 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
903 set_store(store_mem);
906 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
908 ir_mode *value_mode = get_irn_mode(value);
910 if (value_mode == dest_mode || is_Bad(value))
913 if(dest_mode == mode_b) {
914 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
915 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
916 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
920 return new_d_Conv(dbgi, value, dest_mode);
923 static ir_node *create_incdec(const unary_expression_t *expression)
925 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
926 type_t *type = expression->expression.datatype;
927 ir_mode *mode = get_ir_mode(type);
928 expression_t *value = expression->value;
930 ir_node *value_node = expression_to_firm(value);
933 if(type->type == TYPE_POINTER) {
934 pointer_type_t *pointer_type = (pointer_type_t*) type;
935 unsigned elem_size = get_type_size(pointer_type->points_to);
936 offset = new_Const_long(mode_Is, elem_size);
938 assert(is_type_arithmetic(type));
939 offset = new_Const(mode, get_mode_one(mode));
942 switch(expression->type) {
943 case UNEXPR_POSTFIX_INCREMENT: {
944 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
945 set_value_for_expression(value, new_value);
948 case UNEXPR_POSTFIX_DECREMENT: {
949 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
950 set_value_for_expression(value, new_value);
953 case UNEXPR_PREFIX_INCREMENT: {
954 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
955 set_value_for_expression(value, new_value);
958 case UNEXPR_PREFIX_DECREMENT: {
959 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
960 set_value_for_expression(value, new_value);
964 panic("no incdec expr in create_incdec");
969 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
971 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
972 type_t *type = skip_typeref(expression->expression.datatype);
974 if(expression->type == UNEXPR_TAKE_ADDRESS)
975 return expression_to_addr(expression->value);
977 const expression_t *value = expression->value;
978 ir_node *value_node = expression_to_firm(value);
980 switch(expression->type) {
982 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
985 case UNEXPR_BITWISE_NEGATE:
986 return new_d_Not(dbgi, value_node, get_ir_mode(type));
988 if(get_irn_mode(value_node) != mode_b) {
989 value_node = create_conv(dbgi, value_node, mode_b);
991 value_node = new_d_Not(dbgi, value_node, mode_b);
992 ir_mode *const mode = get_ir_mode(type);
994 value_node = create_conv(dbgi, value_node, mode);
998 case UNEXPR_DEREFERENCE: {
999 ir_type *irtype = get_ir_type(type);
1000 return deref_address(irtype, value_node, dbgi);
1002 case UNEXPR_POSTFIX_INCREMENT:
1003 case UNEXPR_POSTFIX_DECREMENT:
1004 case UNEXPR_PREFIX_INCREMENT:
1005 case UNEXPR_PREFIX_DECREMENT:
1006 return create_incdec(expression);
1008 return create_conv(dbgi, value_node, get_ir_mode(type));
1010 case UNEXPR_TAKE_ADDRESS:
1011 case UNEXPR_INVALID:
1014 panic("invalid UNEXPR type found");
1017 static long get_pnc(binary_expression_type_t type)
1020 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1021 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1022 case BINEXPR_LESS: return pn_Cmp_Lt;
1023 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1024 case BINEXPR_GREATER: return pn_Cmp_Gt;
1025 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1029 panic("trying to get pn_Cmp from non-comparison binexpr type");
1032 static ir_node *create_lazy_op(const binary_expression_t *expression)
1034 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1035 type_t *type = expression->expression.datatype;
1036 ir_mode *mode = get_ir_mode(type);
1038 ir_node *cur_block = get_cur_block();
1040 ir_node *one_block = new_immBlock();
1041 ir_node *one = new_Const(mode, get_mode_one(mode));
1042 ir_node *jmp_one = new_d_Jmp(dbgi);
1044 ir_node *zero_block = new_immBlock();
1045 ir_node *zero = new_Const(mode, get_mode_null(mode));
1046 ir_node *jmp_zero = new_d_Jmp(dbgi);
1048 set_cur_block(cur_block);
1049 create_condition_evaluation((const expression_t*) expression,
1050 one_block, zero_block);
1051 mature_immBlock(one_block);
1052 mature_immBlock(zero_block);
1054 ir_node *common_block = new_immBlock();
1055 add_immBlock_pred(common_block, jmp_one);
1056 add_immBlock_pred(common_block, jmp_zero);
1057 mature_immBlock(common_block);
1059 ir_node *in[2] = { one, zero };
1060 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1065 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1066 ir_node *right, ir_mode *mode);
1068 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1069 create_arithmetic_func func)
1071 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1072 ir_node *left = expression_to_firm(expression->left);
1073 ir_node *right = expression_to_firm(expression->right);
1074 type_t *type = expression->right->datatype;
1075 /* be careful with the modes, because in arithmetic assign nodes only
1076 * the right operand has the mode of the arithmetic already */
1077 ir_mode *mode = get_ir_mode(type);
1078 left = create_conv(dbgi, left, mode);
1079 ir_node *res = func(dbgi, left, right, mode);
1084 static ir_node *pointer_arithmetic(ir_node *const pointer,
1087 dbg_info *const dbgi,
1088 const create_arithmetic_func func)
1090 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1091 type_t *const points_to = pointer_type->points_to;
1092 const unsigned elem_size = get_type_size(points_to);
1094 assert(elem_size >= 1);
1095 if (elem_size > 1) {
1096 integer = create_conv(dbgi, integer, mode_Is);
1097 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1098 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1102 ir_mode *const mode = get_ir_mode(type);
1103 return func(dbgi, pointer, integer, mode);
1106 static ir_node *create_arithmetic_assign_binop(
1107 const binary_expression_t *expression, create_arithmetic_func func)
1109 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1110 type_t *const type = expression->expression.datatype;
1113 if (type->type == TYPE_POINTER) {
1114 ir_node *const pointer = expression_to_firm(expression->left);
1115 ir_node * integer = expression_to_firm(expression->right);
1116 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1118 value = create_arithmetic_binop(expression, func);
1121 ir_mode *const mode = get_ir_mode(type);
1122 value = create_conv(dbgi, value, mode);
1123 set_value_for_expression(expression->left, value);
1128 static ir_node *create_add(const binary_expression_t *expression)
1130 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1131 ir_node *left = expression_to_firm(expression->left);
1132 ir_node *right = expression_to_firm(expression->right);
1133 type_t *type = expression->expression.datatype;
1135 expression_t *expr_left = expression->left;
1136 expression_t *expr_right = expression->right;
1137 type_t *type_left = skip_typeref(expr_left->datatype);
1138 type_t *type_right = skip_typeref(expr_right->datatype);
1140 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1141 ir_mode *const mode = get_ir_mode(type);
1142 return new_d_Add(dbgi, left, right, mode);
1145 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1146 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1148 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1149 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1153 static ir_node *create_sub(const binary_expression_t *expression)
1155 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1156 expression_t *const expr_left = expression->left;
1157 expression_t *const expr_right = expression->right;
1158 ir_node *const left = expression_to_firm(expr_left);
1159 ir_node *const right = expression_to_firm(expr_right);
1160 type_t *const type = expression->expression.datatype;
1161 type_t *const type_left = skip_typeref(expr_left->datatype);
1162 type_t *const type_right = skip_typeref(expr_right->datatype);
1164 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1165 ir_mode *const mode = get_ir_mode(type);
1166 return new_d_Sub(dbgi, left, right, mode);
1167 } else if (type_left->type == TYPE_POINTER && type_right->type == TYPE_POINTER) {
1168 const pointer_type_t *const ptr_type = (const pointer_type_t*)type_left;
1169 const unsigned elem_size = get_type_size(ptr_type->points_to);
1170 ir_mode *const mode = get_ir_mode(type);
1171 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1172 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1173 ir_node *const no_mem = new_NoMem();
1174 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1175 op_pin_state_floats);
1176 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1179 assert(type_left->type == TYPE_POINTER);
1180 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1183 static ir_node *create_shift(const binary_expression_t *expression)
1185 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1186 ir_node *left = expression_to_firm(expression->left);
1187 ir_node *right = expression_to_firm(expression->right);
1188 type_t *type = expression->expression.datatype;
1189 ir_mode *mode = get_ir_mode(type);
1191 /* firm always wants the shift count to be unsigned */
1192 right = create_conv(dbgi, right, mode_Iu);
1196 switch(expression->type) {
1197 case BINEXPR_SHIFTLEFT_ASSIGN:
1198 case BINEXPR_SHIFTLEFT:
1199 res = new_d_Shl(dbgi, left, right, mode);
1201 case BINEXPR_SHIFTRIGHT_ASSIGN:
1202 case BINEXPR_SHIFTRIGHT: {
1203 expression_t *expr_left = expression->left;
1204 type_t *type_left = skip_typeref(expr_left->datatype);
1206 if(is_type_signed(type_left)) {
1207 res = new_d_Shrs(dbgi, left, right, mode);
1209 res = new_d_Shr(dbgi, left, right, mode);
1214 panic("create shift op called for non-shift op");
1221 static ir_node *create_divmod(const binary_expression_t *expression)
1223 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1224 ir_node *left = expression_to_firm(expression->left);
1225 ir_node *right = expression_to_firm(expression->right);
1226 ir_node *pin = new_Pin(new_NoMem());
1227 /* be careful with the modes, because in arithmetic assign nodes only
1228 * the right operand has the mode of the arithmetic already */
1229 type_t *type = expression->right->datatype;
1230 ir_mode *mode = get_ir_mode(type);
1231 left = create_conv(dbgi, left, mode);
1235 switch (expression->type) {
1237 case BINEXPR_DIV_ASSIGN:
1238 if(mode_is_float(mode)) {
1239 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1240 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1242 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1243 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1248 case BINEXPR_MOD_ASSIGN:
1249 assert(!mode_is_float(mode));
1250 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1251 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1254 default: panic("unexpected binary expression type in create_divmod()");
1260 static ir_node *create_arithmetic_assign_divmod(
1261 const binary_expression_t *expression)
1263 ir_node * value = create_divmod(expression);
1264 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1265 type_t *const type = expression->expression.datatype;
1266 ir_mode *const mode = get_ir_mode(type);
1268 assert(type->type != TYPE_POINTER);
1270 value = create_conv(dbgi, value, mode);
1271 set_value_for_expression(expression->left, value);
1276 static ir_node *create_arithmetic_assign_shift(
1277 const binary_expression_t *expression)
1279 ir_node * value = create_shift(expression);
1280 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1281 type_t *const type = expression->expression.datatype;
1282 ir_mode *const mode = get_ir_mode(type);
1284 value = create_conv(dbgi, value, mode);
1285 set_value_for_expression(expression->left, value);
1290 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1292 binary_expression_type_t type = expression->type;
1295 case BINEXPR_NOTEQUAL:
1297 case BINEXPR_LESSEQUAL:
1298 case BINEXPR_GREATER:
1299 case BINEXPR_GREATEREQUAL: {
1300 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1301 ir_node *left = expression_to_firm(expression->left);
1302 ir_node *right = expression_to_firm(expression->right);
1303 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1304 long pnc = get_pnc(type);
1305 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1308 case BINEXPR_ASSIGN: {
1309 ir_node *right = expression_to_firm(expression->right);
1310 set_value_for_expression(expression->left, right);
1314 return create_add(expression);
1316 return create_sub(expression);
1318 return create_arithmetic_binop(expression, new_d_Mul);
1319 case BINEXPR_BITWISE_AND:
1320 return create_arithmetic_binop(expression, new_d_And);
1321 case BINEXPR_BITWISE_OR:
1322 return create_arithmetic_binop(expression, new_d_Or);
1323 case BINEXPR_BITWISE_XOR:
1324 return create_arithmetic_binop(expression, new_d_Eor);
1325 case BINEXPR_SHIFTLEFT:
1326 case BINEXPR_SHIFTRIGHT:
1327 return create_shift(expression);
1330 return create_divmod(expression);
1331 case BINEXPR_LOGICAL_AND:
1332 case BINEXPR_LOGICAL_OR:
1333 return create_lazy_op(expression);
1335 expression_to_firm(expression->left);
1336 return expression_to_firm(expression->right);
1337 case BINEXPR_ADD_ASSIGN:
1338 return create_arithmetic_assign_binop(expression, new_d_Add);
1339 case BINEXPR_SUB_ASSIGN:
1340 return create_arithmetic_assign_binop(expression, new_d_Sub);
1341 case BINEXPR_MUL_ASSIGN:
1342 return create_arithmetic_assign_binop(expression, new_d_Mul);
1343 case BINEXPR_DIV_ASSIGN:
1344 return create_arithmetic_assign_divmod(expression);
1345 case BINEXPR_BITWISE_AND_ASSIGN:
1346 return create_arithmetic_assign_binop(expression, new_d_And);
1347 case BINEXPR_BITWISE_OR_ASSIGN:
1348 return create_arithmetic_assign_binop(expression, new_d_Or);
1349 case BINEXPR_BITWISE_XOR_ASSIGN:
1350 return create_arithmetic_assign_binop(expression, new_d_Eor);
1351 case BINEXPR_SHIFTLEFT_ASSIGN:
1352 case BINEXPR_SHIFTRIGHT_ASSIGN:
1353 return create_arithmetic_assign_shift(expression);
1355 panic("TODO binexpr type");
1359 static ir_node *array_access_addr(const array_access_expression_t *expression)
1361 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1362 ir_node *base_addr = expression_to_firm(expression->array_ref);
1363 ir_node *offset = expression_to_firm(expression->index);
1364 offset = create_conv(dbgi, offset, mode_Iu);
1366 type_t *ref_type = skip_typeref(expression->array_ref->datatype);
1367 assert(is_type_pointer(ref_type));
1368 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1370 unsigned elem_size = get_type_size(pointer_type->points_to);
1371 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1372 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1374 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1379 static ir_node *array_access_to_firm(
1380 const array_access_expression_t *expression)
1382 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1383 ir_node *addr = array_access_addr(expression);
1384 type_t *type = revert_automatic_type_conversion(
1385 (const expression_t*) expression);
1386 type = skip_typeref(type);
1387 ir_type *irtype = get_ir_type(type);
1389 return deref_address(irtype, addr, dbgi);
1392 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1394 type_t *type = expression->type;
1396 type = expression->size_expression->datatype;
1397 assert(type != NULL);
1400 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1401 unsigned size = get_type_size(type);
1402 ir_node *size_node = new_Const_long(mode, size);
1407 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1409 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1411 ir_node *cur_block = get_cur_block();
1413 /* create the true block */
1414 ir_node *true_block = new_immBlock();
1416 ir_node *true_val = expression_to_firm(expression->true_expression);
1417 ir_node *true_jmp = new_Jmp();
1419 /* create the false block */
1420 ir_node *false_block = new_immBlock();
1422 ir_node *false_val = expression_to_firm(expression->false_expression);
1423 ir_node *false_jmp = new_Jmp();
1425 /* create the condition evaluation */
1426 set_cur_block(cur_block);
1427 create_condition_evaluation(expression->condition, true_block, false_block);
1428 mature_immBlock(true_block);
1429 mature_immBlock(false_block);
1431 /* create the common block */
1432 ir_node *common_block = new_immBlock();
1433 add_immBlock_pred(common_block, true_jmp);
1434 add_immBlock_pred(common_block, false_jmp);
1435 mature_immBlock(common_block);
1437 /* TODO improve static semantics, so either both or no values are NULL */
1438 if (true_val == NULL || false_val == NULL) return NULL;
1440 ir_node *in[2] = { true_val, false_val };
1441 ir_mode *mode = get_irn_mode(true_val);
1442 assert(get_irn_mode(false_val) == mode);
1443 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1448 static ir_node *select_addr(const select_expression_t *expression)
1450 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1452 ir_node *compound_addr = expression_to_firm(expression->compound);
1454 declaration_t *entry = expression->compound_entry;
1455 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1456 ir_entity *entity = entry->v.entity;
1458 assert(entity != NULL);
1460 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1465 static ir_node *select_to_firm(const select_expression_t *expression)
1467 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1468 ir_node *addr = select_addr(expression);
1469 type_t *type = revert_automatic_type_conversion(
1470 (const expression_t*) expression);
1471 type = skip_typeref(type);
1472 ir_type *irtype = get_ir_type(type);
1474 return deref_address(irtype, addr, dbgi);
1477 /* Values returned by __builtin_classify_type. */
1478 typedef enum gcc_type_class
1484 enumeral_type_class,
1487 reference_type_class,
1491 function_type_class,
1502 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1504 const type_t *const type = expr->type_expression->datatype;
1510 const atomic_type_t *const atomic_type = (const atomic_type_t*)type;
1511 switch (atomic_type->atype) {
1512 // should not be reached
1513 case ATOMIC_TYPE_INVALID:
1517 // gcc cannot do that
1518 case ATOMIC_TYPE_VOID:
1519 tc = void_type_class;
1522 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1523 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1524 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1525 case ATOMIC_TYPE_SHORT:
1526 case ATOMIC_TYPE_USHORT:
1527 case ATOMIC_TYPE_INT:
1528 case ATOMIC_TYPE_UINT:
1529 case ATOMIC_TYPE_LONG:
1530 case ATOMIC_TYPE_ULONG:
1531 case ATOMIC_TYPE_LONGLONG:
1532 case ATOMIC_TYPE_ULONGLONG:
1533 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1534 tc = integer_type_class;
1537 case ATOMIC_TYPE_FLOAT:
1538 case ATOMIC_TYPE_DOUBLE:
1539 case ATOMIC_TYPE_LONG_DOUBLE:
1540 tc = real_type_class;
1543 #ifdef PROVIDE_COMPLEX
1544 case ATOMIC_TYPE_FLOAT_COMPLEX:
1545 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1546 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1547 tc = complex_type_class;
1549 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1550 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1551 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1552 tc = complex_type_class;
1557 panic("Unimplemented case in classify_type_to_firm().");
1562 case TYPE_ARRAY: // gcc handles this as pointer
1563 case TYPE_FUNCTION: // gcc handles this as pointer
1564 case TYPE_POINTER: tc = pointer_type_class; break;
1565 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1566 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1568 // gcc handles this as integer
1569 case TYPE_ENUM: tc = integer_type_class; break;
1572 panic("Unimplemented case in classify_type_to_firm().");
1575 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1576 ir_mode *const mode = mode_Is;
1577 tarval *const tv = new_tarval_from_long(tc, mode);
1578 return new_d_Const(dbgi, mode, tv);
1581 static ir_node *function_name_to_firm(const string_literal_t *const expr)
1583 if (current_function_name == NULL) {
1584 const source_position_t *const src_pos =
1585 &expr->expression.source_position;
1586 const char *const name = current_function_decl->symbol->string;
1587 current_function_name = string_to_firm(src_pos, "__func__", name);
1590 return current_function_name;
1593 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1595 statement_t *statement = expr->statement;
1597 assert(statement->type == STATEMENT_COMPOUND);
1598 return compound_statement_to_firm((compound_statement_t*) statement);
1601 static ir_node *dereference_addr(const unary_expression_t *const expression)
1603 assert(expression->type == UNEXPR_DEREFERENCE);
1604 return expression_to_firm(expression->value);
1607 static ir_node *expression_to_addr(const expression_t *expression)
1609 switch(expression->type) {
1610 case EXPR_REFERENCE:
1611 return reference_addr((const reference_expression_t*) expression);
1612 case EXPR_ARRAY_ACCESS:
1613 return array_access_addr((const array_access_expression_t*) expression);
1615 return select_addr((const select_expression_t*) expression);
1617 const unary_expression_t *const unary_expr =
1618 (const unary_expression_t*)expression;
1619 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1620 return dereference_addr(unary_expr);
1627 panic("trying to get address of non-lvalue");
1630 static ir_node *_expression_to_firm(const expression_t *expression)
1632 switch(expression->type) {
1634 return const_to_firm((const const_t*) expression);
1635 case EXPR_STRING_LITERAL:
1636 return string_literal_to_firm((const string_literal_t*) expression);
1637 case EXPR_REFERENCE:
1638 return reference_expression_to_firm(
1639 (const reference_expression_t*) expression);
1641 return call_expression_to_firm((const call_expression_t*) expression);
1643 return unary_expression_to_firm((const unary_expression_t*) expression);
1645 return binary_expression_to_firm(
1646 (const binary_expression_t*) expression);
1647 case EXPR_ARRAY_ACCESS:
1648 return array_access_to_firm(
1649 (const array_access_expression_t*) expression);
1651 return sizeof_to_firm((const sizeof_expression_t*) expression);
1652 case EXPR_CONDITIONAL:
1653 return conditional_to_firm((const conditional_expression_t*)expression);
1655 return select_to_firm((const select_expression_t*) expression);
1656 case EXPR_CLASSIFY_TYPE:
1657 return classify_type_to_firm((const classify_type_expression_t*)expression);
1659 case EXPR_PRETTY_FUNCTION:
1660 return function_name_to_firm((const string_literal_t*)expression);
1661 case EXPR_STATEMENT:
1662 return statement_expression_to_firm(
1663 (const statement_expression_t*) expression);
1666 case EXPR_BUILTIN_SYMBOL:
1667 panic("unimplemented expression found");
1673 panic("invalid expression found");
1676 static ir_node *expression_to_firm(const expression_t *expression)
1678 ir_node *res = _expression_to_firm(expression);
1680 if(res != NULL && get_irn_mode(res) == mode_b) {
1681 ir_mode *mode = get_ir_mode(expression->datatype);
1682 res = create_conv(NULL, res, mode);
1688 static ir_node *expression_to_modeb(const expression_t *expression)
1690 ir_node *res = _expression_to_firm(expression);
1691 res = create_conv(NULL, res, mode_b);
1697 * create a short-circuit expression evaluation that tries to construct
1698 * efficient control flow structures for &&, || and ! expressions
1700 static void create_condition_evaluation(const expression_t *expression,
1701 ir_node *true_block,
1702 ir_node *false_block)
1704 switch(expression->type) {
1706 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1707 if(unary_expression->type == UNEXPR_NOT) {
1708 create_condition_evaluation(unary_expression->value, false_block,
1715 binary_expression_t *binary_expression
1716 = (binary_expression_t*) expression;
1717 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1718 ir_node *cur_block = get_cur_block();
1719 ir_node *extra_block = new_immBlock();
1720 set_cur_block(cur_block);
1721 create_condition_evaluation(binary_expression->left, extra_block,
1723 mature_immBlock(extra_block);
1724 set_cur_block(extra_block);
1725 create_condition_evaluation(binary_expression->right, true_block,
1729 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1730 ir_node *cur_block = get_cur_block();
1731 ir_node *extra_block = new_immBlock();
1732 set_cur_block(cur_block);
1733 create_condition_evaluation(binary_expression->left, true_block,
1735 mature_immBlock(extra_block);
1736 set_cur_block(extra_block);
1737 create_condition_evaluation(binary_expression->right, true_block,
1747 dbg_info *dbgi = get_dbg_info(&expression->source_position);
1748 ir_node *condition = expression_to_modeb(expression);
1749 ir_node *cond = new_d_Cond(dbgi, condition);
1750 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1751 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1753 add_immBlock_pred(true_block, true_proj);
1754 add_immBlock_pred(false_block, false_proj);
1756 set_cur_block(NULL);
1760 static void return_statement_to_firm(return_statement_t *statement)
1762 if(get_cur_block() == NULL)
1766 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1768 ir_node *value = NULL;
1769 if(statement->return_value != NULL) {
1770 value = expression_to_firm(statement->return_value);
1775 if(get_method_n_ress(func_irtype) > 0) {
1777 ir_type *res_type = get_method_res_type(func_irtype, 0);
1778 ir_mode *mode = get_type_mode(res_type);
1779 value = new_Unknown(mode);
1788 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1789 ir_node *store = get_store();
1790 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1792 ir_node *end_block = get_irg_end_block(current_ir_graph);
1793 add_immBlock_pred(end_block, ret);
1795 set_cur_block(NULL);
1798 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1800 if(get_cur_block() == NULL)
1803 return expression_to_firm(statement->expression);
1806 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1808 ir_node *result = NULL;
1809 statement_t *statement = compound->statements;
1810 for( ; statement != NULL; statement = statement->next) {
1811 //context2firm(&statement->context);
1813 if(statement->next == NULL && statement->type == STATEMENT_EXPRESSION) {
1814 result = expression_statement_to_firm(
1815 (expression_statement_t*) statement);
1818 statement_to_firm(statement);
1824 static void if_statement_to_firm(if_statement_t *statement)
1826 ir_node *cur_block = get_cur_block();
1828 ir_node *fallthrough_block = new_immBlock();
1830 /* the true (blocks) */
1831 ir_node *true_block;
1832 if (statement->true_statement != NULL) {
1833 true_block = new_immBlock();
1834 statement_to_firm(statement->true_statement);
1835 if(get_cur_block() != NULL) {
1836 ir_node *jmp = new_Jmp();
1837 add_immBlock_pred(fallthrough_block, jmp);
1840 true_block = fallthrough_block;
1843 /* the false (blocks) */
1844 ir_node *false_block;
1845 if(statement->false_statement != NULL) {
1846 false_block = new_immBlock();
1848 statement_to_firm(statement->false_statement);
1849 if(get_cur_block() != NULL) {
1850 ir_node *jmp = new_Jmp();
1851 add_immBlock_pred(fallthrough_block, jmp);
1854 false_block = fallthrough_block;
1857 /* create the condition */
1858 if(cur_block != NULL) {
1859 set_cur_block(cur_block);
1860 create_condition_evaluation(statement->condition, true_block,
1864 mature_immBlock(true_block);
1865 if(false_block != fallthrough_block) {
1866 mature_immBlock(false_block);
1868 mature_immBlock(fallthrough_block);
1870 set_cur_block(fallthrough_block);
1873 static void while_statement_to_firm(while_statement_t *statement)
1875 ir_node *jmp = NULL;
1876 if(get_cur_block() != NULL) {
1880 /* create the header block */
1881 ir_node *header_block = new_immBlock();
1883 add_immBlock_pred(header_block, jmp);
1886 /* the false block */
1887 ir_node *false_block = new_immBlock();
1890 ir_node *body_block;
1891 if (statement->body != NULL) {
1892 ir_node *old_continue_label = continue_label;
1893 ir_node *old_break_label = break_label;
1894 continue_label = header_block;
1895 break_label = false_block;
1897 body_block = new_immBlock();
1898 statement_to_firm(statement->body);
1900 assert(continue_label == header_block);
1901 assert(break_label == false_block);
1902 continue_label = old_continue_label;
1903 break_label = old_break_label;
1905 if(get_cur_block() != NULL) {
1906 ir_node *jmp = new_Jmp();
1907 add_immBlock_pred(header_block, jmp);
1910 body_block = header_block;
1913 /* create the condition */
1914 set_cur_block(header_block);
1916 create_condition_evaluation(statement->condition, body_block, false_block);
1917 mature_immBlock(body_block);
1918 mature_immBlock(false_block);
1919 mature_immBlock(header_block);
1921 set_cur_block(false_block);
1924 static void do_while_statement_to_firm(do_while_statement_t *statement)
1926 ir_node *jmp = NULL;
1927 if(get_cur_block() != NULL) {
1931 /* create the header block */
1932 ir_node *header_block = new_immBlock();
1934 /* the false block */
1935 ir_node *false_block = new_immBlock();
1938 ir_node *body_block = new_immBlock();
1940 add_immBlock_pred(body_block, jmp);
1943 if (statement->body != NULL) {
1944 ir_node *old_continue_label = continue_label;
1945 ir_node *old_break_label = break_label;
1946 continue_label = header_block;
1947 break_label = false_block;
1949 statement_to_firm(statement->body);
1951 assert(continue_label == header_block);
1952 assert(break_label == false_block);
1953 continue_label = old_continue_label;
1954 break_label = old_break_label;
1956 if (get_cur_block() == NULL) {
1957 mature_immBlock(header_block);
1958 mature_immBlock(body_block);
1959 mature_immBlock(false_block);
1964 ir_node *body_jmp = new_Jmp();
1965 add_immBlock_pred(header_block, body_jmp);
1966 mature_immBlock(header_block);
1968 /* create the condition */
1969 set_cur_block(header_block);
1971 create_condition_evaluation(statement->condition, body_block, false_block);
1972 mature_immBlock(body_block);
1973 mature_immBlock(false_block);
1974 mature_immBlock(header_block);
1976 set_cur_block(false_block);
1979 static void for_statement_to_firm(for_statement_t *statement)
1981 ir_node *jmp = NULL;
1982 if (get_cur_block() != NULL) {
1983 if(statement->initialisation != NULL) {
1984 expression_to_firm(statement->initialisation);
1989 /* create the step block */
1990 ir_node *const step_block = new_immBlock();
1991 if (statement->step != NULL) {
1992 expression_to_firm(statement->step);
1994 ir_node *const step_jmp = new_Jmp();
1996 /* create the header block */
1997 ir_node *const header_block = new_immBlock();
1999 add_immBlock_pred(header_block, jmp);
2001 add_immBlock_pred(header_block, step_jmp);
2003 /* the false block */
2004 ir_node *const false_block = new_immBlock();
2007 ir_node * body_block;
2008 if (statement->body != NULL) {
2009 ir_node *const old_continue_label = continue_label;
2010 ir_node *const old_break_label = break_label;
2011 continue_label = step_block;
2012 break_label = false_block;
2014 body_block = new_immBlock();
2015 statement_to_firm(statement->body);
2017 assert(continue_label == step_block);
2018 assert(break_label == false_block);
2019 continue_label = old_continue_label;
2020 break_label = old_break_label;
2022 if (get_cur_block() != NULL) {
2023 ir_node *const jmp = new_Jmp();
2024 add_immBlock_pred(step_block, jmp);
2027 body_block = step_block;
2030 /* create the condition */
2031 set_cur_block(header_block);
2032 if (statement->condition != NULL) {
2033 create_condition_evaluation(statement->condition, body_block,
2036 keep_alive(header_block);
2037 ir_node *jmp = new_Jmp();
2038 add_immBlock_pred(body_block, jmp);
2041 mature_immBlock(body_block);
2042 mature_immBlock(false_block);
2043 mature_immBlock(step_block);
2044 mature_immBlock(header_block);
2045 mature_immBlock(false_block);
2047 set_cur_block(false_block);
2050 static void create_declaration_entity(declaration_t *declaration,
2051 declaration_type_t declaration_type,
2052 ir_type *parent_type)
2054 ident *id = new_id_from_str(declaration->symbol->string);
2055 ir_type *irtype = get_ir_type(declaration->type);
2056 ir_entity *entity = new_entity(parent_type, id, irtype);
2057 set_entity_ld_ident(entity, id);
2059 declaration->declaration_type = declaration_type;
2060 declaration->v.entity = entity;
2061 set_entity_variability(entity, variability_uninitialized);
2062 /* TODO: visibility? */
2065 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2067 enum compound_graph_entry_type_t {
2068 COMPOUND_GRAPH_ENTRY_ARRAY,
2069 COMPOUND_GRAPH_ENTRY_COMPOUND
2072 struct compound_graph_path_entry_t {
2078 compound_graph_path_entry_t *prev;
2081 static void create_initializer_object(initializer_t *initializer, type_t *type,
2082 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2084 static compound_graph_path *create_compound_path(ir_type *type,
2085 compound_graph_path_entry_t *entry, int len)
2087 compound_graph_path *path = new_compound_graph_path(type, len);
2090 for( ; entry != NULL; entry = entry->prev, --i) {
2092 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2093 set_compound_graph_path_node(path, i, entry->v.entity);
2095 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2096 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2104 static void create_initializer_value(initializer_value_t *initializer,
2106 compound_graph_path_entry_t *entry,
2109 ir_node *node = expression_to_firm(initializer->value);
2110 ir_type *type = get_entity_type(entity);
2111 compound_graph_path *path = create_compound_path(type, entry, len);
2112 add_compound_ent_value_w_path(entity, node, path);
2115 static void create_initializer_compound(initializer_list_t *initializer,
2116 compound_type_t *type,
2118 compound_graph_path_entry_t *last_entry,
2121 declaration_t *compound_declaration = type->declaration;
2123 declaration_t *compound_entry = compound_declaration->context.declarations;
2125 compound_graph_path_entry_t entry;
2126 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2127 entry.prev = last_entry;
2131 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2132 if(compound_entry->symbol == NULL)
2134 if(compound_entry->namespc != NAMESPACE_NORMAL)
2137 if(i >= initializer->len)
2140 entry.v.entity = compound_entry->v.entity;
2142 initializer_t *sub_initializer = initializer->initializers[i];
2144 assert(compound_entry != NULL);
2145 assert(compound_entry->declaration_type
2146 == DECLARATION_TYPE_COMPOUND_MEMBER);
2148 if(sub_initializer->type == INITIALIZER_VALUE) {
2149 create_initializer_value(&sub_initializer->value,
2150 entity, &entry, len);
2152 type_t *type = skip_typeref(compound_entry->type);
2153 create_initializer_object(sub_initializer, type, entity, &entry,
2161 static void create_initializer_array(initializer_list_t *initializer,
2162 array_type_t *type, ir_entity *entity,
2163 compound_graph_path_entry_t *last_entry,
2166 type_t *element_type = type->element_type;
2167 element_type = skip_typeref(element_type);
2169 compound_graph_path_entry_t entry;
2170 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2171 entry.prev = last_entry;
2175 for(i = 0; i < initializer->len; ++i) {
2176 entry.v.array_index = i;
2178 initializer_t *sub_initializer = initializer->initializers[i];
2180 if(sub_initializer->type == INITIALIZER_VALUE) {
2181 create_initializer_value(&sub_initializer->value,
2182 entity, &entry, len);
2184 create_initializer_object(sub_initializer, element_type, entity,
2190 /* TODO: initializer rest... */
2191 if(type->size_expression != NULL) {
2192 size_t array_len = fold_constant(type->size_expression);
2193 for( ; i < array_len; ++i) {
2200 static void create_initializer_string(initializer_string_t *initializer,
2201 array_type_t *type, ir_entity *entity,
2202 compound_graph_path_entry_t *last_entry,
2205 type_t *element_type = type->element_type;
2206 element_type = skip_typeref(element_type);
2208 compound_graph_path_entry_t entry;
2209 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2210 entry.prev = last_entry;
2213 ir_type *irtype = get_entity_type(entity);
2214 size_t arr_len = get_array_type_size(type);
2215 const char *p = initializer->string;
2217 for(i = 0; i < arr_len; ++i, ++p) {
2218 entry.v.array_index = i;
2220 ir_node *node = new_Const_long(mode_Bs, *p);
2221 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2222 add_compound_ent_value_w_path(entity, node, path);
2229 static void create_initializer_object(initializer_t *initializer, type_t *type,
2230 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2232 if(type->type == TYPE_ARRAY) {
2233 array_type_t *array_type = (array_type_t*) type;
2235 if(initializer->type == INITIALIZER_STRING) {
2236 initializer_string_t *string = &initializer->string;
2237 create_initializer_string(string, array_type, entity, entry, len);
2239 assert(initializer->type == INITIALIZER_LIST);
2240 initializer_list_t *list = &initializer->list;
2241 create_initializer_array(list, array_type, entity, entry, len);
2244 assert(initializer->type == INITIALIZER_LIST);
2245 initializer_list_t *list = &initializer->list;
2247 assert(type->type == TYPE_COMPOUND_STRUCT
2248 || type->type == TYPE_COMPOUND_UNION);
2249 compound_type_t *compound_type = (compound_type_t*) type;
2250 create_initializer_compound(list, compound_type, entity, entry, len);
2254 static void create_initializer_local_variable_entity(declaration_t *declaration)
2256 initializer_t *initializer = declaration->init.initializer;
2257 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2258 ir_entity *entity = declaration->v.entity;
2259 ir_node *memory = get_store();
2260 ir_node *nomem = new_NoMem();
2261 ir_node *frame = get_irg_frame(current_ir_graph);
2262 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2264 if(is_atomic_entity(entity)) {
2265 assert(initializer->type == INITIALIZER_VALUE);
2266 initializer_value_t *initializer_value = &initializer->value;
2268 ir_node *value = expression_to_firm(initializer_value->value);
2269 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2270 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2271 set_store(store_mem);
2275 /* create a "template" entity which is copied to the entity on the stack */
2276 ident *id = unique_ident("initializer");
2277 ir_type *irtype = get_ir_type(declaration->type);
2278 ir_type *global_type = get_glob_type();
2279 ir_entity *init_entity = new_entity(global_type, id, irtype);
2280 set_entity_ld_ident(init_entity, id);
2282 set_entity_variability(init_entity, variability_initialized);
2283 set_entity_visibility(init_entity, visibility_local);
2285 ir_graph *old_current_ir_graph = current_ir_graph;
2286 current_ir_graph = get_const_code_irg();
2288 type_t *type = skip_typeref(declaration->type);
2289 create_initializer_object(initializer, type, init_entity, NULL, 0);
2291 assert(current_ir_graph == get_const_code_irg());
2292 current_ir_graph = old_current_ir_graph;
2294 ir_node *src_addr = create_symconst(dbgi, init_entity);
2295 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2297 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2298 set_store(copyb_mem);
2301 static void create_initializer(declaration_t *declaration)
2303 initializer_t *initializer = declaration->init.initializer;
2304 if(initializer == NULL)
2307 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2308 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2309 create_initializer_local_variable_entity(declaration);
2313 if(initializer->type == INITIALIZER_VALUE) {
2314 initializer_value_t *initializer_value = &initializer->value;
2316 ir_node *value = expression_to_firm(initializer_value->value);
2318 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2319 set_value(declaration->v.value_number, value);
2321 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2323 ir_entity *entity = declaration->v.entity;
2325 set_entity_variability(entity, variability_initialized);
2326 set_atomic_ent_value(entity, value);
2329 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2330 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2331 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2333 ir_entity *entity = declaration->v.entity;
2334 set_entity_variability(entity, variability_initialized);
2336 type_t *type = skip_typeref(declaration->type);
2337 create_initializer_object(initializer, type, entity, NULL, 0);
2341 static void create_local_variable(declaration_t *declaration)
2343 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2345 bool needs_entity = declaration->address_taken;
2346 type_t *type = skip_typeref(declaration->type);
2348 if(type->type == TYPE_ARRAY
2349 || type->type == TYPE_COMPOUND_STRUCT
2350 || type->type == TYPE_COMPOUND_UNION) {
2351 needs_entity = true;
2355 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2356 create_declaration_entity(declaration,
2357 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2360 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2361 declaration->v.value_number = next_value_number_function;
2362 ++next_value_number_function;
2365 create_initializer(declaration);
2368 static void create_local_static_variable(declaration_t *declaration)
2370 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2372 type_t *type = skip_typeref(declaration->type);
2373 ir_type *global_type = get_glob_type();
2374 ident *id = unique_ident(declaration->symbol->string);
2375 ir_type *irtype = get_ir_type(type);
2376 ir_entity *entity = new_entity(global_type, id, irtype);
2377 set_entity_ld_ident(entity, id);
2379 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2380 declaration->v.entity = entity;
2381 set_entity_variability(entity, variability_uninitialized);
2382 set_entity_visibility(entity, visibility_local);
2384 ir_graph *old_current_ir_graph = current_ir_graph;
2385 current_ir_graph = get_const_code_irg();
2387 create_initializer(declaration);
2389 assert(current_ir_graph == get_const_code_irg());
2390 current_ir_graph = old_current_ir_graph;
2393 static void declaration_statement_to_firm(declaration_statement_t *statement)
2395 declaration_t *declaration = statement->declarations_begin;
2396 declaration_t *end = statement->declarations_end->next;
2397 for( ; declaration != end; declaration = declaration->next) {
2398 type_t *type = declaration->type;
2400 switch(declaration->storage_class) {
2401 case STORAGE_CLASS_TYPEDEF:
2403 case STORAGE_CLASS_STATIC:
2404 create_local_static_variable(declaration);
2406 case STORAGE_CLASS_ENUM_ENTRY:
2407 panic("enum entry declaration in local block found");
2408 case STORAGE_CLASS_EXTERN:
2409 panic("extern declaration in local block found");
2410 case STORAGE_CLASS_NONE:
2411 case STORAGE_CLASS_AUTO:
2412 case STORAGE_CLASS_REGISTER:
2413 if(type->type == TYPE_FUNCTION) {
2414 panic("nested functions not supported yet");
2416 create_local_variable(declaration);
2420 panic("invalid storage class found");
2424 static void create_jump_statement(const statement_t *statement,
2425 ir_node *target_block)
2427 if(get_cur_block() == NULL)
2430 dbg_info *dbgi = get_dbg_info(&statement->source_position);
2431 ir_node *jump = new_d_Jmp(dbgi);
2432 add_immBlock_pred(target_block, jump);
2434 set_cur_block(NULL);
2437 static void switch_statement_to_firm(const switch_statement_t *statement)
2439 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2441 ir_node *expression = expression_to_firm(statement->expression);
2442 ir_node *cond = new_d_Cond(dbgi, expression);
2443 ir_node *break_block = new_immBlock();
2445 set_cur_block(NULL);
2447 ir_node *const old_switch_cond = current_switch_cond;
2448 ir_node *const old_break_label = break_label;
2449 const bool old_saw_default_label = saw_default_label;
2450 current_switch_cond = cond;
2451 break_label = break_block;
2453 statement_to_firm(statement->body);
2455 if(get_cur_block() != NULL) {
2456 ir_node *jmp = new_Jmp();
2457 add_immBlock_pred(break_block, jmp);
2460 if (!saw_default_label) {
2461 set_cur_block(get_nodes_block(cond));
2462 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2463 MAGIC_DEFAULT_PN_NUMBER);
2464 add_immBlock_pred(break_block, proj);
2467 assert(current_switch_cond == cond);
2468 assert(break_label == break_block);
2469 current_switch_cond = old_switch_cond;
2470 break_label = old_break_label;
2471 saw_default_label = old_saw_default_label;
2473 mature_immBlock(break_block);
2474 set_cur_block(break_block);
2477 static long fold_constant(const expression_t *expression)
2479 ir_graph *old_current_ir_graph = current_ir_graph;
2480 current_ir_graph = get_const_code_irg();
2482 ir_node *cnst = expression_to_firm(expression);
2483 if(!is_Const(cnst)) {
2484 panic("couldn't fold constantl");
2486 tarval *tv = get_Const_tarval(cnst);
2487 if(!tarval_is_long(tv)) {
2488 panic("folded constant not an integer");
2491 long res = get_tarval_long(tv);
2493 current_ir_graph = old_current_ir_graph;
2497 static void case_label_to_firm(const case_label_statement_t *statement)
2499 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2501 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2503 /* let's create a node and hope firm constant folding creates a Const
2506 set_cur_block(get_nodes_block(current_switch_cond));
2507 if(statement->expression) {
2508 long pn = fold_constant(statement->expression);
2509 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2510 /* oops someone detected our cheating... */
2511 panic("magic default pn used");
2513 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2515 saw_default_label = true;
2516 proj = new_d_defaultProj(dbgi, current_switch_cond,
2517 MAGIC_DEFAULT_PN_NUMBER);
2520 ir_node *block = new_immBlock();
2521 if (fallthrough != NULL) {
2522 add_immBlock_pred(block, fallthrough);
2524 add_immBlock_pred(block, proj);
2525 mature_immBlock(block);
2527 statement_to_firm(statement->label_statement);
2530 static ir_node *get_label_block(declaration_t *label)
2532 assert(label->namespc == NAMESPACE_LABEL);
2534 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2535 return label->v.block;
2537 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2539 ir_node *old_cur_block = get_cur_block();
2540 ir_node *block = new_immBlock();
2541 set_cur_block(old_cur_block);
2543 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2544 label->v.block = block;
2546 ARR_APP1(ir_node *, imature_blocks, block);
2551 static void label_to_firm(const label_statement_t *statement)
2553 ir_node *block = get_label_block(statement->label);
2555 if(get_cur_block() != NULL) {
2556 ir_node *jmp = new_Jmp();
2557 add_immBlock_pred(block, jmp);
2560 set_cur_block(block);
2563 statement_to_firm(statement->label_statement);
2566 static void goto_to_firm(const goto_statement_t *statement)
2568 if(get_cur_block() == NULL)
2571 ir_node *block = get_label_block(statement->label);
2572 ir_node *jmp = new_Jmp();
2573 add_immBlock_pred(block, jmp);
2575 set_cur_block(NULL);
2578 static void statement_to_firm(statement_t *statement)
2580 switch(statement->type) {
2581 case STATEMENT_COMPOUND:
2582 compound_statement_to_firm((compound_statement_t*) statement);
2584 case STATEMENT_RETURN:
2585 return_statement_to_firm((return_statement_t*) statement);
2587 case STATEMENT_EXPRESSION:
2588 expression_statement_to_firm((expression_statement_t*) statement);
2591 if_statement_to_firm((if_statement_t*) statement);
2593 case STATEMENT_WHILE:
2594 while_statement_to_firm((while_statement_t*) statement);
2596 case STATEMENT_DO_WHILE:
2597 do_while_statement_to_firm((do_while_statement_t*) statement);
2599 case STATEMENT_DECLARATION:
2600 declaration_statement_to_firm((declaration_statement_t*) statement);
2602 case STATEMENT_BREAK:
2603 create_jump_statement(statement, break_label);
2605 case STATEMENT_CONTINUE:
2606 create_jump_statement(statement, continue_label);
2608 case STATEMENT_SWITCH:
2609 switch_statement_to_firm((switch_statement_t*) statement);
2611 case STATEMENT_CASE_LABEL:
2612 case_label_to_firm((case_label_statement_t*) statement);
2615 for_statement_to_firm((for_statement_t*) statement);
2617 case STATEMENT_LABEL:
2618 label_to_firm((label_statement_t*) statement);
2620 case STATEMENT_GOTO:
2621 goto_to_firm((goto_statement_t*) statement);
2626 panic("Statement not implemented\n");
2629 static int count_local_declarations(const declaration_t * decl,
2630 const declaration_t *const end)
2633 for (; decl != end; decl = decl->next) {
2634 const type_t *type = skip_typeref(decl->type);
2635 switch (type->type) {
2639 if (!decl->address_taken) ++count;
2648 static int count_decls_in_stmts(const statement_t *stmt)
2651 for (; stmt != NULL; stmt = stmt->next) {
2652 switch (stmt->type) {
2653 case STATEMENT_DECLARATION: {
2654 const declaration_statement_t *const decl_stmt =
2655 (const declaration_statement_t*)stmt;
2656 count += count_local_declarations(decl_stmt->declarations_begin,
2657 decl_stmt->declarations_end->next);
2661 case STATEMENT_COMPOUND: {
2662 const compound_statement_t *const comp =
2663 (const compound_statement_t*)stmt;
2664 count += count_decls_in_stmts(comp->statements);
2668 case STATEMENT_IF: {
2669 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2670 count += count_decls_in_stmts(if_stmt->true_statement);
2671 count += count_decls_in_stmts(if_stmt->false_statement);
2675 case STATEMENT_SWITCH: {
2676 const switch_statement_t *const switch_stmt =
2677 (const switch_statement_t*)stmt;
2678 count += count_decls_in_stmts(switch_stmt->body);
2682 case STATEMENT_LABEL: {
2683 const label_statement_t *const label_stmt =
2684 (const label_statement_t*)stmt;
2685 count += count_decls_in_stmts(label_stmt->label_statement);
2689 case STATEMENT_WHILE: {
2690 const while_statement_t *const while_stmt =
2691 (const while_statement_t*)stmt;
2692 count += count_decls_in_stmts(while_stmt->body);
2696 case STATEMENT_DO_WHILE: {
2697 const do_while_statement_t *const do_while_stmt =
2698 (const do_while_statement_t*)stmt;
2699 count += count_decls_in_stmts(do_while_stmt->body);
2703 case STATEMENT_FOR: {
2704 const for_statement_t *const for_stmt =
2705 (const for_statement_t*)stmt;
2706 /* TODO initialisation */
2707 count += count_decls_in_stmts(for_stmt->body);
2711 case STATEMENT_BREAK:
2712 case STATEMENT_CASE_LABEL:
2713 case STATEMENT_CONTINUE:
2714 case STATEMENT_EXPRESSION:
2715 case STATEMENT_GOTO:
2716 case STATEMENT_INVALID:
2717 case STATEMENT_RETURN:
2724 static int get_function_n_local_vars(declaration_t *declaration)
2728 /* count parameters */
2729 count += count_local_declarations(declaration->context.declarations, NULL);
2731 /* count local variables declared in body */
2732 count += count_decls_in_stmts(declaration->init.statement);
2734 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2735 * for expression statements... */
2741 static void initialize_function_parameters(declaration_t *declaration)
2743 ir_graph *irg = current_ir_graph;
2744 ir_node *args = get_irg_args(irg);
2745 ir_node *start_block = get_irg_start_block(irg);
2746 ir_type *function_irtype = get_ir_type(declaration->type);
2749 declaration_t *parameter = declaration->context.declarations;
2750 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2751 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2752 type_t *type = skip_typeref(parameter->type);
2754 bool needs_entity = parameter->address_taken;
2755 if(type->type == TYPE_COMPOUND_STRUCT
2756 || type->type == TYPE_COMPOUND_UNION) {
2757 needs_entity = true;
2761 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2762 ident *id = new_id_from_str(parameter->symbol->string);
2763 set_entity_ident(entity, id);
2765 parameter->declaration_type
2766 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2767 parameter->v.entity = entity;
2771 ir_mode *mode = get_ir_mode(parameter->type);
2773 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2775 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2776 parameter->v.value_number = next_value_number_function;
2777 ++next_value_number_function;
2779 set_value(parameter->v.value_number, proj);
2783 static void create_function(declaration_t *declaration)
2785 ir_entity *entity = get_function_entity(declaration);
2787 if(declaration->init.statement == NULL)
2790 current_function_decl = declaration;
2791 current_function_name = NULL;
2793 assert(imature_blocks == NULL);
2794 imature_blocks = NEW_ARR_F(ir_node*, 0);
2796 int n_local_vars = get_function_n_local_vars(declaration);
2797 ir_graph *irg = new_ir_graph(entity, n_local_vars);
2798 ir_node *first_block = get_cur_block();
2800 next_value_number_function = 0;
2801 initialize_function_parameters(declaration);
2803 statement_to_firm(declaration->init.statement);
2805 ir_node *end_block = get_irg_end_block(irg);
2807 /* do we have a return statement yet? */
2808 if(get_cur_block() != NULL) {
2809 assert(declaration->type->type == TYPE_FUNCTION);
2810 const function_type_t* const func_type
2811 = (const function_type_t*) declaration->type;
2813 if (func_type->result_type == type_void) {
2814 ret = new_Return(get_store(), 0, NULL);
2816 ir_mode *const mode = get_ir_mode(func_type->result_type);
2818 // ยง5.1.2.2.3 main implicitly returns 0
2819 if (strcmp(declaration->symbol->string, "main") == 0) {
2820 in[0] = new_Const(mode, get_mode_null(mode));
2822 in[0] = new_Unknown(mode);
2824 ret = new_Return(get_store(), 1, in);
2826 add_immBlock_pred(end_block, ret);
2829 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2830 mature_immBlock(imature_blocks[i]);
2832 DEL_ARR_F(imature_blocks);
2833 imature_blocks = NULL;
2835 mature_immBlock(first_block);
2836 mature_immBlock(end_block);
2838 irg_finalize_cons(irg);
2840 /* finalize the frame type */
2841 ir_type *frame_type = get_irg_frame_type(irg);
2842 int n = get_compound_n_members(frame_type);
2845 for(int i = 0; i < n; ++i) {
2846 ir_entity *entity = get_compound_member(frame_type, i);
2847 ir_type *entity_type = get_entity_type(entity);
2849 int align = get_type_alignment_bytes(entity_type);
2850 if(align > align_all)
2854 misalign = offset % align;
2856 offset += align - misalign;
2860 set_entity_offset(entity, offset);
2861 offset += get_type_size_bytes(entity_type);
2863 set_type_size_bytes(frame_type, offset);
2864 set_type_alignment_bytes(frame_type, align_all);
2865 set_type_state(frame_type, layout_fixed);
2870 static void create_global_variable(declaration_t *declaration)
2872 ir_type *global_type = get_glob_type();
2873 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2876 ir_entity *entity = declaration->v.entity;
2877 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2878 set_entity_visibility(entity, visibility_local);
2879 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2880 set_entity_visibility(entity, visibility_external_allocated);
2882 set_entity_visibility(entity, visibility_external_visible);
2884 current_ir_graph = get_const_code_irg();
2885 create_initializer(declaration);
2888 static void context_to_firm(context_t *context)
2890 /* first pass: create declarations */
2891 declaration_t *declaration = context->declarations;
2892 for( ; declaration != NULL; declaration = declaration->next) {
2893 if(declaration->namespc != NAMESPACE_NORMAL)
2895 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2896 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2898 if(declaration->symbol == NULL)
2901 type_t *type = declaration->type;
2902 if(type->type == TYPE_FUNCTION) {
2903 get_function_entity(declaration);
2905 create_global_variable(declaration);
2909 /* second pass: create code */
2910 declaration = context->declarations;
2911 for( ; declaration != NULL; declaration = declaration->next) {
2912 if(declaration->namespc != NAMESPACE_NORMAL)
2914 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2915 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2917 if(declaration->symbol == NULL)
2920 type_t *type = declaration->type;
2921 if(type->type != TYPE_FUNCTION)
2924 create_function(declaration);
2928 void translation_unit_to_firm(translation_unit_t *unit)
2930 /* just to be sure */
2931 continue_label = NULL;
2933 current_switch_cond = NULL;
2935 context_to_firm(& unit->context);