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(const expression_type_t type)
1131 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1132 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1133 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Ne;
1134 case EXPR_BINARY_ISLESS:
1135 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1136 case EXPR_BINARY_ISLESSEQUAL:
1137 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1138 case EXPR_BINARY_ISGREATER:
1139 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1140 case EXPR_BINARY_ISGREATEREQUAL:
1141 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1142 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1147 panic("trying to get pn_Cmp from non-comparison binexpr type");
1150 static ir_node *create_lazy_op(const binary_expression_t *expression)
1152 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1153 type_t *type = expression->expression.datatype;
1154 ir_mode *mode = get_ir_mode(type);
1156 ir_node *cur_block = get_cur_block();
1158 ir_node *one_block = new_immBlock();
1159 ir_node *one = new_Const(mode, get_mode_one(mode));
1160 ir_node *jmp_one = new_d_Jmp(dbgi);
1162 ir_node *zero_block = new_immBlock();
1163 ir_node *zero = new_Const(mode, get_mode_null(mode));
1164 ir_node *jmp_zero = new_d_Jmp(dbgi);
1166 set_cur_block(cur_block);
1167 create_condition_evaluation((const expression_t*) expression,
1168 one_block, zero_block);
1169 mature_immBlock(one_block);
1170 mature_immBlock(zero_block);
1172 ir_node *common_block = new_immBlock();
1173 add_immBlock_pred(common_block, jmp_one);
1174 add_immBlock_pred(common_block, jmp_zero);
1175 mature_immBlock(common_block);
1177 ir_node *in[2] = { one, zero };
1178 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1183 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1184 ir_node *right, ir_mode *mode);
1186 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1187 create_arithmetic_func func)
1189 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1190 ir_node *left = expression_to_firm(expression->left);
1191 ir_node *right = expression_to_firm(expression->right);
1192 type_t *type = expression->right->base.datatype;
1193 /* be careful with the modes, because in arithmetic assign nodes only
1194 * the right operand has the mode of the arithmetic already */
1195 ir_mode *mode = get_ir_mode(type);
1196 left = create_conv(dbgi, left, mode);
1197 ir_node *res = func(dbgi, left, right, mode);
1202 static ir_node *pointer_arithmetic(ir_node *const pointer,
1205 dbg_info *const dbgi,
1206 const create_arithmetic_func func)
1208 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1209 type_t *const points_to = pointer_type->points_to;
1210 const unsigned elem_size = get_type_size(points_to);
1212 assert(elem_size >= 1);
1213 if (elem_size > 1) {
1214 integer = create_conv(dbgi, integer, mode_Is);
1215 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1216 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_Is);
1220 ir_mode *const mode = get_ir_mode(type);
1221 return func(dbgi, pointer, integer, mode);
1224 static ir_node *create_arithmetic_assign_binop(
1225 const binary_expression_t *expression, create_arithmetic_func func)
1227 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1228 type_t *const type = skip_typeref(expression->expression.datatype);
1231 if (is_type_pointer(type)) {
1232 ir_node *const pointer = expression_to_firm(expression->left);
1233 ir_node * integer = expression_to_firm(expression->right);
1234 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1236 value = create_arithmetic_binop(expression, func);
1239 ir_mode *const mode = get_ir_mode(type);
1240 value = create_conv(dbgi, value, mode);
1241 set_value_for_expression(expression->left, value);
1246 static ir_node *create_add(const binary_expression_t *expression)
1248 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1249 ir_node *left = expression_to_firm(expression->left);
1250 ir_node *right = expression_to_firm(expression->right);
1251 type_t *type = expression->expression.datatype;
1253 expression_t *expr_left = expression->left;
1254 expression_t *expr_right = expression->right;
1255 type_t *type_left = skip_typeref(expr_left->base.datatype);
1256 type_t *type_right = skip_typeref(expr_right->base.datatype);
1258 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1259 ir_mode *const mode = get_ir_mode(type);
1260 return new_d_Add(dbgi, left, right, mode);
1263 if (is_type_pointer(type_left)) {
1264 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1266 assert(is_type_pointer(type_right));
1267 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1271 static ir_node *create_sub(const binary_expression_t *expression)
1273 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1274 expression_t *const expr_left = expression->left;
1275 expression_t *const expr_right = expression->right;
1276 ir_node *const left = expression_to_firm(expr_left);
1277 ir_node *const right = expression_to_firm(expr_right);
1278 type_t *const type = expression->expression.datatype;
1279 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1280 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1282 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1283 ir_mode *const mode = get_ir_mode(type);
1284 return new_d_Sub(dbgi, left, right, mode);
1285 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1286 const pointer_type_t *const ptr_type = &type_left->pointer;
1287 const unsigned elem_size = get_type_size(ptr_type->points_to);
1288 ir_mode *const mode = get_ir_mode(type);
1289 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1290 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1291 ir_node *const no_mem = new_NoMem();
1292 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1293 op_pin_state_floats);
1294 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1297 assert(is_type_pointer(type_left));
1298 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1301 static ir_node *create_shift(const binary_expression_t *expression)
1303 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1304 ir_node *left = expression_to_firm(expression->left);
1305 ir_node *right = expression_to_firm(expression->right);
1306 type_t *type = expression->expression.datatype;
1307 ir_mode *mode = get_ir_mode(type);
1309 /* firm always wants the shift count to be unsigned */
1310 right = create_conv(dbgi, right, mode_Iu);
1314 switch(expression->expression.type) {
1315 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1316 case EXPR_BINARY_SHIFTLEFT:
1317 res = new_d_Shl(dbgi, left, right, mode);
1319 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1320 case EXPR_BINARY_SHIFTRIGHT: {
1321 expression_t *expr_left = expression->left;
1322 type_t *type_left = skip_typeref(expr_left->base.datatype);
1324 if(is_type_signed(type_left)) {
1325 res = new_d_Shrs(dbgi, left, right, mode);
1327 res = new_d_Shr(dbgi, left, right, mode);
1332 panic("create shift op called for non-shift op");
1339 static ir_node *create_divmod(const binary_expression_t *expression)
1341 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1342 ir_node *left = expression_to_firm(expression->left);
1343 ir_node *right = expression_to_firm(expression->right);
1344 ir_node *pin = new_Pin(new_NoMem());
1345 /* be careful with the modes, because in arithmetic assign nodes only
1346 * the right operand has the mode of the arithmetic already */
1347 type_t *type = expression->right->base.datatype;
1348 ir_mode *mode = get_ir_mode(type);
1349 left = create_conv(dbgi, left, mode);
1353 switch (expression->expression.type) {
1354 case EXPR_BINARY_DIV:
1355 case EXPR_BINARY_DIV_ASSIGN:
1356 if(mode_is_float(mode)) {
1357 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1358 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1360 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1361 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1365 case EXPR_BINARY_MOD:
1366 case EXPR_BINARY_MOD_ASSIGN:
1367 assert(!mode_is_float(mode));
1368 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1369 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1372 default: panic("unexpected binary expression type in create_divmod()");
1378 static ir_node *create_arithmetic_assign_divmod(
1379 const binary_expression_t *expression)
1381 ir_node * value = create_divmod(expression);
1382 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1383 type_t *const type = expression->expression.datatype;
1384 ir_mode *const mode = get_ir_mode(type);
1386 assert(type->type != TYPE_POINTER);
1388 value = create_conv(dbgi, value, mode);
1389 set_value_for_expression(expression->left, value);
1394 static ir_node *create_arithmetic_assign_shift(
1395 const binary_expression_t *expression)
1397 ir_node * value = create_shift(expression);
1398 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1399 type_t *const type = expression->expression.datatype;
1400 ir_mode *const mode = get_ir_mode(type);
1402 value = create_conv(dbgi, value, mode);
1403 set_value_for_expression(expression->left, value);
1408 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1410 expression_type_t type = expression->expression.type;
1413 case EXPR_BINARY_EQUAL:
1414 case EXPR_BINARY_NOTEQUAL:
1415 case EXPR_BINARY_LESS:
1416 case EXPR_BINARY_LESSEQUAL:
1417 case EXPR_BINARY_GREATER:
1418 case EXPR_BINARY_GREATEREQUAL:
1419 case EXPR_BINARY_ISGREATER:
1420 case EXPR_BINARY_ISGREATEREQUAL:
1421 case EXPR_BINARY_ISLESS:
1422 case EXPR_BINARY_ISLESSEQUAL:
1423 case EXPR_BINARY_ISLESSGREATER:
1424 case EXPR_BINARY_ISUNORDERED: {
1425 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1426 ir_node *left = expression_to_firm(expression->left);
1427 ir_node *right = expression_to_firm(expression->right);
1428 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1429 long pnc = get_pnc(type);
1430 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1433 case EXPR_BINARY_ASSIGN: {
1434 ir_node *right = expression_to_firm(expression->right);
1435 set_value_for_expression(expression->left, right);
1439 case EXPR_BINARY_ADD:
1440 return create_add(expression);
1441 case EXPR_BINARY_SUB:
1442 return create_sub(expression);
1443 case EXPR_BINARY_MUL:
1444 return create_arithmetic_binop(expression, new_d_Mul);
1445 case EXPR_BINARY_BITWISE_AND:
1446 return create_arithmetic_binop(expression, new_d_And);
1447 case EXPR_BINARY_BITWISE_OR:
1448 return create_arithmetic_binop(expression, new_d_Or);
1449 case EXPR_BINARY_BITWISE_XOR:
1450 return create_arithmetic_binop(expression, new_d_Eor);
1451 case EXPR_BINARY_SHIFTLEFT:
1452 case EXPR_BINARY_SHIFTRIGHT:
1453 return create_shift(expression);
1454 case EXPR_BINARY_DIV:
1455 case EXPR_BINARY_MOD:
1456 return create_divmod(expression);
1457 case EXPR_BINARY_LOGICAL_AND:
1458 case EXPR_BINARY_LOGICAL_OR:
1459 return create_lazy_op(expression);
1460 case EXPR_BINARY_COMMA:
1461 expression_to_firm(expression->left);
1462 return expression_to_firm(expression->right);
1463 case EXPR_BINARY_ADD_ASSIGN:
1464 return create_arithmetic_assign_binop(expression, new_d_Add);
1465 case EXPR_BINARY_SUB_ASSIGN:
1466 return create_arithmetic_assign_binop(expression, new_d_Sub);
1467 case EXPR_BINARY_MUL_ASSIGN:
1468 return create_arithmetic_assign_binop(expression, new_d_Mul);
1469 case EXPR_BINARY_DIV_ASSIGN:
1470 return create_arithmetic_assign_divmod(expression);
1471 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1472 return create_arithmetic_assign_binop(expression, new_d_And);
1473 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1474 return create_arithmetic_assign_binop(expression, new_d_Or);
1475 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1476 return create_arithmetic_assign_binop(expression, new_d_Eor);
1477 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1478 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1479 return create_arithmetic_assign_shift(expression);
1481 panic("TODO binexpr type");
1485 static ir_node *array_access_addr(const array_access_expression_t *expression)
1487 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1488 ir_node *base_addr = expression_to_firm(expression->array_ref);
1489 ir_node *offset = expression_to_firm(expression->index);
1490 offset = create_conv(dbgi, offset, mode_Iu);
1492 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1493 assert(is_type_pointer(ref_type));
1494 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1496 unsigned elem_size = get_type_size(pointer_type->points_to);
1497 ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1498 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1500 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1505 static ir_node *array_access_to_firm(
1506 const array_access_expression_t *expression)
1508 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1509 ir_node *addr = array_access_addr(expression);
1510 type_t *type = revert_automatic_type_conversion(
1511 (const expression_t*) expression);
1512 type = skip_typeref(type);
1513 ir_type *irtype = get_ir_type(type);
1515 return deref_address(irtype, addr, dbgi);
1518 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1520 type_t *type = expression->type;
1522 type = expression->size_expression->base.datatype;
1523 assert(type != NULL);
1526 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1527 unsigned size = get_type_size(type);
1528 ir_node *size_node = new_Const_long(mode, size);
1533 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1535 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1537 ir_node *cur_block = get_cur_block();
1539 /* create the true block */
1540 ir_node *true_block = new_immBlock();
1542 ir_node *true_val = expression_to_firm(expression->true_expression);
1543 ir_node *true_jmp = new_Jmp();
1545 /* create the false block */
1546 ir_node *false_block = new_immBlock();
1548 ir_node *false_val = expression_to_firm(expression->false_expression);
1549 ir_node *false_jmp = new_Jmp();
1551 /* create the condition evaluation */
1552 set_cur_block(cur_block);
1553 create_condition_evaluation(expression->condition, true_block, false_block);
1554 mature_immBlock(true_block);
1555 mature_immBlock(false_block);
1557 /* create the common block */
1558 ir_node *common_block = new_immBlock();
1559 add_immBlock_pred(common_block, true_jmp);
1560 add_immBlock_pred(common_block, false_jmp);
1561 mature_immBlock(common_block);
1563 /* TODO improve static semantics, so either both or no values are NULL */
1564 if (true_val == NULL || false_val == NULL)
1567 ir_node *in[2] = { true_val, false_val };
1568 ir_mode *mode = get_irn_mode(true_val);
1569 assert(get_irn_mode(false_val) == mode);
1570 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1575 static ir_node *select_addr(const select_expression_t *expression)
1577 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1579 ir_node *compound_addr = expression_to_firm(expression->compound);
1581 declaration_t *entry = expression->compound_entry;
1582 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1583 ir_entity *entity = entry->v.entity;
1585 assert(entity != NULL);
1587 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1592 static ir_node *select_to_firm(const select_expression_t *expression)
1594 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1595 ir_node *addr = select_addr(expression);
1596 type_t *type = revert_automatic_type_conversion(
1597 (const expression_t*) expression);
1598 type = skip_typeref(type);
1599 ir_type *irtype = get_ir_type(type);
1601 return deref_address(irtype, addr, dbgi);
1604 /* Values returned by __builtin_classify_type. */
1605 typedef enum gcc_type_class
1611 enumeral_type_class,
1614 reference_type_class,
1618 function_type_class,
1629 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1631 const type_t *const type = expr->type_expression->base.datatype;
1637 const atomic_type_t *const atomic_type = &type->atomic;
1638 switch (atomic_type->atype) {
1639 // should not be reached
1640 case ATOMIC_TYPE_INVALID:
1644 // gcc cannot do that
1645 case ATOMIC_TYPE_VOID:
1646 tc = void_type_class;
1649 case ATOMIC_TYPE_CHAR: // gcc handles this as integer
1650 case ATOMIC_TYPE_SCHAR: // gcc handles this as integer
1651 case ATOMIC_TYPE_UCHAR: // gcc handles this as integer
1652 case ATOMIC_TYPE_SHORT:
1653 case ATOMIC_TYPE_USHORT:
1654 case ATOMIC_TYPE_INT:
1655 case ATOMIC_TYPE_UINT:
1656 case ATOMIC_TYPE_LONG:
1657 case ATOMIC_TYPE_ULONG:
1658 case ATOMIC_TYPE_LONGLONG:
1659 case ATOMIC_TYPE_ULONGLONG:
1660 case ATOMIC_TYPE_BOOL: // gcc handles this as integer
1661 tc = integer_type_class;
1664 case ATOMIC_TYPE_FLOAT:
1665 case ATOMIC_TYPE_DOUBLE:
1666 case ATOMIC_TYPE_LONG_DOUBLE:
1667 tc = real_type_class;
1670 #ifdef PROVIDE_COMPLEX
1671 case ATOMIC_TYPE_FLOAT_COMPLEX:
1672 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1673 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1674 tc = complex_type_class;
1676 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1677 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1678 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1679 tc = complex_type_class;
1684 panic("Unimplemented case in classify_type_to_firm().");
1689 case TYPE_ARRAY: // gcc handles this as pointer
1690 case TYPE_FUNCTION: // gcc handles this as pointer
1691 case TYPE_POINTER: tc = pointer_type_class; break;
1692 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1693 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1695 // gcc handles this as integer
1696 case TYPE_ENUM: tc = integer_type_class; break;
1699 panic("Unimplemented case in classify_type_to_firm().");
1702 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1703 ir_mode *const mode = mode_Is;
1704 tarval *const tv = new_tarval_from_long(tc, mode);
1705 return new_d_Const(dbgi, mode, tv);
1708 static ir_node *function_name_to_firm(
1709 const string_literal_expression_t *const expr)
1711 if (current_function_name == NULL) {
1712 const source_position_t *const src_pos =
1713 &expr->expression.source_position;
1714 const char *const name = current_function_decl->symbol->string;
1715 current_function_name = string_to_firm(src_pos, "__func__", name);
1718 return current_function_name;
1721 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1723 statement_t *statement = expr->statement;
1725 assert(statement->type == STATEMENT_COMPOUND);
1726 return compound_statement_to_firm((compound_statement_t*) statement);
1729 static ir_node *va_start_expression_to_firm(
1730 const va_start_expression_t *const expr)
1732 ir_type *const method_type = get_ir_type(current_function_decl->type);
1733 int const n = get_method_n_params(method_type) - 1;
1734 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
1735 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
1736 dbg_info *const dbgi =
1737 get_dbg_info(&expr->expression.source_position);
1738 ir_node *const no_mem = new_NoMem();
1739 ir_node *const arg_sel =
1740 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
1742 size_t const parm_size = get_type_size(expr->parameter->type);
1743 ir_node *const cnst = new_Const_long(mode_Iu, parm_size);
1744 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
1745 set_value_for_expression(expr->ap, add);
1750 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
1752 ir_type *const irtype = get_ir_type(expr->expression.datatype);
1753 ir_node *const ap = expression_to_firm(expr->ap);
1754 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1755 ir_node *const res = deref_address(irtype, ap, dbgi);
1757 size_t const parm_size = get_type_size(expr->expression.datatype);
1758 ir_node *const cnst = new_Const_long(mode_Iu, parm_size);
1759 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
1760 set_value_for_expression(expr->ap, add);
1765 static ir_node *dereference_addr(const unary_expression_t *const expression)
1767 assert(expression->expression.type == EXPR_UNARY_DEREFERENCE);
1768 return expression_to_firm(expression->value);
1771 static ir_node *expression_to_addr(const expression_t *expression)
1773 switch(expression->type) {
1774 case EXPR_REFERENCE:
1775 return reference_addr(&expression->reference);
1776 case EXPR_ARRAY_ACCESS:
1777 return array_access_addr(&expression->array_access);
1779 return select_addr(&expression->select);
1781 return call_expression_to_firm(&expression->call);
1782 case EXPR_UNARY_DEREFERENCE: {
1783 return dereference_addr(&expression->unary);
1788 panic("trying to get address of non-lvalue");
1791 static ir_node *_expression_to_firm(const expression_t *expression)
1793 switch(expression->type) {
1795 return const_to_firm(&expression->conste);
1796 case EXPR_STRING_LITERAL:
1797 return string_literal_to_firm(&expression->string);
1798 case EXPR_WIDE_STRING_LITERAL:
1799 return wide_string_literal_to_firm(&expression->wide_string);
1800 case EXPR_REFERENCE:
1801 return reference_expression_to_firm(&expression->reference);
1803 return call_expression_to_firm(&expression->call);
1805 return unary_expression_to_firm(&expression->unary);
1807 return binary_expression_to_firm(&expression->binary);
1808 case EXPR_ARRAY_ACCESS:
1809 return array_access_to_firm(&expression->array_access);
1811 return sizeof_to_firm(&expression->sizeofe);
1812 case EXPR_CONDITIONAL:
1813 return conditional_to_firm(&expression->conditional);
1815 return select_to_firm(&expression->select);
1816 case EXPR_CLASSIFY_TYPE:
1817 return classify_type_to_firm(&expression->classify_type);
1819 case EXPR_PRETTY_FUNCTION:
1820 return function_name_to_firm(&expression->string);
1821 case EXPR_STATEMENT:
1822 return statement_expression_to_firm(&expression->statement);
1824 return va_start_expression_to_firm(&expression->va_starte);
1826 return va_arg_expression_to_firm(&expression->va_arge);
1828 case EXPR_BUILTIN_SYMBOL:
1829 panic("unimplemented expression found");
1835 panic("invalid expression found");
1838 static ir_node *expression_to_firm(const expression_t *expression)
1840 ir_node *res = _expression_to_firm(expression);
1842 if(res != NULL && get_irn_mode(res) == mode_b) {
1843 ir_mode *mode = get_ir_mode(expression->base.datatype);
1844 res = create_conv(NULL, res, mode);
1850 static ir_node *expression_to_modeb(const expression_t *expression)
1852 ir_node *res = _expression_to_firm(expression);
1853 res = create_conv(NULL, res, mode_b);
1859 * create a short-circuit expression evaluation that tries to construct
1860 * efficient control flow structures for &&, || and ! expressions
1862 static void create_condition_evaluation(const expression_t *expression,
1863 ir_node *true_block,
1864 ir_node *false_block)
1866 switch(expression->type) {
1867 case EXPR_UNARY_NOT: {
1868 const unary_expression_t *unary_expression = &expression->unary;
1869 create_condition_evaluation(unary_expression->value, false_block,
1873 case EXPR_BINARY_LOGICAL_AND: {
1874 const binary_expression_t *binary_expression = &expression->binary;
1876 ir_node *cur_block = get_cur_block();
1877 ir_node *extra_block = new_immBlock();
1878 set_cur_block(cur_block);
1879 create_condition_evaluation(binary_expression->left, extra_block,
1881 mature_immBlock(extra_block);
1882 set_cur_block(extra_block);
1883 create_condition_evaluation(binary_expression->right, true_block,
1887 case EXPR_BINARY_LOGICAL_OR: {
1888 const binary_expression_t *binary_expression = &expression->binary;
1890 ir_node *cur_block = get_cur_block();
1891 ir_node *extra_block = new_immBlock();
1892 set_cur_block(cur_block);
1893 create_condition_evaluation(binary_expression->left, true_block,
1895 mature_immBlock(extra_block);
1896 set_cur_block(extra_block);
1897 create_condition_evaluation(binary_expression->right, true_block,
1905 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1906 ir_node *condition = expression_to_modeb(expression);
1907 ir_node *cond = new_d_Cond(dbgi, condition);
1908 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1909 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1911 add_immBlock_pred(true_block, true_proj);
1912 add_immBlock_pred(false_block, false_proj);
1914 set_cur_block(NULL);
1917 static void return_statement_to_firm(return_statement_t *statement)
1919 if(get_cur_block() == NULL)
1922 ir_type *func_irtype = get_ir_type(current_function_decl->type);
1924 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1928 if(get_method_n_ress(func_irtype) > 0) {
1929 ir_type *res_type = get_method_res_type(func_irtype, 0);
1931 if(statement->return_value != NULL) {
1932 ir_node *node = expression_to_firm(statement->return_value);
1933 node = do_strict_conv(dbgi, node);
1937 if(is_compound_type(res_type)) {
1940 mode = get_type_mode(res_type);
1942 in[0] = new_Unknown(mode);
1946 /* build return_value for its side effects */
1947 if(statement->return_value != NULL) {
1948 expression_to_firm(statement->return_value);
1953 ir_node *store = get_store();
1954 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
1956 ir_node *end_block = get_irg_end_block(current_ir_graph);
1957 add_immBlock_pred(end_block, ret);
1959 set_cur_block(NULL);
1962 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1964 if(get_cur_block() == NULL)
1967 return expression_to_firm(statement->expression);
1970 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1972 ir_node *result = NULL;
1973 statement_t *statement = compound->statements;
1974 for( ; statement != NULL; statement = statement->base.next) {
1975 //context2firm(&statement->context);
1977 if(statement->base.next == NULL
1978 && statement->type == STATEMENT_EXPRESSION) {
1979 result = expression_statement_to_firm(
1980 (expression_statement_t*) statement);
1983 statement_to_firm(statement);
1989 static void if_statement_to_firm(if_statement_t *statement)
1991 ir_node *cur_block = get_cur_block();
1993 ir_node *fallthrough_block = new_immBlock();
1995 /* the true (blocks) */
1996 ir_node *true_block;
1997 if (statement->true_statement != NULL) {
1998 true_block = new_immBlock();
1999 statement_to_firm(statement->true_statement);
2000 if(get_cur_block() != NULL) {
2001 ir_node *jmp = new_Jmp();
2002 add_immBlock_pred(fallthrough_block, jmp);
2005 true_block = fallthrough_block;
2008 /* the false (blocks) */
2009 ir_node *false_block;
2010 if(statement->false_statement != NULL) {
2011 false_block = new_immBlock();
2013 statement_to_firm(statement->false_statement);
2014 if(get_cur_block() != NULL) {
2015 ir_node *jmp = new_Jmp();
2016 add_immBlock_pred(fallthrough_block, jmp);
2019 false_block = fallthrough_block;
2022 /* create the condition */
2023 if(cur_block != NULL) {
2024 set_cur_block(cur_block);
2025 create_condition_evaluation(statement->condition, true_block,
2029 mature_immBlock(true_block);
2030 if(false_block != fallthrough_block) {
2031 mature_immBlock(false_block);
2033 mature_immBlock(fallthrough_block);
2035 set_cur_block(fallthrough_block);
2038 static void while_statement_to_firm(while_statement_t *statement)
2040 ir_node *jmp = NULL;
2041 if(get_cur_block() != NULL) {
2045 /* create the header block */
2046 ir_node *header_block = new_immBlock();
2048 add_immBlock_pred(header_block, jmp);
2051 /* the false block */
2052 ir_node *false_block = new_immBlock();
2055 ir_node *body_block;
2056 if (statement->body != NULL) {
2057 ir_node *old_continue_label = continue_label;
2058 ir_node *old_break_label = break_label;
2059 continue_label = header_block;
2060 break_label = false_block;
2062 body_block = new_immBlock();
2063 statement_to_firm(statement->body);
2065 assert(continue_label == header_block);
2066 assert(break_label == false_block);
2067 continue_label = old_continue_label;
2068 break_label = old_break_label;
2070 if(get_cur_block() != NULL) {
2072 add_immBlock_pred(header_block, jmp);
2075 body_block = header_block;
2078 /* create the condition */
2079 set_cur_block(header_block);
2081 create_condition_evaluation(statement->condition, body_block, false_block);
2082 mature_immBlock(body_block);
2083 mature_immBlock(false_block);
2084 mature_immBlock(header_block);
2086 set_cur_block(false_block);
2089 static void do_while_statement_to_firm(do_while_statement_t *statement)
2091 ir_node *jmp = NULL;
2092 if(get_cur_block() != NULL) {
2096 /* create the header block */
2097 ir_node *header_block = new_immBlock();
2099 /* the false block */
2100 ir_node *false_block = new_immBlock();
2103 ir_node *body_block = new_immBlock();
2105 add_immBlock_pred(body_block, jmp);
2108 if (statement->body != NULL) {
2109 ir_node *old_continue_label = continue_label;
2110 ir_node *old_break_label = break_label;
2111 continue_label = header_block;
2112 break_label = false_block;
2114 statement_to_firm(statement->body);
2116 assert(continue_label == header_block);
2117 assert(break_label == false_block);
2118 continue_label = old_continue_label;
2119 break_label = old_break_label;
2121 if (get_cur_block() == NULL) {
2122 mature_immBlock(header_block);
2123 mature_immBlock(body_block);
2124 mature_immBlock(false_block);
2129 ir_node *body_jmp = new_Jmp();
2130 add_immBlock_pred(header_block, body_jmp);
2131 mature_immBlock(header_block);
2133 /* create the condition */
2134 set_cur_block(header_block);
2136 create_condition_evaluation(statement->condition, body_block, false_block);
2137 mature_immBlock(body_block);
2138 mature_immBlock(false_block);
2139 mature_immBlock(header_block);
2141 set_cur_block(false_block);
2144 static void for_statement_to_firm(for_statement_t *statement)
2146 ir_node *jmp = NULL;
2147 if (get_cur_block() != NULL) {
2148 if(statement->initialisation != NULL) {
2149 expression_to_firm(statement->initialisation);
2154 /* create the step block */
2155 ir_node *const step_block = new_immBlock();
2156 if (statement->step != NULL) {
2157 expression_to_firm(statement->step);
2159 ir_node *const step_jmp = new_Jmp();
2161 /* create the header block */
2162 ir_node *const header_block = new_immBlock();
2164 add_immBlock_pred(header_block, jmp);
2166 add_immBlock_pred(header_block, step_jmp);
2168 /* the false block */
2169 ir_node *const false_block = new_immBlock();
2172 ir_node * body_block;
2173 if (statement->body != NULL) {
2174 ir_node *const old_continue_label = continue_label;
2175 ir_node *const old_break_label = break_label;
2176 continue_label = step_block;
2177 break_label = false_block;
2179 body_block = new_immBlock();
2180 statement_to_firm(statement->body);
2182 assert(continue_label == step_block);
2183 assert(break_label == false_block);
2184 continue_label = old_continue_label;
2185 break_label = old_break_label;
2187 if (get_cur_block() != NULL) {
2189 add_immBlock_pred(step_block, jmp);
2192 body_block = step_block;
2195 /* create the condition */
2196 set_cur_block(header_block);
2197 if (statement->condition != NULL) {
2198 create_condition_evaluation(statement->condition, body_block,
2201 keep_alive(header_block);
2203 add_immBlock_pred(body_block, jmp);
2206 mature_immBlock(body_block);
2207 mature_immBlock(false_block);
2208 mature_immBlock(step_block);
2209 mature_immBlock(header_block);
2210 mature_immBlock(false_block);
2212 set_cur_block(false_block);
2215 static void create_declaration_entity(declaration_t *declaration,
2216 declaration_type_t declaration_type,
2217 ir_type *parent_type)
2219 ident *id = new_id_from_str(declaration->symbol->string);
2220 ir_type *irtype = get_ir_type(declaration->type);
2221 ir_entity *entity = new_entity(parent_type, id, irtype);
2222 set_entity_ld_ident(entity, id);
2224 declaration->declaration_type = (unsigned char) declaration_type;
2225 declaration->v.entity = entity;
2226 set_entity_variability(entity, variability_uninitialized);
2227 /* TODO: visibility? */
2230 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2232 enum compound_graph_entry_type_t {
2233 COMPOUND_GRAPH_ENTRY_ARRAY,
2234 COMPOUND_GRAPH_ENTRY_COMPOUND
2237 struct compound_graph_path_entry_t {
2243 compound_graph_path_entry_t *prev;
2246 static void create_initializer_object(initializer_t *initializer, type_t *type,
2247 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2249 static compound_graph_path *create_compound_path(ir_type *type,
2250 compound_graph_path_entry_t *entry, int len)
2252 compound_graph_path *path = new_compound_graph_path(type, len);
2255 for( ; entry != NULL; entry = entry->prev, --i) {
2257 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2258 set_compound_graph_path_node(path, i, entry->v.entity);
2260 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2261 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2269 static void create_initializer_value(initializer_value_t *initializer,
2271 compound_graph_path_entry_t *entry,
2274 ir_node *node = expression_to_firm(initializer->value);
2275 ir_type *type = get_entity_type(entity);
2276 compound_graph_path *path = create_compound_path(type, entry, len);
2277 add_compound_ent_value_w_path(entity, node, path);
2280 static void create_initializer_compound(initializer_list_t *initializer,
2281 compound_type_t *type,
2283 compound_graph_path_entry_t *last_entry,
2286 declaration_t *compound_declaration = type->declaration;
2288 declaration_t *compound_entry = compound_declaration->context.declarations;
2290 compound_graph_path_entry_t entry;
2291 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2292 entry.prev = last_entry;
2296 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2297 if(compound_entry->symbol == NULL)
2299 if(compound_entry->namespc != NAMESPACE_NORMAL)
2302 if(i >= initializer->len)
2305 entry.v.entity = compound_entry->v.entity;
2307 initializer_t *sub_initializer = initializer->initializers[i];
2309 assert(compound_entry != NULL);
2310 assert(compound_entry->declaration_type
2311 == DECLARATION_TYPE_COMPOUND_MEMBER);
2313 if(sub_initializer->type == INITIALIZER_VALUE) {
2314 create_initializer_value(&sub_initializer->value,
2315 entity, &entry, len);
2317 type_t *entry_type = skip_typeref(compound_entry->type);
2318 create_initializer_object(sub_initializer, entry_type, entity,
2326 static void create_initializer_array(initializer_list_t *initializer,
2327 array_type_t *type, ir_entity *entity,
2328 compound_graph_path_entry_t *last_entry,
2331 type_t *element_type = type->element_type;
2332 element_type = skip_typeref(element_type);
2334 compound_graph_path_entry_t entry;
2335 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2336 entry.prev = last_entry;
2340 for(i = 0; i < initializer->len; ++i) {
2341 entry.v.array_index = i;
2343 initializer_t *sub_initializer = initializer->initializers[i];
2345 if(sub_initializer->type == INITIALIZER_VALUE) {
2346 create_initializer_value(&sub_initializer->value,
2347 entity, &entry, len);
2349 create_initializer_object(sub_initializer, element_type, entity,
2355 /* TODO: initialize rest... */
2356 if(type->size_expression != NULL) {
2357 size_t array_len = fold_constant(type->size_expression);
2358 for( ; i < array_len; ++i) {
2365 static void create_initializer_string(initializer_string_t *initializer,
2366 array_type_t *type, ir_entity *entity,
2367 compound_graph_path_entry_t *last_entry,
2370 type_t *element_type = type->element_type;
2371 element_type = skip_typeref(element_type);
2373 compound_graph_path_entry_t entry;
2374 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2375 entry.prev = last_entry;
2378 ir_type *irtype = get_entity_type(entity);
2379 size_t arr_len = get_array_type_size(type);
2380 const char *p = initializer->string;
2382 for(i = 0; i < arr_len; ++i, ++p) {
2383 entry.v.array_index = i;
2385 ir_node *node = new_Const_long(mode_Bs, *p);
2386 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2387 add_compound_ent_value_w_path(entity, node, path);
2394 static void create_initializer_wide_string(
2395 const initializer_wide_string_t *const initializer, array_type_t *const type,
2396 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2399 type_t *element_type = type->element_type;
2400 element_type = skip_typeref(element_type);
2402 compound_graph_path_entry_t entry;
2403 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2404 entry.prev = last_entry;
2407 ir_type *const irtype = get_entity_type(entity);
2408 const size_t arr_len = get_array_type_size(type);
2409 const wchar_rep_t * p = initializer->string.begin;
2410 const wchar_rep_t *const end = p + initializer->string.size;
2411 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2412 entry.v.array_index = i;
2414 ir_node *node = new_Const_long(mode_Is, *p);
2415 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2416 add_compound_ent_value_w_path(entity, node, path);
2420 static void create_initializer_object(initializer_t *initializer, type_t *type,
2421 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2423 if(is_type_array(type)) {
2424 array_type_t *array_type = &type->array;
2426 switch (initializer->type) {
2427 case INITIALIZER_STRING: {
2428 initializer_string_t *const string = &initializer->string;
2429 create_initializer_string(string, array_type, entity, entry, len);
2433 case INITIALIZER_WIDE_STRING: {
2434 initializer_wide_string_t *const string = &initializer->wide_string;
2435 create_initializer_wide_string(string, array_type, entity, entry, len);
2439 case INITIALIZER_LIST: {
2440 initializer_list_t *const list = &initializer->list;
2441 create_initializer_array(list, array_type, entity, entry, len);
2445 case INITIALIZER_VALUE:
2448 panic("Unhandled initializer");
2450 assert(initializer->type == INITIALIZER_LIST);
2451 initializer_list_t *list = &initializer->list;
2453 assert(is_type_compound(type));
2454 compound_type_t *compound_type = &type->compound;
2455 create_initializer_compound(list, compound_type, entity, entry, len);
2459 static void create_initializer_local_variable_entity(declaration_t *declaration)
2461 initializer_t *initializer = declaration->init.initializer;
2462 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2463 ir_entity *entity = declaration->v.entity;
2464 ir_node *memory = get_store();
2465 ir_node *nomem = new_NoMem();
2466 ir_node *frame = get_irg_frame(current_ir_graph);
2467 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2469 if(initializer->type == INITIALIZER_VALUE) {
2470 initializer_value_t *initializer_value = &initializer->value;
2472 ir_node *value = expression_to_firm(initializer_value->value);
2473 type_t *type = skip_typeref(declaration->type);
2474 assign_value(dbgi, addr, type, value);
2478 /* create a "template" entity which is copied to the entity on the stack */
2479 ident *id = unique_ident("initializer");
2480 ir_type *irtype = get_ir_type(declaration->type);
2481 ir_type *global_type = get_glob_type();
2482 ir_entity *init_entity = new_entity(global_type, id, irtype);
2483 set_entity_ld_ident(init_entity, id);
2485 set_entity_variability(init_entity, variability_initialized);
2486 set_entity_visibility(init_entity, visibility_local);
2488 ir_graph *old_current_ir_graph = current_ir_graph;
2489 current_ir_graph = get_const_code_irg();
2491 type_t *type = skip_typeref(declaration->type);
2492 create_initializer_object(initializer, type, init_entity, NULL, 0);
2494 assert(current_ir_graph == get_const_code_irg());
2495 current_ir_graph = old_current_ir_graph;
2497 ir_node *src_addr = create_symconst(dbgi, init_entity);
2498 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2500 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2501 set_store(copyb_mem);
2504 static void create_initializer(declaration_t *declaration)
2506 initializer_t *initializer = declaration->init.initializer;
2507 if(initializer == NULL)
2510 declaration_type_t declaration_type
2511 = (declaration_type_t) declaration->declaration_type;
2512 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2513 create_initializer_local_variable_entity(declaration);
2517 if(initializer->type == INITIALIZER_VALUE) {
2518 initializer_value_t *initializer_value = &initializer->value;
2520 ir_node *value = expression_to_firm(initializer_value->value);
2522 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2523 set_value(declaration->v.value_number, value);
2525 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2527 ir_entity *entity = declaration->v.entity;
2529 set_entity_variability(entity, variability_initialized);
2530 set_atomic_ent_value(entity, value);
2533 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2534 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2536 ir_entity *entity = declaration->v.entity;
2537 set_entity_variability(entity, variability_initialized);
2539 type_t *type = skip_typeref(declaration->type);
2540 create_initializer_object(initializer, type, entity, NULL, 0);
2544 static void create_local_variable(declaration_t *declaration)
2546 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2548 bool needs_entity = declaration->address_taken;
2549 type_t *type = skip_typeref(declaration->type);
2551 if(is_type_array(type) || is_type_compound(type)) {
2552 needs_entity = true;
2556 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2557 create_declaration_entity(declaration,
2558 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2561 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2562 declaration->v.value_number = next_value_number_function;
2563 ++next_value_number_function;
2566 create_initializer(declaration);
2569 static void create_local_static_variable(declaration_t *declaration)
2571 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2573 type_t *type = skip_typeref(declaration->type);
2574 ir_type *global_type = get_glob_type();
2575 ident *id = unique_ident(declaration->symbol->string);
2576 ir_type *irtype = get_ir_type(type);
2577 ir_entity *entity = new_entity(global_type, id, irtype);
2578 set_entity_ld_ident(entity, id);
2580 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2581 declaration->v.entity = entity;
2582 set_entity_variability(entity, variability_uninitialized);
2583 set_entity_visibility(entity, visibility_local);
2585 ir_graph *old_current_ir_graph = current_ir_graph;
2586 current_ir_graph = get_const_code_irg();
2588 create_initializer(declaration);
2590 assert(current_ir_graph == get_const_code_irg());
2591 current_ir_graph = old_current_ir_graph;
2594 static void declaration_statement_to_firm(declaration_statement_t *statement)
2596 declaration_t *declaration = statement->declarations_begin;
2597 declaration_t *end = statement->declarations_end->next;
2598 for( ; declaration != end; declaration = declaration->next) {
2599 type_t *type = skip_typeref(declaration->type);
2601 switch ((storage_class_tag_t) declaration->storage_class) {
2602 case STORAGE_CLASS_TYPEDEF:
2604 case STORAGE_CLASS_STATIC:
2605 create_local_static_variable(declaration);
2607 case STORAGE_CLASS_ENUM_ENTRY:
2608 panic("enum entry declaration in local block found");
2609 case STORAGE_CLASS_EXTERN:
2610 panic("extern declaration in local block found");
2611 case STORAGE_CLASS_NONE:
2612 case STORAGE_CLASS_AUTO:
2613 case STORAGE_CLASS_REGISTER:
2614 if(is_type_function(type)) {
2615 panic("nested functions not supported yet");
2617 create_local_variable(declaration);
2620 case STORAGE_CLASS_THREAD:
2621 case STORAGE_CLASS_THREAD_EXTERN:
2622 case STORAGE_CLASS_THREAD_STATIC:
2625 panic("invalid storage class found");
2629 static void create_jump_statement(const statement_t *statement,
2630 ir_node *target_block)
2632 if(get_cur_block() == NULL)
2635 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2636 ir_node *jump = new_d_Jmp(dbgi);
2637 add_immBlock_pred(target_block, jump);
2639 set_cur_block(NULL);
2642 static void switch_statement_to_firm(const switch_statement_t *statement)
2644 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2646 ir_node *expression = expression_to_firm(statement->expression);
2647 ir_node *cond = new_d_Cond(dbgi, expression);
2648 ir_node *break_block = new_immBlock();
2650 set_cur_block(NULL);
2652 ir_node *const old_switch_cond = current_switch_cond;
2653 ir_node *const old_break_label = break_label;
2654 const bool old_saw_default_label = saw_default_label;
2655 current_switch_cond = cond;
2656 break_label = break_block;
2658 statement_to_firm(statement->body);
2660 if(get_cur_block() != NULL) {
2661 ir_node *jmp = new_Jmp();
2662 add_immBlock_pred(break_block, jmp);
2665 if (!saw_default_label) {
2666 set_cur_block(get_nodes_block(cond));
2667 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2668 MAGIC_DEFAULT_PN_NUMBER);
2669 add_immBlock_pred(break_block, proj);
2672 assert(current_switch_cond == cond);
2673 assert(break_label == break_block);
2674 current_switch_cond = old_switch_cond;
2675 break_label = old_break_label;
2676 saw_default_label = old_saw_default_label;
2678 mature_immBlock(break_block);
2679 set_cur_block(break_block);
2682 static long fold_constant(const expression_t *expression)
2684 ir_graph *old_current_ir_graph = current_ir_graph;
2685 current_ir_graph = get_const_code_irg();
2687 ir_node *cnst = expression_to_firm(expression);
2688 if(!is_Const(cnst)) {
2689 panic("couldn't fold constantl");
2691 tarval *tv = get_Const_tarval(cnst);
2692 if(!tarval_is_long(tv)) {
2693 panic("folded constant not an integer");
2696 long res = get_tarval_long(tv);
2698 current_ir_graph = old_current_ir_graph;
2702 static void case_label_to_firm(const case_label_statement_t *statement)
2704 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2706 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2708 /* let's create a node and hope firm constant folding creates a Const
2711 set_cur_block(get_nodes_block(current_switch_cond));
2712 if(statement->expression) {
2713 long pn = fold_constant(statement->expression);
2714 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2715 /* oops someone detected our cheating... */
2716 panic("magic default pn used");
2718 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2720 saw_default_label = true;
2721 proj = new_d_defaultProj(dbgi, current_switch_cond,
2722 MAGIC_DEFAULT_PN_NUMBER);
2725 ir_node *block = new_immBlock();
2726 if (fallthrough != NULL) {
2727 add_immBlock_pred(block, fallthrough);
2729 add_immBlock_pred(block, proj);
2730 mature_immBlock(block);
2732 statement_to_firm(statement->label_statement);
2735 static ir_node *get_label_block(declaration_t *label)
2737 assert(label->namespc == NAMESPACE_LABEL);
2739 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2740 return label->v.block;
2742 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2744 ir_node *old_cur_block = get_cur_block();
2745 ir_node *block = new_immBlock();
2746 set_cur_block(old_cur_block);
2748 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2749 label->v.block = block;
2751 ARR_APP1(ir_node *, imature_blocks, block);
2756 static void label_to_firm(const label_statement_t *statement)
2758 ir_node *block = get_label_block(statement->label);
2760 if(get_cur_block() != NULL) {
2761 ir_node *jmp = new_Jmp();
2762 add_immBlock_pred(block, jmp);
2765 set_cur_block(block);
2768 statement_to_firm(statement->label_statement);
2771 static void goto_to_firm(const goto_statement_t *statement)
2773 if(get_cur_block() == NULL)
2776 ir_node *block = get_label_block(statement->label);
2777 ir_node *jmp = new_Jmp();
2778 add_immBlock_pred(block, jmp);
2780 set_cur_block(NULL);
2783 static void statement_to_firm(statement_t *statement)
2785 switch(statement->type) {
2786 case STATEMENT_INVALID:
2787 panic("invalid statement found");
2788 case STATEMENT_COMPOUND:
2789 compound_statement_to_firm(&statement->compound);
2791 case STATEMENT_RETURN:
2792 return_statement_to_firm(&statement->returns);
2794 case STATEMENT_EXPRESSION:
2795 expression_statement_to_firm(&statement->expression);
2798 if_statement_to_firm(&statement->ifs);
2800 case STATEMENT_WHILE:
2801 while_statement_to_firm(&statement->whiles);
2803 case STATEMENT_DO_WHILE:
2804 do_while_statement_to_firm(&statement->do_while);
2806 case STATEMENT_DECLARATION:
2807 declaration_statement_to_firm(&statement->declaration);
2809 case STATEMENT_BREAK:
2810 create_jump_statement(statement, break_label);
2812 case STATEMENT_CONTINUE:
2813 create_jump_statement(statement, continue_label);
2815 case STATEMENT_SWITCH:
2816 switch_statement_to_firm(&statement->switchs);
2818 case STATEMENT_CASE_LABEL:
2819 case_label_to_firm(&statement->case_label);
2822 for_statement_to_firm(&statement->fors);
2824 case STATEMENT_LABEL:
2825 label_to_firm(&statement->label);
2827 case STATEMENT_GOTO:
2828 goto_to_firm(&statement->gotos);
2833 panic("Statement not implemented\n");
2836 static int count_local_declarations(const declaration_t * decl,
2837 const declaration_t *const end)
2840 for (; decl != end; decl = decl->next) {
2841 const type_t *type = skip_typeref(decl->type);
2842 switch (type->type) {
2846 if (!decl->address_taken)
2856 static int count_decls_in_stmts(const statement_t *stmt)
2859 for (; stmt != NULL; stmt = stmt->base.next) {
2860 switch (stmt->type) {
2861 case STATEMENT_DECLARATION: {
2862 const declaration_statement_t *const decl_stmt =
2863 (const declaration_statement_t*)stmt;
2864 count += count_local_declarations(decl_stmt->declarations_begin,
2865 decl_stmt->declarations_end->next);
2869 case STATEMENT_COMPOUND: {
2870 const compound_statement_t *const comp =
2871 (const compound_statement_t*)stmt;
2872 count += count_decls_in_stmts(comp->statements);
2876 case STATEMENT_IF: {
2877 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2878 count += count_decls_in_stmts(if_stmt->true_statement);
2879 count += count_decls_in_stmts(if_stmt->false_statement);
2883 case STATEMENT_SWITCH: {
2884 const switch_statement_t *const switch_stmt =
2885 (const switch_statement_t*)stmt;
2886 count += count_decls_in_stmts(switch_stmt->body);
2890 case STATEMENT_LABEL: {
2891 const label_statement_t *const label_stmt =
2892 (const label_statement_t*)stmt;
2893 count += count_decls_in_stmts(label_stmt->label_statement);
2897 case STATEMENT_WHILE: {
2898 const while_statement_t *const while_stmt =
2899 (const while_statement_t*)stmt;
2900 count += count_decls_in_stmts(while_stmt->body);
2904 case STATEMENT_DO_WHILE: {
2905 const do_while_statement_t *const do_while_stmt =
2906 (const do_while_statement_t*)stmt;
2907 count += count_decls_in_stmts(do_while_stmt->body);
2911 case STATEMENT_FOR: {
2912 const for_statement_t *const for_stmt =
2913 (const for_statement_t*)stmt;
2914 /* TODO initialisation */
2915 count += count_decls_in_stmts(for_stmt->body);
2920 case STATEMENT_BREAK:
2921 case STATEMENT_CASE_LABEL:
2922 case STATEMENT_CONTINUE:
2923 case STATEMENT_EXPRESSION:
2924 case STATEMENT_GOTO:
2925 case STATEMENT_INVALID:
2926 case STATEMENT_RETURN:
2933 static int get_function_n_local_vars(declaration_t *declaration)
2937 /* count parameters */
2938 count += count_local_declarations(declaration->context.declarations, NULL);
2940 /* count local variables declared in body */
2941 count += count_decls_in_stmts(declaration->init.statement);
2943 /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2944 * for expression statements... */
2950 static void initialize_function_parameters(declaration_t *declaration)
2952 ir_graph *irg = current_ir_graph;
2953 ir_node *args = get_irg_args(irg);
2954 ir_node *start_block = get_irg_start_block(irg);
2955 ir_type *function_irtype = get_ir_type(declaration->type);
2958 declaration_t *parameter = declaration->context.declarations;
2959 for( ; parameter != NULL; parameter = parameter->next, ++n) {
2960 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2961 type_t *type = skip_typeref(parameter->type);
2963 bool needs_entity = parameter->address_taken;
2964 assert(!is_type_array(type));
2965 if(is_type_compound(type)) {
2966 needs_entity = true;
2970 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2971 ident *id = new_id_from_str(parameter->symbol->string);
2972 set_entity_ident(entity, id);
2974 parameter->declaration_type
2975 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2976 parameter->v.entity = entity;
2980 ir_mode *mode = get_ir_mode(parameter->type);
2982 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2984 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2985 parameter->v.value_number = next_value_number_function;
2986 ++next_value_number_function;
2988 set_value(parameter->v.value_number, proj);
2992 static void create_function(declaration_t *declaration)
2994 ir_entity *function_entity = get_function_entity(declaration);
2996 if(declaration->init.statement == NULL)
2999 current_function_decl = declaration;
3000 current_function_name = NULL;
3002 assert(imature_blocks == NULL);
3003 imature_blocks = NEW_ARR_F(ir_node*, 0);
3005 int n_local_vars = get_function_n_local_vars(declaration);
3006 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3007 ir_node *first_block = get_cur_block();
3009 next_value_number_function = 0;
3010 initialize_function_parameters(declaration);
3012 statement_to_firm(declaration->init.statement);
3014 ir_node *end_block = get_irg_end_block(irg);
3016 /* do we have a return statement yet? */
3017 if(get_cur_block() != NULL) {
3018 type_t *type = skip_typeref(declaration->type);
3019 assert(is_type_function(type));
3020 const function_type_t *func_type = &type->function;
3021 const type_t *return_type
3022 = skip_typeref(func_type->return_type);
3025 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3026 ret = new_Return(get_store(), 0, NULL);
3029 if(is_type_scalar(return_type)) {
3030 mode = get_ir_mode(func_type->return_type);
3036 // ยง5.1.2.2.3 main implicitly returns 0
3037 if (strcmp(declaration->symbol->string, "main") == 0) {
3038 in[0] = new_Const(mode, get_mode_null(mode));
3040 in[0] = new_Unknown(mode);
3042 ret = new_Return(get_store(), 1, in);
3044 add_immBlock_pred(end_block, ret);
3047 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3048 mature_immBlock(imature_blocks[i]);
3050 DEL_ARR_F(imature_blocks);
3051 imature_blocks = NULL;
3053 mature_immBlock(first_block);
3054 mature_immBlock(end_block);
3056 irg_finalize_cons(irg);
3058 /* finalize the frame type */
3059 ir_type *frame_type = get_irg_frame_type(irg);
3060 int n = get_compound_n_members(frame_type);
3063 for(int i = 0; i < n; ++i) {
3064 ir_entity *entity = get_compound_member(frame_type, i);
3065 ir_type *entity_type = get_entity_type(entity);
3067 int align = get_type_alignment_bytes(entity_type);
3068 if(align > align_all)
3072 misalign = offset % align;
3074 offset += align - misalign;
3078 set_entity_offset(entity, offset);
3079 offset += get_type_size_bytes(entity_type);
3081 set_type_size_bytes(frame_type, offset);
3082 set_type_alignment_bytes(frame_type, align_all);
3083 set_type_state(frame_type, layout_fixed);
3088 static void create_global_variable(declaration_t *declaration)
3092 switch ((storage_class_tag_t)declaration->storage_class) {
3093 case STORAGE_CLASS_STATIC:
3094 vis = visibility_local;
3097 case STORAGE_CLASS_EXTERN:
3098 vis = visibility_external_allocated;
3101 case STORAGE_CLASS_NONE:
3102 vis = visibility_external_visible;
3105 case STORAGE_CLASS_THREAD:
3106 vis = visibility_external_visible;
3109 case STORAGE_CLASS_THREAD_EXTERN:
3110 vis = visibility_external_allocated;
3113 case STORAGE_CLASS_THREAD_STATIC:
3114 vis = visibility_local;
3118 var_type = get_tls_type();
3122 var_type = get_glob_type();
3126 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3128 set_entity_visibility(declaration->v.entity, vis);
3130 current_ir_graph = get_const_code_irg();
3131 create_initializer(declaration);
3134 case STORAGE_CLASS_TYPEDEF:
3135 case STORAGE_CLASS_AUTO:
3136 case STORAGE_CLASS_REGISTER:
3137 case STORAGE_CLASS_ENUM_ENTRY:
3140 panic("Invalid storage class for global variable");
3143 static void context_to_firm(context_t *context)
3145 /* first pass: create declarations */
3146 declaration_t *declaration = context->declarations;
3147 for( ; declaration != NULL; declaration = declaration->next) {
3148 if(declaration->namespc != NAMESPACE_NORMAL)
3150 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3151 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3153 if(declaration->symbol == NULL)
3156 type_t *type = skip_typeref(declaration->type);
3157 if(is_type_function(type)) {
3158 get_function_entity(declaration);
3160 create_global_variable(declaration);
3164 /* second pass: create code */
3165 declaration = context->declarations;
3166 for( ; declaration != NULL; declaration = declaration->next) {
3167 if(declaration->namespc != NAMESPACE_NORMAL)
3169 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3170 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3172 if(declaration->symbol == NULL)
3175 type_t *type = declaration->type;
3176 if(type->type != TYPE_FUNCTION)
3179 create_function(declaration);
3183 void translation_unit_to_firm(translation_unit_t *unit)
3185 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3186 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3187 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3189 ir_type_int = get_ir_type(type_int);
3190 ir_type_const_char = get_ir_type(type_const_char);
3191 ir_type_wchar_t = get_ir_type(type_wchar_t);
3192 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3195 type_void->base.firm_type = ir_type_void;
3197 /* just to be sure */
3198 continue_label = NULL;
3200 current_switch_cond = NULL;
3202 context_to_firm(& unit->context);