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);
230 return get_type_size(type->builtin.real_type);
236 panic("Trying to determine size of invalid type");
239 static unsigned count_parameters(const function_type_t *function_type)
243 function_parameter_t *parameter = function_type->parameters;
244 for ( ; parameter != NULL; parameter = parameter->next) {
254 static long fold_constant(const expression_t *expression);
256 static ir_type *create_atomic_type(const atomic_type_t *type)
258 ir_mode *mode = get_atomic_mode(type);
259 ident *id = get_mode_ident(mode);
260 ir_type *irtype = new_type_primitive(id, mode);
265 static ir_type *create_method_type(const function_type_t *function_type)
267 type_t *return_type = function_type->return_type;
269 ident *id = unique_ident("functiontype");
270 int n_parameters = count_parameters(function_type);
271 int n_results = return_type == type_void ? 0 : 1;
272 ir_type *irtype = new_type_method(id, n_parameters, n_results);
274 if(return_type != type_void) {
275 ir_type *restype = get_ir_type(return_type);
276 set_method_res_type(irtype, 0, restype);
279 function_parameter_t *parameter = function_type->parameters;
281 for( ; parameter != NULL; parameter = parameter->next) {
282 ir_type *p_irtype = get_ir_type(parameter->type);
283 set_method_param_type(irtype, n, p_irtype);
287 if(function_type->variadic || function_type->unspecified_parameters) {
288 set_method_variadicity(irtype, variadicity_variadic);
294 static ir_type *create_pointer_type(pointer_type_t *type)
296 type_t *points_to = type->points_to;
297 ir_type *ir_points_to;
298 /* Avoid endless recursion if the points_to type contains this poiner type
299 * again (might be a struct). We therefore first create a void* pointer
300 * and then set the real points_to type
302 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
303 ir_type_void, mode_P_data);
304 type->type.firm_type = ir_type;
306 ir_points_to = get_ir_type(points_to);
307 set_pointer_points_to_type(ir_type, ir_points_to);
312 static ir_type *create_array_type(array_type_t *type)
314 type_t *element_type = type->element_type;
315 ir_type *ir_element_type = get_ir_type(element_type);
317 ident *id = unique_ident("array");
318 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
320 if(type->size != NULL) {
321 int n_elements = fold_constant(type->size);
323 set_array_bounds_int(ir_type, 0, 0, n_elements);
325 size_t elemsize = get_type_size_bytes(ir_element_type);
326 int align = get_type_alignment_bytes(ir_element_type);
327 if(elemsize % align > 0) {
328 elemsize += align - (elemsize % align);
330 set_type_size_bytes(ir_type, n_elements * elemsize);
331 set_type_alignment_bytes(ir_type, align);
332 set_type_state(ir_type, layout_fixed);
338 #define INVALID_TYPE ((ir_type_ptr)-1)
340 static ir_type *create_struct_type(compound_type_t *type)
342 symbol_t *symbol = type->declaration->symbol;
345 id = unique_ident(symbol->string);
347 id = unique_ident("__anonymous_struct");
349 ir_type *ir_type = new_type_struct(id);
351 type->type.firm_type = ir_type;
355 declaration_t *entry = type->declaration->context.declarations;
356 for( ; entry != NULL; entry = entry->next) {
357 if(entry->namespc != NAMESPACE_NORMAL)
360 ident *ident = new_id_from_str(entry->symbol->string);
361 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
363 int entry_size = get_type_size_bytes(entry_ir_type);
364 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
365 int misalign = offset % entry_alignment;
367 offset += entry_alignment - misalign;
369 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
370 set_entity_offset(entity, offset);
371 add_struct_member(ir_type, entity);
372 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
373 entry->v.entity = entity;
375 offset += entry_size;
376 if(entry_alignment > align_all) {
377 if(entry_alignment % align_all != 0) {
378 panic("Uneven alignments not supported yet");
380 align_all = entry_alignment;
384 int misalign = offset % align_all;
386 set_type_alignment_bytes(ir_type, align_all);
387 set_type_size_bytes(ir_type, offset);
388 set_type_state(ir_type, layout_fixed);
393 static ir_type *create_union_type(compound_type_t *type)
395 declaration_t *declaration = type->declaration;
396 symbol_t *symbol = declaration->symbol;
399 id = unique_ident(symbol->string);
401 id = unique_ident("__anonymous_union");
403 ir_type *ir_type = new_type_union(id);
405 type->type.firm_type = ir_type;
409 declaration_t *entry = declaration->context.declarations;
410 for( ; entry != NULL; entry = entry->next) {
411 if(entry->namespc != NAMESPACE_NORMAL)
414 ident *ident = new_id_from_str(entry->symbol->string);
415 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
417 int entry_size = get_type_size_bytes(entry_ir_type);
418 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
420 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
421 add_union_member(ir_type, entity);
422 set_entity_offset(entity, 0);
423 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
424 entry->v.entity = entity;
426 if(entry_size > size) {
429 if(entry_alignment > align_all) {
430 if(entry_alignment % align_all != 0) {
431 panic("Uneven alignments not supported yet");
433 align_all = entry_alignment;
437 set_type_alignment_bytes(ir_type, align_all);
438 set_type_size_bytes(ir_type, size);
439 set_type_state(ir_type, layout_fixed);
444 static ir_node *expression_to_firm(const expression_t *expression);
445 static inline ir_mode *get_ir_mode(type_t *type);
447 static ir_type *create_enum_type(enum_type_t *const type)
449 type->type.firm_type = ir_type_int;
451 ir_mode *const mode = get_ir_mode((type_t*) type);
452 tarval *const one = get_mode_one(mode);
453 tarval * tv_next = get_tarval_null(mode);
455 declaration_t *declaration = type->declaration->next;
456 for (; declaration != NULL; declaration = declaration->next) {
457 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
460 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
462 expression_t *const init = declaration->init.enum_value;
464 ir_node *const cnst = expression_to_firm(init);
465 if (!is_Const(cnst)) {
466 panic("couldn't fold constant");
468 tv_next = get_Const_tarval(cnst);
470 declaration->v.enum_val = tv_next;
471 tv_next = tarval_add(tv_next, one);
477 static ir_type *get_ir_type(type_t *type)
479 assert(type != NULL);
481 type = skip_typeref(type);
483 if(type->base.firm_type != NULL) {
484 assert(type->base.firm_type != INVALID_TYPE);
485 return type->base.firm_type;
488 ir_type *firm_type = NULL;
491 firm_type = create_atomic_type(&type->atomic);
494 firm_type = create_method_type(&type->function);
497 firm_type = create_pointer_type(&type->pointer);
500 firm_type = create_array_type(&type->array);
502 case TYPE_COMPOUND_STRUCT:
503 firm_type = create_struct_type(&type->compound);
505 case TYPE_COMPOUND_UNION:
506 firm_type = create_union_type(&type->compound);
509 firm_type = create_enum_type(&type->enumt);
512 firm_type = get_ir_type(type->builtin.real_type);
519 if(firm_type == NULL)
520 panic("unknown type found");
522 type->base.firm_type = firm_type;
526 static inline ir_mode *get_ir_mode(type_t *type)
528 ir_type *irtype = get_ir_type(type);
530 /* firm doesn't report a mode for arrays somehow... */
531 if(is_Array_type(irtype)) {
535 ir_mode *mode = get_type_mode(irtype);
536 assert(mode != NULL);
540 static ir_entity* get_function_entity(declaration_t *declaration)
542 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
543 return declaration->v.entity;
544 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
546 symbol_t *symbol = declaration->symbol;
547 ident *id = new_id_from_str(symbol->string);
549 ir_type *global_type = get_glob_type();
550 ir_type *ir_type_method = get_ir_type(declaration->type);
551 assert(is_Method_type(ir_type_method));
553 ir_entity *entity = new_entity(global_type, id, ir_type_method);
554 set_entity_ld_ident(entity, id);
555 if(declaration->storage_class == STORAGE_CLASS_STATIC
556 || declaration->is_inline) {
557 set_entity_visibility(entity, visibility_local);
558 } else if(declaration->init.statement != NULL) {
559 set_entity_visibility(entity, visibility_external_visible);
561 set_entity_visibility(entity, visibility_external_allocated);
564 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
565 declaration->v.entity = entity;
570 static dbg_info *get_dbg_info(const source_position_t *pos)
572 return (dbg_info*) pos;
575 static ir_node *const_to_firm(const const_expression_t *cnst)
577 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
578 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
583 if(mode_is_float(mode)) {
584 tv = new_tarval_from_double(cnst->v.float_value, mode);
586 if(mode_is_signed(mode)) {
587 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
589 len = snprintf(buf, sizeof(buf), "%llu",
590 (unsigned long long) cnst->v.int_value);
592 tv = new_tarval_from_str(buf, len, mode);
595 return new_d_Const(dbgi, mode, tv);
598 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
600 assert(entity != NULL);
601 union symconst_symbol sym;
602 sym.entity_p = entity;
603 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
606 static ir_node *string_to_firm(const source_position_t *const src_pos,
607 const char *const id_prefix,
608 const char *const string)
610 ir_type *const global_type = get_glob_type();
611 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
614 ident *const id = unique_ident(id_prefix);
615 ir_entity *const entity = new_entity(global_type, id, type);
616 set_entity_ld_ident(entity, id);
617 set_entity_variability(entity, variability_constant);
619 ir_type *const elem_type = ir_type_const_char;
620 ir_mode *const mode = get_type_mode(elem_type);
622 const size_t slen = strlen(string) + 1;
624 set_array_lower_bound_int(type, 0, 0);
625 set_array_upper_bound_int(type, 0, slen);
626 set_type_size_bytes(type, slen);
627 set_type_state(type, layout_fixed);
629 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
630 for(size_t i = 0; i < slen; ++i) {
631 tvs[i] = new_tarval_from_long(string[i], mode);
634 set_array_entity_values(entity, tvs, slen);
637 dbg_info *const dbgi = get_dbg_info(src_pos);
639 return create_symconst(dbgi, entity);
642 static ir_node *string_literal_to_firm(
643 const string_literal_expression_t* literal)
645 return string_to_firm(&literal->expression.source_position, "Lstr",
649 static ir_node *wide_string_literal_to_firm(
650 const wide_string_literal_expression_t* const literal)
652 ir_type *const global_type = get_glob_type();
653 ir_type *const elem_type = ir_type_wchar_t;
654 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
657 ident *const id = unique_ident("Lstr");
658 ir_entity *const entity = new_entity(global_type, id, type);
659 set_entity_ld_ident(entity, id);
660 set_entity_variability(entity, variability_constant);
662 ir_mode *const mode = get_type_mode(elem_type);
664 const wchar_rep_t *const string = literal->value.begin;
665 const size_t slen = literal->value.size;
667 set_array_lower_bound_int(type, 0, 0);
668 set_array_upper_bound_int(type, 0, slen);
669 set_type_size_bytes(type, slen);
670 set_type_state(type, layout_fixed);
672 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
673 for(size_t i = 0; i < slen; ++i) {
674 tvs[i] = new_tarval_from_long(string[i], mode);
677 set_array_entity_values(entity, tvs, slen);
680 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
682 return create_symconst(dbgi, entity);
685 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
686 dbg_info *const dbgi)
688 if(is_compound_type(irtype) || is_Array_type(irtype)) {
692 ir_mode *const mode = get_type_mode(irtype);
693 ir_node *const memory = get_store();
694 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
695 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
696 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
701 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
703 ir_mode *mode = get_irn_mode(node);
705 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
707 if(!mode_is_float(mode))
710 /* check if there is already a Conv */
711 if (get_irn_op(node) == op_Conv) {
712 /* convert it into a strict Conv */
713 set_Conv_strict(node, 1);
717 /* otherwise create a new one */
718 return new_d_strictConv(dbgi, node, mode);
721 static ir_node *get_global_var_address(dbg_info *const dbgi,
722 const declaration_t *const decl)
724 assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
726 ir_entity *const entity = decl->v.entity;
727 switch ((storage_class_tag_t)decl->storage_class) {
728 case STORAGE_CLASS_THREAD:
729 case STORAGE_CLASS_THREAD_EXTERN:
730 case STORAGE_CLASS_THREAD_STATIC: {
731 ir_node *const no_mem = new_NoMem();
732 ir_node *const tls = get_irg_tls(current_ir_graph);
733 return new_d_simpleSel(dbgi, no_mem, tls, entity);
737 return create_symconst(dbgi, entity);
741 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
743 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
744 declaration_t *declaration = ref->declaration;
745 type_t *type = skip_typeref(declaration->type);
747 switch((declaration_type_t) declaration->declaration_type) {
748 case DECLARATION_TYPE_UNKNOWN:
749 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
755 case DECLARATION_TYPE_ENUM_ENTRY: {
756 ir_mode *const mode = get_ir_mode(type);
757 return new_Const(mode, declaration->v.enum_val);
760 case DECLARATION_TYPE_LOCAL_VARIABLE: {
761 ir_mode *mode = get_ir_mode(type);
762 return get_value(declaration->v.value_number, mode);
764 case DECLARATION_TYPE_FUNCTION: {
765 return create_symconst(dbgi, declaration->v.entity);
767 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
768 ir_node *const addr = get_global_var_address(dbgi, declaration);
769 ir_type *const irtype = get_entity_type(declaration->v.entity);
770 return deref_address(irtype, addr, dbgi);
773 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
774 ir_entity *entity = declaration->v.entity;
775 ir_node *frame = get_irg_frame(current_ir_graph);
776 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
777 ir_type *irtype = get_entity_type(entity);
778 return deref_address(irtype, sel, dbgi);
781 case DECLARATION_TYPE_COMPOUND_MEMBER:
782 case DECLARATION_TYPE_LABEL_BLOCK:
783 panic("not implemented reference type");
786 panic("reference to declaration with unknown type found");
789 static ir_node *reference_addr(const reference_expression_t *ref)
791 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
792 declaration_t *declaration = ref->declaration;
794 switch((declaration_type_t) declaration->declaration_type) {
795 case DECLARATION_TYPE_UNKNOWN:
797 case DECLARATION_TYPE_LOCAL_VARIABLE:
798 panic("local variable without entity has no address");
799 case DECLARATION_TYPE_FUNCTION: {
800 return create_symconst(dbgi, declaration->v.entity);
802 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
803 ir_node *const addr = get_global_var_address(dbgi, declaration);
806 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
807 ir_entity *entity = declaration->v.entity;
808 ir_node *frame = get_irg_frame(current_ir_graph);
809 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
814 case DECLARATION_TYPE_ENUM_ENTRY:
815 panic("trying to reference enum entry");
817 case DECLARATION_TYPE_COMPOUND_MEMBER:
818 case DECLARATION_TYPE_LABEL_BLOCK:
819 panic("not implemented reference type");
822 panic("reference to declaration with unknown type found");
825 static ir_node *process_builtin_call(const call_expression_t *call)
827 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
829 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
830 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
832 type_t *type = skip_typeref(builtin->expression.datatype);
833 assert(is_type_pointer(type));
835 type_t *function_type = skip_typeref(type->pointer.points_to);
836 symbol_t *symbol = builtin->symbol;
839 case T___builtin_alloca: {
840 if(call->arguments == NULL || call->arguments->next != NULL) {
841 panic("invalid number of parameters on __builtin_alloca");
843 expression_t *argument = call->arguments->expression;
844 ir_node *size = expression_to_firm(argument);
846 ir_node *store = get_store();
847 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
849 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
851 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
855 case T___builtin_nan:
856 case T___builtin_nanf:
857 case T___builtin_nand: {
858 /* Ignore string for now... */
859 assert(is_type_function(function_type));
860 ir_mode *mode = get_ir_mode(function_type->function.return_type);
861 tarval *tv = get_mode_NAN(mode);
862 ir_node *res = new_d_Const(dbgi, mode, tv);
865 case T___builtin_va_end:
868 panic("Unsupported builtin found\n");
872 static ir_node *call_expression_to_firm(const call_expression_t *call)
874 assert(get_cur_block() != NULL);
876 expression_t *function = call->function;
877 if(function->type == EXPR_BUILTIN_SYMBOL) {
878 return process_builtin_call(call);
880 ir_node *callee = expression_to_firm(function);
882 type_t *type = skip_typeref(function->base.datatype);
883 assert(is_type_pointer(type));
884 pointer_type_t *pointer_type = &type->pointer;
885 type_t *points_to = skip_typeref(pointer_type->points_to);
886 assert(is_type_function(points_to));
887 function_type_t *function_type = &points_to->function;
889 int n_parameters = 0;
890 call_argument_t *argument = call->arguments;
891 for( ; argument != NULL; argument = argument->next) {
895 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
897 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
898 ir_type *new_method_type = NULL;
899 if(function_type->variadic || function_type->unspecified_parameters) {
900 /* we need to construct a new method type matching the call
902 int n_res = get_method_n_ress(ir_method_type);
903 new_method_type = new_type_method(unique_ident("calltype"),
904 n_parameters, n_res);
905 set_method_calling_convention(new_method_type,
906 get_method_calling_convention(ir_method_type));
907 set_method_additional_properties(new_method_type,
908 get_method_additional_properties(ir_method_type));
910 for(int i = 0; i < n_res; ++i) {
911 set_method_res_type(new_method_type, i,
912 get_method_res_type(ir_method_type, i));
915 ir_node *in[n_parameters];
917 argument = call->arguments;
919 for( ; argument != NULL; argument = argument->next) {
920 expression_t *expression = argument->expression;
921 ir_node *arg_node = expression_to_firm(expression);
923 arg_node = do_strict_conv(dbgi, arg_node);
926 if(new_method_type != NULL) {
927 ir_type *irtype = get_ir_type(expression->base.datatype);
928 set_method_param_type(new_method_type, n, irtype);
933 assert(n == n_parameters);
935 if(new_method_type != NULL)
936 ir_method_type = new_method_type;
938 ir_node *store = get_store();
939 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
941 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
944 type_t *return_type = skip_typeref(function_type->return_type);
945 ir_node *result = NULL;
947 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
949 if(is_type_scalar(return_type)) {
950 mode = get_ir_mode(return_type);
954 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
955 result = new_d_Proj(dbgi, resproj, mode, 0);
961 static void statement_to_firm(statement_t *statement);
962 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
964 static ir_node *expression_to_addr(const expression_t *expression);
965 static void create_condition_evaluation(const expression_t *expression,
967 ir_node *false_block);
969 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
972 value = do_strict_conv(dbgi, value);
974 ir_node *memory = get_store();
976 if(is_type_scalar(type)) {
977 ir_node *store = new_d_Store(dbgi, memory, addr, value);
978 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
979 set_store(store_mem);
981 ir_type *irtype = get_ir_type(type);
982 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
983 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
984 set_store(copyb_mem);
988 static void set_value_for_expression(const expression_t *expression,
991 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
992 value = do_strict_conv(dbgi, value);
994 if(expression->type == EXPR_REFERENCE) {
995 reference_expression_t *ref = (reference_expression_t*) expression;
997 declaration_t *declaration = ref->declaration;
998 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
999 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1000 set_value(declaration->v.value_number, value);
1005 ir_node *addr = expression_to_addr(expression);
1006 type_t *type = skip_typeref(expression->base.datatype);
1007 assign_value(dbgi, addr, type, value);
1010 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1012 ir_mode *value_mode = get_irn_mode(value);
1014 if (value_mode == dest_mode || is_Bad(value))
1017 if(dest_mode == mode_b) {
1018 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1019 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1020 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1024 return new_d_Conv(dbgi, value, dest_mode);
1027 static ir_node *create_incdec(const unary_expression_t *expression)
1029 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1030 type_t *type = skip_typeref(expression->expression.datatype);
1031 ir_mode *mode = get_ir_mode(type);
1032 expression_t *value = expression->value;
1034 ir_node *value_node = expression_to_firm(value);
1037 if(is_type_pointer(type)) {
1038 pointer_type_t *pointer_type = &type->pointer;
1039 unsigned elem_size = get_type_size(pointer_type->points_to);
1040 offset = new_Const_long(mode_Is, elem_size);
1042 assert(is_type_arithmetic(type));
1043 offset = new_Const(mode, get_mode_one(mode));
1046 switch(expression->type) {
1047 case UNEXPR_POSTFIX_INCREMENT: {
1048 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1049 set_value_for_expression(value, new_value);
1052 case UNEXPR_POSTFIX_DECREMENT: {
1053 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1054 set_value_for_expression(value, new_value);
1057 case UNEXPR_PREFIX_INCREMENT: {
1058 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1059 set_value_for_expression(value, new_value);
1062 case UNEXPR_PREFIX_DECREMENT: {
1063 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1064 set_value_for_expression(value, new_value);
1068 panic("no incdec expr in create_incdec");
1073 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1075 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1076 type_t *type = skip_typeref(expression->expression.datatype);
1077 ir_mode *mode = get_ir_mode(type);
1079 if(expression->type == UNEXPR_TAKE_ADDRESS)
1080 return expression_to_addr(expression->value);
1082 const expression_t *value = expression->value;
1083 ir_node *value_node = expression_to_firm(value);
1085 switch(expression->type) {
1087 return new_d_Minus(dbgi, value_node, mode);
1090 case UNEXPR_BITWISE_NEGATE:
1091 return new_d_Not(dbgi, value_node, mode);
1093 if(get_irn_mode(value_node) != mode_b) {
1094 value_node = create_conv(dbgi, value_node, mode_b);
1096 value_node = new_d_Not(dbgi, value_node, mode_b);
1097 if(mode != mode_b) {
1098 value_node = create_conv(dbgi, value_node, mode);
1102 case UNEXPR_DEREFERENCE: {
1103 type_t *value_type = skip_typeref(value->base.datatype);
1104 ir_type *irtype = get_ir_type(value_type);
1105 assert(is_Pointer_type(irtype));
1106 ir_type *points_to = get_pointer_points_to_type(irtype);
1107 return deref_address(points_to, value_node, dbgi);
1109 case UNEXPR_POSTFIX_INCREMENT:
1110 case UNEXPR_POSTFIX_DECREMENT:
1111 case UNEXPR_PREFIX_INCREMENT:
1112 case UNEXPR_PREFIX_DECREMENT:
1113 return create_incdec(expression);
1115 ir_node *node = create_conv(dbgi, value_node, get_ir_mode(type));
1116 node = do_strict_conv(dbgi, node);
1119 case UNEXPR_CAST_IMPLICIT:
1120 return create_conv(dbgi, value_node, get_ir_mode(type));
1122 case UNEXPR_TAKE_ADDRESS:
1123 case UNEXPR_INVALID:
1126 panic("invalid UNEXPR type found");
1129 static long get_pnc(binary_expression_type_t type)
1132 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1133 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1134 case BINEXPR_LESS: return pn_Cmp_Lt;
1135 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1136 case BINEXPR_GREATER: return pn_Cmp_Gt;
1137 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1141 panic("trying to get pn_Cmp from non-comparison binexpr type");
1144 static ir_node *create_lazy_op(const binary_expression_t *expression)
1146 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1147 type_t *type = expression->expression.datatype;
1148 ir_mode *mode = get_ir_mode(type);
1150 ir_node *cur_block = get_cur_block();
1152 ir_node *one_block = new_immBlock();
1153 ir_node *one = new_Const(mode, get_mode_one(mode));
1154 ir_node *jmp_one = new_d_Jmp(dbgi);
1156 ir_node *zero_block = new_immBlock();
1157 ir_node *zero = new_Const(mode, get_mode_null(mode));
1158 ir_node *jmp_zero = new_d_Jmp(dbgi);
1160 set_cur_block(cur_block);
1161 create_condition_evaluation((const expression_t*) expression,
1162 one_block, zero_block);
1163 mature_immBlock(one_block);
1164 mature_immBlock(zero_block);
1166 ir_node *common_block = new_immBlock();
1167 add_immBlock_pred(common_block, jmp_one);
1168 add_immBlock_pred(common_block, jmp_zero);
1169 mature_immBlock(common_block);
1171 ir_node *in[2] = { one, zero };
1172 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1177 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1178 ir_node *right, ir_mode *mode);
1180 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1181 create_arithmetic_func func)
1183 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1184 ir_node *left = expression_to_firm(expression->left);
1185 ir_node *right = expression_to_firm(expression->right);
1186 type_t *type = expression->right->base.datatype;
1187 /* be careful with the modes, because in arithmetic assign nodes only
1188 * the right operand has the mode of the arithmetic already */
1189 ir_mode *mode = get_ir_mode(type);
1190 left = create_conv(dbgi, left, mode);
1191 ir_node *res = func(dbgi, left, right, mode);
1196 static ir_node *pointer_arithmetic(ir_node *const pointer,
1199 dbg_info *const dbgi,
1200 const create_arithmetic_func func)
1202 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1203 type_t *const points_to = pointer_type->points_to;
1204 const unsigned elem_size = get_type_size(points_to);
1206 assert(elem_size >= 1);
1207 if (elem_size > 1) {
1208 integer = create_conv(dbgi, integer, mode_Is);
1209 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1210 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1214 ir_mode *const mode = get_ir_mode(type);
1215 return func(dbgi, pointer, integer, mode);
1218 static ir_node *create_arithmetic_assign_binop(
1219 const binary_expression_t *expression, create_arithmetic_func func)
1221 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1222 type_t *const type = skip_typeref(expression->expression.datatype);
1225 if (is_type_pointer(type)) {
1226 ir_node *const pointer = expression_to_firm(expression->left);
1227 ir_node * integer = expression_to_firm(expression->right);
1228 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1230 value = create_arithmetic_binop(expression, func);
1233 ir_mode *const mode = get_ir_mode(type);
1234 value = create_conv(dbgi, value, mode);
1235 set_value_for_expression(expression->left, value);
1240 static ir_node *create_add(const binary_expression_t *expression)
1242 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1243 ir_node *left = expression_to_firm(expression->left);
1244 ir_node *right = expression_to_firm(expression->right);
1245 type_t *type = expression->expression.datatype;
1247 expression_t *expr_left = expression->left;
1248 expression_t *expr_right = expression->right;
1249 type_t *type_left = skip_typeref(expr_left->base.datatype);
1250 type_t *type_right = skip_typeref(expr_right->base.datatype);
1252 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1253 ir_mode *const mode = get_ir_mode(type);
1254 return new_d_Add(dbgi, left, right, mode);
1257 if (is_type_pointer(type_left)) {
1258 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1260 assert(is_type_pointer(type_right));
1261 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1265 static ir_node *create_sub(const binary_expression_t *expression)
1267 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1268 expression_t *const expr_left = expression->left;
1269 expression_t *const expr_right = expression->right;
1270 ir_node *const left = expression_to_firm(expr_left);
1271 ir_node *const right = expression_to_firm(expr_right);
1272 type_t *const type = expression->expression.datatype;
1273 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1274 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1276 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1277 ir_mode *const mode = get_ir_mode(type);
1278 return new_d_Sub(dbgi, left, right, mode);
1279 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1280 const pointer_type_t *const ptr_type = &type_left->pointer;
1281 const unsigned elem_size = get_type_size(ptr_type->points_to);
1282 ir_mode *const mode = get_ir_mode(type);
1283 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1284 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1285 ir_node *const no_mem = new_NoMem();
1286 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1287 op_pin_state_floats);
1288 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1291 assert(is_type_pointer(type_left));
1292 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1295 static ir_node *create_shift(const binary_expression_t *expression)
1297 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1298 ir_node *left = expression_to_firm(expression->left);
1299 ir_node *right = expression_to_firm(expression->right);
1300 type_t *type = expression->expression.datatype;
1301 ir_mode *mode = get_ir_mode(type);
1303 /* firm always wants the shift count to be unsigned */
1304 right = create_conv(dbgi, right, mode_Iu);
1308 switch(expression->type) {
1309 case BINEXPR_SHIFTLEFT_ASSIGN:
1310 case BINEXPR_SHIFTLEFT:
1311 res = new_d_Shl(dbgi, left, right, mode);
1313 case BINEXPR_SHIFTRIGHT_ASSIGN:
1314 case BINEXPR_SHIFTRIGHT: {
1315 expression_t *expr_left = expression->left;
1316 type_t *type_left = skip_typeref(expr_left->base.datatype);
1318 if(is_type_signed(type_left)) {
1319 res = new_d_Shrs(dbgi, left, right, mode);
1321 res = new_d_Shr(dbgi, left, right, mode);
1326 panic("create shift op called for non-shift op");
1333 static ir_node *create_divmod(const binary_expression_t *expression)
1335 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1336 ir_node *left = expression_to_firm(expression->left);
1337 ir_node *right = expression_to_firm(expression->right);
1338 ir_node *pin = new_Pin(new_NoMem());
1339 /* be careful with the modes, because in arithmetic assign nodes only
1340 * the right operand has the mode of the arithmetic already */
1341 type_t *type = expression->right->base.datatype;
1342 ir_mode *mode = get_ir_mode(type);
1343 left = create_conv(dbgi, left, mode);
1347 switch (expression->type) {
1349 case BINEXPR_DIV_ASSIGN:
1350 if(mode_is_float(mode)) {
1351 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1352 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1354 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1355 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1360 case BINEXPR_MOD_ASSIGN:
1361 assert(!mode_is_float(mode));
1362 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1363 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1366 default: panic("unexpected binary expression type in create_divmod()");
1372 static ir_node *create_arithmetic_assign_divmod(
1373 const binary_expression_t *expression)
1375 ir_node * value = create_divmod(expression);
1376 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1377 type_t *const type = expression->expression.datatype;
1378 ir_mode *const mode = get_ir_mode(type);
1380 assert(type->type != TYPE_POINTER);
1382 value = create_conv(dbgi, value, mode);
1383 set_value_for_expression(expression->left, value);
1388 static ir_node *create_arithmetic_assign_shift(
1389 const binary_expression_t *expression)
1391 ir_node * value = create_shift(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 value = create_conv(dbgi, value, mode);
1397 set_value_for_expression(expression->left, value);
1402 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1404 binary_expression_type_t type = expression->type;
1407 case BINEXPR_NOTEQUAL:
1409 case BINEXPR_LESSEQUAL:
1410 case BINEXPR_GREATER:
1411 case BINEXPR_GREATEREQUAL: {
1412 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1413 ir_node *left = expression_to_firm(expression->left);
1414 ir_node *right = expression_to_firm(expression->right);
1415 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1416 long pnc = get_pnc(type);
1417 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1420 case BINEXPR_ASSIGN: {
1421 ir_node *right = expression_to_firm(expression->right);
1422 set_value_for_expression(expression->left, right);
1427 return create_add(expression);
1429 return create_sub(expression);
1431 return create_arithmetic_binop(expression, new_d_Mul);
1432 case BINEXPR_BITWISE_AND:
1433 return create_arithmetic_binop(expression, new_d_And);
1434 case BINEXPR_BITWISE_OR:
1435 return create_arithmetic_binop(expression, new_d_Or);
1436 case BINEXPR_BITWISE_XOR:
1437 return create_arithmetic_binop(expression, new_d_Eor);
1438 case BINEXPR_SHIFTLEFT:
1439 case BINEXPR_SHIFTRIGHT:
1440 return create_shift(expression);
1443 return create_divmod(expression);
1444 case BINEXPR_LOGICAL_AND:
1445 case BINEXPR_LOGICAL_OR:
1446 return create_lazy_op(expression);
1448 expression_to_firm(expression->left);
1449 return expression_to_firm(expression->right);
1450 case BINEXPR_ADD_ASSIGN:
1451 return create_arithmetic_assign_binop(expression, new_d_Add);
1452 case BINEXPR_SUB_ASSIGN:
1453 return create_arithmetic_assign_binop(expression, new_d_Sub);
1454 case BINEXPR_MUL_ASSIGN:
1455 return create_arithmetic_assign_binop(expression, new_d_Mul);
1456 case BINEXPR_DIV_ASSIGN:
1457 return create_arithmetic_assign_divmod(expression);
1458 case BINEXPR_BITWISE_AND_ASSIGN:
1459 return create_arithmetic_assign_binop(expression, new_d_And);
1460 case BINEXPR_BITWISE_OR_ASSIGN:
1461 return create_arithmetic_assign_binop(expression, new_d_Or);
1462 case BINEXPR_BITWISE_XOR_ASSIGN:
1463 return create_arithmetic_assign_binop(expression, new_d_Eor);
1464 case BINEXPR_SHIFTLEFT_ASSIGN:
1465 case BINEXPR_SHIFTRIGHT_ASSIGN:
1466 return create_arithmetic_assign_shift(expression);
1468 panic("TODO binexpr type");
1472 static ir_node *array_access_addr(const array_access_expression_t *expression)
1474 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1475 ir_node *base_addr = expression_to_firm(expression->array_ref);
1476 ir_node *offset = expression_to_firm(expression->index);
1477 offset = create_conv(dbgi, offset, mode_Iu);
1479 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1480 assert(is_type_pointer(ref_type));
1481 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1483 unsigned elem_size = get_type_size(pointer_type->points_to);
1484 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1485 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1487 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1492 static ir_node *array_access_to_firm(
1493 const array_access_expression_t *expression)
1495 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1496 ir_node *addr = array_access_addr(expression);
1497 type_t *type = revert_automatic_type_conversion(
1498 (const expression_t*) expression);
1499 type = skip_typeref(type);
1500 ir_type *irtype = get_ir_type(type);
1502 return deref_address(irtype, addr, dbgi);
1505 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1507 type_t *type = expression->type;
1509 type = expression->size_expression->base.datatype;
1510 assert(type != NULL);
1513 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1514 unsigned size = get_type_size(type);
1515 ir_node *size_node = new_Const_long(mode, size);
1520 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1522 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1524 ir_node *cur_block = get_cur_block();
1526 /* create the true block */
1527 ir_node *true_block = new_immBlock();
1529 ir_node *true_val = expression_to_firm(expression->true_expression);
1530 ir_node *true_jmp = new_Jmp();
1532 /* create the false block */
1533 ir_node *false_block = new_immBlock();
1535 ir_node *false_val = expression_to_firm(expression->false_expression);
1536 ir_node *false_jmp = new_Jmp();
1538 /* create the condition evaluation */
1539 set_cur_block(cur_block);
1540 create_condition_evaluation(expression->condition, true_block, false_block);
1541 mature_immBlock(true_block);
1542 mature_immBlock(false_block);
1544 /* create the common block */
1545 ir_node *common_block = new_immBlock();
1546 add_immBlock_pred(common_block, true_jmp);
1547 add_immBlock_pred(common_block, false_jmp);
1548 mature_immBlock(common_block);
1550 /* TODO improve static semantics, so either both or no values are NULL */
1551 if (true_val == NULL || false_val == NULL)
1554 ir_node *in[2] = { true_val, false_val };
1555 ir_mode *mode = get_irn_mode(true_val);
1556 assert(get_irn_mode(false_val) == mode);
1557 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1562 static ir_node *select_addr(const select_expression_t *expression)
1564 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1566 ir_node *compound_addr = expression_to_firm(expression->compound);
1568 declaration_t *entry = expression->compound_entry;
1569 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1570 ir_entity *entity = entry->v.entity;
1572 assert(entity != NULL);
1574 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1579 static ir_node *select_to_firm(const select_expression_t *expression)
1581 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1582 ir_node *addr = select_addr(expression);
1583 type_t *type = revert_automatic_type_conversion(
1584 (const expression_t*) expression);
1585 type = skip_typeref(type);
1586 ir_type *irtype = get_ir_type(type);
1588 return deref_address(irtype, addr, dbgi);
1591 /* Values returned by __builtin_classify_type. */
1592 typedef enum gcc_type_class
1598 enumeral_type_class,
1601 reference_type_class,
1605 function_type_class,
1616 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1618 const type_t *const type = expr->type_expression->base.datatype;
1624 const atomic_type_t *const atomic_type = &type->atomic;
1625 switch (atomic_type->atype) {
1626 // should not be reached
1627 case ATOMIC_TYPE_INVALID:
1631 // gcc cannot do that
1632 case ATOMIC_TYPE_VOID:
1633 tc = void_type_class;
1636 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1637 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1638 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1639 case ATOMIC_TYPE_SHORT:
1640 case ATOMIC_TYPE_USHORT:
1641 case ATOMIC_TYPE_INT:
1642 case ATOMIC_TYPE_UINT:
1643 case ATOMIC_TYPE_LONG:
1644 case ATOMIC_TYPE_ULONG:
1645 case ATOMIC_TYPE_LONGLONG:
1646 case ATOMIC_TYPE_ULONGLONG:
1647 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1648 tc = integer_type_class;
1651 case ATOMIC_TYPE_FLOAT:
1652 case ATOMIC_TYPE_DOUBLE:
1653 case ATOMIC_TYPE_LONG_DOUBLE:
1654 tc = real_type_class;
1657 #ifdef PROVIDE_COMPLEX
1658 case ATOMIC_TYPE_FLOAT_COMPLEX:
1659 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1660 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1661 tc = complex_type_class;
1663 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1664 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1665 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1666 tc = complex_type_class;
1671 panic("Unimplemented case in classify_type_to_firm().");
1676 case TYPE_ARRAY: // gcc handles this as pointer
1677 case TYPE_FUNCTION: // gcc handles this as pointer
1678 case TYPE_POINTER: tc = pointer_type_class; break;
1679 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1680 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1682 // gcc handles this as integer
1683 case TYPE_ENUM: tc = integer_type_class; break;
1686 panic("Unimplemented case in classify_type_to_firm().");
1689 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1690 ir_mode *const mode = mode_Is;
1691 tarval *const tv = new_tarval_from_long(tc, mode);
1692 return new_d_Const(dbgi, mode, tv);
1695 static ir_node *function_name_to_firm(
1696 const string_literal_expression_t *const expr)
1698 if (current_function_name == NULL) {
1699 const source_position_t *const src_pos =
1700 &expr->expression.source_position;
1701 const char *const name = current_function_decl->symbol->string;
1702 current_function_name = string_to_firm(src_pos, "__func__", name);
1705 return current_function_name;
1708 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1710 statement_t *statement = expr->statement;
1712 assert(statement->type == STATEMENT_COMPOUND);
1713 return compound_statement_to_firm((compound_statement_t*) statement);
1716 static ir_node *va_start_expression_to_firm(
1717 const va_start_expression_t *const expr)
1719 ir_type *const method_type = get_ir_type(current_function_decl->type);
1720 int const n = get_method_n_params(method_type) - 1;
1721 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
1722 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
1723 dbg_info *const dbgi =
1724 get_dbg_info(&expr->expression.source_position);
1725 ir_node *const no_mem = new_NoMem();
1726 ir_node *const arg_sel =
1727 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
1729 size_t const parm_size = get_type_size(expr->parameter->type);
1730 ir_node *const cnst = new_Const_long(mode_Iu, parm_size);
1731 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
1732 set_value_for_expression(expr->ap, add);
1737 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
1739 ir_type *const irtype = get_ir_type(expr->expression.datatype);
1740 ir_node *const ap = expression_to_firm(expr->ap);
1741 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1742 ir_node *const res = deref_address(irtype, ap, dbgi);
1744 size_t const parm_size = get_type_size(expr->expression.datatype);
1745 ir_node *const cnst = new_Const_long(mode_Iu, parm_size);
1746 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
1747 set_value_for_expression(expr->ap, add);
1752 static ir_node *dereference_addr(const unary_expression_t *const expression)
1754 assert(expression->type == UNEXPR_DEREFERENCE);
1755 return expression_to_firm(expression->value);
1758 static ir_node *expression_to_addr(const expression_t *expression)
1760 switch(expression->type) {
1761 case EXPR_REFERENCE:
1762 return reference_addr(&expression->reference);
1763 case EXPR_ARRAY_ACCESS:
1764 return array_access_addr(&expression->array_access);
1766 return select_addr(&expression->select);
1768 return call_expression_to_firm(&expression->call);
1770 const unary_expression_t *const unary_expr = &expression->unary;
1771 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1772 return dereference_addr(unary_expr);
1779 panic("trying to get address of non-lvalue");
1782 static ir_node *_expression_to_firm(const expression_t *expression)
1784 switch(expression->type) {
1786 return const_to_firm(&expression->conste);
1787 case EXPR_STRING_LITERAL:
1788 return string_literal_to_firm(&expression->string);
1789 case EXPR_WIDE_STRING_LITERAL:
1790 return wide_string_literal_to_firm(&expression->wide_string);
1791 case EXPR_REFERENCE:
1792 return reference_expression_to_firm(&expression->reference);
1794 return call_expression_to_firm(&expression->call);
1796 return unary_expression_to_firm(&expression->unary);
1798 return binary_expression_to_firm(&expression->binary);
1799 case EXPR_ARRAY_ACCESS:
1800 return array_access_to_firm(&expression->array_access);
1802 return sizeof_to_firm(&expression->sizeofe);
1803 case EXPR_CONDITIONAL:
1804 return conditional_to_firm(&expression->conditional);
1806 return select_to_firm(&expression->select);
1807 case EXPR_CLASSIFY_TYPE:
1808 return classify_type_to_firm(&expression->classify_type);
1810 case EXPR_PRETTY_FUNCTION:
1811 return function_name_to_firm(&expression->string);
1812 case EXPR_STATEMENT:
1813 return statement_expression_to_firm(&expression->statement);
1815 return va_start_expression_to_firm(&expression->va_starte);
1817 return va_arg_expression_to_firm(&expression->va_arge);
1819 case EXPR_BUILTIN_SYMBOL:
1820 panic("unimplemented expression found");
1826 panic("invalid expression found");
1829 static ir_node *expression_to_firm(const expression_t *expression)
1831 ir_node *res = _expression_to_firm(expression);
1833 if(res != NULL && get_irn_mode(res) == mode_b) {
1834 ir_mode *mode = get_ir_mode(expression->base.datatype);
1835 res = create_conv(NULL, res, mode);
1841 static ir_node *expression_to_modeb(const expression_t *expression)
1843 ir_node *res = _expression_to_firm(expression);
1844 res = create_conv(NULL, res, mode_b);
1850 * create a short-circuit expression evaluation that tries to construct
1851 * efficient control flow structures for &&, || and ! expressions
1853 static void create_condition_evaluation(const expression_t *expression,
1854 ir_node *true_block,
1855 ir_node *false_block)
1857 switch(expression->type) {
1859 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1860 if(unary_expression->type == UNEXPR_NOT) {
1861 create_condition_evaluation(unary_expression->value, false_block,
1868 binary_expression_t *binary_expression
1869 = (binary_expression_t*) expression;
1870 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1871 ir_node *cur_block = get_cur_block();
1872 ir_node *extra_block = new_immBlock();
1873 set_cur_block(cur_block);
1874 create_condition_evaluation(binary_expression->left, extra_block,
1876 mature_immBlock(extra_block);
1877 set_cur_block(extra_block);
1878 create_condition_evaluation(binary_expression->right, true_block,
1882 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1883 ir_node *cur_block = get_cur_block();
1884 ir_node *extra_block = new_immBlock();
1885 set_cur_block(cur_block);
1886 create_condition_evaluation(binary_expression->left, true_block,
1888 mature_immBlock(extra_block);
1889 set_cur_block(extra_block);
1890 create_condition_evaluation(binary_expression->right, true_block,
1900 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1901 ir_node *condition = expression_to_modeb(expression);
1902 ir_node *cond = new_d_Cond(dbgi, condition);
1903 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1904 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1906 add_immBlock_pred(true_block, true_proj);
1907 add_immBlock_pred(false_block, false_proj);
1909 set_cur_block(NULL);
1912 static void return_statement_to_firm(return_statement_t *statement)
1914 if(get_cur_block() == NULL)
1917 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1919 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1923 if(get_method_n_ress(func_irtype) > 0) {
1924 ir_type *res_type = get_method_res_type(func_irtype, 0);
1926 if(statement->return_value != NULL) {
1927 ir_node *node = expression_to_firm(statement->return_value);
1928 node = do_strict_conv(dbgi, node);
1932 if(is_compound_type(res_type)) {
1935 mode = get_type_mode(res_type);
1937 in[0] = new_Unknown(mode);
1941 /* build return_value for its side effects */
1942 if(statement->return_value != NULL) {
1943 expression_to_firm(statement->return_value);
1948 ir_node *store = get_store();
1949 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1951 ir_node *end_block = get_irg_end_block(current_ir_graph);
1952 add_immBlock_pred(end_block, ret);
1954 set_cur_block(NULL);
1957 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1959 if(get_cur_block() == NULL)
1962 return expression_to_firm(statement->expression);
1965 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1967 ir_node *result = NULL;
1968 statement_t *statement = compound->statements;
1969 for( ; statement != NULL; statement = statement->base.next) {
1970 //context2firm(&statement->context);
1972 if(statement->base.next == NULL
1973 && statement->type == STATEMENT_EXPRESSION) {
1974 result = expression_statement_to_firm(
1975 (expression_statement_t*) statement);
1978 statement_to_firm(statement);
1984 static void if_statement_to_firm(if_statement_t *statement)
1986 ir_node *cur_block = get_cur_block();
1988 ir_node *fallthrough_block = new_immBlock();
1990 /* the true (blocks) */
1991 ir_node *true_block;
1992 if (statement->true_statement != NULL) {
1993 true_block = new_immBlock();
1994 statement_to_firm(statement->true_statement);
1995 if(get_cur_block() != NULL) {
1996 ir_node *jmp = new_Jmp();
1997 add_immBlock_pred(fallthrough_block, jmp);
2000 true_block = fallthrough_block;
2003 /* the false (blocks) */
2004 ir_node *false_block;
2005 if(statement->false_statement != NULL) {
2006 false_block = new_immBlock();
2008 statement_to_firm(statement->false_statement);
2009 if(get_cur_block() != NULL) {
2010 ir_node *jmp = new_Jmp();
2011 add_immBlock_pred(fallthrough_block, jmp);
2014 false_block = fallthrough_block;
2017 /* create the condition */
2018 if(cur_block != NULL) {
2019 set_cur_block(cur_block);
2020 create_condition_evaluation(statement->condition, true_block,
2024 mature_immBlock(true_block);
2025 if(false_block != fallthrough_block) {
2026 mature_immBlock(false_block);
2028 mature_immBlock(fallthrough_block);
2030 set_cur_block(fallthrough_block);
2033 static void while_statement_to_firm(while_statement_t *statement)
2035 ir_node *jmp = NULL;
2036 if(get_cur_block() != NULL) {
2040 /* create the header block */
2041 ir_node *header_block = new_immBlock();
2043 add_immBlock_pred(header_block, jmp);
2046 /* the false block */
2047 ir_node *false_block = new_immBlock();
2050 ir_node *body_block;
2051 if (statement->body != NULL) {
2052 ir_node *old_continue_label = continue_label;
2053 ir_node *old_break_label = break_label;
2054 continue_label = header_block;
2055 break_label = false_block;
2057 body_block = new_immBlock();
2058 statement_to_firm(statement->body);
2060 assert(continue_label == header_block);
2061 assert(break_label == false_block);
2062 continue_label = old_continue_label;
2063 break_label = old_break_label;
2065 if(get_cur_block() != NULL) {
2067 add_immBlock_pred(header_block, jmp);
2070 body_block = header_block;
2073 /* create the condition */
2074 set_cur_block(header_block);
2076 create_condition_evaluation(statement->condition, body_block, false_block);
2077 mature_immBlock(body_block);
2078 mature_immBlock(false_block);
2079 mature_immBlock(header_block);
2081 set_cur_block(false_block);
2084 static void do_while_statement_to_firm(do_while_statement_t *statement)
2086 ir_node *jmp = NULL;
2087 if(get_cur_block() != NULL) {
2091 /* create the header block */
2092 ir_node *header_block = new_immBlock();
2094 /* the false block */
2095 ir_node *false_block = new_immBlock();
2098 ir_node *body_block = new_immBlock();
2100 add_immBlock_pred(body_block, jmp);
2103 if (statement->body != NULL) {
2104 ir_node *old_continue_label = continue_label;
2105 ir_node *old_break_label = break_label;
2106 continue_label = header_block;
2107 break_label = false_block;
2109 statement_to_firm(statement->body);
2111 assert(continue_label == header_block);
2112 assert(break_label == false_block);
2113 continue_label = old_continue_label;
2114 break_label = old_break_label;
2116 if (get_cur_block() == NULL) {
2117 mature_immBlock(header_block);
2118 mature_immBlock(body_block);
2119 mature_immBlock(false_block);
2124 ir_node *body_jmp = new_Jmp();
2125 add_immBlock_pred(header_block, body_jmp);
2126 mature_immBlock(header_block);
2128 /* create the condition */
2129 set_cur_block(header_block);
2131 create_condition_evaluation(statement->condition, body_block, false_block);
2132 mature_immBlock(body_block);
2133 mature_immBlock(false_block);
2134 mature_immBlock(header_block);
2136 set_cur_block(false_block);
2139 static void for_statement_to_firm(for_statement_t *statement)
2141 ir_node *jmp = NULL;
2142 if (get_cur_block() != NULL) {
2143 if(statement->initialisation != NULL) {
2144 expression_to_firm(statement->initialisation);
2149 /* create the step block */
2150 ir_node *const step_block = new_immBlock();
2151 if (statement->step != NULL) {
2152 expression_to_firm(statement->step);
2154 ir_node *const step_jmp = new_Jmp();
2156 /* create the header block */
2157 ir_node *const header_block = new_immBlock();
2159 add_immBlock_pred(header_block, jmp);
2161 add_immBlock_pred(header_block, step_jmp);
2163 /* the false block */
2164 ir_node *const false_block = new_immBlock();
2167 ir_node * body_block;
2168 if (statement->body != NULL) {
2169 ir_node *const old_continue_label = continue_label;
2170 ir_node *const old_break_label = break_label;
2171 continue_label = step_block;
2172 break_label = false_block;
2174 body_block = new_immBlock();
2175 statement_to_firm(statement->body);
2177 assert(continue_label == step_block);
2178 assert(break_label == false_block);
2179 continue_label = old_continue_label;
2180 break_label = old_break_label;
2182 if (get_cur_block() != NULL) {
2184 add_immBlock_pred(step_block, jmp);
2187 body_block = step_block;
2190 /* create the condition */
2191 set_cur_block(header_block);
2192 if (statement->condition != NULL) {
2193 create_condition_evaluation(statement->condition, body_block,
2196 keep_alive(header_block);
2198 add_immBlock_pred(body_block, jmp);
2201 mature_immBlock(body_block);
2202 mature_immBlock(false_block);
2203 mature_immBlock(step_block);
2204 mature_immBlock(header_block);
2205 mature_immBlock(false_block);
2207 set_cur_block(false_block);
2210 static void create_declaration_entity(declaration_t *declaration,
2211 declaration_type_t declaration_type,
2212 ir_type *parent_type)
2214 ident *id = new_id_from_str(declaration->symbol->string);
2215 ir_type *irtype = get_ir_type(declaration->type);
2216 ir_entity *entity = new_entity(parent_type, id, irtype);
2217 set_entity_ld_ident(entity, id);
2219 declaration->declaration_type = (unsigned char) declaration_type;
2220 declaration->v.entity = entity;
2221 set_entity_variability(entity, variability_uninitialized);
2222 /* TODO: visibility? */
2225 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2227 enum compound_graph_entry_type_t {
2228 COMPOUND_GRAPH_ENTRY_ARRAY,
2229 COMPOUND_GRAPH_ENTRY_COMPOUND
2232 struct compound_graph_path_entry_t {
2238 compound_graph_path_entry_t *prev;
2241 static void create_initializer_object(initializer_t *initializer, type_t *type,
2242 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2244 static compound_graph_path *create_compound_path(ir_type *type,
2245 compound_graph_path_entry_t *entry, int len)
2247 compound_graph_path *path = new_compound_graph_path(type, len);
2250 for( ; entry != NULL; entry = entry->prev, --i) {
2252 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2253 set_compound_graph_path_node(path, i, entry->v.entity);
2255 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2256 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2264 static void create_initializer_value(initializer_value_t *initializer,
2266 compound_graph_path_entry_t *entry,
2269 ir_node *node = expression_to_firm(initializer->value);
2270 ir_type *type = get_entity_type(entity);
2271 compound_graph_path *path = create_compound_path(type, entry, len);
2272 add_compound_ent_value_w_path(entity, node, path);
2275 static void create_initializer_compound(initializer_list_t *initializer,
2276 compound_type_t *type,
2278 compound_graph_path_entry_t *last_entry,
2281 declaration_t *compound_declaration = type->declaration;
2283 declaration_t *compound_entry = compound_declaration->context.declarations;
2285 compound_graph_path_entry_t entry;
2286 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2287 entry.prev = last_entry;
2291 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2292 if(compound_entry->symbol == NULL)
2294 if(compound_entry->namespc != NAMESPACE_NORMAL)
2297 if(i >= initializer->len)
2300 entry.v.entity = compound_entry->v.entity;
2302 initializer_t *sub_initializer = initializer->initializers[i];
2304 assert(compound_entry != NULL);
2305 assert(compound_entry->declaration_type
2306 == DECLARATION_TYPE_COMPOUND_MEMBER);
2308 if(sub_initializer->type == INITIALIZER_VALUE) {
2309 create_initializer_value(&sub_initializer->value,
2310 entity, &entry, len);
2312 type_t *entry_type = skip_typeref(compound_entry->type);
2313 create_initializer_object(sub_initializer, entry_type, entity,
2321 static void create_initializer_array(initializer_list_t *initializer,
2322 array_type_t *type, ir_entity *entity,
2323 compound_graph_path_entry_t *last_entry,
2326 type_t *element_type = type->element_type;
2327 element_type = skip_typeref(element_type);
2329 compound_graph_path_entry_t entry;
2330 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2331 entry.prev = last_entry;
2335 for(i = 0; i < initializer->len; ++i) {
2336 entry.v.array_index = i;
2338 initializer_t *sub_initializer = initializer->initializers[i];
2340 if(sub_initializer->type == INITIALIZER_VALUE) {
2341 create_initializer_value(&sub_initializer->value,
2342 entity, &entry, len);
2344 create_initializer_object(sub_initializer, element_type, entity,
2350 /* TODO: initialize rest... */
2351 if(type->size_expression != NULL) {
2352 size_t array_len = fold_constant(type->size_expression);
2353 for( ; i < array_len; ++i) {
2360 static void create_initializer_string(initializer_string_t *initializer,
2361 array_type_t *type, ir_entity *entity,
2362 compound_graph_path_entry_t *last_entry,
2365 type_t *element_type = type->element_type;
2366 element_type = skip_typeref(element_type);
2368 compound_graph_path_entry_t entry;
2369 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2370 entry.prev = last_entry;
2373 ir_type *irtype = get_entity_type(entity);
2374 size_t arr_len = get_array_type_size(type);
2375 const char *p = initializer->string;
2377 for(i = 0; i < arr_len; ++i, ++p) {
2378 entry.v.array_index = i;
2380 ir_node *node = new_Const_long(mode_Bs, *p);
2381 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2382 add_compound_ent_value_w_path(entity, node, path);
2389 static void create_initializer_wide_string(
2390 const initializer_wide_string_t *const initializer, array_type_t *const type,
2391 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2394 type_t *element_type = type->element_type;
2395 element_type = skip_typeref(element_type);
2397 compound_graph_path_entry_t entry;
2398 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2399 entry.prev = last_entry;
2402 ir_type *const irtype = get_entity_type(entity);
2403 const size_t arr_len = get_array_type_size(type);
2404 const wchar_rep_t * p = initializer->string.begin;
2405 const wchar_rep_t *const end = p + initializer->string.size;
2406 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2407 entry.v.array_index = i;
2409 ir_node *node = new_Const_long(mode_Is, *p);
2410 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2411 add_compound_ent_value_w_path(entity, node, path);
2415 static void create_initializer_object(initializer_t *initializer, type_t *type,
2416 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2418 if(is_type_array(type)) {
2419 array_type_t *array_type = &type->array;
2421 switch (initializer->type) {
2422 case INITIALIZER_STRING: {
2423 initializer_string_t *const string = &initializer->string;
2424 create_initializer_string(string, array_type, entity, entry, len);
2428 case INITIALIZER_WIDE_STRING: {
2429 initializer_wide_string_t *const string = &initializer->wide_string;
2430 create_initializer_wide_string(string, array_type, entity, entry, len);
2434 case INITIALIZER_LIST: {
2435 initializer_list_t *const list = &initializer->list;
2436 create_initializer_array(list, array_type, entity, entry, len);
2440 case INITIALIZER_VALUE:
2443 panic("Unhandled initializer");
2445 assert(initializer->type == INITIALIZER_LIST);
2446 initializer_list_t *list = &initializer->list;
2448 assert(is_type_compound(type));
2449 compound_type_t *compound_type = &type->compound;
2450 create_initializer_compound(list, compound_type, entity, entry, len);
2454 static void create_initializer_local_variable_entity(declaration_t *declaration)
2456 initializer_t *initializer = declaration->init.initializer;
2457 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2458 ir_entity *entity = declaration->v.entity;
2459 ir_node *memory = get_store();
2460 ir_node *nomem = new_NoMem();
2461 ir_node *frame = get_irg_frame(current_ir_graph);
2462 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2464 if(initializer->type == INITIALIZER_VALUE) {
2465 initializer_value_t *initializer_value = &initializer->value;
2467 ir_node *value = expression_to_firm(initializer_value->value);
2468 type_t *type = skip_typeref(declaration->type);
2469 assign_value(dbgi, addr, type, value);
2473 /* create a "template" entity which is copied to the entity on the stack */
2474 ident *id = unique_ident("initializer");
2475 ir_type *irtype = get_ir_type(declaration->type);
2476 ir_type *global_type = get_glob_type();
2477 ir_entity *init_entity = new_entity(global_type, id, irtype);
2478 set_entity_ld_ident(init_entity, id);
2480 set_entity_variability(init_entity, variability_initialized);
2481 set_entity_visibility(init_entity, visibility_local);
2483 ir_graph *old_current_ir_graph = current_ir_graph;
2484 current_ir_graph = get_const_code_irg();
2486 type_t *type = skip_typeref(declaration->type);
2487 create_initializer_object(initializer, type, init_entity, NULL, 0);
2489 assert(current_ir_graph == get_const_code_irg());
2490 current_ir_graph = old_current_ir_graph;
2492 ir_node *src_addr = create_symconst(dbgi, init_entity);
2493 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2495 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2496 set_store(copyb_mem);
2499 static void create_initializer(declaration_t *declaration)
2501 initializer_t *initializer = declaration->init.initializer;
2502 if(initializer == NULL)
2505 declaration_type_t declaration_type
2506 = (declaration_type_t) declaration->declaration_type;
2507 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2508 create_initializer_local_variable_entity(declaration);
2512 if(initializer->type == INITIALIZER_VALUE) {
2513 initializer_value_t *initializer_value = &initializer->value;
2515 ir_node *value = expression_to_firm(initializer_value->value);
2517 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2518 set_value(declaration->v.value_number, value);
2520 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2522 ir_entity *entity = declaration->v.entity;
2524 set_entity_variability(entity, variability_initialized);
2525 set_atomic_ent_value(entity, value);
2528 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2529 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2531 ir_entity *entity = declaration->v.entity;
2532 set_entity_variability(entity, variability_initialized);
2534 type_t *type = skip_typeref(declaration->type);
2535 create_initializer_object(initializer, type, entity, NULL, 0);
2539 static void create_local_variable(declaration_t *declaration)
2541 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2543 bool needs_entity = declaration->address_taken;
2544 type_t *type = skip_typeref(declaration->type);
2546 if(is_type_array(type) || is_type_compound(type)) {
2547 needs_entity = true;
2551 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2552 create_declaration_entity(declaration,
2553 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2556 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2557 declaration->v.value_number = next_value_number_function;
2558 ++next_value_number_function;
2561 create_initializer(declaration);
2564 static void create_local_static_variable(declaration_t *declaration)
2566 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2568 type_t *type = skip_typeref(declaration->type);
2569 ir_type *global_type = get_glob_type();
2570 ident *id = unique_ident(declaration->symbol->string);
2571 ir_type *irtype = get_ir_type(type);
2572 ir_entity *entity = new_entity(global_type, id, irtype);
2573 set_entity_ld_ident(entity, id);
2575 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2576 declaration->v.entity = entity;
2577 set_entity_variability(entity, variability_uninitialized);
2578 set_entity_visibility(entity, visibility_local);
2580 ir_graph *old_current_ir_graph = current_ir_graph;
2581 current_ir_graph = get_const_code_irg();
2583 create_initializer(declaration);
2585 assert(current_ir_graph == get_const_code_irg());
2586 current_ir_graph = old_current_ir_graph;
2589 static void declaration_statement_to_firm(declaration_statement_t *statement)
2591 declaration_t *declaration = statement->declarations_begin;
2592 declaration_t *end = statement->declarations_end->next;
2593 for( ; declaration != end; declaration = declaration->next) {
2594 type_t *type = skip_typeref(declaration->type);
2596 switch ((storage_class_tag_t) declaration->storage_class) {
2597 case STORAGE_CLASS_TYPEDEF:
2599 case STORAGE_CLASS_STATIC:
2600 create_local_static_variable(declaration);
2602 case STORAGE_CLASS_ENUM_ENTRY:
2603 panic("enum entry declaration in local block found");
2604 case STORAGE_CLASS_EXTERN:
2605 panic("extern declaration in local block found");
2606 case STORAGE_CLASS_NONE:
2607 case STORAGE_CLASS_AUTO:
2608 case STORAGE_CLASS_REGISTER:
2609 if(is_type_function(type)) {
2610 panic("nested functions not supported yet");
2612 create_local_variable(declaration);
2615 case STORAGE_CLASS_THREAD:
2616 case STORAGE_CLASS_THREAD_EXTERN:
2617 case STORAGE_CLASS_THREAD_STATIC:
2620 panic("invalid storage class found");
2624 static void create_jump_statement(const statement_t *statement,
2625 ir_node *target_block)
2627 if(get_cur_block() == NULL)
2630 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2631 ir_node *jump = new_d_Jmp(dbgi);
2632 add_immBlock_pred(target_block, jump);
2634 set_cur_block(NULL);
2637 static void switch_statement_to_firm(const switch_statement_t *statement)
2639 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2641 ir_node *expression = expression_to_firm(statement->expression);
2642 ir_node *cond = new_d_Cond(dbgi, expression);
2643 ir_node *break_block = new_immBlock();
2645 set_cur_block(NULL);
2647 ir_node *const old_switch_cond = current_switch_cond;
2648 ir_node *const old_break_label = break_label;
2649 const bool old_saw_default_label = saw_default_label;
2650 current_switch_cond = cond;
2651 break_label = break_block;
2653 statement_to_firm(statement->body);
2655 if(get_cur_block() != NULL) {
2656 ir_node *jmp = new_Jmp();
2657 add_immBlock_pred(break_block, jmp);
2660 if (!saw_default_label) {
2661 set_cur_block(get_nodes_block(cond));
2662 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2663 MAGIC_DEFAULT_PN_NUMBER);
2664 add_immBlock_pred(break_block, proj);
2667 assert(current_switch_cond == cond);
2668 assert(break_label == break_block);
2669 current_switch_cond = old_switch_cond;
2670 break_label = old_break_label;
2671 saw_default_label = old_saw_default_label;
2673 mature_immBlock(break_block);
2674 set_cur_block(break_block);
2677 static long fold_constant(const expression_t *expression)
2679 ir_graph *old_current_ir_graph = current_ir_graph;
2680 current_ir_graph = get_const_code_irg();
2682 ir_node *cnst = expression_to_firm(expression);
2683 if(!is_Const(cnst)) {
2684 panic("couldn't fold constantl");
2686 tarval *tv = get_Const_tarval(cnst);
2687 if(!tarval_is_long(tv)) {
2688 panic("folded constant not an integer");
2691 long res = get_tarval_long(tv);
2693 current_ir_graph = old_current_ir_graph;
2697 static void case_label_to_firm(const case_label_statement_t *statement)
2699 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2701 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2703 /* let's create a node and hope firm constant folding creates a Const
2706 set_cur_block(get_nodes_block(current_switch_cond));
2707 if(statement->expression) {
2708 long pn = fold_constant(statement->expression);
2709 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2710 /* oops someone detected our cheating... */
2711 panic("magic default pn used");
2713 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2715 saw_default_label = true;
2716 proj = new_d_defaultProj(dbgi, current_switch_cond,
2717 MAGIC_DEFAULT_PN_NUMBER);
2720 ir_node *block = new_immBlock();
2721 if (fallthrough != NULL) {
2722 add_immBlock_pred(block, fallthrough);
2724 add_immBlock_pred(block, proj);
2725 mature_immBlock(block);
2727 statement_to_firm(statement->label_statement);
2730 static ir_node *get_label_block(declaration_t *label)
2732 assert(label->namespc == NAMESPACE_LABEL);
2734 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2735 return label->v.block;
2737 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2739 ir_node *old_cur_block = get_cur_block();
2740 ir_node *block = new_immBlock();
2741 set_cur_block(old_cur_block);
2743 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2744 label->v.block = block;
2746 ARR_APP1(ir_node *, imature_blocks, block);
2751 static void label_to_firm(const label_statement_t *statement)
2753 ir_node *block = get_label_block(statement->label);
2755 if(get_cur_block() != NULL) {
2756 ir_node *jmp = new_Jmp();
2757 add_immBlock_pred(block, jmp);
2760 set_cur_block(block);
2763 statement_to_firm(statement->label_statement);
2766 static void goto_to_firm(const goto_statement_t *statement)
2768 if(get_cur_block() == NULL)
2771 ir_node *block = get_label_block(statement->label);
2772 ir_node *jmp = new_Jmp();
2773 add_immBlock_pred(block, jmp);
2775 set_cur_block(NULL);
2778 static void statement_to_firm(statement_t *statement)
2780 switch(statement->type) {
2781 case STATEMENT_INVALID:
2782 panic("invalid statement found");
2783 case STATEMENT_COMPOUND:
2784 compound_statement_to_firm(&statement->compound);
2786 case STATEMENT_RETURN:
2787 return_statement_to_firm(&statement->returns);
2789 case STATEMENT_EXPRESSION:
2790 expression_statement_to_firm(&statement->expression);
2793 if_statement_to_firm(&statement->ifs);
2795 case STATEMENT_WHILE:
2796 while_statement_to_firm(&statement->whiles);
2798 case STATEMENT_DO_WHILE:
2799 do_while_statement_to_firm(&statement->do_while);
2801 case STATEMENT_DECLARATION:
2802 declaration_statement_to_firm(&statement->declaration);
2804 case STATEMENT_BREAK:
2805 create_jump_statement(statement, break_label);
2807 case STATEMENT_CONTINUE:
2808 create_jump_statement(statement, continue_label);
2810 case STATEMENT_SWITCH:
2811 switch_statement_to_firm(&statement->switchs);
2813 case STATEMENT_CASE_LABEL:
2814 case_label_to_firm(&statement->case_label);
2817 for_statement_to_firm(&statement->fors);
2819 case STATEMENT_LABEL:
2820 label_to_firm(&statement->label);
2822 case STATEMENT_GOTO:
2823 goto_to_firm(&statement->gotos);
2828 panic("Statement not implemented\n");
2831 static int count_local_declarations(const declaration_t * decl,
2832 const declaration_t *const end)
2835 for (; decl != end; decl = decl->next) {
2836 const type_t *type = skip_typeref(decl->type);
2837 switch (type->type) {
2841 if (!decl->address_taken)
2851 static int count_decls_in_stmts(const statement_t *stmt)
2854 for (; stmt != NULL; stmt = stmt->base.next) {
2855 switch (stmt->type) {
2856 case STATEMENT_DECLARATION: {
2857 const declaration_statement_t *const decl_stmt =
2858 (const declaration_statement_t*)stmt;
2859 count += count_local_declarations(decl_stmt->declarations_begin,
2860 decl_stmt->declarations_end->next);
2864 case STATEMENT_COMPOUND: {
2865 const compound_statement_t *const comp =
2866 (const compound_statement_t*)stmt;
2867 count += count_decls_in_stmts(comp->statements);
2871 case STATEMENT_IF: {
2872 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2873 count += count_decls_in_stmts(if_stmt->true_statement);
2874 count += count_decls_in_stmts(if_stmt->false_statement);
2878 case STATEMENT_SWITCH: {
2879 const switch_statement_t *const switch_stmt =
2880 (const switch_statement_t*)stmt;
2881 count += count_decls_in_stmts(switch_stmt->body);
2885 case STATEMENT_LABEL: {
2886 const label_statement_t *const label_stmt =
2887 (const label_statement_t*)stmt;
2888 count += count_decls_in_stmts(label_stmt->label_statement);
2892 case STATEMENT_WHILE: {
2893 const while_statement_t *const while_stmt =
2894 (const while_statement_t*)stmt;
2895 count += count_decls_in_stmts(while_stmt->body);
2899 case STATEMENT_DO_WHILE: {
2900 const do_while_statement_t *const do_while_stmt =
2901 (const do_while_statement_t*)stmt;
2902 count += count_decls_in_stmts(do_while_stmt->body);
2906 case STATEMENT_FOR: {
2907 const for_statement_t *const for_stmt =
2908 (const for_statement_t*)stmt;
2909 /* TODO initialisation */
2910 count += count_decls_in_stmts(for_stmt->body);
2915 case STATEMENT_BREAK:
2916 case STATEMENT_CASE_LABEL:
2917 case STATEMENT_CONTINUE:
2918 case STATEMENT_EXPRESSION:
2919 case STATEMENT_GOTO:
2920 case STATEMENT_INVALID:
2921 case STATEMENT_RETURN:
2928 static int get_function_n_local_vars(declaration_t *declaration)
2932 /* count parameters */
2933 count += count_local_declarations(declaration->context.declarations, NULL);
2935 /* count local variables declared in body */
2936 count += count_decls_in_stmts(declaration->init.statement);
2938 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2939 * for expression statements... */
2945 static void initialize_function_parameters(declaration_t *declaration)
2947 ir_graph *irg = current_ir_graph;
2948 ir_node *args = get_irg_args(irg);
2949 ir_node *start_block = get_irg_start_block(irg);
2950 ir_type *function_irtype = get_ir_type(declaration->type);
2953 declaration_t *parameter = declaration->context.declarations;
2954 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2955 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2956 type_t *type = skip_typeref(parameter->type);
2958 bool needs_entity = parameter->address_taken;
2959 assert(!is_type_array(type));
2960 if(is_type_compound(type)) {
2961 needs_entity = true;
2965 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2966 ident *id = new_id_from_str(parameter->symbol->string);
2967 set_entity_ident(entity, id);
2969 parameter->declaration_type
2970 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2971 parameter->v.entity = entity;
2975 ir_mode *mode = get_ir_mode(parameter->type);
2977 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2979 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2980 parameter->v.value_number = next_value_number_function;
2981 ++next_value_number_function;
2983 set_value(parameter->v.value_number, proj);
2987 static void create_function(declaration_t *declaration)
2989 ir_entity *function_entity = get_function_entity(declaration);
2991 if(declaration->init.statement == NULL)
2994 current_function_decl = declaration;
2995 current_function_name = NULL;
2997 assert(imature_blocks == NULL);
2998 imature_blocks = NEW_ARR_F(ir_node*, 0);
3000 int n_local_vars = get_function_n_local_vars(declaration);
3001 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3002 ir_node *first_block = get_cur_block();
3004 next_value_number_function = 0;
3005 initialize_function_parameters(declaration);
3007 statement_to_firm(declaration->init.statement);
3009 ir_node *end_block = get_irg_end_block(irg);
3011 /* do we have a return statement yet? */
3012 if(get_cur_block() != NULL) {
3013 type_t *type = skip_typeref(declaration->type);
3014 assert(is_type_function(type));
3015 const function_type_t *func_type = &type->function;
3016 const type_t *return_type
3017 = skip_typeref(func_type->return_type);
3020 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3021 ret = new_Return(get_store(), 0, NULL);
3024 if(is_type_scalar(return_type)) {
3025 mode = get_ir_mode(func_type->return_type);
3031 // ยง5.1.2.2.3 main implicitly returns 0
3032 if (strcmp(declaration->symbol->string, "main") == 0) {
3033 in[0] = new_Const(mode, get_mode_null(mode));
3035 in[0] = new_Unknown(mode);
3037 ret = new_Return(get_store(), 1, in);
3039 add_immBlock_pred(end_block, ret);
3042 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3043 mature_immBlock(imature_blocks[i]);
3045 DEL_ARR_F(imature_blocks);
3046 imature_blocks = NULL;
3048 mature_immBlock(first_block);
3049 mature_immBlock(end_block);
3051 irg_finalize_cons(irg);
3053 /* finalize the frame type */
3054 ir_type *frame_type = get_irg_frame_type(irg);
3055 int n = get_compound_n_members(frame_type);
3058 for(int i = 0; i < n; ++i) {
3059 ir_entity *entity = get_compound_member(frame_type, i);
3060 ir_type *entity_type = get_entity_type(entity);
3062 int align = get_type_alignment_bytes(entity_type);
3063 if(align > align_all)
3067 misalign = offset % align;
3069 offset += align - misalign;
3073 set_entity_offset(entity, offset);
3074 offset += get_type_size_bytes(entity_type);
3076 set_type_size_bytes(frame_type, offset);
3077 set_type_alignment_bytes(frame_type, align_all);
3078 set_type_state(frame_type, layout_fixed);
3083 static void create_global_variable(declaration_t *declaration)
3087 switch ((storage_class_tag_t)declaration->storage_class) {
3088 case STORAGE_CLASS_STATIC:
3089 vis = visibility_local;
3092 case STORAGE_CLASS_EXTERN:
3093 vis = visibility_external_allocated;
3096 case STORAGE_CLASS_NONE:
3097 vis = visibility_external_visible;
3100 case STORAGE_CLASS_THREAD:
3101 vis = visibility_external_visible;
3104 case STORAGE_CLASS_THREAD_EXTERN:
3105 vis = visibility_external_allocated;
3108 case STORAGE_CLASS_THREAD_STATIC:
3109 vis = visibility_local;
3113 var_type = get_tls_type();
3117 var_type = get_glob_type();
3121 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3123 set_entity_visibility(declaration->v.entity, vis);
3125 current_ir_graph = get_const_code_irg();
3126 create_initializer(declaration);
3129 case STORAGE_CLASS_TYPEDEF:
3130 case STORAGE_CLASS_AUTO:
3131 case STORAGE_CLASS_REGISTER:
3132 case STORAGE_CLASS_ENUM_ENTRY:
3135 panic("Invalid storage class for global variable");
3138 static void context_to_firm(context_t *context)
3140 /* first pass: create declarations */
3141 declaration_t *declaration = context->declarations;
3142 for( ; declaration != NULL; declaration = declaration->next) {
3143 if(declaration->namespc != NAMESPACE_NORMAL)
3145 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3146 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3148 if(declaration->symbol == NULL)
3151 type_t *type = skip_typeref(declaration->type);
3152 if(is_type_function(type)) {
3153 get_function_entity(declaration);
3155 create_global_variable(declaration);
3159 /* second pass: create code */
3160 declaration = context->declarations;
3161 for( ; declaration != NULL; declaration = declaration->next) {
3162 if(declaration->namespc != NAMESPACE_NORMAL)
3164 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3165 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3167 if(declaration->symbol == NULL)
3170 type_t *type = declaration->type;
3171 if(type->type != TYPE_FUNCTION)
3174 create_function(declaration);
3178 void translation_unit_to_firm(translation_unit_t *unit)
3180 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3181 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3182 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3184 ir_type_int = get_ir_type(type_int);
3185 ir_type_const_char = get_ir_type(type_const_char);
3186 ir_type_wchar_t = get_ir_type(type_wchar_t);
3187 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3190 type_void->base.firm_type = ir_type_void;
3192 /* just to be sure */
3193 continue_label = NULL;
3195 current_switch_cond = NULL;
3197 context_to_firm(& unit->context);