9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
15 #include "adt/array.h"
20 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
22 static ir_type *ir_type_const_char;
23 static ir_type *ir_type_void;
24 static ir_type *ir_type_int;
25 static ir_type *ir_type_void_ptr;
27 static type_t *type_const_char;
28 static type_t *type_void;
29 static type_t *type_int;
31 static symbol_t *symbol_alloca;
33 static int next_value_number_function;
34 static ir_node *continue_label;
35 static ir_node *break_label;
36 static ir_node *current_switch_cond;
37 static bool saw_default_label;
38 static ir_node **imature_blocks;
40 static const declaration_t *current_function_decl;
41 static ir_node *current_function_name;
43 typedef enum declaration_type_t {
44 DECLARATION_TYPE_UNKNOWN,
45 DECLARATION_TYPE_FUNCTION,
46 DECLARATION_TYPE_GLOBAL_VARIABLE,
47 DECLARATION_TYPE_LOCAL_VARIABLE,
48 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
49 DECLARATION_TYPE_COMPOUND_MEMBER,
50 DECLARATION_TYPE_LABEL_BLOCK,
53 static ir_type *get_ir_type(type_t *type);
55 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
59 const declaration_t *declaration = & value_numbers[pos]->declaration;
61 print_warning_prefix(declaration->source_position);
62 fprintf(stderr, "variable '%s' might be used uninitialized\n",
63 declaration->symbol->string);
65 fprintf(stderr, "Some variable might be used uninitialized\n");
66 return new_r_Unknown(irg, mode);
69 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
71 const source_position_t *pos = (const source_position_t*) dbg;
74 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
78 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
80 const source_position_t *pos = (const source_position_t*) dbg;
85 return pos->input_name;
88 void init_ast2firm(void)
90 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
91 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
92 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
94 ir_type_int = get_ir_type(type_int);
95 ir_type_const_char = get_ir_type(type_const_char);
96 ir_type_void = get_ir_type(type_int); /* we don't have a real void
98 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
99 ir_type_void, mode_P_data);
101 type_void->firm_type = ir_type_void;
103 symbol_alloca = symbol_table_insert("__builtin_alloca");
106 void exit_ast2firm(void)
110 static unsigned unique_id = 0;
112 static ident *unique_ident(const char *tag)
116 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
118 return new_id_from_str(buf);
121 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
123 switch(atomic_type->atype) {
124 case ATOMIC_TYPE_SCHAR:
125 case ATOMIC_TYPE_CHAR:
127 case ATOMIC_TYPE_UCHAR:
129 case ATOMIC_TYPE_SHORT:
131 case ATOMIC_TYPE_USHORT:
133 case ATOMIC_TYPE_LONG:
134 case ATOMIC_TYPE_INT:
136 case ATOMIC_TYPE_ULONG:
137 case ATOMIC_TYPE_UINT:
139 case ATOMIC_TYPE_LONGLONG:
141 case ATOMIC_TYPE_ULONGLONG:
143 case ATOMIC_TYPE_FLOAT:
145 case ATOMIC_TYPE_DOUBLE:
147 case ATOMIC_TYPE_LONG_DOUBLE:
149 case ATOMIC_TYPE_BOOL:
151 #ifdef PROVIDE_COMPLEX
152 case ATOMIC_TYPE_FLOAT_COMPLEX:
153 case ATOMIC_TYPE_DOUBLE_COMPLEX:
154 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
155 panic("complex lowering not implemented yet");
157 case ATOMIC_TYPE_FLOAT_IMAGINARY:
158 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
159 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
160 panic("imaginary lowering not implemented yet");
163 case ATOMIC_TYPE_VOID:
164 /* firm has no real void... */
166 case ATOMIC_TYPE_INVALID:
169 panic("Encountered unknown atomic type");
173 static unsigned get_type_size(type_t *type);
175 static unsigned get_atomic_type_size(const atomic_type_t *type)
177 switch(type->atype) {
178 case ATOMIC_TYPE_CHAR:
179 case ATOMIC_TYPE_SCHAR:
180 case ATOMIC_TYPE_UCHAR:
183 case ATOMIC_TYPE_SHORT:
184 case ATOMIC_TYPE_USHORT:
187 case ATOMIC_TYPE_BOOL:
188 case ATOMIC_TYPE_INT:
189 case ATOMIC_TYPE_UINT:
190 case ATOMIC_TYPE_LONG:
191 case ATOMIC_TYPE_ULONG:
192 case ATOMIC_TYPE_FLOAT:
195 case ATOMIC_TYPE_LONGLONG:
196 case ATOMIC_TYPE_ULONGLONG:
197 case ATOMIC_TYPE_DOUBLE:
200 case ATOMIC_TYPE_LONG_DOUBLE:
203 case ATOMIC_TYPE_VOID:
206 case ATOMIC_TYPE_INVALID:
209 panic("Trying to determine size of invalid atomic type");
212 static unsigned get_compound_type_size(compound_type_t *type)
214 ir_type *irtype = get_ir_type(&type->type);
215 return get_type_size_bytes(irtype);
218 static unsigned get_array_type_size(array_type_t *type)
220 ir_type *irtype = get_ir_type(&type->type);
221 return get_type_size_bytes(irtype);
224 static unsigned get_type_size(type_t *type)
226 type = skip_typeref(type);
230 return get_atomic_type_size((const atomic_type_t*) type);
232 return get_mode_size_bytes(mode_Is);
233 case TYPE_COMPOUND_UNION:
234 case TYPE_COMPOUND_STRUCT:
235 return get_compound_type_size((compound_type_t*) type);
237 /* just a pointer to the function */
238 return get_mode_size_bytes(mode_P_code);
240 return get_mode_size_bytes(mode_P_data);
242 return get_array_type_size((array_type_t*) type);
249 panic("Trying to determine size of invalid type");
252 static unsigned count_parameters(const function_type_t *function_type)
256 function_parameter_t *parameter = function_type->parameters;
257 for ( ; parameter != NULL; parameter = parameter->next) {
267 static long fold_constant(const expression_t *expression);
269 static ir_type *create_atomic_type(const atomic_type_t *type)
271 ir_mode *mode = get_atomic_mode(type);
272 ident *id = get_mode_ident(mode);
273 ir_type *irtype = new_type_primitive(id, mode);
278 static ir_type *create_method_type(const function_type_t *function_type)
280 type_t *result_type = function_type->result_type;
282 ident *id = unique_ident("functiontype");
283 int n_parameters = count_parameters(function_type);
284 int n_results = result_type == type_void ? 0 : 1;
285 ir_type *irtype = new_type_method(id, n_parameters, n_results);
287 if(result_type != type_void) {
288 ir_type *restype = get_ir_type(result_type);
289 set_method_res_type(irtype, 0, restype);
292 function_parameter_t *parameter = function_type->parameters;
294 for( ; parameter != NULL; parameter = parameter->next) {
295 ir_type *p_irtype = get_ir_type(parameter->type);
296 set_method_param_type(irtype, n, p_irtype);
300 if(function_type->variadic || function_type->unspecified_parameters) {
301 set_method_variadicity(irtype, variadicity_variadic);
307 static ir_type *create_pointer_type(pointer_type_t *type)
309 type_t *points_to = type->points_to;
310 ir_type *ir_points_to;
311 /* Avoid endless recursion if the points_to type contains this poiner type
312 * again (might be a struct). We therefore first create a void* pointer
313 * and then set the real points_to type
315 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
316 ir_type_void, mode_P_data);
317 type->type.firm_type = ir_type;
319 ir_points_to = get_ir_type(points_to);
320 set_pointer_points_to_type(ir_type, ir_points_to);
325 static ir_type *create_array_type(array_type_t *type)
327 type_t *element_type = type->element_type;
328 ir_type *ir_element_type = get_ir_type(element_type);
330 ident *id = unique_ident("array");
331 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
333 if(type->size != NULL) {
334 int n_elements = fold_constant(type->size);
336 set_array_bounds_int(ir_type, 0, 0, n_elements);
338 size_t elemsize = get_type_size_bytes(ir_element_type);
339 int align = get_type_alignment_bytes(ir_element_type);
340 if(elemsize % align > 0) {
341 elemsize += align - (elemsize % align);
343 set_type_size_bytes(ir_type, n_elements * elemsize);
344 set_type_alignment_bytes(ir_type, align);
345 set_type_state(ir_type, layout_fixed);
351 #define INVALID_TYPE ((ir_type_ptr)-1)
353 static ir_type *create_struct_type(compound_type_t *type)
355 symbol_t *symbol = type->declaration->symbol;
358 id = unique_ident(symbol->string);
360 id = unique_ident("__anonymous_struct");
362 ir_type *ir_type = new_type_struct(id);
364 type->type.firm_type = ir_type;
368 declaration_t *entry = type->declaration->context.declarations;
369 for( ; entry != NULL; entry = entry->next) {
370 if(entry->namespc != NAMESPACE_NORMAL)
373 ident *ident = new_id_from_str(entry->symbol->string);
374 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
376 int entry_size = get_type_size_bytes(entry_ir_type);
377 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
378 int misalign = offset % entry_alignment;
380 offset += entry_alignment - misalign;
382 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
383 set_entity_offset(entity, offset);
384 add_struct_member(ir_type, entity);
385 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
386 entry->v.entity = entity;
388 offset += entry_size;
389 if(entry_alignment > align_all) {
390 if(entry_alignment % align_all != 0) {
391 panic("Uneven alignments not supported yet");
393 align_all = entry_alignment;
397 int misalign = offset % align_all;
399 set_type_alignment_bytes(ir_type, align_all);
400 set_type_size_bytes(ir_type, offset);
401 set_type_state(ir_type, layout_fixed);
406 static ir_type *create_union_type(compound_type_t *type)
408 declaration_t *declaration = type->declaration;
409 symbol_t *symbol = declaration->symbol;
412 id = unique_ident(symbol->string);
414 id = unique_ident("__anonymous_union");
416 ir_type *ir_type = new_type_union(id);
418 type->type.firm_type = ir_type;
422 declaration_t *entry = declaration->context.declarations;
423 for( ; entry != NULL; entry = entry->next) {
424 if(entry->namespc != NAMESPACE_NORMAL)
427 ident *ident = new_id_from_str(entry->symbol->string);
428 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
430 int entry_size = get_type_size_bytes(entry_ir_type);
431 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
433 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
434 add_union_member(ir_type, entity);
435 set_entity_offset(entity, 0);
436 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
437 entry->v.entity = entity;
439 if(entry_size > size) {
442 if(entry_alignment > align_all) {
443 if(entry_alignment % align_all != 0) {
444 panic("Uneven alignments not supported yet");
446 align_all = entry_alignment;
450 set_type_alignment_bytes(ir_type, align_all);
451 set_type_size_bytes(ir_type, size);
452 set_type_state(ir_type, layout_fixed);
457 static ir_type *get_ir_type(type_t *type)
459 assert(type != NULL);
461 type = skip_typeref(type);
463 if(type->firm_type != NULL) {
464 assert(type->firm_type != INVALID_TYPE);
465 return type->firm_type;
468 ir_type *firm_type = NULL;
471 firm_type = create_atomic_type((atomic_type_t*) type);
474 firm_type = create_method_type((function_type_t*) type);
477 firm_type = create_pointer_type((pointer_type_t*) type);
480 firm_type = create_array_type((array_type_t*) type);
482 case TYPE_COMPOUND_STRUCT:
483 firm_type = create_struct_type((compound_type_t*) type);
485 case TYPE_COMPOUND_UNION:
486 firm_type = create_union_type((compound_type_t*) type);
489 firm_type = ir_type_int;
497 if(firm_type == NULL)
498 panic("unknown type found");
500 type->firm_type = firm_type;
504 static inline ir_mode *get_ir_mode(type_t *type)
506 ir_type *irtype = get_ir_type(type);
508 /* firm doesn't report a mode for arrays somehow... */
509 if(is_Array_type(irtype)) {
513 ir_mode *mode = get_type_mode(irtype);
514 assert(mode != NULL);
518 static ir_entity* get_function_entity(declaration_t *declaration)
520 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
521 return declaration->v.entity;
522 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
524 symbol_t *symbol = declaration->symbol;
525 ident *id = new_id_from_str(symbol->string);
527 ir_type *global_type = get_glob_type();
528 ir_type *ir_type_method = get_ir_type(declaration->type);
529 assert(is_Method_type(ir_type_method));
531 ir_entity *entity = new_entity(global_type, id, ir_type_method);
532 set_entity_ld_ident(entity, id);
533 if(declaration->storage_class == STORAGE_CLASS_STATIC
534 || declaration->is_inline) {
535 set_entity_visibility(entity, visibility_local);
536 } else if(declaration->init.statement != NULL) {
537 set_entity_visibility(entity, visibility_external_visible);
539 set_entity_visibility(entity, visibility_external_allocated);
542 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
543 declaration->v.entity = entity;
550 static ir_node *expression_to_firm(const expression_t *expression);
552 static dbg_info *get_dbg_info(const source_position_t *pos)
554 return (dbg_info*) pos;
557 static ir_node *const_to_firm(const const_t *cnst)
559 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
560 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
565 if(mode_is_float(mode)) {
566 tv = new_tarval_from_double(cnst->v.float_value, mode);
568 if(mode_is_signed(mode)) {
569 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
571 len = snprintf(buf, sizeof(buf), "%llu", cnst->v.int_value);
573 tv = new_tarval_from_str(buf, len, mode);
576 return new_d_Const(dbgi, mode, tv);
579 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
581 assert(entity != NULL);
582 union symconst_symbol sym;
583 sym.entity_p = entity;
584 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
587 static ir_node *string_to_firm(const source_position_t *const src_pos,
588 const char *const id_prefix,
589 const char *const string)
591 ir_type *const global_type = get_glob_type();
592 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
595 ident *const id = unique_ident(id_prefix);
596 ir_entity *const entity = new_entity(global_type, id, type);
597 set_entity_ld_ident(entity, id);
598 set_entity_variability(entity, variability_constant);
600 ir_type *const elem_type = ir_type_const_char;
601 ir_mode *const mode = get_type_mode(elem_type);
603 const size_t slen = strlen(string) + 1;
605 set_array_lower_bound_int(type, 0, 0);
606 set_array_upper_bound_int(type, 0, slen);
607 set_type_size_bytes(type, slen);
608 set_type_state(type, layout_fixed);
610 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
611 for(size_t i = 0; i < slen; ++i) {
612 tvs[i] = new_tarval_from_long(string[i], mode);
615 set_array_entity_values(entity, tvs, slen);
618 dbg_info *const dbgi = get_dbg_info(src_pos);
620 return create_symconst(dbgi, entity);
623 static ir_node *string_literal_to_firm(const string_literal_t* literal)
625 return string_to_firm(&literal->expression.source_position, "Lstr",
629 static ir_node *deref_address(type_t *const type, ir_node *const addr,
630 dbg_info *const dbgi)
632 switch (type->type) {
634 case TYPE_COMPOUND_STRUCT:
635 case TYPE_COMPOUND_UNION:
639 ir_mode *const mode = get_ir_mode(type);
640 ir_node *const memory = get_store();
641 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
642 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
643 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
650 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
652 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
653 declaration_t *declaration = ref->declaration;
654 type_t *type = skip_typeref(declaration->type);
656 switch((declaration_type_t) declaration->declaration_type) {
657 case DECLARATION_TYPE_UNKNOWN:
659 case DECLARATION_TYPE_LOCAL_VARIABLE: {
660 ir_mode *mode = get_ir_mode(type);
661 return get_value(declaration->v.value_number, mode);
663 case DECLARATION_TYPE_FUNCTION: {
664 return create_symconst(dbgi, declaration->v.entity);
666 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
667 ir_entity *entity = declaration->v.entity;
668 ir_node *symconst = create_symconst(dbgi, entity);
669 return deref_address(type, symconst, dbgi);
671 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
672 ir_entity *entity = declaration->v.entity;
673 ir_node *frame = get_irg_frame(current_ir_graph);
674 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
675 return deref_address(type, sel, dbgi);
678 case DECLARATION_TYPE_COMPOUND_MEMBER:
679 case DECLARATION_TYPE_LABEL_BLOCK:
680 panic("not implemented reference type");
683 panic("reference to declaration with unknown type found");
686 static ir_node *reference_addr(const reference_expression_t *ref)
688 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
689 declaration_t *declaration = ref->declaration;
691 switch((declaration_type_t) declaration->declaration_type) {
692 case DECLARATION_TYPE_UNKNOWN:
694 case DECLARATION_TYPE_LOCAL_VARIABLE:
695 panic("local variable without entity has no address");
696 case DECLARATION_TYPE_FUNCTION: {
697 return create_symconst(dbgi, declaration->v.entity);
699 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
700 ir_entity *entity = declaration->v.entity;
701 ir_node *symconst = create_symconst(dbgi, entity);
704 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
705 ir_entity *entity = declaration->v.entity;
706 ir_node *frame = get_irg_frame(current_ir_graph);
707 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
711 case DECLARATION_TYPE_COMPOUND_MEMBER:
712 case DECLARATION_TYPE_LABEL_BLOCK:
713 panic("not implemented reference type");
716 panic("reference to declaration with unknown type found");
719 static ir_node *process_builtin_call(const call_expression_t *call)
721 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
723 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
724 builtin_symbol_expression_t *builtin
725 = (builtin_symbol_expression_t*) call->function;
726 symbol_t *symbol = builtin->symbol;
728 if(symbol == symbol_alloca) {
729 if(call->arguments == NULL || call->arguments->next != NULL) {
730 panic("invalid number of parameters on __builtin_alloca");
732 expression_t *argument = call->arguments->expression;
733 ir_node *size = expression_to_firm(argument);
735 ir_node *store = get_store();
736 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
738 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
740 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
744 panic("Unsupported builtin found\n");
748 static ir_node *call_expression_to_firm(const call_expression_t *call)
750 assert(get_cur_block() != NULL);
752 expression_t *function = call->function;
753 if(function->type == EXPR_BUILTIN_SYMBOL) {
754 return process_builtin_call(call);
756 ir_node *callee = expression_to_firm(function);
758 function_type_t *function_type;
759 if (function->datatype->type == TYPE_POINTER) {
760 pointer_type_t *const ptr_type = (pointer_type_t*)function->datatype;
761 assert(ptr_type->points_to->type == TYPE_FUNCTION);
762 function_type = (function_type_t*)ptr_type->points_to;
764 assert(function->datatype->type == TYPE_FUNCTION);
765 function_type = (function_type_t*)function->datatype;
768 int n_parameters = 0;
769 call_argument_t *argument = call->arguments;
770 for( ; argument != NULL; argument = argument->next) {
774 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
775 ir_type *new_method_type = NULL;
776 if(function_type->variadic || function_type->unspecified_parameters) {
777 /* we need to construct a new method type matching the call
779 int n_res = get_method_n_ress(ir_method_type);
780 new_method_type = new_type_method(unique_ident("calltype"),
781 n_parameters, n_res);
782 set_method_calling_convention(new_method_type,
783 get_method_calling_convention(ir_method_type));
784 set_method_additional_properties(new_method_type,
785 get_method_additional_properties(ir_method_type));
787 for(int i = 0; i < n_res; ++i) {
788 set_method_res_type(new_method_type, i,
789 get_method_res_type(ir_method_type, i));
792 ir_node *in[n_parameters];
794 argument = call->arguments;
796 for( ; argument != NULL; argument = argument->next) {
797 expression_t *expression = argument->expression;
798 ir_node *arg_node = expression_to_firm(expression);
801 if(new_method_type != NULL) {
802 ir_type *irtype = get_ir_type(expression->datatype);
803 set_method_param_type(new_method_type, n, irtype);
808 assert(n == n_parameters);
810 if(new_method_type != NULL)
811 ir_method_type = new_method_type;
813 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
814 ir_node *store = get_store();
815 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
817 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
820 type_t *result_type = function_type->result_type;
821 ir_node *result = NULL;
822 if(result_type != type_void) {
823 ir_mode *mode = get_ir_mode(result_type);
824 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
825 result = new_d_Proj(dbgi, resproj, mode, 0);
831 static void statement_to_firm(statement_t *statement);
832 static ir_node *compound_statement_to_firm(statement_t *compound);
834 static ir_node *expression_to_addr(const expression_t *expression);
835 static void create_condition_evaluation(const expression_t *expression,
837 ir_node *false_block);
839 static void set_value_for_expression(const expression_t *expression,
842 if(expression->type == EXPR_REFERENCE) {
843 reference_expression_t *ref = (reference_expression_t*) expression;
845 declaration_t *declaration = ref->declaration;
846 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
847 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
848 set_value(declaration->v.value_number, value);
853 dbg_info *dbgi = get_dbg_info(&expression->source_position);
854 ir_node *addr = expression_to_addr(expression);
855 assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
856 ir_node *memory = get_store();
857 ir_node *store = new_d_Store(dbgi, memory, addr, value);
858 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
859 set_store(store_mem);
862 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
864 ir_mode *value_mode = get_irn_mode(value);
866 if (value_mode == dest_mode || is_Bad(value))
869 if(dest_mode == mode_b) {
870 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
871 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
872 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
876 return new_d_Conv(dbgi, value, dest_mode);
879 static ir_node *create_incdec(const unary_expression_t *expression)
881 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
882 type_t *type = expression->expression.datatype;
883 ir_mode *mode = get_ir_mode(type);
884 expression_t *value = expression->value;
886 ir_node *value_node = expression_to_firm(value);
889 if(type->type == TYPE_POINTER) {
890 pointer_type_t *pointer_type = (pointer_type_t*) type;
891 unsigned elem_size = get_type_size(pointer_type->points_to);
892 offset = new_Const_long(mode_Is, elem_size);
894 assert(is_type_arithmetic(type));
895 offset = new_Const(mode, get_mode_one(mode));
898 switch(expression->type) {
899 case UNEXPR_POSTFIX_INCREMENT: {
900 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
901 set_value_for_expression(value, new_value);
904 case UNEXPR_POSTFIX_DECREMENT: {
905 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
906 set_value_for_expression(value, new_value);
909 case UNEXPR_PREFIX_INCREMENT: {
910 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
911 set_value_for_expression(value, new_value);
914 case UNEXPR_PREFIX_DECREMENT: {
915 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
916 set_value_for_expression(value, new_value);
920 panic("no incdec expr in create_incdec");
925 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
927 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
928 type_t *type = skip_typeref(expression->expression.datatype);
930 if(expression->type == UNEXPR_TAKE_ADDRESS)
931 return expression_to_addr(expression->value);
933 const expression_t *value = expression->value;
934 ir_node *value_node = expression_to_firm(value);
936 switch(expression->type) {
938 return new_d_Minus(dbgi, value_node, get_ir_mode(type));
941 case UNEXPR_BITWISE_NEGATE:
942 return new_d_Not(dbgi, value_node, get_ir_mode(type));
944 if(get_irn_mode(value_node) != mode_b) {
945 value_node = create_conv(dbgi, value_node, mode_b);
947 value_node = new_d_Not(dbgi, value_node, mode_b);
948 ir_mode *const mode = get_ir_mode(type);
950 value_node = create_conv(dbgi, value_node, mode);
954 case UNEXPR_DEREFERENCE:
955 return deref_address(type, value_node, dbgi);
956 case UNEXPR_POSTFIX_INCREMENT:
957 case UNEXPR_POSTFIX_DECREMENT:
958 case UNEXPR_PREFIX_INCREMENT:
959 case UNEXPR_PREFIX_DECREMENT:
960 return create_incdec(expression);
962 return create_conv(dbgi, value_node, get_ir_mode(type));
964 case UNEXPR_TAKE_ADDRESS:
968 panic("invalid UNEXPR type found");
971 static long get_pnc(binary_expression_type_t type)
974 case BINEXPR_EQUAL: return pn_Cmp_Eq;
975 case BINEXPR_NOTEQUAL: return pn_Cmp_Lg;
976 case BINEXPR_LESS: return pn_Cmp_Lt;
977 case BINEXPR_LESSEQUAL: return pn_Cmp_Le;
978 case BINEXPR_GREATER: return pn_Cmp_Gt;
979 case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
983 panic("trying to get pn_Cmp from non-comparison binexpr type");
986 static ir_node *create_lazy_op(const binary_expression_t *expression)
988 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
989 type_t *type = expression->expression.datatype;
990 ir_mode *mode = get_ir_mode(type);
992 ir_node *cur_block = get_cur_block();
994 ir_node *one_block = new_immBlock();
995 ir_node *one = new_Const(mode, get_mode_one(mode));
996 ir_node *jmp_one = new_d_Jmp(dbgi);
998 ir_node *zero_block = new_immBlock();
999 ir_node *zero = new_Const(mode, get_mode_null(mode));
1000 ir_node *jmp_zero = new_d_Jmp(dbgi);
1002 set_cur_block(cur_block);
1003 create_condition_evaluation((const expression_t*) expression,
1004 one_block, zero_block);
1005 mature_immBlock(one_block);
1006 mature_immBlock(zero_block);
1008 ir_node *common_block = new_immBlock();
1009 add_immBlock_pred(common_block, jmp_one);
1010 add_immBlock_pred(common_block, jmp_zero);
1011 mature_immBlock(common_block);
1013 ir_node *in[2] = { one, zero };
1014 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1019 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1020 ir_node *right, ir_mode *mode);
1022 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1023 create_arithmetic_func func)
1025 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1026 ir_node *left = expression_to_firm(expression->left);
1027 ir_node *right = expression_to_firm(expression->right);
1028 type_t *type = expression->right->datatype;
1029 /* be careful with the modes, because in arithmetic assign nodes only
1030 * the right operand has the mode of the arithmetic already */
1031 ir_mode *mode = get_ir_mode(type);
1032 left = create_conv(dbgi, left, mode);
1033 ir_node *res = func(dbgi, left, right, mode);
1038 static ir_node *pointer_arithmetic(ir_node *const pointer,
1041 dbg_info *const dbgi,
1042 const create_arithmetic_func func)
1044 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1045 type_t *const points_to = pointer_type->points_to;
1046 const unsigned elem_size = get_type_size(points_to);
1048 assert(elem_size >= 1);
1049 if (elem_size > 1) {
1050 integer = create_conv(dbgi, integer, mode_Is);
1051 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1052 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1056 ir_mode *const mode = get_ir_mode(type);
1057 return func(dbgi, pointer, integer, mode);
1060 static ir_node *create_arithmetic_assign_binop(
1061 const binary_expression_t *expression, create_arithmetic_func func)
1063 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1064 type_t *const type = expression->expression.datatype;
1067 if (type->type == TYPE_POINTER) {
1068 ir_node *const pointer = expression_to_firm(expression->left);
1069 ir_node * integer = expression_to_firm(expression->right);
1070 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1072 value = create_arithmetic_binop(expression, func);
1075 ir_mode *const mode = get_ir_mode(type);
1076 value = create_conv(dbgi, value, mode);
1077 set_value_for_expression(expression->left, value);
1082 static ir_node *create_add(const binary_expression_t *expression)
1084 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1085 ir_node *left = expression_to_firm(expression->left);
1086 ir_node *right = expression_to_firm(expression->right);
1087 type_t *type = expression->expression.datatype;
1089 expression_t *expr_left = expression->left;
1090 expression_t *expr_right = expression->right;
1091 type_t *type_left = skip_typeref(expr_left->datatype);
1092 type_t *type_right = skip_typeref(expr_right->datatype);
1094 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1095 ir_mode *const mode = get_ir_mode(type);
1096 return new_d_Add(dbgi, left, right, mode);
1099 if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1100 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1102 assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
1103 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1107 static ir_node *create_sub(const binary_expression_t *expression)
1109 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1110 expression_t *const expr_left = expression->left;
1111 expression_t *const expr_right = expression->right;
1112 ir_node *const left = expression_to_firm(expr_left);
1113 ir_node *const right = expression_to_firm(expr_right);
1114 type_t *const type = expression->expression.datatype;
1115 type_t *const type_left = skip_typeref(expr_left->datatype);
1116 type_t *const type_right = skip_typeref(expr_right->datatype);
1118 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1119 ir_mode *const mode = get_ir_mode(type);
1120 return new_d_Sub(dbgi, left, right, mode);
1121 } else if (type_left->type == TYPE_POINTER && type_right->type == TYPE_POINTER) {
1122 const pointer_type_t *const ptr_type = (const pointer_type_t*)type_left;
1123 const unsigned elem_size = get_type_size(ptr_type->points_to);
1124 ir_mode *const mode = get_ir_mode(type);
1125 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1126 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1127 ir_node *const no_mem = new_NoMem();
1128 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1129 op_pin_state_floats);
1130 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1133 assert(type_left->type == TYPE_POINTER);
1134 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1137 static ir_node *create_shift(const binary_expression_t *expression)
1139 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1140 ir_node *left = expression_to_firm(expression->left);
1141 ir_node *right = expression_to_firm(expression->right);
1142 type_t *type = expression->expression.datatype;
1143 ir_mode *mode = get_ir_mode(type);
1145 /* firm always wants the shift count to be unsigned */
1146 right = create_conv(dbgi, right, mode_Iu);
1150 switch(expression->type) {
1151 case BINEXPR_SHIFTLEFT_ASSIGN:
1152 case BINEXPR_SHIFTLEFT:
1153 res = new_d_Shl(dbgi, left, right, mode);
1155 case BINEXPR_SHIFTRIGHT_ASSIGN:
1156 case BINEXPR_SHIFTRIGHT: {
1157 expression_t *expr_left = expression->left;
1158 type_t *type_left = skip_typeref(expr_left->datatype);
1160 if(is_type_signed(type_left)) {
1161 res = new_d_Shrs(dbgi, left, right, mode);
1163 res = new_d_Shr(dbgi, left, right, mode);
1168 panic("create shift op called for non-shift op");
1175 static ir_node *create_divmod(const binary_expression_t *expression)
1177 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1178 ir_node *left = expression_to_firm(expression->left);
1179 ir_node *right = expression_to_firm(expression->right);
1180 ir_node *pin = new_Pin(new_NoMem());
1181 /* be careful with the modes, because in arithmetic assign nodes only
1182 * the right operand has the mode of the arithmetic already */
1183 type_t *type = expression->right->datatype;
1184 ir_mode *mode = get_ir_mode(type);
1185 left = create_conv(dbgi, left, mode);
1189 switch (expression->type) {
1191 case BINEXPR_DIV_ASSIGN:
1192 if(mode_is_float(mode)) {
1193 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1194 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1196 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1197 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1202 case BINEXPR_MOD_ASSIGN:
1203 assert(!mode_is_float(mode));
1204 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1205 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1208 default: panic("unexpected binary expression type in create_divmod()");
1214 static ir_node *create_arithmetic_assign_divmod(
1215 const binary_expression_t *expression)
1217 ir_node * value = create_divmod(expression);
1218 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1219 type_t *const type = expression->expression.datatype;
1220 ir_mode *const mode = get_ir_mode(type);
1222 assert(type->type != TYPE_POINTER);
1224 value = create_conv(dbgi, value, mode);
1225 set_value_for_expression(expression->left, value);
1230 static ir_node *create_arithmetic_assign_shift(
1231 const binary_expression_t *expression)
1233 ir_node * value = create_shift(expression);
1234 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1235 type_t *const type = expression->expression.datatype;
1236 ir_mode *const mode = get_ir_mode(type);
1238 value = create_conv(dbgi, value, mode);
1239 set_value_for_expression(expression->left, value);
1244 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1246 binary_expression_type_t type = expression->type;
1249 case BINEXPR_NOTEQUAL:
1251 case BINEXPR_LESSEQUAL:
1252 case BINEXPR_GREATER:
1253 case BINEXPR_GREATEREQUAL: {
1254 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1255 ir_node *left = expression_to_firm(expression->left);
1256 ir_node *right = expression_to_firm(expression->right);
1257 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1258 long pnc = get_pnc(type);
1259 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1262 case BINEXPR_ASSIGN: {
1263 ir_node *right = expression_to_firm(expression->right);
1264 set_value_for_expression(expression->left, right);
1268 return create_add(expression);
1270 return create_sub(expression);
1272 return create_arithmetic_binop(expression, new_d_Mul);
1273 case BINEXPR_BITWISE_AND:
1274 return create_arithmetic_binop(expression, new_d_And);
1275 case BINEXPR_BITWISE_OR:
1276 return create_arithmetic_binop(expression, new_d_Or);
1277 case BINEXPR_BITWISE_XOR:
1278 return create_arithmetic_binop(expression, new_d_Eor);
1279 case BINEXPR_SHIFTLEFT:
1280 case BINEXPR_SHIFTRIGHT:
1281 return create_shift(expression);
1284 return create_divmod(expression);
1285 case BINEXPR_LOGICAL_AND:
1286 case BINEXPR_LOGICAL_OR:
1287 return create_lazy_op(expression);
1289 expression_to_firm(expression->left);
1290 return expression_to_firm(expression->right);
1291 case BINEXPR_ADD_ASSIGN:
1292 return create_arithmetic_assign_binop(expression, new_d_Add);
1293 case BINEXPR_SUB_ASSIGN:
1294 return create_arithmetic_assign_binop(expression, new_d_Sub);
1295 case BINEXPR_MUL_ASSIGN:
1296 return create_arithmetic_assign_binop(expression, new_d_Mul);
1297 case BINEXPR_DIV_ASSIGN:
1298 return create_arithmetic_assign_divmod(expression);
1299 case BINEXPR_BITWISE_AND_ASSIGN:
1300 return create_arithmetic_assign_binop(expression, new_d_And);
1301 case BINEXPR_BITWISE_OR_ASSIGN:
1302 return create_arithmetic_assign_binop(expression, new_d_Or);
1303 case BINEXPR_BITWISE_XOR_ASSIGN:
1304 return create_arithmetic_assign_binop(expression, new_d_Eor);
1305 case BINEXPR_SHIFTLEFT_ASSIGN:
1306 case BINEXPR_SHIFTRIGHT_ASSIGN:
1307 return create_arithmetic_assign_shift(expression);
1309 panic("TODO binexpr type");
1313 static ir_node *array_access_addr(const array_access_expression_t *expression)
1315 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1319 type_t *type_left = skip_typeref(expression->array_ref->datatype);
1320 type_t *type_right = skip_typeref(expression->index->datatype);
1322 if(type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
1323 base_addr = expression_to_firm(expression->array_ref);
1324 offset = expression_to_firm(expression->index);
1326 assert(type_right->type == TYPE_POINTER
1327 || type_right->type == TYPE_ARRAY);
1328 base_addr = expression_to_firm(expression->index);
1329 offset = expression_to_firm(expression->array_ref);
1331 offset = create_conv(dbgi, offset, mode_Iu);
1333 unsigned elem_size = get_type_size(expression->expression.datatype);
1334 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1335 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1337 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1342 static ir_node *array_access_to_firm(
1343 const array_access_expression_t *expression)
1345 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1346 ir_node *addr = array_access_addr(expression);
1347 type_t *type = skip_typeref(expression->expression.datatype);
1348 return deref_address(type, addr, dbgi);
1351 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1353 type_t *type = expression->type;
1355 type = expression->size_expression->datatype;
1356 assert(type != NULL);
1359 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1360 unsigned size = get_type_size(type);
1361 ir_node *size_node = new_Const_long(mode, size);
1366 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1368 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1370 ir_node *cur_block = get_cur_block();
1372 /* create the true block */
1373 ir_node *true_block = new_immBlock();
1375 ir_node *true_val = expression_to_firm(expression->true_expression);
1376 ir_node *true_jmp = new_Jmp();
1378 /* create the false block */
1379 ir_node *false_block = new_immBlock();
1381 ir_node *false_val = expression_to_firm(expression->false_expression);
1382 ir_node *false_jmp = new_Jmp();
1384 /* create the condition evaluation */
1385 set_cur_block(cur_block);
1386 create_condition_evaluation(expression->condition, true_block, false_block);
1387 mature_immBlock(true_block);
1388 mature_immBlock(false_block);
1390 /* create the common block */
1391 ir_node *common_block = new_immBlock();
1392 add_immBlock_pred(common_block, true_jmp);
1393 add_immBlock_pred(common_block, false_jmp);
1394 mature_immBlock(common_block);
1396 /* TODO improve static semantics, so either both or no values are NULL */
1397 if (true_val == NULL || false_val == NULL) return NULL;
1399 ir_node *in[2] = { true_val, false_val };
1400 ir_mode *mode = get_irn_mode(true_val);
1401 assert(get_irn_mode(false_val) == mode);
1402 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1407 static ir_node *select_addr(const select_expression_t *expression)
1409 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1411 ir_node *compound_addr = expression_to_firm(expression->compound);
1413 declaration_t *entry = expression->compound_entry;
1414 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1415 ir_entity *entity = entry->v.entity;
1417 assert(entity != NULL);
1419 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1424 static ir_node *select_to_firm(const select_expression_t *expression)
1426 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1427 ir_node *addr = select_addr(expression);
1428 type_t *type = skip_typeref(expression->expression.datatype);
1429 return deref_address(type, addr, dbgi);
1432 /* Values returned by __builtin_classify_type. */
1433 typedef enum gcc_type_class
1439 enumeral_type_class,
1442 reference_type_class,
1446 function_type_class,
1457 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1459 const type_t *const type = expr->type_expression->datatype;
1465 const atomic_type_t *const atomic_type = (const atomic_type_t*)type;
1466 switch (atomic_type->atype) {
1467 // should not be reached
1468 case ATOMIC_TYPE_INVALID:
1472 // gcc cannot do that
1473 case ATOMIC_TYPE_VOID:
1474 tc = void_type_class;
1477 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1478 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1479 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1480 case ATOMIC_TYPE_SHORT:
1481 case ATOMIC_TYPE_USHORT:
1482 case ATOMIC_TYPE_INT:
1483 case ATOMIC_TYPE_UINT:
1484 case ATOMIC_TYPE_LONG:
1485 case ATOMIC_TYPE_ULONG:
1486 case ATOMIC_TYPE_LONGLONG:
1487 case ATOMIC_TYPE_ULONGLONG:
1488 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1489 tc = integer_type_class;
1492 case ATOMIC_TYPE_FLOAT:
1493 case ATOMIC_TYPE_DOUBLE:
1494 case ATOMIC_TYPE_LONG_DOUBLE:
1495 tc = real_type_class;
1498 #ifdef PROVIDE_COMPLEX
1499 case ATOMIC_TYPE_FLOAT_COMPLEX:
1500 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1501 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1502 tc = complex_type_class;
1504 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1505 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1506 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1507 tc = complex_type_class;
1512 panic("Unimplemented case in classify_type_to_firm().");
1517 case TYPE_ARRAY: // gcc handles this as pointer
1518 case TYPE_FUNCTION: // gcc handles this as pointer
1519 case TYPE_POINTER: tc = pointer_type_class; break;
1520 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1521 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1523 // gcc handles this as integer
1524 case TYPE_ENUM: tc = integer_type_class; break;
1527 panic("Unimplemented case in classify_type_to_firm().");
1530 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1531 ir_mode *const mode = mode_Is;
1532 tarval *const tv = new_tarval_from_long(tc, mode);
1533 return new_d_Const(dbgi, mode, tv);
1536 static ir_node *function_name_to_firm(const string_literal_t *const expr)
1538 if (current_function_name == NULL) {
1539 const source_position_t *const src_pos =
1540 &expr->expression.source_position;
1541 const char *const name = current_function_decl->symbol->string;
1542 current_function_name = string_to_firm(src_pos, "__func__", name);
1545 return current_function_name;
1548 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1550 statement_t *statement = expr->statement;
1552 assert(statement->type == STATEMENT_COMPOUND);
1553 return compound_statement_to_firm(statement);
1556 static ir_node *dereference_addr(const unary_expression_t *const expression)
1558 assert(expression->type == UNEXPR_DEREFERENCE);
1559 return expression_to_firm(expression->value);
1562 static ir_node *expression_to_addr(const expression_t *expression)
1564 switch(expression->type) {
1565 case EXPR_REFERENCE:
1566 return reference_addr((const reference_expression_t*) expression);
1567 case EXPR_ARRAY_ACCESS:
1568 return array_access_addr((const array_access_expression_t*) expression);
1570 return select_addr((const select_expression_t*) expression);
1572 const unary_expression_t *const unary_expr =
1573 (const unary_expression_t*)expression;
1574 if (unary_expr->type == UNEXPR_DEREFERENCE) {
1575 return dereference_addr(unary_expr);
1582 panic("trying to get address of non-lvalue");
1585 static ir_node *_expression_to_firm(const expression_t *expression)
1587 switch(expression->type) {
1589 return const_to_firm((const const_t*) expression);
1590 case EXPR_STRING_LITERAL:
1591 return string_literal_to_firm((const string_literal_t*) expression);
1592 case EXPR_REFERENCE:
1593 return reference_expression_to_firm(
1594 (const reference_expression_t*) expression);
1596 return call_expression_to_firm((const call_expression_t*) expression);
1598 return unary_expression_to_firm((const unary_expression_t*) expression);
1600 return binary_expression_to_firm(
1601 (const binary_expression_t*) expression);
1602 case EXPR_ARRAY_ACCESS:
1603 return array_access_to_firm(
1604 (const array_access_expression_t*) expression);
1606 return sizeof_to_firm((const sizeof_expression_t*) expression);
1607 case EXPR_CONDITIONAL:
1608 return conditional_to_firm((const conditional_expression_t*)expression);
1610 return select_to_firm((const select_expression_t*) expression);
1611 case EXPR_CLASSIFY_TYPE:
1612 return classify_type_to_firm((const classify_type_expression_t*)expression);
1614 case EXPR_PRETTY_FUNCTION:
1615 return function_name_to_firm((const string_literal_t*)expression);
1616 case EXPR_STATEMENT:
1617 return statement_expression_to_firm(
1618 (const statement_expression_t*) expression);
1621 case EXPR_BUILTIN_SYMBOL:
1622 panic("unimplemented expression found");
1628 panic("invalid expression found");
1631 static ir_node *expression_to_firm(const expression_t *expression)
1633 ir_node *res = _expression_to_firm(expression);
1635 if(res != NULL && get_irn_mode(res) == mode_b) {
1636 ir_mode *mode = get_ir_mode(expression->datatype);
1637 res = create_conv(NULL, res, mode);
1643 static ir_node *expression_to_modeb(const expression_t *expression)
1645 ir_node *res = _expression_to_firm(expression);
1646 res = create_conv(NULL, res, mode_b);
1652 * create a short-circuit expression evaluation that tries to construct
1653 * efficient control flow structures for &&, || and ! expressions
1655 static void create_condition_evaluation(const expression_t *expression,
1656 ir_node *true_block,
1657 ir_node *false_block)
1659 switch(expression->type) {
1661 unary_expression_t *unary_expression = (unary_expression_t*) expression;
1662 if(unary_expression->type == UNEXPR_NOT) {
1663 create_condition_evaluation(unary_expression->value, false_block,
1670 binary_expression_t *binary_expression
1671 = (binary_expression_t*) expression;
1672 if(binary_expression->type == BINEXPR_LOGICAL_AND) {
1673 ir_node *cur_block = get_cur_block();
1674 ir_node *extra_block = new_immBlock();
1675 set_cur_block(cur_block);
1676 create_condition_evaluation(binary_expression->left, extra_block,
1678 mature_immBlock(extra_block);
1679 set_cur_block(extra_block);
1680 create_condition_evaluation(binary_expression->right, true_block,
1684 if(binary_expression->type == BINEXPR_LOGICAL_OR) {
1685 ir_node *cur_block = get_cur_block();
1686 ir_node *extra_block = new_immBlock();
1687 set_cur_block(cur_block);
1688 create_condition_evaluation(binary_expression->left, true_block,
1690 mature_immBlock(extra_block);
1691 set_cur_block(extra_block);
1692 create_condition_evaluation(binary_expression->right, true_block,
1702 dbg_info *dbgi = get_dbg_info(&expression->source_position);
1703 ir_node *condition = expression_to_modeb(expression);
1704 ir_node *cond = new_d_Cond(dbgi, condition);
1705 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1706 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1708 add_immBlock_pred(true_block, true_proj);
1709 add_immBlock_pred(false_block, false_proj);
1711 set_cur_block(NULL);
1715 static void return_statement_to_firm(statement_t *statement)
1717 if(get_cur_block() == NULL)
1720 dbg_info *dbgi = get_dbg_info(&statement->source_position);
1723 if(statement->v.return_value != NULL) {
1724 ir_node *retval = expression_to_firm(statement->v.return_value);
1728 ret = new_d_Return(dbgi, get_store(), 1, in);
1730 ret = new_d_Return(dbgi, get_store(), 0, NULL);
1732 ir_node *end_block = get_irg_end_block(current_ir_graph);
1733 add_immBlock_pred(end_block, ret);
1735 set_cur_block(NULL);
1738 static ir_node *expression_statement_to_firm(statement_t *statement)
1740 if(get_cur_block() == NULL)
1743 return expression_to_firm(statement->v.expression);
1746 static ir_node *compound_statement_to_firm(statement_t *compound)
1748 ir_node *result = NULL;
1749 statement_t *statement = compound->v.compound_stmt.statements;
1750 for( ; statement != NULL; statement = statement->next) {
1751 //context2firm(&statement->context);
1753 if(statement->next == NULL && statement->type == STATEMENT_EXPRESSION) {
1754 result = expression_statement_to_firm(statement);
1757 statement_to_firm(statement);
1763 static void if_statement_to_firm(statement_t *statement)
1765 ir_node *cur_block = get_cur_block();
1767 ir_node *fallthrough_block = new_immBlock();
1769 /* the true (blocks) */
1770 ir_node *true_block;
1771 if (statement->v.if_stmt.true_statement != NULL) {
1772 true_block = new_immBlock();
1773 statement_to_firm(statement->v.if_stmt.true_statement);
1774 if(get_cur_block() != NULL) {
1775 ir_node *jmp = new_Jmp();
1776 add_immBlock_pred(fallthrough_block, jmp);
1779 true_block = fallthrough_block;
1782 /* the false (blocks) */
1783 ir_node *false_block;
1784 if(statement->v.if_stmt.false_statement != NULL) {
1785 false_block = new_immBlock();
1787 statement_to_firm(statement->v.if_stmt.false_statement);
1788 if(get_cur_block() != NULL) {
1789 ir_node *jmp = new_Jmp();
1790 add_immBlock_pred(fallthrough_block, jmp);
1793 false_block = fallthrough_block;
1796 /* create the condition */
1797 if(cur_block != NULL) {
1798 set_cur_block(cur_block);
1799 create_condition_evaluation(statement->v.if_stmt.condition, true_block,
1803 mature_immBlock(true_block);
1804 if(false_block != fallthrough_block) {
1805 mature_immBlock(false_block);
1807 mature_immBlock(fallthrough_block);
1809 set_cur_block(fallthrough_block);
1812 static void while_statement_to_firm(statement_t *statement)
1814 ir_node *jmp = NULL;
1815 if(get_cur_block() != NULL) {
1819 /* create the header block */
1820 ir_node *header_block = new_immBlock();
1822 add_immBlock_pred(header_block, jmp);
1825 /* the false block */
1826 ir_node *false_block = new_immBlock();
1829 ir_node *body_block;
1830 if (statement->v.while_stmt.body != NULL) {
1831 ir_node *old_continue_label = continue_label;
1832 ir_node *old_break_label = break_label;
1833 continue_label = header_block;
1834 break_label = false_block;
1836 body_block = new_immBlock();
1837 statement_to_firm(statement->v.while_stmt.body);
1839 assert(continue_label == header_block);
1840 assert(break_label == false_block);
1841 continue_label = old_continue_label;
1842 break_label = old_break_label;
1844 if(get_cur_block() != NULL) {
1845 ir_node *jmp = new_Jmp();
1846 add_immBlock_pred(header_block, jmp);
1849 body_block = header_block;
1852 /* create the condition */
1853 set_cur_block(header_block);
1855 create_condition_evaluation(statement->v.while_stmt.condition, body_block, false_block);
1856 mature_immBlock(body_block);
1857 mature_immBlock(false_block);
1858 mature_immBlock(header_block);
1860 set_cur_block(false_block);
1863 static void do_while_statement_to_firm(statement_t *statement)
1865 ir_node *jmp = NULL;
1866 if(get_cur_block() != NULL) {
1870 /* create the header block */
1871 ir_node *header_block = new_immBlock();
1873 /* the false block */
1874 ir_node *false_block = new_immBlock();
1877 ir_node *body_block = new_immBlock();
1879 add_immBlock_pred(body_block, jmp);
1882 if (statement->v.while_stmt.body != NULL) {
1883 ir_node *old_continue_label = continue_label;
1884 ir_node *old_break_label = break_label;
1885 continue_label = header_block;
1886 break_label = false_block;
1888 statement_to_firm(statement->v.while_stmt.body);
1890 assert(continue_label == header_block);
1891 assert(break_label == false_block);
1892 continue_label = old_continue_label;
1893 break_label = old_break_label;
1895 if (get_cur_block() == NULL) {
1896 mature_immBlock(header_block);
1897 mature_immBlock(body_block);
1898 mature_immBlock(false_block);
1903 ir_node *body_jmp = new_Jmp();
1904 add_immBlock_pred(header_block, body_jmp);
1905 mature_immBlock(header_block);
1907 /* create the condition */
1908 set_cur_block(header_block);
1910 create_condition_evaluation(statement->v.while_stmt.condition, body_block, false_block);
1911 mature_immBlock(body_block);
1912 mature_immBlock(false_block);
1913 mature_immBlock(header_block);
1915 set_cur_block(false_block);
1918 static void for_statement_to_firm(statement_t *statement)
1920 ir_node *jmp = NULL;
1921 if (get_cur_block() != NULL) {
1922 if(statement->v.for_stmt.initialisation != NULL) {
1923 expression_to_firm(statement->v.for_stmt.initialisation);
1928 /* create the step block */
1929 ir_node *const step_block = new_immBlock();
1930 if (statement->v.for_stmt.step != NULL) {
1931 expression_to_firm(statement->v.for_stmt.step);
1933 ir_node *const step_jmp = new_Jmp();
1935 /* create the header block */
1936 ir_node *const header_block = new_immBlock();
1938 add_immBlock_pred(header_block, jmp);
1940 add_immBlock_pred(header_block, step_jmp);
1942 /* the false block */
1943 ir_node *const false_block = new_immBlock();
1946 ir_node * body_block;
1947 if (statement->v.for_stmt.body != NULL) {
1948 ir_node *const old_continue_label = continue_label;
1949 ir_node *const old_break_label = break_label;
1950 continue_label = step_block;
1951 break_label = false_block;
1953 body_block = new_immBlock();
1954 statement_to_firm(statement->v.for_stmt.body);
1956 assert(continue_label == step_block);
1957 assert(break_label == false_block);
1958 continue_label = old_continue_label;
1959 break_label = old_break_label;
1961 if (get_cur_block() != NULL) {
1962 ir_node *const jmp = new_Jmp();
1963 add_immBlock_pred(step_block, jmp);
1966 body_block = step_block;
1969 /* create the condition */
1970 set_cur_block(header_block);
1971 if (statement->v.for_stmt.condition != NULL) {
1972 create_condition_evaluation(statement->v.for_stmt.condition, body_block,
1975 keep_alive(header_block);
1976 ir_node *jmp = new_Jmp();
1977 add_immBlock_pred(body_block, jmp);
1980 mature_immBlock(body_block);
1981 mature_immBlock(false_block);
1982 mature_immBlock(step_block);
1983 mature_immBlock(header_block);
1984 mature_immBlock(false_block);
1986 set_cur_block(false_block);
1989 static void create_declaration_entity(declaration_t *declaration,
1990 declaration_type_t declaration_type,
1991 ir_type *parent_type)
1993 ident *id = new_id_from_str(declaration->symbol->string);
1994 ir_type *irtype = get_ir_type(declaration->type);
1995 ir_entity *entity = new_entity(parent_type, id, irtype);
1996 set_entity_ld_ident(entity, id);
1998 declaration->declaration_type = declaration_type;
1999 declaration->v.entity = entity;
2000 set_entity_variability(entity, variability_uninitialized);
2001 /* TODO: visibility? */
2004 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2006 enum compound_graph_entry_type_t {
2007 COMPOUND_GRAPH_ENTRY_ARRAY,
2008 COMPOUND_GRAPH_ENTRY_COMPOUND
2011 struct compound_graph_path_entry_t {
2017 compound_graph_path_entry_t *prev;
2020 static void create_initializer_object(initializer_t *initializer, type_t *type,
2021 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2023 static compound_graph_path *create_compound_path(ir_type *type,
2024 compound_graph_path_entry_t *entry, int len)
2026 compound_graph_path *path = new_compound_graph_path(type, len);
2029 for( ; entry != NULL; entry = entry->prev, --i) {
2031 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2032 set_compound_graph_path_node(path, i, entry->v.entity);
2034 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2035 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2043 static void create_initializer_value(initializer_t *initializer,
2045 compound_graph_path_entry_t *entry,
2048 ir_node *node = expression_to_firm(initializer->v.value);
2049 ir_type *type = get_entity_type(entity);
2050 compound_graph_path *path = create_compound_path(type, entry, len);
2051 add_compound_ent_value_w_path(entity, node, path);
2054 static void create_initializer_compound(initializer_t *initializer,
2055 compound_type_t *type,
2057 compound_graph_path_entry_t *last_entry,
2060 declaration_t *compound_declaration = type->declaration;
2062 declaration_t *compound_entry = compound_declaration->context.declarations;
2064 compound_graph_path_entry_t entry;
2065 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2066 entry.prev = last_entry;
2070 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2071 if(compound_entry->symbol == NULL)
2073 if(compound_entry->namespc != NAMESPACE_NORMAL)
2076 if(i >= initializer->v.list.len)
2079 entry.v.entity = compound_entry->v.entity;
2081 initializer_t *sub_initializer = initializer->v.list.initializers[i];
2083 assert(compound_entry != NULL);
2084 assert(compound_entry->declaration_type
2085 == DECLARATION_TYPE_COMPOUND_MEMBER);
2087 if(sub_initializer->type == INITIALIZER_VALUE) {
2088 create_initializer_value(sub_initializer, entity, &entry, len);
2090 type_t *type = skip_typeref(compound_entry->type);
2091 create_initializer_object(sub_initializer, type, entity, &entry,
2099 static void create_initializer_array(initializer_t *initializer,
2100 array_type_t *type, ir_entity *entity,
2101 compound_graph_path_entry_t *last_entry,
2104 type_t *element_type = type->element_type;
2105 element_type = skip_typeref(element_type);
2107 compound_graph_path_entry_t entry;
2108 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2109 entry.prev = last_entry;
2112 for(size_t i = 0; i < initializer->v.list.len; ++i) {
2113 entry.v.array_index = i;
2115 initializer_t *sub_initializer = initializer->v.list.initializers[i];
2117 if(sub_initializer->type == INITIALIZER_VALUE) {
2118 create_initializer_value(sub_initializer, entity, &entry, len);
2120 assert(sub_initializer->type == INITIALIZER_LIST);
2121 create_initializer_object(sub_initializer, element_type, entity,
2127 static void create_initializer_string(initializer_t *initializer,
2128 array_type_t *type, ir_entity *entity,
2129 compound_graph_path_entry_t *last_entry,
2132 type_t *element_type = type->element_type;
2133 element_type = skip_typeref(element_type);
2135 compound_graph_path_entry_t entry;
2136 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2137 entry.prev = last_entry;
2140 ir_type *irtype = get_entity_type(entity);
2141 size_t arr_len = get_array_type_size(type);
2142 const char *p = initializer->v.string;
2144 for(i = 0; i < arr_len; ++i, ++p) {
2145 entry.v.array_index = i;
2147 ir_node *node = new_Const_long(mode_Bs, *p);
2148 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2149 add_compound_ent_value_w_path(entity, node, path);
2156 static void create_initializer_object(initializer_t *initializer, type_t *type,
2157 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2159 if(type->type == TYPE_ARRAY) {
2160 array_type_t *array_type = (array_type_t*) type;
2162 if(initializer->type == INITIALIZER_STRING) {
2163 create_initializer_string(initializer, array_type, entity, entry, len);
2165 assert(initializer->type == INITIALIZER_LIST);
2166 create_initializer_array(initializer, array_type, entity, entry, len);
2169 assert(initializer->type == INITIALIZER_LIST);
2171 assert(type->type == TYPE_COMPOUND_STRUCT
2172 || type->type == TYPE_COMPOUND_UNION);
2173 compound_type_t *compound_type = (compound_type_t*) type;
2174 create_initializer_compound(initializer, compound_type, entity, entry, len);
2178 static void create_initializer_local_variable_entity(declaration_t *declaration)
2180 initializer_t *initializer = declaration->init.initializer;
2181 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2182 ir_entity *entity = declaration->v.entity;
2183 ir_node *memory = get_store();
2184 ir_node *nomem = new_NoMem();
2185 ir_node *frame = get_irg_frame(current_ir_graph);
2186 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2188 if(is_atomic_entity(entity)) {
2189 assert(initializer->type == INITIALIZER_VALUE);
2191 ir_node *value = expression_to_firm(initializer->v.value);
2192 ir_node *store = new_d_Store(dbgi, memory, addr, value);
2193 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2194 set_store(store_mem);
2198 /* create a "template" entity which is copied to the entity on the stack */
2199 ident *id = unique_ident("initializer");
2200 ir_type *irtype = get_ir_type(declaration->type);
2201 ir_type *global_type = get_glob_type();
2202 ir_entity *init_entity = new_entity(global_type, id, irtype);
2203 set_entity_ld_ident(init_entity, id);
2205 set_entity_variability(init_entity, variability_initialized);
2206 set_entity_visibility(init_entity, visibility_local);
2208 ir_graph *old_current_ir_graph = current_ir_graph;
2209 current_ir_graph = get_const_code_irg();
2211 type_t *type = skip_typeref(declaration->type);
2212 create_initializer_object(initializer, type, init_entity, NULL, 0);
2214 assert(current_ir_graph == get_const_code_irg());
2215 current_ir_graph = old_current_ir_graph;
2217 ir_node *src_addr = create_symconst(dbgi, init_entity);
2218 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2220 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2221 set_store(copyb_mem);
2224 static void create_initializer(declaration_t *declaration)
2226 initializer_t *initializer = declaration->init.initializer;
2227 if(initializer == NULL)
2230 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2231 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2232 create_initializer_local_variable_entity(declaration);
2236 if(initializer->type == INITIALIZER_VALUE) {
2237 ir_node *value = expression_to_firm(initializer->v.value);
2239 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2240 set_value(declaration->v.value_number, value);
2242 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2244 ir_entity *entity = declaration->v.entity;
2246 set_entity_variability(entity, variability_initialized);
2247 set_atomic_ent_value(entity, value);
2250 declaration_type_t declaration_type = (declaration_type_t)declaration->declaration_type;
2251 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2252 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2254 ir_entity *entity = declaration->v.entity;
2255 set_entity_variability(entity, variability_initialized);
2257 type_t *type = skip_typeref(declaration->type);
2258 create_initializer_object(initializer, type, entity, NULL, 0);
2262 static void create_local_variable(declaration_t *declaration)
2264 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2266 bool needs_entity = declaration->address_taken;
2267 type_t *type = skip_typeref(declaration->type);
2269 if(type->type == TYPE_ARRAY
2270 || type->type == TYPE_COMPOUND_STRUCT
2271 || type->type == TYPE_COMPOUND_UNION) {
2272 needs_entity = true;
2276 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2277 create_declaration_entity(declaration,
2278 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2281 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2282 declaration->v.value_number = next_value_number_function;
2283 ++next_value_number_function;
2286 create_initializer(declaration);
2289 static void create_local_static_variable(declaration_t *declaration)
2291 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2293 type_t *type = skip_typeref(declaration->type);
2294 ir_type *global_type = get_glob_type();
2295 ident *id = unique_ident(declaration->symbol->string);
2296 ir_type *irtype = get_ir_type(type);
2297 ir_entity *entity = new_entity(global_type, id, irtype);
2298 set_entity_ld_ident(entity, id);
2300 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2301 declaration->v.entity = entity;
2302 set_entity_variability(entity, variability_uninitialized);
2303 set_entity_visibility(entity, visibility_local);
2305 ir_graph *old_current_ir_graph = current_ir_graph;
2306 current_ir_graph = get_const_code_irg();
2308 create_initializer(declaration);
2310 assert(current_ir_graph == get_const_code_irg());
2311 current_ir_graph = old_current_ir_graph;
2314 static void declaration_statement_to_firm(statement_t *statement)
2316 declaration_t *declaration = statement->v.declaration_stmt.begin;
2317 declaration_t *end = statement->v.declaration_stmt.end->next;
2318 for( ; declaration != end; declaration = declaration->next) {
2319 type_t *type = declaration->type;
2321 switch(declaration->storage_class) {
2322 case STORAGE_CLASS_TYPEDEF:
2324 case STORAGE_CLASS_STATIC:
2325 create_local_static_variable(declaration);
2327 case STORAGE_CLASS_ENUM_ENTRY:
2328 panic("enum entry declaration in local block found");
2329 case STORAGE_CLASS_EXTERN:
2330 panic("extern declaration in local block found");
2331 case STORAGE_CLASS_NONE:
2332 case STORAGE_CLASS_AUTO:
2333 case STORAGE_CLASS_REGISTER:
2334 if(type->type == TYPE_FUNCTION) {
2335 panic("nested functions not supported yet");
2337 create_local_variable(declaration);
2341 panic("invalid storage class found");
2345 static void create_jump_statement(const statement_t *statement,
2346 ir_node *target_block)
2348 if(get_cur_block() == NULL)
2351 dbg_info *dbgi = get_dbg_info(&statement->source_position);
2352 ir_node *jump = new_d_Jmp(dbgi);
2353 add_immBlock_pred(target_block, jump);
2355 set_cur_block(NULL);
2358 static void switch_statement_to_firm(const statement_t *statement)
2360 dbg_info *dbgi = get_dbg_info(&statement->source_position);
2362 ir_node *expression = expression_to_firm(statement->v.switch_stmt.expression);
2363 ir_node *cond = new_d_Cond(dbgi, expression);
2364 ir_node *break_block = new_immBlock();
2366 set_cur_block(NULL);
2368 ir_node *const old_switch_cond = current_switch_cond;
2369 ir_node *const old_break_label = break_label;
2370 const bool old_saw_default_label = saw_default_label;
2371 current_switch_cond = cond;
2372 break_label = break_block;
2374 statement_to_firm(statement->v.switch_stmt.body);
2376 if(get_cur_block() != NULL) {
2377 ir_node *jmp = new_Jmp();
2378 add_immBlock_pred(break_block, jmp);
2381 if (!saw_default_label) {
2382 set_cur_block(get_nodes_block(cond));
2383 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2384 MAGIC_DEFAULT_PN_NUMBER);
2385 add_immBlock_pred(break_block, proj);
2388 assert(current_switch_cond == cond);
2389 assert(break_label == break_block);
2390 current_switch_cond = old_switch_cond;
2391 break_label = old_break_label;
2392 saw_default_label = old_saw_default_label;
2394 mature_immBlock(break_block);
2395 set_cur_block(break_block);
2398 static long fold_constant(const expression_t *expression)
2400 ir_graph *old_current_ir_graph = current_ir_graph;
2401 current_ir_graph = get_const_code_irg();
2403 ir_node *cnst = expression_to_firm(expression);
2404 if(!is_Const(cnst)) {
2405 panic("couldn't fold constant");
2407 tarval *tv = get_Const_tarval(cnst);
2408 if(!tarval_is_long(tv)) {
2409 panic("folded constant not an integer");
2412 long res = get_tarval_long(tv);
2414 current_ir_graph = old_current_ir_graph;
2418 static void case_label_to_firm(const statement_t *statement)
2420 dbg_info *dbgi = get_dbg_info(&statement->source_position);
2422 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2424 /* let's create a node and hope firm constant folding creates a Const
2427 set_cur_block(get_nodes_block(current_switch_cond));
2428 if(statement->v.case_label_stmt.expression) {
2429 long pn = fold_constant(statement->v.case_label_stmt.expression);
2430 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2431 /* oops someone detected our cheating... */
2432 panic("magic default pn used");
2434 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2436 saw_default_label = true;
2437 proj = new_d_defaultProj(dbgi, current_switch_cond,
2438 MAGIC_DEFAULT_PN_NUMBER);
2441 ir_node *block = new_immBlock();
2442 if (fallthrough != NULL) {
2443 add_immBlock_pred(block, fallthrough);
2445 add_immBlock_pred(block, proj);
2446 mature_immBlock(block);
2448 statement_to_firm(statement->v.case_label_stmt.label_statement);
2451 static ir_node *get_label_block(declaration_t *label)
2453 assert(label->namespc == NAMESPACE_LABEL);
2455 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2456 return label->v.block;
2458 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2460 ir_node *old_cur_block = get_cur_block();
2461 ir_node *block = new_immBlock();
2462 set_cur_block(old_cur_block);
2464 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2465 label->v.block = block;
2467 ARR_APP1(ir_node *, imature_blocks, block);
2472 static void label_to_firm(const statement_t *statement)
2474 ir_node *block = get_label_block(statement->v.label_stmt.label);
2476 if(get_cur_block() != NULL) {
2477 ir_node *jmp = new_Jmp();
2478 add_immBlock_pred(block, jmp);
2481 set_cur_block(block);
2484 statement_to_firm(statement->v.label_stmt.label_statement);
2487 static void goto_to_firm(const statement_t *statement)
2489 if(get_cur_block() == NULL)
2492 ir_node *block = get_label_block(statement->v.goto_label);
2493 ir_node *jmp = new_Jmp();
2494 add_immBlock_pred(block, jmp);
2496 set_cur_block(NULL);
2499 static void statement_to_firm(statement_t *statement)
2501 switch(statement->type) {
2502 case STATEMENT_COMPOUND:
2503 compound_statement_to_firm(statement);
2505 case STATEMENT_RETURN:
2506 return_statement_to_firm(statement);
2508 case STATEMENT_EXPRESSION:
2509 expression_statement_to_firm(statement);
2512 if_statement_to_firm(statement);
2514 case STATEMENT_WHILE:
2515 while_statement_to_firm(statement);
2517 case STATEMENT_DO_WHILE:
2518 do_while_statement_to_firm(statement);
2520 case STATEMENT_DECLARATION:
2521 declaration_statement_to_firm(statement);
2523 case STATEMENT_BREAK:
2524 create_jump_statement(statement, break_label);
2526 case STATEMENT_CONTINUE:
2527 create_jump_statement(statement, continue_label);
2529 case STATEMENT_SWITCH:
2530 switch_statement_to_firm(statement);
2532 case STATEMENT_CASE_LABEL:
2533 case_label_to_firm(statement);
2536 for_statement_to_firm(statement);
2538 case STATEMENT_LABEL:
2539 label_to_firm(statement);
2541 case STATEMENT_GOTO:
2542 goto_to_firm(statement);
2547 panic("Statement not implemented\n");
2550 static int count_local_declarations(const declaration_t * decl,
2551 const declaration_t *const end)
2554 for (; decl != end; decl = decl->next) {
2555 const type_t *type = skip_typeref(decl->type);
2556 switch (type->type) {
2560 if (!decl->address_taken) ++count;
2569 static int count_decls_in_stmts(const statement_t *stmt)
2572 for (; stmt != NULL; stmt = stmt->next) {
2573 switch (stmt->type) {
2574 case STATEMENT_DECLARATION:
2575 count += count_local_declarations(stmt->v.declaration_stmt.begin,
2576 stmt->v.declaration_stmt.end->next);
2579 case STATEMENT_COMPOUND:
2580 count += count_decls_in_stmts(stmt->v.compound_stmt.statements);
2584 count += count_decls_in_stmts(stmt->v.if_stmt.true_statement);
2585 count += count_decls_in_stmts(stmt->v.if_stmt.false_statement);
2588 case STATEMENT_SWITCH:
2589 count += count_decls_in_stmts(stmt->v.switch_stmt.body);
2592 case STATEMENT_LABEL:
2593 count += count_decls_in_stmts(stmt->v.label_stmt.label_statement);
2596 case STATEMENT_WHILE:
2597 count += count_decls_in_stmts(stmt->v.while_stmt.body);
2600 case STATEMENT_DO_WHILE:
2601 count += count_decls_in_stmts(stmt->v.while_stmt.body);
2604 case STATEMENT_FOR: {
2605 /* TODO initialisation */
2606 count += count_decls_in_stmts(stmt->v.for_stmt.body);
2610 case STATEMENT_BREAK:
2611 case STATEMENT_CASE_LABEL:
2612 case STATEMENT_CONTINUE:
2613 case STATEMENT_EXPRESSION:
2614 case STATEMENT_GOTO:
2615 case STATEMENT_INVALID:
2616 case STATEMENT_RETURN:
2623 static int get_function_n_local_vars(declaration_t *declaration)
2627 /* count parameters */
2628 count += count_local_declarations(declaration->context.declarations, NULL);
2630 /* count local variables declared in body */
2631 count += count_decls_in_stmts(declaration->init.statement);
2633 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2634 * for expression statements... */
2640 static void initialize_function_parameters(declaration_t *declaration)
2642 ir_graph *irg = current_ir_graph;
2643 ir_node *args = get_irg_args(irg);
2644 ir_node *start_block = get_irg_start_block(irg);
2645 ir_type *function_irtype = get_ir_type(declaration->type);
2648 declaration_t *parameter = declaration->context.declarations;
2649 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2650 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2651 type_t *type = skip_typeref(parameter->type);
2653 bool needs_entity = parameter->address_taken;
2654 if(type->type == TYPE_COMPOUND_STRUCT
2655 || type->type == TYPE_COMPOUND_UNION) {
2656 needs_entity = true;
2660 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2661 ident *id = new_id_from_str(parameter->symbol->string);
2662 set_entity_ident(entity, id);
2664 parameter->declaration_type
2665 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2666 parameter->v.entity = entity;
2670 ir_mode *mode = get_ir_mode(parameter->type);
2672 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2674 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2675 parameter->v.value_number = next_value_number_function;
2676 ++next_value_number_function;
2678 set_value(parameter->v.value_number, proj);
2682 static void create_function(declaration_t *declaration)
2684 ir_entity *entity = get_function_entity(declaration);
2686 if(declaration->init.statement == NULL)
2689 current_function_decl = declaration;
2690 current_function_name = NULL;
2692 assert(imature_blocks == NULL);
2693 imature_blocks = NEW_ARR_F(ir_node*, 0);
2695 int n_local_vars = get_function_n_local_vars(declaration);
2696 ir_graph *irg = new_ir_graph(entity, n_local_vars);
2697 ir_node *first_block = get_cur_block();
2699 next_value_number_function = 0;
2700 initialize_function_parameters(declaration);
2702 statement_to_firm(declaration->init.statement);
2704 ir_node *end_block = get_irg_end_block(irg);
2706 /* do we have a return statement yet? */
2707 if(get_cur_block() != NULL) {
2708 assert(declaration->type->type == TYPE_FUNCTION);
2709 const function_type_t* const func_type
2710 = (const function_type_t*) declaration->type;
2712 if (func_type->result_type == type_void) {
2713 ret = new_Return(get_store(), 0, NULL);
2715 ir_mode *const mode = get_ir_mode(func_type->result_type);
2717 // ยง5.1.2.2.3 main implicitly returns 0
2718 if (strcmp(declaration->symbol->string, "main") == 0) {
2719 in[0] = new_Const(mode, get_mode_null(mode));
2721 in[0] = new_Unknown(mode);
2723 ret = new_Return(get_store(), 1, in);
2725 add_immBlock_pred(end_block, ret);
2728 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
2729 mature_immBlock(imature_blocks[i]);
2731 DEL_ARR_F(imature_blocks);
2732 imature_blocks = NULL;
2734 mature_immBlock(first_block);
2735 mature_immBlock(end_block);
2737 irg_finalize_cons(irg);
2739 /* finalize the frame type */
2740 ir_type *frame_type = get_irg_frame_type(irg);
2741 int n = get_compound_n_members(frame_type);
2744 for(int i = 0; i < n; ++i) {
2745 ir_entity *entity = get_compound_member(frame_type, i);
2746 ir_type *entity_type = get_entity_type(entity);
2748 int align = get_type_alignment_bytes(entity_type);
2749 if(align > align_all)
2753 misalign = offset % align;
2755 offset += align - misalign;
2759 set_entity_offset(entity, offset);
2760 offset += get_type_size_bytes(entity_type);
2762 set_type_size_bytes(frame_type, offset);
2763 set_type_alignment_bytes(frame_type, align_all);
2764 set_type_state(frame_type, layout_fixed);
2769 static void create_global_variable(declaration_t *declaration)
2771 ir_type *global_type = get_glob_type();
2772 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2775 ir_entity *entity = declaration->v.entity;
2776 if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2777 set_entity_visibility(entity, visibility_local);
2778 } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2779 set_entity_visibility(entity, visibility_external_allocated);
2781 set_entity_visibility(entity, visibility_external_visible);
2783 current_ir_graph = get_const_code_irg();
2784 create_initializer(declaration);
2787 static void context_to_firm(context_t *context)
2789 /* first pass: create declarations */
2790 declaration_t *declaration = context->declarations;
2791 for( ; declaration != NULL; declaration = declaration->next) {
2792 if(declaration->namespc != NAMESPACE_NORMAL)
2794 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2795 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2797 if(declaration->symbol == NULL)
2800 type_t *type = declaration->type;
2801 if(type->type == TYPE_FUNCTION) {
2802 get_function_entity(declaration);
2804 create_global_variable(declaration);
2808 /* second pass: create code */
2809 declaration = context->declarations;
2810 for( ; declaration != NULL; declaration = declaration->next) {
2811 if(declaration->namespc != NAMESPACE_NORMAL)
2813 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2814 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2816 if(declaration->symbol == NULL)
2819 type_t *type = declaration->type;
2820 if(type->type != TYPE_FUNCTION)
2823 create_function(declaration);
2827 void translation_unit_to_firm(translation_unit_t *unit)
2829 /* just to be sure */
2830 continue_label = NULL;
2832 current_switch_cond = NULL;
2834 context_to_firm(& unit->context);