9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
15 #include "adt/array.h"
20 #include "diagnostic.h"
21 #include "lang_features.h"
22 #include "driver/firm_opt.h"
23 #include "driver/firm_cmdline.h"
25 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
27 static ir_type *ir_type_const_char;
28 static ir_type *ir_type_wchar_t;
29 static ir_type *ir_type_void;
30 static ir_type *ir_type_int;
32 static type_t *type_const_char;
33 static type_t *type_void;
34 static type_t *type_int;
36 static int next_value_number_function;
37 static ir_node *continue_label;
38 static ir_node *break_label;
39 static ir_node *current_switch_cond;
40 static bool saw_default_label;
41 static ir_node **imature_blocks;
43 static const declaration_t *current_function_decl;
44 static ir_node *current_function_name;
46 static struct obstack asm_obst;
48 typedef enum declaration_kind_t {
49 DECLARATION_KIND_UNKNOWN,
50 DECLARATION_KIND_FUNCTION,
51 DECLARATION_KIND_GLOBAL_VARIABLE,
52 DECLARATION_KIND_LOCAL_VARIABLE,
53 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
54 DECLARATION_KIND_COMPOUND_MEMBER,
55 DECLARATION_KIND_LABEL_BLOCK,
56 DECLARATION_KIND_ENUM_ENTRY
59 static ir_type *get_ir_type(type_t *type);
60 static int count_decls_in_stmts(const statement_t *stmt);
62 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
64 const declaration_t *declaration = get_irg_loc_description(irg, pos);
66 warningf(declaration->source_position, "variable '%#T' might be used uninitialized",
67 declaration->type, declaration->symbol);
68 return new_r_Unknown(irg, mode);
71 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
73 const source_position_t *pos = (const source_position_t*) dbg;
76 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
80 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
82 const source_position_t *pos = (const source_position_t*) dbg;
87 return pos->input_name;
90 static dbg_info *get_dbg_info(const source_position_t *pos)
92 return (dbg_info*) pos;
95 static unsigned unique_id = 0;
97 static ident *unique_ident(const char *tag)
101 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
103 return new_id_from_str(buf);
107 * Return the signed integer mode of size bytes.
109 * @param size the size
111 static ir_mode *get_smode(unsigned size)
113 static ir_mode *s_modes[16 + 1] = {0, };
116 if (size <= 0 || size > 16)
125 snprintf(name, sizeof(name), "i%u", bits);
126 res = new_ir_mode(name, irms_int_number, bits, 1, irma_twos_complement,
127 bits <= machine_size ? machine_size : bits );
135 * Return the unsigned integer mode of size bytes.
137 * @param size the size
139 static ir_mode *get_umode(unsigned size)
141 static ir_mode *u_modes[16 + 1] = {0, };
144 if (size <= 0 || size > 16)
153 snprintf(name, sizeof(name), "u%u", bits);
154 res = new_ir_mode(name, irms_int_number, bits, 0, irma_twos_complement,
155 bits <= machine_size ? machine_size : bits );
163 * Return the pointer mode of size bytes.
165 * @param size the size
167 static ir_mode *get_ptrmode(unsigned size, char *name)
169 static ir_mode *p_modes[16 + 1] = {0, };
172 if (size <= 0 || size > 16)
182 snprintf(buf, sizeof(buf), "p%u", bits);
185 res = new_ir_mode(name, irms_reference, bits, 0, irma_twos_complement,
186 bits <= machine_size ? machine_size : bits);
190 set_reference_mode_signed_eq(res, get_smode(size));
191 set_reference_mode_unsigned_eq(res, get_umode(size));
196 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST];
198 static ir_mode *mode_int, *mode_uint;
201 * Initialises the atomic modes depending on the machine size.
203 static void init_atomic_modes(void) {
204 unsigned int_size = machine_size < 32 ? 2 : 4;
205 unsigned long_size = machine_size < 64 ? 4 : 8;
206 unsigned llong_size = machine_size < 32 ? 4 : 8;
208 /* firm has no real void... */
209 _atomic_modes[ATOMIC_TYPE_VOID] = mode_T;
210 _atomic_modes[ATOMIC_TYPE_CHAR] = char_is_signed ? get_smode(1) : get_umode(1);
211 _atomic_modes[ATOMIC_TYPE_SCHAR] = get_smode(1);
212 _atomic_modes[ATOMIC_TYPE_UCHAR] = get_umode(1);
213 _atomic_modes[ATOMIC_TYPE_SHORT] = get_smode(2);
214 _atomic_modes[ATOMIC_TYPE_USHORT] = get_umode(2);
215 _atomic_modes[ATOMIC_TYPE_INT] = get_smode(int_size);
216 _atomic_modes[ATOMIC_TYPE_UINT] = get_umode(int_size);
217 _atomic_modes[ATOMIC_TYPE_LONG] = get_smode(long_size);
218 _atomic_modes[ATOMIC_TYPE_ULONG] = get_umode(long_size);
219 _atomic_modes[ATOMIC_TYPE_LONGLONG] = get_smode(llong_size);
220 _atomic_modes[ATOMIC_TYPE_ULONGLONG] = get_umode(llong_size);
221 _atomic_modes[ATOMIC_TYPE_FLOAT] = mode_F;
222 _atomic_modes[ATOMIC_TYPE_DOUBLE] = mode_D;
223 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = mode_E;
224 _atomic_modes[ATOMIC_TYPE_BOOL] = get_umode(int_size);
226 #ifdef PROVIDE_COMPLEX
227 _atomic_modes[ATOMIC_TYPE_BOOL] = _atomic_modes[ATOMIC_TYPE_INT];
228 _atomic_modes[ATOMIC_TYPE_FLOAT_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_FLOAT];
229 _atomic_modes[ATOMIC_TYPE_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_DOUBLE];
230 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE];
233 /* Hmm, pointers should be machine size */
234 set_modeP_data(get_ptrmode(machine_size >> 3, NULL));
235 set_modeP_code(get_ptrmode(machine_size >> 3, NULL));
237 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
238 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
241 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
244 if ((unsigned)atomic_type->akind < (unsigned)ATOMIC_TYPE_LAST)
245 res = _atomic_modes[(unsigned)atomic_type->akind];
247 panic("Encountered unknown atomic type");
251 static unsigned get_type_size(type_t *type);
253 static unsigned get_atomic_type_size(const atomic_type_t *type)
255 switch(type->akind) {
256 case ATOMIC_TYPE_CHAR:
257 case ATOMIC_TYPE_SCHAR:
258 case ATOMIC_TYPE_UCHAR:
261 case ATOMIC_TYPE_SHORT:
262 case ATOMIC_TYPE_USHORT:
265 case ATOMIC_TYPE_BOOL:
266 case ATOMIC_TYPE_INT:
267 case ATOMIC_TYPE_UINT:
268 return machine_size >> 3;
270 case ATOMIC_TYPE_LONG:
271 case ATOMIC_TYPE_ULONG:
272 return machine_size > 16 ? machine_size >> 3 : 4;
274 case ATOMIC_TYPE_LONGLONG:
275 case ATOMIC_TYPE_ULONGLONG:
276 return machine_size > 16 ? 8 : 4;
278 case ATOMIC_TYPE_FLOAT:
281 case ATOMIC_TYPE_DOUBLE:
284 case ATOMIC_TYPE_LONG_DOUBLE:
287 case ATOMIC_TYPE_VOID:
290 case ATOMIC_TYPE_INVALID:
291 case ATOMIC_TYPE_LAST:
294 panic("Trying to determine size of invalid atomic type");
297 static unsigned get_compound_type_size(compound_type_t *type)
299 ir_type *irtype = get_ir_type((type_t*) type);
300 return get_type_size_bytes(irtype);
303 static unsigned get_array_type_size(array_type_t *type)
305 ir_type *irtype = get_ir_type((type_t*) type);
306 return get_type_size_bytes(irtype);
310 static unsigned get_type_size(type_t *type)
312 type = skip_typeref(type);
316 panic("error type occured");
318 return get_atomic_type_size(&type->atomic);
320 return get_mode_size_bytes(mode_int);
321 case TYPE_COMPOUND_UNION:
322 case TYPE_COMPOUND_STRUCT:
323 return get_compound_type_size(&type->compound);
325 /* just a pointer to the function */
326 return get_mode_size_bytes(mode_P_code);
328 return get_mode_size_bytes(mode_P_data);
330 return get_array_type_size(&type->array);
332 return get_type_size(type->builtin.real_type);
334 panic("type size of bitfield request");
340 panic("Trying to determine size of invalid type");
343 static unsigned count_parameters(const function_type_t *function_type)
347 function_parameter_t *parameter = function_type->parameters;
348 for ( ; parameter != NULL; parameter = parameter->next) {
358 static long fold_constant(const expression_t *expression);
360 static ir_type *create_atomic_type(const atomic_type_t *type)
362 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
363 ir_mode *mode = get_atomic_mode(type);
364 ident *id = get_mode_ident(mode);
365 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
367 if(type->akind == ATOMIC_TYPE_LONG_DOUBLE
368 || type->akind == ATOMIC_TYPE_DOUBLE) {
369 set_type_alignment_bytes(irtype, 4);
375 static ir_type *create_method_type(const function_type_t *function_type)
377 type_t *return_type = function_type->return_type;
379 ident *id = unique_ident("functiontype");
380 int n_parameters = count_parameters(function_type);
381 int n_results = return_type == type_void ? 0 : 1;
382 dbg_info *dbgi = get_dbg_info(&function_type->type.source_position);
383 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
385 if(return_type != type_void) {
386 ir_type *restype = get_ir_type(return_type);
387 set_method_res_type(irtype, 0, restype);
390 function_parameter_t *parameter = function_type->parameters;
392 for( ; parameter != NULL; parameter = parameter->next) {
393 ir_type *p_irtype = get_ir_type(parameter->type);
394 set_method_param_type(irtype, n, p_irtype);
398 if(function_type->variadic || function_type->unspecified_parameters) {
399 set_method_variadicity(irtype, variadicity_variadic);
405 static ir_type *create_pointer_type(pointer_type_t *type)
407 type_t *points_to = type->points_to;
408 ir_type *ir_points_to;
409 /* Avoid endless recursion if the points_to type contains this poiner type
410 * again (might be a struct). We therefore first create a void* pointer
411 * and then set the real points_to type
413 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
414 ir_type *ir_type = new_d_type_pointer(unique_ident("pointer"),
415 ir_type_void, mode_P_data, dbgi);
416 type->type.firm_type = ir_type;
418 ir_points_to = get_ir_type(points_to);
419 set_pointer_points_to_type(ir_type, ir_points_to);
424 static ir_type *create_array_type(array_type_t *type)
426 type_t *element_type = type->element_type;
427 ir_type *ir_element_type = get_ir_type(element_type);
429 ident *id = unique_ident("array");
430 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
431 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
433 const int align = get_type_alignment_bytes(ir_element_type);
434 set_type_alignment_bytes(ir_type, align);
436 if(type->size != NULL) {
437 int n_elements = fold_constant(type->size);
439 set_array_bounds_int(ir_type, 0, 0, n_elements);
441 size_t elemsize = get_type_size_bytes(ir_element_type);
442 if(elemsize % align > 0) {
443 elemsize += align - (elemsize % align);
445 set_type_size_bytes(ir_type, n_elements * elemsize);
447 set_array_lower_bound_int(ir_type, 0, 0);
449 set_type_state(ir_type, layout_fixed);
455 * Return the signed integer type of size bits.
457 * @param size the size
459 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
462 static ir_mode *s_modes[64 + 1] = {NULL, };
466 if (size <= 0 || size > 64)
469 mode = s_modes[size];
473 snprintf(name, sizeof(name), "bf_I%u", size);
474 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
475 size <= 32 ? 32 : size );
476 s_modes[size] = mode;
480 snprintf(name, sizeof(name), "I%u", size);
481 ident *id = new_id_from_str(name);
482 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
483 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
484 set_primitive_base_type(res, base_tp);
490 * Return the unsigned integer type of size bits.
492 * @param size the size
494 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
497 static ir_mode *u_modes[64 + 1] = {NULL, };
501 if (size <= 0 || size > 64)
504 mode = u_modes[size];
508 snprintf(name, sizeof(name), "bf_U%u", size);
509 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
510 size <= 32 ? 32 : size );
511 u_modes[size] = mode;
516 snprintf(name, sizeof(name), "U%u", size);
517 ident *id = new_id_from_str(name);
518 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
519 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
520 set_primitive_base_type(res, base_tp);
525 static ir_type *create_bitfield_type(bitfield_type_t *const type)
527 type_t *base = skip_typeref(type->base);
528 assert(base->kind == TYPE_ATOMIC);
529 ir_type *irbase = get_ir_type(base);
531 unsigned size = fold_constant(type->size);
533 assert(!is_type_float(base));
534 if(is_type_signed(base)) {
535 return get_signed_int_type_for_bit_size(irbase, size);
537 return get_unsigned_int_type_for_bit_size(irbase, size);
541 #define INVALID_TYPE ((ir_type_ptr)-1)
543 static ir_type *create_union_type(compound_type_t *type, ir_type *irtype,
544 size_t *outer_offset, size_t *outer_align);
546 static ir_type *create_struct_type(compound_type_t *type, ir_type *irtype,
547 size_t *outer_offset, size_t *outer_align)
549 declaration_t *declaration = type->declaration;
550 if(declaration->v.irtype != NULL) {
551 return declaration->v.irtype;
554 size_t align_all = 1;
556 size_t bit_offset = 0;
558 symbol_t *symbol = declaration->symbol;
561 id = unique_ident(symbol->string);
563 id = unique_ident("__anonymous_struct");
565 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
567 irtype = new_d_type_struct(id, dbgi);
569 declaration->v.irtype = irtype;
570 type->type.firm_type = irtype;
572 offset = *outer_offset;
573 align_all = *outer_align;
576 declaration_t *entry = declaration->scope.declarations;
577 for( ; entry != NULL; entry = entry->next) {
578 if(entry->namespc != NAMESPACE_NORMAL)
581 symbol_t *symbol = entry->symbol;
582 type_t *entry_type = skip_typeref(entry->type);
583 dbg_info *dbgi = get_dbg_info(&entry->source_position);
586 ident = new_id_from_str(symbol->string);
588 if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
589 create_struct_type(&entry_type->compound, irtype, &offset,
592 } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
593 create_union_type(&entry_type->compound, irtype, &offset,
597 assert(entry_type->kind == TYPE_BITFIELD);
599 ident = unique_ident("anon");
602 ir_type *base_irtype;
603 if(entry_type->kind == TYPE_BITFIELD) {
604 base_irtype = get_ir_type(entry_type->bitfield.base);
606 base_irtype = get_ir_type(entry_type);
609 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
610 size_t misalign = offset % entry_alignment;
612 ir_type *entry_irtype = get_ir_type(entry_type);
613 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
616 size_t bits_remainder;
617 if(entry_type->kind == TYPE_BITFIELD) {
618 size_t size_bits = fold_constant(entry_type->bitfield.size);
619 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
621 if(size_bits > rest_size_bits) {
622 /* start a new bucket */
623 offset += entry_alignment - misalign;
629 /* put into current bucket */
630 base = offset - misalign;
631 bits_remainder = misalign * 8 + bit_offset;
634 offset += size_bits / 8;
635 bit_offset = bit_offset + (size_bits % 8);
637 size_t entry_size = get_type_size_bytes(base_irtype);
638 if(misalign > 0 || bit_offset > 0)
639 offset += entry_alignment - misalign;
643 offset += entry_size;
647 if(entry_alignment > align_all) {
648 if(entry_alignment % align_all != 0) {
649 panic("uneven alignments not supported yet");
651 align_all = entry_alignment;
654 set_entity_offset(entity, base);
655 set_entity_offset_bits_remainder(entity,
656 (unsigned char) bits_remainder);
657 //add_struct_member(irtype, entity);
658 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
659 assert(entry->v.entity == NULL);
660 entry->v.entity = entity;
663 size_t misalign = offset % align_all;
664 if(misalign > 0 || bit_offset > 0) {
665 offset += align_all - misalign;
668 if(outer_offset != NULL) {
669 *outer_offset = offset;
670 *outer_align = align_all;
672 set_type_alignment_bytes(irtype, align_all);
673 set_type_size_bytes(irtype, offset);
674 set_type_state(irtype, layout_fixed);
680 static ir_type *create_union_type(compound_type_t *type, ir_type *irtype,
681 size_t *outer_offset, size_t *outer_align)
683 declaration_t *declaration = type->declaration;
684 if(declaration->v.irtype != NULL) {
685 return declaration->v.irtype;
689 size_t align_all = 1;
693 symbol_t *symbol = declaration->symbol;
696 id = unique_ident(symbol->string);
698 id = unique_ident("__anonymous_union");
700 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
702 irtype = new_d_type_union(id, dbgi);
704 offset = *outer_offset;
705 align_all = *outer_align;
708 type->type.firm_type = irtype;
710 declaration_t *entry = declaration->scope.declarations;
711 for( ; entry != NULL; entry = entry->next) {
712 if(entry->namespc != NAMESPACE_NORMAL)
715 type_t *entry_type = skip_typeref(entry->type);
716 ir_type *entry_ir_type = get_ir_type(entry_type);
719 if(entry->symbol != NULL) {
720 ident = new_id_from_str(entry->symbol->string);
722 size_t offs = offset;
723 if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
724 create_struct_type(&entry_type->compound, irtype, &offs,
727 } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
728 create_union_type(&entry_type->compound, irtype, &offs,
732 panic("anonymous union member must be struct or union");
734 size_t entry_size = offs - offset;
735 if(entry_size > size) {
738 ident = unique_ident("anon");
741 size_t entry_size = get_type_size_bytes(entry_ir_type);
742 size_t entry_alignment = get_type_alignment_bytes(entry_ir_type);
744 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
745 ir_entity *const entity = new_d_entity(irtype, ident, entry_ir_type,
747 //add_union_member(irtype, entity);
748 set_entity_offset(entity, 0);
749 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
750 assert(entry->v.entity == NULL);
751 entry->v.entity = entity;
753 if(entry_size > size) {
756 if(entry_alignment > align_all) {
757 if(entry_alignment % align_all != 0) {
758 panic("Uneven alignments not supported yet");
760 align_all = entry_alignment;
764 if(outer_offset != NULL) {
765 assert(*outer_offset == offset);
767 size_t misalign = offset % align_all;
769 size += align_all - misalign;
770 *outer_offset += size;
772 if(align_all > *outer_align) {
773 if(align_all % *outer_align != 0) {
774 panic("uneven alignments not supported yet");
776 *outer_align = align_all;
779 set_type_alignment_bytes(irtype, align_all);
780 set_type_size_bytes(irtype, size);
781 set_type_state(irtype, layout_fixed);
782 declaration->v.irtype = irtype;
788 static ir_node *expression_to_firm(const expression_t *expression);
789 static inline ir_mode *get_ir_mode(type_t *type);
791 static ir_type *create_enum_type(enum_type_t *const type)
793 type->type.firm_type = ir_type_int;
795 ir_mode *const mode = get_ir_mode((type_t*) type);
796 tarval *const one = get_mode_one(mode);
797 tarval * tv_next = get_tarval_null(mode);
799 declaration_t *declaration = type->declaration->next;
800 for (; declaration != NULL; declaration = declaration->next) {
801 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
804 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
806 expression_t *const init = declaration->init.enum_value;
808 ir_node *const cnst = expression_to_firm(init);
809 if (!is_Const(cnst)) {
810 panic("couldn't fold constant");
812 tv_next = get_Const_tarval(cnst);
814 declaration->v.enum_val = tv_next;
815 tv_next = tarval_add(tv_next, one);
821 static ir_type *get_ir_type(type_t *type)
823 assert(type != NULL);
825 type = skip_typeref(type);
827 if(type->base.firm_type != NULL) {
828 assert(type->base.firm_type != INVALID_TYPE);
829 return type->base.firm_type;
832 ir_type *firm_type = NULL;
835 panic("error type occured");
837 firm_type = create_atomic_type(&type->atomic);
840 firm_type = create_method_type(&type->function);
843 firm_type = create_pointer_type(&type->pointer);
846 firm_type = create_array_type(&type->array);
848 case TYPE_COMPOUND_STRUCT:
849 firm_type = create_struct_type(&type->compound, NULL, NULL, NULL);
851 case TYPE_COMPOUND_UNION:
852 firm_type = create_union_type(&type->compound, NULL, NULL, NULL);
855 firm_type = create_enum_type(&type->enumt);
858 firm_type = get_ir_type(type->builtin.real_type);
861 firm_type = create_bitfield_type(&type->bitfield);
869 if(firm_type == NULL)
870 panic("unknown type found");
872 type->base.firm_type = firm_type;
876 static inline ir_mode *get_ir_mode(type_t *type)
878 ir_type *irtype = get_ir_type(type);
880 /* firm doesn't report a mode for arrays somehow... */
881 if(is_Array_type(irtype)) {
885 ir_mode *mode = get_type_mode(irtype);
886 assert(mode != NULL);
890 static ident *predef_idents[rts_max];
892 /** Names of the runtime functions. */
893 static const struct {
894 int id; /**< the rts id */
895 int n_res; /**< number of return values */
896 const char *name; /**< the name of the rts function */
897 int n_params; /**< number of parameters */
898 unsigned flags; /**< language flags */
900 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
901 { rts_abort, 0, "abort", 0, _C89 },
902 { rts_abs, 1, "abs", 1, _C89 },
903 { rts_labs, 1, "labs", 1, _C89 },
904 { rts_llabs, 1, "llabs", 1, _C99 },
905 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
907 { rts_fabs, 1, "fabs", 1, _C89 },
908 { rts_sqrt, 1, "sqrt", 1, _C89 },
909 { rts_cbrt, 1, "cbrt", 1, _C99 },
910 { rts_exp, 1, "exp", 1, _C89 },
911 { rts_exp2, 1, "exp2", 1, _C89 },
912 { rts_exp10, 1, "exp10", 1, _GNUC },
913 { rts_log, 1, "log", 1, _C89 },
914 { rts_log2, 1, "log2", 1, _C89 },
915 { rts_log10, 1, "log10", 1, _C89 },
916 { rts_pow, 1, "pow", 2, _C89 },
917 { rts_sin, 1, "sin", 1, _C89 },
918 { rts_cos, 1, "cos", 1, _C89 },
919 { rts_tan, 1, "tan", 1, _C89 },
920 { rts_asin, 1, "asin", 1, _C89 },
921 { rts_acos, 1, "acos", 1, _C89 },
922 { rts_atan, 1, "atan", 1, _C89 },
923 { rts_sinh, 1, "sinh", 1, _C89 },
924 { rts_cosh, 1, "cosh", 1, _C89 },
925 { rts_tanh, 1, "tanh", 1, _C89 },
927 { rts_fabsf, 1, "fabsf", 1, _C99 },
928 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
929 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
930 { rts_expf, 1, "expf", 1, _C99 },
931 { rts_exp2f, 1, "exp2f", 1, _C99 },
932 { rts_exp10f, 1, "exp10f", 1, _GNUC },
933 { rts_logf, 1, "logf", 1, _C99 },
934 { rts_log2f, 1, "log2f", 1, _C99 },
935 { rts_log10f, 1, "log10f", 1, _C99 },
936 { rts_powf, 1, "powf", 2, _C99 },
937 { rts_sinf, 1, "sinf", 1, _C99 },
938 { rts_cosf, 1, "cosf", 1, _C99 },
939 { rts_tanf, 1, "tanf", 1, _C99 },
940 { rts_asinf, 1, "asinf", 1, _C99 },
941 { rts_acosf, 1, "acosf", 1, _C99 },
942 { rts_atanf, 1, "atanf", 1, _C99 },
943 { rts_sinhf, 1, "sinhf", 1, _C99 },
944 { rts_coshf, 1, "coshf", 1, _C99 },
945 { rts_tanhf, 1, "tanhf", 1, _C99 },
947 { rts_fabsl, 1, "fabsl", 1, _C99 },
948 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
949 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
950 { rts_expl, 1, "expl", 1, _C99 },
951 { rts_exp2l, 1, "exp2l", 1, _C99 },
952 { rts_exp10l, 1, "exp10l", 1, _GNUC },
953 { rts_logl, 1, "logl", 1, _C99 },
954 { rts_log2l, 1, "log2l", 1, _C99 },
955 { rts_log10l, 1, "log10l", 1, _C99 },
956 { rts_powl, 1, "powl", 2, _C99 },
957 { rts_sinl, 1, "sinl", 1, _C99 },
958 { rts_cosl, 1, "cosl", 1, _C99 },
959 { rts_tanl, 1, "tanl", 1, _C99 },
960 { rts_asinl, 1, "asinl", 1, _C99 },
961 { rts_acosl, 1, "acosl", 1, _C99 },
962 { rts_atanl, 1, "atanl", 1, _C99 },
963 { rts_sinhl, 1, "sinhl", 1, _C99 },
964 { rts_coshl, 1, "coshl", 1, _C99 },
965 { rts_tanhl, 1, "tanhl", 1, _C99 },
967 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
968 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
969 { rts_strcmp, 1, "strcmp", 2, _C89 },
970 { rts_strncmp, 1, "strncmp", 3, _C89 }
973 static ir_entity* get_function_entity(declaration_t *declaration)
975 if(declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
976 return declaration->v.entity;
977 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
979 symbol_t *symbol = declaration->symbol;
980 ident *id = new_id_from_str(symbol->string);
982 ir_type *global_type = get_glob_type();
983 ir_type *ir_type_method = get_ir_type(declaration->type);
984 assert(is_Method_type(ir_type_method));
986 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
987 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
988 set_entity_ld_ident(entity, id);
989 if(declaration->storage_class == STORAGE_CLASS_STATIC
990 || declaration->is_inline) {
991 set_entity_visibility(entity, visibility_local);
992 } else if(declaration->init.statement != NULL) {
993 set_entity_visibility(entity, visibility_external_visible);
995 set_entity_visibility(entity, visibility_external_allocated);
997 /* We should check for file scope here, but as long as we compile C only
998 this is not needed. */
999 int n_params = get_method_n_params(ir_type_method);
1000 int n_res = get_method_n_ress(ir_type_method);
1003 if (n_params == 0 && n_res == 0 && id == predef_idents[rts_abort]) {
1004 /* found abort(), store for later */
1008 if (! firm_opt.freestanding) {
1009 /* check for a known runtime function */
1010 for (i = 0; i < rts_max; ++i) {
1011 /* ignore those rts functions not necessary needed for current mode */
1012 if ((c_mode & rts_data[i].flags) == 0)
1014 if (n_params == rts_data[i].n_params && n_res == rts_data[i].n_res &&
1015 id == predef_idents[rts_data[i].id])
1016 rts_entities[rts_data[i].id] = entity;
1021 set_entity_allocation(entity, allocation_static);
1023 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1024 declaration->v.entity = entity;
1029 static ir_node *const_to_firm(const const_expression_t *cnst)
1031 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1032 ir_mode *mode = get_ir_mode(cnst->base.type);
1037 if(mode_is_float(mode)) {
1038 tv = new_tarval_from_double(cnst->v.float_value, mode);
1040 if(mode_is_signed(mode)) {
1041 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1043 len = snprintf(buf, sizeof(buf), "%llu",
1044 (unsigned long long) cnst->v.int_value);
1046 tv = new_tarval_from_str(buf, len, mode);
1049 return new_d_Const(dbgi, mode, tv);
1052 static ir_node *char_const_to_firm(const const_expression_t *cnst)
1054 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1055 ir_mode *mode = get_ir_mode(cnst->base.type);
1057 long long int v = 0;
1058 for (size_t i = 0; i < cnst->v.chars.size; ++i) {
1059 v = (v << 8) | ((unsigned char)cnst->v.chars.begin[i]);
1062 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1063 tarval *tv = new_tarval_from_str(buf, len, mode);
1065 return new_d_Const(dbgi, mode, tv);
1068 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1071 assert(entity != NULL);
1072 union symconst_symbol sym;
1073 sym.entity_p = entity;
1074 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1077 static ir_node *string_to_firm(const source_position_t *const src_pos,
1078 const char *const id_prefix,
1079 const string_t *const value)
1081 ir_type *const global_type = get_glob_type();
1082 dbg_info *const dbgi = get_dbg_info(src_pos);
1083 ir_type *const type = new_d_type_array(unique_ident("strtype"), 1,
1084 ir_type_const_char, dbgi);
1086 ident *const id = unique_ident(id_prefix);
1087 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1088 set_entity_ld_ident(entity, id);
1089 set_entity_variability(entity, variability_constant);
1090 set_entity_allocation(entity, allocation_static);
1092 ir_type *const elem_type = ir_type_const_char;
1093 ir_mode *const mode = get_type_mode(elem_type);
1095 const char* const string = value->begin;
1096 const size_t slen = value->size;
1098 set_array_lower_bound_int(type, 0, 0);
1099 set_array_upper_bound_int(type, 0, slen);
1100 set_type_size_bytes(type, slen);
1101 set_type_state(type, layout_fixed);
1103 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1104 for(size_t i = 0; i < slen; ++i) {
1105 tvs[i] = new_tarval_from_long(string[i], mode);
1108 set_array_entity_values(entity, tvs, slen);
1111 return create_symconst(dbgi, mode_P_data, entity);
1114 static ir_node *string_literal_to_firm(
1115 const string_literal_expression_t* literal)
1117 return string_to_firm(&literal->base.source_position, "Lstr",
1121 static ir_node *wide_string_literal_to_firm(
1122 const wide_string_literal_expression_t* const literal)
1124 ir_type *const global_type = get_glob_type();
1125 ir_type *const elem_type = ir_type_wchar_t;
1126 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1127 ir_type *const type = new_d_type_array(unique_ident("strtype"), 1,
1130 ident *const id = unique_ident("Lstr");
1131 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1132 set_entity_ld_ident(entity, id);
1133 set_entity_variability(entity, variability_constant);
1134 set_entity_allocation(entity, allocation_static);
1136 ir_mode *const mode = get_type_mode(elem_type);
1138 const wchar_rep_t *const string = literal->value.begin;
1139 const size_t slen = literal->value.size;
1141 set_array_lower_bound_int(type, 0, 0);
1142 set_array_upper_bound_int(type, 0, slen);
1143 set_type_size_bytes(type, slen);
1144 set_type_state(type, layout_fixed);
1146 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1147 for(size_t i = 0; i < slen; ++i) {
1148 tvs[i] = new_tarval_from_long(string[i], mode);
1151 set_array_entity_values(entity, tvs, slen);
1154 return create_symconst(dbgi, mode_P_data, entity);
1157 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
1158 dbg_info *const dbgi)
1160 if (is_compound_type(irtype) ||
1161 is_Method_type(irtype) ||
1162 is_Array_type(irtype)) {
1166 ir_mode *const mode = get_type_mode(irtype);
1167 ir_node *const memory = get_store();
1168 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1169 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1170 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1171 set_store(load_mem);
1175 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1177 ir_mode *mode = get_irn_mode(node);
1179 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1181 if(!mode_is_float(mode))
1184 /* check if there is already a Conv */
1185 if (get_irn_op(node) == op_Conv) {
1186 /* convert it into a strict Conv */
1187 set_Conv_strict(node, 1);
1191 /* otherwise create a new one */
1192 return new_d_strictConv(dbgi, node, mode);
1195 static ir_node *get_global_var_address(dbg_info *const dbgi,
1196 const declaration_t *const decl)
1198 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1200 ir_entity *const entity = decl->v.entity;
1201 switch ((storage_class_tag_t)decl->storage_class) {
1202 case STORAGE_CLASS_THREAD:
1203 case STORAGE_CLASS_THREAD_EXTERN:
1204 case STORAGE_CLASS_THREAD_STATIC: {
1205 ir_node *const no_mem = new_NoMem();
1206 ir_node *const tls = get_irg_tls(current_ir_graph);
1207 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1211 return create_symconst(dbgi, mode_P_data, entity);
1215 /* Returns the correct base address depending on whether it is a parameter or a
1216 * normal local variable */
1217 static ir_node *get_local_frame(ir_entity *const ent)
1219 ir_graph *const irg = current_ir_graph;
1220 const ir_type *const owner = get_entity_owner(ent);
1221 if (owner == get_irg_frame_type(irg)) {
1222 return get_irg_frame(irg);
1224 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1225 return get_irg_value_param_base(irg);
1229 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1231 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1232 declaration_t *declaration = ref->declaration;
1233 type_t *type = skip_typeref(declaration->type);
1235 switch((declaration_kind_t) declaration->declaration_kind) {
1236 case DECLARATION_KIND_UNKNOWN:
1237 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
1243 case DECLARATION_KIND_ENUM_ENTRY: {
1244 ir_mode *const mode = get_ir_mode(type);
1245 return new_Const(mode, declaration->v.enum_val);
1248 case DECLARATION_KIND_LOCAL_VARIABLE: {
1249 ir_mode *const mode = get_ir_mode(type);
1250 return get_value(declaration->v.value_number, mode);
1252 case DECLARATION_KIND_FUNCTION: {
1253 ir_mode *const mode = get_ir_mode(type);
1254 return create_symconst(dbgi, mode, declaration->v.entity);
1256 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1257 ir_node *const addr = get_global_var_address(dbgi, declaration);
1258 ir_type *const irtype = get_entity_type(declaration->v.entity);
1259 return deref_address(irtype, addr, dbgi);
1262 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1263 ir_entity *entity = declaration->v.entity;
1264 ir_node *frame = get_local_frame(entity);
1265 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1266 ir_type *irtype = get_entity_type(entity);
1267 return deref_address(irtype, sel, dbgi);
1270 case DECLARATION_KIND_COMPOUND_MEMBER:
1271 case DECLARATION_KIND_LABEL_BLOCK:
1272 panic("not implemented reference type");
1275 panic("reference to declaration with unknown type found");
1278 static ir_node *reference_addr(const reference_expression_t *ref)
1280 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1281 declaration_t *declaration = ref->declaration;
1283 switch((declaration_kind_t) declaration->declaration_kind) {
1284 case DECLARATION_KIND_UNKNOWN:
1286 case DECLARATION_KIND_LOCAL_VARIABLE:
1287 panic("local variable without entity has no address");
1288 case DECLARATION_KIND_FUNCTION: {
1289 type_t *const type = skip_typeref(ref->base.type);
1290 ir_mode *const mode = get_ir_mode(type);
1291 return create_symconst(dbgi, mode, declaration->v.entity);
1293 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1294 ir_node *const addr = get_global_var_address(dbgi, declaration);
1297 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1298 ir_entity *entity = declaration->v.entity;
1299 ir_node *frame = get_local_frame(entity);
1300 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1305 case DECLARATION_KIND_ENUM_ENTRY:
1306 panic("trying to reference enum entry");
1308 case DECLARATION_KIND_COMPOUND_MEMBER:
1309 case DECLARATION_KIND_LABEL_BLOCK:
1310 panic("not implemented reference type");
1313 panic("reference to declaration with unknown type found");
1316 static ir_node *process_builtin_call(const call_expression_t *call)
1318 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1320 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1321 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1323 type_t *type = skip_typeref(builtin->base.type);
1324 assert(is_type_pointer(type));
1326 type_t *function_type = skip_typeref(type->pointer.points_to);
1327 symbol_t *symbol = builtin->symbol;
1329 switch(symbol->ID) {
1330 case T___builtin_alloca: {
1331 if(call->arguments == NULL || call->arguments->next != NULL) {
1332 panic("invalid number of parameters on __builtin_alloca");
1334 expression_t *argument = call->arguments->expression;
1335 ir_node *size = expression_to_firm(argument);
1337 ir_node *store = get_store();
1338 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1340 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1342 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1346 case T___builtin_nan:
1347 case T___builtin_nanf:
1348 case T___builtin_nand: {
1349 /* Ignore string for now... */
1350 assert(is_type_function(function_type));
1351 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1352 tarval *tv = get_mode_NAN(mode);
1353 ir_node *res = new_d_Const(dbgi, mode, tv);
1356 case T___builtin_va_end:
1359 panic("Unsupported builtin found\n");
1363 static ir_node *call_expression_to_firm(const call_expression_t *call)
1365 assert(get_cur_block() != NULL);
1367 expression_t *function = call->function;
1368 if(function->kind == EXPR_BUILTIN_SYMBOL) {
1369 return process_builtin_call(call);
1371 ir_node *callee = expression_to_firm(function);
1373 type_t *type = skip_typeref(function->base.type);
1374 assert(is_type_pointer(type));
1375 pointer_type_t *pointer_type = &type->pointer;
1376 type_t *points_to = skip_typeref(pointer_type->points_to);
1377 assert(is_type_function(points_to));
1378 function_type_t *function_type = &points_to->function;
1380 int n_parameters = 0;
1381 call_argument_t *argument = call->arguments;
1382 for( ; argument != NULL; argument = argument->next) {
1386 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1388 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1389 ir_type *new_method_type = NULL;
1390 if(function_type->variadic || function_type->unspecified_parameters) {
1391 /* we need to construct a new method type matching the call
1393 int n_res = get_method_n_ress(ir_method_type);
1394 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1395 new_method_type = new_d_type_method(unique_ident("calltype"),
1396 n_parameters, n_res, dbgi);
1397 set_method_calling_convention(new_method_type,
1398 get_method_calling_convention(ir_method_type));
1399 set_method_additional_properties(new_method_type,
1400 get_method_additional_properties(ir_method_type));
1402 for(int i = 0; i < n_res; ++i) {
1403 set_method_res_type(new_method_type, i,
1404 get_method_res_type(ir_method_type, i));
1407 ir_node *in[n_parameters];
1409 argument = call->arguments;
1411 for( ; argument != NULL; argument = argument->next) {
1412 expression_t *expression = argument->expression;
1413 ir_node *arg_node = expression_to_firm(expression);
1415 arg_node = do_strict_conv(dbgi, arg_node);
1418 if(new_method_type != NULL) {
1419 ir_type *irtype = get_ir_type(expression->base.type);
1420 set_method_param_type(new_method_type, n, irtype);
1425 assert(n == n_parameters);
1427 if(new_method_type != NULL)
1428 ir_method_type = new_method_type;
1430 ir_node *store = get_store();
1431 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1433 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1436 type_t *return_type = skip_typeref(function_type->return_type);
1437 ir_node *result = NULL;
1439 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1441 if(is_type_scalar(return_type)) {
1442 mode = get_ir_mode(return_type);
1446 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1447 result = new_d_Proj(dbgi, resproj, mode, 0);
1453 static void statement_to_firm(statement_t *statement);
1454 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1456 static ir_node *expression_to_addr(const expression_t *expression);
1457 static void create_condition_evaluation(const expression_t *expression,
1458 ir_node *true_block,
1459 ir_node *false_block);
1461 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1464 value = do_strict_conv(dbgi, value);
1466 ir_node *memory = get_store();
1468 if(is_type_scalar(type)) {
1469 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1470 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1471 set_store(store_mem);
1473 ir_type *irtype = get_ir_type(type);
1474 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1475 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1476 set_store(copyb_mem);
1480 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1482 tarval *all_one = get_mode_all_one(mode);
1483 int mode_size = get_mode_size_bits(mode);
1485 assert(offset >= 0 && size >= 0);
1486 assert(offset + size <= mode_size);
1487 if(size == mode_size) {
1491 long shiftr = get_mode_size_bits(mode) - size;
1492 long shiftl = offset;
1493 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1494 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1495 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1496 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1501 static void bitfield_store_to_firm(const unary_expression_t *expression,
1504 expression_t *select = expression->value;
1505 assert(select->kind == EXPR_SELECT);
1506 type_t *type = select->base.type;
1507 assert(type->kind == TYPE_BITFIELD);
1508 ir_mode *mode = get_ir_mode(type->bitfield.base);
1509 ir_node *addr = expression_to_addr(select);
1511 assert(get_irn_mode(value) == mode);
1513 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1515 /* kill upper bits of value and shift to right position */
1516 ir_entity *entity = select->select.compound_entry->v.entity;
1517 int bitoffset = get_entity_offset_bits_remainder(entity);
1518 ir_type *entity_type = get_entity_type(entity);
1519 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1521 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1522 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1523 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1524 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1525 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1526 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1528 /* load current value */
1529 ir_node *mem = get_store();
1530 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1531 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1532 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1533 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1534 tarval *inv_mask = tarval_not(shift_mask);
1535 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1536 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1538 /* construct new value and store */
1539 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1540 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1541 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1542 set_store(store_mem);
1545 static void set_value_for_expression(const expression_t *expression,
1548 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1549 value = do_strict_conv(dbgi, value);
1551 if(expression->kind == EXPR_REFERENCE) {
1552 const reference_expression_t *ref = &expression->reference;
1554 declaration_t *declaration = ref->declaration;
1555 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1556 if(declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1557 set_value(declaration->v.value_number, value);
1562 if(expression->kind == EXPR_UNARY_BITFIELD_EXTRACT) {
1563 bitfield_store_to_firm(&expression->unary, value);
1567 ir_node *addr = expression_to_addr(expression);
1568 type_t *type = skip_typeref(expression->base.type);
1569 assign_value(dbgi, addr, type, value);
1572 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1574 ir_mode *value_mode = get_irn_mode(value);
1576 if (value_mode == dest_mode || is_Bad(value))
1579 if(dest_mode == mode_b) {
1580 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1581 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1582 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1586 return new_d_Conv(dbgi, value, dest_mode);
1589 static ir_node *create_incdec(const unary_expression_t *expression)
1591 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1592 type_t *type = skip_typeref(expression->base.type);
1593 ir_mode *mode = get_ir_mode(type);
1594 expression_t *value = expression->value;
1596 ir_node *value_node = expression_to_firm(value);
1599 if(is_type_pointer(type)) {
1600 pointer_type_t *pointer_type = &type->pointer;
1601 unsigned elem_size = get_type_size(pointer_type->points_to);
1602 offset = new_Const_long(mode_int, elem_size);
1604 assert(is_type_arithmetic(type));
1605 offset = new_Const(mode, get_mode_one(mode));
1608 switch(expression->base.kind) {
1609 case EXPR_UNARY_POSTFIX_INCREMENT: {
1610 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1611 set_value_for_expression(value, new_value);
1614 case EXPR_UNARY_POSTFIX_DECREMENT: {
1615 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1616 set_value_for_expression(value, new_value);
1619 case EXPR_UNARY_PREFIX_INCREMENT: {
1620 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1621 set_value_for_expression(value, new_value);
1624 case EXPR_UNARY_PREFIX_DECREMENT: {
1625 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1626 set_value_for_expression(value, new_value);
1630 panic("no incdec expr in create_incdec");
1635 static bool is_local_variable(expression_t *expression)
1637 if (expression->kind != EXPR_REFERENCE)
1639 reference_expression_t *ref_expr = &expression->reference;
1640 declaration_t *declaration = ref_expr->declaration;
1641 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1644 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1647 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1648 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1649 case EXPR_BINARY_NOTEQUAL:
1650 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1651 case EXPR_BINARY_ISLESS:
1652 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1653 case EXPR_BINARY_ISLESSEQUAL:
1654 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1655 case EXPR_BINARY_ISGREATER:
1656 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1657 case EXPR_BINARY_ISGREATEREQUAL:
1658 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1659 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1664 panic("trying to get pn_Cmp from non-comparison binexpr type");
1668 * Handle the assume optimizer hint: check if a Confirm
1669 * node can be created.
1671 * @param dbi debug info
1672 * @param expr the IL assume expression
1674 * we support here only some simple cases:
1679 static ir_node *handle_assume_compare(dbg_info *dbi,
1680 const binary_expression_t *expression)
1682 expression_t *op1 = expression->left;
1683 expression_t *op2 = expression->right;
1684 declaration_t *var2, *var = NULL;
1685 ir_node *res = NULL;
1688 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1690 if (is_local_variable(op1) && is_local_variable(op2)) {
1691 var = op1->reference.declaration;
1692 var2 = op2->reference.declaration;
1694 type_t *const type = skip_typeref(var->type);
1695 ir_mode *const mode = get_ir_mode(type);
1697 ir_node *const irn1 = get_value(var->v.value_number, mode);
1698 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1700 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1701 set_value(var2->v.value_number, res);
1703 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1704 set_value(var->v.value_number, res);
1710 if (is_local_variable(op1) && is_constant_expression(op2)) {
1711 var = op1->reference.declaration;
1713 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1714 cmp_val = get_inversed_pnc(cmp_val);
1715 var = op2->reference.declaration;
1720 type_t *const type = skip_typeref(var->type);
1721 ir_mode *const mode = get_ir_mode(type);
1723 res = get_value(var->v.value_number, mode);
1724 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1725 set_value(var->v.value_number, res);
1731 * Handle the assume optimizer hint.
1733 * @param dbi debug info
1734 * @param expr the IL assume expression
1736 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1737 switch(expression->kind) {
1738 case EXPR_BINARY_EQUAL:
1739 case EXPR_BINARY_NOTEQUAL:
1740 case EXPR_BINARY_LESS:
1741 case EXPR_BINARY_LESSEQUAL:
1742 case EXPR_BINARY_GREATER:
1743 case EXPR_BINARY_GREATEREQUAL:
1744 return handle_assume_compare(dbi, &expression->binary);
1750 static ir_node *bitfield_extract_to_firm(const unary_expression_t *expression)
1752 expression_t *select = expression->value;
1753 assert(select->kind == EXPR_SELECT);
1755 type_t *type = select->base.type;
1756 assert(type->kind == TYPE_BITFIELD);
1757 ir_mode *mode = get_ir_mode(type->bitfield.base);
1758 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1759 ir_node *addr = expression_to_addr(select);
1760 ir_node *mem = get_store();
1761 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1762 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1763 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1765 load_res = create_conv(dbgi, load_res, mode_int);
1767 set_store(load_mem);
1769 /* kill upper bits */
1770 ir_entity *entity = select->select.compound_entry->v.entity;
1771 int bitoffset = get_entity_offset_bits_remainder(entity);
1772 ir_type *entity_type = get_entity_type(entity);
1773 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1774 long shift_bitsl = machine_size - bitoffset - bitsize;
1775 assert(shift_bitsl >= 0);
1776 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1777 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1778 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1780 long shift_bitsr = bitoffset + shift_bitsl;
1781 assert(shift_bitsr <= (long) machine_size);
1782 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1783 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1785 if(mode_is_signed(mode)) {
1786 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1788 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1791 return create_conv(dbgi, shiftr, mode);
1794 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1796 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1797 type_t *type = skip_typeref(expression->base.type);
1799 if(expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
1800 return expression_to_addr(expression->value);
1802 const expression_t *value = expression->value;
1804 switch(expression->base.kind) {
1805 case EXPR_UNARY_NEGATE: {
1806 ir_node *value_node = expression_to_firm(value);
1807 ir_mode *mode = get_ir_mode(type);
1808 return new_d_Minus(dbgi, value_node, mode);
1810 case EXPR_UNARY_PLUS:
1811 return expression_to_firm(value);
1812 case EXPR_UNARY_BITWISE_NEGATE: {
1813 ir_node *value_node = expression_to_firm(value);
1814 ir_mode *mode = get_ir_mode(type);
1815 return new_d_Not(dbgi, value_node, mode);
1817 case EXPR_UNARY_NOT: {
1818 ir_node *value_node = expression_to_firm(value);
1819 ir_mode *mode = get_ir_mode(type);
1820 if(get_irn_mode(value_node) != mode_b) {
1821 value_node = create_conv(dbgi, value_node, mode_b);
1823 value_node = new_d_Not(dbgi, value_node, mode_b);
1824 if(mode != mode_b) {
1825 value_node = create_conv(dbgi, value_node, mode);
1829 case EXPR_UNARY_DEREFERENCE: {
1830 ir_node *value_node = expression_to_firm(value);
1831 type_t *value_type = skip_typeref(value->base.type);
1832 ir_type *irtype = get_ir_type(value_type);
1833 assert(is_Pointer_type(irtype));
1834 ir_type *points_to = get_pointer_points_to_type(irtype);
1835 return deref_address(points_to, value_node, dbgi);
1837 case EXPR_UNARY_POSTFIX_INCREMENT:
1838 case EXPR_UNARY_POSTFIX_DECREMENT:
1839 case EXPR_UNARY_PREFIX_INCREMENT:
1840 case EXPR_UNARY_PREFIX_DECREMENT:
1841 return create_incdec(expression);
1842 case EXPR_UNARY_CAST: {
1843 ir_node *value_node = expression_to_firm(value);
1844 if(is_type_scalar(type)) {
1845 ir_mode *mode = get_ir_mode(type);
1846 ir_node *node = create_conv(dbgi, value_node, mode);
1847 node = do_strict_conv(dbgi, node);
1853 case EXPR_UNARY_CAST_IMPLICIT: {
1854 ir_node *value_node = expression_to_firm(value);
1855 if(is_type_scalar(type)) {
1856 ir_mode *mode = get_ir_mode(type);
1857 return create_conv(dbgi, value_node, mode);
1862 case EXPR_UNARY_ASSUME:
1863 if(firm_opt.confirm)
1864 return handle_assume(dbgi, value);
1867 case EXPR_UNARY_BITFIELD_EXTRACT:
1868 return bitfield_extract_to_firm(expression);
1873 panic("invalid UNEXPR type found");
1876 static ir_node *create_lazy_op(const binary_expression_t *expression)
1878 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1879 type_t *type = expression->base.type;
1880 ir_mode *mode = get_ir_mode(type);
1882 if(is_constant_expression(expression->left)) {
1883 long val = fold_constant(expression->left);
1884 expression_kind_t ekind = expression->base.kind;
1885 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
1886 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1887 return expression_to_firm(expression->right);
1889 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
1890 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
1891 return new_Const(mode, get_mode_one(mode));
1895 ir_node *cur_block = get_cur_block();
1897 ir_node *one_block = new_immBlock();
1898 ir_node *one = new_Const(mode, get_mode_one(mode));
1899 ir_node *jmp_one = new_d_Jmp(dbgi);
1901 ir_node *zero_block = new_immBlock();
1902 ir_node *zero = new_Const(mode, get_mode_null(mode));
1903 ir_node *jmp_zero = new_d_Jmp(dbgi);
1905 set_cur_block(cur_block);
1906 create_condition_evaluation((const expression_t*) expression,
1907 one_block, zero_block);
1908 mature_immBlock(one_block);
1909 mature_immBlock(zero_block);
1911 ir_node *common_block = new_immBlock();
1912 add_immBlock_pred(common_block, jmp_one);
1913 add_immBlock_pred(common_block, jmp_zero);
1914 mature_immBlock(common_block);
1916 ir_node *in[2] = { one, zero };
1917 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1922 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1923 ir_node *right, ir_mode *mode);
1925 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1926 create_arithmetic_func func)
1928 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1929 ir_node *left = expression_to_firm(expression->left);
1930 ir_node *right = expression_to_firm(expression->right);
1931 type_t *type = expression->right->base.type;
1932 /* be careful with the modes, because in arithmetic assign nodes only
1933 * the right operand has the mode of the arithmetic already */
1934 ir_mode *mode = get_ir_mode(type);
1935 left = create_conv(dbgi, left, mode);
1936 ir_node *res = func(dbgi, left, right, mode);
1941 static ir_node *pointer_arithmetic(ir_node *const pointer,
1944 dbg_info *const dbgi,
1945 const create_arithmetic_func func)
1947 pointer_type_t *const pointer_type = &type->pointer;
1948 type_t *const points_to = pointer_type->points_to;
1949 const unsigned elem_size = get_type_size(points_to);
1951 assert(elem_size >= 1);
1952 if (elem_size > 1) {
1953 integer = create_conv(dbgi, integer, mode_int);
1954 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1955 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1959 ir_mode *const mode = get_ir_mode(type);
1960 return func(dbgi, pointer, integer, mode);
1963 static ir_node *create_arithmetic_assign_binop(
1964 const binary_expression_t *expression, create_arithmetic_func func)
1966 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1967 type_t *const type = skip_typeref(expression->base.type);
1970 if (is_type_pointer(type)) {
1971 ir_node *const pointer = expression_to_firm(expression->left);
1972 ir_node * integer = expression_to_firm(expression->right);
1973 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1975 value = create_arithmetic_binop(expression, func);
1978 ir_mode *const mode = get_ir_mode(type);
1979 value = create_conv(dbgi, value, mode);
1980 set_value_for_expression(expression->left, value);
1985 static ir_node *create_add(const binary_expression_t *expression)
1987 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1988 ir_node *left = expression_to_firm(expression->left);
1989 ir_node *right = expression_to_firm(expression->right);
1990 type_t *type = expression->base.type;
1992 expression_t *expr_left = expression->left;
1993 expression_t *expr_right = expression->right;
1994 type_t *type_left = skip_typeref(expr_left->base.type);
1995 type_t *type_right = skip_typeref(expr_right->base.type);
1997 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1998 ir_mode *const mode = get_ir_mode(type);
1999 return new_d_Add(dbgi, left, right, mode);
2002 if (is_type_pointer(type_left)) {
2003 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
2005 assert(is_type_pointer(type_right));
2006 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
2010 static ir_node *create_sub(const binary_expression_t *expression)
2012 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2013 expression_t *const expr_left = expression->left;
2014 expression_t *const expr_right = expression->right;
2015 ir_node *const left = expression_to_firm(expr_left);
2016 ir_node *const right = expression_to_firm(expr_right);
2017 type_t *const type = expression->base.type;
2018 type_t *const type_left = skip_typeref(expr_left->base.type);
2019 type_t *const type_right = skip_typeref(expr_right->base.type);
2021 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2022 ir_mode *const mode = get_ir_mode(type);
2023 return new_d_Sub(dbgi, left, right, mode);
2024 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2025 const pointer_type_t *const ptr_type = &type_left->pointer;
2026 const unsigned elem_size = get_type_size(ptr_type->points_to);
2027 ir_mode *const mode = get_ir_mode(type);
2028 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2029 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2030 ir_node *const no_mem = new_NoMem();
2031 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
2032 op_pin_state_floats);
2033 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2036 assert(is_type_pointer(type_left));
2037 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
2040 static ir_node *create_shift(const binary_expression_t *expression)
2042 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2043 ir_node *left = expression_to_firm(expression->left);
2044 ir_node *right = expression_to_firm(expression->right);
2045 type_t *type = expression->base.type;
2046 ir_mode *mode = get_ir_mode(type);
2048 /* firm always wants the shift count to be unsigned */
2049 right = create_conv(dbgi, right, mode_uint);
2053 switch(expression->base.kind) {
2054 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2055 case EXPR_BINARY_SHIFTLEFT:
2056 res = new_d_Shl(dbgi, left, right, mode);
2058 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2059 case EXPR_BINARY_SHIFTRIGHT: {
2060 expression_t *expr_left = expression->left;
2061 type_t *type_left = skip_typeref(expr_left->base.type);
2063 if(is_type_signed(type_left)) {
2064 res = new_d_Shrs(dbgi, left, right, mode);
2066 res = new_d_Shr(dbgi, left, right, mode);
2071 panic("create shift op called for non-shift op");
2078 static ir_node *create_divmod(const binary_expression_t *expression)
2080 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2081 ir_node *left = expression_to_firm(expression->left);
2082 ir_node *right = expression_to_firm(expression->right);
2083 ir_node *pin = new_Pin(new_NoMem());
2084 /* be careful with the modes, because in arithmetic assign nodes only
2085 * the right operand has the mode of the arithmetic already */
2086 type_t *type = expression->right->base.type;
2087 ir_mode *mode = get_ir_mode(type);
2088 left = create_conv(dbgi, left, mode);
2092 switch (expression->base.kind) {
2093 case EXPR_BINARY_DIV:
2094 case EXPR_BINARY_DIV_ASSIGN:
2095 if(mode_is_float(mode)) {
2096 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2097 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2099 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2100 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2104 case EXPR_BINARY_MOD:
2105 case EXPR_BINARY_MOD_ASSIGN:
2106 assert(!mode_is_float(mode));
2107 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
2108 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2111 default: panic("unexpected binary expression type in create_divmod()");
2117 static ir_node *create_arithmetic_assign_divmod(
2118 const binary_expression_t *expression)
2120 ir_node * value = create_divmod(expression);
2121 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2122 type_t *const type = expression->base.type;
2123 ir_mode *const mode = get_ir_mode(type);
2125 assert(type->kind != TYPE_POINTER);
2127 value = create_conv(dbgi, value, mode);
2128 set_value_for_expression(expression->left, value);
2133 static ir_node *create_arithmetic_assign_shift(
2134 const binary_expression_t *expression)
2136 ir_node * value = create_shift(expression);
2137 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2138 type_t *const type = expression->base.type;
2139 ir_mode *const mode = get_ir_mode(type);
2141 value = create_conv(dbgi, value, mode);
2142 set_value_for_expression(expression->left, value);
2147 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2149 expression_kind_t kind = expression->base.kind;
2152 case EXPR_BINARY_EQUAL:
2153 case EXPR_BINARY_NOTEQUAL:
2154 case EXPR_BINARY_LESS:
2155 case EXPR_BINARY_LESSEQUAL:
2156 case EXPR_BINARY_GREATER:
2157 case EXPR_BINARY_GREATEREQUAL:
2158 case EXPR_BINARY_ISGREATER:
2159 case EXPR_BINARY_ISGREATEREQUAL:
2160 case EXPR_BINARY_ISLESS:
2161 case EXPR_BINARY_ISLESSEQUAL:
2162 case EXPR_BINARY_ISLESSGREATER:
2163 case EXPR_BINARY_ISUNORDERED: {
2164 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2165 ir_node *left = expression_to_firm(expression->left);
2166 ir_node *right = expression_to_firm(expression->right);
2167 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2168 long pnc = get_pnc(kind, expression->left->base.type);
2169 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2172 case EXPR_BINARY_ASSIGN: {
2173 ir_node *right = expression_to_firm(expression->right);
2174 set_value_for_expression(expression->left, right);
2178 case EXPR_BINARY_ADD:
2179 return create_add(expression);
2180 case EXPR_BINARY_SUB:
2181 return create_sub(expression);
2182 case EXPR_BINARY_MUL:
2183 return create_arithmetic_binop(expression, new_d_Mul);
2184 case EXPR_BINARY_BITWISE_AND:
2185 return create_arithmetic_binop(expression, new_d_And);
2186 case EXPR_BINARY_BITWISE_OR:
2187 return create_arithmetic_binop(expression, new_d_Or);
2188 case EXPR_BINARY_BITWISE_XOR:
2189 return create_arithmetic_binop(expression, new_d_Eor);
2190 case EXPR_BINARY_SHIFTLEFT:
2191 case EXPR_BINARY_SHIFTRIGHT:
2192 return create_shift(expression);
2193 case EXPR_BINARY_DIV:
2194 case EXPR_BINARY_MOD:
2195 return create_divmod(expression);
2196 case EXPR_BINARY_LOGICAL_AND:
2197 case EXPR_BINARY_LOGICAL_OR:
2198 return create_lazy_op(expression);
2199 case EXPR_BINARY_COMMA:
2200 expression_to_firm(expression->left);
2201 return expression_to_firm(expression->right);
2202 case EXPR_BINARY_ADD_ASSIGN:
2203 return create_arithmetic_assign_binop(expression, new_d_Add);
2204 case EXPR_BINARY_SUB_ASSIGN:
2205 return create_arithmetic_assign_binop(expression, new_d_Sub);
2206 case EXPR_BINARY_MUL_ASSIGN:
2207 return create_arithmetic_assign_binop(expression, new_d_Mul);
2208 case EXPR_BINARY_MOD_ASSIGN:
2209 case EXPR_BINARY_DIV_ASSIGN:
2210 return create_arithmetic_assign_divmod(expression);
2211 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2212 return create_arithmetic_assign_binop(expression, new_d_And);
2213 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2214 return create_arithmetic_assign_binop(expression, new_d_Or);
2215 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2216 return create_arithmetic_assign_binop(expression, new_d_Eor);
2217 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2218 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2219 return create_arithmetic_assign_shift(expression);
2220 case EXPR_BINARY_BUILTIN_EXPECT:
2221 return expression_to_firm(expression->left);
2223 panic("TODO binexpr type");
2227 static ir_node *array_access_addr(const array_access_expression_t *expression)
2229 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2230 ir_node *base_addr = expression_to_firm(expression->array_ref);
2231 ir_node *offset = expression_to_firm(expression->index);
2232 offset = create_conv(dbgi, offset, mode_uint);
2234 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2235 assert(is_type_pointer(ref_type));
2236 pointer_type_t *pointer_type = &ref_type->pointer;
2238 unsigned elem_size = get_type_size(pointer_type->points_to);
2239 ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
2240 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2242 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2247 static ir_node *array_access_to_firm(
2248 const array_access_expression_t *expression)
2250 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2251 ir_node *addr = array_access_addr(expression);
2252 type_t *type = revert_automatic_type_conversion(
2253 (const expression_t*) expression);
2254 type = skip_typeref(type);
2255 ir_type *irtype = get_ir_type(type);
2257 return deref_address(irtype, addr, dbgi);
2261 * Transform a sizeof expression into Firm code.
2263 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2265 type_t *type = expression->type;
2267 type = expression->tp_expression->base.type;
2268 assert(type != NULL);
2271 ir_mode *const mode = get_ir_mode(expression->base.type);
2272 symconst_symbol sym;
2273 sym.type_p = get_ir_type(type);
2274 return new_SymConst(mode, sym, symconst_type_size);
2278 * Transform an alignof expression into Firm code.
2280 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2282 type_t *type = expression->type;
2284 /* beware: if expression is a variable reference, return the
2285 alignment of the variable. */
2286 const expression_t *tp_expression = expression->tp_expression;
2287 const declaration_t *declaration = expr_is_variable(tp_expression);
2288 if (declaration != NULL) {
2289 /* TODO: get the alignment of this variable. */
2291 type = tp_expression->base.type;
2292 assert(type != NULL);
2295 ir_mode *const mode = get_ir_mode(expression->base.type);
2296 symconst_symbol sym;
2297 sym.type_p = get_ir_type(type);
2298 return new_SymConst(mode, sym, symconst_type_align);
2301 static long fold_constant(const expression_t *expression)
2303 assert(is_constant_expression(expression));
2305 ir_graph *old_current_ir_graph = current_ir_graph;
2306 if(current_ir_graph == NULL) {
2307 current_ir_graph = get_const_code_irg();
2310 ir_node *cnst = expression_to_firm(expression);
2311 current_ir_graph = old_current_ir_graph;
2313 if(!is_Const(cnst)) {
2314 panic("couldn't fold constant\n");
2317 tarval *tv = get_Const_tarval(cnst);
2318 if(!tarval_is_long(tv)) {
2319 panic("result of constant folding is not integer\n");
2322 return get_tarval_long(tv);
2325 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2327 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2329 /* first try to fold a constant condition */
2330 if(is_constant_expression(expression->condition)) {
2331 long val = fold_constant(expression->condition);
2333 return expression_to_firm(expression->true_expression);
2335 return expression_to_firm(expression->false_expression);
2339 ir_node *cur_block = get_cur_block();
2341 /* create the true block */
2342 ir_node *true_block = new_immBlock();
2344 ir_node *true_val = expression_to_firm(expression->true_expression);
2345 ir_node *true_jmp = new_Jmp();
2347 /* create the false block */
2348 ir_node *false_block = new_immBlock();
2350 ir_node *false_val = expression_to_firm(expression->false_expression);
2351 ir_node *false_jmp = new_Jmp();
2353 /* create the condition evaluation */
2354 set_cur_block(cur_block);
2355 create_condition_evaluation(expression->condition, true_block, false_block);
2356 mature_immBlock(true_block);
2357 mature_immBlock(false_block);
2359 /* create the common block */
2360 ir_node *common_block = new_immBlock();
2361 add_immBlock_pred(common_block, true_jmp);
2362 add_immBlock_pred(common_block, false_jmp);
2363 mature_immBlock(common_block);
2365 /* TODO improve static semantics, so either both or no values are NULL */
2366 if (true_val == NULL || false_val == NULL)
2369 ir_node *in[2] = { true_val, false_val };
2370 ir_mode *mode = get_irn_mode(true_val);
2371 assert(get_irn_mode(false_val) == mode);
2372 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2377 static ir_node *select_addr(const select_expression_t *expression)
2379 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2381 ir_node *compound_addr = expression_to_firm(expression->compound);
2383 declaration_t *entry = expression->compound_entry;
2384 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2385 ir_entity *entity = entry->v.entity;
2387 assert(entity != NULL);
2389 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2394 static ir_node *select_to_firm(const select_expression_t *expression)
2396 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2397 ir_node *addr = select_addr(expression);
2398 type_t *type = revert_automatic_type_conversion(
2399 (const expression_t*) expression);
2400 type = skip_typeref(type);
2401 ir_type *irtype = get_ir_type(type);
2403 return deref_address(irtype, addr, dbgi);
2406 /* Values returned by __builtin_classify_type. */
2407 typedef enum gcc_type_class
2413 enumeral_type_class,
2416 reference_type_class,
2420 function_type_class,
2431 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2433 const type_t *const type = expr->type_expression->base.type;
2439 const atomic_type_t *const atomic_type = &type->atomic;
2440 switch (atomic_type->akind) {
2441 /* should not be reached */
2442 case ATOMIC_TYPE_INVALID:
2446 /* gcc cannot do that */
2447 case ATOMIC_TYPE_VOID:
2448 tc = void_type_class;
2451 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2452 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2453 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2454 case ATOMIC_TYPE_SHORT:
2455 case ATOMIC_TYPE_USHORT:
2456 case ATOMIC_TYPE_INT:
2457 case ATOMIC_TYPE_UINT:
2458 case ATOMIC_TYPE_LONG:
2459 case ATOMIC_TYPE_ULONG:
2460 case ATOMIC_TYPE_LONGLONG:
2461 case ATOMIC_TYPE_ULONGLONG:
2462 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2463 tc = integer_type_class;
2466 case ATOMIC_TYPE_FLOAT:
2467 case ATOMIC_TYPE_DOUBLE:
2468 case ATOMIC_TYPE_LONG_DOUBLE:
2469 tc = real_type_class;
2472 #ifdef PROVIDE_COMPLEX
2473 case ATOMIC_TYPE_FLOAT_COMPLEX:
2474 case ATOMIC_TYPE_DOUBLE_COMPLEX:
2475 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
2476 tc = complex_type_class;
2478 case ATOMIC_TYPE_FLOAT_IMAGINARY:
2479 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
2480 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
2481 tc = complex_type_class;
2486 panic("Unimplemented case in classify_type_to_firm().");
2491 case TYPE_ARRAY: /* gcc handles this as pointer */
2492 case TYPE_FUNCTION: /* gcc handles this as pointer */
2493 case TYPE_POINTER: tc = pointer_type_class; break;
2494 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
2495 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
2497 /* gcc handles this as integer */
2498 case TYPE_ENUM: tc = integer_type_class; break;
2501 panic("Unimplemented case in classify_type_to_firm().");
2504 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2505 ir_mode *const mode = mode_int;
2506 tarval *const tv = new_tarval_from_long(tc, mode);
2507 return new_d_Const(dbgi, mode, tv);
2510 static ir_node *function_name_to_firm(
2511 const string_literal_expression_t *const expr)
2513 if (current_function_name == NULL) {
2514 const source_position_t *const src_pos = &expr->base.source_position;
2515 const char *const name = current_function_decl->symbol->string;
2516 const string_t string = { name, strlen(name) + 1 };
2517 current_function_name = string_to_firm(src_pos, "__func__", &string);
2520 return current_function_name;
2523 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2525 statement_t *statement = expr->statement;
2527 assert(statement->kind == STATEMENT_COMPOUND);
2528 return compound_statement_to_firm(&statement->compound);
2531 static ir_node *va_start_expression_to_firm(
2532 const va_start_expression_t *const expr)
2534 ir_type *const method_type = get_ir_type(current_function_decl->type);
2535 int const n = get_method_n_params(method_type) - 1;
2536 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2537 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2538 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2539 ir_node *const no_mem = new_NoMem();
2540 ir_node *const arg_sel =
2541 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2543 size_t const parm_size = get_type_size(expr->parameter->type);
2544 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2545 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2546 set_value_for_expression(expr->ap, add);
2551 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2553 ir_type *const irtype = get_ir_type(expr->base.type);
2554 ir_node *const ap = expression_to_firm(expr->ap);
2555 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2556 ir_node *const res = deref_address(irtype, ap, dbgi);
2558 size_t const parm_size = get_type_size(expr->base.type);
2559 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2560 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2561 set_value_for_expression(expr->ap, add);
2566 static ir_node *dereference_addr(const unary_expression_t *const expression)
2568 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2569 return expression_to_firm(expression->value);
2572 static ir_node *expression_to_addr(const expression_t *expression)
2574 switch(expression->kind) {
2575 case EXPR_REFERENCE:
2576 return reference_addr(&expression->reference);
2577 case EXPR_ARRAY_ACCESS:
2578 return array_access_addr(&expression->array_access);
2580 return select_addr(&expression->select);
2582 return call_expression_to_firm(&expression->call);
2583 case EXPR_UNARY_DEREFERENCE: {
2584 return dereference_addr(&expression->unary);
2589 panic("trying to get address of non-lvalue");
2592 static ir_node *builtin_constant_to_firm(
2593 const builtin_constant_expression_t *expression)
2595 ir_mode *mode = get_ir_mode(expression->base.type);
2598 if (is_constant_expression(expression->value)) {
2603 return new_Const_long(mode, v);
2606 static ir_node *builtin_prefetch_to_firm(
2607 const builtin_prefetch_expression_t *expression)
2609 ir_node *adr = expression_to_firm(expression->adr);
2610 /* no Firm support for prefetch yet */
2615 static ir_node *_expression_to_firm(const expression_t *expression)
2617 switch(expression->kind) {
2618 case EXPR_CHAR_CONST:
2619 return char_const_to_firm(&expression->conste);
2621 return const_to_firm(&expression->conste);
2622 case EXPR_STRING_LITERAL:
2623 return string_literal_to_firm(&expression->string);
2624 case EXPR_WIDE_STRING_LITERAL:
2625 return wide_string_literal_to_firm(&expression->wide_string);
2626 case EXPR_REFERENCE:
2627 return reference_expression_to_firm(&expression->reference);
2629 return call_expression_to_firm(&expression->call);
2631 return unary_expression_to_firm(&expression->unary);
2633 return binary_expression_to_firm(&expression->binary);
2634 case EXPR_ARRAY_ACCESS:
2635 return array_access_to_firm(&expression->array_access);
2637 return sizeof_to_firm(&expression->typeprop);
2639 return alignof_to_firm(&expression->typeprop);
2640 case EXPR_CONDITIONAL:
2641 return conditional_to_firm(&expression->conditional);
2643 return select_to_firm(&expression->select);
2644 case EXPR_CLASSIFY_TYPE:
2645 return classify_type_to_firm(&expression->classify_type);
2647 case EXPR_PRETTY_FUNCTION:
2648 return function_name_to_firm(&expression->string);
2649 case EXPR_STATEMENT:
2650 return statement_expression_to_firm(&expression->statement);
2652 return va_start_expression_to_firm(&expression->va_starte);
2654 return va_arg_expression_to_firm(&expression->va_arge);
2656 case EXPR_BUILTIN_SYMBOL:
2657 panic("unimplemented expression found");
2658 case EXPR_BUILTIN_CONSTANT_P:
2659 return builtin_constant_to_firm(&expression->builtin_constant);
2660 case EXPR_BUILTIN_PREFETCH:
2661 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2667 panic("invalid expression found");
2670 static ir_node *expression_to_firm(const expression_t *expression)
2672 ir_node *res = _expression_to_firm(expression);
2674 if(res != NULL && get_irn_mode(res) == mode_b) {
2675 ir_mode *mode = get_ir_mode(expression->base.type);
2676 res = create_conv(NULL, res, mode);
2682 static ir_node *expression_to_modeb(const expression_t *expression)
2684 ir_node *res = _expression_to_firm(expression);
2685 res = create_conv(NULL, res, mode_b);
2691 * create a short-circuit expression evaluation that tries to construct
2692 * efficient control flow structures for &&, || and ! expressions
2694 static void create_condition_evaluation(const expression_t *expression,
2695 ir_node *true_block,
2696 ir_node *false_block)
2698 switch(expression->kind) {
2699 case EXPR_UNARY_NOT: {
2700 const unary_expression_t *unary_expression = &expression->unary;
2701 create_condition_evaluation(unary_expression->value, false_block,
2705 case EXPR_BINARY_LOGICAL_AND: {
2706 const binary_expression_t *binary_expression = &expression->binary;
2708 ir_node *cur_block = get_cur_block();
2709 ir_node *extra_block = new_immBlock();
2710 set_cur_block(cur_block);
2711 create_condition_evaluation(binary_expression->left, extra_block,
2713 mature_immBlock(extra_block);
2714 set_cur_block(extra_block);
2715 create_condition_evaluation(binary_expression->right, true_block,
2719 case EXPR_BINARY_LOGICAL_OR: {
2720 const binary_expression_t *binary_expression = &expression->binary;
2722 ir_node *cur_block = get_cur_block();
2723 ir_node *extra_block = new_immBlock();
2724 set_cur_block(cur_block);
2725 create_condition_evaluation(binary_expression->left, true_block,
2727 mature_immBlock(extra_block);
2728 set_cur_block(extra_block);
2729 create_condition_evaluation(binary_expression->right, true_block,
2737 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2738 ir_node *condition = expression_to_modeb(expression);
2739 ir_node *cond = new_d_Cond(dbgi, condition);
2740 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2741 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2743 /* set branch prediction info based on __builtin_expect */
2744 if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
2745 long cnst = fold_constant(expression->binary.right);
2746 cond_jmp_predicate pred;
2749 pred = COND_JMP_PRED_FALSE;
2751 pred = COND_JMP_PRED_TRUE;
2753 set_Cond_jmp_pred(cond, pred);
2756 add_immBlock_pred(true_block, true_proj);
2757 add_immBlock_pred(false_block, false_proj);
2759 set_cur_block(NULL);
2764 static void create_declaration_entity(declaration_t *declaration,
2765 declaration_kind_t declaration_kind,
2766 ir_type *parent_type)
2768 ident *const id = new_id_from_str(declaration->symbol->string);
2769 ir_type *const irtype = get_ir_type(declaration->type);
2770 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2771 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2772 set_entity_ld_ident(entity, id);
2774 declaration->declaration_kind = (unsigned char) declaration_kind;
2775 declaration->v.entity = entity;
2776 set_entity_variability(entity, variability_uninitialized);
2777 if(parent_type == get_tls_type())
2778 set_entity_allocation(entity, allocation_automatic);
2779 else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
2780 set_entity_allocation(entity, allocation_static);
2781 /* TODO: visibility? */
2784 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2786 enum compound_graph_entry_type_t {
2787 COMPOUND_GRAPH_ENTRY_ARRAY,
2788 COMPOUND_GRAPH_ENTRY_COMPOUND
2791 struct compound_graph_path_entry_t {
2797 compound_graph_path_entry_t *prev;
2800 static void create_initializer_object(initializer_t *initializer, type_t *type,
2801 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2803 static compound_graph_path *create_compound_path(ir_type *type,
2804 compound_graph_path_entry_t *entry, int len)
2806 compound_graph_path *path = new_compound_graph_path(type, len);
2809 for( ; entry != NULL; entry = entry->prev, --i) {
2811 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2812 set_compound_graph_path_node(path, i, entry->v.entity);
2814 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2815 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2823 static void create_initializer_value(initializer_value_t *initializer,
2825 compound_graph_path_entry_t *entry,
2828 ir_node *node = expression_to_firm(initializer->value);
2829 ir_type *type = get_entity_type(entity);
2830 compound_graph_path *path = create_compound_path(type, entry, len);
2831 add_compound_ent_value_w_path(entity, node, path);
2834 static void create_initializer_compound(initializer_list_t *initializer,
2835 compound_type_t *type,
2837 compound_graph_path_entry_t *last_entry,
2840 declaration_t *compound_declaration = type->declaration;
2842 declaration_t *compound_entry = compound_declaration->scope.declarations;
2844 compound_graph_path_entry_t entry;
2845 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2846 entry.prev = last_entry;
2850 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2851 if(compound_entry->symbol == NULL)
2853 if(compound_entry->namespc != NAMESPACE_NORMAL)
2856 if(i >= initializer->len)
2859 entry.v.entity = compound_entry->v.entity;
2861 initializer_t *sub_initializer = initializer->initializers[i];
2863 assert(compound_entry != NULL);
2864 assert(compound_entry->declaration_kind
2865 == DECLARATION_KIND_COMPOUND_MEMBER);
2867 if(sub_initializer->kind == INITIALIZER_VALUE) {
2868 create_initializer_value(&sub_initializer->value,
2869 entity, &entry, len);
2871 type_t *entry_type = skip_typeref(compound_entry->type);
2872 create_initializer_object(sub_initializer, entry_type, entity,
2880 static void create_initializer_array(initializer_list_t *initializer,
2881 array_type_t *type, ir_entity *entity,
2882 compound_graph_path_entry_t *last_entry,
2885 type_t *element_type = type->element_type;
2886 element_type = skip_typeref(element_type);
2888 compound_graph_path_entry_t entry;
2889 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2890 entry.prev = last_entry;
2894 for(i = 0; i < initializer->len; ++i) {
2895 entry.v.array_index = i;
2897 initializer_t *sub_initializer = initializer->initializers[i];
2899 if(sub_initializer->kind == INITIALIZER_VALUE) {
2900 create_initializer_value(&sub_initializer->value,
2901 entity, &entry, len);
2903 create_initializer_object(sub_initializer, element_type, entity,
2909 /* TODO: initialize rest... */
2910 if(type->size_expression != NULL) {
2911 size_t array_len = fold_constant(type->size_expression);
2912 for( ; i < array_len; ++i) {
2919 static void create_initializer_string(initializer_string_t *initializer,
2920 array_type_t *type, ir_entity *entity,
2921 compound_graph_path_entry_t *last_entry,
2924 type_t *element_type = type->element_type;
2925 element_type = skip_typeref(element_type);
2927 compound_graph_path_entry_t entry;
2928 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2929 entry.prev = last_entry;
2932 ir_type *const irtype = get_entity_type(entity);
2933 size_t arr_len = get_array_type_size(type);
2934 const char *const p = initializer->string.begin;
2935 if (initializer->string.size < arr_len) {
2936 arr_len = initializer->string.size;
2938 for (size_t i = 0; i < arr_len; ++i) {
2939 entry.v.array_index = i;
2941 ir_node *node = new_Const_long(mode_Bs, p[i]);
2942 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2943 add_compound_ent_value_w_path(entity, node, path);
2947 static void create_initializer_wide_string(
2948 const initializer_wide_string_t *const initializer, array_type_t *const type,
2949 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2952 type_t *element_type = type->element_type;
2953 element_type = skip_typeref(element_type);
2955 compound_graph_path_entry_t entry;
2956 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2957 entry.prev = last_entry;
2960 ir_type *const irtype = get_entity_type(entity);
2961 const size_t arr_len = get_array_type_size(type);
2962 const wchar_rep_t * p = initializer->string.begin;
2963 const wchar_rep_t *const end = p + initializer->string.size;
2964 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2965 entry.v.array_index = i;
2967 ir_node *node = new_Const_long(mode_int, *p);
2968 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2969 add_compound_ent_value_w_path(entity, node, path);
2973 static void create_initializer_object(initializer_t *initializer, type_t *type,
2974 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2976 if(is_type_array(type)) {
2977 array_type_t *array_type = &type->array;
2979 switch (initializer->kind) {
2980 case INITIALIZER_STRING: {
2981 initializer_string_t *const string = &initializer->string;
2982 create_initializer_string(string, array_type, entity, entry, len);
2986 case INITIALIZER_WIDE_STRING: {
2987 initializer_wide_string_t *const string = &initializer->wide_string;
2988 create_initializer_wide_string(string, array_type, entity, entry, len);
2992 case INITIALIZER_LIST: {
2993 initializer_list_t *const list = &initializer->list;
2994 create_initializer_array(list, array_type, entity, entry, len);
2998 case INITIALIZER_VALUE:
3001 panic("Unhandled initializer");
3003 assert(initializer->kind == INITIALIZER_LIST);
3004 initializer_list_t *list = &initializer->list;
3006 assert(is_type_compound(type));
3007 compound_type_t *compound_type = &type->compound;
3008 create_initializer_compound(list, compound_type, entity, entry, len);
3012 static void create_initializer_local_variable_entity(declaration_t *declaration)
3014 initializer_t *initializer = declaration->init.initializer;
3015 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3016 ir_entity *entity = declaration->v.entity;
3017 ir_node *memory = get_store();
3018 ir_node *nomem = new_NoMem();
3019 ir_node *frame = get_irg_frame(current_ir_graph);
3020 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3022 if(initializer->kind == INITIALIZER_VALUE) {
3023 initializer_value_t *initializer_value = &initializer->value;
3025 ir_node *value = expression_to_firm(initializer_value->value);
3026 type_t *type = skip_typeref(declaration->type);
3027 assign_value(dbgi, addr, type, value);
3031 /* create a "template" entity which is copied to the entity on the stack */
3032 ident *const id = unique_ident("initializer");
3033 ir_type *const irtype = get_ir_type(declaration->type);
3034 ir_type *const global_type = get_glob_type();
3035 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3036 set_entity_ld_ident(init_entity, id);
3038 set_entity_variability(init_entity, variability_initialized);
3039 set_entity_visibility(init_entity, visibility_local);
3040 set_entity_allocation(init_entity, allocation_static);
3042 ir_graph *const old_current_ir_graph = current_ir_graph;
3043 current_ir_graph = get_const_code_irg();
3045 type_t *const type = skip_typeref(declaration->type);
3046 create_initializer_object(initializer, type, init_entity, NULL, 0);
3048 assert(current_ir_graph == get_const_code_irg());
3049 current_ir_graph = old_current_ir_graph;
3051 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3052 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3054 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3055 set_store(copyb_mem);
3058 static void create_initializer(declaration_t *declaration)
3060 initializer_t *initializer = declaration->init.initializer;
3061 if(initializer == NULL)
3064 declaration_kind_t declaration_kind
3065 = (declaration_kind_t) declaration->declaration_kind;
3066 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3067 create_initializer_local_variable_entity(declaration);
3071 if(initializer->kind == INITIALIZER_VALUE) {
3072 initializer_value_t *initializer_value = &initializer->value;
3074 ir_node *value = expression_to_firm(initializer_value->value);
3076 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3077 set_value(declaration->v.value_number, value);
3079 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3081 ir_entity *entity = declaration->v.entity;
3083 set_entity_variability(entity, variability_initialized);
3084 set_atomic_ent_value(entity, value);
3087 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3088 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3090 ir_entity *entity = declaration->v.entity;
3091 set_entity_variability(entity, variability_initialized);
3093 type_t *type = skip_typeref(declaration->type);
3094 create_initializer_object(initializer, type, entity, NULL, 0);
3099 * Creates a Firm local variable from a declaration.
3101 static void create_local_variable(declaration_t *declaration)
3103 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3105 bool needs_entity = declaration->address_taken;
3106 type_t *type = skip_typeref(declaration->type);
3108 if(is_type_array(type) || is_type_compound(type)) {
3109 needs_entity = true;
3113 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3114 create_declaration_entity(declaration,
3115 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3118 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3119 declaration->v.value_number = next_value_number_function;
3120 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3121 ++next_value_number_function;
3124 create_initializer(declaration);
3127 static void create_local_static_variable(declaration_t *declaration)
3129 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3131 type_t *const type = skip_typeref(declaration->type);
3132 ir_type *const global_type = get_glob_type();
3133 ident *const id = unique_ident(declaration->symbol->string);
3134 ir_type *const irtype = get_ir_type(type);
3135 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3136 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3137 set_entity_ld_ident(entity, id);
3139 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3140 declaration->v.entity = entity;
3141 set_entity_variability(entity, variability_uninitialized);
3142 set_entity_visibility(entity, visibility_local);
3143 set_entity_allocation(entity, allocation_static);
3145 ir_graph *const old_current_ir_graph = current_ir_graph;
3146 current_ir_graph = get_const_code_irg();
3148 create_initializer(declaration);
3150 assert(current_ir_graph == get_const_code_irg());
3151 current_ir_graph = old_current_ir_graph;
3156 static void return_statement_to_firm(return_statement_t *statement)
3158 if(get_cur_block() == NULL)
3161 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3162 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3167 if(get_method_n_ress(func_irtype) > 0) {
3168 ir_type *res_type = get_method_res_type(func_irtype, 0);
3170 if(statement->value != NULL) {
3171 ir_node *node = expression_to_firm(statement->value);
3172 node = do_strict_conv(dbgi, node);
3176 if(is_compound_type(res_type)) {
3179 mode = get_type_mode(res_type);
3181 in[0] = new_Unknown(mode);
3185 /* build return_value for its side effects */
3186 if(statement->value != NULL) {
3187 expression_to_firm(statement->value);
3192 ir_node *store = get_store();
3193 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3195 ir_node *end_block = get_irg_end_block(current_ir_graph);
3196 add_immBlock_pred(end_block, ret);
3198 set_cur_block(NULL);
3201 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3203 if(get_cur_block() == NULL)
3206 return expression_to_firm(statement->expression);
3209 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3211 ir_node *result = NULL;
3212 statement_t *statement = compound->statements;
3213 for( ; statement != NULL; statement = statement->base.next) {
3214 //context2firm(&statement->scope);
3216 if(statement->base.next == NULL
3217 && statement->kind == STATEMENT_EXPRESSION) {
3218 result = expression_statement_to_firm(
3219 &statement->expression);
3222 statement_to_firm(statement);
3228 static void create_local_declaration(declaration_t *declaration)
3230 if(declaration->symbol == NULL)
3233 type_t *type = skip_typeref(declaration->type);
3235 switch ((storage_class_tag_t) declaration->storage_class) {
3236 case STORAGE_CLASS_STATIC:
3237 create_local_static_variable(declaration);
3239 case STORAGE_CLASS_ENUM_ENTRY:
3240 panic("enum entry declaration in local block found");
3241 case STORAGE_CLASS_EXTERN:
3242 panic("extern declaration in local block found");
3243 case STORAGE_CLASS_NONE:
3244 case STORAGE_CLASS_AUTO:
3245 case STORAGE_CLASS_REGISTER:
3246 if(is_type_function(type)) {
3247 if(declaration->init.statement != NULL) {
3248 panic("nested functions not supported yet");
3250 get_function_entity(declaration);
3253 create_local_variable(declaration);
3256 case STORAGE_CLASS_TYPEDEF:
3257 case STORAGE_CLASS_THREAD:
3258 case STORAGE_CLASS_THREAD_EXTERN:
3259 case STORAGE_CLASS_THREAD_STATIC:
3262 panic("invalid storage class found");
3265 static void declaration_statement_to_firm(declaration_statement_t *statement)
3267 declaration_t *declaration = statement->declarations_begin;
3268 declaration_t *end = statement->declarations_end->next;
3269 for( ; declaration != end; declaration = declaration->next) {
3270 if(declaration->namespc != NAMESPACE_NORMAL)
3272 create_local_declaration(declaration);
3276 static void if_statement_to_firm(if_statement_t *statement)
3278 ir_node *cur_block = get_cur_block();
3280 ir_node *fallthrough_block = new_immBlock();
3282 /* the true (blocks) */
3283 ir_node *true_block;
3284 if (statement->true_statement != NULL) {
3285 true_block = new_immBlock();
3286 statement_to_firm(statement->true_statement);
3287 if(get_cur_block() != NULL) {
3288 ir_node *jmp = new_Jmp();
3289 add_immBlock_pred(fallthrough_block, jmp);
3292 true_block = fallthrough_block;
3295 /* the false (blocks) */
3296 ir_node *false_block;
3297 if(statement->false_statement != NULL) {
3298 false_block = new_immBlock();
3300 statement_to_firm(statement->false_statement);
3301 if(get_cur_block() != NULL) {
3302 ir_node *jmp = new_Jmp();
3303 add_immBlock_pred(fallthrough_block, jmp);
3306 false_block = fallthrough_block;
3309 /* create the condition */
3310 if(cur_block != NULL) {
3311 set_cur_block(cur_block);
3312 create_condition_evaluation(statement->condition, true_block,
3316 mature_immBlock(true_block);
3317 if(false_block != fallthrough_block) {
3318 mature_immBlock(false_block);
3320 mature_immBlock(fallthrough_block);
3322 set_cur_block(fallthrough_block);
3325 static void while_statement_to_firm(while_statement_t *statement)
3327 ir_node *jmp = NULL;
3328 if(get_cur_block() != NULL) {
3332 /* create the header block */
3333 ir_node *header_block = new_immBlock();
3335 add_immBlock_pred(header_block, jmp);
3338 /* the false block */
3339 ir_node *false_block = new_immBlock();
3342 ir_node *body_block;
3343 if (statement->body != NULL) {
3344 ir_node *old_continue_label = continue_label;
3345 ir_node *old_break_label = break_label;
3346 continue_label = header_block;
3347 break_label = false_block;
3349 body_block = new_immBlock();
3350 statement_to_firm(statement->body);
3352 assert(continue_label == header_block);
3353 assert(break_label == false_block);
3354 continue_label = old_continue_label;
3355 break_label = old_break_label;
3357 if(get_cur_block() != NULL) {
3359 add_immBlock_pred(header_block, jmp);
3362 body_block = header_block;
3365 /* create the condition */
3366 set_cur_block(header_block);
3368 create_condition_evaluation(statement->condition, body_block, false_block);
3369 mature_immBlock(body_block);
3370 mature_immBlock(false_block);
3371 mature_immBlock(header_block);
3373 set_cur_block(false_block);
3376 static void do_while_statement_to_firm(do_while_statement_t *statement)
3378 ir_node *jmp = NULL;
3379 if(get_cur_block() != NULL) {
3383 /* create the header block */
3384 ir_node *header_block = new_immBlock();
3386 /* the false block */
3387 ir_node *false_block = new_immBlock();
3390 ir_node *body_block = new_immBlock();
3392 add_immBlock_pred(body_block, jmp);
3395 if (statement->body != NULL) {
3396 ir_node *old_continue_label = continue_label;
3397 ir_node *old_break_label = break_label;
3398 continue_label = header_block;
3399 break_label = false_block;
3401 statement_to_firm(statement->body);
3403 assert(continue_label == header_block);
3404 assert(break_label == false_block);
3405 continue_label = old_continue_label;
3406 break_label = old_break_label;
3408 if (get_cur_block() == NULL) {
3409 mature_immBlock(header_block);
3410 mature_immBlock(body_block);
3411 mature_immBlock(false_block);
3416 ir_node *body_jmp = new_Jmp();
3417 add_immBlock_pred(header_block, body_jmp);
3418 mature_immBlock(header_block);
3420 /* create the condition */
3421 set_cur_block(header_block);
3423 create_condition_evaluation(statement->condition, body_block, false_block);
3424 mature_immBlock(body_block);
3425 mature_immBlock(false_block);
3426 mature_immBlock(header_block);
3428 set_cur_block(false_block);
3431 static void for_statement_to_firm(for_statement_t *statement)
3433 ir_node *jmp = NULL;
3434 if (get_cur_block() != NULL) {
3435 if(statement->initialisation != NULL) {
3436 expression_to_firm(statement->initialisation);
3439 /* create declarations */
3440 declaration_t *declaration = statement->scope.declarations;
3441 for( ; declaration != NULL; declaration = declaration->next) {
3442 create_local_declaration(declaration);
3449 /* create the step block */
3450 ir_node *const step_block = new_immBlock();
3451 if (statement->step != NULL) {
3452 expression_to_firm(statement->step);
3454 ir_node *const step_jmp = new_Jmp();
3456 /* create the header block */
3457 ir_node *const header_block = new_immBlock();
3459 add_immBlock_pred(header_block, jmp);
3461 add_immBlock_pred(header_block, step_jmp);
3463 /* the false block */
3464 ir_node *const false_block = new_immBlock();
3467 ir_node * body_block;
3468 if (statement->body != NULL) {
3469 ir_node *const old_continue_label = continue_label;
3470 ir_node *const old_break_label = break_label;
3471 continue_label = step_block;
3472 break_label = false_block;
3474 body_block = new_immBlock();
3475 statement_to_firm(statement->body);
3477 assert(continue_label == step_block);
3478 assert(break_label == false_block);
3479 continue_label = old_continue_label;
3480 break_label = old_break_label;
3482 if (get_cur_block() != NULL) {
3484 add_immBlock_pred(step_block, jmp);
3487 body_block = step_block;
3490 /* create the condition */
3491 set_cur_block(header_block);
3492 if (statement->condition != NULL) {
3493 create_condition_evaluation(statement->condition, body_block,
3496 keep_alive(header_block);
3498 add_immBlock_pred(body_block, jmp);
3501 mature_immBlock(body_block);
3502 mature_immBlock(false_block);
3503 mature_immBlock(step_block);
3504 mature_immBlock(header_block);
3505 mature_immBlock(false_block);
3507 set_cur_block(false_block);
3510 static void create_jump_statement(const statement_t *statement,
3511 ir_node *target_block)
3513 if(get_cur_block() == NULL)
3516 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3517 ir_node *jump = new_d_Jmp(dbgi);
3518 add_immBlock_pred(target_block, jump);
3520 set_cur_block(NULL);
3523 static void switch_statement_to_firm(const switch_statement_t *statement)
3525 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3527 ir_node *expression = expression_to_firm(statement->expression);
3528 ir_node *cond = new_d_Cond(dbgi, expression);
3529 ir_node *break_block = new_immBlock();
3531 set_cur_block(NULL);
3533 ir_node *const old_switch_cond = current_switch_cond;
3534 ir_node *const old_break_label = break_label;
3535 const bool old_saw_default_label = saw_default_label;
3536 current_switch_cond = cond;
3537 break_label = break_block;
3539 if (statement->body != NULL) {
3540 statement_to_firm(statement->body);
3543 if(get_cur_block() != NULL) {
3544 ir_node *jmp = new_Jmp();
3545 add_immBlock_pred(break_block, jmp);
3548 if (!saw_default_label) {
3549 set_cur_block(get_nodes_block(cond));
3550 ir_node *const proj = new_d_defaultProj(dbgi, cond,
3551 MAGIC_DEFAULT_PN_NUMBER);
3552 add_immBlock_pred(break_block, proj);
3555 assert(current_switch_cond == cond);
3556 assert(break_label == break_block);
3557 current_switch_cond = old_switch_cond;
3558 break_label = old_break_label;
3559 saw_default_label = old_saw_default_label;
3561 mature_immBlock(break_block);
3562 set_cur_block(break_block);
3565 static void case_label_to_firm(const case_label_statement_t *statement)
3567 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3569 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
3571 /* let's create a node and hope firm constant folding creates a Const
3574 ir_node *old_block = get_nodes_block(current_switch_cond);
3575 ir_node *block = new_immBlock();
3577 set_cur_block(old_block);
3578 if(statement->expression != NULL) {
3579 long start_pn = fold_constant(statement->expression);
3580 long end_pn = start_pn;
3581 if (statement->end_range != NULL) {
3582 end_pn = fold_constant(statement->end_range);
3584 assert(start_pn <= end_pn);
3585 /* create jumps for all cases in the given range */
3586 for (long pn = start_pn; pn <= end_pn; ++pn) {
3587 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
3588 /* oops someone detected our cheating... */
3589 panic("magic default pn used");
3591 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
3592 add_immBlock_pred(block, proj);
3595 saw_default_label = true;
3596 proj = new_d_defaultProj(dbgi, current_switch_cond,
3597 MAGIC_DEFAULT_PN_NUMBER);
3599 add_immBlock_pred(block, proj);
3602 if (fallthrough != NULL) {
3603 add_immBlock_pred(block, fallthrough);
3605 mature_immBlock(block);
3606 set_cur_block(block);
3608 if(statement->statement != NULL) {
3609 statement_to_firm(statement->statement);
3613 static ir_node *get_label_block(declaration_t *label)
3615 assert(label->namespc == NAMESPACE_LABEL);
3617 if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
3618 return label->v.block;
3620 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
3622 ir_node *old_cur_block = get_cur_block();
3623 ir_node *block = new_immBlock();
3624 set_cur_block(old_cur_block);
3626 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
3627 label->v.block = block;
3629 ARR_APP1(ir_node *, imature_blocks, block);
3634 static void label_to_firm(const label_statement_t *statement)
3636 ir_node *block = get_label_block(statement->label);
3638 if(get_cur_block() != NULL) {
3639 ir_node *jmp = new_Jmp();
3640 add_immBlock_pred(block, jmp);
3643 set_cur_block(block);
3646 if(statement->statement != NULL) {
3647 statement_to_firm(statement->statement);
3651 static void goto_to_firm(const goto_statement_t *statement)
3653 if(get_cur_block() == NULL)
3656 ir_node *block = get_label_block(statement->label);
3657 ir_node *jmp = new_Jmp();
3658 add_immBlock_pred(block, jmp);
3660 set_cur_block(NULL);
3663 typedef enum modifier_t {
3664 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
3665 ASM_MODIFIER_READ_WRITE = 1 << 1,
3666 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
3667 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
3670 static void asm_statement_to_firm(const asm_statement_t *statement)
3673 fprintf(stderr, "WARNING asm not implemented yet!\n");
3675 bool needs_memory = false;
3677 size_t n_clobbers = 0;
3678 asm_clobber_t *clobber = statement->clobbers;
3679 for( ; clobber != NULL; clobber = clobber->next) {
3680 if(strcmp(clobber->clobber, "memory") == 0) {
3681 needs_memory = true;
3685 ident *id = new_id_from_str(clobber->clobber);
3686 obstack_ptr_grow(&asm_obst, id);
3689 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
3690 ident **clobbers = NULL;
3691 if(n_clobbers > 0) {
3692 clobbers = obstack_finish(&asm_obst);
3695 /* find and count input and output constraints */
3696 asm_constraint_t *constraint = statement->inputs;
3697 for( ; constraint != NULL; constraint = constraint->next) {
3699 bool supports_memop = false;
3700 for(const char *c = constraint->constraints; *c != 0; ++c) {
3701 /* TODO: improve error messages */
3705 panic("multiple alternative assembler constraints not "
3713 supports_memop = true;
3714 obstack_1grow(&asm_obst, *c);
3717 if(modifiers & ASM_MODIFIER_READ_WRITE)
3718 panic("inconsistent register constraints");
3719 modifiers |= ASM_MODIFIER_WRITE_ONLY;
3722 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
3723 panic("inconsistent register constraints");
3724 modifiers |= ASM_MODIFIER_READ_WRITE;
3727 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
3728 panic("early clobber assembler constraint not supported yet");
3731 modifiers |= ASM_MODIFIER_COMMUTATIVE;
3732 panic("commutative assembler constraint not supported yet");
3735 /* skip register preferences stuff... */
3736 while(*c != 0 && *c != ',')
3740 /* skip register preferences stuff... */
3744 obstack_1grow(&asm_obst, *c);
3748 obstack_1grow(&asm_obst, '\0');
3749 const char *constraint_string = obstack_finish(&asm_obst);
3751 needs_memory |= supports_memop;
3752 if(supports_memop) {
3759 static void statement_to_firm(statement_t *statement)
3761 switch(statement->kind) {
3762 case STATEMENT_INVALID:
3763 panic("invalid statement found");
3764 case STATEMENT_COMPOUND:
3765 compound_statement_to_firm(&statement->compound);
3767 case STATEMENT_RETURN:
3768 return_statement_to_firm(&statement->returns);
3770 case STATEMENT_EXPRESSION:
3771 expression_statement_to_firm(&statement->expression);
3774 if_statement_to_firm(&statement->ifs);
3776 case STATEMENT_WHILE:
3777 while_statement_to_firm(&statement->whiles);
3779 case STATEMENT_DO_WHILE:
3780 do_while_statement_to_firm(&statement->do_while);
3782 case STATEMENT_DECLARATION:
3783 declaration_statement_to_firm(&statement->declaration);
3785 case STATEMENT_BREAK:
3786 create_jump_statement(statement, break_label);
3788 case STATEMENT_CONTINUE:
3789 create_jump_statement(statement, continue_label);
3791 case STATEMENT_SWITCH:
3792 switch_statement_to_firm(&statement->switchs);
3794 case STATEMENT_CASE_LABEL:
3795 case_label_to_firm(&statement->case_label);
3798 for_statement_to_firm(&statement->fors);
3800 case STATEMENT_LABEL:
3801 label_to_firm(&statement->label);
3803 case STATEMENT_GOTO:
3804 goto_to_firm(&statement->gotos);
3807 asm_statement_to_firm(&statement->asms);
3810 panic("Statement not implemented\n");
3813 static int count_decls_in_expression(const expression_t *expression);
3815 static int count_local_declarations(const declaration_t * decl,
3816 const declaration_t *const end)
3819 for (; decl != end; decl = decl->next) {
3820 if(decl->namespc != NAMESPACE_NORMAL)
3822 const type_t *type = skip_typeref(decl->type);
3823 if (!decl->address_taken && is_type_scalar(type))
3825 const initializer_t *initializer = decl->init.initializer;
3826 /* FIXME: should walk initializer hierarchies... */
3827 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
3828 count += count_decls_in_expression(initializer->value.value);
3834 static int count_decls_in_expression(const expression_t *expression) {
3835 if(expression == NULL)
3838 switch(expression->base.kind) {
3839 case EXPR_STATEMENT:
3840 return count_decls_in_stmts(expression->statement.statement);
3842 int count_left = count_decls_in_expression(expression->binary.left);
3843 int count_right = count_decls_in_expression(expression->binary.right);
3844 return count_left + count_right;
3847 return count_decls_in_expression(expression->unary.value);
3850 call_argument_t *argument = expression->call.arguments;
3851 for( ; argument != NULL; argument = argument->next) {
3852 count += count_decls_in_expression(argument->expression);
3861 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
3862 * (or implement all the missing expressions here/implement a walker)
3868 static int count_decls_in_stmts(const statement_t *stmt)
3871 for (; stmt != NULL; stmt = stmt->base.next) {
3872 switch (stmt->kind) {
3873 case STATEMENT_DECLARATION: {
3874 const declaration_statement_t *const decl_stmt = &stmt->declaration;
3875 count += count_local_declarations(decl_stmt->declarations_begin,
3876 decl_stmt->declarations_end->next);
3880 case STATEMENT_COMPOUND: {
3881 const compound_statement_t *const comp =
3883 count += count_decls_in_stmts(comp->statements);
3887 case STATEMENT_IF: {
3888 const if_statement_t *const if_stmt = &stmt->ifs;
3889 count += count_decls_in_expression(if_stmt->condition);
3890 count += count_decls_in_stmts(if_stmt->true_statement);
3891 count += count_decls_in_stmts(if_stmt->false_statement);
3895 case STATEMENT_SWITCH: {
3896 const switch_statement_t *const switch_stmt = &stmt->switchs;
3897 count += count_decls_in_expression(switch_stmt->expression);
3898 count += count_decls_in_stmts(switch_stmt->body);
3902 case STATEMENT_LABEL: {
3903 const label_statement_t *const label_stmt = &stmt->label;
3904 if(label_stmt->statement != NULL) {
3905 count += count_decls_in_stmts(label_stmt->statement);
3910 case STATEMENT_WHILE: {
3911 const while_statement_t *const while_stmt = &stmt->whiles;
3912 count += count_decls_in_expression(while_stmt->condition);
3913 count += count_decls_in_stmts(while_stmt->body);
3917 case STATEMENT_DO_WHILE: {
3918 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
3919 count += count_decls_in_expression(do_while_stmt->condition);
3920 count += count_decls_in_stmts(do_while_stmt->body);
3924 case STATEMENT_FOR: {
3925 const for_statement_t *const for_stmt = &stmt->fors;
3926 count += count_local_declarations(for_stmt->scope.declarations, NULL);
3927 count += count_decls_in_expression(for_stmt->initialisation);
3928 count += count_decls_in_expression(for_stmt->condition);
3929 count += count_decls_in_expression(for_stmt->step);
3930 count += count_decls_in_stmts(for_stmt->body);
3934 case STATEMENT_CASE_LABEL: {
3935 const case_label_statement_t *label = &stmt->case_label;
3936 count += count_decls_in_expression(label->expression);
3937 if(label->statement != NULL) {
3938 count += count_decls_in_stmts(label->statement);
3944 case STATEMENT_BREAK:
3945 case STATEMENT_CONTINUE:
3948 case STATEMENT_EXPRESSION: {
3949 const expression_statement_t *expr_stmt = &stmt->expression;
3950 count += count_decls_in_expression(expr_stmt->expression);
3954 case STATEMENT_GOTO:
3955 case STATEMENT_INVALID:
3958 case STATEMENT_RETURN: {
3959 const return_statement_t *ret_stmt = &stmt->returns;
3960 count += count_decls_in_expression(ret_stmt->value);
3968 static int get_function_n_local_vars(declaration_t *declaration)
3972 /* count parameters */
3973 count += count_local_declarations(declaration->scope.declarations, NULL);
3975 /* count local variables declared in body */
3976 count += count_decls_in_stmts(declaration->init.statement);
3981 static void initialize_function_parameters(declaration_t *declaration)
3983 ir_graph *irg = current_ir_graph;
3984 ir_node *args = get_irg_args(irg);
3985 ir_node *start_block = get_irg_start_block(irg);
3986 ir_type *function_irtype = get_ir_type(declaration->type);
3989 declaration_t *parameter = declaration->scope.declarations;
3990 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3991 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
3992 type_t *type = skip_typeref(parameter->type);
3994 bool needs_entity = parameter->address_taken;
3995 assert(!is_type_array(type));
3996 if(is_type_compound(type)) {
3997 needs_entity = true;
4001 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
4002 ident *id = new_id_from_str(parameter->symbol->string);
4003 set_entity_ident(entity, id);
4005 parameter->declaration_kind
4006 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
4007 parameter->v.entity = entity;
4011 ir_mode *mode = get_ir_mode(parameter->type);
4013 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
4015 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
4016 parameter->v.value_number = next_value_number_function;
4017 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
4018 ++next_value_number_function;
4020 set_value(parameter->v.value_number, proj);
4025 * Handle additional decl modifiers for IR-graphs
4027 * @param irg the IR-graph
4028 * @param dec_modifiers additional modifiers
4030 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
4032 if (decl_modifiers & DM_NORETURN) {
4033 /* TRUE if the declaration includes the Microsoft
4034 __declspec(noreturn) specifier. */
4035 set_irg_additional_property(irg, mtp_property_noreturn);
4037 if (decl_modifiers & DM_NOTHROW) {
4038 /* TRUE if the declaration includes the Microsoft
4039 __declspec(nothrow) specifier. */
4040 set_irg_additional_property(irg, mtp_property_nothrow);
4042 if (decl_modifiers & DM_NAKED) {
4043 /* TRUE if the declaration includes the Microsoft
4044 __declspec(naked) specifier. */
4045 set_irg_additional_property(irg, mtp_property_naked);
4047 if (decl_modifiers & DM_FORCEINLINE) {
4048 /* TRUE if the declaration includes the
4049 Microsoft __forceinline specifier. */
4050 set_irg_inline_property(irg, irg_inline_forced);
4052 if (decl_modifiers & DM_NOINLINE) {
4053 /* TRUE if the declaration includes the Microsoft
4054 __declspec(noinline) specifier. */
4055 set_irg_inline_property(irg, irg_inline_forbidden);
4059 static void create_function(declaration_t *declaration)
4061 ir_entity *function_entity = get_function_entity(declaration);
4063 if(declaration->init.statement == NULL)
4066 current_function_decl = declaration;
4067 current_function_name = NULL;
4069 assert(imature_blocks == NULL);
4070 imature_blocks = NEW_ARR_F(ir_node*, 0);
4072 int n_local_vars = get_function_n_local_vars(declaration);
4073 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
4074 ir_node *first_block = get_cur_block();
4076 /* set inline flags */
4077 if (declaration->is_inline)
4078 set_irg_inline_property(irg, irg_inline_recomended);
4079 handle_decl_modifier_irg(irg, declaration->modifiers);
4081 next_value_number_function = 0;
4082 initialize_function_parameters(declaration);
4084 statement_to_firm(declaration->init.statement);
4086 ir_node *end_block = get_irg_end_block(irg);
4088 /* do we have a return statement yet? */
4089 if(get_cur_block() != NULL) {
4090 type_t *type = skip_typeref(declaration->type);
4091 assert(is_type_function(type));
4092 const function_type_t *func_type = &type->function;
4093 const type_t *return_type
4094 = skip_typeref(func_type->return_type);
4097 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4098 ret = new_Return(get_store(), 0, NULL);
4101 if(is_type_scalar(return_type)) {
4102 mode = get_ir_mode(func_type->return_type);
4108 /* ยง5.1.2.2.3 main implicitly returns 0 */
4109 if (strcmp(declaration->symbol->string, "main") == 0) {
4110 in[0] = new_Const(mode, get_mode_null(mode));
4112 in[0] = new_Unknown(mode);
4114 ret = new_Return(get_store(), 1, in);
4116 add_immBlock_pred(end_block, ret);
4119 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
4120 mature_immBlock(imature_blocks[i]);
4122 DEL_ARR_F(imature_blocks);
4123 imature_blocks = NULL;
4125 mature_immBlock(first_block);
4126 mature_immBlock(end_block);
4128 irg_finalize_cons(irg);
4130 /* finalize the frame type */
4131 ir_type *frame_type = get_irg_frame_type(irg);
4132 int n = get_compound_n_members(frame_type);
4135 for(int i = 0; i < n; ++i) {
4136 ir_entity *entity = get_compound_member(frame_type, i);
4137 ir_type *entity_type = get_entity_type(entity);
4139 int align = get_type_alignment_bytes(entity_type);
4140 if(align > align_all)
4144 misalign = offset % align;
4146 offset += align - misalign;
4150 set_entity_offset(entity, offset);
4151 offset += get_type_size_bytes(entity_type);
4153 set_type_size_bytes(frame_type, offset);
4154 set_type_alignment_bytes(frame_type, align_all);
4155 set_type_state(frame_type, layout_fixed);
4160 static void create_global_variable(declaration_t *declaration)
4164 switch ((storage_class_tag_t)declaration->storage_class) {
4165 case STORAGE_CLASS_STATIC:
4166 vis = visibility_local;
4169 case STORAGE_CLASS_EXTERN:
4170 vis = visibility_external_allocated;
4173 case STORAGE_CLASS_NONE:
4174 vis = visibility_external_visible;
4177 case STORAGE_CLASS_THREAD:
4178 vis = visibility_external_visible;
4181 case STORAGE_CLASS_THREAD_EXTERN:
4182 vis = visibility_external_allocated;
4185 case STORAGE_CLASS_THREAD_STATIC:
4186 vis = visibility_local;
4190 var_type = get_tls_type();
4194 var_type = get_glob_type();
4198 create_declaration_entity(declaration,
4199 DECLARATION_KIND_GLOBAL_VARIABLE,
4201 set_entity_visibility(declaration->v.entity, vis);
4205 case STORAGE_CLASS_TYPEDEF:
4206 case STORAGE_CLASS_AUTO:
4207 case STORAGE_CLASS_REGISTER:
4208 case STORAGE_CLASS_ENUM_ENTRY:
4211 panic("Invalid storage class for global variable");
4214 static void scope_to_firm(scope_t *scope)
4216 /* first pass: create declarations */
4217 declaration_t *declaration = scope->declarations;
4218 for( ; declaration != NULL; declaration = declaration->next) {
4219 if(declaration->namespc != NAMESPACE_NORMAL)
4221 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4222 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4224 if(declaration->symbol == NULL)
4227 type_t *type = skip_typeref(declaration->type);
4228 if(is_type_function(type)) {
4229 get_function_entity(declaration);
4231 create_global_variable(declaration);
4235 /* second pass: create code/initializers */
4236 declaration = scope->declarations;
4237 for( ; declaration != NULL; declaration = declaration->next) {
4238 if(declaration->namespc != NAMESPACE_NORMAL)
4240 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4241 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4243 if(declaration->symbol == NULL)
4246 type_t *type = declaration->type;
4247 if(type->kind == TYPE_FUNCTION) {
4248 create_function(declaration);
4250 assert(declaration->declaration_kind
4251 == DECLARATION_KIND_GLOBAL_VARIABLE);
4252 current_ir_graph = get_const_code_irg();
4253 create_initializer(declaration);
4258 void init_ast2firm(void)
4260 obstack_init(&asm_obst);
4261 init_atomic_modes();
4263 /* create idents for all known runtime functions */
4264 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
4265 predef_idents[rts_data[i].id] = new_id_from_str(rts_data[i].name);
4269 void exit_ast2firm(void)
4271 obstack_free(&asm_obst, NULL);
4274 void translation_unit_to_firm(translation_unit_t *unit)
4276 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4277 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4278 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4280 ir_type_int = get_ir_type(type_int);
4281 ir_type_const_char = get_ir_type(type_const_char);
4282 ir_type_wchar_t = get_ir_type(type_wchar_t);
4283 ir_type_void = get_ir_type(type_int); /* we don't have a real void
4286 type_void->base.firm_type = ir_type_void;
4288 /* just to be sure */
4289 continue_label = NULL;
4291 current_switch_cond = NULL;
4293 scope_to_firm(&unit->scope);