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(&type->atomic);
234 return get_mode_size_bytes(mode_Is);
235 case TYPE_COMPOUND_UNION:
236 case TYPE_COMPOUND_STRUCT:
237 return get_compound_type_size(&type->compound);
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(&type->array);
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(&type->atomic);
510 firm_type = create_method_type(&type->function);
513 firm_type = create_pointer_type(&type->pointer);
516 firm_type = create_array_type(&type->array);
518 case TYPE_COMPOUND_STRUCT:
519 firm_type = create_struct_type(&type->compound);
521 case TYPE_COMPOUND_UNION:
522 firm_type = create_union_type(&type->compound);
525 firm_type = create_enum_type(&type->enumt);
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 assert(function->datatype->type == TYPE_POINTER);
804 pointer_type_t *const ptr_type = &function->datatype->pointer;
805 assert(ptr_type->points_to->type == TYPE_FUNCTION);
806 function_type = &ptr_type->points_to->function;
808 int n_parameters = 0;
809 call_argument_t *argument = call->arguments;
810 for( ; argument != NULL; argument = argument->next) {
814 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
815 ir_type *new_method_type = NULL;
816 if(function_type->variadic || function_type->unspecified_parameters) {
817 /* we need to construct a new method type matching the call
819 int n_res = get_method_n_ress(ir_method_type);
820 new_method_type = new_type_method(unique_ident("calltype"),
821 n_parameters, n_res);
822 set_method_calling_convention(new_method_type,
823 get_method_calling_convention(ir_method_type));
824 set_method_additional_properties(new_method_type,
825 get_method_additional_properties(ir_method_type));
827 for(int i = 0; i < n_res; ++i) {
828 set_method_res_type(new_method_type, i,
829 get_method_res_type(ir_method_type, i));
832 ir_node *in[n_parameters];
834 argument = call->arguments;
836 for( ; argument != NULL; argument = argument->next) {
837 expression_t *expression = argument->expression;
838 ir_node *arg_node = expression_to_firm(expression);
841 if(new_method_type != NULL) {
842 ir_type *irtype = get_ir_type(expression->datatype);
843 set_method_param_type(new_method_type, n, irtype);
848 assert(n == n_parameters);
850 if(new_method_type != NULL)
851 ir_method_type = new_method_type;
853 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
854 ir_node *store = get_store();
855 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
857 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
860 type_t *result_type = function_type->result_type;
861 ir_node *result = NULL;
862 if(result_type != type_void) {
863 ir_mode *mode = get_ir_mode(result_type);
864 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
865 result = new_d_Proj(dbgi, resproj, mode, 0);
871 static void statement_to_firm(statement_t *statement);
872 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
874 static ir_node *expression_to_addr(const expression_t *expression);
875 static void create_condition_evaluation(const expression_t *expression,
877 ir_node *false_block);
879 static void set_value_for_expression(const expression_t *expression,
882 if(expression->type == EXPR_REFERENCE) {
883 reference_expression_t *ref = (reference_expression_t*) expression;
885 declaration_t *declaration = ref->declaration;
886 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
887 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
888 set_value(declaration->v.value_number, value);
893 dbg_info *dbgi = get_dbg_info(&expression->source_position);
894 ir_node *addr = expression_to_addr(expression);
895 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
896 ir_node *memory = get_store();
897 ir_node *store = new_d_Store(dbgi, memory, addr, value);
898 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
899 set_store(store_mem);
902 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
904 ir_mode *value_mode = get_irn_mode(value);
906 if (value_mode == dest_mode || is_Bad(value))
909 if(dest_mode == mode_b) {
910 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
911 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
912 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
916 return new_d_Conv(dbgi, value, dest_mode);
919 static ir_node *create_incdec(const unary_expression_t *expression)
921 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
922 type_t *type = expression->expression.datatype;
923 ir_mode *mode = get_ir_mode(type);
924 expression_t *value = expression->value;
926 ir_node *value_node = expression_to_firm(value);
929 if(type->type == TYPE_POINTER) {
930 pointer_type_t *pointer_type = &type->pointer;
931 unsigned elem_size = get_type_size(pointer_type->points_to);
932 offset = new_Const_long(mode_Is, elem_size);
934 assert(is_type_arithmetic(type));
935 offset = new_Const(mode, get_mode_one(mode));
938 switch(expression->type) {
939 case UNEXPR_POSTFIX_INCREMENT: {
940 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
941 set_value_for_expression(value, new_value);
944 case UNEXPR_POSTFIX_DECREMENT: {
945 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
946 set_value_for_expression(value, new_value);
949 case UNEXPR_PREFIX_INCREMENT: {
950 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
951 set_value_for_expression(value, new_value);
954 case UNEXPR_PREFIX_DECREMENT: {
955 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
956 set_value_for_expression(value, new_value);
960 panic("no incdec expr in create_incdec");
965 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
967 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
968 type_t *type = skip_typeref(expression->expression.datatype);
970 if(expression->type == UNEXPR_TAKE_ADDRESS)
971 return expression_to_addr(expression->value);
973 const expression_t *value = expression->value;
974 ir_node *value_node = expression_to_firm(value);
976 switch(expression->type) {
978 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
981 case UNEXPR_BITWISE_NEGATE:
982 return new_d_Not(dbgi, value_node, get_ir_mode(type));
984 if(get_irn_mode(value_node) != mode_b) {
985 value_node = create_conv(dbgi, value_node, mode_b);
987 value_node = new_d_Not(dbgi, value_node, mode_b);
988 ir_mode *const mode = get_ir_mode(type);
990 value_node = create_conv(dbgi, value_node, mode);
994 case UNEXPR_DEREFERENCE: {
995 ir_type *irtype = get_ir_type(type);
996 return deref_address(irtype, value_node, dbgi);
998 case UNEXPR_POSTFIX_INCREMENT:
999 case UNEXPR_POSTFIX_DECREMENT:
1000 case UNEXPR_PREFIX_INCREMENT:
1001 case UNEXPR_PREFIX_DECREMENT:
1002 return create_incdec(expression);
1004 return create_conv(dbgi, value_node, get_ir_mode(type));
1006 case UNEXPR_TAKE_ADDRESS:
1007 case UNEXPR_INVALID:
1010 panic("invalid UNEXPR type found");
1013 static long get_pnc(binary_expression_type_t type)
1016 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1017 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1018 case BINEXPR_LESS: return pn_Cmp_Lt;
1019 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1020 case BINEXPR_GREATER: return pn_Cmp_Gt;
1021 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1025 panic("trying to get pn_Cmp from non-comparison binexpr type");
1028 static ir_node *create_lazy_op(const binary_expression_t *expression)
1030 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1031 type_t *type = expression->expression.datatype;
1032 ir_mode *mode = get_ir_mode(type);
1034 ir_node *cur_block = get_cur_block();
1036 ir_node *one_block = new_immBlock();
1037 ir_node *one = new_Const(mode, get_mode_one(mode));
1038 ir_node *jmp_one = new_d_Jmp(dbgi);
1040 ir_node *zero_block = new_immBlock();
1041 ir_node *zero = new_Const(mode, get_mode_null(mode));
1042 ir_node *jmp_zero = new_d_Jmp(dbgi);
1044 set_cur_block(cur_block);
1045 create_condition_evaluation((const expression_t*) expression,
1046 one_block, zero_block);
1047 mature_immBlock(one_block);
1048 mature_immBlock(zero_block);
1050 ir_node *common_block = new_immBlock();
1051 add_immBlock_pred(common_block, jmp_one);
1052 add_immBlock_pred(common_block, jmp_zero);
1053 mature_immBlock(common_block);
1055 ir_node *in[2] = { one, zero };
1056 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1061 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1062 ir_node *right, ir_mode *mode);
1064 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1065 create_arithmetic_func func)
1067 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1068 ir_node *left = expression_to_firm(expression->left);
1069 ir_node *right = expression_to_firm(expression->right);
1070 type_t *type = expression->right->datatype;
1071 /* be careful with the modes, because in arithmetic assign nodes only
1072 * the right operand has the mode of the arithmetic already */
1073 ir_mode *mode = get_ir_mode(type);
1074 left = create_conv(dbgi, left, mode);
1075 ir_node *res = func(dbgi, left, right, mode);
1080 static ir_node *pointer_arithmetic(ir_node *const pointer,
1083 dbg_info *const dbgi,
1084 const create_arithmetic_func func)
1086 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1087 type_t *const points_to = pointer_type->points_to;
1088 const unsigned elem_size = get_type_size(points_to);
1090 assert(elem_size >= 1);
1091 if (elem_size > 1) {
1092 integer = create_conv(dbgi, integer, mode_Is);
1093 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1094 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1098 ir_mode *const mode = get_ir_mode(type);
1099 return func(dbgi, pointer, integer, mode);
1102 static ir_node *create_arithmetic_assign_binop(
1103 const binary_expression_t *expression, create_arithmetic_func func)
1105 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1106 type_t *const type = expression->expression.datatype;
1109 if (type->type == TYPE_POINTER) {
1110 ir_node *const pointer = expression_to_firm(expression->left);
1111 ir_node * integer = expression_to_firm(expression->right);
1112 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1114 value = create_arithmetic_binop(expression, func);
1117 ir_mode *const mode = get_ir_mode(type);
1118 value = create_conv(dbgi, value, mode);
1119 set_value_for_expression(expression->left, value);
1124 static ir_node *create_add(const binary_expression_t *expression)
1126 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1127 ir_node *left = expression_to_firm(expression->left);
1128 ir_node *right = expression_to_firm(expression->right);
1129 type_t *type = expression->expression.datatype;
1131 expression_t *expr_left = expression->left;
1132 expression_t *expr_right = expression->right;
1133 type_t *type_left = skip_typeref(expr_left->datatype);
1134 type_t *type_right = skip_typeref(expr_right->datatype);
1136 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1137 ir_mode *const mode = get_ir_mode(type);
1138 return new_d_Add(dbgi, left, right, mode);
1141 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1142 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1144 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1145 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1149 static ir_node *create_sub(const binary_expression_t *expression)
1151 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1152 expression_t *const expr_left = expression->left;
1153 expression_t *const expr_right = expression->right;
1154 ir_node *const left = expression_to_firm(expr_left);
1155 ir_node *const right = expression_to_firm(expr_right);
1156 type_t *const type = expression->expression.datatype;
1157 type_t *const type_left = skip_typeref(expr_left->datatype);
1158 type_t *const type_right = skip_typeref(expr_right->datatype);
1160 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1161 ir_mode *const mode = get_ir_mode(type);
1162 return new_d_Sub(dbgi, left, right, mode);
1163 } else if (type_left->type == TYPE_POINTER
1164 && type_right->type == TYPE_POINTER) {
1165 const pointer_type_t *const ptr_type = &type_left->pointer;
1166 const unsigned elem_size = get_type_size(ptr_type->points_to);
1167 ir_mode *const mode = get_ir_mode(type);
1168 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1169 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1170 ir_node *const no_mem = new_NoMem();
1171 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1172 op_pin_state_floats);
1173 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1176 assert(type_left->type == TYPE_POINTER);
1177 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1180 static ir_node *create_shift(const binary_expression_t *expression)
1182 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1183 ir_node *left = expression_to_firm(expression->left);
1184 ir_node *right = expression_to_firm(expression->right);
1185 type_t *type = expression->expression.datatype;
1186 ir_mode *mode = get_ir_mode(type);
1188 /* firm always wants the shift count to be unsigned */
1189 right = create_conv(dbgi, right, mode_Iu);
1193 switch(expression->type) {
1194 case BINEXPR_SHIFTLEFT_ASSIGN:
1195 case BINEXPR_SHIFTLEFT:
1196 res = new_d_Shl(dbgi, left, right, mode);
1198 case BINEXPR_SHIFTRIGHT_ASSIGN:
1199 case BINEXPR_SHIFTRIGHT: {
1200 expression_t *expr_left = expression->left;
1201 type_t *type_left = skip_typeref(expr_left->datatype);
1203 if(is_type_signed(type_left)) {
1204 res = new_d_Shrs(dbgi, left, right, mode);
1206 res = new_d_Shr(dbgi, left, right, mode);
1211 panic("create shift op called for non-shift op");
1218 static ir_node *create_divmod(const binary_expression_t *expression)
1220 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1221 ir_node *left = expression_to_firm(expression->left);
1222 ir_node *right = expression_to_firm(expression->right);
1223 ir_node *pin = new_Pin(new_NoMem());
1224 /* be careful with the modes, because in arithmetic assign nodes only
1225 * the right operand has the mode of the arithmetic already */
1226 type_t *type = expression->right->datatype;
1227 ir_mode *mode = get_ir_mode(type);
1228 left = create_conv(dbgi, left, mode);
1232 switch (expression->type) {
1234 case BINEXPR_DIV_ASSIGN:
1235 if(mode_is_float(mode)) {
1236 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1237 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1239 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1240 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1245 case BINEXPR_MOD_ASSIGN:
1246 assert(!mode_is_float(mode));
1247 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1248 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1251 default: panic("unexpected binary expression type in create_divmod()");
1257 static ir_node *create_arithmetic_assign_divmod(
1258 const binary_expression_t *expression)
1260 ir_node * value = create_divmod(expression);
1261 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1262 type_t *const type = expression->expression.datatype;
1263 ir_mode *const mode = get_ir_mode(type);
1265 assert(type->type != TYPE_POINTER);
1267 value = create_conv(dbgi, value, mode);
1268 set_value_for_expression(expression->left, value);
1273 static ir_node *create_arithmetic_assign_shift(
1274 const binary_expression_t *expression)
1276 ir_node * value = create_shift(expression);
1277 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1278 type_t *const type = expression->expression.datatype;
1279 ir_mode *const mode = get_ir_mode(type);
1281 value = create_conv(dbgi, value, mode);
1282 set_value_for_expression(expression->left, value);
1287 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1289 binary_expression_type_t type = expression->type;
1292 case BINEXPR_NOTEQUAL:
1294 case BINEXPR_LESSEQUAL:
1295 case BINEXPR_GREATER:
1296 case BINEXPR_GREATEREQUAL: {
1297 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1298 ir_node *left = expression_to_firm(expression->left);
1299 ir_node *right = expression_to_firm(expression->right);
1300 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1301 long pnc = get_pnc(type);
1302 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1305 case BINEXPR_ASSIGN: {
1306 ir_node *right = expression_to_firm(expression->right);
1307 set_value_for_expression(expression->left, right);
1311 return create_add(expression);
1313 return create_sub(expression);
1315 return create_arithmetic_binop(expression, new_d_Mul);
1316 case BINEXPR_BITWISE_AND:
1317 return create_arithmetic_binop(expression, new_d_And);
1318 case BINEXPR_BITWISE_OR:
1319 return create_arithmetic_binop(expression, new_d_Or);
1320 case BINEXPR_BITWISE_XOR:
1321 return create_arithmetic_binop(expression, new_d_Eor);
1322 case BINEXPR_SHIFTLEFT:
1323 case BINEXPR_SHIFTRIGHT:
1324 return create_shift(expression);
1327 return create_divmod(expression);
1328 case BINEXPR_LOGICAL_AND:
1329 case BINEXPR_LOGICAL_OR:
1330 return create_lazy_op(expression);
1332 expression_to_firm(expression->left);
1333 return expression_to_firm(expression->right);
1334 case BINEXPR_ADD_ASSIGN:
1335 return create_arithmetic_assign_binop(expression, new_d_Add);
1336 case BINEXPR_SUB_ASSIGN:
1337 return create_arithmetic_assign_binop(expression, new_d_Sub);
1338 case BINEXPR_MUL_ASSIGN:
1339 return create_arithmetic_assign_binop(expression, new_d_Mul);
1340 case BINEXPR_DIV_ASSIGN:
1341 return create_arithmetic_assign_divmod(expression);
1342 case BINEXPR_BITWISE_AND_ASSIGN:
1343 return create_arithmetic_assign_binop(expression, new_d_And);
1344 case BINEXPR_BITWISE_OR_ASSIGN:
1345 return create_arithmetic_assign_binop(expression, new_d_Or);
1346 case BINEXPR_BITWISE_XOR_ASSIGN:
1347 return create_arithmetic_assign_binop(expression, new_d_Eor);
1348 case BINEXPR_SHIFTLEFT_ASSIGN:
1349 case BINEXPR_SHIFTRIGHT_ASSIGN:
1350 return create_arithmetic_assign_shift(expression);
1352 panic("TODO binexpr type");
1356 static ir_node *array_access_addr(const array_access_expression_t *expression)
1358 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1359 ir_node *base_addr = expression_to_firm(expression->array_ref);
1360 ir_node *offset = expression_to_firm(expression->index);
1361 offset = create_conv(dbgi, offset, mode_Iu);
1363 type_t *ref_type = skip_typeref(expression->array_ref->datatype);
1364 assert(is_type_pointer(ref_type));
1365 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1367 unsigned elem_size = get_type_size(pointer_type->points_to);
1368 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1369 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1371 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1376 static ir_node *array_access_to_firm(
1377 const array_access_expression_t *expression)
1379 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1380 ir_node *addr = array_access_addr(expression);
1381 type_t *type = revert_automatic_type_conversion(
1382 (const expression_t*) expression);
1383 type = skip_typeref(type);
1384 ir_type *irtype = get_ir_type(type);
1386 return deref_address(irtype, addr, dbgi);
1389 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1391 type_t *type = expression->type;
1393 type = expression->size_expression->datatype;
1394 assert(type != NULL);
1397 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1398 unsigned size = get_type_size(type);
1399 ir_node *size_node = new_Const_long(mode, size);
1404 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1406 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1408 ir_node *cur_block = get_cur_block();
1410 /* create the true block */
1411 ir_node *true_block = new_immBlock();
1413 ir_node *true_val = expression_to_firm(expression->true_expression);
1414 ir_node *true_jmp = new_Jmp();
1416 /* create the false block */
1417 ir_node *false_block = new_immBlock();
1419 ir_node *false_val = expression_to_firm(expression->false_expression);
1420 ir_node *false_jmp = new_Jmp();
1422 /* create the condition evaluation */
1423 set_cur_block(cur_block);
1424 create_condition_evaluation(expression->condition, true_block, false_block);
1425 mature_immBlock(true_block);
1426 mature_immBlock(false_block);
1428 /* create the common block */
1429 ir_node *common_block = new_immBlock();
1430 add_immBlock_pred(common_block, true_jmp);
1431 add_immBlock_pred(common_block, false_jmp);
1432 mature_immBlock(common_block);
1434 /* TODO improve static semantics, so either both or no values are NULL */
1435 if (true_val == NULL || false_val == NULL) return NULL;
1437 ir_node *in[2] = { true_val, false_val };
1438 ir_mode *mode = get_irn_mode(true_val);
1439 assert(get_irn_mode(false_val) == mode);
1440 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1445 static ir_node *select_addr(const select_expression_t *expression)
1447 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1449 ir_node *compound_addr = expression_to_firm(expression->compound);
1451 declaration_t *entry = expression->compound_entry;
1452 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1453 ir_entity *entity = entry->v.entity;
1455 assert(entity != NULL);
1457 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1462 static ir_node *select_to_firm(const select_expression_t *expression)
1464 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1465 ir_node *addr = select_addr(expression);
1466 type_t *type = revert_automatic_type_conversion(
1467 (const expression_t*) expression);
1468 type = skip_typeref(type);
1469 ir_type *irtype = get_ir_type(type);
1471 return deref_address(irtype, addr, dbgi);
1474 /* Values returned by __builtin_classify_type. */
1475 typedef enum gcc_type_class
1481 enumeral_type_class,
1484 reference_type_class,
1488 function_type_class,
1499 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1501 const type_t *const type = expr->type_expression->datatype;
1507 const atomic_type_t *const atomic_type = &type->atomic;
1508 switch (atomic_type->atype) {
1509 // should not be reached
1510 case ATOMIC_TYPE_INVALID:
1514 // gcc cannot do that
1515 case ATOMIC_TYPE_VOID:
1516 tc = void_type_class;
1519 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1520 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1521 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1522 case ATOMIC_TYPE_SHORT:
1523 case ATOMIC_TYPE_USHORT:
1524 case ATOMIC_TYPE_INT:
1525 case ATOMIC_TYPE_UINT:
1526 case ATOMIC_TYPE_LONG:
1527 case ATOMIC_TYPE_ULONG:
1528 case ATOMIC_TYPE_LONGLONG:
1529 case ATOMIC_TYPE_ULONGLONG:
1530 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1531 tc = integer_type_class;
1534 case ATOMIC_TYPE_FLOAT:
1535 case ATOMIC_TYPE_DOUBLE:
1536 case ATOMIC_TYPE_LONG_DOUBLE:
1537 tc = real_type_class;
1540 #ifdef PROVIDE_COMPLEX
1541 case ATOMIC_TYPE_FLOAT_COMPLEX:
1542 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1543 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1544 tc = complex_type_class;
1546 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1547 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1548 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1549 tc = complex_type_class;
1554 panic("Unimplemented case in classify_type_to_firm().");
1559 case TYPE_ARRAY: // gcc handles this as pointer
1560 case TYPE_FUNCTION: // gcc handles this as pointer
1561 case TYPE_POINTER: tc = pointer_type_class; break;
1562 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1563 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1565 // gcc handles this as integer
1566 case TYPE_ENUM: tc = integer_type_class; break;
1569 panic("Unimplemented case in classify_type_to_firm().");
1572 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1573 ir_mode *const mode = mode_Is;
1574 tarval *const tv = new_tarval_from_long(tc, mode);
1575 return new_d_Const(dbgi, mode, tv);
1578 static ir_node *function_name_to_firm(const string_literal_t *const expr)
1580 if (current_function_name == NULL) {
1581 const source_position_t *const src_pos =
1582 &expr->expression.source_position;
1583 const char *const name = current_function_decl->symbol->string;
1584 current_function_name = string_to_firm(src_pos, "__func__", name);
1587 return current_function_name;
1590 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1592 statement_t *statement = expr->statement;
1594 assert(statement->type == STATEMENT_COMPOUND);
1595 return compound_statement_to_firm((compound_statement_t*) statement);
1598 static ir_node *dereference_addr(const unary_expression_t *const expression)
1600 assert(expression->type == UNEXPR_DEREFERENCE);
1601 return expression_to_firm(expression->value);
1604 static ir_node *expression_to_addr(const expression_t *expression)
1606 switch(expression->type) {
1607 case EXPR_REFERENCE:
1608 return reference_addr((const reference_expression_t*) expression);
1609 case EXPR_ARRAY_ACCESS:
1610 return array_access_addr((const array_access_expression_t*) expression);
1612 return select_addr((const select_expression_t*) expression);
1614 const unary_expression_t *const unary_expr =
1615 (const unary_expression_t*)expression;
1616 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1617 return dereference_addr(unary_expr);
1624 panic("trying to get address of non-lvalue");
1627 static ir_node *_expression_to_firm(const expression_t *expression)
1629 switch(expression->type) {
1631 return const_to_firm((const const_t*) expression);
1632 case EXPR_STRING_LITERAL:
1633 return string_literal_to_firm((const string_literal_t*) expression);
1634 case EXPR_REFERENCE:
1635 return reference_expression_to_firm(
1636 (const reference_expression_t*) expression);
1638 return call_expression_to_firm((const call_expression_t*) expression);
1640 return unary_expression_to_firm((const unary_expression_t*) expression);
1642 return binary_expression_to_firm(
1643 (const binary_expression_t*) expression);
1644 case EXPR_ARRAY_ACCESS:
1645 return array_access_to_firm(
1646 (const array_access_expression_t*) expression);
1648 return sizeof_to_firm((const sizeof_expression_t*) expression);
1649 case EXPR_CONDITIONAL:
1650 return conditional_to_firm((const conditional_expression_t*)expression);
1652 return select_to_firm((const select_expression_t*) expression);
1653 case EXPR_CLASSIFY_TYPE:
1654 return classify_type_to_firm((const classify_type_expression_t*)expression);
1656 case EXPR_PRETTY_FUNCTION:
1657 return function_name_to_firm((const string_literal_t*)expression);
1658 case EXPR_STATEMENT:
1659 return statement_expression_to_firm(
1660 (const statement_expression_t*) expression);
1663 case EXPR_BUILTIN_SYMBOL:
1664 panic("unimplemented expression found");
1670 panic("invalid expression found");
1673 static ir_node *expression_to_firm(const expression_t *expression)
1675 ir_node *res = _expression_to_firm(expression);
1677 if(res != NULL && get_irn_mode(res) == mode_b) {
1678 ir_mode *mode = get_ir_mode(expression->datatype);
1679 res = create_conv(NULL, res, mode);
1685 static ir_node *expression_to_modeb(const expression_t *expression)
1687 ir_node *res = _expression_to_firm(expression);
1688 res = create_conv(NULL, res, mode_b);
1694 * create a short-circuit expression evaluation that tries to construct
1695 * efficient control flow structures for &&, || and ! expressions
1697 static void create_condition_evaluation(const expression_t *expression,
1698 ir_node *true_block,
1699 ir_node *false_block)
1701 switch(expression->type) {
1703 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1704 if(unary_expression->type == UNEXPR_NOT) {
1705 create_condition_evaluation(unary_expression->value, false_block,
1712 binary_expression_t *binary_expression
1713 = (binary_expression_t*) expression;
1714 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1715 ir_node *cur_block = get_cur_block();
1716 ir_node *extra_block = new_immBlock();
1717 set_cur_block(cur_block);
1718 create_condition_evaluation(binary_expression->left, extra_block,
1720 mature_immBlock(extra_block);
1721 set_cur_block(extra_block);
1722 create_condition_evaluation(binary_expression->right, true_block,
1726 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1727 ir_node *cur_block = get_cur_block();
1728 ir_node *extra_block = new_immBlock();
1729 set_cur_block(cur_block);
1730 create_condition_evaluation(binary_expression->left, true_block,
1732 mature_immBlock(extra_block);
1733 set_cur_block(extra_block);
1734 create_condition_evaluation(binary_expression->right, true_block,
1744 dbg_info *dbgi = get_dbg_info(&expression->source_position);
1745 ir_node *condition = expression_to_modeb(expression);
1746 ir_node *cond = new_d_Cond(dbgi, condition);
1747 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1748 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1750 add_immBlock_pred(true_block, true_proj);
1751 add_immBlock_pred(false_block, false_proj);
1753 set_cur_block(NULL);
1757 static void return_statement_to_firm(return_statement_t *statement)
1759 if(get_cur_block() == NULL)
1763 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1765 ir_node *value = NULL;
1766 if(statement->return_value != NULL) {
1767 value = expression_to_firm(statement->return_value);
1772 if(get_method_n_ress(func_irtype) > 0) {
1774 ir_type *res_type = get_method_res_type(func_irtype, 0);
1775 ir_mode *mode = get_type_mode(res_type);
1776 value = new_Unknown(mode);
1785 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1786 ir_node *store = get_store();
1787 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1789 ir_node *end_block = get_irg_end_block(current_ir_graph);
1790 add_immBlock_pred(end_block, ret);
1792 set_cur_block(NULL);
1795 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1797 if(get_cur_block() == NULL)
1800 return expression_to_firm(statement->expression);
1803 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1805 ir_node *result = NULL;
1806 statement_t *statement = compound->statements;
1807 for( ; statement != NULL; statement = statement->next) {
1808 //context2firm(&statement->context);
1810 if(statement->next == NULL && statement->type == STATEMENT_EXPRESSION) {
1811 result = expression_statement_to_firm(
1812 (expression_statement_t*) statement);
1815 statement_to_firm(statement);
1821 static void if_statement_to_firm(if_statement_t *statement)
1823 ir_node *cur_block = get_cur_block();
1825 ir_node *fallthrough_block = new_immBlock();
1827 /* the true (blocks) */
1828 ir_node *true_block;
1829 if (statement->true_statement != NULL) {
1830 true_block = new_immBlock();
1831 statement_to_firm(statement->true_statement);
1832 if(get_cur_block() != NULL) {
1833 ir_node *jmp = new_Jmp();
1834 add_immBlock_pred(fallthrough_block, jmp);
1837 true_block = fallthrough_block;
1840 /* the false (blocks) */
1841 ir_node *false_block;
1842 if(statement->false_statement != NULL) {
1843 false_block = new_immBlock();
1845 statement_to_firm(statement->false_statement);
1846 if(get_cur_block() != NULL) {
1847 ir_node *jmp = new_Jmp();
1848 add_immBlock_pred(fallthrough_block, jmp);
1851 false_block = fallthrough_block;
1854 /* create the condition */
1855 if(cur_block != NULL) {
1856 set_cur_block(cur_block);
1857 create_condition_evaluation(statement->condition, true_block,
1861 mature_immBlock(true_block);
1862 if(false_block != fallthrough_block) {
1863 mature_immBlock(false_block);
1865 mature_immBlock(fallthrough_block);
1867 set_cur_block(fallthrough_block);
1870 static void while_statement_to_firm(while_statement_t *statement)
1872 ir_node *jmp = NULL;
1873 if(get_cur_block() != NULL) {
1877 /* create the header block */
1878 ir_node *header_block = new_immBlock();
1880 add_immBlock_pred(header_block, jmp);
1883 /* the false block */
1884 ir_node *false_block = new_immBlock();
1887 ir_node *body_block;
1888 if (statement->body != NULL) {
1889 ir_node *old_continue_label = continue_label;
1890 ir_node *old_break_label = break_label;
1891 continue_label = header_block;
1892 break_label = false_block;
1894 body_block = new_immBlock();
1895 statement_to_firm(statement->body);
1897 assert(continue_label == header_block);
1898 assert(break_label == false_block);
1899 continue_label = old_continue_label;
1900 break_label = old_break_label;
1902 if(get_cur_block() != NULL) {
1903 ir_node *jmp = new_Jmp();
1904 add_immBlock_pred(header_block, jmp);
1907 body_block = header_block;
1910 /* create the condition */
1911 set_cur_block(header_block);
1913 create_condition_evaluation(statement->condition, body_block, false_block);
1914 mature_immBlock(body_block);
1915 mature_immBlock(false_block);
1916 mature_immBlock(header_block);
1918 set_cur_block(false_block);
1921 static void do_while_statement_to_firm(do_while_statement_t *statement)
1923 ir_node *jmp = NULL;
1924 if(get_cur_block() != NULL) {
1928 /* create the header block */
1929 ir_node *header_block = new_immBlock();
1931 /* the false block */
1932 ir_node *false_block = new_immBlock();
1935 ir_node *body_block = new_immBlock();
1937 add_immBlock_pred(body_block, jmp);
1940 if (statement->body != NULL) {
1941 ir_node *old_continue_label = continue_label;
1942 ir_node *old_break_label = break_label;
1943 continue_label = header_block;
1944 break_label = false_block;
1946 statement_to_firm(statement->body);
1948 assert(continue_label == header_block);
1949 assert(break_label == false_block);
1950 continue_label = old_continue_label;
1951 break_label = old_break_label;
1953 if (get_cur_block() == NULL) {
1954 mature_immBlock(header_block);
1955 mature_immBlock(body_block);
1956 mature_immBlock(false_block);
1961 ir_node *body_jmp = new_Jmp();
1962 add_immBlock_pred(header_block, body_jmp);
1963 mature_immBlock(header_block);
1965 /* create the condition */
1966 set_cur_block(header_block);
1968 create_condition_evaluation(statement->condition, body_block, false_block);
1969 mature_immBlock(body_block);
1970 mature_immBlock(false_block);
1971 mature_immBlock(header_block);
1973 set_cur_block(false_block);
1976 static void for_statement_to_firm(for_statement_t *statement)
1978 ir_node *jmp = NULL;
1979 if (get_cur_block() != NULL) {
1980 if(statement->initialisation != NULL) {
1981 expression_to_firm(statement->initialisation);
1986 /* create the step block */
1987 ir_node *const step_block = new_immBlock();
1988 if (statement->step != NULL) {
1989 expression_to_firm(statement->step);
1991 ir_node *const step_jmp = new_Jmp();
1993 /* create the header block */
1994 ir_node *const header_block = new_immBlock();
1996 add_immBlock_pred(header_block, jmp);
1998 add_immBlock_pred(header_block, step_jmp);
2000 /* the false block */
2001 ir_node *const false_block = new_immBlock();
2004 ir_node * body_block;
2005 if (statement->body != NULL) {
2006 ir_node *const old_continue_label = continue_label;
2007 ir_node *const old_break_label = break_label;
2008 continue_label = step_block;
2009 break_label = false_block;
2011 body_block = new_immBlock();
2012 statement_to_firm(statement->body);
2014 assert(continue_label == step_block);
2015 assert(break_label == false_block);
2016 continue_label = old_continue_label;
2017 break_label = old_break_label;
2019 if (get_cur_block() != NULL) {
2020 ir_node *const jmp = new_Jmp();
2021 add_immBlock_pred(step_block, jmp);
2024 body_block = step_block;
2027 /* create the condition */
2028 set_cur_block(header_block);
2029 if (statement->condition != NULL) {
2030 create_condition_evaluation(statement->condition, body_block,
2033 keep_alive(header_block);
2034 ir_node *jmp = new_Jmp();
2035 add_immBlock_pred(body_block, jmp);
2038 mature_immBlock(body_block);
2039 mature_immBlock(false_block);
2040 mature_immBlock(step_block);
2041 mature_immBlock(header_block);
2042 mature_immBlock(false_block);
2044 set_cur_block(false_block);
2047 static void create_declaration_entity(declaration_t *declaration,
2048 declaration_type_t declaration_type,
2049 ir_type *parent_type)
2051 ident *id = new_id_from_str(declaration->symbol->string);
2052 ir_type *irtype = get_ir_type(declaration->type);
2053 ir_entity *entity = new_entity(parent_type, id, irtype);
2054 set_entity_ld_ident(entity, id);
2056 declaration->declaration_type = declaration_type;
2057 declaration->v.entity = entity;
2058 set_entity_variability(entity, variability_uninitialized);
2059 /* TODO: visibility? */
2062 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2064 enum compound_graph_entry_type_t {
2065 COMPOUND_GRAPH_ENTRY_ARRAY,
2066 COMPOUND_GRAPH_ENTRY_COMPOUND
2069 struct compound_graph_path_entry_t {
2075 compound_graph_path_entry_t *prev;
2078 static void create_initializer_object(initializer_t *initializer, type_t *type,
2079 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2081 static compound_graph_path *create_compound_path(ir_type *type,
2082 compound_graph_path_entry_t *entry, int len)
2084 compound_graph_path *path = new_compound_graph_path(type, len);
2087 for( ; entry != NULL; entry = entry->prev, --i) {
2089 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2090 set_compound_graph_path_node(path, i, entry->v.entity);
2092 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2093 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2101 static void create_initializer_value(initializer_value_t *initializer,
2103 compound_graph_path_entry_t *entry,
2106 ir_node *node = expression_to_firm(initializer->value);
2107 ir_type *type = get_entity_type(entity);
2108 compound_graph_path *path = create_compound_path(type, entry, len);
2109 add_compound_ent_value_w_path(entity, node, path);
2112 static void create_initializer_compound(initializer_list_t *initializer,
2113 compound_type_t *type,
2115 compound_graph_path_entry_t *last_entry,
2118 declaration_t *compound_declaration = type->declaration;
2120 declaration_t *compound_entry = compound_declaration->context.declarations;
2122 compound_graph_path_entry_t entry;
2123 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2124 entry.prev = last_entry;
2128 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2129 if(compound_entry->symbol == NULL)
2131 if(compound_entry->namespc != NAMESPACE_NORMAL)
2134 if(i >= initializer->len)
2137 entry.v.entity = compound_entry->v.entity;
2139 initializer_t *sub_initializer = initializer->initializers[i];
2141 assert(compound_entry != NULL);
2142 assert(compound_entry->declaration_type
2143 == DECLARATION_TYPE_COMPOUND_MEMBER);
2145 if(sub_initializer->type == INITIALIZER_VALUE) {
2146 create_initializer_value(&sub_initializer->value,
2147 entity, &entry, len);
2149 type_t *type = skip_typeref(compound_entry->type);
2150 create_initializer_object(sub_initializer, type, entity, &entry,
2158 static void create_initializer_array(initializer_list_t *initializer,
2159 array_type_t *type, ir_entity *entity,
2160 compound_graph_path_entry_t *last_entry,
2163 type_t *element_type = type->element_type;
2164 element_type = skip_typeref(element_type);
2166 compound_graph_path_entry_t entry;
2167 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2168 entry.prev = last_entry;
2172 for(i = 0; i < initializer->len; ++i) {
2173 entry.v.array_index = i;
2175 initializer_t *sub_initializer = initializer->initializers[i];
2177 if(sub_initializer->type == INITIALIZER_VALUE) {
2178 create_initializer_value(&sub_initializer->value,
2179 entity, &entry, len);
2181 create_initializer_object(sub_initializer, element_type, entity,
2187 /* TODO: initializer rest... */
2188 if(type->size_expression != NULL) {
2189 size_t array_len = fold_constant(type->size_expression);
2190 for( ; i < array_len; ++i) {
2197 static void create_initializer_string(initializer_string_t *initializer,
2198 array_type_t *type, ir_entity *entity,
2199 compound_graph_path_entry_t *last_entry,
2202 type_t *element_type = type->element_type;
2203 element_type = skip_typeref(element_type);
2205 compound_graph_path_entry_t entry;
2206 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2207 entry.prev = last_entry;
2210 ir_type *irtype = get_entity_type(entity);
2211 size_t arr_len = get_array_type_size(type);
2212 const char *p = initializer->string;
2214 for(i = 0; i < arr_len; ++i, ++p) {
2215 entry.v.array_index = i;
2217 ir_node *node = new_Const_long(mode_Bs, *p);
2218 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2219 add_compound_ent_value_w_path(entity, node, path);
2226 static void create_initializer_object(initializer_t *initializer, type_t *type,
2227 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2229 if(type->type == TYPE_ARRAY) {
2230 array_type_t *array_type = &type->array;
2232 if(initializer->type == INITIALIZER_STRING) {
2233 initializer_string_t *string = &initializer->string;
2234 create_initializer_string(string, array_type, entity, entry, len);
2236 assert(initializer->type == INITIALIZER_LIST);
2237 initializer_list_t *list = &initializer->list;
2238 create_initializer_array(list, array_type, entity, entry, len);
2241 assert(initializer->type == INITIALIZER_LIST);
2242 initializer_list_t *list = &initializer->list;
2244 assert(type->type == TYPE_COMPOUND_STRUCT
2245 || type->type == TYPE_COMPOUND_UNION);
2246 compound_type_t *compound_type = &type->compound;
2247 create_initializer_compound(list, compound_type, entity, entry, len);
2251 static void create_initializer_local_variable_entity(declaration_t *declaration)
2253 initializer_t *initializer = declaration->init.initializer;
2254 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2255 ir_entity *entity = declaration->v.entity;
2256 ir_node *memory = get_store();
2257 ir_node *nomem = new_NoMem();
2258 ir_node *frame = get_irg_frame(current_ir_graph);
2259 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2261 if(is_atomic_entity(entity)) {
2262 assert(initializer->type == INITIALIZER_VALUE);
2263 initializer_value_t *initializer_value = &initializer->value;
2265 ir_node *value = expression_to_firm(initializer_value->value);
2266 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2267 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2268 set_store(store_mem);
2272 /* create a "template" entity which is copied to the entity on the stack */
2273 ident *id = unique_ident("initializer");
2274 ir_type *irtype = get_ir_type(declaration->type);
2275 ir_type *global_type = get_glob_type();
2276 ir_entity *init_entity = new_entity(global_type, id, irtype);
2277 set_entity_ld_ident(init_entity, id);
2279 set_entity_variability(init_entity, variability_initialized);
2280 set_entity_visibility(init_entity, visibility_local);
2282 ir_graph *old_current_ir_graph = current_ir_graph;
2283 current_ir_graph = get_const_code_irg();
2285 type_t *type = skip_typeref(declaration->type);
2286 create_initializer_object(initializer, type, init_entity, NULL, 0);
2288 assert(current_ir_graph == get_const_code_irg());
2289 current_ir_graph = old_current_ir_graph;
2291 ir_node *src_addr = create_symconst(dbgi, init_entity);
2292 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2294 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2295 set_store(copyb_mem);
2298 static void create_initializer(declaration_t *declaration)
2300 initializer_t *initializer = declaration->init.initializer;
2301 if(initializer == NULL)
2304 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2305 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2306 create_initializer_local_variable_entity(declaration);
2310 if(initializer->type == INITIALIZER_VALUE) {
2311 initializer_value_t *initializer_value = &initializer->value;
2313 ir_node *value = expression_to_firm(initializer_value->value);
2315 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2316 set_value(declaration->v.value_number, value);
2318 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2320 ir_entity *entity = declaration->v.entity;
2322 set_entity_variability(entity, variability_initialized);
2323 set_atomic_ent_value(entity, value);
2326 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2327 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2328 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2330 ir_entity *entity = declaration->v.entity;
2331 set_entity_variability(entity, variability_initialized);
2333 type_t *type = skip_typeref(declaration->type);
2334 create_initializer_object(initializer, type, entity, NULL, 0);
2338 static void create_local_variable(declaration_t *declaration)
2340 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2342 bool needs_entity = declaration->address_taken;
2343 type_t *type = skip_typeref(declaration->type);
2345 if(type->type == TYPE_ARRAY
2346 || type->type == TYPE_COMPOUND_STRUCT
2347 || type->type == TYPE_COMPOUND_UNION) {
2348 needs_entity = true;
2352 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2353 create_declaration_entity(declaration,
2354 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2357 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2358 declaration->v.value_number = next_value_number_function;
2359 ++next_value_number_function;
2362 create_initializer(declaration);
2365 static void create_local_static_variable(declaration_t *declaration)
2367 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2369 type_t *type = skip_typeref(declaration->type);
2370 ir_type *global_type = get_glob_type();
2371 ident *id = unique_ident(declaration->symbol->string);
2372 ir_type *irtype = get_ir_type(type);
2373 ir_entity *entity = new_entity(global_type, id, irtype);
2374 set_entity_ld_ident(entity, id);
2376 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2377 declaration->v.entity = entity;
2378 set_entity_variability(entity, variability_uninitialized);
2379 set_entity_visibility(entity, visibility_local);
2381 ir_graph *old_current_ir_graph = current_ir_graph;
2382 current_ir_graph = get_const_code_irg();
2384 create_initializer(declaration);
2386 assert(current_ir_graph == get_const_code_irg());
2387 current_ir_graph = old_current_ir_graph;
2390 static void declaration_statement_to_firm(declaration_statement_t *statement)
2392 declaration_t *declaration = statement->declarations_begin;
2393 declaration_t *end = statement->declarations_end->next;
2394 for( ; declaration != end; declaration = declaration->next) {
2395 type_t *type = declaration->type;
2397 switch(declaration->storage_class) {
2398 case STORAGE_CLASS_TYPEDEF:
2400 case STORAGE_CLASS_STATIC:
2401 create_local_static_variable(declaration);
2403 case STORAGE_CLASS_ENUM_ENTRY:
2404 panic("enum entry declaration in local block found");
2405 case STORAGE_CLASS_EXTERN:
2406 panic("extern declaration in local block found");
2407 case STORAGE_CLASS_NONE:
2408 case STORAGE_CLASS_AUTO:
2409 case STORAGE_CLASS_REGISTER:
2410 if(type->type == TYPE_FUNCTION) {
2411 panic("nested functions not supported yet");
2413 create_local_variable(declaration);
2417 panic("invalid storage class found");
2421 static void create_jump_statement(const statement_t *statement,
2422 ir_node *target_block)
2424 if(get_cur_block() == NULL)
2427 dbg_info *dbgi = get_dbg_info(&statement->source_position);
2428 ir_node *jump = new_d_Jmp(dbgi);
2429 add_immBlock_pred(target_block, jump);
2431 set_cur_block(NULL);
2434 static void switch_statement_to_firm(const switch_statement_t *statement)
2436 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2438 ir_node *expression = expression_to_firm(statement->expression);
2439 ir_node *cond = new_d_Cond(dbgi, expression);
2440 ir_node *break_block = new_immBlock();
2442 set_cur_block(NULL);
2444 ir_node *const old_switch_cond = current_switch_cond;
2445 ir_node *const old_break_label = break_label;
2446 const bool old_saw_default_label = saw_default_label;
2447 current_switch_cond = cond;
2448 break_label = break_block;
2450 statement_to_firm(statement->body);
2452 if(get_cur_block() != NULL) {
2453 ir_node *jmp = new_Jmp();
2454 add_immBlock_pred(break_block, jmp);
2457 if (!saw_default_label) {
2458 set_cur_block(get_nodes_block(cond));
2459 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2460 MAGIC_DEFAULT_PN_NUMBER);
2461 add_immBlock_pred(break_block, proj);
2464 assert(current_switch_cond == cond);
2465 assert(break_label == break_block);
2466 current_switch_cond = old_switch_cond;
2467 break_label = old_break_label;
2468 saw_default_label = old_saw_default_label;
2470 mature_immBlock(break_block);
2471 set_cur_block(break_block);
2474 static long fold_constant(const expression_t *expression)
2476 ir_graph *old_current_ir_graph = current_ir_graph;
2477 current_ir_graph = get_const_code_irg();
2479 ir_node *cnst = expression_to_firm(expression);
2480 if(!is_Const(cnst)) {
2481 panic("couldn't fold constantl");
2483 tarval *tv = get_Const_tarval(cnst);
2484 if(!tarval_is_long(tv)) {
2485 panic("folded constant not an integer");
2488 long res = get_tarval_long(tv);
2490 current_ir_graph = old_current_ir_graph;
2494 static void case_label_to_firm(const case_label_statement_t *statement)
2496 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2498 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2500 /* let's create a node and hope firm constant folding creates a Const
2503 set_cur_block(get_nodes_block(current_switch_cond));
2504 if(statement->expression) {
2505 long pn = fold_constant(statement->expression);
2506 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2507 /* oops someone detected our cheating... */
2508 panic("magic default pn used");
2510 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2512 saw_default_label = true;
2513 proj = new_d_defaultProj(dbgi, current_switch_cond,
2514 MAGIC_DEFAULT_PN_NUMBER);
2517 ir_node *block = new_immBlock();
2518 if (fallthrough != NULL) {
2519 add_immBlock_pred(block, fallthrough);
2521 add_immBlock_pred(block, proj);
2522 mature_immBlock(block);
2524 statement_to_firm(statement->label_statement);
2527 static ir_node *get_label_block(declaration_t *label)
2529 assert(label->namespc == NAMESPACE_LABEL);
2531 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2532 return label->v.block;
2534 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2536 ir_node *old_cur_block = get_cur_block();
2537 ir_node *block = new_immBlock();
2538 set_cur_block(old_cur_block);
2540 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2541 label->v.block = block;
2543 ARR_APP1(ir_node *, imature_blocks, block);
2548 static void label_to_firm(const label_statement_t *statement)
2550 ir_node *block = get_label_block(statement->label);
2552 if(get_cur_block() != NULL) {
2553 ir_node *jmp = new_Jmp();
2554 add_immBlock_pred(block, jmp);
2557 set_cur_block(block);
2560 statement_to_firm(statement->label_statement);
2563 static void goto_to_firm(const goto_statement_t *statement)
2565 if(get_cur_block() == NULL)
2568 ir_node *block = get_label_block(statement->label);
2569 ir_node *jmp = new_Jmp();
2570 add_immBlock_pred(block, jmp);
2572 set_cur_block(NULL);
2575 static void statement_to_firm(statement_t *statement)
2577 switch(statement->type) {
2578 case STATEMENT_COMPOUND:
2579 compound_statement_to_firm((compound_statement_t*) statement);
2581 case STATEMENT_RETURN:
2582 return_statement_to_firm((return_statement_t*) statement);
2584 case STATEMENT_EXPRESSION:
2585 expression_statement_to_firm((expression_statement_t*) statement);
2588 if_statement_to_firm((if_statement_t*) statement);
2590 case STATEMENT_WHILE:
2591 while_statement_to_firm((while_statement_t*) statement);
2593 case STATEMENT_DO_WHILE:
2594 do_while_statement_to_firm((do_while_statement_t*) statement);
2596 case STATEMENT_DECLARATION:
2597 declaration_statement_to_firm((declaration_statement_t*) statement);
2599 case STATEMENT_BREAK:
2600 create_jump_statement(statement, break_label);
2602 case STATEMENT_CONTINUE:
2603 create_jump_statement(statement, continue_label);
2605 case STATEMENT_SWITCH:
2606 switch_statement_to_firm((switch_statement_t*) statement);
2608 case STATEMENT_CASE_LABEL:
2609 case_label_to_firm((case_label_statement_t*) statement);
2612 for_statement_to_firm((for_statement_t*) statement);
2614 case STATEMENT_LABEL:
2615 label_to_firm((label_statement_t*) statement);
2617 case STATEMENT_GOTO:
2618 goto_to_firm((goto_statement_t*) statement);
2623 panic("Statement not implemented\n");
2626 static int count_local_declarations(const declaration_t * decl,
2627 const declaration_t *const end)
2630 for (; decl != end; decl = decl->next) {
2631 const type_t *type = skip_typeref(decl->type);
2632 switch (type->type) {
2636 if (!decl->address_taken)
2646 static int count_decls_in_stmts(const statement_t *stmt)
2649 for (; stmt != NULL; stmt = stmt->next) {
2650 switch (stmt->type) {
2651 case STATEMENT_DECLARATION: {
2652 const declaration_statement_t *const decl_stmt =
2653 (const declaration_statement_t*)stmt;
2654 count += count_local_declarations(decl_stmt->declarations_begin,
2655 decl_stmt->declarations_end->next);
2659 case STATEMENT_COMPOUND: {
2660 const compound_statement_t *const comp =
2661 (const compound_statement_t*)stmt;
2662 count += count_decls_in_stmts(comp->statements);
2666 case STATEMENT_IF: {
2667 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2668 count += count_decls_in_stmts(if_stmt->true_statement);
2669 count += count_decls_in_stmts(if_stmt->false_statement);
2673 case STATEMENT_SWITCH: {
2674 const switch_statement_t *const switch_stmt =
2675 (const switch_statement_t*)stmt;
2676 count += count_decls_in_stmts(switch_stmt->body);
2680 case STATEMENT_LABEL: {
2681 const label_statement_t *const label_stmt =
2682 (const label_statement_t*)stmt;
2683 count += count_decls_in_stmts(label_stmt->label_statement);
2687 case STATEMENT_WHILE: {
2688 const while_statement_t *const while_stmt =
2689 (const while_statement_t*)stmt;
2690 count += count_decls_in_stmts(while_stmt->body);
2694 case STATEMENT_DO_WHILE: {
2695 const do_while_statement_t *const do_while_stmt =
2696 (const do_while_statement_t*)stmt;
2697 count += count_decls_in_stmts(do_while_stmt->body);
2701 case STATEMENT_FOR: {
2702 const for_statement_t *const for_stmt =
2703 (const for_statement_t*)stmt;
2704 /* TODO initialisation */
2705 count += count_decls_in_stmts(for_stmt->body);
2709 case STATEMENT_BREAK:
2710 case STATEMENT_CASE_LABEL:
2711 case STATEMENT_CONTINUE:
2712 case STATEMENT_EXPRESSION:
2713 case STATEMENT_GOTO:
2714 case STATEMENT_INVALID:
2715 case STATEMENT_RETURN:
2722 static int get_function_n_local_vars(declaration_t *declaration)
2726 /* count parameters */
2727 count += count_local_declarations(declaration->context.declarations, NULL);
2729 /* count local variables declared in body */
2730 count += count_decls_in_stmts(declaration->init.statement);
2732 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2733 * for expression statements... */
2739 static void initialize_function_parameters(declaration_t *declaration)
2741 ir_graph *irg = current_ir_graph;
2742 ir_node *args = get_irg_args(irg);
2743 ir_node *start_block = get_irg_start_block(irg);
2744 ir_type *function_irtype = get_ir_type(declaration->type);
2747 declaration_t *parameter = declaration->context.declarations;
2748 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2749 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2750 type_t *type = skip_typeref(parameter->type);
2752 bool needs_entity = parameter->address_taken;
2753 if(type->type == TYPE_COMPOUND_STRUCT
2754 || type->type == TYPE_COMPOUND_UNION) {
2755 needs_entity = true;
2759 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2760 ident *id = new_id_from_str(parameter->symbol->string);
2761 set_entity_ident(entity, id);
2763 parameter->declaration_type
2764 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2765 parameter->v.entity = entity;
2769 ir_mode *mode = get_ir_mode(parameter->type);
2771 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2773 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2774 parameter->v.value_number = next_value_number_function;
2775 ++next_value_number_function;
2777 set_value(parameter->v.value_number, proj);
2781 static void create_function(declaration_t *declaration)
2783 ir_entity *entity = get_function_entity(declaration);
2785 if(declaration->init.statement == NULL)
2788 current_function_decl = declaration;
2789 current_function_name = NULL;
2791 assert(imature_blocks == NULL);
2792 imature_blocks = NEW_ARR_F(ir_node*, 0);
2794 int n_local_vars = get_function_n_local_vars(declaration);
2795 ir_graph *irg = new_ir_graph(entity, n_local_vars);
2796 ir_node *first_block = get_cur_block();
2798 next_value_number_function = 0;
2799 initialize_function_parameters(declaration);
2801 statement_to_firm(declaration->init.statement);
2803 ir_node *end_block = get_irg_end_block(irg);
2805 /* do we have a return statement yet? */
2806 if(get_cur_block() != NULL) {
2807 assert(declaration->type->type == TYPE_FUNCTION);
2808 const function_type_t* const func_type = &declaration->type->function;
2811 if (func_type->result_type == type_void) {
2812 ret = new_Return(get_store(), 0, NULL);
2814 ir_mode *const mode = get_ir_mode(func_type->result_type);
2816 // ยง5.1.2.2.3 main implicitly returns 0
2817 if (strcmp(declaration->symbol->string, "main") == 0) {
2818 in[0] = new_Const(mode, get_mode_null(mode));
2820 in[0] = new_Unknown(mode);
2822 ret = new_Return(get_store(), 1, in);
2824 add_immBlock_pred(end_block, ret);
2827 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2828 mature_immBlock(imature_blocks[i]);
2830 DEL_ARR_F(imature_blocks);
2831 imature_blocks = NULL;
2833 mature_immBlock(first_block);
2834 mature_immBlock(end_block);
2836 irg_finalize_cons(irg);
2838 /* finalize the frame type */
2839 ir_type *frame_type = get_irg_frame_type(irg);
2840 int n = get_compound_n_members(frame_type);
2843 for(int i = 0; i < n; ++i) {
2844 ir_entity *entity = get_compound_member(frame_type, i);
2845 ir_type *entity_type = get_entity_type(entity);
2847 int align = get_type_alignment_bytes(entity_type);
2848 if(align > align_all)
2852 misalign = offset % align;
2854 offset += align - misalign;
2858 set_entity_offset(entity, offset);
2859 offset += get_type_size_bytes(entity_type);
2861 set_type_size_bytes(frame_type, offset);
2862 set_type_alignment_bytes(frame_type, align_all);
2863 set_type_state(frame_type, layout_fixed);
2868 static void create_global_variable(declaration_t *declaration)
2870 ir_type *global_type = get_glob_type();
2871 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2874 ir_entity *entity = declaration->v.entity;
2875 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2876 set_entity_visibility(entity, visibility_local);
2877 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2878 set_entity_visibility(entity, visibility_external_allocated);
2880 set_entity_visibility(entity, visibility_external_visible);
2882 current_ir_graph = get_const_code_irg();
2883 create_initializer(declaration);
2886 static void context_to_firm(context_t *context)
2888 /* first pass: create declarations */
2889 declaration_t *declaration = context->declarations;
2890 for( ; declaration != NULL; declaration = declaration->next) {
2891 if(declaration->namespc != NAMESPACE_NORMAL)
2893 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2894 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2896 if(declaration->symbol == NULL)
2899 type_t *type = declaration->type;
2900 if(type->type == TYPE_FUNCTION) {
2901 get_function_entity(declaration);
2903 create_global_variable(declaration);
2907 /* second pass: create code */
2908 declaration = context->declarations;
2909 for( ; declaration != NULL; declaration = declaration->next) {
2910 if(declaration->namespc != NAMESPACE_NORMAL)
2912 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2913 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2915 if(declaration->symbol == NULL)
2918 type_t *type = declaration->type;
2919 if(type->type != TYPE_FUNCTION)
2922 create_function(declaration);
2926 void translation_unit_to_firm(translation_unit_t *unit)
2928 /* just to be sure */
2929 continue_label = NULL;
2931 current_switch_cond = NULL;
2933 context_to_firm(& unit->context);