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\n",
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 char *const string)
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 size_t slen = strlen(string) + 1;
981 set_array_lower_bound_int(type, 0, 0);
982 set_array_upper_bound_int(type, 0, slen);
983 set_type_size_bytes(type, slen);
984 set_type_state(type, layout_fixed);
986 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
987 for(size_t i = 0; i < slen; ++i) {
988 tvs[i] = new_tarval_from_long(string[i], mode);
991 set_array_entity_values(entity, tvs, slen);
994 return create_symconst(dbgi, mode_P_data, entity);
997 static ir_node *string_literal_to_firm(
998 const string_literal_expression_t* literal)
1000 return string_to_firm(&literal->expression.source_position, "Lstr",
1004 static ir_node *wide_string_literal_to_firm(
1005 const wide_string_literal_expression_t* const literal)
1007 ir_type *const global_type = get_glob_type();
1008 ir_type *const elem_type = ir_type_wchar_t;
1009 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
1012 ident *const id = unique_ident("Lstr");
1013 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
1014 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1015 set_entity_ld_ident(entity, id);
1016 set_entity_variability(entity, variability_constant);
1017 set_entity_allocation(entity, allocation_static);
1019 ir_mode *const mode = get_type_mode(elem_type);
1021 const wchar_rep_t *const string = literal->value.begin;
1022 const size_t slen = literal->value.size;
1024 set_array_lower_bound_int(type, 0, 0);
1025 set_array_upper_bound_int(type, 0, slen);
1026 set_type_size_bytes(type, slen);
1027 set_type_state(type, layout_fixed);
1029 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1030 for(size_t i = 0; i < slen; ++i) {
1031 tvs[i] = new_tarval_from_long(string[i], mode);
1034 set_array_entity_values(entity, tvs, slen);
1037 return create_symconst(dbgi, mode_P_data, entity);
1040 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
1041 dbg_info *const dbgi)
1043 if(is_compound_type(irtype) || is_Array_type(irtype)) {
1047 ir_mode *const mode = get_type_mode(irtype);
1048 ir_node *const memory = get_store();
1049 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1050 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1051 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1052 set_store(load_mem);
1056 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1058 ir_mode *mode = get_irn_mode(node);
1060 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1062 if(!mode_is_float(mode))
1065 /* check if there is already a Conv */
1066 if (get_irn_op(node) == op_Conv) {
1067 /* convert it into a strict Conv */
1068 set_Conv_strict(node, 1);
1072 /* otherwise create a new one */
1073 return new_d_strictConv(dbgi, node, mode);
1076 static ir_node *get_global_var_address(dbg_info *const dbgi,
1077 const declaration_t *const decl)
1079 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1081 ir_entity *const entity = decl->v.entity;
1082 switch ((storage_class_tag_t)decl->storage_class) {
1083 case STORAGE_CLASS_THREAD:
1084 case STORAGE_CLASS_THREAD_EXTERN:
1085 case STORAGE_CLASS_THREAD_STATIC: {
1086 ir_node *const no_mem = new_NoMem();
1087 ir_node *const tls = get_irg_tls(current_ir_graph);
1088 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1092 return create_symconst(dbgi, mode_P_data, entity);
1096 /* Returns the correct base address depending on whether it is a parameter or a
1097 * normal local variable */
1098 static ir_node *get_local_frame(ir_entity *const ent)
1100 ir_graph *const irg = current_ir_graph;
1101 const ir_type *const owner = get_entity_owner(ent);
1102 if (owner == get_irg_frame_type(irg)) {
1103 return get_irg_frame(irg);
1105 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1106 return get_irg_value_param_base(irg);
1110 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1112 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
1113 declaration_t *declaration = ref->declaration;
1114 type_t *type = skip_typeref(declaration->type);
1116 switch((declaration_kind_t) declaration->declaration_kind) {
1117 case DECLARATION_KIND_UNKNOWN:
1118 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
1124 case DECLARATION_KIND_ENUM_ENTRY: {
1125 ir_mode *const mode = get_ir_mode(type);
1126 return new_Const(mode, declaration->v.enum_val);
1129 case DECLARATION_KIND_LOCAL_VARIABLE: {
1130 ir_mode *const mode = get_ir_mode(type);
1131 return get_value(declaration->v.value_number, mode);
1133 case DECLARATION_KIND_FUNCTION: {
1134 ir_mode *const mode = get_ir_mode(type);
1135 return create_symconst(dbgi, mode, declaration->v.entity);
1137 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1138 ir_node *const addr = get_global_var_address(dbgi, declaration);
1139 ir_type *const irtype = get_entity_type(declaration->v.entity);
1140 return deref_address(irtype, addr, dbgi);
1143 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1144 ir_entity *entity = declaration->v.entity;
1145 ir_node *frame = get_local_frame(entity);
1146 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1147 ir_type *irtype = get_entity_type(entity);
1148 return deref_address(irtype, sel, dbgi);
1151 case DECLARATION_KIND_COMPOUND_MEMBER:
1152 case DECLARATION_KIND_LABEL_BLOCK:
1153 panic("not implemented reference type");
1156 panic("reference to declaration with unknown type found");
1159 static ir_node *reference_addr(const reference_expression_t *ref)
1161 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
1162 declaration_t *declaration = ref->declaration;
1164 switch((declaration_kind_t) declaration->declaration_kind) {
1165 case DECLARATION_KIND_UNKNOWN:
1167 case DECLARATION_KIND_LOCAL_VARIABLE:
1168 panic("local variable without entity has no address");
1169 case DECLARATION_KIND_FUNCTION: {
1170 type_t *const type = skip_typeref(ref->expression.datatype);
1171 ir_mode *const mode = get_ir_mode(type);
1172 return create_symconst(dbgi, mode, declaration->v.entity);
1174 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1175 ir_node *const addr = get_global_var_address(dbgi, declaration);
1178 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1179 ir_entity *entity = declaration->v.entity;
1180 ir_node *frame = get_local_frame(entity);
1181 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1186 case DECLARATION_KIND_ENUM_ENTRY:
1187 panic("trying to reference enum entry");
1189 case DECLARATION_KIND_COMPOUND_MEMBER:
1190 case DECLARATION_KIND_LABEL_BLOCK:
1191 panic("not implemented reference type");
1194 panic("reference to declaration with unknown type found");
1197 static ir_node *process_builtin_call(const call_expression_t *call)
1199 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1201 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1202 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1204 type_t *type = skip_typeref(builtin->expression.datatype);
1205 assert(is_type_pointer(type));
1207 type_t *function_type = skip_typeref(type->pointer.points_to);
1208 symbol_t *symbol = builtin->symbol;
1210 switch(symbol->ID) {
1211 case T___builtin_alloca: {
1212 if(call->arguments == NULL || call->arguments->next != NULL) {
1213 panic("invalid number of parameters on __builtin_alloca");
1215 expression_t *argument = call->arguments->expression;
1216 ir_node *size = expression_to_firm(argument);
1218 ir_node *store = get_store();
1219 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1221 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1223 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1227 case T___builtin_nan:
1228 case T___builtin_nanf:
1229 case T___builtin_nand: {
1230 /* Ignore string for now... */
1231 assert(is_type_function(function_type));
1232 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1233 tarval *tv = get_mode_NAN(mode);
1234 ir_node *res = new_d_Const(dbgi, mode, tv);
1237 case T___builtin_va_end:
1240 panic("Unsupported builtin found\n");
1244 static ir_node *call_expression_to_firm(const call_expression_t *call)
1246 assert(get_cur_block() != NULL);
1248 expression_t *function = call->function;
1249 if(function->kind == EXPR_BUILTIN_SYMBOL) {
1250 return process_builtin_call(call);
1252 ir_node *callee = expression_to_firm(function);
1254 type_t *type = skip_typeref(function->base.datatype);
1255 assert(is_type_pointer(type));
1256 pointer_type_t *pointer_type = &type->pointer;
1257 type_t *points_to = skip_typeref(pointer_type->points_to);
1258 assert(is_type_function(points_to));
1259 function_type_t *function_type = &points_to->function;
1261 int n_parameters = 0;
1262 call_argument_t *argument = call->arguments;
1263 for( ; argument != NULL; argument = argument->next) {
1267 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1269 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1270 ir_type *new_method_type = NULL;
1271 if(function_type->variadic || function_type->unspecified_parameters) {
1272 /* we need to construct a new method type matching the call
1274 int n_res = get_method_n_ress(ir_method_type);
1275 new_method_type = new_type_method(unique_ident("calltype"),
1276 n_parameters, n_res);
1277 set_method_calling_convention(new_method_type,
1278 get_method_calling_convention(ir_method_type));
1279 set_method_additional_properties(new_method_type,
1280 get_method_additional_properties(ir_method_type));
1282 for(int i = 0; i < n_res; ++i) {
1283 set_method_res_type(new_method_type, i,
1284 get_method_res_type(ir_method_type, i));
1287 ir_node *in[n_parameters];
1289 argument = call->arguments;
1291 for( ; argument != NULL; argument = argument->next) {
1292 expression_t *expression = argument->expression;
1293 ir_node *arg_node = expression_to_firm(expression);
1295 arg_node = do_strict_conv(dbgi, arg_node);
1298 if(new_method_type != NULL) {
1299 ir_type *irtype = get_ir_type(expression->base.datatype);
1300 set_method_param_type(new_method_type, n, irtype);
1305 assert(n == n_parameters);
1307 if(new_method_type != NULL)
1308 ir_method_type = new_method_type;
1310 ir_node *store = get_store();
1311 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1313 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1316 type_t *return_type = skip_typeref(function_type->return_type);
1317 ir_node *result = NULL;
1319 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1321 if(is_type_scalar(return_type)) {
1322 mode = get_ir_mode(return_type);
1326 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1327 result = new_d_Proj(dbgi, resproj, mode, 0);
1333 static void statement_to_firm(statement_t *statement);
1334 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1336 static ir_node *expression_to_addr(const expression_t *expression);
1337 static void create_condition_evaluation(const expression_t *expression,
1338 ir_node *true_block,
1339 ir_node *false_block);
1341 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1344 value = do_strict_conv(dbgi, value);
1346 ir_node *memory = get_store();
1348 if(is_type_scalar(type)) {
1349 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1350 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1351 set_store(store_mem);
1353 ir_type *irtype = get_ir_type(type);
1354 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1355 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1356 set_store(copyb_mem);
1360 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1362 tarval *all_one = get_mode_all_one(mode);
1363 int mode_size = get_mode_size_bits(mode);
1365 assert(offset >= 0 && size >= 0);
1366 assert(offset + size <= mode_size);
1367 if(size == mode_size) {
1371 long shiftr = get_mode_size_bits(mode) - size;
1372 long shiftl = offset;
1373 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1374 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1375 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1376 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1381 static void bitfield_store_to_firm(const unary_expression_t *expression,
1384 expression_t *select = expression->value;
1385 assert(select->kind == EXPR_SELECT);
1386 type_t *type = select->base.datatype;
1387 assert(type->kind == TYPE_BITFIELD);
1388 ir_mode *mode = get_ir_mode(type->bitfield.base);
1389 ir_node *addr = expression_to_addr(select);
1391 assert(get_irn_mode(value) == mode);
1393 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1395 /* kill upper bits of value and shift to right position */
1396 ir_entity *entity = select->select.compound_entry->v.entity;
1397 int bitoffset = get_entity_offset_bits_remainder(entity);
1398 ir_type *entity_type = get_entity_type(entity);
1399 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1401 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1402 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1403 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1404 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1405 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1406 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1408 /* load current value */
1409 ir_node *mem = get_store();
1410 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1411 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1412 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1413 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1414 tarval *inv_mask = tarval_not(shift_mask);
1415 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1416 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1418 /* construct new value and store */
1419 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1420 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1421 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1422 set_store(store_mem);
1425 static void set_value_for_expression(const expression_t *expression,
1428 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1429 value = do_strict_conv(dbgi, value);
1431 if(expression->kind == EXPR_REFERENCE) {
1432 const reference_expression_t *ref = &expression->reference;
1434 declaration_t *declaration = ref->declaration;
1435 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1436 if(declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1437 set_value(declaration->v.value_number, value);
1442 if(expression->kind == EXPR_UNARY_BITFIELD_EXTRACT) {
1443 bitfield_store_to_firm(&expression->unary, value);
1447 ir_node *addr = expression_to_addr(expression);
1448 type_t *type = skip_typeref(expression->base.datatype);
1449 assign_value(dbgi, addr, type, value);
1452 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1454 ir_mode *value_mode = get_irn_mode(value);
1456 if (value_mode == dest_mode || is_Bad(value))
1459 if(dest_mode == mode_b) {
1460 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1461 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1462 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1466 return new_d_Conv(dbgi, value, dest_mode);
1469 static ir_node *create_incdec(const unary_expression_t *expression)
1471 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1472 type_t *type = skip_typeref(expression->expression.datatype);
1473 ir_mode *mode = get_ir_mode(type);
1474 expression_t *value = expression->value;
1476 ir_node *value_node = expression_to_firm(value);
1479 if(is_type_pointer(type)) {
1480 pointer_type_t *pointer_type = &type->pointer;
1481 unsigned elem_size = get_type_size(pointer_type->points_to);
1482 offset = new_Const_long(mode_int, elem_size);
1484 assert(is_type_arithmetic(type));
1485 offset = new_Const(mode, get_mode_one(mode));
1488 switch(expression->expression.kind) {
1489 case EXPR_UNARY_POSTFIX_INCREMENT: {
1490 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1491 set_value_for_expression(value, new_value);
1494 case EXPR_UNARY_POSTFIX_DECREMENT: {
1495 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1496 set_value_for_expression(value, new_value);
1499 case EXPR_UNARY_PREFIX_INCREMENT: {
1500 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1501 set_value_for_expression(value, new_value);
1504 case EXPR_UNARY_PREFIX_DECREMENT: {
1505 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1506 set_value_for_expression(value, new_value);
1510 panic("no incdec expr in create_incdec");
1515 static bool is_local_variable(expression_t *expression)
1517 if (expression->kind != EXPR_REFERENCE)
1519 reference_expression_t *ref_expr = &expression->reference;
1520 declaration_t *declaration = ref_expr->declaration;
1521 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1524 static pn_Cmp get_pnc(const expression_kind_t kind)
1527 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1528 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1529 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Ne;
1530 case EXPR_BINARY_ISLESS:
1531 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1532 case EXPR_BINARY_ISLESSEQUAL:
1533 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1534 case EXPR_BINARY_ISGREATER:
1535 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1536 case EXPR_BINARY_ISGREATEREQUAL:
1537 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1538 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1543 panic("trying to get pn_Cmp from non-comparison binexpr type");
1547 * Handle the assume optimizer hint: check if a Confirm
1548 * node can be created.
1550 * @param dbi debug info
1551 * @param expr the IL assume expression
1553 * we support here only some simple cases:
1558 static ir_node *handle_assume_compare(dbg_info *dbi,
1559 const binary_expression_t *expression)
1561 expression_t *op1 = expression->left;
1562 expression_t *op2 = expression->right;
1563 declaration_t *var2, *var = NULL;
1564 ir_node *res = NULL;
1567 cmp_val = get_pnc(expression->expression.kind);
1569 if (is_local_variable(op1) && is_local_variable(op2)) {
1570 var = op1->reference.declaration;
1571 var2 = op2->reference.declaration;
1573 type_t *const type = skip_typeref(var->type);
1574 ir_mode *const mode = get_ir_mode(type);
1576 ir_node *const irn1 = get_value(var->v.value_number, mode);
1577 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1579 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1580 set_value(var2->v.value_number, res);
1582 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1583 set_value(var->v.value_number, res);
1589 if (is_local_variable(op1) && is_constant_expression(op2)) {
1590 var = op1->reference.declaration;
1592 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1593 cmp_val = get_inversed_pnc(cmp_val);
1594 var = op2->reference.declaration;
1599 type_t *const type = skip_typeref(var->type);
1600 ir_mode *const mode = get_ir_mode(type);
1602 res = get_value(var->v.value_number, mode);
1603 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1604 set_value(var->v.value_number, res);
1610 * Handle the assume optimizer hint.
1612 * @param dbi debug info
1613 * @param expr the IL assume expression
1615 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1616 switch(expression->kind) {
1617 case EXPR_BINARY_EQUAL:
1618 case EXPR_BINARY_NOTEQUAL:
1619 case EXPR_BINARY_LESS:
1620 case EXPR_BINARY_LESSEQUAL:
1621 case EXPR_BINARY_GREATER:
1622 case EXPR_BINARY_GREATEREQUAL:
1623 return handle_assume_compare(dbi, &expression->binary);
1629 static ir_node *bitfield_extract_to_firm(const unary_expression_t *expression)
1631 expression_t *select = expression->value;
1632 assert(select->kind == EXPR_SELECT);
1634 type_t *type = select->base.datatype;
1635 assert(type->kind == TYPE_BITFIELD);
1636 ir_mode *mode = get_ir_mode(type->bitfield.base);
1637 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1638 ir_node *addr = expression_to_addr(select);
1639 ir_node *mem = get_store();
1640 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1641 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1642 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1644 load_res = create_conv(dbgi, load_res, mode_int);
1646 set_store(load_mem);
1648 /* kill upper bits */
1649 ir_entity *entity = select->select.compound_entry->v.entity;
1650 int bitoffset = get_entity_offset_bits_remainder(entity);
1651 ir_type *entity_type = get_entity_type(entity);
1652 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1653 long shift_bitsl = machine_size - bitoffset - bitsize;
1654 assert(shift_bitsl >= 0);
1655 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1656 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1657 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1659 long shift_bitsr = bitoffset + shift_bitsl;
1660 assert(shift_bitsr <= (long) machine_size);
1661 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1662 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1664 if(mode_is_signed(mode)) {
1665 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1667 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1670 return create_conv(dbgi, shiftr, mode);
1673 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1675 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1676 type_t *type = skip_typeref(expression->expression.datatype);
1678 if(expression->expression.kind == EXPR_UNARY_TAKE_ADDRESS)
1679 return expression_to_addr(expression->value);
1681 const expression_t *value = expression->value;
1683 switch(expression->expression.kind) {
1684 case EXPR_UNARY_NEGATE: {
1685 ir_node *value_node = expression_to_firm(value);
1686 ir_mode *mode = get_ir_mode(type);
1687 return new_d_Minus(dbgi, value_node, mode);
1689 case EXPR_UNARY_PLUS:
1690 return expression_to_firm(value);
1691 case EXPR_UNARY_BITWISE_NEGATE: {
1692 ir_node *value_node = expression_to_firm(value);
1693 ir_mode *mode = get_ir_mode(type);
1694 return new_d_Not(dbgi, value_node, mode);
1696 case EXPR_UNARY_NOT: {
1697 ir_node *value_node = expression_to_firm(value);
1698 ir_mode *mode = get_ir_mode(type);
1699 if(get_irn_mode(value_node) != mode_b) {
1700 value_node = create_conv(dbgi, value_node, mode_b);
1702 value_node = new_d_Not(dbgi, value_node, mode_b);
1703 if(mode != mode_b) {
1704 value_node = create_conv(dbgi, value_node, mode);
1708 case EXPR_UNARY_DEREFERENCE: {
1709 ir_node *value_node = expression_to_firm(value);
1710 type_t *value_type = skip_typeref(value->base.datatype);
1711 ir_type *irtype = get_ir_type(value_type);
1712 assert(is_Pointer_type(irtype));
1713 ir_type *points_to = get_pointer_points_to_type(irtype);
1714 return deref_address(points_to, value_node, dbgi);
1716 case EXPR_UNARY_POSTFIX_INCREMENT:
1717 case EXPR_UNARY_POSTFIX_DECREMENT:
1718 case EXPR_UNARY_PREFIX_INCREMENT:
1719 case EXPR_UNARY_PREFIX_DECREMENT:
1720 return create_incdec(expression);
1721 case EXPR_UNARY_CAST: {
1722 ir_node *value_node = expression_to_firm(value);
1723 ir_mode *mode = get_ir_mode(type);
1724 ir_node *node = create_conv(dbgi, value_node, mode);
1725 node = do_strict_conv(dbgi, node);
1728 case EXPR_UNARY_CAST_IMPLICIT: {
1729 ir_node *value_node = expression_to_firm(value);
1730 ir_mode *mode = get_ir_mode(type);
1731 return create_conv(dbgi, value_node, mode);
1733 case EXPR_UNARY_ASSUME:
1734 if(firm_opt.confirm)
1735 return handle_assume(dbgi, value);
1738 case EXPR_UNARY_BITFIELD_EXTRACT:
1739 return bitfield_extract_to_firm(expression);
1744 panic("invalid UNEXPR type found");
1747 static ir_node *create_lazy_op(const binary_expression_t *expression)
1749 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1750 type_t *type = expression->expression.datatype;
1751 ir_mode *mode = get_ir_mode(type);
1753 if(is_constant_expression(expression->left)) {
1754 long val = fold_constant(expression->left);
1755 expression_kind_t ekind = expression->expression.kind;
1756 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
1757 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1758 return expression_to_firm(expression->right);
1760 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
1761 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
1762 return new_Const(mode, get_mode_one(mode));
1766 ir_node *cur_block = get_cur_block();
1768 ir_node *one_block = new_immBlock();
1769 ir_node *one = new_Const(mode, get_mode_one(mode));
1770 ir_node *jmp_one = new_d_Jmp(dbgi);
1772 ir_node *zero_block = new_immBlock();
1773 ir_node *zero = new_Const(mode, get_mode_null(mode));
1774 ir_node *jmp_zero = new_d_Jmp(dbgi);
1776 set_cur_block(cur_block);
1777 create_condition_evaluation((const expression_t*) expression,
1778 one_block, zero_block);
1779 mature_immBlock(one_block);
1780 mature_immBlock(zero_block);
1782 ir_node *common_block = new_immBlock();
1783 add_immBlock_pred(common_block, jmp_one);
1784 add_immBlock_pred(common_block, jmp_zero);
1785 mature_immBlock(common_block);
1787 ir_node *in[2] = { one, zero };
1788 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1793 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1794 ir_node *right, ir_mode *mode);
1796 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1797 create_arithmetic_func func)
1799 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1800 ir_node *left = expression_to_firm(expression->left);
1801 ir_node *right = expression_to_firm(expression->right);
1802 type_t *type = expression->right->base.datatype;
1803 /* be careful with the modes, because in arithmetic assign nodes only
1804 * the right operand has the mode of the arithmetic already */
1805 ir_mode *mode = get_ir_mode(type);
1806 left = create_conv(dbgi, left, mode);
1807 ir_node *res = func(dbgi, left, right, mode);
1812 static ir_node *pointer_arithmetic(ir_node *const pointer,
1815 dbg_info *const dbgi,
1816 const create_arithmetic_func func)
1818 pointer_type_t *const pointer_type = &type->pointer;
1819 type_t *const points_to = pointer_type->points_to;
1820 const unsigned elem_size = get_type_size(points_to);
1822 assert(elem_size >= 1);
1823 if (elem_size > 1) {
1824 integer = create_conv(dbgi, integer, mode_int);
1825 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1826 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1830 ir_mode *const mode = get_ir_mode(type);
1831 return func(dbgi, pointer, integer, mode);
1834 static ir_node *create_arithmetic_assign_binop(
1835 const binary_expression_t *expression, create_arithmetic_func func)
1837 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1838 type_t *const type = skip_typeref(expression->expression.datatype);
1841 if (is_type_pointer(type)) {
1842 ir_node *const pointer = expression_to_firm(expression->left);
1843 ir_node * integer = expression_to_firm(expression->right);
1844 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1846 value = create_arithmetic_binop(expression, func);
1849 ir_mode *const mode = get_ir_mode(type);
1850 value = create_conv(dbgi, value, mode);
1851 set_value_for_expression(expression->left, value);
1856 static ir_node *create_add(const binary_expression_t *expression)
1858 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1859 ir_node *left = expression_to_firm(expression->left);
1860 ir_node *right = expression_to_firm(expression->right);
1861 type_t *type = expression->expression.datatype;
1863 expression_t *expr_left = expression->left;
1864 expression_t *expr_right = expression->right;
1865 type_t *type_left = skip_typeref(expr_left->base.datatype);
1866 type_t *type_right = skip_typeref(expr_right->base.datatype);
1868 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1869 ir_mode *const mode = get_ir_mode(type);
1870 return new_d_Add(dbgi, left, right, mode);
1873 if (is_type_pointer(type_left)) {
1874 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1876 assert(is_type_pointer(type_right));
1877 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1881 static ir_node *create_sub(const binary_expression_t *expression)
1883 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1884 expression_t *const expr_left = expression->left;
1885 expression_t *const expr_right = expression->right;
1886 ir_node *const left = expression_to_firm(expr_left);
1887 ir_node *const right = expression_to_firm(expr_right);
1888 type_t *const type = expression->expression.datatype;
1889 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1890 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1892 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1893 ir_mode *const mode = get_ir_mode(type);
1894 return new_d_Sub(dbgi, left, right, mode);
1895 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1896 const pointer_type_t *const ptr_type = &type_left->pointer;
1897 const unsigned elem_size = get_type_size(ptr_type->points_to);
1898 ir_mode *const mode = get_ir_mode(type);
1899 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1900 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1901 ir_node *const no_mem = new_NoMem();
1902 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1903 op_pin_state_floats);
1904 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1907 assert(is_type_pointer(type_left));
1908 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1911 static ir_node *create_shift(const binary_expression_t *expression)
1913 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1914 ir_node *left = expression_to_firm(expression->left);
1915 ir_node *right = expression_to_firm(expression->right);
1916 type_t *type = expression->expression.datatype;
1917 ir_mode *mode = get_ir_mode(type);
1919 /* firm always wants the shift count to be unsigned */
1920 right = create_conv(dbgi, right, mode_uint);
1924 switch(expression->expression.kind) {
1925 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1926 case EXPR_BINARY_SHIFTLEFT:
1927 res = new_d_Shl(dbgi, left, right, mode);
1929 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1930 case EXPR_BINARY_SHIFTRIGHT: {
1931 expression_t *expr_left = expression->left;
1932 type_t *type_left = skip_typeref(expr_left->base.datatype);
1934 if(is_type_signed(type_left)) {
1935 res = new_d_Shrs(dbgi, left, right, mode);
1937 res = new_d_Shr(dbgi, left, right, mode);
1942 panic("create shift op called for non-shift op");
1949 static ir_node *create_divmod(const binary_expression_t *expression)
1951 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1952 ir_node *left = expression_to_firm(expression->left);
1953 ir_node *right = expression_to_firm(expression->right);
1954 ir_node *pin = new_Pin(new_NoMem());
1955 /* be careful with the modes, because in arithmetic assign nodes only
1956 * the right operand has the mode of the arithmetic already */
1957 type_t *type = expression->right->base.datatype;
1958 ir_mode *mode = get_ir_mode(type);
1959 left = create_conv(dbgi, left, mode);
1963 switch (expression->expression.kind) {
1964 case EXPR_BINARY_DIV:
1965 case EXPR_BINARY_DIV_ASSIGN:
1966 if(mode_is_float(mode)) {
1967 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1968 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1970 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1971 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1975 case EXPR_BINARY_MOD:
1976 case EXPR_BINARY_MOD_ASSIGN:
1977 assert(!mode_is_float(mode));
1978 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1979 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1982 default: panic("unexpected binary expression type in create_divmod()");
1988 static ir_node *create_arithmetic_assign_divmod(
1989 const binary_expression_t *expression)
1991 ir_node * value = create_divmod(expression);
1992 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1993 type_t *const type = expression->expression.datatype;
1994 ir_mode *const mode = get_ir_mode(type);
1996 assert(type->kind != TYPE_POINTER);
1998 value = create_conv(dbgi, value, mode);
1999 set_value_for_expression(expression->left, value);
2004 static ir_node *create_arithmetic_assign_shift(
2005 const binary_expression_t *expression)
2007 ir_node * value = create_shift(expression);
2008 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
2009 type_t *const type = expression->expression.datatype;
2010 ir_mode *const mode = get_ir_mode(type);
2012 value = create_conv(dbgi, value, mode);
2013 set_value_for_expression(expression->left, value);
2018 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2020 expression_kind_t kind = expression->expression.kind;
2023 case EXPR_BINARY_EQUAL:
2024 case EXPR_BINARY_NOTEQUAL:
2025 case EXPR_BINARY_LESS:
2026 case EXPR_BINARY_LESSEQUAL:
2027 case EXPR_BINARY_GREATER:
2028 case EXPR_BINARY_GREATEREQUAL:
2029 case EXPR_BINARY_ISGREATER:
2030 case EXPR_BINARY_ISGREATEREQUAL:
2031 case EXPR_BINARY_ISLESS:
2032 case EXPR_BINARY_ISLESSEQUAL:
2033 case EXPR_BINARY_ISLESSGREATER:
2034 case EXPR_BINARY_ISUNORDERED: {
2035 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2036 ir_node *left = expression_to_firm(expression->left);
2037 ir_node *right = expression_to_firm(expression->right);
2038 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2039 long pnc = get_pnc(kind);
2040 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2043 case EXPR_BINARY_ASSIGN: {
2044 ir_node *right = expression_to_firm(expression->right);
2045 set_value_for_expression(expression->left, right);
2049 case EXPR_BINARY_ADD:
2050 return create_add(expression);
2051 case EXPR_BINARY_SUB:
2052 return create_sub(expression);
2053 case EXPR_BINARY_MUL:
2054 return create_arithmetic_binop(expression, new_d_Mul);
2055 case EXPR_BINARY_BITWISE_AND:
2056 return create_arithmetic_binop(expression, new_d_And);
2057 case EXPR_BINARY_BITWISE_OR:
2058 return create_arithmetic_binop(expression, new_d_Or);
2059 case EXPR_BINARY_BITWISE_XOR:
2060 return create_arithmetic_binop(expression, new_d_Eor);
2061 case EXPR_BINARY_SHIFTLEFT:
2062 case EXPR_BINARY_SHIFTRIGHT:
2063 return create_shift(expression);
2064 case EXPR_BINARY_DIV:
2065 case EXPR_BINARY_MOD:
2066 return create_divmod(expression);
2067 case EXPR_BINARY_LOGICAL_AND:
2068 case EXPR_BINARY_LOGICAL_OR:
2069 return create_lazy_op(expression);
2070 case EXPR_BINARY_COMMA:
2071 expression_to_firm(expression->left);
2072 return expression_to_firm(expression->right);
2073 case EXPR_BINARY_ADD_ASSIGN:
2074 return create_arithmetic_assign_binop(expression, new_d_Add);
2075 case EXPR_BINARY_SUB_ASSIGN:
2076 return create_arithmetic_assign_binop(expression, new_d_Sub);
2077 case EXPR_BINARY_MUL_ASSIGN:
2078 return create_arithmetic_assign_binop(expression, new_d_Mul);
2079 case EXPR_BINARY_DIV_ASSIGN:
2080 return create_arithmetic_assign_divmod(expression);
2081 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2082 return create_arithmetic_assign_binop(expression, new_d_And);
2083 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2084 return create_arithmetic_assign_binop(expression, new_d_Or);
2085 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2086 return create_arithmetic_assign_binop(expression, new_d_Eor);
2087 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2088 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2089 return create_arithmetic_assign_shift(expression);
2090 case EXPR_BINARY_BUILTIN_EXPECT:
2091 return expression_to_firm(expression->left);
2093 panic("TODO binexpr type");
2097 static ir_node *array_access_addr(const array_access_expression_t *expression)
2099 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2100 ir_node *base_addr = expression_to_firm(expression->array_ref);
2101 ir_node *offset = expression_to_firm(expression->index);
2102 offset = create_conv(dbgi, offset, mode_uint);
2104 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
2105 assert(is_type_pointer(ref_type));
2106 pointer_type_t *pointer_type = &ref_type->pointer;
2108 unsigned elem_size = get_type_size(pointer_type->points_to);
2109 ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
2110 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2112 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2117 static ir_node *array_access_to_firm(
2118 const array_access_expression_t *expression)
2120 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2121 ir_node *addr = array_access_addr(expression);
2122 type_t *type = revert_automatic_type_conversion(
2123 (const expression_t*) expression);
2124 type = skip_typeref(type);
2125 ir_type *irtype = get_ir_type(type);
2127 return deref_address(irtype, addr, dbgi);
2130 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
2132 type_t *type = expression->type;
2134 type = expression->size_expression->base.datatype;
2135 assert(type != NULL);
2138 ir_mode *const mode = get_ir_mode(expression->expression.datatype);
2139 symconst_symbol sym;
2140 sym.type_p = get_ir_type(type);
2141 return new_SymConst(mode, sym, symconst_type_size);
2144 static ir_node *alignof_to_firm(const alignof_expression_t *expression)
2146 type_t *const type = expression->type;
2147 ir_mode *const mode = get_ir_mode(expression->expression.datatype);
2148 symconst_symbol sym;
2149 sym.type_p = get_ir_type(type);
2150 return new_SymConst(mode, sym, symconst_type_align);
2153 static long fold_constant(const expression_t *expression)
2155 assert(is_constant_expression(expression));
2157 ir_graph *old_current_ir_graph = current_ir_graph;
2158 if(current_ir_graph == NULL) {
2159 current_ir_graph = get_const_code_irg();
2162 ir_node *cnst = expression_to_firm(expression);
2163 current_ir_graph = old_current_ir_graph;
2165 if(!is_Const(cnst)) {
2166 panic("couldn't fold constant\n");
2169 tarval *tv = get_Const_tarval(cnst);
2170 if(!tarval_is_long(tv)) {
2171 panic("result of constant folding is not integer\n");
2174 return get_tarval_long(tv);
2177 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2179 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
2181 /* first try to fold a constant condition */
2182 if(is_constant_expression(expression->condition)) {
2183 long val = fold_constant(expression->condition);
2185 return expression_to_firm(expression->true_expression);
2187 return expression_to_firm(expression->false_expression);
2191 ir_node *cur_block = get_cur_block();
2193 /* create the true block */
2194 ir_node *true_block = new_immBlock();
2196 ir_node *true_val = expression_to_firm(expression->true_expression);
2197 ir_node *true_jmp = new_Jmp();
2199 /* create the false block */
2200 ir_node *false_block = new_immBlock();
2202 ir_node *false_val = expression_to_firm(expression->false_expression);
2203 ir_node *false_jmp = new_Jmp();
2205 /* create the condition evaluation */
2206 set_cur_block(cur_block);
2207 create_condition_evaluation(expression->condition, true_block, false_block);
2208 mature_immBlock(true_block);
2209 mature_immBlock(false_block);
2211 /* create the common block */
2212 ir_node *common_block = new_immBlock();
2213 add_immBlock_pred(common_block, true_jmp);
2214 add_immBlock_pred(common_block, false_jmp);
2215 mature_immBlock(common_block);
2217 /* TODO improve static semantics, so either both or no values are NULL */
2218 if (true_val == NULL || false_val == NULL)
2221 ir_node *in[2] = { true_val, false_val };
2222 ir_mode *mode = get_irn_mode(true_val);
2223 assert(get_irn_mode(false_val) == mode);
2224 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2229 static ir_node *select_addr(const select_expression_t *expression)
2231 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2233 ir_node *compound_addr = expression_to_firm(expression->compound);
2235 declaration_t *entry = expression->compound_entry;
2236 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2237 ir_entity *entity = entry->v.entity;
2239 assert(entity != NULL);
2241 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2246 static ir_node *select_to_firm(const select_expression_t *expression)
2248 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
2249 ir_node *addr = select_addr(expression);
2250 type_t *type = revert_automatic_type_conversion(
2251 (const expression_t*) expression);
2252 type = skip_typeref(type);
2253 ir_type *irtype = get_ir_type(type);
2255 return deref_address(irtype, addr, dbgi);
2258 /* Values returned by __builtin_classify_type. */
2259 typedef enum gcc_type_class
2265 enumeral_type_class,
2268 reference_type_class,
2272 function_type_class,
2283 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2285 const type_t *const type = expr->type_expression->base.datatype;
2291 const atomic_type_t *const atomic_type = &type->atomic;
2292 switch (atomic_type->akind) {
2293 /* should not be reached */
2294 case ATOMIC_TYPE_INVALID:
2298 /* gcc cannot do that */
2299 case ATOMIC_TYPE_VOID:
2300 tc = void_type_class;
2303 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2304 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2305 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2306 case ATOMIC_TYPE_SHORT:
2307 case ATOMIC_TYPE_USHORT:
2308 case ATOMIC_TYPE_INT:
2309 case ATOMIC_TYPE_UINT:
2310 case ATOMIC_TYPE_LONG:
2311 case ATOMIC_TYPE_ULONG:
2312 case ATOMIC_TYPE_LONGLONG:
2313 case ATOMIC_TYPE_ULONGLONG:
2314 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2315 tc = integer_type_class;
2318 case ATOMIC_TYPE_FLOAT:
2319 case ATOMIC_TYPE_DOUBLE:
2320 case ATOMIC_TYPE_LONG_DOUBLE:
2321 tc = real_type_class;
2324 #ifdef PROVIDE_COMPLEX
2325 case ATOMIC_TYPE_FLOAT_COMPLEX:
2326 case ATOMIC_TYPE_DOUBLE_COMPLEX:
2327 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
2328 tc = complex_type_class;
2330 case ATOMIC_TYPE_FLOAT_IMAGINARY:
2331 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
2332 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
2333 tc = complex_type_class;
2338 panic("Unimplemented case in classify_type_to_firm().");
2343 case TYPE_ARRAY: /* gcc handles this as pointer */
2344 case TYPE_FUNCTION: /* gcc handles this as pointer */
2345 case TYPE_POINTER: tc = pointer_type_class; break;
2346 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
2347 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
2349 /* gcc handles this as integer */
2350 case TYPE_ENUM: tc = integer_type_class; break;
2353 panic("Unimplemented case in classify_type_to_firm().");
2356 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
2357 ir_mode *const mode = mode_int;
2358 tarval *const tv = new_tarval_from_long(tc, mode);
2359 return new_d_Const(dbgi, mode, tv);
2362 static ir_node *function_name_to_firm(
2363 const string_literal_expression_t *const expr)
2365 if (current_function_name == NULL) {
2366 const source_position_t *const src_pos =
2367 &expr->expression.source_position;
2368 const char *const name = current_function_decl->symbol->string;
2369 current_function_name = string_to_firm(src_pos, "__func__", name);
2372 return current_function_name;
2375 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2377 statement_t *statement = expr->statement;
2379 assert(statement->kind == STATEMENT_COMPOUND);
2380 return compound_statement_to_firm(&statement->compound);
2383 static ir_node *va_start_expression_to_firm(
2384 const va_start_expression_t *const expr)
2386 ir_type *const method_type = get_ir_type(current_function_decl->type);
2387 int const n = get_method_n_params(method_type) - 1;
2388 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2389 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2390 dbg_info *const dbgi =
2391 get_dbg_info(&expr->expression.source_position);
2392 ir_node *const no_mem = new_NoMem();
2393 ir_node *const arg_sel =
2394 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2396 size_t const parm_size = get_type_size(expr->parameter->type);
2397 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2398 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2399 set_value_for_expression(expr->ap, add);
2404 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2406 ir_type *const irtype = get_ir_type(expr->expression.datatype);
2407 ir_node *const ap = expression_to_firm(expr->ap);
2408 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
2409 ir_node *const res = deref_address(irtype, ap, dbgi);
2411 size_t const parm_size = get_type_size(expr->expression.datatype);
2412 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2413 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2414 set_value_for_expression(expr->ap, add);
2419 static ir_node *dereference_addr(const unary_expression_t *const expression)
2421 assert(expression->expression.kind == EXPR_UNARY_DEREFERENCE);
2422 return expression_to_firm(expression->value);
2425 static ir_node *expression_to_addr(const expression_t *expression)
2427 switch(expression->kind) {
2428 case EXPR_REFERENCE:
2429 return reference_addr(&expression->reference);
2430 case EXPR_ARRAY_ACCESS:
2431 return array_access_addr(&expression->array_access);
2433 return select_addr(&expression->select);
2435 return call_expression_to_firm(&expression->call);
2436 case EXPR_UNARY_DEREFERENCE: {
2437 return dereference_addr(&expression->unary);
2442 panic("trying to get address of non-lvalue");
2445 static ir_node *builtin_constant_to_firm(
2446 const builtin_constant_expression_t *expression)
2448 ir_mode *mode = get_ir_mode(expression->expression.datatype);
2451 if (is_constant_expression(expression->value)) {
2456 return new_Const_long(mode, v);
2459 static ir_node *builtin_prefetch_to_firm(
2460 const builtin_prefetch_expression_t *expression)
2462 ir_node *adr = expression_to_firm(expression->adr);
2463 /* no Firm support for prefetch yet */
2468 static ir_node *_expression_to_firm(const expression_t *expression)
2470 switch(expression->kind) {
2472 return const_to_firm(&expression->conste);
2473 case EXPR_STRING_LITERAL:
2474 return string_literal_to_firm(&expression->string);
2475 case EXPR_WIDE_STRING_LITERAL:
2476 return wide_string_literal_to_firm(&expression->wide_string);
2477 case EXPR_REFERENCE:
2478 return reference_expression_to_firm(&expression->reference);
2480 return call_expression_to_firm(&expression->call);
2482 return unary_expression_to_firm(&expression->unary);
2484 return binary_expression_to_firm(&expression->binary);
2485 case EXPR_ARRAY_ACCESS:
2486 return array_access_to_firm(&expression->array_access);
2488 return sizeof_to_firm(&expression->sizeofe);
2490 return alignof_to_firm(&expression->alignofe);
2491 case EXPR_CONDITIONAL:
2492 return conditional_to_firm(&expression->conditional);
2494 return select_to_firm(&expression->select);
2495 case EXPR_CLASSIFY_TYPE:
2496 return classify_type_to_firm(&expression->classify_type);
2498 case EXPR_PRETTY_FUNCTION:
2499 return function_name_to_firm(&expression->string);
2500 case EXPR_STATEMENT:
2501 return statement_expression_to_firm(&expression->statement);
2503 return va_start_expression_to_firm(&expression->va_starte);
2505 return va_arg_expression_to_firm(&expression->va_arge);
2507 case EXPR_BUILTIN_SYMBOL:
2508 panic("unimplemented expression found");
2509 case EXPR_BUILTIN_CONSTANT_P:
2510 return builtin_constant_to_firm(&expression->builtin_constant);
2511 case EXPR_BUILTIN_PREFETCH:
2512 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2518 panic("invalid expression found");
2521 static ir_node *expression_to_firm(const expression_t *expression)
2523 ir_node *res = _expression_to_firm(expression);
2525 if(res != NULL && get_irn_mode(res) == mode_b) {
2526 ir_mode *mode = get_ir_mode(expression->base.datatype);
2527 res = create_conv(NULL, res, mode);
2533 static ir_node *expression_to_modeb(const expression_t *expression)
2535 ir_node *res = _expression_to_firm(expression);
2536 res = create_conv(NULL, res, mode_b);
2542 * create a short-circuit expression evaluation that tries to construct
2543 * efficient control flow structures for &&, || and ! expressions
2545 static void create_condition_evaluation(const expression_t *expression,
2546 ir_node *true_block,
2547 ir_node *false_block)
2549 switch(expression->kind) {
2550 case EXPR_UNARY_NOT: {
2551 const unary_expression_t *unary_expression = &expression->unary;
2552 create_condition_evaluation(unary_expression->value, false_block,
2556 case EXPR_BINARY_LOGICAL_AND: {
2557 const binary_expression_t *binary_expression = &expression->binary;
2559 ir_node *cur_block = get_cur_block();
2560 ir_node *extra_block = new_immBlock();
2561 set_cur_block(cur_block);
2562 create_condition_evaluation(binary_expression->left, extra_block,
2564 mature_immBlock(extra_block);
2565 set_cur_block(extra_block);
2566 create_condition_evaluation(binary_expression->right, true_block,
2570 case EXPR_BINARY_LOGICAL_OR: {
2571 const binary_expression_t *binary_expression = &expression->binary;
2573 ir_node *cur_block = get_cur_block();
2574 ir_node *extra_block = new_immBlock();
2575 set_cur_block(cur_block);
2576 create_condition_evaluation(binary_expression->left, true_block,
2578 mature_immBlock(extra_block);
2579 set_cur_block(extra_block);
2580 create_condition_evaluation(binary_expression->right, true_block,
2588 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2589 ir_node *condition = expression_to_modeb(expression);
2590 ir_node *cond = new_d_Cond(dbgi, condition);
2591 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2592 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2594 /* set branch prediction info based on __builtin_expect */
2595 if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
2596 long cnst = fold_constant(expression->binary.right);
2597 cond_jmp_predicate pred;
2600 pred = COND_JMP_PRED_FALSE;
2602 pred = COND_JMP_PRED_TRUE;
2604 set_Cond_jmp_pred(cond, pred);
2607 add_immBlock_pred(true_block, true_proj);
2608 add_immBlock_pred(false_block, false_proj);
2610 set_cur_block(NULL);
2615 static void create_declaration_entity(declaration_t *declaration,
2616 declaration_kind_t declaration_kind,
2617 ir_type *parent_type)
2619 ident *const id = new_id_from_str(declaration->symbol->string);
2620 ir_type *const irtype = get_ir_type(declaration->type);
2621 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2622 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2623 set_entity_ld_ident(entity, id);
2625 declaration->declaration_kind = (unsigned char) declaration_kind;
2626 declaration->v.entity = entity;
2627 set_entity_variability(entity, variability_uninitialized);
2628 if(parent_type == get_tls_type())
2629 set_entity_allocation(entity, allocation_automatic);
2630 else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
2631 set_entity_allocation(entity, allocation_static);
2632 /* TODO: visibility? */
2635 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2637 enum compound_graph_entry_type_t {
2638 COMPOUND_GRAPH_ENTRY_ARRAY,
2639 COMPOUND_GRAPH_ENTRY_COMPOUND
2642 struct compound_graph_path_entry_t {
2648 compound_graph_path_entry_t *prev;
2651 static void create_initializer_object(initializer_t *initializer, type_t *type,
2652 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2654 static compound_graph_path *create_compound_path(ir_type *type,
2655 compound_graph_path_entry_t *entry, int len)
2657 compound_graph_path *path = new_compound_graph_path(type, len);
2660 for( ; entry != NULL; entry = entry->prev, --i) {
2662 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2663 set_compound_graph_path_node(path, i, entry->v.entity);
2665 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2666 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2674 static void create_initializer_value(initializer_value_t *initializer,
2676 compound_graph_path_entry_t *entry,
2679 ir_node *node = expression_to_firm(initializer->value);
2680 ir_type *type = get_entity_type(entity);
2681 compound_graph_path *path = create_compound_path(type, entry, len);
2682 add_compound_ent_value_w_path(entity, node, path);
2685 static void create_initializer_compound(initializer_list_t *initializer,
2686 compound_type_t *type,
2688 compound_graph_path_entry_t *last_entry,
2691 declaration_t *compound_declaration = type->declaration;
2693 declaration_t *compound_entry = compound_declaration->context.declarations;
2695 compound_graph_path_entry_t entry;
2696 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2697 entry.prev = last_entry;
2701 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2702 if(compound_entry->symbol == NULL)
2704 if(compound_entry->namespc != NAMESPACE_NORMAL)
2707 if(i >= initializer->len)
2710 entry.v.entity = compound_entry->v.entity;
2712 initializer_t *sub_initializer = initializer->initializers[i];
2714 assert(compound_entry != NULL);
2715 assert(compound_entry->declaration_kind
2716 == DECLARATION_KIND_COMPOUND_MEMBER);
2718 if(sub_initializer->kind == INITIALIZER_VALUE) {
2719 create_initializer_value(&sub_initializer->value,
2720 entity, &entry, len);
2722 type_t *entry_type = skip_typeref(compound_entry->type);
2723 create_initializer_object(sub_initializer, entry_type, entity,
2731 static void create_initializer_array(initializer_list_t *initializer,
2732 array_type_t *type, ir_entity *entity,
2733 compound_graph_path_entry_t *last_entry,
2736 type_t *element_type = type->element_type;
2737 element_type = skip_typeref(element_type);
2739 compound_graph_path_entry_t entry;
2740 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2741 entry.prev = last_entry;
2745 for(i = 0; i < initializer->len; ++i) {
2746 entry.v.array_index = i;
2748 initializer_t *sub_initializer = initializer->initializers[i];
2750 if(sub_initializer->kind == INITIALIZER_VALUE) {
2751 create_initializer_value(&sub_initializer->value,
2752 entity, &entry, len);
2754 create_initializer_object(sub_initializer, element_type, entity,
2760 /* TODO: initialize rest... */
2761 if(type->size_expression != NULL) {
2762 size_t array_len = fold_constant(type->size_expression);
2763 for( ; i < array_len; ++i) {
2770 static void create_initializer_string(initializer_string_t *initializer,
2771 array_type_t *type, ir_entity *entity,
2772 compound_graph_path_entry_t *last_entry,
2775 type_t *element_type = type->element_type;
2776 element_type = skip_typeref(element_type);
2778 compound_graph_path_entry_t entry;
2779 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2780 entry.prev = last_entry;
2783 ir_type *irtype = get_entity_type(entity);
2784 size_t arr_len = get_array_type_size(type);
2785 const char *p = initializer->string;
2787 for(i = 0; i < arr_len; ++i, ++p) {
2788 entry.v.array_index = i;
2790 ir_node *node = new_Const_long(mode_Bs, *p);
2791 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2792 add_compound_ent_value_w_path(entity, node, path);
2799 static void create_initializer_wide_string(
2800 const initializer_wide_string_t *const initializer, array_type_t *const type,
2801 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2804 type_t *element_type = type->element_type;
2805 element_type = skip_typeref(element_type);
2807 compound_graph_path_entry_t entry;
2808 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2809 entry.prev = last_entry;
2812 ir_type *const irtype = get_entity_type(entity);
2813 const size_t arr_len = get_array_type_size(type);
2814 const wchar_rep_t * p = initializer->string.begin;
2815 const wchar_rep_t *const end = p + initializer->string.size;
2816 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2817 entry.v.array_index = i;
2819 ir_node *node = new_Const_long(mode_int, *p);
2820 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2821 add_compound_ent_value_w_path(entity, node, path);
2825 static void create_initializer_object(initializer_t *initializer, type_t *type,
2826 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2828 if(is_type_array(type)) {
2829 array_type_t *array_type = &type->array;
2831 switch (initializer->kind) {
2832 case INITIALIZER_STRING: {
2833 initializer_string_t *const string = &initializer->string;
2834 create_initializer_string(string, array_type, entity, entry, len);
2838 case INITIALIZER_WIDE_STRING: {
2839 initializer_wide_string_t *const string = &initializer->wide_string;
2840 create_initializer_wide_string(string, array_type, entity, entry, len);
2844 case INITIALIZER_LIST: {
2845 initializer_list_t *const list = &initializer->list;
2846 create_initializer_array(list, array_type, entity, entry, len);
2850 case INITIALIZER_VALUE:
2853 panic("Unhandled initializer");
2855 assert(initializer->kind == INITIALIZER_LIST);
2856 initializer_list_t *list = &initializer->list;
2858 assert(is_type_compound(type));
2859 compound_type_t *compound_type = &type->compound;
2860 create_initializer_compound(list, compound_type, entity, entry, len);
2864 static void create_initializer_local_variable_entity(declaration_t *declaration)
2866 initializer_t *initializer = declaration->init.initializer;
2867 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2868 ir_entity *entity = declaration->v.entity;
2869 ir_node *memory = get_store();
2870 ir_node *nomem = new_NoMem();
2871 ir_node *frame = get_irg_frame(current_ir_graph);
2872 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2874 if(initializer->kind == INITIALIZER_VALUE) {
2875 initializer_value_t *initializer_value = &initializer->value;
2877 ir_node *value = expression_to_firm(initializer_value->value);
2878 type_t *type = skip_typeref(declaration->type);
2879 assign_value(dbgi, addr, type, value);
2883 /* create a "template" entity which is copied to the entity on the stack */
2884 ident *const id = unique_ident("initializer");
2885 ir_type *const irtype = get_ir_type(declaration->type);
2886 ir_type *const global_type = get_glob_type();
2887 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
2888 set_entity_ld_ident(init_entity, id);
2890 set_entity_variability(init_entity, variability_initialized);
2891 set_entity_visibility(init_entity, visibility_local);
2892 set_entity_allocation(init_entity, allocation_static);
2894 ir_graph *const old_current_ir_graph = current_ir_graph;
2895 current_ir_graph = get_const_code_irg();
2897 type_t *const type = skip_typeref(declaration->type);
2898 create_initializer_object(initializer, type, init_entity, NULL, 0);
2900 assert(current_ir_graph == get_const_code_irg());
2901 current_ir_graph = old_current_ir_graph;
2903 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
2904 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2906 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2907 set_store(copyb_mem);
2910 static void create_initializer(declaration_t *declaration)
2912 initializer_t *initializer = declaration->init.initializer;
2913 if(initializer == NULL)
2916 declaration_kind_t declaration_kind
2917 = (declaration_kind_t) declaration->declaration_kind;
2918 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
2919 create_initializer_local_variable_entity(declaration);
2923 if(initializer->kind == INITIALIZER_VALUE) {
2924 initializer_value_t *initializer_value = &initializer->value;
2926 ir_node *value = expression_to_firm(initializer_value->value);
2928 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2929 set_value(declaration->v.value_number, value);
2931 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
2933 ir_entity *entity = declaration->v.entity;
2935 set_entity_variability(entity, variability_initialized);
2936 set_atomic_ent_value(entity, value);
2939 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
2940 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
2942 ir_entity *entity = declaration->v.entity;
2943 set_entity_variability(entity, variability_initialized);
2945 type_t *type = skip_typeref(declaration->type);
2946 create_initializer_object(initializer, type, entity, NULL, 0);
2951 * Creates a Firm local variable from a declaration.
2953 static void create_local_variable(declaration_t *declaration)
2955 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
2957 bool needs_entity = declaration->address_taken;
2958 type_t *type = skip_typeref(declaration->type);
2960 if(is_type_array(type) || is_type_compound(type)) {
2961 needs_entity = true;
2965 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2966 create_declaration_entity(declaration,
2967 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
2970 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
2971 declaration->v.value_number = next_value_number_function;
2972 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
2973 ++next_value_number_function;
2976 create_initializer(declaration);
2979 static void create_local_static_variable(declaration_t *declaration)
2981 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
2983 type_t *const type = skip_typeref(declaration->type);
2984 ir_type *const global_type = get_glob_type();
2985 ident *const id = unique_ident(declaration->symbol->string);
2986 ir_type *const irtype = get_ir_type(type);
2987 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2988 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
2989 set_entity_ld_ident(entity, id);
2991 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
2992 declaration->v.entity = entity;
2993 set_entity_variability(entity, variability_uninitialized);
2994 set_entity_visibility(entity, visibility_local);
2995 set_entity_allocation(entity, allocation_static);
2997 ir_graph *const old_current_ir_graph = current_ir_graph;
2998 current_ir_graph = get_const_code_irg();
3000 create_initializer(declaration);
3002 assert(current_ir_graph == get_const_code_irg());
3003 current_ir_graph = old_current_ir_graph;
3008 static void return_statement_to_firm(return_statement_t *statement)
3010 if(get_cur_block() == NULL)
3013 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3015 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
3019 if(get_method_n_ress(func_irtype) > 0) {
3020 ir_type *res_type = get_method_res_type(func_irtype, 0);
3022 if(statement->return_value != NULL) {
3023 ir_node *node = expression_to_firm(statement->return_value);
3024 node = do_strict_conv(dbgi, node);
3028 if(is_compound_type(res_type)) {
3031 mode = get_type_mode(res_type);
3033 in[0] = new_Unknown(mode);
3037 /* build return_value for its side effects */
3038 if(statement->return_value != NULL) {
3039 expression_to_firm(statement->return_value);
3044 ir_node *store = get_store();
3045 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3047 ir_node *end_block = get_irg_end_block(current_ir_graph);
3048 add_immBlock_pred(end_block, ret);
3050 set_cur_block(NULL);
3053 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3055 if(get_cur_block() == NULL)
3058 return expression_to_firm(statement->expression);
3061 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3063 ir_node *result = NULL;
3064 statement_t *statement = compound->statements;
3065 for( ; statement != NULL; statement = statement->base.next) {
3066 //context2firm(&statement->context);
3068 if(statement->base.next == NULL
3069 && statement->kind == STATEMENT_EXPRESSION) {
3070 result = expression_statement_to_firm(
3071 &statement->expression);
3074 statement_to_firm(statement);
3080 static void create_local_declaration(declaration_t *declaration)
3082 type_t *type = skip_typeref(declaration->type);
3084 switch ((storage_class_tag_t) declaration->storage_class) {
3085 case STORAGE_CLASS_STATIC:
3086 create_local_static_variable(declaration);
3088 case STORAGE_CLASS_ENUM_ENTRY:
3089 panic("enum entry declaration in local block found");
3090 case STORAGE_CLASS_EXTERN:
3091 panic("extern declaration in local block found");
3092 case STORAGE_CLASS_NONE:
3093 case STORAGE_CLASS_AUTO:
3094 case STORAGE_CLASS_REGISTER:
3095 if(is_type_function(type)) {
3096 panic("nested functions not supported yet");
3098 create_local_variable(declaration);
3101 case STORAGE_CLASS_TYPEDEF:
3102 case STORAGE_CLASS_THREAD:
3103 case STORAGE_CLASS_THREAD_EXTERN:
3104 case STORAGE_CLASS_THREAD_STATIC:
3107 panic("invalid storage class found");
3110 static void declaration_statement_to_firm(declaration_statement_t *statement)
3112 declaration_t *declaration = statement->declarations_begin;
3113 declaration_t *end = statement->declarations_end->next;
3114 for( ; declaration != end; declaration = declaration->next) {
3115 if(declaration->namespc != NAMESPACE_NORMAL)
3117 create_local_variable(declaration);
3121 static void if_statement_to_firm(if_statement_t *statement)
3123 ir_node *cur_block = get_cur_block();
3125 ir_node *fallthrough_block = new_immBlock();
3127 /* the true (blocks) */
3128 ir_node *true_block;
3129 if (statement->true_statement != NULL) {
3130 true_block = new_immBlock();
3131 statement_to_firm(statement->true_statement);
3132 if(get_cur_block() != NULL) {
3133 ir_node *jmp = new_Jmp();
3134 add_immBlock_pred(fallthrough_block, jmp);
3137 true_block = fallthrough_block;
3140 /* the false (blocks) */
3141 ir_node *false_block;
3142 if(statement->false_statement != NULL) {
3143 false_block = new_immBlock();
3145 statement_to_firm(statement->false_statement);
3146 if(get_cur_block() != NULL) {
3147 ir_node *jmp = new_Jmp();
3148 add_immBlock_pred(fallthrough_block, jmp);
3151 false_block = fallthrough_block;
3154 /* create the condition */
3155 if(cur_block != NULL) {
3156 set_cur_block(cur_block);
3157 create_condition_evaluation(statement->condition, true_block,
3161 mature_immBlock(true_block);
3162 if(false_block != fallthrough_block) {
3163 mature_immBlock(false_block);
3165 mature_immBlock(fallthrough_block);
3167 set_cur_block(fallthrough_block);
3170 static void while_statement_to_firm(while_statement_t *statement)
3172 ir_node *jmp = NULL;
3173 if(get_cur_block() != NULL) {
3177 /* create the header block */
3178 ir_node *header_block = new_immBlock();
3180 add_immBlock_pred(header_block, jmp);
3183 /* the false block */
3184 ir_node *false_block = new_immBlock();
3187 ir_node *body_block;
3188 if (statement->body != NULL) {
3189 ir_node *old_continue_label = continue_label;
3190 ir_node *old_break_label = break_label;
3191 continue_label = header_block;
3192 break_label = false_block;
3194 body_block = new_immBlock();
3195 statement_to_firm(statement->body);
3197 assert(continue_label == header_block);
3198 assert(break_label == false_block);
3199 continue_label = old_continue_label;
3200 break_label = old_break_label;
3202 if(get_cur_block() != NULL) {
3204 add_immBlock_pred(header_block, jmp);
3207 body_block = header_block;
3210 /* create the condition */
3211 set_cur_block(header_block);
3213 create_condition_evaluation(statement->condition, body_block, false_block);
3214 mature_immBlock(body_block);
3215 mature_immBlock(false_block);
3216 mature_immBlock(header_block);
3218 set_cur_block(false_block);
3221 static void do_while_statement_to_firm(do_while_statement_t *statement)
3223 ir_node *jmp = NULL;
3224 if(get_cur_block() != NULL) {
3228 /* create the header block */
3229 ir_node *header_block = new_immBlock();
3231 /* the false block */
3232 ir_node *false_block = new_immBlock();
3235 ir_node *body_block = new_immBlock();
3237 add_immBlock_pred(body_block, jmp);
3240 if (statement->body != NULL) {
3241 ir_node *old_continue_label = continue_label;
3242 ir_node *old_break_label = break_label;
3243 continue_label = header_block;
3244 break_label = false_block;
3246 statement_to_firm(statement->body);
3248 assert(continue_label == header_block);
3249 assert(break_label == false_block);
3250 continue_label = old_continue_label;
3251 break_label = old_break_label;
3253 if (get_cur_block() == NULL) {
3254 mature_immBlock(header_block);
3255 mature_immBlock(body_block);
3256 mature_immBlock(false_block);
3261 ir_node *body_jmp = new_Jmp();
3262 add_immBlock_pred(header_block, body_jmp);
3263 mature_immBlock(header_block);
3265 /* create the condition */
3266 set_cur_block(header_block);
3268 create_condition_evaluation(statement->condition, body_block, false_block);
3269 mature_immBlock(body_block);
3270 mature_immBlock(false_block);
3271 mature_immBlock(header_block);
3273 set_cur_block(false_block);
3276 static void for_statement_to_firm(for_statement_t *statement)
3278 ir_node *jmp = NULL;
3279 if (get_cur_block() != NULL) {
3280 if(statement->initialisation != NULL) {
3281 expression_to_firm(statement->initialisation);
3284 /* create declarations */
3285 declaration_t *declaration = statement->context.declarations;
3286 for( ; declaration != NULL; declaration = declaration->next) {
3287 create_local_declaration(declaration);
3294 /* create the step block */
3295 ir_node *const step_block = new_immBlock();
3296 if (statement->step != NULL) {
3297 expression_to_firm(statement->step);
3299 ir_node *const step_jmp = new_Jmp();
3301 /* create the header block */
3302 ir_node *const header_block = new_immBlock();
3304 add_immBlock_pred(header_block, jmp);
3306 add_immBlock_pred(header_block, step_jmp);
3308 /* the false block */
3309 ir_node *const false_block = new_immBlock();
3312 ir_node * body_block;
3313 if (statement->body != NULL) {
3314 ir_node *const old_continue_label = continue_label;
3315 ir_node *const old_break_label = break_label;
3316 continue_label = step_block;
3317 break_label = false_block;
3319 body_block = new_immBlock();
3320 statement_to_firm(statement->body);
3322 assert(continue_label == step_block);
3323 assert(break_label == false_block);
3324 continue_label = old_continue_label;
3325 break_label = old_break_label;
3327 if (get_cur_block() != NULL) {
3329 add_immBlock_pred(step_block, jmp);
3332 body_block = step_block;
3335 /* create the condition */
3336 set_cur_block(header_block);
3337 if (statement->condition != NULL) {
3338 create_condition_evaluation(statement->condition, body_block,
3341 keep_alive(header_block);
3343 add_immBlock_pred(body_block, jmp);
3346 mature_immBlock(body_block);
3347 mature_immBlock(false_block);
3348 mature_immBlock(step_block);
3349 mature_immBlock(header_block);
3350 mature_immBlock(false_block);
3352 set_cur_block(false_block);
3355 static void create_jump_statement(const statement_t *statement,
3356 ir_node *target_block)
3358 if(get_cur_block() == NULL)
3361 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3362 ir_node *jump = new_d_Jmp(dbgi);
3363 add_immBlock_pred(target_block, jump);
3365 set_cur_block(NULL);
3368 static void switch_statement_to_firm(const switch_statement_t *statement)
3370 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
3372 ir_node *expression = expression_to_firm(statement->expression);
3373 ir_node *cond = new_d_Cond(dbgi, expression);
3374 ir_node *break_block = new_immBlock();
3376 set_cur_block(NULL);
3378 ir_node *const old_switch_cond = current_switch_cond;
3379 ir_node *const old_break_label = break_label;
3380 const bool old_saw_default_label = saw_default_label;
3381 current_switch_cond = cond;
3382 break_label = break_block;
3384 statement_to_firm(statement->body);
3386 if(get_cur_block() != NULL) {
3387 ir_node *jmp = new_Jmp();
3388 add_immBlock_pred(break_block, jmp);
3391 if (!saw_default_label) {
3392 set_cur_block(get_nodes_block(cond));
3393 ir_node *const proj = new_d_defaultProj(dbgi, cond,
3394 MAGIC_DEFAULT_PN_NUMBER);
3395 add_immBlock_pred(break_block, proj);
3398 assert(current_switch_cond == cond);
3399 assert(break_label == break_block);
3400 current_switch_cond = old_switch_cond;
3401 break_label = old_break_label;
3402 saw_default_label = old_saw_default_label;
3404 mature_immBlock(break_block);
3405 set_cur_block(break_block);
3408 static void case_label_to_firm(const case_label_statement_t *statement)
3410 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
3412 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
3414 /* let's create a node and hope firm constant folding creates a Const
3417 set_cur_block(get_nodes_block(current_switch_cond));
3418 if(statement->expression) {
3419 long pn = fold_constant(statement->expression);
3420 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
3421 /* oops someone detected our cheating... */
3422 panic("magic default pn used");
3424 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
3426 saw_default_label = true;
3427 proj = new_d_defaultProj(dbgi, current_switch_cond,
3428 MAGIC_DEFAULT_PN_NUMBER);
3431 ir_node *block = new_immBlock();
3432 if (fallthrough != NULL) {
3433 add_immBlock_pred(block, fallthrough);
3435 add_immBlock_pred(block, proj);
3436 mature_immBlock(block);
3438 if(statement->label_statement != NULL) {
3439 statement_to_firm(statement->label_statement);
3443 static ir_node *get_label_block(declaration_t *label)
3445 assert(label->namespc == NAMESPACE_LABEL);
3447 if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
3448 return label->v.block;
3450 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
3452 ir_node *old_cur_block = get_cur_block();
3453 ir_node *block = new_immBlock();
3454 set_cur_block(old_cur_block);
3456 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
3457 label->v.block = block;
3459 ARR_APP1(ir_node *, imature_blocks, block);
3464 static void label_to_firm(const label_statement_t *statement)
3466 ir_node *block = get_label_block(statement->label);
3468 if(get_cur_block() != NULL) {
3469 ir_node *jmp = new_Jmp();
3470 add_immBlock_pred(block, jmp);
3473 set_cur_block(block);
3476 if(statement->label_statement != NULL) {
3477 statement_to_firm(statement->label_statement);
3481 static void goto_to_firm(const goto_statement_t *statement)
3483 if(get_cur_block() == NULL)
3486 ir_node *block = get_label_block(statement->label);
3487 ir_node *jmp = new_Jmp();
3488 add_immBlock_pred(block, jmp);
3490 set_cur_block(NULL);
3493 typedef enum modifier_t {
3494 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
3495 ASM_MODIFIER_READ_WRITE = 1 << 1,
3496 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
3497 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
3501 static void asm_statement_to_firm(const asm_statement_t *statement)
3503 bool needs_memory = false;
3505 size_t n_clobbers = 0;
3506 asm_clobber_t *clobber = statement->clobbers;
3507 for( ; clobber != NULL; clobber = clobber->next) {
3508 if(strcmp(clobber->clobber, "memory") == 0) {
3509 needs_memory = true;
3513 ident *id = new_id_from_str(clobber->clobber);
3514 obstack_ptr_grow(&asm_obst, id);
3517 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
3518 ident **clobbers = NULL;
3519 if(n_clobbers > 0) {
3520 clobbers = obstack_finish(&asm_obst);
3523 /* find and count input and output constraints */
3524 asm_constraint_t *constraint = statement->inputs;
3525 for( ; constraint != NULL; constraint = constraint->next) {
3527 bool supports_memop = false;
3528 for(const char *c = constraint->constraints; *c != 0; ++c) {
3529 /* TODO: improve error messages */
3533 panic("multiple alternative assembler constraints not "
3541 supports_memop = true;
3542 obstack_1grow(&asm_obst, *c);
3545 if(modifiers & ASM_MODIFIER_READ_WRITE)
3546 panic("inconsistent register constraints");
3547 modifiers |= ASM_MODIFIER_WRITE_ONLY;
3550 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
3551 panic("inconsistent register constraints");
3552 modifiers |= ASM_MODIFIER_READ_WRITE;
3555 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
3556 panic("early clobber assembler constraint not supported yet");
3559 modifiers |= ASM_MODIFIER_COMMUTATIVE;
3560 panic("commutative assembler constraint not supported yet");
3563 /* skip register preferences stuff... */
3564 while(*c != 0 && *c != ',')
3568 /* skip register preferences stuff... */
3572 obstack_1grow(&asm_obst, *c);
3576 obstack_1grow(&asm_obst, '\0');
3577 const char *constraint_string = obstack_finish(&asm_obst);
3579 needs_memory |= supports_memop;
3580 if(supports_memop) {
3588 static void statement_to_firm(statement_t *statement)
3590 switch(statement->kind) {
3591 case STATEMENT_INVALID:
3592 panic("invalid statement found");
3593 case STATEMENT_COMPOUND:
3594 compound_statement_to_firm(&statement->compound);
3596 case STATEMENT_RETURN:
3597 return_statement_to_firm(&statement->returns);
3599 case STATEMENT_EXPRESSION:
3600 expression_statement_to_firm(&statement->expression);
3603 if_statement_to_firm(&statement->ifs);
3605 case STATEMENT_WHILE:
3606 while_statement_to_firm(&statement->whiles);
3608 case STATEMENT_DO_WHILE:
3609 do_while_statement_to_firm(&statement->do_while);
3611 case STATEMENT_DECLARATION:
3612 declaration_statement_to_firm(&statement->declaration);
3614 case STATEMENT_BREAK:
3615 create_jump_statement(statement, break_label);
3617 case STATEMENT_CONTINUE:
3618 create_jump_statement(statement, continue_label);
3620 case STATEMENT_SWITCH:
3621 switch_statement_to_firm(&statement->switchs);
3623 case STATEMENT_CASE_LABEL:
3624 case_label_to_firm(&statement->case_label);
3627 for_statement_to_firm(&statement->fors);
3629 case STATEMENT_LABEL:
3630 label_to_firm(&statement->label);
3632 case STATEMENT_GOTO:
3633 goto_to_firm(&statement->gotos);
3636 //asm_statement_to_firm(&statement->asms);
3640 panic("Statement not implemented\n");
3643 static int count_decls_in_expression(const expression_t *expression);
3645 static int count_local_declarations(const declaration_t * decl,
3646 const declaration_t *const end)
3649 for (; decl != end; decl = decl->next) {
3650 if(decl->namespc != NAMESPACE_NORMAL)
3652 const type_t *type = skip_typeref(decl->type);
3653 if (!decl->address_taken && is_type_scalar(type))
3655 const initializer_t *initializer = decl->init.initializer;
3656 /* FIXME: should walk initializer hierarchies... */
3657 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
3658 count += count_decls_in_expression(initializer->value.value);
3664 static int count_decls_in_expression(const expression_t *expression) {
3665 if(expression == NULL)
3668 switch(expression->base.kind) {
3669 case EXPR_STATEMENT:
3670 return count_decls_in_stmts(expression->statement.statement);
3672 int count_left = count_decls_in_expression(expression->binary.left);
3673 int count_right = count_decls_in_expression(expression->binary.right);
3674 return count_left + count_right;
3677 return count_decls_in_expression(expression->unary.value);
3683 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
3684 * (or implement all the missing expressions here/implement a walker)
3690 static int count_decls_in_stmts(const statement_t *stmt)
3693 for (; stmt != NULL; stmt = stmt->base.next) {
3694 switch (stmt->kind) {
3695 case STATEMENT_DECLARATION: {
3696 const declaration_statement_t *const decl_stmt = &stmt->declaration;
3697 count += count_local_declarations(decl_stmt->declarations_begin,
3698 decl_stmt->declarations_end->next);
3702 case STATEMENT_COMPOUND: {
3703 const compound_statement_t *const comp =
3705 count += count_decls_in_stmts(comp->statements);
3709 case STATEMENT_IF: {
3710 const if_statement_t *const if_stmt = &stmt->ifs;
3711 count += count_decls_in_expression(if_stmt->condition);
3712 count += count_decls_in_stmts(if_stmt->true_statement);
3713 count += count_decls_in_stmts(if_stmt->false_statement);
3717 case STATEMENT_SWITCH: {
3718 const switch_statement_t *const switch_stmt = &stmt->switchs;
3719 count += count_decls_in_expression(switch_stmt->expression);
3720 count += count_decls_in_stmts(switch_stmt->body);
3724 case STATEMENT_LABEL: {
3725 const label_statement_t *const label_stmt = &stmt->label;
3726 count += count_decls_in_stmts(label_stmt->label_statement);
3730 case STATEMENT_WHILE: {
3731 const while_statement_t *const while_stmt = &stmt->whiles;
3732 count += count_decls_in_expression(while_stmt->condition);
3733 count += count_decls_in_stmts(while_stmt->body);
3737 case STATEMENT_DO_WHILE: {
3738 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
3739 count += count_decls_in_expression(do_while_stmt->condition);
3740 count += count_decls_in_stmts(do_while_stmt->body);
3744 case STATEMENT_FOR: {
3745 const for_statement_t *const for_stmt = &stmt->fors;
3746 count += count_local_declarations(for_stmt->context.declarations, NULL);
3747 count += count_decls_in_expression(for_stmt->initialisation);
3748 count += count_decls_in_expression(for_stmt->condition);
3749 count += count_decls_in_expression(for_stmt->step);
3750 count += count_decls_in_stmts(for_stmt->body);
3754 case STATEMENT_CASE_LABEL: {
3755 const case_label_statement_t *label = &stmt->case_label;
3756 count += count_decls_in_expression(label->expression);
3757 count += count_decls_in_stmts(label->label_statement);
3762 case STATEMENT_BREAK:
3763 case STATEMENT_CONTINUE:
3766 case STATEMENT_EXPRESSION: {
3767 const expression_statement_t *expr_stmt = &stmt->expression;
3768 count += count_decls_in_expression(expr_stmt->expression);
3772 case STATEMENT_GOTO:
3773 case STATEMENT_INVALID:
3776 case STATEMENT_RETURN: {
3777 const return_statement_t *ret_stmt = &stmt->returns;
3778 count += count_decls_in_expression(ret_stmt->return_value);
3786 static int get_function_n_local_vars(declaration_t *declaration)
3790 /* count parameters */
3791 count += count_local_declarations(declaration->context.declarations, NULL);
3793 /* count local variables declared in body */
3794 count += count_decls_in_stmts(declaration->init.statement);
3799 static void initialize_function_parameters(declaration_t *declaration)
3801 ir_graph *irg = current_ir_graph;
3802 ir_node *args = get_irg_args(irg);
3803 ir_node *start_block = get_irg_start_block(irg);
3804 ir_type *function_irtype = get_ir_type(declaration->type);
3807 declaration_t *parameter = declaration->context.declarations;
3808 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3809 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
3810 type_t *type = skip_typeref(parameter->type);
3812 bool needs_entity = parameter->address_taken;
3813 assert(!is_type_array(type));
3814 if(is_type_compound(type)) {
3815 needs_entity = true;
3819 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3820 ident *id = new_id_from_str(parameter->symbol->string);
3821 set_entity_ident(entity, id);
3823 parameter->declaration_kind
3824 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
3825 parameter->v.entity = entity;
3829 ir_mode *mode = get_ir_mode(parameter->type);
3831 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3833 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3834 parameter->v.value_number = next_value_number_function;
3835 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
3836 ++next_value_number_function;
3838 set_value(parameter->v.value_number, proj);
3843 * Handle additional decl modifiers for IR-graphs
3845 * @param irg the IR-graph
3846 * @param dec_modifiers additional modifiers
3848 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
3850 if (decl_modifiers & DM_NORETURN) {
3851 /* TRUE if the declaration includes the Microsoft
3852 __declspec(noreturn) specifier. */
3853 set_irg_additional_property(irg, mtp_property_noreturn);
3855 if (decl_modifiers & DM_NOTHROW) {
3856 /* TRUE if the declaration includes the Microsoft
3857 __declspec(nothrow) specifier. */
3858 set_irg_additional_property(irg, mtp_property_nothrow);
3860 if (decl_modifiers & DM_NAKED) {
3861 /* TRUE if the declaration includes the Microsoft
3862 __declspec(naked) specifier. */
3863 set_irg_additional_property(irg, mtp_property_naked);
3865 if (decl_modifiers & DM_FORCEINLINE) {
3866 /* TRUE if the declaration includes the
3867 Microsoft __forceinline specifier. */
3868 set_irg_inline_property(irg, irg_inline_forced);
3870 if (decl_modifiers & DM_NOINLINE) {
3871 /* TRUE if the declaration includes the Microsoft
3872 __declspec(noinline) specifier. */
3873 set_irg_inline_property(irg, irg_inline_forbidden);
3877 static void create_function(declaration_t *declaration)
3879 ir_entity *function_entity = get_function_entity(declaration);
3881 if(declaration->init.statement == NULL)
3884 current_function_decl = declaration;
3885 current_function_name = NULL;
3887 assert(imature_blocks == NULL);
3888 imature_blocks = NEW_ARR_F(ir_node*, 0);
3890 int n_local_vars = get_function_n_local_vars(declaration);
3891 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3892 ir_node *first_block = get_cur_block();
3894 /* set inline flags */
3895 if (declaration->is_inline)
3896 set_irg_inline_property(irg, irg_inline_recomended);
3897 handle_decl_modifier_irg(irg, declaration->modifiers);
3899 next_value_number_function = 0;
3900 initialize_function_parameters(declaration);
3902 statement_to_firm(declaration->init.statement);
3904 ir_node *end_block = get_irg_end_block(irg);
3906 /* do we have a return statement yet? */
3907 if(get_cur_block() != NULL) {
3908 type_t *type = skip_typeref(declaration->type);
3909 assert(is_type_function(type));
3910 const function_type_t *func_type = &type->function;
3911 const type_t *return_type
3912 = skip_typeref(func_type->return_type);
3915 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3916 ret = new_Return(get_store(), 0, NULL);
3919 if(is_type_scalar(return_type)) {
3920 mode = get_ir_mode(func_type->return_type);
3926 /* ยง5.1.2.2.3 main implicitly returns 0 */
3927 if (strcmp(declaration->symbol->string, "main") == 0) {
3928 in[0] = new_Const(mode, get_mode_null(mode));
3930 in[0] = new_Unknown(mode);
3932 ret = new_Return(get_store(), 1, in);
3934 add_immBlock_pred(end_block, ret);
3937 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3938 mature_immBlock(imature_blocks[i]);
3940 DEL_ARR_F(imature_blocks);
3941 imature_blocks = NULL;
3943 mature_immBlock(first_block);
3944 mature_immBlock(end_block);
3946 irg_finalize_cons(irg);
3948 /* finalize the frame type */
3949 ir_type *frame_type = get_irg_frame_type(irg);
3950 int n = get_compound_n_members(frame_type);
3953 for(int i = 0; i < n; ++i) {
3954 ir_entity *entity = get_compound_member(frame_type, i);
3955 ir_type *entity_type = get_entity_type(entity);
3957 int align = get_type_alignment_bytes(entity_type);
3958 if(align > align_all)
3962 misalign = offset % align;
3964 offset += align - misalign;
3968 set_entity_offset(entity, offset);
3969 offset += get_type_size_bytes(entity_type);
3971 set_type_size_bytes(frame_type, offset);
3972 set_type_alignment_bytes(frame_type, align_all);
3973 set_type_state(frame_type, layout_fixed);
3978 static void create_global_variable(declaration_t *declaration)
3982 switch ((storage_class_tag_t)declaration->storage_class) {
3983 case STORAGE_CLASS_STATIC:
3984 vis = visibility_local;
3987 case STORAGE_CLASS_EXTERN:
3988 vis = visibility_external_allocated;
3991 case STORAGE_CLASS_NONE:
3992 vis = visibility_external_visible;
3995 case STORAGE_CLASS_THREAD:
3996 vis = visibility_external_visible;
3999 case STORAGE_CLASS_THREAD_EXTERN:
4000 vis = visibility_external_allocated;
4003 case STORAGE_CLASS_THREAD_STATIC:
4004 vis = visibility_local;
4008 var_type = get_tls_type();
4012 var_type = get_glob_type();
4016 create_declaration_entity(declaration,
4017 DECLARATION_KIND_GLOBAL_VARIABLE,
4019 set_entity_visibility(declaration->v.entity, vis);
4021 current_ir_graph = get_const_code_irg();
4022 create_initializer(declaration);
4025 case STORAGE_CLASS_TYPEDEF:
4026 case STORAGE_CLASS_AUTO:
4027 case STORAGE_CLASS_REGISTER:
4028 case STORAGE_CLASS_ENUM_ENTRY:
4031 panic("Invalid storage class for global variable");
4034 static void context_to_firm(context_t *context)
4036 /* first pass: create declarations */
4037 declaration_t *declaration = context->declarations;
4038 for( ; declaration != NULL; declaration = declaration->next) {
4039 if(declaration->namespc != NAMESPACE_NORMAL)
4041 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4042 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4044 if(declaration->symbol == NULL)
4047 type_t *type = skip_typeref(declaration->type);
4048 if(is_type_function(type)) {
4049 get_function_entity(declaration);
4051 create_global_variable(declaration);
4055 /* second pass: create code */
4056 declaration = context->declarations;
4057 for( ; declaration != NULL; declaration = declaration->next) {
4058 if(declaration->namespc != NAMESPACE_NORMAL)
4060 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4061 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4063 if(declaration->symbol == NULL)
4066 type_t *type = declaration->type;
4067 if(type->kind != TYPE_FUNCTION)
4070 create_function(declaration);
4074 void init_ast2firm(void)
4076 obstack_init(&asm_obst);
4077 init_atomic_modes();
4079 /* create idents for all known runtime functions */
4080 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
4081 predef_idents[rts_data[i].id] = new_id_from_str(rts_data[i].name);
4085 void exit_ast2firm(void)
4087 obstack_free(&asm_obst, NULL);
4090 void translation_unit_to_firm(translation_unit_t *unit)
4092 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4093 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4094 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4096 ir_type_int = get_ir_type(type_int);
4097 ir_type_const_char = get_ir_type(type_const_char);
4098 ir_type_wchar_t = get_ir_type(type_wchar_t);
4099 ir_type_void = get_ir_type(type_int); /* we don't have a real void
4102 type_void->base.firm_type = ir_type_void;
4104 /* just to be sure */
4105 continue_label = NULL;
4107 current_switch_cond = NULL;
4109 context_to_firm(&unit->context);