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 if(is_constant_expression(expression->left)) {
1271 long val = fold_constant(expression->left);
1272 expression_type_t etype = expression->expression.type;
1273 if((etype == EXPR_BINARY_LOGICAL_AND && val != 0)
1274 || (etype == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1275 return expression_to_firm(expression->right);
1277 assert((etype == EXPR_BINARY_LOGICAL_AND && val == 0)
1278 || (etype == EXPR_BINARY_LOGICAL_OR && val != 0));
1279 return new_Const(mode, get_mode_one(mode));
1283 ir_node *cur_block = get_cur_block();
1285 ir_node *one_block = new_immBlock();
1286 ir_node *one = new_Const(mode, get_mode_one(mode));
1287 ir_node *jmp_one = new_d_Jmp(dbgi);
1289 ir_node *zero_block = new_immBlock();
1290 ir_node *zero = new_Const(mode, get_mode_null(mode));
1291 ir_node *jmp_zero = new_d_Jmp(dbgi);
1293 set_cur_block(cur_block);
1294 create_condition_evaluation((const expression_t*) expression,
1295 one_block, zero_block);
1296 mature_immBlock(one_block);
1297 mature_immBlock(zero_block);
1299 ir_node *common_block = new_immBlock();
1300 add_immBlock_pred(common_block, jmp_one);
1301 add_immBlock_pred(common_block, jmp_zero);
1302 mature_immBlock(common_block);
1304 ir_node *in[2] = { one, zero };
1305 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1310 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1311 ir_node *right, ir_mode *mode);
1313 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1314 create_arithmetic_func func)
1316 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1317 ir_node *left = expression_to_firm(expression->left);
1318 ir_node *right = expression_to_firm(expression->right);
1319 type_t *type = expression->right->base.datatype;
1320 /* be careful with the modes, because in arithmetic assign nodes only
1321 * the right operand has the mode of the arithmetic already */
1322 ir_mode *mode = get_ir_mode(type);
1323 left = create_conv(dbgi, left, mode);
1324 ir_node *res = func(dbgi, left, right, mode);
1329 static ir_node *pointer_arithmetic(ir_node *const pointer,
1332 dbg_info *const dbgi,
1333 const create_arithmetic_func func)
1335 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1336 type_t *const points_to = pointer_type->points_to;
1337 const unsigned elem_size = get_type_size(points_to);
1339 assert(elem_size >= 1);
1340 if (elem_size > 1) {
1341 integer = create_conv(dbgi, integer, mode_int);
1342 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1343 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1347 ir_mode *const mode = get_ir_mode(type);
1348 return func(dbgi, pointer, integer, mode);
1351 static ir_node *create_arithmetic_assign_binop(
1352 const binary_expression_t *expression, create_arithmetic_func func)
1354 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1355 type_t *const type = skip_typeref(expression->expression.datatype);
1358 if (is_type_pointer(type)) {
1359 ir_node *const pointer = expression_to_firm(expression->left);
1360 ir_node * integer = expression_to_firm(expression->right);
1361 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1363 value = create_arithmetic_binop(expression, func);
1366 ir_mode *const mode = get_ir_mode(type);
1367 value = create_conv(dbgi, value, mode);
1368 set_value_for_expression(expression->left, value);
1373 static ir_node *create_add(const binary_expression_t *expression)
1375 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1376 ir_node *left = expression_to_firm(expression->left);
1377 ir_node *right = expression_to_firm(expression->right);
1378 type_t *type = expression->expression.datatype;
1380 expression_t *expr_left = expression->left;
1381 expression_t *expr_right = expression->right;
1382 type_t *type_left = skip_typeref(expr_left->base.datatype);
1383 type_t *type_right = skip_typeref(expr_right->base.datatype);
1385 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1386 ir_mode *const mode = get_ir_mode(type);
1387 return new_d_Add(dbgi, left, right, mode);
1390 if (is_type_pointer(type_left)) {
1391 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1393 assert(is_type_pointer(type_right));
1394 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1398 static ir_node *create_sub(const binary_expression_t *expression)
1400 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1401 expression_t *const expr_left = expression->left;
1402 expression_t *const expr_right = expression->right;
1403 ir_node *const left = expression_to_firm(expr_left);
1404 ir_node *const right = expression_to_firm(expr_right);
1405 type_t *const type = expression->expression.datatype;
1406 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1407 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1409 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1410 ir_mode *const mode = get_ir_mode(type);
1411 return new_d_Sub(dbgi, left, right, mode);
1412 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1413 const pointer_type_t *const ptr_type = &type_left->pointer;
1414 const unsigned elem_size = get_type_size(ptr_type->points_to);
1415 ir_mode *const mode = get_ir_mode(type);
1416 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1417 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1418 ir_node *const no_mem = new_NoMem();
1419 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1420 op_pin_state_floats);
1421 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1424 assert(is_type_pointer(type_left));
1425 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1428 static ir_node *create_shift(const binary_expression_t *expression)
1430 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1431 ir_node *left = expression_to_firm(expression->left);
1432 ir_node *right = expression_to_firm(expression->right);
1433 type_t *type = expression->expression.datatype;
1434 ir_mode *mode = get_ir_mode(type);
1436 /* firm always wants the shift count to be unsigned */
1437 right = create_conv(dbgi, right, mode_uint);
1441 switch(expression->expression.type) {
1442 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1443 case EXPR_BINARY_SHIFTLEFT:
1444 res = new_d_Shl(dbgi, left, right, mode);
1446 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1447 case EXPR_BINARY_SHIFTRIGHT: {
1448 expression_t *expr_left = expression->left;
1449 type_t *type_left = skip_typeref(expr_left->base.datatype);
1451 if(is_type_signed(type_left)) {
1452 res = new_d_Shrs(dbgi, left, right, mode);
1454 res = new_d_Shr(dbgi, left, right, mode);
1459 panic("create shift op called for non-shift op");
1466 static ir_node *create_divmod(const binary_expression_t *expression)
1468 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1469 ir_node *left = expression_to_firm(expression->left);
1470 ir_node *right = expression_to_firm(expression->right);
1471 ir_node *pin = new_Pin(new_NoMem());
1472 /* be careful with the modes, because in arithmetic assign nodes only
1473 * the right operand has the mode of the arithmetic already */
1474 type_t *type = expression->right->base.datatype;
1475 ir_mode *mode = get_ir_mode(type);
1476 left = create_conv(dbgi, left, mode);
1480 switch (expression->expression.type) {
1481 case EXPR_BINARY_DIV:
1482 case EXPR_BINARY_DIV_ASSIGN:
1483 if(mode_is_float(mode)) {
1484 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1485 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1487 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1488 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1492 case EXPR_BINARY_MOD:
1493 case EXPR_BINARY_MOD_ASSIGN:
1494 assert(!mode_is_float(mode));
1495 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1496 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1499 default: panic("unexpected binary expression type in create_divmod()");
1505 static ir_node *create_arithmetic_assign_divmod(
1506 const binary_expression_t *expression)
1508 ir_node * value = create_divmod(expression);
1509 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1510 type_t *const type = expression->expression.datatype;
1511 ir_mode *const mode = get_ir_mode(type);
1513 assert(type->type != TYPE_POINTER);
1515 value = create_conv(dbgi, value, mode);
1516 set_value_for_expression(expression->left, value);
1521 static ir_node *create_arithmetic_assign_shift(
1522 const binary_expression_t *expression)
1524 ir_node * value = create_shift(expression);
1525 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1526 type_t *const type = expression->expression.datatype;
1527 ir_mode *const mode = get_ir_mode(type);
1529 value = create_conv(dbgi, value, mode);
1530 set_value_for_expression(expression->left, value);
1535 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1537 expression_type_t type = expression->expression.type;
1540 case EXPR_BINARY_EQUAL:
1541 case EXPR_BINARY_NOTEQUAL:
1542 case EXPR_BINARY_LESS:
1543 case EXPR_BINARY_LESSEQUAL:
1544 case EXPR_BINARY_GREATER:
1545 case EXPR_BINARY_GREATEREQUAL:
1546 case EXPR_BINARY_ISGREATER:
1547 case EXPR_BINARY_ISGREATEREQUAL:
1548 case EXPR_BINARY_ISLESS:
1549 case EXPR_BINARY_ISLESSEQUAL:
1550 case EXPR_BINARY_ISLESSGREATER:
1551 case EXPR_BINARY_ISUNORDERED: {
1552 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1553 ir_node *left = expression_to_firm(expression->left);
1554 ir_node *right = expression_to_firm(expression->right);
1555 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1556 long pnc = get_pnc(type);
1557 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1560 case EXPR_BINARY_ASSIGN: {
1561 ir_node *right = expression_to_firm(expression->right);
1562 set_value_for_expression(expression->left, right);
1566 case EXPR_BINARY_ADD:
1567 return create_add(expression);
1568 case EXPR_BINARY_SUB:
1569 return create_sub(expression);
1570 case EXPR_BINARY_MUL:
1571 return create_arithmetic_binop(expression, new_d_Mul);
1572 case EXPR_BINARY_BITWISE_AND:
1573 return create_arithmetic_binop(expression, new_d_And);
1574 case EXPR_BINARY_BITWISE_OR:
1575 return create_arithmetic_binop(expression, new_d_Or);
1576 case EXPR_BINARY_BITWISE_XOR:
1577 return create_arithmetic_binop(expression, new_d_Eor);
1578 case EXPR_BINARY_SHIFTLEFT:
1579 case EXPR_BINARY_SHIFTRIGHT:
1580 return create_shift(expression);
1581 case EXPR_BINARY_DIV:
1582 case EXPR_BINARY_MOD:
1583 return create_divmod(expression);
1584 case EXPR_BINARY_LOGICAL_AND:
1585 case EXPR_BINARY_LOGICAL_OR:
1586 return create_lazy_op(expression);
1587 case EXPR_BINARY_COMMA:
1588 expression_to_firm(expression->left);
1589 return expression_to_firm(expression->right);
1590 case EXPR_BINARY_ADD_ASSIGN:
1591 return create_arithmetic_assign_binop(expression, new_d_Add);
1592 case EXPR_BINARY_SUB_ASSIGN:
1593 return create_arithmetic_assign_binop(expression, new_d_Sub);
1594 case EXPR_BINARY_MUL_ASSIGN:
1595 return create_arithmetic_assign_binop(expression, new_d_Mul);
1596 case EXPR_BINARY_DIV_ASSIGN:
1597 return create_arithmetic_assign_divmod(expression);
1598 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1599 return create_arithmetic_assign_binop(expression, new_d_And);
1600 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1601 return create_arithmetic_assign_binop(expression, new_d_Or);
1602 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1603 return create_arithmetic_assign_binop(expression, new_d_Eor);
1604 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1605 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1606 return create_arithmetic_assign_shift(expression);
1607 case EXPR_BINARY_BUILTIN_EXPECT:
1608 return expression_to_firm(expression->left);
1610 panic("TODO binexpr type");
1614 static ir_node *array_access_addr(const array_access_expression_t *expression)
1616 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1617 ir_node *base_addr = expression_to_firm(expression->array_ref);
1618 ir_node *offset = expression_to_firm(expression->index);
1619 offset = create_conv(dbgi, offset, mode_uint);
1621 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1622 assert(is_type_pointer(ref_type));
1623 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1625 unsigned elem_size = get_type_size(pointer_type->points_to);
1626 ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
1627 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1629 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
1634 static ir_node *array_access_to_firm(
1635 const array_access_expression_t *expression)
1637 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1638 ir_node *addr = array_access_addr(expression);
1639 type_t *type = revert_automatic_type_conversion(
1640 (const expression_t*) expression);
1641 type = skip_typeref(type);
1642 ir_type *irtype = get_ir_type(type);
1644 return deref_address(irtype, addr, dbgi);
1647 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1649 type_t *type = expression->type;
1651 type = expression->size_expression->base.datatype;
1652 assert(type != NULL);
1655 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1656 unsigned size = get_type_size(type);
1657 ir_node *size_node = new_Const_long(mode, size);
1662 static long fold_constant(const expression_t *expression)
1664 assert(is_constant_expression(expression));
1666 ir_graph *old_current_ir_graph = current_ir_graph;
1667 if(current_ir_graph == NULL) {
1668 current_ir_graph = get_const_code_irg();
1671 ir_node *cnst = expression_to_firm(expression);
1672 current_ir_graph = old_current_ir_graph;
1674 if(!is_Const(cnst)) {
1675 panic("couldn't fold constant\n");
1678 tarval *tv = get_Const_tarval(cnst);
1679 if(!tarval_is_long(tv)) {
1680 panic("result of constant folding is not integer\n");
1683 return get_tarval_long(tv);
1686 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1688 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1690 /* first try to fold a constant condition */
1691 if(is_constant_expression(expression->condition)) {
1692 long val = fold_constant(expression->condition);
1694 return expression_to_firm(expression->true_expression);
1696 return expression_to_firm(expression->false_expression);
1700 ir_node *cur_block = get_cur_block();
1702 /* create the true block */
1703 ir_node *true_block = new_immBlock();
1705 ir_node *true_val = expression_to_firm(expression->true_expression);
1706 ir_node *true_jmp = new_Jmp();
1708 /* create the false block */
1709 ir_node *false_block = new_immBlock();
1711 ir_node *false_val = expression_to_firm(expression->false_expression);
1712 ir_node *false_jmp = new_Jmp();
1714 /* create the condition evaluation */
1715 set_cur_block(cur_block);
1716 create_condition_evaluation(expression->condition, true_block, false_block);
1717 mature_immBlock(true_block);
1718 mature_immBlock(false_block);
1720 /* create the common block */
1721 ir_node *common_block = new_immBlock();
1722 add_immBlock_pred(common_block, true_jmp);
1723 add_immBlock_pred(common_block, false_jmp);
1724 mature_immBlock(common_block);
1726 /* TODO improve static semantics, so either both or no values are NULL */
1727 if (true_val == NULL || false_val == NULL)
1730 ir_node *in[2] = { true_val, false_val };
1731 ir_mode *mode = get_irn_mode(true_val);
1732 assert(get_irn_mode(false_val) == mode);
1733 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1738 static ir_node *select_addr(const select_expression_t *expression)
1740 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1742 ir_node *compound_addr = expression_to_firm(expression->compound);
1744 declaration_t *entry = expression->compound_entry;
1745 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1746 ir_entity *entity = entry->v.entity;
1748 assert(entity != NULL);
1750 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1755 static ir_node *select_to_firm(const select_expression_t *expression)
1757 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1758 ir_node *addr = select_addr(expression);
1759 type_t *type = revert_automatic_type_conversion(
1760 (const expression_t*) expression);
1761 type = skip_typeref(type);
1762 ir_type *irtype = get_ir_type(type);
1764 return deref_address(irtype, addr, dbgi);
1767 /* Values returned by __builtin_classify_type. */
1768 typedef enum gcc_type_class
1774 enumeral_type_class,
1777 reference_type_class,
1781 function_type_class,
1792 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1794 const type_t *const type = expr->type_expression->base.datatype;
1800 const atomic_type_t *const atomic_type = &type->atomic;
1801 switch (atomic_type->atype) {
1802 /* should not be reached */
1803 case ATOMIC_TYPE_INVALID:
1807 /* gcc cannot do that */
1808 case ATOMIC_TYPE_VOID:
1809 tc = void_type_class;
1812 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
1813 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
1814 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
1815 case ATOMIC_TYPE_SHORT:
1816 case ATOMIC_TYPE_USHORT:
1817 case ATOMIC_TYPE_INT:
1818 case ATOMIC_TYPE_UINT:
1819 case ATOMIC_TYPE_LONG:
1820 case ATOMIC_TYPE_ULONG:
1821 case ATOMIC_TYPE_LONGLONG:
1822 case ATOMIC_TYPE_ULONGLONG:
1823 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
1824 tc = integer_type_class;
1827 case ATOMIC_TYPE_FLOAT:
1828 case ATOMIC_TYPE_DOUBLE:
1829 case ATOMIC_TYPE_LONG_DOUBLE:
1830 tc = real_type_class;
1833 #ifdef PROVIDE_COMPLEX
1834 case ATOMIC_TYPE_FLOAT_COMPLEX:
1835 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1836 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1837 tc = complex_type_class;
1839 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1840 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1841 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1842 tc = complex_type_class;
1847 panic("Unimplemented case in classify_type_to_firm().");
1852 case TYPE_ARRAY: /* gcc handles this as pointer */
1853 case TYPE_FUNCTION: /* gcc handles this as pointer */
1854 case TYPE_POINTER: tc = pointer_type_class; break;
1855 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1856 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1858 /* gcc handles this as integer */
1859 case TYPE_ENUM: tc = integer_type_class; break;
1862 panic("Unimplemented case in classify_type_to_firm().");
1865 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1866 ir_mode *const mode = mode_int;
1867 tarval *const tv = new_tarval_from_long(tc, mode);
1868 return new_d_Const(dbgi, mode, tv);
1871 static ir_node *function_name_to_firm(
1872 const string_literal_expression_t *const expr)
1874 if (current_function_name == NULL) {
1875 const source_position_t *const src_pos =
1876 &expr->expression.source_position;
1877 const char *const name = current_function_decl->symbol->string;
1878 current_function_name = string_to_firm(src_pos, "__func__", name);
1881 return current_function_name;
1884 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1886 statement_t *statement = expr->statement;
1888 assert(statement->type == STATEMENT_COMPOUND);
1889 return compound_statement_to_firm((compound_statement_t*) statement);
1892 static ir_node *va_start_expression_to_firm(
1893 const va_start_expression_t *const expr)
1895 ir_type *const method_type = get_ir_type(current_function_decl->type);
1896 int const n = get_method_n_params(method_type) - 1;
1897 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
1898 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
1899 dbg_info *const dbgi =
1900 get_dbg_info(&expr->expression.source_position);
1901 ir_node *const no_mem = new_NoMem();
1902 ir_node *const arg_sel =
1903 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
1905 size_t const parm_size = get_type_size(expr->parameter->type);
1906 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
1907 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
1908 set_value_for_expression(expr->ap, add);
1913 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
1915 ir_type *const irtype = get_ir_type(expr->expression.datatype);
1916 ir_node *const ap = expression_to_firm(expr->ap);
1917 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1918 ir_node *const res = deref_address(irtype, ap, dbgi);
1920 size_t const parm_size = get_type_size(expr->expression.datatype);
1921 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
1922 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
1923 set_value_for_expression(expr->ap, add);
1928 static ir_node *dereference_addr(const unary_expression_t *const expression)
1930 assert(expression->expression.type == EXPR_UNARY_DEREFERENCE);
1931 return expression_to_firm(expression->value);
1934 static ir_node *expression_to_addr(const expression_t *expression)
1936 switch(expression->type) {
1937 case EXPR_REFERENCE:
1938 return reference_addr(&expression->reference);
1939 case EXPR_ARRAY_ACCESS:
1940 return array_access_addr(&expression->array_access);
1942 return select_addr(&expression->select);
1944 return call_expression_to_firm(&expression->call);
1945 case EXPR_UNARY_DEREFERENCE: {
1946 return dereference_addr(&expression->unary);
1951 panic("trying to get address of non-lvalue");
1954 static ir_node *_expression_to_firm(const expression_t *expression)
1956 switch(expression->type) {
1958 return const_to_firm(&expression->conste);
1959 case EXPR_STRING_LITERAL:
1960 return string_literal_to_firm(&expression->string);
1961 case EXPR_WIDE_STRING_LITERAL:
1962 return wide_string_literal_to_firm(&expression->wide_string);
1963 case EXPR_REFERENCE:
1964 return reference_expression_to_firm(&expression->reference);
1966 return call_expression_to_firm(&expression->call);
1968 return unary_expression_to_firm(&expression->unary);
1970 return binary_expression_to_firm(&expression->binary);
1971 case EXPR_ARRAY_ACCESS:
1972 return array_access_to_firm(&expression->array_access);
1974 return sizeof_to_firm(&expression->sizeofe);
1975 case EXPR_CONDITIONAL:
1976 return conditional_to_firm(&expression->conditional);
1978 return select_to_firm(&expression->select);
1979 case EXPR_CLASSIFY_TYPE:
1980 return classify_type_to_firm(&expression->classify_type);
1982 case EXPR_PRETTY_FUNCTION:
1983 return function_name_to_firm(&expression->string);
1984 case EXPR_STATEMENT:
1985 return statement_expression_to_firm(&expression->statement);
1987 return va_start_expression_to_firm(&expression->va_starte);
1989 return va_arg_expression_to_firm(&expression->va_arge);
1991 case EXPR_BUILTIN_SYMBOL:
1992 panic("unimplemented expression found");
1998 panic("invalid expression found");
2001 static ir_node *expression_to_firm(const expression_t *expression)
2003 ir_node *res = _expression_to_firm(expression);
2005 if(res != NULL && get_irn_mode(res) == mode_b) {
2006 ir_mode *mode = get_ir_mode(expression->base.datatype);
2007 res = create_conv(NULL, res, mode);
2013 static ir_node *expression_to_modeb(const expression_t *expression)
2015 ir_node *res = _expression_to_firm(expression);
2016 res = create_conv(NULL, res, mode_b);
2022 * create a short-circuit expression evaluation that tries to construct
2023 * efficient control flow structures for &&, || and ! expressions
2025 static void create_condition_evaluation(const expression_t *expression,
2026 ir_node *true_block,
2027 ir_node *false_block)
2029 switch(expression->type) {
2030 case EXPR_UNARY_NOT: {
2031 const unary_expression_t *unary_expression = &expression->unary;
2032 create_condition_evaluation(unary_expression->value, false_block,
2036 case EXPR_BINARY_LOGICAL_AND: {
2037 const binary_expression_t *binary_expression = &expression->binary;
2039 ir_node *cur_block = get_cur_block();
2040 ir_node *extra_block = new_immBlock();
2041 set_cur_block(cur_block);
2042 create_condition_evaluation(binary_expression->left, extra_block,
2044 mature_immBlock(extra_block);
2045 set_cur_block(extra_block);
2046 create_condition_evaluation(binary_expression->right, true_block,
2050 case EXPR_BINARY_LOGICAL_OR: {
2051 const binary_expression_t *binary_expression = &expression->binary;
2053 ir_node *cur_block = get_cur_block();
2054 ir_node *extra_block = new_immBlock();
2055 set_cur_block(cur_block);
2056 create_condition_evaluation(binary_expression->left, true_block,
2058 mature_immBlock(extra_block);
2059 set_cur_block(extra_block);
2060 create_condition_evaluation(binary_expression->right, true_block,
2068 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2069 ir_node *condition = expression_to_modeb(expression);
2070 ir_node *cond = new_d_Cond(dbgi, condition);
2071 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2072 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2074 add_immBlock_pred(true_block, true_proj);
2075 add_immBlock_pred(false_block, false_proj);
2077 set_cur_block(NULL);
2082 static void create_declaration_entity(declaration_t *declaration,
2083 declaration_type_t declaration_type,
2084 ir_type *parent_type)
2086 ident *const id = new_id_from_str(declaration->symbol->string);
2087 ir_type *const irtype = get_ir_type(declaration->type);
2088 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2089 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2090 set_entity_ld_ident(entity, id);
2092 declaration->declaration_type = (unsigned char) declaration_type;
2093 declaration->v.entity = entity;
2094 set_entity_variability(entity, variability_uninitialized);
2095 if(parent_type == get_tls_type())
2096 set_entity_allocation(entity, allocation_automatic);
2097 else if(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE)
2098 set_entity_allocation(entity, allocation_static);
2099 /* TODO: visibility? */
2102 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2104 enum compound_graph_entry_type_t {
2105 COMPOUND_GRAPH_ENTRY_ARRAY,
2106 COMPOUND_GRAPH_ENTRY_COMPOUND
2109 struct compound_graph_path_entry_t {
2115 compound_graph_path_entry_t *prev;
2118 static void create_initializer_object(initializer_t *initializer, type_t *type,
2119 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2121 static compound_graph_path *create_compound_path(ir_type *type,
2122 compound_graph_path_entry_t *entry, int len)
2124 compound_graph_path *path = new_compound_graph_path(type, len);
2127 for( ; entry != NULL; entry = entry->prev, --i) {
2129 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2130 set_compound_graph_path_node(path, i, entry->v.entity);
2132 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2133 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2141 static void create_initializer_value(initializer_value_t *initializer,
2143 compound_graph_path_entry_t *entry,
2146 ir_node *node = expression_to_firm(initializer->value);
2147 ir_type *type = get_entity_type(entity);
2148 compound_graph_path *path = create_compound_path(type, entry, len);
2149 add_compound_ent_value_w_path(entity, node, path);
2152 static void create_initializer_compound(initializer_list_t *initializer,
2153 compound_type_t *type,
2155 compound_graph_path_entry_t *last_entry,
2158 declaration_t *compound_declaration = type->declaration;
2160 declaration_t *compound_entry = compound_declaration->context.declarations;
2162 compound_graph_path_entry_t entry;
2163 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2164 entry.prev = last_entry;
2168 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2169 if(compound_entry->symbol == NULL)
2171 if(compound_entry->namespc != NAMESPACE_NORMAL)
2174 if(i >= initializer->len)
2177 entry.v.entity = compound_entry->v.entity;
2179 initializer_t *sub_initializer = initializer->initializers[i];
2181 assert(compound_entry != NULL);
2182 assert(compound_entry->declaration_type
2183 == DECLARATION_TYPE_COMPOUND_MEMBER);
2185 if(sub_initializer->type == INITIALIZER_VALUE) {
2186 create_initializer_value(&sub_initializer->value,
2187 entity, &entry, len);
2189 type_t *entry_type = skip_typeref(compound_entry->type);
2190 create_initializer_object(sub_initializer, entry_type, entity,
2198 static void create_initializer_array(initializer_list_t *initializer,
2199 array_type_t *type, ir_entity *entity,
2200 compound_graph_path_entry_t *last_entry,
2203 type_t *element_type = type->element_type;
2204 element_type = skip_typeref(element_type);
2206 compound_graph_path_entry_t entry;
2207 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2208 entry.prev = last_entry;
2212 for(i = 0; i < initializer->len; ++i) {
2213 entry.v.array_index = i;
2215 initializer_t *sub_initializer = initializer->initializers[i];
2217 if(sub_initializer->type == INITIALIZER_VALUE) {
2218 create_initializer_value(&sub_initializer->value,
2219 entity, &entry, len);
2221 create_initializer_object(sub_initializer, element_type, entity,
2227 /* TODO: initialize rest... */
2228 if(type->size_expression != NULL) {
2229 size_t array_len = fold_constant(type->size_expression);
2230 for( ; i < array_len; ++i) {
2237 static void create_initializer_string(initializer_string_t *initializer,
2238 array_type_t *type, ir_entity *entity,
2239 compound_graph_path_entry_t *last_entry,
2242 type_t *element_type = type->element_type;
2243 element_type = skip_typeref(element_type);
2245 compound_graph_path_entry_t entry;
2246 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2247 entry.prev = last_entry;
2250 ir_type *irtype = get_entity_type(entity);
2251 size_t arr_len = get_array_type_size(type);
2252 const char *p = initializer->string;
2254 for(i = 0; i < arr_len; ++i, ++p) {
2255 entry.v.array_index = i;
2257 ir_node *node = new_Const_long(mode_Bs, *p);
2258 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2259 add_compound_ent_value_w_path(entity, node, path);
2266 static void create_initializer_wide_string(
2267 const initializer_wide_string_t *const initializer, array_type_t *const type,
2268 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2271 type_t *element_type = type->element_type;
2272 element_type = skip_typeref(element_type);
2274 compound_graph_path_entry_t entry;
2275 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2276 entry.prev = last_entry;
2279 ir_type *const irtype = get_entity_type(entity);
2280 const size_t arr_len = get_array_type_size(type);
2281 const wchar_rep_t * p = initializer->string.begin;
2282 const wchar_rep_t *const end = p + initializer->string.size;
2283 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2284 entry.v.array_index = i;
2286 ir_node *node = new_Const_long(mode_int, *p);
2287 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2288 add_compound_ent_value_w_path(entity, node, path);
2292 static void create_initializer_object(initializer_t *initializer, type_t *type,
2293 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2295 if(is_type_array(type)) {
2296 array_type_t *array_type = &type->array;
2298 switch (initializer->type) {
2299 case INITIALIZER_STRING: {
2300 initializer_string_t *const string = &initializer->string;
2301 create_initializer_string(string, array_type, entity, entry, len);
2305 case INITIALIZER_WIDE_STRING: {
2306 initializer_wide_string_t *const string = &initializer->wide_string;
2307 create_initializer_wide_string(string, array_type, entity, entry, len);
2311 case INITIALIZER_LIST: {
2312 initializer_list_t *const list = &initializer->list;
2313 create_initializer_array(list, array_type, entity, entry, len);
2317 case INITIALIZER_VALUE:
2320 panic("Unhandled initializer");
2322 assert(initializer->type == INITIALIZER_LIST);
2323 initializer_list_t *list = &initializer->list;
2325 assert(is_type_compound(type));
2326 compound_type_t *compound_type = &type->compound;
2327 create_initializer_compound(list, compound_type, entity, entry, len);
2331 static void create_initializer_local_variable_entity(declaration_t *declaration)
2333 initializer_t *initializer = declaration->init.initializer;
2334 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2335 ir_entity *entity = declaration->v.entity;
2336 ir_node *memory = get_store();
2337 ir_node *nomem = new_NoMem();
2338 ir_node *frame = get_irg_frame(current_ir_graph);
2339 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2341 if(initializer->type == INITIALIZER_VALUE) {
2342 initializer_value_t *initializer_value = &initializer->value;
2344 ir_node *value = expression_to_firm(initializer_value->value);
2345 type_t *type = skip_typeref(declaration->type);
2346 assign_value(dbgi, addr, type, value);
2350 /* create a "template" entity which is copied to the entity on the stack */
2351 ident *const id = unique_ident("initializer");
2352 ir_type *const irtype = get_ir_type(declaration->type);
2353 ir_type *const global_type = get_glob_type();
2354 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
2355 set_entity_ld_ident(init_entity, id);
2357 set_entity_variability(init_entity, variability_initialized);
2358 set_entity_visibility(init_entity, visibility_local);
2359 set_entity_allocation(init_entity, allocation_static);
2361 ir_graph *const old_current_ir_graph = current_ir_graph;
2362 current_ir_graph = get_const_code_irg();
2364 type_t *const type = skip_typeref(declaration->type);
2365 create_initializer_object(initializer, type, init_entity, NULL, 0);
2367 assert(current_ir_graph == get_const_code_irg());
2368 current_ir_graph = old_current_ir_graph;
2370 ir_node *const src_addr = create_symconst(dbgi, init_entity);
2371 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2373 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2374 set_store(copyb_mem);
2377 static void create_initializer(declaration_t *declaration)
2379 initializer_t *initializer = declaration->init.initializer;
2380 if(initializer == NULL)
2383 declaration_type_t declaration_type
2384 = (declaration_type_t) declaration->declaration_type;
2385 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2386 create_initializer_local_variable_entity(declaration);
2390 if(initializer->type == INITIALIZER_VALUE) {
2391 initializer_value_t *initializer_value = &initializer->value;
2393 ir_node *value = expression_to_firm(initializer_value->value);
2395 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2396 set_value(declaration->v.value_number, value);
2398 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2400 ir_entity *entity = declaration->v.entity;
2402 set_entity_variability(entity, variability_initialized);
2403 set_atomic_ent_value(entity, value);
2406 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2407 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2409 ir_entity *entity = declaration->v.entity;
2410 set_entity_variability(entity, variability_initialized);
2412 type_t *type = skip_typeref(declaration->type);
2413 create_initializer_object(initializer, type, entity, NULL, 0);
2417 static void create_local_variable(declaration_t *declaration)
2419 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2421 bool needs_entity = declaration->address_taken;
2422 type_t *type = skip_typeref(declaration->type);
2424 if(is_type_array(type) || is_type_compound(type)) {
2425 needs_entity = true;
2429 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2430 create_declaration_entity(declaration,
2431 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2434 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2435 declaration->v.value_number = next_value_number_function;
2436 ++next_value_number_function;
2439 create_initializer(declaration);
2442 static void create_local_static_variable(declaration_t *declaration)
2444 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2446 type_t *const type = skip_typeref(declaration->type);
2447 ir_type *const global_type = get_glob_type();
2448 ident *const id = unique_ident(declaration->symbol->string);
2449 ir_type *const irtype = get_ir_type(type);
2450 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2451 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
2452 set_entity_ld_ident(entity, id);
2454 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2455 declaration->v.entity = entity;
2456 set_entity_variability(entity, variability_uninitialized);
2457 set_entity_visibility(entity, visibility_local);
2458 set_entity_allocation(entity, allocation_static);
2460 ir_graph *const old_current_ir_graph = current_ir_graph;
2461 current_ir_graph = get_const_code_irg();
2463 create_initializer(declaration);
2465 assert(current_ir_graph == get_const_code_irg());
2466 current_ir_graph = old_current_ir_graph;
2471 static void return_statement_to_firm(return_statement_t *statement)
2473 if(get_cur_block() == NULL)
2476 ir_type *func_irtype = get_ir_type(current_function_decl->type);
2478 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2482 if(get_method_n_ress(func_irtype) > 0) {
2483 ir_type *res_type = get_method_res_type(func_irtype, 0);
2485 if(statement->return_value != NULL) {
2486 ir_node *node = expression_to_firm(statement->return_value);
2487 node = do_strict_conv(dbgi, node);
2491 if(is_compound_type(res_type)) {
2494 mode = get_type_mode(res_type);
2496 in[0] = new_Unknown(mode);
2500 /* build return_value for its side effects */
2501 if(statement->return_value != NULL) {
2502 expression_to_firm(statement->return_value);
2507 ir_node *store = get_store();
2508 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
2510 ir_node *end_block = get_irg_end_block(current_ir_graph);
2511 add_immBlock_pred(end_block, ret);
2513 set_cur_block(NULL);
2516 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
2518 if(get_cur_block() == NULL)
2521 return expression_to_firm(statement->expression);
2524 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
2526 ir_node *result = NULL;
2527 statement_t *statement = compound->statements;
2528 for( ; statement != NULL; statement = statement->base.next) {
2529 //context2firm(&statement->context);
2531 if(statement->base.next == NULL
2532 && statement->type == STATEMENT_EXPRESSION) {
2533 result = expression_statement_to_firm(
2534 (expression_statement_t*) statement);
2537 statement_to_firm(statement);
2543 static void create_local_declaration(declaration_t *declaration)
2545 type_t *type = skip_typeref(declaration->type);
2547 switch ((storage_class_tag_t) declaration->storage_class) {
2548 case STORAGE_CLASS_STATIC:
2549 create_local_static_variable(declaration);
2551 case STORAGE_CLASS_ENUM_ENTRY:
2552 panic("enum entry declaration in local block found");
2553 case STORAGE_CLASS_EXTERN:
2554 panic("extern declaration in local block found");
2555 case STORAGE_CLASS_NONE:
2556 case STORAGE_CLASS_AUTO:
2557 case STORAGE_CLASS_REGISTER:
2558 if(is_type_function(type)) {
2559 panic("nested functions not supported yet");
2561 create_local_variable(declaration);
2564 case STORAGE_CLASS_TYPEDEF:
2565 case STORAGE_CLASS_THREAD:
2566 case STORAGE_CLASS_THREAD_EXTERN:
2567 case STORAGE_CLASS_THREAD_STATIC:
2570 panic("invalid storage class found");
2573 static void declaration_statement_to_firm(declaration_statement_t *statement)
2575 declaration_t *declaration = statement->declarations_begin;
2576 declaration_t *end = statement->declarations_end->next;
2577 for( ; declaration != end; declaration = declaration->next) {
2578 create_local_variable(declaration);
2582 static void if_statement_to_firm(if_statement_t *statement)
2584 ir_node *cur_block = get_cur_block();
2586 ir_node *fallthrough_block = new_immBlock();
2588 /* the true (blocks) */
2589 ir_node *true_block;
2590 if (statement->true_statement != NULL) {
2591 true_block = new_immBlock();
2592 statement_to_firm(statement->true_statement);
2593 if(get_cur_block() != NULL) {
2594 ir_node *jmp = new_Jmp();
2595 add_immBlock_pred(fallthrough_block, jmp);
2598 true_block = fallthrough_block;
2601 /* the false (blocks) */
2602 ir_node *false_block;
2603 if(statement->false_statement != NULL) {
2604 false_block = new_immBlock();
2606 statement_to_firm(statement->false_statement);
2607 if(get_cur_block() != NULL) {
2608 ir_node *jmp = new_Jmp();
2609 add_immBlock_pred(fallthrough_block, jmp);
2612 false_block = fallthrough_block;
2615 /* create the condition */
2616 if(cur_block != NULL) {
2617 set_cur_block(cur_block);
2618 create_condition_evaluation(statement->condition, true_block,
2622 mature_immBlock(true_block);
2623 if(false_block != fallthrough_block) {
2624 mature_immBlock(false_block);
2626 mature_immBlock(fallthrough_block);
2628 set_cur_block(fallthrough_block);
2631 static void while_statement_to_firm(while_statement_t *statement)
2633 ir_node *jmp = NULL;
2634 if(get_cur_block() != NULL) {
2638 /* create the header block */
2639 ir_node *header_block = new_immBlock();
2641 add_immBlock_pred(header_block, jmp);
2644 /* the false block */
2645 ir_node *false_block = new_immBlock();
2648 ir_node *body_block;
2649 if (statement->body != NULL) {
2650 ir_node *old_continue_label = continue_label;
2651 ir_node *old_break_label = break_label;
2652 continue_label = header_block;
2653 break_label = false_block;
2655 body_block = new_immBlock();
2656 statement_to_firm(statement->body);
2658 assert(continue_label == header_block);
2659 assert(break_label == false_block);
2660 continue_label = old_continue_label;
2661 break_label = old_break_label;
2663 if(get_cur_block() != NULL) {
2665 add_immBlock_pred(header_block, jmp);
2668 body_block = header_block;
2671 /* create the condition */
2672 set_cur_block(header_block);
2674 create_condition_evaluation(statement->condition, body_block, false_block);
2675 mature_immBlock(body_block);
2676 mature_immBlock(false_block);
2677 mature_immBlock(header_block);
2679 set_cur_block(false_block);
2682 static void do_while_statement_to_firm(do_while_statement_t *statement)
2684 ir_node *jmp = NULL;
2685 if(get_cur_block() != NULL) {
2689 /* create the header block */
2690 ir_node *header_block = new_immBlock();
2692 /* the false block */
2693 ir_node *false_block = new_immBlock();
2696 ir_node *body_block = new_immBlock();
2698 add_immBlock_pred(body_block, jmp);
2701 if (statement->body != NULL) {
2702 ir_node *old_continue_label = continue_label;
2703 ir_node *old_break_label = break_label;
2704 continue_label = header_block;
2705 break_label = false_block;
2707 statement_to_firm(statement->body);
2709 assert(continue_label == header_block);
2710 assert(break_label == false_block);
2711 continue_label = old_continue_label;
2712 break_label = old_break_label;
2714 if (get_cur_block() == NULL) {
2715 mature_immBlock(header_block);
2716 mature_immBlock(body_block);
2717 mature_immBlock(false_block);
2722 ir_node *body_jmp = new_Jmp();
2723 add_immBlock_pred(header_block, body_jmp);
2724 mature_immBlock(header_block);
2726 /* create the condition */
2727 set_cur_block(header_block);
2729 create_condition_evaluation(statement->condition, body_block, false_block);
2730 mature_immBlock(body_block);
2731 mature_immBlock(false_block);
2732 mature_immBlock(header_block);
2734 set_cur_block(false_block);
2737 static void for_statement_to_firm(for_statement_t *statement)
2739 ir_node *jmp = NULL;
2740 if (get_cur_block() != NULL) {
2741 if(statement->initialisation != NULL) {
2742 expression_to_firm(statement->initialisation);
2745 /* create declarations */
2746 declaration_t *declaration = statement->context.declarations;
2747 for( ; declaration != NULL; declaration = declaration->next) {
2748 create_local_declaration(declaration);
2755 /* create the step block */
2756 ir_node *const step_block = new_immBlock();
2757 if (statement->step != NULL) {
2758 expression_to_firm(statement->step);
2760 ir_node *const step_jmp = new_Jmp();
2762 /* create the header block */
2763 ir_node *const header_block = new_immBlock();
2765 add_immBlock_pred(header_block, jmp);
2767 add_immBlock_pred(header_block, step_jmp);
2769 /* the false block */
2770 ir_node *const false_block = new_immBlock();
2773 ir_node * body_block;
2774 if (statement->body != NULL) {
2775 ir_node *const old_continue_label = continue_label;
2776 ir_node *const old_break_label = break_label;
2777 continue_label = step_block;
2778 break_label = false_block;
2780 body_block = new_immBlock();
2781 statement_to_firm(statement->body);
2783 assert(continue_label == step_block);
2784 assert(break_label == false_block);
2785 continue_label = old_continue_label;
2786 break_label = old_break_label;
2788 if (get_cur_block() != NULL) {
2790 add_immBlock_pred(step_block, jmp);
2793 body_block = step_block;
2796 /* create the condition */
2797 set_cur_block(header_block);
2798 if (statement->condition != NULL) {
2799 create_condition_evaluation(statement->condition, body_block,
2802 keep_alive(header_block);
2804 add_immBlock_pred(body_block, jmp);
2807 mature_immBlock(body_block);
2808 mature_immBlock(false_block);
2809 mature_immBlock(step_block);
2810 mature_immBlock(header_block);
2811 mature_immBlock(false_block);
2813 set_cur_block(false_block);
2816 static void create_jump_statement(const statement_t *statement,
2817 ir_node *target_block)
2819 if(get_cur_block() == NULL)
2822 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2823 ir_node *jump = new_d_Jmp(dbgi);
2824 add_immBlock_pred(target_block, jump);
2826 set_cur_block(NULL);
2829 static void switch_statement_to_firm(const switch_statement_t *statement)
2831 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2833 ir_node *expression = expression_to_firm(statement->expression);
2834 ir_node *cond = new_d_Cond(dbgi, expression);
2835 ir_node *break_block = new_immBlock();
2837 set_cur_block(NULL);
2839 ir_node *const old_switch_cond = current_switch_cond;
2840 ir_node *const old_break_label = break_label;
2841 const bool old_saw_default_label = saw_default_label;
2842 current_switch_cond = cond;
2843 break_label = break_block;
2845 statement_to_firm(statement->body);
2847 if(get_cur_block() != NULL) {
2848 ir_node *jmp = new_Jmp();
2849 add_immBlock_pred(break_block, jmp);
2852 if (!saw_default_label) {
2853 set_cur_block(get_nodes_block(cond));
2854 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2855 MAGIC_DEFAULT_PN_NUMBER);
2856 add_immBlock_pred(break_block, proj);
2859 assert(current_switch_cond == cond);
2860 assert(break_label == break_block);
2861 current_switch_cond = old_switch_cond;
2862 break_label = old_break_label;
2863 saw_default_label = old_saw_default_label;
2865 mature_immBlock(break_block);
2866 set_cur_block(break_block);
2869 static void case_label_to_firm(const case_label_statement_t *statement)
2871 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2873 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2875 /* let's create a node and hope firm constant folding creates a Const
2878 set_cur_block(get_nodes_block(current_switch_cond));
2879 if(statement->expression) {
2880 long pn = fold_constant(statement->expression);
2881 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2882 /* oops someone detected our cheating... */
2883 panic("magic default pn used");
2885 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2887 saw_default_label = true;
2888 proj = new_d_defaultProj(dbgi, current_switch_cond,
2889 MAGIC_DEFAULT_PN_NUMBER);
2892 ir_node *block = new_immBlock();
2893 if (fallthrough != NULL) {
2894 add_immBlock_pred(block, fallthrough);
2896 add_immBlock_pred(block, proj);
2897 mature_immBlock(block);
2899 if(statement->label_statement != NULL) {
2900 statement_to_firm(statement->label_statement);
2904 static ir_node *get_label_block(declaration_t *label)
2906 assert(label->namespc == NAMESPACE_LABEL);
2908 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2909 return label->v.block;
2911 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2913 ir_node *old_cur_block = get_cur_block();
2914 ir_node *block = new_immBlock();
2915 set_cur_block(old_cur_block);
2917 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2918 label->v.block = block;
2920 ARR_APP1(ir_node *, imature_blocks, block);
2925 static void label_to_firm(const label_statement_t *statement)
2927 ir_node *block = get_label_block(statement->label);
2929 if(get_cur_block() != NULL) {
2930 ir_node *jmp = new_Jmp();
2931 add_immBlock_pred(block, jmp);
2934 set_cur_block(block);
2937 if(statement->label_statement != NULL) {
2938 statement_to_firm(statement->label_statement);
2942 static void goto_to_firm(const goto_statement_t *statement)
2944 if(get_cur_block() == NULL)
2947 ir_node *block = get_label_block(statement->label);
2948 ir_node *jmp = new_Jmp();
2949 add_immBlock_pred(block, jmp);
2951 set_cur_block(NULL);
2954 typedef enum modifier_t {
2955 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
2956 ASM_MODIFIER_READ_WRITE = 1 << 1,
2957 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
2958 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
2962 static void asm_statement_to_firm(const asm_statement_t *statement)
2964 bool needs_memory = false;
2966 size_t n_clobbers = 0;
2967 asm_clobber_t *clobber = statement->clobbers;
2968 for( ; clobber != NULL; clobber = clobber->next) {
2969 if(strcmp(clobber->clobber, "memory") == 0) {
2970 needs_memory = true;
2974 ident *id = new_id_from_str(clobber->clobber);
2975 obstack_ptr_grow(&asm_obst, id);
2978 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
2979 ident **clobbers = NULL;
2980 if(n_clobbers > 0) {
2981 clobbers = obstack_finish(&asm_obst);
2984 /* find and count input and output constraints */
2985 asm_constraint_t *constraint = statement->inputs;
2986 for( ; constraint != NULL; constraint = constraint->next) {
2988 bool supports_memop = false;
2989 for(const char *c = constraint->constraints; *c != 0; ++c) {
2990 /* TODO: improve error messages */
2994 panic("multiple alternative assembler constraints not "
3002 supports_memop = true;
3003 obstack_1grow(&asm_obst, *c);
3006 if(modifiers & ASM_MODIFIER_READ_WRITE)
3007 panic("inconsistent register constraints");
3008 modifiers |= ASM_MODIFIER_WRITE_ONLY;
3011 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
3012 panic("inconsistent register constraints");
3013 modifiers |= ASM_MODIFIER_READ_WRITE;
3016 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
3017 panic("early clobber assembler constraint not supported yet");
3020 modifiers |= ASM_MODIFIER_COMMUTATIVE;
3021 panic("commutative assembler constraint not supported yet");
3024 /* skip register preferences stuff... */
3025 while(*c != 0 && *c != ',')
3029 /* skip register preferences stuff... */
3033 obstack_1grow(&asm_obst, *c);
3037 obstack_1grow(&asm_obst, '\0');
3038 const char *constraint_string = obstack_finish(&asm_obst);
3040 needs_memory |= supports_memop;
3041 if(supports_memop) {
3049 static void statement_to_firm(statement_t *statement)
3051 switch(statement->type) {
3052 case STATEMENT_INVALID:
3053 panic("invalid statement found");
3054 case STATEMENT_COMPOUND:
3055 compound_statement_to_firm(&statement->compound);
3057 case STATEMENT_RETURN:
3058 return_statement_to_firm(&statement->returns);
3060 case STATEMENT_EXPRESSION:
3061 expression_statement_to_firm(&statement->expression);
3064 if_statement_to_firm(&statement->ifs);
3066 case STATEMENT_WHILE:
3067 while_statement_to_firm(&statement->whiles);
3069 case STATEMENT_DO_WHILE:
3070 do_while_statement_to_firm(&statement->do_while);
3072 case STATEMENT_DECLARATION:
3073 declaration_statement_to_firm(&statement->declaration);
3075 case STATEMENT_BREAK:
3076 create_jump_statement(statement, break_label);
3078 case STATEMENT_CONTINUE:
3079 create_jump_statement(statement, continue_label);
3081 case STATEMENT_SWITCH:
3082 switch_statement_to_firm(&statement->switchs);
3084 case STATEMENT_CASE_LABEL:
3085 case_label_to_firm(&statement->case_label);
3088 for_statement_to_firm(&statement->fors);
3090 case STATEMENT_LABEL:
3091 label_to_firm(&statement->label);
3093 case STATEMENT_GOTO:
3094 goto_to_firm(&statement->gotos);
3097 //asm_statement_to_firm(&statement->asms);
3101 panic("Statement not implemented\n");
3104 static int count_decls_in_expression(const expression_t *expression);
3106 static int count_local_declarations(const declaration_t * decl,
3107 const declaration_t *const end)
3110 for (; decl != end; decl = decl->next) {
3111 const type_t *type = skip_typeref(decl->type);
3112 switch (type->type) {
3116 if (!decl->address_taken)
3122 const initializer_t *initializer = decl->init.initializer;
3123 /* FIXME: should walk initializer hierarchies... */
3124 if(initializer != NULL && initializer->type == INITIALIZER_VALUE) {
3125 count += count_decls_in_expression(initializer->value.value);
3131 static int count_decls_in_expression(const expression_t *expression) {
3132 if(expression == NULL)
3135 switch(expression->base.type) {
3136 case EXPR_STATEMENT:
3137 return count_decls_in_stmts(expression->statement.statement);
3139 return count_decls_in_expression(expression->binary.left)
3140 + count_decls_in_expression(expression->binary.right);
3142 return count_decls_in_expression(expression->unary.value);
3148 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
3149 * (or implement all the missing expressions here/implement a walker)
3155 static int count_decls_in_stmts(const statement_t *stmt)
3158 for (; stmt != NULL; stmt = stmt->base.next) {
3159 switch (stmt->type) {
3160 case STATEMENT_DECLARATION: {
3161 const declaration_statement_t *const decl_stmt = &stmt->declaration;
3162 count += count_local_declarations(decl_stmt->declarations_begin,
3163 decl_stmt->declarations_end->next);
3167 case STATEMENT_COMPOUND: {
3168 const compound_statement_t *const comp =
3169 (const compound_statement_t*)stmt;
3170 count += count_decls_in_stmts(comp->statements);
3174 case STATEMENT_IF: {
3175 const if_statement_t *const if_stmt = &stmt->ifs;
3176 count += count_decls_in_expression(if_stmt->condition);
3177 count += count_decls_in_stmts(if_stmt->true_statement);
3178 count += count_decls_in_stmts(if_stmt->false_statement);
3182 case STATEMENT_SWITCH: {
3183 const switch_statement_t *const switch_stmt = &stmt->switchs;
3184 count += count_decls_in_expression(switch_stmt->expression);
3185 count += count_decls_in_stmts(switch_stmt->body);
3189 case STATEMENT_LABEL: {
3190 const label_statement_t *const label_stmt = &stmt->label;
3191 count += count_decls_in_stmts(label_stmt->label_statement);
3195 case STATEMENT_WHILE: {
3196 const while_statement_t *const while_stmt = &stmt->whiles;
3197 count += count_decls_in_expression(while_stmt->condition);
3198 count += count_decls_in_stmts(while_stmt->body);
3202 case STATEMENT_DO_WHILE: {
3203 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
3204 count += count_decls_in_expression(do_while_stmt->condition);
3205 count += count_decls_in_stmts(do_while_stmt->body);
3209 case STATEMENT_FOR: {
3210 const for_statement_t *const for_stmt = &stmt->fors;
3211 count += count_local_declarations(for_stmt->context.declarations, NULL);
3212 count += count_decls_in_expression(for_stmt->initialisation);
3213 count += count_decls_in_expression(for_stmt->condition);
3214 count += count_decls_in_expression(for_stmt->step);
3215 count += count_decls_in_stmts(for_stmt->body);
3219 case STATEMENT_CASE_LABEL: {
3220 const case_label_statement_t *label = &stmt->case_label;
3221 count += count_decls_in_expression(label->expression);
3222 count += count_decls_in_stmts(label->label_statement);
3227 case STATEMENT_BREAK:
3228 case STATEMENT_CONTINUE:
3231 case STATEMENT_EXPRESSION: {
3232 const expression_statement_t *expr_stmt = &stmt->expression;
3233 count += count_decls_in_expression(expr_stmt->expression);
3237 case STATEMENT_GOTO:
3238 case STATEMENT_INVALID:
3241 case STATEMENT_RETURN: {
3242 const return_statement_t *ret_stmt = &stmt->returns;
3243 count += count_decls_in_expression(ret_stmt->return_value);
3251 static int get_function_n_local_vars(declaration_t *declaration)
3255 /* count parameters */
3256 count += count_local_declarations(declaration->context.declarations, NULL);
3258 /* count local variables declared in body */
3259 count += count_decls_in_stmts(declaration->init.statement);
3264 static void initialize_function_parameters(declaration_t *declaration)
3266 ir_graph *irg = current_ir_graph;
3267 ir_node *args = get_irg_args(irg);
3268 ir_node *start_block = get_irg_start_block(irg);
3269 ir_type *function_irtype = get_ir_type(declaration->type);
3272 declaration_t *parameter = declaration->context.declarations;
3273 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3274 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
3275 type_t *type = skip_typeref(parameter->type);
3277 bool needs_entity = parameter->address_taken;
3278 assert(!is_type_array(type));
3279 if(is_type_compound(type)) {
3280 needs_entity = true;
3284 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3285 ident *id = new_id_from_str(parameter->symbol->string);
3286 set_entity_ident(entity, id);
3288 parameter->declaration_type
3289 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
3290 parameter->v.entity = entity;
3294 ir_mode *mode = get_ir_mode(parameter->type);
3296 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3298 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
3299 parameter->v.value_number = next_value_number_function;
3300 ++next_value_number_function;
3302 set_value(parameter->v.value_number, proj);
3307 * Handle additional decl modifiers for IR-graphs
3309 * @param irg the IR-graph
3310 * @param dec_modifiers additional modifiers
3312 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
3314 if (decl_modifiers & DM_NORETURN) {
3315 /* TRUE if the declaration includes the Microsoft
3316 __declspec(noreturn) specifier. */
3317 set_irg_additional_property(irg, mtp_property_noreturn);
3319 if (decl_modifiers & DM_NOTHROW) {
3320 /* TRUE if the declaration includes the Microsoft
3321 __declspec(nothrow) specifier. */
3322 set_irg_additional_property(irg, mtp_property_nothrow);
3324 if (decl_modifiers & DM_NAKED) {
3325 /* TRUE if the declaration includes the Microsoft
3326 __declspec(naked) specifier. */
3327 set_irg_additional_property(irg, mtp_property_naked);
3329 if (decl_modifiers & DM_FORCEINLINE) {
3330 /* TRUE if the declaration includes the
3331 Microsoft __forceinline specifier. */
3332 set_irg_inline_property(irg, irg_inline_forced);
3334 if (decl_modifiers & DM_NOINLINE) {
3335 /* TRUE if the declaration includes the Microsoft
3336 __declspec(noinline) specifier. */
3337 set_irg_inline_property(irg, irg_inline_forbidden);
3341 static void create_function(declaration_t *declaration)
3343 ir_entity *function_entity = get_function_entity(declaration);
3345 if(declaration->init.statement == NULL)
3348 current_function_decl = declaration;
3349 current_function_name = NULL;
3351 assert(imature_blocks == NULL);
3352 imature_blocks = NEW_ARR_F(ir_node*, 0);
3354 int n_local_vars = get_function_n_local_vars(declaration);
3355 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3356 ir_node *first_block = get_cur_block();
3358 /* set inline flags */
3359 if (declaration->is_inline)
3360 set_irg_inline_property(irg, irg_inline_recomended);
3361 handle_decl_modifier_irg(irg, declaration->decl_modifiers);
3363 next_value_number_function = 0;
3364 initialize_function_parameters(declaration);
3366 statement_to_firm(declaration->init.statement);
3368 ir_node *end_block = get_irg_end_block(irg);
3370 /* do we have a return statement yet? */
3371 if(get_cur_block() != NULL) {
3372 type_t *type = skip_typeref(declaration->type);
3373 assert(is_type_function(type));
3374 const function_type_t *func_type = &type->function;
3375 const type_t *return_type
3376 = skip_typeref(func_type->return_type);
3379 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3380 ret = new_Return(get_store(), 0, NULL);
3383 if(is_type_scalar(return_type)) {
3384 mode = get_ir_mode(func_type->return_type);
3390 /* ยง5.1.2.2.3 main implicitly returns 0 */
3391 if (strcmp(declaration->symbol->string, "main") == 0) {
3392 in[0] = new_Const(mode, get_mode_null(mode));
3394 in[0] = new_Unknown(mode);
3396 ret = new_Return(get_store(), 1, in);
3398 add_immBlock_pred(end_block, ret);
3401 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3402 mature_immBlock(imature_blocks[i]);
3404 DEL_ARR_F(imature_blocks);
3405 imature_blocks = NULL;
3407 mature_immBlock(first_block);
3408 mature_immBlock(end_block);
3410 irg_finalize_cons(irg);
3412 /* finalize the frame type */
3413 ir_type *frame_type = get_irg_frame_type(irg);
3414 int n = get_compound_n_members(frame_type);
3417 for(int i = 0; i < n; ++i) {
3418 ir_entity *entity = get_compound_member(frame_type, i);
3419 ir_type *entity_type = get_entity_type(entity);
3421 int align = get_type_alignment_bytes(entity_type);
3422 if(align > align_all)
3426 misalign = offset % align;
3428 offset += align - misalign;
3432 set_entity_offset(entity, offset);
3433 offset += get_type_size_bytes(entity_type);
3435 set_type_size_bytes(frame_type, offset);
3436 set_type_alignment_bytes(frame_type, align_all);
3437 set_type_state(frame_type, layout_fixed);
3442 static void create_global_variable(declaration_t *declaration)
3446 switch ((storage_class_tag_t)declaration->storage_class) {
3447 case STORAGE_CLASS_STATIC:
3448 vis = visibility_local;
3451 case STORAGE_CLASS_EXTERN:
3452 vis = visibility_external_allocated;
3455 case STORAGE_CLASS_NONE:
3456 vis = visibility_external_visible;
3459 case STORAGE_CLASS_THREAD:
3460 vis = visibility_external_visible;
3463 case STORAGE_CLASS_THREAD_EXTERN:
3464 vis = visibility_external_allocated;
3467 case STORAGE_CLASS_THREAD_STATIC:
3468 vis = visibility_local;
3472 var_type = get_tls_type();
3476 var_type = get_glob_type();
3480 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3482 set_entity_visibility(declaration->v.entity, vis);
3484 current_ir_graph = get_const_code_irg();
3485 create_initializer(declaration);
3488 case STORAGE_CLASS_TYPEDEF:
3489 case STORAGE_CLASS_AUTO:
3490 case STORAGE_CLASS_REGISTER:
3491 case STORAGE_CLASS_ENUM_ENTRY:
3494 panic("Invalid storage class for global variable");
3497 static void context_to_firm(context_t *context)
3499 /* first pass: create declarations */
3500 declaration_t *declaration = context->declarations;
3501 for( ; declaration != NULL; declaration = declaration->next) {
3502 if(declaration->namespc != NAMESPACE_NORMAL)
3504 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3505 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3507 if(declaration->symbol == NULL)
3510 type_t *type = skip_typeref(declaration->type);
3511 if(is_type_function(type)) {
3512 get_function_entity(declaration);
3514 create_global_variable(declaration);
3518 /* second pass: create code */
3519 declaration = context->declarations;
3520 for( ; declaration != NULL; declaration = declaration->next) {
3521 if(declaration->namespc != NAMESPACE_NORMAL)
3523 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3524 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3526 if(declaration->symbol == NULL)
3529 type_t *type = declaration->type;
3530 if(type->type != TYPE_FUNCTION)
3533 create_function(declaration);
3537 void init_ast2firm(void)
3539 obstack_init(&asm_obst);
3540 init_atomic_modes();
3543 void exit_ast2firm(void)
3545 obstack_free(&asm_obst, NULL);
3548 void translation_unit_to_firm(translation_unit_t *unit)
3550 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3551 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3552 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3554 ir_type_int = get_ir_type(type_int);
3555 ir_type_const_char = get_ir_type(type_const_char);
3556 ir_type_wchar_t = get_ir_type(type_wchar_t);
3557 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3560 type_void->base.firm_type = ir_type_void;
3562 /* just to be sure */
3563 continue_label = NULL;
3565 current_switch_cond = NULL;
3567 context_to_firm(& unit->context);