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_builtin_alloca;
32 static symbol_t *symbol_builtin_nanf;
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
101 type_void->base.firm_type = ir_type_void;
103 symbol_builtin_alloca = symbol_table_insert("__builtin_alloca");
104 symbol_builtin_nanf = symbol_table_insert("__builtin_nanf");
107 void exit_ast2firm(void)
111 static unsigned unique_id = 0;
113 static ident *unique_ident(const char *tag)
117 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
119 return new_id_from_str(buf);
122 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
124 switch(atomic_type->atype) {
125 case ATOMIC_TYPE_SCHAR:
126 case ATOMIC_TYPE_CHAR:
128 case ATOMIC_TYPE_UCHAR:
130 case ATOMIC_TYPE_SHORT:
132 case ATOMIC_TYPE_USHORT:
134 case ATOMIC_TYPE_LONG:
135 case ATOMIC_TYPE_INT:
137 case ATOMIC_TYPE_ULONG:
138 case ATOMIC_TYPE_UINT:
140 case ATOMIC_TYPE_LONGLONG:
142 case ATOMIC_TYPE_ULONGLONG:
144 case ATOMIC_TYPE_FLOAT:
146 case ATOMIC_TYPE_DOUBLE:
148 case ATOMIC_TYPE_LONG_DOUBLE:
150 case ATOMIC_TYPE_BOOL:
152 #ifdef PROVIDE_COMPLEX
153 case ATOMIC_TYPE_FLOAT_COMPLEX:
154 case ATOMIC_TYPE_DOUBLE_COMPLEX:
155 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
156 panic("complex lowering not implemented yet");
158 case ATOMIC_TYPE_FLOAT_IMAGINARY:
159 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
160 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
161 panic("imaginary lowering not implemented yet");
164 case ATOMIC_TYPE_VOID:
165 /* firm has no real void... */
167 case ATOMIC_TYPE_INVALID:
170 panic("Encountered unknown atomic type");
174 static unsigned get_type_size(type_t *type);
176 static unsigned get_atomic_type_size(const atomic_type_t *type)
178 switch(type->atype) {
179 case ATOMIC_TYPE_CHAR:
180 case ATOMIC_TYPE_SCHAR:
181 case ATOMIC_TYPE_UCHAR:
184 case ATOMIC_TYPE_SHORT:
185 case ATOMIC_TYPE_USHORT:
188 case ATOMIC_TYPE_BOOL:
189 case ATOMIC_TYPE_INT:
190 case ATOMIC_TYPE_UINT:
191 case ATOMIC_TYPE_LONG:
192 case ATOMIC_TYPE_ULONG:
193 case ATOMIC_TYPE_FLOAT:
196 case ATOMIC_TYPE_LONGLONG:
197 case ATOMIC_TYPE_ULONGLONG:
198 case ATOMIC_TYPE_DOUBLE:
201 case ATOMIC_TYPE_LONG_DOUBLE:
204 case ATOMIC_TYPE_VOID:
207 case ATOMIC_TYPE_INVALID:
210 panic("Trying to determine size of invalid atomic type");
213 static unsigned get_compound_type_size(compound_type_t *type)
215 ir_type *irtype = get_ir_type((type_t*) type);
216 return get_type_size_bytes(irtype);
219 static unsigned get_array_type_size(array_type_t *type)
221 ir_type *irtype = get_ir_type((type_t*) type);
222 return get_type_size_bytes(irtype);
225 static unsigned get_type_size(type_t *type)
227 type = skip_typeref(type);
231 return get_atomic_type_size(&type->atomic);
233 return get_mode_size_bytes(mode_Is);
234 case TYPE_COMPOUND_UNION:
235 case TYPE_COMPOUND_STRUCT:
236 return get_compound_type_size(&type->compound);
238 /* just a pointer to the function */
239 return get_mode_size_bytes(mode_P_code);
241 return get_mode_size_bytes(mode_P_data);
243 return get_array_type_size(&type->array);
250 panic("Trying to determine size of invalid type");
253 static unsigned count_parameters(const function_type_t *function_type)
257 function_parameter_t *parameter = function_type->parameters;
258 for ( ; parameter != NULL; parameter = parameter->next) {
268 static long fold_constant(const expression_t *expression);
270 static ir_type *create_atomic_type(const atomic_type_t *type)
272 ir_mode *mode = get_atomic_mode(type);
273 ident *id = get_mode_ident(mode);
274 ir_type *irtype = new_type_primitive(id, mode);
279 static ir_type *create_method_type(const function_type_t *function_type)
281 type_t *result_type = function_type->result_type;
283 ident *id = unique_ident("functiontype");
284 int n_parameters = count_parameters(function_type);
285 int n_results = result_type == type_void ? 0 : 1;
286 ir_type *irtype = new_type_method(id, n_parameters, n_results);
288 if(result_type != type_void) {
289 ir_type *restype = get_ir_type(result_type);
290 set_method_res_type(irtype, 0, restype);
293 function_parameter_t *parameter = function_type->parameters;
295 for( ; parameter != NULL; parameter = parameter->next) {
296 ir_type *p_irtype = get_ir_type(parameter->type);
297 set_method_param_type(irtype, n, p_irtype);
301 if(function_type->variadic || function_type->unspecified_parameters) {
302 set_method_variadicity(irtype, variadicity_variadic);
308 static ir_type *create_pointer_type(pointer_type_t *type)
310 type_t *points_to = type->points_to;
311 ir_type *ir_points_to;
312 /* Avoid endless recursion if the points_to type contains this poiner type
313 * again (might be a struct). We therefore first create a void* pointer
314 * and then set the real points_to type
316 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
317 ir_type_void, mode_P_data);
318 type->type.firm_type = ir_type;
320 ir_points_to = get_ir_type(points_to);
321 set_pointer_points_to_type(ir_type, ir_points_to);
326 static ir_type *create_array_type(array_type_t *type)
328 type_t *element_type = type->element_type;
329 ir_type *ir_element_type = get_ir_type(element_type);
331 ident *id = unique_ident("array");
332 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
334 if(type->size != NULL) {
335 int n_elements = fold_constant(type->size);
337 set_array_bounds_int(ir_type, 0, 0, n_elements);
339 size_t elemsize = get_type_size_bytes(ir_element_type);
340 int align = get_type_alignment_bytes(ir_element_type);
341 if(elemsize % align > 0) {
342 elemsize += align - (elemsize % align);
344 set_type_size_bytes(ir_type, n_elements * elemsize);
345 set_type_alignment_bytes(ir_type, align);
346 set_type_state(ir_type, layout_fixed);
352 #define INVALID_TYPE ((ir_type_ptr)-1)
354 static ir_type *create_struct_type(compound_type_t *type)
356 symbol_t *symbol = type->declaration->symbol;
359 id = unique_ident(symbol->string);
361 id = unique_ident("__anonymous_struct");
363 ir_type *ir_type = new_type_struct(id);
365 type->type.firm_type = ir_type;
369 declaration_t *entry = type->declaration->context.declarations;
370 for( ; entry != NULL; entry = entry->next) {
371 if(entry->namespc != NAMESPACE_NORMAL)
374 ident *ident = new_id_from_str(entry->symbol->string);
375 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
377 int entry_size = get_type_size_bytes(entry_ir_type);
378 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
379 int misalign = offset % entry_alignment;
381 offset += entry_alignment - misalign;
383 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
384 set_entity_offset(entity, offset);
385 add_struct_member(ir_type, entity);
386 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
387 entry->v.entity = entity;
389 offset += entry_size;
390 if(entry_alignment > align_all) {
391 if(entry_alignment % align_all != 0) {
392 panic("Uneven alignments not supported yet");
394 align_all = entry_alignment;
398 int misalign = offset % align_all;
400 set_type_alignment_bytes(ir_type, align_all);
401 set_type_size_bytes(ir_type, offset);
402 set_type_state(ir_type, layout_fixed);
407 static ir_type *create_union_type(compound_type_t *type)
409 declaration_t *declaration = type->declaration;
410 symbol_t *symbol = declaration->symbol;
413 id = unique_ident(symbol->string);
415 id = unique_ident("__anonymous_union");
417 ir_type *ir_type = new_type_union(id);
419 type->type.firm_type = ir_type;
423 declaration_t *entry = declaration->context.declarations;
424 for( ; entry != NULL; entry = entry->next) {
425 if(entry->namespc != NAMESPACE_NORMAL)
428 ident *ident = new_id_from_str(entry->symbol->string);
429 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
431 int entry_size = get_type_size_bytes(entry_ir_type);
432 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
434 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
435 add_union_member(ir_type, entity);
436 set_entity_offset(entity, 0);
437 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
438 entry->v.entity = entity;
440 if(entry_size > size) {
443 if(entry_alignment > align_all) {
444 if(entry_alignment % align_all != 0) {
445 panic("Uneven alignments not supported yet");
447 align_all = entry_alignment;
451 set_type_alignment_bytes(ir_type, align_all);
452 set_type_size_bytes(ir_type, size);
453 set_type_state(ir_type, layout_fixed);
458 static ir_node *expression_to_firm(const expression_t *expression);
459 static inline ir_mode *get_ir_mode(type_t *type);
461 static ir_type *create_enum_type(enum_type_t *const type)
463 type->type.firm_type = ir_type_int;
465 ir_mode *const mode = get_ir_mode((type_t*) type);
466 tarval *const one = get_mode_one(mode);
467 tarval * tv_next = get_tarval_null(mode);
469 declaration_t *declaration = type->declaration->next;
470 for (; declaration != NULL; declaration = declaration->next) {
471 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
474 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
476 expression_t *const init = declaration->init.enum_value;
478 ir_node *const cnst = expression_to_firm(init);
479 if (!is_Const(cnst)) {
480 panic("couldn't fold constant");
482 tv_next = get_Const_tarval(cnst);
484 declaration->v.enum_val = tv_next;
485 tv_next = tarval_add(tv_next, one);
491 static ir_type *get_ir_type(type_t *type)
493 assert(type != NULL);
495 type = skip_typeref(type);
497 if(type->base.firm_type != NULL) {
498 assert(type->base.firm_type != INVALID_TYPE);
499 return type->base.firm_type;
502 ir_type *firm_type = NULL;
505 firm_type = create_atomic_type(&type->atomic);
508 firm_type = create_method_type(&type->function);
511 firm_type = create_pointer_type(&type->pointer);
514 firm_type = create_array_type(&type->array);
516 case TYPE_COMPOUND_STRUCT:
517 firm_type = create_struct_type(&type->compound);
519 case TYPE_COMPOUND_UNION:
520 firm_type = create_union_type(&type->compound);
523 firm_type = create_enum_type(&type->enumt);
531 if(firm_type == NULL)
532 panic("unknown type found");
534 type->base.firm_type = firm_type;
538 static inline ir_mode *get_ir_mode(type_t *type)
540 ir_type *irtype = get_ir_type(type);
542 /* firm doesn't report a mode for arrays somehow... */
543 if(is_Array_type(irtype)) {
547 ir_mode *mode = get_type_mode(irtype);
548 assert(mode != NULL);
552 static ir_entity* get_function_entity(declaration_t *declaration)
554 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
555 return declaration->v.entity;
556 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
558 symbol_t *symbol = declaration->symbol;
559 ident *id = new_id_from_str(symbol->string);
561 ir_type *global_type = get_glob_type();
562 ir_type *ir_type_method = get_ir_type(declaration->type);
563 assert(is_Method_type(ir_type_method));
565 ir_entity *entity = new_entity(global_type, id, ir_type_method);
566 set_entity_ld_ident(entity, id);
567 if(declaration->storage_class == STORAGE_CLASS_STATIC
568 || declaration->is_inline) {
569 set_entity_visibility(entity, visibility_local);
570 } else if(declaration->init.statement != NULL) {
571 set_entity_visibility(entity, visibility_external_visible);
573 set_entity_visibility(entity, visibility_external_allocated);
576 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
577 declaration->v.entity = entity;
582 static dbg_info *get_dbg_info(const source_position_t *pos)
584 return (dbg_info*) pos;
587 static ir_node *const_to_firm(const const_expression_t *cnst)
589 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
590 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
595 if(mode_is_float(mode)) {
596 tv = new_tarval_from_double(cnst->v.float_value, mode);
598 if(mode_is_signed(mode)) {
599 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
601 len = snprintf(buf, sizeof(buf), "%llu",
602 (unsigned long long) 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(
655 const string_literal_expression_t* literal)
657 return string_to_firm(&literal->expression.source_position, "Lstr",
661 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
662 dbg_info *const dbgi)
664 if(is_compound_type(irtype) || is_Array_type(irtype)) {
668 ir_mode *const mode = get_type_mode(irtype);
669 ir_node *const memory = get_store();
670 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
671 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
672 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
677 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
679 ir_mode *mode = get_irn_mode(node);
681 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
683 if(!mode_is_float(mode))
686 /* check if there is already a Conv */
687 if (get_irn_op(node) == op_Conv) {
688 /* convert it into a strict Conv */
689 set_Conv_strict(node, 1);
693 /* otherwise create a new one */
694 return new_d_strictConv(dbgi, node, mode);
697 static ir_node *get_global_var_address(dbg_info *const dbgi,
698 const declaration_t *const decl)
700 assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
702 ir_entity *const entity = decl->v.entity;
703 switch ((storage_class_tag_t)decl->storage_class) {
704 case STORAGE_CLASS_THREAD:
705 case STORAGE_CLASS_THREAD_EXTERN:
706 case STORAGE_CLASS_THREAD_STATIC: {
707 ir_node *const no_mem = new_NoMem();
708 ir_node *const tls = get_irg_tls(current_ir_graph);
709 return new_d_simpleSel(dbgi, no_mem, tls, entity);
713 return create_symconst(dbgi, entity);
717 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
719 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
720 declaration_t *declaration = ref->declaration;
721 type_t *type = skip_typeref(declaration->type);
723 switch((declaration_type_t) declaration->declaration_type) {
724 case DECLARATION_TYPE_UNKNOWN:
725 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
731 case DECLARATION_TYPE_ENUM_ENTRY: {
732 ir_mode *const mode = get_ir_mode(type);
733 return new_Const(mode, declaration->v.enum_val);
736 case DECLARATION_TYPE_LOCAL_VARIABLE: {
737 ir_mode *mode = get_ir_mode(type);
738 return get_value(declaration->v.value_number, mode);
740 case DECLARATION_TYPE_FUNCTION: {
741 return create_symconst(dbgi, declaration->v.entity);
743 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
744 ir_node *const addr = get_global_var_address(dbgi, declaration);
745 ir_type *const irtype = get_entity_type(declaration->v.entity);
746 return deref_address(irtype, addr, dbgi);
749 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
750 ir_entity *entity = declaration->v.entity;
751 ir_node *frame = get_irg_frame(current_ir_graph);
752 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
753 ir_type *irtype = get_entity_type(entity);
754 return deref_address(irtype, sel, dbgi);
757 case DECLARATION_TYPE_COMPOUND_MEMBER:
758 case DECLARATION_TYPE_LABEL_BLOCK:
759 panic("not implemented reference type");
762 panic("reference to declaration with unknown type found");
765 static ir_node *reference_addr(const reference_expression_t *ref)
767 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
768 declaration_t *declaration = ref->declaration;
770 switch((declaration_type_t) declaration->declaration_type) {
771 case DECLARATION_TYPE_UNKNOWN:
773 case DECLARATION_TYPE_LOCAL_VARIABLE:
774 panic("local variable without entity has no address");
775 case DECLARATION_TYPE_FUNCTION: {
776 return create_symconst(dbgi, declaration->v.entity);
778 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
779 ir_node *const addr = get_global_var_address(dbgi, declaration);
782 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
783 ir_entity *entity = declaration->v.entity;
784 ir_node *frame = get_irg_frame(current_ir_graph);
785 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
790 case DECLARATION_TYPE_ENUM_ENTRY:
791 panic("trying to reference enum entry");
793 case DECLARATION_TYPE_COMPOUND_MEMBER:
794 case DECLARATION_TYPE_LABEL_BLOCK:
795 panic("not implemented reference type");
798 panic("reference to declaration with unknown type found");
801 static ir_node *process_builtin_call(const call_expression_t *call)
803 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
805 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
806 builtin_symbol_expression_t *builtin
807 = (builtin_symbol_expression_t*) call->function;
808 symbol_t *symbol = builtin->symbol;
810 if(symbol == symbol_builtin_alloca) {
811 if(call->arguments == NULL || call->arguments->next != NULL) {
812 panic("invalid number of parameters on __builtin_alloca");
814 expression_t *argument = call->arguments->expression;
815 ir_node *size = expression_to_firm(argument);
817 ir_node *store = get_store();
818 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
820 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
822 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
825 } else if(symbol == symbol_builtin_nanf) {
826 /* Ignore string for now... */
827 ir_mode *mode = mode_D;
828 tarval *tv = get_mode_NAN(mode);
829 ir_node *res = new_d_Const(dbgi, mode, tv);
832 panic("Unsupported builtin found\n");
836 static ir_node *call_expression_to_firm(const call_expression_t *call)
838 assert(get_cur_block() != NULL);
840 expression_t *function = call->function;
841 if(function->type == EXPR_BUILTIN_SYMBOL) {
842 return process_builtin_call(call);
844 ir_node *callee = expression_to_firm(function);
846 type_t *type = function->base.datatype;
847 assert(type->type == TYPE_POINTER);
848 pointer_type_t *const ptr_type = &type->pointer;
849 assert(ptr_type->points_to->type == TYPE_FUNCTION);
850 function_type_t *function_type = &ptr_type->points_to->function;
852 int n_parameters = 0;
853 call_argument_t *argument = call->arguments;
854 for( ; argument != NULL; argument = argument->next) {
858 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
860 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
861 ir_type *new_method_type = NULL;
862 if(function_type->variadic || function_type->unspecified_parameters) {
863 /* we need to construct a new method type matching the call
865 int n_res = get_method_n_ress(ir_method_type);
866 new_method_type = new_type_method(unique_ident("calltype"),
867 n_parameters, n_res);
868 set_method_calling_convention(new_method_type,
869 get_method_calling_convention(ir_method_type));
870 set_method_additional_properties(new_method_type,
871 get_method_additional_properties(ir_method_type));
873 for(int i = 0; i < n_res; ++i) {
874 set_method_res_type(new_method_type, i,
875 get_method_res_type(ir_method_type, i));
878 ir_node *in[n_parameters];
880 argument = call->arguments;
882 for( ; argument != NULL; argument = argument->next) {
883 expression_t *expression = argument->expression;
884 ir_node *arg_node = expression_to_firm(expression);
886 arg_node = do_strict_conv(dbgi, arg_node);
889 if(new_method_type != NULL) {
890 ir_type *irtype = get_ir_type(expression->base.datatype);
891 set_method_param_type(new_method_type, n, irtype);
896 assert(n == n_parameters);
898 if(new_method_type != NULL)
899 ir_method_type = new_method_type;
901 ir_node *store = get_store();
902 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
904 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
907 type_t *result_type = skip_typeref(function_type->result_type);
908 ir_node *result = NULL;
910 if(!is_type_atomic(result_type, ATOMIC_TYPE_VOID)) {
912 if(is_type_scalar(result_type)) {
913 mode = get_ir_mode(result_type);
917 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
918 result = new_d_Proj(dbgi, resproj, mode, 0);
924 static void statement_to_firm(statement_t *statement);
925 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
927 static ir_node *expression_to_addr(const expression_t *expression);
928 static void create_condition_evaluation(const expression_t *expression,
930 ir_node *false_block);
932 static void set_value_for_expression(const expression_t *expression,
935 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
936 value = do_strict_conv(dbgi, value);
938 if(expression->type == EXPR_REFERENCE) {
939 reference_expression_t *ref = (reference_expression_t*) expression;
941 declaration_t *declaration = ref->declaration;
942 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
943 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
944 set_value(declaration->v.value_number, value);
949 ir_node *addr = expression_to_addr(expression);
950 ir_node *memory = get_store();
952 type_t *type = skip_typeref(expression->base.datatype);
953 if(is_type_scalar(type)) {
954 ir_node *store = new_d_Store(dbgi, memory, addr, value);
955 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
956 set_store(store_mem);
958 ir_type *irtype = get_ir_type(type);
959 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
960 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
961 set_store(copyb_mem);
965 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
967 ir_mode *value_mode = get_irn_mode(value);
969 if (value_mode == dest_mode || is_Bad(value))
972 if(dest_mode == mode_b) {
973 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
974 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
975 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
979 return new_d_Conv(dbgi, value, dest_mode);
982 static ir_node *create_incdec(const unary_expression_t *expression)
984 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
985 type_t *type = expression->expression.datatype;
986 ir_mode *mode = get_ir_mode(type);
987 expression_t *value = expression->value;
989 ir_node *value_node = expression_to_firm(value);
992 if(type->type == TYPE_POINTER) {
993 pointer_type_t *pointer_type = &type->pointer;
994 unsigned elem_size = get_type_size(pointer_type->points_to);
995 offset = new_Const_long(mode_Is, elem_size);
997 assert(is_type_arithmetic(type));
998 offset = new_Const(mode, get_mode_one(mode));
1001 switch(expression->type) {
1002 case UNEXPR_POSTFIX_INCREMENT: {
1003 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1004 set_value_for_expression(value, new_value);
1007 case UNEXPR_POSTFIX_DECREMENT: {
1008 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1009 set_value_for_expression(value, new_value);
1012 case UNEXPR_PREFIX_INCREMENT: {
1013 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1014 set_value_for_expression(value, new_value);
1017 case UNEXPR_PREFIX_DECREMENT: {
1018 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1019 set_value_for_expression(value, new_value);
1023 panic("no incdec expr in create_incdec");
1028 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1030 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1031 type_t *type = skip_typeref(expression->expression.datatype);
1033 if(expression->type == UNEXPR_TAKE_ADDRESS)
1034 return expression_to_addr(expression->value);
1036 const expression_t *value = expression->value;
1037 ir_node *value_node = expression_to_firm(value);
1039 switch(expression->type) {
1041 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
1044 case UNEXPR_BITWISE_NEGATE:
1045 return new_d_Not(dbgi, value_node, get_ir_mode(type));
1047 if(get_irn_mode(value_node) != mode_b) {
1048 value_node = create_conv(dbgi, value_node, mode_b);
1050 value_node = new_d_Not(dbgi, value_node, mode_b);
1051 ir_mode *const mode = get_ir_mode(type);
1052 if(mode != mode_b) {
1053 value_node = create_conv(dbgi, value_node, mode);
1057 case UNEXPR_DEREFERENCE: {
1058 ir_type *irtype = get_ir_type(type);
1059 return deref_address(irtype, value_node, dbgi);
1061 case UNEXPR_POSTFIX_INCREMENT:
1062 case UNEXPR_POSTFIX_DECREMENT:
1063 case UNEXPR_PREFIX_INCREMENT:
1064 case UNEXPR_PREFIX_DECREMENT:
1065 return create_incdec(expression);
1067 ir_node *node = create_conv(dbgi, value_node, get_ir_mode(type));
1068 node = do_strict_conv(dbgi, node);
1071 case UNEXPR_CAST_IMPLICIT:
1072 return create_conv(dbgi, value_node, get_ir_mode(type));
1074 case UNEXPR_TAKE_ADDRESS:
1075 case UNEXPR_INVALID:
1078 panic("invalid UNEXPR type found");
1081 static long get_pnc(binary_expression_type_t type)
1084 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1085 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1086 case BINEXPR_LESS: return pn_Cmp_Lt;
1087 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1088 case BINEXPR_GREATER: return pn_Cmp_Gt;
1089 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1093 panic("trying to get pn_Cmp from non-comparison binexpr type");
1096 static ir_node *create_lazy_op(const binary_expression_t *expression)
1098 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1099 type_t *type = expression->expression.datatype;
1100 ir_mode *mode = get_ir_mode(type);
1102 ir_node *cur_block = get_cur_block();
1104 ir_node *one_block = new_immBlock();
1105 ir_node *one = new_Const(mode, get_mode_one(mode));
1106 ir_node *jmp_one = new_d_Jmp(dbgi);
1108 ir_node *zero_block = new_immBlock();
1109 ir_node *zero = new_Const(mode, get_mode_null(mode));
1110 ir_node *jmp_zero = new_d_Jmp(dbgi);
1112 set_cur_block(cur_block);
1113 create_condition_evaluation((const expression_t*) expression,
1114 one_block, zero_block);
1115 mature_immBlock(one_block);
1116 mature_immBlock(zero_block);
1118 ir_node *common_block = new_immBlock();
1119 add_immBlock_pred(common_block, jmp_one);
1120 add_immBlock_pred(common_block, jmp_zero);
1121 mature_immBlock(common_block);
1123 ir_node *in[2] = { one, zero };
1124 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1129 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1130 ir_node *right, ir_mode *mode);
1132 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1133 create_arithmetic_func func)
1135 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1136 ir_node *left = expression_to_firm(expression->left);
1137 ir_node *right = expression_to_firm(expression->right);
1138 type_t *type = expression->right->base.datatype;
1139 /* be careful with the modes, because in arithmetic assign nodes only
1140 * the right operand has the mode of the arithmetic already */
1141 ir_mode *mode = get_ir_mode(type);
1142 left = create_conv(dbgi, left, mode);
1143 ir_node *res = func(dbgi, left, right, mode);
1148 static ir_node *pointer_arithmetic(ir_node *const pointer,
1151 dbg_info *const dbgi,
1152 const create_arithmetic_func func)
1154 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1155 type_t *const points_to = pointer_type->points_to;
1156 const unsigned elem_size = get_type_size(points_to);
1158 assert(elem_size >= 1);
1159 if (elem_size > 1) {
1160 integer = create_conv(dbgi, integer, mode_Is);
1161 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1162 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1166 ir_mode *const mode = get_ir_mode(type);
1167 return func(dbgi, pointer, integer, mode);
1170 static ir_node *create_arithmetic_assign_binop(
1171 const binary_expression_t *expression, create_arithmetic_func func)
1173 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1174 type_t *const type = expression->expression.datatype;
1177 if (type->type == TYPE_POINTER) {
1178 ir_node *const pointer = expression_to_firm(expression->left);
1179 ir_node * integer = expression_to_firm(expression->right);
1180 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1182 value = create_arithmetic_binop(expression, func);
1185 ir_mode *const mode = get_ir_mode(type);
1186 value = create_conv(dbgi, value, mode);
1187 set_value_for_expression(expression->left, value);
1192 static ir_node *create_add(const binary_expression_t *expression)
1194 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1195 ir_node *left = expression_to_firm(expression->left);
1196 ir_node *right = expression_to_firm(expression->right);
1197 type_t *type = expression->expression.datatype;
1199 expression_t *expr_left = expression->left;
1200 expression_t *expr_right = expression->right;
1201 type_t *type_left = skip_typeref(expr_left->base.datatype);
1202 type_t *type_right = skip_typeref(expr_right->base.datatype);
1204 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1205 ir_mode *const mode = get_ir_mode(type);
1206 return new_d_Add(dbgi, left, right, mode);
1209 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1210 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1212 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1213 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1217 static ir_node *create_sub(const binary_expression_t *expression)
1219 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1220 expression_t *const expr_left = expression->left;
1221 expression_t *const expr_right = expression->right;
1222 ir_node *const left = expression_to_firm(expr_left);
1223 ir_node *const right = expression_to_firm(expr_right);
1224 type_t *const type = expression->expression.datatype;
1225 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1226 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1228 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1229 ir_mode *const mode = get_ir_mode(type);
1230 return new_d_Sub(dbgi, left, right, mode);
1231 } else if (type_left->type == TYPE_POINTER
1232 && type_right->type == TYPE_POINTER) {
1233 const pointer_type_t *const ptr_type = &type_left->pointer;
1234 const unsigned elem_size = get_type_size(ptr_type->points_to);
1235 ir_mode *const mode = get_ir_mode(type);
1236 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1237 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1238 ir_node *const no_mem = new_NoMem();
1239 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1240 op_pin_state_floats);
1241 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1244 assert(type_left->type == TYPE_POINTER);
1245 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1248 static ir_node *create_shift(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 type_t *type = expression->expression.datatype;
1254 ir_mode *mode = get_ir_mode(type);
1256 /* firm always wants the shift count to be unsigned */
1257 right = create_conv(dbgi, right, mode_Iu);
1261 switch(expression->type) {
1262 case BINEXPR_SHIFTLEFT_ASSIGN:
1263 case BINEXPR_SHIFTLEFT:
1264 res = new_d_Shl(dbgi, left, right, mode);
1266 case BINEXPR_SHIFTRIGHT_ASSIGN:
1267 case BINEXPR_SHIFTRIGHT: {
1268 expression_t *expr_left = expression->left;
1269 type_t *type_left = skip_typeref(expr_left->base.datatype);
1271 if(is_type_signed(type_left)) {
1272 res = new_d_Shrs(dbgi, left, right, mode);
1274 res = new_d_Shr(dbgi, left, right, mode);
1279 panic("create shift op called for non-shift op");
1286 static ir_node *create_divmod(const binary_expression_t *expression)
1288 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1289 ir_node *left = expression_to_firm(expression->left);
1290 ir_node *right = expression_to_firm(expression->right);
1291 ir_node *pin = new_Pin(new_NoMem());
1292 /* be careful with the modes, because in arithmetic assign nodes only
1293 * the right operand has the mode of the arithmetic already */
1294 type_t *type = expression->right->base.datatype;
1295 ir_mode *mode = get_ir_mode(type);
1296 left = create_conv(dbgi, left, mode);
1300 switch (expression->type) {
1302 case BINEXPR_DIV_ASSIGN:
1303 if(mode_is_float(mode)) {
1304 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1305 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1307 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1308 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1313 case BINEXPR_MOD_ASSIGN:
1314 assert(!mode_is_float(mode));
1315 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1316 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1319 default: panic("unexpected binary expression type in create_divmod()");
1325 static ir_node *create_arithmetic_assign_divmod(
1326 const binary_expression_t *expression)
1328 ir_node * value = create_divmod(expression);
1329 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1330 type_t *const type = expression->expression.datatype;
1331 ir_mode *const mode = get_ir_mode(type);
1333 assert(type->type != TYPE_POINTER);
1335 value = create_conv(dbgi, value, mode);
1336 set_value_for_expression(expression->left, value);
1341 static ir_node *create_arithmetic_assign_shift(
1342 const binary_expression_t *expression)
1344 ir_node * value = create_shift(expression);
1345 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1346 type_t *const type = expression->expression.datatype;
1347 ir_mode *const mode = get_ir_mode(type);
1349 value = create_conv(dbgi, value, mode);
1350 set_value_for_expression(expression->left, value);
1355 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1357 binary_expression_type_t type = expression->type;
1360 case BINEXPR_NOTEQUAL:
1362 case BINEXPR_LESSEQUAL:
1363 case BINEXPR_GREATER:
1364 case BINEXPR_GREATEREQUAL: {
1365 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1366 ir_node *left = expression_to_firm(expression->left);
1367 ir_node *right = expression_to_firm(expression->right);
1368 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1369 long pnc = get_pnc(type);
1370 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1373 case BINEXPR_ASSIGN: {
1374 ir_node *right = expression_to_firm(expression->right);
1375 set_value_for_expression(expression->left, right);
1380 return create_add(expression);
1382 return create_sub(expression);
1384 return create_arithmetic_binop(expression, new_d_Mul);
1385 case BINEXPR_BITWISE_AND:
1386 return create_arithmetic_binop(expression, new_d_And);
1387 case BINEXPR_BITWISE_OR:
1388 return create_arithmetic_binop(expression, new_d_Or);
1389 case BINEXPR_BITWISE_XOR:
1390 return create_arithmetic_binop(expression, new_d_Eor);
1391 case BINEXPR_SHIFTLEFT:
1392 case BINEXPR_SHIFTRIGHT:
1393 return create_shift(expression);
1396 return create_divmod(expression);
1397 case BINEXPR_LOGICAL_AND:
1398 case BINEXPR_LOGICAL_OR:
1399 return create_lazy_op(expression);
1401 expression_to_firm(expression->left);
1402 return expression_to_firm(expression->right);
1403 case BINEXPR_ADD_ASSIGN:
1404 return create_arithmetic_assign_binop(expression, new_d_Add);
1405 case BINEXPR_SUB_ASSIGN:
1406 return create_arithmetic_assign_binop(expression, new_d_Sub);
1407 case BINEXPR_MUL_ASSIGN:
1408 return create_arithmetic_assign_binop(expression, new_d_Mul);
1409 case BINEXPR_DIV_ASSIGN:
1410 return create_arithmetic_assign_divmod(expression);
1411 case BINEXPR_BITWISE_AND_ASSIGN:
1412 return create_arithmetic_assign_binop(expression, new_d_And);
1413 case BINEXPR_BITWISE_OR_ASSIGN:
1414 return create_arithmetic_assign_binop(expression, new_d_Or);
1415 case BINEXPR_BITWISE_XOR_ASSIGN:
1416 return create_arithmetic_assign_binop(expression, new_d_Eor);
1417 case BINEXPR_SHIFTLEFT_ASSIGN:
1418 case BINEXPR_SHIFTRIGHT_ASSIGN:
1419 return create_arithmetic_assign_shift(expression);
1421 panic("TODO binexpr type");
1425 static ir_node *array_access_addr(const array_access_expression_t *expression)
1427 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1428 ir_node *base_addr = expression_to_firm(expression->array_ref);
1429 ir_node *offset = expression_to_firm(expression->index);
1430 offset = create_conv(dbgi, offset, mode_Iu);
1432 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1433 assert(is_type_pointer(ref_type));
1434 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1436 unsigned elem_size = get_type_size(pointer_type->points_to);
1437 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1438 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1440 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1445 static ir_node *array_access_to_firm(
1446 const array_access_expression_t *expression)
1448 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1449 ir_node *addr = array_access_addr(expression);
1450 type_t *type = revert_automatic_type_conversion(
1451 (const expression_t*) expression);
1452 type = skip_typeref(type);
1453 ir_type *irtype = get_ir_type(type);
1455 return deref_address(irtype, addr, dbgi);
1458 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1460 type_t *type = expression->type;
1462 type = expression->size_expression->base.datatype;
1463 assert(type != NULL);
1466 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1467 unsigned size = get_type_size(type);
1468 ir_node *size_node = new_Const_long(mode, size);
1473 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1475 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1477 ir_node *cur_block = get_cur_block();
1479 /* create the true block */
1480 ir_node *true_block = new_immBlock();
1482 ir_node *true_val = expression_to_firm(expression->true_expression);
1483 ir_node *true_jmp = new_Jmp();
1485 /* create the false block */
1486 ir_node *false_block = new_immBlock();
1488 ir_node *false_val = expression_to_firm(expression->false_expression);
1489 ir_node *false_jmp = new_Jmp();
1491 /* create the condition evaluation */
1492 set_cur_block(cur_block);
1493 create_condition_evaluation(expression->condition, true_block, false_block);
1494 mature_immBlock(true_block);
1495 mature_immBlock(false_block);
1497 /* create the common block */
1498 ir_node *common_block = new_immBlock();
1499 add_immBlock_pred(common_block, true_jmp);
1500 add_immBlock_pred(common_block, false_jmp);
1501 mature_immBlock(common_block);
1503 /* TODO improve static semantics, so either both or no values are NULL */
1504 if (true_val == NULL || false_val == NULL) return NULL;
1506 ir_node *in[2] = { true_val, false_val };
1507 ir_mode *mode = get_irn_mode(true_val);
1508 assert(get_irn_mode(false_val) == mode);
1509 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1514 static ir_node *select_addr(const select_expression_t *expression)
1516 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1518 ir_node *compound_addr = expression_to_firm(expression->compound);
1520 declaration_t *entry = expression->compound_entry;
1521 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1522 ir_entity *entity = entry->v.entity;
1524 assert(entity != NULL);
1526 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1531 static ir_node *select_to_firm(const select_expression_t *expression)
1533 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1534 ir_node *addr = select_addr(expression);
1535 type_t *type = revert_automatic_type_conversion(
1536 (const expression_t*) expression);
1537 type = skip_typeref(type);
1538 ir_type *irtype = get_ir_type(type);
1540 return deref_address(irtype, addr, dbgi);
1543 /* Values returned by __builtin_classify_type. */
1544 typedef enum gcc_type_class
1550 enumeral_type_class,
1553 reference_type_class,
1557 function_type_class,
1568 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1570 const type_t *const type = expr->type_expression->base.datatype;
1576 const atomic_type_t *const atomic_type = &type->atomic;
1577 switch (atomic_type->atype) {
1578 // should not be reached
1579 case ATOMIC_TYPE_INVALID:
1583 // gcc cannot do that
1584 case ATOMIC_TYPE_VOID:
1585 tc = void_type_class;
1588 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1589 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1590 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1591 case ATOMIC_TYPE_SHORT:
1592 case ATOMIC_TYPE_USHORT:
1593 case ATOMIC_TYPE_INT:
1594 case ATOMIC_TYPE_UINT:
1595 case ATOMIC_TYPE_LONG:
1596 case ATOMIC_TYPE_ULONG:
1597 case ATOMIC_TYPE_LONGLONG:
1598 case ATOMIC_TYPE_ULONGLONG:
1599 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1600 tc = integer_type_class;
1603 case ATOMIC_TYPE_FLOAT:
1604 case ATOMIC_TYPE_DOUBLE:
1605 case ATOMIC_TYPE_LONG_DOUBLE:
1606 tc = real_type_class;
1609 #ifdef PROVIDE_COMPLEX
1610 case ATOMIC_TYPE_FLOAT_COMPLEX:
1611 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1612 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1613 tc = complex_type_class;
1615 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1616 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1617 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1618 tc = complex_type_class;
1623 panic("Unimplemented case in classify_type_to_firm().");
1628 case TYPE_ARRAY: // gcc handles this as pointer
1629 case TYPE_FUNCTION: // gcc handles this as pointer
1630 case TYPE_POINTER: tc = pointer_type_class; break;
1631 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1632 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1634 // gcc handles this as integer
1635 case TYPE_ENUM: tc = integer_type_class; break;
1638 panic("Unimplemented case in classify_type_to_firm().");
1641 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1642 ir_mode *const mode = mode_Is;
1643 tarval *const tv = new_tarval_from_long(tc, mode);
1644 return new_d_Const(dbgi, mode, tv);
1647 static ir_node *function_name_to_firm(
1648 const string_literal_expression_t *const expr)
1650 if (current_function_name == NULL) {
1651 const source_position_t *const src_pos =
1652 &expr->expression.source_position;
1653 const char *const name = current_function_decl->symbol->string;
1654 current_function_name = string_to_firm(src_pos, "__func__", name);
1657 return current_function_name;
1660 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1662 statement_t *statement = expr->statement;
1664 assert(statement->type == STATEMENT_COMPOUND);
1665 return compound_statement_to_firm((compound_statement_t*) statement);
1668 static ir_node *dereference_addr(const unary_expression_t *const expression)
1670 assert(expression->type == UNEXPR_DEREFERENCE);
1671 return expression_to_firm(expression->value);
1674 static ir_node *expression_to_addr(const expression_t *expression)
1676 switch(expression->type) {
1677 case EXPR_REFERENCE:
1678 return reference_addr(&expression->reference);
1679 case EXPR_ARRAY_ACCESS:
1680 return array_access_addr(&expression->array_access);
1682 return select_addr(&expression->select);
1684 return call_expression_to_firm(&expression->call);
1686 const unary_expression_t *const unary_expr = &expression->unary;
1687 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1688 return dereference_addr(unary_expr);
1695 panic("trying to get address of non-lvalue");
1698 static ir_node *_expression_to_firm(const expression_t *expression)
1700 switch(expression->type) {
1702 return const_to_firm(&expression->conste);
1703 case EXPR_STRING_LITERAL:
1704 return string_literal_to_firm(&expression->string);
1705 case EXPR_REFERENCE:
1706 return reference_expression_to_firm(&expression->reference);
1708 return call_expression_to_firm(&expression->call);
1710 return unary_expression_to_firm(&expression->unary);
1712 return binary_expression_to_firm(&expression->binary);
1713 case EXPR_ARRAY_ACCESS:
1714 return array_access_to_firm(&expression->array_access);
1716 return sizeof_to_firm(&expression->sizeofe);
1717 case EXPR_CONDITIONAL:
1718 return conditional_to_firm(&expression->conditional);
1720 return select_to_firm(&expression->select);
1721 case EXPR_CLASSIFY_TYPE:
1722 return classify_type_to_firm(&expression->classify_type);
1724 case EXPR_PRETTY_FUNCTION:
1725 return function_name_to_firm(&expression->string);
1726 case EXPR_STATEMENT:
1727 return statement_expression_to_firm(&expression->statement);
1730 case EXPR_BUILTIN_SYMBOL:
1731 panic("unimplemented expression found");
1737 panic("invalid expression found");
1740 static ir_node *expression_to_firm(const expression_t *expression)
1742 ir_node *res = _expression_to_firm(expression);
1744 if(res != NULL && get_irn_mode(res) == mode_b) {
1745 ir_mode *mode = get_ir_mode(expression->base.datatype);
1746 res = create_conv(NULL, res, mode);
1752 static ir_node *expression_to_modeb(const expression_t *expression)
1754 ir_node *res = _expression_to_firm(expression);
1755 res = create_conv(NULL, res, mode_b);
1761 * create a short-circuit expression evaluation that tries to construct
1762 * efficient control flow structures for &&, || and ! expressions
1764 static void create_condition_evaluation(const expression_t *expression,
1765 ir_node *true_block,
1766 ir_node *false_block)
1768 switch(expression->type) {
1770 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1771 if(unary_expression->type == UNEXPR_NOT) {
1772 create_condition_evaluation(unary_expression->value, false_block,
1779 binary_expression_t *binary_expression
1780 = (binary_expression_t*) expression;
1781 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1782 ir_node *cur_block = get_cur_block();
1783 ir_node *extra_block = new_immBlock();
1784 set_cur_block(cur_block);
1785 create_condition_evaluation(binary_expression->left, extra_block,
1787 mature_immBlock(extra_block);
1788 set_cur_block(extra_block);
1789 create_condition_evaluation(binary_expression->right, true_block,
1793 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1794 ir_node *cur_block = get_cur_block();
1795 ir_node *extra_block = new_immBlock();
1796 set_cur_block(cur_block);
1797 create_condition_evaluation(binary_expression->left, true_block,
1799 mature_immBlock(extra_block);
1800 set_cur_block(extra_block);
1801 create_condition_evaluation(binary_expression->right, true_block,
1811 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1812 ir_node *condition = expression_to_modeb(expression);
1813 ir_node *cond = new_d_Cond(dbgi, condition);
1814 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1815 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1817 add_immBlock_pred(true_block, true_proj);
1818 add_immBlock_pred(false_block, false_proj);
1820 set_cur_block(NULL);
1823 static void return_statement_to_firm(return_statement_t *statement)
1825 if(get_cur_block() == NULL)
1828 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1830 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1834 if(get_method_n_ress(func_irtype) > 0) {
1835 ir_type *res_type = get_method_res_type(func_irtype, 0);
1837 if(statement->return_value != NULL) {
1838 ir_node *node = expression_to_firm(statement->return_value);
1839 node = do_strict_conv(dbgi, node);
1843 if(is_compound_type(res_type)) {
1846 mode = get_type_mode(res_type);
1848 in[0] = new_Unknown(mode);
1852 /* build return_value for its side effects */
1853 if(statement->return_value != NULL) {
1854 expression_to_firm(statement->return_value);
1859 ir_node *store = get_store();
1860 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1862 ir_node *end_block = get_irg_end_block(current_ir_graph);
1863 add_immBlock_pred(end_block, ret);
1865 set_cur_block(NULL);
1868 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1870 if(get_cur_block() == NULL)
1873 return expression_to_firm(statement->expression);
1876 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1878 ir_node *result = NULL;
1879 statement_t *statement = compound->statements;
1880 for( ; statement != NULL; statement = statement->base.next) {
1881 //context2firm(&statement->context);
1883 if(statement->base.next == NULL
1884 && statement->type == STATEMENT_EXPRESSION) {
1885 result = expression_statement_to_firm(
1886 (expression_statement_t*) statement);
1889 statement_to_firm(statement);
1895 static void if_statement_to_firm(if_statement_t *statement)
1897 ir_node *cur_block = get_cur_block();
1899 ir_node *fallthrough_block = new_immBlock();
1901 /* the true (blocks) */
1902 ir_node *true_block;
1903 if (statement->true_statement != NULL) {
1904 true_block = new_immBlock();
1905 statement_to_firm(statement->true_statement);
1906 if(get_cur_block() != NULL) {
1907 ir_node *jmp = new_Jmp();
1908 add_immBlock_pred(fallthrough_block, jmp);
1911 true_block = fallthrough_block;
1914 /* the false (blocks) */
1915 ir_node *false_block;
1916 if(statement->false_statement != NULL) {
1917 false_block = new_immBlock();
1919 statement_to_firm(statement->false_statement);
1920 if(get_cur_block() != NULL) {
1921 ir_node *jmp = new_Jmp();
1922 add_immBlock_pred(fallthrough_block, jmp);
1925 false_block = fallthrough_block;
1928 /* create the condition */
1929 if(cur_block != NULL) {
1930 set_cur_block(cur_block);
1931 create_condition_evaluation(statement->condition, true_block,
1935 mature_immBlock(true_block);
1936 if(false_block != fallthrough_block) {
1937 mature_immBlock(false_block);
1939 mature_immBlock(fallthrough_block);
1941 set_cur_block(fallthrough_block);
1944 static void while_statement_to_firm(while_statement_t *statement)
1946 ir_node *jmp = NULL;
1947 if(get_cur_block() != NULL) {
1951 /* create the header block */
1952 ir_node *header_block = new_immBlock();
1954 add_immBlock_pred(header_block, jmp);
1957 /* the false block */
1958 ir_node *false_block = new_immBlock();
1961 ir_node *body_block;
1962 if (statement->body != NULL) {
1963 ir_node *old_continue_label = continue_label;
1964 ir_node *old_break_label = break_label;
1965 continue_label = header_block;
1966 break_label = false_block;
1968 body_block = new_immBlock();
1969 statement_to_firm(statement->body);
1971 assert(continue_label == header_block);
1972 assert(break_label == false_block);
1973 continue_label = old_continue_label;
1974 break_label = old_break_label;
1976 if(get_cur_block() != NULL) {
1978 add_immBlock_pred(header_block, jmp);
1981 body_block = header_block;
1984 /* create the condition */
1985 set_cur_block(header_block);
1987 create_condition_evaluation(statement->condition, body_block, false_block);
1988 mature_immBlock(body_block);
1989 mature_immBlock(false_block);
1990 mature_immBlock(header_block);
1992 set_cur_block(false_block);
1995 static void do_while_statement_to_firm(do_while_statement_t *statement)
1997 ir_node *jmp = NULL;
1998 if(get_cur_block() != NULL) {
2002 /* create the header block */
2003 ir_node *header_block = new_immBlock();
2005 /* the false block */
2006 ir_node *false_block = new_immBlock();
2009 ir_node *body_block = new_immBlock();
2011 add_immBlock_pred(body_block, jmp);
2014 if (statement->body != NULL) {
2015 ir_node *old_continue_label = continue_label;
2016 ir_node *old_break_label = break_label;
2017 continue_label = header_block;
2018 break_label = false_block;
2020 statement_to_firm(statement->body);
2022 assert(continue_label == header_block);
2023 assert(break_label == false_block);
2024 continue_label = old_continue_label;
2025 break_label = old_break_label;
2027 if (get_cur_block() == NULL) {
2028 mature_immBlock(header_block);
2029 mature_immBlock(body_block);
2030 mature_immBlock(false_block);
2035 ir_node *body_jmp = new_Jmp();
2036 add_immBlock_pred(header_block, body_jmp);
2037 mature_immBlock(header_block);
2039 /* create the condition */
2040 set_cur_block(header_block);
2042 create_condition_evaluation(statement->condition, body_block, false_block);
2043 mature_immBlock(body_block);
2044 mature_immBlock(false_block);
2045 mature_immBlock(header_block);
2047 set_cur_block(false_block);
2050 static void for_statement_to_firm(for_statement_t *statement)
2052 ir_node *jmp = NULL;
2053 if (get_cur_block() != NULL) {
2054 if(statement->initialisation != NULL) {
2055 expression_to_firm(statement->initialisation);
2060 /* create the step block */
2061 ir_node *const step_block = new_immBlock();
2062 if (statement->step != NULL) {
2063 expression_to_firm(statement->step);
2065 ir_node *const step_jmp = new_Jmp();
2067 /* create the header block */
2068 ir_node *const header_block = new_immBlock();
2070 add_immBlock_pred(header_block, jmp);
2072 add_immBlock_pred(header_block, step_jmp);
2074 /* the false block */
2075 ir_node *const false_block = new_immBlock();
2078 ir_node * body_block;
2079 if (statement->body != NULL) {
2080 ir_node *const old_continue_label = continue_label;
2081 ir_node *const old_break_label = break_label;
2082 continue_label = step_block;
2083 break_label = false_block;
2085 body_block = new_immBlock();
2086 statement_to_firm(statement->body);
2088 assert(continue_label == step_block);
2089 assert(break_label == false_block);
2090 continue_label = old_continue_label;
2091 break_label = old_break_label;
2093 if (get_cur_block() != NULL) {
2095 add_immBlock_pred(step_block, jmp);
2098 body_block = step_block;
2101 /* create the condition */
2102 set_cur_block(header_block);
2103 if (statement->condition != NULL) {
2104 create_condition_evaluation(statement->condition, body_block,
2107 keep_alive(header_block);
2109 add_immBlock_pred(body_block, jmp);
2112 mature_immBlock(body_block);
2113 mature_immBlock(false_block);
2114 mature_immBlock(step_block);
2115 mature_immBlock(header_block);
2116 mature_immBlock(false_block);
2118 set_cur_block(false_block);
2121 static void create_declaration_entity(declaration_t *declaration,
2122 declaration_type_t declaration_type,
2123 ir_type *parent_type)
2125 ident *id = new_id_from_str(declaration->symbol->string);
2126 ir_type *irtype = get_ir_type(declaration->type);
2127 ir_entity *entity = new_entity(parent_type, id, irtype);
2128 set_entity_ld_ident(entity, id);
2130 declaration->declaration_type = (unsigned char) declaration_type;
2131 declaration->v.entity = entity;
2132 set_entity_variability(entity, variability_uninitialized);
2133 /* TODO: visibility? */
2136 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2138 enum compound_graph_entry_type_t {
2139 COMPOUND_GRAPH_ENTRY_ARRAY,
2140 COMPOUND_GRAPH_ENTRY_COMPOUND
2143 struct compound_graph_path_entry_t {
2149 compound_graph_path_entry_t *prev;
2152 static void create_initializer_object(initializer_t *initializer, type_t *type,
2153 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2155 static compound_graph_path *create_compound_path(ir_type *type,
2156 compound_graph_path_entry_t *entry, int len)
2158 compound_graph_path *path = new_compound_graph_path(type, len);
2161 for( ; entry != NULL; entry = entry->prev, --i) {
2163 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2164 set_compound_graph_path_node(path, i, entry->v.entity);
2166 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2167 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2175 static void create_initializer_value(initializer_value_t *initializer,
2177 compound_graph_path_entry_t *entry,
2180 ir_node *node = expression_to_firm(initializer->value);
2181 ir_type *type = get_entity_type(entity);
2182 compound_graph_path *path = create_compound_path(type, entry, len);
2183 add_compound_ent_value_w_path(entity, node, path);
2186 static void create_initializer_compound(initializer_list_t *initializer,
2187 compound_type_t *type,
2189 compound_graph_path_entry_t *last_entry,
2192 declaration_t *compound_declaration = type->declaration;
2194 declaration_t *compound_entry = compound_declaration->context.declarations;
2196 compound_graph_path_entry_t entry;
2197 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2198 entry.prev = last_entry;
2202 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2203 if(compound_entry->symbol == NULL)
2205 if(compound_entry->namespc != NAMESPACE_NORMAL)
2208 if(i >= initializer->len)
2211 entry.v.entity = compound_entry->v.entity;
2213 initializer_t *sub_initializer = initializer->initializers[i];
2215 assert(compound_entry != NULL);
2216 assert(compound_entry->declaration_type
2217 == DECLARATION_TYPE_COMPOUND_MEMBER);
2219 if(sub_initializer->type == INITIALIZER_VALUE) {
2220 create_initializer_value(&sub_initializer->value,
2221 entity, &entry, len);
2223 type_t *entry_type = skip_typeref(compound_entry->type);
2224 create_initializer_object(sub_initializer, entry_type, entity,
2232 static void create_initializer_array(initializer_list_t *initializer,
2233 array_type_t *type, ir_entity *entity,
2234 compound_graph_path_entry_t *last_entry,
2237 type_t *element_type = type->element_type;
2238 element_type = skip_typeref(element_type);
2240 compound_graph_path_entry_t entry;
2241 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2242 entry.prev = last_entry;
2246 for(i = 0; i < initializer->len; ++i) {
2247 entry.v.array_index = i;
2249 initializer_t *sub_initializer = initializer->initializers[i];
2251 if(sub_initializer->type == INITIALIZER_VALUE) {
2252 create_initializer_value(&sub_initializer->value,
2253 entity, &entry, len);
2255 create_initializer_object(sub_initializer, element_type, entity,
2261 /* TODO: initialize rest... */
2262 if(type->size_expression != NULL) {
2263 size_t array_len = fold_constant(type->size_expression);
2264 for( ; i < array_len; ++i) {
2271 static void create_initializer_string(initializer_string_t *initializer,
2272 array_type_t *type, ir_entity *entity,
2273 compound_graph_path_entry_t *last_entry,
2276 type_t *element_type = type->element_type;
2277 element_type = skip_typeref(element_type);
2279 compound_graph_path_entry_t entry;
2280 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2281 entry.prev = last_entry;
2284 ir_type *irtype = get_entity_type(entity);
2285 size_t arr_len = get_array_type_size(type);
2286 const char *p = initializer->string;
2288 for(i = 0; i < arr_len; ++i, ++p) {
2289 entry.v.array_index = i;
2291 ir_node *node = new_Const_long(mode_Bs, *p);
2292 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2293 add_compound_ent_value_w_path(entity, node, path);
2300 static void create_initializer_object(initializer_t *initializer, type_t *type,
2301 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2303 if(type->type == TYPE_ARRAY) {
2304 array_type_t *array_type = &type->array;
2306 if(initializer->type == INITIALIZER_STRING) {
2307 initializer_string_t *string = &initializer->string;
2308 create_initializer_string(string, array_type, entity, entry, len);
2310 assert(initializer->type == INITIALIZER_LIST);
2311 initializer_list_t *list = &initializer->list;
2312 create_initializer_array(list, array_type, entity, entry, len);
2315 assert(initializer->type == INITIALIZER_LIST);
2316 initializer_list_t *list = &initializer->list;
2318 assert(type->type == TYPE_COMPOUND_STRUCT
2319 || type->type == TYPE_COMPOUND_UNION);
2320 compound_type_t *compound_type = &type->compound;
2321 create_initializer_compound(list, compound_type, entity, entry, len);
2325 static void create_initializer_local_variable_entity(declaration_t *declaration)
2327 initializer_t *initializer = declaration->init.initializer;
2328 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2329 ir_entity *entity = declaration->v.entity;
2330 ir_node *memory = get_store();
2331 ir_node *nomem = new_NoMem();
2332 ir_node *frame = get_irg_frame(current_ir_graph);
2333 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2335 if(is_atomic_entity(entity)) {
2336 assert(initializer->type == INITIALIZER_VALUE);
2337 initializer_value_t *initializer_value = &initializer->value;
2339 ir_node *value = expression_to_firm(initializer_value->value);
2340 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2341 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2342 set_store(store_mem);
2346 /* create a "template" entity which is copied to the entity on the stack */
2347 ident *id = unique_ident("initializer");
2348 ir_type *irtype = get_ir_type(declaration->type);
2349 ir_type *global_type = get_glob_type();
2350 ir_entity *init_entity = new_entity(global_type, id, irtype);
2351 set_entity_ld_ident(init_entity, id);
2353 set_entity_variability(init_entity, variability_initialized);
2354 set_entity_visibility(init_entity, visibility_local);
2356 ir_graph *old_current_ir_graph = current_ir_graph;
2357 current_ir_graph = get_const_code_irg();
2359 type_t *type = skip_typeref(declaration->type);
2360 create_initializer_object(initializer, type, init_entity, NULL, 0);
2362 assert(current_ir_graph == get_const_code_irg());
2363 current_ir_graph = old_current_ir_graph;
2365 ir_node *src_addr = create_symconst(dbgi, init_entity);
2366 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2368 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2369 set_store(copyb_mem);
2372 static void create_initializer(declaration_t *declaration)
2374 initializer_t *initializer = declaration->init.initializer;
2375 if(initializer == NULL)
2378 declaration_type_t declaration_type
2379 = (declaration_type_t) declaration->declaration_type;
2380 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2381 create_initializer_local_variable_entity(declaration);
2385 if(initializer->type == INITIALIZER_VALUE) {
2386 initializer_value_t *initializer_value = &initializer->value;
2388 ir_node *value = expression_to_firm(initializer_value->value);
2390 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2391 set_value(declaration->v.value_number, value);
2393 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2395 ir_entity *entity = declaration->v.entity;
2397 set_entity_variability(entity, variability_initialized);
2398 set_atomic_ent_value(entity, value);
2401 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2402 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2404 ir_entity *entity = declaration->v.entity;
2405 set_entity_variability(entity, variability_initialized);
2407 type_t *type = skip_typeref(declaration->type);
2408 create_initializer_object(initializer, type, entity, NULL, 0);
2412 static void create_local_variable(declaration_t *declaration)
2414 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2416 bool needs_entity = declaration->address_taken;
2417 type_t *type = skip_typeref(declaration->type);
2419 if(type->type == TYPE_ARRAY
2420 || type->type == TYPE_COMPOUND_STRUCT
2421 || type->type == TYPE_COMPOUND_UNION) {
2422 needs_entity = true;
2426 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2427 create_declaration_entity(declaration,
2428 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2431 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2432 declaration->v.value_number = next_value_number_function;
2433 ++next_value_number_function;
2436 create_initializer(declaration);
2439 static void create_local_static_variable(declaration_t *declaration)
2441 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2443 type_t *type = skip_typeref(declaration->type);
2444 ir_type *global_type = get_glob_type();
2445 ident *id = unique_ident(declaration->symbol->string);
2446 ir_type *irtype = get_ir_type(type);
2447 ir_entity *entity = new_entity(global_type, id, irtype);
2448 set_entity_ld_ident(entity, id);
2450 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2451 declaration->v.entity = entity;
2452 set_entity_variability(entity, variability_uninitialized);
2453 set_entity_visibility(entity, visibility_local);
2455 ir_graph *old_current_ir_graph = current_ir_graph;
2456 current_ir_graph = get_const_code_irg();
2458 create_initializer(declaration);
2460 assert(current_ir_graph == get_const_code_irg());
2461 current_ir_graph = old_current_ir_graph;
2464 static void declaration_statement_to_firm(declaration_statement_t *statement)
2466 declaration_t *declaration = statement->declarations_begin;
2467 declaration_t *end = statement->declarations_end->next;
2468 for( ; declaration != end; declaration = declaration->next) {
2469 type_t *type = declaration->type;
2471 switch ((storage_class_tag_t)declaration->storage_class) {
2472 case STORAGE_CLASS_TYPEDEF:
2474 case STORAGE_CLASS_STATIC:
2475 create_local_static_variable(declaration);
2477 case STORAGE_CLASS_ENUM_ENTRY:
2478 panic("enum entry declaration in local block found");
2479 case STORAGE_CLASS_EXTERN:
2480 panic("extern declaration in local block found");
2481 case STORAGE_CLASS_NONE:
2482 case STORAGE_CLASS_AUTO:
2483 case STORAGE_CLASS_REGISTER:
2484 if(type->type == TYPE_FUNCTION) {
2485 panic("nested functions not supported yet");
2487 create_local_variable(declaration);
2490 case STORAGE_CLASS_THREAD:
2491 case STORAGE_CLASS_THREAD_EXTERN:
2492 case STORAGE_CLASS_THREAD_STATIC:
2495 panic("invalid storage class found");
2499 static void create_jump_statement(const statement_t *statement,
2500 ir_node *target_block)
2502 if(get_cur_block() == NULL)
2505 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2506 ir_node *jump = new_d_Jmp(dbgi);
2507 add_immBlock_pred(target_block, jump);
2509 set_cur_block(NULL);
2512 static void switch_statement_to_firm(const switch_statement_t *statement)
2514 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2516 ir_node *expression = expression_to_firm(statement->expression);
2517 ir_node *cond = new_d_Cond(dbgi, expression);
2518 ir_node *break_block = new_immBlock();
2520 set_cur_block(NULL);
2522 ir_node *const old_switch_cond = current_switch_cond;
2523 ir_node *const old_break_label = break_label;
2524 const bool old_saw_default_label = saw_default_label;
2525 current_switch_cond = cond;
2526 break_label = break_block;
2528 statement_to_firm(statement->body);
2530 if(get_cur_block() != NULL) {
2531 ir_node *jmp = new_Jmp();
2532 add_immBlock_pred(break_block, jmp);
2535 if (!saw_default_label) {
2536 set_cur_block(get_nodes_block(cond));
2537 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2538 MAGIC_DEFAULT_PN_NUMBER);
2539 add_immBlock_pred(break_block, proj);
2542 assert(current_switch_cond == cond);
2543 assert(break_label == break_block);
2544 current_switch_cond = old_switch_cond;
2545 break_label = old_break_label;
2546 saw_default_label = old_saw_default_label;
2548 mature_immBlock(break_block);
2549 set_cur_block(break_block);
2552 static long fold_constant(const expression_t *expression)
2554 ir_graph *old_current_ir_graph = current_ir_graph;
2555 current_ir_graph = get_const_code_irg();
2557 ir_node *cnst = expression_to_firm(expression);
2558 if(!is_Const(cnst)) {
2559 panic("couldn't fold constantl");
2561 tarval *tv = get_Const_tarval(cnst);
2562 if(!tarval_is_long(tv)) {
2563 panic("folded constant not an integer");
2566 long res = get_tarval_long(tv);
2568 current_ir_graph = old_current_ir_graph;
2572 static void case_label_to_firm(const case_label_statement_t *statement)
2574 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2576 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2578 /* let's create a node and hope firm constant folding creates a Const
2581 set_cur_block(get_nodes_block(current_switch_cond));
2582 if(statement->expression) {
2583 long pn = fold_constant(statement->expression);
2584 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2585 /* oops someone detected our cheating... */
2586 panic("magic default pn used");
2588 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2590 saw_default_label = true;
2591 proj = new_d_defaultProj(dbgi, current_switch_cond,
2592 MAGIC_DEFAULT_PN_NUMBER);
2595 ir_node *block = new_immBlock();
2596 if (fallthrough != NULL) {
2597 add_immBlock_pred(block, fallthrough);
2599 add_immBlock_pred(block, proj);
2600 mature_immBlock(block);
2602 statement_to_firm(statement->label_statement);
2605 static ir_node *get_label_block(declaration_t *label)
2607 assert(label->namespc == NAMESPACE_LABEL);
2609 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2610 return label->v.block;
2612 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2614 ir_node *old_cur_block = get_cur_block();
2615 ir_node *block = new_immBlock();
2616 set_cur_block(old_cur_block);
2618 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2619 label->v.block = block;
2621 ARR_APP1(ir_node *, imature_blocks, block);
2626 static void label_to_firm(const label_statement_t *statement)
2628 ir_node *block = get_label_block(statement->label);
2630 if(get_cur_block() != NULL) {
2631 ir_node *jmp = new_Jmp();
2632 add_immBlock_pred(block, jmp);
2635 set_cur_block(block);
2638 statement_to_firm(statement->label_statement);
2641 static void goto_to_firm(const goto_statement_t *statement)
2643 if(get_cur_block() == NULL)
2646 ir_node *block = get_label_block(statement->label);
2647 ir_node *jmp = new_Jmp();
2648 add_immBlock_pred(block, jmp);
2650 set_cur_block(NULL);
2653 static void statement_to_firm(statement_t *statement)
2655 switch(statement->type) {
2656 case STATEMENT_INVALID:
2657 panic("invalid statement found");
2658 case STATEMENT_COMPOUND:
2659 compound_statement_to_firm(&statement->compound);
2661 case STATEMENT_RETURN:
2662 return_statement_to_firm(&statement->returns);
2664 case STATEMENT_EXPRESSION:
2665 expression_statement_to_firm(&statement->expression);
2668 if_statement_to_firm(&statement->ifs);
2670 case STATEMENT_WHILE:
2671 while_statement_to_firm(&statement->whiles);
2673 case STATEMENT_DO_WHILE:
2674 do_while_statement_to_firm(&statement->do_while);
2676 case STATEMENT_DECLARATION:
2677 declaration_statement_to_firm(&statement->declaration);
2679 case STATEMENT_BREAK:
2680 create_jump_statement(statement, break_label);
2682 case STATEMENT_CONTINUE:
2683 create_jump_statement(statement, continue_label);
2685 case STATEMENT_SWITCH:
2686 switch_statement_to_firm(&statement->switchs);
2688 case STATEMENT_CASE_LABEL:
2689 case_label_to_firm(&statement->case_label);
2692 for_statement_to_firm(&statement->fors);
2694 case STATEMENT_LABEL:
2695 label_to_firm(&statement->label);
2697 case STATEMENT_GOTO:
2698 goto_to_firm(&statement->gotos);
2703 panic("Statement not implemented\n");
2706 static int count_local_declarations(const declaration_t * decl,
2707 const declaration_t *const end)
2710 for (; decl != end; decl = decl->next) {
2711 const type_t *type = skip_typeref(decl->type);
2712 switch (type->type) {
2716 if (!decl->address_taken)
2726 static int count_decls_in_stmts(const statement_t *stmt)
2729 for (; stmt != NULL; stmt = stmt->base.next) {
2730 switch (stmt->type) {
2731 case STATEMENT_DECLARATION: {
2732 const declaration_statement_t *const decl_stmt =
2733 (const declaration_statement_t*)stmt;
2734 count += count_local_declarations(decl_stmt->declarations_begin,
2735 decl_stmt->declarations_end->next);
2739 case STATEMENT_COMPOUND: {
2740 const compound_statement_t *const comp =
2741 (const compound_statement_t*)stmt;
2742 count += count_decls_in_stmts(comp->statements);
2746 case STATEMENT_IF: {
2747 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2748 count += count_decls_in_stmts(if_stmt->true_statement);
2749 count += count_decls_in_stmts(if_stmt->false_statement);
2753 case STATEMENT_SWITCH: {
2754 const switch_statement_t *const switch_stmt =
2755 (const switch_statement_t*)stmt;
2756 count += count_decls_in_stmts(switch_stmt->body);
2760 case STATEMENT_LABEL: {
2761 const label_statement_t *const label_stmt =
2762 (const label_statement_t*)stmt;
2763 count += count_decls_in_stmts(label_stmt->label_statement);
2767 case STATEMENT_WHILE: {
2768 const while_statement_t *const while_stmt =
2769 (const while_statement_t*)stmt;
2770 count += count_decls_in_stmts(while_stmt->body);
2774 case STATEMENT_DO_WHILE: {
2775 const do_while_statement_t *const do_while_stmt =
2776 (const do_while_statement_t*)stmt;
2777 count += count_decls_in_stmts(do_while_stmt->body);
2781 case STATEMENT_FOR: {
2782 const for_statement_t *const for_stmt =
2783 (const for_statement_t*)stmt;
2784 /* TODO initialisation */
2785 count += count_decls_in_stmts(for_stmt->body);
2790 case STATEMENT_BREAK:
2791 case STATEMENT_CASE_LABEL:
2792 case STATEMENT_CONTINUE:
2793 case STATEMENT_EXPRESSION:
2794 case STATEMENT_GOTO:
2795 case STATEMENT_INVALID:
2796 case STATEMENT_RETURN:
2803 static int get_function_n_local_vars(declaration_t *declaration)
2807 /* count parameters */
2808 count += count_local_declarations(declaration->context.declarations, NULL);
2810 /* count local variables declared in body */
2811 count += count_decls_in_stmts(declaration->init.statement);
2813 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2814 * for expression statements... */
2820 static void initialize_function_parameters(declaration_t *declaration)
2822 ir_graph *irg = current_ir_graph;
2823 ir_node *args = get_irg_args(irg);
2824 ir_node *start_block = get_irg_start_block(irg);
2825 ir_type *function_irtype = get_ir_type(declaration->type);
2828 declaration_t *parameter = declaration->context.declarations;
2829 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2830 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2831 type_t *type = skip_typeref(parameter->type);
2833 bool needs_entity = parameter->address_taken;
2834 if(type->type == TYPE_COMPOUND_STRUCT
2835 || type->type == TYPE_COMPOUND_UNION) {
2836 needs_entity = true;
2840 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2841 ident *id = new_id_from_str(parameter->symbol->string);
2842 set_entity_ident(entity, id);
2844 parameter->declaration_type
2845 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2846 parameter->v.entity = entity;
2850 ir_mode *mode = get_ir_mode(parameter->type);
2852 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2854 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2855 parameter->v.value_number = next_value_number_function;
2856 ++next_value_number_function;
2858 set_value(parameter->v.value_number, proj);
2862 static void create_function(declaration_t *declaration)
2864 ir_entity *function_entity = get_function_entity(declaration);
2866 if(declaration->init.statement == NULL)
2869 current_function_decl = declaration;
2870 current_function_name = NULL;
2872 assert(imature_blocks == NULL);
2873 imature_blocks = NEW_ARR_F(ir_node*, 0);
2875 int n_local_vars = get_function_n_local_vars(declaration);
2876 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
2877 ir_node *first_block = get_cur_block();
2879 next_value_number_function = 0;
2880 initialize_function_parameters(declaration);
2882 statement_to_firm(declaration->init.statement);
2884 ir_node *end_block = get_irg_end_block(irg);
2886 /* do we have a return statement yet? */
2887 if(get_cur_block() != NULL) {
2888 assert(declaration->type->type == TYPE_FUNCTION);
2889 const function_type_t* const func_type = &declaration->type->function;
2890 const type_t *result_type = skip_typeref(func_type->result_type);
2893 if (is_type_atomic(result_type, ATOMIC_TYPE_VOID)) {
2894 ret = new_Return(get_store(), 0, NULL);
2897 if(is_type_scalar(result_type)) {
2898 mode = get_ir_mode(func_type->result_type);
2904 // ยง5.1.2.2.3 main implicitly returns 0
2905 if (strcmp(declaration->symbol->string, "main") == 0) {
2906 in[0] = new_Const(mode, get_mode_null(mode));
2908 in[0] = new_Unknown(mode);
2910 ret = new_Return(get_store(), 1, in);
2912 add_immBlock_pred(end_block, ret);
2915 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2916 mature_immBlock(imature_blocks[i]);
2918 DEL_ARR_F(imature_blocks);
2919 imature_blocks = NULL;
2921 mature_immBlock(first_block);
2922 mature_immBlock(end_block);
2924 irg_finalize_cons(irg);
2926 /* finalize the frame type */
2927 ir_type *frame_type = get_irg_frame_type(irg);
2928 int n = get_compound_n_members(frame_type);
2931 for(int i = 0; i < n; ++i) {
2932 ir_entity *entity = get_compound_member(frame_type, i);
2933 ir_type *entity_type = get_entity_type(entity);
2935 int align = get_type_alignment_bytes(entity_type);
2936 if(align > align_all)
2940 misalign = offset % align;
2942 offset += align - misalign;
2946 set_entity_offset(entity, offset);
2947 offset += get_type_size_bytes(entity_type);
2949 set_type_size_bytes(frame_type, offset);
2950 set_type_alignment_bytes(frame_type, align_all);
2951 set_type_state(frame_type, layout_fixed);
2956 static void create_global_variable(declaration_t *declaration)
2960 switch ((storage_class_tag_t)declaration->storage_class) {
2961 case STORAGE_CLASS_STATIC:
2962 vis = visibility_local;
2965 case STORAGE_CLASS_EXTERN:
2966 vis = visibility_external_allocated;
2969 case STORAGE_CLASS_NONE:
2970 vis = visibility_external_visible;
2973 case STORAGE_CLASS_THREAD:
2974 vis = visibility_external_visible;
2977 case STORAGE_CLASS_THREAD_EXTERN:
2978 vis = visibility_external_allocated;
2981 case STORAGE_CLASS_THREAD_STATIC:
2982 vis = visibility_local;
2986 var_type = get_tls_type();
2990 var_type = get_glob_type();
2994 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2996 set_entity_visibility(declaration->v.entity, vis);
2998 current_ir_graph = get_const_code_irg();
2999 create_initializer(declaration);
3002 case STORAGE_CLASS_TYPEDEF:
3003 case STORAGE_CLASS_AUTO:
3004 case STORAGE_CLASS_REGISTER:
3005 case STORAGE_CLASS_ENUM_ENTRY:
3008 panic("Invalid storage class for global variable");
3011 static void context_to_firm(context_t *context)
3013 /* first pass: create declarations */
3014 declaration_t *declaration = context->declarations;
3015 for( ; declaration != NULL; declaration = declaration->next) {
3016 if(declaration->namespc != NAMESPACE_NORMAL)
3018 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3019 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3021 if(declaration->symbol == NULL)
3024 type_t *type = declaration->type;
3025 if(type->type == TYPE_FUNCTION) {
3026 get_function_entity(declaration);
3028 create_global_variable(declaration);
3032 /* second pass: create code */
3033 declaration = context->declarations;
3034 for( ; declaration != NULL; declaration = declaration->next) {
3035 if(declaration->namespc != NAMESPACE_NORMAL)
3037 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3038 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3040 if(declaration->symbol == NULL)
3043 type_t *type = declaration->type;
3044 if(type->type != TYPE_FUNCTION)
3047 create_function(declaration);
3051 void translation_unit_to_firm(translation_unit_t *unit)
3053 /* just to be sure */
3054 continue_label = NULL;
3056 current_switch_cond = NULL;
3058 context_to_firm(& unit->context);