9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
19 static ir_type *ir_type_const_char;
20 static ir_type *ir_type_void;
21 static ir_type *ir_type_int;
22 static ir_type *ir_type_void_ptr;
24 static type_t *type_const_char;
25 static type_t *type_void;
26 static type_t *type_int;
28 static int next_value_number_function;
30 typedef enum declaration_type_t {
31 DECLARATION_TYPE_UNKNOWN,
32 DECLARATION_TYPE_FUNCTION,
33 DECLARATION_TYPE_GLOBAL_VARIABLE,
34 DECLARATION_TYPE_LOCAL_VARIABLE,
35 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
36 DECLARATION_TYPE_COMPOUND_MEMBER
39 typedef struct type2firm_env_t type2firm_env_t;
40 struct type2firm_env_t {
41 int can_cache; /* nonzero if type can safely be cached because
42 no typevariables are in the hierarchy */
45 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type);
46 static ir_type *get_ir_type(type_t *type);
48 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
52 const declaration_t *declaration = & value_numbers[pos]->declaration;
54 print_warning_prefix(declaration->source_position);
55 fprintf(stderr, "variable '%s' might be used uninitialized\n",
56 declaration->symbol->string);
58 fprintf(stderr, "Some variable might be used uninitialized\n");
59 return new_r_Unknown(irg, mode);
62 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
64 const source_position_t *pos = (const source_position_t*) dbg;
67 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
71 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
73 const source_position_t *pos = (const source_position_t*) dbg;
78 return pos->input_name;
81 void init_ast2firm(void)
83 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
84 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
85 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
87 ir_type_int = get_ir_type(type_int);
88 ir_type_const_char = get_ir_type(type_const_char);
89 ir_type_void = get_ir_type(type_int); /* we don't have a real void
91 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
92 ir_type_void, mode_P_data);
94 type_void->firm_type = ir_type_void;
97 void exit_ast2firm(void)
101 static unsigned unique_id = 0;
103 static ident *unique_ident(const char *tag)
107 snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
109 return new_id_from_str(buf);
112 static type_t *skip_typeref(type_t *type)
117 const typedef_type_t *typedef_type = (const typedef_type_t*) type;
118 type = typedef_type->declaration->type;
122 const typeof_type_t *typeof_type = (const typeof_type_t *) type;
123 if(typeof_type->typeof_type != NULL) {
124 type = typeof_type->typeof_type;
126 type = typeof_type->expression->datatype;
139 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
141 switch(atomic_type->atype) {
142 case ATOMIC_TYPE_SCHAR:
143 case ATOMIC_TYPE_CHAR:
145 case ATOMIC_TYPE_UCHAR:
147 case ATOMIC_TYPE_SHORT:
149 case ATOMIC_TYPE_USHORT:
151 case ATOMIC_TYPE_LONG:
152 case ATOMIC_TYPE_INT:
154 case ATOMIC_TYPE_ULONG:
155 case ATOMIC_TYPE_UINT:
157 case ATOMIC_TYPE_LONGLONG:
159 case ATOMIC_TYPE_ULONGLONG:
161 case ATOMIC_TYPE_FLOAT:
163 case ATOMIC_TYPE_DOUBLE:
165 case ATOMIC_TYPE_LONG_DOUBLE:
167 case ATOMIC_TYPE_BOOL:
169 #ifdef PROVIDE_COMPLEX
170 case ATOMIC_TYPE_FLOAT_COMPLEX:
171 case ATOMIC_TYPE_DOUBLE_COMPLEX:
172 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
173 panic("complex lowering not implemented yet");
175 case ATOMIC_TYPE_FLOAT_IMAGINARY:
176 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
177 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
178 panic("imaginary lowering not implemented yet");
181 case ATOMIC_TYPE_VOID:
182 panic("tried to get mode from void type");
184 case ATOMIC_TYPE_INVALID:
187 panic("Encountered unknown atomic type");
191 static unsigned get_type_size(type_t *type);
193 static unsigned get_atomic_type_size(const atomic_type_t *type)
195 switch(type->atype) {
196 case ATOMIC_TYPE_CHAR:
197 case ATOMIC_TYPE_SCHAR:
198 case ATOMIC_TYPE_UCHAR:
201 case ATOMIC_TYPE_SHORT:
202 case ATOMIC_TYPE_USHORT:
205 case ATOMIC_TYPE_BOOL:
206 case ATOMIC_TYPE_INT:
207 case ATOMIC_TYPE_UINT:
208 case ATOMIC_TYPE_LONG:
209 case ATOMIC_TYPE_ULONG:
210 case ATOMIC_TYPE_FLOAT:
213 case ATOMIC_TYPE_LONGLONG:
214 case ATOMIC_TYPE_ULONGLONG:
215 case ATOMIC_TYPE_DOUBLE:
218 case ATOMIC_TYPE_LONG_DOUBLE:
221 case ATOMIC_TYPE_VOID:
224 case ATOMIC_TYPE_INVALID:
227 panic("Trying to determine size of invalid atomic type");
230 static unsigned get_compound_type_size(compound_type_t *type)
232 ir_type *irtype = get_ir_type(&type->type);
233 return get_type_size_bytes(irtype);
236 static unsigned get_array_type_size(array_type_t *type)
238 ir_type *irtype = get_ir_type(&type->type);
239 return get_type_size_bytes(irtype);
242 static unsigned get_type_size(type_t *type)
244 type = skip_typeref(type);
248 return get_atomic_type_size((const atomic_type_t*) type);
250 return get_mode_size_bytes(mode_Is);
251 case TYPE_COMPOUND_UNION:
252 case TYPE_COMPOUND_STRUCT:
253 return get_compound_type_size((compound_type_t*) type);
255 /* just a pointer to the function */
256 return get_mode_size_bytes(mode_P_code);
258 return get_mode_size_bytes(mode_P_data);
260 return get_array_type_size((array_type_t*) type);
267 panic("Trying to determine size of invalid type");
270 static unsigned count_parameters(const function_type_t *function_type)
274 function_parameter_t *parameter = function_type->parameters;
275 for ( ; parameter != NULL; parameter = parameter->next) {
285 static ir_type *create_atomic_type(type2firm_env_t *env,
286 const atomic_type_t *type)
289 ir_mode *mode = get_atomic_mode(type);
290 ident *id = get_mode_ident(mode);
291 ir_type *irtype = new_type_primitive(id, mode);
296 static ir_type *create_method_type(type2firm_env_t *env,
297 const function_type_t *function_type)
299 type_t *result_type = function_type->result_type;
301 ident *id = unique_ident("functiontype");
302 int n_parameters = count_parameters(function_type);
303 int n_results = result_type == type_void ? 0 : 1;
304 ir_type *irtype = new_type_method(id, n_parameters, n_results);
306 if(result_type != type_void) {
307 ir_type *restype = _get_ir_type(env, result_type);
308 set_method_res_type(irtype, 0, restype);
311 function_parameter_t *parameter = function_type->parameters;
313 for( ; parameter != NULL; parameter = parameter->next) {
314 ir_type *p_irtype = _get_ir_type(env, parameter->type);
315 set_method_param_type(irtype, n, p_irtype);
319 if(function_type->variadic) {
320 set_method_variadicity(irtype, variadicity_variadic);
326 static ir_type *create_pointer_type(type2firm_env_t *env, pointer_type_t *type)
328 type_t *points_to = type->points_to;
329 ir_type *ir_points_to;
330 /* Avoid endless recursion if the points_to type contains this poiner type
331 * again (might be a struct). We therefore first create a void* pointer
332 * and then set the real points_to type
334 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
335 ir_type_void, mode_P_data);
336 type->type.firm_type = ir_type;
338 ir_points_to = _get_ir_type(env, points_to);
339 set_pointer_points_to_type(ir_type, ir_points_to);
344 static ir_type *create_array_type(type2firm_env_t *env, array_type_t *type)
346 type_t *element_type = type->element_type;
347 ir_type *ir_element_type = _get_ir_type(env, element_type);
351 panic("TODO arraytpye size not implemented yet");
353 ir_type *ir_type = new_type_array(unique_ident("array"), 1, ir_element_type);
354 set_array_bounds_int(ir_type, 0, 0, n_elements);
356 size_t elemsize = get_type_size_bytes(ir_element_type);
357 int align = get_type_alignment_bytes(ir_element_type);
358 if(elemsize % align > 0) {
359 elemsize += align - (elemsize % align);
361 set_type_size_bytes(ir_type, n_elements * elemsize);
362 set_type_alignment_bytes(ir_type, align);
363 set_type_state(ir_type, layout_fixed);
368 #define INVALID_TYPE ((ir_type_ptr)-1)
370 static ir_type *create_struct_type(type2firm_env_t *env, compound_type_t *type)
372 symbol_t *symbol = type->declaration->symbol;
375 id = unique_ident(symbol->string);
377 id = unique_ident("__anonymous_struct");
379 ir_type *ir_type = new_type_struct(id);
381 type->type.firm_type = ir_type;
385 declaration_t *entry = type->declaration->context.declarations;
386 for( ; entry != NULL; entry = entry->next) {
387 ident *ident = new_id_from_str(entry->symbol->string);
388 ir_type_ptr entry_ir_type = _get_ir_type(env, entry->type);
390 int entry_size = get_type_size_bytes(entry_ir_type);
391 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
392 int misalign = offset % entry_alignment;
395 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
396 set_entity_offset(entity, offset);
397 add_struct_member(ir_type, entity);
398 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
399 entry->v.entity = entity;
401 offset += entry_size;
402 if(entry_alignment > align_all) {
403 if(entry_alignment % align_all != 0) {
404 panic("Uneven alignments not supported yet");
406 align_all = entry_alignment;
410 int misalign = offset % align_all;
412 set_type_alignment_bytes(ir_type, align_all);
413 set_type_size_bytes(ir_type, offset);
414 set_type_state(ir_type, layout_fixed);
419 static ir_type *create_union_type(type2firm_env_t *env, compound_type_t *type)
421 declaration_t *declaration = type->declaration;
422 symbol_t *symbol = declaration->symbol;
425 id = unique_ident(symbol->string);
427 id = unique_ident("__anonymous_union");
429 ir_type *ir_type = new_type_union(id);
431 type->type.firm_type = ir_type;
435 declaration_t *entry = declaration->context.declarations;
436 for( ; entry != NULL; entry = entry->next) {
437 ident *ident = new_id_from_str(entry->symbol->string);
438 ir_type_ptr entry_ir_type = _get_ir_type(env, entry->type);
440 int entry_size = get_type_size_bytes(entry_ir_type);
441 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
443 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
444 add_union_member(ir_type, entity);
445 set_entity_offset(entity, 0);
446 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
447 entry->v.entity = entity;
449 if(entry_size > size) {
452 if(entry_alignment > align_all) {
453 if(entry_alignment % align_all != 0) {
454 panic("Uneven alignments not supported yet");
456 align_all = entry_alignment;
460 set_type_alignment_bytes(ir_type, align_all);
461 set_type_size_bytes(ir_type, size);
462 set_type_state(ir_type, layout_fixed);
467 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type)
469 assert(type != NULL);
471 type = skip_typeref(type);
473 if(type->firm_type != NULL) {
474 assert(type->firm_type != INVALID_TYPE);
475 return type->firm_type;
478 ir_type *firm_type = NULL;
481 firm_type = create_atomic_type(env, (atomic_type_t*) type);
484 firm_type = create_method_type(env, (function_type_t*) type);
487 firm_type = create_pointer_type(env, (pointer_type_t*) type);
490 firm_type = create_array_type(env, (array_type_t*) type);
492 case TYPE_COMPOUND_STRUCT:
493 firm_type = create_struct_type(env, (compound_type_t*) type);
495 case TYPE_COMPOUND_UNION:
496 firm_type = create_union_type(env, (compound_type_t*) type);
499 firm_type = ir_type_int;
507 if(firm_type == NULL)
508 panic("unknown type found");
511 type->firm_type = firm_type;
517 static ir_type *get_ir_type(type_t *type)
522 return _get_ir_type(&env, type);
525 static inline ir_mode *get_ir_mode(type_t *type)
527 ir_type *irtype = get_ir_type(type);
528 ir_mode *mode = get_type_mode(irtype);
529 assert(mode != NULL);
533 static ir_entity* get_function_entity(declaration_t *declaration)
535 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
536 return declaration->v.entity;
537 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
539 symbol_t *symbol = declaration->symbol;
540 ident *id = new_id_from_str(symbol->string);
542 ir_type *global_type = get_glob_type();
543 ir_type *ir_type_method = get_ir_type(declaration->type);
544 assert(is_Method_type(ir_type_method));
546 type_t *type = declaration->type;
547 ir_entity *entity = new_entity(global_type, id, ir_type_method);
548 set_entity_ld_ident(entity, id);
549 if(declaration->storage_class & STORAGE_CLASS_STATIC
550 || type->qualifiers & TYPE_QUALIFIER_INLINE) {
551 set_entity_visibility(entity, visibility_local);
552 } else if(declaration->init.statement != NULL) {
553 set_entity_visibility(entity, visibility_external_visible);
555 set_entity_visibility(entity, visibility_external_allocated);
558 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
559 declaration->v.entity = entity;
566 static ir_node *expression_to_firm(const expression_t *expression);
568 static dbg_info *get_dbg_info(const source_position_t *pos)
570 return (dbg_info*) pos;
573 static ir_node *const_to_firm(const const_t *cnst)
575 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
576 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
579 if(mode_is_float(mode)) {
580 tv = new_tarval_from_double(cnst->v.float_value, mode);
582 tv = new_tarval_from_long(cnst->v.int_value, mode);
585 return new_d_Const(dbgi, mode, tv);
588 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
590 assert(entity != NULL);
591 union symconst_symbol sym;
592 sym.entity_p = entity;
593 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
596 static ir_node *string_literal_to_firm(const string_literal_t* literal)
598 ir_type *global_type = get_glob_type();
599 ir_type *type = new_type_array(unique_ident("strtype"), 1,
602 ident *id = unique_ident("Lstr");
603 ir_entity *entity = new_entity(global_type, id, type);
604 set_entity_ld_ident(entity, id);
605 set_entity_variability(entity, variability_constant);
607 ir_type *elem_type = ir_type_const_char;
608 ir_mode *mode = get_type_mode(elem_type);
610 const char *string = literal->value;
611 size_t slen = strlen(string) + 1;
613 set_array_lower_bound_int(type, 0, 0);
614 set_array_upper_bound_int(type, 0, slen);
615 set_type_size_bytes(type, slen);
616 set_type_state(type, layout_fixed);
618 tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
619 for(size_t i = 0; i < slen; ++i) {
620 tvs[i] = new_tarval_from_long(string[i], mode);
623 set_array_entity_values(entity, tvs, slen);
626 dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
628 return create_symconst(dbgi, entity);
631 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
633 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
634 declaration_t *declaration = ref->declaration;
635 type_t *type = declaration->type;
636 ir_mode *mode = get_ir_mode(type);
638 switch((declaration_type_t) declaration->declaration_type) {
639 case DECLARATION_TYPE_UNKNOWN:
641 case DECLARATION_TYPE_LOCAL_VARIABLE:
642 return get_value(declaration->v.value_number, mode);
643 case DECLARATION_TYPE_FUNCTION: {
644 return create_symconst(dbgi, declaration->v.entity);
646 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
647 case DECLARATION_TYPE_GLOBAL_VARIABLE:
648 case DECLARATION_TYPE_COMPOUND_MEMBER:
649 panic("not implemented reference type");
652 panic("reference to declaration with unknown type found");
655 static ir_node *call_expression_to_firm(const call_expression_t *call)
657 expression_t *function = call->function;
658 ir_node *callee = expression_to_firm(function);
660 assert(function->datatype->type == TYPE_FUNCTION);
661 function_type_t *function_type = (function_type_t*) function->datatype;
663 int n_parameters = 0;
664 call_argument_t *argument = call->arguments;
665 for( ; argument != NULL; argument = argument->next) {
669 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
670 ir_type *new_method_type = NULL;
671 if(function_type->variadic) {
672 /* we need to construct a new method type matching the call
674 int n_res = get_method_n_ress(ir_method_type);
675 new_method_type = new_type_method(unique_ident("calltype"),
676 n_parameters, n_res);
677 set_method_calling_convention(new_method_type,
678 get_method_calling_convention(ir_method_type));
679 set_method_additional_properties(new_method_type,
680 get_method_additional_properties(ir_method_type));
682 for(int i = 0; i < n_res; ++i) {
683 set_method_res_type(new_method_type, i,
684 get_method_res_type(ir_method_type, i));
687 ir_node *in[n_parameters];
689 argument = call->arguments;
691 for( ; argument != NULL; argument = argument->next) {
692 expression_t *expression = argument->expression;
693 ir_node *arg_node = expression_to_firm(expression);
696 if(new_method_type != NULL) {
697 ir_type *irtype = get_ir_type(expression->datatype);
698 set_method_param_type(new_method_type, n, irtype);
703 assert(n == n_parameters);
705 if(new_method_type != NULL)
706 ir_method_type = new_method_type;
708 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
709 ir_node *store = get_store();
710 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
712 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
715 type_t *result_type = function_type->result_type;
716 ir_node *result = NULL;
717 if(result_type != type_void) {
718 ir_mode *mode = get_ir_mode(result_type);
719 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
720 result = new_d_Proj(dbgi, resproj, mode, 0);
726 static ir_node *load_from_expression_addr(type_t *type, ir_node *addr,
729 ir_mode *mode = get_ir_mode(type);
730 ir_node *memory = get_store();
731 ir_node *load = new_d_Load(dbgi, memory, addr, mode);
732 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
733 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
739 static ir_node *expression_addr(const expression_t *expression)
742 panic("expression_addr not implemented yet");
746 static void set_value_for_expression(const expression_t *expression,
749 if(expression->type == EXPR_REFERENCE) {
750 reference_expression_t *ref = (reference_expression_t*) expression;
752 declaration_t *declaration = ref->declaration;
753 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
754 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
755 set_value(declaration->v.value_number, value);
759 panic("set_value_for_expression not implemented yet");
762 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
764 ir_mode *value_mode = get_irn_mode(value);
766 if(value_mode == dest_mode)
769 if(dest_mode == mode_b) {
770 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
771 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
772 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
776 return new_d_Conv(dbgi, value, dest_mode);
779 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
781 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
782 type_t *type = expression->expression.datatype;
783 ir_mode *mode = get_ir_mode(type);
785 if(expression->type == UNEXPR_TAKE_ADDRESS)
786 return expression_addr(expression->value);
788 const expression_t *value = expression->value;
789 ir_node *value_node = expression_to_firm(value);
791 switch(expression->type) {
793 return new_d_Minus(dbgi, value_node, mode);
796 case UNEXPR_BITWISE_NEGATE:
797 return new_d_Not(dbgi, value_node, mode);
799 if(get_irn_mode(value_node) != mode_b) {
800 value_node = create_conv(dbgi, value_node, mode_b);
802 value_node = new_d_Not(dbgi, value_node, mode_b);
804 value_node = create_conv(dbgi, value_node, mode);
807 case UNEXPR_DEREFERENCE:
808 return load_from_expression_addr(type, value_node, dbgi);
809 case UNEXPR_POSTFIX_INCREMENT: {
810 ir_node *one = new_Const(mode, get_mode_one(mode));
811 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
812 set_value_for_expression(value, new_value);
815 case UNEXPR_POSTFIX_DECREMENT: {
816 ir_node *one = new_Const(mode, get_mode_one(mode));
817 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
818 set_value_for_expression(value, new_value);
821 case UNEXPR_PREFIX_INCREMENT: {
822 ir_node *one = new_Const(mode, get_mode_one(mode));
823 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
824 set_value_for_expression(value, new_value);
827 case UNEXPR_PREFIX_DECREMENT: {
828 ir_node *one = new_Const(mode, get_mode_one(mode));
829 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
830 set_value_for_expression(value, new_value);
834 return create_conv(dbgi, value_node, mode);
836 case UNEXPR_TAKE_ADDRESS:
840 panic("invalid UNEXPR type found");
843 static long get_pnc(binary_expression_type_t type)
846 case BINEXPR_EQUAL: return pn_Cmp_Eq;
847 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
848 case BINEXPR_LESS: return pn_Cmp_Lt;
849 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
850 case BINEXPR_GREATER: return pn_Cmp_Gt;
851 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
855 panic("trying to get pn_Cmp from non-comparison binexpr type");
858 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
860 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
862 binary_expression_type_t type = expression->type;
865 case BINEXPR_NOTEQUAL:
867 case BINEXPR_LESSEQUAL:
868 case BINEXPR_GREATER:
869 case BINEXPR_GREATEREQUAL: {
870 ir_node *left = expression_to_firm(expression->left);
871 ir_node *right = expression_to_firm(expression->right);
872 ir_node *cmp = new_d_Cmp(dbgi, left, right);
873 long pnc = get_pnc(type);
874 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
877 case BINEXPR_ASSIGN: {
878 ir_node *right = expression_to_firm(expression->right);
879 set_value_for_expression(expression->left, right);
883 panic("TODO binexpr type");
887 static ir_node *expression_to_firm(const expression_t *expression)
889 switch(expression->type) {
891 return const_to_firm((const const_t*) expression);
892 case EXPR_STRING_LITERAL:
893 return string_literal_to_firm((const string_literal_t*) expression);
895 return reference_expression_to_firm(
896 (const reference_expression_t*) expression);
898 return call_expression_to_firm((const call_expression_t*) expression);
900 return unary_expression_to_firm((const unary_expression_t*) expression);
902 return binary_expression_to_firm(
903 (const binary_expression_t*) expression);
907 panic("unsupported expression found");
912 static void statement_to_firm(statement_t *statement);
914 static void return_statement_to_firm(return_statement_t *statement)
916 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
919 if(statement->return_value != NULL) {
920 ir_node *retval = expression_to_firm(statement->return_value);
924 ret = new_d_Return(dbgi, get_store(), 1, in);
926 ret = new_d_Return(dbgi, get_store(), 0, NULL);
928 ir_node *end_block = get_irg_end_block(current_ir_graph);
929 add_immBlock_pred(end_block, ret);
934 static void compound_statement_to_firm(compound_statement_t *compound)
936 statement_t *statement = compound->statements;
937 for( ; statement != NULL; statement = statement->next) {
938 //context2firm(&statement->context);
939 statement_to_firm(statement);
943 static void expression_statement_to_firm(expression_statement_t *statement)
945 expression_to_firm(statement->expression);
948 static void if_statement_to_firm(if_statement_t *statement)
950 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
951 ir_node *condition = expression_to_firm(statement->condition);
952 assert(condition != NULL);
954 /* make sure we have a mode_b condition */
955 condition = create_conv(dbgi, condition, mode_b);
957 ir_node *cond = new_d_Cond(dbgi, condition);
958 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
959 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
961 ir_node *fallthrough_block = new_immBlock();
963 /* the true (blocks) */
964 ir_node *true_block = new_immBlock();
965 add_immBlock_pred(true_block, true_proj);
966 mature_immBlock(true_block);
968 statement_to_firm(statement->true_statement);
969 if(get_cur_block() != NULL) {
970 ir_node *jmp = new_Jmp();
971 add_immBlock_pred(fallthrough_block, jmp);
974 /* the false (blocks) */
975 if(statement->false_statement != NULL) {
976 ir_node *false_block = new_immBlock();
977 add_immBlock_pred(false_block, false_proj);
978 mature_immBlock(false_block);
980 statement_to_firm(statement->false_statement);
981 if(get_cur_block() != NULL) {
982 ir_node *jmp = new_Jmp();
983 add_immBlock_pred(fallthrough_block, jmp);
986 add_immBlock_pred(fallthrough_block, false_proj);
988 mature_immBlock(fallthrough_block);
990 set_cur_block(fallthrough_block);
993 static void while_statement_to_firm(while_statement_t *statement)
995 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
997 /* create the header block */
998 ir_node *jmp = new_Jmp();
999 ir_node *header_block = new_immBlock();
1000 add_immBlock_pred(header, jmp);
1002 /* create the condition */
1003 ir_node *condition = expression_to_firm(statement->condition);
1004 assert(condition != NULL);
1005 /* make sure we have a mode_b condition */
1006 condition = create_conv(dbgi, condition, mode_b);
1008 ir_node *cond = new_d_Cond(dbgi, condition);
1009 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1010 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1013 ir_node *body_block = new_immBlock();
1014 add_immBlock_pred(body_block, true_proj);
1015 mature_immBlock(true_block);
1017 statement_to_firm(statement->true_statement);
1018 if(get_cur_block() != NULL) {
1019 ir_node *jmp = new_Jmp();
1020 add_immBlock_pred(fallthrough_block, jmp);
1023 /* the false (blocks) */
1024 if(statement->false_statement != NULL) {
1025 ir_node *false_block = new_immBlock();
1026 add_immBlock_pred(false_block, false_proj);
1027 mature_immBlock(false_block);
1029 statement_to_firm(statement->false_statement);
1030 if(get_cur_block() != NULL) {
1031 ir_node *jmp = new_Jmp();
1032 add_immBlock_pred(fallthrough_block, jmp);
1035 add_immBlock_pred(fallthrough_block, false_proj);
1037 mature_immBlock(fallthrough_block);
1039 set_cur_block(fallthrough_block);
1042 static void create_declaration_entity(declaration_t *declaration,
1043 declaration_type_t declaration_type,
1044 ir_type *parent_type)
1046 ident *id = new_id_from_str(declaration->symbol->string);
1047 ir_type *irtype = get_ir_type(declaration->type);
1048 ir_entity *entity = new_entity(parent_type, id, irtype);
1049 set_entity_ld_ident(entity, id);
1051 declaration->declaration_type = declaration_type;
1052 declaration->v.entity = entity;
1053 set_entity_variability(entity, variability_uninitialized);
1054 /* TODO: visibility? */
1057 static void create_initializer(declaration_t *declaration)
1059 initializer_t *initializer = declaration->init.initializer;
1060 if(initializer == NULL)
1063 if(initializer->type == INITIALIZER_VALUE) {
1064 assert(initializer->designator == NULL);
1065 assert(initializer->next == NULL);
1066 ir_node *init_node = expression_to_firm(initializer->v.value);
1068 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1069 set_value(declaration->v.value_number, init_node);
1071 panic("initializer not completely implemented yet");
1074 assert(initializer->type == INITIALIZER_LIST);
1075 panic("list initializer not supported yet");
1079 static void create_local_variable(declaration_t *declaration)
1081 bool needs_entity = declaration->address_taken;
1083 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1086 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1087 create_declaration_entity(declaration,
1088 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1091 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1092 declaration->v.value_number = next_value_number_function;
1093 ++next_value_number_function;
1096 create_initializer(declaration);
1099 static void declaration_statement_to_firm(declaration_statement_t *statement)
1101 declaration_t *declaration = statement->declarations_begin;
1102 declaration_t *end = statement->declarations_end->next;
1103 for( ; declaration != end; declaration = declaration->next) {
1104 type_t *type = declaration->type;
1106 switch(declaration->storage_class) {
1107 case STORAGE_CLASS_TYPEDEF:
1109 case STORAGE_CLASS_STATIC:
1110 panic("static local vars not implemented yet");
1111 case STORAGE_CLASS_ENUM_ENTRY:
1112 panic("enum entry declaration in local block found");
1113 case STORAGE_CLASS_EXTERN:
1114 panic("extern declaration in local block found");
1115 case STORAGE_CLASS_NONE:
1116 case STORAGE_CLASS_AUTO:
1117 case STORAGE_CLASS_REGISTER:
1118 if(type->type == TYPE_FUNCTION) {
1119 panic("nested functions not supported yet");
1121 create_local_variable(declaration);
1125 panic("invalid storage class found");
1129 static void statement_to_firm(statement_t *statement)
1131 switch(statement->type) {
1132 case STATEMENT_COMPOUND:
1133 compound_statement_to_firm((compound_statement_t*) statement);
1135 case STATEMENT_RETURN:
1136 return_statement_to_firm((return_statement_t*) statement);
1138 case STATEMENT_EXPRESSION:
1139 expression_statement_to_firm((expression_statement_t*) statement);
1142 if_statement_to_firm((if_statement_t*) statement);
1144 case STATEMENT_DECLARATION:
1145 declaration_statement_to_firm((declaration_statement_t*) statement);
1150 panic("Statement not implemented\n");
1153 static int get_function_n_local_vars(declaration_t *declaration)
1160 static void initialize_function_parameters(declaration_t *declaration)
1162 ir_graph *irg = current_ir_graph;
1163 ir_node *args = get_irg_args(irg);
1164 ir_node *start_block = get_irg_start_block(irg);
1167 declaration_t *parameter = declaration->context.declarations;
1168 for( ; parameter != NULL; parameter = parameter->next) {
1169 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
1171 if(parameter->address_taken) {
1172 panic("address take from parameter not implemented yet");
1175 ir_mode *mode = get_ir_mode(parameter->type);
1177 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
1180 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1181 parameter->v.value_number = next_value_number_function;
1182 ++next_value_number_function;
1184 set_value(parameter->v.value_number, proj);
1188 static void create_function(declaration_t *declaration)
1190 ir_entity *entity = get_function_entity(declaration);
1192 if(declaration->init.statement == NULL)
1195 int n_local_vars = get_function_n_local_vars(declaration);
1196 ir_graph *irg = new_ir_graph(entity, n_local_vars);
1197 ir_node *first_block = get_cur_block();
1199 next_value_number_function = 0;
1200 initialize_function_parameters(declaration);
1202 statement_to_firm(declaration->init.statement);
1204 ir_node *end_block = get_irg_end_block(irg);
1206 /* do we have a return statement yet? */
1207 if(get_cur_block() != NULL) {
1208 ir_node *ret = new_Return(get_store(), 0, NULL);
1209 add_immBlock_pred(end_block, ret);
1212 mature_immBlock(first_block);
1213 mature_immBlock(end_block);
1215 irg_finalize_cons(irg);
1217 /* finalize the frame type */
1218 ir_type *frame_type = get_irg_frame_type(irg);
1219 int n = get_compound_n_members(frame_type);
1222 for(int i = 0; i < n; ++i) {
1223 ir_entity *entity = get_compound_member(frame_type, i);
1224 ir_type *entity_type = get_entity_type(entity);
1226 int align = get_type_alignment_bytes(entity_type);
1227 if(align > align_all)
1231 misalign = offset % align;
1235 set_entity_offset(entity, offset);
1236 offset += get_type_size_bytes(entity_type);
1238 set_type_size_bytes(frame_type, offset);
1239 set_type_alignment_bytes(frame_type, align_all);
1240 set_type_state(frame_type, layout_fixed);
1245 static void context_to_firm(context_t *context)
1247 declaration_t *declaration = context->declarations;
1248 for( ; declaration != NULL; declaration = declaration->next) {
1249 type_t *type = declaration->type;
1250 if(type->type == TYPE_FUNCTION) {
1251 create_function(declaration);
1258 void translation_unit_to_firm(translation_unit_t *unit)
1260 /* remove me later TODO FIXME */
1261 (void) get_type_size;
1263 context_to_firm(& unit->context);