9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
15 #include "adt/array.h"
20 #include "diagnostic.h"
21 #include "lang_features.h"
22 #include "driver/firm_opt.h"
23 #include "driver/firm_cmdline.h"
25 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
27 static ir_type *ir_type_const_char;
28 static ir_type *ir_type_wchar_t;
29 static ir_type *ir_type_void;
30 static ir_type *ir_type_int;
32 static type_t *type_const_char;
33 static type_t *type_void;
34 static type_t *type_int;
36 static int next_value_number_function;
37 static ir_node *continue_label;
38 static ir_node *break_label;
39 static ir_node *current_switch_cond;
40 static bool saw_default_label;
41 static ir_node **imature_blocks;
43 static const declaration_t *current_function_decl;
44 static ir_node *current_function_name;
46 static struct obstack asm_obst;
48 typedef enum declaration_kind_t {
49 DECLARATION_KIND_UNKNOWN,
50 DECLARATION_KIND_FUNCTION,
51 DECLARATION_KIND_GLOBAL_VARIABLE,
52 DECLARATION_KIND_LOCAL_VARIABLE,
53 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
54 DECLARATION_KIND_COMPOUND_MEMBER,
55 DECLARATION_KIND_LABEL_BLOCK,
56 DECLARATION_KIND_ENUM_ENTRY
59 static ir_type *get_ir_type(type_t *type);
60 static int count_decls_in_stmts(const statement_t *stmt);
62 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
64 const declaration_t *declaration = get_irg_loc_description(irg, pos);
66 warningf(declaration->source_position, "variable '%#T' might be used uninitialized",
67 declaration->type, declaration->symbol);
68 return new_r_Unknown(irg, mode);
71 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
73 const source_position_t *pos = (const source_position_t*) dbg;
76 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
80 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
82 const source_position_t *pos = (const source_position_t*) dbg;
87 return pos->input_name;
90 static dbg_info *get_dbg_info(const source_position_t *pos)
92 return (dbg_info*) pos;
95 static unsigned unique_id = 0;
97 static ident *unique_ident(const char *tag)
101 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
103 return new_id_from_str(buf);
107 * Return the signed integer mode of size bytes.
109 * @param size the size
111 static ir_mode *get_smode(unsigned size)
113 static ir_mode *s_modes[16 + 1] = {0, };
116 if (size <= 0 || size > 16)
125 snprintf(name, sizeof(name), "i%u", bits);
126 res = new_ir_mode(name, irms_int_number, bits, 1, irma_twos_complement,
127 bits <= machine_size ? machine_size : bits );
135 * Return the unsigned integer mode of size bytes.
137 * @param size the size
139 static ir_mode *get_umode(unsigned size)
141 static ir_mode *u_modes[16 + 1] = {0, };
144 if (size <= 0 || size > 16)
153 snprintf(name, sizeof(name), "u%u", bits);
154 res = new_ir_mode(name, irms_int_number, bits, 0, irma_twos_complement,
155 bits <= machine_size ? machine_size : bits );
163 * Return the pointer mode of size bytes.
165 * @param size the size
167 static ir_mode *get_ptrmode(unsigned size, char *name)
169 static ir_mode *p_modes[16 + 1] = {0, };
172 if (size <= 0 || size > 16)
182 snprintf(buf, sizeof(buf), "p%u", bits);
185 res = new_ir_mode(name, irms_reference, bits, 0, irma_twos_complement,
186 bits <= machine_size ? machine_size : bits);
190 set_reference_mode_signed_eq(res, get_smode(size));
191 set_reference_mode_unsigned_eq(res, get_umode(size));
196 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST];
198 static ir_mode *mode_int, *mode_uint;
201 * Initialises the atomic modes depending on the machine size.
203 static void init_atomic_modes(void) {
204 unsigned int_size = machine_size < 32 ? 2 : 4;
205 unsigned long_size = machine_size < 64 ? 4 : 8;
206 unsigned llong_size = machine_size < 32 ? 4 : 8;
208 /* firm has no real void... */
209 _atomic_modes[ATOMIC_TYPE_VOID] = mode_T;
210 _atomic_modes[ATOMIC_TYPE_CHAR] = char_is_signed ? get_smode(1) : get_umode(1);
211 _atomic_modes[ATOMIC_TYPE_SCHAR] = get_smode(1);
212 _atomic_modes[ATOMIC_TYPE_UCHAR] = get_umode(1);
213 _atomic_modes[ATOMIC_TYPE_SHORT] = get_smode(2);
214 _atomic_modes[ATOMIC_TYPE_USHORT] = get_umode(2);
215 _atomic_modes[ATOMIC_TYPE_INT] = get_smode(int_size);
216 _atomic_modes[ATOMIC_TYPE_UINT] = get_umode(int_size);
217 _atomic_modes[ATOMIC_TYPE_LONG] = get_smode(long_size);
218 _atomic_modes[ATOMIC_TYPE_ULONG] = get_umode(long_size);
219 _atomic_modes[ATOMIC_TYPE_LONGLONG] = get_smode(llong_size);
220 _atomic_modes[ATOMIC_TYPE_ULONGLONG] = get_umode(llong_size);
221 _atomic_modes[ATOMIC_TYPE_FLOAT] = mode_F;
222 _atomic_modes[ATOMIC_TYPE_DOUBLE] = mode_D;
223 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = mode_E;
224 _atomic_modes[ATOMIC_TYPE_BOOL] = get_umode(int_size);
226 #ifdef PROVIDE_COMPLEX
227 _atomic_modes[ATOMIC_TYPE_BOOL] = _atomic_modes[ATOMIC_TYPE_INT];
228 _atomic_modes[ATOMIC_TYPE_FLOAT_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_FLOAT];
229 _atomic_modes[ATOMIC_TYPE_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_DOUBLE];
230 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE];
233 /* Hmm, pointers should be machine size */
234 set_modeP_data(get_ptrmode(machine_size >> 3, NULL));
235 set_modeP_code(get_ptrmode(machine_size >> 3, NULL));
237 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
238 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
241 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
244 if ((unsigned)atomic_type->akind < (unsigned)ATOMIC_TYPE_LAST)
245 res = _atomic_modes[(unsigned)atomic_type->akind];
247 panic("Encountered unknown atomic type");
251 static unsigned get_type_size(type_t *type);
253 static unsigned get_atomic_type_size(const atomic_type_t *type)
255 switch(type->akind) {
256 case ATOMIC_TYPE_CHAR:
257 case ATOMIC_TYPE_SCHAR:
258 case ATOMIC_TYPE_UCHAR:
261 case ATOMIC_TYPE_SHORT:
262 case ATOMIC_TYPE_USHORT:
265 case ATOMIC_TYPE_BOOL:
266 case ATOMIC_TYPE_INT:
267 case ATOMIC_TYPE_UINT:
268 return machine_size >> 3;
270 case ATOMIC_TYPE_LONG:
271 case ATOMIC_TYPE_ULONG:
272 return machine_size > 16 ? machine_size >> 3 : 4;
274 case ATOMIC_TYPE_LONGLONG:
275 case ATOMIC_TYPE_ULONGLONG:
276 return machine_size > 16 ? 8 : 4;
278 case ATOMIC_TYPE_FLOAT:
281 case ATOMIC_TYPE_DOUBLE:
284 case ATOMIC_TYPE_LONG_DOUBLE:
287 case ATOMIC_TYPE_VOID:
290 case ATOMIC_TYPE_INVALID:
291 case ATOMIC_TYPE_LAST:
294 panic("Trying to determine size of invalid atomic type");
297 static unsigned get_compound_type_size(compound_type_t *type)
299 ir_type *irtype = get_ir_type((type_t*) type);
300 return get_type_size_bytes(irtype);
303 static unsigned get_array_type_size(array_type_t *type)
305 ir_type *irtype = get_ir_type((type_t*) type);
306 return get_type_size_bytes(irtype);
310 static unsigned get_type_size(type_t *type)
312 type = skip_typeref(type);
316 return get_atomic_type_size(&type->atomic);
318 return get_mode_size_bytes(mode_int);
319 case TYPE_COMPOUND_UNION:
320 case TYPE_COMPOUND_STRUCT:
321 return get_compound_type_size(&type->compound);
323 /* just a pointer to the function */
324 return get_mode_size_bytes(mode_P_code);
326 return get_mode_size_bytes(mode_P_data);
328 return get_array_type_size(&type->array);
330 return get_type_size(type->builtin.real_type);
332 panic("type size of bitfield request");
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->akind == ATOMIC_TYPE_LONG_DOUBLE
365 || type->akind == ATOMIC_TYPE_DOUBLE) {
366 set_type_alignment_bytes(irtype, 4);
372 static ir_type *create_method_type(const function_type_t *function_type)
374 type_t *return_type = function_type->return_type;
376 ident *id = unique_ident("functiontype");
377 int n_parameters = count_parameters(function_type);
378 int n_results = return_type == type_void ? 0 : 1;
379 ir_type *irtype = new_type_method(id, n_parameters, n_results);
381 if(return_type != type_void) {
382 ir_type *restype = get_ir_type(return_type);
383 set_method_res_type(irtype, 0, restype);
386 function_parameter_t *parameter = function_type->parameters;
388 for( ; parameter != NULL; parameter = parameter->next) {
389 ir_type *p_irtype = get_ir_type(parameter->type);
390 set_method_param_type(irtype, n, p_irtype);
394 if(function_type->variadic || function_type->unspecified_parameters) {
395 set_method_variadicity(irtype, variadicity_variadic);
401 static ir_type *create_pointer_type(pointer_type_t *type)
403 type_t *points_to = type->points_to;
404 ir_type *ir_points_to;
405 /* Avoid endless recursion if the points_to type contains this poiner type
406 * again (might be a struct). We therefore first create a void* pointer
407 * and then set the real points_to type
409 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
410 ir_type_void, mode_P_data);
411 type->type.firm_type = ir_type;
413 ir_points_to = get_ir_type(points_to);
414 set_pointer_points_to_type(ir_type, ir_points_to);
419 static ir_type *create_array_type(array_type_t *type)
421 type_t *element_type = type->element_type;
422 ir_type *ir_element_type = get_ir_type(element_type);
424 ident *id = unique_ident("array");
425 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
427 if(type->size != NULL) {
428 int n_elements = fold_constant(type->size);
430 set_array_bounds_int(ir_type, 0, 0, n_elements);
432 size_t elemsize = get_type_size_bytes(ir_element_type);
433 int align = get_type_alignment_bytes(ir_element_type);
434 if(elemsize % align > 0) {
435 elemsize += align - (elemsize % align);
437 set_type_size_bytes(ir_type, n_elements * elemsize);
438 set_type_alignment_bytes(ir_type, align);
439 set_type_state(ir_type, layout_fixed);
441 set_array_lower_bound_int(ir_type, 0, 0);
448 * Return the signed integer type of size bits.
450 * @param size the size
452 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
455 static ir_mode *s_modes[64 + 1] = {NULL, };
459 if (size <= 0 || size > 64)
462 mode = s_modes[size];
466 snprintf(name, sizeof(name), "bf_I%u", size);
467 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
468 size <= 32 ? 32 : size );
469 s_modes[size] = mode;
473 snprintf(name, sizeof(name), "I%u", size);
474 ident *id = new_id_from_str(name);
475 res = new_type_primitive(mangle_u(get_type_ident(base_tp), id), mode);
476 set_primitive_base_type(res, base_tp);
482 * Return the unsigned integer type of size bits.
484 * @param size the size
486 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
489 static ir_mode *u_modes[64 + 1] = {NULL, };
493 if (size <= 0 || size > 64)
496 mode = u_modes[size];
500 snprintf(name, sizeof(name), "bf_U%u", size);
501 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
502 size <= 32 ? 32 : size );
503 u_modes[size] = mode;
508 snprintf(name, sizeof(name), "U%u", size);
509 ident *id = new_id_from_str(name);
510 res = new_type_primitive(mangle_u(get_type_ident(base_tp), id), mode);
511 set_primitive_base_type(res, base_tp);
516 static ir_type *create_bitfield_type(bitfield_type_t *const type)
518 type_t *base = skip_typeref(type->base);
519 assert(base->kind == TYPE_ATOMIC);
520 ir_type *irbase = get_ir_type(base);
522 unsigned size = fold_constant(type->size);
524 assert(!is_type_floating(base));
525 if(is_type_signed(base)) {
526 return get_signed_int_type_for_bit_size(irbase, size);
528 return get_unsigned_int_type_for_bit_size(irbase, size);
532 #define INVALID_TYPE ((ir_type_ptr)-1)
534 static ir_type *create_struct_type(compound_type_t *type)
536 symbol_t *symbol = type->declaration->symbol;
539 id = unique_ident(symbol->string);
541 id = unique_ident("__anonymous_struct");
543 ir_type *irtype = new_type_struct(id);
545 type->type.firm_type = irtype;
547 size_t align_all = 1;
549 size_t bit_offset = 0;
550 declaration_t *entry = type->declaration->context.declarations;
551 for( ; entry != NULL; entry = entry->next) {
552 if(entry->namespc != NAMESPACE_NORMAL)
555 type_t *entry_type = skip_typeref(entry->type);
556 ir_type *base_irtype;
557 if(entry_type->kind == TYPE_BITFIELD) {
558 base_irtype = get_ir_type(entry_type->bitfield.base);
560 base_irtype = get_ir_type(entry_type);
563 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
564 size_t misalign = offset % entry_alignment;
566 dbg_info *dbgi = get_dbg_info(&entry->source_position);
567 ir_entity *entity = NULL;
568 if(entry->symbol != NULL) {
569 ident *ident = new_id_from_str(entry->symbol->string);
570 ir_type *entry_irtype = get_ir_type(entry_type);
571 entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
573 /* only bitfields are allowed to be anonymous */
574 assert(entry_type->kind == TYPE_BITFIELD);
578 size_t bits_remainder;
579 if(entry_type->kind == TYPE_BITFIELD) {
580 size_t size_bits = fold_constant(entry_type->bitfield.size);
581 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
583 if(size_bits > rest_size_bits) {
584 /* start a new bucket */
585 offset += entry_alignment - misalign;
591 /* put into current bucket */
592 base = offset - misalign;
593 bits_remainder = misalign * 8 + bit_offset;
596 offset += size_bits / 8;
597 bit_offset = bit_offset + (size_bits % 8);
599 size_t entry_size = get_type_size_bytes(base_irtype);
600 if(misalign > 0 || bit_offset > 0)
601 offset += entry_alignment - misalign;
605 offset += entry_size;
609 if(entry_alignment > align_all) {
610 if(entry_alignment % align_all != 0) {
611 panic("uneven alignments not supported yet");
613 align_all = entry_alignment;
617 set_entity_offset(entity, base);
618 set_entity_offset_bits_remainder(entity,
619 (unsigned char) bits_remainder);
620 add_struct_member(irtype, entity);
621 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
622 entry->v.entity = entity;
626 size_t misalign = offset % align_all;
627 if(misalign > 0 || bit_offset > 0) {
628 offset += align_all - misalign;
630 set_type_alignment_bytes(irtype, align_all);
631 set_type_size_bytes(irtype, offset);
632 set_type_state(irtype, layout_fixed);
637 static ir_type *create_union_type(compound_type_t *type)
639 declaration_t *declaration = type->declaration;
640 symbol_t *symbol = declaration->symbol;
643 id = unique_ident(symbol->string);
645 id = unique_ident("__anonymous_union");
647 ir_type *irtype = new_type_union(id);
649 type->type.firm_type = irtype;
653 declaration_t *entry = declaration->context.declarations;
654 for( ; entry != NULL; entry = entry->next) {
655 if(entry->namespc != NAMESPACE_NORMAL)
658 ident *ident = new_id_from_str(entry->symbol->string);
659 ir_type *entry_ir_type = get_ir_type(entry->type);
661 int entry_size = get_type_size_bytes(entry_ir_type);
662 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
664 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
665 ir_entity *const entity = new_d_entity(irtype, ident, entry_ir_type,
667 add_union_member(irtype, entity);
668 set_entity_offset(entity, 0);
669 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
670 entry->v.entity = entity;
672 if(entry_size > size) {
675 if(entry_alignment > align_all) {
676 if(entry_alignment % align_all != 0) {
677 panic("Uneven alignments not supported yet");
679 align_all = entry_alignment;
683 set_type_alignment_bytes(irtype, align_all);
684 set_type_size_bytes(irtype, size);
685 set_type_state(irtype, layout_fixed);
690 static ir_node *expression_to_firm(const expression_t *expression);
691 static inline ir_mode *get_ir_mode(type_t *type);
693 static ir_type *create_enum_type(enum_type_t *const type)
695 type->type.firm_type = ir_type_int;
697 ir_mode *const mode = get_ir_mode((type_t*) type);
698 tarval *const one = get_mode_one(mode);
699 tarval * tv_next = get_tarval_null(mode);
701 declaration_t *declaration = type->declaration->next;
702 for (; declaration != NULL; declaration = declaration->next) {
703 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
706 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
708 expression_t *const init = declaration->init.enum_value;
710 ir_node *const cnst = expression_to_firm(init);
711 if (!is_Const(cnst)) {
712 panic("couldn't fold constant");
714 tv_next = get_Const_tarval(cnst);
716 declaration->v.enum_val = tv_next;
717 tv_next = tarval_add(tv_next, one);
723 static ir_type *get_ir_type(type_t *type)
725 assert(type != NULL);
727 type = skip_typeref(type);
729 if(type->base.firm_type != NULL) {
730 assert(type->base.firm_type != INVALID_TYPE);
731 return type->base.firm_type;
734 ir_type *firm_type = NULL;
737 firm_type = create_atomic_type(&type->atomic);
740 firm_type = create_method_type(&type->function);
743 firm_type = create_pointer_type(&type->pointer);
746 firm_type = create_array_type(&type->array);
748 case TYPE_COMPOUND_STRUCT:
749 firm_type = create_struct_type(&type->compound);
751 case TYPE_COMPOUND_UNION:
752 firm_type = create_union_type(&type->compound);
755 firm_type = create_enum_type(&type->enumt);
758 firm_type = get_ir_type(type->builtin.real_type);
761 firm_type = create_bitfield_type(&type->bitfield);
769 if(firm_type == NULL)
770 panic("unknown type found");
772 type->base.firm_type = firm_type;
776 static inline ir_mode *get_ir_mode(type_t *type)
778 ir_type *irtype = get_ir_type(type);
780 /* firm doesn't report a mode for arrays somehow... */
781 if(is_Array_type(irtype)) {
785 ir_mode *mode = get_type_mode(irtype);
786 assert(mode != NULL);
790 static ident *predef_idents[rts_max];
792 /** Names of the runtime functions. */
793 static const struct {
794 int id; /**< the rts id */
795 int n_res; /**< number of return values */
796 const char *name; /**< the name of the rts function */
797 int n_params; /**< number of parameters */
798 unsigned flags; /**< language flags */
800 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
801 { rts_abort, 0, "abort", 0, _C89 },
802 { rts_abs, 1, "abs", 1, _C89 },
803 { rts_labs, 1, "labs", 1, _C89 },
804 { rts_llabs, 1, "llabs", 1, _C99 },
805 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
807 { rts_fabs, 1, "fabs", 1, _C89 },
808 { rts_sqrt, 1, "sqrt", 1, _C89 },
809 { rts_cbrt, 1, "cbrt", 1, _C99 },
810 { rts_exp, 1, "exp", 1, _C89 },
811 { rts_exp2, 1, "exp2", 1, _C89 },
812 { rts_exp10, 1, "exp10", 1, _GNUC },
813 { rts_log, 1, "log", 1, _C89 },
814 { rts_log2, 1, "log2", 1, _C89 },
815 { rts_log10, 1, "log10", 1, _C89 },
816 { rts_pow, 1, "pow", 2, _C89 },
817 { rts_sin, 1, "sin", 1, _C89 },
818 { rts_cos, 1, "cos", 1, _C89 },
819 { rts_tan, 1, "tan", 1, _C89 },
820 { rts_asin, 1, "asin", 1, _C89 },
821 { rts_acos, 1, "acos", 1, _C89 },
822 { rts_atan, 1, "atan", 1, _C89 },
823 { rts_sinh, 1, "sinh", 1, _C89 },
824 { rts_cosh, 1, "cosh", 1, _C89 },
825 { rts_tanh, 1, "tanh", 1, _C89 },
827 { rts_fabsf, 1, "fabsf", 1, _C99 },
828 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
829 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
830 { rts_expf, 1, "expf", 1, _C99 },
831 { rts_exp2f, 1, "exp2f", 1, _C99 },
832 { rts_exp10f, 1, "exp10f", 1, _GNUC },
833 { rts_logf, 1, "logf", 1, _C99 },
834 { rts_log2f, 1, "log2f", 1, _C99 },
835 { rts_log10f, 1, "log10f", 1, _C99 },
836 { rts_powf, 1, "powf", 2, _C99 },
837 { rts_sinf, 1, "sinf", 1, _C99 },
838 { rts_cosf, 1, "cosf", 1, _C99 },
839 { rts_tanf, 1, "tanf", 1, _C99 },
840 { rts_asinf, 1, "asinf", 1, _C99 },
841 { rts_acosf, 1, "acosf", 1, _C99 },
842 { rts_atanf, 1, "atanf", 1, _C99 },
843 { rts_sinhf, 1, "sinhf", 1, _C99 },
844 { rts_coshf, 1, "coshf", 1, _C99 },
845 { rts_tanhf, 1, "tanhf", 1, _C99 },
847 { rts_fabsl, 1, "fabsl", 1, _C99 },
848 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
849 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
850 { rts_expl, 1, "expl", 1, _C99 },
851 { rts_exp2l, 1, "exp2l", 1, _C99 },
852 { rts_exp10l, 1, "exp10l", 1, _GNUC },
853 { rts_logl, 1, "logl", 1, _C99 },
854 { rts_log2l, 1, "log2l", 1, _C99 },
855 { rts_log10l, 1, "log10l", 1, _C99 },
856 { rts_powl, 1, "powl", 2, _C99 },
857 { rts_sinl, 1, "sinl", 1, _C99 },
858 { rts_cosl, 1, "cosl", 1, _C99 },
859 { rts_tanl, 1, "tanl", 1, _C99 },
860 { rts_asinl, 1, "asinl", 1, _C99 },
861 { rts_acosl, 1, "acosl", 1, _C99 },
862 { rts_atanl, 1, "atanl", 1, _C99 },
863 { rts_sinhl, 1, "sinhl", 1, _C99 },
864 { rts_coshl, 1, "coshl", 1, _C99 },
865 { rts_tanhl, 1, "tanhl", 1, _C99 },
867 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
868 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
869 { rts_strcmp, 1, "strcmp", 2, _C89 },
870 { rts_strncmp, 1, "strncmp", 3, _C89 }
873 static ir_entity* get_function_entity(declaration_t *declaration)
875 if(declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
876 return declaration->v.entity;
877 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
879 symbol_t *symbol = declaration->symbol;
880 ident *id = new_id_from_str(symbol->string);
882 ir_type *global_type = get_glob_type();
883 ir_type *ir_type_method = get_ir_type(declaration->type);
884 assert(is_Method_type(ir_type_method));
886 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
887 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
888 set_entity_ld_ident(entity, id);
889 if(declaration->storage_class == STORAGE_CLASS_STATIC
890 || declaration->is_inline) {
891 set_entity_visibility(entity, visibility_local);
892 } else if(declaration->init.statement != NULL) {
893 set_entity_visibility(entity, visibility_external_visible);
895 set_entity_visibility(entity, visibility_external_allocated);
897 /* We should check for file scope here, but as long as we compile C only
898 this is not needed. */
899 int n_params = get_method_n_params(ir_type_method);
900 int n_res = get_method_n_ress(ir_type_method);
903 if (n_params == 0 && n_res == 0 && id == predef_idents[rts_abort]) {
904 /* found abort(), store for later */
908 if (! firm_opt.freestanding) {
909 /* check for a known runtime function */
910 for (i = 0; i < rts_max; ++i) {
911 /* ignore those rts functions not necessary needed for current mode */
912 if ((c_mode & rts_data[i].flags) == 0)
914 if (n_params == rts_data[i].n_params && n_res == rts_data[i].n_res &&
915 id == predef_idents[rts_data[i].id])
916 rts_entities[rts_data[i].id] = entity;
921 set_entity_allocation(entity, allocation_static);
923 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
924 declaration->v.entity = entity;
929 static ir_node *const_to_firm(const const_expression_t *cnst)
931 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
932 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
937 if(mode_is_float(mode)) {
938 tv = new_tarval_from_double(cnst->v.float_value, mode);
940 if(mode_is_signed(mode)) {
941 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
943 len = snprintf(buf, sizeof(buf), "%llu",
944 (unsigned long long) cnst->v.int_value);
946 tv = new_tarval_from_str(buf, len, mode);
949 return new_d_Const(dbgi, mode, tv);
952 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
955 assert(entity != NULL);
956 union symconst_symbol sym;
957 sym.entity_p = entity;
958 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
961 static ir_node *string_to_firm(const source_position_t *const src_pos,
962 const char *const id_prefix,
963 const string_t *const value)
965 ir_type *const global_type = get_glob_type();
966 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
969 ident *const id = unique_ident(id_prefix);
970 dbg_info *const dbgi = get_dbg_info(src_pos);
971 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
972 set_entity_ld_ident(entity, id);
973 set_entity_variability(entity, variability_constant);
974 set_entity_allocation(entity, allocation_static);
976 ir_type *const elem_type = ir_type_const_char;
977 ir_mode *const mode = get_type_mode(elem_type);
979 const char* const string = value->begin;
980 const size_t slen = value->size;
982 set_array_lower_bound_int(type, 0, 0);
983 set_array_upper_bound_int(type, 0, slen);
984 set_type_size_bytes(type, slen);
985 set_type_state(type, layout_fixed);
987 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
988 for(size_t i = 0; i < slen; ++i) {
989 tvs[i] = new_tarval_from_long(string[i], mode);
992 set_array_entity_values(entity, tvs, slen);
995 return create_symconst(dbgi, mode_P_data, entity);
998 static ir_node *string_literal_to_firm(
999 const string_literal_expression_t* literal)
1001 return string_to_firm(&literal->expression.source_position, "Lstr",
1005 static ir_node *wide_string_literal_to_firm(
1006 const wide_string_literal_expression_t* const literal)
1008 ir_type *const global_type = get_glob_type();
1009 ir_type *const elem_type = ir_type_wchar_t;
1010 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
1013 ident *const id = unique_ident("Lstr");
1014 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
1015 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1016 set_entity_ld_ident(entity, id);
1017 set_entity_variability(entity, variability_constant);
1018 set_entity_allocation(entity, allocation_static);
1020 ir_mode *const mode = get_type_mode(elem_type);
1022 const wchar_rep_t *const string = literal->value.begin;
1023 const size_t slen = literal->value.size;
1025 set_array_lower_bound_int(type, 0, 0);
1026 set_array_upper_bound_int(type, 0, slen);
1027 set_type_size_bytes(type, slen);
1028 set_type_state(type, layout_fixed);
1030 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1031 for(size_t i = 0; i < slen; ++i) {
1032 tvs[i] = new_tarval_from_long(string[i], mode);
1035 set_array_entity_values(entity, tvs, slen);
1038 return create_symconst(dbgi, mode_P_data, entity);
1041 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
1042 dbg_info *const dbgi)
1044 if(is_compound_type(irtype) || is_Array_type(irtype)) {
1048 ir_mode *const mode = get_type_mode(irtype);
1049 ir_node *const memory = get_store();
1050 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1051 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1052 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1053 set_store(load_mem);
1057 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1059 ir_mode *mode = get_irn_mode(node);
1061 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1063 if(!mode_is_float(mode))
1066 /* check if there is already a Conv */
1067 if (get_irn_op(node) == op_Conv) {
1068 /* convert it into a strict Conv */
1069 set_Conv_strict(node, 1);
1073 /* otherwise create a new one */
1074 return new_d_strictConv(dbgi, node, mode);
1077 static ir_node *get_global_var_address(dbg_info *const dbgi,
1078 const declaration_t *const decl)
1080 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1082 ir_entity *const entity = decl->v.entity;
1083 switch ((storage_class_tag_t)decl->storage_class) {
1084 case STORAGE_CLASS_THREAD:
1085 case STORAGE_CLASS_THREAD_EXTERN:
1086 case STORAGE_CLASS_THREAD_STATIC: {
1087 ir_node *const no_mem = new_NoMem();
1088 ir_node *const tls = get_irg_tls(current_ir_graph);
1089 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1093 return create_symconst(dbgi, mode_P_data, entity);
1097 /* Returns the correct base address depending on whether it is a parameter or a
1098 * normal local variable */
1099 static ir_node *get_local_frame(ir_entity *const ent)
1101 ir_graph *const irg = current_ir_graph;
1102 const ir_type *const owner = get_entity_owner(ent);
1103 if (owner == get_irg_frame_type(irg)) {
1104 return get_irg_frame(irg);
1106 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1107 return get_irg_value_param_base(irg);
1111 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1113 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
1114 declaration_t *declaration = ref->declaration;
1115 type_t *type = skip_typeref(declaration->type);
1117 switch((declaration_kind_t) declaration->declaration_kind) {
1118 case DECLARATION_KIND_UNKNOWN:
1119 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
1125 case DECLARATION_KIND_ENUM_ENTRY: {
1126 ir_mode *const mode = get_ir_mode(type);
1127 return new_Const(mode, declaration->v.enum_val);
1130 case DECLARATION_KIND_LOCAL_VARIABLE: {
1131 ir_mode *const mode = get_ir_mode(type);
1132 return get_value(declaration->v.value_number, mode);
1134 case DECLARATION_KIND_FUNCTION: {
1135 ir_mode *const mode = get_ir_mode(type);
1136 return create_symconst(dbgi, mode, declaration->v.entity);
1138 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1139 ir_node *const addr = get_global_var_address(dbgi, declaration);
1140 ir_type *const irtype = get_entity_type(declaration->v.entity);
1141 return deref_address(irtype, addr, dbgi);
1144 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1145 ir_entity *entity = declaration->v.entity;
1146 ir_node *frame = get_local_frame(entity);
1147 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1148 ir_type *irtype = get_entity_type(entity);
1149 return deref_address(irtype, sel, dbgi);
1152 case DECLARATION_KIND_COMPOUND_MEMBER:
1153 case DECLARATION_KIND_LABEL_BLOCK:
1154 panic("not implemented reference type");
1157 panic("reference to declaration with unknown type found");
1160 static ir_node *reference_addr(const reference_expression_t *ref)
1162 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
1163 declaration_t *declaration = ref->declaration;
1165 switch((declaration_kind_t) declaration->declaration_kind) {
1166 case DECLARATION_KIND_UNKNOWN:
1168 case DECLARATION_KIND_LOCAL_VARIABLE:
1169 panic("local variable without entity has no address");
1170 case DECLARATION_KIND_FUNCTION: {
1171 type_t *const type = skip_typeref(ref->expression.datatype);
1172 ir_mode *const mode = get_ir_mode(type);
1173 return create_symconst(dbgi, mode, declaration->v.entity);
1175 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1176 ir_node *const addr = get_global_var_address(dbgi, declaration);
1179 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1180 ir_entity *entity = declaration->v.entity;
1181 ir_node *frame = get_local_frame(entity);
1182 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1187 case DECLARATION_KIND_ENUM_ENTRY:
1188 panic("trying to reference enum entry");
1190 case DECLARATION_KIND_COMPOUND_MEMBER:
1191 case DECLARATION_KIND_LABEL_BLOCK:
1192 panic("not implemented reference type");
1195 panic("reference to declaration with unknown type found");
1198 static ir_node *process_builtin_call(const call_expression_t *call)
1200 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1202 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1203 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1205 type_t *type = skip_typeref(builtin->expression.datatype);
1206 assert(is_type_pointer(type));
1208 type_t *function_type = skip_typeref(type->pointer.points_to);
1209 symbol_t *symbol = builtin->symbol;
1211 switch(symbol->ID) {
1212 case T___builtin_alloca: {
1213 if(call->arguments == NULL || call->arguments->next != NULL) {
1214 panic("invalid number of parameters on __builtin_alloca");
1216 expression_t *argument = call->arguments->expression;
1217 ir_node *size = expression_to_firm(argument);
1219 ir_node *store = get_store();
1220 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1222 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1224 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1228 case T___builtin_nan:
1229 case T___builtin_nanf:
1230 case T___builtin_nand: {
1231 /* Ignore string for now... */
1232 assert(is_type_function(function_type));
1233 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1234 tarval *tv = get_mode_NAN(mode);
1235 ir_node *res = new_d_Const(dbgi, mode, tv);
1238 case T___builtin_va_end:
1241 panic("Unsupported builtin found\n");
1245 static ir_node *call_expression_to_firm(const call_expression_t *call)
1247 assert(get_cur_block() != NULL);
1249 expression_t *function = call->function;
1250 if(function->kind == EXPR_BUILTIN_SYMBOL) {
1251 return process_builtin_call(call);
1253 ir_node *callee = expression_to_firm(function);
1255 type_t *type = skip_typeref(function->base.datatype);
1256 assert(is_type_pointer(type));
1257 pointer_type_t *pointer_type = &type->pointer;
1258 type_t *points_to = skip_typeref(pointer_type->points_to);
1259 assert(is_type_function(points_to));
1260 function_type_t *function_type = &points_to->function;
1262 int n_parameters = 0;
1263 call_argument_t *argument = call->arguments;
1264 for( ; argument != NULL; argument = argument->next) {
1268 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1270 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1271 ir_type *new_method_type = NULL;
1272 if(function_type->variadic || function_type->unspecified_parameters) {
1273 /* we need to construct a new method type matching the call
1275 int n_res = get_method_n_ress(ir_method_type);
1276 new_method_type = new_type_method(unique_ident("calltype"),
1277 n_parameters, n_res);
1278 set_method_calling_convention(new_method_type,
1279 get_method_calling_convention(ir_method_type));
1280 set_method_additional_properties(new_method_type,
1281 get_method_additional_properties(ir_method_type));
1283 for(int i = 0; i < n_res; ++i) {
1284 set_method_res_type(new_method_type, i,
1285 get_method_res_type(ir_method_type, i));
1288 ir_node *in[n_parameters];
1290 argument = call->arguments;
1292 for( ; argument != NULL; argument = argument->next) {
1293 expression_t *expression = argument->expression;
1294 ir_node *arg_node = expression_to_firm(expression);
1296 arg_node = do_strict_conv(dbgi, arg_node);
1299 if(new_method_type != NULL) {
1300 ir_type *irtype = get_ir_type(expression->base.datatype);
1301 set_method_param_type(new_method_type, n, irtype);
1306 assert(n == n_parameters);
1308 if(new_method_type != NULL)
1309 ir_method_type = new_method_type;
1311 ir_node *store = get_store();
1312 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1314 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1317 type_t *return_type = skip_typeref(function_type->return_type);
1318 ir_node *result = NULL;
1320 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1322 if(is_type_scalar(return_type)) {
1323 mode = get_ir_mode(return_type);
1327 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1328 result = new_d_Proj(dbgi, resproj, mode, 0);
1334 static void statement_to_firm(statement_t *statement);
1335 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1337 static ir_node *expression_to_addr(const expression_t *expression);
1338 static void create_condition_evaluation(const expression_t *expression,
1339 ir_node *true_block,
1340 ir_node *false_block);
1342 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1345 value = do_strict_conv(dbgi, value);
1347 ir_node *memory = get_store();
1349 if(is_type_scalar(type)) {
1350 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1351 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1352 set_store(store_mem);
1354 ir_type *irtype = get_ir_type(type);
1355 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1356 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1357 set_store(copyb_mem);
1361 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1363 tarval *all_one = get_mode_all_one(mode);
1364 int mode_size = get_mode_size_bits(mode);
1366 assert(offset >= 0 && size >= 0);
1367 assert(offset + size <= mode_size);
1368 if(size == mode_size) {
1372 long shiftr = get_mode_size_bits(mode) - size;
1373 long shiftl = offset;
1374 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1375 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1376 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1377 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1382 static void bitfield_store_to_firm(const unary_expression_t *expression,
1385 expression_t *select = expression->value;
1386 assert(select->kind == EXPR_SELECT);
1387 type_t *type = select->base.datatype;
1388 assert(type->kind == TYPE_BITFIELD);
1389 ir_mode *mode = get_ir_mode(type->bitfield.base);
1390 ir_node *addr = expression_to_addr(select);
1392 assert(get_irn_mode(value) == mode);
1394 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1396 /* kill upper bits of value and shift to right position */
1397 ir_entity *entity = select->select.compound_entry->v.entity;
1398 int bitoffset = get_entity_offset_bits_remainder(entity);
1399 ir_type *entity_type = get_entity_type(entity);
1400 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1402 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1403 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1404 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1405 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1406 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1407 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1409 /* load current value */
1410 ir_node *mem = get_store();
1411 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1412 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1413 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1414 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1415 tarval *inv_mask = tarval_not(shift_mask);
1416 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1417 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1419 /* construct new value and store */
1420 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1421 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1422 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1423 set_store(store_mem);
1426 static void set_value_for_expression(const expression_t *expression,
1429 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1430 value = do_strict_conv(dbgi, value);
1432 if(expression->kind == EXPR_REFERENCE) {
1433 const reference_expression_t *ref = &expression->reference;
1435 declaration_t *declaration = ref->declaration;
1436 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1437 if(declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1438 set_value(declaration->v.value_number, value);
1443 if(expression->kind == EXPR_UNARY_BITFIELD_EXTRACT) {
1444 bitfield_store_to_firm(&expression->unary, value);
1448 ir_node *addr = expression_to_addr(expression);
1449 type_t *type = skip_typeref(expression->base.datatype);
1450 assign_value(dbgi, addr, type, value);
1453 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1455 ir_mode *value_mode = get_irn_mode(value);
1457 if (value_mode == dest_mode || is_Bad(value))
1460 if(dest_mode == mode_b) {
1461 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1462 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1463 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1467 return new_d_Conv(dbgi, value, dest_mode);
1470 static ir_node *create_incdec(const unary_expression_t *expression)
1472 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1473 type_t *type = skip_typeref(expression->expression.datatype);
1474 ir_mode *mode = get_ir_mode(type);
1475 expression_t *value = expression->value;
1477 ir_node *value_node = expression_to_firm(value);
1480 if(is_type_pointer(type)) {
1481 pointer_type_t *pointer_type = &type->pointer;
1482 unsigned elem_size = get_type_size(pointer_type->points_to);
1483 offset = new_Const_long(mode_int, elem_size);
1485 assert(is_type_arithmetic(type));
1486 offset = new_Const(mode, get_mode_one(mode));
1489 switch(expression->expression.kind) {
1490 case EXPR_UNARY_POSTFIX_INCREMENT: {
1491 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1492 set_value_for_expression(value, new_value);
1495 case EXPR_UNARY_POSTFIX_DECREMENT: {
1496 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1497 set_value_for_expression(value, new_value);
1500 case EXPR_UNARY_PREFIX_INCREMENT: {
1501 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1502 set_value_for_expression(value, new_value);
1505 case EXPR_UNARY_PREFIX_DECREMENT: {
1506 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1507 set_value_for_expression(value, new_value);
1511 panic("no incdec expr in create_incdec");
1516 static bool is_local_variable(expression_t *expression)
1518 if (expression->kind != EXPR_REFERENCE)
1520 reference_expression_t *ref_expr = &expression->reference;
1521 declaration_t *declaration = ref_expr->declaration;
1522 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1525 static pn_Cmp get_pnc(const expression_kind_t kind)
1528 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1529 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1530 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Ne;
1531 case EXPR_BINARY_ISLESS:
1532 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1533 case EXPR_BINARY_ISLESSEQUAL:
1534 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1535 case EXPR_BINARY_ISGREATER:
1536 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1537 case EXPR_BINARY_ISGREATEREQUAL:
1538 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1539 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1544 panic("trying to get pn_Cmp from non-comparison binexpr type");
1548 * Handle the assume optimizer hint: check if a Confirm
1549 * node can be created.
1551 * @param dbi debug info
1552 * @param expr the IL assume expression
1554 * we support here only some simple cases:
1559 static ir_node *handle_assume_compare(dbg_info *dbi,
1560 const binary_expression_t *expression)
1562 expression_t *op1 = expression->left;
1563 expression_t *op2 = expression->right;
1564 declaration_t *var2, *var = NULL;
1565 ir_node *res = NULL;
1568 cmp_val = get_pnc(expression->expression.kind);
1570 if (is_local_variable(op1) && is_local_variable(op2)) {
1571 var = op1->reference.declaration;
1572 var2 = op2->reference.declaration;
1574 type_t *const type = skip_typeref(var->type);
1575 ir_mode *const mode = get_ir_mode(type);
1577 ir_node *const irn1 = get_value(var->v.value_number, mode);
1578 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1580 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1581 set_value(var2->v.value_number, res);
1583 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1584 set_value(var->v.value_number, res);
1590 if (is_local_variable(op1) && is_constant_expression(op2)) {
1591 var = op1->reference.declaration;
1593 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1594 cmp_val = get_inversed_pnc(cmp_val);
1595 var = op2->reference.declaration;
1600 type_t *const type = skip_typeref(var->type);
1601 ir_mode *const mode = get_ir_mode(type);
1603 res = get_value(var->v.value_number, mode);
1604 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1605 set_value(var->v.value_number, res);
1611 * Handle the assume optimizer hint.
1613 * @param dbi debug info
1614 * @param expr the IL assume expression
1616 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1617 switch(expression->kind) {
1618 case EXPR_BINARY_EQUAL:
1619 case EXPR_BINARY_NOTEQUAL:
1620 case EXPR_BINARY_LESS:
1621 case EXPR_BINARY_LESSEQUAL:
1622 case EXPR_BINARY_GREATER:
1623 case EXPR_BINARY_GREATEREQUAL:
1624 return handle_assume_compare(dbi, &expression->binary);
1630 static ir_node *bitfield_extract_to_firm(const unary_expression_t *expression)
1632 expression_t *select = expression->value;
1633 assert(select->kind == EXPR_SELECT);
1635 type_t *type = select->base.datatype;
1636 assert(type->kind == TYPE_BITFIELD);
1637 ir_mode *mode = get_ir_mode(type->bitfield.base);
1638 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1639 ir_node *addr = expression_to_addr(select);
1640 ir_node *mem = get_store();
1641 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1642 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1643 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1645 load_res = create_conv(dbgi, load_res, mode_int);
1647 set_store(load_mem);
1649 /* kill upper bits */
1650 ir_entity *entity = select->select.compound_entry->v.entity;
1651 int bitoffset = get_entity_offset_bits_remainder(entity);
1652 ir_type *entity_type = get_entity_type(entity);
1653 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1654 long shift_bitsl = machine_size - bitoffset - bitsize;
1655 assert(shift_bitsl >= 0);
1656 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1657 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1658 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1660 long shift_bitsr = bitoffset + shift_bitsl;
1661 assert(shift_bitsr <= (long) machine_size);
1662 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1663 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1665 if(mode_is_signed(mode)) {
1666 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1668 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1671 return create_conv(dbgi, shiftr, mode);
1674 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1676 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1677 type_t *type = skip_typeref(expression->expression.datatype);
1679 if(expression->expression.kind == EXPR_UNARY_TAKE_ADDRESS)
1680 return expression_to_addr(expression->value);
1682 const expression_t *value = expression->value;
1684 switch(expression->expression.kind) {
1685 case EXPR_UNARY_NEGATE: {
1686 ir_node *value_node = expression_to_firm(value);
1687 ir_mode *mode = get_ir_mode(type);
1688 return new_d_Minus(dbgi, value_node, mode);
1690 case EXPR_UNARY_PLUS:
1691 return expression_to_firm(value);
1692 case EXPR_UNARY_BITWISE_NEGATE: {
1693 ir_node *value_node = expression_to_firm(value);
1694 ir_mode *mode = get_ir_mode(type);
1695 return new_d_Not(dbgi, value_node, mode);
1697 case EXPR_UNARY_NOT: {
1698 ir_node *value_node = expression_to_firm(value);
1699 ir_mode *mode = get_ir_mode(type);
1700 if(get_irn_mode(value_node) != mode_b) {
1701 value_node = create_conv(dbgi, value_node, mode_b);
1703 value_node = new_d_Not(dbgi, value_node, mode_b);
1704 if(mode != mode_b) {
1705 value_node = create_conv(dbgi, value_node, mode);
1709 case EXPR_UNARY_DEREFERENCE: {
1710 ir_node *value_node = expression_to_firm(value);
1711 type_t *value_type = skip_typeref(value->base.datatype);
1712 ir_type *irtype = get_ir_type(value_type);
1713 assert(is_Pointer_type(irtype));
1714 ir_type *points_to = get_pointer_points_to_type(irtype);
1715 return deref_address(points_to, value_node, dbgi);
1717 case EXPR_UNARY_POSTFIX_INCREMENT:
1718 case EXPR_UNARY_POSTFIX_DECREMENT:
1719 case EXPR_UNARY_PREFIX_INCREMENT:
1720 case EXPR_UNARY_PREFIX_DECREMENT:
1721 return create_incdec(expression);
1722 case EXPR_UNARY_CAST: {
1723 ir_node *value_node = expression_to_firm(value);
1724 ir_mode *mode = get_ir_mode(type);
1725 ir_node *node = create_conv(dbgi, value_node, mode);
1726 node = do_strict_conv(dbgi, node);
1729 case EXPR_UNARY_CAST_IMPLICIT: {
1730 ir_node *value_node = expression_to_firm(value);
1731 ir_mode *mode = get_ir_mode(type);
1732 return create_conv(dbgi, value_node, mode);
1734 case EXPR_UNARY_ASSUME:
1735 if(firm_opt.confirm)
1736 return handle_assume(dbgi, value);
1739 case EXPR_UNARY_BITFIELD_EXTRACT:
1740 return bitfield_extract_to_firm(expression);
1745 panic("invalid UNEXPR type found");
1748 static ir_node *create_lazy_op(const binary_expression_t *expression)
1750 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1751 type_t *type = expression->expression.datatype;
1752 ir_mode *mode = get_ir_mode(type);
1754 if(is_constant_expression(expression->left)) {
1755 long val = fold_constant(expression->left);
1756 expression_kind_t ekind = expression->expression.kind;
1757 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
1758 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1759 return expression_to_firm(expression->right);
1761 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
1762 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
1763 return new_Const(mode, get_mode_one(mode));
1767 ir_node *cur_block = get_cur_block();
1769 ir_node *one_block = new_immBlock();
1770 ir_node *one = new_Const(mode, get_mode_one(mode));
1771 ir_node *jmp_one = new_d_Jmp(dbgi);
1773 ir_node *zero_block = new_immBlock();
1774 ir_node *zero = new_Const(mode, get_mode_null(mode));
1775 ir_node *jmp_zero = new_d_Jmp(dbgi);
1777 set_cur_block(cur_block);
1778 create_condition_evaluation((const expression_t*) expression,
1779 one_block, zero_block);
1780 mature_immBlock(one_block);
1781 mature_immBlock(zero_block);
1783 ir_node *common_block = new_immBlock();
1784 add_immBlock_pred(common_block, jmp_one);
1785 add_immBlock_pred(common_block, jmp_zero);
1786 mature_immBlock(common_block);
1788 ir_node *in[2] = { one, zero };
1789 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1794 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1795 ir_node *right, ir_mode *mode);
1797 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1798 create_arithmetic_func func)
1800 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1801 ir_node *left = expression_to_firm(expression->left);
1802 ir_node *right = expression_to_firm(expression->right);
1803 type_t *type = expression->right->base.datatype;
1804 /* be careful with the modes, because in arithmetic assign nodes only
1805 * the right operand has the mode of the arithmetic already */
1806 ir_mode *mode = get_ir_mode(type);
1807 left = create_conv(dbgi, left, mode);
1808 ir_node *res = func(dbgi, left, right, mode);
1813 static ir_node *pointer_arithmetic(ir_node *const pointer,
1816 dbg_info *const dbgi,
1817 const create_arithmetic_func func)
1819 pointer_type_t *const pointer_type = &type->pointer;
1820 type_t *const points_to = pointer_type->points_to;
1821 const unsigned elem_size = get_type_size(points_to);
1823 assert(elem_size >= 1);
1824 if (elem_size > 1) {
1825 integer = create_conv(dbgi, integer, mode_int);
1826 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1827 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1831 ir_mode *const mode = get_ir_mode(type);
1832 return func(dbgi, pointer, integer, mode);
1835 static ir_node *create_arithmetic_assign_binop(
1836 const binary_expression_t *expression, create_arithmetic_func func)
1838 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1839 type_t *const type = skip_typeref(expression->expression.datatype);
1842 if (is_type_pointer(type)) {
1843 ir_node *const pointer = expression_to_firm(expression->left);
1844 ir_node * integer = expression_to_firm(expression->right);
1845 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1847 value = create_arithmetic_binop(expression, func);
1850 ir_mode *const mode = get_ir_mode(type);
1851 value = create_conv(dbgi, value, mode);
1852 set_value_for_expression(expression->left, value);
1857 static ir_node *create_add(const binary_expression_t *expression)
1859 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1860 ir_node *left = expression_to_firm(expression->left);
1861 ir_node *right = expression_to_firm(expression->right);
1862 type_t *type = expression->expression.datatype;
1864 expression_t *expr_left = expression->left;
1865 expression_t *expr_right = expression->right;
1866 type_t *type_left = skip_typeref(expr_left->base.datatype);
1867 type_t *type_right = skip_typeref(expr_right->base.datatype);
1869 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1870 ir_mode *const mode = get_ir_mode(type);
1871 return new_d_Add(dbgi, left, right, mode);
1874 if (is_type_pointer(type_left)) {
1875 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1877 assert(is_type_pointer(type_right));
1878 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1882 static ir_node *create_sub(const binary_expression_t *expression)
1884 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1885 expression_t *const expr_left = expression->left;
1886 expression_t *const expr_right = expression->right;
1887 ir_node *const left = expression_to_firm(expr_left);
1888 ir_node *const right = expression_to_firm(expr_right);
1889 type_t *const type = expression->expression.datatype;
1890 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1891 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1893 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1894 ir_mode *const mode = get_ir_mode(type);
1895 return new_d_Sub(dbgi, left, right, mode);
1896 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1897 const pointer_type_t *const ptr_type = &type_left->pointer;
1898 const unsigned elem_size = get_type_size(ptr_type->points_to);
1899 ir_mode *const mode = get_ir_mode(type);
1900 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1901 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1902 ir_node *const no_mem = new_NoMem();
1903 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1904 op_pin_state_floats);
1905 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1908 assert(is_type_pointer(type_left));
1909 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1912 static ir_node *create_shift(const binary_expression_t *expression)
1914 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1915 ir_node *left = expression_to_firm(expression->left);
1916 ir_node *right = expression_to_firm(expression->right);
1917 type_t *type = expression->expression.datatype;
1918 ir_mode *mode = get_ir_mode(type);
1920 /* firm always wants the shift count to be unsigned */
1921 right = create_conv(dbgi, right, mode_uint);
1925 switch(expression->expression.kind) {
1926 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1927 case EXPR_BINARY_SHIFTLEFT:
1928 res = new_d_Shl(dbgi, left, right, mode);
1930 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1931 case EXPR_BINARY_SHIFTRIGHT: {
1932 expression_t *expr_left = expression->left;
1933 type_t *type_left = skip_typeref(expr_left->base.datatype);
1935 if(is_type_signed(type_left)) {
1936 res = new_d_Shrs(dbgi, left, right, mode);
1938 res = new_d_Shr(dbgi, left, right, mode);
1943 panic("create shift op called for non-shift op");
1950 static ir_node *create_divmod(const binary_expression_t *expression)
1952 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1953 ir_node *left = expression_to_firm(expression->left);
1954 ir_node *right = expression_to_firm(expression->right);
1955 ir_node *pin = new_Pin(new_NoMem());
1956 /* be careful with the modes, because in arithmetic assign nodes only
1957 * the right operand has the mode of the arithmetic already */
1958 type_t *type = expression->right->base.datatype;
1959 ir_mode *mode = get_ir_mode(type);
1960 left = create_conv(dbgi, left, mode);
1964 switch (expression->expression.kind) {
1965 case EXPR_BINARY_DIV:
1966 case EXPR_BINARY_DIV_ASSIGN:
1967 if(mode_is_float(mode)) {
1968 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1969 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1971 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1972 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1976 case EXPR_BINARY_MOD:
1977 case EXPR_BINARY_MOD_ASSIGN:
1978 assert(!mode_is_float(mode));
1979 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1980 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1983 default: panic("unexpected binary expression type in create_divmod()");
1989 static ir_node *create_arithmetic_assign_divmod(
1990 const binary_expression_t *expression)
1992 ir_node * value = create_divmod(expression);
1993 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1994 type_t *const type = expression->expression.datatype;
1995 ir_mode *const mode = get_ir_mode(type);
1997 assert(type->kind != TYPE_POINTER);
1999 value = create_conv(dbgi, value, mode);
2000 set_value_for_expression(expression->left, value);
2005 static ir_node *create_arithmetic_assign_shift(
2006 const binary_expression_t *expression)
2008 ir_node * value = create_shift(expression);
2009 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
2010 type_t *const type = expression->expression.datatype;
2011 ir_mode *const mode = get_ir_mode(type);
2013 value = create_conv(dbgi, value, mode);
2014 set_value_for_expression(expression->left, value);
2019 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2021 expression_kind_t kind = expression->expression.kind;
2024 case EXPR_BINARY_EQUAL:
2025 case EXPR_BINARY_NOTEQUAL:
2026 case EXPR_BINARY_LESS:
2027 case EXPR_BINARY_LESSEQUAL:
2028 case EXPR_BINARY_GREATER:
2029 case EXPR_BINARY_GREATEREQUAL:
2030 case EXPR_BINARY_ISGREATER:
2031 case EXPR_BINARY_ISGREATEREQUAL:
2032 case EXPR_BINARY_ISLESS:
2033 case EXPR_BINARY_ISLESSEQUAL:
2034 case EXPR_BINARY_ISLESSGREATER:
2035 case EXPR_BINARY_ISUNORDERED: {
2036 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2037 ir_node *left = expression_to_firm(expression->left);
2038 ir_node *right = expression_to_firm(expression->right);
2039 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2040 long pnc = get_pnc(kind);
2041 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2044 case EXPR_BINARY_ASSIGN: {
2045 ir_node *right = expression_to_firm(expression->right);
2046 set_value_for_expression(expression->left, right);
2050 case EXPR_BINARY_ADD:
2051 return create_add(expression);
2052 case EXPR_BINARY_SUB:
2053 return create_sub(expression);
2054 case EXPR_BINARY_MUL:
2055 return create_arithmetic_binop(expression, new_d_Mul);
2056 case EXPR_BINARY_BITWISE_AND:
2057 return create_arithmetic_binop(expression, new_d_And);
2058 case EXPR_BINARY_BITWISE_OR:
2059 return create_arithmetic_binop(expression, new_d_Or);
2060 case EXPR_BINARY_BITWISE_XOR:
2061 return create_arithmetic_binop(expression, new_d_Eor);
2062 case EXPR_BINARY_SHIFTLEFT:
2063 case EXPR_BINARY_SHIFTRIGHT:
2064 return create_shift(expression);
2065 case EXPR_BINARY_DIV:
2066 case EXPR_BINARY_MOD:
2067 return create_divmod(expression);
2068 case EXPR_BINARY_LOGICAL_AND:
2069 case EXPR_BINARY_LOGICAL_OR:
2070 return create_lazy_op(expression);
2071 case EXPR_BINARY_COMMA:
2072 expression_to_firm(expression->left);
2073 return expression_to_firm(expression->right);
2074 case EXPR_BINARY_ADD_ASSIGN:
2075 return create_arithmetic_assign_binop(expression, new_d_Add);
2076 case EXPR_BINARY_SUB_ASSIGN:
2077 return create_arithmetic_assign_binop(expression, new_d_Sub);
2078 case EXPR_BINARY_MUL_ASSIGN:
2079 return create_arithmetic_assign_binop(expression, new_d_Mul);
2080 case EXPR_BINARY_DIV_ASSIGN:
2081 return create_arithmetic_assign_divmod(expression);
2082 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2083 return create_arithmetic_assign_binop(expression, new_d_And);
2084 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2085 return create_arithmetic_assign_binop(expression, new_d_Or);
2086 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2087 return create_arithmetic_assign_binop(expression, new_d_Eor);
2088 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2089 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2090 return create_arithmetic_assign_shift(expression);
2091 case EXPR_BINARY_BUILTIN_EXPECT:
2092 return expression_to_firm(expression->left);
2094 panic("TODO binexpr type");
2098 static ir_node *array_access_addr(const array_access_expression_t *expression)
2100 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2101 ir_node *base_addr = expression_to_firm(expression->array_ref);
2102 ir_node *offset = expression_to_firm(expression->index);
2103 offset = create_conv(dbgi, offset, mode_uint);
2105 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
2106 assert(is_type_pointer(ref_type));
2107 pointer_type_t *pointer_type = &ref_type->pointer;
2109 unsigned elem_size = get_type_size(pointer_type->points_to);
2110 ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
2111 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2113 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2118 static ir_node *array_access_to_firm(
2119 const array_access_expression_t *expression)
2121 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2122 ir_node *addr = array_access_addr(expression);
2123 type_t *type = revert_automatic_type_conversion(
2124 (const expression_t*) expression);
2125 type = skip_typeref(type);
2126 ir_type *irtype = get_ir_type(type);
2128 return deref_address(irtype, addr, dbgi);
2131 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
2133 type_t *type = expression->type;
2135 type = expression->size_expression->base.datatype;
2136 assert(type != NULL);
2139 ir_mode *const mode = get_ir_mode(expression->expression.datatype);
2140 symconst_symbol sym;
2141 sym.type_p = get_ir_type(type);
2142 return new_SymConst(mode, sym, symconst_type_size);
2145 static ir_node *alignof_to_firm(const alignof_expression_t *expression)
2147 type_t *const type = expression->type;
2148 ir_mode *const mode = get_ir_mode(expression->expression.datatype);
2149 symconst_symbol sym;
2150 sym.type_p = get_ir_type(type);
2151 return new_SymConst(mode, sym, symconst_type_align);
2154 static long fold_constant(const expression_t *expression)
2156 assert(is_constant_expression(expression));
2158 ir_graph *old_current_ir_graph = current_ir_graph;
2159 if(current_ir_graph == NULL) {
2160 current_ir_graph = get_const_code_irg();
2163 ir_node *cnst = expression_to_firm(expression);
2164 current_ir_graph = old_current_ir_graph;
2166 if(!is_Const(cnst)) {
2167 panic("couldn't fold constant\n");
2170 tarval *tv = get_Const_tarval(cnst);
2171 if(!tarval_is_long(tv)) {
2172 panic("result of constant folding is not integer\n");
2175 return get_tarval_long(tv);
2178 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2180 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
2182 /* first try to fold a constant condition */
2183 if(is_constant_expression(expression->condition)) {
2184 long val = fold_constant(expression->condition);
2186 return expression_to_firm(expression->true_expression);
2188 return expression_to_firm(expression->false_expression);
2192 ir_node *cur_block = get_cur_block();
2194 /* create the true block */
2195 ir_node *true_block = new_immBlock();
2197 ir_node *true_val = expression_to_firm(expression->true_expression);
2198 ir_node *true_jmp = new_Jmp();
2200 /* create the false block */
2201 ir_node *false_block = new_immBlock();
2203 ir_node *false_val = expression_to_firm(expression->false_expression);
2204 ir_node *false_jmp = new_Jmp();
2206 /* create the condition evaluation */
2207 set_cur_block(cur_block);
2208 create_condition_evaluation(expression->condition, true_block, false_block);
2209 mature_immBlock(true_block);
2210 mature_immBlock(false_block);
2212 /* create the common block */
2213 ir_node *common_block = new_immBlock();
2214 add_immBlock_pred(common_block, true_jmp);
2215 add_immBlock_pred(common_block, false_jmp);
2216 mature_immBlock(common_block);
2218 /* TODO improve static semantics, so either both or no values are NULL */
2219 if (true_val == NULL || false_val == NULL)
2222 ir_node *in[2] = { true_val, false_val };
2223 ir_mode *mode = get_irn_mode(true_val);
2224 assert(get_irn_mode(false_val) == mode);
2225 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2230 static ir_node *select_addr(const select_expression_t *expression)
2232 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2234 ir_node *compound_addr = expression_to_firm(expression->compound);
2236 declaration_t *entry = expression->compound_entry;
2237 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2238 ir_entity *entity = entry->v.entity;
2240 assert(entity != NULL);
2242 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2247 static ir_node *select_to_firm(const select_expression_t *expression)
2249 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2250 ir_node *addr = select_addr(expression);
2251 type_t *type = revert_automatic_type_conversion(
2252 (const expression_t*) expression);
2253 type = skip_typeref(type);
2254 ir_type *irtype = get_ir_type(type);
2256 return deref_address(irtype, addr, dbgi);
2259 /* Values returned by __builtin_classify_type. */
2260 typedef enum gcc_type_class
2266 enumeral_type_class,
2269 reference_type_class,
2273 function_type_class,
2284 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2286 const type_t *const type = expr->type_expression->base.datatype;
2292 const atomic_type_t *const atomic_type = &type->atomic;
2293 switch (atomic_type->akind) {
2294 /* should not be reached */
2295 case ATOMIC_TYPE_INVALID:
2299 /* gcc cannot do that */
2300 case ATOMIC_TYPE_VOID:
2301 tc = void_type_class;
2304 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2305 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2306 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2307 case ATOMIC_TYPE_SHORT:
2308 case ATOMIC_TYPE_USHORT:
2309 case ATOMIC_TYPE_INT:
2310 case ATOMIC_TYPE_UINT:
2311 case ATOMIC_TYPE_LONG:
2312 case ATOMIC_TYPE_ULONG:
2313 case ATOMIC_TYPE_LONGLONG:
2314 case ATOMIC_TYPE_ULONGLONG:
2315 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2316 tc = integer_type_class;
2319 case ATOMIC_TYPE_FLOAT:
2320 case ATOMIC_TYPE_DOUBLE:
2321 case ATOMIC_TYPE_LONG_DOUBLE:
2322 tc = real_type_class;
2325 #ifdef PROVIDE_COMPLEX
2326 case ATOMIC_TYPE_FLOAT_COMPLEX:
2327 case ATOMIC_TYPE_DOUBLE_COMPLEX:
2328 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
2329 tc = complex_type_class;
2331 case ATOMIC_TYPE_FLOAT_IMAGINARY:
2332 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
2333 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
2334 tc = complex_type_class;
2339 panic("Unimplemented case in classify_type_to_firm().");
2344 case TYPE_ARRAY: /* gcc handles this as pointer */
2345 case TYPE_FUNCTION: /* gcc handles this as pointer */
2346 case TYPE_POINTER: tc = pointer_type_class; break;
2347 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
2348 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
2350 /* gcc handles this as integer */
2351 case TYPE_ENUM: tc = integer_type_class; break;
2354 panic("Unimplemented case in classify_type_to_firm().");
2357 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
2358 ir_mode *const mode = mode_int;
2359 tarval *const tv = new_tarval_from_long(tc, mode);
2360 return new_d_Const(dbgi, mode, tv);
2363 static ir_node *function_name_to_firm(
2364 const string_literal_expression_t *const expr)
2366 if (current_function_name == NULL) {
2367 const source_position_t *const src_pos =
2368 &expr->expression.source_position;
2369 const char *const name = current_function_decl->symbol->string;
2370 const string_t string = { name, strlen(name) + 1 };
2371 current_function_name = string_to_firm(src_pos, "__func__", &string);
2374 return current_function_name;
2377 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2379 statement_t *statement = expr->statement;
2381 assert(statement->kind == STATEMENT_COMPOUND);
2382 return compound_statement_to_firm(&statement->compound);
2385 static ir_node *va_start_expression_to_firm(
2386 const va_start_expression_t *const expr)
2388 ir_type *const method_type = get_ir_type(current_function_decl->type);
2389 int const n = get_method_n_params(method_type) - 1;
2390 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2391 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2392 dbg_info *const dbgi =
2393 get_dbg_info(&expr->expression.source_position);
2394 ir_node *const no_mem = new_NoMem();
2395 ir_node *const arg_sel =
2396 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2398 size_t const parm_size = get_type_size(expr->parameter->type);
2399 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2400 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2401 set_value_for_expression(expr->ap, add);
2406 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2408 ir_type *const irtype = get_ir_type(expr->expression.datatype);
2409 ir_node *const ap = expression_to_firm(expr->ap);
2410 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
2411 ir_node *const res = deref_address(irtype, ap, dbgi);
2413 size_t const parm_size = get_type_size(expr->expression.datatype);
2414 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2415 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2416 set_value_for_expression(expr->ap, add);
2421 static ir_node *dereference_addr(const unary_expression_t *const expression)
2423 assert(expression->expression.kind == EXPR_UNARY_DEREFERENCE);
2424 return expression_to_firm(expression->value);
2427 static ir_node *expression_to_addr(const expression_t *expression)
2429 switch(expression->kind) {
2430 case EXPR_REFERENCE:
2431 return reference_addr(&expression->reference);
2432 case EXPR_ARRAY_ACCESS:
2433 return array_access_addr(&expression->array_access);
2435 return select_addr(&expression->select);
2437 return call_expression_to_firm(&expression->call);
2438 case EXPR_UNARY_DEREFERENCE: {
2439 return dereference_addr(&expression->unary);
2444 panic("trying to get address of non-lvalue");
2447 static ir_node *builtin_constant_to_firm(
2448 const builtin_constant_expression_t *expression)
2450 ir_mode *mode = get_ir_mode(expression->expression.datatype);
2453 if (is_constant_expression(expression->value)) {
2458 return new_Const_long(mode, v);
2461 static ir_node *builtin_prefetch_to_firm(
2462 const builtin_prefetch_expression_t *expression)
2464 ir_node *adr = expression_to_firm(expression->adr);
2465 /* no Firm support for prefetch yet */
2470 static ir_node *_expression_to_firm(const expression_t *expression)
2472 switch(expression->kind) {
2474 return const_to_firm(&expression->conste);
2475 case EXPR_STRING_LITERAL:
2476 return string_literal_to_firm(&expression->string);
2477 case EXPR_WIDE_STRING_LITERAL:
2478 return wide_string_literal_to_firm(&expression->wide_string);
2479 case EXPR_REFERENCE:
2480 return reference_expression_to_firm(&expression->reference);
2482 return call_expression_to_firm(&expression->call);
2484 return unary_expression_to_firm(&expression->unary);
2486 return binary_expression_to_firm(&expression->binary);
2487 case EXPR_ARRAY_ACCESS:
2488 return array_access_to_firm(&expression->array_access);
2490 return sizeof_to_firm(&expression->sizeofe);
2492 return alignof_to_firm(&expression->alignofe);
2493 case EXPR_CONDITIONAL:
2494 return conditional_to_firm(&expression->conditional);
2496 return select_to_firm(&expression->select);
2497 case EXPR_CLASSIFY_TYPE:
2498 return classify_type_to_firm(&expression->classify_type);
2500 case EXPR_PRETTY_FUNCTION:
2501 return function_name_to_firm(&expression->string);
2502 case EXPR_STATEMENT:
2503 return statement_expression_to_firm(&expression->statement);
2505 return va_start_expression_to_firm(&expression->va_starte);
2507 return va_arg_expression_to_firm(&expression->va_arge);
2509 case EXPR_BUILTIN_SYMBOL:
2510 panic("unimplemented expression found");
2511 case EXPR_BUILTIN_CONSTANT_P:
2512 return builtin_constant_to_firm(&expression->builtin_constant);
2513 case EXPR_BUILTIN_PREFETCH:
2514 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2520 panic("invalid expression found");
2523 static ir_node *expression_to_firm(const expression_t *expression)
2525 ir_node *res = _expression_to_firm(expression);
2527 if(res != NULL && get_irn_mode(res) == mode_b) {
2528 ir_mode *mode = get_ir_mode(expression->base.datatype);
2529 res = create_conv(NULL, res, mode);
2535 static ir_node *expression_to_modeb(const expression_t *expression)
2537 ir_node *res = _expression_to_firm(expression);
2538 res = create_conv(NULL, res, mode_b);
2544 * create a short-circuit expression evaluation that tries to construct
2545 * efficient control flow structures for &&, || and ! expressions
2547 static void create_condition_evaluation(const expression_t *expression,
2548 ir_node *true_block,
2549 ir_node *false_block)
2551 switch(expression->kind) {
2552 case EXPR_UNARY_NOT: {
2553 const unary_expression_t *unary_expression = &expression->unary;
2554 create_condition_evaluation(unary_expression->value, false_block,
2558 case EXPR_BINARY_LOGICAL_AND: {
2559 const binary_expression_t *binary_expression = &expression->binary;
2561 ir_node *cur_block = get_cur_block();
2562 ir_node *extra_block = new_immBlock();
2563 set_cur_block(cur_block);
2564 create_condition_evaluation(binary_expression->left, extra_block,
2566 mature_immBlock(extra_block);
2567 set_cur_block(extra_block);
2568 create_condition_evaluation(binary_expression->right, true_block,
2572 case EXPR_BINARY_LOGICAL_OR: {
2573 const binary_expression_t *binary_expression = &expression->binary;
2575 ir_node *cur_block = get_cur_block();
2576 ir_node *extra_block = new_immBlock();
2577 set_cur_block(cur_block);
2578 create_condition_evaluation(binary_expression->left, true_block,
2580 mature_immBlock(extra_block);
2581 set_cur_block(extra_block);
2582 create_condition_evaluation(binary_expression->right, true_block,
2590 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2591 ir_node *condition = expression_to_modeb(expression);
2592 ir_node *cond = new_d_Cond(dbgi, condition);
2593 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2594 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2596 /* set branch prediction info based on __builtin_expect */
2597 if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
2598 long cnst = fold_constant(expression->binary.right);
2599 cond_jmp_predicate pred;
2602 pred = COND_JMP_PRED_FALSE;
2604 pred = COND_JMP_PRED_TRUE;
2606 set_Cond_jmp_pred(cond, pred);
2609 add_immBlock_pred(true_block, true_proj);
2610 add_immBlock_pred(false_block, false_proj);
2612 set_cur_block(NULL);
2617 static void create_declaration_entity(declaration_t *declaration,
2618 declaration_kind_t declaration_kind,
2619 ir_type *parent_type)
2621 ident *const id = new_id_from_str(declaration->symbol->string);
2622 ir_type *const irtype = get_ir_type(declaration->type);
2623 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2624 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2625 set_entity_ld_ident(entity, id);
2627 declaration->declaration_kind = (unsigned char) declaration_kind;
2628 declaration->v.entity = entity;
2629 set_entity_variability(entity, variability_uninitialized);
2630 if(parent_type == get_tls_type())
2631 set_entity_allocation(entity, allocation_automatic);
2632 else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
2633 set_entity_allocation(entity, allocation_static);
2634 /* TODO: visibility? */
2637 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2639 enum compound_graph_entry_type_t {
2640 COMPOUND_GRAPH_ENTRY_ARRAY,
2641 COMPOUND_GRAPH_ENTRY_COMPOUND
2644 struct compound_graph_path_entry_t {
2650 compound_graph_path_entry_t *prev;
2653 static void create_initializer_object(initializer_t *initializer, type_t *type,
2654 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2656 static compound_graph_path *create_compound_path(ir_type *type,
2657 compound_graph_path_entry_t *entry, int len)
2659 compound_graph_path *path = new_compound_graph_path(type, len);
2662 for( ; entry != NULL; entry = entry->prev, --i) {
2664 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2665 set_compound_graph_path_node(path, i, entry->v.entity);
2667 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2668 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2676 static void create_initializer_value(initializer_value_t *initializer,
2678 compound_graph_path_entry_t *entry,
2681 ir_node *node = expression_to_firm(initializer->value);
2682 ir_type *type = get_entity_type(entity);
2683 compound_graph_path *path = create_compound_path(type, entry, len);
2684 add_compound_ent_value_w_path(entity, node, path);
2687 static void create_initializer_compound(initializer_list_t *initializer,
2688 compound_type_t *type,
2690 compound_graph_path_entry_t *last_entry,
2693 declaration_t *compound_declaration = type->declaration;
2695 declaration_t *compound_entry = compound_declaration->context.declarations;
2697 compound_graph_path_entry_t entry;
2698 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2699 entry.prev = last_entry;
2703 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2704 if(compound_entry->symbol == NULL)
2706 if(compound_entry->namespc != NAMESPACE_NORMAL)
2709 if(i >= initializer->len)
2712 entry.v.entity = compound_entry->v.entity;
2714 initializer_t *sub_initializer = initializer->initializers[i];
2716 assert(compound_entry != NULL);
2717 assert(compound_entry->declaration_kind
2718 == DECLARATION_KIND_COMPOUND_MEMBER);
2720 if(sub_initializer->kind == INITIALIZER_VALUE) {
2721 create_initializer_value(&sub_initializer->value,
2722 entity, &entry, len);
2724 type_t *entry_type = skip_typeref(compound_entry->type);
2725 create_initializer_object(sub_initializer, entry_type, entity,
2733 static void create_initializer_array(initializer_list_t *initializer,
2734 array_type_t *type, ir_entity *entity,
2735 compound_graph_path_entry_t *last_entry,
2738 type_t *element_type = type->element_type;
2739 element_type = skip_typeref(element_type);
2741 compound_graph_path_entry_t entry;
2742 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2743 entry.prev = last_entry;
2747 for(i = 0; i < initializer->len; ++i) {
2748 entry.v.array_index = i;
2750 initializer_t *sub_initializer = initializer->initializers[i];
2752 if(sub_initializer->kind == INITIALIZER_VALUE) {
2753 create_initializer_value(&sub_initializer->value,
2754 entity, &entry, len);
2756 create_initializer_object(sub_initializer, element_type, entity,
2762 /* TODO: initialize rest... */
2763 if(type->size_expression != NULL) {
2764 size_t array_len = fold_constant(type->size_expression);
2765 for( ; i < array_len; ++i) {
2772 static void create_initializer_string(initializer_string_t *initializer,
2773 array_type_t *type, ir_entity *entity,
2774 compound_graph_path_entry_t *last_entry,
2777 type_t *element_type = type->element_type;
2778 element_type = skip_typeref(element_type);
2780 compound_graph_path_entry_t entry;
2781 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2782 entry.prev = last_entry;
2785 ir_type *const irtype = get_entity_type(entity);
2786 size_t arr_len = get_array_type_size(type);
2787 const char *const p = initializer->string.begin;
2788 if (initializer->string.size < arr_len) {
2789 arr_len = initializer->string.size;
2791 for (size_t i = 0; i < arr_len; ++i) {
2792 entry.v.array_index = i;
2794 ir_node *node = new_Const_long(mode_Bs, p[i]);
2795 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2796 add_compound_ent_value_w_path(entity, node, path);
2800 static void create_initializer_wide_string(
2801 const initializer_wide_string_t *const initializer, array_type_t *const type,
2802 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2805 type_t *element_type = type->element_type;
2806 element_type = skip_typeref(element_type);
2808 compound_graph_path_entry_t entry;
2809 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2810 entry.prev = last_entry;
2813 ir_type *const irtype = get_entity_type(entity);
2814 const size_t arr_len = get_array_type_size(type);
2815 const wchar_rep_t * p = initializer->string.begin;
2816 const wchar_rep_t *const end = p + initializer->string.size;
2817 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2818 entry.v.array_index = i;
2820 ir_node *node = new_Const_long(mode_int, *p);
2821 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2822 add_compound_ent_value_w_path(entity, node, path);
2826 static void create_initializer_object(initializer_t *initializer, type_t *type,
2827 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2829 if(is_type_array(type)) {
2830 array_type_t *array_type = &type->array;
2832 switch (initializer->kind) {
2833 case INITIALIZER_STRING: {
2834 initializer_string_t *const string = &initializer->string;
2835 create_initializer_string(string, array_type, entity, entry, len);
2839 case INITIALIZER_WIDE_STRING: {
2840 initializer_wide_string_t *const string = &initializer->wide_string;
2841 create_initializer_wide_string(string, array_type, entity, entry, len);
2845 case INITIALIZER_LIST: {
2846 initializer_list_t *const list = &initializer->list;
2847 create_initializer_array(list, array_type, entity, entry, len);
2851 case INITIALIZER_VALUE:
2854 panic("Unhandled initializer");
2856 assert(initializer->kind == INITIALIZER_LIST);
2857 initializer_list_t *list = &initializer->list;
2859 assert(is_type_compound(type));
2860 compound_type_t *compound_type = &type->compound;
2861 create_initializer_compound(list, compound_type, entity, entry, len);
2865 static void create_initializer_local_variable_entity(declaration_t *declaration)
2867 initializer_t *initializer = declaration->init.initializer;
2868 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2869 ir_entity *entity = declaration->v.entity;
2870 ir_node *memory = get_store();
2871 ir_node *nomem = new_NoMem();
2872 ir_node *frame = get_irg_frame(current_ir_graph);
2873 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2875 if(initializer->kind == INITIALIZER_VALUE) {
2876 initializer_value_t *initializer_value = &initializer->value;
2878 ir_node *value = expression_to_firm(initializer_value->value);
2879 type_t *type = skip_typeref(declaration->type);
2880 assign_value(dbgi, addr, type, value);
2884 /* create a "template" entity which is copied to the entity on the stack */
2885 ident *const id = unique_ident("initializer");
2886 ir_type *const irtype = get_ir_type(declaration->type);
2887 ir_type *const global_type = get_glob_type();
2888 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
2889 set_entity_ld_ident(init_entity, id);
2891 set_entity_variability(init_entity, variability_initialized);
2892 set_entity_visibility(init_entity, visibility_local);
2893 set_entity_allocation(init_entity, allocation_static);
2895 ir_graph *const old_current_ir_graph = current_ir_graph;
2896 current_ir_graph = get_const_code_irg();
2898 type_t *const type = skip_typeref(declaration->type);
2899 create_initializer_object(initializer, type, init_entity, NULL, 0);
2901 assert(current_ir_graph == get_const_code_irg());
2902 current_ir_graph = old_current_ir_graph;
2904 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
2905 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2907 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2908 set_store(copyb_mem);
2911 static void create_initializer(declaration_t *declaration)
2913 initializer_t *initializer = declaration->init.initializer;
2914 if(initializer == NULL)
2917 declaration_kind_t declaration_kind
2918 = (declaration_kind_t) declaration->declaration_kind;
2919 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
2920 create_initializer_local_variable_entity(declaration);
2924 if(initializer->kind == INITIALIZER_VALUE) {
2925 initializer_value_t *initializer_value = &initializer->value;
2927 ir_node *value = expression_to_firm(initializer_value->value);
2929 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2930 set_value(declaration->v.value_number, value);
2932 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
2934 ir_entity *entity = declaration->v.entity;
2936 set_entity_variability(entity, variability_initialized);
2937 set_atomic_ent_value(entity, value);
2940 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
2941 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
2943 ir_entity *entity = declaration->v.entity;
2944 set_entity_variability(entity, variability_initialized);
2946 type_t *type = skip_typeref(declaration->type);
2947 create_initializer_object(initializer, type, entity, NULL, 0);
2952 * Creates a Firm local variable from a declaration.
2954 static void create_local_variable(declaration_t *declaration)
2956 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
2958 bool needs_entity = declaration->address_taken;
2959 type_t *type = skip_typeref(declaration->type);
2961 if(is_type_array(type) || is_type_compound(type)) {
2962 needs_entity = true;
2966 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2967 create_declaration_entity(declaration,
2968 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
2971 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
2972 declaration->v.value_number = next_value_number_function;
2973 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
2974 ++next_value_number_function;
2977 create_initializer(declaration);
2980 static void create_local_static_variable(declaration_t *declaration)
2982 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
2984 type_t *const type = skip_typeref(declaration->type);
2985 ir_type *const global_type = get_glob_type();
2986 ident *const id = unique_ident(declaration->symbol->string);
2987 ir_type *const irtype = get_ir_type(type);
2988 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2989 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
2990 set_entity_ld_ident(entity, id);
2992 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
2993 declaration->v.entity = entity;
2994 set_entity_variability(entity, variability_uninitialized);
2995 set_entity_visibility(entity, visibility_local);
2996 set_entity_allocation(entity, allocation_static);
2998 ir_graph *const old_current_ir_graph = current_ir_graph;
2999 current_ir_graph = get_const_code_irg();
3001 create_initializer(declaration);
3003 assert(current_ir_graph == get_const_code_irg());
3004 current_ir_graph = old_current_ir_graph;
3009 static void return_statement_to_firm(return_statement_t *statement)
3011 if(get_cur_block() == NULL)
3014 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3016 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
3020 if(get_method_n_ress(func_irtype) > 0) {
3021 ir_type *res_type = get_method_res_type(func_irtype, 0);
3023 if(statement->return_value != NULL) {
3024 ir_node *node = expression_to_firm(statement->return_value);
3025 node = do_strict_conv(dbgi, node);
3029 if(is_compound_type(res_type)) {
3032 mode = get_type_mode(res_type);
3034 in[0] = new_Unknown(mode);
3038 /* build return_value for its side effects */
3039 if(statement->return_value != NULL) {
3040 expression_to_firm(statement->return_value);
3045 ir_node *store = get_store();
3046 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3048 ir_node *end_block = get_irg_end_block(current_ir_graph);
3049 add_immBlock_pred(end_block, ret);
3051 set_cur_block(NULL);
3054 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3056 if(get_cur_block() == NULL)
3059 return expression_to_firm(statement->expression);
3062 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3064 ir_node *result = NULL;
3065 statement_t *statement = compound->statements;
3066 for( ; statement != NULL; statement = statement->base.next) {
3067 //context2firm(&statement->context);
3069 if(statement->base.next == NULL
3070 && statement->kind == STATEMENT_EXPRESSION) {
3071 result = expression_statement_to_firm(
3072 &statement->expression);
3075 statement_to_firm(statement);
3081 static void create_local_declaration(declaration_t *declaration)
3083 type_t *type = skip_typeref(declaration->type);
3085 switch ((storage_class_tag_t) declaration->storage_class) {
3086 case STORAGE_CLASS_STATIC:
3087 create_local_static_variable(declaration);
3089 case STORAGE_CLASS_ENUM_ENTRY:
3090 panic("enum entry declaration in local block found");
3091 case STORAGE_CLASS_EXTERN:
3092 panic("extern declaration in local block found");
3093 case STORAGE_CLASS_NONE:
3094 case STORAGE_CLASS_AUTO:
3095 case STORAGE_CLASS_REGISTER:
3096 if(is_type_function(type)) {
3097 panic("nested functions not supported yet");
3099 create_local_variable(declaration);
3102 case STORAGE_CLASS_TYPEDEF:
3103 case STORAGE_CLASS_THREAD:
3104 case STORAGE_CLASS_THREAD_EXTERN:
3105 case STORAGE_CLASS_THREAD_STATIC:
3108 panic("invalid storage class found");
3111 static void declaration_statement_to_firm(declaration_statement_t *statement)
3113 declaration_t *declaration = statement->declarations_begin;
3114 declaration_t *end = statement->declarations_end->next;
3115 for( ; declaration != end; declaration = declaration->next) {
3116 if(declaration->namespc != NAMESPACE_NORMAL)
3118 create_local_variable(declaration);
3122 static void if_statement_to_firm(if_statement_t *statement)
3124 ir_node *cur_block = get_cur_block();
3126 ir_node *fallthrough_block = new_immBlock();
3128 /* the true (blocks) */
3129 ir_node *true_block;
3130 if (statement->true_statement != NULL) {
3131 true_block = new_immBlock();
3132 statement_to_firm(statement->true_statement);
3133 if(get_cur_block() != NULL) {
3134 ir_node *jmp = new_Jmp();
3135 add_immBlock_pred(fallthrough_block, jmp);
3138 true_block = fallthrough_block;
3141 /* the false (blocks) */
3142 ir_node *false_block;
3143 if(statement->false_statement != NULL) {
3144 false_block = new_immBlock();
3146 statement_to_firm(statement->false_statement);
3147 if(get_cur_block() != NULL) {
3148 ir_node *jmp = new_Jmp();
3149 add_immBlock_pred(fallthrough_block, jmp);
3152 false_block = fallthrough_block;
3155 /* create the condition */
3156 if(cur_block != NULL) {
3157 set_cur_block(cur_block);
3158 create_condition_evaluation(statement->condition, true_block,
3162 mature_immBlock(true_block);
3163 if(false_block != fallthrough_block) {
3164 mature_immBlock(false_block);
3166 mature_immBlock(fallthrough_block);
3168 set_cur_block(fallthrough_block);
3171 static void while_statement_to_firm(while_statement_t *statement)
3173 ir_node *jmp = NULL;
3174 if(get_cur_block() != NULL) {
3178 /* create the header block */
3179 ir_node *header_block = new_immBlock();
3181 add_immBlock_pred(header_block, jmp);
3184 /* the false block */
3185 ir_node *false_block = new_immBlock();
3188 ir_node *body_block;
3189 if (statement->body != NULL) {
3190 ir_node *old_continue_label = continue_label;
3191 ir_node *old_break_label = break_label;
3192 continue_label = header_block;
3193 break_label = false_block;
3195 body_block = new_immBlock();
3196 statement_to_firm(statement->body);
3198 assert(continue_label == header_block);
3199 assert(break_label == false_block);
3200 continue_label = old_continue_label;
3201 break_label = old_break_label;
3203 if(get_cur_block() != NULL) {
3205 add_immBlock_pred(header_block, jmp);
3208 body_block = header_block;
3211 /* create the condition */
3212 set_cur_block(header_block);
3214 create_condition_evaluation(statement->condition, body_block, false_block);
3215 mature_immBlock(body_block);
3216 mature_immBlock(false_block);
3217 mature_immBlock(header_block);
3219 set_cur_block(false_block);
3222 static void do_while_statement_to_firm(do_while_statement_t *statement)
3224 ir_node *jmp = NULL;
3225 if(get_cur_block() != NULL) {
3229 /* create the header block */
3230 ir_node *header_block = new_immBlock();
3232 /* the false block */
3233 ir_node *false_block = new_immBlock();
3236 ir_node *body_block = new_immBlock();
3238 add_immBlock_pred(body_block, jmp);
3241 if (statement->body != NULL) {
3242 ir_node *old_continue_label = continue_label;
3243 ir_node *old_break_label = break_label;
3244 continue_label = header_block;
3245 break_label = false_block;
3247 statement_to_firm(statement->body);
3249 assert(continue_label == header_block);
3250 assert(break_label == false_block);
3251 continue_label = old_continue_label;
3252 break_label = old_break_label;
3254 if (get_cur_block() == NULL) {
3255 mature_immBlock(header_block);
3256 mature_immBlock(body_block);
3257 mature_immBlock(false_block);
3262 ir_node *body_jmp = new_Jmp();
3263 add_immBlock_pred(header_block, body_jmp);
3264 mature_immBlock(header_block);
3266 /* create the condition */
3267 set_cur_block(header_block);
3269 create_condition_evaluation(statement->condition, body_block, false_block);
3270 mature_immBlock(body_block);
3271 mature_immBlock(false_block);
3272 mature_immBlock(header_block);
3274 set_cur_block(false_block);
3277 static void for_statement_to_firm(for_statement_t *statement)
3279 ir_node *jmp = NULL;
3280 if (get_cur_block() != NULL) {
3281 if(statement->initialisation != NULL) {
3282 expression_to_firm(statement->initialisation);
3285 /* create declarations */
3286 declaration_t *declaration = statement->context.declarations;
3287 for( ; declaration != NULL; declaration = declaration->next) {
3288 create_local_declaration(declaration);
3295 /* create the step block */
3296 ir_node *const step_block = new_immBlock();
3297 if (statement->step != NULL) {
3298 expression_to_firm(statement->step);
3300 ir_node *const step_jmp = new_Jmp();
3302 /* create the header block */
3303 ir_node *const header_block = new_immBlock();
3305 add_immBlock_pred(header_block, jmp);
3307 add_immBlock_pred(header_block, step_jmp);
3309 /* the false block */
3310 ir_node *const false_block = new_immBlock();
3313 ir_node * body_block;
3314 if (statement->body != NULL) {
3315 ir_node *const old_continue_label = continue_label;
3316 ir_node *const old_break_label = break_label;
3317 continue_label = step_block;
3318 break_label = false_block;
3320 body_block = new_immBlock();
3321 statement_to_firm(statement->body);
3323 assert(continue_label == step_block);
3324 assert(break_label == false_block);
3325 continue_label = old_continue_label;
3326 break_label = old_break_label;
3328 if (get_cur_block() != NULL) {
3330 add_immBlock_pred(step_block, jmp);
3333 body_block = step_block;
3336 /* create the condition */
3337 set_cur_block(header_block);
3338 if (statement->condition != NULL) {
3339 create_condition_evaluation(statement->condition, body_block,
3342 keep_alive(header_block);
3344 add_immBlock_pred(body_block, jmp);
3347 mature_immBlock(body_block);
3348 mature_immBlock(false_block);
3349 mature_immBlock(step_block);
3350 mature_immBlock(header_block);
3351 mature_immBlock(false_block);
3353 set_cur_block(false_block);
3356 static void create_jump_statement(const statement_t *statement,
3357 ir_node *target_block)
3359 if(get_cur_block() == NULL)
3362 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3363 ir_node *jump = new_d_Jmp(dbgi);
3364 add_immBlock_pred(target_block, jump);
3366 set_cur_block(NULL);
3369 static void switch_statement_to_firm(const switch_statement_t *statement)
3371 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
3373 ir_node *expression = expression_to_firm(statement->expression);
3374 ir_node *cond = new_d_Cond(dbgi, expression);
3375 ir_node *break_block = new_immBlock();
3377 set_cur_block(NULL);
3379 ir_node *const old_switch_cond = current_switch_cond;
3380 ir_node *const old_break_label = break_label;
3381 const bool old_saw_default_label = saw_default_label;
3382 current_switch_cond = cond;
3383 break_label = break_block;
3385 statement_to_firm(statement->body);
3387 if(get_cur_block() != NULL) {
3388 ir_node *jmp = new_Jmp();
3389 add_immBlock_pred(break_block, jmp);
3392 if (!saw_default_label) {
3393 set_cur_block(get_nodes_block(cond));
3394 ir_node *const proj = new_d_defaultProj(dbgi, cond,
3395 MAGIC_DEFAULT_PN_NUMBER);
3396 add_immBlock_pred(break_block, proj);
3399 assert(current_switch_cond == cond);
3400 assert(break_label == break_block);
3401 current_switch_cond = old_switch_cond;
3402 break_label = old_break_label;
3403 saw_default_label = old_saw_default_label;
3405 mature_immBlock(break_block);
3406 set_cur_block(break_block);
3409 static void case_label_to_firm(const case_label_statement_t *statement)
3411 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
3413 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
3415 /* let's create a node and hope firm constant folding creates a Const
3418 set_cur_block(get_nodes_block(current_switch_cond));
3419 if(statement->expression) {
3420 long pn = fold_constant(statement->expression);
3421 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
3422 /* oops someone detected our cheating... */
3423 panic("magic default pn used");
3425 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
3427 saw_default_label = true;
3428 proj = new_d_defaultProj(dbgi, current_switch_cond,
3429 MAGIC_DEFAULT_PN_NUMBER);
3432 ir_node *block = new_immBlock();
3433 if (fallthrough != NULL) {
3434 add_immBlock_pred(block, fallthrough);
3436 add_immBlock_pred(block, proj);
3437 mature_immBlock(block);
3439 if(statement->label_statement != NULL) {
3440 statement_to_firm(statement->label_statement);
3444 static ir_node *get_label_block(declaration_t *label)
3446 assert(label->namespc == NAMESPACE_LABEL);
3448 if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
3449 return label->v.block;
3451 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
3453 ir_node *old_cur_block = get_cur_block();
3454 ir_node *block = new_immBlock();
3455 set_cur_block(old_cur_block);
3457 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
3458 label->v.block = block;
3460 ARR_APP1(ir_node *, imature_blocks, block);
3465 static void label_to_firm(const label_statement_t *statement)
3467 ir_node *block = get_label_block(statement->label);
3469 if(get_cur_block() != NULL) {
3470 ir_node *jmp = new_Jmp();
3471 add_immBlock_pred(block, jmp);
3474 set_cur_block(block);
3477 if(statement->label_statement != NULL) {
3478 statement_to_firm(statement->label_statement);
3482 static void goto_to_firm(const goto_statement_t *statement)
3484 if(get_cur_block() == NULL)
3487 ir_node *block = get_label_block(statement->label);
3488 ir_node *jmp = new_Jmp();
3489 add_immBlock_pred(block, jmp);
3491 set_cur_block(NULL);
3494 typedef enum modifier_t {
3495 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
3496 ASM_MODIFIER_READ_WRITE = 1 << 1,
3497 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
3498 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
3502 static void asm_statement_to_firm(const asm_statement_t *statement)
3504 bool needs_memory = false;
3506 size_t n_clobbers = 0;
3507 asm_clobber_t *clobber = statement->clobbers;
3508 for( ; clobber != NULL; clobber = clobber->next) {
3509 if(strcmp(clobber->clobber, "memory") == 0) {
3510 needs_memory = true;
3514 ident *id = new_id_from_str(clobber->clobber);
3515 obstack_ptr_grow(&asm_obst, id);
3518 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
3519 ident **clobbers = NULL;
3520 if(n_clobbers > 0) {
3521 clobbers = obstack_finish(&asm_obst);
3524 /* find and count input and output constraints */
3525 asm_constraint_t *constraint = statement->inputs;
3526 for( ; constraint != NULL; constraint = constraint->next) {
3528 bool supports_memop = false;
3529 for(const char *c = constraint->constraints; *c != 0; ++c) {
3530 /* TODO: improve error messages */
3534 panic("multiple alternative assembler constraints not "
3542 supports_memop = true;
3543 obstack_1grow(&asm_obst, *c);
3546 if(modifiers & ASM_MODIFIER_READ_WRITE)
3547 panic("inconsistent register constraints");
3548 modifiers |= ASM_MODIFIER_WRITE_ONLY;
3551 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
3552 panic("inconsistent register constraints");
3553 modifiers |= ASM_MODIFIER_READ_WRITE;
3556 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
3557 panic("early clobber assembler constraint not supported yet");
3560 modifiers |= ASM_MODIFIER_COMMUTATIVE;
3561 panic("commutative assembler constraint not supported yet");
3564 /* skip register preferences stuff... */
3565 while(*c != 0 && *c != ',')
3569 /* skip register preferences stuff... */
3573 obstack_1grow(&asm_obst, *c);
3577 obstack_1grow(&asm_obst, '\0');
3578 const char *constraint_string = obstack_finish(&asm_obst);
3580 needs_memory |= supports_memop;
3581 if(supports_memop) {
3589 static void statement_to_firm(statement_t *statement)
3591 switch(statement->kind) {
3592 case STATEMENT_INVALID:
3593 panic("invalid statement found");
3594 case STATEMENT_COMPOUND:
3595 compound_statement_to_firm(&statement->compound);
3597 case STATEMENT_RETURN:
3598 return_statement_to_firm(&statement->returns);
3600 case STATEMENT_EXPRESSION:
3601 expression_statement_to_firm(&statement->expression);
3604 if_statement_to_firm(&statement->ifs);
3606 case STATEMENT_WHILE:
3607 while_statement_to_firm(&statement->whiles);
3609 case STATEMENT_DO_WHILE:
3610 do_while_statement_to_firm(&statement->do_while);
3612 case STATEMENT_DECLARATION:
3613 declaration_statement_to_firm(&statement->declaration);
3615 case STATEMENT_BREAK:
3616 create_jump_statement(statement, break_label);
3618 case STATEMENT_CONTINUE:
3619 create_jump_statement(statement, continue_label);
3621 case STATEMENT_SWITCH:
3622 switch_statement_to_firm(&statement->switchs);
3624 case STATEMENT_CASE_LABEL:
3625 case_label_to_firm(&statement->case_label);
3628 for_statement_to_firm(&statement->fors);
3630 case STATEMENT_LABEL:
3631 label_to_firm(&statement->label);
3633 case STATEMENT_GOTO:
3634 goto_to_firm(&statement->gotos);
3637 //asm_statement_to_firm(&statement->asms);
3641 panic("Statement not implemented\n");
3644 static int count_decls_in_expression(const expression_t *expression);
3646 static int count_local_declarations(const declaration_t * decl,
3647 const declaration_t *const end)
3650 for (; decl != end; decl = decl->next) {
3651 if(decl->namespc != NAMESPACE_NORMAL)
3653 const type_t *type = skip_typeref(decl->type);
3654 if (!decl->address_taken && is_type_scalar(type))
3656 const initializer_t *initializer = decl->init.initializer;
3657 /* FIXME: should walk initializer hierarchies... */
3658 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
3659 count += count_decls_in_expression(initializer->value.value);
3665 static int count_decls_in_expression(const expression_t *expression) {
3666 if(expression == NULL)
3669 switch(expression->base.kind) {
3670 case EXPR_STATEMENT:
3671 return count_decls_in_stmts(expression->statement.statement);
3673 int count_left = count_decls_in_expression(expression->binary.left);
3674 int count_right = count_decls_in_expression(expression->binary.right);
3675 return count_left + count_right;
3678 return count_decls_in_expression(expression->unary.value);
3684 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
3685 * (or implement all the missing expressions here/implement a walker)
3691 static int count_decls_in_stmts(const statement_t *stmt)
3694 for (; stmt != NULL; stmt = stmt->base.next) {
3695 switch (stmt->kind) {
3696 case STATEMENT_DECLARATION: {
3697 const declaration_statement_t *const decl_stmt = &stmt->declaration;
3698 count += count_local_declarations(decl_stmt->declarations_begin,
3699 decl_stmt->declarations_end->next);
3703 case STATEMENT_COMPOUND: {
3704 const compound_statement_t *const comp =
3706 count += count_decls_in_stmts(comp->statements);
3710 case STATEMENT_IF: {
3711 const if_statement_t *const if_stmt = &stmt->ifs;
3712 count += count_decls_in_expression(if_stmt->condition);
3713 count += count_decls_in_stmts(if_stmt->true_statement);
3714 count += count_decls_in_stmts(if_stmt->false_statement);
3718 case STATEMENT_SWITCH: {
3719 const switch_statement_t *const switch_stmt = &stmt->switchs;
3720 count += count_decls_in_expression(switch_stmt->expression);
3721 count += count_decls_in_stmts(switch_stmt->body);
3725 case STATEMENT_LABEL: {
3726 const label_statement_t *const label_stmt = &stmt->label;
3727 count += count_decls_in_stmts(label_stmt->label_statement);
3731 case STATEMENT_WHILE: {
3732 const while_statement_t *const while_stmt = &stmt->whiles;
3733 count += count_decls_in_expression(while_stmt->condition);
3734 count += count_decls_in_stmts(while_stmt->body);
3738 case STATEMENT_DO_WHILE: {
3739 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
3740 count += count_decls_in_expression(do_while_stmt->condition);
3741 count += count_decls_in_stmts(do_while_stmt->body);
3745 case STATEMENT_FOR: {
3746 const for_statement_t *const for_stmt = &stmt->fors;
3747 count += count_local_declarations(for_stmt->context.declarations, NULL);
3748 count += count_decls_in_expression(for_stmt->initialisation);
3749 count += count_decls_in_expression(for_stmt->condition);
3750 count += count_decls_in_expression(for_stmt->step);
3751 count += count_decls_in_stmts(for_stmt->body);
3755 case STATEMENT_CASE_LABEL: {
3756 const case_label_statement_t *label = &stmt->case_label;
3757 count += count_decls_in_expression(label->expression);
3758 count += count_decls_in_stmts(label->label_statement);
3763 case STATEMENT_BREAK:
3764 case STATEMENT_CONTINUE:
3767 case STATEMENT_EXPRESSION: {
3768 const expression_statement_t *expr_stmt = &stmt->expression;
3769 count += count_decls_in_expression(expr_stmt->expression);
3773 case STATEMENT_GOTO:
3774 case STATEMENT_INVALID:
3777 case STATEMENT_RETURN: {
3778 const return_statement_t *ret_stmt = &stmt->returns;
3779 count += count_decls_in_expression(ret_stmt->return_value);
3787 static int get_function_n_local_vars(declaration_t *declaration)
3791 /* count parameters */
3792 count += count_local_declarations(declaration->context.declarations, NULL);
3794 /* count local variables declared in body */
3795 count += count_decls_in_stmts(declaration->init.statement);
3800 static void initialize_function_parameters(declaration_t *declaration)
3802 ir_graph *irg = current_ir_graph;
3803 ir_node *args = get_irg_args(irg);
3804 ir_node *start_block = get_irg_start_block(irg);
3805 ir_type *function_irtype = get_ir_type(declaration->type);
3808 declaration_t *parameter = declaration->context.declarations;
3809 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3810 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
3811 type_t *type = skip_typeref(parameter->type);
3813 bool needs_entity = parameter->address_taken;
3814 assert(!is_type_array(type));
3815 if(is_type_compound(type)) {
3816 needs_entity = true;
3820 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3821 ident *id = new_id_from_str(parameter->symbol->string);
3822 set_entity_ident(entity, id);
3824 parameter->declaration_kind
3825 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
3826 parameter->v.entity = entity;
3830 ir_mode *mode = get_ir_mode(parameter->type);
3832 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3834 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3835 parameter->v.value_number = next_value_number_function;
3836 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
3837 ++next_value_number_function;
3839 set_value(parameter->v.value_number, proj);
3844 * Handle additional decl modifiers for IR-graphs
3846 * @param irg the IR-graph
3847 * @param dec_modifiers additional modifiers
3849 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
3851 if (decl_modifiers & DM_NORETURN) {
3852 /* TRUE if the declaration includes the Microsoft
3853 __declspec(noreturn) specifier. */
3854 set_irg_additional_property(irg, mtp_property_noreturn);
3856 if (decl_modifiers & DM_NOTHROW) {
3857 /* TRUE if the declaration includes the Microsoft
3858 __declspec(nothrow) specifier. */
3859 set_irg_additional_property(irg, mtp_property_nothrow);
3861 if (decl_modifiers & DM_NAKED) {
3862 /* TRUE if the declaration includes the Microsoft
3863 __declspec(naked) specifier. */
3864 set_irg_additional_property(irg, mtp_property_naked);
3866 if (decl_modifiers & DM_FORCEINLINE) {
3867 /* TRUE if the declaration includes the
3868 Microsoft __forceinline specifier. */
3869 set_irg_inline_property(irg, irg_inline_forced);
3871 if (decl_modifiers & DM_NOINLINE) {
3872 /* TRUE if the declaration includes the Microsoft
3873 __declspec(noinline) specifier. */
3874 set_irg_inline_property(irg, irg_inline_forbidden);
3878 static void create_function(declaration_t *declaration)
3880 ir_entity *function_entity = get_function_entity(declaration);
3882 if(declaration->init.statement == NULL)
3885 current_function_decl = declaration;
3886 current_function_name = NULL;
3888 assert(imature_blocks == NULL);
3889 imature_blocks = NEW_ARR_F(ir_node*, 0);
3891 int n_local_vars = get_function_n_local_vars(declaration);
3892 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3893 ir_node *first_block = get_cur_block();
3895 /* set inline flags */
3896 if (declaration->is_inline)
3897 set_irg_inline_property(irg, irg_inline_recomended);
3898 handle_decl_modifier_irg(irg, declaration->modifiers);
3900 next_value_number_function = 0;
3901 initialize_function_parameters(declaration);
3903 statement_to_firm(declaration->init.statement);
3905 ir_node *end_block = get_irg_end_block(irg);
3907 /* do we have a return statement yet? */
3908 if(get_cur_block() != NULL) {
3909 type_t *type = skip_typeref(declaration->type);
3910 assert(is_type_function(type));
3911 const function_type_t *func_type = &type->function;
3912 const type_t *return_type
3913 = skip_typeref(func_type->return_type);
3916 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3917 ret = new_Return(get_store(), 0, NULL);
3920 if(is_type_scalar(return_type)) {
3921 mode = get_ir_mode(func_type->return_type);
3927 /* ยง5.1.2.2.3 main implicitly returns 0 */
3928 if (strcmp(declaration->symbol->string, "main") == 0) {
3929 in[0] = new_Const(mode, get_mode_null(mode));
3931 in[0] = new_Unknown(mode);
3933 ret = new_Return(get_store(), 1, in);
3935 add_immBlock_pred(end_block, ret);
3938 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3939 mature_immBlock(imature_blocks[i]);
3941 DEL_ARR_F(imature_blocks);
3942 imature_blocks = NULL;
3944 mature_immBlock(first_block);
3945 mature_immBlock(end_block);
3947 irg_finalize_cons(irg);
3949 /* finalize the frame type */
3950 ir_type *frame_type = get_irg_frame_type(irg);
3951 int n = get_compound_n_members(frame_type);
3954 for(int i = 0; i < n; ++i) {
3955 ir_entity *entity = get_compound_member(frame_type, i);
3956 ir_type *entity_type = get_entity_type(entity);
3958 int align = get_type_alignment_bytes(entity_type);
3959 if(align > align_all)
3963 misalign = offset % align;
3965 offset += align - misalign;
3969 set_entity_offset(entity, offset);
3970 offset += get_type_size_bytes(entity_type);
3972 set_type_size_bytes(frame_type, offset);
3973 set_type_alignment_bytes(frame_type, align_all);
3974 set_type_state(frame_type, layout_fixed);
3979 static void create_global_variable(declaration_t *declaration)
3983 switch ((storage_class_tag_t)declaration->storage_class) {
3984 case STORAGE_CLASS_STATIC:
3985 vis = visibility_local;
3988 case STORAGE_CLASS_EXTERN:
3989 vis = visibility_external_allocated;
3992 case STORAGE_CLASS_NONE:
3993 vis = visibility_external_visible;
3996 case STORAGE_CLASS_THREAD:
3997 vis = visibility_external_visible;
4000 case STORAGE_CLASS_THREAD_EXTERN:
4001 vis = visibility_external_allocated;
4004 case STORAGE_CLASS_THREAD_STATIC:
4005 vis = visibility_local;
4009 var_type = get_tls_type();
4013 var_type = get_glob_type();
4017 create_declaration_entity(declaration,
4018 DECLARATION_KIND_GLOBAL_VARIABLE,
4020 set_entity_visibility(declaration->v.entity, vis);
4022 current_ir_graph = get_const_code_irg();
4023 create_initializer(declaration);
4026 case STORAGE_CLASS_TYPEDEF:
4027 case STORAGE_CLASS_AUTO:
4028 case STORAGE_CLASS_REGISTER:
4029 case STORAGE_CLASS_ENUM_ENTRY:
4032 panic("Invalid storage class for global variable");
4035 static void context_to_firm(context_t *context)
4037 /* first pass: create declarations */
4038 declaration_t *declaration = context->declarations;
4039 for( ; declaration != NULL; declaration = declaration->next) {
4040 if(declaration->namespc != NAMESPACE_NORMAL)
4042 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4043 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4045 if(declaration->symbol == NULL)
4048 type_t *type = skip_typeref(declaration->type);
4049 if(is_type_function(type)) {
4050 get_function_entity(declaration);
4052 create_global_variable(declaration);
4056 /* second pass: create code */
4057 declaration = context->declarations;
4058 for( ; declaration != NULL; declaration = declaration->next) {
4059 if(declaration->namespc != NAMESPACE_NORMAL)
4061 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4062 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4064 if(declaration->symbol == NULL)
4067 type_t *type = declaration->type;
4068 if(type->kind != TYPE_FUNCTION)
4071 create_function(declaration);
4075 void init_ast2firm(void)
4077 obstack_init(&asm_obst);
4078 init_atomic_modes();
4080 /* create idents for all known runtime functions */
4081 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
4082 predef_idents[rts_data[i].id] = new_id_from_str(rts_data[i].name);
4086 void exit_ast2firm(void)
4088 obstack_free(&asm_obst, NULL);
4091 void translation_unit_to_firm(translation_unit_t *unit)
4093 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4094 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4095 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4097 ir_type_int = get_ir_type(type_int);
4098 ir_type_const_char = get_ir_type(type_const_char);
4099 ir_type_wchar_t = get_ir_type(type_wchar_t);
4100 ir_type_void = get_ir_type(type_int); /* we don't have a real void
4103 type_void->base.firm_type = ir_type_void;
4105 /* just to be sure */
4106 continue_label = NULL;
4108 current_switch_cond = NULL;
4110 context_to_firm(&unit->context);