9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
15 #include "adt/array.h"
21 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
23 static ir_type *ir_type_const_char;
24 static ir_type *ir_type_void;
25 static ir_type *ir_type_int;
27 static type_t *type_const_char;
28 static type_t *type_void;
29 static type_t *type_int;
31 static symbol_t *symbol_alloca;
33 static int next_value_number_function;
34 static ir_node *continue_label;
35 static ir_node *break_label;
36 static ir_node *current_switch_cond;
37 static bool saw_default_label;
38 static ir_node **imature_blocks;
40 static const declaration_t *current_function_decl;
41 static ir_node *current_function_name;
43 typedef enum declaration_type_t {
44 DECLARATION_TYPE_UNKNOWN,
45 DECLARATION_TYPE_FUNCTION,
46 DECLARATION_TYPE_GLOBAL_VARIABLE,
47 DECLARATION_TYPE_LOCAL_VARIABLE,
48 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
49 DECLARATION_TYPE_COMPOUND_MEMBER,
50 DECLARATION_TYPE_LABEL_BLOCK,
51 DECLARATION_TYPE_ENUM_ENTRY
54 static ir_type *get_ir_type(type_t *type);
56 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
60 const declaration_t *declaration = & value_numbers[pos]->declaration;
62 print_warning_prefix(declaration->source_position);
63 fprintf(stderr, "variable '%s' might be used uninitialized\n",
64 declaration->symbol->string);
66 fprintf(stderr, "Some variable might be used uninitialized\n");
67 return new_r_Unknown(irg, mode);
70 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
72 const source_position_t *pos = (const source_position_t*) dbg;
75 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
79 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
81 const source_position_t *pos = (const source_position_t*) dbg;
86 return pos->input_name;
89 void init_ast2firm(void)
91 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
92 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
93 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
95 ir_type_int = get_ir_type(type_int);
96 ir_type_const_char = get_ir_type(type_const_char);
97 ir_type_void = get_ir_type(type_int); /* we don't have a real void
100 type_void->base.firm_type = ir_type_void;
102 symbol_alloca = symbol_table_insert("__builtin_alloca");
105 void exit_ast2firm(void)
109 static unsigned unique_id = 0;
111 static ident *unique_ident(const char *tag)
115 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
117 return new_id_from_str(buf);
120 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
122 switch(atomic_type->atype) {
123 case ATOMIC_TYPE_SCHAR:
124 case ATOMIC_TYPE_CHAR:
126 case ATOMIC_TYPE_UCHAR:
128 case ATOMIC_TYPE_SHORT:
130 case ATOMIC_TYPE_USHORT:
132 case ATOMIC_TYPE_LONG:
133 case ATOMIC_TYPE_INT:
135 case ATOMIC_TYPE_ULONG:
136 case ATOMIC_TYPE_UINT:
138 case ATOMIC_TYPE_LONGLONG:
140 case ATOMIC_TYPE_ULONGLONG:
142 case ATOMIC_TYPE_FLOAT:
144 case ATOMIC_TYPE_DOUBLE:
146 case ATOMIC_TYPE_LONG_DOUBLE:
148 case ATOMIC_TYPE_BOOL:
150 #ifdef PROVIDE_COMPLEX
151 case ATOMIC_TYPE_FLOAT_COMPLEX:
152 case ATOMIC_TYPE_DOUBLE_COMPLEX:
153 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
154 panic("complex lowering not implemented yet");
156 case ATOMIC_TYPE_FLOAT_IMAGINARY:
157 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
158 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
159 panic("imaginary lowering not implemented yet");
162 case ATOMIC_TYPE_VOID:
163 /* firm has no real void... */
165 case ATOMIC_TYPE_INVALID:
168 panic("Encountered unknown atomic type");
172 static unsigned get_type_size(type_t *type);
174 static unsigned get_atomic_type_size(const atomic_type_t *type)
176 switch(type->atype) {
177 case ATOMIC_TYPE_CHAR:
178 case ATOMIC_TYPE_SCHAR:
179 case ATOMIC_TYPE_UCHAR:
182 case ATOMIC_TYPE_SHORT:
183 case ATOMIC_TYPE_USHORT:
186 case ATOMIC_TYPE_BOOL:
187 case ATOMIC_TYPE_INT:
188 case ATOMIC_TYPE_UINT:
189 case ATOMIC_TYPE_LONG:
190 case ATOMIC_TYPE_ULONG:
191 case ATOMIC_TYPE_FLOAT:
194 case ATOMIC_TYPE_LONGLONG:
195 case ATOMIC_TYPE_ULONGLONG:
196 case ATOMIC_TYPE_DOUBLE:
199 case ATOMIC_TYPE_LONG_DOUBLE:
202 case ATOMIC_TYPE_VOID:
205 case ATOMIC_TYPE_INVALID:
208 panic("Trying to determine size of invalid atomic type");
211 static unsigned get_compound_type_size(compound_type_t *type)
213 ir_type *irtype = get_ir_type((type_t*) type);
214 return get_type_size_bytes(irtype);
217 static unsigned get_array_type_size(array_type_t *type)
219 ir_type *irtype = get_ir_type((type_t*) type);
220 return get_type_size_bytes(irtype);
223 static unsigned get_type_size(type_t *type)
225 type = skip_typeref(type);
229 return get_atomic_type_size(&type->atomic);
231 return get_mode_size_bytes(mode_Is);
232 case TYPE_COMPOUND_UNION:
233 case TYPE_COMPOUND_STRUCT:
234 return get_compound_type_size(&type->compound);
236 /* just a pointer to the function */
237 return get_mode_size_bytes(mode_P_code);
239 return get_mode_size_bytes(mode_P_data);
241 return get_array_type_size(&type->array);
248 panic("Trying to determine size of invalid type");
251 static unsigned count_parameters(const function_type_t *function_type)
255 function_parameter_t *parameter = function_type->parameters;
256 for ( ; parameter != NULL; parameter = parameter->next) {
266 static long fold_constant(const expression_t *expression);
268 static ir_type *create_atomic_type(const atomic_type_t *type)
270 ir_mode *mode = get_atomic_mode(type);
271 ident *id = get_mode_ident(mode);
272 ir_type *irtype = new_type_primitive(id, mode);
277 static ir_type *create_method_type(const function_type_t *function_type)
279 type_t *result_type = function_type->result_type;
281 ident *id = unique_ident("functiontype");
282 int n_parameters = count_parameters(function_type);
283 int n_results = result_type == type_void ? 0 : 1;
284 ir_type *irtype = new_type_method(id, n_parameters, n_results);
286 if(result_type != type_void) {
287 ir_type *restype = get_ir_type(result_type);
288 set_method_res_type(irtype, 0, restype);
291 function_parameter_t *parameter = function_type->parameters;
293 for( ; parameter != NULL; parameter = parameter->next) {
294 ir_type *p_irtype = get_ir_type(parameter->type);
295 set_method_param_type(irtype, n, p_irtype);
299 if(function_type->variadic || function_type->unspecified_parameters) {
300 set_method_variadicity(irtype, variadicity_variadic);
306 static ir_type *create_pointer_type(pointer_type_t *type)
308 type_t *points_to = type->points_to;
309 ir_type *ir_points_to;
310 /* Avoid endless recursion if the points_to type contains this poiner type
311 * again (might be a struct). We therefore first create a void* pointer
312 * and then set the real points_to type
314 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
315 ir_type_void, mode_P_data);
316 type->type.firm_type = ir_type;
318 ir_points_to = get_ir_type(points_to);
319 set_pointer_points_to_type(ir_type, ir_points_to);
324 static ir_type *create_array_type(array_type_t *type)
326 type_t *element_type = type->element_type;
327 ir_type *ir_element_type = get_ir_type(element_type);
329 ident *id = unique_ident("array");
330 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
332 if(type->size != NULL) {
333 int n_elements = fold_constant(type->size);
335 set_array_bounds_int(ir_type, 0, 0, n_elements);
337 size_t elemsize = get_type_size_bytes(ir_element_type);
338 int align = get_type_alignment_bytes(ir_element_type);
339 if(elemsize % align > 0) {
340 elemsize += align - (elemsize % align);
342 set_type_size_bytes(ir_type, n_elements * elemsize);
343 set_type_alignment_bytes(ir_type, align);
344 set_type_state(ir_type, layout_fixed);
350 #define INVALID_TYPE ((ir_type_ptr)-1)
352 static ir_type *create_struct_type(compound_type_t *type)
354 symbol_t *symbol = type->declaration->symbol;
357 id = unique_ident(symbol->string);
359 id = unique_ident("__anonymous_struct");
361 ir_type *ir_type = new_type_struct(id);
363 type->type.firm_type = ir_type;
367 declaration_t *entry = type->declaration->context.declarations;
368 for( ; entry != NULL; entry = entry->next) {
369 if(entry->namespc != NAMESPACE_NORMAL)
372 ident *ident = new_id_from_str(entry->symbol->string);
373 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
375 int entry_size = get_type_size_bytes(entry_ir_type);
376 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
377 int misalign = offset % entry_alignment;
379 offset += entry_alignment - misalign;
381 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
382 set_entity_offset(entity, offset);
383 add_struct_member(ir_type, entity);
384 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
385 entry->v.entity = entity;
387 offset += entry_size;
388 if(entry_alignment > align_all) {
389 if(entry_alignment % align_all != 0) {
390 panic("Uneven alignments not supported yet");
392 align_all = entry_alignment;
396 int misalign = offset % align_all;
398 set_type_alignment_bytes(ir_type, align_all);
399 set_type_size_bytes(ir_type, offset);
400 set_type_state(ir_type, layout_fixed);
405 static ir_type *create_union_type(compound_type_t *type)
407 declaration_t *declaration = type->declaration;
408 symbol_t *symbol = declaration->symbol;
411 id = unique_ident(symbol->string);
413 id = unique_ident("__anonymous_union");
415 ir_type *ir_type = new_type_union(id);
417 type->type.firm_type = ir_type;
421 declaration_t *entry = declaration->context.declarations;
422 for( ; entry != NULL; entry = entry->next) {
423 if(entry->namespc != NAMESPACE_NORMAL)
426 ident *ident = new_id_from_str(entry->symbol->string);
427 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
429 int entry_size = get_type_size_bytes(entry_ir_type);
430 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
432 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
433 add_union_member(ir_type, entity);
434 set_entity_offset(entity, 0);
435 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
436 entry->v.entity = entity;
438 if(entry_size > size) {
441 if(entry_alignment > align_all) {
442 if(entry_alignment % align_all != 0) {
443 panic("Uneven alignments not supported yet");
445 align_all = entry_alignment;
449 set_type_alignment_bytes(ir_type, align_all);
450 set_type_size_bytes(ir_type, size);
451 set_type_state(ir_type, layout_fixed);
456 static ir_node *expression_to_firm(const expression_t *expression);
457 static inline ir_mode *get_ir_mode(type_t *type);
459 static ir_type *create_enum_type(enum_type_t *const type)
461 type->type.firm_type = ir_type_int;
463 ir_mode *const mode = get_ir_mode((type_t*) type);
464 tarval *const one = get_mode_one(mode);
465 tarval * tv_next = get_tarval_null(mode);
467 declaration_t *declaration = type->declaration->next;
468 for (; declaration != NULL; declaration = declaration->next) {
469 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
472 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
474 expression_t *const init = declaration->init.enum_value;
476 ir_node *const cnst = expression_to_firm(init);
477 if (!is_Const(cnst)) {
478 panic("couldn't fold constant");
480 tv_next = get_Const_tarval(cnst);
482 declaration->v.enum_val = tv_next;
483 tv_next = tarval_add(tv_next, one);
489 static ir_type *get_ir_type(type_t *type)
491 assert(type != NULL);
493 type = skip_typeref(type);
495 if(type->base.firm_type != NULL) {
496 assert(type->base.firm_type != INVALID_TYPE);
497 return type->base.firm_type;
500 ir_type *firm_type = NULL;
503 firm_type = create_atomic_type(&type->atomic);
506 firm_type = create_method_type(&type->function);
509 firm_type = create_pointer_type(&type->pointer);
512 firm_type = create_array_type(&type->array);
514 case TYPE_COMPOUND_STRUCT:
515 firm_type = create_struct_type(&type->compound);
517 case TYPE_COMPOUND_UNION:
518 firm_type = create_union_type(&type->compound);
521 firm_type = create_enum_type(&type->enumt);
529 if(firm_type == NULL)
530 panic("unknown type found");
532 type->base.firm_type = firm_type;
536 static inline ir_mode *get_ir_mode(type_t *type)
538 ir_type *irtype = get_ir_type(type);
540 /* firm doesn't report a mode for arrays somehow... */
541 if(is_Array_type(irtype)) {
545 ir_mode *mode = get_type_mode(irtype);
546 assert(mode != NULL);
550 static ir_entity* get_function_entity(declaration_t *declaration)
552 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
553 return declaration->v.entity;
554 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
556 symbol_t *symbol = declaration->symbol;
557 ident *id = new_id_from_str(symbol->string);
559 ir_type *global_type = get_glob_type();
560 ir_type *ir_type_method = get_ir_type(declaration->type);
561 assert(is_Method_type(ir_type_method));
563 ir_entity *entity = new_entity(global_type, id, ir_type_method);
564 set_entity_ld_ident(entity, id);
565 if(declaration->storage_class == STORAGE_CLASS_STATIC
566 || declaration->is_inline) {
567 set_entity_visibility(entity, visibility_local);
568 } else if(declaration->init.statement != NULL) {
569 set_entity_visibility(entity, visibility_external_visible);
571 set_entity_visibility(entity, visibility_external_allocated);
574 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
575 declaration->v.entity = entity;
580 static dbg_info *get_dbg_info(const source_position_t *pos)
582 return (dbg_info*) pos;
585 static ir_node *const_to_firm(const const_expression_t *cnst)
587 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
588 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
593 if(mode_is_float(mode)) {
594 tv = new_tarval_from_double(cnst->v.float_value, mode);
596 if(mode_is_signed(mode)) {
597 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
599 len = snprintf(buf, sizeof(buf), "%llu",
600 (unsigned long long) cnst->v.int_value);
602 tv = new_tarval_from_str(buf, len, mode);
605 return new_d_Const(dbgi, mode, tv);
608 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
610 assert(entity != NULL);
611 union symconst_symbol sym;
612 sym.entity_p = entity;
613 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
616 static ir_node *string_to_firm(const source_position_t *const src_pos,
617 const char *const id_prefix,
618 const char *const string)
620 ir_type *const global_type = get_glob_type();
621 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
624 ident *const id = unique_ident(id_prefix);
625 ir_entity *const entity = new_entity(global_type, id, type);
626 set_entity_ld_ident(entity, id);
627 set_entity_variability(entity, variability_constant);
629 ir_type *const elem_type = ir_type_const_char;
630 ir_mode *const mode = get_type_mode(elem_type);
632 const size_t slen = strlen(string) + 1;
634 set_array_lower_bound_int(type, 0, 0);
635 set_array_upper_bound_int(type, 0, slen);
636 set_type_size_bytes(type, slen);
637 set_type_state(type, layout_fixed);
639 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
640 for(size_t i = 0; i < slen; ++i) {
641 tvs[i] = new_tarval_from_long(string[i], mode);
644 set_array_entity_values(entity, tvs, slen);
647 dbg_info *const dbgi = get_dbg_info(src_pos);
649 return create_symconst(dbgi, entity);
652 static ir_node *string_literal_to_firm(
653 const string_literal_expression_t* literal)
655 return string_to_firm(&literal->expression.source_position, "Lstr",
659 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
660 dbg_info *const dbgi)
662 if(is_compound_type(irtype) || is_Array_type(irtype)) {
666 ir_mode *const mode = get_type_mode(irtype);
667 ir_node *const memory = get_store();
668 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
669 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
670 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
675 static ir_node *get_global_var_address(dbg_info *const dbgi,
676 const declaration_t *const decl)
678 assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
680 ir_entity *const entity = decl->v.entity;
681 switch ((storage_class_tag_t)decl->storage_class) {
682 case STORAGE_CLASS_THREAD:
683 case STORAGE_CLASS_THREAD_EXTERN:
684 case STORAGE_CLASS_THREAD_STATIC: {
685 ir_node *const no_mem = new_NoMem();
686 ir_node *const tls = get_irg_tls(current_ir_graph);
687 return new_d_simpleSel(dbgi, no_mem, tls, entity);
691 return create_symconst(dbgi, entity);
695 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
697 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
698 declaration_t *declaration = ref->declaration;
699 type_t *type = skip_typeref(declaration->type);
701 switch((declaration_type_t) declaration->declaration_type) {
702 case DECLARATION_TYPE_UNKNOWN:
703 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
709 case DECLARATION_TYPE_ENUM_ENTRY: {
710 ir_mode *const mode = get_ir_mode(type);
711 return new_Const(mode, declaration->v.enum_val);
714 case DECLARATION_TYPE_LOCAL_VARIABLE: {
715 ir_mode *mode = get_ir_mode(type);
716 return get_value(declaration->v.value_number, mode);
718 case DECLARATION_TYPE_FUNCTION: {
719 return create_symconst(dbgi, declaration->v.entity);
721 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
722 ir_node *const addr = get_global_var_address(dbgi, declaration);
723 ir_type *const irtype = get_entity_type(declaration->v.entity);
724 return deref_address(irtype, addr, dbgi);
727 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
728 ir_entity *entity = declaration->v.entity;
729 ir_node *frame = get_irg_frame(current_ir_graph);
730 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
731 ir_type *irtype = get_entity_type(entity);
732 return deref_address(irtype, sel, dbgi);
735 case DECLARATION_TYPE_COMPOUND_MEMBER:
736 case DECLARATION_TYPE_LABEL_BLOCK:
737 panic("not implemented reference type");
740 panic("reference to declaration with unknown type found");
743 static ir_node *reference_addr(const reference_expression_t *ref)
745 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
746 declaration_t *declaration = ref->declaration;
748 switch((declaration_type_t) declaration->declaration_type) {
749 case DECLARATION_TYPE_UNKNOWN:
751 case DECLARATION_TYPE_LOCAL_VARIABLE:
752 panic("local variable without entity has no address");
753 case DECLARATION_TYPE_FUNCTION: {
754 return create_symconst(dbgi, declaration->v.entity);
756 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
757 ir_node *const addr = get_global_var_address(dbgi, declaration);
760 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
761 ir_entity *entity = declaration->v.entity;
762 ir_node *frame = get_irg_frame(current_ir_graph);
763 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
768 case DECLARATION_TYPE_ENUM_ENTRY:
769 panic("trying to reference enum entry");
771 case DECLARATION_TYPE_COMPOUND_MEMBER:
772 case DECLARATION_TYPE_LABEL_BLOCK:
773 panic("not implemented reference type");
776 panic("reference to declaration with unknown type found");
779 static ir_node *process_builtin_call(const call_expression_t *call)
781 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
783 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
784 builtin_symbol_expression_t *builtin
785 = (builtin_symbol_expression_t*) call->function;
786 symbol_t *symbol = builtin->symbol;
788 if(symbol == symbol_alloca) {
789 if(call->arguments == NULL || call->arguments->next != NULL) {
790 panic("invalid number of parameters on __builtin_alloca");
792 expression_t *argument = call->arguments->expression;
793 ir_node *size = expression_to_firm(argument);
795 ir_node *store = get_store();
796 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
798 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
800 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
804 panic("Unsupported builtin found\n");
808 static ir_node *call_expression_to_firm(const call_expression_t *call)
810 assert(get_cur_block() != NULL);
812 expression_t *function = call->function;
813 if(function->type == EXPR_BUILTIN_SYMBOL) {
814 return process_builtin_call(call);
816 ir_node *callee = expression_to_firm(function);
818 type_t *type = function->base.datatype;
819 assert(type->type == TYPE_POINTER);
820 pointer_type_t *const ptr_type = &type->pointer;
821 assert(ptr_type->points_to->type == TYPE_FUNCTION);
822 function_type_t *function_type = &ptr_type->points_to->function;
824 int n_parameters = 0;
825 call_argument_t *argument = call->arguments;
826 for( ; argument != NULL; argument = argument->next) {
830 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
831 ir_type *new_method_type = NULL;
832 if(function_type->variadic || function_type->unspecified_parameters) {
833 /* we need to construct a new method type matching the call
835 int n_res = get_method_n_ress(ir_method_type);
836 new_method_type = new_type_method(unique_ident("calltype"),
837 n_parameters, n_res);
838 set_method_calling_convention(new_method_type,
839 get_method_calling_convention(ir_method_type));
840 set_method_additional_properties(new_method_type,
841 get_method_additional_properties(ir_method_type));
843 for(int i = 0; i < n_res; ++i) {
844 set_method_res_type(new_method_type, i,
845 get_method_res_type(ir_method_type, i));
848 ir_node *in[n_parameters];
850 argument = call->arguments;
852 for( ; argument != NULL; argument = argument->next) {
853 expression_t *expression = argument->expression;
854 ir_node *arg_node = expression_to_firm(expression);
857 if(new_method_type != NULL) {
858 ir_type *irtype = get_ir_type(expression->base.datatype);
859 set_method_param_type(new_method_type, n, irtype);
864 assert(n == n_parameters);
866 if(new_method_type != NULL)
867 ir_method_type = new_method_type;
869 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
870 ir_node *store = get_store();
871 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
873 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
876 type_t *result_type = skip_typeref(function_type->result_type);
877 ir_node *result = NULL;
879 if(!is_type_atomic(result_type, ATOMIC_TYPE_VOID)) {
881 if(is_type_scalar(result_type)) {
882 mode = get_ir_mode(result_type);
886 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
887 result = new_d_Proj(dbgi, resproj, mode, 0);
893 static void statement_to_firm(statement_t *statement);
894 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
896 static ir_node *expression_to_addr(const expression_t *expression);
897 static void create_condition_evaluation(const expression_t *expression,
899 ir_node *false_block);
901 static void set_value_for_expression(const expression_t *expression,
904 if(expression->type == EXPR_REFERENCE) {
905 reference_expression_t *ref = (reference_expression_t*) expression;
907 declaration_t *declaration = ref->declaration;
908 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
909 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
910 set_value(declaration->v.value_number, value);
915 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
916 ir_node *addr = expression_to_addr(expression);
917 ir_node *memory = get_store();
919 type_t *type = skip_typeref(expression->base.datatype);
920 if(is_type_scalar(type)) {
921 ir_node *store = new_d_Store(dbgi, memory, addr, value);
922 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
923 set_store(store_mem);
925 ir_type *irtype = get_ir_type(type);
926 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
927 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
928 set_store(copyb_mem);
932 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
934 ir_mode *value_mode = get_irn_mode(value);
936 if (value_mode == dest_mode || is_Bad(value))
939 if(dest_mode == mode_b) {
940 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
941 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
942 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
946 return new_d_Conv(dbgi, value, dest_mode);
949 static ir_node *create_incdec(const unary_expression_t *expression)
951 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
952 type_t *type = expression->expression.datatype;
953 ir_mode *mode = get_ir_mode(type);
954 expression_t *value = expression->value;
956 ir_node *value_node = expression_to_firm(value);
959 if(type->type == TYPE_POINTER) {
960 pointer_type_t *pointer_type = &type->pointer;
961 unsigned elem_size = get_type_size(pointer_type->points_to);
962 offset = new_Const_long(mode_Is, elem_size);
964 assert(is_type_arithmetic(type));
965 offset = new_Const(mode, get_mode_one(mode));
968 switch(expression->type) {
969 case UNEXPR_POSTFIX_INCREMENT: {
970 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
971 set_value_for_expression(value, new_value);
974 case UNEXPR_POSTFIX_DECREMENT: {
975 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
976 set_value_for_expression(value, new_value);
979 case UNEXPR_PREFIX_INCREMENT: {
980 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
981 set_value_for_expression(value, new_value);
984 case UNEXPR_PREFIX_DECREMENT: {
985 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
986 set_value_for_expression(value, new_value);
990 panic("no incdec expr in create_incdec");
995 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
997 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
998 type_t *type = skip_typeref(expression->expression.datatype);
1000 if(expression->type == UNEXPR_TAKE_ADDRESS)
1001 return expression_to_addr(expression->value);
1003 const expression_t *value = expression->value;
1004 ir_node *value_node = expression_to_firm(value);
1006 switch(expression->type) {
1008 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
1011 case UNEXPR_BITWISE_NEGATE:
1012 return new_d_Not(dbgi, value_node, get_ir_mode(type));
1014 if(get_irn_mode(value_node) != mode_b) {
1015 value_node = create_conv(dbgi, value_node, mode_b);
1017 value_node = new_d_Not(dbgi, value_node, mode_b);
1018 ir_mode *const mode = get_ir_mode(type);
1019 if(mode != mode_b) {
1020 value_node = create_conv(dbgi, value_node, mode);
1024 case UNEXPR_DEREFERENCE: {
1025 ir_type *irtype = get_ir_type(type);
1026 return deref_address(irtype, value_node, dbgi);
1028 case UNEXPR_POSTFIX_INCREMENT:
1029 case UNEXPR_POSTFIX_DECREMENT:
1030 case UNEXPR_PREFIX_INCREMENT:
1031 case UNEXPR_PREFIX_DECREMENT:
1032 return create_incdec(expression);
1034 return create_conv(dbgi, value_node, get_ir_mode(type));
1036 case UNEXPR_TAKE_ADDRESS:
1037 case UNEXPR_INVALID:
1040 panic("invalid UNEXPR type found");
1043 static long get_pnc(binary_expression_type_t type)
1046 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1047 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1048 case BINEXPR_LESS: return pn_Cmp_Lt;
1049 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1050 case BINEXPR_GREATER: return pn_Cmp_Gt;
1051 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1055 panic("trying to get pn_Cmp from non-comparison binexpr type");
1058 static ir_node *create_lazy_op(const binary_expression_t *expression)
1060 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1061 type_t *type = expression->expression.datatype;
1062 ir_mode *mode = get_ir_mode(type);
1064 ir_node *cur_block = get_cur_block();
1066 ir_node *one_block = new_immBlock();
1067 ir_node *one = new_Const(mode, get_mode_one(mode));
1068 ir_node *jmp_one = new_d_Jmp(dbgi);
1070 ir_node *zero_block = new_immBlock();
1071 ir_node *zero = new_Const(mode, get_mode_null(mode));
1072 ir_node *jmp_zero = new_d_Jmp(dbgi);
1074 set_cur_block(cur_block);
1075 create_condition_evaluation((const expression_t*) expression,
1076 one_block, zero_block);
1077 mature_immBlock(one_block);
1078 mature_immBlock(zero_block);
1080 ir_node *common_block = new_immBlock();
1081 add_immBlock_pred(common_block, jmp_one);
1082 add_immBlock_pred(common_block, jmp_zero);
1083 mature_immBlock(common_block);
1085 ir_node *in[2] = { one, zero };
1086 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1091 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1092 ir_node *right, ir_mode *mode);
1094 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1095 create_arithmetic_func func)
1097 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1098 ir_node *left = expression_to_firm(expression->left);
1099 ir_node *right = expression_to_firm(expression->right);
1100 type_t *type = expression->right->base.datatype;
1101 /* be careful with the modes, because in arithmetic assign nodes only
1102 * the right operand has the mode of the arithmetic already */
1103 ir_mode *mode = get_ir_mode(type);
1104 left = create_conv(dbgi, left, mode);
1105 ir_node *res = func(dbgi, left, right, mode);
1110 static ir_node *pointer_arithmetic(ir_node *const pointer,
1113 dbg_info *const dbgi,
1114 const create_arithmetic_func func)
1116 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1117 type_t *const points_to = pointer_type->points_to;
1118 const unsigned elem_size = get_type_size(points_to);
1120 assert(elem_size >= 1);
1121 if (elem_size > 1) {
1122 integer = create_conv(dbgi, integer, mode_Is);
1123 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1124 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1128 ir_mode *const mode = get_ir_mode(type);
1129 return func(dbgi, pointer, integer, mode);
1132 static ir_node *create_arithmetic_assign_binop(
1133 const binary_expression_t *expression, create_arithmetic_func func)
1135 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1136 type_t *const type = expression->expression.datatype;
1139 if (type->type == TYPE_POINTER) {
1140 ir_node *const pointer = expression_to_firm(expression->left);
1141 ir_node * integer = expression_to_firm(expression->right);
1142 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1144 value = create_arithmetic_binop(expression, func);
1147 ir_mode *const mode = get_ir_mode(type);
1148 value = create_conv(dbgi, value, mode);
1149 set_value_for_expression(expression->left, value);
1154 static ir_node *create_add(const binary_expression_t *expression)
1156 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1157 ir_node *left = expression_to_firm(expression->left);
1158 ir_node *right = expression_to_firm(expression->right);
1159 type_t *type = expression->expression.datatype;
1161 expression_t *expr_left = expression->left;
1162 expression_t *expr_right = expression->right;
1163 type_t *type_left = skip_typeref(expr_left->base.datatype);
1164 type_t *type_right = skip_typeref(expr_right->base.datatype);
1166 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1167 ir_mode *const mode = get_ir_mode(type);
1168 return new_d_Add(dbgi, left, right, mode);
1171 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1172 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1174 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1175 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1179 static ir_node *create_sub(const binary_expression_t *expression)
1181 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1182 expression_t *const expr_left = expression->left;
1183 expression_t *const expr_right = expression->right;
1184 ir_node *const left = expression_to_firm(expr_left);
1185 ir_node *const right = expression_to_firm(expr_right);
1186 type_t *const type = expression->expression.datatype;
1187 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1188 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1190 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1191 ir_mode *const mode = get_ir_mode(type);
1192 return new_d_Sub(dbgi, left, right, mode);
1193 } else if (type_left->type == TYPE_POINTER
1194 && type_right->type == TYPE_POINTER) {
1195 const pointer_type_t *const ptr_type = &type_left->pointer;
1196 const unsigned elem_size = get_type_size(ptr_type->points_to);
1197 ir_mode *const mode = get_ir_mode(type);
1198 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1199 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1200 ir_node *const no_mem = new_NoMem();
1201 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1202 op_pin_state_floats);
1203 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1206 assert(type_left->type == TYPE_POINTER);
1207 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1210 static ir_node *create_shift(const binary_expression_t *expression)
1212 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1213 ir_node *left = expression_to_firm(expression->left);
1214 ir_node *right = expression_to_firm(expression->right);
1215 type_t *type = expression->expression.datatype;
1216 ir_mode *mode = get_ir_mode(type);
1218 /* firm always wants the shift count to be unsigned */
1219 right = create_conv(dbgi, right, mode_Iu);
1223 switch(expression->type) {
1224 case BINEXPR_SHIFTLEFT_ASSIGN:
1225 case BINEXPR_SHIFTLEFT:
1226 res = new_d_Shl(dbgi, left, right, mode);
1228 case BINEXPR_SHIFTRIGHT_ASSIGN:
1229 case BINEXPR_SHIFTRIGHT: {
1230 expression_t *expr_left = expression->left;
1231 type_t *type_left = skip_typeref(expr_left->base.datatype);
1233 if(is_type_signed(type_left)) {
1234 res = new_d_Shrs(dbgi, left, right, mode);
1236 res = new_d_Shr(dbgi, left, right, mode);
1241 panic("create shift op called for non-shift op");
1248 static ir_node *create_divmod(const binary_expression_t *expression)
1250 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1251 ir_node *left = expression_to_firm(expression->left);
1252 ir_node *right = expression_to_firm(expression->right);
1253 ir_node *pin = new_Pin(new_NoMem());
1254 /* be careful with the modes, because in arithmetic assign nodes only
1255 * the right operand has the mode of the arithmetic already */
1256 type_t *type = expression->right->base.datatype;
1257 ir_mode *mode = get_ir_mode(type);
1258 left = create_conv(dbgi, left, mode);
1262 switch (expression->type) {
1264 case BINEXPR_DIV_ASSIGN:
1265 if(mode_is_float(mode)) {
1266 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1267 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1269 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1270 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1275 case BINEXPR_MOD_ASSIGN:
1276 assert(!mode_is_float(mode));
1277 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1278 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1281 default: panic("unexpected binary expression type in create_divmod()");
1287 static ir_node *create_arithmetic_assign_divmod(
1288 const binary_expression_t *expression)
1290 ir_node * value = create_divmod(expression);
1291 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1292 type_t *const type = expression->expression.datatype;
1293 ir_mode *const mode = get_ir_mode(type);
1295 assert(type->type != TYPE_POINTER);
1297 value = create_conv(dbgi, value, mode);
1298 set_value_for_expression(expression->left, value);
1303 static ir_node *create_arithmetic_assign_shift(
1304 const binary_expression_t *expression)
1306 ir_node * value = create_shift(expression);
1307 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1308 type_t *const type = expression->expression.datatype;
1309 ir_mode *const mode = get_ir_mode(type);
1311 value = create_conv(dbgi, value, mode);
1312 set_value_for_expression(expression->left, value);
1317 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1319 binary_expression_type_t type = expression->type;
1322 case BINEXPR_NOTEQUAL:
1324 case BINEXPR_LESSEQUAL:
1325 case BINEXPR_GREATER:
1326 case BINEXPR_GREATEREQUAL: {
1327 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1328 ir_node *left = expression_to_firm(expression->left);
1329 ir_node *right = expression_to_firm(expression->right);
1330 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1331 long pnc = get_pnc(type);
1332 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1335 case BINEXPR_ASSIGN: {
1336 ir_node *right = expression_to_firm(expression->right);
1337 set_value_for_expression(expression->left, right);
1342 return create_add(expression);
1344 return create_sub(expression);
1346 return create_arithmetic_binop(expression, new_d_Mul);
1347 case BINEXPR_BITWISE_AND:
1348 return create_arithmetic_binop(expression, new_d_And);
1349 case BINEXPR_BITWISE_OR:
1350 return create_arithmetic_binop(expression, new_d_Or);
1351 case BINEXPR_BITWISE_XOR:
1352 return create_arithmetic_binop(expression, new_d_Eor);
1353 case BINEXPR_SHIFTLEFT:
1354 case BINEXPR_SHIFTRIGHT:
1355 return create_shift(expression);
1358 return create_divmod(expression);
1359 case BINEXPR_LOGICAL_AND:
1360 case BINEXPR_LOGICAL_OR:
1361 return create_lazy_op(expression);
1363 expression_to_firm(expression->left);
1364 return expression_to_firm(expression->right);
1365 case BINEXPR_ADD_ASSIGN:
1366 return create_arithmetic_assign_binop(expression, new_d_Add);
1367 case BINEXPR_SUB_ASSIGN:
1368 return create_arithmetic_assign_binop(expression, new_d_Sub);
1369 case BINEXPR_MUL_ASSIGN:
1370 return create_arithmetic_assign_binop(expression, new_d_Mul);
1371 case BINEXPR_DIV_ASSIGN:
1372 return create_arithmetic_assign_divmod(expression);
1373 case BINEXPR_BITWISE_AND_ASSIGN:
1374 return create_arithmetic_assign_binop(expression, new_d_And);
1375 case BINEXPR_BITWISE_OR_ASSIGN:
1376 return create_arithmetic_assign_binop(expression, new_d_Or);
1377 case BINEXPR_BITWISE_XOR_ASSIGN:
1378 return create_arithmetic_assign_binop(expression, new_d_Eor);
1379 case BINEXPR_SHIFTLEFT_ASSIGN:
1380 case BINEXPR_SHIFTRIGHT_ASSIGN:
1381 return create_arithmetic_assign_shift(expression);
1383 panic("TODO binexpr type");
1387 static ir_node *array_access_addr(const array_access_expression_t *expression)
1389 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1390 ir_node *base_addr = expression_to_firm(expression->array_ref);
1391 ir_node *offset = expression_to_firm(expression->index);
1392 offset = create_conv(dbgi, offset, mode_Iu);
1394 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1395 assert(is_type_pointer(ref_type));
1396 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1398 unsigned elem_size = get_type_size(pointer_type->points_to);
1399 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1400 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1402 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1407 static ir_node *array_access_to_firm(
1408 const array_access_expression_t *expression)
1410 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1411 ir_node *addr = array_access_addr(expression);
1412 type_t *type = revert_automatic_type_conversion(
1413 (const expression_t*) expression);
1414 type = skip_typeref(type);
1415 ir_type *irtype = get_ir_type(type);
1417 return deref_address(irtype, addr, dbgi);
1420 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1422 type_t *type = expression->type;
1424 type = expression->size_expression->base.datatype;
1425 assert(type != NULL);
1428 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1429 unsigned size = get_type_size(type);
1430 ir_node *size_node = new_Const_long(mode, size);
1435 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1437 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1439 ir_node *cur_block = get_cur_block();
1441 /* create the true block */
1442 ir_node *true_block = new_immBlock();
1444 ir_node *true_val = expression_to_firm(expression->true_expression);
1445 ir_node *true_jmp = new_Jmp();
1447 /* create the false block */
1448 ir_node *false_block = new_immBlock();
1450 ir_node *false_val = expression_to_firm(expression->false_expression);
1451 ir_node *false_jmp = new_Jmp();
1453 /* create the condition evaluation */
1454 set_cur_block(cur_block);
1455 create_condition_evaluation(expression->condition, true_block, false_block);
1456 mature_immBlock(true_block);
1457 mature_immBlock(false_block);
1459 /* create the common block */
1460 ir_node *common_block = new_immBlock();
1461 add_immBlock_pred(common_block, true_jmp);
1462 add_immBlock_pred(common_block, false_jmp);
1463 mature_immBlock(common_block);
1465 /* TODO improve static semantics, so either both or no values are NULL */
1466 if (true_val == NULL || false_val == NULL) return NULL;
1468 ir_node *in[2] = { true_val, false_val };
1469 ir_mode *mode = get_irn_mode(true_val);
1470 assert(get_irn_mode(false_val) == mode);
1471 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1476 static ir_node *select_addr(const select_expression_t *expression)
1478 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1480 ir_node *compound_addr = expression_to_firm(expression->compound);
1482 declaration_t *entry = expression->compound_entry;
1483 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1484 ir_entity *entity = entry->v.entity;
1486 assert(entity != NULL);
1488 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1493 static ir_node *select_to_firm(const select_expression_t *expression)
1495 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1496 ir_node *addr = select_addr(expression);
1497 type_t *type = revert_automatic_type_conversion(
1498 (const expression_t*) expression);
1499 type = skip_typeref(type);
1500 ir_type *irtype = get_ir_type(type);
1502 return deref_address(irtype, addr, dbgi);
1505 /* Values returned by __builtin_classify_type. */
1506 typedef enum gcc_type_class
1512 enumeral_type_class,
1515 reference_type_class,
1519 function_type_class,
1530 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1532 const type_t *const type = expr->type_expression->base.datatype;
1538 const atomic_type_t *const atomic_type = &type->atomic;
1539 switch (atomic_type->atype) {
1540 // should not be reached
1541 case ATOMIC_TYPE_INVALID:
1545 // gcc cannot do that
1546 case ATOMIC_TYPE_VOID:
1547 tc = void_type_class;
1550 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1551 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1552 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1553 case ATOMIC_TYPE_SHORT:
1554 case ATOMIC_TYPE_USHORT:
1555 case ATOMIC_TYPE_INT:
1556 case ATOMIC_TYPE_UINT:
1557 case ATOMIC_TYPE_LONG:
1558 case ATOMIC_TYPE_ULONG:
1559 case ATOMIC_TYPE_LONGLONG:
1560 case ATOMIC_TYPE_ULONGLONG:
1561 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1562 tc = integer_type_class;
1565 case ATOMIC_TYPE_FLOAT:
1566 case ATOMIC_TYPE_DOUBLE:
1567 case ATOMIC_TYPE_LONG_DOUBLE:
1568 tc = real_type_class;
1571 #ifdef PROVIDE_COMPLEX
1572 case ATOMIC_TYPE_FLOAT_COMPLEX:
1573 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1574 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1575 tc = complex_type_class;
1577 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1578 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1579 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1580 tc = complex_type_class;
1585 panic("Unimplemented case in classify_type_to_firm().");
1590 case TYPE_ARRAY: // gcc handles this as pointer
1591 case TYPE_FUNCTION: // gcc handles this as pointer
1592 case TYPE_POINTER: tc = pointer_type_class; break;
1593 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1594 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1596 // gcc handles this as integer
1597 case TYPE_ENUM: tc = integer_type_class; break;
1600 panic("Unimplemented case in classify_type_to_firm().");
1603 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1604 ir_mode *const mode = mode_Is;
1605 tarval *const tv = new_tarval_from_long(tc, mode);
1606 return new_d_Const(dbgi, mode, tv);
1609 static ir_node *function_name_to_firm(
1610 const string_literal_expression_t *const expr)
1612 if (current_function_name == NULL) {
1613 const source_position_t *const src_pos =
1614 &expr->expression.source_position;
1615 const char *const name = current_function_decl->symbol->string;
1616 current_function_name = string_to_firm(src_pos, "__func__", name);
1619 return current_function_name;
1622 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1624 statement_t *statement = expr->statement;
1626 assert(statement->type == STATEMENT_COMPOUND);
1627 return compound_statement_to_firm((compound_statement_t*) statement);
1630 static ir_node *dereference_addr(const unary_expression_t *const expression)
1632 assert(expression->type == UNEXPR_DEREFERENCE);
1633 return expression_to_firm(expression->value);
1636 static ir_node *expression_to_addr(const expression_t *expression)
1638 switch(expression->type) {
1639 case EXPR_REFERENCE:
1640 return reference_addr(&expression->reference);
1641 case EXPR_ARRAY_ACCESS:
1642 return array_access_addr(&expression->array_access);
1644 return select_addr(&expression->select);
1646 return call_expression_to_firm(&expression->call);
1648 const unary_expression_t *const unary_expr = &expression->unary;
1649 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1650 return dereference_addr(unary_expr);
1657 panic("trying to get address of non-lvalue");
1660 static ir_node *_expression_to_firm(const expression_t *expression)
1662 switch(expression->type) {
1664 return const_to_firm(&expression->conste);
1665 case EXPR_STRING_LITERAL:
1666 return string_literal_to_firm(&expression->string);
1667 case EXPR_REFERENCE:
1668 return reference_expression_to_firm(&expression->reference);
1670 return call_expression_to_firm(&expression->call);
1672 return unary_expression_to_firm(&expression->unary);
1674 return binary_expression_to_firm(&expression->binary);
1675 case EXPR_ARRAY_ACCESS:
1676 return array_access_to_firm(&expression->array_access);
1678 return sizeof_to_firm(&expression->sizeofe);
1679 case EXPR_CONDITIONAL:
1680 return conditional_to_firm(&expression->conditional);
1682 return select_to_firm(&expression->select);
1683 case EXPR_CLASSIFY_TYPE:
1684 return classify_type_to_firm(&expression->classify_type);
1686 case EXPR_PRETTY_FUNCTION:
1687 return function_name_to_firm(&expression->string);
1688 case EXPR_STATEMENT:
1689 return statement_expression_to_firm(&expression->statement);
1692 case EXPR_BUILTIN_SYMBOL:
1693 panic("unimplemented expression found");
1699 panic("invalid expression found");
1702 static ir_node *expression_to_firm(const expression_t *expression)
1704 ir_node *res = _expression_to_firm(expression);
1706 if(res != NULL && get_irn_mode(res) == mode_b) {
1707 ir_mode *mode = get_ir_mode(expression->base.datatype);
1708 res = create_conv(NULL, res, mode);
1714 static ir_node *expression_to_modeb(const expression_t *expression)
1716 ir_node *res = _expression_to_firm(expression);
1717 res = create_conv(NULL, res, mode_b);
1723 * create a short-circuit expression evaluation that tries to construct
1724 * efficient control flow structures for &&, || and ! expressions
1726 static void create_condition_evaluation(const expression_t *expression,
1727 ir_node *true_block,
1728 ir_node *false_block)
1730 switch(expression->type) {
1732 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1733 if(unary_expression->type == UNEXPR_NOT) {
1734 create_condition_evaluation(unary_expression->value, false_block,
1741 binary_expression_t *binary_expression
1742 = (binary_expression_t*) expression;
1743 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1744 ir_node *cur_block = get_cur_block();
1745 ir_node *extra_block = new_immBlock();
1746 set_cur_block(cur_block);
1747 create_condition_evaluation(binary_expression->left, extra_block,
1749 mature_immBlock(extra_block);
1750 set_cur_block(extra_block);
1751 create_condition_evaluation(binary_expression->right, true_block,
1755 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1756 ir_node *cur_block = get_cur_block();
1757 ir_node *extra_block = new_immBlock();
1758 set_cur_block(cur_block);
1759 create_condition_evaluation(binary_expression->left, true_block,
1761 mature_immBlock(extra_block);
1762 set_cur_block(extra_block);
1763 create_condition_evaluation(binary_expression->right, true_block,
1773 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1774 ir_node *condition = expression_to_modeb(expression);
1775 ir_node *cond = new_d_Cond(dbgi, condition);
1776 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1777 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1779 add_immBlock_pred(true_block, true_proj);
1780 add_immBlock_pred(false_block, false_proj);
1782 set_cur_block(NULL);
1786 static void return_statement_to_firm(return_statement_t *statement)
1788 if(get_cur_block() == NULL)
1791 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1793 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1797 if(get_method_n_ress(func_irtype) > 0) {
1798 ir_type *res_type = get_method_res_type(func_irtype, 0);
1800 if(is_compound_type(res_type)) {
1801 ir_entity *entity = get_method_value_res_ent(func_irtype, 0);
1803 ir_node *frame = get_irg_frame(current_ir_graph);
1804 ir_node *nomem = new_NoMem();
1805 ir_node *source_addr = expression_to_addr(statement->return_value);
1806 ir_node *dest_addr = new_simpleSel(nomem, frame, entity);
1808 ir_node *store = get_store();
1809 ir_node *copyb = new_d_CopyB(dbgi, store, dest_addr, source_addr,
1812 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1813 set_store(copyb_mem);
1819 if(statement->return_value != NULL) {
1820 in[0] = expression_to_firm(statement->return_value);
1822 ir_mode *mode = get_type_mode(res_type);
1823 in[0] = new_Unknown(mode);
1827 /* build return_value for its side effects */
1828 if(statement->return_value != NULL) {
1829 expression_to_firm(statement->return_value);
1834 ir_node *store = get_store();
1835 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1837 ir_node *end_block = get_irg_end_block(current_ir_graph);
1838 add_immBlock_pred(end_block, ret);
1840 set_cur_block(NULL);
1843 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1845 if(get_cur_block() == NULL)
1848 return expression_to_firm(statement->expression);
1851 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1853 ir_node *result = NULL;
1854 statement_t *statement = compound->statements;
1855 for( ; statement != NULL; statement = statement->base.next) {
1856 //context2firm(&statement->context);
1858 if(statement->base.next == NULL
1859 && statement->type == STATEMENT_EXPRESSION) {
1860 result = expression_statement_to_firm(
1861 (expression_statement_t*) statement);
1864 statement_to_firm(statement);
1870 static void if_statement_to_firm(if_statement_t *statement)
1872 ir_node *cur_block = get_cur_block();
1874 ir_node *fallthrough_block = new_immBlock();
1876 /* the true (blocks) */
1877 ir_node *true_block;
1878 if (statement->true_statement != NULL) {
1879 true_block = new_immBlock();
1880 statement_to_firm(statement->true_statement);
1881 if(get_cur_block() != NULL) {
1882 ir_node *jmp = new_Jmp();
1883 add_immBlock_pred(fallthrough_block, jmp);
1886 true_block = fallthrough_block;
1889 /* the false (blocks) */
1890 ir_node *false_block;
1891 if(statement->false_statement != NULL) {
1892 false_block = new_immBlock();
1894 statement_to_firm(statement->false_statement);
1895 if(get_cur_block() != NULL) {
1896 ir_node *jmp = new_Jmp();
1897 add_immBlock_pred(fallthrough_block, jmp);
1900 false_block = fallthrough_block;
1903 /* create the condition */
1904 if(cur_block != NULL) {
1905 set_cur_block(cur_block);
1906 create_condition_evaluation(statement->condition, true_block,
1910 mature_immBlock(true_block);
1911 if(false_block != fallthrough_block) {
1912 mature_immBlock(false_block);
1914 mature_immBlock(fallthrough_block);
1916 set_cur_block(fallthrough_block);
1919 static void while_statement_to_firm(while_statement_t *statement)
1921 ir_node *jmp = NULL;
1922 if(get_cur_block() != NULL) {
1926 /* create the header block */
1927 ir_node *header_block = new_immBlock();
1929 add_immBlock_pred(header_block, jmp);
1932 /* the false block */
1933 ir_node *false_block = new_immBlock();
1936 ir_node *body_block;
1937 if (statement->body != NULL) {
1938 ir_node *old_continue_label = continue_label;
1939 ir_node *old_break_label = break_label;
1940 continue_label = header_block;
1941 break_label = false_block;
1943 body_block = new_immBlock();
1944 statement_to_firm(statement->body);
1946 assert(continue_label == header_block);
1947 assert(break_label == false_block);
1948 continue_label = old_continue_label;
1949 break_label = old_break_label;
1951 if(get_cur_block() != NULL) {
1953 add_immBlock_pred(header_block, jmp);
1956 body_block = header_block;
1959 /* create the condition */
1960 set_cur_block(header_block);
1962 create_condition_evaluation(statement->condition, body_block, false_block);
1963 mature_immBlock(body_block);
1964 mature_immBlock(false_block);
1965 mature_immBlock(header_block);
1967 set_cur_block(false_block);
1970 static void do_while_statement_to_firm(do_while_statement_t *statement)
1972 ir_node *jmp = NULL;
1973 if(get_cur_block() != NULL) {
1977 /* create the header block */
1978 ir_node *header_block = new_immBlock();
1980 /* the false block */
1981 ir_node *false_block = new_immBlock();
1984 ir_node *body_block = new_immBlock();
1986 add_immBlock_pred(body_block, jmp);
1989 if (statement->body != NULL) {
1990 ir_node *old_continue_label = continue_label;
1991 ir_node *old_break_label = break_label;
1992 continue_label = header_block;
1993 break_label = false_block;
1995 statement_to_firm(statement->body);
1997 assert(continue_label == header_block);
1998 assert(break_label == false_block);
1999 continue_label = old_continue_label;
2000 break_label = old_break_label;
2002 if (get_cur_block() == NULL) {
2003 mature_immBlock(header_block);
2004 mature_immBlock(body_block);
2005 mature_immBlock(false_block);
2010 ir_node *body_jmp = new_Jmp();
2011 add_immBlock_pred(header_block, body_jmp);
2012 mature_immBlock(header_block);
2014 /* create the condition */
2015 set_cur_block(header_block);
2017 create_condition_evaluation(statement->condition, body_block, false_block);
2018 mature_immBlock(body_block);
2019 mature_immBlock(false_block);
2020 mature_immBlock(header_block);
2022 set_cur_block(false_block);
2025 static void for_statement_to_firm(for_statement_t *statement)
2027 ir_node *jmp = NULL;
2028 if (get_cur_block() != NULL) {
2029 if(statement->initialisation != NULL) {
2030 expression_to_firm(statement->initialisation);
2035 /* create the step block */
2036 ir_node *const step_block = new_immBlock();
2037 if (statement->step != NULL) {
2038 expression_to_firm(statement->step);
2040 ir_node *const step_jmp = new_Jmp();
2042 /* create the header block */
2043 ir_node *const header_block = new_immBlock();
2045 add_immBlock_pred(header_block, jmp);
2047 add_immBlock_pred(header_block, step_jmp);
2049 /* the false block */
2050 ir_node *const false_block = new_immBlock();
2053 ir_node * body_block;
2054 if (statement->body != NULL) {
2055 ir_node *const old_continue_label = continue_label;
2056 ir_node *const old_break_label = break_label;
2057 continue_label = step_block;
2058 break_label = false_block;
2060 body_block = new_immBlock();
2061 statement_to_firm(statement->body);
2063 assert(continue_label == step_block);
2064 assert(break_label == false_block);
2065 continue_label = old_continue_label;
2066 break_label = old_break_label;
2068 if (get_cur_block() != NULL) {
2070 add_immBlock_pred(step_block, jmp);
2073 body_block = step_block;
2076 /* create the condition */
2077 set_cur_block(header_block);
2078 if (statement->condition != NULL) {
2079 create_condition_evaluation(statement->condition, body_block,
2082 keep_alive(header_block);
2084 add_immBlock_pred(body_block, jmp);
2087 mature_immBlock(body_block);
2088 mature_immBlock(false_block);
2089 mature_immBlock(step_block);
2090 mature_immBlock(header_block);
2091 mature_immBlock(false_block);
2093 set_cur_block(false_block);
2096 static void create_declaration_entity(declaration_t *declaration,
2097 declaration_type_t declaration_type,
2098 ir_type *parent_type)
2100 ident *id = new_id_from_str(declaration->symbol->string);
2101 ir_type *irtype = get_ir_type(declaration->type);
2102 ir_entity *entity = new_entity(parent_type, id, irtype);
2103 set_entity_ld_ident(entity, id);
2105 declaration->declaration_type = (unsigned char) declaration_type;
2106 declaration->v.entity = entity;
2107 set_entity_variability(entity, variability_uninitialized);
2108 /* TODO: visibility? */
2111 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2113 enum compound_graph_entry_type_t {
2114 COMPOUND_GRAPH_ENTRY_ARRAY,
2115 COMPOUND_GRAPH_ENTRY_COMPOUND
2118 struct compound_graph_path_entry_t {
2124 compound_graph_path_entry_t *prev;
2127 static void create_initializer_object(initializer_t *initializer, type_t *type,
2128 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2130 static compound_graph_path *create_compound_path(ir_type *type,
2131 compound_graph_path_entry_t *entry, int len)
2133 compound_graph_path *path = new_compound_graph_path(type, len);
2136 for( ; entry != NULL; entry = entry->prev, --i) {
2138 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2139 set_compound_graph_path_node(path, i, entry->v.entity);
2141 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2142 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2150 static void create_initializer_value(initializer_value_t *initializer,
2152 compound_graph_path_entry_t *entry,
2155 ir_node *node = expression_to_firm(initializer->value);
2156 ir_type *type = get_entity_type(entity);
2157 compound_graph_path *path = create_compound_path(type, entry, len);
2158 add_compound_ent_value_w_path(entity, node, path);
2161 static void create_initializer_compound(initializer_list_t *initializer,
2162 compound_type_t *type,
2164 compound_graph_path_entry_t *last_entry,
2167 declaration_t *compound_declaration = type->declaration;
2169 declaration_t *compound_entry = compound_declaration->context.declarations;
2171 compound_graph_path_entry_t entry;
2172 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2173 entry.prev = last_entry;
2177 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2178 if(compound_entry->symbol == NULL)
2180 if(compound_entry->namespc != NAMESPACE_NORMAL)
2183 if(i >= initializer->len)
2186 entry.v.entity = compound_entry->v.entity;
2188 initializer_t *sub_initializer = initializer->initializers[i];
2190 assert(compound_entry != NULL);
2191 assert(compound_entry->declaration_type
2192 == DECLARATION_TYPE_COMPOUND_MEMBER);
2194 if(sub_initializer->type == INITIALIZER_VALUE) {
2195 create_initializer_value(&sub_initializer->value,
2196 entity, &entry, len);
2198 type_t *entry_type = skip_typeref(compound_entry->type);
2199 create_initializer_object(sub_initializer, entry_type, entity,
2207 static void create_initializer_array(initializer_list_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;
2221 for(i = 0; i < initializer->len; ++i) {
2222 entry.v.array_index = i;
2224 initializer_t *sub_initializer = initializer->initializers[i];
2226 if(sub_initializer->type == INITIALIZER_VALUE) {
2227 create_initializer_value(&sub_initializer->value,
2228 entity, &entry, len);
2230 create_initializer_object(sub_initializer, element_type, entity,
2236 /* TODO: initialize rest... */
2237 if(type->size_expression != NULL) {
2238 size_t array_len = fold_constant(type->size_expression);
2239 for( ; i < array_len; ++i) {
2246 static void create_initializer_string(initializer_string_t *initializer,
2247 array_type_t *type, ir_entity *entity,
2248 compound_graph_path_entry_t *last_entry,
2251 type_t *element_type = type->element_type;
2252 element_type = skip_typeref(element_type);
2254 compound_graph_path_entry_t entry;
2255 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2256 entry.prev = last_entry;
2259 ir_type *irtype = get_entity_type(entity);
2260 size_t arr_len = get_array_type_size(type);
2261 const char *p = initializer->string;
2263 for(i = 0; i < arr_len; ++i, ++p) {
2264 entry.v.array_index = i;
2266 ir_node *node = new_Const_long(mode_Bs, *p);
2267 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2268 add_compound_ent_value_w_path(entity, node, path);
2275 static void create_initializer_object(initializer_t *initializer, type_t *type,
2276 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2278 if(type->type == TYPE_ARRAY) {
2279 array_type_t *array_type = &type->array;
2281 if(initializer->type == INITIALIZER_STRING) {
2282 initializer_string_t *string = &initializer->string;
2283 create_initializer_string(string, array_type, entity, entry, len);
2285 assert(initializer->type == INITIALIZER_LIST);
2286 initializer_list_t *list = &initializer->list;
2287 create_initializer_array(list, array_type, entity, entry, len);
2290 assert(initializer->type == INITIALIZER_LIST);
2291 initializer_list_t *list = &initializer->list;
2293 assert(type->type == TYPE_COMPOUND_STRUCT
2294 || type->type == TYPE_COMPOUND_UNION);
2295 compound_type_t *compound_type = &type->compound;
2296 create_initializer_compound(list, compound_type, entity, entry, len);
2300 static void create_initializer_local_variable_entity(declaration_t *declaration)
2302 initializer_t *initializer = declaration->init.initializer;
2303 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2304 ir_entity *entity = declaration->v.entity;
2305 ir_node *memory = get_store();
2306 ir_node *nomem = new_NoMem();
2307 ir_node *frame = get_irg_frame(current_ir_graph);
2308 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2310 if(is_atomic_entity(entity)) {
2311 assert(initializer->type == INITIALIZER_VALUE);
2312 initializer_value_t *initializer_value = &initializer->value;
2314 ir_node *value = expression_to_firm(initializer_value->value);
2315 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2316 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2317 set_store(store_mem);
2321 /* create a "template" entity which is copied to the entity on the stack */
2322 ident *id = unique_ident("initializer");
2323 ir_type *irtype = get_ir_type(declaration->type);
2324 ir_type *global_type = get_glob_type();
2325 ir_entity *init_entity = new_entity(global_type, id, irtype);
2326 set_entity_ld_ident(init_entity, id);
2328 set_entity_variability(init_entity, variability_initialized);
2329 set_entity_visibility(init_entity, visibility_local);
2331 ir_graph *old_current_ir_graph = current_ir_graph;
2332 current_ir_graph = get_const_code_irg();
2334 type_t *type = skip_typeref(declaration->type);
2335 create_initializer_object(initializer, type, init_entity, NULL, 0);
2337 assert(current_ir_graph == get_const_code_irg());
2338 current_ir_graph = old_current_ir_graph;
2340 ir_node *src_addr = create_symconst(dbgi, init_entity);
2341 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2343 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2344 set_store(copyb_mem);
2347 static void create_initializer(declaration_t *declaration)
2349 initializer_t *initializer = declaration->init.initializer;
2350 if(initializer == NULL)
2353 declaration_type_t declaration_type
2354 = (declaration_type_t) declaration->declaration_type;
2355 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2356 create_initializer_local_variable_entity(declaration);
2360 if(initializer->type == INITIALIZER_VALUE) {
2361 initializer_value_t *initializer_value = &initializer->value;
2363 ir_node *value = expression_to_firm(initializer_value->value);
2365 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2366 set_value(declaration->v.value_number, value);
2368 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2370 ir_entity *entity = declaration->v.entity;
2372 set_entity_variability(entity, variability_initialized);
2373 set_atomic_ent_value(entity, value);
2376 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2377 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2379 ir_entity *entity = declaration->v.entity;
2380 set_entity_variability(entity, variability_initialized);
2382 type_t *type = skip_typeref(declaration->type);
2383 create_initializer_object(initializer, type, entity, NULL, 0);
2387 static void create_local_variable(declaration_t *declaration)
2389 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2391 bool needs_entity = declaration->address_taken;
2392 type_t *type = skip_typeref(declaration->type);
2394 if(type->type == TYPE_ARRAY
2395 || type->type == TYPE_COMPOUND_STRUCT
2396 || type->type == TYPE_COMPOUND_UNION) {
2397 needs_entity = true;
2401 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2402 create_declaration_entity(declaration,
2403 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2406 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2407 declaration->v.value_number = next_value_number_function;
2408 ++next_value_number_function;
2411 create_initializer(declaration);
2414 static void create_local_static_variable(declaration_t *declaration)
2416 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2418 type_t *type = skip_typeref(declaration->type);
2419 ir_type *global_type = get_glob_type();
2420 ident *id = unique_ident(declaration->symbol->string);
2421 ir_type *irtype = get_ir_type(type);
2422 ir_entity *entity = new_entity(global_type, id, irtype);
2423 set_entity_ld_ident(entity, id);
2425 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2426 declaration->v.entity = entity;
2427 set_entity_variability(entity, variability_uninitialized);
2428 set_entity_visibility(entity, visibility_local);
2430 ir_graph *old_current_ir_graph = current_ir_graph;
2431 current_ir_graph = get_const_code_irg();
2433 create_initializer(declaration);
2435 assert(current_ir_graph == get_const_code_irg());
2436 current_ir_graph = old_current_ir_graph;
2439 static void declaration_statement_to_firm(declaration_statement_t *statement)
2441 declaration_t *declaration = statement->declarations_begin;
2442 declaration_t *end = statement->declarations_end->next;
2443 for( ; declaration != end; declaration = declaration->next) {
2444 type_t *type = declaration->type;
2446 switch ((storage_class_tag_t)declaration->storage_class) {
2447 case STORAGE_CLASS_TYPEDEF:
2449 case STORAGE_CLASS_STATIC:
2450 create_local_static_variable(declaration);
2452 case STORAGE_CLASS_ENUM_ENTRY:
2453 panic("enum entry declaration in local block found");
2454 case STORAGE_CLASS_EXTERN:
2455 panic("extern declaration in local block found");
2456 case STORAGE_CLASS_NONE:
2457 case STORAGE_CLASS_AUTO:
2458 case STORAGE_CLASS_REGISTER:
2459 if(type->type == TYPE_FUNCTION) {
2460 panic("nested functions not supported yet");
2462 create_local_variable(declaration);
2465 case STORAGE_CLASS_THREAD:
2466 case STORAGE_CLASS_THREAD_EXTERN:
2467 case STORAGE_CLASS_THREAD_STATIC:
2470 panic("invalid storage class found");
2474 static void create_jump_statement(const statement_t *statement,
2475 ir_node *target_block)
2477 if(get_cur_block() == NULL)
2480 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2481 ir_node *jump = new_d_Jmp(dbgi);
2482 add_immBlock_pred(target_block, jump);
2484 set_cur_block(NULL);
2487 static void switch_statement_to_firm(const switch_statement_t *statement)
2489 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2491 ir_node *expression = expression_to_firm(statement->expression);
2492 ir_node *cond = new_d_Cond(dbgi, expression);
2493 ir_node *break_block = new_immBlock();
2495 set_cur_block(NULL);
2497 ir_node *const old_switch_cond = current_switch_cond;
2498 ir_node *const old_break_label = break_label;
2499 const bool old_saw_default_label = saw_default_label;
2500 current_switch_cond = cond;
2501 break_label = break_block;
2503 statement_to_firm(statement->body);
2505 if(get_cur_block() != NULL) {
2506 ir_node *jmp = new_Jmp();
2507 add_immBlock_pred(break_block, jmp);
2510 if (!saw_default_label) {
2511 set_cur_block(get_nodes_block(cond));
2512 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2513 MAGIC_DEFAULT_PN_NUMBER);
2514 add_immBlock_pred(break_block, proj);
2517 assert(current_switch_cond == cond);
2518 assert(break_label == break_block);
2519 current_switch_cond = old_switch_cond;
2520 break_label = old_break_label;
2521 saw_default_label = old_saw_default_label;
2523 mature_immBlock(break_block);
2524 set_cur_block(break_block);
2527 static long fold_constant(const expression_t *expression)
2529 ir_graph *old_current_ir_graph = current_ir_graph;
2530 current_ir_graph = get_const_code_irg();
2532 ir_node *cnst = expression_to_firm(expression);
2533 if(!is_Const(cnst)) {
2534 panic("couldn't fold constantl");
2536 tarval *tv = get_Const_tarval(cnst);
2537 if(!tarval_is_long(tv)) {
2538 panic("folded constant not an integer");
2541 long res = get_tarval_long(tv);
2543 current_ir_graph = old_current_ir_graph;
2547 static void case_label_to_firm(const case_label_statement_t *statement)
2549 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2551 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2553 /* let's create a node and hope firm constant folding creates a Const
2556 set_cur_block(get_nodes_block(current_switch_cond));
2557 if(statement->expression) {
2558 long pn = fold_constant(statement->expression);
2559 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2560 /* oops someone detected our cheating... */
2561 panic("magic default pn used");
2563 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2565 saw_default_label = true;
2566 proj = new_d_defaultProj(dbgi, current_switch_cond,
2567 MAGIC_DEFAULT_PN_NUMBER);
2570 ir_node *block = new_immBlock();
2571 if (fallthrough != NULL) {
2572 add_immBlock_pred(block, fallthrough);
2574 add_immBlock_pred(block, proj);
2575 mature_immBlock(block);
2577 statement_to_firm(statement->label_statement);
2580 static ir_node *get_label_block(declaration_t *label)
2582 assert(label->namespc == NAMESPACE_LABEL);
2584 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2585 return label->v.block;
2587 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2589 ir_node *old_cur_block = get_cur_block();
2590 ir_node *block = new_immBlock();
2591 set_cur_block(old_cur_block);
2593 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2594 label->v.block = block;
2596 ARR_APP1(ir_node *, imature_blocks, block);
2601 static void label_to_firm(const label_statement_t *statement)
2603 ir_node *block = get_label_block(statement->label);
2605 if(get_cur_block() != NULL) {
2606 ir_node *jmp = new_Jmp();
2607 add_immBlock_pred(block, jmp);
2610 set_cur_block(block);
2613 statement_to_firm(statement->label_statement);
2616 static void goto_to_firm(const goto_statement_t *statement)
2618 if(get_cur_block() == NULL)
2621 ir_node *block = get_label_block(statement->label);
2622 ir_node *jmp = new_Jmp();
2623 add_immBlock_pred(block, jmp);
2625 set_cur_block(NULL);
2628 static void statement_to_firm(statement_t *statement)
2630 switch(statement->type) {
2631 case STATEMENT_COMPOUND:
2632 compound_statement_to_firm((compound_statement_t*) statement);
2634 case STATEMENT_RETURN:
2635 return_statement_to_firm((return_statement_t*) statement);
2637 case STATEMENT_EXPRESSION:
2638 expression_statement_to_firm((expression_statement_t*) statement);
2641 if_statement_to_firm((if_statement_t*) statement);
2643 case STATEMENT_WHILE:
2644 while_statement_to_firm((while_statement_t*) statement);
2646 case STATEMENT_DO_WHILE:
2647 do_while_statement_to_firm((do_while_statement_t*) statement);
2649 case STATEMENT_DECLARATION:
2650 declaration_statement_to_firm((declaration_statement_t*) statement);
2652 case STATEMENT_BREAK:
2653 create_jump_statement(statement, break_label);
2655 case STATEMENT_CONTINUE:
2656 create_jump_statement(statement, continue_label);
2658 case STATEMENT_SWITCH:
2659 switch_statement_to_firm((switch_statement_t*) statement);
2661 case STATEMENT_CASE_LABEL:
2662 case_label_to_firm((case_label_statement_t*) statement);
2665 for_statement_to_firm((for_statement_t*) statement);
2667 case STATEMENT_LABEL:
2668 label_to_firm((label_statement_t*) statement);
2670 case STATEMENT_GOTO:
2671 goto_to_firm((goto_statement_t*) statement);
2676 panic("Statement not implemented\n");
2679 static int count_local_declarations(const declaration_t * decl,
2680 const declaration_t *const end)
2683 for (; decl != end; decl = decl->next) {
2684 const type_t *type = skip_typeref(decl->type);
2685 switch (type->type) {
2689 if (!decl->address_taken)
2699 static int count_decls_in_stmts(const statement_t *stmt)
2702 for (; stmt != NULL; stmt = stmt->base.next) {
2703 switch (stmt->type) {
2704 case STATEMENT_DECLARATION: {
2705 const declaration_statement_t *const decl_stmt =
2706 (const declaration_statement_t*)stmt;
2707 count += count_local_declarations(decl_stmt->declarations_begin,
2708 decl_stmt->declarations_end->next);
2712 case STATEMENT_COMPOUND: {
2713 const compound_statement_t *const comp =
2714 (const compound_statement_t*)stmt;
2715 count += count_decls_in_stmts(comp->statements);
2719 case STATEMENT_IF: {
2720 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2721 count += count_decls_in_stmts(if_stmt->true_statement);
2722 count += count_decls_in_stmts(if_stmt->false_statement);
2726 case STATEMENT_SWITCH: {
2727 const switch_statement_t *const switch_stmt =
2728 (const switch_statement_t*)stmt;
2729 count += count_decls_in_stmts(switch_stmt->body);
2733 case STATEMENT_LABEL: {
2734 const label_statement_t *const label_stmt =
2735 (const label_statement_t*)stmt;
2736 count += count_decls_in_stmts(label_stmt->label_statement);
2740 case STATEMENT_WHILE: {
2741 const while_statement_t *const while_stmt =
2742 (const while_statement_t*)stmt;
2743 count += count_decls_in_stmts(while_stmt->body);
2747 case STATEMENT_DO_WHILE: {
2748 const do_while_statement_t *const do_while_stmt =
2749 (const do_while_statement_t*)stmt;
2750 count += count_decls_in_stmts(do_while_stmt->body);
2754 case STATEMENT_FOR: {
2755 const for_statement_t *const for_stmt =
2756 (const for_statement_t*)stmt;
2757 /* TODO initialisation */
2758 count += count_decls_in_stmts(for_stmt->body);
2762 case STATEMENT_BREAK:
2763 case STATEMENT_CASE_LABEL:
2764 case STATEMENT_CONTINUE:
2765 case STATEMENT_EXPRESSION:
2766 case STATEMENT_GOTO:
2767 case STATEMENT_INVALID:
2768 case STATEMENT_RETURN:
2775 static int get_function_n_local_vars(declaration_t *declaration)
2779 /* count parameters */
2780 count += count_local_declarations(declaration->context.declarations, NULL);
2782 /* count local variables declared in body */
2783 count += count_decls_in_stmts(declaration->init.statement);
2785 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2786 * for expression statements... */
2792 static void initialize_function_parameters(declaration_t *declaration)
2794 ir_graph *irg = current_ir_graph;
2795 ir_node *args = get_irg_args(irg);
2796 ir_node *start_block = get_irg_start_block(irg);
2797 ir_type *function_irtype = get_ir_type(declaration->type);
2800 declaration_t *parameter = declaration->context.declarations;
2801 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2802 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2803 type_t *type = skip_typeref(parameter->type);
2805 bool needs_entity = parameter->address_taken;
2806 if(type->type == TYPE_COMPOUND_STRUCT
2807 || type->type == TYPE_COMPOUND_UNION) {
2808 needs_entity = true;
2812 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2813 ident *id = new_id_from_str(parameter->symbol->string);
2814 set_entity_ident(entity, id);
2816 parameter->declaration_type
2817 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2818 parameter->v.entity = entity;
2822 ir_mode *mode = get_ir_mode(parameter->type);
2824 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2826 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2827 parameter->v.value_number = next_value_number_function;
2828 ++next_value_number_function;
2830 set_value(parameter->v.value_number, proj);
2834 static void create_function(declaration_t *declaration)
2836 ir_entity *function_entity = get_function_entity(declaration);
2838 if(declaration->init.statement == NULL)
2841 current_function_decl = declaration;
2842 current_function_name = NULL;
2844 assert(imature_blocks == NULL);
2845 imature_blocks = NEW_ARR_F(ir_node*, 0);
2847 int n_local_vars = get_function_n_local_vars(declaration);
2848 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
2849 ir_node *first_block = get_cur_block();
2851 next_value_number_function = 0;
2852 initialize_function_parameters(declaration);
2854 statement_to_firm(declaration->init.statement);
2856 ir_node *end_block = get_irg_end_block(irg);
2858 /* do we have a return statement yet? */
2859 if(get_cur_block() != NULL) {
2860 assert(declaration->type->type == TYPE_FUNCTION);
2861 const function_type_t* const func_type = &declaration->type->function;
2864 if (func_type->result_type == type_void) {
2865 ret = new_Return(get_store(), 0, NULL);
2867 ir_mode *const mode = get_ir_mode(func_type->result_type);
2869 // ยง5.1.2.2.3 main implicitly returns 0
2870 if (strcmp(declaration->symbol->string, "main") == 0) {
2871 in[0] = new_Const(mode, get_mode_null(mode));
2873 in[0] = new_Unknown(mode);
2875 ret = new_Return(get_store(), 1, in);
2877 add_immBlock_pred(end_block, ret);
2880 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2881 mature_immBlock(imature_blocks[i]);
2883 DEL_ARR_F(imature_blocks);
2884 imature_blocks = NULL;
2886 mature_immBlock(first_block);
2887 mature_immBlock(end_block);
2889 irg_finalize_cons(irg);
2891 /* finalize the frame type */
2892 ir_type *frame_type = get_irg_frame_type(irg);
2893 int n = get_compound_n_members(frame_type);
2896 for(int i = 0; i < n; ++i) {
2897 ir_entity *entity = get_compound_member(frame_type, i);
2898 ir_type *entity_type = get_entity_type(entity);
2900 int align = get_type_alignment_bytes(entity_type);
2901 if(align > align_all)
2905 misalign = offset % align;
2907 offset += align - misalign;
2911 set_entity_offset(entity, offset);
2912 offset += get_type_size_bytes(entity_type);
2914 set_type_size_bytes(frame_type, offset);
2915 set_type_alignment_bytes(frame_type, align_all);
2916 set_type_state(frame_type, layout_fixed);
2921 static void create_global_variable(declaration_t *declaration)
2925 switch ((storage_class_tag_t)declaration->storage_class) {
2926 case STORAGE_CLASS_STATIC:
2927 vis = visibility_local;
2930 case STORAGE_CLASS_EXTERN:
2931 vis = visibility_external_allocated;
2934 case STORAGE_CLASS_NONE:
2935 vis = visibility_external_visible;
2938 case STORAGE_CLASS_THREAD:
2939 vis = visibility_external_visible;
2942 case STORAGE_CLASS_THREAD_EXTERN:
2943 vis = visibility_external_allocated;
2946 case STORAGE_CLASS_THREAD_STATIC:
2947 vis = visibility_local;
2951 var_type = get_tls_type();
2955 var_type = get_glob_type();
2959 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2961 set_entity_visibility(declaration->v.entity, vis);
2963 current_ir_graph = get_const_code_irg();
2964 create_initializer(declaration);
2967 case STORAGE_CLASS_TYPEDEF:
2968 case STORAGE_CLASS_AUTO:
2969 case STORAGE_CLASS_REGISTER:
2970 case STORAGE_CLASS_ENUM_ENTRY:
2973 panic("Invalid storage class for global variable");
2976 static void context_to_firm(context_t *context)
2978 /* first pass: create declarations */
2979 declaration_t *declaration = context->declarations;
2980 for( ; declaration != NULL; declaration = declaration->next) {
2981 if(declaration->namespc != NAMESPACE_NORMAL)
2983 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2984 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2986 if(declaration->symbol == NULL)
2989 type_t *type = declaration->type;
2990 if(type->type == TYPE_FUNCTION) {
2991 get_function_entity(declaration);
2993 create_global_variable(declaration);
2997 /* second pass: create code */
2998 declaration = context->declarations;
2999 for( ; declaration != NULL; declaration = declaration->next) {
3000 if(declaration->namespc != NAMESPACE_NORMAL)
3002 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3003 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3005 if(declaration->symbol == NULL)
3008 type_t *type = declaration->type;
3009 if(type->type != TYPE_FUNCTION)
3012 create_function(declaration);
3016 void translation_unit_to_firm(translation_unit_t *unit)
3018 /* just to be sure */
3019 continue_label = NULL;
3021 current_switch_cond = NULL;
3023 context_to_firm(& unit->context);