9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
15 #include "adt/array.h"
20 #include "lang_features.h"
22 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
24 static ir_type *ir_type_const_char;
25 static ir_type *ir_type_wchar_t;
26 static ir_type *ir_type_void;
27 static ir_type *ir_type_int;
29 static type_t *type_const_char;
30 static type_t *type_void;
31 static type_t *type_int;
33 static int next_value_number_function;
34 static ir_node *continue_label;
35 static ir_node *break_label;
36 static ir_node *current_switch_cond;
37 static bool saw_default_label;
38 static ir_node **imature_blocks;
40 static const declaration_t *current_function_decl;
41 static ir_node *current_function_name;
43 static struct obstack asm_obst;
45 typedef enum declaration_type_t {
46 DECLARATION_TYPE_UNKNOWN,
47 DECLARATION_TYPE_FUNCTION,
48 DECLARATION_TYPE_GLOBAL_VARIABLE,
49 DECLARATION_TYPE_LOCAL_VARIABLE,
50 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
51 DECLARATION_TYPE_COMPOUND_MEMBER,
52 DECLARATION_TYPE_LABEL_BLOCK,
53 DECLARATION_TYPE_ENUM_ENTRY
56 static ir_type *get_ir_type(type_t *type);
57 static int count_decls_in_stmts(const statement_t *stmt);
59 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
63 const declaration_t *declaration = & value_numbers[pos]->declaration;
65 print_warning_prefix(declaration->source_position);
66 fprintf(stderr, "variable '%s' might be used uninitialized\n",
67 declaration->symbol->string);
69 fprintf(stderr, "Some variable might be used uninitialized\n");
70 return new_r_Unknown(irg, mode);
73 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
75 const source_position_t *pos = (const source_position_t*) dbg;
78 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
82 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
84 const source_position_t *pos = (const source_position_t*) dbg;
89 return pos->input_name;
92 static dbg_info *get_dbg_info(const source_position_t *pos)
94 return (dbg_info*) pos;
97 static unsigned unique_id = 0;
99 static ident *unique_ident(const char *tag)
103 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
105 return new_id_from_str(buf);
109 * Return the signed integer mode of size bytes.
111 * @param size the size
113 static ir_mode *get_smode(unsigned size)
115 static ir_mode *s_modes[16 + 1] = {0, };
118 if (size <= 0 || size > 16)
127 snprintf(name, sizeof(name), "i%u", bits);
128 res = new_ir_mode(name, irms_int_number, bits, 1, irma_twos_complement,
129 bits <= machine_size ? machine_size : bits );
137 * Return the unsigned integer mode of size bytes.
139 * @param size the size
141 static ir_mode *get_umode(unsigned size)
143 static ir_mode *u_modes[16 + 1] = {0, };
146 if (size <= 0 || size > 16)
155 snprintf(name, sizeof(name), "u%u", bits);
156 res = new_ir_mode(name, irms_int_number, bits, 0, irma_twos_complement,
157 bits <= machine_size ? machine_size : bits );
165 * Return the pointer mode of size bytes.
167 * @param size the size
169 static ir_mode *get_ptrmode(unsigned size, char *name)
171 static ir_mode *p_modes[16 + 1] = {0, };
174 if (size <= 0 || size > 16)
184 snprintf(buf, sizeof(buf), "p%u", bits);
187 res = new_ir_mode(name, irms_reference, bits, 0, irma_twos_complement,
188 bits <= machine_size ? machine_size : bits);
192 set_reference_mode_signed_eq(res, get_smode(size));
193 set_reference_mode_unsigned_eq(res, get_umode(size));
198 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST];
200 static ir_mode *mode_int, *mode_uint;
203 * Initialises the atomic modes depending on the machine size.
205 static void init_atomic_modes(void) {
206 unsigned int_size = machine_size < 32 ? 2 : 4;
207 unsigned long_size = machine_size < 64 ? 4 : 8;
208 unsigned llong_size = machine_size < 32 ? 4 : 8;
210 /* firm has no real void... */
211 _atomic_modes[ATOMIC_TYPE_VOID] = mode_T;
212 _atomic_modes[ATOMIC_TYPE_CHAR] = char_is_signed ? get_smode(1) : get_umode(1);
213 _atomic_modes[ATOMIC_TYPE_SCHAR] = get_smode(1);
214 _atomic_modes[ATOMIC_TYPE_UCHAR] = get_umode(1);
215 _atomic_modes[ATOMIC_TYPE_SHORT] = get_smode(2);
216 _atomic_modes[ATOMIC_TYPE_USHORT] = get_umode(2);
217 _atomic_modes[ATOMIC_TYPE_INT] = get_smode(int_size);
218 _atomic_modes[ATOMIC_TYPE_UINT] = get_umode(int_size);
219 _atomic_modes[ATOMIC_TYPE_LONG] = get_smode(long_size);
220 _atomic_modes[ATOMIC_TYPE_ULONG] = get_umode(long_size);
221 _atomic_modes[ATOMIC_TYPE_LONGLONG] = get_smode(llong_size);
222 _atomic_modes[ATOMIC_TYPE_ULONGLONG] = get_umode(llong_size);
223 _atomic_modes[ATOMIC_TYPE_FLOAT] = mode_F;
224 _atomic_modes[ATOMIC_TYPE_DOUBLE] = mode_D;
225 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = mode_E;
227 #ifdef PROVIDE_COMPLEX
228 _atomic_modes[ATOMIC_TYPE_BOOL] = _atomic_modes[ATOMIC_TYPE_INT];
229 _atomic_modes[ATOMIC_TYPE_FLOAT_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_FLOAT];
230 _atomic_modes[ATOMIC_TYPE_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_DOUBLE];
231 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE];
234 /* Hmm, pointers should be machine size */
235 set_modeP_data(get_ptrmode(machine_size >> 3, NULL));
236 set_modeP_code(get_ptrmode(machine_size >> 3, NULL));
238 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
239 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
242 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
245 if ((unsigned)atomic_type->atype < (unsigned)ATOMIC_TYPE_LAST)
246 res = _atomic_modes[(unsigned)atomic_type->atype];
248 panic("Encountered unknown atomic type");
252 static unsigned get_type_size(type_t *type);
254 static unsigned get_atomic_type_size(const atomic_type_t *type)
256 switch(type->atype) {
257 case ATOMIC_TYPE_CHAR:
258 case ATOMIC_TYPE_SCHAR:
259 case ATOMIC_TYPE_UCHAR:
262 case ATOMIC_TYPE_SHORT:
263 case ATOMIC_TYPE_USHORT:
266 case ATOMIC_TYPE_BOOL:
267 case ATOMIC_TYPE_INT:
268 case ATOMIC_TYPE_UINT:
269 return machine_size >> 3;
271 case ATOMIC_TYPE_LONG:
272 case ATOMIC_TYPE_ULONG:
273 return machine_size > 16 ? machine_size >> 3 : 4;
275 case ATOMIC_TYPE_LONGLONG:
276 case ATOMIC_TYPE_ULONGLONG:
277 return machine_size > 16 ? 8 : 4;
279 case ATOMIC_TYPE_FLOAT:
282 case ATOMIC_TYPE_DOUBLE:
285 case ATOMIC_TYPE_LONG_DOUBLE:
288 case ATOMIC_TYPE_VOID:
291 case ATOMIC_TYPE_INVALID:
292 case ATOMIC_TYPE_LAST:
295 panic("Trying to determine size of invalid atomic type");
298 static unsigned get_compound_type_size(compound_type_t *type)
300 ir_type *irtype = get_ir_type((type_t*) type);
301 return get_type_size_bytes(irtype);
304 static unsigned get_array_type_size(array_type_t *type)
306 ir_type *irtype = get_ir_type((type_t*) type);
307 return get_type_size_bytes(irtype);
311 static unsigned get_type_size(type_t *type)
313 type = skip_typeref(type);
317 return get_atomic_type_size(&type->atomic);
319 return get_mode_size_bytes(mode_int);
320 case TYPE_COMPOUND_UNION:
321 case TYPE_COMPOUND_STRUCT:
322 return get_compound_type_size(&type->compound);
324 /* just a pointer to the function */
325 return get_mode_size_bytes(mode_P_code);
327 return get_mode_size_bytes(mode_P_data);
329 return get_array_type_size(&type->array);
331 return get_type_size(type->builtin.real_type);
337 panic("Trying to determine size of invalid type");
340 static unsigned count_parameters(const function_type_t *function_type)
344 function_parameter_t *parameter = function_type->parameters;
345 for ( ; parameter != NULL; parameter = parameter->next) {
355 static long fold_constant(const expression_t *expression);
357 static ir_type *create_atomic_type(const atomic_type_t *type)
359 ir_mode *mode = get_atomic_mode(type);
360 ident *id = get_mode_ident(mode);
361 ir_type *irtype = new_type_primitive(id, mode);
363 if(type->atype == ATOMIC_TYPE_LONG_DOUBLE) {
364 set_type_alignment_bytes(irtype, 4);
370 static ir_type *create_method_type(const function_type_t *function_type)
372 type_t *return_type = function_type->return_type;
374 ident *id = unique_ident("functiontype");
375 int n_parameters = count_parameters(function_type);
376 int n_results = return_type == type_void ? 0 : 1;
377 ir_type *irtype = new_type_method(id, n_parameters, n_results);
379 if(return_type != type_void) {
380 ir_type *restype = get_ir_type(return_type);
381 set_method_res_type(irtype, 0, restype);
384 function_parameter_t *parameter = function_type->parameters;
386 for( ; parameter != NULL; parameter = parameter->next) {
387 ir_type *p_irtype = get_ir_type(parameter->type);
388 set_method_param_type(irtype, n, p_irtype);
392 if(function_type->variadic || function_type->unspecified_parameters) {
393 set_method_variadicity(irtype, variadicity_variadic);
399 static ir_type *create_pointer_type(pointer_type_t *type)
401 type_t *points_to = type->points_to;
402 ir_type *ir_points_to;
403 /* Avoid endless recursion if the points_to type contains this poiner type
404 * again (might be a struct). We therefore first create a void* pointer
405 * and then set the real points_to type
407 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
408 ir_type_void, mode_P_data);
409 type->type.firm_type = ir_type;
411 ir_points_to = get_ir_type(points_to);
412 set_pointer_points_to_type(ir_type, ir_points_to);
417 static ir_type *create_array_type(array_type_t *type)
419 type_t *element_type = type->element_type;
420 ir_type *ir_element_type = get_ir_type(element_type);
422 ident *id = unique_ident("array");
423 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
425 if(type->size != NULL) {
426 int n_elements = fold_constant(type->size);
428 set_array_bounds_int(ir_type, 0, 0, n_elements);
430 size_t elemsize = get_type_size_bytes(ir_element_type);
431 int align = get_type_alignment_bytes(ir_element_type);
432 if(elemsize % align > 0) {
433 elemsize += align - (elemsize % align);
435 set_type_size_bytes(ir_type, n_elements * elemsize);
436 set_type_alignment_bytes(ir_type, align);
437 set_type_state(ir_type, layout_fixed);
439 set_array_lower_bound_int(ir_type, 0, 0);
445 #define INVALID_TYPE ((ir_type_ptr)-1)
447 static ir_type *create_struct_type(compound_type_t *type)
449 symbol_t *symbol = type->declaration->symbol;
452 id = unique_ident(symbol->string);
454 id = unique_ident("__anonymous_struct");
456 ir_type *ir_type = new_type_struct(id);
458 type->type.firm_type = ir_type;
462 declaration_t *entry = type->declaration->context.declarations;
463 for( ; entry != NULL; entry = entry->next) {
464 if(entry->namespc != NAMESPACE_NORMAL)
467 ident *ident = new_id_from_str(entry->symbol->string);
468 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
470 int entry_size = get_type_size_bytes(entry_ir_type);
471 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
472 int misalign = offset % entry_alignment;
474 offset += entry_alignment - misalign;
476 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
477 ir_entity *const entity = new_d_entity(ir_type, ident, entry_ir_type, dbgi);
478 set_entity_offset(entity, offset);
479 add_struct_member(ir_type, entity);
480 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
481 entry->v.entity = entity;
483 offset += entry_size;
484 if(entry_alignment > align_all) {
485 if(entry_alignment % align_all != 0) {
486 panic("Uneven alignments not supported yet");
488 align_all = entry_alignment;
492 int misalign = offset % align_all;
494 set_type_alignment_bytes(ir_type, align_all);
495 set_type_size_bytes(ir_type, offset);
496 set_type_state(ir_type, layout_fixed);
501 static ir_type *create_union_type(compound_type_t *type)
503 declaration_t *declaration = type->declaration;
504 symbol_t *symbol = declaration->symbol;
507 id = unique_ident(symbol->string);
509 id = unique_ident("__anonymous_union");
511 ir_type *ir_type = new_type_union(id);
513 type->type.firm_type = ir_type;
517 declaration_t *entry = declaration->context.declarations;
518 for( ; entry != NULL; entry = entry->next) {
519 if(entry->namespc != NAMESPACE_NORMAL)
522 ident *ident = new_id_from_str(entry->symbol->string);
523 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
525 int entry_size = get_type_size_bytes(entry_ir_type);
526 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
528 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
529 ir_entity *const entity = new_d_entity(ir_type, ident, entry_ir_type, dbgi);
530 add_union_member(ir_type, entity);
531 set_entity_offset(entity, 0);
532 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
533 entry->v.entity = entity;
535 if(entry_size > size) {
538 if(entry_alignment > align_all) {
539 if(entry_alignment % align_all != 0) {
540 panic("Uneven alignments not supported yet");
542 align_all = entry_alignment;
546 set_type_alignment_bytes(ir_type, align_all);
547 set_type_size_bytes(ir_type, size);
548 set_type_state(ir_type, layout_fixed);
553 static ir_node *expression_to_firm(const expression_t *expression);
554 static inline ir_mode *get_ir_mode(type_t *type);
556 static ir_type *create_enum_type(enum_type_t *const type)
558 type->type.firm_type = ir_type_int;
560 ir_mode *const mode = get_ir_mode((type_t*) type);
561 tarval *const one = get_mode_one(mode);
562 tarval * tv_next = get_tarval_null(mode);
564 declaration_t *declaration = type->declaration->next;
565 for (; declaration != NULL; declaration = declaration->next) {
566 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
569 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
571 expression_t *const init = declaration->init.enum_value;
573 ir_node *const cnst = expression_to_firm(init);
574 if (!is_Const(cnst)) {
575 panic("couldn't fold constant");
577 tv_next = get_Const_tarval(cnst);
579 declaration->v.enum_val = tv_next;
580 tv_next = tarval_add(tv_next, one);
586 static ir_type *get_ir_type(type_t *type)
588 assert(type != NULL);
590 type = skip_typeref(type);
592 if(type->base.firm_type != NULL) {
593 assert(type->base.firm_type != INVALID_TYPE);
594 return type->base.firm_type;
597 ir_type *firm_type = NULL;
600 firm_type = create_atomic_type(&type->atomic);
603 firm_type = create_method_type(&type->function);
606 firm_type = create_pointer_type(&type->pointer);
609 firm_type = create_array_type(&type->array);
611 case TYPE_COMPOUND_STRUCT:
612 firm_type = create_struct_type(&type->compound);
614 case TYPE_COMPOUND_UNION:
615 firm_type = create_union_type(&type->compound);
618 firm_type = create_enum_type(&type->enumt);
621 firm_type = get_ir_type(type->builtin.real_type);
628 if(firm_type == NULL)
629 panic("unknown type found");
631 type->base.firm_type = firm_type;
635 static inline ir_mode *get_ir_mode(type_t *type)
637 ir_type *irtype = get_ir_type(type);
639 /* firm doesn't report a mode for arrays somehow... */
640 if(is_Array_type(irtype)) {
644 ir_mode *mode = get_type_mode(irtype);
645 assert(mode != NULL);
649 static ir_entity* get_function_entity(declaration_t *declaration)
651 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
652 return declaration->v.entity;
653 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
655 symbol_t *symbol = declaration->symbol;
656 ident *id = new_id_from_str(symbol->string);
658 ir_type *global_type = get_glob_type();
659 ir_type *ir_type_method = get_ir_type(declaration->type);
660 assert(is_Method_type(ir_type_method));
662 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
663 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
664 set_entity_ld_ident(entity, id);
665 if(declaration->storage_class == STORAGE_CLASS_STATIC
666 || declaration->is_inline) {
667 set_entity_visibility(entity, visibility_local);
668 } else if(declaration->init.statement != NULL) {
669 set_entity_visibility(entity, visibility_external_visible);
671 set_entity_visibility(entity, visibility_external_allocated);
673 set_entity_allocation(entity, allocation_static);
675 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
676 declaration->v.entity = entity;
681 static ir_node *const_to_firm(const const_expression_t *cnst)
683 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
684 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
689 if(mode_is_float(mode)) {
690 tv = new_tarval_from_double(cnst->v.float_value, mode);
692 if(mode_is_signed(mode)) {
693 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
695 len = snprintf(buf, sizeof(buf), "%llu",
696 (unsigned long long) cnst->v.int_value);
698 tv = new_tarval_from_str(buf, len, mode);
701 return new_d_Const(dbgi, mode, tv);
704 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
706 assert(entity != NULL);
707 union symconst_symbol sym;
708 sym.entity_p = entity;
709 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
712 static ir_node *string_to_firm(const source_position_t *const src_pos,
713 const char *const id_prefix,
714 const char *const string)
716 ir_type *const global_type = get_glob_type();
717 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
720 ident *const id = unique_ident(id_prefix);
721 dbg_info *const dbgi = get_dbg_info(src_pos);
722 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
723 set_entity_ld_ident(entity, id);
724 set_entity_variability(entity, variability_constant);
725 set_entity_allocation(entity, allocation_static);
727 ir_type *const elem_type = ir_type_const_char;
728 ir_mode *const mode = get_type_mode(elem_type);
730 const size_t slen = strlen(string) + 1;
732 set_array_lower_bound_int(type, 0, 0);
733 set_array_upper_bound_int(type, 0, slen);
734 set_type_size_bytes(type, slen);
735 set_type_state(type, layout_fixed);
737 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
738 for(size_t i = 0; i < slen; ++i) {
739 tvs[i] = new_tarval_from_long(string[i], mode);
742 set_array_entity_values(entity, tvs, slen);
745 return create_symconst(dbgi, entity);
748 static ir_node *string_literal_to_firm(
749 const string_literal_expression_t* literal)
751 return string_to_firm(&literal->expression.source_position, "Lstr",
755 static ir_node *wide_string_literal_to_firm(
756 const wide_string_literal_expression_t* const literal)
758 ir_type *const global_type = get_glob_type();
759 ir_type *const elem_type = ir_type_wchar_t;
760 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
763 ident *const id = unique_ident("Lstr");
764 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
765 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
766 set_entity_ld_ident(entity, id);
767 set_entity_variability(entity, variability_constant);
768 set_entity_allocation(entity, allocation_static);
770 ir_mode *const mode = get_type_mode(elem_type);
772 const wchar_rep_t *const string = literal->value.begin;
773 const size_t slen = literal->value.size;
775 set_array_lower_bound_int(type, 0, 0);
776 set_array_upper_bound_int(type, 0, slen);
777 set_type_size_bytes(type, slen);
778 set_type_state(type, layout_fixed);
780 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
781 for(size_t i = 0; i < slen; ++i) {
782 tvs[i] = new_tarval_from_long(string[i], mode);
785 set_array_entity_values(entity, tvs, slen);
788 return create_symconst(dbgi, entity);
791 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
792 dbg_info *const dbgi)
794 if(is_compound_type(irtype) || is_Array_type(irtype)) {
798 ir_mode *const mode = get_type_mode(irtype);
799 ir_node *const memory = get_store();
800 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
801 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
802 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
807 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
809 ir_mode *mode = get_irn_mode(node);
811 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
813 if(!mode_is_float(mode))
816 /* check if there is already a Conv */
817 if (get_irn_op(node) == op_Conv) {
818 /* convert it into a strict Conv */
819 set_Conv_strict(node, 1);
823 /* otherwise create a new one */
824 return new_d_strictConv(dbgi, node, mode);
827 static ir_node *get_global_var_address(dbg_info *const dbgi,
828 const declaration_t *const decl)
830 assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
832 ir_entity *const entity = decl->v.entity;
833 switch ((storage_class_tag_t)decl->storage_class) {
834 case STORAGE_CLASS_THREAD:
835 case STORAGE_CLASS_THREAD_EXTERN:
836 case STORAGE_CLASS_THREAD_STATIC: {
837 ir_node *const no_mem = new_NoMem();
838 ir_node *const tls = get_irg_tls(current_ir_graph);
839 return new_d_simpleSel(dbgi, no_mem, tls, entity);
843 return create_symconst(dbgi, entity);
847 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
849 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
850 declaration_t *declaration = ref->declaration;
851 type_t *type = skip_typeref(declaration->type);
853 switch((declaration_type_t) declaration->declaration_type) {
854 case DECLARATION_TYPE_UNKNOWN:
855 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
861 case DECLARATION_TYPE_ENUM_ENTRY: {
862 ir_mode *const mode = get_ir_mode(type);
863 return new_Const(mode, declaration->v.enum_val);
866 case DECLARATION_TYPE_LOCAL_VARIABLE: {
867 ir_mode *mode = get_ir_mode(type);
868 return get_value(declaration->v.value_number, mode);
870 case DECLARATION_TYPE_FUNCTION: {
871 return create_symconst(dbgi, declaration->v.entity);
873 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
874 ir_node *const addr = get_global_var_address(dbgi, declaration);
875 ir_type *const irtype = get_entity_type(declaration->v.entity);
876 return deref_address(irtype, addr, dbgi);
879 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
880 ir_entity *entity = declaration->v.entity;
881 ir_node *frame = get_irg_frame(current_ir_graph);
882 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
883 ir_type *irtype = get_entity_type(entity);
884 return deref_address(irtype, sel, dbgi);
887 case DECLARATION_TYPE_COMPOUND_MEMBER:
888 case DECLARATION_TYPE_LABEL_BLOCK:
889 panic("not implemented reference type");
892 panic("reference to declaration with unknown type found");
895 static ir_node *reference_addr(const reference_expression_t *ref)
897 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
898 declaration_t *declaration = ref->declaration;
900 switch((declaration_type_t) declaration->declaration_type) {
901 case DECLARATION_TYPE_UNKNOWN:
903 case DECLARATION_TYPE_LOCAL_VARIABLE:
904 panic("local variable without entity has no address");
905 case DECLARATION_TYPE_FUNCTION: {
906 return create_symconst(dbgi, declaration->v.entity);
908 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
909 ir_node *const addr = get_global_var_address(dbgi, declaration);
912 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
913 ir_entity *entity = declaration->v.entity;
914 ir_node *frame = get_irg_frame(current_ir_graph);
915 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
920 case DECLARATION_TYPE_ENUM_ENTRY:
921 panic("trying to reference enum entry");
923 case DECLARATION_TYPE_COMPOUND_MEMBER:
924 case DECLARATION_TYPE_LABEL_BLOCK:
925 panic("not implemented reference type");
928 panic("reference to declaration with unknown type found");
931 static ir_node *process_builtin_call(const call_expression_t *call)
933 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
935 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
936 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
938 type_t *type = skip_typeref(builtin->expression.datatype);
939 assert(is_type_pointer(type));
941 type_t *function_type = skip_typeref(type->pointer.points_to);
942 symbol_t *symbol = builtin->symbol;
945 case T___builtin_alloca: {
946 if(call->arguments == NULL || call->arguments->next != NULL) {
947 panic("invalid number of parameters on __builtin_alloca");
949 expression_t *argument = call->arguments->expression;
950 ir_node *size = expression_to_firm(argument);
952 ir_node *store = get_store();
953 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
955 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
957 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
961 case T___builtin_nan:
962 case T___builtin_nanf:
963 case T___builtin_nand: {
964 /* Ignore string for now... */
965 assert(is_type_function(function_type));
966 ir_mode *mode = get_ir_mode(function_type->function.return_type);
967 tarval *tv = get_mode_NAN(mode);
968 ir_node *res = new_d_Const(dbgi, mode, tv);
971 case T___builtin_va_end:
974 panic("Unsupported builtin found\n");
978 static ir_node *call_expression_to_firm(const call_expression_t *call)
980 assert(get_cur_block() != NULL);
982 expression_t *function = call->function;
983 if(function->type == EXPR_BUILTIN_SYMBOL) {
984 return process_builtin_call(call);
986 ir_node *callee = expression_to_firm(function);
988 type_t *type = skip_typeref(function->base.datatype);
989 assert(is_type_pointer(type));
990 pointer_type_t *pointer_type = &type->pointer;
991 type_t *points_to = skip_typeref(pointer_type->points_to);
992 assert(is_type_function(points_to));
993 function_type_t *function_type = &points_to->function;
995 int n_parameters = 0;
996 call_argument_t *argument = call->arguments;
997 for( ; argument != NULL; argument = argument->next) {
1001 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1003 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1004 ir_type *new_method_type = NULL;
1005 if(function_type->variadic || function_type->unspecified_parameters) {
1006 /* we need to construct a new method type matching the call
1008 int n_res = get_method_n_ress(ir_method_type);
1009 new_method_type = new_type_method(unique_ident("calltype"),
1010 n_parameters, n_res);
1011 set_method_calling_convention(new_method_type,
1012 get_method_calling_convention(ir_method_type));
1013 set_method_additional_properties(new_method_type,
1014 get_method_additional_properties(ir_method_type));
1016 for(int i = 0; i < n_res; ++i) {
1017 set_method_res_type(new_method_type, i,
1018 get_method_res_type(ir_method_type, i));
1021 ir_node *in[n_parameters];
1023 argument = call->arguments;
1025 for( ; argument != NULL; argument = argument->next) {
1026 expression_t *expression = argument->expression;
1027 ir_node *arg_node = expression_to_firm(expression);
1029 arg_node = do_strict_conv(dbgi, arg_node);
1032 if(new_method_type != NULL) {
1033 ir_type *irtype = get_ir_type(expression->base.datatype);
1034 set_method_param_type(new_method_type, n, irtype);
1039 assert(n == n_parameters);
1041 if(new_method_type != NULL)
1042 ir_method_type = new_method_type;
1044 ir_node *store = get_store();
1045 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1047 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1050 type_t *return_type = skip_typeref(function_type->return_type);
1051 ir_node *result = NULL;
1053 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1055 if(is_type_scalar(return_type)) {
1056 mode = get_ir_mode(return_type);
1060 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1061 result = new_d_Proj(dbgi, resproj, mode, 0);
1067 static void statement_to_firm(statement_t *statement);
1068 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1070 static ir_node *expression_to_addr(const expression_t *expression);
1071 static void create_condition_evaluation(const expression_t *expression,
1072 ir_node *true_block,
1073 ir_node *false_block);
1075 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1078 value = do_strict_conv(dbgi, value);
1080 ir_node *memory = get_store();
1082 if(is_type_scalar(type)) {
1083 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1084 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1085 set_store(store_mem);
1087 ir_type *irtype = get_ir_type(type);
1088 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1089 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1090 set_store(copyb_mem);
1094 static void set_value_for_expression(const expression_t *expression,
1097 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1098 value = do_strict_conv(dbgi, value);
1100 if(expression->type == EXPR_REFERENCE) {
1101 reference_expression_t *ref = (reference_expression_t*) expression;
1103 declaration_t *declaration = ref->declaration;
1104 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
1105 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1106 set_value(declaration->v.value_number, value);
1111 ir_node *addr = expression_to_addr(expression);
1112 type_t *type = skip_typeref(expression->base.datatype);
1113 assign_value(dbgi, addr, type, value);
1116 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1118 ir_mode *value_mode = get_irn_mode(value);
1120 if (value_mode == dest_mode || is_Bad(value))
1123 if(dest_mode == mode_b) {
1124 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1125 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1126 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1130 return new_d_Conv(dbgi, value, dest_mode);
1133 static ir_node *create_incdec(const unary_expression_t *expression)
1135 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1136 type_t *type = skip_typeref(expression->expression.datatype);
1137 ir_mode *mode = get_ir_mode(type);
1138 expression_t *value = expression->value;
1140 ir_node *value_node = expression_to_firm(value);
1143 if(is_type_pointer(type)) {
1144 pointer_type_t *pointer_type = &type->pointer;
1145 unsigned elem_size = get_type_size(pointer_type->points_to);
1146 offset = new_Const_long(mode_int, elem_size);
1148 assert(is_type_arithmetic(type));
1149 offset = new_Const(mode, get_mode_one(mode));
1152 switch(expression->expression.type) {
1153 case EXPR_UNARY_POSTFIX_INCREMENT: {
1154 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1155 set_value_for_expression(value, new_value);
1158 case EXPR_UNARY_POSTFIX_DECREMENT: {
1159 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1160 set_value_for_expression(value, new_value);
1163 case EXPR_UNARY_PREFIX_INCREMENT: {
1164 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1165 set_value_for_expression(value, new_value);
1168 case EXPR_UNARY_PREFIX_DECREMENT: {
1169 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1170 set_value_for_expression(value, new_value);
1174 panic("no incdec expr in create_incdec");
1179 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1181 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1182 type_t *type = skip_typeref(expression->expression.datatype);
1184 if(expression->expression.type == EXPR_UNARY_TAKE_ADDRESS)
1185 return expression_to_addr(expression->value);
1187 const expression_t *value = expression->value;
1188 ir_node *value_node = expression_to_firm(value);
1190 switch(expression->expression.type) {
1191 case EXPR_UNARY_NEGATE: {
1192 ir_mode *mode = get_ir_mode(type);
1193 return new_d_Minus(dbgi, value_node, mode);
1195 case EXPR_UNARY_PLUS:
1197 case EXPR_UNARY_BITWISE_NEGATE: {
1198 ir_mode *mode = get_ir_mode(type);
1199 return new_d_Not(dbgi, value_node, mode);
1201 case EXPR_UNARY_NOT: {
1202 ir_mode *mode = get_ir_mode(type);
1203 if(get_irn_mode(value_node) != mode_b) {
1204 value_node = create_conv(dbgi, value_node, mode_b);
1206 value_node = new_d_Not(dbgi, value_node, mode_b);
1207 if(mode != mode_b) {
1208 value_node = create_conv(dbgi, value_node, mode);
1212 case EXPR_UNARY_DEREFERENCE: {
1213 type_t *value_type = skip_typeref(value->base.datatype);
1214 ir_type *irtype = get_ir_type(value_type);
1215 assert(is_Pointer_type(irtype));
1216 ir_type *points_to = get_pointer_points_to_type(irtype);
1217 return deref_address(points_to, value_node, dbgi);
1219 case EXPR_UNARY_POSTFIX_INCREMENT:
1220 case EXPR_UNARY_POSTFIX_DECREMENT:
1221 case EXPR_UNARY_PREFIX_INCREMENT:
1222 case EXPR_UNARY_PREFIX_DECREMENT:
1223 return create_incdec(expression);
1224 case EXPR_UNARY_CAST: {
1225 ir_mode *mode = get_ir_mode(type);
1226 ir_node *node = create_conv(dbgi, value_node, mode);
1227 node = do_strict_conv(dbgi, node);
1230 case EXPR_UNARY_CAST_IMPLICIT: {
1231 ir_mode *mode = get_ir_mode(type);
1232 return create_conv(dbgi, value_node, mode);
1238 panic("invalid UNEXPR type found");
1241 static long get_pnc(const expression_type_t type)
1244 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1245 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1246 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Ne;
1247 case EXPR_BINARY_ISLESS:
1248 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1249 case EXPR_BINARY_ISLESSEQUAL:
1250 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1251 case EXPR_BINARY_ISGREATER:
1252 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1253 case EXPR_BINARY_ISGREATEREQUAL:
1254 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1255 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1260 panic("trying to get pn_Cmp from non-comparison binexpr type");
1263 static ir_node *create_lazy_op(const binary_expression_t *expression)
1265 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1266 type_t *type = expression->expression.datatype;
1267 ir_mode *mode = get_ir_mode(type);
1269 ir_node *cur_block = get_cur_block();
1271 ir_node *one_block = new_immBlock();
1272 ir_node *one = new_Const(mode, get_mode_one(mode));
1273 ir_node *jmp_one = new_d_Jmp(dbgi);
1275 ir_node *zero_block = new_immBlock();
1276 ir_node *zero = new_Const(mode, get_mode_null(mode));
1277 ir_node *jmp_zero = new_d_Jmp(dbgi);
1279 set_cur_block(cur_block);
1280 create_condition_evaluation((const expression_t*) expression,
1281 one_block, zero_block);
1282 mature_immBlock(one_block);
1283 mature_immBlock(zero_block);
1285 ir_node *common_block = new_immBlock();
1286 add_immBlock_pred(common_block, jmp_one);
1287 add_immBlock_pred(common_block, jmp_zero);
1288 mature_immBlock(common_block);
1290 ir_node *in[2] = { one, zero };
1291 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1296 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1297 ir_node *right, ir_mode *mode);
1299 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1300 create_arithmetic_func func)
1302 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1303 ir_node *left = expression_to_firm(expression->left);
1304 ir_node *right = expression_to_firm(expression->right);
1305 type_t *type = expression->right->base.datatype;
1306 /* be careful with the modes, because in arithmetic assign nodes only
1307 * the right operand has the mode of the arithmetic already */
1308 ir_mode *mode = get_ir_mode(type);
1309 left = create_conv(dbgi, left, mode);
1310 ir_node *res = func(dbgi, left, right, mode);
1315 static ir_node *pointer_arithmetic(ir_node *const pointer,
1318 dbg_info *const dbgi,
1319 const create_arithmetic_func func)
1321 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1322 type_t *const points_to = pointer_type->points_to;
1323 const unsigned elem_size = get_type_size(points_to);
1325 assert(elem_size >= 1);
1326 if (elem_size > 1) {
1327 integer = create_conv(dbgi, integer, mode_int);
1328 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1329 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1333 ir_mode *const mode = get_ir_mode(type);
1334 return func(dbgi, pointer, integer, mode);
1337 static ir_node *create_arithmetic_assign_binop(
1338 const binary_expression_t *expression, create_arithmetic_func func)
1340 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1341 type_t *const type = skip_typeref(expression->expression.datatype);
1344 if (is_type_pointer(type)) {
1345 ir_node *const pointer = expression_to_firm(expression->left);
1346 ir_node * integer = expression_to_firm(expression->right);
1347 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1349 value = create_arithmetic_binop(expression, func);
1352 ir_mode *const mode = get_ir_mode(type);
1353 value = create_conv(dbgi, value, mode);
1354 set_value_for_expression(expression->left, value);
1359 static ir_node *create_add(const binary_expression_t *expression)
1361 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1362 ir_node *left = expression_to_firm(expression->left);
1363 ir_node *right = expression_to_firm(expression->right);
1364 type_t *type = expression->expression.datatype;
1366 expression_t *expr_left = expression->left;
1367 expression_t *expr_right = expression->right;
1368 type_t *type_left = skip_typeref(expr_left->base.datatype);
1369 type_t *type_right = skip_typeref(expr_right->base.datatype);
1371 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1372 ir_mode *const mode = get_ir_mode(type);
1373 return new_d_Add(dbgi, left, right, mode);
1376 if (is_type_pointer(type_left)) {
1377 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1379 assert(is_type_pointer(type_right));
1380 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1384 static ir_node *create_sub(const binary_expression_t *expression)
1386 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1387 expression_t *const expr_left = expression->left;
1388 expression_t *const expr_right = expression->right;
1389 ir_node *const left = expression_to_firm(expr_left);
1390 ir_node *const right = expression_to_firm(expr_right);
1391 type_t *const type = expression->expression.datatype;
1392 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1393 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1395 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1396 ir_mode *const mode = get_ir_mode(type);
1397 return new_d_Sub(dbgi, left, right, mode);
1398 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1399 const pointer_type_t *const ptr_type = &type_left->pointer;
1400 const unsigned elem_size = get_type_size(ptr_type->points_to);
1401 ir_mode *const mode = get_ir_mode(type);
1402 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1403 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1404 ir_node *const no_mem = new_NoMem();
1405 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1406 op_pin_state_floats);
1407 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1410 assert(is_type_pointer(type_left));
1411 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1414 static ir_node *create_shift(const binary_expression_t *expression)
1416 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1417 ir_node *left = expression_to_firm(expression->left);
1418 ir_node *right = expression_to_firm(expression->right);
1419 type_t *type = expression->expression.datatype;
1420 ir_mode *mode = get_ir_mode(type);
1422 /* firm always wants the shift count to be unsigned */
1423 right = create_conv(dbgi, right, mode_uint);
1427 switch(expression->expression.type) {
1428 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1429 case EXPR_BINARY_SHIFTLEFT:
1430 res = new_d_Shl(dbgi, left, right, mode);
1432 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1433 case EXPR_BINARY_SHIFTRIGHT: {
1434 expression_t *expr_left = expression->left;
1435 type_t *type_left = skip_typeref(expr_left->base.datatype);
1437 if(is_type_signed(type_left)) {
1438 res = new_d_Shrs(dbgi, left, right, mode);
1440 res = new_d_Shr(dbgi, left, right, mode);
1445 panic("create shift op called for non-shift op");
1452 static ir_node *create_divmod(const binary_expression_t *expression)
1454 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1455 ir_node *left = expression_to_firm(expression->left);
1456 ir_node *right = expression_to_firm(expression->right);
1457 ir_node *pin = new_Pin(new_NoMem());
1458 /* be careful with the modes, because in arithmetic assign nodes only
1459 * the right operand has the mode of the arithmetic already */
1460 type_t *type = expression->right->base.datatype;
1461 ir_mode *mode = get_ir_mode(type);
1462 left = create_conv(dbgi, left, mode);
1466 switch (expression->expression.type) {
1467 case EXPR_BINARY_DIV:
1468 case EXPR_BINARY_DIV_ASSIGN:
1469 if(mode_is_float(mode)) {
1470 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1471 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1473 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1474 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1478 case EXPR_BINARY_MOD:
1479 case EXPR_BINARY_MOD_ASSIGN:
1480 assert(!mode_is_float(mode));
1481 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1482 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1485 default: panic("unexpected binary expression type in create_divmod()");
1491 static ir_node *create_arithmetic_assign_divmod(
1492 const binary_expression_t *expression)
1494 ir_node * value = create_divmod(expression);
1495 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1496 type_t *const type = expression->expression.datatype;
1497 ir_mode *const mode = get_ir_mode(type);
1499 assert(type->type != TYPE_POINTER);
1501 value = create_conv(dbgi, value, mode);
1502 set_value_for_expression(expression->left, value);
1507 static ir_node *create_arithmetic_assign_shift(
1508 const binary_expression_t *expression)
1510 ir_node * value = create_shift(expression);
1511 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1512 type_t *const type = expression->expression.datatype;
1513 ir_mode *const mode = get_ir_mode(type);
1515 value = create_conv(dbgi, value, mode);
1516 set_value_for_expression(expression->left, value);
1521 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1523 expression_type_t type = expression->expression.type;
1526 case EXPR_BINARY_EQUAL:
1527 case EXPR_BINARY_NOTEQUAL:
1528 case EXPR_BINARY_LESS:
1529 case EXPR_BINARY_LESSEQUAL:
1530 case EXPR_BINARY_GREATER:
1531 case EXPR_BINARY_GREATEREQUAL:
1532 case EXPR_BINARY_ISGREATER:
1533 case EXPR_BINARY_ISGREATEREQUAL:
1534 case EXPR_BINARY_ISLESS:
1535 case EXPR_BINARY_ISLESSEQUAL:
1536 case EXPR_BINARY_ISLESSGREATER:
1537 case EXPR_BINARY_ISUNORDERED: {
1538 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1539 ir_node *left = expression_to_firm(expression->left);
1540 ir_node *right = expression_to_firm(expression->right);
1541 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1542 long pnc = get_pnc(type);
1543 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1546 case EXPR_BINARY_ASSIGN: {
1547 ir_node *right = expression_to_firm(expression->right);
1548 set_value_for_expression(expression->left, right);
1552 case EXPR_BINARY_ADD:
1553 return create_add(expression);
1554 case EXPR_BINARY_SUB:
1555 return create_sub(expression);
1556 case EXPR_BINARY_MUL:
1557 return create_arithmetic_binop(expression, new_d_Mul);
1558 case EXPR_BINARY_BITWISE_AND:
1559 return create_arithmetic_binop(expression, new_d_And);
1560 case EXPR_BINARY_BITWISE_OR:
1561 return create_arithmetic_binop(expression, new_d_Or);
1562 case EXPR_BINARY_BITWISE_XOR:
1563 return create_arithmetic_binop(expression, new_d_Eor);
1564 case EXPR_BINARY_SHIFTLEFT:
1565 case EXPR_BINARY_SHIFTRIGHT:
1566 return create_shift(expression);
1567 case EXPR_BINARY_DIV:
1568 case EXPR_BINARY_MOD:
1569 return create_divmod(expression);
1570 case EXPR_BINARY_LOGICAL_AND:
1571 case EXPR_BINARY_LOGICAL_OR:
1572 return create_lazy_op(expression);
1573 case EXPR_BINARY_COMMA:
1574 expression_to_firm(expression->left);
1575 return expression_to_firm(expression->right);
1576 case EXPR_BINARY_ADD_ASSIGN:
1577 return create_arithmetic_assign_binop(expression, new_d_Add);
1578 case EXPR_BINARY_SUB_ASSIGN:
1579 return create_arithmetic_assign_binop(expression, new_d_Sub);
1580 case EXPR_BINARY_MUL_ASSIGN:
1581 return create_arithmetic_assign_binop(expression, new_d_Mul);
1582 case EXPR_BINARY_DIV_ASSIGN:
1583 return create_arithmetic_assign_divmod(expression);
1584 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1585 return create_arithmetic_assign_binop(expression, new_d_And);
1586 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1587 return create_arithmetic_assign_binop(expression, new_d_Or);
1588 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1589 return create_arithmetic_assign_binop(expression, new_d_Eor);
1590 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1591 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1592 return create_arithmetic_assign_shift(expression);
1594 panic("TODO binexpr type");
1598 static ir_node *array_access_addr(const array_access_expression_t *expression)
1600 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1601 ir_node *base_addr = expression_to_firm(expression->array_ref);
1602 ir_node *offset = expression_to_firm(expression->index);
1603 offset = create_conv(dbgi, offset, mode_uint);
1605 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1606 assert(is_type_pointer(ref_type));
1607 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1609 unsigned elem_size = get_type_size(pointer_type->points_to);
1610 ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
1611 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1613 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
1618 static ir_node *array_access_to_firm(
1619 const array_access_expression_t *expression)
1621 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1622 ir_node *addr = array_access_addr(expression);
1623 type_t *type = revert_automatic_type_conversion(
1624 (const expression_t*) expression);
1625 type = skip_typeref(type);
1626 ir_type *irtype = get_ir_type(type);
1628 return deref_address(irtype, addr, dbgi);
1631 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1633 type_t *type = expression->type;
1635 type = expression->size_expression->base.datatype;
1636 assert(type != NULL);
1639 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1640 unsigned size = get_type_size(type);
1641 ir_node *size_node = new_Const_long(mode, size);
1646 static long fold_constant(const expression_t *expression)
1648 assert(is_constant_expression(expression));
1650 ir_graph *old_current_ir_graph = current_ir_graph;
1651 if(current_ir_graph == NULL) {
1652 current_ir_graph = get_const_code_irg();
1655 ir_node *cnst = expression_to_firm(expression);
1656 current_ir_graph = old_current_ir_graph;
1658 if(!is_Const(cnst)) {
1659 panic("couldn't fold constant\n");
1662 tarval *tv = get_Const_tarval(cnst);
1663 if(!tarval_is_long(tv)) {
1664 panic("result of constant folding is not integer\n");
1667 return get_tarval_long(tv);
1670 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1672 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1674 /* first try to fold a constant condition */
1675 if(is_constant_expression(expression->condition)) {
1676 long val = fold_constant(expression->condition);
1678 return expression_to_firm(expression->true_expression);
1680 return expression_to_firm(expression->false_expression);
1684 ir_node *cur_block = get_cur_block();
1686 /* create the true block */
1687 ir_node *true_block = new_immBlock();
1689 ir_node *true_val = expression_to_firm(expression->true_expression);
1690 ir_node *true_jmp = new_Jmp();
1692 /* create the false block */
1693 ir_node *false_block = new_immBlock();
1695 ir_node *false_val = expression_to_firm(expression->false_expression);
1696 ir_node *false_jmp = new_Jmp();
1698 /* create the condition evaluation */
1699 set_cur_block(cur_block);
1700 create_condition_evaluation(expression->condition, true_block, false_block);
1701 mature_immBlock(true_block);
1702 mature_immBlock(false_block);
1704 /* create the common block */
1705 ir_node *common_block = new_immBlock();
1706 add_immBlock_pred(common_block, true_jmp);
1707 add_immBlock_pred(common_block, false_jmp);
1708 mature_immBlock(common_block);
1710 /* TODO improve static semantics, so either both or no values are NULL */
1711 if (true_val == NULL || false_val == NULL)
1714 ir_node *in[2] = { true_val, false_val };
1715 ir_mode *mode = get_irn_mode(true_val);
1716 assert(get_irn_mode(false_val) == mode);
1717 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1722 static ir_node *select_addr(const select_expression_t *expression)
1724 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1726 ir_node *compound_addr = expression_to_firm(expression->compound);
1728 declaration_t *entry = expression->compound_entry;
1729 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1730 ir_entity *entity = entry->v.entity;
1732 assert(entity != NULL);
1734 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1739 static ir_node *select_to_firm(const select_expression_t *expression)
1741 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1742 ir_node *addr = select_addr(expression);
1743 type_t *type = revert_automatic_type_conversion(
1744 (const expression_t*) expression);
1745 type = skip_typeref(type);
1746 ir_type *irtype = get_ir_type(type);
1748 return deref_address(irtype, addr, dbgi);
1751 /* Values returned by __builtin_classify_type. */
1752 typedef enum gcc_type_class
1758 enumeral_type_class,
1761 reference_type_class,
1765 function_type_class,
1776 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1778 const type_t *const type = expr->type_expression->base.datatype;
1784 const atomic_type_t *const atomic_type = &type->atomic;
1785 switch (atomic_type->atype) {
1786 /* should not be reached */
1787 case ATOMIC_TYPE_INVALID:
1791 /* gcc cannot do that */
1792 case ATOMIC_TYPE_VOID:
1793 tc = void_type_class;
1796 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
1797 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
1798 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
1799 case ATOMIC_TYPE_SHORT:
1800 case ATOMIC_TYPE_USHORT:
1801 case ATOMIC_TYPE_INT:
1802 case ATOMIC_TYPE_UINT:
1803 case ATOMIC_TYPE_LONG:
1804 case ATOMIC_TYPE_ULONG:
1805 case ATOMIC_TYPE_LONGLONG:
1806 case ATOMIC_TYPE_ULONGLONG:
1807 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
1808 tc = integer_type_class;
1811 case ATOMIC_TYPE_FLOAT:
1812 case ATOMIC_TYPE_DOUBLE:
1813 case ATOMIC_TYPE_LONG_DOUBLE:
1814 tc = real_type_class;
1817 #ifdef PROVIDE_COMPLEX
1818 case ATOMIC_TYPE_FLOAT_COMPLEX:
1819 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1820 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1821 tc = complex_type_class;
1823 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1824 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1825 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1826 tc = complex_type_class;
1831 panic("Unimplemented case in classify_type_to_firm().");
1836 case TYPE_ARRAY: /* gcc handles this as pointer */
1837 case TYPE_FUNCTION: /* gcc handles this as pointer */
1838 case TYPE_POINTER: tc = pointer_type_class; break;
1839 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1840 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1842 /* gcc handles this as integer */
1843 case TYPE_ENUM: tc = integer_type_class; break;
1846 panic("Unimplemented case in classify_type_to_firm().");
1849 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1850 ir_mode *const mode = mode_int;
1851 tarval *const tv = new_tarval_from_long(tc, mode);
1852 return new_d_Const(dbgi, mode, tv);
1855 static ir_node *function_name_to_firm(
1856 const string_literal_expression_t *const expr)
1858 if (current_function_name == NULL) {
1859 const source_position_t *const src_pos =
1860 &expr->expression.source_position;
1861 const char *const name = current_function_decl->symbol->string;
1862 current_function_name = string_to_firm(src_pos, "__func__", name);
1865 return current_function_name;
1868 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1870 statement_t *statement = expr->statement;
1872 assert(statement->type == STATEMENT_COMPOUND);
1873 return compound_statement_to_firm((compound_statement_t*) statement);
1876 static ir_node *va_start_expression_to_firm(
1877 const va_start_expression_t *const expr)
1879 ir_type *const method_type = get_ir_type(current_function_decl->type);
1880 int const n = get_method_n_params(method_type) - 1;
1881 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
1882 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
1883 dbg_info *const dbgi =
1884 get_dbg_info(&expr->expression.source_position);
1885 ir_node *const no_mem = new_NoMem();
1886 ir_node *const arg_sel =
1887 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
1889 size_t const parm_size = get_type_size(expr->parameter->type);
1890 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
1891 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
1892 set_value_for_expression(expr->ap, add);
1897 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
1899 ir_type *const irtype = get_ir_type(expr->expression.datatype);
1900 ir_node *const ap = expression_to_firm(expr->ap);
1901 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1902 ir_node *const res = deref_address(irtype, ap, dbgi);
1904 size_t const parm_size = get_type_size(expr->expression.datatype);
1905 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
1906 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
1907 set_value_for_expression(expr->ap, add);
1912 static ir_node *dereference_addr(const unary_expression_t *const expression)
1914 assert(expression->expression.type == EXPR_UNARY_DEREFERENCE);
1915 return expression_to_firm(expression->value);
1918 static ir_node *expression_to_addr(const expression_t *expression)
1920 switch(expression->type) {
1921 case EXPR_REFERENCE:
1922 return reference_addr(&expression->reference);
1923 case EXPR_ARRAY_ACCESS:
1924 return array_access_addr(&expression->array_access);
1926 return select_addr(&expression->select);
1928 return call_expression_to_firm(&expression->call);
1929 case EXPR_UNARY_DEREFERENCE: {
1930 return dereference_addr(&expression->unary);
1935 panic("trying to get address of non-lvalue");
1938 static ir_node *_expression_to_firm(const expression_t *expression)
1940 switch(expression->type) {
1942 return const_to_firm(&expression->conste);
1943 case EXPR_STRING_LITERAL:
1944 return string_literal_to_firm(&expression->string);
1945 case EXPR_WIDE_STRING_LITERAL:
1946 return wide_string_literal_to_firm(&expression->wide_string);
1947 case EXPR_REFERENCE:
1948 return reference_expression_to_firm(&expression->reference);
1950 return call_expression_to_firm(&expression->call);
1952 return unary_expression_to_firm(&expression->unary);
1954 return binary_expression_to_firm(&expression->binary);
1955 case EXPR_ARRAY_ACCESS:
1956 return array_access_to_firm(&expression->array_access);
1958 return sizeof_to_firm(&expression->sizeofe);
1959 case EXPR_CONDITIONAL:
1960 return conditional_to_firm(&expression->conditional);
1962 return select_to_firm(&expression->select);
1963 case EXPR_CLASSIFY_TYPE:
1964 return classify_type_to_firm(&expression->classify_type);
1966 case EXPR_PRETTY_FUNCTION:
1967 return function_name_to_firm(&expression->string);
1968 case EXPR_STATEMENT:
1969 return statement_expression_to_firm(&expression->statement);
1971 return va_start_expression_to_firm(&expression->va_starte);
1973 return va_arg_expression_to_firm(&expression->va_arge);
1975 case EXPR_BUILTIN_SYMBOL:
1976 panic("unimplemented expression found");
1982 panic("invalid expression found");
1985 static ir_node *expression_to_firm(const expression_t *expression)
1987 ir_node *res = _expression_to_firm(expression);
1989 if(res != NULL && get_irn_mode(res) == mode_b) {
1990 ir_mode *mode = get_ir_mode(expression->base.datatype);
1991 res = create_conv(NULL, res, mode);
1997 static ir_node *expression_to_modeb(const expression_t *expression)
1999 ir_node *res = _expression_to_firm(expression);
2000 res = create_conv(NULL, res, mode_b);
2006 * create a short-circuit expression evaluation that tries to construct
2007 * efficient control flow structures for &&, || and ! expressions
2009 static void create_condition_evaluation(const expression_t *expression,
2010 ir_node *true_block,
2011 ir_node *false_block)
2013 switch(expression->type) {
2014 case EXPR_UNARY_NOT: {
2015 const unary_expression_t *unary_expression = &expression->unary;
2016 create_condition_evaluation(unary_expression->value, false_block,
2020 case EXPR_BINARY_LOGICAL_AND: {
2021 const binary_expression_t *binary_expression = &expression->binary;
2023 ir_node *cur_block = get_cur_block();
2024 ir_node *extra_block = new_immBlock();
2025 set_cur_block(cur_block);
2026 create_condition_evaluation(binary_expression->left, extra_block,
2028 mature_immBlock(extra_block);
2029 set_cur_block(extra_block);
2030 create_condition_evaluation(binary_expression->right, true_block,
2034 case EXPR_BINARY_LOGICAL_OR: {
2035 const binary_expression_t *binary_expression = &expression->binary;
2037 ir_node *cur_block = get_cur_block();
2038 ir_node *extra_block = new_immBlock();
2039 set_cur_block(cur_block);
2040 create_condition_evaluation(binary_expression->left, true_block,
2042 mature_immBlock(extra_block);
2043 set_cur_block(extra_block);
2044 create_condition_evaluation(binary_expression->right, true_block,
2052 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2053 ir_node *condition = expression_to_modeb(expression);
2054 ir_node *cond = new_d_Cond(dbgi, condition);
2055 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2056 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2058 add_immBlock_pred(true_block, true_proj);
2059 add_immBlock_pred(false_block, false_proj);
2061 set_cur_block(NULL);
2066 static void create_declaration_entity(declaration_t *declaration,
2067 declaration_type_t declaration_type,
2068 ir_type *parent_type)
2070 ident *const id = new_id_from_str(declaration->symbol->string);
2071 ir_type *const irtype = get_ir_type(declaration->type);
2072 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2073 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2074 set_entity_ld_ident(entity, id);
2076 declaration->declaration_type = (unsigned char) declaration_type;
2077 declaration->v.entity = entity;
2078 set_entity_variability(entity, variability_uninitialized);
2079 if(parent_type == get_tls_type())
2080 set_entity_allocation(entity, allocation_automatic);
2081 else if(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE)
2082 set_entity_allocation(entity, allocation_static);
2083 /* TODO: visibility? */
2086 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2088 enum compound_graph_entry_type_t {
2089 COMPOUND_GRAPH_ENTRY_ARRAY,
2090 COMPOUND_GRAPH_ENTRY_COMPOUND
2093 struct compound_graph_path_entry_t {
2099 compound_graph_path_entry_t *prev;
2102 static void create_initializer_object(initializer_t *initializer, type_t *type,
2103 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2105 static compound_graph_path *create_compound_path(ir_type *type,
2106 compound_graph_path_entry_t *entry, int len)
2108 compound_graph_path *path = new_compound_graph_path(type, len);
2111 for( ; entry != NULL; entry = entry->prev, --i) {
2113 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2114 set_compound_graph_path_node(path, i, entry->v.entity);
2116 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2117 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2125 static void create_initializer_value(initializer_value_t *initializer,
2127 compound_graph_path_entry_t *entry,
2130 ir_node *node = expression_to_firm(initializer->value);
2131 ir_type *type = get_entity_type(entity);
2132 compound_graph_path *path = create_compound_path(type, entry, len);
2133 add_compound_ent_value_w_path(entity, node, path);
2136 static void create_initializer_compound(initializer_list_t *initializer,
2137 compound_type_t *type,
2139 compound_graph_path_entry_t *last_entry,
2142 declaration_t *compound_declaration = type->declaration;
2144 declaration_t *compound_entry = compound_declaration->context.declarations;
2146 compound_graph_path_entry_t entry;
2147 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2148 entry.prev = last_entry;
2152 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2153 if(compound_entry->symbol == NULL)
2155 if(compound_entry->namespc != NAMESPACE_NORMAL)
2158 if(i >= initializer->len)
2161 entry.v.entity = compound_entry->v.entity;
2163 initializer_t *sub_initializer = initializer->initializers[i];
2165 assert(compound_entry != NULL);
2166 assert(compound_entry->declaration_type
2167 == DECLARATION_TYPE_COMPOUND_MEMBER);
2169 if(sub_initializer->type == INITIALIZER_VALUE) {
2170 create_initializer_value(&sub_initializer->value,
2171 entity, &entry, len);
2173 type_t *entry_type = skip_typeref(compound_entry->type);
2174 create_initializer_object(sub_initializer, entry_type, entity,
2182 static void create_initializer_array(initializer_list_t *initializer,
2183 array_type_t *type, ir_entity *entity,
2184 compound_graph_path_entry_t *last_entry,
2187 type_t *element_type = type->element_type;
2188 element_type = skip_typeref(element_type);
2190 compound_graph_path_entry_t entry;
2191 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2192 entry.prev = last_entry;
2196 for(i = 0; i < initializer->len; ++i) {
2197 entry.v.array_index = i;
2199 initializer_t *sub_initializer = initializer->initializers[i];
2201 if(sub_initializer->type == INITIALIZER_VALUE) {
2202 create_initializer_value(&sub_initializer->value,
2203 entity, &entry, len);
2205 create_initializer_object(sub_initializer, element_type, entity,
2211 /* TODO: initialize rest... */
2212 if(type->size_expression != NULL) {
2213 size_t array_len = fold_constant(type->size_expression);
2214 for( ; i < array_len; ++i) {
2221 static void create_initializer_string(initializer_string_t *initializer,
2222 array_type_t *type, ir_entity *entity,
2223 compound_graph_path_entry_t *last_entry,
2226 type_t *element_type = type->element_type;
2227 element_type = skip_typeref(element_type);
2229 compound_graph_path_entry_t entry;
2230 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2231 entry.prev = last_entry;
2234 ir_type *irtype = get_entity_type(entity);
2235 size_t arr_len = get_array_type_size(type);
2236 const char *p = initializer->string;
2238 for(i = 0; i < arr_len; ++i, ++p) {
2239 entry.v.array_index = i;
2241 ir_node *node = new_Const_long(mode_Bs, *p);
2242 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2243 add_compound_ent_value_w_path(entity, node, path);
2250 static void create_initializer_wide_string(
2251 const initializer_wide_string_t *const initializer, array_type_t *const type,
2252 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2255 type_t *element_type = type->element_type;
2256 element_type = skip_typeref(element_type);
2258 compound_graph_path_entry_t entry;
2259 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2260 entry.prev = last_entry;
2263 ir_type *const irtype = get_entity_type(entity);
2264 const size_t arr_len = get_array_type_size(type);
2265 const wchar_rep_t * p = initializer->string.begin;
2266 const wchar_rep_t *const end = p + initializer->string.size;
2267 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2268 entry.v.array_index = i;
2270 ir_node *node = new_Const_long(mode_int, *p);
2271 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2272 add_compound_ent_value_w_path(entity, node, path);
2276 static void create_initializer_object(initializer_t *initializer, type_t *type,
2277 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2279 if(is_type_array(type)) {
2280 array_type_t *array_type = &type->array;
2282 switch (initializer->type) {
2283 case INITIALIZER_STRING: {
2284 initializer_string_t *const string = &initializer->string;
2285 create_initializer_string(string, array_type, entity, entry, len);
2289 case INITIALIZER_WIDE_STRING: {
2290 initializer_wide_string_t *const string = &initializer->wide_string;
2291 create_initializer_wide_string(string, array_type, entity, entry, len);
2295 case INITIALIZER_LIST: {
2296 initializer_list_t *const list = &initializer->list;
2297 create_initializer_array(list, array_type, entity, entry, len);
2301 case INITIALIZER_VALUE:
2304 panic("Unhandled initializer");
2306 assert(initializer->type == INITIALIZER_LIST);
2307 initializer_list_t *list = &initializer->list;
2309 assert(is_type_compound(type));
2310 compound_type_t *compound_type = &type->compound;
2311 create_initializer_compound(list, compound_type, entity, entry, len);
2315 static void create_initializer_local_variable_entity(declaration_t *declaration)
2317 initializer_t *initializer = declaration->init.initializer;
2318 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2319 ir_entity *entity = declaration->v.entity;
2320 ir_node *memory = get_store();
2321 ir_node *nomem = new_NoMem();
2322 ir_node *frame = get_irg_frame(current_ir_graph);
2323 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2325 if(initializer->type == INITIALIZER_VALUE) {
2326 initializer_value_t *initializer_value = &initializer->value;
2328 ir_node *value = expression_to_firm(initializer_value->value);
2329 type_t *type = skip_typeref(declaration->type);
2330 assign_value(dbgi, addr, type, value);
2334 /* create a "template" entity which is copied to the entity on the stack */
2335 ident *const id = unique_ident("initializer");
2336 ir_type *const irtype = get_ir_type(declaration->type);
2337 ir_type *const global_type = get_glob_type();
2338 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
2339 set_entity_ld_ident(init_entity, id);
2341 set_entity_variability(init_entity, variability_initialized);
2342 set_entity_visibility(init_entity, visibility_local);
2343 set_entity_allocation(init_entity, allocation_static);
2345 ir_graph *const old_current_ir_graph = current_ir_graph;
2346 current_ir_graph = get_const_code_irg();
2348 type_t *const type = skip_typeref(declaration->type);
2349 create_initializer_object(initializer, type, init_entity, NULL, 0);
2351 assert(current_ir_graph == get_const_code_irg());
2352 current_ir_graph = old_current_ir_graph;
2354 ir_node *const src_addr = create_symconst(dbgi, init_entity);
2355 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2357 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2358 set_store(copyb_mem);
2361 static void create_initializer(declaration_t *declaration)
2363 initializer_t *initializer = declaration->init.initializer;
2364 if(initializer == NULL)
2367 declaration_type_t declaration_type
2368 = (declaration_type_t) declaration->declaration_type;
2369 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2370 create_initializer_local_variable_entity(declaration);
2374 if(initializer->type == INITIALIZER_VALUE) {
2375 initializer_value_t *initializer_value = &initializer->value;
2377 ir_node *value = expression_to_firm(initializer_value->value);
2379 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2380 set_value(declaration->v.value_number, value);
2382 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2384 ir_entity *entity = declaration->v.entity;
2386 set_entity_variability(entity, variability_initialized);
2387 set_atomic_ent_value(entity, value);
2390 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2391 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2393 ir_entity *entity = declaration->v.entity;
2394 set_entity_variability(entity, variability_initialized);
2396 type_t *type = skip_typeref(declaration->type);
2397 create_initializer_object(initializer, type, entity, NULL, 0);
2401 static void create_local_variable(declaration_t *declaration)
2403 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2405 bool needs_entity = declaration->address_taken;
2406 type_t *type = skip_typeref(declaration->type);
2408 if(is_type_array(type) || is_type_compound(type)) {
2409 needs_entity = true;
2413 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2414 create_declaration_entity(declaration,
2415 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2418 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2419 declaration->v.value_number = next_value_number_function;
2420 ++next_value_number_function;
2423 create_initializer(declaration);
2426 static void create_local_static_variable(declaration_t *declaration)
2428 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2430 type_t *const type = skip_typeref(declaration->type);
2431 ir_type *const global_type = get_glob_type();
2432 ident *const id = unique_ident(declaration->symbol->string);
2433 ir_type *const irtype = get_ir_type(type);
2434 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2435 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
2436 set_entity_ld_ident(entity, id);
2438 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2439 declaration->v.entity = entity;
2440 set_entity_variability(entity, variability_uninitialized);
2441 set_entity_visibility(entity, visibility_local);
2442 set_entity_allocation(entity, allocation_static);
2444 ir_graph *const old_current_ir_graph = current_ir_graph;
2445 current_ir_graph = get_const_code_irg();
2447 create_initializer(declaration);
2449 assert(current_ir_graph == get_const_code_irg());
2450 current_ir_graph = old_current_ir_graph;
2455 static void return_statement_to_firm(return_statement_t *statement)
2457 if(get_cur_block() == NULL)
2460 ir_type *func_irtype = get_ir_type(current_function_decl->type);
2462 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2466 if(get_method_n_ress(func_irtype) > 0) {
2467 ir_type *res_type = get_method_res_type(func_irtype, 0);
2469 if(statement->return_value != NULL) {
2470 ir_node *node = expression_to_firm(statement->return_value);
2471 node = do_strict_conv(dbgi, node);
2475 if(is_compound_type(res_type)) {
2478 mode = get_type_mode(res_type);
2480 in[0] = new_Unknown(mode);
2484 /* build return_value for its side effects */
2485 if(statement->return_value != NULL) {
2486 expression_to_firm(statement->return_value);
2491 ir_node *store = get_store();
2492 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
2494 ir_node *end_block = get_irg_end_block(current_ir_graph);
2495 add_immBlock_pred(end_block, ret);
2497 set_cur_block(NULL);
2500 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
2502 if(get_cur_block() == NULL)
2505 return expression_to_firm(statement->expression);
2508 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
2510 ir_node *result = NULL;
2511 statement_t *statement = compound->statements;
2512 for( ; statement != NULL; statement = statement->base.next) {
2513 //context2firm(&statement->context);
2515 if(statement->base.next == NULL
2516 && statement->type == STATEMENT_EXPRESSION) {
2517 result = expression_statement_to_firm(
2518 (expression_statement_t*) statement);
2521 statement_to_firm(statement);
2527 static void create_local_declaration(declaration_t *declaration)
2529 type_t *type = skip_typeref(declaration->type);
2531 switch ((storage_class_tag_t) declaration->storage_class) {
2532 case STORAGE_CLASS_STATIC:
2533 create_local_static_variable(declaration);
2535 case STORAGE_CLASS_ENUM_ENTRY:
2536 panic("enum entry declaration in local block found");
2537 case STORAGE_CLASS_EXTERN:
2538 panic("extern declaration in local block found");
2539 case STORAGE_CLASS_NONE:
2540 case STORAGE_CLASS_AUTO:
2541 case STORAGE_CLASS_REGISTER:
2542 if(is_type_function(type)) {
2543 panic("nested functions not supported yet");
2545 create_local_variable(declaration);
2548 case STORAGE_CLASS_TYPEDEF:
2549 case STORAGE_CLASS_THREAD:
2550 case STORAGE_CLASS_THREAD_EXTERN:
2551 case STORAGE_CLASS_THREAD_STATIC:
2554 panic("invalid storage class found");
2557 static void declaration_statement_to_firm(declaration_statement_t *statement)
2559 declaration_t *declaration = statement->declarations_begin;
2560 declaration_t *end = statement->declarations_end->next;
2561 for( ; declaration != end; declaration = declaration->next) {
2562 create_local_variable(declaration);
2566 static void if_statement_to_firm(if_statement_t *statement)
2568 ir_node *cur_block = get_cur_block();
2570 ir_node *fallthrough_block = new_immBlock();
2572 /* the true (blocks) */
2573 ir_node *true_block;
2574 if (statement->true_statement != NULL) {
2575 true_block = new_immBlock();
2576 statement_to_firm(statement->true_statement);
2577 if(get_cur_block() != NULL) {
2578 ir_node *jmp = new_Jmp();
2579 add_immBlock_pred(fallthrough_block, jmp);
2582 true_block = fallthrough_block;
2585 /* the false (blocks) */
2586 ir_node *false_block;
2587 if(statement->false_statement != NULL) {
2588 false_block = new_immBlock();
2590 statement_to_firm(statement->false_statement);
2591 if(get_cur_block() != NULL) {
2592 ir_node *jmp = new_Jmp();
2593 add_immBlock_pred(fallthrough_block, jmp);
2596 false_block = fallthrough_block;
2599 /* create the condition */
2600 if(cur_block != NULL) {
2601 set_cur_block(cur_block);
2602 create_condition_evaluation(statement->condition, true_block,
2606 mature_immBlock(true_block);
2607 if(false_block != fallthrough_block) {
2608 mature_immBlock(false_block);
2610 mature_immBlock(fallthrough_block);
2612 set_cur_block(fallthrough_block);
2615 static void while_statement_to_firm(while_statement_t *statement)
2617 ir_node *jmp = NULL;
2618 if(get_cur_block() != NULL) {
2622 /* create the header block */
2623 ir_node *header_block = new_immBlock();
2625 add_immBlock_pred(header_block, jmp);
2628 /* the false block */
2629 ir_node *false_block = new_immBlock();
2632 ir_node *body_block;
2633 if (statement->body != NULL) {
2634 ir_node *old_continue_label = continue_label;
2635 ir_node *old_break_label = break_label;
2636 continue_label = header_block;
2637 break_label = false_block;
2639 body_block = new_immBlock();
2640 statement_to_firm(statement->body);
2642 assert(continue_label == header_block);
2643 assert(break_label == false_block);
2644 continue_label = old_continue_label;
2645 break_label = old_break_label;
2647 if(get_cur_block() != NULL) {
2649 add_immBlock_pred(header_block, jmp);
2652 body_block = header_block;
2655 /* create the condition */
2656 set_cur_block(header_block);
2658 create_condition_evaluation(statement->condition, body_block, false_block);
2659 mature_immBlock(body_block);
2660 mature_immBlock(false_block);
2661 mature_immBlock(header_block);
2663 set_cur_block(false_block);
2666 static void do_while_statement_to_firm(do_while_statement_t *statement)
2668 ir_node *jmp = NULL;
2669 if(get_cur_block() != NULL) {
2673 /* create the header block */
2674 ir_node *header_block = new_immBlock();
2676 /* the false block */
2677 ir_node *false_block = new_immBlock();
2680 ir_node *body_block = new_immBlock();
2682 add_immBlock_pred(body_block, jmp);
2685 if (statement->body != NULL) {
2686 ir_node *old_continue_label = continue_label;
2687 ir_node *old_break_label = break_label;
2688 continue_label = header_block;
2689 break_label = false_block;
2691 statement_to_firm(statement->body);
2693 assert(continue_label == header_block);
2694 assert(break_label == false_block);
2695 continue_label = old_continue_label;
2696 break_label = old_break_label;
2698 if (get_cur_block() == NULL) {
2699 mature_immBlock(header_block);
2700 mature_immBlock(body_block);
2701 mature_immBlock(false_block);
2706 ir_node *body_jmp = new_Jmp();
2707 add_immBlock_pred(header_block, body_jmp);
2708 mature_immBlock(header_block);
2710 /* create the condition */
2711 set_cur_block(header_block);
2713 create_condition_evaluation(statement->condition, body_block, false_block);
2714 mature_immBlock(body_block);
2715 mature_immBlock(false_block);
2716 mature_immBlock(header_block);
2718 set_cur_block(false_block);
2721 static void for_statement_to_firm(for_statement_t *statement)
2723 ir_node *jmp = NULL;
2724 if (get_cur_block() != NULL) {
2725 if(statement->initialisation != NULL) {
2726 expression_to_firm(statement->initialisation);
2729 /* create declarations */
2730 declaration_t *declaration = statement->context.declarations;
2731 for( ; declaration != NULL; declaration = declaration->next) {
2732 create_local_declaration(declaration);
2739 /* create the step block */
2740 ir_node *const step_block = new_immBlock();
2741 if (statement->step != NULL) {
2742 expression_to_firm(statement->step);
2744 ir_node *const step_jmp = new_Jmp();
2746 /* create the header block */
2747 ir_node *const header_block = new_immBlock();
2749 add_immBlock_pred(header_block, jmp);
2751 add_immBlock_pred(header_block, step_jmp);
2753 /* the false block */
2754 ir_node *const false_block = new_immBlock();
2757 ir_node * body_block;
2758 if (statement->body != NULL) {
2759 ir_node *const old_continue_label = continue_label;
2760 ir_node *const old_break_label = break_label;
2761 continue_label = step_block;
2762 break_label = false_block;
2764 body_block = new_immBlock();
2765 statement_to_firm(statement->body);
2767 assert(continue_label == step_block);
2768 assert(break_label == false_block);
2769 continue_label = old_continue_label;
2770 break_label = old_break_label;
2772 if (get_cur_block() != NULL) {
2774 add_immBlock_pred(step_block, jmp);
2777 body_block = step_block;
2780 /* create the condition */
2781 set_cur_block(header_block);
2782 if (statement->condition != NULL) {
2783 create_condition_evaluation(statement->condition, body_block,
2786 keep_alive(header_block);
2788 add_immBlock_pred(body_block, jmp);
2791 mature_immBlock(body_block);
2792 mature_immBlock(false_block);
2793 mature_immBlock(step_block);
2794 mature_immBlock(header_block);
2795 mature_immBlock(false_block);
2797 set_cur_block(false_block);
2800 static void create_jump_statement(const statement_t *statement,
2801 ir_node *target_block)
2803 if(get_cur_block() == NULL)
2806 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2807 ir_node *jump = new_d_Jmp(dbgi);
2808 add_immBlock_pred(target_block, jump);
2810 set_cur_block(NULL);
2813 static void switch_statement_to_firm(const switch_statement_t *statement)
2815 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2817 ir_node *expression = expression_to_firm(statement->expression);
2818 ir_node *cond = new_d_Cond(dbgi, expression);
2819 ir_node *break_block = new_immBlock();
2821 set_cur_block(NULL);
2823 ir_node *const old_switch_cond = current_switch_cond;
2824 ir_node *const old_break_label = break_label;
2825 const bool old_saw_default_label = saw_default_label;
2826 current_switch_cond = cond;
2827 break_label = break_block;
2829 statement_to_firm(statement->body);
2831 if(get_cur_block() != NULL) {
2832 ir_node *jmp = new_Jmp();
2833 add_immBlock_pred(break_block, jmp);
2836 if (!saw_default_label) {
2837 set_cur_block(get_nodes_block(cond));
2838 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2839 MAGIC_DEFAULT_PN_NUMBER);
2840 add_immBlock_pred(break_block, proj);
2843 assert(current_switch_cond == cond);
2844 assert(break_label == break_block);
2845 current_switch_cond = old_switch_cond;
2846 break_label = old_break_label;
2847 saw_default_label = old_saw_default_label;
2849 mature_immBlock(break_block);
2850 set_cur_block(break_block);
2853 static void case_label_to_firm(const case_label_statement_t *statement)
2855 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2857 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2859 /* let's create a node and hope firm constant folding creates a Const
2862 set_cur_block(get_nodes_block(current_switch_cond));
2863 if(statement->expression) {
2864 long pn = fold_constant(statement->expression);
2865 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2866 /* oops someone detected our cheating... */
2867 panic("magic default pn used");
2869 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2871 saw_default_label = true;
2872 proj = new_d_defaultProj(dbgi, current_switch_cond,
2873 MAGIC_DEFAULT_PN_NUMBER);
2876 ir_node *block = new_immBlock();
2877 if (fallthrough != NULL) {
2878 add_immBlock_pred(block, fallthrough);
2880 add_immBlock_pred(block, proj);
2881 mature_immBlock(block);
2883 statement_to_firm(statement->label_statement);
2886 static ir_node *get_label_block(declaration_t *label)
2888 assert(label->namespc == NAMESPACE_LABEL);
2890 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2891 return label->v.block;
2893 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2895 ir_node *old_cur_block = get_cur_block();
2896 ir_node *block = new_immBlock();
2897 set_cur_block(old_cur_block);
2899 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2900 label->v.block = block;
2902 ARR_APP1(ir_node *, imature_blocks, block);
2907 static void label_to_firm(const label_statement_t *statement)
2909 ir_node *block = get_label_block(statement->label);
2911 if(get_cur_block() != NULL) {
2912 ir_node *jmp = new_Jmp();
2913 add_immBlock_pred(block, jmp);
2916 set_cur_block(block);
2919 statement_to_firm(statement->label_statement);
2922 static void goto_to_firm(const goto_statement_t *statement)
2924 if(get_cur_block() == NULL)
2927 ir_node *block = get_label_block(statement->label);
2928 ir_node *jmp = new_Jmp();
2929 add_immBlock_pred(block, jmp);
2931 set_cur_block(NULL);
2934 typedef enum modifier_t {
2935 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
2936 ASM_MODIFIER_READ_WRITE = 1 << 1,
2937 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
2938 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
2942 static void asm_statement_to_firm(const asm_statement_t *statement)
2944 bool needs_memory = false;
2946 size_t n_clobbers = 0;
2947 asm_clobber_t *clobber = statement->clobbers;
2948 for( ; clobber != NULL; clobber = clobber->next) {
2949 if(strcmp(clobber->clobber, "memory") == 0) {
2950 needs_memory = true;
2954 ident *id = new_id_from_str(clobber->clobber);
2955 obstack_ptr_grow(&asm_obst, id);
2958 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
2959 ident **clobbers = NULL;
2960 if(n_clobbers > 0) {
2961 clobbers = obstack_finish(&asm_obst);
2964 /* find and count input and output constraints */
2965 asm_constraint_t *constraint = statement->inputs;
2966 for( ; constraint != NULL; constraint = constraint->next) {
2968 bool supports_memop = false;
2969 for(const char *c = constraint->constraints; *c != 0; ++c) {
2970 /* TODO: improve error messages */
2974 panic("multiple alternative assembler constraints not "
2982 supports_memop = true;
2983 obstack_1grow(&asm_obst, *c);
2986 if(modifiers & ASM_MODIFIER_READ_WRITE)
2987 panic("inconsistent register constraints");
2988 modifiers |= ASM_MODIFIER_WRITE_ONLY;
2991 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
2992 panic("inconsistent register constraints");
2993 modifiers |= ASM_MODIFIER_READ_WRITE;
2996 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
2997 panic("early clobber assembler constraint not supported yet");
3000 modifiers |= ASM_MODIFIER_COMMUTATIVE;
3001 panic("commutative assembler constraint not supported yet");
3004 /* skip register preferences stuff... */
3005 while(*c != 0 && *c != ',')
3009 /* skip register preferences stuff... */
3013 obstack_1grow(&asm_obst, *c);
3017 obstack_1grow(&asm_obst, '\0');
3018 const char *constraint_string = obstack_finish(&asm_obst);
3020 needs_memory |= supports_memop;
3021 if(supports_memop) {
3029 static void statement_to_firm(statement_t *statement)
3031 switch(statement->type) {
3032 case STATEMENT_INVALID:
3033 panic("invalid statement found");
3034 case STATEMENT_COMPOUND:
3035 compound_statement_to_firm(&statement->compound);
3037 case STATEMENT_RETURN:
3038 return_statement_to_firm(&statement->returns);
3040 case STATEMENT_EXPRESSION:
3041 expression_statement_to_firm(&statement->expression);
3044 if_statement_to_firm(&statement->ifs);
3046 case STATEMENT_WHILE:
3047 while_statement_to_firm(&statement->whiles);
3049 case STATEMENT_DO_WHILE:
3050 do_while_statement_to_firm(&statement->do_while);
3052 case STATEMENT_DECLARATION:
3053 declaration_statement_to_firm(&statement->declaration);
3055 case STATEMENT_BREAK:
3056 create_jump_statement(statement, break_label);
3058 case STATEMENT_CONTINUE:
3059 create_jump_statement(statement, continue_label);
3061 case STATEMENT_SWITCH:
3062 switch_statement_to_firm(&statement->switchs);
3064 case STATEMENT_CASE_LABEL:
3065 case_label_to_firm(&statement->case_label);
3068 for_statement_to_firm(&statement->fors);
3070 case STATEMENT_LABEL:
3071 label_to_firm(&statement->label);
3073 case STATEMENT_GOTO:
3074 goto_to_firm(&statement->gotos);
3077 //asm_statement_to_firm(&statement->asms);
3081 panic("Statement not implemented\n");
3084 static int count_local_declarations(const declaration_t * decl,
3085 const declaration_t *const end)
3088 for (; decl != end; decl = decl->next) {
3089 const type_t *type = skip_typeref(decl->type);
3090 switch (type->type) {
3094 if (!decl->address_taken)
3104 static int count_decls_in_expr(const expression_t *expr) {
3105 if (expr != NULL && expr->base.type == EXPR_STATEMENT)
3106 return count_decls_in_stmts(expr->statement.statement);
3110 static int count_decls_in_stmts(const statement_t *stmt)
3113 for (; stmt != NULL; stmt = stmt->base.next) {
3114 switch (stmt->type) {
3115 case STATEMENT_DECLARATION: {
3116 const declaration_statement_t *const decl_stmt = &stmt->declaration;
3117 count += count_local_declarations(decl_stmt->declarations_begin,
3118 decl_stmt->declarations_end->next);
3122 case STATEMENT_COMPOUND: {
3123 const compound_statement_t *const comp =
3124 (const compound_statement_t*)stmt;
3125 count += count_decls_in_stmts(comp->statements);
3129 case STATEMENT_IF: {
3130 const if_statement_t *const if_stmt = &stmt->ifs;
3131 count += count_decls_in_expr(if_stmt->condition);
3132 count += count_decls_in_stmts(if_stmt->true_statement);
3133 count += count_decls_in_stmts(if_stmt->false_statement);
3137 case STATEMENT_SWITCH: {
3138 const switch_statement_t *const switch_stmt = &stmt->switchs;
3139 count += count_decls_in_expr(switch_stmt->expression);
3140 count += count_decls_in_stmts(switch_stmt->body);
3144 case STATEMENT_LABEL: {
3145 const label_statement_t *const label_stmt = &stmt->label;
3146 count += count_decls_in_stmts(label_stmt->label_statement);
3150 case STATEMENT_WHILE: {
3151 const while_statement_t *const while_stmt = &stmt->whiles;
3152 count += count_decls_in_expr(while_stmt->condition);
3153 count += count_decls_in_stmts(while_stmt->body);
3157 case STATEMENT_DO_WHILE: {
3158 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
3159 count += count_decls_in_expr(do_while_stmt->condition);
3160 count += count_decls_in_stmts(do_while_stmt->body);
3164 case STATEMENT_FOR: {
3165 const for_statement_t *const for_stmt = &stmt->fors;
3166 count += count_local_declarations(for_stmt->context.declarations, NULL);
3167 count += count_decls_in_expr(for_stmt->initialisation);
3168 count += count_decls_in_expr(for_stmt->condition);
3169 count += count_decls_in_expr(for_stmt->step);
3170 count += count_decls_in_stmts(for_stmt->body);
3175 case STATEMENT_BREAK:
3176 case STATEMENT_CASE_LABEL:
3177 case STATEMENT_CONTINUE:
3180 case STATEMENT_EXPRESSION: {
3181 const expression_statement_t *expr_stmt = &stmt->expression;
3182 count += count_decls_in_expr(expr_stmt->expression);
3186 case STATEMENT_GOTO:
3187 case STATEMENT_INVALID:
3190 case STATEMENT_RETURN: {
3191 const return_statement_t *ret_stmt = &stmt->returns;
3192 count += count_decls_in_expr(ret_stmt->return_value);
3200 static int get_function_n_local_vars(declaration_t *declaration)
3204 /* count parameters */
3205 count += count_local_declarations(declaration->context.declarations, NULL);
3207 /* count local variables declared in body */
3208 count += count_decls_in_stmts(declaration->init.statement);
3213 static void initialize_function_parameters(declaration_t *declaration)
3215 ir_graph *irg = current_ir_graph;
3216 ir_node *args = get_irg_args(irg);
3217 ir_node *start_block = get_irg_start_block(irg);
3218 ir_type *function_irtype = get_ir_type(declaration->type);
3221 declaration_t *parameter = declaration->context.declarations;
3222 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3223 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
3224 type_t *type = skip_typeref(parameter->type);
3226 bool needs_entity = parameter->address_taken;
3227 assert(!is_type_array(type));
3228 if(is_type_compound(type)) {
3229 needs_entity = true;
3233 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3234 ident *id = new_id_from_str(parameter->symbol->string);
3235 set_entity_ident(entity, id);
3237 parameter->declaration_type
3238 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
3239 parameter->v.entity = entity;
3243 ir_mode *mode = get_ir_mode(parameter->type);
3245 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3247 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
3248 parameter->v.value_number = next_value_number_function;
3249 ++next_value_number_function;
3251 set_value(parameter->v.value_number, proj);
3256 * Handle additional decl modifiers for IR-graphs
3258 * @param irg the IR-graph
3259 * @param dec_modifiers additional modifiers
3261 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
3263 if (decl_modifiers & DM_NORETURN) {
3264 /* TRUE if the declaration includes the Microsoft
3265 __declspec(noreturn) specifier. */
3266 set_irg_additional_property(irg, mtp_property_noreturn);
3268 if (decl_modifiers & DM_NOTHROW) {
3269 /* TRUE if the declaration includes the Microsoft
3270 __declspec(nothrow) specifier. */
3271 set_irg_additional_property(irg, mtp_property_nothrow);
3273 if (decl_modifiers & DM_NAKED) {
3274 /* TRUE if the declaration includes the Microsoft
3275 __declspec(naked) specifier. */
3276 set_irg_additional_property(irg, mtp_property_naked);
3278 if (decl_modifiers & DM_FORCEINLINE) {
3279 /* TRUE if the declaration includes the
3280 Microsoft __forceinline specifier. */
3281 set_irg_inline_property(irg, irg_inline_forced);
3283 if (decl_modifiers & DM_NOINLINE) {
3284 /* TRUE if the declaration includes the Microsoft
3285 __declspec(noinline) specifier. */
3286 set_irg_inline_property(irg, irg_inline_forbidden);
3290 static void create_function(declaration_t *declaration)
3292 ir_entity *function_entity = get_function_entity(declaration);
3294 if(declaration->init.statement == NULL)
3297 current_function_decl = declaration;
3298 current_function_name = NULL;
3300 assert(imature_blocks == NULL);
3301 imature_blocks = NEW_ARR_F(ir_node*, 0);
3303 int n_local_vars = get_function_n_local_vars(declaration);
3304 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3305 ir_node *first_block = get_cur_block();
3307 /* set inline flags */
3308 if (declaration->is_inline)
3309 set_irg_inline_property(irg, irg_inline_recomended);
3310 handle_decl_modifier_irg(irg, declaration->decl_modifiers);
3312 next_value_number_function = 0;
3313 initialize_function_parameters(declaration);
3315 statement_to_firm(declaration->init.statement);
3317 ir_node *end_block = get_irg_end_block(irg);
3319 /* do we have a return statement yet? */
3320 if(get_cur_block() != NULL) {
3321 type_t *type = skip_typeref(declaration->type);
3322 assert(is_type_function(type));
3323 const function_type_t *func_type = &type->function;
3324 const type_t *return_type
3325 = skip_typeref(func_type->return_type);
3328 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3329 ret = new_Return(get_store(), 0, NULL);
3332 if(is_type_scalar(return_type)) {
3333 mode = get_ir_mode(func_type->return_type);
3339 /* ยง5.1.2.2.3 main implicitly returns 0 */
3340 if (strcmp(declaration->symbol->string, "main") == 0) {
3341 in[0] = new_Const(mode, get_mode_null(mode));
3343 in[0] = new_Unknown(mode);
3345 ret = new_Return(get_store(), 1, in);
3347 add_immBlock_pred(end_block, ret);
3350 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3351 mature_immBlock(imature_blocks[i]);
3353 DEL_ARR_F(imature_blocks);
3354 imature_blocks = NULL;
3356 mature_immBlock(first_block);
3357 mature_immBlock(end_block);
3359 irg_finalize_cons(irg);
3361 /* finalize the frame type */
3362 ir_type *frame_type = get_irg_frame_type(irg);
3363 int n = get_compound_n_members(frame_type);
3366 for(int i = 0; i < n; ++i) {
3367 ir_entity *entity = get_compound_member(frame_type, i);
3368 ir_type *entity_type = get_entity_type(entity);
3370 int align = get_type_alignment_bytes(entity_type);
3371 if(align > align_all)
3375 misalign = offset % align;
3377 offset += align - misalign;
3381 set_entity_offset(entity, offset);
3382 offset += get_type_size_bytes(entity_type);
3384 set_type_size_bytes(frame_type, offset);
3385 set_type_alignment_bytes(frame_type, align_all);
3386 set_type_state(frame_type, layout_fixed);
3391 static void create_global_variable(declaration_t *declaration)
3395 switch ((storage_class_tag_t)declaration->storage_class) {
3396 case STORAGE_CLASS_STATIC:
3397 vis = visibility_local;
3400 case STORAGE_CLASS_EXTERN:
3401 vis = visibility_external_allocated;
3404 case STORAGE_CLASS_NONE:
3405 vis = visibility_external_visible;
3408 case STORAGE_CLASS_THREAD:
3409 vis = visibility_external_visible;
3412 case STORAGE_CLASS_THREAD_EXTERN:
3413 vis = visibility_external_allocated;
3416 case STORAGE_CLASS_THREAD_STATIC:
3417 vis = visibility_local;
3421 var_type = get_tls_type();
3425 var_type = get_glob_type();
3429 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3431 set_entity_visibility(declaration->v.entity, vis);
3433 current_ir_graph = get_const_code_irg();
3434 create_initializer(declaration);
3437 case STORAGE_CLASS_TYPEDEF:
3438 case STORAGE_CLASS_AUTO:
3439 case STORAGE_CLASS_REGISTER:
3440 case STORAGE_CLASS_ENUM_ENTRY:
3443 panic("Invalid storage class for global variable");
3446 static void context_to_firm(context_t *context)
3448 /* first pass: create declarations */
3449 declaration_t *declaration = context->declarations;
3450 for( ; declaration != NULL; declaration = declaration->next) {
3451 if(declaration->namespc != NAMESPACE_NORMAL)
3453 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3454 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3456 if(declaration->symbol == NULL)
3459 type_t *type = skip_typeref(declaration->type);
3460 if(is_type_function(type)) {
3461 get_function_entity(declaration);
3463 create_global_variable(declaration);
3467 /* second pass: create code */
3468 declaration = context->declarations;
3469 for( ; declaration != NULL; declaration = declaration->next) {
3470 if(declaration->namespc != NAMESPACE_NORMAL)
3472 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3473 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3475 if(declaration->symbol == NULL)
3478 type_t *type = declaration->type;
3479 if(type->type != TYPE_FUNCTION)
3482 create_function(declaration);
3486 void init_ast2firm(void)
3488 obstack_init(&asm_obst);
3489 init_atomic_modes();
3492 void exit_ast2firm(void)
3494 obstack_free(&asm_obst, NULL);
3497 void translation_unit_to_firm(translation_unit_t *unit)
3499 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3500 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3501 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3503 ir_type_int = get_ir_type(type_int);
3504 ir_type_const_char = get_ir_type(type_const_char);
3505 ir_type_wchar_t = get_ir_type(type_wchar_t);
3506 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3509 type_void->base.firm_type = ir_type_void;
3511 /* just to be sure */
3512 continue_label = NULL;
3514 current_switch_cond = NULL;
3516 context_to_firm(& unit->context);