2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include <libfirm/firm.h>
27 #include <libfirm/adt/obst.h>
31 #include "adt/error.h"
32 #include "adt/array.h"
38 #include "diagnostic.h"
39 #include "lang_features.h"
41 #include "driver/firm_opt.h"
42 #include "driver/firm_cmdline.h"
44 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
46 static ir_type *ir_type_const_char;
47 static ir_type *ir_type_wchar_t;
48 static ir_type *ir_type_void;
49 static ir_type *ir_type_int;
51 static type_t *type_const_char;
53 static int next_value_number_function;
54 static ir_node *continue_label;
55 static ir_node *break_label;
56 static ir_node *current_switch_cond;
57 static bool saw_default_label;
58 static ir_node **imature_blocks;
60 static const declaration_t *current_function_decl;
61 static ir_node *current_function_name;
62 static ir_node *current_funcsig;
64 static struct obstack asm_obst;
66 typedef enum declaration_kind_t {
67 DECLARATION_KIND_UNKNOWN,
68 DECLARATION_KIND_FUNCTION,
69 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
70 DECLARATION_KIND_GLOBAL_VARIABLE,
71 DECLARATION_KIND_LOCAL_VARIABLE,
72 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
73 DECLARATION_KIND_COMPOUND_MEMBER,
74 DECLARATION_KIND_LABEL_BLOCK,
75 DECLARATION_KIND_ENUM_ENTRY
78 static ir_type *get_ir_type(type_t *type);
79 static int count_decls_in_stmts(const statement_t *stmt);
81 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
83 const declaration_t *declaration = get_irg_loc_description(irg, pos);
85 warningf(declaration->source_position,
86 "variable '%#T' might be used uninitialized",
87 declaration->type, declaration->symbol);
88 return new_r_Unknown(irg, mode);
91 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
93 const source_position_t *pos = (const source_position_t*) dbg;
96 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
100 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
102 const source_position_t *pos = (const source_position_t*) dbg;
107 return pos->input_name;
110 static dbg_info *get_dbg_info(const source_position_t *pos)
112 return (dbg_info*) pos;
115 static unsigned unique_id = 0;
117 static ident *unique_ident(const char *tag)
121 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
123 return new_id_from_str(buf);
127 * Return the signed integer mode of size bytes.
129 * @param size the size
131 static ir_mode *get_smode(unsigned size)
133 static ir_mode *s_modes[16 + 1] = {0, };
136 if (size <= 0 || size > 16)
145 snprintf(name, sizeof(name), "i%u", bits);
146 res = new_ir_mode(name, irms_int_number, bits, 1, irma_twos_complement,
147 bits <= machine_size ? machine_size : bits );
155 * Return the unsigned integer mode of size bytes.
157 * @param size the size
159 static ir_mode *get_umode(unsigned size)
161 static ir_mode *u_modes[16 + 1] = {0, };
164 if (size <= 0 || size > 16)
173 snprintf(name, sizeof(name), "u%u", bits);
174 res = new_ir_mode(name, irms_int_number, bits, 0, irma_twos_complement,
175 bits <= machine_size ? machine_size : bits );
183 * Return the pointer mode of size bytes.
185 * @param size the size
187 static ir_mode *get_ptrmode(unsigned size, char *name)
189 static ir_mode *p_modes[16 + 1] = {0, };
192 if (size <= 0 || size > 16)
202 snprintf(buf, sizeof(buf), "p%u", bits);
205 res = new_ir_mode(name, irms_reference, bits, 0, irma_twos_complement,
206 bits <= machine_size ? machine_size : bits);
210 set_reference_mode_signed_eq(res, get_smode(size));
211 set_reference_mode_unsigned_eq(res, get_umode(size));
216 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST+1];
218 static ir_mode *mode_int, *mode_uint;
220 static ir_node *expression_to_firm(const expression_t *expression);
221 static inline ir_mode *get_ir_mode(type_t *type);
224 * Initialises the atomic modes depending on the machine size.
226 static void init_atomic_modes(void) {
227 unsigned int_size = machine_size < 32 ? 2 : 4;
228 unsigned long_size = machine_size < 64 ? 4 : 8;
229 unsigned llong_size = machine_size < 32 ? 4 : 8;
231 /* firm has no real void... */
232 _atomic_modes[ATOMIC_TYPE_VOID] = mode_T;
233 _atomic_modes[ATOMIC_TYPE_CHAR] = char_is_signed ? get_smode(1) : get_umode(1);
234 _atomic_modes[ATOMIC_TYPE_SCHAR] = get_smode(1);
235 _atomic_modes[ATOMIC_TYPE_UCHAR] = get_umode(1);
236 _atomic_modes[ATOMIC_TYPE_SHORT] = get_smode(2);
237 _atomic_modes[ATOMIC_TYPE_USHORT] = get_umode(2);
238 _atomic_modes[ATOMIC_TYPE_INT] = get_smode(int_size);
239 _atomic_modes[ATOMIC_TYPE_UINT] = get_umode(int_size);
240 _atomic_modes[ATOMIC_TYPE_LONG] = get_smode(long_size);
241 _atomic_modes[ATOMIC_TYPE_ULONG] = get_umode(long_size);
242 _atomic_modes[ATOMIC_TYPE_LONGLONG] = get_smode(llong_size);
243 _atomic_modes[ATOMIC_TYPE_ULONGLONG] = get_umode(llong_size);
244 _atomic_modes[ATOMIC_TYPE_FLOAT] = mode_F;
245 _atomic_modes[ATOMIC_TYPE_DOUBLE] = mode_D;
246 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = mode_E;
247 _atomic_modes[ATOMIC_TYPE_BOOL] = get_umode(int_size);
249 _atomic_modes[ATOMIC_TYPE_BOOL] = _atomic_modes[ATOMIC_TYPE_INT];
250 _atomic_modes[ATOMIC_TYPE_FLOAT_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_FLOAT];
251 _atomic_modes[ATOMIC_TYPE_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_DOUBLE];
252 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE];
254 /* Hmm, pointers should be machine size */
255 set_modeP_data(get_ptrmode(machine_size >> 3, NULL));
256 set_modeP_code(get_ptrmode(machine_size >> 3, NULL));
258 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
259 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
262 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
265 if ((unsigned)atomic_type->akind <= (unsigned)ATOMIC_TYPE_LAST)
266 res = _atomic_modes[(unsigned)atomic_type->akind];
268 panic("Encountered unknown atomic type");
272 static unsigned get_compound_type_size(compound_type_t *type)
274 ir_type *irtype = get_ir_type((type_t*) type);
275 return get_type_size_bytes(irtype);
278 static unsigned get_array_type_size(array_type_t *type)
280 assert(!type->is_vla);
281 ir_type *irtype = get_ir_type((type_t*) type);
282 return get_type_size_bytes(irtype);
286 static unsigned get_type_size_const(type_t *type)
288 type = skip_typeref(type);
292 panic("error type occured");
294 return get_atomic_type_size(type->atomic.akind);
296 return get_mode_size_bytes(mode_int);
297 case TYPE_COMPOUND_UNION:
298 case TYPE_COMPOUND_STRUCT:
299 return get_compound_type_size(&type->compound);
301 /* just a pointer to the function */
302 return get_mode_size_bytes(mode_P_code);
304 return get_mode_size_bytes(mode_P_data);
306 return get_array_type_size(&type->array);
308 return get_type_size_const(type->builtin.real_type);
310 panic("type size of bitfield request");
316 panic("Trying to determine size of invalid type");
319 static ir_node *get_type_size(type_t *type)
321 type = skip_typeref(type);
323 if(is_type_array(type) && type->array.is_vla) {
324 ir_node *size_node = type->array.size_node;
325 if(size_node == NULL) {
326 size_node = expression_to_firm(type->array.size_expression);
327 assert(!is_Const(size_node));
328 type->array.size_node = size_node;
331 ir_node *elem_size = get_type_size(type->array.element_type);
332 ir_mode *mode = get_irn_mode(size_node);
333 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
337 ir_mode *mode = get_ir_mode(type_size_t);
339 sym.type_p = get_ir_type(type);
340 return new_SymConst(mode, sym, symconst_type_size);
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) {
356 static ir_type *create_atomic_type(const atomic_type_t *type)
358 dbg_info *dbgi = get_dbg_info(&type->type.source_position);
359 ir_mode *mode = get_atomic_mode(type);
360 ident *id = get_mode_ident(mode);
361 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
363 /* TODO: this is x86 specific, we should fiddle this into
364 * lang_features.h somehow... */
365 if(type->akind == ATOMIC_TYPE_LONG_DOUBLE
366 || type->akind == ATOMIC_TYPE_DOUBLE
367 || type->akind == ATOMIC_TYPE_LONGLONG
368 || type->akind == ATOMIC_TYPE_ULONGLONG) {
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_constant) {
437 int n_elements = 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;
688 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 declaration->v.irtype = irtype;
705 type->type.firm_type = irtype;
707 offset = *outer_offset;
708 align_all = *outer_align;
711 type->type.firm_type = irtype;
713 declaration_t *entry = declaration->scope.declarations;
714 for( ; entry != NULL; entry = entry->next) {
715 if(entry->namespc != NAMESPACE_NORMAL)
718 symbol_t *symbol = entry->symbol;
719 type_t *entry_type = skip_typeref(entry->type);
720 ir_type *entry_ir_type = get_ir_type(entry_type);
724 ident = new_id_from_str(entry->symbol->string);
726 size_t offs = offset;
727 if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
728 create_struct_type(&entry_type->compound, irtype, &offs,
731 } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
732 create_union_type(&entry_type->compound, irtype, &offs,
736 panic("anonymous union member must be struct or union");
738 size_t entry_size = offs - offset;
739 if(entry_size > size) {
742 ident = unique_ident("anon");
745 size_t entry_size = get_type_size_bytes(entry_ir_type);
746 size_t entry_alignment = get_type_alignment_bytes(entry_ir_type);
748 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
749 ir_entity *const entity = new_d_entity(irtype, ident, entry_ir_type,
751 //add_union_member(irtype, entity);
752 set_entity_offset(entity, 0);
753 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
754 assert(entry->v.entity == NULL);
755 entry->v.entity = entity;
757 if(entry_size > size) {
760 if(entry_alignment > align_all) {
761 if(entry_alignment % align_all != 0) {
762 panic("Uneven alignments not supported yet");
764 align_all = entry_alignment;
768 if(outer_offset != NULL) {
769 assert(*outer_offset == offset);
771 size_t misalign = offset % align_all;
773 size += align_all - misalign;
774 *outer_offset += size;
776 if(align_all > *outer_align) {
777 if(align_all % *outer_align != 0) {
778 panic("uneven alignments not supported yet");
780 *outer_align = align_all;
783 set_type_alignment_bytes(irtype, align_all);
784 set_type_size_bytes(irtype, size);
785 set_type_state(irtype, layout_fixed);
786 declaration->v.irtype = irtype;
792 static ir_type *create_enum_type(enum_type_t *const type)
794 type->type.firm_type = ir_type_int;
796 ir_mode *const mode = mode_int;
797 tarval *const one = get_mode_one(mode);
798 tarval * tv_next = get_tarval_null(mode);
800 declaration_t *declaration = type->declaration->next;
801 for (; declaration != NULL; declaration = declaration->next) {
802 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
805 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
807 expression_t *const init = declaration->init.enum_value;
809 ir_node *const cnst = expression_to_firm(init);
810 if (!is_Const(cnst)) {
811 panic("couldn't fold constant");
813 tv_next = get_Const_tarval(cnst);
815 declaration->v.enum_val = tv_next;
816 tv_next = tarval_add(tv_next, one);
822 static ir_type *get_ir_type(type_t *type)
824 assert(type != NULL);
826 type = skip_typeref(type);
828 if(type->base.firm_type != NULL) {
829 assert(type->base.firm_type != INVALID_TYPE);
830 return type->base.firm_type;
833 ir_type *firm_type = NULL;
836 panic("error type occured");
838 firm_type = create_atomic_type(&type->atomic);
841 firm_type = create_method_type(&type->function);
844 firm_type = create_pointer_type(&type->pointer);
847 firm_type = create_array_type(&type->array);
849 case TYPE_COMPOUND_STRUCT:
850 firm_type = create_struct_type(&type->compound, NULL, NULL, NULL);
852 case TYPE_COMPOUND_UNION:
853 firm_type = create_union_type(&type->compound, NULL, NULL, NULL);
856 firm_type = create_enum_type(&type->enumt);
859 firm_type = get_ir_type(type->builtin.real_type);
862 firm_type = create_bitfield_type(&type->bitfield);
870 if(firm_type == NULL)
871 panic("unknown type found");
873 type->base.firm_type = firm_type;
877 static inline ir_mode *get_ir_mode(type_t *type)
879 ir_type *irtype = get_ir_type(type);
881 /* firm doesn't report a mode for arrays somehow... */
882 if(is_Array_type(irtype)) {
886 ir_mode *mode = get_type_mode(irtype);
887 assert(mode != NULL);
891 static ident *predef_idents[rts_max];
893 /** Names of the runtime functions. */
894 static const struct {
895 int id; /**< the rts id */
896 int n_res; /**< number of return values */
897 const char *name; /**< the name of the rts function */
898 int n_params; /**< number of parameters */
899 unsigned flags; /**< language flags */
901 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
902 { rts_abort, 0, "abort", 0, _C89 },
903 { rts_abs, 1, "abs", 1, _C89 },
904 { rts_labs, 1, "labs", 1, _C89 },
905 { rts_llabs, 1, "llabs", 1, _C99 },
906 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
908 { rts_fabs, 1, "fabs", 1, _C89 },
909 { rts_sqrt, 1, "sqrt", 1, _C89 },
910 { rts_cbrt, 1, "cbrt", 1, _C99 },
911 { rts_exp, 1, "exp", 1, _C89 },
912 { rts_exp2, 1, "exp2", 1, _C89 },
913 { rts_exp10, 1, "exp10", 1, _GNUC },
914 { rts_log, 1, "log", 1, _C89 },
915 { rts_log2, 1, "log2", 1, _C89 },
916 { rts_log10, 1, "log10", 1, _C89 },
917 { rts_pow, 1, "pow", 2, _C89 },
918 { rts_sin, 1, "sin", 1, _C89 },
919 { rts_cos, 1, "cos", 1, _C89 },
920 { rts_tan, 1, "tan", 1, _C89 },
921 { rts_asin, 1, "asin", 1, _C89 },
922 { rts_acos, 1, "acos", 1, _C89 },
923 { rts_atan, 1, "atan", 1, _C89 },
924 { rts_sinh, 1, "sinh", 1, _C89 },
925 { rts_cosh, 1, "cosh", 1, _C89 },
926 { rts_tanh, 1, "tanh", 1, _C89 },
928 { rts_fabsf, 1, "fabsf", 1, _C99 },
929 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
930 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
931 { rts_expf, 1, "expf", 1, _C99 },
932 { rts_exp2f, 1, "exp2f", 1, _C99 },
933 { rts_exp10f, 1, "exp10f", 1, _GNUC },
934 { rts_logf, 1, "logf", 1, _C99 },
935 { rts_log2f, 1, "log2f", 1, _C99 },
936 { rts_log10f, 1, "log10f", 1, _C99 },
937 { rts_powf, 1, "powf", 2, _C99 },
938 { rts_sinf, 1, "sinf", 1, _C99 },
939 { rts_cosf, 1, "cosf", 1, _C99 },
940 { rts_tanf, 1, "tanf", 1, _C99 },
941 { rts_asinf, 1, "asinf", 1, _C99 },
942 { rts_acosf, 1, "acosf", 1, _C99 },
943 { rts_atanf, 1, "atanf", 1, _C99 },
944 { rts_sinhf, 1, "sinhf", 1, _C99 },
945 { rts_coshf, 1, "coshf", 1, _C99 },
946 { rts_tanhf, 1, "tanhf", 1, _C99 },
948 { rts_fabsl, 1, "fabsl", 1, _C99 },
949 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
950 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
951 { rts_expl, 1, "expl", 1, _C99 },
952 { rts_exp2l, 1, "exp2l", 1, _C99 },
953 { rts_exp10l, 1, "exp10l", 1, _GNUC },
954 { rts_logl, 1, "logl", 1, _C99 },
955 { rts_log2l, 1, "log2l", 1, _C99 },
956 { rts_log10l, 1, "log10l", 1, _C99 },
957 { rts_powl, 1, "powl", 2, _C99 },
958 { rts_sinl, 1, "sinl", 1, _C99 },
959 { rts_cosl, 1, "cosl", 1, _C99 },
960 { rts_tanl, 1, "tanl", 1, _C99 },
961 { rts_asinl, 1, "asinl", 1, _C99 },
962 { rts_acosl, 1, "acosl", 1, _C99 },
963 { rts_atanl, 1, "atanl", 1, _C99 },
964 { rts_sinhl, 1, "sinhl", 1, _C99 },
965 { rts_coshl, 1, "coshl", 1, _C99 },
966 { rts_tanhl, 1, "tanhl", 1, _C99 },
968 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
969 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
970 { rts_strcmp, 1, "strcmp", 2, _C89 },
971 { rts_strncmp, 1, "strncmp", 3, _C89 }
974 static ir_entity* get_function_entity(declaration_t *declaration)
976 if(declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
977 return declaration->v.entity;
978 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
980 symbol_t *symbol = declaration->symbol;
981 ident *id = new_id_from_str(symbol->string);
983 ir_type *global_type = get_glob_type();
984 ir_type *ir_type_method = get_ir_type(declaration->type);
985 assert(is_Method_type(ir_type_method));
987 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
988 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
989 set_entity_ld_ident(entity, id);
990 if(declaration->storage_class == STORAGE_CLASS_STATIC
991 || declaration->is_inline) {
992 set_entity_visibility(entity, visibility_local);
993 } else if(declaration->init.statement != NULL) {
994 set_entity_visibility(entity, visibility_external_visible);
996 set_entity_visibility(entity, visibility_external_allocated);
998 /* We should check for file scope here, but as long as we compile C only
999 this is not needed. */
1000 int n_params = get_method_n_params(ir_type_method);
1001 int n_res = get_method_n_ress(ir_type_method);
1004 if (n_params == 0 && n_res == 0 && id == predef_idents[rts_abort]) {
1005 /* found abort(), store for later */
1009 if (! firm_opt.freestanding) {
1010 /* check for a known runtime function */
1011 for (i = 0; i < rts_max; ++i) {
1012 /* ignore those rts functions not necessary needed for current mode */
1013 if ((c_mode & rts_data[i].flags) == 0)
1015 if (n_params == rts_data[i].n_params && n_res == rts_data[i].n_res &&
1016 id == predef_idents[rts_data[i].id])
1017 rts_entities[rts_data[i].id] = entity;
1022 set_entity_allocation(entity, allocation_static);
1024 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1025 declaration->v.entity = entity;
1030 static ir_node *const_to_firm(const const_expression_t *cnst)
1032 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1033 ir_mode *mode = get_ir_mode(cnst->base.type);
1038 if(mode_is_float(mode)) {
1039 tv = new_tarval_from_double(cnst->v.float_value, mode);
1041 if(mode_is_signed(mode)) {
1042 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1044 len = snprintf(buf, sizeof(buf), "%llu",
1045 (unsigned long long) cnst->v.int_value);
1047 tv = new_tarval_from_str(buf, len, mode);
1050 return new_d_Const(dbgi, mode, tv);
1053 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1055 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1056 ir_mode *mode = get_ir_mode(cnst->base.type);
1058 long long int v = 0;
1059 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1060 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1063 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1064 tarval *tv = new_tarval_from_str(buf, len, mode);
1066 return new_d_Const(dbgi, mode, tv);
1069 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1071 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1072 ir_mode *mode = get_ir_mode(cnst->base.type);
1074 long long int v = cnst->v.wide_character.begin[0];
1077 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1078 tarval *tv = new_tarval_from_str(buf, len, mode);
1080 return new_d_Const(dbgi, mode, tv);
1083 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1086 assert(entity != NULL);
1087 union symconst_symbol sym;
1088 sym.entity_p = entity;
1089 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1092 static ir_node *string_to_firm(const source_position_t *const src_pos,
1093 const char *const id_prefix,
1094 const string_t *const value)
1096 ir_type *const global_type = get_glob_type();
1097 dbg_info *const dbgi = get_dbg_info(src_pos);
1098 ir_type *const type = new_d_type_array(unique_ident("strtype"), 1,
1099 ir_type_const_char, dbgi);
1101 ident *const id = unique_ident(id_prefix);
1102 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1103 set_entity_ld_ident(entity, id);
1104 set_entity_variability(entity, variability_constant);
1105 set_entity_allocation(entity, allocation_static);
1107 ir_type *const elem_type = ir_type_const_char;
1108 ir_mode *const mode = get_type_mode(elem_type);
1110 const char* const string = value->begin;
1111 const size_t slen = value->size;
1113 set_array_lower_bound_int(type, 0, 0);
1114 set_array_upper_bound_int(type, 0, slen);
1115 set_type_size_bytes(type, slen);
1116 set_type_state(type, layout_fixed);
1118 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1119 for(size_t i = 0; i < slen; ++i) {
1120 tvs[i] = new_tarval_from_long(string[i], mode);
1123 set_array_entity_values(entity, tvs, slen);
1126 return create_symconst(dbgi, mode_P_data, entity);
1129 static ir_node *string_literal_to_firm(
1130 const string_literal_expression_t* literal)
1132 return string_to_firm(&literal->base.source_position, "Lstr",
1136 static ir_node *wide_string_literal_to_firm(
1137 const wide_string_literal_expression_t* const literal)
1139 ir_type *const global_type = get_glob_type();
1140 ir_type *const elem_type = ir_type_wchar_t;
1141 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1142 ir_type *const type = new_d_type_array(unique_ident("strtype"), 1,
1145 ident *const id = unique_ident("Lstr");
1146 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1147 set_entity_ld_ident(entity, id);
1148 set_entity_variability(entity, variability_constant);
1149 set_entity_allocation(entity, allocation_static);
1151 ir_mode *const mode = get_type_mode(elem_type);
1153 const wchar_rep_t *const string = literal->value.begin;
1154 const size_t slen = literal->value.size;
1156 set_array_lower_bound_int(type, 0, 0);
1157 set_array_upper_bound_int(type, 0, slen);
1158 set_type_size_bytes(type, slen);
1159 set_type_state(type, layout_fixed);
1161 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1162 for(size_t i = 0; i < slen; ++i) {
1163 tvs[i] = new_tarval_from_long(string[i], mode);
1166 set_array_entity_values(entity, tvs, slen);
1169 return create_symconst(dbgi, mode_P_data, entity);
1172 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
1173 dbg_info *const dbgi)
1175 if (is_compound_type(irtype) ||
1176 is_Method_type(irtype) ||
1177 is_Array_type(irtype)) {
1181 ir_mode *const mode = get_type_mode(irtype);
1182 ir_node *const memory = get_store();
1183 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1184 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1185 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1186 set_store(load_mem);
1190 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1192 ir_mode *mode = get_irn_mode(node);
1194 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1196 if(!mode_is_float(mode))
1199 /* check if there is already a Conv */
1200 if (get_irn_op(node) == op_Conv) {
1201 /* convert it into a strict Conv */
1202 set_Conv_strict(node, 1);
1206 /* otherwise create a new one */
1207 return new_d_strictConv(dbgi, node, mode);
1210 static ir_node *get_global_var_address(dbg_info *const dbgi,
1211 const declaration_t *const decl)
1213 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1215 ir_entity *const entity = decl->v.entity;
1216 switch ((storage_class_tag_t)decl->storage_class) {
1217 case STORAGE_CLASS_THREAD:
1218 case STORAGE_CLASS_THREAD_EXTERN:
1219 case STORAGE_CLASS_THREAD_STATIC: {
1220 ir_node *const no_mem = new_NoMem();
1221 ir_node *const tls = get_irg_tls(current_ir_graph);
1222 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1226 return create_symconst(dbgi, mode_P_data, entity);
1230 /* Returns the correct base address depending on whether it is a parameter or a
1231 * normal local variable */
1232 static ir_node *get_local_frame(ir_entity *const ent)
1234 ir_graph *const irg = current_ir_graph;
1235 const ir_type *const owner = get_entity_owner(ent);
1236 if (owner == get_irg_frame_type(irg)) {
1237 return get_irg_frame(irg);
1239 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1240 return get_irg_value_param_base(irg);
1244 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1246 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1247 declaration_t *declaration = ref->declaration;
1248 type_t *type = skip_typeref(declaration->type);
1250 switch((declaration_kind_t) declaration->declaration_kind) {
1251 case DECLARATION_KIND_UNKNOWN:
1252 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
1258 case DECLARATION_KIND_ENUM_ENTRY: {
1259 ir_mode *const mode = get_ir_mode(type);
1260 return new_Const(mode, declaration->v.enum_val);
1263 case DECLARATION_KIND_LOCAL_VARIABLE: {
1264 ir_mode *const mode = get_ir_mode(type);
1265 return get_value(declaration->v.value_number, mode);
1267 case DECLARATION_KIND_FUNCTION: {
1268 ir_mode *const mode = get_ir_mode(type);
1269 return create_symconst(dbgi, mode, declaration->v.entity);
1271 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1272 ir_node *const addr = get_global_var_address(dbgi, declaration);
1273 ir_type *const irtype = get_entity_type(declaration->v.entity);
1274 return deref_address(irtype, addr, dbgi);
1277 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1278 ir_entity *entity = declaration->v.entity;
1279 ir_node *frame = get_local_frame(entity);
1280 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1281 ir_type *irtype = get_entity_type(entity);
1282 return deref_address(irtype, sel, dbgi);
1285 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1286 return declaration->v.vla_base;
1288 case DECLARATION_KIND_COMPOUND_MEMBER:
1289 case DECLARATION_KIND_LABEL_BLOCK:
1290 panic("not implemented reference type");
1293 panic("reference to declaration with unknown type found");
1296 static ir_node *reference_addr(const reference_expression_t *ref)
1298 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1299 declaration_t *declaration = ref->declaration;
1301 switch((declaration_kind_t) declaration->declaration_kind) {
1302 case DECLARATION_KIND_UNKNOWN:
1304 case DECLARATION_KIND_LOCAL_VARIABLE:
1305 panic("local variable without entity has no address");
1306 case DECLARATION_KIND_FUNCTION: {
1307 type_t *const type = skip_typeref(ref->base.type);
1308 ir_mode *const mode = get_ir_mode(type);
1309 return create_symconst(dbgi, mode, declaration->v.entity);
1311 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1312 ir_node *const addr = get_global_var_address(dbgi, declaration);
1315 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1316 ir_entity *entity = declaration->v.entity;
1317 ir_node *frame = get_local_frame(entity);
1318 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1323 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1324 return declaration->v.vla_base;
1326 case DECLARATION_KIND_ENUM_ENTRY:
1327 panic("trying to reference enum entry");
1329 case DECLARATION_KIND_COMPOUND_MEMBER:
1330 case DECLARATION_KIND_LABEL_BLOCK:
1331 panic("not implemented reference type");
1334 panic("reference to declaration with unknown type found");
1337 static ir_node *process_builtin_call(const call_expression_t *call)
1339 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1341 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1342 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1344 type_t *type = skip_typeref(builtin->base.type);
1345 assert(is_type_pointer(type));
1347 type_t *function_type = skip_typeref(type->pointer.points_to);
1348 symbol_t *symbol = builtin->symbol;
1350 switch(symbol->ID) {
1351 case T___builtin_alloca: {
1352 if(call->arguments == NULL || call->arguments->next != NULL) {
1353 panic("invalid number of parameters on __builtin_alloca");
1355 expression_t *argument = call->arguments->expression;
1356 ir_node *size = expression_to_firm(argument);
1358 ir_node *store = get_store();
1359 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1361 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1363 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1367 case T___builtin_nan:
1368 case T___builtin_nanf:
1369 case T___builtin_nand: {
1370 /* Ignore string for now... */
1371 assert(is_type_function(function_type));
1372 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1373 tarval *tv = get_mode_NAN(mode);
1374 ir_node *res = new_d_Const(dbgi, mode, tv);
1377 case T___builtin_va_end:
1380 panic("Unsupported builtin found\n");
1384 static ir_node *call_expression_to_firm(const call_expression_t *call)
1386 assert(get_cur_block() != NULL);
1388 expression_t *function = call->function;
1389 if(function->kind == EXPR_BUILTIN_SYMBOL) {
1390 return process_builtin_call(call);
1392 ir_node *callee = expression_to_firm(function);
1394 type_t *type = skip_typeref(function->base.type);
1395 assert(is_type_pointer(type));
1396 pointer_type_t *pointer_type = &type->pointer;
1397 type_t *points_to = skip_typeref(pointer_type->points_to);
1398 assert(is_type_function(points_to));
1399 function_type_t *function_type = &points_to->function;
1401 int n_parameters = 0;
1402 call_argument_t *argument = call->arguments;
1403 for( ; argument != NULL; argument = argument->next) {
1407 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1409 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1410 ir_type *new_method_type = NULL;
1411 if(function_type->variadic || function_type->unspecified_parameters) {
1412 /* we need to construct a new method type matching the call
1414 int n_res = get_method_n_ress(ir_method_type);
1415 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1416 new_method_type = new_d_type_method(unique_ident("calltype"),
1417 n_parameters, n_res, dbgi);
1418 set_method_calling_convention(new_method_type,
1419 get_method_calling_convention(ir_method_type));
1420 set_method_additional_properties(new_method_type,
1421 get_method_additional_properties(ir_method_type));
1423 for(int i = 0; i < n_res; ++i) {
1424 set_method_res_type(new_method_type, i,
1425 get_method_res_type(ir_method_type, i));
1428 ir_node *in[n_parameters];
1430 argument = call->arguments;
1432 for( ; argument != NULL; argument = argument->next) {
1433 expression_t *expression = argument->expression;
1434 ir_node *arg_node = expression_to_firm(expression);
1436 arg_node = do_strict_conv(dbgi, arg_node);
1439 if(new_method_type != NULL) {
1440 ir_type *irtype = get_ir_type(expression->base.type);
1441 set_method_param_type(new_method_type, n, irtype);
1446 assert(n == n_parameters);
1448 if(new_method_type != NULL)
1449 ir_method_type = new_method_type;
1451 ir_node *store = get_store();
1452 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1454 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1457 type_t *return_type = skip_typeref(function_type->return_type);
1458 ir_node *result = NULL;
1460 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1462 if(is_type_scalar(return_type)) {
1463 mode = get_ir_mode(return_type);
1467 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1468 result = new_d_Proj(dbgi, resproj, mode, 0);
1474 static void statement_to_firm(statement_t *statement);
1475 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1477 static ir_node *expression_to_addr(const expression_t *expression);
1478 static void create_condition_evaluation(const expression_t *expression,
1479 ir_node *true_block,
1480 ir_node *false_block);
1482 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1485 value = do_strict_conv(dbgi, value);
1487 ir_node *memory = get_store();
1489 if(is_type_scalar(type)) {
1490 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1491 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1492 set_store(store_mem);
1494 ir_type *irtype = get_ir_type(type);
1495 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1496 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1497 set_store(copyb_mem);
1501 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1503 tarval *all_one = get_mode_all_one(mode);
1504 int mode_size = get_mode_size_bits(mode);
1506 assert(offset >= 0 && size >= 0);
1507 assert(offset + size <= mode_size);
1508 if(size == mode_size) {
1512 long shiftr = get_mode_size_bits(mode) - size;
1513 long shiftl = offset;
1514 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1515 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1516 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1517 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1522 static void bitfield_store_to_firm(const unary_expression_t *expression,
1525 expression_t *select = expression->value;
1526 assert(select->kind == EXPR_SELECT);
1527 type_t *type = select->base.type;
1528 assert(type->kind == TYPE_BITFIELD);
1529 ir_mode *mode = get_ir_mode(type->bitfield.base);
1530 ir_node *addr = expression_to_addr(select);
1532 assert(get_irn_mode(value) == mode);
1534 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1536 /* kill upper bits of value and shift to right position */
1537 ir_entity *entity = select->select.compound_entry->v.entity;
1538 int bitoffset = get_entity_offset_bits_remainder(entity);
1539 ir_type *entity_type = get_entity_type(entity);
1540 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1542 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1543 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1544 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1545 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1546 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1547 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1549 /* load current value */
1550 ir_node *mem = get_store();
1551 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1552 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1553 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1554 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1555 tarval *inv_mask = tarval_not(shift_mask);
1556 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1557 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1559 /* construct new value and store */
1560 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1561 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1562 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1563 set_store(store_mem);
1566 static void set_value_for_expression(const expression_t *expression,
1569 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1570 value = do_strict_conv(dbgi, value);
1572 if(expression->kind == EXPR_REFERENCE) {
1573 const reference_expression_t *ref = &expression->reference;
1575 declaration_t *declaration = ref->declaration;
1576 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1577 if(declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1578 set_value(declaration->v.value_number, value);
1583 if(expression->kind == EXPR_UNARY_BITFIELD_EXTRACT) {
1584 bitfield_store_to_firm(&expression->unary, value);
1588 ir_node *addr = expression_to_addr(expression);
1589 type_t *type = skip_typeref(expression->base.type);
1590 assign_value(dbgi, addr, type, value);
1593 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1595 ir_mode *value_mode = get_irn_mode(value);
1597 if (value_mode == dest_mode || is_Bad(value))
1600 if(dest_mode == mode_b) {
1601 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1602 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1603 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1607 return new_d_Conv(dbgi, value, dest_mode);
1610 static ir_node *create_incdec(const unary_expression_t *expression)
1612 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1613 type_t *type = skip_typeref(expression->base.type);
1614 ir_mode *mode = get_ir_mode(type);
1615 expression_t *value = expression->value;
1617 ir_node *value_node = expression_to_firm(value);
1620 if(is_type_pointer(type)) {
1621 pointer_type_t *pointer_type = &type->pointer;
1622 offset = get_type_size(pointer_type->points_to);
1624 assert(is_type_arithmetic(type));
1625 offset = new_Const(mode, get_mode_one(mode));
1628 switch(expression->base.kind) {
1629 case EXPR_UNARY_POSTFIX_INCREMENT: {
1630 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1631 set_value_for_expression(value, new_value);
1634 case EXPR_UNARY_POSTFIX_DECREMENT: {
1635 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1636 set_value_for_expression(value, new_value);
1639 case EXPR_UNARY_PREFIX_INCREMENT: {
1640 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1641 set_value_for_expression(value, new_value);
1644 case EXPR_UNARY_PREFIX_DECREMENT: {
1645 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1646 set_value_for_expression(value, new_value);
1650 panic("no incdec expr in create_incdec");
1655 static bool is_local_variable(expression_t *expression)
1657 if (expression->kind != EXPR_REFERENCE)
1659 reference_expression_t *ref_expr = &expression->reference;
1660 declaration_t *declaration = ref_expr->declaration;
1661 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1664 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1667 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1668 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1669 case EXPR_BINARY_NOTEQUAL:
1670 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1671 case EXPR_BINARY_ISLESS:
1672 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1673 case EXPR_BINARY_ISLESSEQUAL:
1674 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1675 case EXPR_BINARY_ISGREATER:
1676 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1677 case EXPR_BINARY_ISGREATEREQUAL:
1678 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1679 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1684 panic("trying to get pn_Cmp from non-comparison binexpr type");
1688 * Handle the assume optimizer hint: check if a Confirm
1689 * node can be created.
1691 * @param dbi debug info
1692 * @param expr the IL assume expression
1694 * we support here only some simple cases:
1699 static ir_node *handle_assume_compare(dbg_info *dbi,
1700 const binary_expression_t *expression)
1702 expression_t *op1 = expression->left;
1703 expression_t *op2 = expression->right;
1704 declaration_t *var2, *var = NULL;
1705 ir_node *res = NULL;
1708 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1710 if (is_local_variable(op1) && is_local_variable(op2)) {
1711 var = op1->reference.declaration;
1712 var2 = op2->reference.declaration;
1714 type_t *const type = skip_typeref(var->type);
1715 ir_mode *const mode = get_ir_mode(type);
1717 ir_node *const irn1 = get_value(var->v.value_number, mode);
1718 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1720 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1721 set_value(var2->v.value_number, res);
1723 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1724 set_value(var->v.value_number, res);
1730 if (is_local_variable(op1) && is_constant_expression(op2)) {
1731 var = op1->reference.declaration;
1733 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1734 cmp_val = get_inversed_pnc(cmp_val);
1735 var = op2->reference.declaration;
1740 type_t *const type = skip_typeref(var->type);
1741 ir_mode *const mode = get_ir_mode(type);
1743 res = get_value(var->v.value_number, mode);
1744 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1745 set_value(var->v.value_number, res);
1751 * Handle the assume optimizer hint.
1753 * @param dbi debug info
1754 * @param expr the IL assume expression
1756 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1757 switch(expression->kind) {
1758 case EXPR_BINARY_EQUAL:
1759 case EXPR_BINARY_NOTEQUAL:
1760 case EXPR_BINARY_LESS:
1761 case EXPR_BINARY_LESSEQUAL:
1762 case EXPR_BINARY_GREATER:
1763 case EXPR_BINARY_GREATEREQUAL:
1764 return handle_assume_compare(dbi, &expression->binary);
1770 static ir_node *bitfield_extract_to_firm(const unary_expression_t *expression)
1772 expression_t *select = expression->value;
1773 assert(select->kind == EXPR_SELECT);
1775 type_t *type = select->base.type;
1776 assert(type->kind == TYPE_BITFIELD);
1777 ir_mode *mode = get_ir_mode(type->bitfield.base);
1778 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1779 ir_node *addr = expression_to_addr(select);
1780 ir_node *mem = get_store();
1781 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1782 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1783 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1785 load_res = create_conv(dbgi, load_res, mode_int);
1787 set_store(load_mem);
1789 /* kill upper bits */
1790 ir_entity *entity = select->select.compound_entry->v.entity;
1791 int bitoffset = get_entity_offset_bits_remainder(entity);
1792 ir_type *entity_type = get_entity_type(entity);
1793 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1794 long shift_bitsl = machine_size - bitoffset - bitsize;
1795 assert(shift_bitsl >= 0);
1796 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1797 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1798 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1800 long shift_bitsr = bitoffset + shift_bitsl;
1801 assert(shift_bitsr <= (long) machine_size);
1802 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1803 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1805 if(mode_is_signed(mode)) {
1806 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1808 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1811 return create_conv(dbgi, shiftr, mode);
1814 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1816 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1817 type_t *type = skip_typeref(expression->base.type);
1819 if(expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
1820 return expression_to_addr(expression->value);
1822 const expression_t *value = expression->value;
1824 switch(expression->base.kind) {
1825 case EXPR_UNARY_NEGATE: {
1826 ir_node *value_node = expression_to_firm(value);
1827 ir_mode *mode = get_ir_mode(type);
1828 return new_d_Minus(dbgi, value_node, mode);
1830 case EXPR_UNARY_PLUS:
1831 return expression_to_firm(value);
1832 case EXPR_UNARY_BITWISE_NEGATE: {
1833 ir_node *value_node = expression_to_firm(value);
1834 ir_mode *mode = get_ir_mode(type);
1835 return new_d_Not(dbgi, value_node, mode);
1837 case EXPR_UNARY_NOT: {
1838 ir_node *value_node = expression_to_firm(value);
1839 ir_mode *mode = get_ir_mode(type);
1840 if(get_irn_mode(value_node) != mode_b) {
1841 value_node = create_conv(dbgi, value_node, mode_b);
1843 value_node = new_d_Not(dbgi, value_node, mode_b);
1844 if(mode != mode_b) {
1845 value_node = create_conv(dbgi, value_node, mode);
1849 case EXPR_UNARY_DEREFERENCE: {
1850 ir_node *value_node = expression_to_firm(value);
1851 type_t *value_type = skip_typeref(value->base.type);
1852 ir_type *irtype = get_ir_type(value_type);
1853 assert(is_Pointer_type(irtype));
1854 ir_type *points_to = get_pointer_points_to_type(irtype);
1855 return deref_address(points_to, value_node, dbgi);
1857 case EXPR_UNARY_POSTFIX_INCREMENT:
1858 case EXPR_UNARY_POSTFIX_DECREMENT:
1859 case EXPR_UNARY_PREFIX_INCREMENT:
1860 case EXPR_UNARY_PREFIX_DECREMENT:
1861 return create_incdec(expression);
1862 case EXPR_UNARY_CAST: {
1863 ir_node *value_node = expression_to_firm(value);
1864 if(is_type_scalar(type)) {
1865 ir_mode *mode = get_ir_mode(type);
1866 ir_node *node = create_conv(dbgi, value_node, mode);
1867 node = do_strict_conv(dbgi, node);
1870 /* make sure firm type is constructed */
1871 (void) get_ir_type(type);
1875 case EXPR_UNARY_CAST_IMPLICIT: {
1876 ir_node *value_node = expression_to_firm(value);
1877 if(is_type_scalar(type)) {
1878 ir_mode *mode = get_ir_mode(type);
1879 return create_conv(dbgi, value_node, mode);
1884 case EXPR_UNARY_ASSUME:
1885 if(firm_opt.confirm)
1886 return handle_assume(dbgi, value);
1889 case EXPR_UNARY_BITFIELD_EXTRACT:
1890 return bitfield_extract_to_firm(expression);
1895 panic("invalid UNEXPR type found");
1898 static ir_node *produce_condition_result(const expression_t *expression,
1901 ir_mode *mode = get_ir_mode(expression->base.type);
1902 ir_node *cur_block = get_cur_block();
1904 ir_node *one_block = new_immBlock();
1905 ir_node *one = new_Const(mode, get_mode_one(mode));
1906 ir_node *jmp_one = new_d_Jmp(dbgi);
1908 ir_node *zero_block = new_immBlock();
1909 ir_node *zero = new_Const(mode, get_mode_null(mode));
1910 ir_node *jmp_zero = new_d_Jmp(dbgi);
1912 set_cur_block(cur_block);
1913 create_condition_evaluation(expression, one_block, zero_block);
1914 mature_immBlock(one_block);
1915 mature_immBlock(zero_block);
1917 ir_node *common_block = new_immBlock();
1918 add_immBlock_pred(common_block, jmp_one);
1919 add_immBlock_pred(common_block, jmp_zero);
1920 mature_immBlock(common_block);
1922 ir_node *in[2] = { one, zero };
1923 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1928 static ir_node *create_lazy_op(const binary_expression_t *expression)
1930 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1931 type_t *type = expression->base.type;
1932 ir_mode *mode = get_ir_mode(type);
1934 if(is_constant_expression(expression->left)) {
1935 long val = fold_constant(expression->left);
1936 expression_kind_t ekind = expression->base.kind;
1937 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
1938 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1939 return expression_to_firm(expression->right);
1941 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
1942 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
1943 return new_Const(mode, get_mode_one(mode));
1947 return produce_condition_result((const expression_t*) expression, dbgi);
1950 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1951 ir_node *right, ir_mode *mode);
1953 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1954 create_arithmetic_func func)
1956 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1957 ir_node *left = expression_to_firm(expression->left);
1958 ir_node *right = expression_to_firm(expression->right);
1959 type_t *type = expression->right->base.type;
1960 /* be careful with the modes, because in arithmetic assign nodes only
1961 * the right operand has the mode of the arithmetic already */
1962 ir_mode *mode = get_ir_mode(type);
1963 left = create_conv(dbgi, left, mode);
1964 ir_node *res = func(dbgi, left, right, mode);
1969 static ir_node *pointer_arithmetic(ir_node *const pointer,
1972 dbg_info *const dbgi,
1973 const create_arithmetic_func func)
1975 pointer_type_t *const pointer_type = &type->pointer;
1976 type_t *const points_to = pointer_type->points_to;
1977 const unsigned elem_size = get_type_size_const(points_to);
1979 assert(elem_size >= 1);
1980 if (elem_size > 1) {
1981 integer = create_conv(dbgi, integer, mode_int);
1982 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1983 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1987 ir_mode *const mode = get_ir_mode(type);
1988 return func(dbgi, pointer, integer, mode);
1991 static ir_node *create_arithmetic_assign_binop(
1992 const binary_expression_t *expression, create_arithmetic_func func)
1994 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1995 type_t *const type = skip_typeref(expression->base.type);
1998 if (is_type_pointer(type)) {
1999 ir_node *const pointer = expression_to_firm(expression->left);
2000 ir_node * integer = expression_to_firm(expression->right);
2001 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
2003 value = create_arithmetic_binop(expression, func);
2006 ir_mode *const mode = get_ir_mode(type);
2007 value = create_conv(dbgi, value, mode);
2008 set_value_for_expression(expression->left, value);
2013 static ir_node *create_add(const binary_expression_t *expression)
2015 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2016 ir_node *left = expression_to_firm(expression->left);
2017 ir_node *right = expression_to_firm(expression->right);
2018 type_t *type = expression->base.type;
2020 expression_t *expr_left = expression->left;
2021 expression_t *expr_right = expression->right;
2022 type_t *type_left = skip_typeref(expr_left->base.type);
2023 type_t *type_right = skip_typeref(expr_right->base.type);
2025 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2026 ir_mode *const mode = get_ir_mode(type);
2027 return new_d_Add(dbgi, left, right, mode);
2030 if (is_type_pointer(type_left)) {
2031 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
2033 assert(is_type_pointer(type_right));
2034 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
2038 static ir_node *create_sub(const binary_expression_t *expression)
2040 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2041 expression_t *const expr_left = expression->left;
2042 expression_t *const expr_right = expression->right;
2043 ir_node *const left = expression_to_firm(expr_left);
2044 ir_node *const right = expression_to_firm(expr_right);
2045 type_t *const type = expression->base.type;
2046 type_t *const type_left = skip_typeref(expr_left->base.type);
2047 type_t *const type_right = skip_typeref(expr_right->base.type);
2049 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2050 ir_mode *const mode = get_ir_mode(type);
2051 return new_d_Sub(dbgi, left, right, mode);
2052 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2053 const pointer_type_t *const ptr_type = &type_left->pointer;
2055 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2056 ir_mode *const mode = get_ir_mode(type);
2057 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2058 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2059 ir_node *const no_mem = new_NoMem();
2060 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size, mode,
2061 op_pin_state_floats);
2062 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2065 assert(is_type_pointer(type_left));
2066 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
2069 static ir_node *create_shift(const binary_expression_t *expression)
2071 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2072 ir_node *left = expression_to_firm(expression->left);
2073 ir_node *right = expression_to_firm(expression->right);
2074 type_t *type = expression->base.type;
2075 ir_mode *mode = get_ir_mode(type);
2077 /* firm always wants the shift count to be unsigned */
2078 right = create_conv(dbgi, right, mode_uint);
2082 switch(expression->base.kind) {
2083 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2084 case EXPR_BINARY_SHIFTLEFT:
2085 res = new_d_Shl(dbgi, left, right, mode);
2087 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2088 case EXPR_BINARY_SHIFTRIGHT: {
2089 expression_t *expr_left = expression->left;
2090 type_t *type_left = skip_typeref(expr_left->base.type);
2092 if(is_type_signed(type_left)) {
2093 res = new_d_Shrs(dbgi, left, right, mode);
2095 res = new_d_Shr(dbgi, left, right, mode);
2100 panic("create shift op called for non-shift op");
2107 static ir_node *create_divmod(const binary_expression_t *expression)
2109 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2110 ir_node *left = expression_to_firm(expression->left);
2111 ir_node *right = expression_to_firm(expression->right);
2112 ir_node *pin = new_Pin(new_NoMem());
2113 /* be careful with the modes, because in arithmetic assign nodes only
2114 * the right operand has the mode of the arithmetic already */
2115 type_t *type = expression->right->base.type;
2116 ir_mode *mode = get_ir_mode(type);
2117 left = create_conv(dbgi, left, mode);
2121 switch (expression->base.kind) {
2122 case EXPR_BINARY_DIV:
2123 case EXPR_BINARY_DIV_ASSIGN:
2124 if(mode_is_float(mode)) {
2125 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2126 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2128 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2129 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2133 case EXPR_BINARY_MOD:
2134 case EXPR_BINARY_MOD_ASSIGN:
2135 assert(!mode_is_float(mode));
2136 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
2137 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2140 default: panic("unexpected binary expression type in create_divmod()");
2146 static ir_node *create_arithmetic_assign_divmod(
2147 const binary_expression_t *expression)
2149 ir_node * value = create_divmod(expression);
2150 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2151 type_t *const type = expression->base.type;
2152 ir_mode *const mode = get_ir_mode(type);
2154 assert(type->kind != TYPE_POINTER);
2156 value = create_conv(dbgi, value, mode);
2157 set_value_for_expression(expression->left, value);
2162 static ir_node *create_arithmetic_assign_shift(
2163 const binary_expression_t *expression)
2165 ir_node * value = create_shift(expression);
2166 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2167 type_t *const type = expression->base.type;
2168 ir_mode *const mode = get_ir_mode(type);
2170 value = create_conv(dbgi, value, mode);
2171 set_value_for_expression(expression->left, value);
2176 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2178 expression_kind_t kind = expression->base.kind;
2181 case EXPR_BINARY_EQUAL:
2182 case EXPR_BINARY_NOTEQUAL:
2183 case EXPR_BINARY_LESS:
2184 case EXPR_BINARY_LESSEQUAL:
2185 case EXPR_BINARY_GREATER:
2186 case EXPR_BINARY_GREATEREQUAL:
2187 case EXPR_BINARY_ISGREATER:
2188 case EXPR_BINARY_ISGREATEREQUAL:
2189 case EXPR_BINARY_ISLESS:
2190 case EXPR_BINARY_ISLESSEQUAL:
2191 case EXPR_BINARY_ISLESSGREATER:
2192 case EXPR_BINARY_ISUNORDERED: {
2193 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2194 ir_node *left = expression_to_firm(expression->left);
2195 ir_node *right = expression_to_firm(expression->right);
2196 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2197 long pnc = get_pnc(kind, expression->left->base.type);
2198 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2201 case EXPR_BINARY_ASSIGN: {
2202 ir_node *right = expression_to_firm(expression->right);
2203 set_value_for_expression(expression->left, right);
2207 case EXPR_BINARY_ADD:
2208 return create_add(expression);
2209 case EXPR_BINARY_SUB:
2210 return create_sub(expression);
2211 case EXPR_BINARY_MUL:
2212 return create_arithmetic_binop(expression, new_d_Mul);
2213 case EXPR_BINARY_BITWISE_AND:
2214 return create_arithmetic_binop(expression, new_d_And);
2215 case EXPR_BINARY_BITWISE_OR:
2216 return create_arithmetic_binop(expression, new_d_Or);
2217 case EXPR_BINARY_BITWISE_XOR:
2218 return create_arithmetic_binop(expression, new_d_Eor);
2219 case EXPR_BINARY_SHIFTLEFT:
2220 case EXPR_BINARY_SHIFTRIGHT:
2221 return create_shift(expression);
2222 case EXPR_BINARY_DIV:
2223 case EXPR_BINARY_MOD:
2224 return create_divmod(expression);
2225 case EXPR_BINARY_LOGICAL_AND:
2226 case EXPR_BINARY_LOGICAL_OR:
2227 return create_lazy_op(expression);
2228 case EXPR_BINARY_COMMA:
2229 expression_to_firm(expression->left);
2230 return expression_to_firm(expression->right);
2231 case EXPR_BINARY_ADD_ASSIGN:
2232 return create_arithmetic_assign_binop(expression, new_d_Add);
2233 case EXPR_BINARY_SUB_ASSIGN:
2234 return create_arithmetic_assign_binop(expression, new_d_Sub);
2235 case EXPR_BINARY_MUL_ASSIGN:
2236 return create_arithmetic_assign_binop(expression, new_d_Mul);
2237 case EXPR_BINARY_MOD_ASSIGN:
2238 case EXPR_BINARY_DIV_ASSIGN:
2239 return create_arithmetic_assign_divmod(expression);
2240 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2241 return create_arithmetic_assign_binop(expression, new_d_And);
2242 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2243 return create_arithmetic_assign_binop(expression, new_d_Or);
2244 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2245 return create_arithmetic_assign_binop(expression, new_d_Eor);
2246 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2247 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2248 return create_arithmetic_assign_shift(expression);
2249 case EXPR_BINARY_BUILTIN_EXPECT:
2250 return expression_to_firm(expression->left);
2252 panic("TODO binexpr type");
2256 static ir_node *array_access_addr(const array_access_expression_t *expression)
2258 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2259 ir_node *base_addr = expression_to_firm(expression->array_ref);
2260 ir_node *offset = expression_to_firm(expression->index);
2261 offset = create_conv(dbgi, offset, mode_uint);
2263 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2264 assert(is_type_pointer(ref_type));
2265 pointer_type_t *pointer_type = &ref_type->pointer;
2267 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2268 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2270 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2275 static ir_node *array_access_to_firm(
2276 const array_access_expression_t *expression)
2278 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2279 ir_node *addr = array_access_addr(expression);
2280 type_t *type = revert_automatic_type_conversion(
2281 (const expression_t*) expression);
2282 type = skip_typeref(type);
2283 ir_type *irtype = get_ir_type(type);
2285 return deref_address(irtype, addr, dbgi);
2288 static long get_offsetof_offset(const offsetof_expression_t *expression)
2290 type_t *orig_type = expression->type;
2293 designator_t *designator = expression->designator;
2294 for( ; designator != NULL; designator = designator->next) {
2295 type_t *type = skip_typeref(orig_type);
2296 /* be sure the type is constructed */
2297 (void) get_ir_type(type);
2299 if(designator->symbol != NULL) {
2300 assert(is_type_compound(type));
2301 symbol_t *symbol = designator->symbol;
2303 declaration_t *declaration = type->compound.declaration;
2304 declaration_t *iter = declaration->scope.declarations;
2305 for( ; iter != NULL; iter = iter->next) {
2306 if(iter->symbol == symbol) {
2310 assert(iter != NULL);
2312 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2313 offset += get_entity_offset(iter->v.entity);
2315 orig_type = iter->type;
2317 expression_t *array_index = designator->array_index;
2318 assert(designator->array_index != NULL);
2319 assert(is_type_array(type));
2320 assert(is_type_valid(array_index->base.type));
2322 long index = fold_constant(array_index);
2323 ir_type *arr_type = get_ir_type(type);
2324 ir_type *elem_type = get_array_element_type(arr_type);
2325 long elem_size = get_type_size_bytes(elem_type);
2327 offset += index * elem_size;
2329 orig_type = type->array.element_type;
2336 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2338 ir_mode *mode = get_ir_mode(expression->base.type);
2339 long offset = get_offsetof_offset(expression);
2340 tarval *tv = new_tarval_from_long(offset, mode);
2341 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2343 return new_d_Const(dbgi, mode, tv);
2346 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2347 ir_entity *entity, type_t *type);
2349 static ir_node *compound_literal_to_firm(
2350 const compound_literal_expression_t *expression)
2352 type_t *type = expression->type;
2354 /* create an entity on the stack */
2355 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2357 ident *const id = unique_ident("CompLit");
2358 ir_type *const irtype = get_ir_type(type);
2359 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2360 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2361 set_entity_ld_ident(entity, id);
2363 set_entity_variability(entity, variability_uninitialized);
2365 /* create initialisation code */
2366 initializer_t *initializer = expression->initializer;
2367 create_local_initializer(initializer, dbgi, entity, type);
2369 /* create a sel for the compound literal address */
2370 ir_node *frame = get_local_frame(entity);
2371 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2376 * Transform a sizeof expression into Firm code.
2378 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2380 type_t *type = expression->type;
2382 type = expression->tp_expression->base.type;
2383 assert(type != NULL);
2386 type = skip_typeref(type);
2387 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2388 if(is_type_array(type) && type->array.is_vla
2389 && expression->tp_expression != NULL) {
2390 expression_to_firm(expression->tp_expression);
2393 return get_type_size(type);
2397 * Transform an alignof expression into Firm code.
2399 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2401 type_t *type = expression->type;
2403 /* beware: if expression is a variable reference, return the
2404 alignment of the variable. */
2405 const expression_t *tp_expression = expression->tp_expression;
2406 const declaration_t *declaration = expr_is_variable(tp_expression);
2407 if (declaration != NULL) {
2408 /* TODO: get the alignment of this variable. */
2410 type = tp_expression->base.type;
2411 assert(type != NULL);
2414 ir_mode *const mode = get_ir_mode(expression->base.type);
2415 symconst_symbol sym;
2416 sym.type_p = get_ir_type(type);
2417 return new_SymConst(mode, sym, symconst_type_align);
2420 static void init_ir_types(void);
2421 long fold_constant(const expression_t *expression)
2425 assert(is_constant_expression(expression));
2427 ir_graph *old_current_ir_graph = current_ir_graph;
2428 if(current_ir_graph == NULL) {
2429 current_ir_graph = get_const_code_irg();
2432 ir_node *cnst = expression_to_firm(expression);
2433 current_ir_graph = old_current_ir_graph;
2435 if(!is_Const(cnst)) {
2436 panic("couldn't fold constant\n");
2439 tarval *tv = get_Const_tarval(cnst);
2440 if(!tarval_is_long(tv)) {
2441 panic("result of constant folding is not integer\n");
2444 return get_tarval_long(tv);
2447 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2449 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2451 /* first try to fold a constant condition */
2452 if(is_constant_expression(expression->condition)) {
2453 long val = fold_constant(expression->condition);
2455 return expression_to_firm(expression->true_expression);
2457 return expression_to_firm(expression->false_expression);
2461 ir_node *cur_block = get_cur_block();
2463 /* create the true block */
2464 ir_node *true_block = new_immBlock();
2466 ir_node *true_val = expression_to_firm(expression->true_expression);
2467 ir_node *true_jmp = new_Jmp();
2469 /* create the false block */
2470 ir_node *false_block = new_immBlock();
2472 ir_node *false_val = expression_to_firm(expression->false_expression);
2473 ir_node *false_jmp = new_Jmp();
2475 /* create the condition evaluation */
2476 set_cur_block(cur_block);
2477 create_condition_evaluation(expression->condition, true_block, false_block);
2478 mature_immBlock(true_block);
2479 mature_immBlock(false_block);
2481 /* create the common block */
2482 ir_node *common_block = new_immBlock();
2483 add_immBlock_pred(common_block, true_jmp);
2484 add_immBlock_pred(common_block, false_jmp);
2485 mature_immBlock(common_block);
2487 /* TODO improve static semantics, so either both or no values are NULL */
2488 if (true_val == NULL || false_val == NULL)
2491 ir_node *in[2] = { true_val, false_val };
2492 ir_mode *mode = get_irn_mode(true_val);
2493 assert(get_irn_mode(false_val) == mode);
2494 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2499 static ir_node *select_addr(const select_expression_t *expression)
2501 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2503 ir_node *compound_addr = expression_to_firm(expression->compound);
2505 declaration_t *entry = expression->compound_entry;
2506 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2507 ir_entity *entity = entry->v.entity;
2509 assert(entity != NULL);
2511 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2516 static ir_node *select_to_firm(const select_expression_t *expression)
2518 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2519 ir_node *addr = select_addr(expression);
2520 type_t *type = revert_automatic_type_conversion(
2521 (const expression_t*) expression);
2522 type = skip_typeref(type);
2523 ir_type *irtype = get_ir_type(type);
2525 return deref_address(irtype, addr, dbgi);
2528 /* Values returned by __builtin_classify_type. */
2529 typedef enum gcc_type_class
2535 enumeral_type_class,
2538 reference_type_class,
2542 function_type_class,
2553 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2555 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2561 const atomic_type_t *const atomic_type = &type->atomic;
2562 switch ((atomic_type_kind_t) atomic_type->akind) {
2563 /* should not be reached */
2564 case ATOMIC_TYPE_INVALID:
2568 /* gcc cannot do that */
2569 case ATOMIC_TYPE_VOID:
2570 tc = void_type_class;
2573 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2574 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2575 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2576 case ATOMIC_TYPE_SHORT:
2577 case ATOMIC_TYPE_USHORT:
2578 case ATOMIC_TYPE_INT:
2579 case ATOMIC_TYPE_UINT:
2580 case ATOMIC_TYPE_LONG:
2581 case ATOMIC_TYPE_ULONG:
2582 case ATOMIC_TYPE_LONGLONG:
2583 case ATOMIC_TYPE_ULONGLONG:
2584 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2585 tc = integer_type_class;
2588 case ATOMIC_TYPE_FLOAT:
2589 case ATOMIC_TYPE_DOUBLE:
2590 case ATOMIC_TYPE_LONG_DOUBLE:
2591 tc = real_type_class;
2594 case ATOMIC_TYPE_FLOAT_COMPLEX:
2595 case ATOMIC_TYPE_DOUBLE_COMPLEX:
2596 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
2597 tc = complex_type_class;
2599 case ATOMIC_TYPE_FLOAT_IMAGINARY:
2600 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
2601 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
2602 tc = complex_type_class;
2605 panic("Unexpected atomic type in classify_type_to_firm().");
2608 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2609 case TYPE_ARRAY: /* gcc handles this as pointer */
2610 case TYPE_FUNCTION: /* gcc handles this as pointer */
2611 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2612 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2613 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2615 /* gcc handles this as integer */
2616 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2619 /* typedef/typeof should be skipped already */
2626 panic("unexpected TYPE classify_type_to_firm().");
2629 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2630 ir_mode *const mode = mode_int;
2631 tarval *const tv = new_tarval_from_long(tc, mode);
2632 return new_d_Const(dbgi, mode, tv);
2635 static ir_node *function_name_to_firm(
2636 const string_literal_expression_t *const expr)
2638 if (current_function_name == NULL) {
2639 const source_position_t *const src_pos = &expr->base.source_position;
2640 const char *const name = current_function_decl->symbol->string;
2641 const string_t string = { name, strlen(name) + 1 };
2642 current_function_name = string_to_firm(src_pos, "__func__", &string);
2645 return current_function_name;
2648 static ir_node *funcsig_to_firm(
2649 const string_literal_expression_t *const expr)
2651 if (current_funcsig == NULL) {
2652 const source_position_t *const src_pos = &expr->base.source_position;
2653 ir_entity *ent = get_irg_entity(current_ir_graph);
2654 const char *const name = get_entity_ld_name(ent);
2655 const string_t string = { name, strlen(name) + 1 };
2656 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__", &string);
2659 return current_funcsig;
2662 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2664 statement_t *statement = expr->statement;
2666 assert(statement->kind == STATEMENT_COMPOUND);
2667 return compound_statement_to_firm(&statement->compound);
2670 static ir_node *va_start_expression_to_firm(
2671 const va_start_expression_t *const expr)
2673 ir_type *const method_type = get_ir_type(current_function_decl->type);
2674 int const n = get_method_n_params(method_type) - 1;
2675 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2676 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2677 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2678 ir_node *const no_mem = new_NoMem();
2679 ir_node *const arg_sel =
2680 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2682 ir_node *const cnst = get_type_size(expr->parameter->type);
2683 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2684 set_value_for_expression(expr->ap, add);
2689 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2691 ir_type *const irtype = get_ir_type(expr->base.type);
2692 ir_node *const ap = expression_to_firm(expr->ap);
2693 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2694 ir_node *const res = deref_address(irtype, ap, dbgi);
2696 ir_node *const cnst = get_type_size(expr->base.type);
2697 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2698 set_value_for_expression(expr->ap, add);
2703 static ir_node *dereference_addr(const unary_expression_t *const expression)
2705 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2706 return expression_to_firm(expression->value);
2709 static ir_node *expression_to_addr(const expression_t *expression)
2711 switch(expression->kind) {
2712 case EXPR_REFERENCE:
2713 return reference_addr(&expression->reference);
2714 case EXPR_ARRAY_ACCESS:
2715 return array_access_addr(&expression->array_access);
2717 return select_addr(&expression->select);
2719 return call_expression_to_firm(&expression->call);
2720 case EXPR_UNARY_DEREFERENCE: {
2721 return dereference_addr(&expression->unary);
2726 panic("trying to get address of non-lvalue");
2729 static ir_node *builtin_constant_to_firm(
2730 const builtin_constant_expression_t *expression)
2732 ir_mode *mode = get_ir_mode(expression->base.type);
2735 if (is_constant_expression(expression->value)) {
2740 return new_Const_long(mode, v);
2743 static ir_node *builtin_prefetch_to_firm(
2744 const builtin_prefetch_expression_t *expression)
2746 ir_node *adr = expression_to_firm(expression->adr);
2747 /* no Firm support for prefetch yet */
2752 static ir_node *_expression_to_firm(const expression_t *expression)
2754 switch(expression->kind) {
2755 case EXPR_CHARACTER_CONSTANT:
2756 return character_constant_to_firm(&expression->conste);
2757 case EXPR_WIDE_CHARACTER_CONSTANT:
2758 return wide_character_constant_to_firm(&expression->conste);
2760 return const_to_firm(&expression->conste);
2761 case EXPR_STRING_LITERAL:
2762 return string_literal_to_firm(&expression->string);
2763 case EXPR_WIDE_STRING_LITERAL:
2764 return wide_string_literal_to_firm(&expression->wide_string);
2765 case EXPR_REFERENCE:
2766 return reference_expression_to_firm(&expression->reference);
2768 return call_expression_to_firm(&expression->call);
2770 return unary_expression_to_firm(&expression->unary);
2772 return binary_expression_to_firm(&expression->binary);
2773 case EXPR_ARRAY_ACCESS:
2774 return array_access_to_firm(&expression->array_access);
2776 return sizeof_to_firm(&expression->typeprop);
2778 return alignof_to_firm(&expression->typeprop);
2779 case EXPR_CONDITIONAL:
2780 return conditional_to_firm(&expression->conditional);
2782 return select_to_firm(&expression->select);
2783 case EXPR_CLASSIFY_TYPE:
2784 return classify_type_to_firm(&expression->classify_type);
2786 case EXPR_PRETTY_FUNCTION:
2787 case EXPR_FUNCDNAME:
2788 return function_name_to_firm(&expression->string);
2790 return funcsig_to_firm(&expression->string);
2791 case EXPR_STATEMENT:
2792 return statement_expression_to_firm(&expression->statement);
2794 return va_start_expression_to_firm(&expression->va_starte);
2796 return va_arg_expression_to_firm(&expression->va_arge);
2797 case EXPR_BUILTIN_SYMBOL:
2798 panic("unimplemented expression found");
2799 case EXPR_BUILTIN_CONSTANT_P:
2800 return builtin_constant_to_firm(&expression->builtin_constant);
2801 case EXPR_BUILTIN_PREFETCH:
2802 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2804 return offsetof_to_firm(&expression->offsetofe);
2805 case EXPR_COMPOUND_LITERAL:
2806 return compound_literal_to_firm(&expression->compound_literal);
2812 panic("invalid expression found");
2815 static ir_node *expression_to_firm(const expression_t *expression)
2817 ir_node *res = _expression_to_firm(expression);
2819 if(res != NULL && get_irn_mode(res) == mode_b) {
2820 ir_mode *mode = get_ir_mode(expression->base.type);
2822 if(is_Const_null(res)) {
2823 return new_Const_long(mode, 0);
2825 assert(is_Const_one(res));
2826 return new_Const_long(mode, 1);
2830 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2831 return produce_condition_result(expression, dbgi);
2837 static ir_node *expression_to_modeb(const expression_t *expression)
2839 ir_node *res = _expression_to_firm(expression);
2840 res = create_conv(NULL, res, mode_b);
2846 * create a short-circuit expression evaluation that tries to construct
2847 * efficient control flow structures for &&, || and ! expressions
2849 static void create_condition_evaluation(const expression_t *expression,
2850 ir_node *true_block,
2851 ir_node *false_block)
2853 switch(expression->kind) {
2854 case EXPR_UNARY_NOT: {
2855 const unary_expression_t *unary_expression = &expression->unary;
2856 create_condition_evaluation(unary_expression->value, false_block,
2860 case EXPR_BINARY_LOGICAL_AND: {
2861 const binary_expression_t *binary_expression = &expression->binary;
2863 ir_node *cur_block = get_cur_block();
2864 ir_node *extra_block = new_immBlock();
2865 set_cur_block(cur_block);
2866 create_condition_evaluation(binary_expression->left, extra_block,
2868 mature_immBlock(extra_block);
2869 set_cur_block(extra_block);
2870 create_condition_evaluation(binary_expression->right, true_block,
2874 case EXPR_BINARY_LOGICAL_OR: {
2875 const binary_expression_t *binary_expression = &expression->binary;
2877 ir_node *cur_block = get_cur_block();
2878 ir_node *extra_block = new_immBlock();
2879 set_cur_block(cur_block);
2880 create_condition_evaluation(binary_expression->left, true_block,
2882 mature_immBlock(extra_block);
2883 set_cur_block(extra_block);
2884 create_condition_evaluation(binary_expression->right, true_block,
2892 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2893 ir_node *condition = expression_to_modeb(expression);
2894 ir_node *cond = new_d_Cond(dbgi, condition);
2895 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2896 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2898 /* set branch prediction info based on __builtin_expect */
2899 if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
2900 long cnst = fold_constant(expression->binary.right);
2901 cond_jmp_predicate pred;
2904 pred = COND_JMP_PRED_FALSE;
2906 pred = COND_JMP_PRED_TRUE;
2908 set_Cond_jmp_pred(cond, pred);
2911 add_immBlock_pred(true_block, true_proj);
2912 add_immBlock_pred(false_block, false_proj);
2914 set_cur_block(NULL);
2919 static void create_declaration_entity(declaration_t *declaration,
2920 declaration_kind_t declaration_kind,
2921 ir_type *parent_type)
2923 ident *const id = new_id_from_str(declaration->symbol->string);
2924 ir_type *const irtype = get_ir_type(declaration->type);
2925 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2926 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2927 set_entity_ld_ident(entity, id);
2929 declaration->declaration_kind = (unsigned char) declaration_kind;
2930 declaration->v.entity = entity;
2931 set_entity_variability(entity, variability_uninitialized);
2932 if(parent_type == get_tls_type())
2933 set_entity_allocation(entity, allocation_automatic);
2934 else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
2935 set_entity_allocation(entity, allocation_static);
2936 /* TODO: visibility? */
2940 typedef struct type_path_entry_t type_path_entry_t;
2941 struct type_path_entry_t {
2943 ir_initializer_t *initializer;
2945 declaration_t *compound_entry;
2948 typedef struct type_path_t type_path_t;
2949 struct type_path_t {
2950 type_path_entry_t *path;
2955 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
2957 size_t len = ARR_LEN(path->path);
2959 for(size_t i = 0; i < len; ++i) {
2960 const type_path_entry_t *entry = & path->path[i];
2962 type_t *type = skip_typeref(entry->type);
2963 if(is_type_compound(type)) {
2964 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
2965 } else if(is_type_array(type)) {
2966 fprintf(stderr, "[%u]", entry->index);
2968 fprintf(stderr, "-INVALID-");
2971 fprintf(stderr, " (");
2972 print_type(path->top_type);
2973 fprintf(stderr, ")");
2976 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2978 size_t len = ARR_LEN(path->path);
2980 return & path->path[len-1];
2983 static type_path_entry_t *append_to_type_path(type_path_t *path)
2985 size_t len = ARR_LEN(path->path);
2986 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2988 type_path_entry_t *result = & path->path[len];
2989 memset(result, 0, sizeof(result[0]));
2993 static size_t get_compound_size(const compound_type_t *type)
2995 declaration_t *declaration = type->declaration;
2996 declaration_t *member = declaration->scope.declarations;
2998 for( ; member != NULL; member = member->next) {
3001 /* TODO: cache results? */
3006 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3008 type_t *orig_top_type = path->top_type;
3009 type_t *top_type = skip_typeref(orig_top_type);
3011 assert(is_type_compound(top_type) || is_type_array(top_type));
3013 if(ARR_LEN(path->path) == 0) {
3016 type_path_entry_t *top = get_type_path_top(path);
3017 ir_initializer_t *initializer = top->initializer;
3018 return get_initializer_compound_value(initializer, top->index);
3022 static void descend_into_subtype(type_path_t *path)
3024 type_t *orig_top_type = path->top_type;
3025 type_t *top_type = skip_typeref(orig_top_type);
3027 assert(is_type_compound(top_type) || is_type_array(top_type));
3029 ir_initializer_t *initializer = get_initializer_entry(path);
3031 type_path_entry_t *top = append_to_type_path(path);
3032 top->type = top_type;
3036 if(is_type_compound(top_type)) {
3037 declaration_t *declaration = top_type->compound.declaration;
3038 declaration_t *entry = declaration->scope.declarations;
3040 top->compound_entry = entry;
3042 len = get_compound_size(&top_type->compound);
3044 path->top_type = entry->type;
3046 assert(is_type_array(top_type));
3047 assert(top_type->array.size > 0);
3050 path->top_type = top_type->array.element_type;
3051 len = top_type->array.size;
3053 if(initializer == NULL
3054 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3055 initializer = create_initializer_compound(len);
3056 /* we have to set the entry at the 2nd latest path entry... */
3057 size_t path_len = ARR_LEN(path->path);
3058 assert(path_len >= 1);
3060 type_path_entry_t *entry = & path->path[path_len-2];
3061 ir_initializer_t *tinitializer = entry->initializer;
3062 set_initializer_compound_value(tinitializer, entry->index,
3066 top->initializer = initializer;
3069 static void ascend_from_subtype(type_path_t *path)
3071 type_path_entry_t *top = get_type_path_top(path);
3073 path->top_type = top->type;
3075 size_t len = ARR_LEN(path->path);
3076 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3079 static void walk_designator(type_path_t *path, const designator_t *designator)
3081 /* designators start at current object type */
3082 ARR_RESIZE(type_path_entry_t, path->path, 1);
3084 for( ; designator != NULL; designator = designator->next) {
3085 type_path_entry_t *top = get_type_path_top(path);
3086 type_t *orig_type = top->type;
3087 type_t *type = skip_typeref(orig_type);
3089 if(designator->symbol != NULL) {
3090 assert(is_type_compound(type));
3092 symbol_t *symbol = designator->symbol;
3094 declaration_t *declaration = type->compound.declaration;
3095 declaration_t *iter = declaration->scope.declarations;
3096 for( ; iter != NULL; iter = iter->next, ++index) {
3097 if(iter->symbol == symbol) {
3101 assert(iter != NULL);
3103 top->type = orig_type;
3104 top->compound_entry = iter;
3106 orig_type = iter->type;
3108 expression_t *array_index = designator->array_index;
3109 assert(designator->array_index != NULL);
3110 assert(is_type_array(type));
3111 assert(is_type_valid(array_index->base.type));
3113 long index = fold_constant(array_index);
3116 if(type->array.size_constant == 1) {
3117 long array_size = type->array.size;
3118 assert(index < array_size);
3122 top->type = orig_type;
3123 top->index = (size_t) index;
3124 orig_type = type->array.element_type;
3126 path->top_type = orig_type;
3128 if(designator->next != NULL) {
3129 descend_into_subtype(path);
3133 path->invalid = false;
3136 static void advance_current_object(type_path_t *path)
3139 /* TODO: handle this... */
3140 panic("invalid initializer in ast2firm (excessive elements)");
3144 type_path_entry_t *top = get_type_path_top(path);
3146 type_t *type = skip_typeref(top->type);
3147 if(is_type_union(type)) {
3148 top->compound_entry = NULL;
3149 } else if(is_type_struct(type)) {
3150 declaration_t *entry = top->compound_entry;
3153 entry = entry->next;
3154 top->compound_entry = entry;
3156 path->top_type = entry->type;
3160 assert(is_type_array(type));
3163 if(!type->array.size_constant || top->index < type->array.size) {
3168 /* we're past the last member of the current sub-aggregate, try if we
3169 * can ascend in the type hierarchy and continue with another subobject */
3170 size_t len = ARR_LEN(path->path);
3173 ascend_from_subtype(path);
3174 advance_current_object(path);
3176 path->invalid = true;
3181 static ir_initializer_t *create_ir_initializer(
3182 const initializer_t *initializer, type_t *type);
3184 static ir_initializer_t *create_ir_initializer_value(
3185 const initializer_value_t *initializer)
3187 ir_node *value = expression_to_firm(initializer->value);
3188 return create_initializer_const(value);
3191 static ir_initializer_t *create_ir_initializer_list(
3192 const initializer_list_t *initializer, type_t *type)
3195 memset(&path, 0, sizeof(path));
3196 path.top_type = type;
3197 path.path = NEW_ARR_F(type_path_entry_t, 0);
3199 descend_into_subtype(&path);
3201 for(size_t i = 0; i < initializer->len; ++i) {
3202 const initializer_t *sub_initializer = initializer->initializers[i];
3204 if(sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3205 walk_designator(&path, sub_initializer->designator.designator);
3209 if(sub_initializer->kind == INITIALIZER_VALUE) {
3210 /* we might have to descend into types until we're at a scalar
3213 type_t *orig_top_type = path.top_type;
3214 type_t *top_type = skip_typeref(orig_top_type);
3216 if(is_type_scalar(top_type))
3218 descend_into_subtype(&path);
3222 ir_initializer_t *sub_irinitializer
3223 = create_ir_initializer(sub_initializer, path.top_type);
3225 size_t path_len = ARR_LEN(path.path);
3226 assert(path_len >= 1);
3227 type_path_entry_t *entry = & path.path[path_len-1];
3228 ir_initializer_t *tinitializer = entry->initializer;
3229 set_initializer_compound_value(tinitializer, entry->index,
3232 advance_current_object(&path);
3235 assert(ARR_LEN(path.path) >= 1);
3236 ir_initializer_t *result = path.path[0].initializer;
3237 DEL_ARR_F(path.path);
3242 static ir_initializer_t *create_ir_initializer_string(
3243 const initializer_string_t *initializer, type_t *type)
3245 type = skip_typeref(type);
3247 size_t string_len = initializer->string.size;
3248 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3249 size_t len = type->array.size;
3250 ir_initializer_t *irinitializer = create_initializer_compound(len);
3252 const char *string = initializer->string.begin;
3253 ir_mode *mode = get_type_mode(ir_type_const_char);
3255 for(size_t i = 0; i < len; ++i) {
3260 tarval *tv = new_tarval_from_long(string[i], mode);
3261 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3263 set_initializer_compound_value(irinitializer, i, char_initializer);
3266 return irinitializer;
3269 static ir_initializer_t *create_ir_initializer_wide_string(
3270 const initializer_wide_string_t *initializer, type_t *type)
3272 size_t string_len = initializer->string.size;
3273 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3274 size_t len = type->array.size;
3275 ir_initializer_t *irinitializer = create_initializer_compound(len);
3277 const wchar_rep_t *string = initializer->string.begin;
3278 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3280 for(size_t i = 0; i < len; ++i) {
3282 if(i < string_len) {
3285 tarval *tv = new_tarval_from_long(string[i], mode);
3286 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3288 set_initializer_compound_value(irinitializer, i, char_initializer);
3291 return irinitializer;
3294 static ir_initializer_t *create_ir_initializer(
3295 const initializer_t *initializer, type_t *type)
3297 switch(initializer->kind) {
3298 case INITIALIZER_STRING:
3299 return create_ir_initializer_string(&initializer->string, type);
3301 case INITIALIZER_WIDE_STRING:
3302 return create_ir_initializer_wide_string(&initializer->wide_string,
3305 case INITIALIZER_LIST:
3306 return create_ir_initializer_list(&initializer->list, type);
3308 case INITIALIZER_VALUE:
3309 return create_ir_initializer_value(&initializer->value);
3311 case INITIALIZER_DESIGNATOR:
3312 panic("unexpected designator initializer found");
3314 panic("unknown initializer");
3317 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3318 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3320 switch(get_initializer_kind(initializer)) {
3321 case IR_INITIALIZER_NULL: {
3322 /* TODO: implement this for compound types... */
3323 assert(type != NULL);
3325 ir_mode *mode = get_type_mode(type);
3326 tarval *zero = get_mode_null(mode);
3327 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3329 /* TODO: bitfields */
3330 ir_node *mem = get_store();
3331 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3332 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3336 case IR_INITIALIZER_CONST: {
3337 ir_node *node = get_initializer_const_value(initializer);
3338 ir_mode *mode = get_irn_mode(node);
3339 assert(get_type_mode(type) == mode);
3341 /* TODO: bitfields... */
3342 ir_node *mem = get_store();
3343 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3344 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3348 case IR_INITIALIZER_TARVAL: {
3349 tarval *tv = get_initializer_tarval_value(initializer);
3350 ir_mode *mode = get_tarval_mode(tv);
3351 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3352 assert(get_type_mode(type) == mode);
3354 /* TODO: bitfields... */
3355 ir_node *mem = get_store();
3356 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3357 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3361 case IR_INITIALIZER_COMPOUND: {
3362 assert(is_compound_type(type));
3364 if(is_Array_type(type)) {
3365 assert(has_array_upper_bound(type, 0));
3366 n_members = get_array_upper_bound_int(type, 0);
3368 n_members = get_compound_n_members(type);
3371 if(get_initializer_compound_n_entries(initializer)
3372 != (unsigned) n_members)
3373 panic("initializer doesn't match compound type");
3375 for(int i = 0; i < n_members; ++i) {
3378 if(is_Array_type(type)) {
3379 ir_entity *entity = get_array_element_entity(type);
3380 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3381 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3382 ir_node *in[1] = { cnst };
3383 irtype = get_array_element_type(type);
3384 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3386 ir_entity *member = get_compound_member(type, i);
3388 irtype = get_entity_type(member);
3389 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3392 ir_initializer_t *sub_init
3393 = get_initializer_compound_value(initializer, i);
3395 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3401 panic("invalid IR_INITIALIZER found");
3404 static void create_dynamic_initializer(ir_initializer_t *initializer,
3405 dbg_info *dbgi, ir_entity *entity)
3407 ir_node *frame = get_local_frame(entity);
3408 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3409 ir_type *type = get_entity_type(entity);
3411 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3414 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3415 ir_entity *entity, type_t *type)
3417 ir_node *memory = get_store();
3418 ir_node *nomem = new_NoMem();
3419 ir_node *frame = get_irg_frame(current_ir_graph);
3420 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3422 if(initializer->kind == INITIALIZER_VALUE) {
3423 initializer_value_t *initializer_value = &initializer->value;
3425 ir_node *value = expression_to_firm(initializer_value->value);
3426 type = skip_typeref(type);
3427 assign_value(dbgi, addr, type, value);
3431 if(!is_constant_initializer(initializer)) {
3432 ir_initializer_t *irinitializer
3433 = create_ir_initializer(initializer, type);
3435 create_dynamic_initializer(irinitializer, dbgi, entity);
3439 /* create the ir_initializer */
3440 ir_graph *const old_current_ir_graph = current_ir_graph;
3441 current_ir_graph = get_const_code_irg();
3443 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3445 assert(current_ir_graph == get_const_code_irg());
3446 current_ir_graph = old_current_ir_graph;
3448 /* create a "template" entity which is copied to the entity on the stack */
3449 ident *const id = unique_ident("initializer");
3450 ir_type *const irtype = get_ir_type(type);
3451 ir_type *const global_type = get_glob_type();
3452 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3453 set_entity_ld_ident(init_entity, id);
3455 set_entity_variability(init_entity, variability_initialized);
3456 set_entity_visibility(init_entity, visibility_local);
3457 set_entity_allocation(init_entity, allocation_static);
3459 set_entity_initializer(init_entity, irinitializer);
3461 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3462 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3464 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3465 set_store(copyb_mem);
3468 static void create_initializer_local_variable_entity(declaration_t *declaration)
3470 initializer_t *initializer = declaration->init.initializer;
3471 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3472 ir_entity *entity = declaration->v.entity;
3473 type_t *type = declaration->type;
3474 create_local_initializer(initializer, dbgi, entity, type);
3477 static void create_declaration_initializer(declaration_t *declaration)
3479 initializer_t *initializer = declaration->init.initializer;
3480 if(initializer == NULL)
3483 declaration_kind_t declaration_kind
3484 = (declaration_kind_t) declaration->declaration_kind;
3485 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3486 create_initializer_local_variable_entity(declaration);
3490 if(initializer->kind == INITIALIZER_VALUE) {
3491 initializer_value_t *initializer_value = &initializer->value;
3493 ir_node *value = expression_to_firm(initializer_value->value);
3495 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3496 set_value(declaration->v.value_number, value);
3498 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3500 ir_entity *entity = declaration->v.entity;
3502 set_entity_variability(entity, variability_initialized);
3503 set_atomic_ent_value(entity, value);
3506 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3507 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3509 ir_entity *entity = declaration->v.entity;
3510 ir_initializer_t *irinitializer
3511 = create_ir_initializer(initializer, declaration->type);
3513 set_entity_variability(entity, variability_initialized);
3514 set_entity_initializer(entity, irinitializer);
3518 static void create_variable_length_array(declaration_t *declaration)
3520 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3521 type_t *type = declaration->type;
3522 ir_node *mem = get_store();
3523 ir_type *el_type = get_ir_type(type->array.element_type);
3525 /* make sure size_node is calculated */
3526 get_type_size(type);
3527 ir_node *elems = type->array.size_node;
3528 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3530 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3531 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3534 /* initializers are not allowed for VLAs */
3535 assert(declaration->init.initializer == NULL);
3537 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3538 declaration->v.vla_base = addr;
3540 /* TODO: record VLA somewhere so we create the free node when we leave
3545 * Creates a Firm local variable from a declaration.
3547 static void create_local_variable(declaration_t *declaration)
3549 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3551 bool needs_entity = declaration->address_taken;
3552 type_t *type = skip_typeref(declaration->type);
3554 /* is it a variable length array? */
3555 if(is_type_array(type) && !type->array.size_constant) {
3556 create_variable_length_array(declaration);
3558 } else if(is_type_array(type) || is_type_compound(type)) {
3559 needs_entity = true;
3563 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3564 create_declaration_entity(declaration,
3565 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3568 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3569 declaration->v.value_number = next_value_number_function;
3570 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3571 ++next_value_number_function;
3574 create_declaration_initializer(declaration);
3577 static void create_local_static_variable(declaration_t *declaration)
3579 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3581 type_t *const type = skip_typeref(declaration->type);
3582 ir_type *const global_type = get_glob_type();
3583 ident *const id = unique_ident(declaration->symbol->string);
3584 ir_type *const irtype = get_ir_type(type);
3585 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3586 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3587 set_entity_ld_ident(entity, id);
3589 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3590 declaration->v.entity = entity;
3591 set_entity_variability(entity, variability_uninitialized);
3592 set_entity_visibility(entity, visibility_local);
3593 set_entity_allocation(entity, allocation_static);
3595 ir_graph *const old_current_ir_graph = current_ir_graph;
3596 current_ir_graph = get_const_code_irg();
3598 create_declaration_initializer(declaration);
3600 assert(current_ir_graph == get_const_code_irg());
3601 current_ir_graph = old_current_ir_graph;
3606 static void return_statement_to_firm(return_statement_t *statement)
3608 if(get_cur_block() == NULL)
3611 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3612 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3617 if(get_method_n_ress(func_irtype) > 0) {
3618 ir_type *res_type = get_method_res_type(func_irtype, 0);
3620 if(statement->value != NULL) {
3621 ir_node *node = expression_to_firm(statement->value);
3622 node = do_strict_conv(dbgi, node);
3626 if(is_compound_type(res_type)) {
3629 mode = get_type_mode(res_type);
3631 in[0] = new_Unknown(mode);
3635 /* build return_value for its side effects */
3636 if(statement->value != NULL) {
3637 expression_to_firm(statement->value);
3642 ir_node *store = get_store();
3643 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3645 ir_node *end_block = get_irg_end_block(current_ir_graph);
3646 add_immBlock_pred(end_block, ret);
3648 set_cur_block(NULL);
3651 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3653 if(get_cur_block() == NULL)
3656 return expression_to_firm(statement->expression);
3659 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3661 ir_node *result = NULL;
3662 statement_t *statement = compound->statements;
3663 for( ; statement != NULL; statement = statement->base.next) {
3664 //context2firm(&statement->scope);
3666 if(statement->base.next == NULL
3667 && statement->kind == STATEMENT_EXPRESSION) {
3668 result = expression_statement_to_firm(
3669 &statement->expression);
3672 statement_to_firm(statement);
3678 static void create_global_variable(declaration_t *declaration)
3682 switch ((storage_class_tag_t)declaration->storage_class) {
3683 case STORAGE_CLASS_STATIC:
3684 vis = visibility_local;
3687 case STORAGE_CLASS_EXTERN:
3688 vis = visibility_external_allocated;
3691 case STORAGE_CLASS_NONE:
3692 vis = visibility_external_visible;
3695 case STORAGE_CLASS_THREAD:
3696 vis = visibility_external_visible;
3699 case STORAGE_CLASS_THREAD_EXTERN:
3700 vis = visibility_external_allocated;
3703 case STORAGE_CLASS_THREAD_STATIC:
3704 vis = visibility_local;
3708 var_type = get_tls_type();
3712 var_type = get_glob_type();
3716 create_declaration_entity(declaration,
3717 DECLARATION_KIND_GLOBAL_VARIABLE,
3719 set_entity_visibility(declaration->v.entity, vis);
3723 case STORAGE_CLASS_TYPEDEF:
3724 case STORAGE_CLASS_AUTO:
3725 case STORAGE_CLASS_REGISTER:
3726 case STORAGE_CLASS_ENUM_ENTRY:
3729 panic("Invalid storage class for global variable");
3732 static void create_local_declaration(declaration_t *declaration)
3734 if(declaration->symbol == NULL)
3737 type_t *type = skip_typeref(declaration->type);
3739 switch ((storage_class_tag_t) declaration->storage_class) {
3740 case STORAGE_CLASS_STATIC:
3741 create_local_static_variable(declaration);
3743 case STORAGE_CLASS_EXTERN:
3744 create_global_variable(declaration);
3745 create_declaration_initializer(declaration);
3747 case STORAGE_CLASS_NONE:
3748 case STORAGE_CLASS_AUTO:
3749 case STORAGE_CLASS_REGISTER:
3750 if(is_type_function(type)) {
3751 if(declaration->init.statement != NULL) {
3752 panic("nested functions not supported yet");
3754 get_function_entity(declaration);
3757 create_local_variable(declaration);
3760 case STORAGE_CLASS_ENUM_ENTRY:
3761 case STORAGE_CLASS_TYPEDEF:
3762 case STORAGE_CLASS_THREAD:
3763 case STORAGE_CLASS_THREAD_EXTERN:
3764 case STORAGE_CLASS_THREAD_STATIC:
3767 panic("invalid storage class found");
3770 static void declaration_statement_to_firm(declaration_statement_t *statement)
3772 declaration_t *declaration = statement->declarations_begin;
3773 declaration_t *end = statement->declarations_end->next;
3774 for( ; declaration != end; declaration = declaration->next) {
3775 if(declaration->namespc != NAMESPACE_NORMAL)
3777 create_local_declaration(declaration);
3781 static void if_statement_to_firm(if_statement_t *statement)
3783 ir_node *cur_block = get_cur_block();
3785 ir_node *fallthrough_block = new_immBlock();
3787 /* the true (blocks) */
3788 ir_node *true_block;
3789 if (statement->true_statement != NULL) {
3790 true_block = new_immBlock();
3791 statement_to_firm(statement->true_statement);
3792 if(get_cur_block() != NULL) {
3793 ir_node *jmp = new_Jmp();
3794 add_immBlock_pred(fallthrough_block, jmp);
3797 true_block = fallthrough_block;
3800 /* the false (blocks) */
3801 ir_node *false_block;
3802 if(statement->false_statement != NULL) {
3803 false_block = new_immBlock();
3805 statement_to_firm(statement->false_statement);
3806 if(get_cur_block() != NULL) {
3807 ir_node *jmp = new_Jmp();
3808 add_immBlock_pred(fallthrough_block, jmp);
3811 false_block = fallthrough_block;
3814 /* create the condition */
3815 if(cur_block != NULL) {
3816 set_cur_block(cur_block);
3817 create_condition_evaluation(statement->condition, true_block,
3821 mature_immBlock(true_block);
3822 if(false_block != fallthrough_block) {
3823 mature_immBlock(false_block);
3825 mature_immBlock(fallthrough_block);
3827 set_cur_block(fallthrough_block);
3830 static void while_statement_to_firm(while_statement_t *statement)
3832 ir_node *jmp = NULL;
3833 if(get_cur_block() != NULL) {
3837 /* create the header block */
3838 ir_node *header_block = new_immBlock();
3840 add_immBlock_pred(header_block, jmp);
3843 /* the false block */
3844 ir_node *false_block = new_immBlock();
3847 ir_node *body_block;
3848 if (statement->body != NULL) {
3849 ir_node *old_continue_label = continue_label;
3850 ir_node *old_break_label = break_label;
3851 continue_label = header_block;
3852 break_label = false_block;
3854 body_block = new_immBlock();
3855 statement_to_firm(statement->body);
3857 assert(continue_label == header_block);
3858 assert(break_label == false_block);
3859 continue_label = old_continue_label;
3860 break_label = old_break_label;
3862 if(get_cur_block() != NULL) {
3864 add_immBlock_pred(header_block, jmp);
3867 body_block = header_block;
3870 /* create the condition */
3871 set_cur_block(header_block);
3873 create_condition_evaluation(statement->condition, body_block, false_block);
3874 mature_immBlock(body_block);
3875 mature_immBlock(false_block);
3876 mature_immBlock(header_block);
3878 set_cur_block(false_block);
3881 static void do_while_statement_to_firm(do_while_statement_t *statement)
3883 ir_node *jmp = NULL;
3884 if(get_cur_block() != NULL) {
3888 /* create the header block */
3889 ir_node *header_block = new_immBlock();
3891 /* the false block */
3892 ir_node *false_block = new_immBlock();
3895 ir_node *body_block = new_immBlock();
3897 add_immBlock_pred(body_block, jmp);
3900 if (statement->body != NULL) {
3901 ir_node *old_continue_label = continue_label;
3902 ir_node *old_break_label = break_label;
3903 continue_label = header_block;
3904 break_label = false_block;
3906 statement_to_firm(statement->body);
3908 assert(continue_label == header_block);
3909 assert(break_label == false_block);
3910 continue_label = old_continue_label;
3911 break_label = old_break_label;
3913 if (get_cur_block() == NULL) {
3914 mature_immBlock(header_block);
3915 mature_immBlock(body_block);
3916 mature_immBlock(false_block);
3921 ir_node *body_jmp = new_Jmp();
3922 add_immBlock_pred(header_block, body_jmp);
3923 mature_immBlock(header_block);
3925 /* create the condition */
3926 set_cur_block(header_block);
3928 create_condition_evaluation(statement->condition, body_block, false_block);
3929 mature_immBlock(body_block);
3930 mature_immBlock(false_block);
3931 mature_immBlock(header_block);
3933 set_cur_block(false_block);
3936 static void for_statement_to_firm(for_statement_t *statement)
3938 ir_node *jmp = NULL;
3939 if (get_cur_block() != NULL) {
3940 if(statement->initialisation != NULL) {
3941 expression_to_firm(statement->initialisation);
3944 /* create declarations */
3945 declaration_t *declaration = statement->scope.declarations;
3946 for( ; declaration != NULL; declaration = declaration->next) {
3947 create_local_declaration(declaration);
3954 /* create the step block */
3955 ir_node *const step_block = new_immBlock();
3956 if (statement->step != NULL) {
3957 expression_to_firm(statement->step);
3959 ir_node *const step_jmp = new_Jmp();
3961 /* create the header block */
3962 ir_node *const header_block = new_immBlock();
3964 add_immBlock_pred(header_block, jmp);
3966 add_immBlock_pred(header_block, step_jmp);
3968 /* the false block */
3969 ir_node *const false_block = new_immBlock();
3972 ir_node * body_block;
3973 if (statement->body != NULL) {
3974 ir_node *const old_continue_label = continue_label;
3975 ir_node *const old_break_label = break_label;
3976 continue_label = step_block;
3977 break_label = false_block;
3979 body_block = new_immBlock();
3980 statement_to_firm(statement->body);
3982 assert(continue_label == step_block);
3983 assert(break_label == false_block);
3984 continue_label = old_continue_label;
3985 break_label = old_break_label;
3987 if (get_cur_block() != NULL) {
3989 add_immBlock_pred(step_block, jmp);
3992 body_block = step_block;
3995 /* create the condition */
3996 set_cur_block(header_block);
3997 if (statement->condition != NULL) {
3998 create_condition_evaluation(statement->condition, body_block,
4001 keep_alive(header_block);
4003 add_immBlock_pred(body_block, jmp);
4006 mature_immBlock(body_block);
4007 mature_immBlock(false_block);
4008 mature_immBlock(step_block);
4009 mature_immBlock(header_block);
4010 mature_immBlock(false_block);
4012 set_cur_block(false_block);
4015 static void create_jump_statement(const statement_t *statement,
4016 ir_node *target_block)
4018 if(get_cur_block() == NULL)
4021 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4022 ir_node *jump = new_d_Jmp(dbgi);
4023 add_immBlock_pred(target_block, jump);
4025 set_cur_block(NULL);
4028 static void switch_statement_to_firm(const switch_statement_t *statement)
4030 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4032 ir_node *expression = expression_to_firm(statement->expression);
4033 ir_node *cond = new_d_Cond(dbgi, expression);
4034 ir_node *break_block = new_immBlock();
4036 set_cur_block(NULL);
4038 ir_node *const old_switch_cond = current_switch_cond;
4039 ir_node *const old_break_label = break_label;
4040 const bool old_saw_default_label = saw_default_label;
4041 current_switch_cond = cond;
4042 break_label = break_block;
4044 if (statement->body != NULL) {
4045 statement_to_firm(statement->body);
4048 if(get_cur_block() != NULL) {
4049 ir_node *jmp = new_Jmp();
4050 add_immBlock_pred(break_block, jmp);
4053 if (!saw_default_label) {
4054 set_cur_block(get_nodes_block(cond));
4055 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4056 MAGIC_DEFAULT_PN_NUMBER);
4057 add_immBlock_pred(break_block, proj);
4060 assert(current_switch_cond == cond);
4061 assert(break_label == break_block);
4062 current_switch_cond = old_switch_cond;
4063 break_label = old_break_label;
4064 saw_default_label = old_saw_default_label;
4066 mature_immBlock(break_block);
4067 set_cur_block(break_block);
4070 static void case_label_to_firm(const case_label_statement_t *statement)
4072 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4074 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4076 /* let's create a node and hope firm constant folding creates a Const
4079 ir_node *old_block = get_nodes_block(current_switch_cond);
4080 ir_node *block = new_immBlock();
4082 set_cur_block(old_block);
4083 if(statement->expression != NULL) {
4084 long start_pn = fold_constant(statement->expression);
4085 long end_pn = start_pn;
4086 if (statement->end_range != NULL) {
4087 end_pn = fold_constant(statement->end_range);
4089 assert(start_pn <= end_pn);
4090 /* create jumps for all cases in the given range */
4091 for (long pn = start_pn; pn <= end_pn; ++pn) {
4092 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
4093 /* oops someone detected our cheating... */
4094 panic("magic default pn used");
4096 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4097 add_immBlock_pred(block, proj);
4100 saw_default_label = true;
4101 proj = new_d_defaultProj(dbgi, current_switch_cond,
4102 MAGIC_DEFAULT_PN_NUMBER);
4104 add_immBlock_pred(block, proj);
4107 if (fallthrough != NULL) {
4108 add_immBlock_pred(block, fallthrough);
4110 mature_immBlock(block);
4111 set_cur_block(block);
4113 if(statement->statement != NULL) {
4114 statement_to_firm(statement->statement);
4118 static ir_node *get_label_block(declaration_t *label)
4120 assert(label->namespc == NAMESPACE_LABEL);
4122 if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4123 return label->v.block;
4125 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4127 ir_node *old_cur_block = get_cur_block();
4128 ir_node *block = new_immBlock();
4129 set_cur_block(old_cur_block);
4131 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4132 label->v.block = block;
4134 ARR_APP1(ir_node *, imature_blocks, block);
4139 static void label_to_firm(const label_statement_t *statement)
4141 ir_node *block = get_label_block(statement->label);
4143 if(get_cur_block() != NULL) {
4144 ir_node *jmp = new_Jmp();
4145 add_immBlock_pred(block, jmp);
4148 set_cur_block(block);
4151 if(statement->statement != NULL) {
4152 statement_to_firm(statement->statement);
4156 static void goto_to_firm(const goto_statement_t *statement)
4158 if(get_cur_block() == NULL)
4161 ir_node *block = get_label_block(statement->label);
4162 ir_node *jmp = new_Jmp();
4163 add_immBlock_pred(block, jmp);
4165 set_cur_block(NULL);
4168 typedef enum modifier_t {
4169 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
4170 ASM_MODIFIER_READ_WRITE = 1 << 1,
4171 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
4172 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
4175 static void asm_statement_to_firm(const asm_statement_t *statement)
4178 fprintf(stderr, "WARNING asm not implemented yet!\n");
4180 bool needs_memory = false;
4182 size_t n_clobbers = 0;
4183 asm_clobber_t *clobber = statement->clobbers;
4184 for( ; clobber != NULL; clobber = clobber->next) {
4185 if(strcmp(clobber->clobber, "memory") == 0) {
4186 needs_memory = true;
4190 ident *id = new_id_from_str(clobber->clobber);
4191 obstack_ptr_grow(&asm_obst, id);
4194 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4195 ident **clobbers = NULL;
4196 if(n_clobbers > 0) {
4197 clobbers = obstack_finish(&asm_obst);
4200 /* find and count input and output constraints */
4201 asm_constraint_t *constraint = statement->inputs;
4202 for( ; constraint != NULL; constraint = constraint->next) {
4204 bool supports_memop = false;
4205 for(const char *c = constraint->constraints; *c != 0; ++c) {
4206 /* TODO: improve error messages */
4210 panic("multiple alternative assembler constraints not "
4218 supports_memop = true;
4219 obstack_1grow(&asm_obst, *c);
4222 if(modifiers & ASM_MODIFIER_READ_WRITE)
4223 panic("inconsistent register constraints");
4224 modifiers |= ASM_MODIFIER_WRITE_ONLY;
4227 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
4228 panic("inconsistent register constraints");
4229 modifiers |= ASM_MODIFIER_READ_WRITE;
4232 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
4233 panic("early clobber assembler constraint not supported yet");
4236 modifiers |= ASM_MODIFIER_COMMUTATIVE;
4237 panic("commutative assembler constraint not supported yet");
4240 /* skip register preferences stuff... */
4241 while(*c != 0 && *c != ',')
4245 /* skip register preferences stuff... */
4249 obstack_1grow(&asm_obst, *c);
4253 obstack_1grow(&asm_obst, '\0');
4254 const char *constraint_string = obstack_finish(&asm_obst);
4256 needs_memory |= supports_memop;
4257 if(supports_memop) {
4264 static void statement_to_firm(statement_t *statement)
4266 switch(statement->kind) {
4267 case STATEMENT_INVALID:
4268 panic("invalid statement found");
4270 case STATEMENT_EMPTY:
4273 case STATEMENT_COMPOUND:
4274 compound_statement_to_firm(&statement->compound);
4276 case STATEMENT_RETURN:
4277 return_statement_to_firm(&statement->returns);
4279 case STATEMENT_EXPRESSION:
4280 expression_statement_to_firm(&statement->expression);
4283 if_statement_to_firm(&statement->ifs);
4285 case STATEMENT_WHILE:
4286 while_statement_to_firm(&statement->whiles);
4288 case STATEMENT_DO_WHILE:
4289 do_while_statement_to_firm(&statement->do_while);
4291 case STATEMENT_DECLARATION:
4292 declaration_statement_to_firm(&statement->declaration);
4294 case STATEMENT_BREAK:
4295 create_jump_statement(statement, break_label);
4297 case STATEMENT_CONTINUE:
4298 create_jump_statement(statement, continue_label);
4300 case STATEMENT_SWITCH:
4301 switch_statement_to_firm(&statement->switchs);
4303 case STATEMENT_CASE_LABEL:
4304 case_label_to_firm(&statement->case_label);
4307 for_statement_to_firm(&statement->fors);
4309 case STATEMENT_LABEL:
4310 label_to_firm(&statement->label);
4312 case STATEMENT_GOTO:
4313 goto_to_firm(&statement->gotos);
4316 asm_statement_to_firm(&statement->asms);
4319 panic("Statement not implemented\n");
4322 static int count_decls_in_expression(const expression_t *expression);
4324 static int count_local_declarations(const declaration_t * decl,
4325 const declaration_t *const end)
4328 for (; decl != end; decl = decl->next) {
4329 if(decl->namespc != NAMESPACE_NORMAL)
4331 const type_t *type = skip_typeref(decl->type);
4332 if (!decl->address_taken && is_type_scalar(type))
4334 const initializer_t *initializer = decl->init.initializer;
4335 /* FIXME: should walk initializer hierarchies... */
4336 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4337 count += count_decls_in_expression(initializer->value.value);
4343 static int count_decls_in_expression(const expression_t *expression) {
4346 if(expression == NULL)
4349 switch((expression_kind_t) expression->base.kind) {
4350 case EXPR_STATEMENT:
4351 return count_decls_in_stmts(expression->statement.statement);
4353 int count_left = count_decls_in_expression(expression->binary.left);
4354 int count_right = count_decls_in_expression(expression->binary.right);
4355 return count_left + count_right;
4358 return count_decls_in_expression(expression->unary.value);
4360 call_argument_t *argument = expression->call.arguments;
4361 for( ; argument != NULL; argument = argument->next) {
4362 count += count_decls_in_expression(argument->expression);
4369 panic("unexpected expression kind");
4371 case EXPR_COMPOUND_LITERAL:
4375 case EXPR_CONDITIONAL:
4376 count += count_decls_in_expression(expression->conditional.condition);
4377 count += count_decls_in_expression(expression->conditional.true_expression);
4378 count += count_decls_in_expression(expression->conditional.false_expression);
4381 case EXPR_BUILTIN_PREFETCH:
4382 count += count_decls_in_expression(expression->builtin_prefetch.adr);
4383 count += count_decls_in_expression(expression->builtin_prefetch.rw);
4384 count += count_decls_in_expression(expression->builtin_prefetch.locality);
4387 case EXPR_BUILTIN_CONSTANT_P:
4388 count += count_decls_in_expression(expression->builtin_constant.value);
4392 count += count_decls_in_expression(expression->select.compound);
4395 case EXPR_ARRAY_ACCESS:
4396 count += count_decls_in_expression(expression->array_access.array_ref);
4397 count += count_decls_in_expression(expression->array_access.index);
4400 case EXPR_CLASSIFY_TYPE:
4401 count += count_decls_in_expression(expression->classify_type.type_expression);
4405 case EXPR_ALIGNOF: {
4406 expression_t *tp_expression = expression->typeprop.tp_expression;
4407 if (tp_expression != NULL) {
4408 count += count_decls_in_expression(tp_expression);
4414 case EXPR_REFERENCE:
4416 case EXPR_CHARACTER_CONSTANT:
4417 case EXPR_WIDE_CHARACTER_CONSTANT:
4418 case EXPR_STRING_LITERAL:
4419 case EXPR_WIDE_STRING_LITERAL:
4421 case EXPR_PRETTY_FUNCTION:
4423 case EXPR_FUNCDNAME:
4424 case EXPR_BUILTIN_SYMBOL:
4430 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
4431 * (or implement all the missing expressions here/implement a walker)
4437 static int count_decls_in_stmts(const statement_t *stmt)
4440 for (; stmt != NULL; stmt = stmt->base.next) {
4441 switch (stmt->kind) {
4442 case STATEMENT_EMPTY:
4445 case STATEMENT_DECLARATION: {
4446 const declaration_statement_t *const decl_stmt = &stmt->declaration;
4447 count += count_local_declarations(decl_stmt->declarations_begin,
4448 decl_stmt->declarations_end->next);
4452 case STATEMENT_COMPOUND: {
4453 const compound_statement_t *const comp =
4455 count += count_decls_in_stmts(comp->statements);
4459 case STATEMENT_IF: {
4460 const if_statement_t *const if_stmt = &stmt->ifs;
4461 count += count_decls_in_expression(if_stmt->condition);
4462 count += count_decls_in_stmts(if_stmt->true_statement);
4463 count += count_decls_in_stmts(if_stmt->false_statement);
4467 case STATEMENT_SWITCH: {
4468 const switch_statement_t *const switch_stmt = &stmt->switchs;
4469 count += count_decls_in_expression(switch_stmt->expression);
4470 count += count_decls_in_stmts(switch_stmt->body);
4474 case STATEMENT_LABEL: {
4475 const label_statement_t *const label_stmt = &stmt->label;
4476 if(label_stmt->statement != NULL) {
4477 count += count_decls_in_stmts(label_stmt->statement);
4482 case STATEMENT_WHILE: {
4483 const while_statement_t *const while_stmt = &stmt->whiles;
4484 count += count_decls_in_expression(while_stmt->condition);
4485 count += count_decls_in_stmts(while_stmt->body);
4489 case STATEMENT_DO_WHILE: {
4490 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
4491 count += count_decls_in_expression(do_while_stmt->condition);
4492 count += count_decls_in_stmts(do_while_stmt->body);
4496 case STATEMENT_FOR: {
4497 const for_statement_t *const for_stmt = &stmt->fors;
4498 count += count_local_declarations(for_stmt->scope.declarations, NULL);
4499 count += count_decls_in_expression(for_stmt->initialisation);
4500 count += count_decls_in_expression(for_stmt->condition);
4501 count += count_decls_in_expression(for_stmt->step);
4502 count += count_decls_in_stmts(for_stmt->body);
4506 case STATEMENT_CASE_LABEL: {
4507 const case_label_statement_t *label = &stmt->case_label;
4508 count += count_decls_in_expression(label->expression);
4509 if(label->statement != NULL) {
4510 count += count_decls_in_stmts(label->statement);
4516 case STATEMENT_BREAK:
4517 case STATEMENT_CONTINUE:
4520 case STATEMENT_EXPRESSION: {
4521 const expression_statement_t *expr_stmt = &stmt->expression;
4522 count += count_decls_in_expression(expr_stmt->expression);
4526 case STATEMENT_GOTO:
4527 case STATEMENT_INVALID:
4530 case STATEMENT_RETURN: {
4531 const return_statement_t *ret_stmt = &stmt->returns;
4532 count += count_decls_in_expression(ret_stmt->value);
4540 static int get_function_n_local_vars(declaration_t *declaration)
4544 /* count parameters */
4545 count += count_local_declarations(declaration->scope.declarations, NULL);
4547 /* count local variables declared in body */
4548 count += count_decls_in_stmts(declaration->init.statement);
4553 static void initialize_function_parameters(declaration_t *declaration)
4555 ir_graph *irg = current_ir_graph;
4556 ir_node *args = get_irg_args(irg);
4557 ir_node *start_block = get_irg_start_block(irg);
4558 ir_type *function_irtype = get_ir_type(declaration->type);
4561 declaration_t *parameter = declaration->scope.declarations;
4562 for( ; parameter != NULL; parameter = parameter->next, ++n) {
4563 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
4564 type_t *type = skip_typeref(parameter->type);
4566 bool needs_entity = parameter->address_taken;
4567 assert(!is_type_array(type));
4568 if(is_type_compound(type)) {
4569 needs_entity = true;
4573 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
4574 ident *id = new_id_from_str(parameter->symbol->string);
4575 set_entity_ident(entity, id);
4577 parameter->declaration_kind
4578 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
4579 parameter->v.entity = entity;
4583 ir_mode *mode = get_ir_mode(parameter->type);
4585 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
4587 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
4588 parameter->v.value_number = next_value_number_function;
4589 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
4590 ++next_value_number_function;
4592 set_value(parameter->v.value_number, proj);
4597 * Handle additional decl modifiers for IR-graphs
4599 * @param irg the IR-graph
4600 * @param dec_modifiers additional modifiers
4602 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
4604 if (decl_modifiers & DM_NORETURN) {
4605 /* TRUE if the declaration includes the Microsoft
4606 __declspec(noreturn) specifier. */
4607 set_irg_additional_property(irg, mtp_property_noreturn);
4609 if (decl_modifiers & DM_NOTHROW) {
4610 /* TRUE if the declaration includes the Microsoft
4611 __declspec(nothrow) specifier. */
4612 set_irg_additional_property(irg, mtp_property_nothrow);
4614 if (decl_modifiers & DM_NAKED) {
4615 /* TRUE if the declaration includes the Microsoft
4616 __declspec(naked) specifier. */
4617 set_irg_additional_property(irg, mtp_property_naked);
4619 if (decl_modifiers & DM_FORCEINLINE) {
4620 /* TRUE if the declaration includes the
4621 Microsoft __forceinline specifier. */
4622 set_irg_inline_property(irg, irg_inline_forced);
4624 if (decl_modifiers & DM_NOINLINE) {
4625 /* TRUE if the declaration includes the Microsoft
4626 __declspec(noinline) specifier. */
4627 set_irg_inline_property(irg, irg_inline_forbidden);
4631 static void create_function(declaration_t *declaration)
4633 ir_entity *function_entity = get_function_entity(declaration);
4635 if(declaration->init.statement == NULL)
4638 current_function_decl = declaration;
4639 current_function_name = NULL;
4640 current_funcsig = NULL;
4642 assert(imature_blocks == NULL);
4643 imature_blocks = NEW_ARR_F(ir_node*, 0);
4645 int n_local_vars = get_function_n_local_vars(declaration);
4646 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
4647 ir_node *first_block = get_cur_block();
4649 /* set inline flags */
4650 if (declaration->is_inline)
4651 set_irg_inline_property(irg, irg_inline_recomended);
4652 handle_decl_modifier_irg(irg, declaration->modifiers);
4654 next_value_number_function = 0;
4655 initialize_function_parameters(declaration);
4657 statement_to_firm(declaration->init.statement);
4659 ir_node *end_block = get_irg_end_block(irg);
4661 /* do we have a return statement yet? */
4662 if(get_cur_block() != NULL) {
4663 type_t *type = skip_typeref(declaration->type);
4664 assert(is_type_function(type));
4665 const function_type_t *func_type = &type->function;
4666 const type_t *return_type
4667 = skip_typeref(func_type->return_type);
4670 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4671 ret = new_Return(get_store(), 0, NULL);
4674 if(is_type_scalar(return_type)) {
4675 mode = get_ir_mode(func_type->return_type);
4681 /* ยง5.1.2.2.3 main implicitly returns 0 */
4682 if (strcmp(declaration->symbol->string, "main") == 0) {
4683 in[0] = new_Const(mode, get_mode_null(mode));
4685 in[0] = new_Unknown(mode);
4687 ret = new_Return(get_store(), 1, in);
4689 add_immBlock_pred(end_block, ret);
4692 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
4693 mature_immBlock(imature_blocks[i]);
4695 DEL_ARR_F(imature_blocks);
4696 imature_blocks = NULL;
4698 mature_immBlock(first_block);
4699 mature_immBlock(end_block);
4701 irg_finalize_cons(irg);
4703 /* finalize the frame type */
4704 ir_type *frame_type = get_irg_frame_type(irg);
4705 int n = get_compound_n_members(frame_type);
4708 for(int i = 0; i < n; ++i) {
4709 ir_entity *entity = get_compound_member(frame_type, i);
4710 ir_type *entity_type = get_entity_type(entity);
4712 int align = get_type_alignment_bytes(entity_type);
4713 if(align > align_all)
4717 misalign = offset % align;
4719 offset += align - misalign;
4723 set_entity_offset(entity, offset);
4724 offset += get_type_size_bytes(entity_type);
4726 set_type_size_bytes(frame_type, offset);
4727 set_type_alignment_bytes(frame_type, align_all);
4732 static void scope_to_firm(scope_t *scope)
4734 /* first pass: create declarations */
4735 declaration_t *declaration = scope->declarations;
4736 for( ; declaration != NULL; declaration = declaration->next) {
4737 if(declaration->namespc != NAMESPACE_NORMAL)
4739 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4740 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4742 if(declaration->symbol == NULL)
4745 type_t *type = skip_typeref(declaration->type);
4746 if(is_type_function(type)) {
4747 get_function_entity(declaration);
4749 create_global_variable(declaration);
4753 /* second pass: create code/initializers */
4754 declaration = scope->declarations;
4755 for( ; declaration != NULL; declaration = declaration->next) {
4756 if(declaration->namespc != NAMESPACE_NORMAL)
4758 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4759 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4761 if(declaration->symbol == NULL)
4764 type_t *type = declaration->type;
4765 if(type->kind == TYPE_FUNCTION) {
4766 create_function(declaration);
4768 assert(declaration->declaration_kind
4769 == DECLARATION_KIND_GLOBAL_VARIABLE);
4770 current_ir_graph = get_const_code_irg();
4771 create_declaration_initializer(declaration);
4776 void init_ast2firm(void)
4778 obstack_init(&asm_obst);
4779 init_atomic_modes();
4781 /* create idents for all known runtime functions */
4782 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
4783 predef_idents[rts_data[i].id] = new_id_from_str(rts_data[i].name);
4787 static void init_ir_types(void)
4789 static int ir_types_initialized = 0;
4790 if(ir_types_initialized)
4792 ir_types_initialized = 1;
4794 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4795 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4796 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4798 ir_type_int = get_ir_type(type_int);
4799 ir_type_const_char = get_ir_type(type_const_char);
4800 ir_type_wchar_t = get_ir_type(type_wchar_t);
4801 ir_type_void = get_ir_type(type_int); /* we don't have a real void
4804 type_void->base.firm_type = ir_type_void;
4807 void exit_ast2firm(void)
4809 obstack_free(&asm_obst, NULL);
4812 void translation_unit_to_firm(translation_unit_t *unit)
4814 /* just to be sure */
4815 continue_label = NULL;
4817 current_switch_cond = NULL;
4821 scope_to_firm(&unit->scope);