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;
226 _atomic_modes[ATOMIC_TYPE_BOOL] = get_umode(int_size);
228 #ifdef PROVIDE_COMPLEX
229 _atomic_modes[ATOMIC_TYPE_BOOL] = _atomic_modes[ATOMIC_TYPE_INT];
230 _atomic_modes[ATOMIC_TYPE_FLOAT_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_FLOAT];
231 _atomic_modes[ATOMIC_TYPE_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_DOUBLE];
232 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE];
235 /* Hmm, pointers should be machine size */
236 set_modeP_data(get_ptrmode(machine_size >> 3, NULL));
237 set_modeP_code(get_ptrmode(machine_size >> 3, NULL));
239 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
240 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
243 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
246 if ((unsigned)atomic_type->atype < (unsigned)ATOMIC_TYPE_LAST)
247 res = _atomic_modes[(unsigned)atomic_type->atype];
249 panic("Encountered unknown atomic type");
253 static unsigned get_type_size(type_t *type);
255 static unsigned get_atomic_type_size(const atomic_type_t *type)
257 switch(type->atype) {
258 case ATOMIC_TYPE_CHAR:
259 case ATOMIC_TYPE_SCHAR:
260 case ATOMIC_TYPE_UCHAR:
263 case ATOMIC_TYPE_SHORT:
264 case ATOMIC_TYPE_USHORT:
267 case ATOMIC_TYPE_BOOL:
268 case ATOMIC_TYPE_INT:
269 case ATOMIC_TYPE_UINT:
270 return machine_size >> 3;
272 case ATOMIC_TYPE_LONG:
273 case ATOMIC_TYPE_ULONG:
274 return machine_size > 16 ? machine_size >> 3 : 4;
276 case ATOMIC_TYPE_LONGLONG:
277 case ATOMIC_TYPE_ULONGLONG:
278 return machine_size > 16 ? 8 : 4;
280 case ATOMIC_TYPE_FLOAT:
283 case ATOMIC_TYPE_DOUBLE:
286 case ATOMIC_TYPE_LONG_DOUBLE:
289 case ATOMIC_TYPE_VOID:
292 case ATOMIC_TYPE_INVALID:
293 case ATOMIC_TYPE_LAST:
296 panic("Trying to determine size of invalid atomic type");
299 static unsigned get_compound_type_size(compound_type_t *type)
301 ir_type *irtype = get_ir_type((type_t*) type);
302 return get_type_size_bytes(irtype);
305 static unsigned get_array_type_size(array_type_t *type)
307 ir_type *irtype = get_ir_type((type_t*) type);
308 return get_type_size_bytes(irtype);
312 static unsigned get_type_size(type_t *type)
314 type = skip_typeref(type);
318 return get_atomic_type_size(&type->atomic);
320 return get_mode_size_bytes(mode_int);
321 case TYPE_COMPOUND_UNION:
322 case TYPE_COMPOUND_STRUCT:
323 return get_compound_type_size(&type->compound);
325 /* just a pointer to the function */
326 return get_mode_size_bytes(mode_P_code);
328 return get_mode_size_bytes(mode_P_data);
330 return get_array_type_size(&type->array);
332 return get_type_size(type->builtin.real_type);
338 panic("Trying to determine size of invalid type");
341 static unsigned count_parameters(const function_type_t *function_type)
345 function_parameter_t *parameter = function_type->parameters;
346 for ( ; parameter != NULL; parameter = parameter->next) {
356 static long fold_constant(const expression_t *expression);
358 static ir_type *create_atomic_type(const atomic_type_t *type)
360 ir_mode *mode = get_atomic_mode(type);
361 ident *id = get_mode_ident(mode);
362 ir_type *irtype = new_type_primitive(id, mode);
364 if(type->atype == ATOMIC_TYPE_LONG_DOUBLE) {
365 set_type_alignment_bytes(irtype, 4);
371 static ir_type *create_method_type(const function_type_t *function_type)
373 type_t *return_type = function_type->return_type;
375 ident *id = unique_ident("functiontype");
376 int n_parameters = count_parameters(function_type);
377 int n_results = return_type == type_void ? 0 : 1;
378 ir_type *irtype = new_type_method(id, n_parameters, n_results);
380 if(return_type != type_void) {
381 ir_type *restype = get_ir_type(return_type);
382 set_method_res_type(irtype, 0, restype);
385 function_parameter_t *parameter = function_type->parameters;
387 for( ; parameter != NULL; parameter = parameter->next) {
388 ir_type *p_irtype = get_ir_type(parameter->type);
389 set_method_param_type(irtype, n, p_irtype);
393 if(function_type->variadic || function_type->unspecified_parameters) {
394 set_method_variadicity(irtype, variadicity_variadic);
400 static ir_type *create_pointer_type(pointer_type_t *type)
402 type_t *points_to = type->points_to;
403 ir_type *ir_points_to;
404 /* Avoid endless recursion if the points_to type contains this poiner type
405 * again (might be a struct). We therefore first create a void* pointer
406 * and then set the real points_to type
408 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
409 ir_type_void, mode_P_data);
410 type->type.firm_type = ir_type;
412 ir_points_to = get_ir_type(points_to);
413 set_pointer_points_to_type(ir_type, ir_points_to);
418 static ir_type *create_array_type(array_type_t *type)
420 type_t *element_type = type->element_type;
421 ir_type *ir_element_type = get_ir_type(element_type);
423 ident *id = unique_ident("array");
424 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
426 if(type->size != NULL) {
427 int n_elements = fold_constant(type->size);
429 set_array_bounds_int(ir_type, 0, 0, n_elements);
431 size_t elemsize = get_type_size_bytes(ir_element_type);
432 int align = get_type_alignment_bytes(ir_element_type);
433 if(elemsize % align > 0) {
434 elemsize += align - (elemsize % align);
436 set_type_size_bytes(ir_type, n_elements * elemsize);
437 set_type_alignment_bytes(ir_type, align);
438 set_type_state(ir_type, layout_fixed);
440 set_array_lower_bound_int(ir_type, 0, 0);
446 #define INVALID_TYPE ((ir_type_ptr)-1)
448 static ir_type *create_struct_type(compound_type_t *type)
450 symbol_t *symbol = type->declaration->symbol;
453 id = unique_ident(symbol->string);
455 id = unique_ident("__anonymous_struct");
457 ir_type *ir_type = new_type_struct(id);
459 type->type.firm_type = ir_type;
463 declaration_t *entry = type->declaration->context.declarations;
464 for( ; entry != NULL; entry = entry->next) {
465 if(entry->namespc != NAMESPACE_NORMAL)
468 ident *ident = new_id_from_str(entry->symbol->string);
469 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
471 int entry_size = get_type_size_bytes(entry_ir_type);
472 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
473 int misalign = offset % entry_alignment;
475 offset += entry_alignment - misalign;
477 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
478 ir_entity *const entity = new_d_entity(ir_type, ident, entry_ir_type, dbgi);
479 set_entity_offset(entity, offset);
480 add_struct_member(ir_type, entity);
481 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
482 entry->v.entity = entity;
484 offset += entry_size;
485 if(entry_alignment > align_all) {
486 if(entry_alignment % align_all != 0) {
487 panic("Uneven alignments not supported yet");
489 align_all = entry_alignment;
493 int misalign = offset % align_all;
495 set_type_alignment_bytes(ir_type, align_all);
496 set_type_size_bytes(ir_type, offset);
497 set_type_state(ir_type, layout_fixed);
502 static ir_type *create_union_type(compound_type_t *type)
504 declaration_t *declaration = type->declaration;
505 symbol_t *symbol = declaration->symbol;
508 id = unique_ident(symbol->string);
510 id = unique_ident("__anonymous_union");
512 ir_type *ir_type = new_type_union(id);
514 type->type.firm_type = ir_type;
518 declaration_t *entry = declaration->context.declarations;
519 for( ; entry != NULL; entry = entry->next) {
520 if(entry->namespc != NAMESPACE_NORMAL)
523 ident *ident = new_id_from_str(entry->symbol->string);
524 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
526 int entry_size = get_type_size_bytes(entry_ir_type);
527 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
529 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
530 ir_entity *const entity = new_d_entity(ir_type, ident, entry_ir_type, dbgi);
531 add_union_member(ir_type, entity);
532 set_entity_offset(entity, 0);
533 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
534 entry->v.entity = entity;
536 if(entry_size > size) {
539 if(entry_alignment > align_all) {
540 if(entry_alignment % align_all != 0) {
541 panic("Uneven alignments not supported yet");
543 align_all = entry_alignment;
547 set_type_alignment_bytes(ir_type, align_all);
548 set_type_size_bytes(ir_type, size);
549 set_type_state(ir_type, layout_fixed);
554 static ir_node *expression_to_firm(const expression_t *expression);
555 static inline ir_mode *get_ir_mode(type_t *type);
557 static ir_type *create_enum_type(enum_type_t *const type)
559 type->type.firm_type = ir_type_int;
561 ir_mode *const mode = get_ir_mode((type_t*) type);
562 tarval *const one = get_mode_one(mode);
563 tarval * tv_next = get_tarval_null(mode);
565 declaration_t *declaration = type->declaration->next;
566 for (; declaration != NULL; declaration = declaration->next) {
567 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
570 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
572 expression_t *const init = declaration->init.enum_value;
574 ir_node *const cnst = expression_to_firm(init);
575 if (!is_Const(cnst)) {
576 panic("couldn't fold constant");
578 tv_next = get_Const_tarval(cnst);
580 declaration->v.enum_val = tv_next;
581 tv_next = tarval_add(tv_next, one);
587 static ir_type *get_ir_type(type_t *type)
589 assert(type != NULL);
591 type = skip_typeref(type);
593 if(type->base.firm_type != NULL) {
594 assert(type->base.firm_type != INVALID_TYPE);
595 return type->base.firm_type;
598 ir_type *firm_type = NULL;
601 firm_type = create_atomic_type(&type->atomic);
604 firm_type = create_method_type(&type->function);
607 firm_type = create_pointer_type(&type->pointer);
610 firm_type = create_array_type(&type->array);
612 case TYPE_COMPOUND_STRUCT:
613 firm_type = create_struct_type(&type->compound);
615 case TYPE_COMPOUND_UNION:
616 firm_type = create_union_type(&type->compound);
619 firm_type = create_enum_type(&type->enumt);
622 firm_type = get_ir_type(type->builtin.real_type);
629 if(firm_type == NULL)
630 panic("unknown type found");
632 type->base.firm_type = firm_type;
636 static inline ir_mode *get_ir_mode(type_t *type)
638 ir_type *irtype = get_ir_type(type);
640 /* firm doesn't report a mode for arrays somehow... */
641 if(is_Array_type(irtype)) {
645 ir_mode *mode = get_type_mode(irtype);
646 assert(mode != NULL);
650 static ir_entity* get_function_entity(declaration_t *declaration)
652 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
653 return declaration->v.entity;
654 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
656 symbol_t *symbol = declaration->symbol;
657 ident *id = new_id_from_str(symbol->string);
659 ir_type *global_type = get_glob_type();
660 ir_type *ir_type_method = get_ir_type(declaration->type);
661 assert(is_Method_type(ir_type_method));
663 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
664 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
665 set_entity_ld_ident(entity, id);
666 if(declaration->storage_class == STORAGE_CLASS_STATIC
667 || declaration->is_inline) {
668 set_entity_visibility(entity, visibility_local);
669 } else if(declaration->init.statement != NULL) {
670 set_entity_visibility(entity, visibility_external_visible);
672 set_entity_visibility(entity, visibility_external_allocated);
674 set_entity_allocation(entity, allocation_static);
676 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
677 declaration->v.entity = entity;
682 static ir_node *const_to_firm(const const_expression_t *cnst)
684 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
685 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
690 if(mode_is_float(mode)) {
691 tv = new_tarval_from_double(cnst->v.float_value, mode);
693 if(mode_is_signed(mode)) {
694 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
696 len = snprintf(buf, sizeof(buf), "%llu",
697 (unsigned long long) cnst->v.int_value);
699 tv = new_tarval_from_str(buf, len, mode);
702 return new_d_Const(dbgi, mode, tv);
705 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
707 assert(entity != NULL);
708 union symconst_symbol sym;
709 sym.entity_p = entity;
710 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
713 static ir_node *string_to_firm(const source_position_t *const src_pos,
714 const char *const id_prefix,
715 const char *const string)
717 ir_type *const global_type = get_glob_type();
718 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
721 ident *const id = unique_ident(id_prefix);
722 dbg_info *const dbgi = get_dbg_info(src_pos);
723 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
724 set_entity_ld_ident(entity, id);
725 set_entity_variability(entity, variability_constant);
726 set_entity_allocation(entity, allocation_static);
728 ir_type *const elem_type = ir_type_const_char;
729 ir_mode *const mode = get_type_mode(elem_type);
731 const size_t slen = strlen(string) + 1;
733 set_array_lower_bound_int(type, 0, 0);
734 set_array_upper_bound_int(type, 0, slen);
735 set_type_size_bytes(type, slen);
736 set_type_state(type, layout_fixed);
738 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
739 for(size_t i = 0; i < slen; ++i) {
740 tvs[i] = new_tarval_from_long(string[i], mode);
743 set_array_entity_values(entity, tvs, slen);
746 return create_symconst(dbgi, entity);
749 static ir_node *string_literal_to_firm(
750 const string_literal_expression_t* literal)
752 return string_to_firm(&literal->expression.source_position, "Lstr",
756 static ir_node *wide_string_literal_to_firm(
757 const wide_string_literal_expression_t* const literal)
759 ir_type *const global_type = get_glob_type();
760 ir_type *const elem_type = ir_type_wchar_t;
761 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
764 ident *const id = unique_ident("Lstr");
765 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
766 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
767 set_entity_ld_ident(entity, id);
768 set_entity_variability(entity, variability_constant);
769 set_entity_allocation(entity, allocation_static);
771 ir_mode *const mode = get_type_mode(elem_type);
773 const wchar_rep_t *const string = literal->value.begin;
774 const size_t slen = literal->value.size;
776 set_array_lower_bound_int(type, 0, 0);
777 set_array_upper_bound_int(type, 0, slen);
778 set_type_size_bytes(type, slen);
779 set_type_state(type, layout_fixed);
781 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
782 for(size_t i = 0; i < slen; ++i) {
783 tvs[i] = new_tarval_from_long(string[i], mode);
786 set_array_entity_values(entity, tvs, slen);
789 return create_symconst(dbgi, entity);
792 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
793 dbg_info *const dbgi)
795 if(is_compound_type(irtype) || is_Array_type(irtype)) {
799 ir_mode *const mode = get_type_mode(irtype);
800 ir_node *const memory = get_store();
801 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
802 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
803 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
808 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
810 ir_mode *mode = get_irn_mode(node);
812 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
814 if(!mode_is_float(mode))
817 /* check if there is already a Conv */
818 if (get_irn_op(node) == op_Conv) {
819 /* convert it into a strict Conv */
820 set_Conv_strict(node, 1);
824 /* otherwise create a new one */
825 return new_d_strictConv(dbgi, node, mode);
828 static ir_node *get_global_var_address(dbg_info *const dbgi,
829 const declaration_t *const decl)
831 assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
833 ir_entity *const entity = decl->v.entity;
834 switch ((storage_class_tag_t)decl->storage_class) {
835 case STORAGE_CLASS_THREAD:
836 case STORAGE_CLASS_THREAD_EXTERN:
837 case STORAGE_CLASS_THREAD_STATIC: {
838 ir_node *const no_mem = new_NoMem();
839 ir_node *const tls = get_irg_tls(current_ir_graph);
840 return new_d_simpleSel(dbgi, no_mem, tls, entity);
844 return create_symconst(dbgi, entity);
848 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
850 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
851 declaration_t *declaration = ref->declaration;
852 type_t *type = skip_typeref(declaration->type);
854 switch((declaration_type_t) declaration->declaration_type) {
855 case DECLARATION_TYPE_UNKNOWN:
856 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
862 case DECLARATION_TYPE_ENUM_ENTRY: {
863 ir_mode *const mode = get_ir_mode(type);
864 return new_Const(mode, declaration->v.enum_val);
867 case DECLARATION_TYPE_LOCAL_VARIABLE: {
868 ir_mode *mode = get_ir_mode(type);
869 return get_value(declaration->v.value_number, mode);
871 case DECLARATION_TYPE_FUNCTION: {
872 return create_symconst(dbgi, declaration->v.entity);
874 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
875 ir_node *const addr = get_global_var_address(dbgi, declaration);
876 ir_type *const irtype = get_entity_type(declaration->v.entity);
877 return deref_address(irtype, addr, dbgi);
880 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
881 ir_entity *entity = declaration->v.entity;
882 ir_node *frame = get_irg_frame(current_ir_graph);
883 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
884 ir_type *irtype = get_entity_type(entity);
885 return deref_address(irtype, sel, dbgi);
888 case DECLARATION_TYPE_COMPOUND_MEMBER:
889 case DECLARATION_TYPE_LABEL_BLOCK:
890 panic("not implemented reference type");
893 panic("reference to declaration with unknown type found");
896 static ir_node *reference_addr(const reference_expression_t *ref)
898 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
899 declaration_t *declaration = ref->declaration;
901 switch((declaration_type_t) declaration->declaration_type) {
902 case DECLARATION_TYPE_UNKNOWN:
904 case DECLARATION_TYPE_LOCAL_VARIABLE:
905 panic("local variable without entity has no address");
906 case DECLARATION_TYPE_FUNCTION: {
907 return create_symconst(dbgi, declaration->v.entity);
909 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
910 ir_node *const addr = get_global_var_address(dbgi, declaration);
913 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
914 ir_entity *entity = declaration->v.entity;
915 ir_node *frame = get_irg_frame(current_ir_graph);
916 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
921 case DECLARATION_TYPE_ENUM_ENTRY:
922 panic("trying to reference enum entry");
924 case DECLARATION_TYPE_COMPOUND_MEMBER:
925 case DECLARATION_TYPE_LABEL_BLOCK:
926 panic("not implemented reference type");
929 panic("reference to declaration with unknown type found");
932 static ir_node *process_builtin_call(const call_expression_t *call)
934 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
936 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
937 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
939 type_t *type = skip_typeref(builtin->expression.datatype);
940 assert(is_type_pointer(type));
942 type_t *function_type = skip_typeref(type->pointer.points_to);
943 symbol_t *symbol = builtin->symbol;
946 case T___builtin_alloca: {
947 if(call->arguments == NULL || call->arguments->next != NULL) {
948 panic("invalid number of parameters on __builtin_alloca");
950 expression_t *argument = call->arguments->expression;
951 ir_node *size = expression_to_firm(argument);
953 ir_node *store = get_store();
954 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
956 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
958 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
962 case T___builtin_nan:
963 case T___builtin_nanf:
964 case T___builtin_nand: {
965 /* Ignore string for now... */
966 assert(is_type_function(function_type));
967 ir_mode *mode = get_ir_mode(function_type->function.return_type);
968 tarval *tv = get_mode_NAN(mode);
969 ir_node *res = new_d_Const(dbgi, mode, tv);
972 case T___builtin_va_end:
975 panic("Unsupported builtin found\n");
979 static ir_node *call_expression_to_firm(const call_expression_t *call)
981 assert(get_cur_block() != NULL);
983 expression_t *function = call->function;
984 if(function->type == EXPR_BUILTIN_SYMBOL) {
985 return process_builtin_call(call);
987 ir_node *callee = expression_to_firm(function);
989 type_t *type = skip_typeref(function->base.datatype);
990 assert(is_type_pointer(type));
991 pointer_type_t *pointer_type = &type->pointer;
992 type_t *points_to = skip_typeref(pointer_type->points_to);
993 assert(is_type_function(points_to));
994 function_type_t *function_type = &points_to->function;
996 int n_parameters = 0;
997 call_argument_t *argument = call->arguments;
998 for( ; argument != NULL; argument = argument->next) {
1002 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1004 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1005 ir_type *new_method_type = NULL;
1006 if(function_type->variadic || function_type->unspecified_parameters) {
1007 /* we need to construct a new method type matching the call
1009 int n_res = get_method_n_ress(ir_method_type);
1010 new_method_type = new_type_method(unique_ident("calltype"),
1011 n_parameters, n_res);
1012 set_method_calling_convention(new_method_type,
1013 get_method_calling_convention(ir_method_type));
1014 set_method_additional_properties(new_method_type,
1015 get_method_additional_properties(ir_method_type));
1017 for(int i = 0; i < n_res; ++i) {
1018 set_method_res_type(new_method_type, i,
1019 get_method_res_type(ir_method_type, i));
1022 ir_node *in[n_parameters];
1024 argument = call->arguments;
1026 for( ; argument != NULL; argument = argument->next) {
1027 expression_t *expression = argument->expression;
1028 ir_node *arg_node = expression_to_firm(expression);
1030 arg_node = do_strict_conv(dbgi, arg_node);
1033 if(new_method_type != NULL) {
1034 ir_type *irtype = get_ir_type(expression->base.datatype);
1035 set_method_param_type(new_method_type, n, irtype);
1040 assert(n == n_parameters);
1042 if(new_method_type != NULL)
1043 ir_method_type = new_method_type;
1045 ir_node *store = get_store();
1046 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1048 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1051 type_t *return_type = skip_typeref(function_type->return_type);
1052 ir_node *result = NULL;
1054 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1056 if(is_type_scalar(return_type)) {
1057 mode = get_ir_mode(return_type);
1061 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1062 result = new_d_Proj(dbgi, resproj, mode, 0);
1068 static void statement_to_firm(statement_t *statement);
1069 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1071 static ir_node *expression_to_addr(const expression_t *expression);
1072 static void create_condition_evaluation(const expression_t *expression,
1073 ir_node *true_block,
1074 ir_node *false_block);
1076 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1079 value = do_strict_conv(dbgi, value);
1081 ir_node *memory = get_store();
1083 if(is_type_scalar(type)) {
1084 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1085 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1086 set_store(store_mem);
1088 ir_type *irtype = get_ir_type(type);
1089 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1090 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1091 set_store(copyb_mem);
1095 static void set_value_for_expression(const expression_t *expression,
1098 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1099 value = do_strict_conv(dbgi, value);
1101 if(expression->type == EXPR_REFERENCE) {
1102 reference_expression_t *ref = (reference_expression_t*) expression;
1104 declaration_t *declaration = ref->declaration;
1105 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
1106 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1107 set_value(declaration->v.value_number, value);
1112 ir_node *addr = expression_to_addr(expression);
1113 type_t *type = skip_typeref(expression->base.datatype);
1114 assign_value(dbgi, addr, type, value);
1117 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1119 ir_mode *value_mode = get_irn_mode(value);
1121 if (value_mode == dest_mode || is_Bad(value))
1124 if(dest_mode == mode_b) {
1125 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1126 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1127 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1131 return new_d_Conv(dbgi, value, dest_mode);
1134 static ir_node *create_incdec(const unary_expression_t *expression)
1136 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1137 type_t *type = skip_typeref(expression->expression.datatype);
1138 ir_mode *mode = get_ir_mode(type);
1139 expression_t *value = expression->value;
1141 ir_node *value_node = expression_to_firm(value);
1144 if(is_type_pointer(type)) {
1145 pointer_type_t *pointer_type = &type->pointer;
1146 unsigned elem_size = get_type_size(pointer_type->points_to);
1147 offset = new_Const_long(mode_int, elem_size);
1149 assert(is_type_arithmetic(type));
1150 offset = new_Const(mode, get_mode_one(mode));
1153 switch(expression->expression.type) {
1154 case EXPR_UNARY_POSTFIX_INCREMENT: {
1155 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1156 set_value_for_expression(value, new_value);
1159 case EXPR_UNARY_POSTFIX_DECREMENT: {
1160 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1161 set_value_for_expression(value, new_value);
1164 case EXPR_UNARY_PREFIX_INCREMENT: {
1165 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1166 set_value_for_expression(value, new_value);
1169 case EXPR_UNARY_PREFIX_DECREMENT: {
1170 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1171 set_value_for_expression(value, new_value);
1175 panic("no incdec expr in create_incdec");
1180 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1182 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1183 type_t *type = skip_typeref(expression->expression.datatype);
1185 if(expression->expression.type == EXPR_UNARY_TAKE_ADDRESS)
1186 return expression_to_addr(expression->value);
1188 const expression_t *value = expression->value;
1189 ir_node *value_node = expression_to_firm(value);
1191 switch(expression->expression.type) {
1192 case EXPR_UNARY_NEGATE: {
1193 ir_mode *mode = get_ir_mode(type);
1194 return new_d_Minus(dbgi, value_node, mode);
1196 case EXPR_UNARY_PLUS:
1198 case EXPR_UNARY_BITWISE_NEGATE: {
1199 ir_mode *mode = get_ir_mode(type);
1200 return new_d_Not(dbgi, value_node, mode);
1202 case EXPR_UNARY_NOT: {
1203 ir_mode *mode = get_ir_mode(type);
1204 if(get_irn_mode(value_node) != mode_b) {
1205 value_node = create_conv(dbgi, value_node, mode_b);
1207 value_node = new_d_Not(dbgi, value_node, mode_b);
1208 if(mode != mode_b) {
1209 value_node = create_conv(dbgi, value_node, mode);
1213 case EXPR_UNARY_DEREFERENCE: {
1214 type_t *value_type = skip_typeref(value->base.datatype);
1215 ir_type *irtype = get_ir_type(value_type);
1216 assert(is_Pointer_type(irtype));
1217 ir_type *points_to = get_pointer_points_to_type(irtype);
1218 return deref_address(points_to, value_node, dbgi);
1220 case EXPR_UNARY_POSTFIX_INCREMENT:
1221 case EXPR_UNARY_POSTFIX_DECREMENT:
1222 case EXPR_UNARY_PREFIX_INCREMENT:
1223 case EXPR_UNARY_PREFIX_DECREMENT:
1224 return create_incdec(expression);
1225 case EXPR_UNARY_CAST: {
1226 ir_mode *mode = get_ir_mode(type);
1227 ir_node *node = create_conv(dbgi, value_node, mode);
1228 node = do_strict_conv(dbgi, node);
1231 case EXPR_UNARY_CAST_IMPLICIT: {
1232 ir_mode *mode = get_ir_mode(type);
1233 return create_conv(dbgi, value_node, mode);
1239 panic("invalid UNEXPR type found");
1242 static long get_pnc(const expression_type_t type)
1245 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1246 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1247 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Ne;
1248 case EXPR_BINARY_ISLESS:
1249 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1250 case EXPR_BINARY_ISLESSEQUAL:
1251 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1252 case EXPR_BINARY_ISGREATER:
1253 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1254 case EXPR_BINARY_ISGREATEREQUAL:
1255 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1256 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1261 panic("trying to get pn_Cmp from non-comparison binexpr type");
1264 static ir_node *create_lazy_op(const binary_expression_t *expression)
1266 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1267 type_t *type = expression->expression.datatype;
1268 ir_mode *mode = get_ir_mode(type);
1270 ir_node *cur_block = get_cur_block();
1272 ir_node *one_block = new_immBlock();
1273 ir_node *one = new_Const(mode, get_mode_one(mode));
1274 ir_node *jmp_one = new_d_Jmp(dbgi);
1276 ir_node *zero_block = new_immBlock();
1277 ir_node *zero = new_Const(mode, get_mode_null(mode));
1278 ir_node *jmp_zero = new_d_Jmp(dbgi);
1280 set_cur_block(cur_block);
1281 create_condition_evaluation((const expression_t*) expression,
1282 one_block, zero_block);
1283 mature_immBlock(one_block);
1284 mature_immBlock(zero_block);
1286 ir_node *common_block = new_immBlock();
1287 add_immBlock_pred(common_block, jmp_one);
1288 add_immBlock_pred(common_block, jmp_zero);
1289 mature_immBlock(common_block);
1291 ir_node *in[2] = { one, zero };
1292 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1297 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1298 ir_node *right, ir_mode *mode);
1300 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1301 create_arithmetic_func func)
1303 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1304 ir_node *left = expression_to_firm(expression->left);
1305 ir_node *right = expression_to_firm(expression->right);
1306 type_t *type = expression->right->base.datatype;
1307 /* be careful with the modes, because in arithmetic assign nodes only
1308 * the right operand has the mode of the arithmetic already */
1309 ir_mode *mode = get_ir_mode(type);
1310 left = create_conv(dbgi, left, mode);
1311 ir_node *res = func(dbgi, left, right, mode);
1316 static ir_node *pointer_arithmetic(ir_node *const pointer,
1319 dbg_info *const dbgi,
1320 const create_arithmetic_func func)
1322 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1323 type_t *const points_to = pointer_type->points_to;
1324 const unsigned elem_size = get_type_size(points_to);
1326 assert(elem_size >= 1);
1327 if (elem_size > 1) {
1328 integer = create_conv(dbgi, integer, mode_int);
1329 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1330 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1334 ir_mode *const mode = get_ir_mode(type);
1335 return func(dbgi, pointer, integer, mode);
1338 static ir_node *create_arithmetic_assign_binop(
1339 const binary_expression_t *expression, create_arithmetic_func func)
1341 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1342 type_t *const type = skip_typeref(expression->expression.datatype);
1345 if (is_type_pointer(type)) {
1346 ir_node *const pointer = expression_to_firm(expression->left);
1347 ir_node * integer = expression_to_firm(expression->right);
1348 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1350 value = create_arithmetic_binop(expression, func);
1353 ir_mode *const mode = get_ir_mode(type);
1354 value = create_conv(dbgi, value, mode);
1355 set_value_for_expression(expression->left, value);
1360 static ir_node *create_add(const binary_expression_t *expression)
1362 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1363 ir_node *left = expression_to_firm(expression->left);
1364 ir_node *right = expression_to_firm(expression->right);
1365 type_t *type = expression->expression.datatype;
1367 expression_t *expr_left = expression->left;
1368 expression_t *expr_right = expression->right;
1369 type_t *type_left = skip_typeref(expr_left->base.datatype);
1370 type_t *type_right = skip_typeref(expr_right->base.datatype);
1372 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1373 ir_mode *const mode = get_ir_mode(type);
1374 return new_d_Add(dbgi, left, right, mode);
1377 if (is_type_pointer(type_left)) {
1378 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1380 assert(is_type_pointer(type_right));
1381 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1385 static ir_node *create_sub(const binary_expression_t *expression)
1387 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1388 expression_t *const expr_left = expression->left;
1389 expression_t *const expr_right = expression->right;
1390 ir_node *const left = expression_to_firm(expr_left);
1391 ir_node *const right = expression_to_firm(expr_right);
1392 type_t *const type = expression->expression.datatype;
1393 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1394 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1396 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1397 ir_mode *const mode = get_ir_mode(type);
1398 return new_d_Sub(dbgi, left, right, mode);
1399 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1400 const pointer_type_t *const ptr_type = &type_left->pointer;
1401 const unsigned elem_size = get_type_size(ptr_type->points_to);
1402 ir_mode *const mode = get_ir_mode(type);
1403 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1404 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1405 ir_node *const no_mem = new_NoMem();
1406 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1407 op_pin_state_floats);
1408 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1411 assert(is_type_pointer(type_left));
1412 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1415 static ir_node *create_shift(const binary_expression_t *expression)
1417 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1418 ir_node *left = expression_to_firm(expression->left);
1419 ir_node *right = expression_to_firm(expression->right);
1420 type_t *type = expression->expression.datatype;
1421 ir_mode *mode = get_ir_mode(type);
1423 /* firm always wants the shift count to be unsigned */
1424 right = create_conv(dbgi, right, mode_uint);
1428 switch(expression->expression.type) {
1429 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1430 case EXPR_BINARY_SHIFTLEFT:
1431 res = new_d_Shl(dbgi, left, right, mode);
1433 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1434 case EXPR_BINARY_SHIFTRIGHT: {
1435 expression_t *expr_left = expression->left;
1436 type_t *type_left = skip_typeref(expr_left->base.datatype);
1438 if(is_type_signed(type_left)) {
1439 res = new_d_Shrs(dbgi, left, right, mode);
1441 res = new_d_Shr(dbgi, left, right, mode);
1446 panic("create shift op called for non-shift op");
1453 static ir_node *create_divmod(const binary_expression_t *expression)
1455 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1456 ir_node *left = expression_to_firm(expression->left);
1457 ir_node *right = expression_to_firm(expression->right);
1458 ir_node *pin = new_Pin(new_NoMem());
1459 /* be careful with the modes, because in arithmetic assign nodes only
1460 * the right operand has the mode of the arithmetic already */
1461 type_t *type = expression->right->base.datatype;
1462 ir_mode *mode = get_ir_mode(type);
1463 left = create_conv(dbgi, left, mode);
1467 switch (expression->expression.type) {
1468 case EXPR_BINARY_DIV:
1469 case EXPR_BINARY_DIV_ASSIGN:
1470 if(mode_is_float(mode)) {
1471 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1472 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1474 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1475 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1479 case EXPR_BINARY_MOD:
1480 case EXPR_BINARY_MOD_ASSIGN:
1481 assert(!mode_is_float(mode));
1482 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1483 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1486 default: panic("unexpected binary expression type in create_divmod()");
1492 static ir_node *create_arithmetic_assign_divmod(
1493 const binary_expression_t *expression)
1495 ir_node * value = create_divmod(expression);
1496 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1497 type_t *const type = expression->expression.datatype;
1498 ir_mode *const mode = get_ir_mode(type);
1500 assert(type->type != TYPE_POINTER);
1502 value = create_conv(dbgi, value, mode);
1503 set_value_for_expression(expression->left, value);
1508 static ir_node *create_arithmetic_assign_shift(
1509 const binary_expression_t *expression)
1511 ir_node * value = create_shift(expression);
1512 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1513 type_t *const type = expression->expression.datatype;
1514 ir_mode *const mode = get_ir_mode(type);
1516 value = create_conv(dbgi, value, mode);
1517 set_value_for_expression(expression->left, value);
1522 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1524 expression_type_t type = expression->expression.type;
1527 case EXPR_BINARY_EQUAL:
1528 case EXPR_BINARY_NOTEQUAL:
1529 case EXPR_BINARY_LESS:
1530 case EXPR_BINARY_LESSEQUAL:
1531 case EXPR_BINARY_GREATER:
1532 case EXPR_BINARY_GREATEREQUAL:
1533 case EXPR_BINARY_ISGREATER:
1534 case EXPR_BINARY_ISGREATEREQUAL:
1535 case EXPR_BINARY_ISLESS:
1536 case EXPR_BINARY_ISLESSEQUAL:
1537 case EXPR_BINARY_ISLESSGREATER:
1538 case EXPR_BINARY_ISUNORDERED: {
1539 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1540 ir_node *left = expression_to_firm(expression->left);
1541 ir_node *right = expression_to_firm(expression->right);
1542 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1543 long pnc = get_pnc(type);
1544 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1547 case EXPR_BINARY_ASSIGN: {
1548 ir_node *right = expression_to_firm(expression->right);
1549 set_value_for_expression(expression->left, right);
1553 case EXPR_BINARY_ADD:
1554 return create_add(expression);
1555 case EXPR_BINARY_SUB:
1556 return create_sub(expression);
1557 case EXPR_BINARY_MUL:
1558 return create_arithmetic_binop(expression, new_d_Mul);
1559 case EXPR_BINARY_BITWISE_AND:
1560 return create_arithmetic_binop(expression, new_d_And);
1561 case EXPR_BINARY_BITWISE_OR:
1562 return create_arithmetic_binop(expression, new_d_Or);
1563 case EXPR_BINARY_BITWISE_XOR:
1564 return create_arithmetic_binop(expression, new_d_Eor);
1565 case EXPR_BINARY_SHIFTLEFT:
1566 case EXPR_BINARY_SHIFTRIGHT:
1567 return create_shift(expression);
1568 case EXPR_BINARY_DIV:
1569 case EXPR_BINARY_MOD:
1570 return create_divmod(expression);
1571 case EXPR_BINARY_LOGICAL_AND:
1572 case EXPR_BINARY_LOGICAL_OR:
1573 return create_lazy_op(expression);
1574 case EXPR_BINARY_COMMA:
1575 expression_to_firm(expression->left);
1576 return expression_to_firm(expression->right);
1577 case EXPR_BINARY_ADD_ASSIGN:
1578 return create_arithmetic_assign_binop(expression, new_d_Add);
1579 case EXPR_BINARY_SUB_ASSIGN:
1580 return create_arithmetic_assign_binop(expression, new_d_Sub);
1581 case EXPR_BINARY_MUL_ASSIGN:
1582 return create_arithmetic_assign_binop(expression, new_d_Mul);
1583 case EXPR_BINARY_DIV_ASSIGN:
1584 return create_arithmetic_assign_divmod(expression);
1585 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1586 return create_arithmetic_assign_binop(expression, new_d_And);
1587 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1588 return create_arithmetic_assign_binop(expression, new_d_Or);
1589 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1590 return create_arithmetic_assign_binop(expression, new_d_Eor);
1591 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1592 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1593 return create_arithmetic_assign_shift(expression);
1594 case EXPR_BINARY_BUILTIN_EXPECT:
1595 return expression_to_firm(expression->left);
1597 panic("TODO binexpr type");
1601 static ir_node *array_access_addr(const array_access_expression_t *expression)
1603 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1604 ir_node *base_addr = expression_to_firm(expression->array_ref);
1605 ir_node *offset = expression_to_firm(expression->index);
1606 offset = create_conv(dbgi, offset, mode_uint);
1608 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1609 assert(is_type_pointer(ref_type));
1610 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1612 unsigned elem_size = get_type_size(pointer_type->points_to);
1613 ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
1614 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1616 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
1621 static ir_node *array_access_to_firm(
1622 const array_access_expression_t *expression)
1624 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1625 ir_node *addr = array_access_addr(expression);
1626 type_t *type = revert_automatic_type_conversion(
1627 (const expression_t*) expression);
1628 type = skip_typeref(type);
1629 ir_type *irtype = get_ir_type(type);
1631 return deref_address(irtype, addr, dbgi);
1634 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1636 type_t *type = expression->type;
1638 type = expression->size_expression->base.datatype;
1639 assert(type != NULL);
1642 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1643 unsigned size = get_type_size(type);
1644 ir_node *size_node = new_Const_long(mode, size);
1649 static long fold_constant(const expression_t *expression)
1651 assert(is_constant_expression(expression));
1653 ir_graph *old_current_ir_graph = current_ir_graph;
1654 if(current_ir_graph == NULL) {
1655 current_ir_graph = get_const_code_irg();
1658 ir_node *cnst = expression_to_firm(expression);
1659 current_ir_graph = old_current_ir_graph;
1661 if(!is_Const(cnst)) {
1662 panic("couldn't fold constant\n");
1665 tarval *tv = get_Const_tarval(cnst);
1666 if(!tarval_is_long(tv)) {
1667 panic("result of constant folding is not integer\n");
1670 return get_tarval_long(tv);
1673 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1675 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1677 /* first try to fold a constant condition */
1678 if(is_constant_expression(expression->condition)) {
1679 long val = fold_constant(expression->condition);
1681 return expression_to_firm(expression->true_expression);
1683 return expression_to_firm(expression->false_expression);
1687 ir_node *cur_block = get_cur_block();
1689 /* create the true block */
1690 ir_node *true_block = new_immBlock();
1692 ir_node *true_val = expression_to_firm(expression->true_expression);
1693 ir_node *true_jmp = new_Jmp();
1695 /* create the false block */
1696 ir_node *false_block = new_immBlock();
1698 ir_node *false_val = expression_to_firm(expression->false_expression);
1699 ir_node *false_jmp = new_Jmp();
1701 /* create the condition evaluation */
1702 set_cur_block(cur_block);
1703 create_condition_evaluation(expression->condition, true_block, false_block);
1704 mature_immBlock(true_block);
1705 mature_immBlock(false_block);
1707 /* create the common block */
1708 ir_node *common_block = new_immBlock();
1709 add_immBlock_pred(common_block, true_jmp);
1710 add_immBlock_pred(common_block, false_jmp);
1711 mature_immBlock(common_block);
1713 /* TODO improve static semantics, so either both or no values are NULL */
1714 if (true_val == NULL || false_val == NULL)
1717 ir_node *in[2] = { true_val, false_val };
1718 ir_mode *mode = get_irn_mode(true_val);
1719 assert(get_irn_mode(false_val) == mode);
1720 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1725 static ir_node *select_addr(const select_expression_t *expression)
1727 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1729 ir_node *compound_addr = expression_to_firm(expression->compound);
1731 declaration_t *entry = expression->compound_entry;
1732 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1733 ir_entity *entity = entry->v.entity;
1735 assert(entity != NULL);
1737 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1742 static ir_node *select_to_firm(const select_expression_t *expression)
1744 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1745 ir_node *addr = select_addr(expression);
1746 type_t *type = revert_automatic_type_conversion(
1747 (const expression_t*) expression);
1748 type = skip_typeref(type);
1749 ir_type *irtype = get_ir_type(type);
1751 return deref_address(irtype, addr, dbgi);
1754 /* Values returned by __builtin_classify_type. */
1755 typedef enum gcc_type_class
1761 enumeral_type_class,
1764 reference_type_class,
1768 function_type_class,
1779 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1781 const type_t *const type = expr->type_expression->base.datatype;
1787 const atomic_type_t *const atomic_type = &type->atomic;
1788 switch (atomic_type->atype) {
1789 /* should not be reached */
1790 case ATOMIC_TYPE_INVALID:
1794 /* gcc cannot do that */
1795 case ATOMIC_TYPE_VOID:
1796 tc = void_type_class;
1799 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
1800 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
1801 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
1802 case ATOMIC_TYPE_SHORT:
1803 case ATOMIC_TYPE_USHORT:
1804 case ATOMIC_TYPE_INT:
1805 case ATOMIC_TYPE_UINT:
1806 case ATOMIC_TYPE_LONG:
1807 case ATOMIC_TYPE_ULONG:
1808 case ATOMIC_TYPE_LONGLONG:
1809 case ATOMIC_TYPE_ULONGLONG:
1810 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
1811 tc = integer_type_class;
1814 case ATOMIC_TYPE_FLOAT:
1815 case ATOMIC_TYPE_DOUBLE:
1816 case ATOMIC_TYPE_LONG_DOUBLE:
1817 tc = real_type_class;
1820 #ifdef PROVIDE_COMPLEX
1821 case ATOMIC_TYPE_FLOAT_COMPLEX:
1822 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1823 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1824 tc = complex_type_class;
1826 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1827 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1828 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1829 tc = complex_type_class;
1834 panic("Unimplemented case in classify_type_to_firm().");
1839 case TYPE_ARRAY: /* gcc handles this as pointer */
1840 case TYPE_FUNCTION: /* gcc handles this as pointer */
1841 case TYPE_POINTER: tc = pointer_type_class; break;
1842 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1843 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1845 /* gcc handles this as integer */
1846 case TYPE_ENUM: tc = integer_type_class; break;
1849 panic("Unimplemented case in classify_type_to_firm().");
1852 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1853 ir_mode *const mode = mode_int;
1854 tarval *const tv = new_tarval_from_long(tc, mode);
1855 return new_d_Const(dbgi, mode, tv);
1858 static ir_node *function_name_to_firm(
1859 const string_literal_expression_t *const expr)
1861 if (current_function_name == NULL) {
1862 const source_position_t *const src_pos =
1863 &expr->expression.source_position;
1864 const char *const name = current_function_decl->symbol->string;
1865 current_function_name = string_to_firm(src_pos, "__func__", name);
1868 return current_function_name;
1871 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1873 statement_t *statement = expr->statement;
1875 assert(statement->type == STATEMENT_COMPOUND);
1876 return compound_statement_to_firm((compound_statement_t*) statement);
1879 static ir_node *va_start_expression_to_firm(
1880 const va_start_expression_t *const expr)
1882 ir_type *const method_type = get_ir_type(current_function_decl->type);
1883 int const n = get_method_n_params(method_type) - 1;
1884 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
1885 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
1886 dbg_info *const dbgi =
1887 get_dbg_info(&expr->expression.source_position);
1888 ir_node *const no_mem = new_NoMem();
1889 ir_node *const arg_sel =
1890 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
1892 size_t const parm_size = get_type_size(expr->parameter->type);
1893 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
1894 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
1895 set_value_for_expression(expr->ap, add);
1900 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
1902 ir_type *const irtype = get_ir_type(expr->expression.datatype);
1903 ir_node *const ap = expression_to_firm(expr->ap);
1904 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1905 ir_node *const res = deref_address(irtype, ap, dbgi);
1907 size_t const parm_size = get_type_size(expr->expression.datatype);
1908 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
1909 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
1910 set_value_for_expression(expr->ap, add);
1915 static ir_node *dereference_addr(const unary_expression_t *const expression)
1917 assert(expression->expression.type == EXPR_UNARY_DEREFERENCE);
1918 return expression_to_firm(expression->value);
1921 static ir_node *expression_to_addr(const expression_t *expression)
1923 switch(expression->type) {
1924 case EXPR_REFERENCE:
1925 return reference_addr(&expression->reference);
1926 case EXPR_ARRAY_ACCESS:
1927 return array_access_addr(&expression->array_access);
1929 return select_addr(&expression->select);
1931 return call_expression_to_firm(&expression->call);
1932 case EXPR_UNARY_DEREFERENCE: {
1933 return dereference_addr(&expression->unary);
1938 panic("trying to get address of non-lvalue");
1941 static ir_node *_expression_to_firm(const expression_t *expression)
1943 switch(expression->type) {
1945 return const_to_firm(&expression->conste);
1946 case EXPR_STRING_LITERAL:
1947 return string_literal_to_firm(&expression->string);
1948 case EXPR_WIDE_STRING_LITERAL:
1949 return wide_string_literal_to_firm(&expression->wide_string);
1950 case EXPR_REFERENCE:
1951 return reference_expression_to_firm(&expression->reference);
1953 return call_expression_to_firm(&expression->call);
1955 return unary_expression_to_firm(&expression->unary);
1957 return binary_expression_to_firm(&expression->binary);
1958 case EXPR_ARRAY_ACCESS:
1959 return array_access_to_firm(&expression->array_access);
1961 return sizeof_to_firm(&expression->sizeofe);
1962 case EXPR_CONDITIONAL:
1963 return conditional_to_firm(&expression->conditional);
1965 return select_to_firm(&expression->select);
1966 case EXPR_CLASSIFY_TYPE:
1967 return classify_type_to_firm(&expression->classify_type);
1969 case EXPR_PRETTY_FUNCTION:
1970 return function_name_to_firm(&expression->string);
1971 case EXPR_STATEMENT:
1972 return statement_expression_to_firm(&expression->statement);
1974 return va_start_expression_to_firm(&expression->va_starte);
1976 return va_arg_expression_to_firm(&expression->va_arge);
1978 case EXPR_BUILTIN_SYMBOL:
1979 panic("unimplemented expression found");
1985 panic("invalid expression found");
1988 static ir_node *expression_to_firm(const expression_t *expression)
1990 ir_node *res = _expression_to_firm(expression);
1992 if(res != NULL && get_irn_mode(res) == mode_b) {
1993 ir_mode *mode = get_ir_mode(expression->base.datatype);
1994 res = create_conv(NULL, res, mode);
2000 static ir_node *expression_to_modeb(const expression_t *expression)
2002 ir_node *res = _expression_to_firm(expression);
2003 res = create_conv(NULL, res, mode_b);
2009 * create a short-circuit expression evaluation that tries to construct
2010 * efficient control flow structures for &&, || and ! expressions
2012 static void create_condition_evaluation(const expression_t *expression,
2013 ir_node *true_block,
2014 ir_node *false_block)
2016 switch(expression->type) {
2017 case EXPR_UNARY_NOT: {
2018 const unary_expression_t *unary_expression = &expression->unary;
2019 create_condition_evaluation(unary_expression->value, false_block,
2023 case EXPR_BINARY_LOGICAL_AND: {
2024 const binary_expression_t *binary_expression = &expression->binary;
2026 ir_node *cur_block = get_cur_block();
2027 ir_node *extra_block = new_immBlock();
2028 set_cur_block(cur_block);
2029 create_condition_evaluation(binary_expression->left, extra_block,
2031 mature_immBlock(extra_block);
2032 set_cur_block(extra_block);
2033 create_condition_evaluation(binary_expression->right, true_block,
2037 case EXPR_BINARY_LOGICAL_OR: {
2038 const binary_expression_t *binary_expression = &expression->binary;
2040 ir_node *cur_block = get_cur_block();
2041 ir_node *extra_block = new_immBlock();
2042 set_cur_block(cur_block);
2043 create_condition_evaluation(binary_expression->left, true_block,
2045 mature_immBlock(extra_block);
2046 set_cur_block(extra_block);
2047 create_condition_evaluation(binary_expression->right, true_block,
2055 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2056 ir_node *condition = expression_to_modeb(expression);
2057 ir_node *cond = new_d_Cond(dbgi, condition);
2058 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2059 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2061 add_immBlock_pred(true_block, true_proj);
2062 add_immBlock_pred(false_block, false_proj);
2064 set_cur_block(NULL);
2069 static void create_declaration_entity(declaration_t *declaration,
2070 declaration_type_t declaration_type,
2071 ir_type *parent_type)
2073 ident *const id = new_id_from_str(declaration->symbol->string);
2074 ir_type *const irtype = get_ir_type(declaration->type);
2075 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2076 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2077 set_entity_ld_ident(entity, id);
2079 declaration->declaration_type = (unsigned char) declaration_type;
2080 declaration->v.entity = entity;
2081 set_entity_variability(entity, variability_uninitialized);
2082 if(parent_type == get_tls_type())
2083 set_entity_allocation(entity, allocation_automatic);
2084 else if(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE)
2085 set_entity_allocation(entity, allocation_static);
2086 /* TODO: visibility? */
2089 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2091 enum compound_graph_entry_type_t {
2092 COMPOUND_GRAPH_ENTRY_ARRAY,
2093 COMPOUND_GRAPH_ENTRY_COMPOUND
2096 struct compound_graph_path_entry_t {
2102 compound_graph_path_entry_t *prev;
2105 static void create_initializer_object(initializer_t *initializer, type_t *type,
2106 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2108 static compound_graph_path *create_compound_path(ir_type *type,
2109 compound_graph_path_entry_t *entry, int len)
2111 compound_graph_path *path = new_compound_graph_path(type, len);
2114 for( ; entry != NULL; entry = entry->prev, --i) {
2116 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2117 set_compound_graph_path_node(path, i, entry->v.entity);
2119 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2120 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2128 static void create_initializer_value(initializer_value_t *initializer,
2130 compound_graph_path_entry_t *entry,
2133 ir_node *node = expression_to_firm(initializer->value);
2134 ir_type *type = get_entity_type(entity);
2135 compound_graph_path *path = create_compound_path(type, entry, len);
2136 add_compound_ent_value_w_path(entity, node, path);
2139 static void create_initializer_compound(initializer_list_t *initializer,
2140 compound_type_t *type,
2142 compound_graph_path_entry_t *last_entry,
2145 declaration_t *compound_declaration = type->declaration;
2147 declaration_t *compound_entry = compound_declaration->context.declarations;
2149 compound_graph_path_entry_t entry;
2150 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2151 entry.prev = last_entry;
2155 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2156 if(compound_entry->symbol == NULL)
2158 if(compound_entry->namespc != NAMESPACE_NORMAL)
2161 if(i >= initializer->len)
2164 entry.v.entity = compound_entry->v.entity;
2166 initializer_t *sub_initializer = initializer->initializers[i];
2168 assert(compound_entry != NULL);
2169 assert(compound_entry->declaration_type
2170 == DECLARATION_TYPE_COMPOUND_MEMBER);
2172 if(sub_initializer->type == INITIALIZER_VALUE) {
2173 create_initializer_value(&sub_initializer->value,
2174 entity, &entry, len);
2176 type_t *entry_type = skip_typeref(compound_entry->type);
2177 create_initializer_object(sub_initializer, entry_type, entity,
2185 static void create_initializer_array(initializer_list_t *initializer,
2186 array_type_t *type, ir_entity *entity,
2187 compound_graph_path_entry_t *last_entry,
2190 type_t *element_type = type->element_type;
2191 element_type = skip_typeref(element_type);
2193 compound_graph_path_entry_t entry;
2194 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2195 entry.prev = last_entry;
2199 for(i = 0; i < initializer->len; ++i) {
2200 entry.v.array_index = i;
2202 initializer_t *sub_initializer = initializer->initializers[i];
2204 if(sub_initializer->type == INITIALIZER_VALUE) {
2205 create_initializer_value(&sub_initializer->value,
2206 entity, &entry, len);
2208 create_initializer_object(sub_initializer, element_type, entity,
2214 /* TODO: initialize rest... */
2215 if(type->size_expression != NULL) {
2216 size_t array_len = fold_constant(type->size_expression);
2217 for( ; i < array_len; ++i) {
2224 static void create_initializer_string(initializer_string_t *initializer,
2225 array_type_t *type, ir_entity *entity,
2226 compound_graph_path_entry_t *last_entry,
2229 type_t *element_type = type->element_type;
2230 element_type = skip_typeref(element_type);
2232 compound_graph_path_entry_t entry;
2233 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2234 entry.prev = last_entry;
2237 ir_type *irtype = get_entity_type(entity);
2238 size_t arr_len = get_array_type_size(type);
2239 const char *p = initializer->string;
2241 for(i = 0; i < arr_len; ++i, ++p) {
2242 entry.v.array_index = i;
2244 ir_node *node = new_Const_long(mode_Bs, *p);
2245 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2246 add_compound_ent_value_w_path(entity, node, path);
2253 static void create_initializer_wide_string(
2254 const initializer_wide_string_t *const initializer, array_type_t *const type,
2255 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2258 type_t *element_type = type->element_type;
2259 element_type = skip_typeref(element_type);
2261 compound_graph_path_entry_t entry;
2262 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2263 entry.prev = last_entry;
2266 ir_type *const irtype = get_entity_type(entity);
2267 const size_t arr_len = get_array_type_size(type);
2268 const wchar_rep_t * p = initializer->string.begin;
2269 const wchar_rep_t *const end = p + initializer->string.size;
2270 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2271 entry.v.array_index = i;
2273 ir_node *node = new_Const_long(mode_int, *p);
2274 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2275 add_compound_ent_value_w_path(entity, node, path);
2279 static void create_initializer_object(initializer_t *initializer, type_t *type,
2280 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2282 if(is_type_array(type)) {
2283 array_type_t *array_type = &type->array;
2285 switch (initializer->type) {
2286 case INITIALIZER_STRING: {
2287 initializer_string_t *const string = &initializer->string;
2288 create_initializer_string(string, array_type, entity, entry, len);
2292 case INITIALIZER_WIDE_STRING: {
2293 initializer_wide_string_t *const string = &initializer->wide_string;
2294 create_initializer_wide_string(string, array_type, entity, entry, len);
2298 case INITIALIZER_LIST: {
2299 initializer_list_t *const list = &initializer->list;
2300 create_initializer_array(list, array_type, entity, entry, len);
2304 case INITIALIZER_VALUE:
2307 panic("Unhandled initializer");
2309 assert(initializer->type == INITIALIZER_LIST);
2310 initializer_list_t *list = &initializer->list;
2312 assert(is_type_compound(type));
2313 compound_type_t *compound_type = &type->compound;
2314 create_initializer_compound(list, compound_type, entity, entry, len);
2318 static void create_initializer_local_variable_entity(declaration_t *declaration)
2320 initializer_t *initializer = declaration->init.initializer;
2321 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2322 ir_entity *entity = declaration->v.entity;
2323 ir_node *memory = get_store();
2324 ir_node *nomem = new_NoMem();
2325 ir_node *frame = get_irg_frame(current_ir_graph);
2326 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2328 if(initializer->type == INITIALIZER_VALUE) {
2329 initializer_value_t *initializer_value = &initializer->value;
2331 ir_node *value = expression_to_firm(initializer_value->value);
2332 type_t *type = skip_typeref(declaration->type);
2333 assign_value(dbgi, addr, type, value);
2337 /* create a "template" entity which is copied to the entity on the stack */
2338 ident *const id = unique_ident("initializer");
2339 ir_type *const irtype = get_ir_type(declaration->type);
2340 ir_type *const global_type = get_glob_type();
2341 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
2342 set_entity_ld_ident(init_entity, id);
2344 set_entity_variability(init_entity, variability_initialized);
2345 set_entity_visibility(init_entity, visibility_local);
2346 set_entity_allocation(init_entity, allocation_static);
2348 ir_graph *const old_current_ir_graph = current_ir_graph;
2349 current_ir_graph = get_const_code_irg();
2351 type_t *const type = skip_typeref(declaration->type);
2352 create_initializer_object(initializer, type, init_entity, NULL, 0);
2354 assert(current_ir_graph == get_const_code_irg());
2355 current_ir_graph = old_current_ir_graph;
2357 ir_node *const src_addr = create_symconst(dbgi, init_entity);
2358 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2360 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2361 set_store(copyb_mem);
2364 static void create_initializer(declaration_t *declaration)
2366 initializer_t *initializer = declaration->init.initializer;
2367 if(initializer == NULL)
2370 declaration_type_t declaration_type
2371 = (declaration_type_t) declaration->declaration_type;
2372 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2373 create_initializer_local_variable_entity(declaration);
2377 if(initializer->type == INITIALIZER_VALUE) {
2378 initializer_value_t *initializer_value = &initializer->value;
2380 ir_node *value = expression_to_firm(initializer_value->value);
2382 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2383 set_value(declaration->v.value_number, value);
2385 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2387 ir_entity *entity = declaration->v.entity;
2389 set_entity_variability(entity, variability_initialized);
2390 set_atomic_ent_value(entity, value);
2393 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2394 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2396 ir_entity *entity = declaration->v.entity;
2397 set_entity_variability(entity, variability_initialized);
2399 type_t *type = skip_typeref(declaration->type);
2400 create_initializer_object(initializer, type, entity, NULL, 0);
2404 static void create_local_variable(declaration_t *declaration)
2406 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2408 bool needs_entity = declaration->address_taken;
2409 type_t *type = skip_typeref(declaration->type);
2411 if(is_type_array(type) || is_type_compound(type)) {
2412 needs_entity = true;
2416 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2417 create_declaration_entity(declaration,
2418 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2421 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2422 declaration->v.value_number = next_value_number_function;
2423 ++next_value_number_function;
2426 create_initializer(declaration);
2429 static void create_local_static_variable(declaration_t *declaration)
2431 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2433 type_t *const type = skip_typeref(declaration->type);
2434 ir_type *const global_type = get_glob_type();
2435 ident *const id = unique_ident(declaration->symbol->string);
2436 ir_type *const irtype = get_ir_type(type);
2437 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2438 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
2439 set_entity_ld_ident(entity, id);
2441 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2442 declaration->v.entity = entity;
2443 set_entity_variability(entity, variability_uninitialized);
2444 set_entity_visibility(entity, visibility_local);
2445 set_entity_allocation(entity, allocation_static);
2447 ir_graph *const old_current_ir_graph = current_ir_graph;
2448 current_ir_graph = get_const_code_irg();
2450 create_initializer(declaration);
2452 assert(current_ir_graph == get_const_code_irg());
2453 current_ir_graph = old_current_ir_graph;
2458 static void return_statement_to_firm(return_statement_t *statement)
2460 if(get_cur_block() == NULL)
2463 ir_type *func_irtype = get_ir_type(current_function_decl->type);
2465 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2469 if(get_method_n_ress(func_irtype) > 0) {
2470 ir_type *res_type = get_method_res_type(func_irtype, 0);
2472 if(statement->return_value != NULL) {
2473 ir_node *node = expression_to_firm(statement->return_value);
2474 node = do_strict_conv(dbgi, node);
2478 if(is_compound_type(res_type)) {
2481 mode = get_type_mode(res_type);
2483 in[0] = new_Unknown(mode);
2487 /* build return_value for its side effects */
2488 if(statement->return_value != NULL) {
2489 expression_to_firm(statement->return_value);
2494 ir_node *store = get_store();
2495 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
2497 ir_node *end_block = get_irg_end_block(current_ir_graph);
2498 add_immBlock_pred(end_block, ret);
2500 set_cur_block(NULL);
2503 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
2505 if(get_cur_block() == NULL)
2508 return expression_to_firm(statement->expression);
2511 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
2513 ir_node *result = NULL;
2514 statement_t *statement = compound->statements;
2515 for( ; statement != NULL; statement = statement->base.next) {
2516 //context2firm(&statement->context);
2518 if(statement->base.next == NULL
2519 && statement->type == STATEMENT_EXPRESSION) {
2520 result = expression_statement_to_firm(
2521 (expression_statement_t*) statement);
2524 statement_to_firm(statement);
2530 static void create_local_declaration(declaration_t *declaration)
2532 type_t *type = skip_typeref(declaration->type);
2534 switch ((storage_class_tag_t) declaration->storage_class) {
2535 case STORAGE_CLASS_STATIC:
2536 create_local_static_variable(declaration);
2538 case STORAGE_CLASS_ENUM_ENTRY:
2539 panic("enum entry declaration in local block found");
2540 case STORAGE_CLASS_EXTERN:
2541 panic("extern declaration in local block found");
2542 case STORAGE_CLASS_NONE:
2543 case STORAGE_CLASS_AUTO:
2544 case STORAGE_CLASS_REGISTER:
2545 if(is_type_function(type)) {
2546 panic("nested functions not supported yet");
2548 create_local_variable(declaration);
2551 case STORAGE_CLASS_TYPEDEF:
2552 case STORAGE_CLASS_THREAD:
2553 case STORAGE_CLASS_THREAD_EXTERN:
2554 case STORAGE_CLASS_THREAD_STATIC:
2557 panic("invalid storage class found");
2560 static void declaration_statement_to_firm(declaration_statement_t *statement)
2562 declaration_t *declaration = statement->declarations_begin;
2563 declaration_t *end = statement->declarations_end->next;
2564 for( ; declaration != end; declaration = declaration->next) {
2565 create_local_variable(declaration);
2569 static void if_statement_to_firm(if_statement_t *statement)
2571 ir_node *cur_block = get_cur_block();
2573 ir_node *fallthrough_block = new_immBlock();
2575 /* the true (blocks) */
2576 ir_node *true_block;
2577 if (statement->true_statement != NULL) {
2578 true_block = new_immBlock();
2579 statement_to_firm(statement->true_statement);
2580 if(get_cur_block() != NULL) {
2581 ir_node *jmp = new_Jmp();
2582 add_immBlock_pred(fallthrough_block, jmp);
2585 true_block = fallthrough_block;
2588 /* the false (blocks) */
2589 ir_node *false_block;
2590 if(statement->false_statement != NULL) {
2591 false_block = new_immBlock();
2593 statement_to_firm(statement->false_statement);
2594 if(get_cur_block() != NULL) {
2595 ir_node *jmp = new_Jmp();
2596 add_immBlock_pred(fallthrough_block, jmp);
2599 false_block = fallthrough_block;
2602 /* create the condition */
2603 if(cur_block != NULL) {
2604 set_cur_block(cur_block);
2605 create_condition_evaluation(statement->condition, true_block,
2609 mature_immBlock(true_block);
2610 if(false_block != fallthrough_block) {
2611 mature_immBlock(false_block);
2613 mature_immBlock(fallthrough_block);
2615 set_cur_block(fallthrough_block);
2618 static void while_statement_to_firm(while_statement_t *statement)
2620 ir_node *jmp = NULL;
2621 if(get_cur_block() != NULL) {
2625 /* create the header block */
2626 ir_node *header_block = new_immBlock();
2628 add_immBlock_pred(header_block, jmp);
2631 /* the false block */
2632 ir_node *false_block = new_immBlock();
2635 ir_node *body_block;
2636 if (statement->body != NULL) {
2637 ir_node *old_continue_label = continue_label;
2638 ir_node *old_break_label = break_label;
2639 continue_label = header_block;
2640 break_label = false_block;
2642 body_block = new_immBlock();
2643 statement_to_firm(statement->body);
2645 assert(continue_label == header_block);
2646 assert(break_label == false_block);
2647 continue_label = old_continue_label;
2648 break_label = old_break_label;
2650 if(get_cur_block() != NULL) {
2652 add_immBlock_pred(header_block, jmp);
2655 body_block = header_block;
2658 /* create the condition */
2659 set_cur_block(header_block);
2661 create_condition_evaluation(statement->condition, body_block, false_block);
2662 mature_immBlock(body_block);
2663 mature_immBlock(false_block);
2664 mature_immBlock(header_block);
2666 set_cur_block(false_block);
2669 static void do_while_statement_to_firm(do_while_statement_t *statement)
2671 ir_node *jmp = NULL;
2672 if(get_cur_block() != NULL) {
2676 /* create the header block */
2677 ir_node *header_block = new_immBlock();
2679 /* the false block */
2680 ir_node *false_block = new_immBlock();
2683 ir_node *body_block = new_immBlock();
2685 add_immBlock_pred(body_block, jmp);
2688 if (statement->body != NULL) {
2689 ir_node *old_continue_label = continue_label;
2690 ir_node *old_break_label = break_label;
2691 continue_label = header_block;
2692 break_label = false_block;
2694 statement_to_firm(statement->body);
2696 assert(continue_label == header_block);
2697 assert(break_label == false_block);
2698 continue_label = old_continue_label;
2699 break_label = old_break_label;
2701 if (get_cur_block() == NULL) {
2702 mature_immBlock(header_block);
2703 mature_immBlock(body_block);
2704 mature_immBlock(false_block);
2709 ir_node *body_jmp = new_Jmp();
2710 add_immBlock_pred(header_block, body_jmp);
2711 mature_immBlock(header_block);
2713 /* create the condition */
2714 set_cur_block(header_block);
2716 create_condition_evaluation(statement->condition, body_block, false_block);
2717 mature_immBlock(body_block);
2718 mature_immBlock(false_block);
2719 mature_immBlock(header_block);
2721 set_cur_block(false_block);
2724 static void for_statement_to_firm(for_statement_t *statement)
2726 ir_node *jmp = NULL;
2727 if (get_cur_block() != NULL) {
2728 if(statement->initialisation != NULL) {
2729 expression_to_firm(statement->initialisation);
2732 /* create declarations */
2733 declaration_t *declaration = statement->context.declarations;
2734 for( ; declaration != NULL; declaration = declaration->next) {
2735 create_local_declaration(declaration);
2742 /* create the step block */
2743 ir_node *const step_block = new_immBlock();
2744 if (statement->step != NULL) {
2745 expression_to_firm(statement->step);
2747 ir_node *const step_jmp = new_Jmp();
2749 /* create the header block */
2750 ir_node *const header_block = new_immBlock();
2752 add_immBlock_pred(header_block, jmp);
2754 add_immBlock_pred(header_block, step_jmp);
2756 /* the false block */
2757 ir_node *const false_block = new_immBlock();
2760 ir_node * body_block;
2761 if (statement->body != NULL) {
2762 ir_node *const old_continue_label = continue_label;
2763 ir_node *const old_break_label = break_label;
2764 continue_label = step_block;
2765 break_label = false_block;
2767 body_block = new_immBlock();
2768 statement_to_firm(statement->body);
2770 assert(continue_label == step_block);
2771 assert(break_label == false_block);
2772 continue_label = old_continue_label;
2773 break_label = old_break_label;
2775 if (get_cur_block() != NULL) {
2777 add_immBlock_pred(step_block, jmp);
2780 body_block = step_block;
2783 /* create the condition */
2784 set_cur_block(header_block);
2785 if (statement->condition != NULL) {
2786 create_condition_evaluation(statement->condition, body_block,
2789 keep_alive(header_block);
2791 add_immBlock_pred(body_block, jmp);
2794 mature_immBlock(body_block);
2795 mature_immBlock(false_block);
2796 mature_immBlock(step_block);
2797 mature_immBlock(header_block);
2798 mature_immBlock(false_block);
2800 set_cur_block(false_block);
2803 static void create_jump_statement(const statement_t *statement,
2804 ir_node *target_block)
2806 if(get_cur_block() == NULL)
2809 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2810 ir_node *jump = new_d_Jmp(dbgi);
2811 add_immBlock_pred(target_block, jump);
2813 set_cur_block(NULL);
2816 static void switch_statement_to_firm(const switch_statement_t *statement)
2818 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2820 ir_node *expression = expression_to_firm(statement->expression);
2821 ir_node *cond = new_d_Cond(dbgi, expression);
2822 ir_node *break_block = new_immBlock();
2824 set_cur_block(NULL);
2826 ir_node *const old_switch_cond = current_switch_cond;
2827 ir_node *const old_break_label = break_label;
2828 const bool old_saw_default_label = saw_default_label;
2829 current_switch_cond = cond;
2830 break_label = break_block;
2832 statement_to_firm(statement->body);
2834 if(get_cur_block() != NULL) {
2835 ir_node *jmp = new_Jmp();
2836 add_immBlock_pred(break_block, jmp);
2839 if (!saw_default_label) {
2840 set_cur_block(get_nodes_block(cond));
2841 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2842 MAGIC_DEFAULT_PN_NUMBER);
2843 add_immBlock_pred(break_block, proj);
2846 assert(current_switch_cond == cond);
2847 assert(break_label == break_block);
2848 current_switch_cond = old_switch_cond;
2849 break_label = old_break_label;
2850 saw_default_label = old_saw_default_label;
2852 mature_immBlock(break_block);
2853 set_cur_block(break_block);
2856 static void case_label_to_firm(const case_label_statement_t *statement)
2858 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2860 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2862 /* let's create a node and hope firm constant folding creates a Const
2865 set_cur_block(get_nodes_block(current_switch_cond));
2866 if(statement->expression) {
2867 long pn = fold_constant(statement->expression);
2868 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2869 /* oops someone detected our cheating... */
2870 panic("magic default pn used");
2872 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2874 saw_default_label = true;
2875 proj = new_d_defaultProj(dbgi, current_switch_cond,
2876 MAGIC_DEFAULT_PN_NUMBER);
2879 ir_node *block = new_immBlock();
2880 if (fallthrough != NULL) {
2881 add_immBlock_pred(block, fallthrough);
2883 add_immBlock_pred(block, proj);
2884 mature_immBlock(block);
2886 if(statement->label_statement != NULL) {
2887 statement_to_firm(statement->label_statement);
2891 static ir_node *get_label_block(declaration_t *label)
2893 assert(label->namespc == NAMESPACE_LABEL);
2895 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2896 return label->v.block;
2898 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2900 ir_node *old_cur_block = get_cur_block();
2901 ir_node *block = new_immBlock();
2902 set_cur_block(old_cur_block);
2904 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2905 label->v.block = block;
2907 ARR_APP1(ir_node *, imature_blocks, block);
2912 static void label_to_firm(const label_statement_t *statement)
2914 ir_node *block = get_label_block(statement->label);
2916 if(get_cur_block() != NULL) {
2917 ir_node *jmp = new_Jmp();
2918 add_immBlock_pred(block, jmp);
2921 set_cur_block(block);
2924 if(statement->label_statement != NULL) {
2925 statement_to_firm(statement->label_statement);
2929 static void goto_to_firm(const goto_statement_t *statement)
2931 if(get_cur_block() == NULL)
2934 ir_node *block = get_label_block(statement->label);
2935 ir_node *jmp = new_Jmp();
2936 add_immBlock_pred(block, jmp);
2938 set_cur_block(NULL);
2941 typedef enum modifier_t {
2942 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
2943 ASM_MODIFIER_READ_WRITE = 1 << 1,
2944 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
2945 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
2949 static void asm_statement_to_firm(const asm_statement_t *statement)
2951 bool needs_memory = false;
2953 size_t n_clobbers = 0;
2954 asm_clobber_t *clobber = statement->clobbers;
2955 for( ; clobber != NULL; clobber = clobber->next) {
2956 if(strcmp(clobber->clobber, "memory") == 0) {
2957 needs_memory = true;
2961 ident *id = new_id_from_str(clobber->clobber);
2962 obstack_ptr_grow(&asm_obst, id);
2965 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
2966 ident **clobbers = NULL;
2967 if(n_clobbers > 0) {
2968 clobbers = obstack_finish(&asm_obst);
2971 /* find and count input and output constraints */
2972 asm_constraint_t *constraint = statement->inputs;
2973 for( ; constraint != NULL; constraint = constraint->next) {
2975 bool supports_memop = false;
2976 for(const char *c = constraint->constraints; *c != 0; ++c) {
2977 /* TODO: improve error messages */
2981 panic("multiple alternative assembler constraints not "
2989 supports_memop = true;
2990 obstack_1grow(&asm_obst, *c);
2993 if(modifiers & ASM_MODIFIER_READ_WRITE)
2994 panic("inconsistent register constraints");
2995 modifiers |= ASM_MODIFIER_WRITE_ONLY;
2998 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
2999 panic("inconsistent register constraints");
3000 modifiers |= ASM_MODIFIER_READ_WRITE;
3003 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
3004 panic("early clobber assembler constraint not supported yet");
3007 modifiers |= ASM_MODIFIER_COMMUTATIVE;
3008 panic("commutative assembler constraint not supported yet");
3011 /* skip register preferences stuff... */
3012 while(*c != 0 && *c != ',')
3016 /* skip register preferences stuff... */
3020 obstack_1grow(&asm_obst, *c);
3024 obstack_1grow(&asm_obst, '\0');
3025 const char *constraint_string = obstack_finish(&asm_obst);
3027 needs_memory |= supports_memop;
3028 if(supports_memop) {
3036 static void statement_to_firm(statement_t *statement)
3038 switch(statement->type) {
3039 case STATEMENT_INVALID:
3040 panic("invalid statement found");
3041 case STATEMENT_COMPOUND:
3042 compound_statement_to_firm(&statement->compound);
3044 case STATEMENT_RETURN:
3045 return_statement_to_firm(&statement->returns);
3047 case STATEMENT_EXPRESSION:
3048 expression_statement_to_firm(&statement->expression);
3051 if_statement_to_firm(&statement->ifs);
3053 case STATEMENT_WHILE:
3054 while_statement_to_firm(&statement->whiles);
3056 case STATEMENT_DO_WHILE:
3057 do_while_statement_to_firm(&statement->do_while);
3059 case STATEMENT_DECLARATION:
3060 declaration_statement_to_firm(&statement->declaration);
3062 case STATEMENT_BREAK:
3063 create_jump_statement(statement, break_label);
3065 case STATEMENT_CONTINUE:
3066 create_jump_statement(statement, continue_label);
3068 case STATEMENT_SWITCH:
3069 switch_statement_to_firm(&statement->switchs);
3071 case STATEMENT_CASE_LABEL:
3072 case_label_to_firm(&statement->case_label);
3075 for_statement_to_firm(&statement->fors);
3077 case STATEMENT_LABEL:
3078 label_to_firm(&statement->label);
3080 case STATEMENT_GOTO:
3081 goto_to_firm(&statement->gotos);
3084 //asm_statement_to_firm(&statement->asms);
3088 panic("Statement not implemented\n");
3091 static int count_decls_in_expression(const expression_t *expression);
3093 static int count_local_declarations(const declaration_t * decl,
3094 const declaration_t *const end)
3097 for (; decl != end; decl = decl->next) {
3098 const type_t *type = skip_typeref(decl->type);
3099 switch (type->type) {
3103 if (!decl->address_taken)
3109 const initializer_t *initializer = decl->init.initializer;
3110 /* FIXME: should walk initializer hierarchies... */
3111 if(initializer != NULL && initializer->type == INITIALIZER_VALUE) {
3112 count += count_decls_in_expression(initializer->value.value);
3118 static int count_decls_in_expression(const expression_t *expression) {
3119 if(expression == NULL)
3122 switch(expression->base.type) {
3123 case EXPR_STATEMENT:
3124 return count_decls_in_stmts(expression->statement.statement);
3126 return count_decls_in_expression(expression->binary.left)
3127 + count_decls_in_expression(expression->binary.right);
3129 return count_decls_in_expression(expression->unary.value);
3135 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
3136 * (or implement all the missing expressions here/implement a walker)
3142 static int count_decls_in_stmts(const statement_t *stmt)
3145 for (; stmt != NULL; stmt = stmt->base.next) {
3146 switch (stmt->type) {
3147 case STATEMENT_DECLARATION: {
3148 const declaration_statement_t *const decl_stmt = &stmt->declaration;
3149 count += count_local_declarations(decl_stmt->declarations_begin,
3150 decl_stmt->declarations_end->next);
3154 case STATEMENT_COMPOUND: {
3155 const compound_statement_t *const comp =
3156 (const compound_statement_t*)stmt;
3157 count += count_decls_in_stmts(comp->statements);
3161 case STATEMENT_IF: {
3162 const if_statement_t *const if_stmt = &stmt->ifs;
3163 count += count_decls_in_expression(if_stmt->condition);
3164 count += count_decls_in_stmts(if_stmt->true_statement);
3165 count += count_decls_in_stmts(if_stmt->false_statement);
3169 case STATEMENT_SWITCH: {
3170 const switch_statement_t *const switch_stmt = &stmt->switchs;
3171 count += count_decls_in_expression(switch_stmt->expression);
3172 count += count_decls_in_stmts(switch_stmt->body);
3176 case STATEMENT_LABEL: {
3177 const label_statement_t *const label_stmt = &stmt->label;
3178 count += count_decls_in_stmts(label_stmt->label_statement);
3182 case STATEMENT_WHILE: {
3183 const while_statement_t *const while_stmt = &stmt->whiles;
3184 count += count_decls_in_expression(while_stmt->condition);
3185 count += count_decls_in_stmts(while_stmt->body);
3189 case STATEMENT_DO_WHILE: {
3190 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
3191 count += count_decls_in_expression(do_while_stmt->condition);
3192 count += count_decls_in_stmts(do_while_stmt->body);
3196 case STATEMENT_FOR: {
3197 const for_statement_t *const for_stmt = &stmt->fors;
3198 count += count_local_declarations(for_stmt->context.declarations, NULL);
3199 count += count_decls_in_expression(for_stmt->initialisation);
3200 count += count_decls_in_expression(for_stmt->condition);
3201 count += count_decls_in_expression(for_stmt->step);
3202 count += count_decls_in_stmts(for_stmt->body);
3206 case STATEMENT_CASE_LABEL: {
3207 const case_label_statement_t *label = &stmt->case_label;
3208 count += count_decls_in_expression(label->expression);
3209 count += count_decls_in_stmts(label->label_statement);
3214 case STATEMENT_BREAK:
3215 case STATEMENT_CONTINUE:
3218 case STATEMENT_EXPRESSION: {
3219 const expression_statement_t *expr_stmt = &stmt->expression;
3220 count += count_decls_in_expression(expr_stmt->expression);
3224 case STATEMENT_GOTO:
3225 case STATEMENT_INVALID:
3228 case STATEMENT_RETURN: {
3229 const return_statement_t *ret_stmt = &stmt->returns;
3230 count += count_decls_in_expression(ret_stmt->return_value);
3238 static int get_function_n_local_vars(declaration_t *declaration)
3242 /* count parameters */
3243 count += count_local_declarations(declaration->context.declarations, NULL);
3245 /* count local variables declared in body */
3246 count += count_decls_in_stmts(declaration->init.statement);
3251 static void initialize_function_parameters(declaration_t *declaration)
3253 ir_graph *irg = current_ir_graph;
3254 ir_node *args = get_irg_args(irg);
3255 ir_node *start_block = get_irg_start_block(irg);
3256 ir_type *function_irtype = get_ir_type(declaration->type);
3259 declaration_t *parameter = declaration->context.declarations;
3260 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3261 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
3262 type_t *type = skip_typeref(parameter->type);
3264 bool needs_entity = parameter->address_taken;
3265 assert(!is_type_array(type));
3266 if(is_type_compound(type)) {
3267 needs_entity = true;
3271 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3272 ident *id = new_id_from_str(parameter->symbol->string);
3273 set_entity_ident(entity, id);
3275 parameter->declaration_type
3276 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
3277 parameter->v.entity = entity;
3281 ir_mode *mode = get_ir_mode(parameter->type);
3283 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3285 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
3286 parameter->v.value_number = next_value_number_function;
3287 ++next_value_number_function;
3289 set_value(parameter->v.value_number, proj);
3294 * Handle additional decl modifiers for IR-graphs
3296 * @param irg the IR-graph
3297 * @param dec_modifiers additional modifiers
3299 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
3301 if (decl_modifiers & DM_NORETURN) {
3302 /* TRUE if the declaration includes the Microsoft
3303 __declspec(noreturn) specifier. */
3304 set_irg_additional_property(irg, mtp_property_noreturn);
3306 if (decl_modifiers & DM_NOTHROW) {
3307 /* TRUE if the declaration includes the Microsoft
3308 __declspec(nothrow) specifier. */
3309 set_irg_additional_property(irg, mtp_property_nothrow);
3311 if (decl_modifiers & DM_NAKED) {
3312 /* TRUE if the declaration includes the Microsoft
3313 __declspec(naked) specifier. */
3314 set_irg_additional_property(irg, mtp_property_naked);
3316 if (decl_modifiers & DM_FORCEINLINE) {
3317 /* TRUE if the declaration includes the
3318 Microsoft __forceinline specifier. */
3319 set_irg_inline_property(irg, irg_inline_forced);
3321 if (decl_modifiers & DM_NOINLINE) {
3322 /* TRUE if the declaration includes the Microsoft
3323 __declspec(noinline) specifier. */
3324 set_irg_inline_property(irg, irg_inline_forbidden);
3328 static void create_function(declaration_t *declaration)
3330 ir_entity *function_entity = get_function_entity(declaration);
3332 if(declaration->init.statement == NULL)
3335 current_function_decl = declaration;
3336 current_function_name = NULL;
3338 assert(imature_blocks == NULL);
3339 imature_blocks = NEW_ARR_F(ir_node*, 0);
3341 int n_local_vars = get_function_n_local_vars(declaration);
3342 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3343 ir_node *first_block = get_cur_block();
3345 /* set inline flags */
3346 if (declaration->is_inline)
3347 set_irg_inline_property(irg, irg_inline_recomended);
3348 handle_decl_modifier_irg(irg, declaration->decl_modifiers);
3350 next_value_number_function = 0;
3351 initialize_function_parameters(declaration);
3353 statement_to_firm(declaration->init.statement);
3355 ir_node *end_block = get_irg_end_block(irg);
3357 /* do we have a return statement yet? */
3358 if(get_cur_block() != NULL) {
3359 type_t *type = skip_typeref(declaration->type);
3360 assert(is_type_function(type));
3361 const function_type_t *func_type = &type->function;
3362 const type_t *return_type
3363 = skip_typeref(func_type->return_type);
3366 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3367 ret = new_Return(get_store(), 0, NULL);
3370 if(is_type_scalar(return_type)) {
3371 mode = get_ir_mode(func_type->return_type);
3377 /* ยง5.1.2.2.3 main implicitly returns 0 */
3378 if (strcmp(declaration->symbol->string, "main") == 0) {
3379 in[0] = new_Const(mode, get_mode_null(mode));
3381 in[0] = new_Unknown(mode);
3383 ret = new_Return(get_store(), 1, in);
3385 add_immBlock_pred(end_block, ret);
3388 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3389 mature_immBlock(imature_blocks[i]);
3391 DEL_ARR_F(imature_blocks);
3392 imature_blocks = NULL;
3394 mature_immBlock(first_block);
3395 mature_immBlock(end_block);
3397 irg_finalize_cons(irg);
3399 /* finalize the frame type */
3400 ir_type *frame_type = get_irg_frame_type(irg);
3401 int n = get_compound_n_members(frame_type);
3404 for(int i = 0; i < n; ++i) {
3405 ir_entity *entity = get_compound_member(frame_type, i);
3406 ir_type *entity_type = get_entity_type(entity);
3408 int align = get_type_alignment_bytes(entity_type);
3409 if(align > align_all)
3413 misalign = offset % align;
3415 offset += align - misalign;
3419 set_entity_offset(entity, offset);
3420 offset += get_type_size_bytes(entity_type);
3422 set_type_size_bytes(frame_type, offset);
3423 set_type_alignment_bytes(frame_type, align_all);
3424 set_type_state(frame_type, layout_fixed);
3429 static void create_global_variable(declaration_t *declaration)
3433 switch ((storage_class_tag_t)declaration->storage_class) {
3434 case STORAGE_CLASS_STATIC:
3435 vis = visibility_local;
3438 case STORAGE_CLASS_EXTERN:
3439 vis = visibility_external_allocated;
3442 case STORAGE_CLASS_NONE:
3443 vis = visibility_external_visible;
3446 case STORAGE_CLASS_THREAD:
3447 vis = visibility_external_visible;
3450 case STORAGE_CLASS_THREAD_EXTERN:
3451 vis = visibility_external_allocated;
3454 case STORAGE_CLASS_THREAD_STATIC:
3455 vis = visibility_local;
3459 var_type = get_tls_type();
3463 var_type = get_glob_type();
3467 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3469 set_entity_visibility(declaration->v.entity, vis);
3471 current_ir_graph = get_const_code_irg();
3472 create_initializer(declaration);
3475 case STORAGE_CLASS_TYPEDEF:
3476 case STORAGE_CLASS_AUTO:
3477 case STORAGE_CLASS_REGISTER:
3478 case STORAGE_CLASS_ENUM_ENTRY:
3481 panic("Invalid storage class for global variable");
3484 static void context_to_firm(context_t *context)
3486 /* first pass: create declarations */
3487 declaration_t *declaration = context->declarations;
3488 for( ; declaration != NULL; declaration = declaration->next) {
3489 if(declaration->namespc != NAMESPACE_NORMAL)
3491 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3492 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3494 if(declaration->symbol == NULL)
3497 type_t *type = skip_typeref(declaration->type);
3498 if(is_type_function(type)) {
3499 get_function_entity(declaration);
3501 create_global_variable(declaration);
3505 /* second pass: create code */
3506 declaration = context->declarations;
3507 for( ; declaration != NULL; declaration = declaration->next) {
3508 if(declaration->namespc != NAMESPACE_NORMAL)
3510 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3511 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3513 if(declaration->symbol == NULL)
3516 type_t *type = declaration->type;
3517 if(type->type != TYPE_FUNCTION)
3520 create_function(declaration);
3524 void init_ast2firm(void)
3526 obstack_init(&asm_obst);
3527 init_atomic_modes();
3530 void exit_ast2firm(void)
3532 obstack_free(&asm_obst, NULL);
3535 void translation_unit_to_firm(translation_unit_t *unit)
3537 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3538 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3539 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3541 ir_type_int = get_ir_type(type_int);
3542 ir_type_const_char = get_ir_type(type_const_char);
3543 ir_type_wchar_t = get_ir_type(type_wchar_t);
3544 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3547 type_void->base.firm_type = ir_type_void;
3549 /* just to be sure */
3550 continue_label = NULL;
3552 current_switch_cond = NULL;
3554 context_to_firm(& unit->context);