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->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->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);
251 panic("Trying to determine size of invalid type");
254 static unsigned count_parameters(const function_type_t *function_type)
258 function_parameter_t *parameter = function_type->parameters;
259 for ( ; parameter != NULL; parameter = parameter->next) {
269 static long fold_constant(const expression_t *expression);
271 static ir_type *create_atomic_type(const atomic_type_t *type)
273 ir_mode *mode = get_atomic_mode(type);
274 ident *id = get_mode_ident(mode);
275 ir_type *irtype = new_type_primitive(id, mode);
280 static ir_type *create_method_type(const function_type_t *function_type)
282 type_t *result_type = function_type->result_type;
284 ident *id = unique_ident("functiontype");
285 int n_parameters = count_parameters(function_type);
286 int n_results = result_type == type_void ? 0 : 1;
287 ir_type *irtype = new_type_method(id, n_parameters, n_results);
289 if(result_type != type_void) {
290 ir_type *restype = get_ir_type(result_type);
291 set_method_res_type(irtype, 0, restype);
294 function_parameter_t *parameter = function_type->parameters;
296 for( ; parameter != NULL; parameter = parameter->next) {
297 ir_type *p_irtype = get_ir_type(parameter->type);
298 set_method_param_type(irtype, n, p_irtype);
302 if(function_type->variadic || function_type->unspecified_parameters) {
303 set_method_variadicity(irtype, variadicity_variadic);
309 static ir_type *create_pointer_type(pointer_type_t *type)
311 type_t *points_to = type->points_to;
312 ir_type *ir_points_to;
313 /* Avoid endless recursion if the points_to type contains this poiner type
314 * again (might be a struct). We therefore first create a void* pointer
315 * and then set the real points_to type
317 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
318 ir_type_void, mode_P_data);
319 type->type.firm_type = ir_type;
321 ir_points_to = get_ir_type(points_to);
322 set_pointer_points_to_type(ir_type, ir_points_to);
327 static ir_type *create_array_type(array_type_t *type)
329 type_t *element_type = type->element_type;
330 ir_type *ir_element_type = get_ir_type(element_type);
332 ident *id = unique_ident("array");
333 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
335 if(type->size != NULL) {
336 int n_elements = fold_constant(type->size);
338 set_array_bounds_int(ir_type, 0, 0, n_elements);
340 size_t elemsize = get_type_size_bytes(ir_element_type);
341 int align = get_type_alignment_bytes(ir_element_type);
342 if(elemsize % align > 0) {
343 elemsize += align - (elemsize % align);
345 set_type_size_bytes(ir_type, n_elements * elemsize);
346 set_type_alignment_bytes(ir_type, align);
347 set_type_state(ir_type, layout_fixed);
353 #define INVALID_TYPE ((ir_type_ptr)-1)
355 static ir_type *create_struct_type(compound_type_t *type)
357 symbol_t *symbol = type->declaration->symbol;
360 id = unique_ident(symbol->string);
362 id = unique_ident("__anonymous_struct");
364 ir_type *ir_type = new_type_struct(id);
366 type->type.firm_type = ir_type;
370 declaration_t *entry = type->declaration->context.declarations;
371 for( ; entry != NULL; entry = entry->next) {
372 if(entry->namespc != NAMESPACE_NORMAL)
375 ident *ident = new_id_from_str(entry->symbol->string);
376 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
378 int entry_size = get_type_size_bytes(entry_ir_type);
379 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
380 int misalign = offset % entry_alignment;
382 offset += entry_alignment - misalign;
384 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
385 set_entity_offset(entity, offset);
386 add_struct_member(ir_type, entity);
387 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
388 entry->v.entity = entity;
390 offset += entry_size;
391 if(entry_alignment > align_all) {
392 if(entry_alignment % align_all != 0) {
393 panic("Uneven alignments not supported yet");
395 align_all = entry_alignment;
399 int misalign = offset % align_all;
401 set_type_alignment_bytes(ir_type, align_all);
402 set_type_size_bytes(ir_type, offset);
403 set_type_state(ir_type, layout_fixed);
408 static ir_type *create_union_type(compound_type_t *type)
410 declaration_t *declaration = type->declaration;
411 symbol_t *symbol = declaration->symbol;
414 id = unique_ident(symbol->string);
416 id = unique_ident("__anonymous_union");
418 ir_type *ir_type = new_type_union(id);
420 type->type.firm_type = ir_type;
424 declaration_t *entry = declaration->context.declarations;
425 for( ; entry != NULL; entry = entry->next) {
426 if(entry->namespc != NAMESPACE_NORMAL)
429 ident *ident = new_id_from_str(entry->symbol->string);
430 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
432 int entry_size = get_type_size_bytes(entry_ir_type);
433 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
435 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
436 add_union_member(ir_type, entity);
437 set_entity_offset(entity, 0);
438 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
439 entry->v.entity = entity;
441 if(entry_size > size) {
444 if(entry_alignment > align_all) {
445 if(entry_alignment % align_all != 0) {
446 panic("Uneven alignments not supported yet");
448 align_all = entry_alignment;
452 set_type_alignment_bytes(ir_type, align_all);
453 set_type_size_bytes(ir_type, size);
454 set_type_state(ir_type, layout_fixed);
459 static ir_node *expression_to_firm(const expression_t *expression);
460 static inline ir_mode *get_ir_mode(type_t *type);
462 static ir_type *create_enum_type(enum_type_t *const type)
464 type->type.firm_type = ir_type_int;
466 ir_mode *const mode = get_ir_mode((type_t*) type);
467 tarval *const one = get_mode_one(mode);
468 tarval * tv_next = get_tarval_null(mode);
470 declaration_t *declaration = type->declaration->next;
471 for (; declaration != NULL; declaration = declaration->next) {
472 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
475 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
477 expression_t *const init = declaration->init.enum_value;
479 ir_node *const cnst = expression_to_firm(init);
480 if (!is_Const(cnst)) {
481 panic("couldn't fold constant");
483 tv_next = get_Const_tarval(cnst);
485 declaration->v.enum_val = tv_next;
486 tv_next = tarval_add(tv_next, one);
492 static ir_type *get_ir_type(type_t *type)
494 assert(type != NULL);
496 type = skip_typeref(type);
498 if(type->firm_type != NULL) {
499 assert(type->firm_type != INVALID_TYPE);
500 return type->firm_type;
503 ir_type *firm_type = NULL;
506 firm_type = create_atomic_type((atomic_type_t*) type);
509 firm_type = create_method_type((function_type_t*) type);
512 firm_type = create_pointer_type((pointer_type_t*) type);
515 firm_type = create_array_type((array_type_t*) type);
517 case TYPE_COMPOUND_STRUCT:
518 firm_type = create_struct_type((compound_type_t*) type);
520 case TYPE_COMPOUND_UNION:
521 firm_type = create_union_type((compound_type_t*) type);
524 firm_type = create_enum_type((enum_type_t*) type);
532 if(firm_type == NULL)
533 panic("unknown type found");
535 type->firm_type = firm_type;
539 static inline ir_mode *get_ir_mode(type_t *type)
541 ir_type *irtype = get_ir_type(type);
543 /* firm doesn't report a mode for arrays somehow... */
544 if(is_Array_type(irtype)) {
548 ir_mode *mode = get_type_mode(irtype);
549 assert(mode != NULL);
553 static ir_entity* get_function_entity(declaration_t *declaration)
555 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
556 return declaration->v.entity;
557 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
559 symbol_t *symbol = declaration->symbol;
560 ident *id = new_id_from_str(symbol->string);
562 ir_type *global_type = get_glob_type();
563 ir_type *ir_type_method = get_ir_type(declaration->type);
564 assert(is_Method_type(ir_type_method));
566 ir_entity *entity = new_entity(global_type, id, ir_type_method);
567 set_entity_ld_ident(entity, id);
568 if(declaration->storage_class == STORAGE_CLASS_STATIC
569 || declaration->is_inline) {
570 set_entity_visibility(entity, visibility_local);
571 } else if(declaration->init.statement != NULL) {
572 set_entity_visibility(entity, visibility_external_visible);
574 set_entity_visibility(entity, visibility_external_allocated);
577 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
578 declaration->v.entity = entity;
583 static dbg_info *get_dbg_info(const source_position_t *pos)
585 return (dbg_info*) pos;
588 static ir_node *const_to_firm(const const_t *cnst)
590 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
591 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
596 if(mode_is_float(mode)) {
597 tv = new_tarval_from_double(cnst->v.float_value, mode);
599 if(mode_is_signed(mode)) {
600 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
602 len = snprintf(buf, sizeof(buf), "%llu", cnst->v.int_value);
604 tv = new_tarval_from_str(buf, len, mode);
607 return new_d_Const(dbgi, mode, tv);
610 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
612 assert(entity != NULL);
613 union symconst_symbol sym;
614 sym.entity_p = entity;
615 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
618 static ir_node *string_to_firm(const source_position_t *const src_pos,
619 const char *const id_prefix,
620 const char *const string)
622 ir_type *const global_type = get_glob_type();
623 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
626 ident *const id = unique_ident(id_prefix);
627 ir_entity *const entity = new_entity(global_type, id, type);
628 set_entity_ld_ident(entity, id);
629 set_entity_variability(entity, variability_constant);
631 ir_type *const elem_type = ir_type_const_char;
632 ir_mode *const mode = get_type_mode(elem_type);
634 const size_t slen = strlen(string) + 1;
636 set_array_lower_bound_int(type, 0, 0);
637 set_array_upper_bound_int(type, 0, slen);
638 set_type_size_bytes(type, slen);
639 set_type_state(type, layout_fixed);
641 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
642 for(size_t i = 0; i < slen; ++i) {
643 tvs[i] = new_tarval_from_long(string[i], mode);
646 set_array_entity_values(entity, tvs, slen);
649 dbg_info *const dbgi = get_dbg_info(src_pos);
651 return create_symconst(dbgi, entity);
654 static ir_node *string_literal_to_firm(const string_literal_t* literal)
656 return string_to_firm(&literal->expression.source_position, "Lstr",
660 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
661 dbg_info *const dbgi)
663 if(is_compound_type(irtype) || is_Array_type(irtype)) {
667 ir_mode *const mode = get_type_mode(irtype);
668 ir_node *const memory = get_store();
669 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
670 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
671 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
676 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
678 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
679 declaration_t *declaration = ref->declaration;
680 type_t *type = skip_typeref(declaration->type);
682 switch((declaration_type_t) declaration->declaration_type) {
683 case DECLARATION_TYPE_UNKNOWN:
684 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
690 case DECLARATION_TYPE_ENUM_ENTRY: {
691 ir_mode *const mode = get_ir_mode(type);
692 return new_Const(mode, declaration->v.enum_val);
695 case DECLARATION_TYPE_LOCAL_VARIABLE: {
696 ir_mode *mode = get_ir_mode(type);
697 return get_value(declaration->v.value_number, mode);
699 case DECLARATION_TYPE_FUNCTION: {
700 return create_symconst(dbgi, declaration->v.entity);
702 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
703 ir_entity *entity = declaration->v.entity;
704 ir_node *symconst = create_symconst(dbgi, entity);
705 ir_type *irtype = get_entity_type(entity);
706 return deref_address(irtype, symconst, dbgi);
708 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
709 ir_entity *entity = declaration->v.entity;
710 ir_node *frame = get_irg_frame(current_ir_graph);
711 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
712 ir_type *irtype = get_entity_type(entity);
713 return deref_address(irtype, sel, dbgi);
716 case DECLARATION_TYPE_COMPOUND_MEMBER:
717 case DECLARATION_TYPE_LABEL_BLOCK:
718 panic("not implemented reference type");
721 panic("reference to declaration with unknown type found");
724 static ir_node *reference_addr(const reference_expression_t *ref)
726 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
727 declaration_t *declaration = ref->declaration;
729 switch((declaration_type_t) declaration->declaration_type) {
730 case DECLARATION_TYPE_UNKNOWN:
732 case DECLARATION_TYPE_LOCAL_VARIABLE:
733 panic("local variable without entity has no address");
734 case DECLARATION_TYPE_FUNCTION: {
735 return create_symconst(dbgi, declaration->v.entity);
737 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
738 ir_entity *entity = declaration->v.entity;
739 ir_node *symconst = create_symconst(dbgi, entity);
742 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
743 ir_entity *entity = declaration->v.entity;
744 ir_node *frame = get_irg_frame(current_ir_graph);
745 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
750 case DECLARATION_TYPE_ENUM_ENTRY:
751 panic("trying to reference enum entry");
753 case DECLARATION_TYPE_COMPOUND_MEMBER:
754 case DECLARATION_TYPE_LABEL_BLOCK:
755 panic("not implemented reference type");
758 panic("reference to declaration with unknown type found");
761 static ir_node *process_builtin_call(const call_expression_t *call)
763 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
765 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
766 builtin_symbol_expression_t *builtin
767 = (builtin_symbol_expression_t*) call->function;
768 symbol_t *symbol = builtin->symbol;
770 if(symbol == symbol_alloca) {
771 if(call->arguments == NULL || call->arguments->next != NULL) {
772 panic("invalid number of parameters on __builtin_alloca");
774 expression_t *argument = call->arguments->expression;
775 ir_node *size = expression_to_firm(argument);
777 ir_node *store = get_store();
778 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
780 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
782 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
786 panic("Unsupported builtin found\n");
790 static ir_node *call_expression_to_firm(const call_expression_t *call)
792 assert(get_cur_block() != NULL);
794 expression_t *function = call->function;
795 if(function->type == EXPR_BUILTIN_SYMBOL) {
796 return process_builtin_call(call);
798 ir_node *callee = expression_to_firm(function);
800 function_type_t *function_type;
801 if (function->datatype->type == TYPE_POINTER) {
802 pointer_type_t *const ptr_type = (pointer_type_t*)function->datatype;
803 assert(ptr_type->points_to->type == TYPE_FUNCTION);
804 function_type = (function_type_t*)ptr_type->points_to;
806 assert(function->datatype->type == TYPE_FUNCTION);
807 function_type = (function_type_t*)function->datatype;
810 int n_parameters = 0;
811 call_argument_t *argument = call->arguments;
812 for( ; argument != NULL; argument = argument->next) {
816 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
817 ir_type *new_method_type = NULL;
818 if(function_type->variadic || function_type->unspecified_parameters) {
819 /* we need to construct a new method type matching the call
821 int n_res = get_method_n_ress(ir_method_type);
822 new_method_type = new_type_method(unique_ident("calltype"),
823 n_parameters, n_res);
824 set_method_calling_convention(new_method_type,
825 get_method_calling_convention(ir_method_type));
826 set_method_additional_properties(new_method_type,
827 get_method_additional_properties(ir_method_type));
829 for(int i = 0; i < n_res; ++i) {
830 set_method_res_type(new_method_type, i,
831 get_method_res_type(ir_method_type, i));
834 ir_node *in[n_parameters];
836 argument = call->arguments;
838 for( ; argument != NULL; argument = argument->next) {
839 expression_t *expression = argument->expression;
840 ir_node *arg_node = expression_to_firm(expression);
843 if(new_method_type != NULL) {
844 ir_type *irtype = get_ir_type(expression->datatype);
845 set_method_param_type(new_method_type, n, irtype);
850 assert(n == n_parameters);
852 if(new_method_type != NULL)
853 ir_method_type = new_method_type;
855 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
856 ir_node *store = get_store();
857 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
859 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
862 type_t *result_type = function_type->result_type;
863 ir_node *result = NULL;
864 if(result_type != type_void) {
865 ir_mode *mode = get_ir_mode(result_type);
866 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
867 result = new_d_Proj(dbgi, resproj, mode, 0);
873 static void statement_to_firm(statement_t *statement);
874 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
876 static ir_node *expression_to_addr(const expression_t *expression);
877 static void create_condition_evaluation(const expression_t *expression,
879 ir_node *false_block);
881 static void set_value_for_expression(const expression_t *expression,
884 if(expression->type == EXPR_REFERENCE) {
885 reference_expression_t *ref = (reference_expression_t*) expression;
887 declaration_t *declaration = ref->declaration;
888 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
889 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
890 set_value(declaration->v.value_number, value);
895 dbg_info *dbgi = get_dbg_info(&expression->source_position);
896 ir_node *addr = expression_to_addr(expression);
897 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
898 ir_node *memory = get_store();
899 ir_node *store = new_d_Store(dbgi, memory, addr, value);
900 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
901 set_store(store_mem);
904 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
906 ir_mode *value_mode = get_irn_mode(value);
908 if (value_mode == dest_mode || is_Bad(value))
911 if(dest_mode == mode_b) {
912 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
913 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
914 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
918 return new_d_Conv(dbgi, value, dest_mode);
921 static ir_node *create_incdec(const unary_expression_t *expression)
923 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
924 type_t *type = expression->expression.datatype;
925 ir_mode *mode = get_ir_mode(type);
926 expression_t *value = expression->value;
928 ir_node *value_node = expression_to_firm(value);
931 if(type->type == TYPE_POINTER) {
932 pointer_type_t *pointer_type = (pointer_type_t*) type;
933 unsigned elem_size = get_type_size(pointer_type->points_to);
934 offset = new_Const_long(mode_Is, elem_size);
936 assert(is_type_arithmetic(type));
937 offset = new_Const(mode, get_mode_one(mode));
940 switch(expression->type) {
941 case UNEXPR_POSTFIX_INCREMENT: {
942 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
943 set_value_for_expression(value, new_value);
946 case UNEXPR_POSTFIX_DECREMENT: {
947 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
948 set_value_for_expression(value, new_value);
951 case UNEXPR_PREFIX_INCREMENT: {
952 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
953 set_value_for_expression(value, new_value);
956 case UNEXPR_PREFIX_DECREMENT: {
957 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
958 set_value_for_expression(value, new_value);
962 panic("no incdec expr in create_incdec");
967 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
969 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
970 type_t *type = skip_typeref(expression->expression.datatype);
972 if(expression->type == UNEXPR_TAKE_ADDRESS)
973 return expression_to_addr(expression->value);
975 const expression_t *value = expression->value;
976 ir_node *value_node = expression_to_firm(value);
978 switch(expression->type) {
980 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
983 case UNEXPR_BITWISE_NEGATE:
984 return new_d_Not(dbgi, value_node, get_ir_mode(type));
986 if(get_irn_mode(value_node) != mode_b) {
987 value_node = create_conv(dbgi, value_node, mode_b);
989 value_node = new_d_Not(dbgi, value_node, mode_b);
990 ir_mode *const mode = get_ir_mode(type);
992 value_node = create_conv(dbgi, value_node, mode);
996 case UNEXPR_DEREFERENCE: {
997 ir_type *irtype = get_ir_type(type);
998 return deref_address(irtype, value_node, dbgi);
1000 case UNEXPR_POSTFIX_INCREMENT:
1001 case UNEXPR_POSTFIX_DECREMENT:
1002 case UNEXPR_PREFIX_INCREMENT:
1003 case UNEXPR_PREFIX_DECREMENT:
1004 return create_incdec(expression);
1006 return create_conv(dbgi, value_node, get_ir_mode(type));
1008 case UNEXPR_TAKE_ADDRESS:
1009 case UNEXPR_INVALID:
1012 panic("invalid UNEXPR type found");
1015 static long get_pnc(binary_expression_type_t type)
1018 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1019 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1020 case BINEXPR_LESS: return pn_Cmp_Lt;
1021 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1022 case BINEXPR_GREATER: return pn_Cmp_Gt;
1023 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1027 panic("trying to get pn_Cmp from non-comparison binexpr type");
1030 static ir_node *create_lazy_op(const binary_expression_t *expression)
1032 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1033 type_t *type = expression->expression.datatype;
1034 ir_mode *mode = get_ir_mode(type);
1036 ir_node *cur_block = get_cur_block();
1038 ir_node *one_block = new_immBlock();
1039 ir_node *one = new_Const(mode, get_mode_one(mode));
1040 ir_node *jmp_one = new_d_Jmp(dbgi);
1042 ir_node *zero_block = new_immBlock();
1043 ir_node *zero = new_Const(mode, get_mode_null(mode));
1044 ir_node *jmp_zero = new_d_Jmp(dbgi);
1046 set_cur_block(cur_block);
1047 create_condition_evaluation((const expression_t*) expression,
1048 one_block, zero_block);
1049 mature_immBlock(one_block);
1050 mature_immBlock(zero_block);
1052 ir_node *common_block = new_immBlock();
1053 add_immBlock_pred(common_block, jmp_one);
1054 add_immBlock_pred(common_block, jmp_zero);
1055 mature_immBlock(common_block);
1057 ir_node *in[2] = { one, zero };
1058 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1063 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1064 ir_node *right, ir_mode *mode);
1066 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1067 create_arithmetic_func func)
1069 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1070 ir_node *left = expression_to_firm(expression->left);
1071 ir_node *right = expression_to_firm(expression->right);
1072 type_t *type = expression->right->datatype;
1073 /* be careful with the modes, because in arithmetic assign nodes only
1074 * the right operand has the mode of the arithmetic already */
1075 ir_mode *mode = get_ir_mode(type);
1076 left = create_conv(dbgi, left, mode);
1077 ir_node *res = func(dbgi, left, right, mode);
1082 static ir_node *pointer_arithmetic(ir_node *const pointer,
1085 dbg_info *const dbgi,
1086 const create_arithmetic_func func)
1088 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1089 type_t *const points_to = pointer_type->points_to;
1090 const unsigned elem_size = get_type_size(points_to);
1092 assert(elem_size >= 1);
1093 if (elem_size > 1) {
1094 integer = create_conv(dbgi, integer, mode_Is);
1095 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1096 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1100 ir_mode *const mode = get_ir_mode(type);
1101 return func(dbgi, pointer, integer, mode);
1104 static ir_node *create_arithmetic_assign_binop(
1105 const binary_expression_t *expression, create_arithmetic_func func)
1107 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1108 type_t *const type = expression->expression.datatype;
1111 if (type->type == TYPE_POINTER) {
1112 ir_node *const pointer = expression_to_firm(expression->left);
1113 ir_node * integer = expression_to_firm(expression->right);
1114 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1116 value = create_arithmetic_binop(expression, func);
1119 ir_mode *const mode = get_ir_mode(type);
1120 value = create_conv(dbgi, value, mode);
1121 set_value_for_expression(expression->left, value);
1126 static ir_node *create_add(const binary_expression_t *expression)
1128 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1129 ir_node *left = expression_to_firm(expression->left);
1130 ir_node *right = expression_to_firm(expression->right);
1131 type_t *type = expression->expression.datatype;
1133 expression_t *expr_left = expression->left;
1134 expression_t *expr_right = expression->right;
1135 type_t *type_left = skip_typeref(expr_left->datatype);
1136 type_t *type_right = skip_typeref(expr_right->datatype);
1138 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1139 ir_mode *const mode = get_ir_mode(type);
1140 return new_d_Add(dbgi, left, right, mode);
1143 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1144 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1146 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1147 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1151 static ir_node *create_sub(const binary_expression_t *expression)
1153 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1154 expression_t *const expr_left = expression->left;
1155 expression_t *const expr_right = expression->right;
1156 ir_node *const left = expression_to_firm(expr_left);
1157 ir_node *const right = expression_to_firm(expr_right);
1158 type_t *const type = expression->expression.datatype;
1159 type_t *const type_left = skip_typeref(expr_left->datatype);
1160 type_t *const type_right = skip_typeref(expr_right->datatype);
1162 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1163 ir_mode *const mode = get_ir_mode(type);
1164 return new_d_Sub(dbgi, left, right, mode);
1165 } else if (type_left->type == TYPE_POINTER && type_right->type == TYPE_POINTER) {
1166 const pointer_type_t *const ptr_type = (const pointer_type_t*)type_left;
1167 const unsigned elem_size = get_type_size(ptr_type->points_to);
1168 ir_mode *const mode = get_ir_mode(type);
1169 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1170 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1171 ir_node *const no_mem = new_NoMem();
1172 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1173 op_pin_state_floats);
1174 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1177 assert(type_left->type == TYPE_POINTER);
1178 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1181 static ir_node *create_shift(const binary_expression_t *expression)
1183 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1184 ir_node *left = expression_to_firm(expression->left);
1185 ir_node *right = expression_to_firm(expression->right);
1186 type_t *type = expression->expression.datatype;
1187 ir_mode *mode = get_ir_mode(type);
1189 /* firm always wants the shift count to be unsigned */
1190 right = create_conv(dbgi, right, mode_Iu);
1194 switch(expression->type) {
1195 case BINEXPR_SHIFTLEFT_ASSIGN:
1196 case BINEXPR_SHIFTLEFT:
1197 res = new_d_Shl(dbgi, left, right, mode);
1199 case BINEXPR_SHIFTRIGHT_ASSIGN:
1200 case BINEXPR_SHIFTRIGHT: {
1201 expression_t *expr_left = expression->left;
1202 type_t *type_left = skip_typeref(expr_left->datatype);
1204 if(is_type_signed(type_left)) {
1205 res = new_d_Shrs(dbgi, left, right, mode);
1207 res = new_d_Shr(dbgi, left, right, mode);
1212 panic("create shift op called for non-shift op");
1219 static ir_node *create_divmod(const binary_expression_t *expression)
1221 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1222 ir_node *left = expression_to_firm(expression->left);
1223 ir_node *right = expression_to_firm(expression->right);
1224 ir_node *pin = new_Pin(new_NoMem());
1225 /* be careful with the modes, because in arithmetic assign nodes only
1226 * the right operand has the mode of the arithmetic already */
1227 type_t *type = expression->right->datatype;
1228 ir_mode *mode = get_ir_mode(type);
1229 left = create_conv(dbgi, left, mode);
1233 switch (expression->type) {
1235 case BINEXPR_DIV_ASSIGN:
1236 if(mode_is_float(mode)) {
1237 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1238 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1240 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1241 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1246 case BINEXPR_MOD_ASSIGN:
1247 assert(!mode_is_float(mode));
1248 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1249 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1252 default: panic("unexpected binary expression type in create_divmod()");
1258 static ir_node *create_arithmetic_assign_divmod(
1259 const binary_expression_t *expression)
1261 ir_node * value = create_divmod(expression);
1262 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1263 type_t *const type = expression->expression.datatype;
1264 ir_mode *const mode = get_ir_mode(type);
1266 assert(type->type != TYPE_POINTER);
1268 value = create_conv(dbgi, value, mode);
1269 set_value_for_expression(expression->left, value);
1274 static ir_node *create_arithmetic_assign_shift(
1275 const binary_expression_t *expression)
1277 ir_node * value = create_shift(expression);
1278 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1279 type_t *const type = expression->expression.datatype;
1280 ir_mode *const mode = get_ir_mode(type);
1282 value = create_conv(dbgi, value, mode);
1283 set_value_for_expression(expression->left, value);
1288 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1290 binary_expression_type_t type = expression->type;
1293 case BINEXPR_NOTEQUAL:
1295 case BINEXPR_LESSEQUAL:
1296 case BINEXPR_GREATER:
1297 case BINEXPR_GREATEREQUAL: {
1298 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1299 ir_node *left = expression_to_firm(expression->left);
1300 ir_node *right = expression_to_firm(expression->right);
1301 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1302 long pnc = get_pnc(type);
1303 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1306 case BINEXPR_ASSIGN: {
1307 ir_node *right = expression_to_firm(expression->right);
1308 set_value_for_expression(expression->left, right);
1312 return create_add(expression);
1314 return create_sub(expression);
1316 return create_arithmetic_binop(expression, new_d_Mul);
1317 case BINEXPR_BITWISE_AND:
1318 return create_arithmetic_binop(expression, new_d_And);
1319 case BINEXPR_BITWISE_OR:
1320 return create_arithmetic_binop(expression, new_d_Or);
1321 case BINEXPR_BITWISE_XOR:
1322 return create_arithmetic_binop(expression, new_d_Eor);
1323 case BINEXPR_SHIFTLEFT:
1324 case BINEXPR_SHIFTRIGHT:
1325 return create_shift(expression);
1328 return create_divmod(expression);
1329 case BINEXPR_LOGICAL_AND:
1330 case BINEXPR_LOGICAL_OR:
1331 return create_lazy_op(expression);
1333 expression_to_firm(expression->left);
1334 return expression_to_firm(expression->right);
1335 case BINEXPR_ADD_ASSIGN:
1336 return create_arithmetic_assign_binop(expression, new_d_Add);
1337 case BINEXPR_SUB_ASSIGN:
1338 return create_arithmetic_assign_binop(expression, new_d_Sub);
1339 case BINEXPR_MUL_ASSIGN:
1340 return create_arithmetic_assign_binop(expression, new_d_Mul);
1341 case BINEXPR_DIV_ASSIGN:
1342 return create_arithmetic_assign_divmod(expression);
1343 case BINEXPR_BITWISE_AND_ASSIGN:
1344 return create_arithmetic_assign_binop(expression, new_d_And);
1345 case BINEXPR_BITWISE_OR_ASSIGN:
1346 return create_arithmetic_assign_binop(expression, new_d_Or);
1347 case BINEXPR_BITWISE_XOR_ASSIGN:
1348 return create_arithmetic_assign_binop(expression, new_d_Eor);
1349 case BINEXPR_SHIFTLEFT_ASSIGN:
1350 case BINEXPR_SHIFTRIGHT_ASSIGN:
1351 return create_arithmetic_assign_shift(expression);
1353 panic("TODO binexpr type");
1357 static ir_node *array_access_addr(const array_access_expression_t *expression)
1359 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1363 type_t *type_left = skip_typeref(expression->array_ref->datatype);
1364 type_t *type_right = skip_typeref(expression->index->datatype);
1366 if(type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1367 base_addr = expression_to_firm(expression->array_ref);
1368 offset = expression_to_firm(expression->index);
1370 assert(type_right->type == TYPE_POINTER
1371 || type_right->type == TYPE_ARRAY);
1372 base_addr = expression_to_firm(expression->index);
1373 offset = expression_to_firm(expression->array_ref);
1375 offset = create_conv(dbgi, offset, mode_Iu);
1377 unsigned elem_size = get_type_size(expression->expression.datatype);
1378 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1379 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1381 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1386 static ir_node *array_access_to_firm(
1387 const array_access_expression_t *expression)
1389 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1390 ir_node *addr = array_access_addr(expression);
1391 type_t *type = revert_automatic_type_conversion(
1392 (const expression_t*) expression);
1393 type = skip_typeref(type);
1394 ir_type *irtype = get_ir_type(type);
1396 return deref_address(irtype, addr, dbgi);
1399 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1401 type_t *type = expression->type;
1403 type = expression->size_expression->datatype;
1404 assert(type != NULL);
1407 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1408 unsigned size = get_type_size(type);
1409 ir_node *size_node = new_Const_long(mode, size);
1414 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1416 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1418 ir_node *cur_block = get_cur_block();
1420 /* create the true block */
1421 ir_node *true_block = new_immBlock();
1423 ir_node *true_val = expression_to_firm(expression->true_expression);
1424 ir_node *true_jmp = new_Jmp();
1426 /* create the false block */
1427 ir_node *false_block = new_immBlock();
1429 ir_node *false_val = expression_to_firm(expression->false_expression);
1430 ir_node *false_jmp = new_Jmp();
1432 /* create the condition evaluation */
1433 set_cur_block(cur_block);
1434 create_condition_evaluation(expression->condition, true_block, false_block);
1435 mature_immBlock(true_block);
1436 mature_immBlock(false_block);
1438 /* create the common block */
1439 ir_node *common_block = new_immBlock();
1440 add_immBlock_pred(common_block, true_jmp);
1441 add_immBlock_pred(common_block, false_jmp);
1442 mature_immBlock(common_block);
1444 /* TODO improve static semantics, so either both or no values are NULL */
1445 if (true_val == NULL || false_val == NULL) return NULL;
1447 ir_node *in[2] = { true_val, false_val };
1448 ir_mode *mode = get_irn_mode(true_val);
1449 assert(get_irn_mode(false_val) == mode);
1450 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1455 static ir_node *select_addr(const select_expression_t *expression)
1457 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1459 ir_node *compound_addr = expression_to_firm(expression->compound);
1461 declaration_t *entry = expression->compound_entry;
1462 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1463 ir_entity *entity = entry->v.entity;
1465 assert(entity != NULL);
1467 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1472 static ir_node *select_to_firm(const select_expression_t *expression)
1474 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1475 ir_node *addr = select_addr(expression);
1476 type_t *type = revert_automatic_type_conversion(
1477 (const expression_t*) expression);
1478 type = skip_typeref(type);
1479 ir_type *irtype = get_ir_type(type);
1481 return deref_address(irtype, addr, dbgi);
1484 /* Values returned by __builtin_classify_type. */
1485 typedef enum gcc_type_class
1491 enumeral_type_class,
1494 reference_type_class,
1498 function_type_class,
1509 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1511 const type_t *const type = expr->type_expression->datatype;
1517 const atomic_type_t *const atomic_type = (const atomic_type_t*)type;
1518 switch (atomic_type->atype) {
1519 // should not be reached
1520 case ATOMIC_TYPE_INVALID:
1524 // gcc cannot do that
1525 case ATOMIC_TYPE_VOID:
1526 tc = void_type_class;
1529 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1530 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1531 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1532 case ATOMIC_TYPE_SHORT:
1533 case ATOMIC_TYPE_USHORT:
1534 case ATOMIC_TYPE_INT:
1535 case ATOMIC_TYPE_UINT:
1536 case ATOMIC_TYPE_LONG:
1537 case ATOMIC_TYPE_ULONG:
1538 case ATOMIC_TYPE_LONGLONG:
1539 case ATOMIC_TYPE_ULONGLONG:
1540 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1541 tc = integer_type_class;
1544 case ATOMIC_TYPE_FLOAT:
1545 case ATOMIC_TYPE_DOUBLE:
1546 case ATOMIC_TYPE_LONG_DOUBLE:
1547 tc = real_type_class;
1550 #ifdef PROVIDE_COMPLEX
1551 case ATOMIC_TYPE_FLOAT_COMPLEX:
1552 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1553 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1554 tc = complex_type_class;
1556 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1557 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1558 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1559 tc = complex_type_class;
1564 panic("Unimplemented case in classify_type_to_firm().");
1569 case TYPE_ARRAY: // gcc handles this as pointer
1570 case TYPE_FUNCTION: // gcc handles this as pointer
1571 case TYPE_POINTER: tc = pointer_type_class; break;
1572 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1573 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1575 // gcc handles this as integer
1576 case TYPE_ENUM: tc = integer_type_class; break;
1579 panic("Unimplemented case in classify_type_to_firm().");
1582 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1583 ir_mode *const mode = mode_Is;
1584 tarval *const tv = new_tarval_from_long(tc, mode);
1585 return new_d_Const(dbgi, mode, tv);
1588 static ir_node *function_name_to_firm(const string_literal_t *const expr)
1590 if (current_function_name == NULL) {
1591 const source_position_t *const src_pos =
1592 &expr->expression.source_position;
1593 const char *const name = current_function_decl->symbol->string;
1594 current_function_name = string_to_firm(src_pos, "__func__", name);
1597 return current_function_name;
1600 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1602 statement_t *statement = expr->statement;
1604 assert(statement->type == STATEMENT_COMPOUND);
1605 return compound_statement_to_firm((compound_statement_t*) statement);
1608 static ir_node *dereference_addr(const unary_expression_t *const expression)
1610 assert(expression->type == UNEXPR_DEREFERENCE);
1611 return expression_to_firm(expression->value);
1614 static ir_node *expression_to_addr(const expression_t *expression)
1616 switch(expression->type) {
1617 case EXPR_REFERENCE:
1618 return reference_addr((const reference_expression_t*) expression);
1619 case EXPR_ARRAY_ACCESS:
1620 return array_access_addr((const array_access_expression_t*) expression);
1622 return select_addr((const select_expression_t*) expression);
1624 const unary_expression_t *const unary_expr =
1625 (const unary_expression_t*)expression;
1626 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1627 return dereference_addr(unary_expr);
1634 panic("trying to get address of non-lvalue");
1637 static ir_node *_expression_to_firm(const expression_t *expression)
1639 switch(expression->type) {
1641 return const_to_firm((const const_t*) expression);
1642 case EXPR_STRING_LITERAL:
1643 return string_literal_to_firm((const string_literal_t*) expression);
1644 case EXPR_REFERENCE:
1645 return reference_expression_to_firm(
1646 (const reference_expression_t*) expression);
1648 return call_expression_to_firm((const call_expression_t*) expression);
1650 return unary_expression_to_firm((const unary_expression_t*) expression);
1652 return binary_expression_to_firm(
1653 (const binary_expression_t*) expression);
1654 case EXPR_ARRAY_ACCESS:
1655 return array_access_to_firm(
1656 (const array_access_expression_t*) expression);
1658 return sizeof_to_firm((const sizeof_expression_t*) expression);
1659 case EXPR_CONDITIONAL:
1660 return conditional_to_firm((const conditional_expression_t*)expression);
1662 return select_to_firm((const select_expression_t*) expression);
1663 case EXPR_CLASSIFY_TYPE:
1664 return classify_type_to_firm((const classify_type_expression_t*)expression);
1666 case EXPR_PRETTY_FUNCTION:
1667 return function_name_to_firm((const string_literal_t*)expression);
1668 case EXPR_STATEMENT:
1669 return statement_expression_to_firm(
1670 (const statement_expression_t*) expression);
1673 case EXPR_BUILTIN_SYMBOL:
1674 panic("unimplemented expression found");
1680 panic("invalid expression found");
1683 static ir_node *expression_to_firm(const expression_t *expression)
1685 ir_node *res = _expression_to_firm(expression);
1687 if(res != NULL && get_irn_mode(res) == mode_b) {
1688 ir_mode *mode = get_ir_mode(expression->datatype);
1689 res = create_conv(NULL, res, mode);
1695 static ir_node *expression_to_modeb(const expression_t *expression)
1697 ir_node *res = _expression_to_firm(expression);
1698 res = create_conv(NULL, res, mode_b);
1704 * create a short-circuit expression evaluation that tries to construct
1705 * efficient control flow structures for &&, || and ! expressions
1707 static void create_condition_evaluation(const expression_t *expression,
1708 ir_node *true_block,
1709 ir_node *false_block)
1711 switch(expression->type) {
1713 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1714 if(unary_expression->type == UNEXPR_NOT) {
1715 create_condition_evaluation(unary_expression->value, false_block,
1722 binary_expression_t *binary_expression
1723 = (binary_expression_t*) expression;
1724 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1725 ir_node *cur_block = get_cur_block();
1726 ir_node *extra_block = new_immBlock();
1727 set_cur_block(cur_block);
1728 create_condition_evaluation(binary_expression->left, extra_block,
1730 mature_immBlock(extra_block);
1731 set_cur_block(extra_block);
1732 create_condition_evaluation(binary_expression->right, true_block,
1736 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1737 ir_node *cur_block = get_cur_block();
1738 ir_node *extra_block = new_immBlock();
1739 set_cur_block(cur_block);
1740 create_condition_evaluation(binary_expression->left, true_block,
1742 mature_immBlock(extra_block);
1743 set_cur_block(extra_block);
1744 create_condition_evaluation(binary_expression->right, true_block,
1754 dbg_info *dbgi = get_dbg_info(&expression->source_position);
1755 ir_node *condition = expression_to_modeb(expression);
1756 ir_node *cond = new_d_Cond(dbgi, condition);
1757 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1758 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1760 add_immBlock_pred(true_block, true_proj);
1761 add_immBlock_pred(false_block, false_proj);
1763 set_cur_block(NULL);
1767 static void return_statement_to_firm(return_statement_t *statement)
1769 if(get_cur_block() == NULL)
1773 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1775 ir_node *value = NULL;
1776 if(statement->return_value != NULL) {
1777 value = expression_to_firm(statement->return_value);
1782 if(get_method_n_ress(func_irtype) > 0) {
1784 ir_type *res_type = get_method_res_type(func_irtype, 0);
1785 ir_mode *mode = get_type_mode(res_type);
1786 value = new_Unknown(mode);
1795 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1796 ir_node *store = get_store();
1797 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1799 ir_node *end_block = get_irg_end_block(current_ir_graph);
1800 add_immBlock_pred(end_block, ret);
1802 set_cur_block(NULL);
1805 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1807 if(get_cur_block() == NULL)
1810 return expression_to_firm(statement->expression);
1813 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1815 ir_node *result = NULL;
1816 statement_t *statement = compound->statements;
1817 for( ; statement != NULL; statement = statement->next) {
1818 //context2firm(&statement->context);
1820 if(statement->next == NULL && statement->type == STATEMENT_EXPRESSION) {
1821 result = expression_statement_to_firm(
1822 (expression_statement_t*) statement);
1825 statement_to_firm(statement);
1831 static void if_statement_to_firm(if_statement_t *statement)
1833 ir_node *cur_block = get_cur_block();
1835 ir_node *fallthrough_block = new_immBlock();
1837 /* the true (blocks) */
1838 ir_node *true_block;
1839 if (statement->true_statement != NULL) {
1840 true_block = new_immBlock();
1841 statement_to_firm(statement->true_statement);
1842 if(get_cur_block() != NULL) {
1843 ir_node *jmp = new_Jmp();
1844 add_immBlock_pred(fallthrough_block, jmp);
1847 true_block = fallthrough_block;
1850 /* the false (blocks) */
1851 ir_node *false_block;
1852 if(statement->false_statement != NULL) {
1853 false_block = new_immBlock();
1855 statement_to_firm(statement->false_statement);
1856 if(get_cur_block() != NULL) {
1857 ir_node *jmp = new_Jmp();
1858 add_immBlock_pred(fallthrough_block, jmp);
1861 false_block = fallthrough_block;
1864 /* create the condition */
1865 if(cur_block != NULL) {
1866 set_cur_block(cur_block);
1867 create_condition_evaluation(statement->condition, true_block,
1871 mature_immBlock(true_block);
1872 if(false_block != fallthrough_block) {
1873 mature_immBlock(false_block);
1875 mature_immBlock(fallthrough_block);
1877 set_cur_block(fallthrough_block);
1880 static void while_statement_to_firm(while_statement_t *statement)
1882 ir_node *jmp = NULL;
1883 if(get_cur_block() != NULL) {
1887 /* create the header block */
1888 ir_node *header_block = new_immBlock();
1890 add_immBlock_pred(header_block, jmp);
1893 /* the false block */
1894 ir_node *false_block = new_immBlock();
1897 ir_node *body_block;
1898 if (statement->body != NULL) {
1899 ir_node *old_continue_label = continue_label;
1900 ir_node *old_break_label = break_label;
1901 continue_label = header_block;
1902 break_label = false_block;
1904 body_block = new_immBlock();
1905 statement_to_firm(statement->body);
1907 assert(continue_label == header_block);
1908 assert(break_label == false_block);
1909 continue_label = old_continue_label;
1910 break_label = old_break_label;
1912 if(get_cur_block() != NULL) {
1913 ir_node *jmp = new_Jmp();
1914 add_immBlock_pred(header_block, jmp);
1917 body_block = header_block;
1920 /* create the condition */
1921 set_cur_block(header_block);
1923 create_condition_evaluation(statement->condition, body_block, false_block);
1924 mature_immBlock(body_block);
1925 mature_immBlock(false_block);
1926 mature_immBlock(header_block);
1928 set_cur_block(false_block);
1931 static void do_while_statement_to_firm(do_while_statement_t *statement)
1933 ir_node *jmp = NULL;
1934 if(get_cur_block() != NULL) {
1938 /* create the header block */
1939 ir_node *header_block = new_immBlock();
1941 /* the false block */
1942 ir_node *false_block = new_immBlock();
1945 ir_node *body_block = new_immBlock();
1947 add_immBlock_pred(body_block, jmp);
1950 if (statement->body != NULL) {
1951 ir_node *old_continue_label = continue_label;
1952 ir_node *old_break_label = break_label;
1953 continue_label = header_block;
1954 break_label = false_block;
1956 statement_to_firm(statement->body);
1958 assert(continue_label == header_block);
1959 assert(break_label == false_block);
1960 continue_label = old_continue_label;
1961 break_label = old_break_label;
1963 if (get_cur_block() == NULL) {
1964 mature_immBlock(header_block);
1965 mature_immBlock(body_block);
1966 mature_immBlock(false_block);
1971 ir_node *body_jmp = new_Jmp();
1972 add_immBlock_pred(header_block, body_jmp);
1973 mature_immBlock(header_block);
1975 /* create the condition */
1976 set_cur_block(header_block);
1978 create_condition_evaluation(statement->condition, body_block, false_block);
1979 mature_immBlock(body_block);
1980 mature_immBlock(false_block);
1981 mature_immBlock(header_block);
1983 set_cur_block(false_block);
1986 static void for_statement_to_firm(for_statement_t *statement)
1988 ir_node *jmp = NULL;
1989 if (get_cur_block() != NULL) {
1990 if(statement->initialisation != NULL) {
1991 expression_to_firm(statement->initialisation);
1996 /* create the step block */
1997 ir_node *const step_block = new_immBlock();
1998 if (statement->step != NULL) {
1999 expression_to_firm(statement->step);
2001 ir_node *const step_jmp = new_Jmp();
2003 /* create the header block */
2004 ir_node *const header_block = new_immBlock();
2006 add_immBlock_pred(header_block, jmp);
2008 add_immBlock_pred(header_block, step_jmp);
2010 /* the false block */
2011 ir_node *const false_block = new_immBlock();
2014 ir_node * body_block;
2015 if (statement->body != NULL) {
2016 ir_node *const old_continue_label = continue_label;
2017 ir_node *const old_break_label = break_label;
2018 continue_label = step_block;
2019 break_label = false_block;
2021 body_block = new_immBlock();
2022 statement_to_firm(statement->body);
2024 assert(continue_label == step_block);
2025 assert(break_label == false_block);
2026 continue_label = old_continue_label;
2027 break_label = old_break_label;
2029 if (get_cur_block() != NULL) {
2030 ir_node *const jmp = new_Jmp();
2031 add_immBlock_pred(step_block, jmp);
2034 body_block = step_block;
2037 /* create the condition */
2038 set_cur_block(header_block);
2039 if (statement->condition != NULL) {
2040 create_condition_evaluation(statement->condition, body_block,
2043 keep_alive(header_block);
2044 ir_node *jmp = new_Jmp();
2045 add_immBlock_pred(body_block, jmp);
2048 mature_immBlock(body_block);
2049 mature_immBlock(false_block);
2050 mature_immBlock(step_block);
2051 mature_immBlock(header_block);
2052 mature_immBlock(false_block);
2054 set_cur_block(false_block);
2057 static void create_declaration_entity(declaration_t *declaration,
2058 declaration_type_t declaration_type,
2059 ir_type *parent_type)
2061 ident *id = new_id_from_str(declaration->symbol->string);
2062 ir_type *irtype = get_ir_type(declaration->type);
2063 ir_entity *entity = new_entity(parent_type, id, irtype);
2064 set_entity_ld_ident(entity, id);
2066 declaration->declaration_type = declaration_type;
2067 declaration->v.entity = entity;
2068 set_entity_variability(entity, variability_uninitialized);
2069 /* TODO: visibility? */
2072 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2074 enum compound_graph_entry_type_t {
2075 COMPOUND_GRAPH_ENTRY_ARRAY,
2076 COMPOUND_GRAPH_ENTRY_COMPOUND
2079 struct compound_graph_path_entry_t {
2085 compound_graph_path_entry_t *prev;
2088 static void create_initializer_object(initializer_t *initializer, type_t *type,
2089 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2091 static compound_graph_path *create_compound_path(ir_type *type,
2092 compound_graph_path_entry_t *entry, int len)
2094 compound_graph_path *path = new_compound_graph_path(type, len);
2097 for( ; entry != NULL; entry = entry->prev, --i) {
2099 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2100 set_compound_graph_path_node(path, i, entry->v.entity);
2102 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2103 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2111 static void create_initializer_value(initializer_value_t *initializer,
2113 compound_graph_path_entry_t *entry,
2116 ir_node *node = expression_to_firm(initializer->value);
2117 ir_type *type = get_entity_type(entity);
2118 compound_graph_path *path = create_compound_path(type, entry, len);
2119 add_compound_ent_value_w_path(entity, node, path);
2122 static void create_initializer_compound(initializer_list_t *initializer,
2123 compound_type_t *type,
2125 compound_graph_path_entry_t *last_entry,
2128 declaration_t *compound_declaration = type->declaration;
2130 declaration_t *compound_entry = compound_declaration->context.declarations;
2132 compound_graph_path_entry_t entry;
2133 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2134 entry.prev = last_entry;
2138 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2139 if(compound_entry->symbol == NULL)
2141 if(compound_entry->namespc != NAMESPACE_NORMAL)
2144 if(i >= initializer->len)
2147 entry.v.entity = compound_entry->v.entity;
2149 initializer_t *sub_initializer = initializer->initializers[i];
2151 assert(compound_entry != NULL);
2152 assert(compound_entry->declaration_type
2153 == DECLARATION_TYPE_COMPOUND_MEMBER);
2155 if(sub_initializer->type == INITIALIZER_VALUE) {
2156 create_initializer_value(&sub_initializer->value,
2157 entity, &entry, len);
2159 type_t *type = skip_typeref(compound_entry->type);
2160 create_initializer_object(sub_initializer, type, entity, &entry,
2168 static void create_initializer_array(initializer_list_t *initializer,
2169 array_type_t *type, ir_entity *entity,
2170 compound_graph_path_entry_t *last_entry,
2173 type_t *element_type = type->element_type;
2174 element_type = skip_typeref(element_type);
2176 compound_graph_path_entry_t entry;
2177 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2178 entry.prev = last_entry;
2182 for(i = 0; i < initializer->len; ++i) {
2183 entry.v.array_index = i;
2185 initializer_t *sub_initializer = initializer->initializers[i];
2187 if(sub_initializer->type == INITIALIZER_VALUE) {
2188 create_initializer_value(&sub_initializer->value,
2189 entity, &entry, len);
2191 create_initializer_object(sub_initializer, element_type, entity,
2197 /* TODO: initializer rest... */
2198 if(type->size_expression != NULL) {
2199 size_t array_len = fold_constant(type->size_expression);
2200 for( ; i < array_len; ++i) {
2207 static void create_initializer_string(initializer_string_t *initializer,
2208 array_type_t *type, ir_entity *entity,
2209 compound_graph_path_entry_t *last_entry,
2212 type_t *element_type = type->element_type;
2213 element_type = skip_typeref(element_type);
2215 compound_graph_path_entry_t entry;
2216 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2217 entry.prev = last_entry;
2220 ir_type *irtype = get_entity_type(entity);
2221 size_t arr_len = get_array_type_size(type);
2222 const char *p = initializer->string;
2224 for(i = 0; i < arr_len; ++i, ++p) {
2225 entry.v.array_index = i;
2227 ir_node *node = new_Const_long(mode_Bs, *p);
2228 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2229 add_compound_ent_value_w_path(entity, node, path);
2236 static void create_initializer_object(initializer_t *initializer, type_t *type,
2237 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2239 if(type->type == TYPE_ARRAY) {
2240 array_type_t *array_type = (array_type_t*) type;
2242 if(initializer->type == INITIALIZER_STRING) {
2243 initializer_string_t *string = &initializer->string;
2244 create_initializer_string(string, array_type, entity, entry, len);
2246 assert(initializer->type == INITIALIZER_LIST);
2247 initializer_list_t *list = &initializer->list;
2248 create_initializer_array(list, array_type, entity, entry, len);
2251 assert(initializer->type == INITIALIZER_LIST);
2252 initializer_list_t *list = &initializer->list;
2254 assert(type->type == TYPE_COMPOUND_STRUCT
2255 || type->type == TYPE_COMPOUND_UNION);
2256 compound_type_t *compound_type = (compound_type_t*) type;
2257 create_initializer_compound(list, compound_type, entity, entry, len);
2261 static void create_initializer_local_variable_entity(declaration_t *declaration)
2263 initializer_t *initializer = declaration->init.initializer;
2264 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2265 ir_entity *entity = declaration->v.entity;
2266 ir_node *memory = get_store();
2267 ir_node *nomem = new_NoMem();
2268 ir_node *frame = get_irg_frame(current_ir_graph);
2269 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2271 if(is_atomic_entity(entity)) {
2272 assert(initializer->type == INITIALIZER_VALUE);
2273 initializer_value_t *initializer_value = &initializer->value;
2275 ir_node *value = expression_to_firm(initializer_value->value);
2276 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2277 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2278 set_store(store_mem);
2282 /* create a "template" entity which is copied to the entity on the stack */
2283 ident *id = unique_ident("initializer");
2284 ir_type *irtype = get_ir_type(declaration->type);
2285 ir_type *global_type = get_glob_type();
2286 ir_entity *init_entity = new_entity(global_type, id, irtype);
2287 set_entity_ld_ident(init_entity, id);
2289 set_entity_variability(init_entity, variability_initialized);
2290 set_entity_visibility(init_entity, visibility_local);
2292 ir_graph *old_current_ir_graph = current_ir_graph;
2293 current_ir_graph = get_const_code_irg();
2295 type_t *type = skip_typeref(declaration->type);
2296 create_initializer_object(initializer, type, init_entity, NULL, 0);
2298 assert(current_ir_graph == get_const_code_irg());
2299 current_ir_graph = old_current_ir_graph;
2301 ir_node *src_addr = create_symconst(dbgi, init_entity);
2302 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2304 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2305 set_store(copyb_mem);
2308 static void create_initializer(declaration_t *declaration)
2310 initializer_t *initializer = declaration->init.initializer;
2311 if(initializer == NULL)
2314 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2315 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2316 create_initializer_local_variable_entity(declaration);
2320 if(initializer->type == INITIALIZER_VALUE) {
2321 initializer_value_t *initializer_value = &initializer->value;
2323 ir_node *value = expression_to_firm(initializer_value->value);
2325 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2326 set_value(declaration->v.value_number, value);
2328 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2330 ir_entity *entity = declaration->v.entity;
2332 set_entity_variability(entity, variability_initialized);
2333 set_atomic_ent_value(entity, value);
2336 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2337 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2338 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2340 ir_entity *entity = declaration->v.entity;
2341 set_entity_variability(entity, variability_initialized);
2343 type_t *type = skip_typeref(declaration->type);
2344 create_initializer_object(initializer, type, entity, NULL, 0);
2348 static void create_local_variable(declaration_t *declaration)
2350 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2352 bool needs_entity = declaration->address_taken;
2353 type_t *type = skip_typeref(declaration->type);
2355 if(type->type == TYPE_ARRAY
2356 || type->type == TYPE_COMPOUND_STRUCT
2357 || type->type == TYPE_COMPOUND_UNION) {
2358 needs_entity = true;
2362 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2363 create_declaration_entity(declaration,
2364 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2367 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2368 declaration->v.value_number = next_value_number_function;
2369 ++next_value_number_function;
2372 create_initializer(declaration);
2375 static void create_local_static_variable(declaration_t *declaration)
2377 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2379 type_t *type = skip_typeref(declaration->type);
2380 ir_type *global_type = get_glob_type();
2381 ident *id = unique_ident(declaration->symbol->string);
2382 ir_type *irtype = get_ir_type(type);
2383 ir_entity *entity = new_entity(global_type, id, irtype);
2384 set_entity_ld_ident(entity, id);
2386 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2387 declaration->v.entity = entity;
2388 set_entity_variability(entity, variability_uninitialized);
2389 set_entity_visibility(entity, visibility_local);
2391 ir_graph *old_current_ir_graph = current_ir_graph;
2392 current_ir_graph = get_const_code_irg();
2394 create_initializer(declaration);
2396 assert(current_ir_graph == get_const_code_irg());
2397 current_ir_graph = old_current_ir_graph;
2400 static void declaration_statement_to_firm(declaration_statement_t *statement)
2402 declaration_t *declaration = statement->declarations_begin;
2403 declaration_t *end = statement->declarations_end->next;
2404 for( ; declaration != end; declaration = declaration->next) {
2405 type_t *type = declaration->type;
2407 switch(declaration->storage_class) {
2408 case STORAGE_CLASS_TYPEDEF:
2410 case STORAGE_CLASS_STATIC:
2411 create_local_static_variable(declaration);
2413 case STORAGE_CLASS_ENUM_ENTRY:
2414 panic("enum entry declaration in local block found");
2415 case STORAGE_CLASS_EXTERN:
2416 panic("extern declaration in local block found");
2417 case STORAGE_CLASS_NONE:
2418 case STORAGE_CLASS_AUTO:
2419 case STORAGE_CLASS_REGISTER:
2420 if(type->type == TYPE_FUNCTION) {
2421 panic("nested functions not supported yet");
2423 create_local_variable(declaration);
2427 panic("invalid storage class found");
2431 static void create_jump_statement(const statement_t *statement,
2432 ir_node *target_block)
2434 if(get_cur_block() == NULL)
2437 dbg_info *dbgi = get_dbg_info(&statement->source_position);
2438 ir_node *jump = new_d_Jmp(dbgi);
2439 add_immBlock_pred(target_block, jump);
2441 set_cur_block(NULL);
2444 static void switch_statement_to_firm(const switch_statement_t *statement)
2446 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2448 ir_node *expression = expression_to_firm(statement->expression);
2449 ir_node *cond = new_d_Cond(dbgi, expression);
2450 ir_node *break_block = new_immBlock();
2452 set_cur_block(NULL);
2454 ir_node *const old_switch_cond = current_switch_cond;
2455 ir_node *const old_break_label = break_label;
2456 const bool old_saw_default_label = saw_default_label;
2457 current_switch_cond = cond;
2458 break_label = break_block;
2460 statement_to_firm(statement->body);
2462 if(get_cur_block() != NULL) {
2463 ir_node *jmp = new_Jmp();
2464 add_immBlock_pred(break_block, jmp);
2467 if (!saw_default_label) {
2468 set_cur_block(get_nodes_block(cond));
2469 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2470 MAGIC_DEFAULT_PN_NUMBER);
2471 add_immBlock_pred(break_block, proj);
2474 assert(current_switch_cond == cond);
2475 assert(break_label == break_block);
2476 current_switch_cond = old_switch_cond;
2477 break_label = old_break_label;
2478 saw_default_label = old_saw_default_label;
2480 mature_immBlock(break_block);
2481 set_cur_block(break_block);
2484 static long fold_constant(const expression_t *expression)
2486 ir_graph *old_current_ir_graph = current_ir_graph;
2487 current_ir_graph = get_const_code_irg();
2489 ir_node *cnst = expression_to_firm(expression);
2490 if(!is_Const(cnst)) {
2491 panic("couldn't fold constantl");
2493 tarval *tv = get_Const_tarval(cnst);
2494 if(!tarval_is_long(tv)) {
2495 panic("folded constant not an integer");
2498 long res = get_tarval_long(tv);
2500 current_ir_graph = old_current_ir_graph;
2504 static void case_label_to_firm(const case_label_statement_t *statement)
2506 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2508 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2510 /* let's create a node and hope firm constant folding creates a Const
2513 set_cur_block(get_nodes_block(current_switch_cond));
2514 if(statement->expression) {
2515 long pn = fold_constant(statement->expression);
2516 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2517 /* oops someone detected our cheating... */
2518 panic("magic default pn used");
2520 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2522 saw_default_label = true;
2523 proj = new_d_defaultProj(dbgi, current_switch_cond,
2524 MAGIC_DEFAULT_PN_NUMBER);
2527 ir_node *block = new_immBlock();
2528 if (fallthrough != NULL) {
2529 add_immBlock_pred(block, fallthrough);
2531 add_immBlock_pred(block, proj);
2532 mature_immBlock(block);
2534 statement_to_firm(statement->label_statement);
2537 static ir_node *get_label_block(declaration_t *label)
2539 assert(label->namespc == NAMESPACE_LABEL);
2541 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2542 return label->v.block;
2544 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2546 ir_node *old_cur_block = get_cur_block();
2547 ir_node *block = new_immBlock();
2548 set_cur_block(old_cur_block);
2550 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2551 label->v.block = block;
2553 ARR_APP1(ir_node *, imature_blocks, block);
2558 static void label_to_firm(const label_statement_t *statement)
2560 ir_node *block = get_label_block(statement->label);
2562 if(get_cur_block() != NULL) {
2563 ir_node *jmp = new_Jmp();
2564 add_immBlock_pred(block, jmp);
2567 set_cur_block(block);
2570 statement_to_firm(statement->label_statement);
2573 static void goto_to_firm(const goto_statement_t *statement)
2575 if(get_cur_block() == NULL)
2578 ir_node *block = get_label_block(statement->label);
2579 ir_node *jmp = new_Jmp();
2580 add_immBlock_pred(block, jmp);
2582 set_cur_block(NULL);
2585 static void statement_to_firm(statement_t *statement)
2587 switch(statement->type) {
2588 case STATEMENT_COMPOUND:
2589 compound_statement_to_firm((compound_statement_t*) statement);
2591 case STATEMENT_RETURN:
2592 return_statement_to_firm((return_statement_t*) statement);
2594 case STATEMENT_EXPRESSION:
2595 expression_statement_to_firm((expression_statement_t*) statement);
2598 if_statement_to_firm((if_statement_t*) statement);
2600 case STATEMENT_WHILE:
2601 while_statement_to_firm((while_statement_t*) statement);
2603 case STATEMENT_DO_WHILE:
2604 do_while_statement_to_firm((do_while_statement_t*) statement);
2606 case STATEMENT_DECLARATION:
2607 declaration_statement_to_firm((declaration_statement_t*) statement);
2609 case STATEMENT_BREAK:
2610 create_jump_statement(statement, break_label);
2612 case STATEMENT_CONTINUE:
2613 create_jump_statement(statement, continue_label);
2615 case STATEMENT_SWITCH:
2616 switch_statement_to_firm((switch_statement_t*) statement);
2618 case STATEMENT_CASE_LABEL:
2619 case_label_to_firm((case_label_statement_t*) statement);
2622 for_statement_to_firm((for_statement_t*) statement);
2624 case STATEMENT_LABEL:
2625 label_to_firm((label_statement_t*) statement);
2627 case STATEMENT_GOTO:
2628 goto_to_firm((goto_statement_t*) statement);
2633 panic("Statement not implemented\n");
2636 static int count_local_declarations(const declaration_t * decl,
2637 const declaration_t *const end)
2640 for (; decl != end; decl = decl->next) {
2641 const type_t *type = skip_typeref(decl->type);
2642 switch (type->type) {
2646 if (!decl->address_taken) ++count;
2655 static int count_decls_in_stmts(const statement_t *stmt)
2658 for (; stmt != NULL; stmt = stmt->next) {
2659 switch (stmt->type) {
2660 case STATEMENT_DECLARATION: {
2661 const declaration_statement_t *const decl_stmt =
2662 (const declaration_statement_t*)stmt;
2663 count += count_local_declarations(decl_stmt->declarations_begin,
2664 decl_stmt->declarations_end->next);
2668 case STATEMENT_COMPOUND: {
2669 const compound_statement_t *const comp =
2670 (const compound_statement_t*)stmt;
2671 count += count_decls_in_stmts(comp->statements);
2675 case STATEMENT_IF: {
2676 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2677 count += count_decls_in_stmts(if_stmt->true_statement);
2678 count += count_decls_in_stmts(if_stmt->false_statement);
2682 case STATEMENT_SWITCH: {
2683 const switch_statement_t *const switch_stmt =
2684 (const switch_statement_t*)stmt;
2685 count += count_decls_in_stmts(switch_stmt->body);
2689 case STATEMENT_LABEL: {
2690 const label_statement_t *const label_stmt =
2691 (const label_statement_t*)stmt;
2692 count += count_decls_in_stmts(label_stmt->label_statement);
2696 case STATEMENT_WHILE: {
2697 const while_statement_t *const while_stmt =
2698 (const while_statement_t*)stmt;
2699 count += count_decls_in_stmts(while_stmt->body);
2703 case STATEMENT_DO_WHILE: {
2704 const do_while_statement_t *const do_while_stmt =
2705 (const do_while_statement_t*)stmt;
2706 count += count_decls_in_stmts(do_while_stmt->body);
2710 case STATEMENT_FOR: {
2711 const for_statement_t *const for_stmt =
2712 (const for_statement_t*)stmt;
2713 /* TODO initialisation */
2714 count += count_decls_in_stmts(for_stmt->body);
2718 case STATEMENT_BREAK:
2719 case STATEMENT_CASE_LABEL:
2720 case STATEMENT_CONTINUE:
2721 case STATEMENT_EXPRESSION:
2722 case STATEMENT_GOTO:
2723 case STATEMENT_INVALID:
2724 case STATEMENT_RETURN:
2731 static int get_function_n_local_vars(declaration_t *declaration)
2735 /* count parameters */
2736 count += count_local_declarations(declaration->context.declarations, NULL);
2738 /* count local variables declared in body */
2739 count += count_decls_in_stmts(declaration->init.statement);
2741 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2742 * for expression statements... */
2748 static void initialize_function_parameters(declaration_t *declaration)
2750 ir_graph *irg = current_ir_graph;
2751 ir_node *args = get_irg_args(irg);
2752 ir_node *start_block = get_irg_start_block(irg);
2753 ir_type *function_irtype = get_ir_type(declaration->type);
2756 declaration_t *parameter = declaration->context.declarations;
2757 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2758 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2759 type_t *type = skip_typeref(parameter->type);
2761 bool needs_entity = parameter->address_taken;
2762 if(type->type == TYPE_COMPOUND_STRUCT
2763 || type->type == TYPE_COMPOUND_UNION) {
2764 needs_entity = true;
2768 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2769 ident *id = new_id_from_str(parameter->symbol->string);
2770 set_entity_ident(entity, id);
2772 parameter->declaration_type
2773 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2774 parameter->v.entity = entity;
2778 ir_mode *mode = get_ir_mode(parameter->type);
2780 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2782 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2783 parameter->v.value_number = next_value_number_function;
2784 ++next_value_number_function;
2786 set_value(parameter->v.value_number, proj);
2790 static void create_function(declaration_t *declaration)
2792 ir_entity *entity = get_function_entity(declaration);
2794 if(declaration->init.statement == NULL)
2797 current_function_decl = declaration;
2798 current_function_name = NULL;
2800 assert(imature_blocks == NULL);
2801 imature_blocks = NEW_ARR_F(ir_node*, 0);
2803 int n_local_vars = get_function_n_local_vars(declaration);
2804 ir_graph *irg = new_ir_graph(entity, n_local_vars);
2805 ir_node *first_block = get_cur_block();
2807 next_value_number_function = 0;
2808 initialize_function_parameters(declaration);
2810 statement_to_firm(declaration->init.statement);
2812 ir_node *end_block = get_irg_end_block(irg);
2814 /* do we have a return statement yet? */
2815 if(get_cur_block() != NULL) {
2816 assert(declaration->type->type == TYPE_FUNCTION);
2817 const function_type_t* const func_type
2818 = (const function_type_t*) declaration->type;
2820 if (func_type->result_type == type_void) {
2821 ret = new_Return(get_store(), 0, NULL);
2823 ir_mode *const mode = get_ir_mode(func_type->result_type);
2825 // ยง5.1.2.2.3 main implicitly returns 0
2826 if (strcmp(declaration->symbol->string, "main") == 0) {
2827 in[0] = new_Const(mode, get_mode_null(mode));
2829 in[0] = new_Unknown(mode);
2831 ret = new_Return(get_store(), 1, in);
2833 add_immBlock_pred(end_block, ret);
2836 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2837 mature_immBlock(imature_blocks[i]);
2839 DEL_ARR_F(imature_blocks);
2840 imature_blocks = NULL;
2842 mature_immBlock(first_block);
2843 mature_immBlock(end_block);
2845 irg_finalize_cons(irg);
2847 /* finalize the frame type */
2848 ir_type *frame_type = get_irg_frame_type(irg);
2849 int n = get_compound_n_members(frame_type);
2852 for(int i = 0; i < n; ++i) {
2853 ir_entity *entity = get_compound_member(frame_type, i);
2854 ir_type *entity_type = get_entity_type(entity);
2856 int align = get_type_alignment_bytes(entity_type);
2857 if(align > align_all)
2861 misalign = offset % align;
2863 offset += align - misalign;
2867 set_entity_offset(entity, offset);
2868 offset += get_type_size_bytes(entity_type);
2870 set_type_size_bytes(frame_type, offset);
2871 set_type_alignment_bytes(frame_type, align_all);
2872 set_type_state(frame_type, layout_fixed);
2877 static void create_global_variable(declaration_t *declaration)
2879 ir_type *global_type = get_glob_type();
2880 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2883 ir_entity *entity = declaration->v.entity;
2884 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2885 set_entity_visibility(entity, visibility_local);
2886 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2887 set_entity_visibility(entity, visibility_external_allocated);
2889 set_entity_visibility(entity, visibility_external_visible);
2891 current_ir_graph = get_const_code_irg();
2892 create_initializer(declaration);
2895 static void context_to_firm(context_t *context)
2897 /* first pass: create declarations */
2898 declaration_t *declaration = context->declarations;
2899 for( ; declaration != NULL; declaration = declaration->next) {
2900 if(declaration->namespc != NAMESPACE_NORMAL)
2902 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2903 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2905 if(declaration->symbol == NULL)
2908 type_t *type = declaration->type;
2909 if(type->type == TYPE_FUNCTION) {
2910 get_function_entity(declaration);
2912 create_global_variable(declaration);
2916 /* second pass: create code */
2917 declaration = context->declarations;
2918 for( ; declaration != NULL; declaration = declaration->next) {
2919 if(declaration->namespc != NAMESPACE_NORMAL)
2921 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2922 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2924 if(declaration->symbol == NULL)
2927 type_t *type = declaration->type;
2928 if(type->type != TYPE_FUNCTION)
2931 create_function(declaration);
2935 void translation_unit_to_firm(translation_unit_t *unit)
2937 /* just to be sure */
2938 continue_label = NULL;
2940 current_switch_cond = NULL;
2942 context_to_firm(& unit->context);