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 typedef enum declaration_type_t {
43 DECLARATION_TYPE_UNKNOWN,
44 DECLARATION_TYPE_FUNCTION,
45 DECLARATION_TYPE_GLOBAL_VARIABLE,
46 DECLARATION_TYPE_LOCAL_VARIABLE,
47 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
48 DECLARATION_TYPE_COMPOUND_MEMBER,
49 DECLARATION_TYPE_LABEL_BLOCK,
50 DECLARATION_TYPE_ENUM_ENTRY
53 static ir_type *get_ir_type(type_t *type);
55 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
59 const declaration_t *declaration = & value_numbers[pos]->declaration;
61 print_warning_prefix(declaration->source_position);
62 fprintf(stderr, "variable '%s' might be used uninitialized\n",
63 declaration->symbol->string);
65 fprintf(stderr, "Some variable might be used uninitialized\n");
66 return new_r_Unknown(irg, mode);
69 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
71 const source_position_t *pos = (const source_position_t*) dbg;
74 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
78 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
80 const source_position_t *pos = (const source_position_t*) dbg;
85 return pos->input_name;
88 void init_ast2firm(void)
92 void exit_ast2firm(void)
96 static unsigned unique_id = 0;
98 static ident *unique_ident(const char *tag)
102 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
104 return new_id_from_str(buf);
107 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
109 switch(atomic_type->atype) {
110 case ATOMIC_TYPE_SCHAR:
111 case ATOMIC_TYPE_CHAR:
113 case ATOMIC_TYPE_UCHAR:
115 case ATOMIC_TYPE_SHORT:
117 case ATOMIC_TYPE_USHORT:
119 case ATOMIC_TYPE_LONG:
120 case ATOMIC_TYPE_INT:
122 case ATOMIC_TYPE_ULONG:
123 case ATOMIC_TYPE_UINT:
125 case ATOMIC_TYPE_LONGLONG:
127 case ATOMIC_TYPE_ULONGLONG:
129 case ATOMIC_TYPE_FLOAT:
131 case ATOMIC_TYPE_DOUBLE:
133 case ATOMIC_TYPE_LONG_DOUBLE:
135 case ATOMIC_TYPE_BOOL:
137 #ifdef PROVIDE_COMPLEX
138 case ATOMIC_TYPE_FLOAT_COMPLEX:
139 case ATOMIC_TYPE_DOUBLE_COMPLEX:
140 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
141 panic("complex lowering not implemented yet");
143 case ATOMIC_TYPE_FLOAT_IMAGINARY:
144 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
145 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
146 panic("imaginary lowering not implemented yet");
149 case ATOMIC_TYPE_VOID:
150 /* firm has no real void... */
152 case ATOMIC_TYPE_INVALID:
155 panic("Encountered unknown atomic type");
159 static unsigned get_type_size(type_t *type);
161 static unsigned get_atomic_type_size(const atomic_type_t *type)
163 switch(type->atype) {
164 case ATOMIC_TYPE_CHAR:
165 case ATOMIC_TYPE_SCHAR:
166 case ATOMIC_TYPE_UCHAR:
169 case ATOMIC_TYPE_SHORT:
170 case ATOMIC_TYPE_USHORT:
173 case ATOMIC_TYPE_BOOL:
174 case ATOMIC_TYPE_INT:
175 case ATOMIC_TYPE_UINT:
176 case ATOMIC_TYPE_LONG:
177 case ATOMIC_TYPE_ULONG:
178 case ATOMIC_TYPE_FLOAT:
181 case ATOMIC_TYPE_LONGLONG:
182 case ATOMIC_TYPE_ULONGLONG:
183 case ATOMIC_TYPE_DOUBLE:
186 case ATOMIC_TYPE_LONG_DOUBLE:
189 case ATOMIC_TYPE_VOID:
192 case ATOMIC_TYPE_INVALID:
195 panic("Trying to determine size of invalid atomic type");
198 static unsigned get_compound_type_size(compound_type_t *type)
200 ir_type *irtype = get_ir_type((type_t*) type);
201 return get_type_size_bytes(irtype);
204 static unsigned get_array_type_size(array_type_t *type)
206 ir_type *irtype = get_ir_type((type_t*) type);
207 return get_type_size_bytes(irtype);
210 static unsigned get_type_size(type_t *type)
212 type = skip_typeref(type);
216 return get_atomic_type_size(&type->atomic);
218 return get_mode_size_bytes(mode_Is);
219 case TYPE_COMPOUND_UNION:
220 case TYPE_COMPOUND_STRUCT:
221 return get_compound_type_size(&type->compound);
223 /* just a pointer to the function */
224 return get_mode_size_bytes(mode_P_code);
226 return get_mode_size_bytes(mode_P_data);
228 return get_array_type_size(&type->array);
235 panic("Trying to determine size of invalid type");
238 static unsigned count_parameters(const function_type_t *function_type)
242 function_parameter_t *parameter = function_type->parameters;
243 for ( ; parameter != NULL; parameter = parameter->next) {
253 static long fold_constant(const expression_t *expression);
255 static ir_type *create_atomic_type(const atomic_type_t *type)
257 ir_mode *mode = get_atomic_mode(type);
258 ident *id = get_mode_ident(mode);
259 ir_type *irtype = new_type_primitive(id, mode);
264 static ir_type *create_method_type(const function_type_t *function_type)
266 type_t *return_type = function_type->return_type;
268 ident *id = unique_ident("functiontype");
269 int n_parameters = count_parameters(function_type);
270 int n_results = return_type == type_void ? 0 : 1;
271 ir_type *irtype = new_type_method(id, n_parameters, n_results);
273 if(return_type != type_void) {
274 ir_type *restype = get_ir_type(return_type);
275 set_method_res_type(irtype, 0, restype);
278 function_parameter_t *parameter = function_type->parameters;
280 for( ; parameter != NULL; parameter = parameter->next) {
281 ir_type *p_irtype = get_ir_type(parameter->type);
282 set_method_param_type(irtype, n, p_irtype);
286 if(function_type->variadic || function_type->unspecified_parameters) {
287 set_method_variadicity(irtype, variadicity_variadic);
293 static ir_type *create_pointer_type(pointer_type_t *type)
295 type_t *points_to = type->points_to;
296 ir_type *ir_points_to;
297 /* Avoid endless recursion if the points_to type contains this poiner type
298 * again (might be a struct). We therefore first create a void* pointer
299 * and then set the real points_to type
301 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
302 ir_type_void, mode_P_data);
303 type->type.firm_type = ir_type;
305 ir_points_to = get_ir_type(points_to);
306 set_pointer_points_to_type(ir_type, ir_points_to);
311 static ir_type *create_array_type(array_type_t *type)
313 type_t *element_type = type->element_type;
314 ir_type *ir_element_type = get_ir_type(element_type);
316 ident *id = unique_ident("array");
317 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
319 if(type->size != NULL) {
320 int n_elements = fold_constant(type->size);
322 set_array_bounds_int(ir_type, 0, 0, n_elements);
324 size_t elemsize = get_type_size_bytes(ir_element_type);
325 int align = get_type_alignment_bytes(ir_element_type);
326 if(elemsize % align > 0) {
327 elemsize += align - (elemsize % align);
329 set_type_size_bytes(ir_type, n_elements * elemsize);
330 set_type_alignment_bytes(ir_type, align);
331 set_type_state(ir_type, layout_fixed);
337 #define INVALID_TYPE ((ir_type_ptr)-1)
339 static ir_type *create_struct_type(compound_type_t *type)
341 symbol_t *symbol = type->declaration->symbol;
344 id = unique_ident(symbol->string);
346 id = unique_ident("__anonymous_struct");
348 ir_type *ir_type = new_type_struct(id);
350 type->type.firm_type = ir_type;
354 declaration_t *entry = type->declaration->context.declarations;
355 for( ; entry != NULL; entry = entry->next) {
356 if(entry->namespc != NAMESPACE_NORMAL)
359 ident *ident = new_id_from_str(entry->symbol->string);
360 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
362 int entry_size = get_type_size_bytes(entry_ir_type);
363 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
364 int misalign = offset % entry_alignment;
366 offset += entry_alignment - misalign;
368 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
369 set_entity_offset(entity, offset);
370 add_struct_member(ir_type, entity);
371 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
372 entry->v.entity = entity;
374 offset += entry_size;
375 if(entry_alignment > align_all) {
376 if(entry_alignment % align_all != 0) {
377 panic("Uneven alignments not supported yet");
379 align_all = entry_alignment;
383 int misalign = offset % align_all;
385 set_type_alignment_bytes(ir_type, align_all);
386 set_type_size_bytes(ir_type, offset);
387 set_type_state(ir_type, layout_fixed);
392 static ir_type *create_union_type(compound_type_t *type)
394 declaration_t *declaration = type->declaration;
395 symbol_t *symbol = declaration->symbol;
398 id = unique_ident(symbol->string);
400 id = unique_ident("__anonymous_union");
402 ir_type *ir_type = new_type_union(id);
404 type->type.firm_type = ir_type;
408 declaration_t *entry = declaration->context.declarations;
409 for( ; entry != NULL; entry = entry->next) {
410 if(entry->namespc != NAMESPACE_NORMAL)
413 ident *ident = new_id_from_str(entry->symbol->string);
414 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
416 int entry_size = get_type_size_bytes(entry_ir_type);
417 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
419 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
420 add_union_member(ir_type, entity);
421 set_entity_offset(entity, 0);
422 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
423 entry->v.entity = entity;
425 if(entry_size > size) {
428 if(entry_alignment > align_all) {
429 if(entry_alignment % align_all != 0) {
430 panic("Uneven alignments not supported yet");
432 align_all = entry_alignment;
436 set_type_alignment_bytes(ir_type, align_all);
437 set_type_size_bytes(ir_type, size);
438 set_type_state(ir_type, layout_fixed);
443 static ir_node *expression_to_firm(const expression_t *expression);
444 static inline ir_mode *get_ir_mode(type_t *type);
446 static ir_type *create_enum_type(enum_type_t *const type)
448 type->type.firm_type = ir_type_int;
450 ir_mode *const mode = get_ir_mode((type_t*) type);
451 tarval *const one = get_mode_one(mode);
452 tarval * tv_next = get_tarval_null(mode);
454 declaration_t *declaration = type->declaration->next;
455 for (; declaration != NULL; declaration = declaration->next) {
456 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
459 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
461 expression_t *const init = declaration->init.enum_value;
463 ir_node *const cnst = expression_to_firm(init);
464 if (!is_Const(cnst)) {
465 panic("couldn't fold constant");
467 tv_next = get_Const_tarval(cnst);
469 declaration->v.enum_val = tv_next;
470 tv_next = tarval_add(tv_next, one);
476 static ir_type *get_ir_type(type_t *type)
478 assert(type != NULL);
480 type = skip_typeref(type);
482 if(type->base.firm_type != NULL) {
483 assert(type->base.firm_type != INVALID_TYPE);
484 return type->base.firm_type;
487 ir_type *firm_type = NULL;
490 firm_type = create_atomic_type(&type->atomic);
493 firm_type = create_method_type(&type->function);
496 firm_type = create_pointer_type(&type->pointer);
499 firm_type = create_array_type(&type->array);
501 case TYPE_COMPOUND_STRUCT:
502 firm_type = create_struct_type(&type->compound);
504 case TYPE_COMPOUND_UNION:
505 firm_type = create_union_type(&type->compound);
508 firm_type = create_enum_type(&type->enumt);
516 if(firm_type == NULL)
517 panic("unknown type found");
519 type->base.firm_type = firm_type;
523 static inline ir_mode *get_ir_mode(type_t *type)
525 ir_type *irtype = get_ir_type(type);
527 /* firm doesn't report a mode for arrays somehow... */
528 if(is_Array_type(irtype)) {
532 ir_mode *mode = get_type_mode(irtype);
533 assert(mode != NULL);
537 static ir_entity* get_function_entity(declaration_t *declaration)
539 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
540 return declaration->v.entity;
541 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
543 symbol_t *symbol = declaration->symbol;
544 ident *id = new_id_from_str(symbol->string);
546 ir_type *global_type = get_glob_type();
547 ir_type *ir_type_method = get_ir_type(declaration->type);
548 assert(is_Method_type(ir_type_method));
550 ir_entity *entity = new_entity(global_type, id, ir_type_method);
551 set_entity_ld_ident(entity, id);
552 if(declaration->storage_class == STORAGE_CLASS_STATIC
553 || declaration->is_inline) {
554 set_entity_visibility(entity, visibility_local);
555 } else if(declaration->init.statement != NULL) {
556 set_entity_visibility(entity, visibility_external_visible);
558 set_entity_visibility(entity, visibility_external_allocated);
561 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
562 declaration->v.entity = entity;
567 static dbg_info *get_dbg_info(const source_position_t *pos)
569 return (dbg_info*) pos;
572 static ir_node *const_to_firm(const const_expression_t *cnst)
574 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
575 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
580 if(mode_is_float(mode)) {
581 tv = new_tarval_from_double(cnst->v.float_value, mode);
583 if(mode_is_signed(mode)) {
584 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
586 len = snprintf(buf, sizeof(buf), "%llu",
587 (unsigned long long) cnst->v.int_value);
589 tv = new_tarval_from_str(buf, len, mode);
592 return new_d_Const(dbgi, mode, tv);
595 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
597 assert(entity != NULL);
598 union symconst_symbol sym;
599 sym.entity_p = entity;
600 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
603 static ir_node *string_to_firm(const source_position_t *const src_pos,
604 const char *const id_prefix,
605 const char *const string)
607 ir_type *const global_type = get_glob_type();
608 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
611 ident *const id = unique_ident(id_prefix);
612 ir_entity *const entity = new_entity(global_type, id, type);
613 set_entity_ld_ident(entity, id);
614 set_entity_variability(entity, variability_constant);
616 ir_type *const elem_type = ir_type_const_char;
617 ir_mode *const mode = get_type_mode(elem_type);
619 const size_t slen = strlen(string) + 1;
621 set_array_lower_bound_int(type, 0, 0);
622 set_array_upper_bound_int(type, 0, slen);
623 set_type_size_bytes(type, slen);
624 set_type_state(type, layout_fixed);
626 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
627 for(size_t i = 0; i < slen; ++i) {
628 tvs[i] = new_tarval_from_long(string[i], mode);
631 set_array_entity_values(entity, tvs, slen);
634 dbg_info *const dbgi = get_dbg_info(src_pos);
636 return create_symconst(dbgi, entity);
639 static ir_node *string_literal_to_firm(
640 const string_literal_expression_t* literal)
642 return string_to_firm(&literal->expression.source_position, "Lstr",
646 static ir_node *wide_string_literal_to_firm(
647 const wide_string_literal_expression_t* const literal)
649 ir_type *const global_type = get_glob_type();
650 ir_type *const elem_type = ir_type_wchar_t;
651 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
654 ident *const id = unique_ident("Lstr");
655 ir_entity *const entity = new_entity(global_type, id, type);
656 set_entity_ld_ident(entity, id);
657 set_entity_variability(entity, variability_constant);
659 ir_mode *const mode = get_type_mode(elem_type);
661 const wchar_rep_t *const string = literal->value.begin;
662 const size_t slen = literal->value.size;
664 set_array_lower_bound_int(type, 0, 0);
665 set_array_upper_bound_int(type, 0, slen);
666 set_type_size_bytes(type, slen);
667 set_type_state(type, layout_fixed);
669 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
670 for(size_t i = 0; i < slen; ++i) {
671 tvs[i] = new_tarval_from_long(string[i], mode);
674 set_array_entity_values(entity, tvs, slen);
677 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
679 return create_symconst(dbgi, entity);
682 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
683 dbg_info *const dbgi)
685 if(is_compound_type(irtype) || is_Array_type(irtype)) {
689 ir_mode *const mode = get_type_mode(irtype);
690 ir_node *const memory = get_store();
691 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
692 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
693 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
698 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
700 ir_mode *mode = get_irn_mode(node);
702 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
704 if(!mode_is_float(mode))
707 /* check if there is already a Conv */
708 if (get_irn_op(node) == op_Conv) {
709 /* convert it into a strict Conv */
710 set_Conv_strict(node, 1);
714 /* otherwise create a new one */
715 return new_d_strictConv(dbgi, node, mode);
718 static ir_node *get_global_var_address(dbg_info *const dbgi,
719 const declaration_t *const decl)
721 assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
723 ir_entity *const entity = decl->v.entity;
724 switch ((storage_class_tag_t)decl->storage_class) {
725 case STORAGE_CLASS_THREAD:
726 case STORAGE_CLASS_THREAD_EXTERN:
727 case STORAGE_CLASS_THREAD_STATIC: {
728 ir_node *const no_mem = new_NoMem();
729 ir_node *const tls = get_irg_tls(current_ir_graph);
730 return new_d_simpleSel(dbgi, no_mem, tls, entity);
734 return create_symconst(dbgi, entity);
738 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
740 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
741 declaration_t *declaration = ref->declaration;
742 type_t *type = skip_typeref(declaration->type);
744 switch((declaration_type_t) declaration->declaration_type) {
745 case DECLARATION_TYPE_UNKNOWN:
746 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
752 case DECLARATION_TYPE_ENUM_ENTRY: {
753 ir_mode *const mode = get_ir_mode(type);
754 return new_Const(mode, declaration->v.enum_val);
757 case DECLARATION_TYPE_LOCAL_VARIABLE: {
758 ir_mode *mode = get_ir_mode(type);
759 return get_value(declaration->v.value_number, mode);
761 case DECLARATION_TYPE_FUNCTION: {
762 return create_symconst(dbgi, declaration->v.entity);
764 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
765 ir_node *const addr = get_global_var_address(dbgi, declaration);
766 ir_type *const irtype = get_entity_type(declaration->v.entity);
767 return deref_address(irtype, addr, dbgi);
770 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
771 ir_entity *entity = declaration->v.entity;
772 ir_node *frame = get_irg_frame(current_ir_graph);
773 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
774 ir_type *irtype = get_entity_type(entity);
775 return deref_address(irtype, sel, dbgi);
778 case DECLARATION_TYPE_COMPOUND_MEMBER:
779 case DECLARATION_TYPE_LABEL_BLOCK:
780 panic("not implemented reference type");
783 panic("reference to declaration with unknown type found");
786 static ir_node *reference_addr(const reference_expression_t *ref)
788 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
789 declaration_t *declaration = ref->declaration;
791 switch((declaration_type_t) declaration->declaration_type) {
792 case DECLARATION_TYPE_UNKNOWN:
794 case DECLARATION_TYPE_LOCAL_VARIABLE:
795 panic("local variable without entity has no address");
796 case DECLARATION_TYPE_FUNCTION: {
797 return create_symconst(dbgi, declaration->v.entity);
799 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
800 ir_node *const addr = get_global_var_address(dbgi, declaration);
803 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
804 ir_entity *entity = declaration->v.entity;
805 ir_node *frame = get_irg_frame(current_ir_graph);
806 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
811 case DECLARATION_TYPE_ENUM_ENTRY:
812 panic("trying to reference enum entry");
814 case DECLARATION_TYPE_COMPOUND_MEMBER:
815 case DECLARATION_TYPE_LABEL_BLOCK:
816 panic("not implemented reference type");
819 panic("reference to declaration with unknown type found");
822 static ir_node *process_builtin_call(const call_expression_t *call)
824 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
826 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
827 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
829 type_t *type = skip_typeref(builtin->expression.datatype);
830 assert(is_type_pointer(type));
832 type_t *function_type = skip_typeref(type->pointer.points_to);
833 symbol_t *symbol = builtin->symbol;
836 case T___builtin_alloca: {
837 if(call->arguments == NULL || call->arguments->next != NULL) {
838 panic("invalid number of parameters on __builtin_alloca");
840 expression_t *argument = call->arguments->expression;
841 ir_node *size = expression_to_firm(argument);
843 ir_node *store = get_store();
844 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
846 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
848 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
852 case T___builtin_nan:
853 case T___builtin_nanf:
854 case T___builtin_nand: {
855 /* Ignore string for now... */
856 assert(is_type_function(function_type));
857 ir_mode *mode = get_ir_mode(function_type->function.return_type);
858 tarval *tv = get_mode_NAN(mode);
859 ir_node *res = new_d_Const(dbgi, mode, tv);
863 panic("Unsupported builtin found\n");
867 static ir_node *call_expression_to_firm(const call_expression_t *call)
869 assert(get_cur_block() != NULL);
871 expression_t *function = call->function;
872 if(function->type == EXPR_BUILTIN_SYMBOL) {
873 return process_builtin_call(call);
875 ir_node *callee = expression_to_firm(function);
877 type_t *type = skip_typeref(function->base.datatype);
878 assert(is_type_pointer(type));
879 pointer_type_t *pointer_type = &type->pointer;
880 type_t *points_to = skip_typeref(pointer_type->points_to);
881 assert(is_type_function(points_to));
882 function_type_t *function_type = &points_to->function;
884 int n_parameters = 0;
885 call_argument_t *argument = call->arguments;
886 for( ; argument != NULL; argument = argument->next) {
890 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
892 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
893 ir_type *new_method_type = NULL;
894 if(function_type->variadic || function_type->unspecified_parameters) {
895 /* we need to construct a new method type matching the call
897 int n_res = get_method_n_ress(ir_method_type);
898 new_method_type = new_type_method(unique_ident("calltype"),
899 n_parameters, n_res);
900 set_method_calling_convention(new_method_type,
901 get_method_calling_convention(ir_method_type));
902 set_method_additional_properties(new_method_type,
903 get_method_additional_properties(ir_method_type));
905 for(int i = 0; i < n_res; ++i) {
906 set_method_res_type(new_method_type, i,
907 get_method_res_type(ir_method_type, i));
910 ir_node *in[n_parameters];
912 argument = call->arguments;
914 for( ; argument != NULL; argument = argument->next) {
915 expression_t *expression = argument->expression;
916 ir_node *arg_node = expression_to_firm(expression);
918 arg_node = do_strict_conv(dbgi, arg_node);
921 if(new_method_type != NULL) {
922 ir_type *irtype = get_ir_type(expression->base.datatype);
923 set_method_param_type(new_method_type, n, irtype);
928 assert(n == n_parameters);
930 if(new_method_type != NULL)
931 ir_method_type = new_method_type;
933 ir_node *store = get_store();
934 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
936 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
939 type_t *return_type = skip_typeref(function_type->return_type);
940 ir_node *result = NULL;
942 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
944 if(is_type_scalar(return_type)) {
945 mode = get_ir_mode(return_type);
949 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
950 result = new_d_Proj(dbgi, resproj, mode, 0);
956 static void statement_to_firm(statement_t *statement);
957 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
959 static ir_node *expression_to_addr(const expression_t *expression);
960 static void create_condition_evaluation(const expression_t *expression,
962 ir_node *false_block);
964 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
967 value = do_strict_conv(dbgi, value);
969 ir_node *memory = get_store();
971 if(is_type_scalar(type)) {
972 ir_node *store = new_d_Store(dbgi, memory, addr, value);
973 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
974 set_store(store_mem);
976 ir_type *irtype = get_ir_type(type);
977 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
978 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
979 set_store(copyb_mem);
983 static void set_value_for_expression(const expression_t *expression,
986 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
987 value = do_strict_conv(dbgi, value);
989 if(expression->type == EXPR_REFERENCE) {
990 reference_expression_t *ref = (reference_expression_t*) expression;
992 declaration_t *declaration = ref->declaration;
993 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
994 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
995 set_value(declaration->v.value_number, value);
1000 ir_node *addr = expression_to_addr(expression);
1001 type_t *type = skip_typeref(expression->base.datatype);
1002 assign_value(dbgi, addr, type, value);
1005 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1007 ir_mode *value_mode = get_irn_mode(value);
1009 if (value_mode == dest_mode || is_Bad(value))
1012 if(dest_mode == mode_b) {
1013 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1014 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1015 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1019 return new_d_Conv(dbgi, value, dest_mode);
1022 static ir_node *create_incdec(const unary_expression_t *expression)
1024 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1025 type_t *type = skip_typeref(expression->expression.datatype);
1026 ir_mode *mode = get_ir_mode(type);
1027 expression_t *value = expression->value;
1029 ir_node *value_node = expression_to_firm(value);
1032 if(is_type_pointer(type)) {
1033 pointer_type_t *pointer_type = &type->pointer;
1034 unsigned elem_size = get_type_size(pointer_type->points_to);
1035 offset = new_Const_long(mode_Is, elem_size);
1037 assert(is_type_arithmetic(type));
1038 offset = new_Const(mode, get_mode_one(mode));
1041 switch(expression->type) {
1042 case UNEXPR_POSTFIX_INCREMENT: {
1043 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1044 set_value_for_expression(value, new_value);
1047 case UNEXPR_POSTFIX_DECREMENT: {
1048 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1049 set_value_for_expression(value, new_value);
1052 case UNEXPR_PREFIX_INCREMENT: {
1053 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1054 set_value_for_expression(value, new_value);
1057 case UNEXPR_PREFIX_DECREMENT: {
1058 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1059 set_value_for_expression(value, new_value);
1063 panic("no incdec expr in create_incdec");
1068 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1070 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1071 type_t *type = skip_typeref(expression->expression.datatype);
1072 ir_mode *mode = get_ir_mode(type);
1074 if(expression->type == UNEXPR_TAKE_ADDRESS)
1075 return expression_to_addr(expression->value);
1077 const expression_t *value = expression->value;
1078 ir_node *value_node = expression_to_firm(value);
1080 switch(expression->type) {
1082 return new_d_Minus(dbgi, value_node, mode);
1085 case UNEXPR_BITWISE_NEGATE:
1086 return new_d_Not(dbgi, value_node, mode);
1088 if(get_irn_mode(value_node) != mode_b) {
1089 value_node = create_conv(dbgi, value_node, mode_b);
1091 value_node = new_d_Not(dbgi, value_node, mode_b);
1092 if(mode != mode_b) {
1093 value_node = create_conv(dbgi, value_node, mode);
1097 case UNEXPR_DEREFERENCE: {
1098 type_t *value_type = skip_typeref(value->base.datatype);
1099 ir_type *irtype = get_ir_type(value_type);
1100 assert(is_Pointer_type(irtype));
1101 ir_type *points_to = get_pointer_points_to_type(irtype);
1102 return deref_address(points_to, value_node, dbgi);
1104 case UNEXPR_POSTFIX_INCREMENT:
1105 case UNEXPR_POSTFIX_DECREMENT:
1106 case UNEXPR_PREFIX_INCREMENT:
1107 case UNEXPR_PREFIX_DECREMENT:
1108 return create_incdec(expression);
1110 ir_node *node = create_conv(dbgi, value_node, get_ir_mode(type));
1111 node = do_strict_conv(dbgi, node);
1114 case UNEXPR_CAST_IMPLICIT:
1115 return create_conv(dbgi, value_node, get_ir_mode(type));
1117 case UNEXPR_TAKE_ADDRESS:
1118 case UNEXPR_INVALID:
1121 panic("invalid UNEXPR type found");
1124 static long get_pnc(binary_expression_type_t type)
1127 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1128 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1129 case BINEXPR_LESS: return pn_Cmp_Lt;
1130 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1131 case BINEXPR_GREATER: return pn_Cmp_Gt;
1132 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1136 panic("trying to get pn_Cmp from non-comparison binexpr type");
1139 static ir_node *create_lazy_op(const binary_expression_t *expression)
1141 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1142 type_t *type = expression->expression.datatype;
1143 ir_mode *mode = get_ir_mode(type);
1145 ir_node *cur_block = get_cur_block();
1147 ir_node *one_block = new_immBlock();
1148 ir_node *one = new_Const(mode, get_mode_one(mode));
1149 ir_node *jmp_one = new_d_Jmp(dbgi);
1151 ir_node *zero_block = new_immBlock();
1152 ir_node *zero = new_Const(mode, get_mode_null(mode));
1153 ir_node *jmp_zero = new_d_Jmp(dbgi);
1155 set_cur_block(cur_block);
1156 create_condition_evaluation((const expression_t*) expression,
1157 one_block, zero_block);
1158 mature_immBlock(one_block);
1159 mature_immBlock(zero_block);
1161 ir_node *common_block = new_immBlock();
1162 add_immBlock_pred(common_block, jmp_one);
1163 add_immBlock_pred(common_block, jmp_zero);
1164 mature_immBlock(common_block);
1166 ir_node *in[2] = { one, zero };
1167 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1172 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1173 ir_node *right, ir_mode *mode);
1175 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1176 create_arithmetic_func func)
1178 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1179 ir_node *left = expression_to_firm(expression->left);
1180 ir_node *right = expression_to_firm(expression->right);
1181 type_t *type = expression->right->base.datatype;
1182 /* be careful with the modes, because in arithmetic assign nodes only
1183 * the right operand has the mode of the arithmetic already */
1184 ir_mode *mode = get_ir_mode(type);
1185 left = create_conv(dbgi, left, mode);
1186 ir_node *res = func(dbgi, left, right, mode);
1191 static ir_node *pointer_arithmetic(ir_node *const pointer,
1194 dbg_info *const dbgi,
1195 const create_arithmetic_func func)
1197 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1198 type_t *const points_to = pointer_type->points_to;
1199 const unsigned elem_size = get_type_size(points_to);
1201 assert(elem_size >= 1);
1202 if (elem_size > 1) {
1203 integer = create_conv(dbgi, integer, mode_Is);
1204 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1205 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1209 ir_mode *const mode = get_ir_mode(type);
1210 return func(dbgi, pointer, integer, mode);
1213 static ir_node *create_arithmetic_assign_binop(
1214 const binary_expression_t *expression, create_arithmetic_func func)
1216 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1217 type_t *const type = skip_typeref(expression->expression.datatype);
1220 if (is_type_pointer(type)) {
1221 ir_node *const pointer = expression_to_firm(expression->left);
1222 ir_node * integer = expression_to_firm(expression->right);
1223 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1225 value = create_arithmetic_binop(expression, func);
1228 ir_mode *const mode = get_ir_mode(type);
1229 value = create_conv(dbgi, value, mode);
1230 set_value_for_expression(expression->left, value);
1235 static ir_node *create_add(const binary_expression_t *expression)
1237 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1238 ir_node *left = expression_to_firm(expression->left);
1239 ir_node *right = expression_to_firm(expression->right);
1240 type_t *type = expression->expression.datatype;
1242 expression_t *expr_left = expression->left;
1243 expression_t *expr_right = expression->right;
1244 type_t *type_left = skip_typeref(expr_left->base.datatype);
1245 type_t *type_right = skip_typeref(expr_right->base.datatype);
1247 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1248 ir_mode *const mode = get_ir_mode(type);
1249 return new_d_Add(dbgi, left, right, mode);
1252 if (is_type_pointer(type_left)) {
1253 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1255 assert(is_type_pointer(type_right));
1256 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1260 static ir_node *create_sub(const binary_expression_t *expression)
1262 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1263 expression_t *const expr_left = expression->left;
1264 expression_t *const expr_right = expression->right;
1265 ir_node *const left = expression_to_firm(expr_left);
1266 ir_node *const right = expression_to_firm(expr_right);
1267 type_t *const type = expression->expression.datatype;
1268 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1269 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1271 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1272 ir_mode *const mode = get_ir_mode(type);
1273 return new_d_Sub(dbgi, left, right, mode);
1274 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1275 const pointer_type_t *const ptr_type = &type_left->pointer;
1276 const unsigned elem_size = get_type_size(ptr_type->points_to);
1277 ir_mode *const mode = get_ir_mode(type);
1278 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1279 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1280 ir_node *const no_mem = new_NoMem();
1281 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1282 op_pin_state_floats);
1283 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1286 assert(is_type_pointer(type_left));
1287 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1290 static ir_node *create_shift(const binary_expression_t *expression)
1292 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1293 ir_node *left = expression_to_firm(expression->left);
1294 ir_node *right = expression_to_firm(expression->right);
1295 type_t *type = expression->expression.datatype;
1296 ir_mode *mode = get_ir_mode(type);
1298 /* firm always wants the shift count to be unsigned */
1299 right = create_conv(dbgi, right, mode_Iu);
1303 switch(expression->type) {
1304 case BINEXPR_SHIFTLEFT_ASSIGN:
1305 case BINEXPR_SHIFTLEFT:
1306 res = new_d_Shl(dbgi, left, right, mode);
1308 case BINEXPR_SHIFTRIGHT_ASSIGN:
1309 case BINEXPR_SHIFTRIGHT: {
1310 expression_t *expr_left = expression->left;
1311 type_t *type_left = skip_typeref(expr_left->base.datatype);
1313 if(is_type_signed(type_left)) {
1314 res = new_d_Shrs(dbgi, left, right, mode);
1316 res = new_d_Shr(dbgi, left, right, mode);
1321 panic("create shift op called for non-shift op");
1328 static ir_node *create_divmod(const binary_expression_t *expression)
1330 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1331 ir_node *left = expression_to_firm(expression->left);
1332 ir_node *right = expression_to_firm(expression->right);
1333 ir_node *pin = new_Pin(new_NoMem());
1334 /* be careful with the modes, because in arithmetic assign nodes only
1335 * the right operand has the mode of the arithmetic already */
1336 type_t *type = expression->right->base.datatype;
1337 ir_mode *mode = get_ir_mode(type);
1338 left = create_conv(dbgi, left, mode);
1342 switch (expression->type) {
1344 case BINEXPR_DIV_ASSIGN:
1345 if(mode_is_float(mode)) {
1346 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1347 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1349 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1350 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1355 case BINEXPR_MOD_ASSIGN:
1356 assert(!mode_is_float(mode));
1357 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1358 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1361 default: panic("unexpected binary expression type in create_divmod()");
1367 static ir_node *create_arithmetic_assign_divmod(
1368 const binary_expression_t *expression)
1370 ir_node * value = create_divmod(expression);
1371 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1372 type_t *const type = expression->expression.datatype;
1373 ir_mode *const mode = get_ir_mode(type);
1375 assert(type->type != TYPE_POINTER);
1377 value = create_conv(dbgi, value, mode);
1378 set_value_for_expression(expression->left, value);
1383 static ir_node *create_arithmetic_assign_shift(
1384 const binary_expression_t *expression)
1386 ir_node * value = create_shift(expression);
1387 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1388 type_t *const type = expression->expression.datatype;
1389 ir_mode *const mode = get_ir_mode(type);
1391 value = create_conv(dbgi, value, mode);
1392 set_value_for_expression(expression->left, value);
1397 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1399 binary_expression_type_t type = expression->type;
1402 case BINEXPR_NOTEQUAL:
1404 case BINEXPR_LESSEQUAL:
1405 case BINEXPR_GREATER:
1406 case BINEXPR_GREATEREQUAL: {
1407 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1408 ir_node *left = expression_to_firm(expression->left);
1409 ir_node *right = expression_to_firm(expression->right);
1410 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1411 long pnc = get_pnc(type);
1412 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1415 case BINEXPR_ASSIGN: {
1416 ir_node *right = expression_to_firm(expression->right);
1417 set_value_for_expression(expression->left, right);
1422 return create_add(expression);
1424 return create_sub(expression);
1426 return create_arithmetic_binop(expression, new_d_Mul);
1427 case BINEXPR_BITWISE_AND:
1428 return create_arithmetic_binop(expression, new_d_And);
1429 case BINEXPR_BITWISE_OR:
1430 return create_arithmetic_binop(expression, new_d_Or);
1431 case BINEXPR_BITWISE_XOR:
1432 return create_arithmetic_binop(expression, new_d_Eor);
1433 case BINEXPR_SHIFTLEFT:
1434 case BINEXPR_SHIFTRIGHT:
1435 return create_shift(expression);
1438 return create_divmod(expression);
1439 case BINEXPR_LOGICAL_AND:
1440 case BINEXPR_LOGICAL_OR:
1441 return create_lazy_op(expression);
1443 expression_to_firm(expression->left);
1444 return expression_to_firm(expression->right);
1445 case BINEXPR_ADD_ASSIGN:
1446 return create_arithmetic_assign_binop(expression, new_d_Add);
1447 case BINEXPR_SUB_ASSIGN:
1448 return create_arithmetic_assign_binop(expression, new_d_Sub);
1449 case BINEXPR_MUL_ASSIGN:
1450 return create_arithmetic_assign_binop(expression, new_d_Mul);
1451 case BINEXPR_DIV_ASSIGN:
1452 return create_arithmetic_assign_divmod(expression);
1453 case BINEXPR_BITWISE_AND_ASSIGN:
1454 return create_arithmetic_assign_binop(expression, new_d_And);
1455 case BINEXPR_BITWISE_OR_ASSIGN:
1456 return create_arithmetic_assign_binop(expression, new_d_Or);
1457 case BINEXPR_BITWISE_XOR_ASSIGN:
1458 return create_arithmetic_assign_binop(expression, new_d_Eor);
1459 case BINEXPR_SHIFTLEFT_ASSIGN:
1460 case BINEXPR_SHIFTRIGHT_ASSIGN:
1461 return create_arithmetic_assign_shift(expression);
1463 panic("TODO binexpr type");
1467 static ir_node *array_access_addr(const array_access_expression_t *expression)
1469 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1470 ir_node *base_addr = expression_to_firm(expression->array_ref);
1471 ir_node *offset = expression_to_firm(expression->index);
1472 offset = create_conv(dbgi, offset, mode_Iu);
1474 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1475 assert(is_type_pointer(ref_type));
1476 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1478 unsigned elem_size = get_type_size(pointer_type->points_to);
1479 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1480 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1482 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1487 static ir_node *array_access_to_firm(
1488 const array_access_expression_t *expression)
1490 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1491 ir_node *addr = array_access_addr(expression);
1492 type_t *type = revert_automatic_type_conversion(
1493 (const expression_t*) expression);
1494 type = skip_typeref(type);
1495 ir_type *irtype = get_ir_type(type);
1497 return deref_address(irtype, addr, dbgi);
1500 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1502 type_t *type = expression->type;
1504 type = expression->size_expression->base.datatype;
1505 assert(type != NULL);
1508 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1509 unsigned size = get_type_size(type);
1510 ir_node *size_node = new_Const_long(mode, size);
1515 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1517 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1519 ir_node *cur_block = get_cur_block();
1521 /* create the true block */
1522 ir_node *true_block = new_immBlock();
1524 ir_node *true_val = expression_to_firm(expression->true_expression);
1525 ir_node *true_jmp = new_Jmp();
1527 /* create the false block */
1528 ir_node *false_block = new_immBlock();
1530 ir_node *false_val = expression_to_firm(expression->false_expression);
1531 ir_node *false_jmp = new_Jmp();
1533 /* create the condition evaluation */
1534 set_cur_block(cur_block);
1535 create_condition_evaluation(expression->condition, true_block, false_block);
1536 mature_immBlock(true_block);
1537 mature_immBlock(false_block);
1539 /* create the common block */
1540 ir_node *common_block = new_immBlock();
1541 add_immBlock_pred(common_block, true_jmp);
1542 add_immBlock_pred(common_block, false_jmp);
1543 mature_immBlock(common_block);
1545 /* TODO improve static semantics, so either both or no values are NULL */
1546 if (true_val == NULL || false_val == NULL)
1549 ir_node *in[2] = { true_val, false_val };
1550 ir_mode *mode = get_irn_mode(true_val);
1551 assert(get_irn_mode(false_val) == mode);
1552 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1557 static ir_node *select_addr(const select_expression_t *expression)
1559 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1561 ir_node *compound_addr = expression_to_firm(expression->compound);
1563 declaration_t *entry = expression->compound_entry;
1564 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1565 ir_entity *entity = entry->v.entity;
1567 assert(entity != NULL);
1569 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1574 static ir_node *select_to_firm(const select_expression_t *expression)
1576 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1577 ir_node *addr = select_addr(expression);
1578 type_t *type = revert_automatic_type_conversion(
1579 (const expression_t*) expression);
1580 type = skip_typeref(type);
1581 ir_type *irtype = get_ir_type(type);
1583 return deref_address(irtype, addr, dbgi);
1586 /* Values returned by __builtin_classify_type. */
1587 typedef enum gcc_type_class
1593 enumeral_type_class,
1596 reference_type_class,
1600 function_type_class,
1611 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1613 const type_t *const type = expr->type_expression->base.datatype;
1619 const atomic_type_t *const atomic_type = &type->atomic;
1620 switch (atomic_type->atype) {
1621 // should not be reached
1622 case ATOMIC_TYPE_INVALID:
1626 // gcc cannot do that
1627 case ATOMIC_TYPE_VOID:
1628 tc = void_type_class;
1631 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1632 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1633 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1634 case ATOMIC_TYPE_SHORT:
1635 case ATOMIC_TYPE_USHORT:
1636 case ATOMIC_TYPE_INT:
1637 case ATOMIC_TYPE_UINT:
1638 case ATOMIC_TYPE_LONG:
1639 case ATOMIC_TYPE_ULONG:
1640 case ATOMIC_TYPE_LONGLONG:
1641 case ATOMIC_TYPE_ULONGLONG:
1642 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1643 tc = integer_type_class;
1646 case ATOMIC_TYPE_FLOAT:
1647 case ATOMIC_TYPE_DOUBLE:
1648 case ATOMIC_TYPE_LONG_DOUBLE:
1649 tc = real_type_class;
1652 #ifdef PROVIDE_COMPLEX
1653 case ATOMIC_TYPE_FLOAT_COMPLEX:
1654 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1655 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1656 tc = complex_type_class;
1658 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1659 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1660 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1661 tc = complex_type_class;
1666 panic("Unimplemented case in classify_type_to_firm().");
1671 case TYPE_ARRAY: // gcc handles this as pointer
1672 case TYPE_FUNCTION: // gcc handles this as pointer
1673 case TYPE_POINTER: tc = pointer_type_class; break;
1674 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1675 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1677 // gcc handles this as integer
1678 case TYPE_ENUM: tc = integer_type_class; break;
1681 panic("Unimplemented case in classify_type_to_firm().");
1684 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1685 ir_mode *const mode = mode_Is;
1686 tarval *const tv = new_tarval_from_long(tc, mode);
1687 return new_d_Const(dbgi, mode, tv);
1690 static ir_node *function_name_to_firm(
1691 const string_literal_expression_t *const expr)
1693 if (current_function_name == NULL) {
1694 const source_position_t *const src_pos =
1695 &expr->expression.source_position;
1696 const char *const name = current_function_decl->symbol->string;
1697 current_function_name = string_to_firm(src_pos, "__func__", name);
1700 return current_function_name;
1703 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1705 statement_t *statement = expr->statement;
1707 assert(statement->type == STATEMENT_COMPOUND);
1708 return compound_statement_to_firm((compound_statement_t*) statement);
1711 static ir_node *dereference_addr(const unary_expression_t *const expression)
1713 assert(expression->type == UNEXPR_DEREFERENCE);
1714 return expression_to_firm(expression->value);
1717 static ir_node *expression_to_addr(const expression_t *expression)
1719 switch(expression->type) {
1720 case EXPR_REFERENCE:
1721 return reference_addr(&expression->reference);
1722 case EXPR_ARRAY_ACCESS:
1723 return array_access_addr(&expression->array_access);
1725 return select_addr(&expression->select);
1727 return call_expression_to_firm(&expression->call);
1729 const unary_expression_t *const unary_expr = &expression->unary;
1730 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1731 return dereference_addr(unary_expr);
1738 panic("trying to get address of non-lvalue");
1741 static ir_node *_expression_to_firm(const expression_t *expression)
1743 switch(expression->type) {
1745 return const_to_firm(&expression->conste);
1746 case EXPR_STRING_LITERAL:
1747 return string_literal_to_firm(&expression->string);
1748 case EXPR_WIDE_STRING_LITERAL:
1749 return wide_string_literal_to_firm(&expression->wide_string);
1750 case EXPR_REFERENCE:
1751 return reference_expression_to_firm(&expression->reference);
1753 return call_expression_to_firm(&expression->call);
1755 return unary_expression_to_firm(&expression->unary);
1757 return binary_expression_to_firm(&expression->binary);
1758 case EXPR_ARRAY_ACCESS:
1759 return array_access_to_firm(&expression->array_access);
1761 return sizeof_to_firm(&expression->sizeofe);
1762 case EXPR_CONDITIONAL:
1763 return conditional_to_firm(&expression->conditional);
1765 return select_to_firm(&expression->select);
1766 case EXPR_CLASSIFY_TYPE:
1767 return classify_type_to_firm(&expression->classify_type);
1769 case EXPR_PRETTY_FUNCTION:
1770 return function_name_to_firm(&expression->string);
1771 case EXPR_STATEMENT:
1772 return statement_expression_to_firm(&expression->statement);
1775 case EXPR_BUILTIN_SYMBOL:
1776 panic("unimplemented expression found");
1782 panic("invalid expression found");
1785 static ir_node *expression_to_firm(const expression_t *expression)
1787 ir_node *res = _expression_to_firm(expression);
1789 if(res != NULL && get_irn_mode(res) == mode_b) {
1790 ir_mode *mode = get_ir_mode(expression->base.datatype);
1791 res = create_conv(NULL, res, mode);
1797 static ir_node *expression_to_modeb(const expression_t *expression)
1799 ir_node *res = _expression_to_firm(expression);
1800 res = create_conv(NULL, res, mode_b);
1806 * create a short-circuit expression evaluation that tries to construct
1807 * efficient control flow structures for &&, || and ! expressions
1809 static void create_condition_evaluation(const expression_t *expression,
1810 ir_node *true_block,
1811 ir_node *false_block)
1813 switch(expression->type) {
1815 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1816 if(unary_expression->type == UNEXPR_NOT) {
1817 create_condition_evaluation(unary_expression->value, false_block,
1824 binary_expression_t *binary_expression
1825 = (binary_expression_t*) expression;
1826 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1827 ir_node *cur_block = get_cur_block();
1828 ir_node *extra_block = new_immBlock();
1829 set_cur_block(cur_block);
1830 create_condition_evaluation(binary_expression->left, extra_block,
1832 mature_immBlock(extra_block);
1833 set_cur_block(extra_block);
1834 create_condition_evaluation(binary_expression->right, true_block,
1838 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1839 ir_node *cur_block = get_cur_block();
1840 ir_node *extra_block = new_immBlock();
1841 set_cur_block(cur_block);
1842 create_condition_evaluation(binary_expression->left, true_block,
1844 mature_immBlock(extra_block);
1845 set_cur_block(extra_block);
1846 create_condition_evaluation(binary_expression->right, true_block,
1856 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1857 ir_node *condition = expression_to_modeb(expression);
1858 ir_node *cond = new_d_Cond(dbgi, condition);
1859 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1860 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1862 add_immBlock_pred(true_block, true_proj);
1863 add_immBlock_pred(false_block, false_proj);
1865 set_cur_block(NULL);
1868 static void return_statement_to_firm(return_statement_t *statement)
1870 if(get_cur_block() == NULL)
1873 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1875 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1879 if(get_method_n_ress(func_irtype) > 0) {
1880 ir_type *res_type = get_method_res_type(func_irtype, 0);
1882 if(statement->return_value != NULL) {
1883 ir_node *node = expression_to_firm(statement->return_value);
1884 node = do_strict_conv(dbgi, node);
1888 if(is_compound_type(res_type)) {
1891 mode = get_type_mode(res_type);
1893 in[0] = new_Unknown(mode);
1897 /* build return_value for its side effects */
1898 if(statement->return_value != NULL) {
1899 expression_to_firm(statement->return_value);
1904 ir_node *store = get_store();
1905 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1907 ir_node *end_block = get_irg_end_block(current_ir_graph);
1908 add_immBlock_pred(end_block, ret);
1910 set_cur_block(NULL);
1913 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1915 if(get_cur_block() == NULL)
1918 return expression_to_firm(statement->expression);
1921 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1923 ir_node *result = NULL;
1924 statement_t *statement = compound->statements;
1925 for( ; statement != NULL; statement = statement->base.next) {
1926 //context2firm(&statement->context);
1928 if(statement->base.next == NULL
1929 && statement->type == STATEMENT_EXPRESSION) {
1930 result = expression_statement_to_firm(
1931 (expression_statement_t*) statement);
1934 statement_to_firm(statement);
1940 static void if_statement_to_firm(if_statement_t *statement)
1942 ir_node *cur_block = get_cur_block();
1944 ir_node *fallthrough_block = new_immBlock();
1946 /* the true (blocks) */
1947 ir_node *true_block;
1948 if (statement->true_statement != NULL) {
1949 true_block = new_immBlock();
1950 statement_to_firm(statement->true_statement);
1951 if(get_cur_block() != NULL) {
1952 ir_node *jmp = new_Jmp();
1953 add_immBlock_pred(fallthrough_block, jmp);
1956 true_block = fallthrough_block;
1959 /* the false (blocks) */
1960 ir_node *false_block;
1961 if(statement->false_statement != NULL) {
1962 false_block = new_immBlock();
1964 statement_to_firm(statement->false_statement);
1965 if(get_cur_block() != NULL) {
1966 ir_node *jmp = new_Jmp();
1967 add_immBlock_pred(fallthrough_block, jmp);
1970 false_block = fallthrough_block;
1973 /* create the condition */
1974 if(cur_block != NULL) {
1975 set_cur_block(cur_block);
1976 create_condition_evaluation(statement->condition, true_block,
1980 mature_immBlock(true_block);
1981 if(false_block != fallthrough_block) {
1982 mature_immBlock(false_block);
1984 mature_immBlock(fallthrough_block);
1986 set_cur_block(fallthrough_block);
1989 static void while_statement_to_firm(while_statement_t *statement)
1991 ir_node *jmp = NULL;
1992 if(get_cur_block() != NULL) {
1996 /* create the header block */
1997 ir_node *header_block = new_immBlock();
1999 add_immBlock_pred(header_block, jmp);
2002 /* the false block */
2003 ir_node *false_block = new_immBlock();
2006 ir_node *body_block;
2007 if (statement->body != NULL) {
2008 ir_node *old_continue_label = continue_label;
2009 ir_node *old_break_label = break_label;
2010 continue_label = header_block;
2011 break_label = false_block;
2013 body_block = new_immBlock();
2014 statement_to_firm(statement->body);
2016 assert(continue_label == header_block);
2017 assert(break_label == false_block);
2018 continue_label = old_continue_label;
2019 break_label = old_break_label;
2021 if(get_cur_block() != NULL) {
2023 add_immBlock_pred(header_block, jmp);
2026 body_block = header_block;
2029 /* create the condition */
2030 set_cur_block(header_block);
2032 create_condition_evaluation(statement->condition, body_block, false_block);
2033 mature_immBlock(body_block);
2034 mature_immBlock(false_block);
2035 mature_immBlock(header_block);
2037 set_cur_block(false_block);
2040 static void do_while_statement_to_firm(do_while_statement_t *statement)
2042 ir_node *jmp = NULL;
2043 if(get_cur_block() != NULL) {
2047 /* create the header block */
2048 ir_node *header_block = new_immBlock();
2050 /* the false block */
2051 ir_node *false_block = new_immBlock();
2054 ir_node *body_block = new_immBlock();
2056 add_immBlock_pred(body_block, jmp);
2059 if (statement->body != NULL) {
2060 ir_node *old_continue_label = continue_label;
2061 ir_node *old_break_label = break_label;
2062 continue_label = header_block;
2063 break_label = false_block;
2065 statement_to_firm(statement->body);
2067 assert(continue_label == header_block);
2068 assert(break_label == false_block);
2069 continue_label = old_continue_label;
2070 break_label = old_break_label;
2072 if (get_cur_block() == NULL) {
2073 mature_immBlock(header_block);
2074 mature_immBlock(body_block);
2075 mature_immBlock(false_block);
2080 ir_node *body_jmp = new_Jmp();
2081 add_immBlock_pred(header_block, body_jmp);
2082 mature_immBlock(header_block);
2084 /* create the condition */
2085 set_cur_block(header_block);
2087 create_condition_evaluation(statement->condition, body_block, false_block);
2088 mature_immBlock(body_block);
2089 mature_immBlock(false_block);
2090 mature_immBlock(header_block);
2092 set_cur_block(false_block);
2095 static void for_statement_to_firm(for_statement_t *statement)
2097 ir_node *jmp = NULL;
2098 if (get_cur_block() != NULL) {
2099 if(statement->initialisation != NULL) {
2100 expression_to_firm(statement->initialisation);
2105 /* create the step block */
2106 ir_node *const step_block = new_immBlock();
2107 if (statement->step != NULL) {
2108 expression_to_firm(statement->step);
2110 ir_node *const step_jmp = new_Jmp();
2112 /* create the header block */
2113 ir_node *const header_block = new_immBlock();
2115 add_immBlock_pred(header_block, jmp);
2117 add_immBlock_pred(header_block, step_jmp);
2119 /* the false block */
2120 ir_node *const false_block = new_immBlock();
2123 ir_node * body_block;
2124 if (statement->body != NULL) {
2125 ir_node *const old_continue_label = continue_label;
2126 ir_node *const old_break_label = break_label;
2127 continue_label = step_block;
2128 break_label = false_block;
2130 body_block = new_immBlock();
2131 statement_to_firm(statement->body);
2133 assert(continue_label == step_block);
2134 assert(break_label == false_block);
2135 continue_label = old_continue_label;
2136 break_label = old_break_label;
2138 if (get_cur_block() != NULL) {
2140 add_immBlock_pred(step_block, jmp);
2143 body_block = step_block;
2146 /* create the condition */
2147 set_cur_block(header_block);
2148 if (statement->condition != NULL) {
2149 create_condition_evaluation(statement->condition, body_block,
2152 keep_alive(header_block);
2154 add_immBlock_pred(body_block, jmp);
2157 mature_immBlock(body_block);
2158 mature_immBlock(false_block);
2159 mature_immBlock(step_block);
2160 mature_immBlock(header_block);
2161 mature_immBlock(false_block);
2163 set_cur_block(false_block);
2166 static void create_declaration_entity(declaration_t *declaration,
2167 declaration_type_t declaration_type,
2168 ir_type *parent_type)
2170 ident *id = new_id_from_str(declaration->symbol->string);
2171 ir_type *irtype = get_ir_type(declaration->type);
2172 ir_entity *entity = new_entity(parent_type, id, irtype);
2173 set_entity_ld_ident(entity, id);
2175 declaration->declaration_type = (unsigned char) declaration_type;
2176 declaration->v.entity = entity;
2177 set_entity_variability(entity, variability_uninitialized);
2178 /* TODO: visibility? */
2181 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2183 enum compound_graph_entry_type_t {
2184 COMPOUND_GRAPH_ENTRY_ARRAY,
2185 COMPOUND_GRAPH_ENTRY_COMPOUND
2188 struct compound_graph_path_entry_t {
2194 compound_graph_path_entry_t *prev;
2197 static void create_initializer_object(initializer_t *initializer, type_t *type,
2198 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2200 static compound_graph_path *create_compound_path(ir_type *type,
2201 compound_graph_path_entry_t *entry, int len)
2203 compound_graph_path *path = new_compound_graph_path(type, len);
2206 for( ; entry != NULL; entry = entry->prev, --i) {
2208 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2209 set_compound_graph_path_node(path, i, entry->v.entity);
2211 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2212 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2220 static void create_initializer_value(initializer_value_t *initializer,
2222 compound_graph_path_entry_t *entry,
2225 ir_node *node = expression_to_firm(initializer->value);
2226 ir_type *type = get_entity_type(entity);
2227 compound_graph_path *path = create_compound_path(type, entry, len);
2228 add_compound_ent_value_w_path(entity, node, path);
2231 static void create_initializer_compound(initializer_list_t *initializer,
2232 compound_type_t *type,
2234 compound_graph_path_entry_t *last_entry,
2237 declaration_t *compound_declaration = type->declaration;
2239 declaration_t *compound_entry = compound_declaration->context.declarations;
2241 compound_graph_path_entry_t entry;
2242 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2243 entry.prev = last_entry;
2247 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2248 if(compound_entry->symbol == NULL)
2250 if(compound_entry->namespc != NAMESPACE_NORMAL)
2253 if(i >= initializer->len)
2256 entry.v.entity = compound_entry->v.entity;
2258 initializer_t *sub_initializer = initializer->initializers[i];
2260 assert(compound_entry != NULL);
2261 assert(compound_entry->declaration_type
2262 == DECLARATION_TYPE_COMPOUND_MEMBER);
2264 if(sub_initializer->type == INITIALIZER_VALUE) {
2265 create_initializer_value(&sub_initializer->value,
2266 entity, &entry, len);
2268 type_t *entry_type = skip_typeref(compound_entry->type);
2269 create_initializer_object(sub_initializer, entry_type, entity,
2277 static void create_initializer_array(initializer_list_t *initializer,
2278 array_type_t *type, ir_entity *entity,
2279 compound_graph_path_entry_t *last_entry,
2282 type_t *element_type = type->element_type;
2283 element_type = skip_typeref(element_type);
2285 compound_graph_path_entry_t entry;
2286 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2287 entry.prev = last_entry;
2291 for(i = 0; i < initializer->len; ++i) {
2292 entry.v.array_index = i;
2294 initializer_t *sub_initializer = initializer->initializers[i];
2296 if(sub_initializer->type == INITIALIZER_VALUE) {
2297 create_initializer_value(&sub_initializer->value,
2298 entity, &entry, len);
2300 create_initializer_object(sub_initializer, element_type, entity,
2306 /* TODO: initialize rest... */
2307 if(type->size_expression != NULL) {
2308 size_t array_len = fold_constant(type->size_expression);
2309 for( ; i < array_len; ++i) {
2316 static void create_initializer_string(initializer_string_t *initializer,
2317 array_type_t *type, ir_entity *entity,
2318 compound_graph_path_entry_t *last_entry,
2321 type_t *element_type = type->element_type;
2322 element_type = skip_typeref(element_type);
2324 compound_graph_path_entry_t entry;
2325 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2326 entry.prev = last_entry;
2329 ir_type *irtype = get_entity_type(entity);
2330 size_t arr_len = get_array_type_size(type);
2331 const char *p = initializer->string;
2333 for(i = 0; i < arr_len; ++i, ++p) {
2334 entry.v.array_index = i;
2336 ir_node *node = new_Const_long(mode_Bs, *p);
2337 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2338 add_compound_ent_value_w_path(entity, node, path);
2345 static void create_initializer_object(initializer_t *initializer, type_t *type,
2346 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2348 if(is_type_array(type)) {
2349 array_type_t *array_type = &type->array;
2351 if(initializer->type == INITIALIZER_STRING) {
2352 initializer_string_t *string = &initializer->string;
2353 create_initializer_string(string, array_type, entity, entry, len);
2355 assert(initializer->type == INITIALIZER_LIST);
2356 initializer_list_t *list = &initializer->list;
2357 create_initializer_array(list, array_type, entity, entry, len);
2360 assert(initializer->type == INITIALIZER_LIST);
2361 initializer_list_t *list = &initializer->list;
2363 assert(is_type_compound(type));
2364 compound_type_t *compound_type = &type->compound;
2365 create_initializer_compound(list, compound_type, entity, entry, len);
2369 static void create_initializer_local_variable_entity(declaration_t *declaration)
2371 initializer_t *initializer = declaration->init.initializer;
2372 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2373 ir_entity *entity = declaration->v.entity;
2374 ir_node *memory = get_store();
2375 ir_node *nomem = new_NoMem();
2376 ir_node *frame = get_irg_frame(current_ir_graph);
2377 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2379 if(initializer->type == INITIALIZER_VALUE) {
2380 initializer_value_t *initializer_value = &initializer->value;
2382 ir_node *value = expression_to_firm(initializer_value->value);
2383 type_t *type = skip_typeref(declaration->type);
2384 assign_value(dbgi, addr, type, value);
2388 /* create a "template" entity which is copied to the entity on the stack */
2389 ident *id = unique_ident("initializer");
2390 ir_type *irtype = get_ir_type(declaration->type);
2391 ir_type *global_type = get_glob_type();
2392 ir_entity *init_entity = new_entity(global_type, id, irtype);
2393 set_entity_ld_ident(init_entity, id);
2395 set_entity_variability(init_entity, variability_initialized);
2396 set_entity_visibility(init_entity, visibility_local);
2398 ir_graph *old_current_ir_graph = current_ir_graph;
2399 current_ir_graph = get_const_code_irg();
2401 type_t *type = skip_typeref(declaration->type);
2402 create_initializer_object(initializer, type, init_entity, NULL, 0);
2404 assert(current_ir_graph == get_const_code_irg());
2405 current_ir_graph = old_current_ir_graph;
2407 ir_node *src_addr = create_symconst(dbgi, init_entity);
2408 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2410 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2411 set_store(copyb_mem);
2414 static void create_initializer(declaration_t *declaration)
2416 initializer_t *initializer = declaration->init.initializer;
2417 if(initializer == NULL)
2420 declaration_type_t declaration_type
2421 = (declaration_type_t) declaration->declaration_type;
2422 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2423 create_initializer_local_variable_entity(declaration);
2427 if(initializer->type == INITIALIZER_VALUE) {
2428 initializer_value_t *initializer_value = &initializer->value;
2430 ir_node *value = expression_to_firm(initializer_value->value);
2432 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2433 set_value(declaration->v.value_number, value);
2435 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2437 ir_entity *entity = declaration->v.entity;
2439 set_entity_variability(entity, variability_initialized);
2440 set_atomic_ent_value(entity, value);
2443 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2444 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2446 ir_entity *entity = declaration->v.entity;
2447 set_entity_variability(entity, variability_initialized);
2449 type_t *type = skip_typeref(declaration->type);
2450 create_initializer_object(initializer, type, entity, NULL, 0);
2454 static void create_local_variable(declaration_t *declaration)
2456 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2458 bool needs_entity = declaration->address_taken;
2459 type_t *type = skip_typeref(declaration->type);
2461 if(is_type_array(type) || is_type_compound(type)) {
2462 needs_entity = true;
2466 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2467 create_declaration_entity(declaration,
2468 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2471 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2472 declaration->v.value_number = next_value_number_function;
2473 ++next_value_number_function;
2476 create_initializer(declaration);
2479 static void create_local_static_variable(declaration_t *declaration)
2481 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2483 type_t *type = skip_typeref(declaration->type);
2484 ir_type *global_type = get_glob_type();
2485 ident *id = unique_ident(declaration->symbol->string);
2486 ir_type *irtype = get_ir_type(type);
2487 ir_entity *entity = new_entity(global_type, id, irtype);
2488 set_entity_ld_ident(entity, id);
2490 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2491 declaration->v.entity = entity;
2492 set_entity_variability(entity, variability_uninitialized);
2493 set_entity_visibility(entity, visibility_local);
2495 ir_graph *old_current_ir_graph = current_ir_graph;
2496 current_ir_graph = get_const_code_irg();
2498 create_initializer(declaration);
2500 assert(current_ir_graph == get_const_code_irg());
2501 current_ir_graph = old_current_ir_graph;
2504 static void declaration_statement_to_firm(declaration_statement_t *statement)
2506 declaration_t *declaration = statement->declarations_begin;
2507 declaration_t *end = statement->declarations_end->next;
2508 for( ; declaration != end; declaration = declaration->next) {
2509 type_t *type = skip_typeref(declaration->type);
2511 switch ((storage_class_tag_t) declaration->storage_class) {
2512 case STORAGE_CLASS_TYPEDEF:
2514 case STORAGE_CLASS_STATIC:
2515 create_local_static_variable(declaration);
2517 case STORAGE_CLASS_ENUM_ENTRY:
2518 panic("enum entry declaration in local block found");
2519 case STORAGE_CLASS_EXTERN:
2520 panic("extern declaration in local block found");
2521 case STORAGE_CLASS_NONE:
2522 case STORAGE_CLASS_AUTO:
2523 case STORAGE_CLASS_REGISTER:
2524 if(is_type_function(type)) {
2525 panic("nested functions not supported yet");
2527 create_local_variable(declaration);
2530 case STORAGE_CLASS_THREAD:
2531 case STORAGE_CLASS_THREAD_EXTERN:
2532 case STORAGE_CLASS_THREAD_STATIC:
2535 panic("invalid storage class found");
2539 static void create_jump_statement(const statement_t *statement,
2540 ir_node *target_block)
2542 if(get_cur_block() == NULL)
2545 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2546 ir_node *jump = new_d_Jmp(dbgi);
2547 add_immBlock_pred(target_block, jump);
2549 set_cur_block(NULL);
2552 static void switch_statement_to_firm(const switch_statement_t *statement)
2554 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2556 ir_node *expression = expression_to_firm(statement->expression);
2557 ir_node *cond = new_d_Cond(dbgi, expression);
2558 ir_node *break_block = new_immBlock();
2560 set_cur_block(NULL);
2562 ir_node *const old_switch_cond = current_switch_cond;
2563 ir_node *const old_break_label = break_label;
2564 const bool old_saw_default_label = saw_default_label;
2565 current_switch_cond = cond;
2566 break_label = break_block;
2568 statement_to_firm(statement->body);
2570 if(get_cur_block() != NULL) {
2571 ir_node *jmp = new_Jmp();
2572 add_immBlock_pred(break_block, jmp);
2575 if (!saw_default_label) {
2576 set_cur_block(get_nodes_block(cond));
2577 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2578 MAGIC_DEFAULT_PN_NUMBER);
2579 add_immBlock_pred(break_block, proj);
2582 assert(current_switch_cond == cond);
2583 assert(break_label == break_block);
2584 current_switch_cond = old_switch_cond;
2585 break_label = old_break_label;
2586 saw_default_label = old_saw_default_label;
2588 mature_immBlock(break_block);
2589 set_cur_block(break_block);
2592 static long fold_constant(const expression_t *expression)
2594 ir_graph *old_current_ir_graph = current_ir_graph;
2595 current_ir_graph = get_const_code_irg();
2597 ir_node *cnst = expression_to_firm(expression);
2598 if(!is_Const(cnst)) {
2599 panic("couldn't fold constantl");
2601 tarval *tv = get_Const_tarval(cnst);
2602 if(!tarval_is_long(tv)) {
2603 panic("folded constant not an integer");
2606 long res = get_tarval_long(tv);
2608 current_ir_graph = old_current_ir_graph;
2612 static void case_label_to_firm(const case_label_statement_t *statement)
2614 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2616 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2618 /* let's create a node and hope firm constant folding creates a Const
2621 set_cur_block(get_nodes_block(current_switch_cond));
2622 if(statement->expression) {
2623 long pn = fold_constant(statement->expression);
2624 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2625 /* oops someone detected our cheating... */
2626 panic("magic default pn used");
2628 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2630 saw_default_label = true;
2631 proj = new_d_defaultProj(dbgi, current_switch_cond,
2632 MAGIC_DEFAULT_PN_NUMBER);
2635 ir_node *block = new_immBlock();
2636 if (fallthrough != NULL) {
2637 add_immBlock_pred(block, fallthrough);
2639 add_immBlock_pred(block, proj);
2640 mature_immBlock(block);
2642 statement_to_firm(statement->label_statement);
2645 static ir_node *get_label_block(declaration_t *label)
2647 assert(label->namespc == NAMESPACE_LABEL);
2649 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2650 return label->v.block;
2652 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2654 ir_node *old_cur_block = get_cur_block();
2655 ir_node *block = new_immBlock();
2656 set_cur_block(old_cur_block);
2658 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2659 label->v.block = block;
2661 ARR_APP1(ir_node *, imature_blocks, block);
2666 static void label_to_firm(const label_statement_t *statement)
2668 ir_node *block = get_label_block(statement->label);
2670 if(get_cur_block() != NULL) {
2671 ir_node *jmp = new_Jmp();
2672 add_immBlock_pred(block, jmp);
2675 set_cur_block(block);
2678 statement_to_firm(statement->label_statement);
2681 static void goto_to_firm(const goto_statement_t *statement)
2683 if(get_cur_block() == NULL)
2686 ir_node *block = get_label_block(statement->label);
2687 ir_node *jmp = new_Jmp();
2688 add_immBlock_pred(block, jmp);
2690 set_cur_block(NULL);
2693 static void statement_to_firm(statement_t *statement)
2695 switch(statement->type) {
2696 case STATEMENT_INVALID:
2697 panic("invalid statement found");
2698 case STATEMENT_COMPOUND:
2699 compound_statement_to_firm(&statement->compound);
2701 case STATEMENT_RETURN:
2702 return_statement_to_firm(&statement->returns);
2704 case STATEMENT_EXPRESSION:
2705 expression_statement_to_firm(&statement->expression);
2708 if_statement_to_firm(&statement->ifs);
2710 case STATEMENT_WHILE:
2711 while_statement_to_firm(&statement->whiles);
2713 case STATEMENT_DO_WHILE:
2714 do_while_statement_to_firm(&statement->do_while);
2716 case STATEMENT_DECLARATION:
2717 declaration_statement_to_firm(&statement->declaration);
2719 case STATEMENT_BREAK:
2720 create_jump_statement(statement, break_label);
2722 case STATEMENT_CONTINUE:
2723 create_jump_statement(statement, continue_label);
2725 case STATEMENT_SWITCH:
2726 switch_statement_to_firm(&statement->switchs);
2728 case STATEMENT_CASE_LABEL:
2729 case_label_to_firm(&statement->case_label);
2732 for_statement_to_firm(&statement->fors);
2734 case STATEMENT_LABEL:
2735 label_to_firm(&statement->label);
2737 case STATEMENT_GOTO:
2738 goto_to_firm(&statement->gotos);
2743 panic("Statement not implemented\n");
2746 static int count_local_declarations(const declaration_t * decl,
2747 const declaration_t *const end)
2750 for (; decl != end; decl = decl->next) {
2751 const type_t *type = skip_typeref(decl->type);
2752 switch (type->type) {
2756 if (!decl->address_taken)
2766 static int count_decls_in_stmts(const statement_t *stmt)
2769 for (; stmt != NULL; stmt = stmt->base.next) {
2770 switch (stmt->type) {
2771 case STATEMENT_DECLARATION: {
2772 const declaration_statement_t *const decl_stmt =
2773 (const declaration_statement_t*)stmt;
2774 count += count_local_declarations(decl_stmt->declarations_begin,
2775 decl_stmt->declarations_end->next);
2779 case STATEMENT_COMPOUND: {
2780 const compound_statement_t *const comp =
2781 (const compound_statement_t*)stmt;
2782 count += count_decls_in_stmts(comp->statements);
2786 case STATEMENT_IF: {
2787 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2788 count += count_decls_in_stmts(if_stmt->true_statement);
2789 count += count_decls_in_stmts(if_stmt->false_statement);
2793 case STATEMENT_SWITCH: {
2794 const switch_statement_t *const switch_stmt =
2795 (const switch_statement_t*)stmt;
2796 count += count_decls_in_stmts(switch_stmt->body);
2800 case STATEMENT_LABEL: {
2801 const label_statement_t *const label_stmt =
2802 (const label_statement_t*)stmt;
2803 count += count_decls_in_stmts(label_stmt->label_statement);
2807 case STATEMENT_WHILE: {
2808 const while_statement_t *const while_stmt =
2809 (const while_statement_t*)stmt;
2810 count += count_decls_in_stmts(while_stmt->body);
2814 case STATEMENT_DO_WHILE: {
2815 const do_while_statement_t *const do_while_stmt =
2816 (const do_while_statement_t*)stmt;
2817 count += count_decls_in_stmts(do_while_stmt->body);
2821 case STATEMENT_FOR: {
2822 const for_statement_t *const for_stmt =
2823 (const for_statement_t*)stmt;
2824 /* TODO initialisation */
2825 count += count_decls_in_stmts(for_stmt->body);
2830 case STATEMENT_BREAK:
2831 case STATEMENT_CASE_LABEL:
2832 case STATEMENT_CONTINUE:
2833 case STATEMENT_EXPRESSION:
2834 case STATEMENT_GOTO:
2835 case STATEMENT_INVALID:
2836 case STATEMENT_RETURN:
2843 static int get_function_n_local_vars(declaration_t *declaration)
2847 /* count parameters */
2848 count += count_local_declarations(declaration->context.declarations, NULL);
2850 /* count local variables declared in body */
2851 count += count_decls_in_stmts(declaration->init.statement);
2853 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2854 * for expression statements... */
2860 static void initialize_function_parameters(declaration_t *declaration)
2862 ir_graph *irg = current_ir_graph;
2863 ir_node *args = get_irg_args(irg);
2864 ir_node *start_block = get_irg_start_block(irg);
2865 ir_type *function_irtype = get_ir_type(declaration->type);
2868 declaration_t *parameter = declaration->context.declarations;
2869 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2870 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2871 type_t *type = skip_typeref(parameter->type);
2873 bool needs_entity = parameter->address_taken;
2874 assert(!is_type_array(type));
2875 if(is_type_compound(type)) {
2876 needs_entity = true;
2880 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2881 ident *id = new_id_from_str(parameter->symbol->string);
2882 set_entity_ident(entity, id);
2884 parameter->declaration_type
2885 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2886 parameter->v.entity = entity;
2890 ir_mode *mode = get_ir_mode(parameter->type);
2892 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2894 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2895 parameter->v.value_number = next_value_number_function;
2896 ++next_value_number_function;
2898 set_value(parameter->v.value_number, proj);
2902 static void create_function(declaration_t *declaration)
2904 ir_entity *function_entity = get_function_entity(declaration);
2906 if(declaration->init.statement == NULL)
2909 current_function_decl = declaration;
2910 current_function_name = NULL;
2912 assert(imature_blocks == NULL);
2913 imature_blocks = NEW_ARR_F(ir_node*, 0);
2915 int n_local_vars = get_function_n_local_vars(declaration);
2916 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
2917 ir_node *first_block = get_cur_block();
2919 next_value_number_function = 0;
2920 initialize_function_parameters(declaration);
2922 statement_to_firm(declaration->init.statement);
2924 ir_node *end_block = get_irg_end_block(irg);
2926 /* do we have a return statement yet? */
2927 if(get_cur_block() != NULL) {
2928 type_t *type = skip_typeref(declaration->type);
2929 assert(is_type_function(type));
2930 const function_type_t *func_type = &type->function;
2931 const type_t *return_type
2932 = skip_typeref(func_type->return_type);
2935 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
2936 ret = new_Return(get_store(), 0, NULL);
2939 if(is_type_scalar(return_type)) {
2940 mode = get_ir_mode(func_type->return_type);
2946 // ยง5.1.2.2.3 main implicitly returns 0
2947 if (strcmp(declaration->symbol->string, "main") == 0) {
2948 in[0] = new_Const(mode, get_mode_null(mode));
2950 in[0] = new_Unknown(mode);
2952 ret = new_Return(get_store(), 1, in);
2954 add_immBlock_pred(end_block, ret);
2957 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2958 mature_immBlock(imature_blocks[i]);
2960 DEL_ARR_F(imature_blocks);
2961 imature_blocks = NULL;
2963 mature_immBlock(first_block);
2964 mature_immBlock(end_block);
2966 irg_finalize_cons(irg);
2968 /* finalize the frame type */
2969 ir_type *frame_type = get_irg_frame_type(irg);
2970 int n = get_compound_n_members(frame_type);
2973 for(int i = 0; i < n; ++i) {
2974 ir_entity *entity = get_compound_member(frame_type, i);
2975 ir_type *entity_type = get_entity_type(entity);
2977 int align = get_type_alignment_bytes(entity_type);
2978 if(align > align_all)
2982 misalign = offset % align;
2984 offset += align - misalign;
2988 set_entity_offset(entity, offset);
2989 offset += get_type_size_bytes(entity_type);
2991 set_type_size_bytes(frame_type, offset);
2992 set_type_alignment_bytes(frame_type, align_all);
2993 set_type_state(frame_type, layout_fixed);
2998 static void create_global_variable(declaration_t *declaration)
3002 switch ((storage_class_tag_t)declaration->storage_class) {
3003 case STORAGE_CLASS_STATIC:
3004 vis = visibility_local;
3007 case STORAGE_CLASS_EXTERN:
3008 vis = visibility_external_allocated;
3011 case STORAGE_CLASS_NONE:
3012 vis = visibility_external_visible;
3015 case STORAGE_CLASS_THREAD:
3016 vis = visibility_external_visible;
3019 case STORAGE_CLASS_THREAD_EXTERN:
3020 vis = visibility_external_allocated;
3023 case STORAGE_CLASS_THREAD_STATIC:
3024 vis = visibility_local;
3028 var_type = get_tls_type();
3032 var_type = get_glob_type();
3036 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3038 set_entity_visibility(declaration->v.entity, vis);
3040 current_ir_graph = get_const_code_irg();
3041 create_initializer(declaration);
3044 case STORAGE_CLASS_TYPEDEF:
3045 case STORAGE_CLASS_AUTO:
3046 case STORAGE_CLASS_REGISTER:
3047 case STORAGE_CLASS_ENUM_ENTRY:
3050 panic("Invalid storage class for global variable");
3053 static void context_to_firm(context_t *context)
3055 /* first pass: create declarations */
3056 declaration_t *declaration = context->declarations;
3057 for( ; declaration != NULL; declaration = declaration->next) {
3058 if(declaration->namespc != NAMESPACE_NORMAL)
3060 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3061 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3063 if(declaration->symbol == NULL)
3066 type_t *type = skip_typeref(declaration->type);
3067 if(is_type_function(type)) {
3068 get_function_entity(declaration);
3070 create_global_variable(declaration);
3074 /* second pass: create code */
3075 declaration = context->declarations;
3076 for( ; declaration != NULL; declaration = declaration->next) {
3077 if(declaration->namespc != NAMESPACE_NORMAL)
3079 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3080 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3082 if(declaration->symbol == NULL)
3085 type_t *type = declaration->type;
3086 if(type->type != TYPE_FUNCTION)
3089 create_function(declaration);
3093 void translation_unit_to_firm(translation_unit_t *unit)
3095 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3096 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3097 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3099 ir_type_int = get_ir_type(type_int);
3100 ir_type_const_char = get_ir_type(type_const_char);
3101 ir_type_wchar_t = get_ir_type(type_wchar_t);
3102 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3105 type_void->base.firm_type = ir_type_void;
3107 /* just to be sure */
3108 continue_label = NULL;
3110 current_switch_cond = NULL;
3112 context_to_firm(& unit->context);