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_void;
25 static ir_type *ir_type_int;
26 static ir_type *ir_type_void_ptr;
28 static type_t *type_const_char;
29 static type_t *type_void;
30 static type_t *type_int;
32 static symbol_t *symbol_alloca;
34 static int next_value_number_function;
35 static ir_node *continue_label;
36 static ir_node *break_label;
37 static ir_node *current_switch_cond;
38 static bool saw_default_label;
39 static ir_node **imature_blocks;
41 static const declaration_t *current_function_decl;
42 static ir_node *current_function_name;
44 typedef enum declaration_type_t {
45 DECLARATION_TYPE_UNKNOWN,
46 DECLARATION_TYPE_FUNCTION,
47 DECLARATION_TYPE_GLOBAL_VARIABLE,
48 DECLARATION_TYPE_LOCAL_VARIABLE,
49 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
50 DECLARATION_TYPE_COMPOUND_MEMBER,
51 DECLARATION_TYPE_LABEL_BLOCK,
52 DECLARATION_TYPE_ENUM_ENTRY
55 static ir_type *get_ir_type(type_t *type);
57 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
61 const declaration_t *declaration = & value_numbers[pos]->declaration;
63 print_warning_prefix(declaration->source_position);
64 fprintf(stderr, "variable '%s' might be used uninitialized\n",
65 declaration->symbol->string);
67 fprintf(stderr, "Some variable might be used uninitialized\n");
68 return new_r_Unknown(irg, mode);
71 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
73 const source_position_t *pos = (const source_position_t*) dbg;
76 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
80 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
82 const source_position_t *pos = (const source_position_t*) dbg;
87 return pos->input_name;
90 void init_ast2firm(void)
92 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
93 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
94 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
96 ir_type_int = get_ir_type(type_int);
97 ir_type_const_char = get_ir_type(type_const_char);
98 ir_type_void = get_ir_type(type_int); /* we don't have a real void
100 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
101 ir_type_void, mode_P_data);
103 type_void->base.firm_type = ir_type_void;
105 symbol_alloca = symbol_table_insert("__builtin_alloca");
108 void exit_ast2firm(void)
112 static unsigned unique_id = 0;
114 static ident *unique_ident(const char *tag)
118 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
120 return new_id_from_str(buf);
123 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
125 switch(atomic_type->atype) {
126 case ATOMIC_TYPE_SCHAR:
127 case ATOMIC_TYPE_CHAR:
129 case ATOMIC_TYPE_UCHAR:
131 case ATOMIC_TYPE_SHORT:
133 case ATOMIC_TYPE_USHORT:
135 case ATOMIC_TYPE_LONG:
136 case ATOMIC_TYPE_INT:
138 case ATOMIC_TYPE_ULONG:
139 case ATOMIC_TYPE_UINT:
141 case ATOMIC_TYPE_LONGLONG:
143 case ATOMIC_TYPE_ULONGLONG:
145 case ATOMIC_TYPE_FLOAT:
147 case ATOMIC_TYPE_DOUBLE:
149 case ATOMIC_TYPE_LONG_DOUBLE:
151 case ATOMIC_TYPE_BOOL:
153 #ifdef PROVIDE_COMPLEX
154 case ATOMIC_TYPE_FLOAT_COMPLEX:
155 case ATOMIC_TYPE_DOUBLE_COMPLEX:
156 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
157 panic("complex lowering not implemented yet");
159 case ATOMIC_TYPE_FLOAT_IMAGINARY:
160 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
161 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
162 panic("imaginary lowering not implemented yet");
165 case ATOMIC_TYPE_VOID:
166 /* firm has no real void... */
168 case ATOMIC_TYPE_INVALID:
171 panic("Encountered unknown atomic type");
175 static unsigned get_type_size(type_t *type);
177 static unsigned get_atomic_type_size(const atomic_type_t *type)
179 switch(type->atype) {
180 case ATOMIC_TYPE_CHAR:
181 case ATOMIC_TYPE_SCHAR:
182 case ATOMIC_TYPE_UCHAR:
185 case ATOMIC_TYPE_SHORT:
186 case ATOMIC_TYPE_USHORT:
189 case ATOMIC_TYPE_BOOL:
190 case ATOMIC_TYPE_INT:
191 case ATOMIC_TYPE_UINT:
192 case ATOMIC_TYPE_LONG:
193 case ATOMIC_TYPE_ULONG:
194 case ATOMIC_TYPE_FLOAT:
197 case ATOMIC_TYPE_LONGLONG:
198 case ATOMIC_TYPE_ULONGLONG:
199 case ATOMIC_TYPE_DOUBLE:
202 case ATOMIC_TYPE_LONG_DOUBLE:
205 case ATOMIC_TYPE_VOID:
208 case ATOMIC_TYPE_INVALID:
211 panic("Trying to determine size of invalid atomic type");
214 static unsigned get_compound_type_size(compound_type_t *type)
216 ir_type *irtype = get_ir_type((type_t*) type);
217 return get_type_size_bytes(irtype);
220 static unsigned get_array_type_size(array_type_t *type)
222 ir_type *irtype = get_ir_type((type_t*) type);
223 return get_type_size_bytes(irtype);
226 static unsigned get_type_size(type_t *type)
228 type = skip_typeref(type);
232 return get_atomic_type_size(&type->atomic);
234 return get_mode_size_bytes(mode_Is);
235 case TYPE_COMPOUND_UNION:
236 case TYPE_COMPOUND_STRUCT:
237 return get_compound_type_size(&type->compound);
239 /* just a pointer to the function */
240 return get_mode_size_bytes(mode_P_code);
242 return get_mode_size_bytes(mode_P_data);
244 return get_array_type_size(&type->array);
252 panic("Trying to determine size of invalid type");
255 static unsigned count_parameters(const function_type_t *function_type)
259 function_parameter_t *parameter = function_type->parameters;
260 for ( ; parameter != NULL; parameter = parameter->next) {
270 static long fold_constant(const expression_t *expression);
272 static ir_type *create_atomic_type(const atomic_type_t *type)
274 ir_mode *mode = get_atomic_mode(type);
275 ident *id = get_mode_ident(mode);
276 ir_type *irtype = new_type_primitive(id, mode);
281 static ir_type *create_method_type(const function_type_t *function_type)
283 type_t *result_type = function_type->result_type;
285 ident *id = unique_ident("functiontype");
286 int n_parameters = count_parameters(function_type);
287 int n_results = result_type == type_void ? 0 : 1;
288 ir_type *irtype = new_type_method(id, n_parameters, n_results);
290 if(result_type != type_void) {
291 ir_type *restype = get_ir_type(result_type);
292 set_method_res_type(irtype, 0, restype);
295 function_parameter_t *parameter = function_type->parameters;
297 for( ; parameter != NULL; parameter = parameter->next) {
298 ir_type *p_irtype = get_ir_type(parameter->type);
299 set_method_param_type(irtype, n, p_irtype);
303 if(function_type->variadic || function_type->unspecified_parameters) {
304 set_method_variadicity(irtype, variadicity_variadic);
310 static ir_type *create_pointer_type(pointer_type_t *type)
312 type_t *points_to = type->points_to;
313 ir_type *ir_points_to;
314 /* Avoid endless recursion if the points_to type contains this poiner type
315 * again (might be a struct). We therefore first create a void* pointer
316 * and then set the real points_to type
318 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
319 ir_type_void, mode_P_data);
320 type->type.firm_type = ir_type;
322 ir_points_to = get_ir_type(points_to);
323 set_pointer_points_to_type(ir_type, ir_points_to);
328 static ir_type *create_array_type(array_type_t *type)
330 type_t *element_type = type->element_type;
331 ir_type *ir_element_type = get_ir_type(element_type);
333 ident *id = unique_ident("array");
334 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
336 if(type->size != NULL) {
337 int n_elements = fold_constant(type->size);
339 set_array_bounds_int(ir_type, 0, 0, n_elements);
341 size_t elemsize = get_type_size_bytes(ir_element_type);
342 int align = get_type_alignment_bytes(ir_element_type);
343 if(elemsize % align > 0) {
344 elemsize += align - (elemsize % align);
346 set_type_size_bytes(ir_type, n_elements * elemsize);
347 set_type_alignment_bytes(ir_type, align);
348 set_type_state(ir_type, layout_fixed);
354 #define INVALID_TYPE ((ir_type_ptr)-1)
356 static ir_type *create_struct_type(compound_type_t *type)
358 symbol_t *symbol = type->declaration->symbol;
361 id = unique_ident(symbol->string);
363 id = unique_ident("__anonymous_struct");
365 ir_type *ir_type = new_type_struct(id);
367 type->type.firm_type = ir_type;
371 declaration_t *entry = type->declaration->context.declarations;
372 for( ; entry != NULL; entry = entry->next) {
373 if(entry->namespc != NAMESPACE_NORMAL)
376 ident *ident = new_id_from_str(entry->symbol->string);
377 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
379 int entry_size = get_type_size_bytes(entry_ir_type);
380 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
381 int misalign = offset % entry_alignment;
383 offset += entry_alignment - misalign;
385 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
386 set_entity_offset(entity, offset);
387 add_struct_member(ir_type, entity);
388 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
389 entry->v.entity = entity;
391 offset += entry_size;
392 if(entry_alignment > align_all) {
393 if(entry_alignment % align_all != 0) {
394 panic("Uneven alignments not supported yet");
396 align_all = entry_alignment;
400 int misalign = offset % align_all;
402 set_type_alignment_bytes(ir_type, align_all);
403 set_type_size_bytes(ir_type, offset);
404 set_type_state(ir_type, layout_fixed);
409 static ir_type *create_union_type(compound_type_t *type)
411 declaration_t *declaration = type->declaration;
412 symbol_t *symbol = declaration->symbol;
415 id = unique_ident(symbol->string);
417 id = unique_ident("__anonymous_union");
419 ir_type *ir_type = new_type_union(id);
421 type->type.firm_type = ir_type;
425 declaration_t *entry = declaration->context.declarations;
426 for( ; entry != NULL; entry = entry->next) {
427 if(entry->namespc != NAMESPACE_NORMAL)
430 ident *ident = new_id_from_str(entry->symbol->string);
431 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
433 int entry_size = get_type_size_bytes(entry_ir_type);
434 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
436 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
437 add_union_member(ir_type, entity);
438 set_entity_offset(entity, 0);
439 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
440 entry->v.entity = entity;
442 if(entry_size > size) {
445 if(entry_alignment > align_all) {
446 if(entry_alignment % align_all != 0) {
447 panic("Uneven alignments not supported yet");
449 align_all = entry_alignment;
453 set_type_alignment_bytes(ir_type, align_all);
454 set_type_size_bytes(ir_type, size);
455 set_type_state(ir_type, layout_fixed);
460 static ir_node *expression_to_firm(const expression_t *expression);
461 static inline ir_mode *get_ir_mode(type_t *type);
463 static ir_type *create_enum_type(enum_type_t *const type)
465 type->type.firm_type = ir_type_int;
467 ir_mode *const mode = get_ir_mode((type_t*) type);
468 tarval *const one = get_mode_one(mode);
469 tarval * tv_next = get_tarval_null(mode);
471 declaration_t *declaration = type->declaration->next;
472 for (; declaration != NULL; declaration = declaration->next) {
473 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
476 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
478 expression_t *const init = declaration->init.enum_value;
480 ir_node *const cnst = expression_to_firm(init);
481 if (!is_Const(cnst)) {
482 panic("couldn't fold constant");
484 tv_next = get_Const_tarval(cnst);
486 declaration->v.enum_val = tv_next;
487 tv_next = tarval_add(tv_next, one);
493 static ir_type *get_ir_type(type_t *type)
495 assert(type != NULL);
497 type = skip_typeref(type);
499 if(type->base.firm_type != NULL) {
500 assert(type->base.firm_type != INVALID_TYPE);
501 return type->base.firm_type;
504 ir_type *firm_type = NULL;
507 firm_type = create_atomic_type(&type->atomic);
510 firm_type = create_method_type(&type->function);
513 firm_type = create_pointer_type(&type->pointer);
516 firm_type = create_array_type(&type->array);
518 case TYPE_COMPOUND_STRUCT:
519 firm_type = create_struct_type(&type->compound);
521 case TYPE_COMPOUND_UNION:
522 firm_type = create_union_type(&type->compound);
525 firm_type = create_enum_type(&type->enumt);
534 if(firm_type == NULL)
535 panic("unknown type found");
537 type->base.firm_type = firm_type;
541 static inline ir_mode *get_ir_mode(type_t *type)
543 ir_type *irtype = get_ir_type(type);
545 /* firm doesn't report a mode for arrays somehow... */
546 if(is_Array_type(irtype)) {
550 ir_mode *mode = get_type_mode(irtype);
551 assert(mode != NULL);
555 static ir_entity* get_function_entity(declaration_t *declaration)
557 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
558 return declaration->v.entity;
559 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
561 symbol_t *symbol = declaration->symbol;
562 ident *id = new_id_from_str(symbol->string);
564 ir_type *global_type = get_glob_type();
565 ir_type *ir_type_method = get_ir_type(declaration->type);
566 assert(is_Method_type(ir_type_method));
568 ir_entity *entity = new_entity(global_type, id, ir_type_method);
569 set_entity_ld_ident(entity, id);
570 if(declaration->storage_class == STORAGE_CLASS_STATIC
571 || declaration->is_inline) {
572 set_entity_visibility(entity, visibility_local);
573 } else if(declaration->init.statement != NULL) {
574 set_entity_visibility(entity, visibility_external_visible);
576 set_entity_visibility(entity, visibility_external_allocated);
579 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
580 declaration->v.entity = entity;
585 static dbg_info *get_dbg_info(const source_position_t *pos)
587 return (dbg_info*) pos;
590 static ir_node *const_to_firm(const const_expression_t *cnst)
592 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
593 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
598 if(mode_is_float(mode)) {
599 tv = new_tarval_from_double(cnst->v.float_value, mode);
601 if(mode_is_signed(mode)) {
602 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
604 len = snprintf(buf, sizeof(buf), "%llu", cnst->v.int_value);
606 tv = new_tarval_from_str(buf, len, mode);
609 return new_d_Const(dbgi, mode, tv);
612 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
614 assert(entity != NULL);
615 union symconst_symbol sym;
616 sym.entity_p = entity;
617 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
620 static ir_node *string_to_firm(const source_position_t *const src_pos,
621 const char *const id_prefix,
622 const char *const string)
624 ir_type *const global_type = get_glob_type();
625 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
628 ident *const id = unique_ident(id_prefix);
629 ir_entity *const entity = new_entity(global_type, id, type);
630 set_entity_ld_ident(entity, id);
631 set_entity_variability(entity, variability_constant);
633 ir_type *const elem_type = ir_type_const_char;
634 ir_mode *const mode = get_type_mode(elem_type);
636 const size_t slen = strlen(string) + 1;
638 set_array_lower_bound_int(type, 0, 0);
639 set_array_upper_bound_int(type, 0, slen);
640 set_type_size_bytes(type, slen);
641 set_type_state(type, layout_fixed);
643 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
644 for(size_t i = 0; i < slen; ++i) {
645 tvs[i] = new_tarval_from_long(string[i], mode);
648 set_array_entity_values(entity, tvs, slen);
651 dbg_info *const dbgi = get_dbg_info(src_pos);
653 return create_symconst(dbgi, entity);
656 static ir_node *string_literal_to_firm(
657 const string_literal_expression_t* literal)
659 return string_to_firm(&literal->expression.source_position, "Lstr",
663 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
664 dbg_info *const dbgi)
666 if(is_compound_type(irtype) || is_Array_type(irtype)) {
670 ir_mode *const mode = get_type_mode(irtype);
671 ir_node *const memory = get_store();
672 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
673 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
674 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
679 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
681 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
682 declaration_t *declaration = ref->declaration;
683 type_t *type = skip_typeref(declaration->type);
685 switch((declaration_type_t) declaration->declaration_type) {
686 case DECLARATION_TYPE_UNKNOWN:
687 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
693 case DECLARATION_TYPE_ENUM_ENTRY: {
694 ir_mode *const mode = get_ir_mode(type);
695 return new_Const(mode, declaration->v.enum_val);
698 case DECLARATION_TYPE_LOCAL_VARIABLE: {
699 ir_mode *mode = get_ir_mode(type);
700 return get_value(declaration->v.value_number, mode);
702 case DECLARATION_TYPE_FUNCTION: {
703 return create_symconst(dbgi, declaration->v.entity);
705 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
706 ir_entity *entity = declaration->v.entity;
707 ir_node *symconst = create_symconst(dbgi, entity);
708 ir_type *irtype = get_entity_type(entity);
709 return deref_address(irtype, symconst, dbgi);
711 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
712 ir_entity *entity = declaration->v.entity;
713 ir_node *frame = get_irg_frame(current_ir_graph);
714 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
715 ir_type *irtype = get_entity_type(entity);
716 return deref_address(irtype, sel, dbgi);
719 case DECLARATION_TYPE_COMPOUND_MEMBER:
720 case DECLARATION_TYPE_LABEL_BLOCK:
721 panic("not implemented reference type");
724 panic("reference to declaration with unknown type found");
727 static ir_node *reference_addr(const reference_expression_t *ref)
729 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
730 declaration_t *declaration = ref->declaration;
732 switch((declaration_type_t) declaration->declaration_type) {
733 case DECLARATION_TYPE_UNKNOWN:
735 case DECLARATION_TYPE_LOCAL_VARIABLE:
736 panic("local variable without entity has no address");
737 case DECLARATION_TYPE_FUNCTION: {
738 return create_symconst(dbgi, declaration->v.entity);
740 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
741 ir_entity *entity = declaration->v.entity;
742 ir_node *symconst = create_symconst(dbgi, entity);
745 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
746 ir_entity *entity = declaration->v.entity;
747 ir_node *frame = get_irg_frame(current_ir_graph);
748 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
753 case DECLARATION_TYPE_ENUM_ENTRY:
754 panic("trying to reference enum entry");
756 case DECLARATION_TYPE_COMPOUND_MEMBER:
757 case DECLARATION_TYPE_LABEL_BLOCK:
758 panic("not implemented reference type");
761 panic("reference to declaration with unknown type found");
764 static ir_node *process_builtin_call(const call_expression_t *call)
766 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
768 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
769 builtin_symbol_expression_t *builtin
770 = (builtin_symbol_expression_t*) call->function;
771 symbol_t *symbol = builtin->symbol;
773 if(symbol == symbol_alloca) {
774 if(call->arguments == NULL || call->arguments->next != NULL) {
775 panic("invalid number of parameters on __builtin_alloca");
777 expression_t *argument = call->arguments->expression;
778 ir_node *size = expression_to_firm(argument);
780 ir_node *store = get_store();
781 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
783 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
785 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
789 panic("Unsupported builtin found\n");
793 static ir_node *call_expression_to_firm(const call_expression_t *call)
795 assert(get_cur_block() != NULL);
797 expression_t *function = call->function;
798 if(function->type == EXPR_BUILTIN_SYMBOL) {
799 return process_builtin_call(call);
801 ir_node *callee = expression_to_firm(function);
803 type_t *type = function->base.datatype;
804 assert(type->type == TYPE_POINTER);
805 pointer_type_t *const ptr_type = &type->pointer;
806 assert(ptr_type->points_to->type == TYPE_FUNCTION);
807 function_type_t *function_type = &ptr_type->points_to->function;
809 int n_parameters = 0;
810 call_argument_t *argument = call->arguments;
811 for( ; argument != NULL; argument = argument->next) {
815 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
816 ir_type *new_method_type = NULL;
817 if(function_type->variadic || function_type->unspecified_parameters) {
818 /* we need to construct a new method type matching the call
820 int n_res = get_method_n_ress(ir_method_type);
821 new_method_type = new_type_method(unique_ident("calltype"),
822 n_parameters, n_res);
823 set_method_calling_convention(new_method_type,
824 get_method_calling_convention(ir_method_type));
825 set_method_additional_properties(new_method_type,
826 get_method_additional_properties(ir_method_type));
828 for(int i = 0; i < n_res; ++i) {
829 set_method_res_type(new_method_type, i,
830 get_method_res_type(ir_method_type, i));
833 ir_node *in[n_parameters];
835 argument = call->arguments;
837 for( ; argument != NULL; argument = argument->next) {
838 expression_t *expression = argument->expression;
839 ir_node *arg_node = expression_to_firm(expression);
842 if(new_method_type != NULL) {
843 ir_type *irtype = get_ir_type(expression->base.datatype);
844 set_method_param_type(new_method_type, n, irtype);
849 assert(n == n_parameters);
851 if(new_method_type != NULL)
852 ir_method_type = new_method_type;
854 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
855 ir_node *store = get_store();
856 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
858 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
861 type_t *result_type = function_type->result_type;
862 ir_node *result = NULL;
863 if(result_type != type_void) {
864 ir_mode *mode = get_ir_mode(result_type);
865 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
866 result = new_d_Proj(dbgi, resproj, mode, 0);
872 static void statement_to_firm(statement_t *statement);
873 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
875 static ir_node *expression_to_addr(const expression_t *expression);
876 static void create_condition_evaluation(const expression_t *expression,
878 ir_node *false_block);
880 static void set_value_for_expression(const expression_t *expression,
883 if(expression->type == EXPR_REFERENCE) {
884 reference_expression_t *ref = (reference_expression_t*) expression;
886 declaration_t *declaration = ref->declaration;
887 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
888 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
889 set_value(declaration->v.value_number, value);
894 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
895 ir_node *addr = expression_to_addr(expression);
896 assert(get_irn_mode(value) == get_ir_mode(expression->base.datatype));
897 ir_node *memory = get_store();
898 ir_node *store = new_d_Store(dbgi, memory, addr, value);
899 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
900 set_store(store_mem);
903 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
905 ir_mode *value_mode = get_irn_mode(value);
907 if (value_mode == dest_mode || is_Bad(value))
910 if(dest_mode == mode_b) {
911 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
912 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
913 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
917 return new_d_Conv(dbgi, value, dest_mode);
920 static ir_node *create_incdec(const unary_expression_t *expression)
922 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
923 type_t *type = expression->expression.datatype;
924 ir_mode *mode = get_ir_mode(type);
925 expression_t *value = expression->value;
927 ir_node *value_node = expression_to_firm(value);
930 if(type->type == TYPE_POINTER) {
931 pointer_type_t *pointer_type = &type->pointer;
932 unsigned elem_size = get_type_size(pointer_type->points_to);
933 offset = new_Const_long(mode_Is, elem_size);
935 assert(is_type_arithmetic(type));
936 offset = new_Const(mode, get_mode_one(mode));
939 switch(expression->type) {
940 case UNEXPR_POSTFIX_INCREMENT: {
941 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
942 set_value_for_expression(value, new_value);
945 case UNEXPR_POSTFIX_DECREMENT: {
946 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
947 set_value_for_expression(value, new_value);
950 case UNEXPR_PREFIX_INCREMENT: {
951 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
952 set_value_for_expression(value, new_value);
955 case UNEXPR_PREFIX_DECREMENT: {
956 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
957 set_value_for_expression(value, new_value);
961 panic("no incdec expr in create_incdec");
966 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
968 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
969 type_t *type = skip_typeref(expression->expression.datatype);
971 if(expression->type == UNEXPR_TAKE_ADDRESS)
972 return expression_to_addr(expression->value);
974 const expression_t *value = expression->value;
975 ir_node *value_node = expression_to_firm(value);
977 switch(expression->type) {
979 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
982 case UNEXPR_BITWISE_NEGATE:
983 return new_d_Not(dbgi, value_node, get_ir_mode(type));
985 if(get_irn_mode(value_node) != mode_b) {
986 value_node = create_conv(dbgi, value_node, mode_b);
988 value_node = new_d_Not(dbgi, value_node, mode_b);
989 ir_mode *const mode = get_ir_mode(type);
991 value_node = create_conv(dbgi, value_node, mode);
995 case UNEXPR_DEREFERENCE: {
996 ir_type *irtype = get_ir_type(type);
997 return deref_address(irtype, value_node, dbgi);
999 case UNEXPR_POSTFIX_INCREMENT:
1000 case UNEXPR_POSTFIX_DECREMENT:
1001 case UNEXPR_PREFIX_INCREMENT:
1002 case UNEXPR_PREFIX_DECREMENT:
1003 return create_incdec(expression);
1005 return create_conv(dbgi, value_node, get_ir_mode(type));
1007 case UNEXPR_TAKE_ADDRESS:
1008 case UNEXPR_INVALID:
1011 panic("invalid UNEXPR type found");
1014 static long get_pnc(binary_expression_type_t type)
1017 case BINEXPR_EQUAL: return pn_Cmp_Eq;
1018 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
1019 case BINEXPR_LESS: return pn_Cmp_Lt;
1020 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
1021 case BINEXPR_GREATER: return pn_Cmp_Gt;
1022 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
1026 panic("trying to get pn_Cmp from non-comparison binexpr type");
1029 static ir_node *create_lazy_op(const binary_expression_t *expression)
1031 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1032 type_t *type = expression->expression.datatype;
1033 ir_mode *mode = get_ir_mode(type);
1035 ir_node *cur_block = get_cur_block();
1037 ir_node *one_block = new_immBlock();
1038 ir_node *one = new_Const(mode, get_mode_one(mode));
1039 ir_node *jmp_one = new_d_Jmp(dbgi);
1041 ir_node *zero_block = new_immBlock();
1042 ir_node *zero = new_Const(mode, get_mode_null(mode));
1043 ir_node *jmp_zero = new_d_Jmp(dbgi);
1045 set_cur_block(cur_block);
1046 create_condition_evaluation((const expression_t*) expression,
1047 one_block, zero_block);
1048 mature_immBlock(one_block);
1049 mature_immBlock(zero_block);
1051 ir_node *common_block = new_immBlock();
1052 add_immBlock_pred(common_block, jmp_one);
1053 add_immBlock_pred(common_block, jmp_zero);
1054 mature_immBlock(common_block);
1056 ir_node *in[2] = { one, zero };
1057 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1062 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1063 ir_node *right, ir_mode *mode);
1065 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1066 create_arithmetic_func func)
1068 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1069 ir_node *left = expression_to_firm(expression->left);
1070 ir_node *right = expression_to_firm(expression->right);
1071 type_t *type = expression->right->base.datatype;
1072 /* be careful with the modes, because in arithmetic assign nodes only
1073 * the right operand has the mode of the arithmetic already */
1074 ir_mode *mode = get_ir_mode(type);
1075 left = create_conv(dbgi, left, mode);
1076 ir_node *res = func(dbgi, left, right, mode);
1081 static ir_node *pointer_arithmetic(ir_node *const pointer,
1084 dbg_info *const dbgi,
1085 const create_arithmetic_func func)
1087 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1088 type_t *const points_to = pointer_type->points_to;
1089 const unsigned elem_size = get_type_size(points_to);
1091 assert(elem_size >= 1);
1092 if (elem_size > 1) {
1093 integer = create_conv(dbgi, integer, mode_Is);
1094 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1095 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1099 ir_mode *const mode = get_ir_mode(type);
1100 return func(dbgi, pointer, integer, mode);
1103 static ir_node *create_arithmetic_assign_binop(
1104 const binary_expression_t *expression, create_arithmetic_func func)
1106 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1107 type_t *const type = expression->expression.datatype;
1110 if (type->type == TYPE_POINTER) {
1111 ir_node *const pointer = expression_to_firm(expression->left);
1112 ir_node * integer = expression_to_firm(expression->right);
1113 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1115 value = create_arithmetic_binop(expression, func);
1118 ir_mode *const mode = get_ir_mode(type);
1119 value = create_conv(dbgi, value, mode);
1120 set_value_for_expression(expression->left, value);
1125 static ir_node *create_add(const binary_expression_t *expression)
1127 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1128 ir_node *left = expression_to_firm(expression->left);
1129 ir_node *right = expression_to_firm(expression->right);
1130 type_t *type = expression->expression.datatype;
1132 expression_t *expr_left = expression->left;
1133 expression_t *expr_right = expression->right;
1134 type_t *type_left = skip_typeref(expr_left->base.datatype);
1135 type_t *type_right = skip_typeref(expr_right->base.datatype);
1137 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1138 ir_mode *const mode = get_ir_mode(type);
1139 return new_d_Add(dbgi, left, right, mode);
1142 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1143 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1145 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1146 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1150 static ir_node *create_sub(const binary_expression_t *expression)
1152 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1153 expression_t *const expr_left = expression->left;
1154 expression_t *const expr_right = expression->right;
1155 ir_node *const left = expression_to_firm(expr_left);
1156 ir_node *const right = expression_to_firm(expr_right);
1157 type_t *const type = expression->expression.datatype;
1158 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1159 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1161 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1162 ir_mode *const mode = get_ir_mode(type);
1163 return new_d_Sub(dbgi, left, right, mode);
1164 } else if (type_left->type == TYPE_POINTER
1165 && type_right->type == TYPE_POINTER) {
1166 const pointer_type_t *const ptr_type = &type_left->pointer;
1167 const unsigned elem_size = get_type_size(ptr_type->points_to);
1168 ir_mode *const mode = get_ir_mode(type);
1169 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1170 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1171 ir_node *const no_mem = new_NoMem();
1172 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1173 op_pin_state_floats);
1174 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1177 assert(type_left->type == TYPE_POINTER);
1178 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1181 static ir_node *create_shift(const binary_expression_t *expression)
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->expression.datatype;
1187 ir_mode *mode = get_ir_mode(type);
1189 /* firm always wants the shift count to be unsigned */
1190 right = create_conv(dbgi, right, mode_Iu);
1194 switch(expression->type) {
1195 case BINEXPR_SHIFTLEFT_ASSIGN:
1196 case BINEXPR_SHIFTLEFT:
1197 res = new_d_Shl(dbgi, left, right, mode);
1199 case BINEXPR_SHIFTRIGHT_ASSIGN:
1200 case BINEXPR_SHIFTRIGHT: {
1201 expression_t *expr_left = expression->left;
1202 type_t *type_left = skip_typeref(expr_left->base.datatype);
1204 if(is_type_signed(type_left)) {
1205 res = new_d_Shrs(dbgi, left, right, mode);
1207 res = new_d_Shr(dbgi, left, right, mode);
1212 panic("create shift op called for non-shift op");
1219 static ir_node *create_divmod(const binary_expression_t *expression)
1221 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1222 ir_node *left = expression_to_firm(expression->left);
1223 ir_node *right = expression_to_firm(expression->right);
1224 ir_node *pin = new_Pin(new_NoMem());
1225 /* be careful with the modes, because in arithmetic assign nodes only
1226 * the right operand has the mode of the arithmetic already */
1227 type_t *type = expression->right->base.datatype;
1228 ir_mode *mode = get_ir_mode(type);
1229 left = create_conv(dbgi, left, mode);
1233 switch (expression->type) {
1235 case BINEXPR_DIV_ASSIGN:
1236 if(mode_is_float(mode)) {
1237 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1238 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1240 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1241 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1246 case BINEXPR_MOD_ASSIGN:
1247 assert(!mode_is_float(mode));
1248 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1249 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1252 default: panic("unexpected binary expression type in create_divmod()");
1258 static ir_node *create_arithmetic_assign_divmod(
1259 const binary_expression_t *expression)
1261 ir_node * value = create_divmod(expression);
1262 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1263 type_t *const type = expression->expression.datatype;
1264 ir_mode *const mode = get_ir_mode(type);
1266 assert(type->type != TYPE_POINTER);
1268 value = create_conv(dbgi, value, mode);
1269 set_value_for_expression(expression->left, value);
1274 static ir_node *create_arithmetic_assign_shift(
1275 const binary_expression_t *expression)
1277 ir_node * value = create_shift(expression);
1278 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1279 type_t *const type = expression->expression.datatype;
1280 ir_mode *const mode = get_ir_mode(type);
1282 value = create_conv(dbgi, value, mode);
1283 set_value_for_expression(expression->left, value);
1288 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1290 binary_expression_type_t type = expression->type;
1293 case BINEXPR_NOTEQUAL:
1295 case BINEXPR_LESSEQUAL:
1296 case BINEXPR_GREATER:
1297 case BINEXPR_GREATEREQUAL: {
1298 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1299 ir_node *left = expression_to_firm(expression->left);
1300 ir_node *right = expression_to_firm(expression->right);
1301 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1302 long pnc = get_pnc(type);
1303 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1306 case BINEXPR_ASSIGN: {
1307 ir_node *right = expression_to_firm(expression->right);
1308 set_value_for_expression(expression->left, right);
1312 return create_add(expression);
1314 return create_sub(expression);
1316 return create_arithmetic_binop(expression, new_d_Mul);
1317 case BINEXPR_BITWISE_AND:
1318 return create_arithmetic_binop(expression, new_d_And);
1319 case BINEXPR_BITWISE_OR:
1320 return create_arithmetic_binop(expression, new_d_Or);
1321 case BINEXPR_BITWISE_XOR:
1322 return create_arithmetic_binop(expression, new_d_Eor);
1323 case BINEXPR_SHIFTLEFT:
1324 case BINEXPR_SHIFTRIGHT:
1325 return create_shift(expression);
1328 return create_divmod(expression);
1329 case BINEXPR_LOGICAL_AND:
1330 case BINEXPR_LOGICAL_OR:
1331 return create_lazy_op(expression);
1333 expression_to_firm(expression->left);
1334 return expression_to_firm(expression->right);
1335 case BINEXPR_ADD_ASSIGN:
1336 return create_arithmetic_assign_binop(expression, new_d_Add);
1337 case BINEXPR_SUB_ASSIGN:
1338 return create_arithmetic_assign_binop(expression, new_d_Sub);
1339 case BINEXPR_MUL_ASSIGN:
1340 return create_arithmetic_assign_binop(expression, new_d_Mul);
1341 case BINEXPR_DIV_ASSIGN:
1342 return create_arithmetic_assign_divmod(expression);
1343 case BINEXPR_BITWISE_AND_ASSIGN:
1344 return create_arithmetic_assign_binop(expression, new_d_And);
1345 case BINEXPR_BITWISE_OR_ASSIGN:
1346 return create_arithmetic_assign_binop(expression, new_d_Or);
1347 case BINEXPR_BITWISE_XOR_ASSIGN:
1348 return create_arithmetic_assign_binop(expression, new_d_Eor);
1349 case BINEXPR_SHIFTLEFT_ASSIGN:
1350 case BINEXPR_SHIFTRIGHT_ASSIGN:
1351 return create_arithmetic_assign_shift(expression);
1353 panic("TODO binexpr type");
1357 static ir_node *array_access_addr(const array_access_expression_t *expression)
1359 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1360 ir_node *base_addr = expression_to_firm(expression->array_ref);
1361 ir_node *offset = expression_to_firm(expression->index);
1362 offset = create_conv(dbgi, offset, mode_Iu);
1364 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1365 assert(is_type_pointer(ref_type));
1366 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1368 unsigned elem_size = get_type_size(pointer_type->points_to);
1369 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1370 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1372 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1377 static ir_node *array_access_to_firm(
1378 const array_access_expression_t *expression)
1380 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1381 ir_node *addr = array_access_addr(expression);
1382 type_t *type = revert_automatic_type_conversion(
1383 (const expression_t*) expression);
1384 type = skip_typeref(type);
1385 ir_type *irtype = get_ir_type(type);
1387 return deref_address(irtype, addr, dbgi);
1390 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1392 type_t *type = expression->type;
1394 type = expression->size_expression->base.datatype;
1395 assert(type != NULL);
1398 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1399 unsigned size = get_type_size(type);
1400 ir_node *size_node = new_Const_long(mode, size);
1405 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1407 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1409 ir_node *cur_block = get_cur_block();
1411 /* create the true block */
1412 ir_node *true_block = new_immBlock();
1414 ir_node *true_val = expression_to_firm(expression->true_expression);
1415 ir_node *true_jmp = new_Jmp();
1417 /* create the false block */
1418 ir_node *false_block = new_immBlock();
1420 ir_node *false_val = expression_to_firm(expression->false_expression);
1421 ir_node *false_jmp = new_Jmp();
1423 /* create the condition evaluation */
1424 set_cur_block(cur_block);
1425 create_condition_evaluation(expression->condition, true_block, false_block);
1426 mature_immBlock(true_block);
1427 mature_immBlock(false_block);
1429 /* create the common block */
1430 ir_node *common_block = new_immBlock();
1431 add_immBlock_pred(common_block, true_jmp);
1432 add_immBlock_pred(common_block, false_jmp);
1433 mature_immBlock(common_block);
1435 /* TODO improve static semantics, so either both or no values are NULL */
1436 if (true_val == NULL || false_val == NULL) return NULL;
1438 ir_node *in[2] = { true_val, false_val };
1439 ir_mode *mode = get_irn_mode(true_val);
1440 assert(get_irn_mode(false_val) == mode);
1441 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1446 static ir_node *select_addr(const select_expression_t *expression)
1448 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1450 ir_node *compound_addr = expression_to_firm(expression->compound);
1452 declaration_t *entry = expression->compound_entry;
1453 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1454 ir_entity *entity = entry->v.entity;
1456 assert(entity != NULL);
1458 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1463 static ir_node *select_to_firm(const select_expression_t *expression)
1465 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1466 ir_node *addr = select_addr(expression);
1467 type_t *type = revert_automatic_type_conversion(
1468 (const expression_t*) expression);
1469 type = skip_typeref(type);
1470 ir_type *irtype = get_ir_type(type);
1472 return deref_address(irtype, addr, dbgi);
1475 /* Values returned by __builtin_classify_type. */
1476 typedef enum gcc_type_class
1482 enumeral_type_class,
1485 reference_type_class,
1489 function_type_class,
1500 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1502 const type_t *const type = expr->type_expression->base.datatype;
1508 const atomic_type_t *const atomic_type = &type->atomic;
1509 switch (atomic_type->atype) {
1510 // should not be reached
1511 case ATOMIC_TYPE_INVALID:
1515 // gcc cannot do that
1516 case ATOMIC_TYPE_VOID:
1517 tc = void_type_class;
1520 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1521 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1522 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1523 case ATOMIC_TYPE_SHORT:
1524 case ATOMIC_TYPE_USHORT:
1525 case ATOMIC_TYPE_INT:
1526 case ATOMIC_TYPE_UINT:
1527 case ATOMIC_TYPE_LONG:
1528 case ATOMIC_TYPE_ULONG:
1529 case ATOMIC_TYPE_LONGLONG:
1530 case ATOMIC_TYPE_ULONGLONG:
1531 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1532 tc = integer_type_class;
1535 case ATOMIC_TYPE_FLOAT:
1536 case ATOMIC_TYPE_DOUBLE:
1537 case ATOMIC_TYPE_LONG_DOUBLE:
1538 tc = real_type_class;
1541 #ifdef PROVIDE_COMPLEX
1542 case ATOMIC_TYPE_FLOAT_COMPLEX:
1543 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1544 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1545 tc = complex_type_class;
1547 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1548 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1549 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1550 tc = complex_type_class;
1555 panic("Unimplemented case in classify_type_to_firm().");
1560 case TYPE_ARRAY: // gcc handles this as pointer
1561 case TYPE_FUNCTION: // gcc handles this as pointer
1562 case TYPE_POINTER: tc = pointer_type_class; break;
1563 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1564 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1566 // gcc handles this as integer
1567 case TYPE_ENUM: tc = integer_type_class; break;
1570 panic("Unimplemented case in classify_type_to_firm().");
1573 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1574 ir_mode *const mode = mode_Is;
1575 tarval *const tv = new_tarval_from_long(tc, mode);
1576 return new_d_Const(dbgi, mode, tv);
1579 static ir_node *function_name_to_firm(
1580 const string_literal_expression_t *const expr)
1582 if (current_function_name == NULL) {
1583 const source_position_t *const src_pos =
1584 &expr->expression.source_position;
1585 const char *const name = current_function_decl->symbol->string;
1586 current_function_name = string_to_firm(src_pos, "__func__", name);
1589 return current_function_name;
1592 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1594 statement_t *statement = expr->statement;
1596 assert(statement->type == STATEMENT_COMPOUND);
1597 return compound_statement_to_firm((compound_statement_t*) statement);
1600 static ir_node *dereference_addr(const unary_expression_t *const expression)
1602 assert(expression->type == UNEXPR_DEREFERENCE);
1603 return expression_to_firm(expression->value);
1606 static ir_node *expression_to_addr(const expression_t *expression)
1608 switch(expression->type) {
1609 case EXPR_REFERENCE:
1610 return reference_addr((const reference_expression_t*) expression);
1611 case EXPR_ARRAY_ACCESS:
1612 return array_access_addr((const array_access_expression_t*) expression);
1614 return select_addr((const select_expression_t*) expression);
1616 const unary_expression_t *const unary_expr =
1617 (const unary_expression_t*)expression;
1618 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1619 return dereference_addr(unary_expr);
1626 panic("trying to get address of non-lvalue");
1629 static ir_node *_expression_to_firm(const expression_t *expression)
1631 switch(expression->type) {
1633 return const_to_firm(&expression->conste);
1634 case EXPR_STRING_LITERAL:
1635 return string_literal_to_firm(&expression->string_literal);
1636 case EXPR_REFERENCE:
1637 return reference_expression_to_firm(&expression->reference);
1639 return call_expression_to_firm(&expression->call);
1641 return unary_expression_to_firm(&expression->unary);
1643 return binary_expression_to_firm(&expression->binary);
1644 case EXPR_ARRAY_ACCESS:
1645 return array_access_to_firm(&expression->array_access);
1647 return sizeof_to_firm(&expression->sizeofe);
1648 case EXPR_CONDITIONAL:
1649 return conditional_to_firm(&expression->conditional);
1651 return select_to_firm(&expression->select);
1652 case EXPR_CLASSIFY_TYPE:
1653 return classify_type_to_firm(&expression->classify_type);
1655 case EXPR_PRETTY_FUNCTION:
1656 return function_name_to_firm(&expression->string_literal);
1657 case EXPR_STATEMENT:
1658 return statement_expression_to_firm(&expression->statement);
1661 case EXPR_BUILTIN_SYMBOL:
1662 panic("unimplemented expression found");
1668 panic("invalid expression found");
1671 static ir_node *expression_to_firm(const expression_t *expression)
1673 ir_node *res = _expression_to_firm(expression);
1675 if(res != NULL && get_irn_mode(res) == mode_b) {
1676 ir_mode *mode = get_ir_mode(expression->base.datatype);
1677 res = create_conv(NULL, res, mode);
1683 static ir_node *expression_to_modeb(const expression_t *expression)
1685 ir_node *res = _expression_to_firm(expression);
1686 res = create_conv(NULL, res, mode_b);
1692 * create a short-circuit expression evaluation that tries to construct
1693 * efficient control flow structures for &&, || and ! expressions
1695 static void create_condition_evaluation(const expression_t *expression,
1696 ir_node *true_block,
1697 ir_node *false_block)
1699 switch(expression->type) {
1701 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1702 if(unary_expression->type == UNEXPR_NOT) {
1703 create_condition_evaluation(unary_expression->value, false_block,
1710 binary_expression_t *binary_expression
1711 = (binary_expression_t*) expression;
1712 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1713 ir_node *cur_block = get_cur_block();
1714 ir_node *extra_block = new_immBlock();
1715 set_cur_block(cur_block);
1716 create_condition_evaluation(binary_expression->left, extra_block,
1718 mature_immBlock(extra_block);
1719 set_cur_block(extra_block);
1720 create_condition_evaluation(binary_expression->right, true_block,
1724 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1725 ir_node *cur_block = get_cur_block();
1726 ir_node *extra_block = new_immBlock();
1727 set_cur_block(cur_block);
1728 create_condition_evaluation(binary_expression->left, true_block,
1730 mature_immBlock(extra_block);
1731 set_cur_block(extra_block);
1732 create_condition_evaluation(binary_expression->right, true_block,
1742 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1743 ir_node *condition = expression_to_modeb(expression);
1744 ir_node *cond = new_d_Cond(dbgi, condition);
1745 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1746 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1748 add_immBlock_pred(true_block, true_proj);
1749 add_immBlock_pred(false_block, false_proj);
1751 set_cur_block(NULL);
1755 static void return_statement_to_firm(return_statement_t *statement)
1757 if(get_cur_block() == NULL)
1761 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1763 ir_node *value = NULL;
1764 if(statement->return_value != NULL) {
1765 value = expression_to_firm(statement->return_value);
1770 if(get_method_n_ress(func_irtype) > 0) {
1772 ir_type *res_type = get_method_res_type(func_irtype, 0);
1773 ir_mode *mode = get_type_mode(res_type);
1774 value = new_Unknown(mode);
1783 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1784 ir_node *store = get_store();
1785 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1787 ir_node *end_block = get_irg_end_block(current_ir_graph);
1788 add_immBlock_pred(end_block, ret);
1790 set_cur_block(NULL);
1793 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1795 if(get_cur_block() == NULL)
1798 return expression_to_firm(statement->expression);
1801 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1803 ir_node *result = NULL;
1804 statement_t *statement = compound->statements;
1805 for( ; statement != NULL; statement = statement->base.next) {
1806 //context2firm(&statement->context);
1808 if(statement->base.next == NULL
1809 && statement->type == STATEMENT_EXPRESSION) {
1810 result = expression_statement_to_firm(
1811 (expression_statement_t*) statement);
1814 statement_to_firm(statement);
1820 static void if_statement_to_firm(if_statement_t *statement)
1822 ir_node *cur_block = get_cur_block();
1824 ir_node *fallthrough_block = new_immBlock();
1826 /* the true (blocks) */
1827 ir_node *true_block;
1828 if (statement->true_statement != NULL) {
1829 true_block = new_immBlock();
1830 statement_to_firm(statement->true_statement);
1831 if(get_cur_block() != NULL) {
1832 ir_node *jmp = new_Jmp();
1833 add_immBlock_pred(fallthrough_block, jmp);
1836 true_block = fallthrough_block;
1839 /* the false (blocks) */
1840 ir_node *false_block;
1841 if(statement->false_statement != NULL) {
1842 false_block = new_immBlock();
1844 statement_to_firm(statement->false_statement);
1845 if(get_cur_block() != NULL) {
1846 ir_node *jmp = new_Jmp();
1847 add_immBlock_pred(fallthrough_block, jmp);
1850 false_block = fallthrough_block;
1853 /* create the condition */
1854 if(cur_block != NULL) {
1855 set_cur_block(cur_block);
1856 create_condition_evaluation(statement->condition, true_block,
1860 mature_immBlock(true_block);
1861 if(false_block != fallthrough_block) {
1862 mature_immBlock(false_block);
1864 mature_immBlock(fallthrough_block);
1866 set_cur_block(fallthrough_block);
1869 static void while_statement_to_firm(while_statement_t *statement)
1871 ir_node *jmp = NULL;
1872 if(get_cur_block() != NULL) {
1876 /* create the header block */
1877 ir_node *header_block = new_immBlock();
1879 add_immBlock_pred(header_block, jmp);
1882 /* the false block */
1883 ir_node *false_block = new_immBlock();
1886 ir_node *body_block;
1887 if (statement->body != NULL) {
1888 ir_node *old_continue_label = continue_label;
1889 ir_node *old_break_label = break_label;
1890 continue_label = header_block;
1891 break_label = false_block;
1893 body_block = new_immBlock();
1894 statement_to_firm(statement->body);
1896 assert(continue_label == header_block);
1897 assert(break_label == false_block);
1898 continue_label = old_continue_label;
1899 break_label = old_break_label;
1901 if(get_cur_block() != NULL) {
1902 ir_node *jmp = new_Jmp();
1903 add_immBlock_pred(header_block, jmp);
1906 body_block = header_block;
1909 /* create the condition */
1910 set_cur_block(header_block);
1912 create_condition_evaluation(statement->condition, body_block, false_block);
1913 mature_immBlock(body_block);
1914 mature_immBlock(false_block);
1915 mature_immBlock(header_block);
1917 set_cur_block(false_block);
1920 static void do_while_statement_to_firm(do_while_statement_t *statement)
1922 ir_node *jmp = NULL;
1923 if(get_cur_block() != NULL) {
1927 /* create the header block */
1928 ir_node *header_block = new_immBlock();
1930 /* the false block */
1931 ir_node *false_block = new_immBlock();
1934 ir_node *body_block = new_immBlock();
1936 add_immBlock_pred(body_block, jmp);
1939 if (statement->body != NULL) {
1940 ir_node *old_continue_label = continue_label;
1941 ir_node *old_break_label = break_label;
1942 continue_label = header_block;
1943 break_label = false_block;
1945 statement_to_firm(statement->body);
1947 assert(continue_label == header_block);
1948 assert(break_label == false_block);
1949 continue_label = old_continue_label;
1950 break_label = old_break_label;
1952 if (get_cur_block() == NULL) {
1953 mature_immBlock(header_block);
1954 mature_immBlock(body_block);
1955 mature_immBlock(false_block);
1960 ir_node *body_jmp = new_Jmp();
1961 add_immBlock_pred(header_block, body_jmp);
1962 mature_immBlock(header_block);
1964 /* create the condition */
1965 set_cur_block(header_block);
1967 create_condition_evaluation(statement->condition, body_block, false_block);
1968 mature_immBlock(body_block);
1969 mature_immBlock(false_block);
1970 mature_immBlock(header_block);
1972 set_cur_block(false_block);
1975 static void for_statement_to_firm(for_statement_t *statement)
1977 ir_node *jmp = NULL;
1978 if (get_cur_block() != NULL) {
1979 if(statement->initialisation != NULL) {
1980 expression_to_firm(statement->initialisation);
1985 /* create the step block */
1986 ir_node *const step_block = new_immBlock();
1987 if (statement->step != NULL) {
1988 expression_to_firm(statement->step);
1990 ir_node *const step_jmp = new_Jmp();
1992 /* create the header block */
1993 ir_node *const header_block = new_immBlock();
1995 add_immBlock_pred(header_block, jmp);
1997 add_immBlock_pred(header_block, step_jmp);
1999 /* the false block */
2000 ir_node *const false_block = new_immBlock();
2003 ir_node * body_block;
2004 if (statement->body != NULL) {
2005 ir_node *const old_continue_label = continue_label;
2006 ir_node *const old_break_label = break_label;
2007 continue_label = step_block;
2008 break_label = false_block;
2010 body_block = new_immBlock();
2011 statement_to_firm(statement->body);
2013 assert(continue_label == step_block);
2014 assert(break_label == false_block);
2015 continue_label = old_continue_label;
2016 break_label = old_break_label;
2018 if (get_cur_block() != NULL) {
2019 ir_node *const jmp = new_Jmp();
2020 add_immBlock_pred(step_block, jmp);
2023 body_block = step_block;
2026 /* create the condition */
2027 set_cur_block(header_block);
2028 if (statement->condition != NULL) {
2029 create_condition_evaluation(statement->condition, body_block,
2032 keep_alive(header_block);
2033 ir_node *jmp = new_Jmp();
2034 add_immBlock_pred(body_block, jmp);
2037 mature_immBlock(body_block);
2038 mature_immBlock(false_block);
2039 mature_immBlock(step_block);
2040 mature_immBlock(header_block);
2041 mature_immBlock(false_block);
2043 set_cur_block(false_block);
2046 static void create_declaration_entity(declaration_t *declaration,
2047 declaration_type_t declaration_type,
2048 ir_type *parent_type)
2050 ident *id = new_id_from_str(declaration->symbol->string);
2051 ir_type *irtype = get_ir_type(declaration->type);
2052 ir_entity *entity = new_entity(parent_type, id, irtype);
2053 set_entity_ld_ident(entity, id);
2055 declaration->declaration_type = declaration_type;
2056 declaration->v.entity = entity;
2057 set_entity_variability(entity, variability_uninitialized);
2058 /* TODO: visibility? */
2061 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2063 enum compound_graph_entry_type_t {
2064 COMPOUND_GRAPH_ENTRY_ARRAY,
2065 COMPOUND_GRAPH_ENTRY_COMPOUND
2068 struct compound_graph_path_entry_t {
2074 compound_graph_path_entry_t *prev;
2077 static void create_initializer_object(initializer_t *initializer, type_t *type,
2078 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2080 static compound_graph_path *create_compound_path(ir_type *type,
2081 compound_graph_path_entry_t *entry, int len)
2083 compound_graph_path *path = new_compound_graph_path(type, len);
2086 for( ; entry != NULL; entry = entry->prev, --i) {
2088 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2089 set_compound_graph_path_node(path, i, entry->v.entity);
2091 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2092 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2100 static void create_initializer_value(initializer_value_t *initializer,
2102 compound_graph_path_entry_t *entry,
2105 ir_node *node = expression_to_firm(initializer->value);
2106 ir_type *type = get_entity_type(entity);
2107 compound_graph_path *path = create_compound_path(type, entry, len);
2108 add_compound_ent_value_w_path(entity, node, path);
2111 static void create_initializer_compound(initializer_list_t *initializer,
2112 compound_type_t *type,
2114 compound_graph_path_entry_t *last_entry,
2117 declaration_t *compound_declaration = type->declaration;
2119 declaration_t *compound_entry = compound_declaration->context.declarations;
2121 compound_graph_path_entry_t entry;
2122 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2123 entry.prev = last_entry;
2127 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2128 if(compound_entry->symbol == NULL)
2130 if(compound_entry->namespc != NAMESPACE_NORMAL)
2133 if(i >= initializer->len)
2136 entry.v.entity = compound_entry->v.entity;
2138 initializer_t *sub_initializer = initializer->initializers[i];
2140 assert(compound_entry != NULL);
2141 assert(compound_entry->declaration_type
2142 == DECLARATION_TYPE_COMPOUND_MEMBER);
2144 if(sub_initializer->type == INITIALIZER_VALUE) {
2145 create_initializer_value(&sub_initializer->value,
2146 entity, &entry, len);
2148 type_t *type = skip_typeref(compound_entry->type);
2149 create_initializer_object(sub_initializer, type, entity, &entry,
2157 static void create_initializer_array(initializer_list_t *initializer,
2158 array_type_t *type, ir_entity *entity,
2159 compound_graph_path_entry_t *last_entry,
2162 type_t *element_type = type->element_type;
2163 element_type = skip_typeref(element_type);
2165 compound_graph_path_entry_t entry;
2166 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2167 entry.prev = last_entry;
2171 for(i = 0; i < initializer->len; ++i) {
2172 entry.v.array_index = i;
2174 initializer_t *sub_initializer = initializer->initializers[i];
2176 if(sub_initializer->type == INITIALIZER_VALUE) {
2177 create_initializer_value(&sub_initializer->value,
2178 entity, &entry, len);
2180 create_initializer_object(sub_initializer, element_type, entity,
2186 /* TODO: initializer rest... */
2187 if(type->size_expression != NULL) {
2188 size_t array_len = fold_constant(type->size_expression);
2189 for( ; i < array_len; ++i) {
2196 static void create_initializer_string(initializer_string_t *initializer,
2197 array_type_t *type, ir_entity *entity,
2198 compound_graph_path_entry_t *last_entry,
2201 type_t *element_type = type->element_type;
2202 element_type = skip_typeref(element_type);
2204 compound_graph_path_entry_t entry;
2205 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2206 entry.prev = last_entry;
2209 ir_type *irtype = get_entity_type(entity);
2210 size_t arr_len = get_array_type_size(type);
2211 const char *p = initializer->string;
2213 for(i = 0; i < arr_len; ++i, ++p) {
2214 entry.v.array_index = i;
2216 ir_node *node = new_Const_long(mode_Bs, *p);
2217 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2218 add_compound_ent_value_w_path(entity, node, path);
2225 static void create_initializer_object(initializer_t *initializer, type_t *type,
2226 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2228 if(type->type == TYPE_ARRAY) {
2229 array_type_t *array_type = &type->array;
2231 if(initializer->type == INITIALIZER_STRING) {
2232 initializer_string_t *string = &initializer->string;
2233 create_initializer_string(string, array_type, entity, entry, len);
2235 assert(initializer->type == INITIALIZER_LIST);
2236 initializer_list_t *list = &initializer->list;
2237 create_initializer_array(list, array_type, entity, entry, len);
2240 assert(initializer->type == INITIALIZER_LIST);
2241 initializer_list_t *list = &initializer->list;
2243 assert(type->type == TYPE_COMPOUND_STRUCT
2244 || type->type == TYPE_COMPOUND_UNION);
2245 compound_type_t *compound_type = &type->compound;
2246 create_initializer_compound(list, compound_type, entity, entry, len);
2250 static void create_initializer_local_variable_entity(declaration_t *declaration)
2252 initializer_t *initializer = declaration->init.initializer;
2253 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2254 ir_entity *entity = declaration->v.entity;
2255 ir_node *memory = get_store();
2256 ir_node *nomem = new_NoMem();
2257 ir_node *frame = get_irg_frame(current_ir_graph);
2258 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2260 if(is_atomic_entity(entity)) {
2261 assert(initializer->type == INITIALIZER_VALUE);
2262 initializer_value_t *initializer_value = &initializer->value;
2264 ir_node *value = expression_to_firm(initializer_value->value);
2265 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2266 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2267 set_store(store_mem);
2271 /* create a "template" entity which is copied to the entity on the stack */
2272 ident *id = unique_ident("initializer");
2273 ir_type *irtype = get_ir_type(declaration->type);
2274 ir_type *global_type = get_glob_type();
2275 ir_entity *init_entity = new_entity(global_type, id, irtype);
2276 set_entity_ld_ident(init_entity, id);
2278 set_entity_variability(init_entity, variability_initialized);
2279 set_entity_visibility(init_entity, visibility_local);
2281 ir_graph *old_current_ir_graph = current_ir_graph;
2282 current_ir_graph = get_const_code_irg();
2284 type_t *type = skip_typeref(declaration->type);
2285 create_initializer_object(initializer, type, init_entity, NULL, 0);
2287 assert(current_ir_graph == get_const_code_irg());
2288 current_ir_graph = old_current_ir_graph;
2290 ir_node *src_addr = create_symconst(dbgi, init_entity);
2291 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2293 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2294 set_store(copyb_mem);
2297 static void create_initializer(declaration_t *declaration)
2299 initializer_t *initializer = declaration->init.initializer;
2300 if(initializer == NULL)
2303 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2304 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2305 create_initializer_local_variable_entity(declaration);
2309 if(initializer->type == INITIALIZER_VALUE) {
2310 initializer_value_t *initializer_value = &initializer->value;
2312 ir_node *value = expression_to_firm(initializer_value->value);
2314 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2315 set_value(declaration->v.value_number, value);
2317 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2319 ir_entity *entity = declaration->v.entity;
2321 set_entity_variability(entity, variability_initialized);
2322 set_atomic_ent_value(entity, value);
2325 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2326 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2327 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2329 ir_entity *entity = declaration->v.entity;
2330 set_entity_variability(entity, variability_initialized);
2332 type_t *type = skip_typeref(declaration->type);
2333 create_initializer_object(initializer, type, entity, NULL, 0);
2337 static void create_local_variable(declaration_t *declaration)
2339 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2341 bool needs_entity = declaration->address_taken;
2342 type_t *type = skip_typeref(declaration->type);
2344 if(type->type == TYPE_ARRAY
2345 || type->type == TYPE_COMPOUND_STRUCT
2346 || type->type == TYPE_COMPOUND_UNION) {
2347 needs_entity = true;
2351 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2352 create_declaration_entity(declaration,
2353 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2356 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2357 declaration->v.value_number = next_value_number_function;
2358 ++next_value_number_function;
2361 create_initializer(declaration);
2364 static void create_local_static_variable(declaration_t *declaration)
2366 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2368 type_t *type = skip_typeref(declaration->type);
2369 ir_type *global_type = get_glob_type();
2370 ident *id = unique_ident(declaration->symbol->string);
2371 ir_type *irtype = get_ir_type(type);
2372 ir_entity *entity = new_entity(global_type, id, irtype);
2373 set_entity_ld_ident(entity, id);
2375 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2376 declaration->v.entity = entity;
2377 set_entity_variability(entity, variability_uninitialized);
2378 set_entity_visibility(entity, visibility_local);
2380 ir_graph *old_current_ir_graph = current_ir_graph;
2381 current_ir_graph = get_const_code_irg();
2383 create_initializer(declaration);
2385 assert(current_ir_graph == get_const_code_irg());
2386 current_ir_graph = old_current_ir_graph;
2389 static void declaration_statement_to_firm(declaration_statement_t *statement)
2391 declaration_t *declaration = statement->declarations_begin;
2392 declaration_t *end = statement->declarations_end->next;
2393 for( ; declaration != end; declaration = declaration->next) {
2394 type_t *type = declaration->type;
2396 switch(declaration->storage_class) {
2397 case STORAGE_CLASS_TYPEDEF:
2399 case STORAGE_CLASS_STATIC:
2400 create_local_static_variable(declaration);
2402 case STORAGE_CLASS_ENUM_ENTRY:
2403 panic("enum entry declaration in local block found");
2404 case STORAGE_CLASS_EXTERN:
2405 panic("extern declaration in local block found");
2406 case STORAGE_CLASS_NONE:
2407 case STORAGE_CLASS_AUTO:
2408 case STORAGE_CLASS_REGISTER:
2409 if(type->type == TYPE_FUNCTION) {
2410 panic("nested functions not supported yet");
2412 create_local_variable(declaration);
2416 panic("invalid storage class found");
2420 static void create_jump_statement(const statement_t *statement,
2421 ir_node *target_block)
2423 if(get_cur_block() == NULL)
2426 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2427 ir_node *jump = new_d_Jmp(dbgi);
2428 add_immBlock_pred(target_block, jump);
2430 set_cur_block(NULL);
2433 static void switch_statement_to_firm(const switch_statement_t *statement)
2435 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2437 ir_node *expression = expression_to_firm(statement->expression);
2438 ir_node *cond = new_d_Cond(dbgi, expression);
2439 ir_node *break_block = new_immBlock();
2441 set_cur_block(NULL);
2443 ir_node *const old_switch_cond = current_switch_cond;
2444 ir_node *const old_break_label = break_label;
2445 const bool old_saw_default_label = saw_default_label;
2446 current_switch_cond = cond;
2447 break_label = break_block;
2449 statement_to_firm(statement->body);
2451 if(get_cur_block() != NULL) {
2452 ir_node *jmp = new_Jmp();
2453 add_immBlock_pred(break_block, jmp);
2456 if (!saw_default_label) {
2457 set_cur_block(get_nodes_block(cond));
2458 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2459 MAGIC_DEFAULT_PN_NUMBER);
2460 add_immBlock_pred(break_block, proj);
2463 assert(current_switch_cond == cond);
2464 assert(break_label == break_block);
2465 current_switch_cond = old_switch_cond;
2466 break_label = old_break_label;
2467 saw_default_label = old_saw_default_label;
2469 mature_immBlock(break_block);
2470 set_cur_block(break_block);
2473 static long fold_constant(const expression_t *expression)
2475 ir_graph *old_current_ir_graph = current_ir_graph;
2476 current_ir_graph = get_const_code_irg();
2478 ir_node *cnst = expression_to_firm(expression);
2479 if(!is_Const(cnst)) {
2480 panic("couldn't fold constantl");
2482 tarval *tv = get_Const_tarval(cnst);
2483 if(!tarval_is_long(tv)) {
2484 panic("folded constant not an integer");
2487 long res = get_tarval_long(tv);
2489 current_ir_graph = old_current_ir_graph;
2493 static void case_label_to_firm(const case_label_statement_t *statement)
2495 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2497 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2499 /* let's create a node and hope firm constant folding creates a Const
2502 set_cur_block(get_nodes_block(current_switch_cond));
2503 if(statement->expression) {
2504 long pn = fold_constant(statement->expression);
2505 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2506 /* oops someone detected our cheating... */
2507 panic("magic default pn used");
2509 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2511 saw_default_label = true;
2512 proj = new_d_defaultProj(dbgi, current_switch_cond,
2513 MAGIC_DEFAULT_PN_NUMBER);
2516 ir_node *block = new_immBlock();
2517 if (fallthrough != NULL) {
2518 add_immBlock_pred(block, fallthrough);
2520 add_immBlock_pred(block, proj);
2521 mature_immBlock(block);
2523 statement_to_firm(statement->label_statement);
2526 static ir_node *get_label_block(declaration_t *label)
2528 assert(label->namespc == NAMESPACE_LABEL);
2530 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2531 return label->v.block;
2533 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2535 ir_node *old_cur_block = get_cur_block();
2536 ir_node *block = new_immBlock();
2537 set_cur_block(old_cur_block);
2539 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2540 label->v.block = block;
2542 ARR_APP1(ir_node *, imature_blocks, block);
2547 static void label_to_firm(const label_statement_t *statement)
2549 ir_node *block = get_label_block(statement->label);
2551 if(get_cur_block() != NULL) {
2552 ir_node *jmp = new_Jmp();
2553 add_immBlock_pred(block, jmp);
2556 set_cur_block(block);
2559 statement_to_firm(statement->label_statement);
2562 static void goto_to_firm(const goto_statement_t *statement)
2564 if(get_cur_block() == NULL)
2567 ir_node *block = get_label_block(statement->label);
2568 ir_node *jmp = new_Jmp();
2569 add_immBlock_pred(block, jmp);
2571 set_cur_block(NULL);
2574 static void statement_to_firm(statement_t *statement)
2576 switch(statement->type) {
2577 case STATEMENT_COMPOUND:
2578 compound_statement_to_firm((compound_statement_t*) statement);
2580 case STATEMENT_RETURN:
2581 return_statement_to_firm((return_statement_t*) statement);
2583 case STATEMENT_EXPRESSION:
2584 expression_statement_to_firm((expression_statement_t*) statement);
2587 if_statement_to_firm((if_statement_t*) statement);
2589 case STATEMENT_WHILE:
2590 while_statement_to_firm((while_statement_t*) statement);
2592 case STATEMENT_DO_WHILE:
2593 do_while_statement_to_firm((do_while_statement_t*) statement);
2595 case STATEMENT_DECLARATION:
2596 declaration_statement_to_firm((declaration_statement_t*) statement);
2598 case STATEMENT_BREAK:
2599 create_jump_statement(statement, break_label);
2601 case STATEMENT_CONTINUE:
2602 create_jump_statement(statement, continue_label);
2604 case STATEMENT_SWITCH:
2605 switch_statement_to_firm((switch_statement_t*) statement);
2607 case STATEMENT_CASE_LABEL:
2608 case_label_to_firm((case_label_statement_t*) statement);
2611 for_statement_to_firm((for_statement_t*) statement);
2613 case STATEMENT_LABEL:
2614 label_to_firm((label_statement_t*) statement);
2616 case STATEMENT_GOTO:
2617 goto_to_firm((goto_statement_t*) statement);
2622 panic("Statement not implemented\n");
2625 static int count_local_declarations(const declaration_t * decl,
2626 const declaration_t *const end)
2629 for (; decl != end; decl = decl->next) {
2630 const type_t *type = skip_typeref(decl->type);
2631 switch (type->type) {
2635 if (!decl->address_taken)
2645 static int count_decls_in_stmts(const statement_t *stmt)
2648 for (; stmt != NULL; stmt = stmt->base.next) {
2649 switch (stmt->type) {
2650 case STATEMENT_DECLARATION: {
2651 const declaration_statement_t *const decl_stmt =
2652 (const declaration_statement_t*)stmt;
2653 count += count_local_declarations(decl_stmt->declarations_begin,
2654 decl_stmt->declarations_end->next);
2658 case STATEMENT_COMPOUND: {
2659 const compound_statement_t *const comp =
2660 (const compound_statement_t*)stmt;
2661 count += count_decls_in_stmts(comp->statements);
2665 case STATEMENT_IF: {
2666 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2667 count += count_decls_in_stmts(if_stmt->true_statement);
2668 count += count_decls_in_stmts(if_stmt->false_statement);
2672 case STATEMENT_SWITCH: {
2673 const switch_statement_t *const switch_stmt =
2674 (const switch_statement_t*)stmt;
2675 count += count_decls_in_stmts(switch_stmt->body);
2679 case STATEMENT_LABEL: {
2680 const label_statement_t *const label_stmt =
2681 (const label_statement_t*)stmt;
2682 count += count_decls_in_stmts(label_stmt->label_statement);
2686 case STATEMENT_WHILE: {
2687 const while_statement_t *const while_stmt =
2688 (const while_statement_t*)stmt;
2689 count += count_decls_in_stmts(while_stmt->body);
2693 case STATEMENT_DO_WHILE: {
2694 const do_while_statement_t *const do_while_stmt =
2695 (const do_while_statement_t*)stmt;
2696 count += count_decls_in_stmts(do_while_stmt->body);
2700 case STATEMENT_FOR: {
2701 const for_statement_t *const for_stmt =
2702 (const for_statement_t*)stmt;
2703 /* TODO initialisation */
2704 count += count_decls_in_stmts(for_stmt->body);
2708 case STATEMENT_BREAK:
2709 case STATEMENT_CASE_LABEL:
2710 case STATEMENT_CONTINUE:
2711 case STATEMENT_EXPRESSION:
2712 case STATEMENT_GOTO:
2713 case STATEMENT_INVALID:
2714 case STATEMENT_RETURN:
2721 static int get_function_n_local_vars(declaration_t *declaration)
2725 /* count parameters */
2726 count += count_local_declarations(declaration->context.declarations, NULL);
2728 /* count local variables declared in body */
2729 count += count_decls_in_stmts(declaration->init.statement);
2731 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2732 * for expression statements... */
2738 static void initialize_function_parameters(declaration_t *declaration)
2740 ir_graph *irg = current_ir_graph;
2741 ir_node *args = get_irg_args(irg);
2742 ir_node *start_block = get_irg_start_block(irg);
2743 ir_type *function_irtype = get_ir_type(declaration->type);
2746 declaration_t *parameter = declaration->context.declarations;
2747 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2748 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2749 type_t *type = skip_typeref(parameter->type);
2751 bool needs_entity = parameter->address_taken;
2752 if(type->type == TYPE_COMPOUND_STRUCT
2753 || type->type == TYPE_COMPOUND_UNION) {
2754 needs_entity = true;
2758 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2759 ident *id = new_id_from_str(parameter->symbol->string);
2760 set_entity_ident(entity, id);
2762 parameter->declaration_type
2763 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2764 parameter->v.entity = entity;
2768 ir_mode *mode = get_ir_mode(parameter->type);
2770 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2772 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2773 parameter->v.value_number = next_value_number_function;
2774 ++next_value_number_function;
2776 set_value(parameter->v.value_number, proj);
2780 static void create_function(declaration_t *declaration)
2782 ir_entity *entity = get_function_entity(declaration);
2784 if(declaration->init.statement == NULL)
2787 current_function_decl = declaration;
2788 current_function_name = NULL;
2790 assert(imature_blocks == NULL);
2791 imature_blocks = NEW_ARR_F(ir_node*, 0);
2793 int n_local_vars = get_function_n_local_vars(declaration);
2794 ir_graph *irg = new_ir_graph(entity, n_local_vars);
2795 ir_node *first_block = get_cur_block();
2797 next_value_number_function = 0;
2798 initialize_function_parameters(declaration);
2800 statement_to_firm(declaration->init.statement);
2802 ir_node *end_block = get_irg_end_block(irg);
2804 /* do we have a return statement yet? */
2805 if(get_cur_block() != NULL) {
2806 assert(declaration->type->type == TYPE_FUNCTION);
2807 const function_type_t* const func_type = &declaration->type->function;
2810 if (func_type->result_type == type_void) {
2811 ret = new_Return(get_store(), 0, NULL);
2813 ir_mode *const mode = get_ir_mode(func_type->result_type);
2815 // ยง5.1.2.2.3 main implicitly returns 0
2816 if (strcmp(declaration->symbol->string, "main") == 0) {
2817 in[0] = new_Const(mode, get_mode_null(mode));
2819 in[0] = new_Unknown(mode);
2821 ret = new_Return(get_store(), 1, in);
2823 add_immBlock_pred(end_block, ret);
2826 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2827 mature_immBlock(imature_blocks[i]);
2829 DEL_ARR_F(imature_blocks);
2830 imature_blocks = NULL;
2832 mature_immBlock(first_block);
2833 mature_immBlock(end_block);
2835 irg_finalize_cons(irg);
2837 /* finalize the frame type */
2838 ir_type *frame_type = get_irg_frame_type(irg);
2839 int n = get_compound_n_members(frame_type);
2842 for(int i = 0; i < n; ++i) {
2843 ir_entity *entity = get_compound_member(frame_type, i);
2844 ir_type *entity_type = get_entity_type(entity);
2846 int align = get_type_alignment_bytes(entity_type);
2847 if(align > align_all)
2851 misalign = offset % align;
2853 offset += align - misalign;
2857 set_entity_offset(entity, offset);
2858 offset += get_type_size_bytes(entity_type);
2860 set_type_size_bytes(frame_type, offset);
2861 set_type_alignment_bytes(frame_type, align_all);
2862 set_type_state(frame_type, layout_fixed);
2867 static void create_global_variable(declaration_t *declaration)
2869 ir_type *global_type = get_glob_type();
2870 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2873 ir_entity *entity = declaration->v.entity;
2874 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2875 set_entity_visibility(entity, visibility_local);
2876 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2877 set_entity_visibility(entity, visibility_external_allocated);
2879 set_entity_visibility(entity, visibility_external_visible);
2881 current_ir_graph = get_const_code_irg();
2882 create_initializer(declaration);
2885 static void context_to_firm(context_t *context)
2887 /* first pass: create declarations */
2888 declaration_t *declaration = context->declarations;
2889 for( ; declaration != NULL; declaration = declaration->next) {
2890 if(declaration->namespc != NAMESPACE_NORMAL)
2892 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2893 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2895 if(declaration->symbol == NULL)
2898 type_t *type = declaration->type;
2899 if(type->type == TYPE_FUNCTION) {
2900 get_function_entity(declaration);
2902 create_global_variable(declaration);
2906 /* second pass: create code */
2907 declaration = context->declarations;
2908 for( ; declaration != NULL; declaration = declaration->next) {
2909 if(declaration->namespc != NAMESPACE_NORMAL)
2911 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2912 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2914 if(declaration->symbol == NULL)
2917 type_t *type = declaration->type;
2918 if(type->type != TYPE_FUNCTION)
2921 create_function(declaration);
2925 void translation_unit_to_firm(translation_unit_t *unit)
2927 /* just to be sure */
2928 continue_label = NULL;
2930 current_switch_cond = NULL;
2932 context_to_firm(& unit->context);