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 *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 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);
568 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
569 declaration->v.entity = entity;
574 static dbg_info *get_dbg_info(const source_position_t *pos)
576 return (dbg_info*) pos;
579 static ir_node *const_to_firm(const const_expression_t *cnst)
581 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
582 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
587 if(mode_is_float(mode)) {
588 tv = new_tarval_from_double(cnst->v.float_value, mode);
590 if(mode_is_signed(mode)) {
591 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
593 len = snprintf(buf, sizeof(buf), "%llu",
594 (unsigned long long) cnst->v.int_value);
596 tv = new_tarval_from_str(buf, len, mode);
599 return new_d_Const(dbgi, mode, tv);
602 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
604 assert(entity != NULL);
605 union symconst_symbol sym;
606 sym.entity_p = entity;
607 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
610 static ir_node *string_to_firm(const source_position_t *const src_pos,
611 const char *const id_prefix,
612 const char *const string)
614 ir_type *const global_type = get_glob_type();
615 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
618 ident *const id = unique_ident(id_prefix);
619 ir_entity *const entity = new_entity(global_type, id, type);
620 set_entity_ld_ident(entity, id);
621 set_entity_variability(entity, variability_constant);
623 ir_type *const elem_type = ir_type_const_char;
624 ir_mode *const mode = get_type_mode(elem_type);
626 const size_t slen = strlen(string) + 1;
628 set_array_lower_bound_int(type, 0, 0);
629 set_array_upper_bound_int(type, 0, slen);
630 set_type_size_bytes(type, slen);
631 set_type_state(type, layout_fixed);
633 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
634 for(size_t i = 0; i < slen; ++i) {
635 tvs[i] = new_tarval_from_long(string[i], mode);
638 set_array_entity_values(entity, tvs, slen);
641 dbg_info *const dbgi = get_dbg_info(src_pos);
643 return create_symconst(dbgi, entity);
646 static ir_node *string_literal_to_firm(
647 const string_literal_expression_t* literal)
649 return string_to_firm(&literal->expression.source_position, "Lstr",
653 static ir_node *wide_string_literal_to_firm(
654 const wide_string_literal_expression_t* const literal)
656 ir_type *const global_type = get_glob_type();
657 ir_type *const elem_type = ir_type_wchar_t;
658 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
661 ident *const id = unique_ident("Lstr");
662 ir_entity *const entity = new_entity(global_type, id, type);
663 set_entity_ld_ident(entity, id);
664 set_entity_variability(entity, variability_constant);
666 ir_mode *const mode = get_type_mode(elem_type);
668 const wchar_rep_t *const string = literal->value.begin;
669 const size_t slen = literal->value.size;
671 set_array_lower_bound_int(type, 0, 0);
672 set_array_upper_bound_int(type, 0, slen);
673 set_type_size_bytes(type, slen);
674 set_type_state(type, layout_fixed);
676 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
677 for(size_t i = 0; i < slen; ++i) {
678 tvs[i] = new_tarval_from_long(string[i], mode);
681 set_array_entity_values(entity, tvs, slen);
684 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
686 return create_symconst(dbgi, entity);
689 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
690 dbg_info *const dbgi)
692 if(is_compound_type(irtype) || is_Array_type(irtype)) {
696 ir_mode *const mode = get_type_mode(irtype);
697 ir_node *const memory = get_store();
698 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
699 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
700 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
705 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
707 ir_mode *mode = get_irn_mode(node);
709 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
711 if(!mode_is_float(mode))
714 /* check if there is already a Conv */
715 if (get_irn_op(node) == op_Conv) {
716 /* convert it into a strict Conv */
717 set_Conv_strict(node, 1);
721 /* otherwise create a new one */
722 return new_d_strictConv(dbgi, node, mode);
725 static ir_node *get_global_var_address(dbg_info *const dbgi,
726 const declaration_t *const decl)
728 assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
730 ir_entity *const entity = decl->v.entity;
731 switch ((storage_class_tag_t)decl->storage_class) {
732 case STORAGE_CLASS_THREAD:
733 case STORAGE_CLASS_THREAD_EXTERN:
734 case STORAGE_CLASS_THREAD_STATIC: {
735 ir_node *const no_mem = new_NoMem();
736 ir_node *const tls = get_irg_tls(current_ir_graph);
737 return new_d_simpleSel(dbgi, no_mem, tls, entity);
741 return create_symconst(dbgi, entity);
745 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
747 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
748 declaration_t *declaration = ref->declaration;
749 type_t *type = skip_typeref(declaration->type);
751 switch((declaration_type_t) declaration->declaration_type) {
752 case DECLARATION_TYPE_UNKNOWN:
753 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
759 case DECLARATION_TYPE_ENUM_ENTRY: {
760 ir_mode *const mode = get_ir_mode(type);
761 return new_Const(mode, declaration->v.enum_val);
764 case DECLARATION_TYPE_LOCAL_VARIABLE: {
765 ir_mode *mode = get_ir_mode(type);
766 return get_value(declaration->v.value_number, mode);
768 case DECLARATION_TYPE_FUNCTION: {
769 return create_symconst(dbgi, declaration->v.entity);
771 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
772 ir_node *const addr = get_global_var_address(dbgi, declaration);
773 ir_type *const irtype = get_entity_type(declaration->v.entity);
774 return deref_address(irtype, addr, dbgi);
777 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
778 ir_entity *entity = declaration->v.entity;
779 ir_node *frame = get_irg_frame(current_ir_graph);
780 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
781 ir_type *irtype = get_entity_type(entity);
782 return deref_address(irtype, sel, dbgi);
785 case DECLARATION_TYPE_COMPOUND_MEMBER:
786 case DECLARATION_TYPE_LABEL_BLOCK:
787 panic("not implemented reference type");
790 panic("reference to declaration with unknown type found");
793 static ir_node *reference_addr(const reference_expression_t *ref)
795 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
796 declaration_t *declaration = ref->declaration;
798 switch((declaration_type_t) declaration->declaration_type) {
799 case DECLARATION_TYPE_UNKNOWN:
801 case DECLARATION_TYPE_LOCAL_VARIABLE:
802 panic("local variable without entity has no address");
803 case DECLARATION_TYPE_FUNCTION: {
804 return create_symconst(dbgi, declaration->v.entity);
806 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
807 ir_node *const addr = get_global_var_address(dbgi, declaration);
810 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
811 ir_entity *entity = declaration->v.entity;
812 ir_node *frame = get_irg_frame(current_ir_graph);
813 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
818 case DECLARATION_TYPE_ENUM_ENTRY:
819 panic("trying to reference enum entry");
821 case DECLARATION_TYPE_COMPOUND_MEMBER:
822 case DECLARATION_TYPE_LABEL_BLOCK:
823 panic("not implemented reference type");
826 panic("reference to declaration with unknown type found");
829 static ir_node *process_builtin_call(const call_expression_t *call)
831 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
833 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
834 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
836 type_t *type = skip_typeref(builtin->expression.datatype);
837 assert(is_type_pointer(type));
839 type_t *function_type = skip_typeref(type->pointer.points_to);
840 symbol_t *symbol = builtin->symbol;
843 case T___builtin_alloca: {
844 if(call->arguments == NULL || call->arguments->next != NULL) {
845 panic("invalid number of parameters on __builtin_alloca");
847 expression_t *argument = call->arguments->expression;
848 ir_node *size = expression_to_firm(argument);
850 ir_node *store = get_store();
851 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
853 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
855 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
859 case T___builtin_nan:
860 case T___builtin_nanf:
861 case T___builtin_nand: {
862 /* Ignore string for now... */
863 assert(is_type_function(function_type));
864 ir_mode *mode = get_ir_mode(function_type->function.return_type);
865 tarval *tv = get_mode_NAN(mode);
866 ir_node *res = new_d_Const(dbgi, mode, tv);
869 case T___builtin_va_end:
872 panic("Unsupported builtin found\n");
876 static ir_node *call_expression_to_firm(const call_expression_t *call)
878 assert(get_cur_block() != NULL);
880 expression_t *function = call->function;
881 if(function->type == EXPR_BUILTIN_SYMBOL) {
882 return process_builtin_call(call);
884 ir_node *callee = expression_to_firm(function);
886 type_t *type = skip_typeref(function->base.datatype);
887 assert(is_type_pointer(type));
888 pointer_type_t *pointer_type = &type->pointer;
889 type_t *points_to = skip_typeref(pointer_type->points_to);
890 assert(is_type_function(points_to));
891 function_type_t *function_type = &points_to->function;
893 int n_parameters = 0;
894 call_argument_t *argument = call->arguments;
895 for( ; argument != NULL; argument = argument->next) {
899 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
901 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
902 ir_type *new_method_type = NULL;
903 if(function_type->variadic || function_type->unspecified_parameters) {
904 /* we need to construct a new method type matching the call
906 int n_res = get_method_n_ress(ir_method_type);
907 new_method_type = new_type_method(unique_ident("calltype"),
908 n_parameters, n_res);
909 set_method_calling_convention(new_method_type,
910 get_method_calling_convention(ir_method_type));
911 set_method_additional_properties(new_method_type,
912 get_method_additional_properties(ir_method_type));
914 for(int i = 0; i < n_res; ++i) {
915 set_method_res_type(new_method_type, i,
916 get_method_res_type(ir_method_type, i));
919 ir_node *in[n_parameters];
921 argument = call->arguments;
923 for( ; argument != NULL; argument = argument->next) {
924 expression_t *expression = argument->expression;
925 ir_node *arg_node = expression_to_firm(expression);
927 arg_node = do_strict_conv(dbgi, arg_node);
930 if(new_method_type != NULL) {
931 ir_type *irtype = get_ir_type(expression->base.datatype);
932 set_method_param_type(new_method_type, n, irtype);
937 assert(n == n_parameters);
939 if(new_method_type != NULL)
940 ir_method_type = new_method_type;
942 ir_node *store = get_store();
943 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
945 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
948 type_t *return_type = skip_typeref(function_type->return_type);
949 ir_node *result = NULL;
951 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
953 if(is_type_scalar(return_type)) {
954 mode = get_ir_mode(return_type);
958 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
959 result = new_d_Proj(dbgi, resproj, mode, 0);
965 static void statement_to_firm(statement_t *statement);
966 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
968 static ir_node *expression_to_addr(const expression_t *expression);
969 static void create_condition_evaluation(const expression_t *expression,
971 ir_node *false_block);
973 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
976 value = do_strict_conv(dbgi, value);
978 ir_node *memory = get_store();
980 if(is_type_scalar(type)) {
981 ir_node *store = new_d_Store(dbgi, memory, addr, value);
982 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
983 set_store(store_mem);
985 ir_type *irtype = get_ir_type(type);
986 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
987 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
988 set_store(copyb_mem);
992 static void set_value_for_expression(const expression_t *expression,
995 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
996 value = do_strict_conv(dbgi, value);
998 if(expression->type == EXPR_REFERENCE) {
999 reference_expression_t *ref = (reference_expression_t*) expression;
1001 declaration_t *declaration = ref->declaration;
1002 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
1003 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1004 set_value(declaration->v.value_number, value);
1009 ir_node *addr = expression_to_addr(expression);
1010 type_t *type = skip_typeref(expression->base.datatype);
1011 assign_value(dbgi, addr, type, value);
1014 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1016 ir_mode *value_mode = get_irn_mode(value);
1018 if (value_mode == dest_mode || is_Bad(value))
1021 if(dest_mode == mode_b) {
1022 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1023 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1024 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1028 return new_d_Conv(dbgi, value, dest_mode);
1031 static ir_node *create_incdec(const unary_expression_t *expression)
1033 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1034 type_t *type = skip_typeref(expression->expression.datatype);
1035 ir_mode *mode = get_ir_mode(type);
1036 expression_t *value = expression->value;
1038 ir_node *value_node = expression_to_firm(value);
1041 if(is_type_pointer(type)) {
1042 pointer_type_t *pointer_type = &type->pointer;
1043 unsigned elem_size = get_type_size(pointer_type->points_to);
1044 offset = new_Const_long(mode_Is, elem_size);
1046 assert(is_type_arithmetic(type));
1047 offset = new_Const(mode, get_mode_one(mode));
1050 switch(expression->expression.type) {
1051 case EXPR_UNARY_POSTFIX_INCREMENT: {
1052 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1053 set_value_for_expression(value, new_value);
1056 case EXPR_UNARY_POSTFIX_DECREMENT: {
1057 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1058 set_value_for_expression(value, new_value);
1061 case EXPR_UNARY_PREFIX_INCREMENT: {
1062 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1063 set_value_for_expression(value, new_value);
1066 case EXPR_UNARY_PREFIX_DECREMENT: {
1067 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1068 set_value_for_expression(value, new_value);
1072 panic("no incdec expr in create_incdec");
1077 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1079 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1080 type_t *type = skip_typeref(expression->expression.datatype);
1081 ir_mode *mode = get_ir_mode(type);
1083 if(expression->expression.type == EXPR_UNARY_TAKE_ADDRESS)
1084 return expression_to_addr(expression->value);
1086 const expression_t *value = expression->value;
1087 ir_node *value_node = expression_to_firm(value);
1089 switch(expression->expression.type) {
1090 case EXPR_UNARY_NEGATE:
1091 return new_d_Minus(dbgi, value_node, mode);
1092 case EXPR_UNARY_PLUS:
1094 case EXPR_UNARY_BITWISE_NEGATE:
1095 return new_d_Not(dbgi, value_node, mode);
1096 case EXPR_UNARY_NOT: {
1097 if(get_irn_mode(value_node) != mode_b) {
1098 value_node = create_conv(dbgi, value_node, mode_b);
1100 value_node = new_d_Not(dbgi, value_node, mode_b);
1101 if(mode != mode_b) {
1102 value_node = create_conv(dbgi, value_node, mode);
1106 case EXPR_UNARY_DEREFERENCE: {
1107 type_t *value_type = skip_typeref(value->base.datatype);
1108 ir_type *irtype = get_ir_type(value_type);
1109 assert(is_Pointer_type(irtype));
1110 ir_type *points_to = get_pointer_points_to_type(irtype);
1111 return deref_address(points_to, value_node, dbgi);
1113 case EXPR_UNARY_POSTFIX_INCREMENT:
1114 case EXPR_UNARY_POSTFIX_DECREMENT:
1115 case EXPR_UNARY_PREFIX_INCREMENT:
1116 case EXPR_UNARY_PREFIX_DECREMENT:
1117 return create_incdec(expression);
1118 case EXPR_UNARY_CAST: {
1119 ir_node *node = create_conv(dbgi, value_node, get_ir_mode(type));
1120 node = do_strict_conv(dbgi, node);
1123 case EXPR_UNARY_CAST_IMPLICIT:
1124 return create_conv(dbgi, value_node, get_ir_mode(type));
1129 panic("invalid UNEXPR type found");
1132 static long get_pnc(const expression_type_t type)
1135 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1136 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1137 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Ne;
1138 case EXPR_BINARY_ISLESS:
1139 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1140 case EXPR_BINARY_ISLESSEQUAL:
1141 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1142 case EXPR_BINARY_ISGREATER:
1143 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1144 case EXPR_BINARY_ISGREATEREQUAL:
1145 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1146 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1151 panic("trying to get pn_Cmp from non-comparison binexpr type");
1154 static ir_node *create_lazy_op(const binary_expression_t *expression)
1156 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1157 type_t *type = expression->expression.datatype;
1158 ir_mode *mode = get_ir_mode(type);
1160 ir_node *cur_block = get_cur_block();
1162 ir_node *one_block = new_immBlock();
1163 ir_node *one = new_Const(mode, get_mode_one(mode));
1164 ir_node *jmp_one = new_d_Jmp(dbgi);
1166 ir_node *zero_block = new_immBlock();
1167 ir_node *zero = new_Const(mode, get_mode_null(mode));
1168 ir_node *jmp_zero = new_d_Jmp(dbgi);
1170 set_cur_block(cur_block);
1171 create_condition_evaluation((const expression_t*) expression,
1172 one_block, zero_block);
1173 mature_immBlock(one_block);
1174 mature_immBlock(zero_block);
1176 ir_node *common_block = new_immBlock();
1177 add_immBlock_pred(common_block, jmp_one);
1178 add_immBlock_pred(common_block, jmp_zero);
1179 mature_immBlock(common_block);
1181 ir_node *in[2] = { one, zero };
1182 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1187 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1188 ir_node *right, ir_mode *mode);
1190 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1191 create_arithmetic_func func)
1193 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1194 ir_node *left = expression_to_firm(expression->left);
1195 ir_node *right = expression_to_firm(expression->right);
1196 type_t *type = expression->right->base.datatype;
1197 /* be careful with the modes, because in arithmetic assign nodes only
1198 * the right operand has the mode of the arithmetic already */
1199 ir_mode *mode = get_ir_mode(type);
1200 left = create_conv(dbgi, left, mode);
1201 ir_node *res = func(dbgi, left, right, mode);
1206 static ir_node *pointer_arithmetic(ir_node *const pointer,
1209 dbg_info *const dbgi,
1210 const create_arithmetic_func func)
1212 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1213 type_t *const points_to = pointer_type->points_to;
1214 const unsigned elem_size = get_type_size(points_to);
1216 assert(elem_size >= 1);
1217 if (elem_size > 1) {
1218 integer = create_conv(dbgi, integer, mode_Is);
1219 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1220 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1224 ir_mode *const mode = get_ir_mode(type);
1225 return func(dbgi, pointer, integer, mode);
1228 static ir_node *create_arithmetic_assign_binop(
1229 const binary_expression_t *expression, create_arithmetic_func func)
1231 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1232 type_t *const type = skip_typeref(expression->expression.datatype);
1235 if (is_type_pointer(type)) {
1236 ir_node *const pointer = expression_to_firm(expression->left);
1237 ir_node * integer = expression_to_firm(expression->right);
1238 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1240 value = create_arithmetic_binop(expression, func);
1243 ir_mode *const mode = get_ir_mode(type);
1244 value = create_conv(dbgi, value, mode);
1245 set_value_for_expression(expression->left, value);
1250 static ir_node *create_add(const binary_expression_t *expression)
1252 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1253 ir_node *left = expression_to_firm(expression->left);
1254 ir_node *right = expression_to_firm(expression->right);
1255 type_t *type = expression->expression.datatype;
1257 expression_t *expr_left = expression->left;
1258 expression_t *expr_right = expression->right;
1259 type_t *type_left = skip_typeref(expr_left->base.datatype);
1260 type_t *type_right = skip_typeref(expr_right->base.datatype);
1262 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1263 ir_mode *const mode = get_ir_mode(type);
1264 return new_d_Add(dbgi, left, right, mode);
1267 if (is_type_pointer(type_left)) {
1268 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1270 assert(is_type_pointer(type_right));
1271 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1275 static ir_node *create_sub(const binary_expression_t *expression)
1277 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1278 expression_t *const expr_left = expression->left;
1279 expression_t *const expr_right = expression->right;
1280 ir_node *const left = expression_to_firm(expr_left);
1281 ir_node *const right = expression_to_firm(expr_right);
1282 type_t *const type = expression->expression.datatype;
1283 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1284 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1286 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1287 ir_mode *const mode = get_ir_mode(type);
1288 return new_d_Sub(dbgi, left, right, mode);
1289 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1290 const pointer_type_t *const ptr_type = &type_left->pointer;
1291 const unsigned elem_size = get_type_size(ptr_type->points_to);
1292 ir_mode *const mode = get_ir_mode(type);
1293 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1294 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1295 ir_node *const no_mem = new_NoMem();
1296 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1297 op_pin_state_floats);
1298 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1301 assert(is_type_pointer(type_left));
1302 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1305 static ir_node *create_shift(const binary_expression_t *expression)
1307 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1308 ir_node *left = expression_to_firm(expression->left);
1309 ir_node *right = expression_to_firm(expression->right);
1310 type_t *type = expression->expression.datatype;
1311 ir_mode *mode = get_ir_mode(type);
1313 /* firm always wants the shift count to be unsigned */
1314 right = create_conv(dbgi, right, mode_Iu);
1318 switch(expression->expression.type) {
1319 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1320 case EXPR_BINARY_SHIFTLEFT:
1321 res = new_d_Shl(dbgi, left, right, mode);
1323 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1324 case EXPR_BINARY_SHIFTRIGHT: {
1325 expression_t *expr_left = expression->left;
1326 type_t *type_left = skip_typeref(expr_left->base.datatype);
1328 if(is_type_signed(type_left)) {
1329 res = new_d_Shrs(dbgi, left, right, mode);
1331 res = new_d_Shr(dbgi, left, right, mode);
1336 panic("create shift op called for non-shift op");
1343 static ir_node *create_divmod(const binary_expression_t *expression)
1345 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1346 ir_node *left = expression_to_firm(expression->left);
1347 ir_node *right = expression_to_firm(expression->right);
1348 ir_node *pin = new_Pin(new_NoMem());
1349 /* be careful with the modes, because in arithmetic assign nodes only
1350 * the right operand has the mode of the arithmetic already */
1351 type_t *type = expression->right->base.datatype;
1352 ir_mode *mode = get_ir_mode(type);
1353 left = create_conv(dbgi, left, mode);
1357 switch (expression->expression.type) {
1358 case EXPR_BINARY_DIV:
1359 case EXPR_BINARY_DIV_ASSIGN:
1360 if(mode_is_float(mode)) {
1361 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1362 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1364 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1365 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1369 case EXPR_BINARY_MOD:
1370 case EXPR_BINARY_MOD_ASSIGN:
1371 assert(!mode_is_float(mode));
1372 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1373 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1376 default: panic("unexpected binary expression type in create_divmod()");
1382 static ir_node *create_arithmetic_assign_divmod(
1383 const binary_expression_t *expression)
1385 ir_node * value = create_divmod(expression);
1386 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1387 type_t *const type = expression->expression.datatype;
1388 ir_mode *const mode = get_ir_mode(type);
1390 assert(type->type != TYPE_POINTER);
1392 value = create_conv(dbgi, value, mode);
1393 set_value_for_expression(expression->left, value);
1398 static ir_node *create_arithmetic_assign_shift(
1399 const binary_expression_t *expression)
1401 ir_node * value = create_shift(expression);
1402 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1403 type_t *const type = expression->expression.datatype;
1404 ir_mode *const mode = get_ir_mode(type);
1406 value = create_conv(dbgi, value, mode);
1407 set_value_for_expression(expression->left, value);
1412 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1414 expression_type_t type = expression->expression.type;
1417 case EXPR_BINARY_EQUAL:
1418 case EXPR_BINARY_NOTEQUAL:
1419 case EXPR_BINARY_LESS:
1420 case EXPR_BINARY_LESSEQUAL:
1421 case EXPR_BINARY_GREATER:
1422 case EXPR_BINARY_GREATEREQUAL:
1423 case EXPR_BINARY_ISGREATER:
1424 case EXPR_BINARY_ISGREATEREQUAL:
1425 case EXPR_BINARY_ISLESS:
1426 case EXPR_BINARY_ISLESSEQUAL:
1427 case EXPR_BINARY_ISLESSGREATER:
1428 case EXPR_BINARY_ISUNORDERED: {
1429 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1430 ir_node *left = expression_to_firm(expression->left);
1431 ir_node *right = expression_to_firm(expression->right);
1432 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1433 long pnc = get_pnc(type);
1434 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1437 case EXPR_BINARY_ASSIGN: {
1438 ir_node *right = expression_to_firm(expression->right);
1439 set_value_for_expression(expression->left, right);
1443 case EXPR_BINARY_ADD:
1444 return create_add(expression);
1445 case EXPR_BINARY_SUB:
1446 return create_sub(expression);
1447 case EXPR_BINARY_MUL:
1448 return create_arithmetic_binop(expression, new_d_Mul);
1449 case EXPR_BINARY_BITWISE_AND:
1450 return create_arithmetic_binop(expression, new_d_And);
1451 case EXPR_BINARY_BITWISE_OR:
1452 return create_arithmetic_binop(expression, new_d_Or);
1453 case EXPR_BINARY_BITWISE_XOR:
1454 return create_arithmetic_binop(expression, new_d_Eor);
1455 case EXPR_BINARY_SHIFTLEFT:
1456 case EXPR_BINARY_SHIFTRIGHT:
1457 return create_shift(expression);
1458 case EXPR_BINARY_DIV:
1459 case EXPR_BINARY_MOD:
1460 return create_divmod(expression);
1461 case EXPR_BINARY_LOGICAL_AND:
1462 case EXPR_BINARY_LOGICAL_OR:
1463 return create_lazy_op(expression);
1464 case EXPR_BINARY_COMMA:
1465 expression_to_firm(expression->left);
1466 return expression_to_firm(expression->right);
1467 case EXPR_BINARY_ADD_ASSIGN:
1468 return create_arithmetic_assign_binop(expression, new_d_Add);
1469 case EXPR_BINARY_SUB_ASSIGN:
1470 return create_arithmetic_assign_binop(expression, new_d_Sub);
1471 case EXPR_BINARY_MUL_ASSIGN:
1472 return create_arithmetic_assign_binop(expression, new_d_Mul);
1473 case EXPR_BINARY_DIV_ASSIGN:
1474 return create_arithmetic_assign_divmod(expression);
1475 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1476 return create_arithmetic_assign_binop(expression, new_d_And);
1477 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1478 return create_arithmetic_assign_binop(expression, new_d_Or);
1479 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1480 return create_arithmetic_assign_binop(expression, new_d_Eor);
1481 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1482 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1483 return create_arithmetic_assign_shift(expression);
1485 panic("TODO binexpr type");
1489 static ir_node *array_access_addr(const array_access_expression_t *expression)
1491 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1492 ir_node *base_addr = expression_to_firm(expression->array_ref);
1493 ir_node *offset = expression_to_firm(expression->index);
1494 offset = create_conv(dbgi, offset, mode_Iu);
1496 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1497 assert(is_type_pointer(ref_type));
1498 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1500 unsigned elem_size = get_type_size(pointer_type->points_to);
1501 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1502 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1504 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1509 static ir_node *array_access_to_firm(
1510 const array_access_expression_t *expression)
1512 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1513 ir_node *addr = array_access_addr(expression);
1514 type_t *type = revert_automatic_type_conversion(
1515 (const expression_t*) expression);
1516 type = skip_typeref(type);
1517 ir_type *irtype = get_ir_type(type);
1519 return deref_address(irtype, addr, dbgi);
1522 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1524 type_t *type = expression->type;
1526 type = expression->size_expression->base.datatype;
1527 assert(type != NULL);
1530 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1531 unsigned size = get_type_size(type);
1532 ir_node *size_node = new_Const_long(mode, size);
1537 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1539 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1541 ir_node *cur_block = get_cur_block();
1543 /* create the true block */
1544 ir_node *true_block = new_immBlock();
1546 ir_node *true_val = expression_to_firm(expression->true_expression);
1547 ir_node *true_jmp = new_Jmp();
1549 /* create the false block */
1550 ir_node *false_block = new_immBlock();
1552 ir_node *false_val = expression_to_firm(expression->false_expression);
1553 ir_node *false_jmp = new_Jmp();
1555 /* create the condition evaluation */
1556 set_cur_block(cur_block);
1557 create_condition_evaluation(expression->condition, true_block, false_block);
1558 mature_immBlock(true_block);
1559 mature_immBlock(false_block);
1561 /* create the common block */
1562 ir_node *common_block = new_immBlock();
1563 add_immBlock_pred(common_block, true_jmp);
1564 add_immBlock_pred(common_block, false_jmp);
1565 mature_immBlock(common_block);
1567 /* TODO improve static semantics, so either both or no values are NULL */
1568 if (true_val == NULL || false_val == NULL)
1571 ir_node *in[2] = { true_val, false_val };
1572 ir_mode *mode = get_irn_mode(true_val);
1573 assert(get_irn_mode(false_val) == mode);
1574 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1579 static ir_node *select_addr(const select_expression_t *expression)
1581 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1583 ir_node *compound_addr = expression_to_firm(expression->compound);
1585 declaration_t *entry = expression->compound_entry;
1586 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1587 ir_entity *entity = entry->v.entity;
1589 assert(entity != NULL);
1591 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1596 static ir_node *select_to_firm(const select_expression_t *expression)
1598 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1599 ir_node *addr = select_addr(expression);
1600 type_t *type = revert_automatic_type_conversion(
1601 (const expression_t*) expression);
1602 type = skip_typeref(type);
1603 ir_type *irtype = get_ir_type(type);
1605 return deref_address(irtype, addr, dbgi);
1608 /* Values returned by __builtin_classify_type. */
1609 typedef enum gcc_type_class
1615 enumeral_type_class,
1618 reference_type_class,
1622 function_type_class,
1633 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1635 const type_t *const type = expr->type_expression->base.datatype;
1641 const atomic_type_t *const atomic_type = &type->atomic;
1642 switch (atomic_type->atype) {
1643 // should not be reached
1644 case ATOMIC_TYPE_INVALID:
1648 // gcc cannot do that
1649 case ATOMIC_TYPE_VOID:
1650 tc = void_type_class;
1653 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1654 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1655 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1656 case ATOMIC_TYPE_SHORT:
1657 case ATOMIC_TYPE_USHORT:
1658 case ATOMIC_TYPE_INT:
1659 case ATOMIC_TYPE_UINT:
1660 case ATOMIC_TYPE_LONG:
1661 case ATOMIC_TYPE_ULONG:
1662 case ATOMIC_TYPE_LONGLONG:
1663 case ATOMIC_TYPE_ULONGLONG:
1664 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1665 tc = integer_type_class;
1668 case ATOMIC_TYPE_FLOAT:
1669 case ATOMIC_TYPE_DOUBLE:
1670 case ATOMIC_TYPE_LONG_DOUBLE:
1671 tc = real_type_class;
1674 #ifdef PROVIDE_COMPLEX
1675 case ATOMIC_TYPE_FLOAT_COMPLEX:
1676 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1677 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1678 tc = complex_type_class;
1680 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1681 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1682 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1683 tc = complex_type_class;
1688 panic("Unimplemented case in classify_type_to_firm().");
1693 case TYPE_ARRAY: // gcc handles this as pointer
1694 case TYPE_FUNCTION: // gcc handles this as pointer
1695 case TYPE_POINTER: tc = pointer_type_class; break;
1696 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1697 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1699 // gcc handles this as integer
1700 case TYPE_ENUM: tc = integer_type_class; break;
1703 panic("Unimplemented case in classify_type_to_firm().");
1706 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1707 ir_mode *const mode = mode_Is;
1708 tarval *const tv = new_tarval_from_long(tc, mode);
1709 return new_d_Const(dbgi, mode, tv);
1712 static ir_node *function_name_to_firm(
1713 const string_literal_expression_t *const expr)
1715 if (current_function_name == NULL) {
1716 const source_position_t *const src_pos =
1717 &expr->expression.source_position;
1718 const char *const name = current_function_decl->symbol->string;
1719 current_function_name = string_to_firm(src_pos, "__func__", name);
1722 return current_function_name;
1725 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1727 statement_t *statement = expr->statement;
1729 assert(statement->type == STATEMENT_COMPOUND);
1730 return compound_statement_to_firm((compound_statement_t*) statement);
1733 static ir_node *va_start_expression_to_firm(
1734 const va_start_expression_t *const expr)
1736 ir_type *const method_type = get_ir_type(current_function_decl->type);
1737 int const n = get_method_n_params(method_type) - 1;
1738 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
1739 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
1740 dbg_info *const dbgi =
1741 get_dbg_info(&expr->expression.source_position);
1742 ir_node *const no_mem = new_NoMem();
1743 ir_node *const arg_sel =
1744 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
1746 size_t const parm_size = get_type_size(expr->parameter->type);
1747 ir_node *const cnst = new_Const_long(mode_Iu, parm_size);
1748 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
1749 set_value_for_expression(expr->ap, add);
1754 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
1756 ir_type *const irtype = get_ir_type(expr->expression.datatype);
1757 ir_node *const ap = expression_to_firm(expr->ap);
1758 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1759 ir_node *const res = deref_address(irtype, ap, dbgi);
1761 size_t const parm_size = get_type_size(expr->expression.datatype);
1762 ir_node *const cnst = new_Const_long(mode_Iu, parm_size);
1763 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
1764 set_value_for_expression(expr->ap, add);
1769 static ir_node *dereference_addr(const unary_expression_t *const expression)
1771 assert(expression->expression.type == EXPR_UNARY_DEREFERENCE);
1772 return expression_to_firm(expression->value);
1775 static ir_node *expression_to_addr(const expression_t *expression)
1777 switch(expression->type) {
1778 case EXPR_REFERENCE:
1779 return reference_addr(&expression->reference);
1780 case EXPR_ARRAY_ACCESS:
1781 return array_access_addr(&expression->array_access);
1783 return select_addr(&expression->select);
1785 return call_expression_to_firm(&expression->call);
1786 case EXPR_UNARY_DEREFERENCE: {
1787 return dereference_addr(&expression->unary);
1792 panic("trying to get address of non-lvalue");
1795 static ir_node *_expression_to_firm(const expression_t *expression)
1797 switch(expression->type) {
1799 return const_to_firm(&expression->conste);
1800 case EXPR_STRING_LITERAL:
1801 return string_literal_to_firm(&expression->string);
1802 case EXPR_WIDE_STRING_LITERAL:
1803 return wide_string_literal_to_firm(&expression->wide_string);
1804 case EXPR_REFERENCE:
1805 return reference_expression_to_firm(&expression->reference);
1807 return call_expression_to_firm(&expression->call);
1809 return unary_expression_to_firm(&expression->unary);
1811 return binary_expression_to_firm(&expression->binary);
1812 case EXPR_ARRAY_ACCESS:
1813 return array_access_to_firm(&expression->array_access);
1815 return sizeof_to_firm(&expression->sizeofe);
1816 case EXPR_CONDITIONAL:
1817 return conditional_to_firm(&expression->conditional);
1819 return select_to_firm(&expression->select);
1820 case EXPR_CLASSIFY_TYPE:
1821 return classify_type_to_firm(&expression->classify_type);
1823 case EXPR_PRETTY_FUNCTION:
1824 return function_name_to_firm(&expression->string);
1825 case EXPR_STATEMENT:
1826 return statement_expression_to_firm(&expression->statement);
1828 return va_start_expression_to_firm(&expression->va_starte);
1830 return va_arg_expression_to_firm(&expression->va_arge);
1832 case EXPR_BUILTIN_SYMBOL:
1833 panic("unimplemented expression found");
1839 panic("invalid expression found");
1842 static ir_node *expression_to_firm(const expression_t *expression)
1844 ir_node *res = _expression_to_firm(expression);
1846 if(res != NULL && get_irn_mode(res) == mode_b) {
1847 ir_mode *mode = get_ir_mode(expression->base.datatype);
1848 res = create_conv(NULL, res, mode);
1854 static ir_node *expression_to_modeb(const expression_t *expression)
1856 ir_node *res = _expression_to_firm(expression);
1857 res = create_conv(NULL, res, mode_b);
1863 * create a short-circuit expression evaluation that tries to construct
1864 * efficient control flow structures for &&, || and ! expressions
1866 static void create_condition_evaluation(const expression_t *expression,
1867 ir_node *true_block,
1868 ir_node *false_block)
1870 switch(expression->type) {
1871 case EXPR_UNARY_NOT: {
1872 const unary_expression_t *unary_expression = &expression->unary;
1873 create_condition_evaluation(unary_expression->value, false_block,
1877 case EXPR_BINARY_LOGICAL_AND: {
1878 const binary_expression_t *binary_expression = &expression->binary;
1880 ir_node *cur_block = get_cur_block();
1881 ir_node *extra_block = new_immBlock();
1882 set_cur_block(cur_block);
1883 create_condition_evaluation(binary_expression->left, extra_block,
1885 mature_immBlock(extra_block);
1886 set_cur_block(extra_block);
1887 create_condition_evaluation(binary_expression->right, true_block,
1891 case EXPR_BINARY_LOGICAL_OR: {
1892 const binary_expression_t *binary_expression = &expression->binary;
1894 ir_node *cur_block = get_cur_block();
1895 ir_node *extra_block = new_immBlock();
1896 set_cur_block(cur_block);
1897 create_condition_evaluation(binary_expression->left, true_block,
1899 mature_immBlock(extra_block);
1900 set_cur_block(extra_block);
1901 create_condition_evaluation(binary_expression->right, true_block,
1909 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1910 ir_node *condition = expression_to_modeb(expression);
1911 ir_node *cond = new_d_Cond(dbgi, condition);
1912 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1913 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1915 add_immBlock_pred(true_block, true_proj);
1916 add_immBlock_pred(false_block, false_proj);
1918 set_cur_block(NULL);
1921 static void return_statement_to_firm(return_statement_t *statement)
1923 if(get_cur_block() == NULL)
1926 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1928 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1932 if(get_method_n_ress(func_irtype) > 0) {
1933 ir_type *res_type = get_method_res_type(func_irtype, 0);
1935 if(statement->return_value != NULL) {
1936 ir_node *node = expression_to_firm(statement->return_value);
1937 node = do_strict_conv(dbgi, node);
1941 if(is_compound_type(res_type)) {
1944 mode = get_type_mode(res_type);
1946 in[0] = new_Unknown(mode);
1950 /* build return_value for its side effects */
1951 if(statement->return_value != NULL) {
1952 expression_to_firm(statement->return_value);
1957 ir_node *store = get_store();
1958 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1960 ir_node *end_block = get_irg_end_block(current_ir_graph);
1961 add_immBlock_pred(end_block, ret);
1963 set_cur_block(NULL);
1966 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1968 if(get_cur_block() == NULL)
1971 return expression_to_firm(statement->expression);
1974 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1976 ir_node *result = NULL;
1977 statement_t *statement = compound->statements;
1978 for( ; statement != NULL; statement = statement->base.next) {
1979 //context2firm(&statement->context);
1981 if(statement->base.next == NULL
1982 && statement->type == STATEMENT_EXPRESSION) {
1983 result = expression_statement_to_firm(
1984 (expression_statement_t*) statement);
1987 statement_to_firm(statement);
1993 static void if_statement_to_firm(if_statement_t *statement)
1995 ir_node *cur_block = get_cur_block();
1997 ir_node *fallthrough_block = new_immBlock();
1999 /* the true (blocks) */
2000 ir_node *true_block;
2001 if (statement->true_statement != NULL) {
2002 true_block = new_immBlock();
2003 statement_to_firm(statement->true_statement);
2004 if(get_cur_block() != NULL) {
2005 ir_node *jmp = new_Jmp();
2006 add_immBlock_pred(fallthrough_block, jmp);
2009 true_block = fallthrough_block;
2012 /* the false (blocks) */
2013 ir_node *false_block;
2014 if(statement->false_statement != NULL) {
2015 false_block = new_immBlock();
2017 statement_to_firm(statement->false_statement);
2018 if(get_cur_block() != NULL) {
2019 ir_node *jmp = new_Jmp();
2020 add_immBlock_pred(fallthrough_block, jmp);
2023 false_block = fallthrough_block;
2026 /* create the condition */
2027 if(cur_block != NULL) {
2028 set_cur_block(cur_block);
2029 create_condition_evaluation(statement->condition, true_block,
2033 mature_immBlock(true_block);
2034 if(false_block != fallthrough_block) {
2035 mature_immBlock(false_block);
2037 mature_immBlock(fallthrough_block);
2039 set_cur_block(fallthrough_block);
2042 static void while_statement_to_firm(while_statement_t *statement)
2044 ir_node *jmp = NULL;
2045 if(get_cur_block() != NULL) {
2049 /* create the header block */
2050 ir_node *header_block = new_immBlock();
2052 add_immBlock_pred(header_block, jmp);
2055 /* the false block */
2056 ir_node *false_block = new_immBlock();
2059 ir_node *body_block;
2060 if (statement->body != NULL) {
2061 ir_node *old_continue_label = continue_label;
2062 ir_node *old_break_label = break_label;
2063 continue_label = header_block;
2064 break_label = false_block;
2066 body_block = new_immBlock();
2067 statement_to_firm(statement->body);
2069 assert(continue_label == header_block);
2070 assert(break_label == false_block);
2071 continue_label = old_continue_label;
2072 break_label = old_break_label;
2074 if(get_cur_block() != NULL) {
2076 add_immBlock_pred(header_block, jmp);
2079 body_block = header_block;
2082 /* create the condition */
2083 set_cur_block(header_block);
2085 create_condition_evaluation(statement->condition, body_block, false_block);
2086 mature_immBlock(body_block);
2087 mature_immBlock(false_block);
2088 mature_immBlock(header_block);
2090 set_cur_block(false_block);
2093 static void do_while_statement_to_firm(do_while_statement_t *statement)
2095 ir_node *jmp = NULL;
2096 if(get_cur_block() != NULL) {
2100 /* create the header block */
2101 ir_node *header_block = new_immBlock();
2103 /* the false block */
2104 ir_node *false_block = new_immBlock();
2107 ir_node *body_block = new_immBlock();
2109 add_immBlock_pred(body_block, jmp);
2112 if (statement->body != NULL) {
2113 ir_node *old_continue_label = continue_label;
2114 ir_node *old_break_label = break_label;
2115 continue_label = header_block;
2116 break_label = false_block;
2118 statement_to_firm(statement->body);
2120 assert(continue_label == header_block);
2121 assert(break_label == false_block);
2122 continue_label = old_continue_label;
2123 break_label = old_break_label;
2125 if (get_cur_block() == NULL) {
2126 mature_immBlock(header_block);
2127 mature_immBlock(body_block);
2128 mature_immBlock(false_block);
2133 ir_node *body_jmp = new_Jmp();
2134 add_immBlock_pred(header_block, body_jmp);
2135 mature_immBlock(header_block);
2137 /* create the condition */
2138 set_cur_block(header_block);
2140 create_condition_evaluation(statement->condition, body_block, false_block);
2141 mature_immBlock(body_block);
2142 mature_immBlock(false_block);
2143 mature_immBlock(header_block);
2145 set_cur_block(false_block);
2148 static void for_statement_to_firm(for_statement_t *statement)
2150 ir_node *jmp = NULL;
2151 if (get_cur_block() != NULL) {
2152 if(statement->initialisation != NULL) {
2153 expression_to_firm(statement->initialisation);
2158 /* create the step block */
2159 ir_node *const step_block = new_immBlock();
2160 if (statement->step != NULL) {
2161 expression_to_firm(statement->step);
2163 ir_node *const step_jmp = new_Jmp();
2165 /* create the header block */
2166 ir_node *const header_block = new_immBlock();
2168 add_immBlock_pred(header_block, jmp);
2170 add_immBlock_pred(header_block, step_jmp);
2172 /* the false block */
2173 ir_node *const false_block = new_immBlock();
2176 ir_node * body_block;
2177 if (statement->body != NULL) {
2178 ir_node *const old_continue_label = continue_label;
2179 ir_node *const old_break_label = break_label;
2180 continue_label = step_block;
2181 break_label = false_block;
2183 body_block = new_immBlock();
2184 statement_to_firm(statement->body);
2186 assert(continue_label == step_block);
2187 assert(break_label == false_block);
2188 continue_label = old_continue_label;
2189 break_label = old_break_label;
2191 if (get_cur_block() != NULL) {
2193 add_immBlock_pred(step_block, jmp);
2196 body_block = step_block;
2199 /* create the condition */
2200 set_cur_block(header_block);
2201 if (statement->condition != NULL) {
2202 create_condition_evaluation(statement->condition, body_block,
2205 keep_alive(header_block);
2207 add_immBlock_pred(body_block, jmp);
2210 mature_immBlock(body_block);
2211 mature_immBlock(false_block);
2212 mature_immBlock(step_block);
2213 mature_immBlock(header_block);
2214 mature_immBlock(false_block);
2216 set_cur_block(false_block);
2219 static void create_declaration_entity(declaration_t *declaration,
2220 declaration_type_t declaration_type,
2221 ir_type *parent_type)
2223 ident *id = new_id_from_str(declaration->symbol->string);
2224 ir_type *irtype = get_ir_type(declaration->type);
2225 ir_entity *entity = new_entity(parent_type, id, irtype);
2226 set_entity_ld_ident(entity, id);
2228 declaration->declaration_type = (unsigned char) declaration_type;
2229 declaration->v.entity = entity;
2230 set_entity_variability(entity, variability_uninitialized);
2231 /* TODO: visibility? */
2234 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2236 enum compound_graph_entry_type_t {
2237 COMPOUND_GRAPH_ENTRY_ARRAY,
2238 COMPOUND_GRAPH_ENTRY_COMPOUND
2241 struct compound_graph_path_entry_t {
2247 compound_graph_path_entry_t *prev;
2250 static void create_initializer_object(initializer_t *initializer, type_t *type,
2251 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2253 static compound_graph_path *create_compound_path(ir_type *type,
2254 compound_graph_path_entry_t *entry, int len)
2256 compound_graph_path *path = new_compound_graph_path(type, len);
2259 for( ; entry != NULL; entry = entry->prev, --i) {
2261 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2262 set_compound_graph_path_node(path, i, entry->v.entity);
2264 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2265 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2273 static void create_initializer_value(initializer_value_t *initializer,
2275 compound_graph_path_entry_t *entry,
2278 ir_node *node = expression_to_firm(initializer->value);
2279 ir_type *type = get_entity_type(entity);
2280 compound_graph_path *path = create_compound_path(type, entry, len);
2281 add_compound_ent_value_w_path(entity, node, path);
2284 static void create_initializer_compound(initializer_list_t *initializer,
2285 compound_type_t *type,
2287 compound_graph_path_entry_t *last_entry,
2290 declaration_t *compound_declaration = type->declaration;
2292 declaration_t *compound_entry = compound_declaration->context.declarations;
2294 compound_graph_path_entry_t entry;
2295 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2296 entry.prev = last_entry;
2300 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2301 if(compound_entry->symbol == NULL)
2303 if(compound_entry->namespc != NAMESPACE_NORMAL)
2306 if(i >= initializer->len)
2309 entry.v.entity = compound_entry->v.entity;
2311 initializer_t *sub_initializer = initializer->initializers[i];
2313 assert(compound_entry != NULL);
2314 assert(compound_entry->declaration_type
2315 == DECLARATION_TYPE_COMPOUND_MEMBER);
2317 if(sub_initializer->type == INITIALIZER_VALUE) {
2318 create_initializer_value(&sub_initializer->value,
2319 entity, &entry, len);
2321 type_t *entry_type = skip_typeref(compound_entry->type);
2322 create_initializer_object(sub_initializer, entry_type, entity,
2330 static void create_initializer_array(initializer_list_t *initializer,
2331 array_type_t *type, ir_entity *entity,
2332 compound_graph_path_entry_t *last_entry,
2335 type_t *element_type = type->element_type;
2336 element_type = skip_typeref(element_type);
2338 compound_graph_path_entry_t entry;
2339 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2340 entry.prev = last_entry;
2344 for(i = 0; i < initializer->len; ++i) {
2345 entry.v.array_index = i;
2347 initializer_t *sub_initializer = initializer->initializers[i];
2349 if(sub_initializer->type == INITIALIZER_VALUE) {
2350 create_initializer_value(&sub_initializer->value,
2351 entity, &entry, len);
2353 create_initializer_object(sub_initializer, element_type, entity,
2359 /* TODO: initialize rest... */
2360 if(type->size_expression != NULL) {
2361 size_t array_len = fold_constant(type->size_expression);
2362 for( ; i < array_len; ++i) {
2369 static void create_initializer_string(initializer_string_t *initializer,
2370 array_type_t *type, ir_entity *entity,
2371 compound_graph_path_entry_t *last_entry,
2374 type_t *element_type = type->element_type;
2375 element_type = skip_typeref(element_type);
2377 compound_graph_path_entry_t entry;
2378 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2379 entry.prev = last_entry;
2382 ir_type *irtype = get_entity_type(entity);
2383 size_t arr_len = get_array_type_size(type);
2384 const char *p = initializer->string;
2386 for(i = 0; i < arr_len; ++i, ++p) {
2387 entry.v.array_index = i;
2389 ir_node *node = new_Const_long(mode_Bs, *p);
2390 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2391 add_compound_ent_value_w_path(entity, node, path);
2398 static void create_initializer_wide_string(
2399 const initializer_wide_string_t *const initializer, array_type_t *const type,
2400 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2403 type_t *element_type = type->element_type;
2404 element_type = skip_typeref(element_type);
2406 compound_graph_path_entry_t entry;
2407 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2408 entry.prev = last_entry;
2411 ir_type *const irtype = get_entity_type(entity);
2412 const size_t arr_len = get_array_type_size(type);
2413 const wchar_rep_t * p = initializer->string.begin;
2414 const wchar_rep_t *const end = p + initializer->string.size;
2415 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2416 entry.v.array_index = i;
2418 ir_node *node = new_Const_long(mode_Is, *p);
2419 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2420 add_compound_ent_value_w_path(entity, node, path);
2424 static void create_initializer_object(initializer_t *initializer, type_t *type,
2425 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2427 if(is_type_array(type)) {
2428 array_type_t *array_type = &type->array;
2430 switch (initializer->type) {
2431 case INITIALIZER_STRING: {
2432 initializer_string_t *const string = &initializer->string;
2433 create_initializer_string(string, array_type, entity, entry, len);
2437 case INITIALIZER_WIDE_STRING: {
2438 initializer_wide_string_t *const string = &initializer->wide_string;
2439 create_initializer_wide_string(string, array_type, entity, entry, len);
2443 case INITIALIZER_LIST: {
2444 initializer_list_t *const list = &initializer->list;
2445 create_initializer_array(list, array_type, entity, entry, len);
2449 case INITIALIZER_VALUE:
2452 panic("Unhandled initializer");
2454 assert(initializer->type == INITIALIZER_LIST);
2455 initializer_list_t *list = &initializer->list;
2457 assert(is_type_compound(type));
2458 compound_type_t *compound_type = &type->compound;
2459 create_initializer_compound(list, compound_type, entity, entry, len);
2463 static void create_initializer_local_variable_entity(declaration_t *declaration)
2465 initializer_t *initializer = declaration->init.initializer;
2466 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2467 ir_entity *entity = declaration->v.entity;
2468 ir_node *memory = get_store();
2469 ir_node *nomem = new_NoMem();
2470 ir_node *frame = get_irg_frame(current_ir_graph);
2471 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2473 if(initializer->type == INITIALIZER_VALUE) {
2474 initializer_value_t *initializer_value = &initializer->value;
2476 ir_node *value = expression_to_firm(initializer_value->value);
2477 type_t *type = skip_typeref(declaration->type);
2478 assign_value(dbgi, addr, type, value);
2482 /* create a "template" entity which is copied to the entity on the stack */
2483 ident *id = unique_ident("initializer");
2484 ir_type *irtype = get_ir_type(declaration->type);
2485 ir_type *global_type = get_glob_type();
2486 ir_entity *init_entity = new_entity(global_type, id, irtype);
2487 set_entity_ld_ident(init_entity, id);
2489 set_entity_variability(init_entity, variability_initialized);
2490 set_entity_visibility(init_entity, visibility_local);
2492 ir_graph *old_current_ir_graph = current_ir_graph;
2493 current_ir_graph = get_const_code_irg();
2495 type_t *type = skip_typeref(declaration->type);
2496 create_initializer_object(initializer, type, init_entity, NULL, 0);
2498 assert(current_ir_graph == get_const_code_irg());
2499 current_ir_graph = old_current_ir_graph;
2501 ir_node *src_addr = create_symconst(dbgi, init_entity);
2502 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2504 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2505 set_store(copyb_mem);
2508 static void create_initializer(declaration_t *declaration)
2510 initializer_t *initializer = declaration->init.initializer;
2511 if(initializer == NULL)
2514 declaration_type_t declaration_type
2515 = (declaration_type_t) declaration->declaration_type;
2516 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2517 create_initializer_local_variable_entity(declaration);
2521 if(initializer->type == INITIALIZER_VALUE) {
2522 initializer_value_t *initializer_value = &initializer->value;
2524 ir_node *value = expression_to_firm(initializer_value->value);
2526 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2527 set_value(declaration->v.value_number, value);
2529 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2531 ir_entity *entity = declaration->v.entity;
2533 set_entity_variability(entity, variability_initialized);
2534 set_atomic_ent_value(entity, value);
2537 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2538 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2540 ir_entity *entity = declaration->v.entity;
2541 set_entity_variability(entity, variability_initialized);
2543 type_t *type = skip_typeref(declaration->type);
2544 create_initializer_object(initializer, type, entity, NULL, 0);
2548 static void create_local_variable(declaration_t *declaration)
2550 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2552 bool needs_entity = declaration->address_taken;
2553 type_t *type = skip_typeref(declaration->type);
2555 if(is_type_array(type) || is_type_compound(type)) {
2556 needs_entity = true;
2560 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2561 create_declaration_entity(declaration,
2562 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2565 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2566 declaration->v.value_number = next_value_number_function;
2567 ++next_value_number_function;
2570 create_initializer(declaration);
2573 static void create_local_static_variable(declaration_t *declaration)
2575 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2577 type_t *type = skip_typeref(declaration->type);
2578 ir_type *global_type = get_glob_type();
2579 ident *id = unique_ident(declaration->symbol->string);
2580 ir_type *irtype = get_ir_type(type);
2581 ir_entity *entity = new_entity(global_type, id, irtype);
2582 set_entity_ld_ident(entity, id);
2584 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2585 declaration->v.entity = entity;
2586 set_entity_variability(entity, variability_uninitialized);
2587 set_entity_visibility(entity, visibility_local);
2589 ir_graph *old_current_ir_graph = current_ir_graph;
2590 current_ir_graph = get_const_code_irg();
2592 create_initializer(declaration);
2594 assert(current_ir_graph == get_const_code_irg());
2595 current_ir_graph = old_current_ir_graph;
2598 static void declaration_statement_to_firm(declaration_statement_t *statement)
2600 declaration_t *declaration = statement->declarations_begin;
2601 declaration_t *end = statement->declarations_end->next;
2602 for( ; declaration != end; declaration = declaration->next) {
2603 type_t *type = skip_typeref(declaration->type);
2605 switch ((storage_class_tag_t) declaration->storage_class) {
2606 case STORAGE_CLASS_TYPEDEF:
2608 case STORAGE_CLASS_STATIC:
2609 create_local_static_variable(declaration);
2611 case STORAGE_CLASS_ENUM_ENTRY:
2612 panic("enum entry declaration in local block found");
2613 case STORAGE_CLASS_EXTERN:
2614 panic("extern declaration in local block found");
2615 case STORAGE_CLASS_NONE:
2616 case STORAGE_CLASS_AUTO:
2617 case STORAGE_CLASS_REGISTER:
2618 if(is_type_function(type)) {
2619 panic("nested functions not supported yet");
2621 create_local_variable(declaration);
2624 case STORAGE_CLASS_THREAD:
2625 case STORAGE_CLASS_THREAD_EXTERN:
2626 case STORAGE_CLASS_THREAD_STATIC:
2629 panic("invalid storage class found");
2633 static void create_jump_statement(const statement_t *statement,
2634 ir_node *target_block)
2636 if(get_cur_block() == NULL)
2639 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2640 ir_node *jump = new_d_Jmp(dbgi);
2641 add_immBlock_pred(target_block, jump);
2643 set_cur_block(NULL);
2646 static void switch_statement_to_firm(const switch_statement_t *statement)
2648 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2650 ir_node *expression = expression_to_firm(statement->expression);
2651 ir_node *cond = new_d_Cond(dbgi, expression);
2652 ir_node *break_block = new_immBlock();
2654 set_cur_block(NULL);
2656 ir_node *const old_switch_cond = current_switch_cond;
2657 ir_node *const old_break_label = break_label;
2658 const bool old_saw_default_label = saw_default_label;
2659 current_switch_cond = cond;
2660 break_label = break_block;
2662 statement_to_firm(statement->body);
2664 if(get_cur_block() != NULL) {
2665 ir_node *jmp = new_Jmp();
2666 add_immBlock_pred(break_block, jmp);
2669 if (!saw_default_label) {
2670 set_cur_block(get_nodes_block(cond));
2671 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2672 MAGIC_DEFAULT_PN_NUMBER);
2673 add_immBlock_pred(break_block, proj);
2676 assert(current_switch_cond == cond);
2677 assert(break_label == break_block);
2678 current_switch_cond = old_switch_cond;
2679 break_label = old_break_label;
2680 saw_default_label = old_saw_default_label;
2682 mature_immBlock(break_block);
2683 set_cur_block(break_block);
2686 static long fold_constant(const expression_t *expression)
2688 ir_graph *old_current_ir_graph = current_ir_graph;
2689 current_ir_graph = get_const_code_irg();
2691 ir_node *cnst = expression_to_firm(expression);
2692 if(!is_Const(cnst)) {
2693 panic("couldn't fold constantl");
2695 tarval *tv = get_Const_tarval(cnst);
2696 if(!tarval_is_long(tv)) {
2697 panic("folded constant not an integer");
2700 long res = get_tarval_long(tv);
2702 current_ir_graph = old_current_ir_graph;
2706 static void case_label_to_firm(const case_label_statement_t *statement)
2708 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2710 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2712 /* let's create a node and hope firm constant folding creates a Const
2715 set_cur_block(get_nodes_block(current_switch_cond));
2716 if(statement->expression) {
2717 long pn = fold_constant(statement->expression);
2718 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2719 /* oops someone detected our cheating... */
2720 panic("magic default pn used");
2722 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2724 saw_default_label = true;
2725 proj = new_d_defaultProj(dbgi, current_switch_cond,
2726 MAGIC_DEFAULT_PN_NUMBER);
2729 ir_node *block = new_immBlock();
2730 if (fallthrough != NULL) {
2731 add_immBlock_pred(block, fallthrough);
2733 add_immBlock_pred(block, proj);
2734 mature_immBlock(block);
2736 statement_to_firm(statement->label_statement);
2739 static ir_node *get_label_block(declaration_t *label)
2741 assert(label->namespc == NAMESPACE_LABEL);
2743 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2744 return label->v.block;
2746 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2748 ir_node *old_cur_block = get_cur_block();
2749 ir_node *block = new_immBlock();
2750 set_cur_block(old_cur_block);
2752 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2753 label->v.block = block;
2755 ARR_APP1(ir_node *, imature_blocks, block);
2760 static void label_to_firm(const label_statement_t *statement)
2762 ir_node *block = get_label_block(statement->label);
2764 if(get_cur_block() != NULL) {
2765 ir_node *jmp = new_Jmp();
2766 add_immBlock_pred(block, jmp);
2769 set_cur_block(block);
2772 statement_to_firm(statement->label_statement);
2775 static void goto_to_firm(const goto_statement_t *statement)
2777 if(get_cur_block() == NULL)
2780 ir_node *block = get_label_block(statement->label);
2781 ir_node *jmp = new_Jmp();
2782 add_immBlock_pred(block, jmp);
2784 set_cur_block(NULL);
2787 typedef enum modifier_t {
2788 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
2789 ASM_MODIFIER_READ_WRITE = 1 << 1,
2790 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
2791 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
2795 static void asm_statement_to_firm(const asm_statement_t *statement)
2797 bool needs_memory = false;
2799 size_t n_clobbers = 0;
2800 asm_clobber_t *clobber = statement->clobbers;
2801 for( ; clobber != NULL; clobber = clobber->next) {
2802 if(strcmp(clobber->clobber, "memory") == 0) {
2803 needs_memory = true;
2807 ident *id = new_id_from_str(clobber->clobber);
2808 obstack_ptr_grow(&asm_obst, id);
2811 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
2812 ident **clobbers = NULL;
2813 if(n_clobbers > 0) {
2814 clobbers = obstack_finish(&asm_obst);
2817 /* find and count input and output constraints */
2818 asm_constraint_t *constraint = statement->inputs;
2819 for( ; constraint != NULL; constraint = constraint->next) {
2821 bool supports_memop = false;
2822 for(const char *c = constraint->constraints; *c != 0; ++c) {
2823 /* TODO: improve error messages */
2827 panic("multiple alternative assembler constraints not "
2835 supports_memop = true;
2836 obstack_1grow(&asm_obst, *c);
2839 if(modifiers & ASM_MODIFIER_READ_WRITE)
2840 panic("inconsistent register constraints");
2841 modifiers |= ASM_MODIFIER_WRITE_ONLY;
2844 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
2845 panic("inconsistent register constraints");
2846 modifiers |= ASM_MODIFIER_READ_WRITE;
2849 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
2850 panic("early clobber assembler constraint not supported yet");
2853 modifiers |= ASM_MODIFIER_COMMUTATIVE;
2854 panic("commutative assembler constraint not supported yet");
2857 /* skip register preferences stuff... */
2858 while(*c != 0 && *c != ',')
2862 /* skip register preferences stuff... */
2866 obstack_1grow(&asm_obst, *c);
2870 obstack_1grow(&asm_obst, '\0');
2871 const char *constraint_string = obstack_finish(&asm_obst);
2873 needs_memory |= supports_memop;
2874 if(supports_memop) {
2882 static void statement_to_firm(statement_t *statement)
2884 switch(statement->type) {
2885 case STATEMENT_INVALID:
2886 panic("invalid statement found");
2887 case STATEMENT_COMPOUND:
2888 compound_statement_to_firm(&statement->compound);
2890 case STATEMENT_RETURN:
2891 return_statement_to_firm(&statement->returns);
2893 case STATEMENT_EXPRESSION:
2894 expression_statement_to_firm(&statement->expression);
2897 if_statement_to_firm(&statement->ifs);
2899 case STATEMENT_WHILE:
2900 while_statement_to_firm(&statement->whiles);
2902 case STATEMENT_DO_WHILE:
2903 do_while_statement_to_firm(&statement->do_while);
2905 case STATEMENT_DECLARATION:
2906 declaration_statement_to_firm(&statement->declaration);
2908 case STATEMENT_BREAK:
2909 create_jump_statement(statement, break_label);
2911 case STATEMENT_CONTINUE:
2912 create_jump_statement(statement, continue_label);
2914 case STATEMENT_SWITCH:
2915 switch_statement_to_firm(&statement->switchs);
2917 case STATEMENT_CASE_LABEL:
2918 case_label_to_firm(&statement->case_label);
2921 for_statement_to_firm(&statement->fors);
2923 case STATEMENT_LABEL:
2924 label_to_firm(&statement->label);
2926 case STATEMENT_GOTO:
2927 goto_to_firm(&statement->gotos);
2930 //asm_statement_to_firm(&statement->asms);
2934 panic("Statement not implemented\n");
2937 static int count_local_declarations(const declaration_t * decl,
2938 const declaration_t *const end)
2941 for (; decl != end; decl = decl->next) {
2942 const type_t *type = skip_typeref(decl->type);
2943 switch (type->type) {
2947 if (!decl->address_taken)
2957 static int count_decls_in_stmts(const statement_t *stmt)
2960 for (; stmt != NULL; stmt = stmt->base.next) {
2961 switch (stmt->type) {
2962 case STATEMENT_DECLARATION: {
2963 const declaration_statement_t *const decl_stmt =
2964 (const declaration_statement_t*)stmt;
2965 count += count_local_declarations(decl_stmt->declarations_begin,
2966 decl_stmt->declarations_end->next);
2970 case STATEMENT_COMPOUND: {
2971 const compound_statement_t *const comp =
2972 (const compound_statement_t*)stmt;
2973 count += count_decls_in_stmts(comp->statements);
2977 case STATEMENT_IF: {
2978 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2979 count += count_decls_in_stmts(if_stmt->true_statement);
2980 count += count_decls_in_stmts(if_stmt->false_statement);
2984 case STATEMENT_SWITCH: {
2985 const switch_statement_t *const switch_stmt =
2986 (const switch_statement_t*)stmt;
2987 count += count_decls_in_stmts(switch_stmt->body);
2991 case STATEMENT_LABEL: {
2992 const label_statement_t *const label_stmt =
2993 (const label_statement_t*)stmt;
2994 count += count_decls_in_stmts(label_stmt->label_statement);
2998 case STATEMENT_WHILE: {
2999 const while_statement_t *const while_stmt =
3000 (const while_statement_t*)stmt;
3001 count += count_decls_in_stmts(while_stmt->body);
3005 case STATEMENT_DO_WHILE: {
3006 const do_while_statement_t *const do_while_stmt =
3007 (const do_while_statement_t*)stmt;
3008 count += count_decls_in_stmts(do_while_stmt->body);
3012 case STATEMENT_FOR: {
3013 const for_statement_t *const for_stmt =
3014 (const for_statement_t*)stmt;
3015 /* TODO initialisation */
3016 count += count_decls_in_stmts(for_stmt->body);
3021 case STATEMENT_BREAK:
3022 case STATEMENT_CASE_LABEL:
3023 case STATEMENT_CONTINUE:
3024 case STATEMENT_EXPRESSION:
3025 case STATEMENT_GOTO:
3026 case STATEMENT_INVALID:
3027 case STATEMENT_RETURN:
3034 static int get_function_n_local_vars(declaration_t *declaration)
3038 /* count parameters */
3039 count += count_local_declarations(declaration->context.declarations, NULL);
3041 /* count local variables declared in body */
3042 count += count_decls_in_stmts(declaration->init.statement);
3044 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
3045 * for expression statements... */
3051 static void initialize_function_parameters(declaration_t *declaration)
3053 ir_graph *irg = current_ir_graph;
3054 ir_node *args = get_irg_args(irg);
3055 ir_node *start_block = get_irg_start_block(irg);
3056 ir_type *function_irtype = get_ir_type(declaration->type);
3059 declaration_t *parameter = declaration->context.declarations;
3060 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3061 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
3062 type_t *type = skip_typeref(parameter->type);
3064 bool needs_entity = parameter->address_taken;
3065 assert(!is_type_array(type));
3066 if(is_type_compound(type)) {
3067 needs_entity = true;
3071 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3072 ident *id = new_id_from_str(parameter->symbol->string);
3073 set_entity_ident(entity, id);
3075 parameter->declaration_type
3076 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
3077 parameter->v.entity = entity;
3081 ir_mode *mode = get_ir_mode(parameter->type);
3083 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3085 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
3086 parameter->v.value_number = next_value_number_function;
3087 ++next_value_number_function;
3089 set_value(parameter->v.value_number, proj);
3093 static void create_function(declaration_t *declaration)
3095 ir_entity *function_entity = get_function_entity(declaration);
3097 if(declaration->init.statement == NULL)
3100 current_function_decl = declaration;
3101 current_function_name = NULL;
3103 assert(imature_blocks == NULL);
3104 imature_blocks = NEW_ARR_F(ir_node*, 0);
3106 int n_local_vars = get_function_n_local_vars(declaration);
3107 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3108 ir_node *first_block = get_cur_block();
3110 next_value_number_function = 0;
3111 initialize_function_parameters(declaration);
3113 statement_to_firm(declaration->init.statement);
3115 ir_node *end_block = get_irg_end_block(irg);
3117 /* do we have a return statement yet? */
3118 if(get_cur_block() != NULL) {
3119 type_t *type = skip_typeref(declaration->type);
3120 assert(is_type_function(type));
3121 const function_type_t *func_type = &type->function;
3122 const type_t *return_type
3123 = skip_typeref(func_type->return_type);
3126 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3127 ret = new_Return(get_store(), 0, NULL);
3130 if(is_type_scalar(return_type)) {
3131 mode = get_ir_mode(func_type->return_type);
3137 // ยง5.1.2.2.3 main implicitly returns 0
3138 if (strcmp(declaration->symbol->string, "main") == 0) {
3139 in[0] = new_Const(mode, get_mode_null(mode));
3141 in[0] = new_Unknown(mode);
3143 ret = new_Return(get_store(), 1, in);
3145 add_immBlock_pred(end_block, ret);
3148 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3149 mature_immBlock(imature_blocks[i]);
3151 DEL_ARR_F(imature_blocks);
3152 imature_blocks = NULL;
3154 mature_immBlock(first_block);
3155 mature_immBlock(end_block);
3157 irg_finalize_cons(irg);
3159 /* finalize the frame type */
3160 ir_type *frame_type = get_irg_frame_type(irg);
3161 int n = get_compound_n_members(frame_type);
3164 for(int i = 0; i < n; ++i) {
3165 ir_entity *entity = get_compound_member(frame_type, i);
3166 ir_type *entity_type = get_entity_type(entity);
3168 int align = get_type_alignment_bytes(entity_type);
3169 if(align > align_all)
3173 misalign = offset % align;
3175 offset += align - misalign;
3179 set_entity_offset(entity, offset);
3180 offset += get_type_size_bytes(entity_type);
3182 set_type_size_bytes(frame_type, offset);
3183 set_type_alignment_bytes(frame_type, align_all);
3184 set_type_state(frame_type, layout_fixed);
3189 static void create_global_variable(declaration_t *declaration)
3193 switch ((storage_class_tag_t)declaration->storage_class) {
3194 case STORAGE_CLASS_STATIC:
3195 vis = visibility_local;
3198 case STORAGE_CLASS_EXTERN:
3199 vis = visibility_external_allocated;
3202 case STORAGE_CLASS_NONE:
3203 vis = visibility_external_visible;
3206 case STORAGE_CLASS_THREAD:
3207 vis = visibility_external_visible;
3210 case STORAGE_CLASS_THREAD_EXTERN:
3211 vis = visibility_external_allocated;
3214 case STORAGE_CLASS_THREAD_STATIC:
3215 vis = visibility_local;
3219 var_type = get_tls_type();
3223 var_type = get_glob_type();
3227 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3229 set_entity_visibility(declaration->v.entity, vis);
3231 current_ir_graph = get_const_code_irg();
3232 create_initializer(declaration);
3235 case STORAGE_CLASS_TYPEDEF:
3236 case STORAGE_CLASS_AUTO:
3237 case STORAGE_CLASS_REGISTER:
3238 case STORAGE_CLASS_ENUM_ENTRY:
3241 panic("Invalid storage class for global variable");
3244 static void context_to_firm(context_t *context)
3246 /* first pass: create declarations */
3247 declaration_t *declaration = context->declarations;
3248 for( ; declaration != NULL; declaration = declaration->next) {
3249 if(declaration->namespc != NAMESPACE_NORMAL)
3251 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3252 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3254 if(declaration->symbol == NULL)
3257 type_t *type = skip_typeref(declaration->type);
3258 if(is_type_function(type)) {
3259 get_function_entity(declaration);
3261 create_global_variable(declaration);
3265 /* second pass: create code */
3266 declaration = context->declarations;
3267 for( ; declaration != NULL; declaration = declaration->next) {
3268 if(declaration->namespc != NAMESPACE_NORMAL)
3270 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3271 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3273 if(declaration->symbol == NULL)
3276 type_t *type = declaration->type;
3277 if(type->type != TYPE_FUNCTION)
3280 create_function(declaration);
3284 void translation_unit_to_firm(translation_unit_t *unit)
3286 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3287 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3288 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3290 ir_type_int = get_ir_type(type_int);
3291 ir_type_const_char = get_ir_type(type_const_char);
3292 ir_type_wchar_t = get_ir_type(type_wchar_t);
3293 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3296 type_void->base.firm_type = ir_type_void;
3298 /* just to be sure */
3299 continue_label = NULL;
3301 current_switch_cond = NULL;
3303 context_to_firm(& unit->context);