9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
15 #include "adt/array.h"
21 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
23 static ir_type *ir_type_const_char;
24 static ir_type *ir_type_wchar_t;
25 static ir_type *ir_type_void;
26 static ir_type *ir_type_int;
28 static type_t *type_const_char;
29 static type_t *type_void;
30 static type_t *type_int;
32 static int next_value_number_function;
33 static ir_node *continue_label;
34 static ir_node *break_label;
35 static ir_node *current_switch_cond;
36 static bool saw_default_label;
37 static ir_node **imature_blocks;
39 static const declaration_t *current_function_decl;
40 static ir_node *current_function_name;
42 typedef enum declaration_type_t {
43 DECLARATION_TYPE_UNKNOWN,
44 DECLARATION_TYPE_FUNCTION,
45 DECLARATION_TYPE_GLOBAL_VARIABLE,
46 DECLARATION_TYPE_LOCAL_VARIABLE,
47 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
48 DECLARATION_TYPE_COMPOUND_MEMBER,
49 DECLARATION_TYPE_LABEL_BLOCK,
50 DECLARATION_TYPE_ENUM_ENTRY
53 static ir_type *get_ir_type(type_t *type);
55 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
59 const declaration_t *declaration = & value_numbers[pos]->declaration;
61 print_warning_prefix(declaration->source_position);
62 fprintf(stderr, "variable '%s' might be used uninitialized\n",
63 declaration->symbol->string);
65 fprintf(stderr, "Some variable might be used uninitialized\n");
66 return new_r_Unknown(irg, mode);
69 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
71 const source_position_t *pos = (const source_position_t*) dbg;
74 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
78 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
80 const source_position_t *pos = (const source_position_t*) dbg;
85 return pos->input_name;
88 void init_ast2firm(void)
92 void exit_ast2firm(void)
96 static unsigned unique_id = 0;
98 static ident *unique_ident(const char *tag)
102 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
104 return new_id_from_str(buf);
107 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
109 switch(atomic_type->atype) {
110 case ATOMIC_TYPE_SCHAR:
111 case ATOMIC_TYPE_CHAR:
113 case ATOMIC_TYPE_UCHAR:
115 case ATOMIC_TYPE_SHORT:
117 case ATOMIC_TYPE_USHORT:
119 case ATOMIC_TYPE_LONG:
120 case ATOMIC_TYPE_INT:
122 case ATOMIC_TYPE_ULONG:
123 case ATOMIC_TYPE_UINT:
125 case ATOMIC_TYPE_LONGLONG:
127 case ATOMIC_TYPE_ULONGLONG:
129 case ATOMIC_TYPE_FLOAT:
131 case ATOMIC_TYPE_DOUBLE:
133 case ATOMIC_TYPE_LONG_DOUBLE:
135 case ATOMIC_TYPE_BOOL:
137 #ifdef PROVIDE_COMPLEX
138 case ATOMIC_TYPE_FLOAT_COMPLEX:
139 case ATOMIC_TYPE_DOUBLE_COMPLEX:
140 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
141 panic("complex lowering not implemented yet");
143 case ATOMIC_TYPE_FLOAT_IMAGINARY:
144 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
145 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
146 panic("imaginary lowering not implemented yet");
149 case ATOMIC_TYPE_VOID:
150 /* firm has no real void... */
152 case ATOMIC_TYPE_INVALID:
155 panic("Encountered unknown atomic type");
159 static unsigned get_type_size(type_t *type);
161 static unsigned get_atomic_type_size(const atomic_type_t *type)
163 switch(type->atype) {
164 case ATOMIC_TYPE_CHAR:
165 case ATOMIC_TYPE_SCHAR:
166 case ATOMIC_TYPE_UCHAR:
169 case ATOMIC_TYPE_SHORT:
170 case ATOMIC_TYPE_USHORT:
173 case ATOMIC_TYPE_BOOL:
174 case ATOMIC_TYPE_INT:
175 case ATOMIC_TYPE_UINT:
176 case ATOMIC_TYPE_LONG:
177 case ATOMIC_TYPE_ULONG:
178 case ATOMIC_TYPE_FLOAT:
181 case ATOMIC_TYPE_LONGLONG:
182 case ATOMIC_TYPE_ULONGLONG:
183 case ATOMIC_TYPE_DOUBLE:
186 case ATOMIC_TYPE_LONG_DOUBLE:
189 case ATOMIC_TYPE_VOID:
192 case ATOMIC_TYPE_INVALID:
195 panic("Trying to determine size of invalid atomic type");
198 static unsigned get_compound_type_size(compound_type_t *type)
200 ir_type *irtype = get_ir_type((type_t*) type);
201 return get_type_size_bytes(irtype);
204 static unsigned get_array_type_size(array_type_t *type)
206 ir_type *irtype = get_ir_type((type_t*) type);
207 return get_type_size_bytes(irtype);
210 static unsigned get_type_size(type_t *type)
212 type = skip_typeref(type);
216 return get_atomic_type_size(&type->atomic);
218 return get_mode_size_bytes(mode_Is);
219 case TYPE_COMPOUND_UNION:
220 case TYPE_COMPOUND_STRUCT:
221 return get_compound_type_size(&type->compound);
223 /* just a pointer to the function */
224 return get_mode_size_bytes(mode_P_code);
226 return get_mode_size_bytes(mode_P_data);
228 return get_array_type_size(&type->array);
230 return get_type_size(type->builtin.real_type);
236 panic("Trying to determine size of invalid type");
239 static unsigned count_parameters(const function_type_t *function_type)
243 function_parameter_t *parameter = function_type->parameters;
244 for ( ; parameter != NULL; parameter = parameter->next) {
254 static long fold_constant(const expression_t *expression);
256 static ir_type *create_atomic_type(const atomic_type_t *type)
258 ir_mode *mode = get_atomic_mode(type);
259 ident *id = get_mode_ident(mode);
260 ir_type *irtype = new_type_primitive(id, mode);
265 static ir_type *create_method_type(const function_type_t *function_type)
267 type_t *return_type = function_type->return_type;
269 ident *id = unique_ident("functiontype");
270 int n_parameters = count_parameters(function_type);
271 int n_results = return_type == type_void ? 0 : 1;
272 ir_type *irtype = new_type_method(id, n_parameters, n_results);
274 if(return_type != type_void) {
275 ir_type *restype = get_ir_type(return_type);
276 set_method_res_type(irtype, 0, restype);
279 function_parameter_t *parameter = function_type->parameters;
281 for( ; parameter != NULL; parameter = parameter->next) {
282 ir_type *p_irtype = get_ir_type(parameter->type);
283 set_method_param_type(irtype, n, p_irtype);
287 if(function_type->variadic || function_type->unspecified_parameters) {
288 set_method_variadicity(irtype, variadicity_variadic);
294 static ir_type *create_pointer_type(pointer_type_t *type)
296 type_t *points_to = type->points_to;
297 ir_type *ir_points_to;
298 /* Avoid endless recursion if the points_to type contains this poiner type
299 * again (might be a struct). We therefore first create a void* pointer
300 * and then set the real points_to type
302 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
303 ir_type_void, mode_P_data);
304 type->type.firm_type = ir_type;
306 ir_points_to = get_ir_type(points_to);
307 set_pointer_points_to_type(ir_type, ir_points_to);
312 static ir_type *create_array_type(array_type_t *type)
314 type_t *element_type = type->element_type;
315 ir_type *ir_element_type = get_ir_type(element_type);
317 ident *id = unique_ident("array");
318 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
320 if(type->size != NULL) {
321 int n_elements = fold_constant(type->size);
323 set_array_bounds_int(ir_type, 0, 0, n_elements);
325 size_t elemsize = get_type_size_bytes(ir_element_type);
326 int align = get_type_alignment_bytes(ir_element_type);
327 if(elemsize % align > 0) {
328 elemsize += align - (elemsize % align);
330 set_type_size_bytes(ir_type, n_elements * elemsize);
331 set_type_alignment_bytes(ir_type, align);
332 set_type_state(ir_type, layout_fixed);
338 #define INVALID_TYPE ((ir_type_ptr)-1)
340 static ir_type *create_struct_type(compound_type_t *type)
342 symbol_t *symbol = type->declaration->symbol;
345 id = unique_ident(symbol->string);
347 id = unique_ident("__anonymous_struct");
349 ir_type *ir_type = new_type_struct(id);
351 type->type.firm_type = ir_type;
355 declaration_t *entry = type->declaration->context.declarations;
356 for( ; entry != NULL; entry = entry->next) {
357 if(entry->namespc != NAMESPACE_NORMAL)
360 ident *ident = new_id_from_str(entry->symbol->string);
361 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
363 int entry_size = get_type_size_bytes(entry_ir_type);
364 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
365 int misalign = offset % entry_alignment;
367 offset += entry_alignment - misalign;
369 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
370 set_entity_offset(entity, offset);
371 add_struct_member(ir_type, entity);
372 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
373 entry->v.entity = entity;
375 offset += entry_size;
376 if(entry_alignment > align_all) {
377 if(entry_alignment % align_all != 0) {
378 panic("Uneven alignments not supported yet");
380 align_all = entry_alignment;
384 int misalign = offset % align_all;
386 set_type_alignment_bytes(ir_type, align_all);
387 set_type_size_bytes(ir_type, offset);
388 set_type_state(ir_type, layout_fixed);
393 static ir_type *create_union_type(compound_type_t *type)
395 declaration_t *declaration = type->declaration;
396 symbol_t *symbol = declaration->symbol;
399 id = unique_ident(symbol->string);
401 id = unique_ident("__anonymous_union");
403 ir_type *ir_type = new_type_union(id);
405 type->type.firm_type = ir_type;
409 declaration_t *entry = declaration->context.declarations;
410 for( ; entry != NULL; entry = entry->next) {
411 if(entry->namespc != NAMESPACE_NORMAL)
414 ident *ident = new_id_from_str(entry->symbol->string);
415 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
417 int entry_size = get_type_size_bytes(entry_ir_type);
418 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
420 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
421 add_union_member(ir_type, entity);
422 set_entity_offset(entity, 0);
423 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
424 entry->v.entity = entity;
426 if(entry_size > size) {
429 if(entry_alignment > align_all) {
430 if(entry_alignment % align_all != 0) {
431 panic("Uneven alignments not supported yet");
433 align_all = entry_alignment;
437 set_type_alignment_bytes(ir_type, align_all);
438 set_type_size_bytes(ir_type, size);
439 set_type_state(ir_type, layout_fixed);
444 static ir_node *expression_to_firm(const expression_t *expression);
445 static inline ir_mode *get_ir_mode(type_t *type);
447 static ir_type *create_enum_type(enum_type_t *const type)
449 type->type.firm_type = ir_type_int;
451 ir_mode *const mode = get_ir_mode((type_t*) type);
452 tarval *const one = get_mode_one(mode);
453 tarval * tv_next = get_tarval_null(mode);
455 declaration_t *declaration = type->declaration->next;
456 for (; declaration != NULL; declaration = declaration->next) {
457 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
460 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
462 expression_t *const init = declaration->init.enum_value;
464 ir_node *const cnst = expression_to_firm(init);
465 if (!is_Const(cnst)) {
466 panic("couldn't fold constant");
468 tv_next = get_Const_tarval(cnst);
470 declaration->v.enum_val = tv_next;
471 tv_next = tarval_add(tv_next, one);
477 static ir_type *get_ir_type(type_t *type)
479 assert(type != NULL);
481 type = skip_typeref(type);
483 if(type->base.firm_type != NULL) {
484 assert(type->base.firm_type != INVALID_TYPE);
485 return type->base.firm_type;
488 ir_type *firm_type = NULL;
491 firm_type = create_atomic_type(&type->atomic);
494 firm_type = create_method_type(&type->function);
497 firm_type = create_pointer_type(&type->pointer);
500 firm_type = create_array_type(&type->array);
502 case TYPE_COMPOUND_STRUCT:
503 firm_type = create_struct_type(&type->compound);
505 case TYPE_COMPOUND_UNION:
506 firm_type = create_union_type(&type->compound);
509 firm_type = create_enum_type(&type->enumt);
512 firm_type = get_ir_type(type->builtin.real_type);
519 if(firm_type == NULL)
520 panic("unknown type found");
522 type->base.firm_type = firm_type;
526 static inline ir_mode *get_ir_mode(type_t *type)
528 ir_type *irtype = get_ir_type(type);
530 /* firm doesn't report a mode for arrays somehow... */
531 if(is_Array_type(irtype)) {
535 ir_mode *mode = get_type_mode(irtype);
536 assert(mode != NULL);
540 static ir_entity* get_function_entity(declaration_t *declaration)
542 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
543 return declaration->v.entity;
544 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
546 symbol_t *symbol = declaration->symbol;
547 ident *id = new_id_from_str(symbol->string);
549 ir_type *global_type = get_glob_type();
550 ir_type *ir_type_method = get_ir_type(declaration->type);
551 assert(is_Method_type(ir_type_method));
553 ir_entity *entity = new_entity(global_type, id, ir_type_method);
554 set_entity_ld_ident(entity, id);
555 if(declaration->storage_class == STORAGE_CLASS_STATIC
556 || declaration->is_inline) {
557 set_entity_visibility(entity, visibility_local);
558 } else if(declaration->init.statement != NULL) {
559 set_entity_visibility(entity, visibility_external_visible);
561 set_entity_visibility(entity, visibility_external_allocated);
564 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
565 declaration->v.entity = entity;
570 static dbg_info *get_dbg_info(const source_position_t *pos)
572 return (dbg_info*) pos;
575 static ir_node *const_to_firm(const const_expression_t *cnst)
577 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
578 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
583 if(mode_is_float(mode)) {
584 tv = new_tarval_from_double(cnst->v.float_value, mode);
586 if(mode_is_signed(mode)) {
587 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
589 len = snprintf(buf, sizeof(buf), "%llu",
590 (unsigned long long) cnst->v.int_value);
592 tv = new_tarval_from_str(buf, len, mode);
595 return new_d_Const(dbgi, mode, tv);
598 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
600 assert(entity != NULL);
601 union symconst_symbol sym;
602 sym.entity_p = entity;
603 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
606 static ir_node *string_to_firm(const source_position_t *const src_pos,
607 const char *const id_prefix,
608 const char *const string)
610 ir_type *const global_type = get_glob_type();
611 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
614 ident *const id = unique_ident(id_prefix);
615 ir_entity *const entity = new_entity(global_type, id, type);
616 set_entity_ld_ident(entity, id);
617 set_entity_variability(entity, variability_constant);
619 ir_type *const elem_type = ir_type_const_char;
620 ir_mode *const mode = get_type_mode(elem_type);
622 const size_t slen = strlen(string) + 1;
624 set_array_lower_bound_int(type, 0, 0);
625 set_array_upper_bound_int(type, 0, slen);
626 set_type_size_bytes(type, slen);
627 set_type_state(type, layout_fixed);
629 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
630 for(size_t i = 0; i < slen; ++i) {
631 tvs[i] = new_tarval_from_long(string[i], mode);
634 set_array_entity_values(entity, tvs, slen);
637 dbg_info *const dbgi = get_dbg_info(src_pos);
639 return create_symconst(dbgi, entity);
642 static ir_node *string_literal_to_firm(
643 const string_literal_expression_t* literal)
645 return string_to_firm(&literal->expression.source_position, "Lstr",
649 static ir_node *wide_string_literal_to_firm(
650 const wide_string_literal_expression_t* const literal)
652 ir_type *const global_type = get_glob_type();
653 ir_type *const elem_type = ir_type_wchar_t;
654 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
657 ident *const id = unique_ident("Lstr");
658 ir_entity *const entity = new_entity(global_type, id, type);
659 set_entity_ld_ident(entity, id);
660 set_entity_variability(entity, variability_constant);
662 ir_mode *const mode = get_type_mode(elem_type);
664 const wchar_rep_t *const string = literal->value.begin;
665 const size_t slen = literal->value.size;
667 set_array_lower_bound_int(type, 0, 0);
668 set_array_upper_bound_int(type, 0, slen);
669 set_type_size_bytes(type, slen);
670 set_type_state(type, layout_fixed);
672 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
673 for(size_t i = 0; i < slen; ++i) {
674 tvs[i] = new_tarval_from_long(string[i], mode);
677 set_array_entity_values(entity, tvs, slen);
680 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
682 return create_symconst(dbgi, entity);
685 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
686 dbg_info *const dbgi)
688 if(is_compound_type(irtype) || is_Array_type(irtype)) {
692 ir_mode *const mode = get_type_mode(irtype);
693 ir_node *const memory = get_store();
694 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
695 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
696 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
701 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
703 ir_mode *mode = get_irn_mode(node);
705 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
707 if(!mode_is_float(mode))
710 /* check if there is already a Conv */
711 if (get_irn_op(node) == op_Conv) {
712 /* convert it into a strict Conv */
713 set_Conv_strict(node, 1);
717 /* otherwise create a new one */
718 return new_d_strictConv(dbgi, node, mode);
721 static ir_node *get_global_var_address(dbg_info *const dbgi,
722 const declaration_t *const decl)
724 assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
726 ir_entity *const entity = decl->v.entity;
727 switch ((storage_class_tag_t)decl->storage_class) {
728 case STORAGE_CLASS_THREAD:
729 case STORAGE_CLASS_THREAD_EXTERN:
730 case STORAGE_CLASS_THREAD_STATIC: {
731 ir_node *const no_mem = new_NoMem();
732 ir_node *const tls = get_irg_tls(current_ir_graph);
733 return new_d_simpleSel(dbgi, no_mem, tls, entity);
737 return create_symconst(dbgi, entity);
741 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
743 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
744 declaration_t *declaration = ref->declaration;
745 type_t *type = skip_typeref(declaration->type);
747 switch((declaration_type_t) declaration->declaration_type) {
748 case DECLARATION_TYPE_UNKNOWN:
749 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
755 case DECLARATION_TYPE_ENUM_ENTRY: {
756 ir_mode *const mode = get_ir_mode(type);
757 return new_Const(mode, declaration->v.enum_val);
760 case DECLARATION_TYPE_LOCAL_VARIABLE: {
761 ir_mode *mode = get_ir_mode(type);
762 return get_value(declaration->v.value_number, mode);
764 case DECLARATION_TYPE_FUNCTION: {
765 return create_symconst(dbgi, declaration->v.entity);
767 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
768 ir_node *const addr = get_global_var_address(dbgi, declaration);
769 ir_type *const irtype = get_entity_type(declaration->v.entity);
770 return deref_address(irtype, addr, dbgi);
773 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
774 ir_entity *entity = declaration->v.entity;
775 ir_node *frame = get_irg_frame(current_ir_graph);
776 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
777 ir_type *irtype = get_entity_type(entity);
778 return deref_address(irtype, sel, dbgi);
781 case DECLARATION_TYPE_COMPOUND_MEMBER:
782 case DECLARATION_TYPE_LABEL_BLOCK:
783 panic("not implemented reference type");
786 panic("reference to declaration with unknown type found");
789 static ir_node *reference_addr(const reference_expression_t *ref)
791 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
792 declaration_t *declaration = ref->declaration;
794 switch((declaration_type_t) declaration->declaration_type) {
795 case DECLARATION_TYPE_UNKNOWN:
797 case DECLARATION_TYPE_LOCAL_VARIABLE:
798 panic("local variable without entity has no address");
799 case DECLARATION_TYPE_FUNCTION: {
800 return create_symconst(dbgi, declaration->v.entity);
802 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
803 ir_node *const addr = get_global_var_address(dbgi, declaration);
806 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
807 ir_entity *entity = declaration->v.entity;
808 ir_node *frame = get_irg_frame(current_ir_graph);
809 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
814 case DECLARATION_TYPE_ENUM_ENTRY:
815 panic("trying to reference enum entry");
817 case DECLARATION_TYPE_COMPOUND_MEMBER:
818 case DECLARATION_TYPE_LABEL_BLOCK:
819 panic("not implemented reference type");
822 panic("reference to declaration with unknown type found");
825 static ir_node *process_builtin_call(const call_expression_t *call)
827 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
829 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
830 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
832 type_t *type = skip_typeref(builtin->expression.datatype);
833 assert(is_type_pointer(type));
835 type_t *function_type = skip_typeref(type->pointer.points_to);
836 symbol_t *symbol = builtin->symbol;
839 case T___builtin_alloca: {
840 if(call->arguments == NULL || call->arguments->next != NULL) {
841 panic("invalid number of parameters on __builtin_alloca");
843 expression_t *argument = call->arguments->expression;
844 ir_node *size = expression_to_firm(argument);
846 ir_node *store = get_store();
847 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
849 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
851 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
855 case T___builtin_nan:
856 case T___builtin_nanf:
857 case T___builtin_nand: {
858 /* Ignore string for now... */
859 assert(is_type_function(function_type));
860 ir_mode *mode = get_ir_mode(function_type->function.return_type);
861 tarval *tv = get_mode_NAN(mode);
862 ir_node *res = new_d_Const(dbgi, mode, tv);
865 case T___builtin_va_end:
868 panic("Unsupported builtin found\n");
872 static ir_node *call_expression_to_firm(const call_expression_t *call)
874 assert(get_cur_block() != NULL);
876 expression_t *function = call->function;
877 if(function->type == EXPR_BUILTIN_SYMBOL) {
878 return process_builtin_call(call);
880 ir_node *callee = expression_to_firm(function);
882 type_t *type = skip_typeref(function->base.datatype);
883 assert(is_type_pointer(type));
884 pointer_type_t *pointer_type = &type->pointer;
885 type_t *points_to = skip_typeref(pointer_type->points_to);
886 assert(is_type_function(points_to));
887 function_type_t *function_type = &points_to->function;
889 int n_parameters = 0;
890 call_argument_t *argument = call->arguments;
891 for( ; argument != NULL; argument = argument->next) {
895 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
897 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
898 ir_type *new_method_type = NULL;
899 if(function_type->variadic || function_type->unspecified_parameters) {
900 /* we need to construct a new method type matching the call
902 int n_res = get_method_n_ress(ir_method_type);
903 new_method_type = new_type_method(unique_ident("calltype"),
904 n_parameters, n_res);
905 set_method_calling_convention(new_method_type,
906 get_method_calling_convention(ir_method_type));
907 set_method_additional_properties(new_method_type,
908 get_method_additional_properties(ir_method_type));
910 for(int i = 0; i < n_res; ++i) {
911 set_method_res_type(new_method_type, i,
912 get_method_res_type(ir_method_type, i));
915 ir_node *in[n_parameters];
917 argument = call->arguments;
919 for( ; argument != NULL; argument = argument->next) {
920 expression_t *expression = argument->expression;
921 ir_node *arg_node = expression_to_firm(expression);
923 arg_node = do_strict_conv(dbgi, arg_node);
926 if(new_method_type != NULL) {
927 ir_type *irtype = get_ir_type(expression->base.datatype);
928 set_method_param_type(new_method_type, n, irtype);
933 assert(n == n_parameters);
935 if(new_method_type != NULL)
936 ir_method_type = new_method_type;
938 ir_node *store = get_store();
939 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
941 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
944 type_t *return_type = skip_typeref(function_type->return_type);
945 ir_node *result = NULL;
947 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
949 if(is_type_scalar(return_type)) {
950 mode = get_ir_mode(return_type);
954 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
955 result = new_d_Proj(dbgi, resproj, mode, 0);
961 static void statement_to_firm(statement_t *statement);
962 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
964 static ir_node *expression_to_addr(const expression_t *expression);
965 static void create_condition_evaluation(const expression_t *expression,
967 ir_node *false_block);
969 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
972 value = do_strict_conv(dbgi, value);
974 ir_node *memory = get_store();
976 if(is_type_scalar(type)) {
977 ir_node *store = new_d_Store(dbgi, memory, addr, value);
978 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
979 set_store(store_mem);
981 ir_type *irtype = get_ir_type(type);
982 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
983 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
984 set_store(copyb_mem);
988 static void set_value_for_expression(const expression_t *expression,
991 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
992 value = do_strict_conv(dbgi, value);
994 if(expression->type == EXPR_REFERENCE) {
995 reference_expression_t *ref = (reference_expression_t*) expression;
997 declaration_t *declaration = ref->declaration;
998 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
999 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1000 set_value(declaration->v.value_number, value);
1005 ir_node *addr = expression_to_addr(expression);
1006 type_t *type = skip_typeref(expression->base.datatype);
1007 assign_value(dbgi, addr, type, value);
1010 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1012 ir_mode *value_mode = get_irn_mode(value);
1014 if (value_mode == dest_mode || is_Bad(value))
1017 if(dest_mode == mode_b) {
1018 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1019 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1020 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1024 return new_d_Conv(dbgi, value, dest_mode);
1027 static ir_node *create_incdec(const unary_expression_t *expression)
1029 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1030 type_t *type = skip_typeref(expression->expression.datatype);
1031 ir_mode *mode = get_ir_mode(type);
1032 expression_t *value = expression->value;
1034 ir_node *value_node = expression_to_firm(value);
1037 if(is_type_pointer(type)) {
1038 pointer_type_t *pointer_type = &type->pointer;
1039 unsigned elem_size = get_type_size(pointer_type->points_to);
1040 offset = new_Const_long(mode_Is, elem_size);
1042 assert(is_type_arithmetic(type));
1043 offset = new_Const(mode, get_mode_one(mode));
1046 switch(expression->expression.type) {
1047 case EXPR_UNARY_POSTFIX_INCREMENT: {
1048 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1049 set_value_for_expression(value, new_value);
1052 case EXPR_UNARY_POSTFIX_DECREMENT: {
1053 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1054 set_value_for_expression(value, new_value);
1057 case EXPR_UNARY_PREFIX_INCREMENT: {
1058 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1059 set_value_for_expression(value, new_value);
1062 case EXPR_UNARY_PREFIX_DECREMENT: {
1063 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1064 set_value_for_expression(value, new_value);
1068 panic("no incdec expr in create_incdec");
1073 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1075 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1076 type_t *type = skip_typeref(expression->expression.datatype);
1077 ir_mode *mode = get_ir_mode(type);
1079 if(expression->expression.type == EXPR_UNARY_TAKE_ADDRESS)
1080 return expression_to_addr(expression->value);
1082 const expression_t *value = expression->value;
1083 ir_node *value_node = expression_to_firm(value);
1085 switch(expression->expression.type) {
1086 case EXPR_UNARY_NEGATE:
1087 return new_d_Minus(dbgi, value_node, mode);
1088 case EXPR_UNARY_PLUS:
1090 case EXPR_UNARY_BITWISE_NEGATE:
1091 return new_d_Not(dbgi, value_node, mode);
1092 case EXPR_UNARY_NOT: {
1093 if(get_irn_mode(value_node) != mode_b) {
1094 value_node = create_conv(dbgi, value_node, mode_b);
1096 value_node = new_d_Not(dbgi, value_node, mode_b);
1097 if(mode != mode_b) {
1098 value_node = create_conv(dbgi, value_node, mode);
1102 case EXPR_UNARY_DEREFERENCE: {
1103 type_t *value_type = skip_typeref(value->base.datatype);
1104 ir_type *irtype = get_ir_type(value_type);
1105 assert(is_Pointer_type(irtype));
1106 ir_type *points_to = get_pointer_points_to_type(irtype);
1107 return deref_address(points_to, value_node, dbgi);
1109 case EXPR_UNARY_POSTFIX_INCREMENT:
1110 case EXPR_UNARY_POSTFIX_DECREMENT:
1111 case EXPR_UNARY_PREFIX_INCREMENT:
1112 case EXPR_UNARY_PREFIX_DECREMENT:
1113 return create_incdec(expression);
1114 case EXPR_UNARY_CAST: {
1115 ir_node *node = create_conv(dbgi, value_node, get_ir_mode(type));
1116 node = do_strict_conv(dbgi, node);
1119 case EXPR_UNARY_CAST_IMPLICIT:
1120 return create_conv(dbgi, value_node, get_ir_mode(type));
1125 panic("invalid UNEXPR type found");
1128 static long get_pnc(expression_type_t type)
1131 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1132 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Lg;
1133 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1134 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1135 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1136 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1140 panic("trying to get pn_Cmp from non-comparison binexpr type");
1143 static ir_node *create_lazy_op(const binary_expression_t *expression)
1145 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1146 type_t *type = expression->expression.datatype;
1147 ir_mode *mode = get_ir_mode(type);
1149 ir_node *cur_block = get_cur_block();
1151 ir_node *one_block = new_immBlock();
1152 ir_node *one = new_Const(mode, get_mode_one(mode));
1153 ir_node *jmp_one = new_d_Jmp(dbgi);
1155 ir_node *zero_block = new_immBlock();
1156 ir_node *zero = new_Const(mode, get_mode_null(mode));
1157 ir_node *jmp_zero = new_d_Jmp(dbgi);
1159 set_cur_block(cur_block);
1160 create_condition_evaluation((const expression_t*) expression,
1161 one_block, zero_block);
1162 mature_immBlock(one_block);
1163 mature_immBlock(zero_block);
1165 ir_node *common_block = new_immBlock();
1166 add_immBlock_pred(common_block, jmp_one);
1167 add_immBlock_pred(common_block, jmp_zero);
1168 mature_immBlock(common_block);
1170 ir_node *in[2] = { one, zero };
1171 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1176 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1177 ir_node *right, ir_mode *mode);
1179 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1180 create_arithmetic_func func)
1182 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1183 ir_node *left = expression_to_firm(expression->left);
1184 ir_node *right = expression_to_firm(expression->right);
1185 type_t *type = expression->right->base.datatype;
1186 /* be careful with the modes, because in arithmetic assign nodes only
1187 * the right operand has the mode of the arithmetic already */
1188 ir_mode *mode = get_ir_mode(type);
1189 left = create_conv(dbgi, left, mode);
1190 ir_node *res = func(dbgi, left, right, mode);
1195 static ir_node *pointer_arithmetic(ir_node *const pointer,
1198 dbg_info *const dbgi,
1199 const create_arithmetic_func func)
1201 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1202 type_t *const points_to = pointer_type->points_to;
1203 const unsigned elem_size = get_type_size(points_to);
1205 assert(elem_size >= 1);
1206 if (elem_size > 1) {
1207 integer = create_conv(dbgi, integer, mode_Is);
1208 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1209 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1213 ir_mode *const mode = get_ir_mode(type);
1214 return func(dbgi, pointer, integer, mode);
1217 static ir_node *create_arithmetic_assign_binop(
1218 const binary_expression_t *expression, create_arithmetic_func func)
1220 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1221 type_t *const type = skip_typeref(expression->expression.datatype);
1224 if (is_type_pointer(type)) {
1225 ir_node *const pointer = expression_to_firm(expression->left);
1226 ir_node * integer = expression_to_firm(expression->right);
1227 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1229 value = create_arithmetic_binop(expression, func);
1232 ir_mode *const mode = get_ir_mode(type);
1233 value = create_conv(dbgi, value, mode);
1234 set_value_for_expression(expression->left, value);
1239 static ir_node *create_add(const binary_expression_t *expression)
1241 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1242 ir_node *left = expression_to_firm(expression->left);
1243 ir_node *right = expression_to_firm(expression->right);
1244 type_t *type = expression->expression.datatype;
1246 expression_t *expr_left = expression->left;
1247 expression_t *expr_right = expression->right;
1248 type_t *type_left = skip_typeref(expr_left->base.datatype);
1249 type_t *type_right = skip_typeref(expr_right->base.datatype);
1251 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1252 ir_mode *const mode = get_ir_mode(type);
1253 return new_d_Add(dbgi, left, right, mode);
1256 if (is_type_pointer(type_left)) {
1257 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1259 assert(is_type_pointer(type_right));
1260 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1264 static ir_node *create_sub(const binary_expression_t *expression)
1266 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1267 expression_t *const expr_left = expression->left;
1268 expression_t *const expr_right = expression->right;
1269 ir_node *const left = expression_to_firm(expr_left);
1270 ir_node *const right = expression_to_firm(expr_right);
1271 type_t *const type = expression->expression.datatype;
1272 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1273 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1275 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1276 ir_mode *const mode = get_ir_mode(type);
1277 return new_d_Sub(dbgi, left, right, mode);
1278 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1279 const pointer_type_t *const ptr_type = &type_left->pointer;
1280 const unsigned elem_size = get_type_size(ptr_type->points_to);
1281 ir_mode *const mode = get_ir_mode(type);
1282 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1283 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1284 ir_node *const no_mem = new_NoMem();
1285 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1286 op_pin_state_floats);
1287 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1290 assert(is_type_pointer(type_left));
1291 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1294 static ir_node *create_shift(const binary_expression_t *expression)
1296 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1297 ir_node *left = expression_to_firm(expression->left);
1298 ir_node *right = expression_to_firm(expression->right);
1299 type_t *type = expression->expression.datatype;
1300 ir_mode *mode = get_ir_mode(type);
1302 /* firm always wants the shift count to be unsigned */
1303 right = create_conv(dbgi, right, mode_Iu);
1307 switch(expression->expression.type) {
1308 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1309 case EXPR_BINARY_SHIFTLEFT:
1310 res = new_d_Shl(dbgi, left, right, mode);
1312 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1313 case EXPR_BINARY_SHIFTRIGHT: {
1314 expression_t *expr_left = expression->left;
1315 type_t *type_left = skip_typeref(expr_left->base.datatype);
1317 if(is_type_signed(type_left)) {
1318 res = new_d_Shrs(dbgi, left, right, mode);
1320 res = new_d_Shr(dbgi, left, right, mode);
1325 panic("create shift op called for non-shift op");
1332 static ir_node *create_divmod(const binary_expression_t *expression)
1334 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1335 ir_node *left = expression_to_firm(expression->left);
1336 ir_node *right = expression_to_firm(expression->right);
1337 ir_node *pin = new_Pin(new_NoMem());
1338 /* be careful with the modes, because in arithmetic assign nodes only
1339 * the right operand has the mode of the arithmetic already */
1340 type_t *type = expression->right->base.datatype;
1341 ir_mode *mode = get_ir_mode(type);
1342 left = create_conv(dbgi, left, mode);
1346 switch (expression->expression.type) {
1347 case EXPR_BINARY_DIV:
1348 case EXPR_BINARY_DIV_ASSIGN:
1349 if(mode_is_float(mode)) {
1350 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1351 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1353 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1354 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1358 case EXPR_BINARY_MOD:
1359 case EXPR_BINARY_MOD_ASSIGN:
1360 assert(!mode_is_float(mode));
1361 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1362 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1365 default: panic("unexpected binary expression type in create_divmod()");
1371 static ir_node *create_arithmetic_assign_divmod(
1372 const binary_expression_t *expression)
1374 ir_node * value = create_divmod(expression);
1375 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1376 type_t *const type = expression->expression.datatype;
1377 ir_mode *const mode = get_ir_mode(type);
1379 assert(type->type != TYPE_POINTER);
1381 value = create_conv(dbgi, value, mode);
1382 set_value_for_expression(expression->left, value);
1387 static ir_node *create_arithmetic_assign_shift(
1388 const binary_expression_t *expression)
1390 ir_node * value = create_shift(expression);
1391 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1392 type_t *const type = expression->expression.datatype;
1393 ir_mode *const mode = get_ir_mode(type);
1395 value = create_conv(dbgi, value, mode);
1396 set_value_for_expression(expression->left, value);
1401 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1403 expression_type_t type = expression->expression.type;
1406 case EXPR_BINARY_EQUAL:
1407 case EXPR_BINARY_NOTEQUAL:
1408 case EXPR_BINARY_LESS:
1409 case EXPR_BINARY_LESSEQUAL:
1410 case EXPR_BINARY_GREATER:
1411 case EXPR_BINARY_GREATEREQUAL: {
1412 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1413 ir_node *left = expression_to_firm(expression->left);
1414 ir_node *right = expression_to_firm(expression->right);
1415 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1416 long pnc = get_pnc(type);
1417 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1420 case EXPR_BINARY_ASSIGN: {
1421 ir_node *right = expression_to_firm(expression->right);
1422 set_value_for_expression(expression->left, right);
1426 case EXPR_BINARY_ADD:
1427 return create_add(expression);
1428 case EXPR_BINARY_SUB:
1429 return create_sub(expression);
1430 case EXPR_BINARY_MUL:
1431 return create_arithmetic_binop(expression, new_d_Mul);
1432 case EXPR_BINARY_BITWISE_AND:
1433 return create_arithmetic_binop(expression, new_d_And);
1434 case EXPR_BINARY_BITWISE_OR:
1435 return create_arithmetic_binop(expression, new_d_Or);
1436 case EXPR_BINARY_BITWISE_XOR:
1437 return create_arithmetic_binop(expression, new_d_Eor);
1438 case EXPR_BINARY_SHIFTLEFT:
1439 case EXPR_BINARY_SHIFTRIGHT:
1440 return create_shift(expression);
1441 case EXPR_BINARY_DIV:
1442 case EXPR_BINARY_MOD:
1443 return create_divmod(expression);
1444 case EXPR_BINARY_LOGICAL_AND:
1445 case EXPR_BINARY_LOGICAL_OR:
1446 return create_lazy_op(expression);
1447 case EXPR_BINARY_COMMA:
1448 expression_to_firm(expression->left);
1449 return expression_to_firm(expression->right);
1450 case EXPR_BINARY_ADD_ASSIGN:
1451 return create_arithmetic_assign_binop(expression, new_d_Add);
1452 case EXPR_BINARY_SUB_ASSIGN:
1453 return create_arithmetic_assign_binop(expression, new_d_Sub);
1454 case EXPR_BINARY_MUL_ASSIGN:
1455 return create_arithmetic_assign_binop(expression, new_d_Mul);
1456 case EXPR_BINARY_DIV_ASSIGN:
1457 return create_arithmetic_assign_divmod(expression);
1458 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1459 return create_arithmetic_assign_binop(expression, new_d_And);
1460 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1461 return create_arithmetic_assign_binop(expression, new_d_Or);
1462 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1463 return create_arithmetic_assign_binop(expression, new_d_Eor);
1464 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1465 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1466 return create_arithmetic_assign_shift(expression);
1468 panic("TODO binexpr type");
1472 static ir_node *array_access_addr(const array_access_expression_t *expression)
1474 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1475 ir_node *base_addr = expression_to_firm(expression->array_ref);
1476 ir_node *offset = expression_to_firm(expression->index);
1477 offset = create_conv(dbgi, offset, mode_Iu);
1479 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1480 assert(is_type_pointer(ref_type));
1481 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1483 unsigned elem_size = get_type_size(pointer_type->points_to);
1484 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1485 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1487 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1492 static ir_node *array_access_to_firm(
1493 const array_access_expression_t *expression)
1495 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1496 ir_node *addr = array_access_addr(expression);
1497 type_t *type = revert_automatic_type_conversion(
1498 (const expression_t*) expression);
1499 type = skip_typeref(type);
1500 ir_type *irtype = get_ir_type(type);
1502 return deref_address(irtype, addr, dbgi);
1505 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1507 type_t *type = expression->type;
1509 type = expression->size_expression->base.datatype;
1510 assert(type != NULL);
1513 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1514 unsigned size = get_type_size(type);
1515 ir_node *size_node = new_Const_long(mode, size);
1520 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1522 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1524 ir_node *cur_block = get_cur_block();
1526 /* create the true block */
1527 ir_node *true_block = new_immBlock();
1529 ir_node *true_val = expression_to_firm(expression->true_expression);
1530 ir_node *true_jmp = new_Jmp();
1532 /* create the false block */
1533 ir_node *false_block = new_immBlock();
1535 ir_node *false_val = expression_to_firm(expression->false_expression);
1536 ir_node *false_jmp = new_Jmp();
1538 /* create the condition evaluation */
1539 set_cur_block(cur_block);
1540 create_condition_evaluation(expression->condition, true_block, false_block);
1541 mature_immBlock(true_block);
1542 mature_immBlock(false_block);
1544 /* create the common block */
1545 ir_node *common_block = new_immBlock();
1546 add_immBlock_pred(common_block, true_jmp);
1547 add_immBlock_pred(common_block, false_jmp);
1548 mature_immBlock(common_block);
1550 /* TODO improve static semantics, so either both or no values are NULL */
1551 if (true_val == NULL || false_val == NULL)
1554 ir_node *in[2] = { true_val, false_val };
1555 ir_mode *mode = get_irn_mode(true_val);
1556 assert(get_irn_mode(false_val) == mode);
1557 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1562 static ir_node *select_addr(const select_expression_t *expression)
1564 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1566 ir_node *compound_addr = expression_to_firm(expression->compound);
1568 declaration_t *entry = expression->compound_entry;
1569 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1570 ir_entity *entity = entry->v.entity;
1572 assert(entity != NULL);
1574 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1579 static ir_node *select_to_firm(const select_expression_t *expression)
1581 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1582 ir_node *addr = select_addr(expression);
1583 type_t *type = revert_automatic_type_conversion(
1584 (const expression_t*) expression);
1585 type = skip_typeref(type);
1586 ir_type *irtype = get_ir_type(type);
1588 return deref_address(irtype, addr, dbgi);
1591 /* Values returned by __builtin_classify_type. */
1592 typedef enum gcc_type_class
1598 enumeral_type_class,
1601 reference_type_class,
1605 function_type_class,
1616 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1618 const type_t *const type = expr->type_expression->base.datatype;
1624 const atomic_type_t *const atomic_type = &type->atomic;
1625 switch (atomic_type->atype) {
1626 // should not be reached
1627 case ATOMIC_TYPE_INVALID:
1631 // gcc cannot do that
1632 case ATOMIC_TYPE_VOID:
1633 tc = void_type_class;
1636 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1637 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1638 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1639 case ATOMIC_TYPE_SHORT:
1640 case ATOMIC_TYPE_USHORT:
1641 case ATOMIC_TYPE_INT:
1642 case ATOMIC_TYPE_UINT:
1643 case ATOMIC_TYPE_LONG:
1644 case ATOMIC_TYPE_ULONG:
1645 case ATOMIC_TYPE_LONGLONG:
1646 case ATOMIC_TYPE_ULONGLONG:
1647 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1648 tc = integer_type_class;
1651 case ATOMIC_TYPE_FLOAT:
1652 case ATOMIC_TYPE_DOUBLE:
1653 case ATOMIC_TYPE_LONG_DOUBLE:
1654 tc = real_type_class;
1657 #ifdef PROVIDE_COMPLEX
1658 case ATOMIC_TYPE_FLOAT_COMPLEX:
1659 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1660 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1661 tc = complex_type_class;
1663 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1664 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1665 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1666 tc = complex_type_class;
1671 panic("Unimplemented case in classify_type_to_firm().");
1676 case TYPE_ARRAY: // gcc handles this as pointer
1677 case TYPE_FUNCTION: // gcc handles this as pointer
1678 case TYPE_POINTER: tc = pointer_type_class; break;
1679 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1680 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1682 // gcc handles this as integer
1683 case TYPE_ENUM: tc = integer_type_class; break;
1686 panic("Unimplemented case in classify_type_to_firm().");
1689 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1690 ir_mode *const mode = mode_Is;
1691 tarval *const tv = new_tarval_from_long(tc, mode);
1692 return new_d_Const(dbgi, mode, tv);
1695 static ir_node *function_name_to_firm(
1696 const string_literal_expression_t *const expr)
1698 if (current_function_name == NULL) {
1699 const source_position_t *const src_pos =
1700 &expr->expression.source_position;
1701 const char *const name = current_function_decl->symbol->string;
1702 current_function_name = string_to_firm(src_pos, "__func__", name);
1705 return current_function_name;
1708 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1710 statement_t *statement = expr->statement;
1712 assert(statement->type == STATEMENT_COMPOUND);
1713 return compound_statement_to_firm((compound_statement_t*) statement);
1716 static ir_node *va_start_expression_to_firm(
1717 const va_start_expression_t *const expr)
1719 ir_type *const method_type = get_ir_type(current_function_decl->type);
1720 int const n = get_method_n_params(method_type) - 1;
1721 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
1722 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
1723 dbg_info *const dbgi =
1724 get_dbg_info(&expr->expression.source_position);
1725 ir_node *const no_mem = new_NoMem();
1726 ir_node *const arg_sel =
1727 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
1729 size_t const parm_size = get_type_size(expr->parameter->type);
1730 ir_node *const cnst = new_Const_long(mode_Iu, parm_size);
1731 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
1732 set_value_for_expression(expr->ap, add);
1737 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
1739 ir_type *const irtype = get_ir_type(expr->expression.datatype);
1740 ir_node *const ap = expression_to_firm(expr->ap);
1741 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1742 ir_node *const res = deref_address(irtype, ap, dbgi);
1744 size_t const parm_size = get_type_size(expr->expression.datatype);
1745 ir_node *const cnst = new_Const_long(mode_Iu, parm_size);
1746 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
1747 set_value_for_expression(expr->ap, add);
1752 static ir_node *dereference_addr(const unary_expression_t *const expression)
1754 assert(expression->expression.type == EXPR_UNARY_DEREFERENCE);
1755 return expression_to_firm(expression->value);
1758 static ir_node *expression_to_addr(const expression_t *expression)
1760 switch(expression->type) {
1761 case EXPR_REFERENCE:
1762 return reference_addr(&expression->reference);
1763 case EXPR_ARRAY_ACCESS:
1764 return array_access_addr(&expression->array_access);
1766 return select_addr(&expression->select);
1768 return call_expression_to_firm(&expression->call);
1769 case EXPR_UNARY_DEREFERENCE: {
1770 return dereference_addr(&expression->unary);
1775 panic("trying to get address of non-lvalue");
1778 static ir_node *_expression_to_firm(const expression_t *expression)
1780 switch(expression->type) {
1782 return const_to_firm(&expression->conste);
1783 case EXPR_STRING_LITERAL:
1784 return string_literal_to_firm(&expression->string);
1785 case EXPR_WIDE_STRING_LITERAL:
1786 return wide_string_literal_to_firm(&expression->wide_string);
1787 case EXPR_REFERENCE:
1788 return reference_expression_to_firm(&expression->reference);
1790 return call_expression_to_firm(&expression->call);
1792 return unary_expression_to_firm(&expression->unary);
1794 return binary_expression_to_firm(&expression->binary);
1795 case EXPR_ARRAY_ACCESS:
1796 return array_access_to_firm(&expression->array_access);
1798 return sizeof_to_firm(&expression->sizeofe);
1799 case EXPR_CONDITIONAL:
1800 return conditional_to_firm(&expression->conditional);
1802 return select_to_firm(&expression->select);
1803 case EXPR_CLASSIFY_TYPE:
1804 return classify_type_to_firm(&expression->classify_type);
1806 case EXPR_PRETTY_FUNCTION:
1807 return function_name_to_firm(&expression->string);
1808 case EXPR_STATEMENT:
1809 return statement_expression_to_firm(&expression->statement);
1811 return va_start_expression_to_firm(&expression->va_starte);
1813 return va_arg_expression_to_firm(&expression->va_arge);
1815 case EXPR_BUILTIN_SYMBOL:
1816 panic("unimplemented expression found");
1822 panic("invalid expression found");
1825 static ir_node *expression_to_firm(const expression_t *expression)
1827 ir_node *res = _expression_to_firm(expression);
1829 if(res != NULL && get_irn_mode(res) == mode_b) {
1830 ir_mode *mode = get_ir_mode(expression->base.datatype);
1831 res = create_conv(NULL, res, mode);
1837 static ir_node *expression_to_modeb(const expression_t *expression)
1839 ir_node *res = _expression_to_firm(expression);
1840 res = create_conv(NULL, res, mode_b);
1846 * create a short-circuit expression evaluation that tries to construct
1847 * efficient control flow structures for &&, || and ! expressions
1849 static void create_condition_evaluation(const expression_t *expression,
1850 ir_node *true_block,
1851 ir_node *false_block)
1853 switch(expression->type) {
1854 case EXPR_UNARY_NOT: {
1855 const unary_expression_t *unary_expression = &expression->unary;
1856 create_condition_evaluation(unary_expression->value, false_block,
1860 case EXPR_BINARY_LOGICAL_AND: {
1861 const binary_expression_t *binary_expression = &expression->binary;
1863 ir_node *cur_block = get_cur_block();
1864 ir_node *extra_block = new_immBlock();
1865 set_cur_block(cur_block);
1866 create_condition_evaluation(binary_expression->left, extra_block,
1868 mature_immBlock(extra_block);
1869 set_cur_block(extra_block);
1870 create_condition_evaluation(binary_expression->right, true_block,
1874 case EXPR_BINARY_LOGICAL_OR: {
1875 const binary_expression_t *binary_expression = &expression->binary;
1877 ir_node *cur_block = get_cur_block();
1878 ir_node *extra_block = new_immBlock();
1879 set_cur_block(cur_block);
1880 create_condition_evaluation(binary_expression->left, true_block,
1882 mature_immBlock(extra_block);
1883 set_cur_block(extra_block);
1884 create_condition_evaluation(binary_expression->right, true_block,
1892 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1893 ir_node *condition = expression_to_modeb(expression);
1894 ir_node *cond = new_d_Cond(dbgi, condition);
1895 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1896 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1898 add_immBlock_pred(true_block, true_proj);
1899 add_immBlock_pred(false_block, false_proj);
1901 set_cur_block(NULL);
1904 static void return_statement_to_firm(return_statement_t *statement)
1906 if(get_cur_block() == NULL)
1909 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1911 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1915 if(get_method_n_ress(func_irtype) > 0) {
1916 ir_type *res_type = get_method_res_type(func_irtype, 0);
1918 if(statement->return_value != NULL) {
1919 ir_node *node = expression_to_firm(statement->return_value);
1920 node = do_strict_conv(dbgi, node);
1924 if(is_compound_type(res_type)) {
1927 mode = get_type_mode(res_type);
1929 in[0] = new_Unknown(mode);
1933 /* build return_value for its side effects */
1934 if(statement->return_value != NULL) {
1935 expression_to_firm(statement->return_value);
1940 ir_node *store = get_store();
1941 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1943 ir_node *end_block = get_irg_end_block(current_ir_graph);
1944 add_immBlock_pred(end_block, ret);
1946 set_cur_block(NULL);
1949 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1951 if(get_cur_block() == NULL)
1954 return expression_to_firm(statement->expression);
1957 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1959 ir_node *result = NULL;
1960 statement_t *statement = compound->statements;
1961 for( ; statement != NULL; statement = statement->base.next) {
1962 //context2firm(&statement->context);
1964 if(statement->base.next == NULL
1965 && statement->type == STATEMENT_EXPRESSION) {
1966 result = expression_statement_to_firm(
1967 (expression_statement_t*) statement);
1970 statement_to_firm(statement);
1976 static void if_statement_to_firm(if_statement_t *statement)
1978 ir_node *cur_block = get_cur_block();
1980 ir_node *fallthrough_block = new_immBlock();
1982 /* the true (blocks) */
1983 ir_node *true_block;
1984 if (statement->true_statement != NULL) {
1985 true_block = new_immBlock();
1986 statement_to_firm(statement->true_statement);
1987 if(get_cur_block() != NULL) {
1988 ir_node *jmp = new_Jmp();
1989 add_immBlock_pred(fallthrough_block, jmp);
1992 true_block = fallthrough_block;
1995 /* the false (blocks) */
1996 ir_node *false_block;
1997 if(statement->false_statement != NULL) {
1998 false_block = new_immBlock();
2000 statement_to_firm(statement->false_statement);
2001 if(get_cur_block() != NULL) {
2002 ir_node *jmp = new_Jmp();
2003 add_immBlock_pred(fallthrough_block, jmp);
2006 false_block = fallthrough_block;
2009 /* create the condition */
2010 if(cur_block != NULL) {
2011 set_cur_block(cur_block);
2012 create_condition_evaluation(statement->condition, true_block,
2016 mature_immBlock(true_block);
2017 if(false_block != fallthrough_block) {
2018 mature_immBlock(false_block);
2020 mature_immBlock(fallthrough_block);
2022 set_cur_block(fallthrough_block);
2025 static void while_statement_to_firm(while_statement_t *statement)
2027 ir_node *jmp = NULL;
2028 if(get_cur_block() != NULL) {
2032 /* create the header block */
2033 ir_node *header_block = new_immBlock();
2035 add_immBlock_pred(header_block, jmp);
2038 /* the false block */
2039 ir_node *false_block = new_immBlock();
2042 ir_node *body_block;
2043 if (statement->body != NULL) {
2044 ir_node *old_continue_label = continue_label;
2045 ir_node *old_break_label = break_label;
2046 continue_label = header_block;
2047 break_label = false_block;
2049 body_block = new_immBlock();
2050 statement_to_firm(statement->body);
2052 assert(continue_label == header_block);
2053 assert(break_label == false_block);
2054 continue_label = old_continue_label;
2055 break_label = old_break_label;
2057 if(get_cur_block() != NULL) {
2059 add_immBlock_pred(header_block, jmp);
2062 body_block = header_block;
2065 /* create the condition */
2066 set_cur_block(header_block);
2068 create_condition_evaluation(statement->condition, body_block, false_block);
2069 mature_immBlock(body_block);
2070 mature_immBlock(false_block);
2071 mature_immBlock(header_block);
2073 set_cur_block(false_block);
2076 static void do_while_statement_to_firm(do_while_statement_t *statement)
2078 ir_node *jmp = NULL;
2079 if(get_cur_block() != NULL) {
2083 /* create the header block */
2084 ir_node *header_block = new_immBlock();
2086 /* the false block */
2087 ir_node *false_block = new_immBlock();
2090 ir_node *body_block = new_immBlock();
2092 add_immBlock_pred(body_block, jmp);
2095 if (statement->body != NULL) {
2096 ir_node *old_continue_label = continue_label;
2097 ir_node *old_break_label = break_label;
2098 continue_label = header_block;
2099 break_label = false_block;
2101 statement_to_firm(statement->body);
2103 assert(continue_label == header_block);
2104 assert(break_label == false_block);
2105 continue_label = old_continue_label;
2106 break_label = old_break_label;
2108 if (get_cur_block() == NULL) {
2109 mature_immBlock(header_block);
2110 mature_immBlock(body_block);
2111 mature_immBlock(false_block);
2116 ir_node *body_jmp = new_Jmp();
2117 add_immBlock_pred(header_block, body_jmp);
2118 mature_immBlock(header_block);
2120 /* create the condition */
2121 set_cur_block(header_block);
2123 create_condition_evaluation(statement->condition, body_block, false_block);
2124 mature_immBlock(body_block);
2125 mature_immBlock(false_block);
2126 mature_immBlock(header_block);
2128 set_cur_block(false_block);
2131 static void for_statement_to_firm(for_statement_t *statement)
2133 ir_node *jmp = NULL;
2134 if (get_cur_block() != NULL) {
2135 if(statement->initialisation != NULL) {
2136 expression_to_firm(statement->initialisation);
2141 /* create the step block */
2142 ir_node *const step_block = new_immBlock();
2143 if (statement->step != NULL) {
2144 expression_to_firm(statement->step);
2146 ir_node *const step_jmp = new_Jmp();
2148 /* create the header block */
2149 ir_node *const header_block = new_immBlock();
2151 add_immBlock_pred(header_block, jmp);
2153 add_immBlock_pred(header_block, step_jmp);
2155 /* the false block */
2156 ir_node *const false_block = new_immBlock();
2159 ir_node * body_block;
2160 if (statement->body != NULL) {
2161 ir_node *const old_continue_label = continue_label;
2162 ir_node *const old_break_label = break_label;
2163 continue_label = step_block;
2164 break_label = false_block;
2166 body_block = new_immBlock();
2167 statement_to_firm(statement->body);
2169 assert(continue_label == step_block);
2170 assert(break_label == false_block);
2171 continue_label = old_continue_label;
2172 break_label = old_break_label;
2174 if (get_cur_block() != NULL) {
2176 add_immBlock_pred(step_block, jmp);
2179 body_block = step_block;
2182 /* create the condition */
2183 set_cur_block(header_block);
2184 if (statement->condition != NULL) {
2185 create_condition_evaluation(statement->condition, body_block,
2188 keep_alive(header_block);
2190 add_immBlock_pred(body_block, jmp);
2193 mature_immBlock(body_block);
2194 mature_immBlock(false_block);
2195 mature_immBlock(step_block);
2196 mature_immBlock(header_block);
2197 mature_immBlock(false_block);
2199 set_cur_block(false_block);
2202 static void create_declaration_entity(declaration_t *declaration,
2203 declaration_type_t declaration_type,
2204 ir_type *parent_type)
2206 ident *id = new_id_from_str(declaration->symbol->string);
2207 ir_type *irtype = get_ir_type(declaration->type);
2208 ir_entity *entity = new_entity(parent_type, id, irtype);
2209 set_entity_ld_ident(entity, id);
2211 declaration->declaration_type = (unsigned char) declaration_type;
2212 declaration->v.entity = entity;
2213 set_entity_variability(entity, variability_uninitialized);
2214 /* TODO: visibility? */
2217 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2219 enum compound_graph_entry_type_t {
2220 COMPOUND_GRAPH_ENTRY_ARRAY,
2221 COMPOUND_GRAPH_ENTRY_COMPOUND
2224 struct compound_graph_path_entry_t {
2230 compound_graph_path_entry_t *prev;
2233 static void create_initializer_object(initializer_t *initializer, type_t *type,
2234 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2236 static compound_graph_path *create_compound_path(ir_type *type,
2237 compound_graph_path_entry_t *entry, int len)
2239 compound_graph_path *path = new_compound_graph_path(type, len);
2242 for( ; entry != NULL; entry = entry->prev, --i) {
2244 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2245 set_compound_graph_path_node(path, i, entry->v.entity);
2247 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2248 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2256 static void create_initializer_value(initializer_value_t *initializer,
2258 compound_graph_path_entry_t *entry,
2261 ir_node *node = expression_to_firm(initializer->value);
2262 ir_type *type = get_entity_type(entity);
2263 compound_graph_path *path = create_compound_path(type, entry, len);
2264 add_compound_ent_value_w_path(entity, node, path);
2267 static void create_initializer_compound(initializer_list_t *initializer,
2268 compound_type_t *type,
2270 compound_graph_path_entry_t *last_entry,
2273 declaration_t *compound_declaration = type->declaration;
2275 declaration_t *compound_entry = compound_declaration->context.declarations;
2277 compound_graph_path_entry_t entry;
2278 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2279 entry.prev = last_entry;
2283 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2284 if(compound_entry->symbol == NULL)
2286 if(compound_entry->namespc != NAMESPACE_NORMAL)
2289 if(i >= initializer->len)
2292 entry.v.entity = compound_entry->v.entity;
2294 initializer_t *sub_initializer = initializer->initializers[i];
2296 assert(compound_entry != NULL);
2297 assert(compound_entry->declaration_type
2298 == DECLARATION_TYPE_COMPOUND_MEMBER);
2300 if(sub_initializer->type == INITIALIZER_VALUE) {
2301 create_initializer_value(&sub_initializer->value,
2302 entity, &entry, len);
2304 type_t *entry_type = skip_typeref(compound_entry->type);
2305 create_initializer_object(sub_initializer, entry_type, entity,
2313 static void create_initializer_array(initializer_list_t *initializer,
2314 array_type_t *type, ir_entity *entity,
2315 compound_graph_path_entry_t *last_entry,
2318 type_t *element_type = type->element_type;
2319 element_type = skip_typeref(element_type);
2321 compound_graph_path_entry_t entry;
2322 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2323 entry.prev = last_entry;
2327 for(i = 0; i < initializer->len; ++i) {
2328 entry.v.array_index = i;
2330 initializer_t *sub_initializer = initializer->initializers[i];
2332 if(sub_initializer->type == INITIALIZER_VALUE) {
2333 create_initializer_value(&sub_initializer->value,
2334 entity, &entry, len);
2336 create_initializer_object(sub_initializer, element_type, entity,
2342 /* TODO: initialize rest... */
2343 if(type->size_expression != NULL) {
2344 size_t array_len = fold_constant(type->size_expression);
2345 for( ; i < array_len; ++i) {
2352 static void create_initializer_string(initializer_string_t *initializer,
2353 array_type_t *type, ir_entity *entity,
2354 compound_graph_path_entry_t *last_entry,
2357 type_t *element_type = type->element_type;
2358 element_type = skip_typeref(element_type);
2360 compound_graph_path_entry_t entry;
2361 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2362 entry.prev = last_entry;
2365 ir_type *irtype = get_entity_type(entity);
2366 size_t arr_len = get_array_type_size(type);
2367 const char *p = initializer->string;
2369 for(i = 0; i < arr_len; ++i, ++p) {
2370 entry.v.array_index = i;
2372 ir_node *node = new_Const_long(mode_Bs, *p);
2373 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2374 add_compound_ent_value_w_path(entity, node, path);
2381 static void create_initializer_wide_string(
2382 const initializer_wide_string_t *const initializer, array_type_t *const type,
2383 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2386 type_t *element_type = type->element_type;
2387 element_type = skip_typeref(element_type);
2389 compound_graph_path_entry_t entry;
2390 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2391 entry.prev = last_entry;
2394 ir_type *const irtype = get_entity_type(entity);
2395 const size_t arr_len = get_array_type_size(type);
2396 const wchar_rep_t * p = initializer->string.begin;
2397 const wchar_rep_t *const end = p + initializer->string.size;
2398 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2399 entry.v.array_index = i;
2401 ir_node *node = new_Const_long(mode_Is, *p);
2402 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2403 add_compound_ent_value_w_path(entity, node, path);
2407 static void create_initializer_object(initializer_t *initializer, type_t *type,
2408 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2410 if(is_type_array(type)) {
2411 array_type_t *array_type = &type->array;
2413 switch (initializer->type) {
2414 case INITIALIZER_STRING: {
2415 initializer_string_t *const string = &initializer->string;
2416 create_initializer_string(string, array_type, entity, entry, len);
2420 case INITIALIZER_WIDE_STRING: {
2421 initializer_wide_string_t *const string = &initializer->wide_string;
2422 create_initializer_wide_string(string, array_type, entity, entry, len);
2426 case INITIALIZER_LIST: {
2427 initializer_list_t *const list = &initializer->list;
2428 create_initializer_array(list, array_type, entity, entry, len);
2432 case INITIALIZER_VALUE:
2435 panic("Unhandled initializer");
2437 assert(initializer->type == INITIALIZER_LIST);
2438 initializer_list_t *list = &initializer->list;
2440 assert(is_type_compound(type));
2441 compound_type_t *compound_type = &type->compound;
2442 create_initializer_compound(list, compound_type, entity, entry, len);
2446 static void create_initializer_local_variable_entity(declaration_t *declaration)
2448 initializer_t *initializer = declaration->init.initializer;
2449 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2450 ir_entity *entity = declaration->v.entity;
2451 ir_node *memory = get_store();
2452 ir_node *nomem = new_NoMem();
2453 ir_node *frame = get_irg_frame(current_ir_graph);
2454 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2456 if(initializer->type == INITIALIZER_VALUE) {
2457 initializer_value_t *initializer_value = &initializer->value;
2459 ir_node *value = expression_to_firm(initializer_value->value);
2460 type_t *type = skip_typeref(declaration->type);
2461 assign_value(dbgi, addr, type, value);
2465 /* create a "template" entity which is copied to the entity on the stack */
2466 ident *id = unique_ident("initializer");
2467 ir_type *irtype = get_ir_type(declaration->type);
2468 ir_type *global_type = get_glob_type();
2469 ir_entity *init_entity = new_entity(global_type, id, irtype);
2470 set_entity_ld_ident(init_entity, id);
2472 set_entity_variability(init_entity, variability_initialized);
2473 set_entity_visibility(init_entity, visibility_local);
2475 ir_graph *old_current_ir_graph = current_ir_graph;
2476 current_ir_graph = get_const_code_irg();
2478 type_t *type = skip_typeref(declaration->type);
2479 create_initializer_object(initializer, type, init_entity, NULL, 0);
2481 assert(current_ir_graph == get_const_code_irg());
2482 current_ir_graph = old_current_ir_graph;
2484 ir_node *src_addr = create_symconst(dbgi, init_entity);
2485 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2487 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2488 set_store(copyb_mem);
2491 static void create_initializer(declaration_t *declaration)
2493 initializer_t *initializer = declaration->init.initializer;
2494 if(initializer == NULL)
2497 declaration_type_t declaration_type
2498 = (declaration_type_t) declaration->declaration_type;
2499 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2500 create_initializer_local_variable_entity(declaration);
2504 if(initializer->type == INITIALIZER_VALUE) {
2505 initializer_value_t *initializer_value = &initializer->value;
2507 ir_node *value = expression_to_firm(initializer_value->value);
2509 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2510 set_value(declaration->v.value_number, value);
2512 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2514 ir_entity *entity = declaration->v.entity;
2516 set_entity_variability(entity, variability_initialized);
2517 set_atomic_ent_value(entity, value);
2520 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2521 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2523 ir_entity *entity = declaration->v.entity;
2524 set_entity_variability(entity, variability_initialized);
2526 type_t *type = skip_typeref(declaration->type);
2527 create_initializer_object(initializer, type, entity, NULL, 0);
2531 static void create_local_variable(declaration_t *declaration)
2533 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2535 bool needs_entity = declaration->address_taken;
2536 type_t *type = skip_typeref(declaration->type);
2538 if(is_type_array(type) || is_type_compound(type)) {
2539 needs_entity = true;
2543 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2544 create_declaration_entity(declaration,
2545 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2548 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2549 declaration->v.value_number = next_value_number_function;
2550 ++next_value_number_function;
2553 create_initializer(declaration);
2556 static void create_local_static_variable(declaration_t *declaration)
2558 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2560 type_t *type = skip_typeref(declaration->type);
2561 ir_type *global_type = get_glob_type();
2562 ident *id = unique_ident(declaration->symbol->string);
2563 ir_type *irtype = get_ir_type(type);
2564 ir_entity *entity = new_entity(global_type, id, irtype);
2565 set_entity_ld_ident(entity, id);
2567 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2568 declaration->v.entity = entity;
2569 set_entity_variability(entity, variability_uninitialized);
2570 set_entity_visibility(entity, visibility_local);
2572 ir_graph *old_current_ir_graph = current_ir_graph;
2573 current_ir_graph = get_const_code_irg();
2575 create_initializer(declaration);
2577 assert(current_ir_graph == get_const_code_irg());
2578 current_ir_graph = old_current_ir_graph;
2581 static void declaration_statement_to_firm(declaration_statement_t *statement)
2583 declaration_t *declaration = statement->declarations_begin;
2584 declaration_t *end = statement->declarations_end->next;
2585 for( ; declaration != end; declaration = declaration->next) {
2586 type_t *type = skip_typeref(declaration->type);
2588 switch ((storage_class_tag_t) declaration->storage_class) {
2589 case STORAGE_CLASS_TYPEDEF:
2591 case STORAGE_CLASS_STATIC:
2592 create_local_static_variable(declaration);
2594 case STORAGE_CLASS_ENUM_ENTRY:
2595 panic("enum entry declaration in local block found");
2596 case STORAGE_CLASS_EXTERN:
2597 panic("extern declaration in local block found");
2598 case STORAGE_CLASS_NONE:
2599 case STORAGE_CLASS_AUTO:
2600 case STORAGE_CLASS_REGISTER:
2601 if(is_type_function(type)) {
2602 panic("nested functions not supported yet");
2604 create_local_variable(declaration);
2607 case STORAGE_CLASS_THREAD:
2608 case STORAGE_CLASS_THREAD_EXTERN:
2609 case STORAGE_CLASS_THREAD_STATIC:
2612 panic("invalid storage class found");
2616 static void create_jump_statement(const statement_t *statement,
2617 ir_node *target_block)
2619 if(get_cur_block() == NULL)
2622 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2623 ir_node *jump = new_d_Jmp(dbgi);
2624 add_immBlock_pred(target_block, jump);
2626 set_cur_block(NULL);
2629 static void switch_statement_to_firm(const switch_statement_t *statement)
2631 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2633 ir_node *expression = expression_to_firm(statement->expression);
2634 ir_node *cond = new_d_Cond(dbgi, expression);
2635 ir_node *break_block = new_immBlock();
2637 set_cur_block(NULL);
2639 ir_node *const old_switch_cond = current_switch_cond;
2640 ir_node *const old_break_label = break_label;
2641 const bool old_saw_default_label = saw_default_label;
2642 current_switch_cond = cond;
2643 break_label = break_block;
2645 statement_to_firm(statement->body);
2647 if(get_cur_block() != NULL) {
2648 ir_node *jmp = new_Jmp();
2649 add_immBlock_pred(break_block, jmp);
2652 if (!saw_default_label) {
2653 set_cur_block(get_nodes_block(cond));
2654 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2655 MAGIC_DEFAULT_PN_NUMBER);
2656 add_immBlock_pred(break_block, proj);
2659 assert(current_switch_cond == cond);
2660 assert(break_label == break_block);
2661 current_switch_cond = old_switch_cond;
2662 break_label = old_break_label;
2663 saw_default_label = old_saw_default_label;
2665 mature_immBlock(break_block);
2666 set_cur_block(break_block);
2669 static long fold_constant(const expression_t *expression)
2671 ir_graph *old_current_ir_graph = current_ir_graph;
2672 current_ir_graph = get_const_code_irg();
2674 ir_node *cnst = expression_to_firm(expression);
2675 if(!is_Const(cnst)) {
2676 panic("couldn't fold constantl");
2678 tarval *tv = get_Const_tarval(cnst);
2679 if(!tarval_is_long(tv)) {
2680 panic("folded constant not an integer");
2683 long res = get_tarval_long(tv);
2685 current_ir_graph = old_current_ir_graph;
2689 static void case_label_to_firm(const case_label_statement_t *statement)
2691 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2693 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2695 /* let's create a node and hope firm constant folding creates a Const
2698 set_cur_block(get_nodes_block(current_switch_cond));
2699 if(statement->expression) {
2700 long pn = fold_constant(statement->expression);
2701 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2702 /* oops someone detected our cheating... */
2703 panic("magic default pn used");
2705 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2707 saw_default_label = true;
2708 proj = new_d_defaultProj(dbgi, current_switch_cond,
2709 MAGIC_DEFAULT_PN_NUMBER);
2712 ir_node *block = new_immBlock();
2713 if (fallthrough != NULL) {
2714 add_immBlock_pred(block, fallthrough);
2716 add_immBlock_pred(block, proj);
2717 mature_immBlock(block);
2719 statement_to_firm(statement->label_statement);
2722 static ir_node *get_label_block(declaration_t *label)
2724 assert(label->namespc == NAMESPACE_LABEL);
2726 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2727 return label->v.block;
2729 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2731 ir_node *old_cur_block = get_cur_block();
2732 ir_node *block = new_immBlock();
2733 set_cur_block(old_cur_block);
2735 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2736 label->v.block = block;
2738 ARR_APP1(ir_node *, imature_blocks, block);
2743 static void label_to_firm(const label_statement_t *statement)
2745 ir_node *block = get_label_block(statement->label);
2747 if(get_cur_block() != NULL) {
2748 ir_node *jmp = new_Jmp();
2749 add_immBlock_pred(block, jmp);
2752 set_cur_block(block);
2755 statement_to_firm(statement->label_statement);
2758 static void goto_to_firm(const goto_statement_t *statement)
2760 if(get_cur_block() == NULL)
2763 ir_node *block = get_label_block(statement->label);
2764 ir_node *jmp = new_Jmp();
2765 add_immBlock_pred(block, jmp);
2767 set_cur_block(NULL);
2770 static void statement_to_firm(statement_t *statement)
2772 switch(statement->type) {
2773 case STATEMENT_INVALID:
2774 panic("invalid statement found");
2775 case STATEMENT_COMPOUND:
2776 compound_statement_to_firm(&statement->compound);
2778 case STATEMENT_RETURN:
2779 return_statement_to_firm(&statement->returns);
2781 case STATEMENT_EXPRESSION:
2782 expression_statement_to_firm(&statement->expression);
2785 if_statement_to_firm(&statement->ifs);
2787 case STATEMENT_WHILE:
2788 while_statement_to_firm(&statement->whiles);
2790 case STATEMENT_DO_WHILE:
2791 do_while_statement_to_firm(&statement->do_while);
2793 case STATEMENT_DECLARATION:
2794 declaration_statement_to_firm(&statement->declaration);
2796 case STATEMENT_BREAK:
2797 create_jump_statement(statement, break_label);
2799 case STATEMENT_CONTINUE:
2800 create_jump_statement(statement, continue_label);
2802 case STATEMENT_SWITCH:
2803 switch_statement_to_firm(&statement->switchs);
2805 case STATEMENT_CASE_LABEL:
2806 case_label_to_firm(&statement->case_label);
2809 for_statement_to_firm(&statement->fors);
2811 case STATEMENT_LABEL:
2812 label_to_firm(&statement->label);
2814 case STATEMENT_GOTO:
2815 goto_to_firm(&statement->gotos);
2820 panic("Statement not implemented\n");
2823 static int count_local_declarations(const declaration_t * decl,
2824 const declaration_t *const end)
2827 for (; decl != end; decl = decl->next) {
2828 const type_t *type = skip_typeref(decl->type);
2829 switch (type->type) {
2833 if (!decl->address_taken)
2843 static int count_decls_in_stmts(const statement_t *stmt)
2846 for (; stmt != NULL; stmt = stmt->base.next) {
2847 switch (stmt->type) {
2848 case STATEMENT_DECLARATION: {
2849 const declaration_statement_t *const decl_stmt =
2850 (const declaration_statement_t*)stmt;
2851 count += count_local_declarations(decl_stmt->declarations_begin,
2852 decl_stmt->declarations_end->next);
2856 case STATEMENT_COMPOUND: {
2857 const compound_statement_t *const comp =
2858 (const compound_statement_t*)stmt;
2859 count += count_decls_in_stmts(comp->statements);
2863 case STATEMENT_IF: {
2864 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2865 count += count_decls_in_stmts(if_stmt->true_statement);
2866 count += count_decls_in_stmts(if_stmt->false_statement);
2870 case STATEMENT_SWITCH: {
2871 const switch_statement_t *const switch_stmt =
2872 (const switch_statement_t*)stmt;
2873 count += count_decls_in_stmts(switch_stmt->body);
2877 case STATEMENT_LABEL: {
2878 const label_statement_t *const label_stmt =
2879 (const label_statement_t*)stmt;
2880 count += count_decls_in_stmts(label_stmt->label_statement);
2884 case STATEMENT_WHILE: {
2885 const while_statement_t *const while_stmt =
2886 (const while_statement_t*)stmt;
2887 count += count_decls_in_stmts(while_stmt->body);
2891 case STATEMENT_DO_WHILE: {
2892 const do_while_statement_t *const do_while_stmt =
2893 (const do_while_statement_t*)stmt;
2894 count += count_decls_in_stmts(do_while_stmt->body);
2898 case STATEMENT_FOR: {
2899 const for_statement_t *const for_stmt =
2900 (const for_statement_t*)stmt;
2901 /* TODO initialisation */
2902 count += count_decls_in_stmts(for_stmt->body);
2907 case STATEMENT_BREAK:
2908 case STATEMENT_CASE_LABEL:
2909 case STATEMENT_CONTINUE:
2910 case STATEMENT_EXPRESSION:
2911 case STATEMENT_GOTO:
2912 case STATEMENT_INVALID:
2913 case STATEMENT_RETURN:
2920 static int get_function_n_local_vars(declaration_t *declaration)
2924 /* count parameters */
2925 count += count_local_declarations(declaration->context.declarations, NULL);
2927 /* count local variables declared in body */
2928 count += count_decls_in_stmts(declaration->init.statement);
2930 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2931 * for expression statements... */
2937 static void initialize_function_parameters(declaration_t *declaration)
2939 ir_graph *irg = current_ir_graph;
2940 ir_node *args = get_irg_args(irg);
2941 ir_node *start_block = get_irg_start_block(irg);
2942 ir_type *function_irtype = get_ir_type(declaration->type);
2945 declaration_t *parameter = declaration->context.declarations;
2946 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2947 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2948 type_t *type = skip_typeref(parameter->type);
2950 bool needs_entity = parameter->address_taken;
2951 assert(!is_type_array(type));
2952 if(is_type_compound(type)) {
2953 needs_entity = true;
2957 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2958 ident *id = new_id_from_str(parameter->symbol->string);
2959 set_entity_ident(entity, id);
2961 parameter->declaration_type
2962 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2963 parameter->v.entity = entity;
2967 ir_mode *mode = get_ir_mode(parameter->type);
2969 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2971 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2972 parameter->v.value_number = next_value_number_function;
2973 ++next_value_number_function;
2975 set_value(parameter->v.value_number, proj);
2979 static void create_function(declaration_t *declaration)
2981 ir_entity *function_entity = get_function_entity(declaration);
2983 if(declaration->init.statement == NULL)
2986 current_function_decl = declaration;
2987 current_function_name = NULL;
2989 assert(imature_blocks == NULL);
2990 imature_blocks = NEW_ARR_F(ir_node*, 0);
2992 int n_local_vars = get_function_n_local_vars(declaration);
2993 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
2994 ir_node *first_block = get_cur_block();
2996 next_value_number_function = 0;
2997 initialize_function_parameters(declaration);
2999 statement_to_firm(declaration->init.statement);
3001 ir_node *end_block = get_irg_end_block(irg);
3003 /* do we have a return statement yet? */
3004 if(get_cur_block() != NULL) {
3005 type_t *type = skip_typeref(declaration->type);
3006 assert(is_type_function(type));
3007 const function_type_t *func_type = &type->function;
3008 const type_t *return_type
3009 = skip_typeref(func_type->return_type);
3012 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3013 ret = new_Return(get_store(), 0, NULL);
3016 if(is_type_scalar(return_type)) {
3017 mode = get_ir_mode(func_type->return_type);
3023 // ยง5.1.2.2.3 main implicitly returns 0
3024 if (strcmp(declaration->symbol->string, "main") == 0) {
3025 in[0] = new_Const(mode, get_mode_null(mode));
3027 in[0] = new_Unknown(mode);
3029 ret = new_Return(get_store(), 1, in);
3031 add_immBlock_pred(end_block, ret);
3034 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3035 mature_immBlock(imature_blocks[i]);
3037 DEL_ARR_F(imature_blocks);
3038 imature_blocks = NULL;
3040 mature_immBlock(first_block);
3041 mature_immBlock(end_block);
3043 irg_finalize_cons(irg);
3045 /* finalize the frame type */
3046 ir_type *frame_type = get_irg_frame_type(irg);
3047 int n = get_compound_n_members(frame_type);
3050 for(int i = 0; i < n; ++i) {
3051 ir_entity *entity = get_compound_member(frame_type, i);
3052 ir_type *entity_type = get_entity_type(entity);
3054 int align = get_type_alignment_bytes(entity_type);
3055 if(align > align_all)
3059 misalign = offset % align;
3061 offset += align - misalign;
3065 set_entity_offset(entity, offset);
3066 offset += get_type_size_bytes(entity_type);
3068 set_type_size_bytes(frame_type, offset);
3069 set_type_alignment_bytes(frame_type, align_all);
3070 set_type_state(frame_type, layout_fixed);
3075 static void create_global_variable(declaration_t *declaration)
3079 switch ((storage_class_tag_t)declaration->storage_class) {
3080 case STORAGE_CLASS_STATIC:
3081 vis = visibility_local;
3084 case STORAGE_CLASS_EXTERN:
3085 vis = visibility_external_allocated;
3088 case STORAGE_CLASS_NONE:
3089 vis = visibility_external_visible;
3092 case STORAGE_CLASS_THREAD:
3093 vis = visibility_external_visible;
3096 case STORAGE_CLASS_THREAD_EXTERN:
3097 vis = visibility_external_allocated;
3100 case STORAGE_CLASS_THREAD_STATIC:
3101 vis = visibility_local;
3105 var_type = get_tls_type();
3109 var_type = get_glob_type();
3113 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3115 set_entity_visibility(declaration->v.entity, vis);
3117 current_ir_graph = get_const_code_irg();
3118 create_initializer(declaration);
3121 case STORAGE_CLASS_TYPEDEF:
3122 case STORAGE_CLASS_AUTO:
3123 case STORAGE_CLASS_REGISTER:
3124 case STORAGE_CLASS_ENUM_ENTRY:
3127 panic("Invalid storage class for global variable");
3130 static void context_to_firm(context_t *context)
3132 /* first pass: create declarations */
3133 declaration_t *declaration = context->declarations;
3134 for( ; declaration != NULL; declaration = declaration->next) {
3135 if(declaration->namespc != NAMESPACE_NORMAL)
3137 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3138 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3140 if(declaration->symbol == NULL)
3143 type_t *type = skip_typeref(declaration->type);
3144 if(is_type_function(type)) {
3145 get_function_entity(declaration);
3147 create_global_variable(declaration);
3151 /* second pass: create code */
3152 declaration = context->declarations;
3153 for( ; declaration != NULL; declaration = declaration->next) {
3154 if(declaration->namespc != NAMESPACE_NORMAL)
3156 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3157 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3159 if(declaration->symbol == NULL)
3162 type_t *type = declaration->type;
3163 if(type->type != TYPE_FUNCTION)
3166 create_function(declaration);
3170 void translation_unit_to_firm(translation_unit_t *unit)
3172 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3173 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3174 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3176 ir_type_int = get_ir_type(type_int);
3177 ir_type_const_char = get_ir_type(type_const_char);
3178 ir_type_wchar_t = get_ir_type(type_wchar_t);
3179 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3182 type_void->base.firm_type = ir_type_void;
3184 /* just to be sure */
3185 continue_label = NULL;
3187 current_switch_cond = NULL;
3189 context_to_firm(& unit->context);