9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
19 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
21 static ir_type *ir_type_const_char;
22 static ir_type *ir_type_void;
23 static ir_type *ir_type_int;
24 static ir_type *ir_type_void_ptr;
26 static type_t *type_const_char;
27 static type_t *type_void;
28 static type_t *type_int;
30 static int next_value_number_function;
31 static ir_node *continue_label;
32 static ir_node *break_label;
33 static ir_node *current_switch_cond;
35 typedef enum declaration_type_t {
36 DECLARATION_TYPE_UNKNOWN,
37 DECLARATION_TYPE_FUNCTION,
38 DECLARATION_TYPE_GLOBAL_VARIABLE,
39 DECLARATION_TYPE_LOCAL_VARIABLE,
40 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
41 DECLARATION_TYPE_COMPOUND_MEMBER
44 typedef struct type2firm_env_t type2firm_env_t;
45 struct type2firm_env_t {
46 int can_cache; /* nonzero if type can safely be cached because
47 no typevariables are in the hierarchy */
50 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type);
51 static ir_type *get_ir_type(type_t *type);
53 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
57 const declaration_t *declaration = & value_numbers[pos]->declaration;
59 print_warning_prefix(declaration->source_position);
60 fprintf(stderr, "variable '%s' might be used uninitialized\n",
61 declaration->symbol->string);
63 fprintf(stderr, "Some variable might be used uninitialized\n");
64 return new_r_Unknown(irg, mode);
67 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
69 const source_position_t *pos = (const source_position_t*) dbg;
72 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
76 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
78 const source_position_t *pos = (const source_position_t*) dbg;
83 return pos->input_name;
86 void init_ast2firm(void)
88 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
89 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
90 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
92 ir_type_int = get_ir_type(type_int);
93 ir_type_const_char = get_ir_type(type_const_char);
94 ir_type_void = get_ir_type(type_int); /* we don't have a real void
96 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
97 ir_type_void, mode_P_data);
99 type_void->firm_type = ir_type_void;
102 void exit_ast2firm(void)
106 static unsigned unique_id = 0;
108 static ident *unique_ident(const char *tag)
112 snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
114 return new_id_from_str(buf);
117 static type_t *skip_typeref(type_t *type)
122 const typedef_type_t *typedef_type = (const typedef_type_t*) type;
123 type = typedef_type->declaration->type;
127 const typeof_type_t *typeof_type = (const typeof_type_t *) type;
128 if(typeof_type->typeof_type != NULL) {
129 type = typeof_type->typeof_type;
131 type = typeof_type->expression->datatype;
144 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
146 switch(atomic_type->atype) {
147 case ATOMIC_TYPE_SCHAR:
148 case ATOMIC_TYPE_CHAR:
150 case ATOMIC_TYPE_UCHAR:
152 case ATOMIC_TYPE_SHORT:
154 case ATOMIC_TYPE_USHORT:
156 case ATOMIC_TYPE_LONG:
157 case ATOMIC_TYPE_INT:
159 case ATOMIC_TYPE_ULONG:
160 case ATOMIC_TYPE_UINT:
162 case ATOMIC_TYPE_LONGLONG:
164 case ATOMIC_TYPE_ULONGLONG:
166 case ATOMIC_TYPE_FLOAT:
168 case ATOMIC_TYPE_DOUBLE:
170 case ATOMIC_TYPE_LONG_DOUBLE:
172 case ATOMIC_TYPE_BOOL:
174 #ifdef PROVIDE_COMPLEX
175 case ATOMIC_TYPE_FLOAT_COMPLEX:
176 case ATOMIC_TYPE_DOUBLE_COMPLEX:
177 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
178 panic("complex lowering not implemented yet");
180 case ATOMIC_TYPE_FLOAT_IMAGINARY:
181 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
182 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
183 panic("imaginary lowering not implemented yet");
186 case ATOMIC_TYPE_VOID:
187 panic("tried to get mode from void type");
189 case ATOMIC_TYPE_INVALID:
192 panic("Encountered unknown atomic type");
196 static unsigned get_type_size(type_t *type);
198 static unsigned get_atomic_type_size(const atomic_type_t *type)
200 switch(type->atype) {
201 case ATOMIC_TYPE_CHAR:
202 case ATOMIC_TYPE_SCHAR:
203 case ATOMIC_TYPE_UCHAR:
206 case ATOMIC_TYPE_SHORT:
207 case ATOMIC_TYPE_USHORT:
210 case ATOMIC_TYPE_BOOL:
211 case ATOMIC_TYPE_INT:
212 case ATOMIC_TYPE_UINT:
213 case ATOMIC_TYPE_LONG:
214 case ATOMIC_TYPE_ULONG:
215 case ATOMIC_TYPE_FLOAT:
218 case ATOMIC_TYPE_LONGLONG:
219 case ATOMIC_TYPE_ULONGLONG:
220 case ATOMIC_TYPE_DOUBLE:
223 case ATOMIC_TYPE_LONG_DOUBLE:
226 case ATOMIC_TYPE_VOID:
229 case ATOMIC_TYPE_INVALID:
232 panic("Trying to determine size of invalid atomic type");
235 static unsigned get_compound_type_size(compound_type_t *type)
237 ir_type *irtype = get_ir_type(&type->type);
238 return get_type_size_bytes(irtype);
241 static unsigned get_array_type_size(array_type_t *type)
243 ir_type *irtype = get_ir_type(&type->type);
244 return get_type_size_bytes(irtype);
247 static unsigned get_type_size(type_t *type)
249 type = skip_typeref(type);
253 return get_atomic_type_size((const atomic_type_t*) type);
255 return get_mode_size_bytes(mode_Is);
256 case TYPE_COMPOUND_UNION:
257 case TYPE_COMPOUND_STRUCT:
258 return get_compound_type_size((compound_type_t*) type);
260 /* just a pointer to the function */
261 return get_mode_size_bytes(mode_P_code);
263 return get_mode_size_bytes(mode_P_data);
265 return get_array_type_size((array_type_t*) type);
272 panic("Trying to determine size of invalid type");
275 static unsigned count_parameters(const function_type_t *function_type)
279 function_parameter_t *parameter = function_type->parameters;
280 for ( ; parameter != NULL; parameter = parameter->next) {
290 static ir_type *create_atomic_type(type2firm_env_t *env,
291 const atomic_type_t *type)
294 ir_mode *mode = get_atomic_mode(type);
295 ident *id = get_mode_ident(mode);
296 ir_type *irtype = new_type_primitive(id, mode);
301 static ir_type *create_method_type(type2firm_env_t *env,
302 const function_type_t *function_type)
304 type_t *result_type = function_type->result_type;
306 ident *id = unique_ident("functiontype");
307 int n_parameters = count_parameters(function_type);
308 int n_results = result_type == type_void ? 0 : 1;
309 ir_type *irtype = new_type_method(id, n_parameters, n_results);
311 if(result_type != type_void) {
312 ir_type *restype = _get_ir_type(env, result_type);
313 set_method_res_type(irtype, 0, restype);
316 function_parameter_t *parameter = function_type->parameters;
318 for( ; parameter != NULL; parameter = parameter->next) {
319 ir_type *p_irtype = _get_ir_type(env, parameter->type);
320 set_method_param_type(irtype, n, p_irtype);
324 if(function_type->variadic) {
325 set_method_variadicity(irtype, variadicity_variadic);
331 static ir_type *create_pointer_type(type2firm_env_t *env, pointer_type_t *type)
333 type_t *points_to = type->points_to;
334 ir_type *ir_points_to;
335 /* Avoid endless recursion if the points_to type contains this poiner type
336 * again (might be a struct). We therefore first create a void* pointer
337 * and then set the real points_to type
339 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
340 ir_type_void, mode_P_data);
341 type->type.firm_type = ir_type;
343 ir_points_to = _get_ir_type(env, points_to);
344 set_pointer_points_to_type(ir_type, ir_points_to);
349 static ir_type *create_array_type(type2firm_env_t *env, array_type_t *type)
351 type_t *element_type = type->element_type;
352 ir_type *ir_element_type = _get_ir_type(env, element_type);
356 panic("TODO arraytpye size not implemented yet");
358 ir_type *ir_type = new_type_array(unique_ident("array"), 1, ir_element_type);
359 set_array_bounds_int(ir_type, 0, 0, n_elements);
361 size_t elemsize = get_type_size_bytes(ir_element_type);
362 int align = get_type_alignment_bytes(ir_element_type);
363 if(elemsize % align > 0) {
364 elemsize += align - (elemsize % align);
366 set_type_size_bytes(ir_type, n_elements * elemsize);
367 set_type_alignment_bytes(ir_type, align);
368 set_type_state(ir_type, layout_fixed);
373 #define INVALID_TYPE ((ir_type_ptr)-1)
375 static ir_type *create_struct_type(type2firm_env_t *env, compound_type_t *type)
377 symbol_t *symbol = type->declaration->symbol;
380 id = unique_ident(symbol->string);
382 id = unique_ident("__anonymous_struct");
384 ir_type *ir_type = new_type_struct(id);
386 type->type.firm_type = ir_type;
390 declaration_t *entry = type->declaration->context.declarations;
391 for( ; entry != NULL; entry = entry->next) {
392 ident *ident = new_id_from_str(entry->symbol->string);
393 ir_type_ptr entry_ir_type = _get_ir_type(env, entry->type);
395 int entry_size = get_type_size_bytes(entry_ir_type);
396 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
397 int misalign = offset % entry_alignment;
400 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
401 set_entity_offset(entity, offset);
402 add_struct_member(ir_type, entity);
403 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
404 entry->v.entity = entity;
406 offset += entry_size;
407 if(entry_alignment > align_all) {
408 if(entry_alignment % align_all != 0) {
409 panic("Uneven alignments not supported yet");
411 align_all = entry_alignment;
415 int misalign = offset % align_all;
417 set_type_alignment_bytes(ir_type, align_all);
418 set_type_size_bytes(ir_type, offset);
419 set_type_state(ir_type, layout_fixed);
424 static ir_type *create_union_type(type2firm_env_t *env, compound_type_t *type)
426 declaration_t *declaration = type->declaration;
427 symbol_t *symbol = declaration->symbol;
430 id = unique_ident(symbol->string);
432 id = unique_ident("__anonymous_union");
434 ir_type *ir_type = new_type_union(id);
436 type->type.firm_type = ir_type;
440 declaration_t *entry = declaration->context.declarations;
441 for( ; entry != NULL; entry = entry->next) {
442 ident *ident = new_id_from_str(entry->symbol->string);
443 ir_type_ptr entry_ir_type = _get_ir_type(env, entry->type);
445 int entry_size = get_type_size_bytes(entry_ir_type);
446 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
448 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
449 add_union_member(ir_type, entity);
450 set_entity_offset(entity, 0);
451 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
452 entry->v.entity = entity;
454 if(entry_size > size) {
457 if(entry_alignment > align_all) {
458 if(entry_alignment % align_all != 0) {
459 panic("Uneven alignments not supported yet");
461 align_all = entry_alignment;
465 set_type_alignment_bytes(ir_type, align_all);
466 set_type_size_bytes(ir_type, size);
467 set_type_state(ir_type, layout_fixed);
472 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type)
474 assert(type != NULL);
476 type = skip_typeref(type);
478 if(type->firm_type != NULL) {
479 assert(type->firm_type != INVALID_TYPE);
480 return type->firm_type;
483 ir_type *firm_type = NULL;
486 firm_type = create_atomic_type(env, (atomic_type_t*) type);
489 firm_type = create_method_type(env, (function_type_t*) type);
492 firm_type = create_pointer_type(env, (pointer_type_t*) type);
495 firm_type = create_array_type(env, (array_type_t*) type);
497 case TYPE_COMPOUND_STRUCT:
498 firm_type = create_struct_type(env, (compound_type_t*) type);
500 case TYPE_COMPOUND_UNION:
501 firm_type = create_union_type(env, (compound_type_t*) type);
504 firm_type = ir_type_int;
512 if(firm_type == NULL)
513 panic("unknown type found");
516 type->firm_type = firm_type;
522 static ir_type *get_ir_type(type_t *type)
527 return _get_ir_type(&env, type);
530 static inline ir_mode *get_ir_mode(type_t *type)
532 ir_type *irtype = get_ir_type(type);
533 ir_mode *mode = get_type_mode(irtype);
534 assert(mode != NULL);
538 static ir_entity* get_function_entity(declaration_t *declaration)
540 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
541 return declaration->v.entity;
542 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
544 symbol_t *symbol = declaration->symbol;
545 ident *id = new_id_from_str(symbol->string);
547 ir_type *global_type = get_glob_type();
548 ir_type *ir_type_method = get_ir_type(declaration->type);
549 assert(is_Method_type(ir_type_method));
551 type_t *type = declaration->type;
552 ir_entity *entity = new_entity(global_type, id, ir_type_method);
553 set_entity_ld_ident(entity, id);
554 if(declaration->storage_class & STORAGE_CLASS_STATIC
555 || type->qualifiers & TYPE_QUALIFIER_INLINE) {
556 set_entity_visibility(entity, visibility_local);
557 } else if(declaration->init.statement != NULL) {
558 set_entity_visibility(entity, visibility_external_visible);
560 set_entity_visibility(entity, visibility_external_allocated);
563 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
564 declaration->v.entity = entity;
571 static ir_node *expression_to_firm(const expression_t *expression);
572 static ir_node *_expression_to_firm(const expression_t *expression);
574 static dbg_info *get_dbg_info(const source_position_t *pos)
576 return (dbg_info*) pos;
579 static ir_node *const_to_firm(const const_t *cnst)
581 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
582 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
585 if(mode_is_float(mode)) {
586 tv = new_tarval_from_double(cnst->v.float_value, mode);
588 tv = new_tarval_from_long(cnst->v.int_value, mode);
591 return new_d_Const(dbgi, mode, tv);
594 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
596 assert(entity != NULL);
597 union symconst_symbol sym;
598 sym.entity_p = entity;
599 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
602 static ir_node *string_literal_to_firm(const string_literal_t* literal)
604 ir_type *global_type = get_glob_type();
605 ir_type *type = new_type_array(unique_ident("strtype"), 1,
608 ident *id = unique_ident("Lstr");
609 ir_entity *entity = new_entity(global_type, id, type);
610 set_entity_ld_ident(entity, id);
611 set_entity_variability(entity, variability_constant);
613 ir_type *elem_type = ir_type_const_char;
614 ir_mode *mode = get_type_mode(elem_type);
616 const char *string = literal->value;
617 size_t slen = strlen(string) + 1;
619 set_array_lower_bound_int(type, 0, 0);
620 set_array_upper_bound_int(type, 0, slen);
621 set_type_size_bytes(type, slen);
622 set_type_state(type, layout_fixed);
624 tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
625 for(size_t i = 0; i < slen; ++i) {
626 tvs[i] = new_tarval_from_long(string[i], mode);
629 set_array_entity_values(entity, tvs, slen);
632 dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
634 return create_symconst(dbgi, entity);
637 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
639 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
640 declaration_t *declaration = ref->declaration;
641 type_t *type = declaration->type;
642 ir_mode *mode = get_ir_mode(type);
644 switch((declaration_type_t) declaration->declaration_type) {
645 case DECLARATION_TYPE_UNKNOWN:
647 case DECLARATION_TYPE_LOCAL_VARIABLE:
648 return get_value(declaration->v.value_number, mode);
649 case DECLARATION_TYPE_FUNCTION: {
650 return create_symconst(dbgi, declaration->v.entity);
652 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
653 case DECLARATION_TYPE_GLOBAL_VARIABLE:
654 case DECLARATION_TYPE_COMPOUND_MEMBER:
655 panic("not implemented reference type");
658 panic("reference to declaration with unknown type found");
661 static ir_node *call_expression_to_firm(const call_expression_t *call)
663 assert(get_cur_block() != NULL);
665 expression_t *function = call->function;
666 ir_node *callee = expression_to_firm(function);
668 assert(function->datatype->type == TYPE_FUNCTION);
669 function_type_t *function_type = (function_type_t*) function->datatype;
671 int n_parameters = 0;
672 call_argument_t *argument = call->arguments;
673 for( ; argument != NULL; argument = argument->next) {
677 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
678 ir_type *new_method_type = NULL;
679 if(function_type->variadic) {
680 /* we need to construct a new method type matching the call
682 int n_res = get_method_n_ress(ir_method_type);
683 new_method_type = new_type_method(unique_ident("calltype"),
684 n_parameters, n_res);
685 set_method_calling_convention(new_method_type,
686 get_method_calling_convention(ir_method_type));
687 set_method_additional_properties(new_method_type,
688 get_method_additional_properties(ir_method_type));
690 for(int i = 0; i < n_res; ++i) {
691 set_method_res_type(new_method_type, i,
692 get_method_res_type(ir_method_type, i));
695 ir_node *in[n_parameters];
697 argument = call->arguments;
699 for( ; argument != NULL; argument = argument->next) {
700 expression_t *expression = argument->expression;
701 ir_node *arg_node = expression_to_firm(expression);
704 if(new_method_type != NULL) {
705 ir_type *irtype = get_ir_type(expression->datatype);
706 set_method_param_type(new_method_type, n, irtype);
711 assert(n == n_parameters);
713 if(new_method_type != NULL)
714 ir_method_type = new_method_type;
716 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
717 ir_node *store = get_store();
718 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
720 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
723 type_t *result_type = function_type->result_type;
724 ir_node *result = NULL;
725 if(result_type != type_void) {
726 ir_mode *mode = get_ir_mode(result_type);
727 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
728 result = new_d_Proj(dbgi, resproj, mode, 0);
734 static ir_node *load_from_expression_addr(type_t *type, ir_node *addr,
737 ir_mode *mode = get_ir_mode(type);
738 ir_node *memory = get_store();
739 ir_node *load = new_d_Load(dbgi, memory, addr, mode);
740 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
741 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
747 static ir_node *expression_to_addr(const expression_t *expression);
749 static void set_value_for_expression(const expression_t *expression,
752 if(expression->type == EXPR_REFERENCE) {
753 reference_expression_t *ref = (reference_expression_t*) expression;
755 declaration_t *declaration = ref->declaration;
756 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
757 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
758 set_value(declaration->v.value_number, value);
763 dbg_info *dbgi = get_dbg_info(&expression->source_position);
764 ir_node *addr = expression_to_addr(expression);
765 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
766 ir_node *memory = get_store();
767 ir_node *store = new_d_Store(dbgi, memory, addr, value);
768 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
769 set_store(store_mem);
772 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
774 ir_mode *value_mode = get_irn_mode(value);
776 if(value_mode == dest_mode)
779 if(dest_mode == mode_b) {
780 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
781 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
782 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
786 return new_d_Conv(dbgi, value, dest_mode);
789 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
791 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
792 type_t *type = expression->expression.datatype;
793 ir_mode *mode = get_ir_mode(type);
795 if(expression->type == UNEXPR_TAKE_ADDRESS)
796 return expression_to_addr(expression->value);
798 const expression_t *value = expression->value;
799 ir_node *value_node = expression_to_firm(value);
801 switch(expression->type) {
803 return new_d_Minus(dbgi, value_node, mode);
806 case UNEXPR_BITWISE_NEGATE:
807 return new_d_Not(dbgi, value_node, mode);
809 if(get_irn_mode(value_node) != mode_b) {
810 value_node = create_conv(dbgi, value_node, mode_b);
812 value_node = new_d_Not(dbgi, value_node, mode_b);
814 value_node = create_conv(dbgi, value_node, mode);
817 case UNEXPR_DEREFERENCE:
818 return load_from_expression_addr(type, value_node, dbgi);
819 case UNEXPR_POSTFIX_INCREMENT: {
820 ir_node *one = new_Const(mode, get_mode_one(mode));
821 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
822 set_value_for_expression(value, new_value);
825 case UNEXPR_POSTFIX_DECREMENT: {
826 ir_node *one = new_Const(mode, get_mode_one(mode));
827 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
828 set_value_for_expression(value, new_value);
831 case UNEXPR_PREFIX_INCREMENT: {
832 ir_node *one = new_Const(mode, get_mode_one(mode));
833 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
834 set_value_for_expression(value, new_value);
837 case UNEXPR_PREFIX_DECREMENT: {
838 ir_node *one = new_Const(mode, get_mode_one(mode));
839 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
840 set_value_for_expression(value, new_value);
844 return create_conv(dbgi, value_node, mode);
846 case UNEXPR_TAKE_ADDRESS:
850 panic("invalid UNEXPR type found");
853 static long get_pnc(binary_expression_type_t type)
856 case BINEXPR_EQUAL: return pn_Cmp_Eq;
857 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
858 case BINEXPR_LESS: return pn_Cmp_Lt;
859 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
860 case BINEXPR_GREATER: return pn_Cmp_Gt;
861 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
865 panic("trying to get pn_Cmp from non-comparison binexpr type");
868 static ir_node *create_lazy_op(const binary_expression_t *expression)
870 bool is_or = (expression->type == BINEXPR_LOGICAL_OR);
871 assert(is_or || expression->type == BINEXPR_LOGICAL_AND);
873 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
874 ir_node *val1 = expression_to_firm(expression->left);
875 val1 = create_conv(dbgi, val1, mode_b);
877 ir_node *cond = new_d_Cond(dbgi, val1);
878 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
879 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
881 ir_node *fallthrough_block = new_immBlock();
884 ir_node *calc_val2_block = new_immBlock();
886 add_immBlock_pred(calc_val2_block, false_proj);
888 add_immBlock_pred(calc_val2_block, true_proj);
891 mature_immBlock(calc_val2_block);
893 ir_node *val2 = expression_to_firm(expression->right);
894 val2 = create_conv(dbgi, val2, mode_b);
895 if(get_cur_block() != NULL) {
896 ir_node *jmp = new_d_Jmp(dbgi);
897 add_immBlock_pred(fallthrough_block, jmp);
903 constb = new_d_Const(dbgi, mode_b, get_tarval_b_true());
904 add_immBlock_pred(fallthrough_block, true_proj);
906 constb = new_d_Const(dbgi, mode_b, get_tarval_b_false());
907 add_immBlock_pred(fallthrough_block, false_proj);
909 mature_immBlock(fallthrough_block);
911 set_cur_block(fallthrough_block);
913 ir_node *in[2] = { val2, constb };
914 ir_node *val = new_d_Phi(dbgi, 2, in, mode_b);
919 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
920 ir_node *right, ir_mode *mode);
922 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
923 create_arithmetic_func func)
925 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
926 ir_node *left = expression_to_firm(expression->left);
927 ir_node *right = expression_to_firm(expression->right);
928 type_t *type = expression->expression.datatype;
929 ir_mode *mode = get_ir_mode(type);
930 /* TODO FIXME Hack for now */
931 right = create_conv(dbgi, right, get_irn_mode(left));
933 ir_node *res = func(dbgi, left, right, mode);
938 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
940 binary_expression_type_t type = expression->type;
943 case BINEXPR_NOTEQUAL:
945 case BINEXPR_LESSEQUAL:
946 case BINEXPR_GREATER:
947 case BINEXPR_GREATEREQUAL: {
948 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
949 ir_node *left = expression_to_firm(expression->left);
950 ir_node *right = expression_to_firm(expression->right);
951 ir_node *cmp = new_d_Cmp(dbgi, left, right);
952 long pnc = get_pnc(type);
953 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
956 case BINEXPR_ASSIGN: {
957 ir_node *right = expression_to_firm(expression->right);
958 set_value_for_expression(expression->left, right);
962 return create_arithmetic_binop(expression, new_d_Add);
964 return create_arithmetic_binop(expression, new_d_Sub);
966 return create_arithmetic_binop(expression, new_d_Mul);
967 case BINEXPR_BITWISE_AND:
968 return create_arithmetic_binop(expression, new_d_And);
969 case BINEXPR_BITWISE_OR:
970 return create_arithmetic_binop(expression, new_d_Or);
971 case BINEXPR_BITWISE_XOR:
972 return create_arithmetic_binop(expression, new_d_Eor);
973 case BINEXPR_SHIFTLEFT:
974 return create_arithmetic_binop(expression, new_d_Shl);
975 case BINEXPR_SHIFTRIGHT:
976 return create_arithmetic_binop(expression, new_d_Shr);
977 case BINEXPR_LOGICAL_AND:
978 case BINEXPR_LOGICAL_OR:
979 return create_lazy_op(expression);
981 panic("TODO binexpr type");
985 static ir_node *array_access_addr(const array_access_expression_t *expression)
987 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
989 ir_node *base_addr = expression_to_firm(expression->array_ref);
990 ir_node *offset = expression_to_firm(expression->index);
991 offset = create_conv(dbgi, offset, mode_Iu);
993 unsigned elem_size = get_type_size(expression->expression.datatype);
994 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
995 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
997 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1002 static ir_node *array_access_to_firm(
1003 const array_access_expression_t *expression)
1006 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1007 ir_node *addr = array_access_addr(expression);
1008 type_t *type = expression->expression.datatype;
1010 return load_from_expression_addr(type, addr, dbgi);
1013 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1015 type_t *type = expression->type;
1017 type = expression->size_expression->datatype;
1018 assert(type != NULL);
1021 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1022 unsigned size = get_type_size(type);
1023 ir_node *size_node = new_Const_long(mode, size);
1028 static ir_node *expression_to_addr(const expression_t *expression)
1030 switch(expression->type) {
1031 case EXPR_ARRAY_ACCESS:
1032 return array_access_addr((const array_access_expression_t*) expression);
1036 panic("trying to get address of non-lvalue");
1039 static ir_node *_expression_to_firm(const expression_t *expression)
1041 switch(expression->type) {
1043 return const_to_firm((const const_t*) expression);
1044 case EXPR_STRING_LITERAL:
1045 return string_literal_to_firm((const string_literal_t*) expression);
1046 case EXPR_REFERENCE:
1047 return reference_expression_to_firm(
1048 (const reference_expression_t*) expression);
1050 return call_expression_to_firm((const call_expression_t*) expression);
1052 return unary_expression_to_firm((const unary_expression_t*) expression);
1054 return binary_expression_to_firm(
1055 (const binary_expression_t*) expression);
1056 case EXPR_ARRAY_ACCESS:
1057 return array_access_to_firm(
1058 (const array_access_expression_t*) expression);
1060 return sizeof_to_firm((const sizeof_expression_t*) expression);
1064 panic("unsupported expression found");
1067 static ir_node *expression_to_firm(const expression_t *expression)
1069 ir_node *res = _expression_to_firm(expression);
1071 if(expression->datatype == type_void)
1074 ir_mode *mode = get_ir_mode(expression->datatype);
1075 res = create_conv(NULL, res, mode);
1080 static void statement_to_firm(statement_t *statement);
1082 static void return_statement_to_firm(return_statement_t *statement)
1084 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1087 if(statement->return_value != NULL) {
1088 ir_node *retval = expression_to_firm(statement->return_value);
1092 ret = new_d_Return(dbgi, get_store(), 1, in);
1094 ret = new_d_Return(dbgi, get_store(), 0, NULL);
1096 ir_node *end_block = get_irg_end_block(current_ir_graph);
1097 add_immBlock_pred(end_block, ret);
1099 set_cur_block(NULL);
1102 static void compound_statement_to_firm(compound_statement_t *compound)
1104 statement_t *statement = compound->statements;
1105 for( ; statement != NULL; statement = statement->next) {
1106 //context2firm(&statement->context);
1107 statement_to_firm(statement);
1111 static void expression_statement_to_firm(expression_statement_t *statement)
1113 expression_to_firm(statement->expression);
1116 static void if_statement_to_firm(if_statement_t *statement)
1118 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1119 ir_node *condition = _expression_to_firm(statement->condition);
1120 assert(condition != NULL);
1122 /* make sure we have a mode_b condition */
1123 condition = create_conv(dbgi, condition, mode_b);
1125 ir_node *cond = new_d_Cond(dbgi, condition);
1126 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1127 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1129 ir_node *fallthrough_block = new_immBlock();
1131 /* the true (blocks) */
1132 ir_node *true_block = new_immBlock();
1133 add_immBlock_pred(true_block, true_proj);
1134 mature_immBlock(true_block);
1136 statement_to_firm(statement->true_statement);
1137 if(get_cur_block() != NULL) {
1138 ir_node *jmp = new_Jmp();
1139 add_immBlock_pred(fallthrough_block, jmp);
1142 /* the false (blocks) */
1143 if(statement->false_statement != NULL) {
1144 ir_node *false_block = new_immBlock();
1145 add_immBlock_pred(false_block, false_proj);
1146 mature_immBlock(false_block);
1148 statement_to_firm(statement->false_statement);
1149 if(get_cur_block() != NULL) {
1150 ir_node *jmp = new_Jmp();
1151 add_immBlock_pred(fallthrough_block, jmp);
1154 add_immBlock_pred(fallthrough_block, false_proj);
1156 mature_immBlock(fallthrough_block);
1158 set_cur_block(fallthrough_block);
1161 static void while_statement_to_firm(while_statement_t *statement)
1163 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1165 ir_node *jmp = NULL;
1166 if(get_cur_block() != NULL) {
1170 /* create the header block */
1171 ir_node *header_block = new_immBlock();
1173 add_immBlock_pred(header_block, jmp);
1176 /* create the condition */
1177 ir_node *condition = _expression_to_firm(statement->condition);
1178 condition = create_conv(dbgi, condition, mode_b);
1180 ir_node *cond = new_d_Cond(dbgi, condition);
1181 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1182 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1184 /* the false block */
1185 ir_node *false_block = new_immBlock();
1186 add_immBlock_pred(false_block, false_proj);
1189 ir_node *body_block = new_immBlock();
1190 add_immBlock_pred(body_block, true_proj);
1191 mature_immBlock(body_block);
1193 ir_node *old_continue_label = continue_label;
1194 ir_node *old_break_label = break_label;
1195 continue_label = header_block;
1196 break_label = false_block;
1198 statement_to_firm(statement->body);
1200 assert(continue_label == header_block);
1201 assert(break_label == false_block);
1202 continue_label = old_continue_label;
1203 break_label = old_break_label;
1205 if(get_cur_block() != NULL) {
1206 ir_node *jmp = new_Jmp();
1207 add_immBlock_pred(header_block, jmp);
1210 mature_immBlock(header_block);
1211 mature_immBlock(false_block);
1213 set_cur_block(false_block);
1216 static void do_while_statement_to_firm(do_while_statement_t *statement)
1218 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1220 ir_node *jmp = NULL;
1221 if(get_cur_block() != NULL) {
1225 /* create the header block */
1226 ir_node *header_block = new_immBlock();
1228 /* the false block */
1229 ir_node *false_block = new_immBlock();
1232 ir_node *body_block = new_immBlock();
1234 add_immBlock_pred(body_block, jmp);
1237 ir_node *old_continue_label = continue_label;
1238 ir_node *old_break_label = break_label;
1239 continue_label = header_block;
1240 break_label = false_block;
1242 statement_to_firm(statement->body);
1244 assert(continue_label == header_block);
1245 assert(break_label == false_block);
1246 continue_label = old_continue_label;
1247 break_label = old_break_label;
1249 if(get_cur_block() == NULL) {
1250 mature_immBlock(header_block);
1251 mature_immBlock(body_block);
1255 ir_node *body_jmp = new_Jmp();
1256 add_immBlock_pred(header_block, body_jmp);
1257 mature_immBlock(header_block);
1259 /* create the condition */
1260 ir_node *condition = _expression_to_firm(statement->condition);
1261 condition = create_conv(dbgi, condition, mode_b);
1263 ir_node *cond = new_d_Cond(dbgi, condition);
1264 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1265 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1267 add_immBlock_pred(body_block, true_proj);
1268 mature_immBlock(body_block);
1270 add_immBlock_pred(false_block, false_proj);
1271 mature_immBlock(false_block);
1273 set_cur_block(false_block);
1276 static void create_declaration_entity(declaration_t *declaration,
1277 declaration_type_t declaration_type,
1278 ir_type *parent_type)
1280 ident *id = new_id_from_str(declaration->symbol->string);
1281 ir_type *irtype = get_ir_type(declaration->type);
1282 ir_entity *entity = new_entity(parent_type, id, irtype);
1283 set_entity_ld_ident(entity, id);
1285 declaration->declaration_type = declaration_type;
1286 declaration->v.entity = entity;
1287 set_entity_variability(entity, variability_uninitialized);
1288 /* TODO: visibility? */
1291 static void create_initializer(declaration_t *declaration)
1293 initializer_t *initializer = declaration->init.initializer;
1294 if(initializer == NULL)
1297 if(initializer->type == INITIALIZER_VALUE) {
1298 assert(initializer->designator == NULL);
1299 assert(initializer->next == NULL);
1300 ir_node *init_node = expression_to_firm(initializer->v.value);
1302 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1303 set_value(declaration->v.value_number, init_node);
1305 panic("initializer not completely implemented yet");
1308 assert(initializer->type == INITIALIZER_LIST);
1309 panic("list initializer not supported yet");
1313 static void create_local_variable(declaration_t *declaration)
1315 bool needs_entity = declaration->address_taken;
1317 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1320 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1321 create_declaration_entity(declaration,
1322 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1325 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1326 declaration->v.value_number = next_value_number_function;
1327 ++next_value_number_function;
1330 create_initializer(declaration);
1333 static void declaration_statement_to_firm(declaration_statement_t *statement)
1335 declaration_t *declaration = statement->declarations_begin;
1336 declaration_t *end = statement->declarations_end->next;
1337 for( ; declaration != end; declaration = declaration->next) {
1338 type_t *type = declaration->type;
1340 switch(declaration->storage_class) {
1341 case STORAGE_CLASS_TYPEDEF:
1343 case STORAGE_CLASS_STATIC:
1344 panic("static local vars not implemented yet");
1345 case STORAGE_CLASS_ENUM_ENTRY:
1346 panic("enum entry declaration in local block found");
1347 case STORAGE_CLASS_EXTERN:
1348 panic("extern declaration in local block found");
1349 case STORAGE_CLASS_NONE:
1350 case STORAGE_CLASS_AUTO:
1351 case STORAGE_CLASS_REGISTER:
1352 if(type->type == TYPE_FUNCTION) {
1353 panic("nested functions not supported yet");
1355 create_local_variable(declaration);
1359 panic("invalid storage class found");
1363 static void create_jump_statement(const statement_t *statement,
1364 ir_node *target_block)
1366 dbg_info *dbgi = get_dbg_info(&statement->source_position);
1367 ir_node *jump = new_d_Jmp(dbgi);
1368 add_immBlock_pred(target_block, jump);
1370 set_cur_block(NULL);
1373 static void switch_statement_to_firm(const switch_statement_t *statement)
1375 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1377 ir_node *expression = expression_to_firm(statement->expression);
1378 ir_node *cond = new_d_Cond(dbgi, expression);
1379 ir_node *break_block = new_immBlock();
1381 set_cur_block(NULL);
1383 ir_node *old_switch_cond = current_switch_cond;
1384 ir_node *old_break_label = break_label;
1385 current_switch_cond = cond;
1386 break_label = break_block;
1388 statement_to_firm(statement->body);
1390 if(get_cur_block() != NULL) {
1391 ir_node *jmp = new_Jmp();
1392 add_immBlock_pred(break_block, jmp);
1395 assert(current_switch_cond == cond);
1396 assert(break_label == break_block);
1397 current_switch_cond = old_switch_cond;
1398 break_label = old_break_label;
1400 mature_immBlock(break_block);
1401 set_cur_block(break_block);
1404 static void case_label_to_firm(const case_label_statement_t *statement)
1406 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1408 /* let's create a node and hope firm constant folding creates a Const
1411 set_cur_block(get_nodes_block(current_switch_cond));
1412 if(statement->expression) {
1413 ir_node *cnst = expression_to_firm(statement->expression);
1414 if(!is_Const(cnst)) {
1415 panic("couldn't fold constant for case label");
1417 tarval *tv = get_Const_tarval(cnst);
1418 if(!mode_is_int(get_tarval_mode(tv))) {
1419 panic("case label not an integer");
1422 long pn = get_tarval_long(tv);
1423 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
1424 /* oops someone detected our cheating... */
1425 panic("magic default pn used");
1427 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
1429 proj = new_d_defaultProj(dbgi, current_switch_cond,
1430 MAGIC_DEFAULT_PN_NUMBER);
1433 ir_node *block = new_immBlock();
1434 add_immBlock_pred(block, proj);
1435 mature_immBlock(block);
1438 static void statement_to_firm(statement_t *statement)
1440 switch(statement->type) {
1441 case STATEMENT_COMPOUND:
1442 compound_statement_to_firm((compound_statement_t*) statement);
1444 case STATEMENT_RETURN:
1445 return_statement_to_firm((return_statement_t*) statement);
1447 case STATEMENT_EXPRESSION:
1448 expression_statement_to_firm((expression_statement_t*) statement);
1451 if_statement_to_firm((if_statement_t*) statement);
1453 case STATEMENT_WHILE:
1454 while_statement_to_firm((while_statement_t*) statement);
1456 case STATEMENT_DO_WHILE:
1457 do_while_statement_to_firm((do_while_statement_t*) statement);
1459 case STATEMENT_DECLARATION:
1460 declaration_statement_to_firm((declaration_statement_t*) statement);
1462 case STATEMENT_BREAK:
1463 create_jump_statement(statement, break_label);
1465 case STATEMENT_CONTINUE:
1466 create_jump_statement(statement, continue_label);
1468 case STATEMENT_SWITCH:
1469 switch_statement_to_firm((switch_statement_t*) statement);
1471 case STATEMENT_CASE_LABEL:
1472 case_label_to_firm((case_label_statement_t*) statement);
1477 panic("Statement not implemented\n");
1480 static int get_function_n_local_vars(declaration_t *declaration)
1487 static void initialize_function_parameters(declaration_t *declaration)
1489 ir_graph *irg = current_ir_graph;
1490 ir_node *args = get_irg_args(irg);
1491 ir_node *start_block = get_irg_start_block(irg);
1494 declaration_t *parameter = declaration->context.declarations;
1495 for( ; parameter != NULL; parameter = parameter->next) {
1496 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
1498 if(parameter->address_taken) {
1499 panic("address take from parameter not implemented yet");
1502 ir_mode *mode = get_ir_mode(parameter->type);
1504 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
1507 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1508 parameter->v.value_number = next_value_number_function;
1509 ++next_value_number_function;
1511 set_value(parameter->v.value_number, proj);
1515 static void create_function(declaration_t *declaration)
1517 ir_entity *entity = get_function_entity(declaration);
1519 if(declaration->init.statement == NULL)
1522 int n_local_vars = get_function_n_local_vars(declaration);
1523 ir_graph *irg = new_ir_graph(entity, n_local_vars);
1524 ir_node *first_block = get_cur_block();
1526 next_value_number_function = 0;
1527 initialize_function_parameters(declaration);
1529 statement_to_firm(declaration->init.statement);
1531 ir_node *end_block = get_irg_end_block(irg);
1533 /* do we have a return statement yet? */
1534 if(get_cur_block() != NULL) {
1535 ir_node *ret = new_Return(get_store(), 0, NULL);
1536 add_immBlock_pred(end_block, ret);
1539 mature_immBlock(first_block);
1540 mature_immBlock(end_block);
1542 irg_finalize_cons(irg);
1544 /* finalize the frame type */
1545 ir_type *frame_type = get_irg_frame_type(irg);
1546 int n = get_compound_n_members(frame_type);
1549 for(int i = 0; i < n; ++i) {
1550 ir_entity *entity = get_compound_member(frame_type, i);
1551 ir_type *entity_type = get_entity_type(entity);
1553 int align = get_type_alignment_bytes(entity_type);
1554 if(align > align_all)
1558 misalign = offset % align;
1562 set_entity_offset(entity, offset);
1563 offset += get_type_size_bytes(entity_type);
1565 set_type_size_bytes(frame_type, offset);
1566 set_type_alignment_bytes(frame_type, align_all);
1567 set_type_state(frame_type, layout_fixed);
1572 static void context_to_firm(context_t *context)
1574 declaration_t *declaration = context->declarations;
1575 for( ; declaration != NULL; declaration = declaration->next) {
1576 if(declaration->namespace != NAMESPACE_NORMAL)
1579 type_t *type = declaration->type;
1580 if(type->type == TYPE_FUNCTION) {
1581 create_function(declaration);
1588 void translation_unit_to_firm(translation_unit_t *unit)
1590 /* remove me later TODO FIXME */
1591 (void) get_type_size;
1593 /* just to be sure */
1594 continue_label = NULL;
1596 current_switch_cond = NULL;
1598 context_to_firm(& unit->context);