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_BOOL:
124 case ATOMIC_TYPE_LONG:
125 case ATOMIC_TYPE_INT:
127 case ATOMIC_TYPE_ULONG:
128 case ATOMIC_TYPE_UINT:
130 case ATOMIC_TYPE_LONGLONG:
132 case ATOMIC_TYPE_ULONGLONG:
134 case ATOMIC_TYPE_FLOAT:
136 case ATOMIC_TYPE_DOUBLE:
138 case ATOMIC_TYPE_LONG_DOUBLE:
140 #ifdef PROVIDE_COMPLEX
141 case ATOMIC_TYPE_FLOAT_COMPLEX:
142 case ATOMIC_TYPE_DOUBLE_COMPLEX:
143 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
144 panic("complex lowering not implemented yet");
146 case ATOMIC_TYPE_FLOAT_IMAGINARY:
147 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
148 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
149 panic("imaginary lowering not implemented yet");
152 case ATOMIC_TYPE_VOID:
153 /* firm has no real void... */
155 case ATOMIC_TYPE_INVALID:
158 panic("Encountered unknown atomic type");
162 static unsigned get_type_size(type_t *type);
164 static unsigned get_atomic_type_size(const atomic_type_t *type)
166 switch(type->atype) {
167 case ATOMIC_TYPE_CHAR:
168 case ATOMIC_TYPE_SCHAR:
169 case ATOMIC_TYPE_UCHAR:
172 case ATOMIC_TYPE_SHORT:
173 case ATOMIC_TYPE_USHORT:
176 case ATOMIC_TYPE_BOOL:
177 case ATOMIC_TYPE_INT:
178 case ATOMIC_TYPE_UINT:
179 case ATOMIC_TYPE_LONG:
180 case ATOMIC_TYPE_ULONG:
181 case ATOMIC_TYPE_FLOAT:
184 case ATOMIC_TYPE_LONGLONG:
185 case ATOMIC_TYPE_ULONGLONG:
186 case ATOMIC_TYPE_DOUBLE:
189 case ATOMIC_TYPE_LONG_DOUBLE:
192 case ATOMIC_TYPE_VOID:
195 case ATOMIC_TYPE_INVALID:
198 panic("Trying to determine size of invalid atomic type");
201 static unsigned get_compound_type_size(compound_type_t *type)
203 ir_type *irtype = get_ir_type((type_t*) type);
204 return get_type_size_bytes(irtype);
207 static unsigned get_array_type_size(array_type_t *type)
209 ir_type *irtype = get_ir_type((type_t*) type);
210 return get_type_size_bytes(irtype);
213 static unsigned get_type_size(type_t *type)
215 type = skip_typeref(type);
219 return get_atomic_type_size(&type->atomic);
221 return get_mode_size_bytes(mode_Is);
222 case TYPE_COMPOUND_UNION:
223 case TYPE_COMPOUND_STRUCT:
224 return get_compound_type_size(&type->compound);
226 /* just a pointer to the function */
227 return get_mode_size_bytes(mode_P_code);
229 return get_mode_size_bytes(mode_P_data);
231 return get_array_type_size(&type->array);
233 return get_type_size(type->builtin.real_type);
239 panic("Trying to determine size of invalid type");
242 static unsigned count_parameters(const function_type_t *function_type)
246 function_parameter_t *parameter = function_type->parameters;
247 for ( ; parameter != NULL; parameter = parameter->next) {
257 static long fold_constant(const expression_t *expression);
259 static ir_type *create_atomic_type(const atomic_type_t *type)
261 ir_mode *mode = get_atomic_mode(type);
262 ident *id = get_mode_ident(mode);
263 ir_type *irtype = new_type_primitive(id, mode);
265 if(type->atype == ATOMIC_TYPE_LONG_DOUBLE) {
266 set_type_alignment_bytes(irtype, 4);
272 static ir_type *create_method_type(const function_type_t *function_type)
274 type_t *return_type = function_type->return_type;
276 ident *id = unique_ident("functiontype");
277 int n_parameters = count_parameters(function_type);
278 int n_results = return_type == type_void ? 0 : 1;
279 ir_type *irtype = new_type_method(id, n_parameters, n_results);
281 if(return_type != type_void) {
282 ir_type *restype = get_ir_type(return_type);
283 set_method_res_type(irtype, 0, restype);
286 function_parameter_t *parameter = function_type->parameters;
288 for( ; parameter != NULL; parameter = parameter->next) {
289 ir_type *p_irtype = get_ir_type(parameter->type);
290 set_method_param_type(irtype, n, p_irtype);
294 if(function_type->variadic || function_type->unspecified_parameters) {
295 set_method_variadicity(irtype, variadicity_variadic);
301 static ir_type *create_pointer_type(pointer_type_t *type)
303 type_t *points_to = type->points_to;
304 ir_type *ir_points_to;
305 /* Avoid endless recursion if the points_to type contains this poiner type
306 * again (might be a struct). We therefore first create a void* pointer
307 * and then set the real points_to type
309 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
310 ir_type_void, mode_P_data);
311 type->type.firm_type = ir_type;
313 ir_points_to = get_ir_type(points_to);
314 set_pointer_points_to_type(ir_type, ir_points_to);
319 static ir_type *create_array_type(array_type_t *type)
321 type_t *element_type = type->element_type;
322 ir_type *ir_element_type = get_ir_type(element_type);
324 ident *id = unique_ident("array");
325 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
327 if(type->size != NULL) {
328 int n_elements = fold_constant(type->size);
330 set_array_bounds_int(ir_type, 0, 0, n_elements);
332 size_t elemsize = get_type_size_bytes(ir_element_type);
333 int align = get_type_alignment_bytes(ir_element_type);
334 if(elemsize % align > 0) {
335 elemsize += align - (elemsize % align);
337 set_type_size_bytes(ir_type, n_elements * elemsize);
338 set_type_alignment_bytes(ir_type, align);
339 set_type_state(ir_type, layout_fixed);
345 #define INVALID_TYPE ((ir_type_ptr)-1)
347 static ir_type *create_struct_type(compound_type_t *type)
349 symbol_t *symbol = type->declaration->symbol;
352 id = unique_ident(symbol->string);
354 id = unique_ident("__anonymous_struct");
356 ir_type *ir_type = new_type_struct(id);
358 type->type.firm_type = ir_type;
362 declaration_t *entry = type->declaration->context.declarations;
363 for( ; entry != NULL; entry = entry->next) {
364 if(entry->namespc != NAMESPACE_NORMAL)
367 ident *ident = new_id_from_str(entry->symbol->string);
368 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
370 int entry_size = get_type_size_bytes(entry_ir_type);
371 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
372 int misalign = offset % entry_alignment;
374 offset += entry_alignment - misalign;
376 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
377 set_entity_offset(entity, offset);
378 add_struct_member(ir_type, entity);
379 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
380 entry->v.entity = entity;
382 offset += entry_size;
383 if(entry_alignment > align_all) {
384 if(entry_alignment % align_all != 0) {
385 panic("Uneven alignments not supported yet");
387 align_all = entry_alignment;
391 int misalign = offset % align_all;
393 set_type_alignment_bytes(ir_type, align_all);
394 set_type_size_bytes(ir_type, offset);
395 set_type_state(ir_type, layout_fixed);
400 static ir_type *create_union_type(compound_type_t *type)
402 declaration_t *declaration = type->declaration;
403 symbol_t *symbol = declaration->symbol;
406 id = unique_ident(symbol->string);
408 id = unique_ident("__anonymous_union");
410 ir_type *ir_type = new_type_union(id);
412 type->type.firm_type = ir_type;
416 declaration_t *entry = declaration->context.declarations;
417 for( ; entry != NULL; entry = entry->next) {
418 if(entry->namespc != NAMESPACE_NORMAL)
421 ident *ident = new_id_from_str(entry->symbol->string);
422 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
424 int entry_size = get_type_size_bytes(entry_ir_type);
425 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
427 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
428 add_union_member(ir_type, entity);
429 set_entity_offset(entity, 0);
430 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
431 entry->v.entity = entity;
433 if(entry_size > size) {
436 if(entry_alignment > align_all) {
437 if(entry_alignment % align_all != 0) {
438 panic("Uneven alignments not supported yet");
440 align_all = entry_alignment;
444 set_type_alignment_bytes(ir_type, align_all);
445 set_type_size_bytes(ir_type, size);
446 set_type_state(ir_type, layout_fixed);
451 static ir_node *expression_to_firm(const expression_t *expression);
452 static inline ir_mode *get_ir_mode(type_t *type);
454 static ir_type *create_enum_type(enum_type_t *const type)
456 type->type.firm_type = ir_type_int;
458 ir_mode *const mode = get_ir_mode((type_t*) type);
459 tarval *const one = get_mode_one(mode);
460 tarval * tv_next = get_tarval_null(mode);
462 declaration_t *declaration = type->declaration->next;
463 for (; declaration != NULL; declaration = declaration->next) {
464 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
467 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
469 expression_t *const init = declaration->init.enum_value;
471 ir_node *const cnst = expression_to_firm(init);
472 if (!is_Const(cnst)) {
473 panic("couldn't fold constant");
475 tv_next = get_Const_tarval(cnst);
477 declaration->v.enum_val = tv_next;
478 tv_next = tarval_add(tv_next, one);
484 static ir_type *get_ir_type(type_t *type)
486 assert(type != NULL);
488 type = skip_typeref(type);
490 if(type->base.firm_type != NULL) {
491 assert(type->base.firm_type != INVALID_TYPE);
492 return type->base.firm_type;
495 ir_type *firm_type = NULL;
498 firm_type = create_atomic_type(&type->atomic);
501 firm_type = create_method_type(&type->function);
504 firm_type = create_pointer_type(&type->pointer);
507 firm_type = create_array_type(&type->array);
509 case TYPE_COMPOUND_STRUCT:
510 firm_type = create_struct_type(&type->compound);
512 case TYPE_COMPOUND_UNION:
513 firm_type = create_union_type(&type->compound);
516 firm_type = create_enum_type(&type->enumt);
519 firm_type = get_ir_type(type->builtin.real_type);
526 if(firm_type == NULL)
527 panic("unknown type found");
529 type->base.firm_type = firm_type;
533 static inline ir_mode *get_ir_mode(type_t *type)
535 ir_type *irtype = get_ir_type(type);
537 /* firm doesn't report a mode for arrays somehow... */
538 if(is_Array_type(irtype)) {
542 ir_mode *mode = get_type_mode(irtype);
543 assert(mode != NULL);
547 static ir_entity* get_function_entity(declaration_t *declaration)
549 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
550 return declaration->v.entity;
551 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
553 symbol_t *symbol = declaration->symbol;
554 ident *id = new_id_from_str(symbol->string);
556 ir_type *global_type = get_glob_type();
557 ir_type *ir_type_method = get_ir_type(declaration->type);
558 assert(is_Method_type(ir_type_method));
560 ir_entity *entity = new_entity(global_type, id, ir_type_method);
561 set_entity_ld_ident(entity, id);
562 if(declaration->storage_class == STORAGE_CLASS_STATIC
563 || declaration->is_inline) {
564 set_entity_visibility(entity, visibility_local);
565 } else if(declaration->init.statement != NULL) {
566 set_entity_visibility(entity, visibility_external_visible);
568 set_entity_visibility(entity, visibility_external_allocated);
570 set_entity_allocation(entity, allocation_static);
572 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
573 declaration->v.entity = entity;
578 static dbg_info *get_dbg_info(const source_position_t *pos)
580 return (dbg_info*) pos;
583 static ir_node *const_to_firm(const const_expression_t *cnst)
585 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
586 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
591 if(mode_is_float(mode)) {
592 tv = new_tarval_from_double(cnst->v.float_value, mode);
594 if(mode_is_signed(mode)) {
595 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
597 len = snprintf(buf, sizeof(buf), "%llu",
598 (unsigned long long) cnst->v.int_value);
600 tv = new_tarval_from_str(buf, len, mode);
603 return new_d_Const(dbgi, mode, tv);
606 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
608 assert(entity != NULL);
609 union symconst_symbol sym;
610 sym.entity_p = entity;
611 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
614 static ir_node *string_to_firm(const source_position_t *const src_pos,
615 const char *const id_prefix,
616 const char *const string)
618 ir_type *const global_type = get_glob_type();
619 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
622 ident *const id = unique_ident(id_prefix);
623 ir_entity *const entity = new_entity(global_type, id, type);
624 set_entity_ld_ident(entity, id);
625 set_entity_variability(entity, variability_constant);
626 set_entity_allocation(entity, allocation_static);
628 ir_type *const elem_type = ir_type_const_char;
629 ir_mode *const mode = get_type_mode(elem_type);
631 const size_t slen = strlen(string) + 1;
633 set_array_lower_bound_int(type, 0, 0);
634 set_array_upper_bound_int(type, 0, slen);
635 set_type_size_bytes(type, slen);
636 set_type_state(type, layout_fixed);
638 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
639 for(size_t i = 0; i < slen; ++i) {
640 tvs[i] = new_tarval_from_long(string[i], mode);
643 set_array_entity_values(entity, tvs, slen);
646 dbg_info *const dbgi = get_dbg_info(src_pos);
648 return create_symconst(dbgi, entity);
651 static ir_node *string_literal_to_firm(
652 const string_literal_expression_t* literal)
654 return string_to_firm(&literal->expression.source_position, "Lstr",
658 static ir_node *wide_string_literal_to_firm(
659 const wide_string_literal_expression_t* const literal)
661 ir_type *const global_type = get_glob_type();
662 ir_type *const elem_type = ir_type_wchar_t;
663 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
666 ident *const id = unique_ident("Lstr");
667 ir_entity *const entity = new_entity(global_type, id, type);
668 set_entity_ld_ident(entity, id);
669 set_entity_variability(entity, variability_constant);
670 set_entity_allocation(entity, allocation_static);
672 ir_mode *const mode = get_type_mode(elem_type);
674 const wchar_rep_t *const string = literal->value.begin;
675 const size_t slen = literal->value.size;
677 set_array_lower_bound_int(type, 0, 0);
678 set_array_upper_bound_int(type, 0, slen);
679 set_type_size_bytes(type, slen);
680 set_type_state(type, layout_fixed);
682 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
683 for(size_t i = 0; i < slen; ++i) {
684 tvs[i] = new_tarval_from_long(string[i], mode);
687 set_array_entity_values(entity, tvs, slen);
690 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
692 return create_symconst(dbgi, entity);
695 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
696 dbg_info *const dbgi)
698 if(is_compound_type(irtype) || is_Array_type(irtype)) {
702 ir_mode *const mode = get_type_mode(irtype);
703 ir_node *const memory = get_store();
704 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
705 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
706 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
711 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
713 ir_mode *mode = get_irn_mode(node);
715 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
717 if(!mode_is_float(mode))
720 /* check if there is already a Conv */
721 if (get_irn_op(node) == op_Conv) {
722 /* convert it into a strict Conv */
723 set_Conv_strict(node, 1);
727 /* otherwise create a new one */
728 return new_d_strictConv(dbgi, node, mode);
731 static ir_node *get_global_var_address(dbg_info *const dbgi,
732 const declaration_t *const decl)
734 assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
736 ir_entity *const entity = decl->v.entity;
737 switch ((storage_class_tag_t)decl->storage_class) {
738 case STORAGE_CLASS_THREAD:
739 case STORAGE_CLASS_THREAD_EXTERN:
740 case STORAGE_CLASS_THREAD_STATIC: {
741 ir_node *const no_mem = new_NoMem();
742 ir_node *const tls = get_irg_tls(current_ir_graph);
743 return new_d_simpleSel(dbgi, no_mem, tls, entity);
747 return create_symconst(dbgi, entity);
751 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
753 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
754 declaration_t *declaration = ref->declaration;
755 type_t *type = skip_typeref(declaration->type);
757 switch((declaration_type_t) declaration->declaration_type) {
758 case DECLARATION_TYPE_UNKNOWN:
759 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
765 case DECLARATION_TYPE_ENUM_ENTRY: {
766 ir_mode *const mode = get_ir_mode(type);
767 return new_Const(mode, declaration->v.enum_val);
770 case DECLARATION_TYPE_LOCAL_VARIABLE: {
771 ir_mode *mode = get_ir_mode(type);
772 return get_value(declaration->v.value_number, mode);
774 case DECLARATION_TYPE_FUNCTION: {
775 return create_symconst(dbgi, declaration->v.entity);
777 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
778 ir_node *const addr = get_global_var_address(dbgi, declaration);
779 ir_type *const irtype = get_entity_type(declaration->v.entity);
780 return deref_address(irtype, addr, dbgi);
783 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
784 ir_entity *entity = declaration->v.entity;
785 ir_node *frame = get_irg_frame(current_ir_graph);
786 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
787 ir_type *irtype = get_entity_type(entity);
788 return deref_address(irtype, sel, dbgi);
791 case DECLARATION_TYPE_COMPOUND_MEMBER:
792 case DECLARATION_TYPE_LABEL_BLOCK:
793 panic("not implemented reference type");
796 panic("reference to declaration with unknown type found");
799 static ir_node *reference_addr(const reference_expression_t *ref)
801 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
802 declaration_t *declaration = ref->declaration;
804 switch((declaration_type_t) declaration->declaration_type) {
805 case DECLARATION_TYPE_UNKNOWN:
807 case DECLARATION_TYPE_LOCAL_VARIABLE:
808 panic("local variable without entity has no address");
809 case DECLARATION_TYPE_FUNCTION: {
810 return create_symconst(dbgi, declaration->v.entity);
812 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
813 ir_node *const addr = get_global_var_address(dbgi, declaration);
816 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
817 ir_entity *entity = declaration->v.entity;
818 ir_node *frame = get_irg_frame(current_ir_graph);
819 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
824 case DECLARATION_TYPE_ENUM_ENTRY:
825 panic("trying to reference enum entry");
827 case DECLARATION_TYPE_COMPOUND_MEMBER:
828 case DECLARATION_TYPE_LABEL_BLOCK:
829 panic("not implemented reference type");
832 panic("reference to declaration with unknown type found");
835 static ir_node *process_builtin_call(const call_expression_t *call)
837 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
839 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
840 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
842 type_t *type = skip_typeref(builtin->expression.datatype);
843 assert(is_type_pointer(type));
845 type_t *function_type = skip_typeref(type->pointer.points_to);
846 symbol_t *symbol = builtin->symbol;
849 case T___builtin_alloca: {
850 if(call->arguments == NULL || call->arguments->next != NULL) {
851 panic("invalid number of parameters on __builtin_alloca");
853 expression_t *argument = call->arguments->expression;
854 ir_node *size = expression_to_firm(argument);
856 ir_node *store = get_store();
857 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
859 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
861 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
865 case T___builtin_nan:
866 case T___builtin_nanf:
867 case T___builtin_nand: {
868 /* Ignore string for now... */
869 assert(is_type_function(function_type));
870 ir_mode *mode = get_ir_mode(function_type->function.return_type);
871 tarval *tv = get_mode_NAN(mode);
872 ir_node *res = new_d_Const(dbgi, mode, tv);
875 case T___builtin_va_end:
878 panic("Unsupported builtin found\n");
882 static ir_node *call_expression_to_firm(const call_expression_t *call)
884 assert(get_cur_block() != NULL);
886 expression_t *function = call->function;
887 if(function->type == EXPR_BUILTIN_SYMBOL) {
888 return process_builtin_call(call);
890 ir_node *callee = expression_to_firm(function);
892 type_t *type = skip_typeref(function->base.datatype);
893 assert(is_type_pointer(type));
894 pointer_type_t *pointer_type = &type->pointer;
895 type_t *points_to = skip_typeref(pointer_type->points_to);
896 assert(is_type_function(points_to));
897 function_type_t *function_type = &points_to->function;
899 int n_parameters = 0;
900 call_argument_t *argument = call->arguments;
901 for( ; argument != NULL; argument = argument->next) {
905 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
907 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
908 ir_type *new_method_type = NULL;
909 if(function_type->variadic || function_type->unspecified_parameters) {
910 /* we need to construct a new method type matching the call
912 int n_res = get_method_n_ress(ir_method_type);
913 new_method_type = new_type_method(unique_ident("calltype"),
914 n_parameters, n_res);
915 set_method_calling_convention(new_method_type,
916 get_method_calling_convention(ir_method_type));
917 set_method_additional_properties(new_method_type,
918 get_method_additional_properties(ir_method_type));
920 for(int i = 0; i < n_res; ++i) {
921 set_method_res_type(new_method_type, i,
922 get_method_res_type(ir_method_type, i));
925 ir_node *in[n_parameters];
927 argument = call->arguments;
929 for( ; argument != NULL; argument = argument->next) {
930 expression_t *expression = argument->expression;
931 ir_node *arg_node = expression_to_firm(expression);
933 arg_node = do_strict_conv(dbgi, arg_node);
936 if(new_method_type != NULL) {
937 ir_type *irtype = get_ir_type(expression->base.datatype);
938 set_method_param_type(new_method_type, n, irtype);
943 assert(n == n_parameters);
945 if(new_method_type != NULL)
946 ir_method_type = new_method_type;
948 ir_node *store = get_store();
949 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
951 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
954 type_t *return_type = skip_typeref(function_type->return_type);
955 ir_node *result = NULL;
957 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
959 if(is_type_scalar(return_type)) {
960 mode = get_ir_mode(return_type);
964 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
965 result = new_d_Proj(dbgi, resproj, mode, 0);
971 static void statement_to_firm(statement_t *statement);
972 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
974 static ir_node *expression_to_addr(const expression_t *expression);
975 static void create_condition_evaluation(const expression_t *expression,
977 ir_node *false_block);
979 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
982 value = do_strict_conv(dbgi, value);
984 ir_node *memory = get_store();
986 if(is_type_scalar(type)) {
987 ir_node *store = new_d_Store(dbgi, memory, addr, value);
988 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
989 set_store(store_mem);
991 ir_type *irtype = get_ir_type(type);
992 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
993 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
994 set_store(copyb_mem);
998 static void set_value_for_expression(const expression_t *expression,
1001 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1002 value = do_strict_conv(dbgi, value);
1004 if(expression->type == EXPR_REFERENCE) {
1005 reference_expression_t *ref = (reference_expression_t*) expression;
1007 declaration_t *declaration = ref->declaration;
1008 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
1009 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1010 set_value(declaration->v.value_number, value);
1015 ir_node *addr = expression_to_addr(expression);
1016 type_t *type = skip_typeref(expression->base.datatype);
1017 assign_value(dbgi, addr, type, value);
1020 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1022 ir_mode *value_mode = get_irn_mode(value);
1024 if (value_mode == dest_mode || is_Bad(value))
1027 if(dest_mode == mode_b) {
1028 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1029 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1030 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1034 return new_d_Conv(dbgi, value, dest_mode);
1037 static ir_node *create_incdec(const unary_expression_t *expression)
1039 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1040 type_t *type = skip_typeref(expression->expression.datatype);
1041 ir_mode *mode = get_ir_mode(type);
1042 expression_t *value = expression->value;
1044 ir_node *value_node = expression_to_firm(value);
1047 if(is_type_pointer(type)) {
1048 pointer_type_t *pointer_type = &type->pointer;
1049 unsigned elem_size = get_type_size(pointer_type->points_to);
1050 offset = new_Const_long(mode_Is, elem_size);
1052 assert(is_type_arithmetic(type));
1053 offset = new_Const(mode, get_mode_one(mode));
1056 switch(expression->expression.type) {
1057 case EXPR_UNARY_POSTFIX_INCREMENT: {
1058 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1059 set_value_for_expression(value, new_value);
1062 case EXPR_UNARY_POSTFIX_DECREMENT: {
1063 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1064 set_value_for_expression(value, new_value);
1067 case EXPR_UNARY_PREFIX_INCREMENT: {
1068 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1069 set_value_for_expression(value, new_value);
1072 case EXPR_UNARY_PREFIX_DECREMENT: {
1073 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1074 set_value_for_expression(value, new_value);
1078 panic("no incdec expr in create_incdec");
1083 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1085 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1086 type_t *type = skip_typeref(expression->expression.datatype);
1087 ir_mode *mode = get_ir_mode(type);
1089 if(expression->expression.type == EXPR_UNARY_TAKE_ADDRESS)
1090 return expression_to_addr(expression->value);
1092 const expression_t *value = expression->value;
1093 ir_node *value_node = expression_to_firm(value);
1095 switch(expression->expression.type) {
1096 case EXPR_UNARY_NEGATE:
1097 return new_d_Minus(dbgi, value_node, mode);
1098 case EXPR_UNARY_PLUS:
1100 case EXPR_UNARY_BITWISE_NEGATE:
1101 return new_d_Not(dbgi, value_node, mode);
1102 case EXPR_UNARY_NOT: {
1103 if(get_irn_mode(value_node) != mode_b) {
1104 value_node = create_conv(dbgi, value_node, mode_b);
1106 value_node = new_d_Not(dbgi, value_node, mode_b);
1107 if(mode != mode_b) {
1108 value_node = create_conv(dbgi, value_node, mode);
1112 case EXPR_UNARY_DEREFERENCE: {
1113 type_t *value_type = skip_typeref(value->base.datatype);
1114 ir_type *irtype = get_ir_type(value_type);
1115 assert(is_Pointer_type(irtype));
1116 ir_type *points_to = get_pointer_points_to_type(irtype);
1117 return deref_address(points_to, value_node, dbgi);
1119 case EXPR_UNARY_POSTFIX_INCREMENT:
1120 case EXPR_UNARY_POSTFIX_DECREMENT:
1121 case EXPR_UNARY_PREFIX_INCREMENT:
1122 case EXPR_UNARY_PREFIX_DECREMENT:
1123 return create_incdec(expression);
1124 case EXPR_UNARY_CAST: {
1125 ir_node *node = create_conv(dbgi, value_node, get_ir_mode(type));
1126 node = do_strict_conv(dbgi, node);
1129 case EXPR_UNARY_CAST_IMPLICIT:
1130 return create_conv(dbgi, value_node, get_ir_mode(type));
1135 panic("invalid UNEXPR type found");
1138 static long get_pnc(const expression_type_t type)
1141 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1142 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1143 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Ne;
1144 case EXPR_BINARY_ISLESS:
1145 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1146 case EXPR_BINARY_ISLESSEQUAL:
1147 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1148 case EXPR_BINARY_ISGREATER:
1149 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1150 case EXPR_BINARY_ISGREATEREQUAL:
1151 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1152 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1157 panic("trying to get pn_Cmp from non-comparison binexpr type");
1160 static ir_node *create_lazy_op(const binary_expression_t *expression)
1162 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1163 type_t *type = expression->expression.datatype;
1164 ir_mode *mode = get_ir_mode(type);
1166 ir_node *cur_block = get_cur_block();
1168 ir_node *one_block = new_immBlock();
1169 ir_node *one = new_Const(mode, get_mode_one(mode));
1170 ir_node *jmp_one = new_d_Jmp(dbgi);
1172 ir_node *zero_block = new_immBlock();
1173 ir_node *zero = new_Const(mode, get_mode_null(mode));
1174 ir_node *jmp_zero = new_d_Jmp(dbgi);
1176 set_cur_block(cur_block);
1177 create_condition_evaluation((const expression_t*) expression,
1178 one_block, zero_block);
1179 mature_immBlock(one_block);
1180 mature_immBlock(zero_block);
1182 ir_node *common_block = new_immBlock();
1183 add_immBlock_pred(common_block, jmp_one);
1184 add_immBlock_pred(common_block, jmp_zero);
1185 mature_immBlock(common_block);
1187 ir_node *in[2] = { one, zero };
1188 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1193 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1194 ir_node *right, ir_mode *mode);
1196 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1197 create_arithmetic_func func)
1199 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1200 ir_node *left = expression_to_firm(expression->left);
1201 ir_node *right = expression_to_firm(expression->right);
1202 type_t *type = expression->right->base.datatype;
1203 /* be careful with the modes, because in arithmetic assign nodes only
1204 * the right operand has the mode of the arithmetic already */
1205 ir_mode *mode = get_ir_mode(type);
1206 left = create_conv(dbgi, left, mode);
1207 ir_node *res = func(dbgi, left, right, mode);
1212 static ir_node *pointer_arithmetic(ir_node *const pointer,
1215 dbg_info *const dbgi,
1216 const create_arithmetic_func func)
1218 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1219 type_t *const points_to = pointer_type->points_to;
1220 const unsigned elem_size = get_type_size(points_to);
1222 assert(elem_size >= 1);
1223 if (elem_size > 1) {
1224 integer = create_conv(dbgi, integer, mode_Is);
1225 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1226 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1230 ir_mode *const mode = get_ir_mode(type);
1231 return func(dbgi, pointer, integer, mode);
1234 static ir_node *create_arithmetic_assign_binop(
1235 const binary_expression_t *expression, create_arithmetic_func func)
1237 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1238 type_t *const type = skip_typeref(expression->expression.datatype);
1241 if (is_type_pointer(type)) {
1242 ir_node *const pointer = expression_to_firm(expression->left);
1243 ir_node * integer = expression_to_firm(expression->right);
1244 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1246 value = create_arithmetic_binop(expression, func);
1249 ir_mode *const mode = get_ir_mode(type);
1250 value = create_conv(dbgi, value, mode);
1251 set_value_for_expression(expression->left, value);
1256 static ir_node *create_add(const binary_expression_t *expression)
1258 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1259 ir_node *left = expression_to_firm(expression->left);
1260 ir_node *right = expression_to_firm(expression->right);
1261 type_t *type = expression->expression.datatype;
1263 expression_t *expr_left = expression->left;
1264 expression_t *expr_right = expression->right;
1265 type_t *type_left = skip_typeref(expr_left->base.datatype);
1266 type_t *type_right = skip_typeref(expr_right->base.datatype);
1268 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1269 ir_mode *const mode = get_ir_mode(type);
1270 return new_d_Add(dbgi, left, right, mode);
1273 if (is_type_pointer(type_left)) {
1274 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1276 assert(is_type_pointer(type_right));
1277 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1281 static ir_node *create_sub(const binary_expression_t *expression)
1283 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1284 expression_t *const expr_left = expression->left;
1285 expression_t *const expr_right = expression->right;
1286 ir_node *const left = expression_to_firm(expr_left);
1287 ir_node *const right = expression_to_firm(expr_right);
1288 type_t *const type = expression->expression.datatype;
1289 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1290 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1292 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1293 ir_mode *const mode = get_ir_mode(type);
1294 return new_d_Sub(dbgi, left, right, mode);
1295 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1296 const pointer_type_t *const ptr_type = &type_left->pointer;
1297 const unsigned elem_size = get_type_size(ptr_type->points_to);
1298 ir_mode *const mode = get_ir_mode(type);
1299 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1300 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1301 ir_node *const no_mem = new_NoMem();
1302 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1303 op_pin_state_floats);
1304 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1307 assert(is_type_pointer(type_left));
1308 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1311 static ir_node *create_shift(const binary_expression_t *expression)
1313 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1314 ir_node *left = expression_to_firm(expression->left);
1315 ir_node *right = expression_to_firm(expression->right);
1316 type_t *type = expression->expression.datatype;
1317 ir_mode *mode = get_ir_mode(type);
1319 /* firm always wants the shift count to be unsigned */
1320 right = create_conv(dbgi, right, mode_Iu);
1324 switch(expression->expression.type) {
1325 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1326 case EXPR_BINARY_SHIFTLEFT:
1327 res = new_d_Shl(dbgi, left, right, mode);
1329 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1330 case EXPR_BINARY_SHIFTRIGHT: {
1331 expression_t *expr_left = expression->left;
1332 type_t *type_left = skip_typeref(expr_left->base.datatype);
1334 if(is_type_signed(type_left)) {
1335 res = new_d_Shrs(dbgi, left, right, mode);
1337 res = new_d_Shr(dbgi, left, right, mode);
1342 panic("create shift op called for non-shift op");
1349 static ir_node *create_divmod(const binary_expression_t *expression)
1351 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1352 ir_node *left = expression_to_firm(expression->left);
1353 ir_node *right = expression_to_firm(expression->right);
1354 ir_node *pin = new_Pin(new_NoMem());
1355 /* be careful with the modes, because in arithmetic assign nodes only
1356 * the right operand has the mode of the arithmetic already */
1357 type_t *type = expression->right->base.datatype;
1358 ir_mode *mode = get_ir_mode(type);
1359 left = create_conv(dbgi, left, mode);
1363 switch (expression->expression.type) {
1364 case EXPR_BINARY_DIV:
1365 case EXPR_BINARY_DIV_ASSIGN:
1366 if(mode_is_float(mode)) {
1367 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1368 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1370 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1371 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1375 case EXPR_BINARY_MOD:
1376 case EXPR_BINARY_MOD_ASSIGN:
1377 assert(!mode_is_float(mode));
1378 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1379 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1382 default: panic("unexpected binary expression type in create_divmod()");
1388 static ir_node *create_arithmetic_assign_divmod(
1389 const binary_expression_t *expression)
1391 ir_node * value = create_divmod(expression);
1392 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1393 type_t *const type = expression->expression.datatype;
1394 ir_mode *const mode = get_ir_mode(type);
1396 assert(type->type != TYPE_POINTER);
1398 value = create_conv(dbgi, value, mode);
1399 set_value_for_expression(expression->left, value);
1404 static ir_node *create_arithmetic_assign_shift(
1405 const binary_expression_t *expression)
1407 ir_node * value = create_shift(expression);
1408 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1409 type_t *const type = expression->expression.datatype;
1410 ir_mode *const mode = get_ir_mode(type);
1412 value = create_conv(dbgi, value, mode);
1413 set_value_for_expression(expression->left, value);
1418 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1420 expression_type_t type = expression->expression.type;
1423 case EXPR_BINARY_EQUAL:
1424 case EXPR_BINARY_NOTEQUAL:
1425 case EXPR_BINARY_LESS:
1426 case EXPR_BINARY_LESSEQUAL:
1427 case EXPR_BINARY_GREATER:
1428 case EXPR_BINARY_GREATEREQUAL:
1429 case EXPR_BINARY_ISGREATER:
1430 case EXPR_BINARY_ISGREATEREQUAL:
1431 case EXPR_BINARY_ISLESS:
1432 case EXPR_BINARY_ISLESSEQUAL:
1433 case EXPR_BINARY_ISLESSGREATER:
1434 case EXPR_BINARY_ISUNORDERED: {
1435 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1436 ir_node *left = expression_to_firm(expression->left);
1437 ir_node *right = expression_to_firm(expression->right);
1438 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1439 long pnc = get_pnc(type);
1440 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1443 case EXPR_BINARY_ASSIGN: {
1444 ir_node *right = expression_to_firm(expression->right);
1445 set_value_for_expression(expression->left, right);
1449 case EXPR_BINARY_ADD:
1450 return create_add(expression);
1451 case EXPR_BINARY_SUB:
1452 return create_sub(expression);
1453 case EXPR_BINARY_MUL:
1454 return create_arithmetic_binop(expression, new_d_Mul);
1455 case EXPR_BINARY_BITWISE_AND:
1456 return create_arithmetic_binop(expression, new_d_And);
1457 case EXPR_BINARY_BITWISE_OR:
1458 return create_arithmetic_binop(expression, new_d_Or);
1459 case EXPR_BINARY_BITWISE_XOR:
1460 return create_arithmetic_binop(expression, new_d_Eor);
1461 case EXPR_BINARY_SHIFTLEFT:
1462 case EXPR_BINARY_SHIFTRIGHT:
1463 return create_shift(expression);
1464 case EXPR_BINARY_DIV:
1465 case EXPR_BINARY_MOD:
1466 return create_divmod(expression);
1467 case EXPR_BINARY_LOGICAL_AND:
1468 case EXPR_BINARY_LOGICAL_OR:
1469 return create_lazy_op(expression);
1470 case EXPR_BINARY_COMMA:
1471 expression_to_firm(expression->left);
1472 return expression_to_firm(expression->right);
1473 case EXPR_BINARY_ADD_ASSIGN:
1474 return create_arithmetic_assign_binop(expression, new_d_Add);
1475 case EXPR_BINARY_SUB_ASSIGN:
1476 return create_arithmetic_assign_binop(expression, new_d_Sub);
1477 case EXPR_BINARY_MUL_ASSIGN:
1478 return create_arithmetic_assign_binop(expression, new_d_Mul);
1479 case EXPR_BINARY_DIV_ASSIGN:
1480 return create_arithmetic_assign_divmod(expression);
1481 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1482 return create_arithmetic_assign_binop(expression, new_d_And);
1483 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1484 return create_arithmetic_assign_binop(expression, new_d_Or);
1485 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1486 return create_arithmetic_assign_binop(expression, new_d_Eor);
1487 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1488 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1489 return create_arithmetic_assign_shift(expression);
1491 panic("TODO binexpr type");
1495 static ir_node *array_access_addr(const array_access_expression_t *expression)
1497 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1498 ir_node *base_addr = expression_to_firm(expression->array_ref);
1499 ir_node *offset = expression_to_firm(expression->index);
1500 offset = create_conv(dbgi, offset, mode_Iu);
1502 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1503 assert(is_type_pointer(ref_type));
1504 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1506 unsigned elem_size = get_type_size(pointer_type->points_to);
1507 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1508 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1510 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1515 static ir_node *array_access_to_firm(
1516 const array_access_expression_t *expression)
1518 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1519 ir_node *addr = array_access_addr(expression);
1520 type_t *type = revert_automatic_type_conversion(
1521 (const expression_t*) expression);
1522 type = skip_typeref(type);
1523 ir_type *irtype = get_ir_type(type);
1525 return deref_address(irtype, addr, dbgi);
1528 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1530 type_t *type = expression->type;
1532 type = expression->size_expression->base.datatype;
1533 assert(type != NULL);
1536 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1537 unsigned size = get_type_size(type);
1538 ir_node *size_node = new_Const_long(mode, size);
1543 static tarval *try_fold_constant(const expression_t *expression)
1545 ir_graph *old_current_ir_graph = current_ir_graph;
1546 if(current_ir_graph == NULL) {
1547 current_ir_graph = get_const_code_irg();
1550 ir_node *cnst = expression_to_firm(expression);
1551 current_ir_graph = old_current_ir_graph;
1553 if(!is_Const(cnst)) {
1557 tarval *tv = get_Const_tarval(cnst);
1558 if(!tarval_is_long(tv)) {
1565 static long fold_constant(const expression_t *expression)
1567 tarval *tv = try_fold_constant(expression);
1569 panic("couldn't fold constantl");
1572 return get_tarval_long(tv);
1575 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1577 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1579 /* first try to fold a constant condition */
1580 tarval *tv = try_fold_constant(expression->condition);
1582 long val = get_tarval_long(tv);
1584 return expression_to_firm(expression->true_expression);
1586 return expression_to_firm(expression->false_expression);
1590 ir_node *cur_block = get_cur_block();
1592 /* create the true block */
1593 ir_node *true_block = new_immBlock();
1595 ir_node *true_val = expression_to_firm(expression->true_expression);
1596 ir_node *true_jmp = new_Jmp();
1598 /* create the false block */
1599 ir_node *false_block = new_immBlock();
1601 ir_node *false_val = expression_to_firm(expression->false_expression);
1602 ir_node *false_jmp = new_Jmp();
1604 /* create the condition evaluation */
1605 set_cur_block(cur_block);
1606 create_condition_evaluation(expression->condition, true_block, false_block);
1607 mature_immBlock(true_block);
1608 mature_immBlock(false_block);
1610 /* create the common block */
1611 ir_node *common_block = new_immBlock();
1612 add_immBlock_pred(common_block, true_jmp);
1613 add_immBlock_pred(common_block, false_jmp);
1614 mature_immBlock(common_block);
1616 /* TODO improve static semantics, so either both or no values are NULL */
1617 if (true_val == NULL || false_val == NULL)
1620 ir_node *in[2] = { true_val, false_val };
1621 ir_mode *mode = get_irn_mode(true_val);
1622 assert(get_irn_mode(false_val) == mode);
1623 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1628 static ir_node *select_addr(const select_expression_t *expression)
1630 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1632 ir_node *compound_addr = expression_to_firm(expression->compound);
1634 declaration_t *entry = expression->compound_entry;
1635 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1636 ir_entity *entity = entry->v.entity;
1638 assert(entity != NULL);
1640 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1645 static ir_node *select_to_firm(const select_expression_t *expression)
1647 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1648 ir_node *addr = select_addr(expression);
1649 type_t *type = revert_automatic_type_conversion(
1650 (const expression_t*) expression);
1651 type = skip_typeref(type);
1652 ir_type *irtype = get_ir_type(type);
1654 return deref_address(irtype, addr, dbgi);
1657 /* Values returned by __builtin_classify_type. */
1658 typedef enum gcc_type_class
1664 enumeral_type_class,
1667 reference_type_class,
1671 function_type_class,
1682 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1684 const type_t *const type = expr->type_expression->base.datatype;
1690 const atomic_type_t *const atomic_type = &type->atomic;
1691 switch (atomic_type->atype) {
1692 // should not be reached
1693 case ATOMIC_TYPE_INVALID:
1697 // gcc cannot do that
1698 case ATOMIC_TYPE_VOID:
1699 tc = void_type_class;
1702 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1703 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1704 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1705 case ATOMIC_TYPE_SHORT:
1706 case ATOMIC_TYPE_USHORT:
1707 case ATOMIC_TYPE_INT:
1708 case ATOMIC_TYPE_UINT:
1709 case ATOMIC_TYPE_LONG:
1710 case ATOMIC_TYPE_ULONG:
1711 case ATOMIC_TYPE_LONGLONG:
1712 case ATOMIC_TYPE_ULONGLONG:
1713 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1714 tc = integer_type_class;
1717 case ATOMIC_TYPE_FLOAT:
1718 case ATOMIC_TYPE_DOUBLE:
1719 case ATOMIC_TYPE_LONG_DOUBLE:
1720 tc = real_type_class;
1723 #ifdef PROVIDE_COMPLEX
1724 case ATOMIC_TYPE_FLOAT_COMPLEX:
1725 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1726 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1727 tc = complex_type_class;
1729 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1730 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1731 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1732 tc = complex_type_class;
1737 panic("Unimplemented case in classify_type_to_firm().");
1742 case TYPE_ARRAY: // gcc handles this as pointer
1743 case TYPE_FUNCTION: // gcc handles this as pointer
1744 case TYPE_POINTER: tc = pointer_type_class; break;
1745 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1746 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1748 // gcc handles this as integer
1749 case TYPE_ENUM: tc = integer_type_class; break;
1752 panic("Unimplemented case in classify_type_to_firm().");
1755 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1756 ir_mode *const mode = mode_Is;
1757 tarval *const tv = new_tarval_from_long(tc, mode);
1758 return new_d_Const(dbgi, mode, tv);
1761 static ir_node *function_name_to_firm(
1762 const string_literal_expression_t *const expr)
1764 if (current_function_name == NULL) {
1765 const source_position_t *const src_pos =
1766 &expr->expression.source_position;
1767 const char *const name = current_function_decl->symbol->string;
1768 current_function_name = string_to_firm(src_pos, "__func__", name);
1771 return current_function_name;
1774 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1776 statement_t *statement = expr->statement;
1778 assert(statement->type == STATEMENT_COMPOUND);
1779 return compound_statement_to_firm((compound_statement_t*) statement);
1782 static ir_node *va_start_expression_to_firm(
1783 const va_start_expression_t *const expr)
1785 ir_type *const method_type = get_ir_type(current_function_decl->type);
1786 int const n = get_method_n_params(method_type) - 1;
1787 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
1788 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
1789 dbg_info *const dbgi =
1790 get_dbg_info(&expr->expression.source_position);
1791 ir_node *const no_mem = new_NoMem();
1792 ir_node *const arg_sel =
1793 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
1795 size_t const parm_size = get_type_size(expr->parameter->type);
1796 ir_node *const cnst = new_Const_long(mode_Iu, parm_size);
1797 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
1798 set_value_for_expression(expr->ap, add);
1803 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
1805 ir_type *const irtype = get_ir_type(expr->expression.datatype);
1806 ir_node *const ap = expression_to_firm(expr->ap);
1807 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1808 ir_node *const res = deref_address(irtype, ap, dbgi);
1810 size_t const parm_size = get_type_size(expr->expression.datatype);
1811 ir_node *const cnst = new_Const_long(mode_Iu, parm_size);
1812 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
1813 set_value_for_expression(expr->ap, add);
1818 static ir_node *dereference_addr(const unary_expression_t *const expression)
1820 assert(expression->expression.type == EXPR_UNARY_DEREFERENCE);
1821 return expression_to_firm(expression->value);
1824 static ir_node *expression_to_addr(const expression_t *expression)
1826 switch(expression->type) {
1827 case EXPR_REFERENCE:
1828 return reference_addr(&expression->reference);
1829 case EXPR_ARRAY_ACCESS:
1830 return array_access_addr(&expression->array_access);
1832 return select_addr(&expression->select);
1834 return call_expression_to_firm(&expression->call);
1835 case EXPR_UNARY_DEREFERENCE: {
1836 return dereference_addr(&expression->unary);
1841 panic("trying to get address of non-lvalue");
1844 static ir_node *_expression_to_firm(const expression_t *expression)
1846 switch(expression->type) {
1848 return const_to_firm(&expression->conste);
1849 case EXPR_STRING_LITERAL:
1850 return string_literal_to_firm(&expression->string);
1851 case EXPR_WIDE_STRING_LITERAL:
1852 return wide_string_literal_to_firm(&expression->wide_string);
1853 case EXPR_REFERENCE:
1854 return reference_expression_to_firm(&expression->reference);
1856 return call_expression_to_firm(&expression->call);
1858 return unary_expression_to_firm(&expression->unary);
1860 return binary_expression_to_firm(&expression->binary);
1861 case EXPR_ARRAY_ACCESS:
1862 return array_access_to_firm(&expression->array_access);
1864 return sizeof_to_firm(&expression->sizeofe);
1865 case EXPR_CONDITIONAL:
1866 return conditional_to_firm(&expression->conditional);
1868 return select_to_firm(&expression->select);
1869 case EXPR_CLASSIFY_TYPE:
1870 return classify_type_to_firm(&expression->classify_type);
1872 case EXPR_PRETTY_FUNCTION:
1873 return function_name_to_firm(&expression->string);
1874 case EXPR_STATEMENT:
1875 return statement_expression_to_firm(&expression->statement);
1877 return va_start_expression_to_firm(&expression->va_starte);
1879 return va_arg_expression_to_firm(&expression->va_arge);
1881 case EXPR_BUILTIN_SYMBOL:
1882 panic("unimplemented expression found");
1888 panic("invalid expression found");
1891 static ir_node *expression_to_firm(const expression_t *expression)
1893 ir_node *res = _expression_to_firm(expression);
1895 if(res != NULL && get_irn_mode(res) == mode_b) {
1896 ir_mode *mode = get_ir_mode(expression->base.datatype);
1897 res = create_conv(NULL, res, mode);
1903 static ir_node *expression_to_modeb(const expression_t *expression)
1905 ir_node *res = _expression_to_firm(expression);
1906 res = create_conv(NULL, res, mode_b);
1912 * create a short-circuit expression evaluation that tries to construct
1913 * efficient control flow structures for &&, || and ! expressions
1915 static void create_condition_evaluation(const expression_t *expression,
1916 ir_node *true_block,
1917 ir_node *false_block)
1919 switch(expression->type) {
1920 case EXPR_UNARY_NOT: {
1921 const unary_expression_t *unary_expression = &expression->unary;
1922 create_condition_evaluation(unary_expression->value, false_block,
1926 case EXPR_BINARY_LOGICAL_AND: {
1927 const binary_expression_t *binary_expression = &expression->binary;
1929 ir_node *cur_block = get_cur_block();
1930 ir_node *extra_block = new_immBlock();
1931 set_cur_block(cur_block);
1932 create_condition_evaluation(binary_expression->left, extra_block,
1934 mature_immBlock(extra_block);
1935 set_cur_block(extra_block);
1936 create_condition_evaluation(binary_expression->right, true_block,
1940 case EXPR_BINARY_LOGICAL_OR: {
1941 const binary_expression_t *binary_expression = &expression->binary;
1943 ir_node *cur_block = get_cur_block();
1944 ir_node *extra_block = new_immBlock();
1945 set_cur_block(cur_block);
1946 create_condition_evaluation(binary_expression->left, true_block,
1948 mature_immBlock(extra_block);
1949 set_cur_block(extra_block);
1950 create_condition_evaluation(binary_expression->right, true_block,
1958 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1959 ir_node *condition = expression_to_modeb(expression);
1960 ir_node *cond = new_d_Cond(dbgi, condition);
1961 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1962 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1964 add_immBlock_pred(true_block, true_proj);
1965 add_immBlock_pred(false_block, false_proj);
1967 set_cur_block(NULL);
1972 static void create_declaration_entity(declaration_t *declaration,
1973 declaration_type_t declaration_type,
1974 ir_type *parent_type)
1976 ident *id = new_id_from_str(declaration->symbol->string);
1977 ir_type *irtype = get_ir_type(declaration->type);
1978 ir_entity *entity = new_entity(parent_type, id, irtype);
1979 set_entity_ld_ident(entity, id);
1981 declaration->declaration_type = (unsigned char) declaration_type;
1982 declaration->v.entity = entity;
1983 set_entity_variability(entity, variability_uninitialized);
1984 if(parent_type == get_tls_type())
1985 set_entity_allocation(entity, allocation_automatic);
1986 else if(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE)
1987 set_entity_allocation(entity, allocation_static);
1988 /* TODO: visibility? */
1991 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
1993 enum compound_graph_entry_type_t {
1994 COMPOUND_GRAPH_ENTRY_ARRAY,
1995 COMPOUND_GRAPH_ENTRY_COMPOUND
1998 struct compound_graph_path_entry_t {
2004 compound_graph_path_entry_t *prev;
2007 static void create_initializer_object(initializer_t *initializer, type_t *type,
2008 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2010 static compound_graph_path *create_compound_path(ir_type *type,
2011 compound_graph_path_entry_t *entry, int len)
2013 compound_graph_path *path = new_compound_graph_path(type, len);
2016 for( ; entry != NULL; entry = entry->prev, --i) {
2018 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2019 set_compound_graph_path_node(path, i, entry->v.entity);
2021 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2022 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2030 static void create_initializer_value(initializer_value_t *initializer,
2032 compound_graph_path_entry_t *entry,
2035 ir_node *node = expression_to_firm(initializer->value);
2036 ir_type *type = get_entity_type(entity);
2037 compound_graph_path *path = create_compound_path(type, entry, len);
2038 add_compound_ent_value_w_path(entity, node, path);
2041 static void create_initializer_compound(initializer_list_t *initializer,
2042 compound_type_t *type,
2044 compound_graph_path_entry_t *last_entry,
2047 declaration_t *compound_declaration = type->declaration;
2049 declaration_t *compound_entry = compound_declaration->context.declarations;
2051 compound_graph_path_entry_t entry;
2052 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2053 entry.prev = last_entry;
2057 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2058 if(compound_entry->symbol == NULL)
2060 if(compound_entry->namespc != NAMESPACE_NORMAL)
2063 if(i >= initializer->len)
2066 entry.v.entity = compound_entry->v.entity;
2068 initializer_t *sub_initializer = initializer->initializers[i];
2070 assert(compound_entry != NULL);
2071 assert(compound_entry->declaration_type
2072 == DECLARATION_TYPE_COMPOUND_MEMBER);
2074 if(sub_initializer->type == INITIALIZER_VALUE) {
2075 create_initializer_value(&sub_initializer->value,
2076 entity, &entry, len);
2078 type_t *entry_type = skip_typeref(compound_entry->type);
2079 create_initializer_object(sub_initializer, entry_type, entity,
2087 static void create_initializer_array(initializer_list_t *initializer,
2088 array_type_t *type, ir_entity *entity,
2089 compound_graph_path_entry_t *last_entry,
2092 type_t *element_type = type->element_type;
2093 element_type = skip_typeref(element_type);
2095 compound_graph_path_entry_t entry;
2096 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2097 entry.prev = last_entry;
2101 for(i = 0; i < initializer->len; ++i) {
2102 entry.v.array_index = i;
2104 initializer_t *sub_initializer = initializer->initializers[i];
2106 if(sub_initializer->type == INITIALIZER_VALUE) {
2107 create_initializer_value(&sub_initializer->value,
2108 entity, &entry, len);
2110 create_initializer_object(sub_initializer, element_type, entity,
2116 /* TODO: initialize rest... */
2117 if(type->size_expression != NULL) {
2118 size_t array_len = fold_constant(type->size_expression);
2119 for( ; i < array_len; ++i) {
2126 static void create_initializer_string(initializer_string_t *initializer,
2127 array_type_t *type, ir_entity *entity,
2128 compound_graph_path_entry_t *last_entry,
2131 type_t *element_type = type->element_type;
2132 element_type = skip_typeref(element_type);
2134 compound_graph_path_entry_t entry;
2135 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2136 entry.prev = last_entry;
2139 ir_type *irtype = get_entity_type(entity);
2140 size_t arr_len = get_array_type_size(type);
2141 const char *p = initializer->string;
2143 for(i = 0; i < arr_len; ++i, ++p) {
2144 entry.v.array_index = i;
2146 ir_node *node = new_Const_long(mode_Bs, *p);
2147 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2148 add_compound_ent_value_w_path(entity, node, path);
2155 static void create_initializer_wide_string(
2156 const initializer_wide_string_t *const initializer, array_type_t *const type,
2157 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2160 type_t *element_type = type->element_type;
2161 element_type = skip_typeref(element_type);
2163 compound_graph_path_entry_t entry;
2164 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2165 entry.prev = last_entry;
2168 ir_type *const irtype = get_entity_type(entity);
2169 const size_t arr_len = get_array_type_size(type);
2170 const wchar_rep_t * p = initializer->string.begin;
2171 const wchar_rep_t *const end = p + initializer->string.size;
2172 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2173 entry.v.array_index = i;
2175 ir_node *node = new_Const_long(mode_Is, *p);
2176 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2177 add_compound_ent_value_w_path(entity, node, path);
2181 static void create_initializer_object(initializer_t *initializer, type_t *type,
2182 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2184 if(is_type_array(type)) {
2185 array_type_t *array_type = &type->array;
2187 switch (initializer->type) {
2188 case INITIALIZER_STRING: {
2189 initializer_string_t *const string = &initializer->string;
2190 create_initializer_string(string, array_type, entity, entry, len);
2194 case INITIALIZER_WIDE_STRING: {
2195 initializer_wide_string_t *const string = &initializer->wide_string;
2196 create_initializer_wide_string(string, array_type, entity, entry, len);
2200 case INITIALIZER_LIST: {
2201 initializer_list_t *const list = &initializer->list;
2202 create_initializer_array(list, array_type, entity, entry, len);
2206 case INITIALIZER_VALUE:
2209 panic("Unhandled initializer");
2211 assert(initializer->type == INITIALIZER_LIST);
2212 initializer_list_t *list = &initializer->list;
2214 assert(is_type_compound(type));
2215 compound_type_t *compound_type = &type->compound;
2216 create_initializer_compound(list, compound_type, entity, entry, len);
2220 static void create_initializer_local_variable_entity(declaration_t *declaration)
2222 initializer_t *initializer = declaration->init.initializer;
2223 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2224 ir_entity *entity = declaration->v.entity;
2225 ir_node *memory = get_store();
2226 ir_node *nomem = new_NoMem();
2227 ir_node *frame = get_irg_frame(current_ir_graph);
2228 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2230 if(initializer->type == INITIALIZER_VALUE) {
2231 initializer_value_t *initializer_value = &initializer->value;
2233 ir_node *value = expression_to_firm(initializer_value->value);
2234 type_t *type = skip_typeref(declaration->type);
2235 assign_value(dbgi, addr, type, value);
2239 /* create a "template" entity which is copied to the entity on the stack */
2240 ident *id = unique_ident("initializer");
2241 ir_type *irtype = get_ir_type(declaration->type);
2242 ir_type *global_type = get_glob_type();
2243 ir_entity *init_entity = new_entity(global_type, id, irtype);
2244 set_entity_ld_ident(init_entity, id);
2246 set_entity_variability(init_entity, variability_initialized);
2247 set_entity_visibility(init_entity, visibility_local);
2248 set_entity_allocation(init_entity, allocation_static);
2250 ir_graph *old_current_ir_graph = current_ir_graph;
2251 current_ir_graph = get_const_code_irg();
2253 type_t *type = skip_typeref(declaration->type);
2254 create_initializer_object(initializer, type, init_entity, NULL, 0);
2256 assert(current_ir_graph == get_const_code_irg());
2257 current_ir_graph = old_current_ir_graph;
2259 ir_node *src_addr = create_symconst(dbgi, init_entity);
2260 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2262 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2263 set_store(copyb_mem);
2266 static void create_initializer(declaration_t *declaration)
2268 initializer_t *initializer = declaration->init.initializer;
2269 if(initializer == NULL)
2272 declaration_type_t declaration_type
2273 = (declaration_type_t) declaration->declaration_type;
2274 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2275 create_initializer_local_variable_entity(declaration);
2279 if(initializer->type == INITIALIZER_VALUE) {
2280 initializer_value_t *initializer_value = &initializer->value;
2282 ir_node *value = expression_to_firm(initializer_value->value);
2284 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2285 set_value(declaration->v.value_number, value);
2287 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2289 ir_entity *entity = declaration->v.entity;
2291 set_entity_variability(entity, variability_initialized);
2292 set_atomic_ent_value(entity, value);
2295 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2296 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2298 ir_entity *entity = declaration->v.entity;
2299 set_entity_variability(entity, variability_initialized);
2301 type_t *type = skip_typeref(declaration->type);
2302 create_initializer_object(initializer, type, entity, NULL, 0);
2306 static void create_local_variable(declaration_t *declaration)
2308 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2310 bool needs_entity = declaration->address_taken;
2311 type_t *type = skip_typeref(declaration->type);
2313 if(is_type_array(type) || is_type_compound(type)) {
2314 needs_entity = true;
2318 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2319 create_declaration_entity(declaration,
2320 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2323 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2324 declaration->v.value_number = next_value_number_function;
2325 ++next_value_number_function;
2328 create_initializer(declaration);
2331 static void create_local_static_variable(declaration_t *declaration)
2333 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2335 type_t *type = skip_typeref(declaration->type);
2336 ir_type *global_type = get_glob_type();
2337 ident *id = unique_ident(declaration->symbol->string);
2338 ir_type *irtype = get_ir_type(type);
2339 ir_entity *entity = new_entity(global_type, id, irtype);
2340 set_entity_ld_ident(entity, id);
2342 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2343 declaration->v.entity = entity;
2344 set_entity_variability(entity, variability_uninitialized);
2345 set_entity_visibility(entity, visibility_local);
2346 set_entity_allocation(entity, allocation_static);
2348 ir_graph *old_current_ir_graph = current_ir_graph;
2349 current_ir_graph = get_const_code_irg();
2351 create_initializer(declaration);
2353 assert(current_ir_graph == get_const_code_irg());
2354 current_ir_graph = old_current_ir_graph;
2359 static void return_statement_to_firm(return_statement_t *statement)
2361 if(get_cur_block() == NULL)
2364 ir_type *func_irtype = get_ir_type(current_function_decl->type);
2366 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2370 if(get_method_n_ress(func_irtype) > 0) {
2371 ir_type *res_type = get_method_res_type(func_irtype, 0);
2373 if(statement->return_value != NULL) {
2374 ir_node *node = expression_to_firm(statement->return_value);
2375 node = do_strict_conv(dbgi, node);
2379 if(is_compound_type(res_type)) {
2382 mode = get_type_mode(res_type);
2384 in[0] = new_Unknown(mode);
2388 /* build return_value for its side effects */
2389 if(statement->return_value != NULL) {
2390 expression_to_firm(statement->return_value);
2395 ir_node *store = get_store();
2396 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
2398 ir_node *end_block = get_irg_end_block(current_ir_graph);
2399 add_immBlock_pred(end_block, ret);
2401 set_cur_block(NULL);
2404 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
2406 if(get_cur_block() == NULL)
2409 return expression_to_firm(statement->expression);
2412 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
2414 ir_node *result = NULL;
2415 statement_t *statement = compound->statements;
2416 for( ; statement != NULL; statement = statement->base.next) {
2417 //context2firm(&statement->context);
2419 if(statement->base.next == NULL
2420 && statement->type == STATEMENT_EXPRESSION) {
2421 result = expression_statement_to_firm(
2422 (expression_statement_t*) statement);
2425 statement_to_firm(statement);
2431 static void create_local_declaration(declaration_t *declaration)
2433 type_t *type = skip_typeref(declaration->type);
2435 switch ((storage_class_tag_t) declaration->storage_class) {
2436 case STORAGE_CLASS_STATIC:
2437 create_local_static_variable(declaration);
2439 case STORAGE_CLASS_ENUM_ENTRY:
2440 panic("enum entry declaration in local block found");
2441 case STORAGE_CLASS_EXTERN:
2442 panic("extern declaration in local block found");
2443 case STORAGE_CLASS_NONE:
2444 case STORAGE_CLASS_AUTO:
2445 case STORAGE_CLASS_REGISTER:
2446 if(is_type_function(type)) {
2447 panic("nested functions not supported yet");
2449 create_local_variable(declaration);
2452 case STORAGE_CLASS_TYPEDEF:
2453 case STORAGE_CLASS_THREAD:
2454 case STORAGE_CLASS_THREAD_EXTERN:
2455 case STORAGE_CLASS_THREAD_STATIC:
2458 panic("invalid storage class found");
2461 static void declaration_statement_to_firm(declaration_statement_t *statement)
2463 declaration_t *declaration = statement->declarations_begin;
2464 declaration_t *end = statement->declarations_end->next;
2465 for( ; declaration != end; declaration = declaration->next) {
2466 create_local_variable(declaration);
2470 static void if_statement_to_firm(if_statement_t *statement)
2472 ir_node *cur_block = get_cur_block();
2474 ir_node *fallthrough_block = new_immBlock();
2476 /* the true (blocks) */
2477 ir_node *true_block;
2478 if (statement->true_statement != NULL) {
2479 true_block = new_immBlock();
2480 statement_to_firm(statement->true_statement);
2481 if(get_cur_block() != NULL) {
2482 ir_node *jmp = new_Jmp();
2483 add_immBlock_pred(fallthrough_block, jmp);
2486 true_block = fallthrough_block;
2489 /* the false (blocks) */
2490 ir_node *false_block;
2491 if(statement->false_statement != NULL) {
2492 false_block = new_immBlock();
2494 statement_to_firm(statement->false_statement);
2495 if(get_cur_block() != NULL) {
2496 ir_node *jmp = new_Jmp();
2497 add_immBlock_pred(fallthrough_block, jmp);
2500 false_block = fallthrough_block;
2503 /* create the condition */
2504 if(cur_block != NULL) {
2505 set_cur_block(cur_block);
2506 create_condition_evaluation(statement->condition, true_block,
2510 mature_immBlock(true_block);
2511 if(false_block != fallthrough_block) {
2512 mature_immBlock(false_block);
2514 mature_immBlock(fallthrough_block);
2516 set_cur_block(fallthrough_block);
2519 static void while_statement_to_firm(while_statement_t *statement)
2521 ir_node *jmp = NULL;
2522 if(get_cur_block() != NULL) {
2526 /* create the header block */
2527 ir_node *header_block = new_immBlock();
2529 add_immBlock_pred(header_block, jmp);
2532 /* the false block */
2533 ir_node *false_block = new_immBlock();
2536 ir_node *body_block;
2537 if (statement->body != NULL) {
2538 ir_node *old_continue_label = continue_label;
2539 ir_node *old_break_label = break_label;
2540 continue_label = header_block;
2541 break_label = false_block;
2543 body_block = new_immBlock();
2544 statement_to_firm(statement->body);
2546 assert(continue_label == header_block);
2547 assert(break_label == false_block);
2548 continue_label = old_continue_label;
2549 break_label = old_break_label;
2551 if(get_cur_block() != NULL) {
2553 add_immBlock_pred(header_block, jmp);
2556 body_block = header_block;
2559 /* create the condition */
2560 set_cur_block(header_block);
2562 create_condition_evaluation(statement->condition, body_block, false_block);
2563 mature_immBlock(body_block);
2564 mature_immBlock(false_block);
2565 mature_immBlock(header_block);
2567 set_cur_block(false_block);
2570 static void do_while_statement_to_firm(do_while_statement_t *statement)
2572 ir_node *jmp = NULL;
2573 if(get_cur_block() != NULL) {
2577 /* create the header block */
2578 ir_node *header_block = new_immBlock();
2580 /* the false block */
2581 ir_node *false_block = new_immBlock();
2584 ir_node *body_block = new_immBlock();
2586 add_immBlock_pred(body_block, jmp);
2589 if (statement->body != NULL) {
2590 ir_node *old_continue_label = continue_label;
2591 ir_node *old_break_label = break_label;
2592 continue_label = header_block;
2593 break_label = false_block;
2595 statement_to_firm(statement->body);
2597 assert(continue_label == header_block);
2598 assert(break_label == false_block);
2599 continue_label = old_continue_label;
2600 break_label = old_break_label;
2602 if (get_cur_block() == NULL) {
2603 mature_immBlock(header_block);
2604 mature_immBlock(body_block);
2605 mature_immBlock(false_block);
2610 ir_node *body_jmp = new_Jmp();
2611 add_immBlock_pred(header_block, body_jmp);
2612 mature_immBlock(header_block);
2614 /* create the condition */
2615 set_cur_block(header_block);
2617 create_condition_evaluation(statement->condition, body_block, false_block);
2618 mature_immBlock(body_block);
2619 mature_immBlock(false_block);
2620 mature_immBlock(header_block);
2622 set_cur_block(false_block);
2625 static void for_statement_to_firm(for_statement_t *statement)
2627 ir_node *jmp = NULL;
2628 if (get_cur_block() != NULL) {
2629 if(statement->initialisation != NULL) {
2630 expression_to_firm(statement->initialisation);
2633 /* create declarations */
2634 declaration_t *declaration = statement->context.declarations;
2635 for( ; declaration != NULL; declaration = declaration->next) {
2636 create_local_declaration(declaration);
2643 /* create the step block */
2644 ir_node *const step_block = new_immBlock();
2645 if (statement->step != NULL) {
2646 expression_to_firm(statement->step);
2648 ir_node *const step_jmp = new_Jmp();
2650 /* create the header block */
2651 ir_node *const header_block = new_immBlock();
2653 add_immBlock_pred(header_block, jmp);
2655 add_immBlock_pred(header_block, step_jmp);
2657 /* the false block */
2658 ir_node *const false_block = new_immBlock();
2661 ir_node * body_block;
2662 if (statement->body != NULL) {
2663 ir_node *const old_continue_label = continue_label;
2664 ir_node *const old_break_label = break_label;
2665 continue_label = step_block;
2666 break_label = false_block;
2668 body_block = new_immBlock();
2669 statement_to_firm(statement->body);
2671 assert(continue_label == step_block);
2672 assert(break_label == false_block);
2673 continue_label = old_continue_label;
2674 break_label = old_break_label;
2676 if (get_cur_block() != NULL) {
2678 add_immBlock_pred(step_block, jmp);
2681 body_block = step_block;
2684 /* create the condition */
2685 set_cur_block(header_block);
2686 if (statement->condition != NULL) {
2687 create_condition_evaluation(statement->condition, body_block,
2690 keep_alive(header_block);
2692 add_immBlock_pred(body_block, jmp);
2695 mature_immBlock(body_block);
2696 mature_immBlock(false_block);
2697 mature_immBlock(step_block);
2698 mature_immBlock(header_block);
2699 mature_immBlock(false_block);
2701 set_cur_block(false_block);
2704 static void create_jump_statement(const statement_t *statement,
2705 ir_node *target_block)
2707 if(get_cur_block() == NULL)
2710 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2711 ir_node *jump = new_d_Jmp(dbgi);
2712 add_immBlock_pred(target_block, jump);
2714 set_cur_block(NULL);
2717 static void switch_statement_to_firm(const switch_statement_t *statement)
2719 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2721 ir_node *expression = expression_to_firm(statement->expression);
2722 ir_node *cond = new_d_Cond(dbgi, expression);
2723 ir_node *break_block = new_immBlock();
2725 set_cur_block(NULL);
2727 ir_node *const old_switch_cond = current_switch_cond;
2728 ir_node *const old_break_label = break_label;
2729 const bool old_saw_default_label = saw_default_label;
2730 current_switch_cond = cond;
2731 break_label = break_block;
2733 statement_to_firm(statement->body);
2735 if(get_cur_block() != NULL) {
2736 ir_node *jmp = new_Jmp();
2737 add_immBlock_pred(break_block, jmp);
2740 if (!saw_default_label) {
2741 set_cur_block(get_nodes_block(cond));
2742 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2743 MAGIC_DEFAULT_PN_NUMBER);
2744 add_immBlock_pred(break_block, proj);
2747 assert(current_switch_cond == cond);
2748 assert(break_label == break_block);
2749 current_switch_cond = old_switch_cond;
2750 break_label = old_break_label;
2751 saw_default_label = old_saw_default_label;
2753 mature_immBlock(break_block);
2754 set_cur_block(break_block);
2757 static void case_label_to_firm(const case_label_statement_t *statement)
2759 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2761 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2763 /* let's create a node and hope firm constant folding creates a Const
2766 set_cur_block(get_nodes_block(current_switch_cond));
2767 if(statement->expression) {
2768 long pn = fold_constant(statement->expression);
2769 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2770 /* oops someone detected our cheating... */
2771 panic("magic default pn used");
2773 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2775 saw_default_label = true;
2776 proj = new_d_defaultProj(dbgi, current_switch_cond,
2777 MAGIC_DEFAULT_PN_NUMBER);
2780 ir_node *block = new_immBlock();
2781 if (fallthrough != NULL) {
2782 add_immBlock_pred(block, fallthrough);
2784 add_immBlock_pred(block, proj);
2785 mature_immBlock(block);
2787 statement_to_firm(statement->label_statement);
2790 static ir_node *get_label_block(declaration_t *label)
2792 assert(label->namespc == NAMESPACE_LABEL);
2794 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2795 return label->v.block;
2797 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2799 ir_node *old_cur_block = get_cur_block();
2800 ir_node *block = new_immBlock();
2801 set_cur_block(old_cur_block);
2803 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2804 label->v.block = block;
2806 ARR_APP1(ir_node *, imature_blocks, block);
2811 static void label_to_firm(const label_statement_t *statement)
2813 ir_node *block = get_label_block(statement->label);
2815 if(get_cur_block() != NULL) {
2816 ir_node *jmp = new_Jmp();
2817 add_immBlock_pred(block, jmp);
2820 set_cur_block(block);
2823 statement_to_firm(statement->label_statement);
2826 static void goto_to_firm(const goto_statement_t *statement)
2828 if(get_cur_block() == NULL)
2831 ir_node *block = get_label_block(statement->label);
2832 ir_node *jmp = new_Jmp();
2833 add_immBlock_pred(block, jmp);
2835 set_cur_block(NULL);
2838 typedef enum modifier_t {
2839 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
2840 ASM_MODIFIER_READ_WRITE = 1 << 1,
2841 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
2842 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
2846 static void asm_statement_to_firm(const asm_statement_t *statement)
2848 bool needs_memory = false;
2850 size_t n_clobbers = 0;
2851 asm_clobber_t *clobber = statement->clobbers;
2852 for( ; clobber != NULL; clobber = clobber->next) {
2853 if(strcmp(clobber->clobber, "memory") == 0) {
2854 needs_memory = true;
2858 ident *id = new_id_from_str(clobber->clobber);
2859 obstack_ptr_grow(&asm_obst, id);
2862 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
2863 ident **clobbers = NULL;
2864 if(n_clobbers > 0) {
2865 clobbers = obstack_finish(&asm_obst);
2868 /* find and count input and output constraints */
2869 asm_constraint_t *constraint = statement->inputs;
2870 for( ; constraint != NULL; constraint = constraint->next) {
2872 bool supports_memop = false;
2873 for(const char *c = constraint->constraints; *c != 0; ++c) {
2874 /* TODO: improve error messages */
2878 panic("multiple alternative assembler constraints not "
2886 supports_memop = true;
2887 obstack_1grow(&asm_obst, *c);
2890 if(modifiers & ASM_MODIFIER_READ_WRITE)
2891 panic("inconsistent register constraints");
2892 modifiers |= ASM_MODIFIER_WRITE_ONLY;
2895 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
2896 panic("inconsistent register constraints");
2897 modifiers |= ASM_MODIFIER_READ_WRITE;
2900 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
2901 panic("early clobber assembler constraint not supported yet");
2904 modifiers |= ASM_MODIFIER_COMMUTATIVE;
2905 panic("commutative assembler constraint not supported yet");
2908 /* skip register preferences stuff... */
2909 while(*c != 0 && *c != ',')
2913 /* skip register preferences stuff... */
2917 obstack_1grow(&asm_obst, *c);
2921 obstack_1grow(&asm_obst, '\0');
2922 const char *constraint_string = obstack_finish(&asm_obst);
2924 needs_memory |= supports_memop;
2925 if(supports_memop) {
2933 static void statement_to_firm(statement_t *statement)
2935 switch(statement->type) {
2936 case STATEMENT_INVALID:
2937 panic("invalid statement found");
2938 case STATEMENT_COMPOUND:
2939 compound_statement_to_firm(&statement->compound);
2941 case STATEMENT_RETURN:
2942 return_statement_to_firm(&statement->returns);
2944 case STATEMENT_EXPRESSION:
2945 expression_statement_to_firm(&statement->expression);
2948 if_statement_to_firm(&statement->ifs);
2950 case STATEMENT_WHILE:
2951 while_statement_to_firm(&statement->whiles);
2953 case STATEMENT_DO_WHILE:
2954 do_while_statement_to_firm(&statement->do_while);
2956 case STATEMENT_DECLARATION:
2957 declaration_statement_to_firm(&statement->declaration);
2959 case STATEMENT_BREAK:
2960 create_jump_statement(statement, break_label);
2962 case STATEMENT_CONTINUE:
2963 create_jump_statement(statement, continue_label);
2965 case STATEMENT_SWITCH:
2966 switch_statement_to_firm(&statement->switchs);
2968 case STATEMENT_CASE_LABEL:
2969 case_label_to_firm(&statement->case_label);
2972 for_statement_to_firm(&statement->fors);
2974 case STATEMENT_LABEL:
2975 label_to_firm(&statement->label);
2977 case STATEMENT_GOTO:
2978 goto_to_firm(&statement->gotos);
2981 //asm_statement_to_firm(&statement->asms);
2985 panic("Statement not implemented\n");
2988 static int count_local_declarations(const declaration_t * decl,
2989 const declaration_t *const end)
2992 for (; decl != end; decl = decl->next) {
2993 const type_t *type = skip_typeref(decl->type);
2994 switch (type->type) {
2998 if (!decl->address_taken)
3008 static int count_decls_in_stmts(const statement_t *stmt)
3011 for (; stmt != NULL; stmt = stmt->base.next) {
3012 switch (stmt->type) {
3013 case STATEMENT_DECLARATION: {
3014 const declaration_statement_t *const decl_stmt =
3015 (const declaration_statement_t*)stmt;
3016 count += count_local_declarations(decl_stmt->declarations_begin,
3017 decl_stmt->declarations_end->next);
3021 case STATEMENT_COMPOUND: {
3022 const compound_statement_t *const comp =
3023 (const compound_statement_t*)stmt;
3024 count += count_decls_in_stmts(comp->statements);
3028 case STATEMENT_IF: {
3029 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
3030 count += count_decls_in_stmts(if_stmt->true_statement);
3031 count += count_decls_in_stmts(if_stmt->false_statement);
3035 case STATEMENT_SWITCH: {
3036 const switch_statement_t *const switch_stmt =
3037 (const switch_statement_t*)stmt;
3038 count += count_decls_in_stmts(switch_stmt->body);
3042 case STATEMENT_LABEL: {
3043 const label_statement_t *const label_stmt =
3044 (const label_statement_t*)stmt;
3045 count += count_decls_in_stmts(label_stmt->label_statement);
3049 case STATEMENT_WHILE: {
3050 const while_statement_t *const while_stmt =
3051 (const while_statement_t*)stmt;
3052 count += count_decls_in_stmts(while_stmt->body);
3056 case STATEMENT_DO_WHILE: {
3057 const do_while_statement_t *const do_while_stmt =
3058 (const do_while_statement_t*)stmt;
3059 count += count_decls_in_stmts(do_while_stmt->body);
3063 case STATEMENT_FOR: {
3064 const for_statement_t *const for_stmt =
3065 (const for_statement_t*)stmt;
3066 /* TODO initialisation */
3067 count += count_decls_in_stmts(for_stmt->body);
3072 case STATEMENT_BREAK:
3073 case STATEMENT_CASE_LABEL:
3074 case STATEMENT_CONTINUE:
3075 case STATEMENT_EXPRESSION:
3076 case STATEMENT_GOTO:
3077 case STATEMENT_INVALID:
3078 case STATEMENT_RETURN:
3085 static int get_function_n_local_vars(declaration_t *declaration)
3089 /* count parameters */
3090 count += count_local_declarations(declaration->context.declarations, NULL);
3092 /* count local variables declared in body */
3093 count += count_decls_in_stmts(declaration->init.statement);
3095 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
3096 * for expression statements... */
3102 static void initialize_function_parameters(declaration_t *declaration)
3104 ir_graph *irg = current_ir_graph;
3105 ir_node *args = get_irg_args(irg);
3106 ir_node *start_block = get_irg_start_block(irg);
3107 ir_type *function_irtype = get_ir_type(declaration->type);
3110 declaration_t *parameter = declaration->context.declarations;
3111 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3112 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
3113 type_t *type = skip_typeref(parameter->type);
3115 bool needs_entity = parameter->address_taken;
3116 assert(!is_type_array(type));
3117 if(is_type_compound(type)) {
3118 needs_entity = true;
3122 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3123 ident *id = new_id_from_str(parameter->symbol->string);
3124 set_entity_ident(entity, id);
3126 parameter->declaration_type
3127 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
3128 parameter->v.entity = entity;
3132 ir_mode *mode = get_ir_mode(parameter->type);
3134 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3136 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
3137 parameter->v.value_number = next_value_number_function;
3138 ++next_value_number_function;
3140 set_value(parameter->v.value_number, proj);
3144 static void create_function(declaration_t *declaration)
3146 ir_entity *function_entity = get_function_entity(declaration);
3148 if(declaration->init.statement == NULL)
3151 current_function_decl = declaration;
3152 current_function_name = NULL;
3154 assert(imature_blocks == NULL);
3155 imature_blocks = NEW_ARR_F(ir_node*, 0);
3157 int n_local_vars = get_function_n_local_vars(declaration);
3158 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3159 ir_node *first_block = get_cur_block();
3161 next_value_number_function = 0;
3162 initialize_function_parameters(declaration);
3164 statement_to_firm(declaration->init.statement);
3166 ir_node *end_block = get_irg_end_block(irg);
3168 /* do we have a return statement yet? */
3169 if(get_cur_block() != NULL) {
3170 type_t *type = skip_typeref(declaration->type);
3171 assert(is_type_function(type));
3172 const function_type_t *func_type = &type->function;
3173 const type_t *return_type
3174 = skip_typeref(func_type->return_type);
3177 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3178 ret = new_Return(get_store(), 0, NULL);
3181 if(is_type_scalar(return_type)) {
3182 mode = get_ir_mode(func_type->return_type);
3188 // ยง5.1.2.2.3 main implicitly returns 0
3189 if (strcmp(declaration->symbol->string, "main") == 0) {
3190 in[0] = new_Const(mode, get_mode_null(mode));
3192 in[0] = new_Unknown(mode);
3194 ret = new_Return(get_store(), 1, in);
3196 add_immBlock_pred(end_block, ret);
3199 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3200 mature_immBlock(imature_blocks[i]);
3202 DEL_ARR_F(imature_blocks);
3203 imature_blocks = NULL;
3205 mature_immBlock(first_block);
3206 mature_immBlock(end_block);
3208 irg_finalize_cons(irg);
3210 /* finalize the frame type */
3211 ir_type *frame_type = get_irg_frame_type(irg);
3212 int n = get_compound_n_members(frame_type);
3215 for(int i = 0; i < n; ++i) {
3216 ir_entity *entity = get_compound_member(frame_type, i);
3217 ir_type *entity_type = get_entity_type(entity);
3219 int align = get_type_alignment_bytes(entity_type);
3220 if(align > align_all)
3224 misalign = offset % align;
3226 offset += align - misalign;
3230 set_entity_offset(entity, offset);
3231 offset += get_type_size_bytes(entity_type);
3233 set_type_size_bytes(frame_type, offset);
3234 set_type_alignment_bytes(frame_type, align_all);
3235 set_type_state(frame_type, layout_fixed);
3240 static void create_global_variable(declaration_t *declaration)
3244 switch ((storage_class_tag_t)declaration->storage_class) {
3245 case STORAGE_CLASS_STATIC:
3246 vis = visibility_local;
3249 case STORAGE_CLASS_EXTERN:
3250 vis = visibility_external_allocated;
3253 case STORAGE_CLASS_NONE:
3254 vis = visibility_external_visible;
3257 case STORAGE_CLASS_THREAD:
3258 vis = visibility_external_visible;
3261 case STORAGE_CLASS_THREAD_EXTERN:
3262 vis = visibility_external_allocated;
3265 case STORAGE_CLASS_THREAD_STATIC:
3266 vis = visibility_local;
3270 var_type = get_tls_type();
3274 var_type = get_glob_type();
3278 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3280 set_entity_visibility(declaration->v.entity, vis);
3282 current_ir_graph = get_const_code_irg();
3283 create_initializer(declaration);
3286 case STORAGE_CLASS_TYPEDEF:
3287 case STORAGE_CLASS_AUTO:
3288 case STORAGE_CLASS_REGISTER:
3289 case STORAGE_CLASS_ENUM_ENTRY:
3292 panic("Invalid storage class for global variable");
3295 static void context_to_firm(context_t *context)
3297 /* first pass: create declarations */
3298 declaration_t *declaration = context->declarations;
3299 for( ; declaration != NULL; declaration = declaration->next) {
3300 if(declaration->namespc != NAMESPACE_NORMAL)
3302 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3303 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3305 if(declaration->symbol == NULL)
3308 type_t *type = skip_typeref(declaration->type);
3309 if(is_type_function(type)) {
3310 get_function_entity(declaration);
3312 create_global_variable(declaration);
3316 /* second pass: create code */
3317 declaration = context->declarations;
3318 for( ; declaration != NULL; declaration = declaration->next) {
3319 if(declaration->namespc != NAMESPACE_NORMAL)
3321 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3322 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3324 if(declaration->symbol == NULL)
3327 type_t *type = declaration->type;
3328 if(type->type != TYPE_FUNCTION)
3331 create_function(declaration);
3335 void translation_unit_to_firm(translation_unit_t *unit)
3337 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3338 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3339 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3341 ir_type_int = get_ir_type(type_int);
3342 ir_type_const_char = get_ir_type(type_const_char);
3343 ir_type_wchar_t = get_ir_type(type_wchar_t);
3344 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3347 type_void->base.firm_type = ir_type_void;
3349 /* just to be sure */
3350 continue_label = NULL;
3352 current_switch_cond = NULL;
3354 context_to_firm(& unit->context);