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_wchar_t;
25 static ir_type *ir_type_void;
26 static ir_type *ir_type_int;
28 static type_t *type_const_char;
29 static type_t *type_void;
30 static type_t *type_int;
32 static int next_value_number_function;
33 static ir_node *continue_label;
34 static ir_node *break_label;
35 static ir_node *current_switch_cond;
36 static bool saw_default_label;
37 static ir_node **imature_blocks;
39 static const declaration_t *current_function_decl;
40 static ir_node *current_function_name;
42 static struct obstack asm_obst;
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 *dbg_retrieve(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 obstack_init(&asm_obst);
95 void exit_ast2firm(void)
97 obstack_free(&asm_obst, NULL);
100 static unsigned unique_id = 0;
102 static ident *unique_ident(const char *tag)
106 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
108 return new_id_from_str(buf);
111 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
113 switch(atomic_type->atype) {
114 case ATOMIC_TYPE_SCHAR:
115 case ATOMIC_TYPE_CHAR:
117 case ATOMIC_TYPE_UCHAR:
119 case ATOMIC_TYPE_SHORT:
121 case ATOMIC_TYPE_USHORT:
123 case ATOMIC_TYPE_LONG:
124 case ATOMIC_TYPE_INT:
126 case ATOMIC_TYPE_ULONG:
127 case ATOMIC_TYPE_UINT:
129 case ATOMIC_TYPE_LONGLONG:
131 case ATOMIC_TYPE_ULONGLONG:
133 case ATOMIC_TYPE_FLOAT:
135 case ATOMIC_TYPE_DOUBLE:
137 case ATOMIC_TYPE_LONG_DOUBLE:
139 case ATOMIC_TYPE_BOOL:
141 #ifdef PROVIDE_COMPLEX
142 case ATOMIC_TYPE_FLOAT_COMPLEX:
143 case ATOMIC_TYPE_DOUBLE_COMPLEX:
144 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
145 panic("complex lowering not implemented yet");
147 case ATOMIC_TYPE_FLOAT_IMAGINARY:
148 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
149 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
150 panic("imaginary lowering not implemented yet");
153 case ATOMIC_TYPE_VOID:
154 /* firm has no real void... */
156 case ATOMIC_TYPE_INVALID:
159 panic("Encountered unknown atomic type");
163 static unsigned get_type_size(type_t *type);
165 static unsigned get_atomic_type_size(const atomic_type_t *type)
167 switch(type->atype) {
168 case ATOMIC_TYPE_CHAR:
169 case ATOMIC_TYPE_SCHAR:
170 case ATOMIC_TYPE_UCHAR:
173 case ATOMIC_TYPE_SHORT:
174 case ATOMIC_TYPE_USHORT:
177 case ATOMIC_TYPE_BOOL:
178 case ATOMIC_TYPE_INT:
179 case ATOMIC_TYPE_UINT:
180 case ATOMIC_TYPE_LONG:
181 case ATOMIC_TYPE_ULONG:
182 case ATOMIC_TYPE_FLOAT:
185 case ATOMIC_TYPE_LONGLONG:
186 case ATOMIC_TYPE_ULONGLONG:
187 case ATOMIC_TYPE_DOUBLE:
190 case ATOMIC_TYPE_LONG_DOUBLE:
193 case ATOMIC_TYPE_VOID:
196 case ATOMIC_TYPE_INVALID:
199 panic("Trying to determine size of invalid atomic type");
202 static unsigned get_compound_type_size(compound_type_t *type)
204 ir_type *irtype = get_ir_type((type_t*) type);
205 return get_type_size_bytes(irtype);
208 static unsigned get_array_type_size(array_type_t *type)
210 ir_type *irtype = get_ir_type((type_t*) type);
211 return get_type_size_bytes(irtype);
214 static unsigned get_type_size(type_t *type)
216 type = skip_typeref(type);
220 return get_atomic_type_size(&type->atomic);
222 return get_mode_size_bytes(mode_Is);
223 case TYPE_COMPOUND_UNION:
224 case TYPE_COMPOUND_STRUCT:
225 return get_compound_type_size(&type->compound);
227 /* just a pointer to the function */
228 return get_mode_size_bytes(mode_P_code);
230 return get_mode_size_bytes(mode_P_data);
232 return get_array_type_size(&type->array);
234 return get_type_size(type->builtin.real_type);
240 panic("Trying to determine size of invalid type");
243 static unsigned count_parameters(const function_type_t *function_type)
247 function_parameter_t *parameter = function_type->parameters;
248 for ( ; parameter != NULL; parameter = parameter->next) {
258 static long fold_constant(const expression_t *expression);
260 static ir_type *create_atomic_type(const atomic_type_t *type)
262 ir_mode *mode = get_atomic_mode(type);
263 ident *id = get_mode_ident(mode);
264 ir_type *irtype = new_type_primitive(id, mode);
269 static ir_type *create_method_type(const function_type_t *function_type)
271 type_t *return_type = function_type->return_type;
273 ident *id = unique_ident("functiontype");
274 int n_parameters = count_parameters(function_type);
275 int n_results = return_type == type_void ? 0 : 1;
276 ir_type *irtype = new_type_method(id, n_parameters, n_results);
278 if(return_type != type_void) {
279 ir_type *restype = get_ir_type(return_type);
280 set_method_res_type(irtype, 0, restype);
283 function_parameter_t *parameter = function_type->parameters;
285 for( ; parameter != NULL; parameter = parameter->next) {
286 ir_type *p_irtype = get_ir_type(parameter->type);
287 set_method_param_type(irtype, n, p_irtype);
291 if(function_type->variadic || function_type->unspecified_parameters) {
292 set_method_variadicity(irtype, variadicity_variadic);
298 static ir_type *create_pointer_type(pointer_type_t *type)
300 type_t *points_to = type->points_to;
301 ir_type *ir_points_to;
302 /* Avoid endless recursion if the points_to type contains this poiner type
303 * again (might be a struct). We therefore first create a void* pointer
304 * and then set the real points_to type
306 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
307 ir_type_void, mode_P_data);
308 type->type.firm_type = ir_type;
310 ir_points_to = get_ir_type(points_to);
311 set_pointer_points_to_type(ir_type, ir_points_to);
316 static ir_type *create_array_type(array_type_t *type)
318 type_t *element_type = type->element_type;
319 ir_type *ir_element_type = get_ir_type(element_type);
321 ident *id = unique_ident("array");
322 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
324 if(type->size != NULL) {
325 int n_elements = fold_constant(type->size);
327 set_array_bounds_int(ir_type, 0, 0, n_elements);
329 size_t elemsize = get_type_size_bytes(ir_element_type);
330 int align = get_type_alignment_bytes(ir_element_type);
331 if(elemsize % align > 0) {
332 elemsize += align - (elemsize % align);
334 set_type_size_bytes(ir_type, n_elements * elemsize);
335 set_type_alignment_bytes(ir_type, align);
336 set_type_state(ir_type, layout_fixed);
342 #define INVALID_TYPE ((ir_type_ptr)-1)
344 static ir_type *create_struct_type(compound_type_t *type)
346 symbol_t *symbol = type->declaration->symbol;
349 id = unique_ident(symbol->string);
351 id = unique_ident("__anonymous_struct");
353 ir_type *ir_type = new_type_struct(id);
355 type->type.firm_type = ir_type;
359 declaration_t *entry = type->declaration->context.declarations;
360 for( ; entry != NULL; entry = entry->next) {
361 if(entry->namespc != NAMESPACE_NORMAL)
364 ident *ident = new_id_from_str(entry->symbol->string);
365 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
367 int entry_size = get_type_size_bytes(entry_ir_type);
368 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
369 int misalign = offset % entry_alignment;
371 offset += entry_alignment - misalign;
373 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
374 set_entity_offset(entity, offset);
375 add_struct_member(ir_type, entity);
376 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
377 entry->v.entity = entity;
379 offset += entry_size;
380 if(entry_alignment > align_all) {
381 if(entry_alignment % align_all != 0) {
382 panic("Uneven alignments not supported yet");
384 align_all = entry_alignment;
388 int misalign = offset % align_all;
390 set_type_alignment_bytes(ir_type, align_all);
391 set_type_size_bytes(ir_type, offset);
392 set_type_state(ir_type, layout_fixed);
397 static ir_type *create_union_type(compound_type_t *type)
399 declaration_t *declaration = type->declaration;
400 symbol_t *symbol = declaration->symbol;
403 id = unique_ident(symbol->string);
405 id = unique_ident("__anonymous_union");
407 ir_type *ir_type = new_type_union(id);
409 type->type.firm_type = ir_type;
413 declaration_t *entry = declaration->context.declarations;
414 for( ; entry != NULL; entry = entry->next) {
415 if(entry->namespc != NAMESPACE_NORMAL)
418 ident *ident = new_id_from_str(entry->symbol->string);
419 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
421 int entry_size = get_type_size_bytes(entry_ir_type);
422 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
424 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
425 add_union_member(ir_type, entity);
426 set_entity_offset(entity, 0);
427 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
428 entry->v.entity = entity;
430 if(entry_size > size) {
433 if(entry_alignment > align_all) {
434 if(entry_alignment % align_all != 0) {
435 panic("Uneven alignments not supported yet");
437 align_all = entry_alignment;
441 set_type_alignment_bytes(ir_type, align_all);
442 set_type_size_bytes(ir_type, size);
443 set_type_state(ir_type, layout_fixed);
448 static ir_node *expression_to_firm(const expression_t *expression);
449 static inline ir_mode *get_ir_mode(type_t *type);
451 static ir_type *create_enum_type(enum_type_t *const type)
453 type->type.firm_type = ir_type_int;
455 ir_mode *const mode = get_ir_mode((type_t*) type);
456 tarval *const one = get_mode_one(mode);
457 tarval * tv_next = get_tarval_null(mode);
459 declaration_t *declaration = type->declaration->next;
460 for (; declaration != NULL; declaration = declaration->next) {
461 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
464 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
466 expression_t *const init = declaration->init.enum_value;
468 ir_node *const cnst = expression_to_firm(init);
469 if (!is_Const(cnst)) {
470 panic("couldn't fold constant");
472 tv_next = get_Const_tarval(cnst);
474 declaration->v.enum_val = tv_next;
475 tv_next = tarval_add(tv_next, one);
481 static ir_type *get_ir_type(type_t *type)
483 assert(type != NULL);
485 type = skip_typeref(type);
487 if(type->base.firm_type != NULL) {
488 assert(type->base.firm_type != INVALID_TYPE);
489 return type->base.firm_type;
492 ir_type *firm_type = NULL;
495 firm_type = create_atomic_type(&type->atomic);
498 firm_type = create_method_type(&type->function);
501 firm_type = create_pointer_type(&type->pointer);
504 firm_type = create_array_type(&type->array);
506 case TYPE_COMPOUND_STRUCT:
507 firm_type = create_struct_type(&type->compound);
509 case TYPE_COMPOUND_UNION:
510 firm_type = create_union_type(&type->compound);
513 firm_type = create_enum_type(&type->enumt);
516 firm_type = get_ir_type(type->builtin.real_type);
523 if(firm_type == NULL)
524 panic("unknown type found");
526 type->base.firm_type = firm_type;
530 static inline ir_mode *get_ir_mode(type_t *type)
532 ir_type *irtype = get_ir_type(type);
534 /* firm doesn't report a mode for arrays somehow... */
535 if(is_Array_type(irtype)) {
539 ir_mode *mode = get_type_mode(irtype);
540 assert(mode != NULL);
544 static ir_entity* get_function_entity(declaration_t *declaration)
546 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
547 return declaration->v.entity;
548 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
550 symbol_t *symbol = declaration->symbol;
551 ident *id = new_id_from_str(symbol->string);
553 ir_type *global_type = get_glob_type();
554 ir_type *ir_type_method = get_ir_type(declaration->type);
555 assert(is_Method_type(ir_type_method));
557 ir_entity *entity = new_entity(global_type, id, ir_type_method);
558 set_entity_ld_ident(entity, id);
559 if(declaration->storage_class == STORAGE_CLASS_STATIC
560 || declaration->is_inline) {
561 set_entity_visibility(entity, visibility_local);
562 } else if(declaration->init.statement != NULL) {
563 set_entity_visibility(entity, visibility_external_visible);
565 set_entity_visibility(entity, visibility_external_allocated);
567 set_entity_allocation(entity, allocation_static);
569 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
570 declaration->v.entity = entity;
575 static dbg_info *get_dbg_info(const source_position_t *pos)
577 return (dbg_info*) pos;
580 static ir_node *const_to_firm(const const_expression_t *cnst)
582 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
583 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
588 if(mode_is_float(mode)) {
589 tv = new_tarval_from_double(cnst->v.float_value, mode);
591 if(mode_is_signed(mode)) {
592 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
594 len = snprintf(buf, sizeof(buf), "%llu",
595 (unsigned long long) cnst->v.int_value);
597 tv = new_tarval_from_str(buf, len, mode);
600 return new_d_Const(dbgi, mode, tv);
603 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
605 assert(entity != NULL);
606 union symconst_symbol sym;
607 sym.entity_p = entity;
608 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
611 static ir_node *string_to_firm(const source_position_t *const src_pos,
612 const char *const id_prefix,
613 const char *const string)
615 ir_type *const global_type = get_glob_type();
616 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
619 ident *const id = unique_ident(id_prefix);
620 ir_entity *const entity = new_entity(global_type, id, type);
621 set_entity_ld_ident(entity, id);
622 set_entity_variability(entity, variability_constant);
623 set_entity_allocation(entity, allocation_static);
625 ir_type *const elem_type = ir_type_const_char;
626 ir_mode *const mode = get_type_mode(elem_type);
628 const size_t slen = strlen(string) + 1;
630 set_array_lower_bound_int(type, 0, 0);
631 set_array_upper_bound_int(type, 0, slen);
632 set_type_size_bytes(type, slen);
633 set_type_state(type, layout_fixed);
635 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
636 for(size_t i = 0; i < slen; ++i) {
637 tvs[i] = new_tarval_from_long(string[i], mode);
640 set_array_entity_values(entity, tvs, slen);
643 dbg_info *const dbgi = get_dbg_info(src_pos);
645 return create_symconst(dbgi, entity);
648 static ir_node *string_literal_to_firm(
649 const string_literal_expression_t* literal)
651 return string_to_firm(&literal->expression.source_position, "Lstr",
655 static ir_node *wide_string_literal_to_firm(
656 const wide_string_literal_expression_t* const literal)
658 ir_type *const global_type = get_glob_type();
659 ir_type *const elem_type = ir_type_wchar_t;
660 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
663 ident *const id = unique_ident("Lstr");
664 ir_entity *const entity = new_entity(global_type, id, type);
665 set_entity_ld_ident(entity, id);
666 set_entity_variability(entity, variability_constant);
667 set_entity_allocation(entity, allocation_static);
669 ir_mode *const mode = get_type_mode(elem_type);
671 const wchar_rep_t *const string = literal->value.begin;
672 const size_t slen = literal->value.size;
674 set_array_lower_bound_int(type, 0, 0);
675 set_array_upper_bound_int(type, 0, slen);
676 set_type_size_bytes(type, slen);
677 set_type_state(type, layout_fixed);
679 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
680 for(size_t i = 0; i < slen; ++i) {
681 tvs[i] = new_tarval_from_long(string[i], mode);
684 set_array_entity_values(entity, tvs, slen);
687 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
689 return create_symconst(dbgi, entity);
692 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
693 dbg_info *const dbgi)
695 if(is_compound_type(irtype) || is_Array_type(irtype)) {
699 ir_mode *const mode = get_type_mode(irtype);
700 ir_node *const memory = get_store();
701 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
702 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
703 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
708 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
710 ir_mode *mode = get_irn_mode(node);
712 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
714 if(!mode_is_float(mode))
717 /* check if there is already a Conv */
718 if (get_irn_op(node) == op_Conv) {
719 /* convert it into a strict Conv */
720 set_Conv_strict(node, 1);
724 /* otherwise create a new one */
725 return new_d_strictConv(dbgi, node, mode);
728 static ir_node *get_global_var_address(dbg_info *const dbgi,
729 const declaration_t *const decl)
731 assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
733 ir_entity *const entity = decl->v.entity;
734 switch ((storage_class_tag_t)decl->storage_class) {
735 case STORAGE_CLASS_THREAD:
736 case STORAGE_CLASS_THREAD_EXTERN:
737 case STORAGE_CLASS_THREAD_STATIC: {
738 ir_node *const no_mem = new_NoMem();
739 ir_node *const tls = get_irg_tls(current_ir_graph);
740 return new_d_simpleSel(dbgi, no_mem, tls, entity);
744 return create_symconst(dbgi, entity);
748 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
750 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
751 declaration_t *declaration = ref->declaration;
752 type_t *type = skip_typeref(declaration->type);
754 switch((declaration_type_t) declaration->declaration_type) {
755 case DECLARATION_TYPE_UNKNOWN:
756 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
762 case DECLARATION_TYPE_ENUM_ENTRY: {
763 ir_mode *const mode = get_ir_mode(type);
764 return new_Const(mode, declaration->v.enum_val);
767 case DECLARATION_TYPE_LOCAL_VARIABLE: {
768 ir_mode *mode = get_ir_mode(type);
769 return get_value(declaration->v.value_number, mode);
771 case DECLARATION_TYPE_FUNCTION: {
772 return create_symconst(dbgi, declaration->v.entity);
774 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
775 ir_node *const addr = get_global_var_address(dbgi, declaration);
776 ir_type *const irtype = get_entity_type(declaration->v.entity);
777 return deref_address(irtype, addr, dbgi);
780 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
781 ir_entity *entity = declaration->v.entity;
782 ir_node *frame = get_irg_frame(current_ir_graph);
783 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
784 ir_type *irtype = get_entity_type(entity);
785 return deref_address(irtype, sel, dbgi);
788 case DECLARATION_TYPE_COMPOUND_MEMBER:
789 case DECLARATION_TYPE_LABEL_BLOCK:
790 panic("not implemented reference type");
793 panic("reference to declaration with unknown type found");
796 static ir_node *reference_addr(const reference_expression_t *ref)
798 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
799 declaration_t *declaration = ref->declaration;
801 switch((declaration_type_t) declaration->declaration_type) {
802 case DECLARATION_TYPE_UNKNOWN:
804 case DECLARATION_TYPE_LOCAL_VARIABLE:
805 panic("local variable without entity has no address");
806 case DECLARATION_TYPE_FUNCTION: {
807 return create_symconst(dbgi, declaration->v.entity);
809 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
810 ir_node *const addr = get_global_var_address(dbgi, declaration);
813 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
814 ir_entity *entity = declaration->v.entity;
815 ir_node *frame = get_irg_frame(current_ir_graph);
816 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
821 case DECLARATION_TYPE_ENUM_ENTRY:
822 panic("trying to reference enum entry");
824 case DECLARATION_TYPE_COMPOUND_MEMBER:
825 case DECLARATION_TYPE_LABEL_BLOCK:
826 panic("not implemented reference type");
829 panic("reference to declaration with unknown type found");
832 static ir_node *process_builtin_call(const call_expression_t *call)
834 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
836 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
837 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
839 type_t *type = skip_typeref(builtin->expression.datatype);
840 assert(is_type_pointer(type));
842 type_t *function_type = skip_typeref(type->pointer.points_to);
843 symbol_t *symbol = builtin->symbol;
846 case T___builtin_alloca: {
847 if(call->arguments == NULL || call->arguments->next != NULL) {
848 panic("invalid number of parameters on __builtin_alloca");
850 expression_t *argument = call->arguments->expression;
851 ir_node *size = expression_to_firm(argument);
853 ir_node *store = get_store();
854 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
856 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
858 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
862 case T___builtin_nan:
863 case T___builtin_nanf:
864 case T___builtin_nand: {
865 /* Ignore string for now... */
866 assert(is_type_function(function_type));
867 ir_mode *mode = get_ir_mode(function_type->function.return_type);
868 tarval *tv = get_mode_NAN(mode);
869 ir_node *res = new_d_Const(dbgi, mode, tv);
872 case T___builtin_va_end:
875 panic("Unsupported builtin found\n");
879 static ir_node *call_expression_to_firm(const call_expression_t *call)
881 assert(get_cur_block() != NULL);
883 expression_t *function = call->function;
884 if(function->type == EXPR_BUILTIN_SYMBOL) {
885 return process_builtin_call(call);
887 ir_node *callee = expression_to_firm(function);
889 type_t *type = skip_typeref(function->base.datatype);
890 assert(is_type_pointer(type));
891 pointer_type_t *pointer_type = &type->pointer;
892 type_t *points_to = skip_typeref(pointer_type->points_to);
893 assert(is_type_function(points_to));
894 function_type_t *function_type = &points_to->function;
896 int n_parameters = 0;
897 call_argument_t *argument = call->arguments;
898 for( ; argument != NULL; argument = argument->next) {
902 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
904 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
905 ir_type *new_method_type = NULL;
906 if(function_type->variadic || function_type->unspecified_parameters) {
907 /* we need to construct a new method type matching the call
909 int n_res = get_method_n_ress(ir_method_type);
910 new_method_type = new_type_method(unique_ident("calltype"),
911 n_parameters, n_res);
912 set_method_calling_convention(new_method_type,
913 get_method_calling_convention(ir_method_type));
914 set_method_additional_properties(new_method_type,
915 get_method_additional_properties(ir_method_type));
917 for(int i = 0; i < n_res; ++i) {
918 set_method_res_type(new_method_type, i,
919 get_method_res_type(ir_method_type, i));
922 ir_node *in[n_parameters];
924 argument = call->arguments;
926 for( ; argument != NULL; argument = argument->next) {
927 expression_t *expression = argument->expression;
928 ir_node *arg_node = expression_to_firm(expression);
930 arg_node = do_strict_conv(dbgi, arg_node);
933 if(new_method_type != NULL) {
934 ir_type *irtype = get_ir_type(expression->base.datatype);
935 set_method_param_type(new_method_type, n, irtype);
940 assert(n == n_parameters);
942 if(new_method_type != NULL)
943 ir_method_type = new_method_type;
945 ir_node *store = get_store();
946 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
948 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
951 type_t *return_type = skip_typeref(function_type->return_type);
952 ir_node *result = NULL;
954 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
956 if(is_type_scalar(return_type)) {
957 mode = get_ir_mode(return_type);
961 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
962 result = new_d_Proj(dbgi, resproj, mode, 0);
968 static void statement_to_firm(statement_t *statement);
969 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
971 static ir_node *expression_to_addr(const expression_t *expression);
972 static void create_condition_evaluation(const expression_t *expression,
974 ir_node *false_block);
976 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
979 value = do_strict_conv(dbgi, value);
981 ir_node *memory = get_store();
983 if(is_type_scalar(type)) {
984 ir_node *store = new_d_Store(dbgi, memory, addr, value);
985 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
986 set_store(store_mem);
988 ir_type *irtype = get_ir_type(type);
989 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
990 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
991 set_store(copyb_mem);
995 static void set_value_for_expression(const expression_t *expression,
998 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
999 value = do_strict_conv(dbgi, value);
1001 if(expression->type == EXPR_REFERENCE) {
1002 reference_expression_t *ref = (reference_expression_t*) expression;
1004 declaration_t *declaration = ref->declaration;
1005 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
1006 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1007 set_value(declaration->v.value_number, value);
1012 ir_node *addr = expression_to_addr(expression);
1013 type_t *type = skip_typeref(expression->base.datatype);
1014 assign_value(dbgi, addr, type, value);
1017 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1019 ir_mode *value_mode = get_irn_mode(value);
1021 if (value_mode == dest_mode || is_Bad(value))
1024 if(dest_mode == mode_b) {
1025 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1026 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1027 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1031 return new_d_Conv(dbgi, value, dest_mode);
1034 static ir_node *create_incdec(const unary_expression_t *expression)
1036 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1037 type_t *type = skip_typeref(expression->expression.datatype);
1038 ir_mode *mode = get_ir_mode(type);
1039 expression_t *value = expression->value;
1041 ir_node *value_node = expression_to_firm(value);
1044 if(is_type_pointer(type)) {
1045 pointer_type_t *pointer_type = &type->pointer;
1046 unsigned elem_size = get_type_size(pointer_type->points_to);
1047 offset = new_Const_long(mode_Is, elem_size);
1049 assert(is_type_arithmetic(type));
1050 offset = new_Const(mode, get_mode_one(mode));
1053 switch(expression->expression.type) {
1054 case EXPR_UNARY_POSTFIX_INCREMENT: {
1055 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1056 set_value_for_expression(value, new_value);
1059 case EXPR_UNARY_POSTFIX_DECREMENT: {
1060 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1061 set_value_for_expression(value, new_value);
1064 case EXPR_UNARY_PREFIX_INCREMENT: {
1065 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1066 set_value_for_expression(value, new_value);
1069 case EXPR_UNARY_PREFIX_DECREMENT: {
1070 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1071 set_value_for_expression(value, new_value);
1075 panic("no incdec expr in create_incdec");
1080 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1082 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1083 type_t *type = skip_typeref(expression->expression.datatype);
1084 ir_mode *mode = get_ir_mode(type);
1086 if(expression->expression.type == EXPR_UNARY_TAKE_ADDRESS)
1087 return expression_to_addr(expression->value);
1089 const expression_t *value = expression->value;
1090 ir_node *value_node = expression_to_firm(value);
1092 switch(expression->expression.type) {
1093 case EXPR_UNARY_NEGATE:
1094 return new_d_Minus(dbgi, value_node, mode);
1095 case EXPR_UNARY_PLUS:
1097 case EXPR_UNARY_BITWISE_NEGATE:
1098 return new_d_Not(dbgi, value_node, mode);
1099 case EXPR_UNARY_NOT: {
1100 if(get_irn_mode(value_node) != mode_b) {
1101 value_node = create_conv(dbgi, value_node, mode_b);
1103 value_node = new_d_Not(dbgi, value_node, mode_b);
1104 if(mode != mode_b) {
1105 value_node = create_conv(dbgi, value_node, mode);
1109 case EXPR_UNARY_DEREFERENCE: {
1110 type_t *value_type = skip_typeref(value->base.datatype);
1111 ir_type *irtype = get_ir_type(value_type);
1112 assert(is_Pointer_type(irtype));
1113 ir_type *points_to = get_pointer_points_to_type(irtype);
1114 return deref_address(points_to, value_node, dbgi);
1116 case EXPR_UNARY_POSTFIX_INCREMENT:
1117 case EXPR_UNARY_POSTFIX_DECREMENT:
1118 case EXPR_UNARY_PREFIX_INCREMENT:
1119 case EXPR_UNARY_PREFIX_DECREMENT:
1120 return create_incdec(expression);
1121 case EXPR_UNARY_CAST: {
1122 ir_node *node = create_conv(dbgi, value_node, get_ir_mode(type));
1123 node = do_strict_conv(dbgi, node);
1126 case EXPR_UNARY_CAST_IMPLICIT:
1127 return create_conv(dbgi, value_node, get_ir_mode(type));
1132 panic("invalid UNEXPR type found");
1135 static long get_pnc(const expression_type_t type)
1138 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1139 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1140 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Ne;
1141 case EXPR_BINARY_ISLESS:
1142 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1143 case EXPR_BINARY_ISLESSEQUAL:
1144 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1145 case EXPR_BINARY_ISGREATER:
1146 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1147 case EXPR_BINARY_ISGREATEREQUAL:
1148 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1149 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1154 panic("trying to get pn_Cmp from non-comparison binexpr type");
1157 static ir_node *create_lazy_op(const binary_expression_t *expression)
1159 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1160 type_t *type = expression->expression.datatype;
1161 ir_mode *mode = get_ir_mode(type);
1163 ir_node *cur_block = get_cur_block();
1165 ir_node *one_block = new_immBlock();
1166 ir_node *one = new_Const(mode, get_mode_one(mode));
1167 ir_node *jmp_one = new_d_Jmp(dbgi);
1169 ir_node *zero_block = new_immBlock();
1170 ir_node *zero = new_Const(mode, get_mode_null(mode));
1171 ir_node *jmp_zero = new_d_Jmp(dbgi);
1173 set_cur_block(cur_block);
1174 create_condition_evaluation((const expression_t*) expression,
1175 one_block, zero_block);
1176 mature_immBlock(one_block);
1177 mature_immBlock(zero_block);
1179 ir_node *common_block = new_immBlock();
1180 add_immBlock_pred(common_block, jmp_one);
1181 add_immBlock_pred(common_block, jmp_zero);
1182 mature_immBlock(common_block);
1184 ir_node *in[2] = { one, zero };
1185 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1190 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1191 ir_node *right, ir_mode *mode);
1193 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1194 create_arithmetic_func func)
1196 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1197 ir_node *left = expression_to_firm(expression->left);
1198 ir_node *right = expression_to_firm(expression->right);
1199 type_t *type = expression->right->base.datatype;
1200 /* be careful with the modes, because in arithmetic assign nodes only
1201 * the right operand has the mode of the arithmetic already */
1202 ir_mode *mode = get_ir_mode(type);
1203 left = create_conv(dbgi, left, mode);
1204 ir_node *res = func(dbgi, left, right, mode);
1209 static ir_node *pointer_arithmetic(ir_node *const pointer,
1212 dbg_info *const dbgi,
1213 const create_arithmetic_func func)
1215 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1216 type_t *const points_to = pointer_type->points_to;
1217 const unsigned elem_size = get_type_size(points_to);
1219 assert(elem_size >= 1);
1220 if (elem_size > 1) {
1221 integer = create_conv(dbgi, integer, mode_Is);
1222 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1223 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1227 ir_mode *const mode = get_ir_mode(type);
1228 return func(dbgi, pointer, integer, mode);
1231 static ir_node *create_arithmetic_assign_binop(
1232 const binary_expression_t *expression, create_arithmetic_func func)
1234 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1235 type_t *const type = skip_typeref(expression->expression.datatype);
1238 if (is_type_pointer(type)) {
1239 ir_node *const pointer = expression_to_firm(expression->left);
1240 ir_node * integer = expression_to_firm(expression->right);
1241 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1243 value = create_arithmetic_binop(expression, func);
1246 ir_mode *const mode = get_ir_mode(type);
1247 value = create_conv(dbgi, value, mode);
1248 set_value_for_expression(expression->left, value);
1253 static ir_node *create_add(const binary_expression_t *expression)
1255 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1256 ir_node *left = expression_to_firm(expression->left);
1257 ir_node *right = expression_to_firm(expression->right);
1258 type_t *type = expression->expression.datatype;
1260 expression_t *expr_left = expression->left;
1261 expression_t *expr_right = expression->right;
1262 type_t *type_left = skip_typeref(expr_left->base.datatype);
1263 type_t *type_right = skip_typeref(expr_right->base.datatype);
1265 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1266 ir_mode *const mode = get_ir_mode(type);
1267 return new_d_Add(dbgi, left, right, mode);
1270 if (is_type_pointer(type_left)) {
1271 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1273 assert(is_type_pointer(type_right));
1274 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1278 static ir_node *create_sub(const binary_expression_t *expression)
1280 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1281 expression_t *const expr_left = expression->left;
1282 expression_t *const expr_right = expression->right;
1283 ir_node *const left = expression_to_firm(expr_left);
1284 ir_node *const right = expression_to_firm(expr_right);
1285 type_t *const type = expression->expression.datatype;
1286 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1287 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1289 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1290 ir_mode *const mode = get_ir_mode(type);
1291 return new_d_Sub(dbgi, left, right, mode);
1292 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1293 const pointer_type_t *const ptr_type = &type_left->pointer;
1294 const unsigned elem_size = get_type_size(ptr_type->points_to);
1295 ir_mode *const mode = get_ir_mode(type);
1296 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1297 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1298 ir_node *const no_mem = new_NoMem();
1299 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1300 op_pin_state_floats);
1301 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1304 assert(is_type_pointer(type_left));
1305 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1308 static ir_node *create_shift(const binary_expression_t *expression)
1310 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1311 ir_node *left = expression_to_firm(expression->left);
1312 ir_node *right = expression_to_firm(expression->right);
1313 type_t *type = expression->expression.datatype;
1314 ir_mode *mode = get_ir_mode(type);
1316 /* firm always wants the shift count to be unsigned */
1317 right = create_conv(dbgi, right, mode_Iu);
1321 switch(expression->expression.type) {
1322 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1323 case EXPR_BINARY_SHIFTLEFT:
1324 res = new_d_Shl(dbgi, left, right, mode);
1326 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1327 case EXPR_BINARY_SHIFTRIGHT: {
1328 expression_t *expr_left = expression->left;
1329 type_t *type_left = skip_typeref(expr_left->base.datatype);
1331 if(is_type_signed(type_left)) {
1332 res = new_d_Shrs(dbgi, left, right, mode);
1334 res = new_d_Shr(dbgi, left, right, mode);
1339 panic("create shift op called for non-shift op");
1346 static ir_node *create_divmod(const binary_expression_t *expression)
1348 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1349 ir_node *left = expression_to_firm(expression->left);
1350 ir_node *right = expression_to_firm(expression->right);
1351 ir_node *pin = new_Pin(new_NoMem());
1352 /* be careful with the modes, because in arithmetic assign nodes only
1353 * the right operand has the mode of the arithmetic already */
1354 type_t *type = expression->right->base.datatype;
1355 ir_mode *mode = get_ir_mode(type);
1356 left = create_conv(dbgi, left, mode);
1360 switch (expression->expression.type) {
1361 case EXPR_BINARY_DIV:
1362 case EXPR_BINARY_DIV_ASSIGN:
1363 if(mode_is_float(mode)) {
1364 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1365 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1367 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1368 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1372 case EXPR_BINARY_MOD:
1373 case EXPR_BINARY_MOD_ASSIGN:
1374 assert(!mode_is_float(mode));
1375 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1376 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1379 default: panic("unexpected binary expression type in create_divmod()");
1385 static ir_node *create_arithmetic_assign_divmod(
1386 const binary_expression_t *expression)
1388 ir_node * value = create_divmod(expression);
1389 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1390 type_t *const type = expression->expression.datatype;
1391 ir_mode *const mode = get_ir_mode(type);
1393 assert(type->type != TYPE_POINTER);
1395 value = create_conv(dbgi, value, mode);
1396 set_value_for_expression(expression->left, value);
1401 static ir_node *create_arithmetic_assign_shift(
1402 const binary_expression_t *expression)
1404 ir_node * value = create_shift(expression);
1405 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1406 type_t *const type = expression->expression.datatype;
1407 ir_mode *const mode = get_ir_mode(type);
1409 value = create_conv(dbgi, value, mode);
1410 set_value_for_expression(expression->left, value);
1415 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1417 expression_type_t type = expression->expression.type;
1420 case EXPR_BINARY_EQUAL:
1421 case EXPR_BINARY_NOTEQUAL:
1422 case EXPR_BINARY_LESS:
1423 case EXPR_BINARY_LESSEQUAL:
1424 case EXPR_BINARY_GREATER:
1425 case EXPR_BINARY_GREATEREQUAL:
1426 case EXPR_BINARY_ISGREATER:
1427 case EXPR_BINARY_ISGREATEREQUAL:
1428 case EXPR_BINARY_ISLESS:
1429 case EXPR_BINARY_ISLESSEQUAL:
1430 case EXPR_BINARY_ISLESSGREATER:
1431 case EXPR_BINARY_ISUNORDERED: {
1432 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1433 ir_node *left = expression_to_firm(expression->left);
1434 ir_node *right = expression_to_firm(expression->right);
1435 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1436 long pnc = get_pnc(type);
1437 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1440 case EXPR_BINARY_ASSIGN: {
1441 ir_node *right = expression_to_firm(expression->right);
1442 set_value_for_expression(expression->left, right);
1446 case EXPR_BINARY_ADD:
1447 return create_add(expression);
1448 case EXPR_BINARY_SUB:
1449 return create_sub(expression);
1450 case EXPR_BINARY_MUL:
1451 return create_arithmetic_binop(expression, new_d_Mul);
1452 case EXPR_BINARY_BITWISE_AND:
1453 return create_arithmetic_binop(expression, new_d_And);
1454 case EXPR_BINARY_BITWISE_OR:
1455 return create_arithmetic_binop(expression, new_d_Or);
1456 case EXPR_BINARY_BITWISE_XOR:
1457 return create_arithmetic_binop(expression, new_d_Eor);
1458 case EXPR_BINARY_SHIFTLEFT:
1459 case EXPR_BINARY_SHIFTRIGHT:
1460 return create_shift(expression);
1461 case EXPR_BINARY_DIV:
1462 case EXPR_BINARY_MOD:
1463 return create_divmod(expression);
1464 case EXPR_BINARY_LOGICAL_AND:
1465 case EXPR_BINARY_LOGICAL_OR:
1466 return create_lazy_op(expression);
1467 case EXPR_BINARY_COMMA:
1468 expression_to_firm(expression->left);
1469 return expression_to_firm(expression->right);
1470 case EXPR_BINARY_ADD_ASSIGN:
1471 return create_arithmetic_assign_binop(expression, new_d_Add);
1472 case EXPR_BINARY_SUB_ASSIGN:
1473 return create_arithmetic_assign_binop(expression, new_d_Sub);
1474 case EXPR_BINARY_MUL_ASSIGN:
1475 return create_arithmetic_assign_binop(expression, new_d_Mul);
1476 case EXPR_BINARY_DIV_ASSIGN:
1477 return create_arithmetic_assign_divmod(expression);
1478 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1479 return create_arithmetic_assign_binop(expression, new_d_And);
1480 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1481 return create_arithmetic_assign_binop(expression, new_d_Or);
1482 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1483 return create_arithmetic_assign_binop(expression, new_d_Eor);
1484 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1485 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1486 return create_arithmetic_assign_shift(expression);
1488 panic("TODO binexpr type");
1492 static ir_node *array_access_addr(const array_access_expression_t *expression)
1494 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1495 ir_node *base_addr = expression_to_firm(expression->array_ref);
1496 ir_node *offset = expression_to_firm(expression->index);
1497 offset = create_conv(dbgi, offset, mode_Iu);
1499 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1500 assert(is_type_pointer(ref_type));
1501 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1503 unsigned elem_size = get_type_size(pointer_type->points_to);
1504 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1505 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1507 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1512 static ir_node *array_access_to_firm(
1513 const array_access_expression_t *expression)
1515 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1516 ir_node *addr = array_access_addr(expression);
1517 type_t *type = revert_automatic_type_conversion(
1518 (const expression_t*) expression);
1519 type = skip_typeref(type);
1520 ir_type *irtype = get_ir_type(type);
1522 return deref_address(irtype, addr, dbgi);
1525 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1527 type_t *type = expression->type;
1529 type = expression->size_expression->base.datatype;
1530 assert(type != NULL);
1533 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1534 unsigned size = get_type_size(type);
1535 ir_node *size_node = new_Const_long(mode, size);
1540 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1542 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1544 ir_node *cur_block = get_cur_block();
1546 /* create the true block */
1547 ir_node *true_block = new_immBlock();
1549 ir_node *true_val = expression_to_firm(expression->true_expression);
1550 ir_node *true_jmp = new_Jmp();
1552 /* create the false block */
1553 ir_node *false_block = new_immBlock();
1555 ir_node *false_val = expression_to_firm(expression->false_expression);
1556 ir_node *false_jmp = new_Jmp();
1558 /* create the condition evaluation */
1559 set_cur_block(cur_block);
1560 create_condition_evaluation(expression->condition, true_block, false_block);
1561 mature_immBlock(true_block);
1562 mature_immBlock(false_block);
1564 /* create the common block */
1565 ir_node *common_block = new_immBlock();
1566 add_immBlock_pred(common_block, true_jmp);
1567 add_immBlock_pred(common_block, false_jmp);
1568 mature_immBlock(common_block);
1570 /* TODO improve static semantics, so either both or no values are NULL */
1571 if (true_val == NULL || false_val == NULL)
1574 ir_node *in[2] = { true_val, false_val };
1575 ir_mode *mode = get_irn_mode(true_val);
1576 assert(get_irn_mode(false_val) == mode);
1577 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1582 static ir_node *select_addr(const select_expression_t *expression)
1584 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1586 ir_node *compound_addr = expression_to_firm(expression->compound);
1588 declaration_t *entry = expression->compound_entry;
1589 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1590 ir_entity *entity = entry->v.entity;
1592 assert(entity != NULL);
1594 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1599 static ir_node *select_to_firm(const select_expression_t *expression)
1601 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1602 ir_node *addr = select_addr(expression);
1603 type_t *type = revert_automatic_type_conversion(
1604 (const expression_t*) expression);
1605 type = skip_typeref(type);
1606 ir_type *irtype = get_ir_type(type);
1608 return deref_address(irtype, addr, dbgi);
1611 /* Values returned by __builtin_classify_type. */
1612 typedef enum gcc_type_class
1618 enumeral_type_class,
1621 reference_type_class,
1625 function_type_class,
1636 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1638 const type_t *const type = expr->type_expression->base.datatype;
1644 const atomic_type_t *const atomic_type = &type->atomic;
1645 switch (atomic_type->atype) {
1646 // should not be reached
1647 case ATOMIC_TYPE_INVALID:
1651 // gcc cannot do that
1652 case ATOMIC_TYPE_VOID:
1653 tc = void_type_class;
1656 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1657 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1658 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1659 case ATOMIC_TYPE_SHORT:
1660 case ATOMIC_TYPE_USHORT:
1661 case ATOMIC_TYPE_INT:
1662 case ATOMIC_TYPE_UINT:
1663 case ATOMIC_TYPE_LONG:
1664 case ATOMIC_TYPE_ULONG:
1665 case ATOMIC_TYPE_LONGLONG:
1666 case ATOMIC_TYPE_ULONGLONG:
1667 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1668 tc = integer_type_class;
1671 case ATOMIC_TYPE_FLOAT:
1672 case ATOMIC_TYPE_DOUBLE:
1673 case ATOMIC_TYPE_LONG_DOUBLE:
1674 tc = real_type_class;
1677 #ifdef PROVIDE_COMPLEX
1678 case ATOMIC_TYPE_FLOAT_COMPLEX:
1679 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1680 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1681 tc = complex_type_class;
1683 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1684 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1685 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1686 tc = complex_type_class;
1691 panic("Unimplemented case in classify_type_to_firm().");
1696 case TYPE_ARRAY: // gcc handles this as pointer
1697 case TYPE_FUNCTION: // gcc handles this as pointer
1698 case TYPE_POINTER: tc = pointer_type_class; break;
1699 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1700 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1702 // gcc handles this as integer
1703 case TYPE_ENUM: tc = integer_type_class; break;
1706 panic("Unimplemented case in classify_type_to_firm().");
1709 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1710 ir_mode *const mode = mode_Is;
1711 tarval *const tv = new_tarval_from_long(tc, mode);
1712 return new_d_Const(dbgi, mode, tv);
1715 static ir_node *function_name_to_firm(
1716 const string_literal_expression_t *const expr)
1718 if (current_function_name == NULL) {
1719 const source_position_t *const src_pos =
1720 &expr->expression.source_position;
1721 const char *const name = current_function_decl->symbol->string;
1722 current_function_name = string_to_firm(src_pos, "__func__", name);
1725 return current_function_name;
1728 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1730 statement_t *statement = expr->statement;
1732 assert(statement->type == STATEMENT_COMPOUND);
1733 return compound_statement_to_firm((compound_statement_t*) statement);
1736 static ir_node *va_start_expression_to_firm(
1737 const va_start_expression_t *const expr)
1739 ir_type *const method_type = get_ir_type(current_function_decl->type);
1740 int const n = get_method_n_params(method_type) - 1;
1741 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
1742 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
1743 dbg_info *const dbgi =
1744 get_dbg_info(&expr->expression.source_position);
1745 ir_node *const no_mem = new_NoMem();
1746 ir_node *const arg_sel =
1747 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
1749 size_t const parm_size = get_type_size(expr->parameter->type);
1750 ir_node *const cnst = new_Const_long(mode_Iu, parm_size);
1751 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
1752 set_value_for_expression(expr->ap, add);
1757 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
1759 ir_type *const irtype = get_ir_type(expr->expression.datatype);
1760 ir_node *const ap = expression_to_firm(expr->ap);
1761 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1762 ir_node *const res = deref_address(irtype, ap, dbgi);
1764 size_t const parm_size = get_type_size(expr->expression.datatype);
1765 ir_node *const cnst = new_Const_long(mode_Iu, parm_size);
1766 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
1767 set_value_for_expression(expr->ap, add);
1772 static ir_node *dereference_addr(const unary_expression_t *const expression)
1774 assert(expression->expression.type == EXPR_UNARY_DEREFERENCE);
1775 return expression_to_firm(expression->value);
1778 static ir_node *expression_to_addr(const expression_t *expression)
1780 switch(expression->type) {
1781 case EXPR_REFERENCE:
1782 return reference_addr(&expression->reference);
1783 case EXPR_ARRAY_ACCESS:
1784 return array_access_addr(&expression->array_access);
1786 return select_addr(&expression->select);
1788 return call_expression_to_firm(&expression->call);
1789 case EXPR_UNARY_DEREFERENCE: {
1790 return dereference_addr(&expression->unary);
1795 panic("trying to get address of non-lvalue");
1798 static ir_node *_expression_to_firm(const expression_t *expression)
1800 switch(expression->type) {
1802 return const_to_firm(&expression->conste);
1803 case EXPR_STRING_LITERAL:
1804 return string_literal_to_firm(&expression->string);
1805 case EXPR_WIDE_STRING_LITERAL:
1806 return wide_string_literal_to_firm(&expression->wide_string);
1807 case EXPR_REFERENCE:
1808 return reference_expression_to_firm(&expression->reference);
1810 return call_expression_to_firm(&expression->call);
1812 return unary_expression_to_firm(&expression->unary);
1814 return binary_expression_to_firm(&expression->binary);
1815 case EXPR_ARRAY_ACCESS:
1816 return array_access_to_firm(&expression->array_access);
1818 return sizeof_to_firm(&expression->sizeofe);
1819 case EXPR_CONDITIONAL:
1820 return conditional_to_firm(&expression->conditional);
1822 return select_to_firm(&expression->select);
1823 case EXPR_CLASSIFY_TYPE:
1824 return classify_type_to_firm(&expression->classify_type);
1826 case EXPR_PRETTY_FUNCTION:
1827 return function_name_to_firm(&expression->string);
1828 case EXPR_STATEMENT:
1829 return statement_expression_to_firm(&expression->statement);
1831 return va_start_expression_to_firm(&expression->va_starte);
1833 return va_arg_expression_to_firm(&expression->va_arge);
1835 case EXPR_BUILTIN_SYMBOL:
1836 panic("unimplemented expression found");
1842 panic("invalid expression found");
1845 static ir_node *expression_to_firm(const expression_t *expression)
1847 ir_node *res = _expression_to_firm(expression);
1849 if(res != NULL && get_irn_mode(res) == mode_b) {
1850 ir_mode *mode = get_ir_mode(expression->base.datatype);
1851 res = create_conv(NULL, res, mode);
1857 static ir_node *expression_to_modeb(const expression_t *expression)
1859 ir_node *res = _expression_to_firm(expression);
1860 res = create_conv(NULL, res, mode_b);
1866 * create a short-circuit expression evaluation that tries to construct
1867 * efficient control flow structures for &&, || and ! expressions
1869 static void create_condition_evaluation(const expression_t *expression,
1870 ir_node *true_block,
1871 ir_node *false_block)
1873 switch(expression->type) {
1874 case EXPR_UNARY_NOT: {
1875 const unary_expression_t *unary_expression = &expression->unary;
1876 create_condition_evaluation(unary_expression->value, false_block,
1880 case EXPR_BINARY_LOGICAL_AND: {
1881 const binary_expression_t *binary_expression = &expression->binary;
1883 ir_node *cur_block = get_cur_block();
1884 ir_node *extra_block = new_immBlock();
1885 set_cur_block(cur_block);
1886 create_condition_evaluation(binary_expression->left, extra_block,
1888 mature_immBlock(extra_block);
1889 set_cur_block(extra_block);
1890 create_condition_evaluation(binary_expression->right, true_block,
1894 case EXPR_BINARY_LOGICAL_OR: {
1895 const binary_expression_t *binary_expression = &expression->binary;
1897 ir_node *cur_block = get_cur_block();
1898 ir_node *extra_block = new_immBlock();
1899 set_cur_block(cur_block);
1900 create_condition_evaluation(binary_expression->left, true_block,
1902 mature_immBlock(extra_block);
1903 set_cur_block(extra_block);
1904 create_condition_evaluation(binary_expression->right, true_block,
1912 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1913 ir_node *condition = expression_to_modeb(expression);
1914 ir_node *cond = new_d_Cond(dbgi, condition);
1915 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1916 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1918 add_immBlock_pred(true_block, true_proj);
1919 add_immBlock_pred(false_block, false_proj);
1921 set_cur_block(NULL);
1924 static void return_statement_to_firm(return_statement_t *statement)
1926 if(get_cur_block() == NULL)
1929 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1931 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1935 if(get_method_n_ress(func_irtype) > 0) {
1936 ir_type *res_type = get_method_res_type(func_irtype, 0);
1938 if(statement->return_value != NULL) {
1939 ir_node *node = expression_to_firm(statement->return_value);
1940 node = do_strict_conv(dbgi, node);
1944 if(is_compound_type(res_type)) {
1947 mode = get_type_mode(res_type);
1949 in[0] = new_Unknown(mode);
1953 /* build return_value for its side effects */
1954 if(statement->return_value != NULL) {
1955 expression_to_firm(statement->return_value);
1960 ir_node *store = get_store();
1961 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1963 ir_node *end_block = get_irg_end_block(current_ir_graph);
1964 add_immBlock_pred(end_block, ret);
1966 set_cur_block(NULL);
1969 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1971 if(get_cur_block() == NULL)
1974 return expression_to_firm(statement->expression);
1977 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1979 ir_node *result = NULL;
1980 statement_t *statement = compound->statements;
1981 for( ; statement != NULL; statement = statement->base.next) {
1982 //context2firm(&statement->context);
1984 if(statement->base.next == NULL
1985 && statement->type == STATEMENT_EXPRESSION) {
1986 result = expression_statement_to_firm(
1987 (expression_statement_t*) statement);
1990 statement_to_firm(statement);
1996 static void if_statement_to_firm(if_statement_t *statement)
1998 ir_node *cur_block = get_cur_block();
2000 ir_node *fallthrough_block = new_immBlock();
2002 /* the true (blocks) */
2003 ir_node *true_block;
2004 if (statement->true_statement != NULL) {
2005 true_block = new_immBlock();
2006 statement_to_firm(statement->true_statement);
2007 if(get_cur_block() != NULL) {
2008 ir_node *jmp = new_Jmp();
2009 add_immBlock_pred(fallthrough_block, jmp);
2012 true_block = fallthrough_block;
2015 /* the false (blocks) */
2016 ir_node *false_block;
2017 if(statement->false_statement != NULL) {
2018 false_block = new_immBlock();
2020 statement_to_firm(statement->false_statement);
2021 if(get_cur_block() != NULL) {
2022 ir_node *jmp = new_Jmp();
2023 add_immBlock_pred(fallthrough_block, jmp);
2026 false_block = fallthrough_block;
2029 /* create the condition */
2030 if(cur_block != NULL) {
2031 set_cur_block(cur_block);
2032 create_condition_evaluation(statement->condition, true_block,
2036 mature_immBlock(true_block);
2037 if(false_block != fallthrough_block) {
2038 mature_immBlock(false_block);
2040 mature_immBlock(fallthrough_block);
2042 set_cur_block(fallthrough_block);
2045 static void while_statement_to_firm(while_statement_t *statement)
2047 ir_node *jmp = NULL;
2048 if(get_cur_block() != NULL) {
2052 /* create the header block */
2053 ir_node *header_block = new_immBlock();
2055 add_immBlock_pred(header_block, jmp);
2058 /* the false block */
2059 ir_node *false_block = new_immBlock();
2062 ir_node *body_block;
2063 if (statement->body != NULL) {
2064 ir_node *old_continue_label = continue_label;
2065 ir_node *old_break_label = break_label;
2066 continue_label = header_block;
2067 break_label = false_block;
2069 body_block = new_immBlock();
2070 statement_to_firm(statement->body);
2072 assert(continue_label == header_block);
2073 assert(break_label == false_block);
2074 continue_label = old_continue_label;
2075 break_label = old_break_label;
2077 if(get_cur_block() != NULL) {
2079 add_immBlock_pred(header_block, jmp);
2082 body_block = header_block;
2085 /* create the condition */
2086 set_cur_block(header_block);
2088 create_condition_evaluation(statement->condition, body_block, false_block);
2089 mature_immBlock(body_block);
2090 mature_immBlock(false_block);
2091 mature_immBlock(header_block);
2093 set_cur_block(false_block);
2096 static void do_while_statement_to_firm(do_while_statement_t *statement)
2098 ir_node *jmp = NULL;
2099 if(get_cur_block() != NULL) {
2103 /* create the header block */
2104 ir_node *header_block = new_immBlock();
2106 /* the false block */
2107 ir_node *false_block = new_immBlock();
2110 ir_node *body_block = new_immBlock();
2112 add_immBlock_pred(body_block, jmp);
2115 if (statement->body != NULL) {
2116 ir_node *old_continue_label = continue_label;
2117 ir_node *old_break_label = break_label;
2118 continue_label = header_block;
2119 break_label = false_block;
2121 statement_to_firm(statement->body);
2123 assert(continue_label == header_block);
2124 assert(break_label == false_block);
2125 continue_label = old_continue_label;
2126 break_label = old_break_label;
2128 if (get_cur_block() == NULL) {
2129 mature_immBlock(header_block);
2130 mature_immBlock(body_block);
2131 mature_immBlock(false_block);
2136 ir_node *body_jmp = new_Jmp();
2137 add_immBlock_pred(header_block, body_jmp);
2138 mature_immBlock(header_block);
2140 /* create the condition */
2141 set_cur_block(header_block);
2143 create_condition_evaluation(statement->condition, body_block, false_block);
2144 mature_immBlock(body_block);
2145 mature_immBlock(false_block);
2146 mature_immBlock(header_block);
2148 set_cur_block(false_block);
2151 static void for_statement_to_firm(for_statement_t *statement)
2153 ir_node *jmp = NULL;
2154 if (get_cur_block() != NULL) {
2155 if(statement->initialisation != NULL) {
2156 expression_to_firm(statement->initialisation);
2161 /* create the step block */
2162 ir_node *const step_block = new_immBlock();
2163 if (statement->step != NULL) {
2164 expression_to_firm(statement->step);
2166 ir_node *const step_jmp = new_Jmp();
2168 /* create the header block */
2169 ir_node *const header_block = new_immBlock();
2171 add_immBlock_pred(header_block, jmp);
2173 add_immBlock_pred(header_block, step_jmp);
2175 /* the false block */
2176 ir_node *const false_block = new_immBlock();
2179 ir_node * body_block;
2180 if (statement->body != NULL) {
2181 ir_node *const old_continue_label = continue_label;
2182 ir_node *const old_break_label = break_label;
2183 continue_label = step_block;
2184 break_label = false_block;
2186 body_block = new_immBlock();
2187 statement_to_firm(statement->body);
2189 assert(continue_label == step_block);
2190 assert(break_label == false_block);
2191 continue_label = old_continue_label;
2192 break_label = old_break_label;
2194 if (get_cur_block() != NULL) {
2196 add_immBlock_pred(step_block, jmp);
2199 body_block = step_block;
2202 /* create the condition */
2203 set_cur_block(header_block);
2204 if (statement->condition != NULL) {
2205 create_condition_evaluation(statement->condition, body_block,
2208 keep_alive(header_block);
2210 add_immBlock_pred(body_block, jmp);
2213 mature_immBlock(body_block);
2214 mature_immBlock(false_block);
2215 mature_immBlock(step_block);
2216 mature_immBlock(header_block);
2217 mature_immBlock(false_block);
2219 set_cur_block(false_block);
2222 static void create_declaration_entity(declaration_t *declaration,
2223 declaration_type_t declaration_type,
2224 ir_type *parent_type)
2226 ident *id = new_id_from_str(declaration->symbol->string);
2227 ir_type *irtype = get_ir_type(declaration->type);
2228 ir_entity *entity = new_entity(parent_type, id, irtype);
2229 set_entity_ld_ident(entity, id);
2231 declaration->declaration_type = (unsigned char) declaration_type;
2232 declaration->v.entity = entity;
2233 set_entity_variability(entity, variability_uninitialized);
2234 if(parent_type == get_tls_type())
2235 set_entity_allocation(entity, allocation_automatic);
2236 else if(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE)
2237 set_entity_allocation(entity, allocation_static);
2238 /* TODO: visibility? */
2241 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2243 enum compound_graph_entry_type_t {
2244 COMPOUND_GRAPH_ENTRY_ARRAY,
2245 COMPOUND_GRAPH_ENTRY_COMPOUND
2248 struct compound_graph_path_entry_t {
2254 compound_graph_path_entry_t *prev;
2257 static void create_initializer_object(initializer_t *initializer, type_t *type,
2258 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2260 static compound_graph_path *create_compound_path(ir_type *type,
2261 compound_graph_path_entry_t *entry, int len)
2263 compound_graph_path *path = new_compound_graph_path(type, len);
2266 for( ; entry != NULL; entry = entry->prev, --i) {
2268 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2269 set_compound_graph_path_node(path, i, entry->v.entity);
2271 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2272 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2280 static void create_initializer_value(initializer_value_t *initializer,
2282 compound_graph_path_entry_t *entry,
2285 ir_node *node = expression_to_firm(initializer->value);
2286 ir_type *type = get_entity_type(entity);
2287 compound_graph_path *path = create_compound_path(type, entry, len);
2288 add_compound_ent_value_w_path(entity, node, path);
2291 static void create_initializer_compound(initializer_list_t *initializer,
2292 compound_type_t *type,
2294 compound_graph_path_entry_t *last_entry,
2297 declaration_t *compound_declaration = type->declaration;
2299 declaration_t *compound_entry = compound_declaration->context.declarations;
2301 compound_graph_path_entry_t entry;
2302 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2303 entry.prev = last_entry;
2307 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2308 if(compound_entry->symbol == NULL)
2310 if(compound_entry->namespc != NAMESPACE_NORMAL)
2313 if(i >= initializer->len)
2316 entry.v.entity = compound_entry->v.entity;
2318 initializer_t *sub_initializer = initializer->initializers[i];
2320 assert(compound_entry != NULL);
2321 assert(compound_entry->declaration_type
2322 == DECLARATION_TYPE_COMPOUND_MEMBER);
2324 if(sub_initializer->type == INITIALIZER_VALUE) {
2325 create_initializer_value(&sub_initializer->value,
2326 entity, &entry, len);
2328 type_t *entry_type = skip_typeref(compound_entry->type);
2329 create_initializer_object(sub_initializer, entry_type, entity,
2337 static void create_initializer_array(initializer_list_t *initializer,
2338 array_type_t *type, ir_entity *entity,
2339 compound_graph_path_entry_t *last_entry,
2342 type_t *element_type = type->element_type;
2343 element_type = skip_typeref(element_type);
2345 compound_graph_path_entry_t entry;
2346 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2347 entry.prev = last_entry;
2351 for(i = 0; i < initializer->len; ++i) {
2352 entry.v.array_index = i;
2354 initializer_t *sub_initializer = initializer->initializers[i];
2356 if(sub_initializer->type == INITIALIZER_VALUE) {
2357 create_initializer_value(&sub_initializer->value,
2358 entity, &entry, len);
2360 create_initializer_object(sub_initializer, element_type, entity,
2366 /* TODO: initialize rest... */
2367 if(type->size_expression != NULL) {
2368 size_t array_len = fold_constant(type->size_expression);
2369 for( ; i < array_len; ++i) {
2376 static void create_initializer_string(initializer_string_t *initializer,
2377 array_type_t *type, ir_entity *entity,
2378 compound_graph_path_entry_t *last_entry,
2381 type_t *element_type = type->element_type;
2382 element_type = skip_typeref(element_type);
2384 compound_graph_path_entry_t entry;
2385 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2386 entry.prev = last_entry;
2389 ir_type *irtype = get_entity_type(entity);
2390 size_t arr_len = get_array_type_size(type);
2391 const char *p = initializer->string;
2393 for(i = 0; i < arr_len; ++i, ++p) {
2394 entry.v.array_index = i;
2396 ir_node *node = new_Const_long(mode_Bs, *p);
2397 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2398 add_compound_ent_value_w_path(entity, node, path);
2405 static void create_initializer_wide_string(
2406 const initializer_wide_string_t *const initializer, array_type_t *const type,
2407 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2410 type_t *element_type = type->element_type;
2411 element_type = skip_typeref(element_type);
2413 compound_graph_path_entry_t entry;
2414 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2415 entry.prev = last_entry;
2418 ir_type *const irtype = get_entity_type(entity);
2419 const size_t arr_len = get_array_type_size(type);
2420 const wchar_rep_t * p = initializer->string.begin;
2421 const wchar_rep_t *const end = p + initializer->string.size;
2422 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2423 entry.v.array_index = i;
2425 ir_node *node = new_Const_long(mode_Is, *p);
2426 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2427 add_compound_ent_value_w_path(entity, node, path);
2431 static void create_initializer_object(initializer_t *initializer, type_t *type,
2432 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2434 if(is_type_array(type)) {
2435 array_type_t *array_type = &type->array;
2437 switch (initializer->type) {
2438 case INITIALIZER_STRING: {
2439 initializer_string_t *const string = &initializer->string;
2440 create_initializer_string(string, array_type, entity, entry, len);
2444 case INITIALIZER_WIDE_STRING: {
2445 initializer_wide_string_t *const string = &initializer->wide_string;
2446 create_initializer_wide_string(string, array_type, entity, entry, len);
2450 case INITIALIZER_LIST: {
2451 initializer_list_t *const list = &initializer->list;
2452 create_initializer_array(list, array_type, entity, entry, len);
2456 case INITIALIZER_VALUE:
2459 panic("Unhandled initializer");
2461 assert(initializer->type == INITIALIZER_LIST);
2462 initializer_list_t *list = &initializer->list;
2464 assert(is_type_compound(type));
2465 compound_type_t *compound_type = &type->compound;
2466 create_initializer_compound(list, compound_type, entity, entry, len);
2470 static void create_initializer_local_variable_entity(declaration_t *declaration)
2472 initializer_t *initializer = declaration->init.initializer;
2473 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2474 ir_entity *entity = declaration->v.entity;
2475 ir_node *memory = get_store();
2476 ir_node *nomem = new_NoMem();
2477 ir_node *frame = get_irg_frame(current_ir_graph);
2478 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2480 if(initializer->type == INITIALIZER_VALUE) {
2481 initializer_value_t *initializer_value = &initializer->value;
2483 ir_node *value = expression_to_firm(initializer_value->value);
2484 type_t *type = skip_typeref(declaration->type);
2485 assign_value(dbgi, addr, type, value);
2489 /* create a "template" entity which is copied to the entity on the stack */
2490 ident *id = unique_ident("initializer");
2491 ir_type *irtype = get_ir_type(declaration->type);
2492 ir_type *global_type = get_glob_type();
2493 ir_entity *init_entity = new_entity(global_type, id, irtype);
2494 set_entity_ld_ident(init_entity, id);
2496 set_entity_variability(init_entity, variability_initialized);
2497 set_entity_visibility(init_entity, visibility_local);
2498 set_entity_allocation(init_entity, allocation_static);
2500 ir_graph *old_current_ir_graph = current_ir_graph;
2501 current_ir_graph = get_const_code_irg();
2503 type_t *type = skip_typeref(declaration->type);
2504 create_initializer_object(initializer, type, init_entity, NULL, 0);
2506 assert(current_ir_graph == get_const_code_irg());
2507 current_ir_graph = old_current_ir_graph;
2509 ir_node *src_addr = create_symconst(dbgi, init_entity);
2510 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2512 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2513 set_store(copyb_mem);
2516 static void create_initializer(declaration_t *declaration)
2518 initializer_t *initializer = declaration->init.initializer;
2519 if(initializer == NULL)
2522 declaration_type_t declaration_type
2523 = (declaration_type_t) declaration->declaration_type;
2524 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2525 create_initializer_local_variable_entity(declaration);
2529 if(initializer->type == INITIALIZER_VALUE) {
2530 initializer_value_t *initializer_value = &initializer->value;
2532 ir_node *value = expression_to_firm(initializer_value->value);
2534 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2535 set_value(declaration->v.value_number, value);
2537 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2539 ir_entity *entity = declaration->v.entity;
2541 set_entity_variability(entity, variability_initialized);
2542 set_atomic_ent_value(entity, value);
2545 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2546 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2548 ir_entity *entity = declaration->v.entity;
2549 set_entity_variability(entity, variability_initialized);
2551 type_t *type = skip_typeref(declaration->type);
2552 create_initializer_object(initializer, type, entity, NULL, 0);
2556 static void create_local_variable(declaration_t *declaration)
2558 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2560 bool needs_entity = declaration->address_taken;
2561 type_t *type = skip_typeref(declaration->type);
2563 if(is_type_array(type) || is_type_compound(type)) {
2564 needs_entity = true;
2568 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2569 create_declaration_entity(declaration,
2570 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2573 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2574 declaration->v.value_number = next_value_number_function;
2575 ++next_value_number_function;
2578 create_initializer(declaration);
2581 static void create_local_static_variable(declaration_t *declaration)
2583 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2585 type_t *type = skip_typeref(declaration->type);
2586 ir_type *global_type = get_glob_type();
2587 ident *id = unique_ident(declaration->symbol->string);
2588 ir_type *irtype = get_ir_type(type);
2589 ir_entity *entity = new_entity(global_type, id, irtype);
2590 set_entity_ld_ident(entity, id);
2592 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2593 declaration->v.entity = entity;
2594 set_entity_variability(entity, variability_uninitialized);
2595 set_entity_visibility(entity, visibility_local);
2596 set_entity_allocation(entity, allocation_static);
2598 ir_graph *old_current_ir_graph = current_ir_graph;
2599 current_ir_graph = get_const_code_irg();
2601 create_initializer(declaration);
2603 assert(current_ir_graph == get_const_code_irg());
2604 current_ir_graph = old_current_ir_graph;
2607 static void declaration_statement_to_firm(declaration_statement_t *statement)
2609 declaration_t *declaration = statement->declarations_begin;
2610 declaration_t *end = statement->declarations_end->next;
2611 for( ; declaration != end; declaration = declaration->next) {
2612 type_t *type = skip_typeref(declaration->type);
2614 switch ((storage_class_tag_t) declaration->storage_class) {
2615 case STORAGE_CLASS_TYPEDEF:
2617 case STORAGE_CLASS_STATIC:
2618 create_local_static_variable(declaration);
2620 case STORAGE_CLASS_ENUM_ENTRY:
2621 panic("enum entry declaration in local block found");
2622 case STORAGE_CLASS_EXTERN:
2623 panic("extern declaration in local block found");
2624 case STORAGE_CLASS_NONE:
2625 case STORAGE_CLASS_AUTO:
2626 case STORAGE_CLASS_REGISTER:
2627 if(is_type_function(type)) {
2628 panic("nested functions not supported yet");
2630 create_local_variable(declaration);
2633 case STORAGE_CLASS_THREAD:
2634 case STORAGE_CLASS_THREAD_EXTERN:
2635 case STORAGE_CLASS_THREAD_STATIC:
2638 panic("invalid storage class found");
2642 static void create_jump_statement(const statement_t *statement,
2643 ir_node *target_block)
2645 if(get_cur_block() == NULL)
2648 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2649 ir_node *jump = new_d_Jmp(dbgi);
2650 add_immBlock_pred(target_block, jump);
2652 set_cur_block(NULL);
2655 static void switch_statement_to_firm(const switch_statement_t *statement)
2657 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2659 ir_node *expression = expression_to_firm(statement->expression);
2660 ir_node *cond = new_d_Cond(dbgi, expression);
2661 ir_node *break_block = new_immBlock();
2663 set_cur_block(NULL);
2665 ir_node *const old_switch_cond = current_switch_cond;
2666 ir_node *const old_break_label = break_label;
2667 const bool old_saw_default_label = saw_default_label;
2668 current_switch_cond = cond;
2669 break_label = break_block;
2671 statement_to_firm(statement->body);
2673 if(get_cur_block() != NULL) {
2674 ir_node *jmp = new_Jmp();
2675 add_immBlock_pred(break_block, jmp);
2678 if (!saw_default_label) {
2679 set_cur_block(get_nodes_block(cond));
2680 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2681 MAGIC_DEFAULT_PN_NUMBER);
2682 add_immBlock_pred(break_block, proj);
2685 assert(current_switch_cond == cond);
2686 assert(break_label == break_block);
2687 current_switch_cond = old_switch_cond;
2688 break_label = old_break_label;
2689 saw_default_label = old_saw_default_label;
2691 mature_immBlock(break_block);
2692 set_cur_block(break_block);
2695 static long fold_constant(const expression_t *expression)
2697 ir_graph *old_current_ir_graph = current_ir_graph;
2698 current_ir_graph = get_const_code_irg();
2700 ir_node *cnst = expression_to_firm(expression);
2701 if(!is_Const(cnst)) {
2702 panic("couldn't fold constantl");
2704 tarval *tv = get_Const_tarval(cnst);
2705 if(!tarval_is_long(tv)) {
2706 panic("folded constant not an integer");
2709 long res = get_tarval_long(tv);
2711 current_ir_graph = old_current_ir_graph;
2715 static void case_label_to_firm(const case_label_statement_t *statement)
2717 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2719 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2721 /* let's create a node and hope firm constant folding creates a Const
2724 set_cur_block(get_nodes_block(current_switch_cond));
2725 if(statement->expression) {
2726 long pn = fold_constant(statement->expression);
2727 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2728 /* oops someone detected our cheating... */
2729 panic("magic default pn used");
2731 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2733 saw_default_label = true;
2734 proj = new_d_defaultProj(dbgi, current_switch_cond,
2735 MAGIC_DEFAULT_PN_NUMBER);
2738 ir_node *block = new_immBlock();
2739 if (fallthrough != NULL) {
2740 add_immBlock_pred(block, fallthrough);
2742 add_immBlock_pred(block, proj);
2743 mature_immBlock(block);
2745 statement_to_firm(statement->label_statement);
2748 static ir_node *get_label_block(declaration_t *label)
2750 assert(label->namespc == NAMESPACE_LABEL);
2752 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2753 return label->v.block;
2755 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2757 ir_node *old_cur_block = get_cur_block();
2758 ir_node *block = new_immBlock();
2759 set_cur_block(old_cur_block);
2761 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2762 label->v.block = block;
2764 ARR_APP1(ir_node *, imature_blocks, block);
2769 static void label_to_firm(const label_statement_t *statement)
2771 ir_node *block = get_label_block(statement->label);
2773 if(get_cur_block() != NULL) {
2774 ir_node *jmp = new_Jmp();
2775 add_immBlock_pred(block, jmp);
2778 set_cur_block(block);
2781 statement_to_firm(statement->label_statement);
2784 static void goto_to_firm(const goto_statement_t *statement)
2786 if(get_cur_block() == NULL)
2789 ir_node *block = get_label_block(statement->label);
2790 ir_node *jmp = new_Jmp();
2791 add_immBlock_pred(block, jmp);
2793 set_cur_block(NULL);
2796 typedef enum modifier_t {
2797 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
2798 ASM_MODIFIER_READ_WRITE = 1 << 1,
2799 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
2800 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
2804 static void asm_statement_to_firm(const asm_statement_t *statement)
2806 bool needs_memory = false;
2808 size_t n_clobbers = 0;
2809 asm_clobber_t *clobber = statement->clobbers;
2810 for( ; clobber != NULL; clobber = clobber->next) {
2811 if(strcmp(clobber->clobber, "memory") == 0) {
2812 needs_memory = true;
2816 ident *id = new_id_from_str(clobber->clobber);
2817 obstack_ptr_grow(&asm_obst, id);
2820 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
2821 ident **clobbers = NULL;
2822 if(n_clobbers > 0) {
2823 clobbers = obstack_finish(&asm_obst);
2826 /* find and count input and output constraints */
2827 asm_constraint_t *constraint = statement->inputs;
2828 for( ; constraint != NULL; constraint = constraint->next) {
2830 bool supports_memop = false;
2831 for(const char *c = constraint->constraints; *c != 0; ++c) {
2832 /* TODO: improve error messages */
2836 panic("multiple alternative assembler constraints not "
2844 supports_memop = true;
2845 obstack_1grow(&asm_obst, *c);
2848 if(modifiers & ASM_MODIFIER_READ_WRITE)
2849 panic("inconsistent register constraints");
2850 modifiers |= ASM_MODIFIER_WRITE_ONLY;
2853 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
2854 panic("inconsistent register constraints");
2855 modifiers |= ASM_MODIFIER_READ_WRITE;
2858 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
2859 panic("early clobber assembler constraint not supported yet");
2862 modifiers |= ASM_MODIFIER_COMMUTATIVE;
2863 panic("commutative assembler constraint not supported yet");
2866 /* skip register preferences stuff... */
2867 while(*c != 0 && *c != ',')
2871 /* skip register preferences stuff... */
2875 obstack_1grow(&asm_obst, *c);
2879 obstack_1grow(&asm_obst, '\0');
2880 const char *constraint_string = obstack_finish(&asm_obst);
2882 needs_memory |= supports_memop;
2883 if(supports_memop) {
2891 static void statement_to_firm(statement_t *statement)
2893 switch(statement->type) {
2894 case STATEMENT_INVALID:
2895 panic("invalid statement found");
2896 case STATEMENT_COMPOUND:
2897 compound_statement_to_firm(&statement->compound);
2899 case STATEMENT_RETURN:
2900 return_statement_to_firm(&statement->returns);
2902 case STATEMENT_EXPRESSION:
2903 expression_statement_to_firm(&statement->expression);
2906 if_statement_to_firm(&statement->ifs);
2908 case STATEMENT_WHILE:
2909 while_statement_to_firm(&statement->whiles);
2911 case STATEMENT_DO_WHILE:
2912 do_while_statement_to_firm(&statement->do_while);
2914 case STATEMENT_DECLARATION:
2915 declaration_statement_to_firm(&statement->declaration);
2917 case STATEMENT_BREAK:
2918 create_jump_statement(statement, break_label);
2920 case STATEMENT_CONTINUE:
2921 create_jump_statement(statement, continue_label);
2923 case STATEMENT_SWITCH:
2924 switch_statement_to_firm(&statement->switchs);
2926 case STATEMENT_CASE_LABEL:
2927 case_label_to_firm(&statement->case_label);
2930 for_statement_to_firm(&statement->fors);
2932 case STATEMENT_LABEL:
2933 label_to_firm(&statement->label);
2935 case STATEMENT_GOTO:
2936 goto_to_firm(&statement->gotos);
2939 //asm_statement_to_firm(&statement->asms);
2943 panic("Statement not implemented\n");
2946 static int count_local_declarations(const declaration_t * decl,
2947 const declaration_t *const end)
2950 for (; decl != end; decl = decl->next) {
2951 const type_t *type = skip_typeref(decl->type);
2952 switch (type->type) {
2956 if (!decl->address_taken)
2966 static int count_decls_in_stmts(const statement_t *stmt)
2969 for (; stmt != NULL; stmt = stmt->base.next) {
2970 switch (stmt->type) {
2971 case STATEMENT_DECLARATION: {
2972 const declaration_statement_t *const decl_stmt =
2973 (const declaration_statement_t*)stmt;
2974 count += count_local_declarations(decl_stmt->declarations_begin,
2975 decl_stmt->declarations_end->next);
2979 case STATEMENT_COMPOUND: {
2980 const compound_statement_t *const comp =
2981 (const compound_statement_t*)stmt;
2982 count += count_decls_in_stmts(comp->statements);
2986 case STATEMENT_IF: {
2987 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2988 count += count_decls_in_stmts(if_stmt->true_statement);
2989 count += count_decls_in_stmts(if_stmt->false_statement);
2993 case STATEMENT_SWITCH: {
2994 const switch_statement_t *const switch_stmt =
2995 (const switch_statement_t*)stmt;
2996 count += count_decls_in_stmts(switch_stmt->body);
3000 case STATEMENT_LABEL: {
3001 const label_statement_t *const label_stmt =
3002 (const label_statement_t*)stmt;
3003 count += count_decls_in_stmts(label_stmt->label_statement);
3007 case STATEMENT_WHILE: {
3008 const while_statement_t *const while_stmt =
3009 (const while_statement_t*)stmt;
3010 count += count_decls_in_stmts(while_stmt->body);
3014 case STATEMENT_DO_WHILE: {
3015 const do_while_statement_t *const do_while_stmt =
3016 (const do_while_statement_t*)stmt;
3017 count += count_decls_in_stmts(do_while_stmt->body);
3021 case STATEMENT_FOR: {
3022 const for_statement_t *const for_stmt =
3023 (const for_statement_t*)stmt;
3024 /* TODO initialisation */
3025 count += count_decls_in_stmts(for_stmt->body);
3030 case STATEMENT_BREAK:
3031 case STATEMENT_CASE_LABEL:
3032 case STATEMENT_CONTINUE:
3033 case STATEMENT_EXPRESSION:
3034 case STATEMENT_GOTO:
3035 case STATEMENT_INVALID:
3036 case STATEMENT_RETURN:
3043 static int get_function_n_local_vars(declaration_t *declaration)
3047 /* count parameters */
3048 count += count_local_declarations(declaration->context.declarations, NULL);
3050 /* count local variables declared in body */
3051 count += count_decls_in_stmts(declaration->init.statement);
3053 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
3054 * for expression statements... */
3060 static void initialize_function_parameters(declaration_t *declaration)
3062 ir_graph *irg = current_ir_graph;
3063 ir_node *args = get_irg_args(irg);
3064 ir_node *start_block = get_irg_start_block(irg);
3065 ir_type *function_irtype = get_ir_type(declaration->type);
3068 declaration_t *parameter = declaration->context.declarations;
3069 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3070 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
3071 type_t *type = skip_typeref(parameter->type);
3073 bool needs_entity = parameter->address_taken;
3074 assert(!is_type_array(type));
3075 if(is_type_compound(type)) {
3076 needs_entity = true;
3080 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3081 ident *id = new_id_from_str(parameter->symbol->string);
3082 set_entity_ident(entity, id);
3084 parameter->declaration_type
3085 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
3086 parameter->v.entity = entity;
3090 ir_mode *mode = get_ir_mode(parameter->type);
3092 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3094 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
3095 parameter->v.value_number = next_value_number_function;
3096 ++next_value_number_function;
3098 set_value(parameter->v.value_number, proj);
3102 static void create_function(declaration_t *declaration)
3104 ir_entity *function_entity = get_function_entity(declaration);
3106 if(declaration->init.statement == NULL)
3109 current_function_decl = declaration;
3110 current_function_name = NULL;
3112 assert(imature_blocks == NULL);
3113 imature_blocks = NEW_ARR_F(ir_node*, 0);
3115 int n_local_vars = get_function_n_local_vars(declaration);
3116 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3117 ir_node *first_block = get_cur_block();
3119 next_value_number_function = 0;
3120 initialize_function_parameters(declaration);
3122 statement_to_firm(declaration->init.statement);
3124 ir_node *end_block = get_irg_end_block(irg);
3126 /* do we have a return statement yet? */
3127 if(get_cur_block() != NULL) {
3128 type_t *type = skip_typeref(declaration->type);
3129 assert(is_type_function(type));
3130 const function_type_t *func_type = &type->function;
3131 const type_t *return_type
3132 = skip_typeref(func_type->return_type);
3135 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3136 ret = new_Return(get_store(), 0, NULL);
3139 if(is_type_scalar(return_type)) {
3140 mode = get_ir_mode(func_type->return_type);
3146 // ยง5.1.2.2.3 main implicitly returns 0
3147 if (strcmp(declaration->symbol->string, "main") == 0) {
3148 in[0] = new_Const(mode, get_mode_null(mode));
3150 in[0] = new_Unknown(mode);
3152 ret = new_Return(get_store(), 1, in);
3154 add_immBlock_pred(end_block, ret);
3157 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3158 mature_immBlock(imature_blocks[i]);
3160 DEL_ARR_F(imature_blocks);
3161 imature_blocks = NULL;
3163 mature_immBlock(first_block);
3164 mature_immBlock(end_block);
3166 irg_finalize_cons(irg);
3168 /* finalize the frame type */
3169 ir_type *frame_type = get_irg_frame_type(irg);
3170 int n = get_compound_n_members(frame_type);
3173 for(int i = 0; i < n; ++i) {
3174 ir_entity *entity = get_compound_member(frame_type, i);
3175 ir_type *entity_type = get_entity_type(entity);
3177 int align = get_type_alignment_bytes(entity_type);
3178 if(align > align_all)
3182 misalign = offset % align;
3184 offset += align - misalign;
3188 set_entity_offset(entity, offset);
3189 offset += get_type_size_bytes(entity_type);
3191 set_type_size_bytes(frame_type, offset);
3192 set_type_alignment_bytes(frame_type, align_all);
3193 set_type_state(frame_type, layout_fixed);
3198 static void create_global_variable(declaration_t *declaration)
3202 switch ((storage_class_tag_t)declaration->storage_class) {
3203 case STORAGE_CLASS_STATIC:
3204 vis = visibility_local;
3207 case STORAGE_CLASS_EXTERN:
3208 vis = visibility_external_allocated;
3211 case STORAGE_CLASS_NONE:
3212 vis = visibility_external_visible;
3215 case STORAGE_CLASS_THREAD:
3216 vis = visibility_external_visible;
3219 case STORAGE_CLASS_THREAD_EXTERN:
3220 vis = visibility_external_allocated;
3223 case STORAGE_CLASS_THREAD_STATIC:
3224 vis = visibility_local;
3228 var_type = get_tls_type();
3232 var_type = get_glob_type();
3236 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3238 set_entity_visibility(declaration->v.entity, vis);
3240 current_ir_graph = get_const_code_irg();
3241 create_initializer(declaration);
3244 case STORAGE_CLASS_TYPEDEF:
3245 case STORAGE_CLASS_AUTO:
3246 case STORAGE_CLASS_REGISTER:
3247 case STORAGE_CLASS_ENUM_ENTRY:
3250 panic("Invalid storage class for global variable");
3253 static void context_to_firm(context_t *context)
3255 /* first pass: create declarations */
3256 declaration_t *declaration = context->declarations;
3257 for( ; declaration != NULL; declaration = declaration->next) {
3258 if(declaration->namespc != NAMESPACE_NORMAL)
3260 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3261 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3263 if(declaration->symbol == NULL)
3266 type_t *type = skip_typeref(declaration->type);
3267 if(is_type_function(type)) {
3268 get_function_entity(declaration);
3270 create_global_variable(declaration);
3274 /* second pass: create code */
3275 declaration = context->declarations;
3276 for( ; declaration != NULL; declaration = declaration->next) {
3277 if(declaration->namespc != NAMESPACE_NORMAL)
3279 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3280 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3282 if(declaration->symbol == NULL)
3285 type_t *type = declaration->type;
3286 if(type->type != TYPE_FUNCTION)
3289 create_function(declaration);
3293 void translation_unit_to_firm(translation_unit_t *unit)
3295 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3296 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3297 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3299 ir_type_int = get_ir_type(type_int);
3300 ir_type_const_char = get_ir_type(type_const_char);
3301 ir_type_wchar_t = get_ir_type(type_wchar_t);
3302 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3305 type_void->base.firm_type = ir_type_void;
3307 /* just to be sure */
3308 continue_label = NULL;
3310 current_switch_cond = NULL;
3312 context_to_firm(& unit->context);