9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
14 #include "adt/error.h"
15 #include "adt/array.h"
20 #include "lang_features.h"
22 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
24 static ir_type *ir_type_const_char;
25 static ir_type *ir_type_wchar_t;
26 static ir_type *ir_type_void;
27 static ir_type *ir_type_int;
29 static type_t *type_const_char;
30 static type_t *type_void;
31 static type_t *type_int;
33 static int next_value_number_function;
34 static ir_node *continue_label;
35 static ir_node *break_label;
36 static ir_node *current_switch_cond;
37 static bool saw_default_label;
38 static ir_node **imature_blocks;
40 static const declaration_t *current_function_decl;
41 static ir_node *current_function_name;
43 static struct obstack asm_obst;
45 typedef enum declaration_type_t {
46 DECLARATION_TYPE_UNKNOWN,
47 DECLARATION_TYPE_FUNCTION,
48 DECLARATION_TYPE_GLOBAL_VARIABLE,
49 DECLARATION_TYPE_LOCAL_VARIABLE,
50 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
51 DECLARATION_TYPE_COMPOUND_MEMBER,
52 DECLARATION_TYPE_LABEL_BLOCK,
53 DECLARATION_TYPE_ENUM_ENTRY
56 static ir_type *get_ir_type(type_t *type);
57 static int count_decls_in_stmts(const statement_t *stmt);
59 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
63 const declaration_t *declaration = & value_numbers[pos]->declaration;
65 print_warning_prefix(declaration->source_position);
66 fprintf(stderr, "variable '%s' might be used uninitialized\n",
67 declaration->symbol->string);
69 fprintf(stderr, "Some variable might be used uninitialized\n");
70 return new_r_Unknown(irg, mode);
73 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
75 const source_position_t *pos = (const source_position_t*) dbg;
78 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
82 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
84 const source_position_t *pos = (const source_position_t*) dbg;
89 return pos->input_name;
92 static dbg_info *get_dbg_info(const source_position_t *pos)
94 return (dbg_info*) pos;
97 static unsigned unique_id = 0;
99 static ident *unique_ident(const char *tag)
103 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
105 return new_id_from_str(buf);
109 * Return the signed integer mode of size bytes.
111 * @param size the size
113 static ir_mode *get_smode(unsigned size)
115 static ir_mode *s_modes[16 + 1] = {0, };
118 if (size <= 0 || size > 16)
127 snprintf(name, sizeof(name), "i%u", bits);
128 res = new_ir_mode(name, irms_int_number, bits, 1, irma_twos_complement,
129 bits <= machine_size ? machine_size : bits );
137 * Return the unsigned integer mode of size bytes.
139 * @param size the size
141 static ir_mode *get_umode(unsigned size)
143 static ir_mode *u_modes[16 + 1] = {0, };
146 if (size <= 0 || size > 16)
155 snprintf(name, sizeof(name), "u%u", bits);
156 res = new_ir_mode(name, irms_int_number, bits, 0, irma_twos_complement,
157 bits <= machine_size ? machine_size : bits );
165 * Return the pointer mode of size bytes.
167 * @param size the size
169 static ir_mode *get_ptrmode(unsigned size, char *name)
171 static ir_mode *p_modes[16 + 1] = {0, };
174 if (size <= 0 || size > 16)
184 snprintf(buf, sizeof(buf), "p%u", bits);
187 res = new_ir_mode(name, irms_reference, bits, 0, irma_twos_complement,
188 bits <= machine_size ? machine_size : bits);
192 set_reference_mode_signed_eq(res, get_smode(size));
193 set_reference_mode_unsigned_eq(res, get_umode(size));
198 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST];
200 static ir_mode *mode_int, *mode_uint;
203 * Initialises the atomic modes depending on the machine size.
205 static void init_atomic_modes(void) {
206 unsigned int_size = machine_size < 32 ? 2 : 4;
207 unsigned long_size = machine_size < 64 ? 4 : 8;
208 unsigned llong_size = machine_size < 32 ? 4 : 8;
210 /* firm has no real void... */
211 _atomic_modes[ATOMIC_TYPE_VOID] = mode_T;
212 _atomic_modes[ATOMIC_TYPE_CHAR] = char_is_signed ? get_smode(1) : get_umode(1);
213 _atomic_modes[ATOMIC_TYPE_SCHAR] = get_smode(1);
214 _atomic_modes[ATOMIC_TYPE_UCHAR] = get_umode(1);
215 _atomic_modes[ATOMIC_TYPE_SHORT] = get_smode(2);
216 _atomic_modes[ATOMIC_TYPE_USHORT] = get_umode(2);
217 _atomic_modes[ATOMIC_TYPE_INT] = get_smode(int_size);
218 _atomic_modes[ATOMIC_TYPE_UINT] = get_umode(int_size);
219 _atomic_modes[ATOMIC_TYPE_LONG] = get_smode(long_size);
220 _atomic_modes[ATOMIC_TYPE_ULONG] = get_umode(long_size);
221 _atomic_modes[ATOMIC_TYPE_LONGLONG] = get_smode(llong_size);
222 _atomic_modes[ATOMIC_TYPE_ULONGLONG] = get_umode(llong_size);
223 _atomic_modes[ATOMIC_TYPE_FLOAT] = mode_F;
224 _atomic_modes[ATOMIC_TYPE_DOUBLE] = mode_D;
225 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = mode_E;
227 #ifdef PROVIDE_COMPLEX
228 _atomic_modes[ATOMIC_TYPE_BOOL] = _atomic_modes[ATOMIC_TYPE_INT];
229 _atomic_modes[ATOMIC_TYPE_FLOAT_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_FLOAT];
230 _atomic_modes[ATOMIC_TYPE_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_DOUBLE];
231 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE];
234 /* Hmm, pointers should be machine size */
235 set_modeP_data(get_ptrmode(machine_size >> 3, NULL));
236 set_modeP_code(get_ptrmode(machine_size >> 3, NULL));
238 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
239 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
242 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
245 if ((unsigned)atomic_type->atype < (unsigned)ATOMIC_TYPE_LAST)
246 res = _atomic_modes[(unsigned)atomic_type->atype];
248 panic("Encountered unknown atomic type");
252 static unsigned get_type_size(type_t *type);
254 static unsigned get_atomic_type_size(const atomic_type_t *type)
256 switch(type->atype) {
257 case ATOMIC_TYPE_CHAR:
258 case ATOMIC_TYPE_SCHAR:
259 case ATOMIC_TYPE_UCHAR:
262 case ATOMIC_TYPE_SHORT:
263 case ATOMIC_TYPE_USHORT:
266 case ATOMIC_TYPE_BOOL:
267 case ATOMIC_TYPE_INT:
268 case ATOMIC_TYPE_UINT:
269 return machine_size >> 3;
271 case ATOMIC_TYPE_LONG:
272 case ATOMIC_TYPE_ULONG:
273 return machine_size > 16 ? machine_size >> 3 : 4;
275 case ATOMIC_TYPE_LONGLONG:
276 case ATOMIC_TYPE_ULONGLONG:
277 return machine_size > 16 ? 8 : 4;
279 case ATOMIC_TYPE_FLOAT:
282 case ATOMIC_TYPE_DOUBLE:
285 case ATOMIC_TYPE_LONG_DOUBLE:
288 case ATOMIC_TYPE_VOID:
291 case ATOMIC_TYPE_INVALID:
292 case ATOMIC_TYPE_LAST:
295 panic("Trying to determine size of invalid atomic type");
298 static unsigned get_compound_type_size(compound_type_t *type)
300 ir_type *irtype = get_ir_type((type_t*) type);
301 return get_type_size_bytes(irtype);
304 static unsigned get_array_type_size(array_type_t *type)
306 ir_type *irtype = get_ir_type((type_t*) type);
307 return get_type_size_bytes(irtype);
311 static unsigned get_type_size(type_t *type)
313 type = skip_typeref(type);
317 return get_atomic_type_size(&type->atomic);
319 return get_mode_size_bytes(mode_int);
320 case TYPE_COMPOUND_UNION:
321 case TYPE_COMPOUND_STRUCT:
322 return get_compound_type_size(&type->compound);
324 /* just a pointer to the function */
325 return get_mode_size_bytes(mode_P_code);
327 return get_mode_size_bytes(mode_P_data);
329 return get_array_type_size(&type->array);
331 return get_type_size(type->builtin.real_type);
337 panic("Trying to determine size of invalid type");
340 static unsigned count_parameters(const function_type_t *function_type)
344 function_parameter_t *parameter = function_type->parameters;
345 for ( ; parameter != NULL; parameter = parameter->next) {
355 static long fold_constant(const expression_t *expression);
357 static ir_type *create_atomic_type(const atomic_type_t *type)
359 ir_mode *mode = get_atomic_mode(type);
360 ident *id = get_mode_ident(mode);
361 ir_type *irtype = new_type_primitive(id, mode);
363 if(type->atype == ATOMIC_TYPE_LONG_DOUBLE) {
364 set_type_alignment_bytes(irtype, 4);
370 static ir_type *create_method_type(const function_type_t *function_type)
372 type_t *return_type = function_type->return_type;
374 ident *id = unique_ident("functiontype");
375 int n_parameters = count_parameters(function_type);
376 int n_results = return_type == type_void ? 0 : 1;
377 ir_type *irtype = new_type_method(id, n_parameters, n_results);
379 if(return_type != type_void) {
380 ir_type *restype = get_ir_type(return_type);
381 set_method_res_type(irtype, 0, restype);
384 function_parameter_t *parameter = function_type->parameters;
386 for( ; parameter != NULL; parameter = parameter->next) {
387 ir_type *p_irtype = get_ir_type(parameter->type);
388 set_method_param_type(irtype, n, p_irtype);
392 if(function_type->variadic || function_type->unspecified_parameters) {
393 set_method_variadicity(irtype, variadicity_variadic);
399 static ir_type *create_pointer_type(pointer_type_t *type)
401 type_t *points_to = type->points_to;
402 ir_type *ir_points_to;
403 /* Avoid endless recursion if the points_to type contains this poiner type
404 * again (might be a struct). We therefore first create a void* pointer
405 * and then set the real points_to type
407 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
408 ir_type_void, mode_P_data);
409 type->type.firm_type = ir_type;
411 ir_points_to = get_ir_type(points_to);
412 set_pointer_points_to_type(ir_type, ir_points_to);
417 static ir_type *create_array_type(array_type_t *type)
419 type_t *element_type = type->element_type;
420 ir_type *ir_element_type = get_ir_type(element_type);
422 ident *id = unique_ident("array");
423 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
425 if(type->size != NULL) {
426 int n_elements = fold_constant(type->size);
428 set_array_bounds_int(ir_type, 0, 0, n_elements);
430 size_t elemsize = get_type_size_bytes(ir_element_type);
431 int align = get_type_alignment_bytes(ir_element_type);
432 if(elemsize % align > 0) {
433 elemsize += align - (elemsize % align);
435 set_type_size_bytes(ir_type, n_elements * elemsize);
436 set_type_alignment_bytes(ir_type, align);
437 set_type_state(ir_type, layout_fixed);
439 set_array_lower_bound_int(ir_type, 0, 0);
445 #define INVALID_TYPE ((ir_type_ptr)-1)
447 static ir_type *create_struct_type(compound_type_t *type)
449 symbol_t *symbol = type->declaration->symbol;
452 id = unique_ident(symbol->string);
454 id = unique_ident("__anonymous_struct");
456 ir_type *ir_type = new_type_struct(id);
458 type->type.firm_type = ir_type;
462 declaration_t *entry = type->declaration->context.declarations;
463 for( ; entry != NULL; entry = entry->next) {
464 if(entry->namespc != NAMESPACE_NORMAL)
467 ident *ident = new_id_from_str(entry->symbol->string);
468 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
470 int entry_size = get_type_size_bytes(entry_ir_type);
471 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
472 int misalign = offset % entry_alignment;
474 offset += entry_alignment - misalign;
476 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
477 ir_entity *const entity = new_d_entity(ir_type, ident, entry_ir_type, dbgi);
478 set_entity_offset(entity, offset);
479 add_struct_member(ir_type, entity);
480 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
481 entry->v.entity = entity;
483 offset += entry_size;
484 if(entry_alignment > align_all) {
485 if(entry_alignment % align_all != 0) {
486 panic("Uneven alignments not supported yet");
488 align_all = entry_alignment;
492 int misalign = offset % align_all;
494 set_type_alignment_bytes(ir_type, align_all);
495 set_type_size_bytes(ir_type, offset);
496 set_type_state(ir_type, layout_fixed);
501 static ir_type *create_union_type(compound_type_t *type)
503 declaration_t *declaration = type->declaration;
504 symbol_t *symbol = declaration->symbol;
507 id = unique_ident(symbol->string);
509 id = unique_ident("__anonymous_union");
511 ir_type *ir_type = new_type_union(id);
513 type->type.firm_type = ir_type;
517 declaration_t *entry = declaration->context.declarations;
518 for( ; entry != NULL; entry = entry->next) {
519 if(entry->namespc != NAMESPACE_NORMAL)
522 ident *ident = new_id_from_str(entry->symbol->string);
523 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
525 int entry_size = get_type_size_bytes(entry_ir_type);
526 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
528 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
529 ir_entity *const entity = new_d_entity(ir_type, ident, entry_ir_type, dbgi);
530 add_union_member(ir_type, entity);
531 set_entity_offset(entity, 0);
532 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
533 entry->v.entity = entity;
535 if(entry_size > size) {
538 if(entry_alignment > align_all) {
539 if(entry_alignment % align_all != 0) {
540 panic("Uneven alignments not supported yet");
542 align_all = entry_alignment;
546 set_type_alignment_bytes(ir_type, align_all);
547 set_type_size_bytes(ir_type, size);
548 set_type_state(ir_type, layout_fixed);
553 static ir_node *expression_to_firm(const expression_t *expression);
554 static inline ir_mode *get_ir_mode(type_t *type);
556 static ir_type *create_enum_type(enum_type_t *const type)
558 type->type.firm_type = ir_type_int;
560 ir_mode *const mode = get_ir_mode((type_t*) type);
561 tarval *const one = get_mode_one(mode);
562 tarval * tv_next = get_tarval_null(mode);
564 declaration_t *declaration = type->declaration->next;
565 for (; declaration != NULL; declaration = declaration->next) {
566 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
569 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
571 expression_t *const init = declaration->init.enum_value;
573 ir_node *const cnst = expression_to_firm(init);
574 if (!is_Const(cnst)) {
575 panic("couldn't fold constant");
577 tv_next = get_Const_tarval(cnst);
579 declaration->v.enum_val = tv_next;
580 tv_next = tarval_add(tv_next, one);
586 static ir_type *get_ir_type(type_t *type)
588 assert(type != NULL);
590 type = skip_typeref(type);
592 if(type->base.firm_type != NULL) {
593 assert(type->base.firm_type != INVALID_TYPE);
594 return type->base.firm_type;
597 ir_type *firm_type = NULL;
600 firm_type = create_atomic_type(&type->atomic);
603 firm_type = create_method_type(&type->function);
606 firm_type = create_pointer_type(&type->pointer);
609 firm_type = create_array_type(&type->array);
611 case TYPE_COMPOUND_STRUCT:
612 firm_type = create_struct_type(&type->compound);
614 case TYPE_COMPOUND_UNION:
615 firm_type = create_union_type(&type->compound);
618 firm_type = create_enum_type(&type->enumt);
621 firm_type = get_ir_type(type->builtin.real_type);
628 if(firm_type == NULL)
629 panic("unknown type found");
631 type->base.firm_type = firm_type;
635 static inline ir_mode *get_ir_mode(type_t *type)
637 ir_type *irtype = get_ir_type(type);
639 /* firm doesn't report a mode for arrays somehow... */
640 if(is_Array_type(irtype)) {
644 ir_mode *mode = get_type_mode(irtype);
645 assert(mode != NULL);
649 static ir_entity* get_function_entity(declaration_t *declaration)
651 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
652 return declaration->v.entity;
653 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
655 symbol_t *symbol = declaration->symbol;
656 ident *id = new_id_from_str(symbol->string);
658 ir_type *global_type = get_glob_type();
659 ir_type *ir_type_method = get_ir_type(declaration->type);
660 assert(is_Method_type(ir_type_method));
662 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
663 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
664 set_entity_ld_ident(entity, id);
665 if(declaration->storage_class == STORAGE_CLASS_STATIC
666 || declaration->is_inline) {
667 set_entity_visibility(entity, visibility_local);
668 } else if(declaration->init.statement != NULL) {
669 set_entity_visibility(entity, visibility_external_visible);
671 set_entity_visibility(entity, visibility_external_allocated);
673 set_entity_allocation(entity, allocation_static);
675 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
676 declaration->v.entity = entity;
681 static ir_node *const_to_firm(const const_expression_t *cnst)
683 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
684 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
689 if(mode_is_float(mode)) {
690 tv = new_tarval_from_double(cnst->v.float_value, mode);
692 if(mode_is_signed(mode)) {
693 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
695 len = snprintf(buf, sizeof(buf), "%llu",
696 (unsigned long long) cnst->v.int_value);
698 tv = new_tarval_from_str(buf, len, mode);
701 return new_d_Const(dbgi, mode, tv);
704 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
706 assert(entity != NULL);
707 union symconst_symbol sym;
708 sym.entity_p = entity;
709 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
712 static ir_node *string_to_firm(const source_position_t *const src_pos,
713 const char *const id_prefix,
714 const char *const string)
716 ir_type *const global_type = get_glob_type();
717 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
720 ident *const id = unique_ident(id_prefix);
721 dbg_info *const dbgi = get_dbg_info(src_pos);
722 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
723 set_entity_ld_ident(entity, id);
724 set_entity_variability(entity, variability_constant);
725 set_entity_allocation(entity, allocation_static);
727 ir_type *const elem_type = ir_type_const_char;
728 ir_mode *const mode = get_type_mode(elem_type);
730 const size_t slen = strlen(string) + 1;
732 set_array_lower_bound_int(type, 0, 0);
733 set_array_upper_bound_int(type, 0, slen);
734 set_type_size_bytes(type, slen);
735 set_type_state(type, layout_fixed);
737 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
738 for(size_t i = 0; i < slen; ++i) {
739 tvs[i] = new_tarval_from_long(string[i], mode);
742 set_array_entity_values(entity, tvs, slen);
745 return create_symconst(dbgi, entity);
748 static ir_node *string_literal_to_firm(
749 const string_literal_expression_t* literal)
751 return string_to_firm(&literal->expression.source_position, "Lstr",
755 static ir_node *wide_string_literal_to_firm(
756 const wide_string_literal_expression_t* const literal)
758 ir_type *const global_type = get_glob_type();
759 ir_type *const elem_type = ir_type_wchar_t;
760 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
763 ident *const id = unique_ident("Lstr");
764 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
765 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
766 set_entity_ld_ident(entity, id);
767 set_entity_variability(entity, variability_constant);
768 set_entity_allocation(entity, allocation_static);
770 ir_mode *const mode = get_type_mode(elem_type);
772 const wchar_rep_t *const string = literal->value.begin;
773 const size_t slen = literal->value.size;
775 set_array_lower_bound_int(type, 0, 0);
776 set_array_upper_bound_int(type, 0, slen);
777 set_type_size_bytes(type, slen);
778 set_type_state(type, layout_fixed);
780 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
781 for(size_t i = 0; i < slen; ++i) {
782 tvs[i] = new_tarval_from_long(string[i], mode);
785 set_array_entity_values(entity, tvs, slen);
788 return create_symconst(dbgi, entity);
791 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
792 dbg_info *const dbgi)
794 if(is_compound_type(irtype) || is_Array_type(irtype)) {
798 ir_mode *const mode = get_type_mode(irtype);
799 ir_node *const memory = get_store();
800 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
801 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
802 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
807 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
809 ir_mode *mode = get_irn_mode(node);
811 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
813 if(!mode_is_float(mode))
816 /* check if there is already a Conv */
817 if (get_irn_op(node) == op_Conv) {
818 /* convert it into a strict Conv */
819 set_Conv_strict(node, 1);
823 /* otherwise create a new one */
824 return new_d_strictConv(dbgi, node, mode);
827 static ir_node *get_global_var_address(dbg_info *const dbgi,
828 const declaration_t *const decl)
830 assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
832 ir_entity *const entity = decl->v.entity;
833 switch ((storage_class_tag_t)decl->storage_class) {
834 case STORAGE_CLASS_THREAD:
835 case STORAGE_CLASS_THREAD_EXTERN:
836 case STORAGE_CLASS_THREAD_STATIC: {
837 ir_node *const no_mem = new_NoMem();
838 ir_node *const tls = get_irg_tls(current_ir_graph);
839 return new_d_simpleSel(dbgi, no_mem, tls, entity);
843 return create_symconst(dbgi, entity);
847 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
849 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
850 declaration_t *declaration = ref->declaration;
851 type_t *type = skip_typeref(declaration->type);
853 switch((declaration_type_t) declaration->declaration_type) {
854 case DECLARATION_TYPE_UNKNOWN:
855 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
861 case DECLARATION_TYPE_ENUM_ENTRY: {
862 ir_mode *const mode = get_ir_mode(type);
863 return new_Const(mode, declaration->v.enum_val);
866 case DECLARATION_TYPE_LOCAL_VARIABLE: {
867 ir_mode *mode = get_ir_mode(type);
868 return get_value(declaration->v.value_number, mode);
870 case DECLARATION_TYPE_FUNCTION: {
871 return create_symconst(dbgi, declaration->v.entity);
873 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
874 ir_node *const addr = get_global_var_address(dbgi, declaration);
875 ir_type *const irtype = get_entity_type(declaration->v.entity);
876 return deref_address(irtype, addr, dbgi);
879 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
880 ir_entity *entity = declaration->v.entity;
881 ir_node *frame = get_irg_frame(current_ir_graph);
882 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
883 ir_type *irtype = get_entity_type(entity);
884 return deref_address(irtype, sel, dbgi);
887 case DECLARATION_TYPE_COMPOUND_MEMBER:
888 case DECLARATION_TYPE_LABEL_BLOCK:
889 panic("not implemented reference type");
892 panic("reference to declaration with unknown type found");
895 static ir_node *reference_addr(const reference_expression_t *ref)
897 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
898 declaration_t *declaration = ref->declaration;
900 switch((declaration_type_t) declaration->declaration_type) {
901 case DECLARATION_TYPE_UNKNOWN:
903 case DECLARATION_TYPE_LOCAL_VARIABLE:
904 panic("local variable without entity has no address");
905 case DECLARATION_TYPE_FUNCTION: {
906 return create_symconst(dbgi, declaration->v.entity);
908 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
909 ir_node *const addr = get_global_var_address(dbgi, declaration);
912 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
913 ir_entity *entity = declaration->v.entity;
914 ir_node *frame = get_irg_frame(current_ir_graph);
915 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
920 case DECLARATION_TYPE_ENUM_ENTRY:
921 panic("trying to reference enum entry");
923 case DECLARATION_TYPE_COMPOUND_MEMBER:
924 case DECLARATION_TYPE_LABEL_BLOCK:
925 panic("not implemented reference type");
928 panic("reference to declaration with unknown type found");
931 static ir_node *process_builtin_call(const call_expression_t *call)
933 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
935 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
936 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
938 type_t *type = skip_typeref(builtin->expression.datatype);
939 assert(is_type_pointer(type));
941 type_t *function_type = skip_typeref(type->pointer.points_to);
942 symbol_t *symbol = builtin->symbol;
945 case T___builtin_alloca: {
946 if(call->arguments == NULL || call->arguments->next != NULL) {
947 panic("invalid number of parameters on __builtin_alloca");
949 expression_t *argument = call->arguments->expression;
950 ir_node *size = expression_to_firm(argument);
952 ir_node *store = get_store();
953 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
955 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
957 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
961 case T___builtin_nan:
962 case T___builtin_nanf:
963 case T___builtin_nand: {
964 /* Ignore string for now... */
965 assert(is_type_function(function_type));
966 ir_mode *mode = get_ir_mode(function_type->function.return_type);
967 tarval *tv = get_mode_NAN(mode);
968 ir_node *res = new_d_Const(dbgi, mode, tv);
971 case T___builtin_va_end:
974 panic("Unsupported builtin found\n");
978 static ir_node *call_expression_to_firm(const call_expression_t *call)
980 assert(get_cur_block() != NULL);
982 expression_t *function = call->function;
983 if(function->type == EXPR_BUILTIN_SYMBOL) {
984 return process_builtin_call(call);
986 ir_node *callee = expression_to_firm(function);
988 type_t *type = skip_typeref(function->base.datatype);
989 assert(is_type_pointer(type));
990 pointer_type_t *pointer_type = &type->pointer;
991 type_t *points_to = skip_typeref(pointer_type->points_to);
992 assert(is_type_function(points_to));
993 function_type_t *function_type = &points_to->function;
995 int n_parameters = 0;
996 call_argument_t *argument = call->arguments;
997 for( ; argument != NULL; argument = argument->next) {
1001 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1003 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1004 ir_type *new_method_type = NULL;
1005 if(function_type->variadic || function_type->unspecified_parameters) {
1006 /* we need to construct a new method type matching the call
1008 int n_res = get_method_n_ress(ir_method_type);
1009 new_method_type = new_type_method(unique_ident("calltype"),
1010 n_parameters, n_res);
1011 set_method_calling_convention(new_method_type,
1012 get_method_calling_convention(ir_method_type));
1013 set_method_additional_properties(new_method_type,
1014 get_method_additional_properties(ir_method_type));
1016 for(int i = 0; i < n_res; ++i) {
1017 set_method_res_type(new_method_type, i,
1018 get_method_res_type(ir_method_type, i));
1021 ir_node *in[n_parameters];
1023 argument = call->arguments;
1025 for( ; argument != NULL; argument = argument->next) {
1026 expression_t *expression = argument->expression;
1027 ir_node *arg_node = expression_to_firm(expression);
1029 arg_node = do_strict_conv(dbgi, arg_node);
1032 if(new_method_type != NULL) {
1033 ir_type *irtype = get_ir_type(expression->base.datatype);
1034 set_method_param_type(new_method_type, n, irtype);
1039 assert(n == n_parameters);
1041 if(new_method_type != NULL)
1042 ir_method_type = new_method_type;
1044 ir_node *store = get_store();
1045 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1047 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1050 type_t *return_type = skip_typeref(function_type->return_type);
1051 ir_node *result = NULL;
1053 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1055 if(is_type_scalar(return_type)) {
1056 mode = get_ir_mode(return_type);
1060 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1061 result = new_d_Proj(dbgi, resproj, mode, 0);
1067 static void statement_to_firm(statement_t *statement);
1068 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1070 static ir_node *expression_to_addr(const expression_t *expression);
1071 static void create_condition_evaluation(const expression_t *expression,
1072 ir_node *true_block,
1073 ir_node *false_block);
1075 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1078 value = do_strict_conv(dbgi, value);
1080 ir_node *memory = get_store();
1082 if(is_type_scalar(type)) {
1083 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1084 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1085 set_store(store_mem);
1087 ir_type *irtype = get_ir_type(type);
1088 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1089 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1090 set_store(copyb_mem);
1094 static void set_value_for_expression(const expression_t *expression,
1097 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1098 value = do_strict_conv(dbgi, value);
1100 if(expression->type == EXPR_REFERENCE) {
1101 reference_expression_t *ref = (reference_expression_t*) expression;
1103 declaration_t *declaration = ref->declaration;
1104 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
1105 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1106 set_value(declaration->v.value_number, value);
1111 ir_node *addr = expression_to_addr(expression);
1112 type_t *type = skip_typeref(expression->base.datatype);
1113 assign_value(dbgi, addr, type, value);
1116 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1118 ir_mode *value_mode = get_irn_mode(value);
1120 if (value_mode == dest_mode || is_Bad(value))
1123 if(dest_mode == mode_b) {
1124 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1125 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1126 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1130 return new_d_Conv(dbgi, value, dest_mode);
1133 static ir_node *create_incdec(const unary_expression_t *expression)
1135 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1136 type_t *type = skip_typeref(expression->expression.datatype);
1137 ir_mode *mode = get_ir_mode(type);
1138 expression_t *value = expression->value;
1140 ir_node *value_node = expression_to_firm(value);
1143 if(is_type_pointer(type)) {
1144 pointer_type_t *pointer_type = &type->pointer;
1145 unsigned elem_size = get_type_size(pointer_type->points_to);
1146 offset = new_Const_long(mode_int, elem_size);
1148 assert(is_type_arithmetic(type));
1149 offset = new_Const(mode, get_mode_one(mode));
1152 switch(expression->expression.type) {
1153 case EXPR_UNARY_POSTFIX_INCREMENT: {
1154 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1155 set_value_for_expression(value, new_value);
1158 case EXPR_UNARY_POSTFIX_DECREMENT: {
1159 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1160 set_value_for_expression(value, new_value);
1163 case EXPR_UNARY_PREFIX_INCREMENT: {
1164 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1165 set_value_for_expression(value, new_value);
1168 case EXPR_UNARY_PREFIX_DECREMENT: {
1169 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1170 set_value_for_expression(value, new_value);
1174 panic("no incdec expr in create_incdec");
1179 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1181 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1182 type_t *type = skip_typeref(expression->expression.datatype);
1184 if(expression->expression.type == EXPR_UNARY_TAKE_ADDRESS)
1185 return expression_to_addr(expression->value);
1187 const expression_t *value = expression->value;
1188 ir_node *value_node = expression_to_firm(value);
1190 switch(expression->expression.type) {
1191 case EXPR_UNARY_NEGATE: {
1192 ir_mode *mode = get_ir_mode(type);
1193 return new_d_Minus(dbgi, value_node, mode);
1195 case EXPR_UNARY_PLUS:
1197 case EXPR_UNARY_BITWISE_NEGATE: {
1198 ir_mode *mode = get_ir_mode(type);
1199 return new_d_Not(dbgi, value_node, mode);
1201 case EXPR_UNARY_NOT: {
1202 ir_mode *mode = get_ir_mode(type);
1203 if(get_irn_mode(value_node) != mode_b) {
1204 value_node = create_conv(dbgi, value_node, mode_b);
1206 value_node = new_d_Not(dbgi, value_node, mode_b);
1207 if(mode != mode_b) {
1208 value_node = create_conv(dbgi, value_node, mode);
1212 case EXPR_UNARY_DEREFERENCE: {
1213 type_t *value_type = skip_typeref(value->base.datatype);
1214 ir_type *irtype = get_ir_type(value_type);
1215 assert(is_Pointer_type(irtype));
1216 ir_type *points_to = get_pointer_points_to_type(irtype);
1217 return deref_address(points_to, value_node, dbgi);
1219 case EXPR_UNARY_POSTFIX_INCREMENT:
1220 case EXPR_UNARY_POSTFIX_DECREMENT:
1221 case EXPR_UNARY_PREFIX_INCREMENT:
1222 case EXPR_UNARY_PREFIX_DECREMENT:
1223 return create_incdec(expression);
1224 case EXPR_UNARY_CAST: {
1225 ir_mode *mode = get_ir_mode(type);
1226 ir_node *node = create_conv(dbgi, value_node, mode);
1227 node = do_strict_conv(dbgi, node);
1230 case EXPR_UNARY_CAST_IMPLICIT: {
1231 ir_mode *mode = get_ir_mode(type);
1232 return create_conv(dbgi, value_node, mode);
1238 panic("invalid UNEXPR type found");
1241 static long get_pnc(const expression_type_t type)
1244 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1245 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1246 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Ne;
1247 case EXPR_BINARY_ISLESS:
1248 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1249 case EXPR_BINARY_ISLESSEQUAL:
1250 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1251 case EXPR_BINARY_ISGREATER:
1252 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1253 case EXPR_BINARY_ISGREATEREQUAL:
1254 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1255 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1260 panic("trying to get pn_Cmp from non-comparison binexpr type");
1263 static ir_node *create_lazy_op(const binary_expression_t *expression)
1265 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1266 type_t *type = expression->expression.datatype;
1267 ir_mode *mode = get_ir_mode(type);
1269 ir_node *cur_block = get_cur_block();
1271 ir_node *one_block = new_immBlock();
1272 ir_node *one = new_Const(mode, get_mode_one(mode));
1273 ir_node *jmp_one = new_d_Jmp(dbgi);
1275 ir_node *zero_block = new_immBlock();
1276 ir_node *zero = new_Const(mode, get_mode_null(mode));
1277 ir_node *jmp_zero = new_d_Jmp(dbgi);
1279 set_cur_block(cur_block);
1280 create_condition_evaluation((const expression_t*) expression,
1281 one_block, zero_block);
1282 mature_immBlock(one_block);
1283 mature_immBlock(zero_block);
1285 ir_node *common_block = new_immBlock();
1286 add_immBlock_pred(common_block, jmp_one);
1287 add_immBlock_pred(common_block, jmp_zero);
1288 mature_immBlock(common_block);
1290 ir_node *in[2] = { one, zero };
1291 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1296 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1297 ir_node *right, ir_mode *mode);
1299 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1300 create_arithmetic_func func)
1302 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1303 ir_node *left = expression_to_firm(expression->left);
1304 ir_node *right = expression_to_firm(expression->right);
1305 type_t *type = expression->right->base.datatype;
1306 /* be careful with the modes, because in arithmetic assign nodes only
1307 * the right operand has the mode of the arithmetic already */
1308 ir_mode *mode = get_ir_mode(type);
1309 left = create_conv(dbgi, left, mode);
1310 ir_node *res = func(dbgi, left, right, mode);
1315 static ir_node *pointer_arithmetic(ir_node *const pointer,
1318 dbg_info *const dbgi,
1319 const create_arithmetic_func func)
1321 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1322 type_t *const points_to = pointer_type->points_to;
1323 const unsigned elem_size = get_type_size(points_to);
1325 assert(elem_size >= 1);
1326 if (elem_size > 1) {
1327 integer = create_conv(dbgi, integer, mode_int);
1328 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1329 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1333 ir_mode *const mode = get_ir_mode(type);
1334 return func(dbgi, pointer, integer, mode);
1337 static ir_node *create_arithmetic_assign_binop(
1338 const binary_expression_t *expression, create_arithmetic_func func)
1340 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1341 type_t *const type = skip_typeref(expression->expression.datatype);
1344 if (is_type_pointer(type)) {
1345 ir_node *const pointer = expression_to_firm(expression->left);
1346 ir_node * integer = expression_to_firm(expression->right);
1347 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1349 value = create_arithmetic_binop(expression, func);
1352 ir_mode *const mode = get_ir_mode(type);
1353 value = create_conv(dbgi, value, mode);
1354 set_value_for_expression(expression->left, value);
1359 static ir_node *create_add(const binary_expression_t *expression)
1361 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1362 ir_node *left = expression_to_firm(expression->left);
1363 ir_node *right = expression_to_firm(expression->right);
1364 type_t *type = expression->expression.datatype;
1366 expression_t *expr_left = expression->left;
1367 expression_t *expr_right = expression->right;
1368 type_t *type_left = skip_typeref(expr_left->base.datatype);
1369 type_t *type_right = skip_typeref(expr_right->base.datatype);
1371 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1372 ir_mode *const mode = get_ir_mode(type);
1373 return new_d_Add(dbgi, left, right, mode);
1376 if (is_type_pointer(type_left)) {
1377 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1379 assert(is_type_pointer(type_right));
1380 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1384 static ir_node *create_sub(const binary_expression_t *expression)
1386 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1387 expression_t *const expr_left = expression->left;
1388 expression_t *const expr_right = expression->right;
1389 ir_node *const left = expression_to_firm(expr_left);
1390 ir_node *const right = expression_to_firm(expr_right);
1391 type_t *const type = expression->expression.datatype;
1392 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1393 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1395 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1396 ir_mode *const mode = get_ir_mode(type);
1397 return new_d_Sub(dbgi, left, right, mode);
1398 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1399 const pointer_type_t *const ptr_type = &type_left->pointer;
1400 const unsigned elem_size = get_type_size(ptr_type->points_to);
1401 ir_mode *const mode = get_ir_mode(type);
1402 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1403 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1404 ir_node *const no_mem = new_NoMem();
1405 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1406 op_pin_state_floats);
1407 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1410 assert(is_type_pointer(type_left));
1411 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1414 static ir_node *create_shift(const binary_expression_t *expression)
1416 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1417 ir_node *left = expression_to_firm(expression->left);
1418 ir_node *right = expression_to_firm(expression->right);
1419 type_t *type = expression->expression.datatype;
1420 ir_mode *mode = get_ir_mode(type);
1422 /* firm always wants the shift count to be unsigned */
1423 right = create_conv(dbgi, right, mode_uint);
1427 switch(expression->expression.type) {
1428 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1429 case EXPR_BINARY_SHIFTLEFT:
1430 res = new_d_Shl(dbgi, left, right, mode);
1432 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1433 case EXPR_BINARY_SHIFTRIGHT: {
1434 expression_t *expr_left = expression->left;
1435 type_t *type_left = skip_typeref(expr_left->base.datatype);
1437 if(is_type_signed(type_left)) {
1438 res = new_d_Shrs(dbgi, left, right, mode);
1440 res = new_d_Shr(dbgi, left, right, mode);
1445 panic("create shift op called for non-shift op");
1452 static ir_node *create_divmod(const binary_expression_t *expression)
1454 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1455 ir_node *left = expression_to_firm(expression->left);
1456 ir_node *right = expression_to_firm(expression->right);
1457 ir_node *pin = new_Pin(new_NoMem());
1458 /* be careful with the modes, because in arithmetic assign nodes only
1459 * the right operand has the mode of the arithmetic already */
1460 type_t *type = expression->right->base.datatype;
1461 ir_mode *mode = get_ir_mode(type);
1462 left = create_conv(dbgi, left, mode);
1466 switch (expression->expression.type) {
1467 case EXPR_BINARY_DIV:
1468 case EXPR_BINARY_DIV_ASSIGN:
1469 if(mode_is_float(mode)) {
1470 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1471 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1473 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1474 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1478 case EXPR_BINARY_MOD:
1479 case EXPR_BINARY_MOD_ASSIGN:
1480 assert(!mode_is_float(mode));
1481 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1482 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1485 default: panic("unexpected binary expression type in create_divmod()");
1491 static ir_node *create_arithmetic_assign_divmod(
1492 const binary_expression_t *expression)
1494 ir_node * value = create_divmod(expression);
1495 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1496 type_t *const type = expression->expression.datatype;
1497 ir_mode *const mode = get_ir_mode(type);
1499 assert(type->type != TYPE_POINTER);
1501 value = create_conv(dbgi, value, mode);
1502 set_value_for_expression(expression->left, value);
1507 static ir_node *create_arithmetic_assign_shift(
1508 const binary_expression_t *expression)
1510 ir_node * value = create_shift(expression);
1511 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1512 type_t *const type = expression->expression.datatype;
1513 ir_mode *const mode = get_ir_mode(type);
1515 value = create_conv(dbgi, value, mode);
1516 set_value_for_expression(expression->left, value);
1521 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1523 expression_type_t type = expression->expression.type;
1526 case EXPR_BINARY_EQUAL:
1527 case EXPR_BINARY_NOTEQUAL:
1528 case EXPR_BINARY_LESS:
1529 case EXPR_BINARY_LESSEQUAL:
1530 case EXPR_BINARY_GREATER:
1531 case EXPR_BINARY_GREATEREQUAL:
1532 case EXPR_BINARY_ISGREATER:
1533 case EXPR_BINARY_ISGREATEREQUAL:
1534 case EXPR_BINARY_ISLESS:
1535 case EXPR_BINARY_ISLESSEQUAL:
1536 case EXPR_BINARY_ISLESSGREATER:
1537 case EXPR_BINARY_ISUNORDERED: {
1538 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1539 ir_node *left = expression_to_firm(expression->left);
1540 ir_node *right = expression_to_firm(expression->right);
1541 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1542 long pnc = get_pnc(type);
1543 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1546 case EXPR_BINARY_ASSIGN: {
1547 ir_node *right = expression_to_firm(expression->right);
1548 set_value_for_expression(expression->left, right);
1552 case EXPR_BINARY_ADD:
1553 return create_add(expression);
1554 case EXPR_BINARY_SUB:
1555 return create_sub(expression);
1556 case EXPR_BINARY_MUL:
1557 return create_arithmetic_binop(expression, new_d_Mul);
1558 case EXPR_BINARY_BITWISE_AND:
1559 return create_arithmetic_binop(expression, new_d_And);
1560 case EXPR_BINARY_BITWISE_OR:
1561 return create_arithmetic_binop(expression, new_d_Or);
1562 case EXPR_BINARY_BITWISE_XOR:
1563 return create_arithmetic_binop(expression, new_d_Eor);
1564 case EXPR_BINARY_SHIFTLEFT:
1565 case EXPR_BINARY_SHIFTRIGHT:
1566 return create_shift(expression);
1567 case EXPR_BINARY_DIV:
1568 case EXPR_BINARY_MOD:
1569 return create_divmod(expression);
1570 case EXPR_BINARY_LOGICAL_AND:
1571 case EXPR_BINARY_LOGICAL_OR:
1572 return create_lazy_op(expression);
1573 case EXPR_BINARY_COMMA:
1574 expression_to_firm(expression->left);
1575 return expression_to_firm(expression->right);
1576 case EXPR_BINARY_ADD_ASSIGN:
1577 return create_arithmetic_assign_binop(expression, new_d_Add);
1578 case EXPR_BINARY_SUB_ASSIGN:
1579 return create_arithmetic_assign_binop(expression, new_d_Sub);
1580 case EXPR_BINARY_MUL_ASSIGN:
1581 return create_arithmetic_assign_binop(expression, new_d_Mul);
1582 case EXPR_BINARY_DIV_ASSIGN:
1583 return create_arithmetic_assign_divmod(expression);
1584 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1585 return create_arithmetic_assign_binop(expression, new_d_And);
1586 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1587 return create_arithmetic_assign_binop(expression, new_d_Or);
1588 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1589 return create_arithmetic_assign_binop(expression, new_d_Eor);
1590 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1591 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1592 return create_arithmetic_assign_shift(expression);
1594 panic("TODO binexpr type");
1598 static ir_node *array_access_addr(const array_access_expression_t *expression)
1600 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1601 ir_node *base_addr = expression_to_firm(expression->array_ref);
1602 ir_node *offset = expression_to_firm(expression->index);
1603 offset = create_conv(dbgi, offset, mode_uint);
1605 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1606 assert(is_type_pointer(ref_type));
1607 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1609 unsigned elem_size = get_type_size(pointer_type->points_to);
1610 ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
1611 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1613 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
1618 static ir_node *array_access_to_firm(
1619 const array_access_expression_t *expression)
1621 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1622 ir_node *addr = array_access_addr(expression);
1623 type_t *type = revert_automatic_type_conversion(
1624 (const expression_t*) expression);
1625 type = skip_typeref(type);
1626 ir_type *irtype = get_ir_type(type);
1628 return deref_address(irtype, addr, dbgi);
1631 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1633 type_t *type = expression->type;
1635 type = expression->size_expression->base.datatype;
1636 assert(type != NULL);
1639 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1640 unsigned size = get_type_size(type);
1641 ir_node *size_node = new_Const_long(mode, size);
1646 static tarval *try_fold_constant(const expression_t *expression)
1648 ir_graph *old_current_ir_graph = current_ir_graph;
1649 if(current_ir_graph == NULL) {
1650 current_ir_graph = get_const_code_irg();
1653 ir_node *cnst = expression_to_firm(expression);
1654 current_ir_graph = old_current_ir_graph;
1656 if(!is_Const(cnst)) {
1660 tarval *tv = get_Const_tarval(cnst);
1661 if(!tarval_is_long(tv)) {
1668 static long fold_constant(const expression_t *expression)
1670 tarval *tv = try_fold_constant(expression);
1672 panic("couldn't fold constantl");
1675 return get_tarval_long(tv);
1678 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1680 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1682 /* first try to fold a constant condition */
1683 tarval *tv = try_fold_constant(expression->condition);
1685 long val = get_tarval_long(tv);
1687 return expression_to_firm(expression->true_expression);
1689 return expression_to_firm(expression->false_expression);
1693 ir_node *cur_block = get_cur_block();
1695 /* create the true block */
1696 ir_node *true_block = new_immBlock();
1698 ir_node *true_val = expression_to_firm(expression->true_expression);
1699 ir_node *true_jmp = new_Jmp();
1701 /* create the false block */
1702 ir_node *false_block = new_immBlock();
1704 ir_node *false_val = expression_to_firm(expression->false_expression);
1705 ir_node *false_jmp = new_Jmp();
1707 /* create the condition evaluation */
1708 set_cur_block(cur_block);
1709 create_condition_evaluation(expression->condition, true_block, false_block);
1710 mature_immBlock(true_block);
1711 mature_immBlock(false_block);
1713 /* create the common block */
1714 ir_node *common_block = new_immBlock();
1715 add_immBlock_pred(common_block, true_jmp);
1716 add_immBlock_pred(common_block, false_jmp);
1717 mature_immBlock(common_block);
1719 /* TODO improve static semantics, so either both or no values are NULL */
1720 if (true_val == NULL || false_val == NULL)
1723 ir_node *in[2] = { true_val, false_val };
1724 ir_mode *mode = get_irn_mode(true_val);
1725 assert(get_irn_mode(false_val) == mode);
1726 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1731 static ir_node *select_addr(const select_expression_t *expression)
1733 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1735 ir_node *compound_addr = expression_to_firm(expression->compound);
1737 declaration_t *entry = expression->compound_entry;
1738 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1739 ir_entity *entity = entry->v.entity;
1741 assert(entity != NULL);
1743 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1748 static ir_node *select_to_firm(const select_expression_t *expression)
1750 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1751 ir_node *addr = select_addr(expression);
1752 type_t *type = revert_automatic_type_conversion(
1753 (const expression_t*) expression);
1754 type = skip_typeref(type);
1755 ir_type *irtype = get_ir_type(type);
1757 return deref_address(irtype, addr, dbgi);
1760 /* Values returned by __builtin_classify_type. */
1761 typedef enum gcc_type_class
1767 enumeral_type_class,
1770 reference_type_class,
1774 function_type_class,
1785 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1787 const type_t *const type = expr->type_expression->base.datatype;
1793 const atomic_type_t *const atomic_type = &type->atomic;
1794 switch (atomic_type->atype) {
1795 /* should not be reached */
1796 case ATOMIC_TYPE_INVALID:
1800 /* gcc cannot do that */
1801 case ATOMIC_TYPE_VOID:
1802 tc = void_type_class;
1805 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
1806 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
1807 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
1808 case ATOMIC_TYPE_SHORT:
1809 case ATOMIC_TYPE_USHORT:
1810 case ATOMIC_TYPE_INT:
1811 case ATOMIC_TYPE_UINT:
1812 case ATOMIC_TYPE_LONG:
1813 case ATOMIC_TYPE_ULONG:
1814 case ATOMIC_TYPE_LONGLONG:
1815 case ATOMIC_TYPE_ULONGLONG:
1816 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
1817 tc = integer_type_class;
1820 case ATOMIC_TYPE_FLOAT:
1821 case ATOMIC_TYPE_DOUBLE:
1822 case ATOMIC_TYPE_LONG_DOUBLE:
1823 tc = real_type_class;
1826 #ifdef PROVIDE_COMPLEX
1827 case ATOMIC_TYPE_FLOAT_COMPLEX:
1828 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1829 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1830 tc = complex_type_class;
1832 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1833 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1834 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1835 tc = complex_type_class;
1840 panic("Unimplemented case in classify_type_to_firm().");
1845 case TYPE_ARRAY: /* gcc handles this as pointer */
1846 case TYPE_FUNCTION: /* gcc handles this as pointer */
1847 case TYPE_POINTER: tc = pointer_type_class; break;
1848 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1849 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1851 /* gcc handles this as integer */
1852 case TYPE_ENUM: tc = integer_type_class; break;
1855 panic("Unimplemented case in classify_type_to_firm().");
1858 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1859 ir_mode *const mode = mode_int;
1860 tarval *const tv = new_tarval_from_long(tc, mode);
1861 return new_d_Const(dbgi, mode, tv);
1864 static ir_node *function_name_to_firm(
1865 const string_literal_expression_t *const expr)
1867 if (current_function_name == NULL) {
1868 const source_position_t *const src_pos =
1869 &expr->expression.source_position;
1870 const char *const name = current_function_decl->symbol->string;
1871 current_function_name = string_to_firm(src_pos, "__func__", name);
1874 return current_function_name;
1877 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1879 statement_t *statement = expr->statement;
1881 assert(statement->type == STATEMENT_COMPOUND);
1882 return compound_statement_to_firm((compound_statement_t*) statement);
1885 static ir_node *va_start_expression_to_firm(
1886 const va_start_expression_t *const expr)
1888 ir_type *const method_type = get_ir_type(current_function_decl->type);
1889 int const n = get_method_n_params(method_type) - 1;
1890 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
1891 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
1892 dbg_info *const dbgi =
1893 get_dbg_info(&expr->expression.source_position);
1894 ir_node *const no_mem = new_NoMem();
1895 ir_node *const arg_sel =
1896 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
1898 size_t const parm_size = get_type_size(expr->parameter->type);
1899 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
1900 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
1901 set_value_for_expression(expr->ap, add);
1906 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
1908 ir_type *const irtype = get_ir_type(expr->expression.datatype);
1909 ir_node *const ap = expression_to_firm(expr->ap);
1910 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1911 ir_node *const res = deref_address(irtype, ap, dbgi);
1913 size_t const parm_size = get_type_size(expr->expression.datatype);
1914 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
1915 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
1916 set_value_for_expression(expr->ap, add);
1921 static ir_node *dereference_addr(const unary_expression_t *const expression)
1923 assert(expression->expression.type == EXPR_UNARY_DEREFERENCE);
1924 return expression_to_firm(expression->value);
1927 static ir_node *expression_to_addr(const expression_t *expression)
1929 switch(expression->type) {
1930 case EXPR_REFERENCE:
1931 return reference_addr(&expression->reference);
1932 case EXPR_ARRAY_ACCESS:
1933 return array_access_addr(&expression->array_access);
1935 return select_addr(&expression->select);
1937 return call_expression_to_firm(&expression->call);
1938 case EXPR_UNARY_DEREFERENCE: {
1939 return dereference_addr(&expression->unary);
1944 panic("trying to get address of non-lvalue");
1947 static ir_node *_expression_to_firm(const expression_t *expression)
1949 switch(expression->type) {
1951 return const_to_firm(&expression->conste);
1952 case EXPR_STRING_LITERAL:
1953 return string_literal_to_firm(&expression->string);
1954 case EXPR_WIDE_STRING_LITERAL:
1955 return wide_string_literal_to_firm(&expression->wide_string);
1956 case EXPR_REFERENCE:
1957 return reference_expression_to_firm(&expression->reference);
1959 return call_expression_to_firm(&expression->call);
1961 return unary_expression_to_firm(&expression->unary);
1963 return binary_expression_to_firm(&expression->binary);
1964 case EXPR_ARRAY_ACCESS:
1965 return array_access_to_firm(&expression->array_access);
1967 return sizeof_to_firm(&expression->sizeofe);
1968 case EXPR_CONDITIONAL:
1969 return conditional_to_firm(&expression->conditional);
1971 return select_to_firm(&expression->select);
1972 case EXPR_CLASSIFY_TYPE:
1973 return classify_type_to_firm(&expression->classify_type);
1975 case EXPR_PRETTY_FUNCTION:
1976 return function_name_to_firm(&expression->string);
1977 case EXPR_STATEMENT:
1978 return statement_expression_to_firm(&expression->statement);
1980 return va_start_expression_to_firm(&expression->va_starte);
1982 return va_arg_expression_to_firm(&expression->va_arge);
1984 case EXPR_BUILTIN_SYMBOL:
1985 panic("unimplemented expression found");
1991 panic("invalid expression found");
1994 static ir_node *expression_to_firm(const expression_t *expression)
1996 ir_node *res = _expression_to_firm(expression);
1998 if(res != NULL && get_irn_mode(res) == mode_b) {
1999 ir_mode *mode = get_ir_mode(expression->base.datatype);
2000 res = create_conv(NULL, res, mode);
2006 static ir_node *expression_to_modeb(const expression_t *expression)
2008 ir_node *res = _expression_to_firm(expression);
2009 res = create_conv(NULL, res, mode_b);
2015 * create a short-circuit expression evaluation that tries to construct
2016 * efficient control flow structures for &&, || and ! expressions
2018 static void create_condition_evaluation(const expression_t *expression,
2019 ir_node *true_block,
2020 ir_node *false_block)
2022 switch(expression->type) {
2023 case EXPR_UNARY_NOT: {
2024 const unary_expression_t *unary_expression = &expression->unary;
2025 create_condition_evaluation(unary_expression->value, false_block,
2029 case EXPR_BINARY_LOGICAL_AND: {
2030 const binary_expression_t *binary_expression = &expression->binary;
2032 ir_node *cur_block = get_cur_block();
2033 ir_node *extra_block = new_immBlock();
2034 set_cur_block(cur_block);
2035 create_condition_evaluation(binary_expression->left, extra_block,
2037 mature_immBlock(extra_block);
2038 set_cur_block(extra_block);
2039 create_condition_evaluation(binary_expression->right, true_block,
2043 case EXPR_BINARY_LOGICAL_OR: {
2044 const binary_expression_t *binary_expression = &expression->binary;
2046 ir_node *cur_block = get_cur_block();
2047 ir_node *extra_block = new_immBlock();
2048 set_cur_block(cur_block);
2049 create_condition_evaluation(binary_expression->left, true_block,
2051 mature_immBlock(extra_block);
2052 set_cur_block(extra_block);
2053 create_condition_evaluation(binary_expression->right, true_block,
2061 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2062 ir_node *condition = expression_to_modeb(expression);
2063 ir_node *cond = new_d_Cond(dbgi, condition);
2064 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2065 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2067 add_immBlock_pred(true_block, true_proj);
2068 add_immBlock_pred(false_block, false_proj);
2070 set_cur_block(NULL);
2075 static void create_declaration_entity(declaration_t *declaration,
2076 declaration_type_t declaration_type,
2077 ir_type *parent_type)
2079 ident *const id = new_id_from_str(declaration->symbol->string);
2080 ir_type *const irtype = get_ir_type(declaration->type);
2081 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2082 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2083 set_entity_ld_ident(entity, id);
2085 declaration->declaration_type = (unsigned char) declaration_type;
2086 declaration->v.entity = entity;
2087 set_entity_variability(entity, variability_uninitialized);
2088 if(parent_type == get_tls_type())
2089 set_entity_allocation(entity, allocation_automatic);
2090 else if(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE)
2091 set_entity_allocation(entity, allocation_static);
2092 /* TODO: visibility? */
2095 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2097 enum compound_graph_entry_type_t {
2098 COMPOUND_GRAPH_ENTRY_ARRAY,
2099 COMPOUND_GRAPH_ENTRY_COMPOUND
2102 struct compound_graph_path_entry_t {
2108 compound_graph_path_entry_t *prev;
2111 static void create_initializer_object(initializer_t *initializer, type_t *type,
2112 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2114 static compound_graph_path *create_compound_path(ir_type *type,
2115 compound_graph_path_entry_t *entry, int len)
2117 compound_graph_path *path = new_compound_graph_path(type, len);
2120 for( ; entry != NULL; entry = entry->prev, --i) {
2122 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2123 set_compound_graph_path_node(path, i, entry->v.entity);
2125 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2126 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2134 static void create_initializer_value(initializer_value_t *initializer,
2136 compound_graph_path_entry_t *entry,
2139 ir_node *node = expression_to_firm(initializer->value);
2140 ir_type *type = get_entity_type(entity);
2141 compound_graph_path *path = create_compound_path(type, entry, len);
2142 add_compound_ent_value_w_path(entity, node, path);
2145 static void create_initializer_compound(initializer_list_t *initializer,
2146 compound_type_t *type,
2148 compound_graph_path_entry_t *last_entry,
2151 declaration_t *compound_declaration = type->declaration;
2153 declaration_t *compound_entry = compound_declaration->context.declarations;
2155 compound_graph_path_entry_t entry;
2156 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2157 entry.prev = last_entry;
2161 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2162 if(compound_entry->symbol == NULL)
2164 if(compound_entry->namespc != NAMESPACE_NORMAL)
2167 if(i >= initializer->len)
2170 entry.v.entity = compound_entry->v.entity;
2172 initializer_t *sub_initializer = initializer->initializers[i];
2174 assert(compound_entry != NULL);
2175 assert(compound_entry->declaration_type
2176 == DECLARATION_TYPE_COMPOUND_MEMBER);
2178 if(sub_initializer->type == INITIALIZER_VALUE) {
2179 create_initializer_value(&sub_initializer->value,
2180 entity, &entry, len);
2182 type_t *entry_type = skip_typeref(compound_entry->type);
2183 create_initializer_object(sub_initializer, entry_type, entity,
2191 static void create_initializer_array(initializer_list_t *initializer,
2192 array_type_t *type, ir_entity *entity,
2193 compound_graph_path_entry_t *last_entry,
2196 type_t *element_type = type->element_type;
2197 element_type = skip_typeref(element_type);
2199 compound_graph_path_entry_t entry;
2200 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2201 entry.prev = last_entry;
2205 for(i = 0; i < initializer->len; ++i) {
2206 entry.v.array_index = i;
2208 initializer_t *sub_initializer = initializer->initializers[i];
2210 if(sub_initializer->type == INITIALIZER_VALUE) {
2211 create_initializer_value(&sub_initializer->value,
2212 entity, &entry, len);
2214 create_initializer_object(sub_initializer, element_type, entity,
2220 /* TODO: initialize rest... */
2221 if(type->size_expression != NULL) {
2222 size_t array_len = fold_constant(type->size_expression);
2223 for( ; i < array_len; ++i) {
2230 static void create_initializer_string(initializer_string_t *initializer,
2231 array_type_t *type, ir_entity *entity,
2232 compound_graph_path_entry_t *last_entry,
2235 type_t *element_type = type->element_type;
2236 element_type = skip_typeref(element_type);
2238 compound_graph_path_entry_t entry;
2239 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2240 entry.prev = last_entry;
2243 ir_type *irtype = get_entity_type(entity);
2244 size_t arr_len = get_array_type_size(type);
2245 const char *p = initializer->string;
2247 for(i = 0; i < arr_len; ++i, ++p) {
2248 entry.v.array_index = i;
2250 ir_node *node = new_Const_long(mode_Bs, *p);
2251 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2252 add_compound_ent_value_w_path(entity, node, path);
2259 static void create_initializer_wide_string(
2260 const initializer_wide_string_t *const initializer, array_type_t *const type,
2261 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2264 type_t *element_type = type->element_type;
2265 element_type = skip_typeref(element_type);
2267 compound_graph_path_entry_t entry;
2268 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2269 entry.prev = last_entry;
2272 ir_type *const irtype = get_entity_type(entity);
2273 const size_t arr_len = get_array_type_size(type);
2274 const wchar_rep_t * p = initializer->string.begin;
2275 const wchar_rep_t *const end = p + initializer->string.size;
2276 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2277 entry.v.array_index = i;
2279 ir_node *node = new_Const_long(mode_int, *p);
2280 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2281 add_compound_ent_value_w_path(entity, node, path);
2285 static void create_initializer_object(initializer_t *initializer, type_t *type,
2286 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2288 if(is_type_array(type)) {
2289 array_type_t *array_type = &type->array;
2291 switch (initializer->type) {
2292 case INITIALIZER_STRING: {
2293 initializer_string_t *const string = &initializer->string;
2294 create_initializer_string(string, array_type, entity, entry, len);
2298 case INITIALIZER_WIDE_STRING: {
2299 initializer_wide_string_t *const string = &initializer->wide_string;
2300 create_initializer_wide_string(string, array_type, entity, entry, len);
2304 case INITIALIZER_LIST: {
2305 initializer_list_t *const list = &initializer->list;
2306 create_initializer_array(list, array_type, entity, entry, len);
2310 case INITIALIZER_VALUE:
2313 panic("Unhandled initializer");
2315 assert(initializer->type == INITIALIZER_LIST);
2316 initializer_list_t *list = &initializer->list;
2318 assert(is_type_compound(type));
2319 compound_type_t *compound_type = &type->compound;
2320 create_initializer_compound(list, compound_type, entity, entry, len);
2324 static void create_initializer_local_variable_entity(declaration_t *declaration)
2326 initializer_t *initializer = declaration->init.initializer;
2327 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2328 ir_entity *entity = declaration->v.entity;
2329 ir_node *memory = get_store();
2330 ir_node *nomem = new_NoMem();
2331 ir_node *frame = get_irg_frame(current_ir_graph);
2332 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2334 if(initializer->type == INITIALIZER_VALUE) {
2335 initializer_value_t *initializer_value = &initializer->value;
2337 ir_node *value = expression_to_firm(initializer_value->value);
2338 type_t *type = skip_typeref(declaration->type);
2339 assign_value(dbgi, addr, type, value);
2343 /* create a "template" entity which is copied to the entity on the stack */
2344 ident *const id = unique_ident("initializer");
2345 ir_type *const irtype = get_ir_type(declaration->type);
2346 ir_type *const global_type = get_glob_type();
2347 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
2348 set_entity_ld_ident(init_entity, id);
2350 set_entity_variability(init_entity, variability_initialized);
2351 set_entity_visibility(init_entity, visibility_local);
2352 set_entity_allocation(init_entity, allocation_static);
2354 ir_graph *const old_current_ir_graph = current_ir_graph;
2355 current_ir_graph = get_const_code_irg();
2357 type_t *const type = skip_typeref(declaration->type);
2358 create_initializer_object(initializer, type, init_entity, NULL, 0);
2360 assert(current_ir_graph == get_const_code_irg());
2361 current_ir_graph = old_current_ir_graph;
2363 ir_node *const src_addr = create_symconst(dbgi, init_entity);
2364 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2366 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2367 set_store(copyb_mem);
2370 static void create_initializer(declaration_t *declaration)
2372 initializer_t *initializer = declaration->init.initializer;
2373 if(initializer == NULL)
2376 declaration_type_t declaration_type
2377 = (declaration_type_t) declaration->declaration_type;
2378 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2379 create_initializer_local_variable_entity(declaration);
2383 if(initializer->type == INITIALIZER_VALUE) {
2384 initializer_value_t *initializer_value = &initializer->value;
2386 ir_node *value = expression_to_firm(initializer_value->value);
2388 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2389 set_value(declaration->v.value_number, value);
2391 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2393 ir_entity *entity = declaration->v.entity;
2395 set_entity_variability(entity, variability_initialized);
2396 set_atomic_ent_value(entity, value);
2399 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2400 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2402 ir_entity *entity = declaration->v.entity;
2403 set_entity_variability(entity, variability_initialized);
2405 type_t *type = skip_typeref(declaration->type);
2406 create_initializer_object(initializer, type, entity, NULL, 0);
2410 static void create_local_variable(declaration_t *declaration)
2412 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2414 bool needs_entity = declaration->address_taken;
2415 type_t *type = skip_typeref(declaration->type);
2417 if(is_type_array(type) || is_type_compound(type)) {
2418 needs_entity = true;
2422 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2423 create_declaration_entity(declaration,
2424 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2427 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2428 declaration->v.value_number = next_value_number_function;
2429 ++next_value_number_function;
2432 create_initializer(declaration);
2435 static void create_local_static_variable(declaration_t *declaration)
2437 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2439 type_t *const type = skip_typeref(declaration->type);
2440 ir_type *const global_type = get_glob_type();
2441 ident *const id = unique_ident(declaration->symbol->string);
2442 ir_type *const irtype = get_ir_type(type);
2443 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2444 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
2445 set_entity_ld_ident(entity, id);
2447 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2448 declaration->v.entity = entity;
2449 set_entity_variability(entity, variability_uninitialized);
2450 set_entity_visibility(entity, visibility_local);
2451 set_entity_allocation(entity, allocation_static);
2453 ir_graph *const old_current_ir_graph = current_ir_graph;
2454 current_ir_graph = get_const_code_irg();
2456 create_initializer(declaration);
2458 assert(current_ir_graph == get_const_code_irg());
2459 current_ir_graph = old_current_ir_graph;
2464 static void return_statement_to_firm(return_statement_t *statement)
2466 if(get_cur_block() == NULL)
2469 ir_type *func_irtype = get_ir_type(current_function_decl->type);
2471 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2475 if(get_method_n_ress(func_irtype) > 0) {
2476 ir_type *res_type = get_method_res_type(func_irtype, 0);
2478 if(statement->return_value != NULL) {
2479 ir_node *node = expression_to_firm(statement->return_value);
2480 node = do_strict_conv(dbgi, node);
2484 if(is_compound_type(res_type)) {
2487 mode = get_type_mode(res_type);
2489 in[0] = new_Unknown(mode);
2493 /* build return_value for its side effects */
2494 if(statement->return_value != NULL) {
2495 expression_to_firm(statement->return_value);
2500 ir_node *store = get_store();
2501 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
2503 ir_node *end_block = get_irg_end_block(current_ir_graph);
2504 add_immBlock_pred(end_block, ret);
2506 set_cur_block(NULL);
2509 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
2511 if(get_cur_block() == NULL)
2514 return expression_to_firm(statement->expression);
2517 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
2519 ir_node *result = NULL;
2520 statement_t *statement = compound->statements;
2521 for( ; statement != NULL; statement = statement->base.next) {
2522 //context2firm(&statement->context);
2524 if(statement->base.next == NULL
2525 && statement->type == STATEMENT_EXPRESSION) {
2526 result = expression_statement_to_firm(
2527 (expression_statement_t*) statement);
2530 statement_to_firm(statement);
2536 static void create_local_declaration(declaration_t *declaration)
2538 type_t *type = skip_typeref(declaration->type);
2540 switch ((storage_class_tag_t) declaration->storage_class) {
2541 case STORAGE_CLASS_STATIC:
2542 create_local_static_variable(declaration);
2544 case STORAGE_CLASS_ENUM_ENTRY:
2545 panic("enum entry declaration in local block found");
2546 case STORAGE_CLASS_EXTERN:
2547 panic("extern declaration in local block found");
2548 case STORAGE_CLASS_NONE:
2549 case STORAGE_CLASS_AUTO:
2550 case STORAGE_CLASS_REGISTER:
2551 if(is_type_function(type)) {
2552 panic("nested functions not supported yet");
2554 create_local_variable(declaration);
2557 case STORAGE_CLASS_TYPEDEF:
2558 case STORAGE_CLASS_THREAD:
2559 case STORAGE_CLASS_THREAD_EXTERN:
2560 case STORAGE_CLASS_THREAD_STATIC:
2563 panic("invalid storage class found");
2566 static void declaration_statement_to_firm(declaration_statement_t *statement)
2568 declaration_t *declaration = statement->declarations_begin;
2569 declaration_t *end = statement->declarations_end->next;
2570 for( ; declaration != end; declaration = declaration->next) {
2571 create_local_variable(declaration);
2575 static void if_statement_to_firm(if_statement_t *statement)
2577 ir_node *cur_block = get_cur_block();
2579 ir_node *fallthrough_block = new_immBlock();
2581 /* the true (blocks) */
2582 ir_node *true_block;
2583 if (statement->true_statement != NULL) {
2584 true_block = new_immBlock();
2585 statement_to_firm(statement->true_statement);
2586 if(get_cur_block() != NULL) {
2587 ir_node *jmp = new_Jmp();
2588 add_immBlock_pred(fallthrough_block, jmp);
2591 true_block = fallthrough_block;
2594 /* the false (blocks) */
2595 ir_node *false_block;
2596 if(statement->false_statement != NULL) {
2597 false_block = new_immBlock();
2599 statement_to_firm(statement->false_statement);
2600 if(get_cur_block() != NULL) {
2601 ir_node *jmp = new_Jmp();
2602 add_immBlock_pred(fallthrough_block, jmp);
2605 false_block = fallthrough_block;
2608 /* create the condition */
2609 if(cur_block != NULL) {
2610 set_cur_block(cur_block);
2611 create_condition_evaluation(statement->condition, true_block,
2615 mature_immBlock(true_block);
2616 if(false_block != fallthrough_block) {
2617 mature_immBlock(false_block);
2619 mature_immBlock(fallthrough_block);
2621 set_cur_block(fallthrough_block);
2624 static void while_statement_to_firm(while_statement_t *statement)
2626 ir_node *jmp = NULL;
2627 if(get_cur_block() != NULL) {
2631 /* create the header block */
2632 ir_node *header_block = new_immBlock();
2634 add_immBlock_pred(header_block, jmp);
2637 /* the false block */
2638 ir_node *false_block = new_immBlock();
2641 ir_node *body_block;
2642 if (statement->body != NULL) {
2643 ir_node *old_continue_label = continue_label;
2644 ir_node *old_break_label = break_label;
2645 continue_label = header_block;
2646 break_label = false_block;
2648 body_block = new_immBlock();
2649 statement_to_firm(statement->body);
2651 assert(continue_label == header_block);
2652 assert(break_label == false_block);
2653 continue_label = old_continue_label;
2654 break_label = old_break_label;
2656 if(get_cur_block() != NULL) {
2658 add_immBlock_pred(header_block, jmp);
2661 body_block = header_block;
2664 /* create the condition */
2665 set_cur_block(header_block);
2667 create_condition_evaluation(statement->condition, body_block, false_block);
2668 mature_immBlock(body_block);
2669 mature_immBlock(false_block);
2670 mature_immBlock(header_block);
2672 set_cur_block(false_block);
2675 static void do_while_statement_to_firm(do_while_statement_t *statement)
2677 ir_node *jmp = NULL;
2678 if(get_cur_block() != NULL) {
2682 /* create the header block */
2683 ir_node *header_block = new_immBlock();
2685 /* the false block */
2686 ir_node *false_block = new_immBlock();
2689 ir_node *body_block = new_immBlock();
2691 add_immBlock_pred(body_block, jmp);
2694 if (statement->body != NULL) {
2695 ir_node *old_continue_label = continue_label;
2696 ir_node *old_break_label = break_label;
2697 continue_label = header_block;
2698 break_label = false_block;
2700 statement_to_firm(statement->body);
2702 assert(continue_label == header_block);
2703 assert(break_label == false_block);
2704 continue_label = old_continue_label;
2705 break_label = old_break_label;
2707 if (get_cur_block() == NULL) {
2708 mature_immBlock(header_block);
2709 mature_immBlock(body_block);
2710 mature_immBlock(false_block);
2715 ir_node *body_jmp = new_Jmp();
2716 add_immBlock_pred(header_block, body_jmp);
2717 mature_immBlock(header_block);
2719 /* create the condition */
2720 set_cur_block(header_block);
2722 create_condition_evaluation(statement->condition, body_block, false_block);
2723 mature_immBlock(body_block);
2724 mature_immBlock(false_block);
2725 mature_immBlock(header_block);
2727 set_cur_block(false_block);
2730 static void for_statement_to_firm(for_statement_t *statement)
2732 ir_node *jmp = NULL;
2733 if (get_cur_block() != NULL) {
2734 if(statement->initialisation != NULL) {
2735 expression_to_firm(statement->initialisation);
2738 /* create declarations */
2739 declaration_t *declaration = statement->context.declarations;
2740 for( ; declaration != NULL; declaration = declaration->next) {
2741 create_local_declaration(declaration);
2748 /* create the step block */
2749 ir_node *const step_block = new_immBlock();
2750 if (statement->step != NULL) {
2751 expression_to_firm(statement->step);
2753 ir_node *const step_jmp = new_Jmp();
2755 /* create the header block */
2756 ir_node *const header_block = new_immBlock();
2758 add_immBlock_pred(header_block, jmp);
2760 add_immBlock_pred(header_block, step_jmp);
2762 /* the false block */
2763 ir_node *const false_block = new_immBlock();
2766 ir_node * body_block;
2767 if (statement->body != NULL) {
2768 ir_node *const old_continue_label = continue_label;
2769 ir_node *const old_break_label = break_label;
2770 continue_label = step_block;
2771 break_label = false_block;
2773 body_block = new_immBlock();
2774 statement_to_firm(statement->body);
2776 assert(continue_label == step_block);
2777 assert(break_label == false_block);
2778 continue_label = old_continue_label;
2779 break_label = old_break_label;
2781 if (get_cur_block() != NULL) {
2783 add_immBlock_pred(step_block, jmp);
2786 body_block = step_block;
2789 /* create the condition */
2790 set_cur_block(header_block);
2791 if (statement->condition != NULL) {
2792 create_condition_evaluation(statement->condition, body_block,
2795 keep_alive(header_block);
2797 add_immBlock_pred(body_block, jmp);
2800 mature_immBlock(body_block);
2801 mature_immBlock(false_block);
2802 mature_immBlock(step_block);
2803 mature_immBlock(header_block);
2804 mature_immBlock(false_block);
2806 set_cur_block(false_block);
2809 static void create_jump_statement(const statement_t *statement,
2810 ir_node *target_block)
2812 if(get_cur_block() == NULL)
2815 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2816 ir_node *jump = new_d_Jmp(dbgi);
2817 add_immBlock_pred(target_block, jump);
2819 set_cur_block(NULL);
2822 static void switch_statement_to_firm(const switch_statement_t *statement)
2824 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2826 ir_node *expression = expression_to_firm(statement->expression);
2827 ir_node *cond = new_d_Cond(dbgi, expression);
2828 ir_node *break_block = new_immBlock();
2830 set_cur_block(NULL);
2832 ir_node *const old_switch_cond = current_switch_cond;
2833 ir_node *const old_break_label = break_label;
2834 const bool old_saw_default_label = saw_default_label;
2835 current_switch_cond = cond;
2836 break_label = break_block;
2838 statement_to_firm(statement->body);
2840 if(get_cur_block() != NULL) {
2841 ir_node *jmp = new_Jmp();
2842 add_immBlock_pred(break_block, jmp);
2845 if (!saw_default_label) {
2846 set_cur_block(get_nodes_block(cond));
2847 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2848 MAGIC_DEFAULT_PN_NUMBER);
2849 add_immBlock_pred(break_block, proj);
2852 assert(current_switch_cond == cond);
2853 assert(break_label == break_block);
2854 current_switch_cond = old_switch_cond;
2855 break_label = old_break_label;
2856 saw_default_label = old_saw_default_label;
2858 mature_immBlock(break_block);
2859 set_cur_block(break_block);
2862 static void case_label_to_firm(const case_label_statement_t *statement)
2864 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2866 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2868 /* let's create a node and hope firm constant folding creates a Const
2871 set_cur_block(get_nodes_block(current_switch_cond));
2872 if(statement->expression) {
2873 long pn = fold_constant(statement->expression);
2874 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2875 /* oops someone detected our cheating... */
2876 panic("magic default pn used");
2878 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2880 saw_default_label = true;
2881 proj = new_d_defaultProj(dbgi, current_switch_cond,
2882 MAGIC_DEFAULT_PN_NUMBER);
2885 ir_node *block = new_immBlock();
2886 if (fallthrough != NULL) {
2887 add_immBlock_pred(block, fallthrough);
2889 add_immBlock_pred(block, proj);
2890 mature_immBlock(block);
2892 statement_to_firm(statement->label_statement);
2895 static ir_node *get_label_block(declaration_t *label)
2897 assert(label->namespc == NAMESPACE_LABEL);
2899 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2900 return label->v.block;
2902 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2904 ir_node *old_cur_block = get_cur_block();
2905 ir_node *block = new_immBlock();
2906 set_cur_block(old_cur_block);
2908 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2909 label->v.block = block;
2911 ARR_APP1(ir_node *, imature_blocks, block);
2916 static void label_to_firm(const label_statement_t *statement)
2918 ir_node *block = get_label_block(statement->label);
2920 if(get_cur_block() != NULL) {
2921 ir_node *jmp = new_Jmp();
2922 add_immBlock_pred(block, jmp);
2925 set_cur_block(block);
2928 statement_to_firm(statement->label_statement);
2931 static void goto_to_firm(const goto_statement_t *statement)
2933 if(get_cur_block() == NULL)
2936 ir_node *block = get_label_block(statement->label);
2937 ir_node *jmp = new_Jmp();
2938 add_immBlock_pred(block, jmp);
2940 set_cur_block(NULL);
2943 typedef enum modifier_t {
2944 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
2945 ASM_MODIFIER_READ_WRITE = 1 << 1,
2946 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
2947 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
2951 static void asm_statement_to_firm(const asm_statement_t *statement)
2953 bool needs_memory = false;
2955 size_t n_clobbers = 0;
2956 asm_clobber_t *clobber = statement->clobbers;
2957 for( ; clobber != NULL; clobber = clobber->next) {
2958 if(strcmp(clobber->clobber, "memory") == 0) {
2959 needs_memory = true;
2963 ident *id = new_id_from_str(clobber->clobber);
2964 obstack_ptr_grow(&asm_obst, id);
2967 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
2968 ident **clobbers = NULL;
2969 if(n_clobbers > 0) {
2970 clobbers = obstack_finish(&asm_obst);
2973 /* find and count input and output constraints */
2974 asm_constraint_t *constraint = statement->inputs;
2975 for( ; constraint != NULL; constraint = constraint->next) {
2977 bool supports_memop = false;
2978 for(const char *c = constraint->constraints; *c != 0; ++c) {
2979 /* TODO: improve error messages */
2983 panic("multiple alternative assembler constraints not "
2991 supports_memop = true;
2992 obstack_1grow(&asm_obst, *c);
2995 if(modifiers & ASM_MODIFIER_READ_WRITE)
2996 panic("inconsistent register constraints");
2997 modifiers |= ASM_MODIFIER_WRITE_ONLY;
3000 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
3001 panic("inconsistent register constraints");
3002 modifiers |= ASM_MODIFIER_READ_WRITE;
3005 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
3006 panic("early clobber assembler constraint not supported yet");
3009 modifiers |= ASM_MODIFIER_COMMUTATIVE;
3010 panic("commutative assembler constraint not supported yet");
3013 /* skip register preferences stuff... */
3014 while(*c != 0 && *c != ',')
3018 /* skip register preferences stuff... */
3022 obstack_1grow(&asm_obst, *c);
3026 obstack_1grow(&asm_obst, '\0');
3027 const char *constraint_string = obstack_finish(&asm_obst);
3029 needs_memory |= supports_memop;
3030 if(supports_memop) {
3038 static void statement_to_firm(statement_t *statement)
3040 switch(statement->type) {
3041 case STATEMENT_INVALID:
3042 panic("invalid statement found");
3043 case STATEMENT_COMPOUND:
3044 compound_statement_to_firm(&statement->compound);
3046 case STATEMENT_RETURN:
3047 return_statement_to_firm(&statement->returns);
3049 case STATEMENT_EXPRESSION:
3050 expression_statement_to_firm(&statement->expression);
3053 if_statement_to_firm(&statement->ifs);
3055 case STATEMENT_WHILE:
3056 while_statement_to_firm(&statement->whiles);
3058 case STATEMENT_DO_WHILE:
3059 do_while_statement_to_firm(&statement->do_while);
3061 case STATEMENT_DECLARATION:
3062 declaration_statement_to_firm(&statement->declaration);
3064 case STATEMENT_BREAK:
3065 create_jump_statement(statement, break_label);
3067 case STATEMENT_CONTINUE:
3068 create_jump_statement(statement, continue_label);
3070 case STATEMENT_SWITCH:
3071 switch_statement_to_firm(&statement->switchs);
3073 case STATEMENT_CASE_LABEL:
3074 case_label_to_firm(&statement->case_label);
3077 for_statement_to_firm(&statement->fors);
3079 case STATEMENT_LABEL:
3080 label_to_firm(&statement->label);
3082 case STATEMENT_GOTO:
3083 goto_to_firm(&statement->gotos);
3086 //asm_statement_to_firm(&statement->asms);
3090 panic("Statement not implemented\n");
3093 static int count_local_declarations(const declaration_t * decl,
3094 const declaration_t *const end)
3097 for (; decl != end; decl = decl->next) {
3098 const type_t *type = skip_typeref(decl->type);
3099 switch (type->type) {
3103 if (!decl->address_taken)
3113 static int count_decls_in_expr(const expression_t *expr) {
3114 if (expr->base.type == EXPR_STATEMENT)
3115 return count_decls_in_stmts(expr->statement.statement);
3119 static int count_decls_in_stmts(const statement_t *stmt)
3122 for (; stmt != NULL; stmt = stmt->base.next) {
3123 switch (stmt->type) {
3124 case STATEMENT_DECLARATION: {
3125 const declaration_statement_t *const decl_stmt = &stmt->declaration;
3126 count += count_local_declarations(decl_stmt->declarations_begin,
3127 decl_stmt->declarations_end->next);
3131 case STATEMENT_COMPOUND: {
3132 const compound_statement_t *const comp =
3133 (const compound_statement_t*)stmt;
3134 count += count_decls_in_stmts(comp->statements);
3138 case STATEMENT_IF: {
3139 const if_statement_t *const if_stmt = &stmt->ifs;
3140 count += count_decls_in_expr(if_stmt->condition);
3141 count += count_decls_in_stmts(if_stmt->true_statement);
3142 count += count_decls_in_stmts(if_stmt->false_statement);
3146 case STATEMENT_SWITCH: {
3147 const switch_statement_t *const switch_stmt = &stmt->switchs;
3148 count += count_decls_in_expr(switch_stmt->expression);
3149 count += count_decls_in_stmts(switch_stmt->body);
3153 case STATEMENT_LABEL: {
3154 const label_statement_t *const label_stmt = &stmt->label;
3155 count += count_decls_in_stmts(label_stmt->label_statement);
3159 case STATEMENT_WHILE: {
3160 const while_statement_t *const while_stmt = &stmt->whiles;
3161 count += count_decls_in_expr(while_stmt->condition);
3162 count += count_decls_in_stmts(while_stmt->body);
3166 case STATEMENT_DO_WHILE: {
3167 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
3168 count += count_decls_in_expr(do_while_stmt->condition);
3169 count += count_decls_in_stmts(do_while_stmt->body);
3173 case STATEMENT_FOR: {
3174 const for_statement_t *const for_stmt = &stmt->fors;
3175 count += count_local_declarations(for_stmt->context.declarations, NULL);
3176 count += count_decls_in_expr(for_stmt->initialisation);
3177 count += count_decls_in_expr(for_stmt->condition);
3178 count += count_decls_in_expr(for_stmt->step);
3179 count += count_decls_in_stmts(for_stmt->body);
3184 case STATEMENT_BREAK:
3185 case STATEMENT_CASE_LABEL:
3186 case STATEMENT_CONTINUE:
3189 case STATEMENT_EXPRESSION: {
3190 const expression_statement_t *expr_stmt = &stmt->expression;
3191 count += count_decls_in_expr(expr_stmt->expression);
3195 case STATEMENT_GOTO:
3196 case STATEMENT_INVALID:
3199 case STATEMENT_RETURN: {
3200 const return_statement_t *ret_stmt = &stmt->returns;
3201 count += count_decls_in_expr(ret_stmt->return_value);
3209 static int get_function_n_local_vars(declaration_t *declaration)
3213 /* count parameters */
3214 count += count_local_declarations(declaration->context.declarations, NULL);
3216 /* count local variables declared in body */
3217 count += count_decls_in_stmts(declaration->init.statement);
3222 static void initialize_function_parameters(declaration_t *declaration)
3224 ir_graph *irg = current_ir_graph;
3225 ir_node *args = get_irg_args(irg);
3226 ir_node *start_block = get_irg_start_block(irg);
3227 ir_type *function_irtype = get_ir_type(declaration->type);
3230 declaration_t *parameter = declaration->context.declarations;
3231 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3232 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
3233 type_t *type = skip_typeref(parameter->type);
3235 bool needs_entity = parameter->address_taken;
3236 assert(!is_type_array(type));
3237 if(is_type_compound(type)) {
3238 needs_entity = true;
3242 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3243 ident *id = new_id_from_str(parameter->symbol->string);
3244 set_entity_ident(entity, id);
3246 parameter->declaration_type
3247 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
3248 parameter->v.entity = entity;
3252 ir_mode *mode = get_ir_mode(parameter->type);
3254 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3256 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
3257 parameter->v.value_number = next_value_number_function;
3258 ++next_value_number_function;
3260 set_value(parameter->v.value_number, proj);
3265 * Handle additional decl modifiers for IR-graphs
3267 * @param irg the IR-graph
3268 * @param dec_modifiers additional modifiers
3270 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
3272 if (decl_modifiers & DM_NORETURN) {
3273 /* TRUE if the declaration includes the Microsoft
3274 __declspec(noreturn) specifier. */
3275 set_irg_additional_property(irg, mtp_property_noreturn);
3277 if (decl_modifiers & DM_NOTHROW) {
3278 /* TRUE if the declaration includes the Microsoft
3279 __declspec(nothrow) specifier. */
3280 set_irg_additional_property(irg, mtp_property_nothrow);
3282 if (decl_modifiers & DM_NAKED) {
3283 /* TRUE if the declaration includes the Microsoft
3284 __declspec(naked) specifier. */
3285 set_irg_additional_property(irg, mtp_property_naked);
3287 if (decl_modifiers & DM_FORCEINLINE) {
3288 /* TRUE if the declaration includes the
3289 Microsoft __forceinline specifier. */
3290 set_irg_inline_property(irg, irg_inline_forced);
3292 if (decl_modifiers & DM_NOINLINE) {
3293 /* TRUE if the declaration includes the Microsoft
3294 __declspec(noinline) specifier. */
3295 set_irg_inline_property(irg, irg_inline_forbidden);
3299 static void create_function(declaration_t *declaration)
3301 ir_entity *function_entity = get_function_entity(declaration);
3303 if(declaration->init.statement == NULL)
3306 current_function_decl = declaration;
3307 current_function_name = NULL;
3309 assert(imature_blocks == NULL);
3310 imature_blocks = NEW_ARR_F(ir_node*, 0);
3312 int n_local_vars = get_function_n_local_vars(declaration);
3313 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3314 ir_node *first_block = get_cur_block();
3316 /* set inline flags */
3317 if (declaration->is_inline)
3318 set_irg_inline_property(irg, irg_inline_recomended);
3319 handle_decl_modifier_irg(irg, declaration->decl_modifiers);
3321 next_value_number_function = 0;
3322 initialize_function_parameters(declaration);
3324 statement_to_firm(declaration->init.statement);
3326 ir_node *end_block = get_irg_end_block(irg);
3328 /* do we have a return statement yet? */
3329 if(get_cur_block() != NULL) {
3330 type_t *type = skip_typeref(declaration->type);
3331 assert(is_type_function(type));
3332 const function_type_t *func_type = &type->function;
3333 const type_t *return_type
3334 = skip_typeref(func_type->return_type);
3337 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3338 ret = new_Return(get_store(), 0, NULL);
3341 if(is_type_scalar(return_type)) {
3342 mode = get_ir_mode(func_type->return_type);
3348 /* ยง5.1.2.2.3 main implicitly returns 0 */
3349 if (strcmp(declaration->symbol->string, "main") == 0) {
3350 in[0] = new_Const(mode, get_mode_null(mode));
3352 in[0] = new_Unknown(mode);
3354 ret = new_Return(get_store(), 1, in);
3356 add_immBlock_pred(end_block, ret);
3359 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3360 mature_immBlock(imature_blocks[i]);
3362 DEL_ARR_F(imature_blocks);
3363 imature_blocks = NULL;
3365 mature_immBlock(first_block);
3366 mature_immBlock(end_block);
3368 irg_finalize_cons(irg);
3370 /* finalize the frame type */
3371 ir_type *frame_type = get_irg_frame_type(irg);
3372 int n = get_compound_n_members(frame_type);
3375 for(int i = 0; i < n; ++i) {
3376 ir_entity *entity = get_compound_member(frame_type, i);
3377 ir_type *entity_type = get_entity_type(entity);
3379 int align = get_type_alignment_bytes(entity_type);
3380 if(align > align_all)
3384 misalign = offset % align;
3386 offset += align - misalign;
3390 set_entity_offset(entity, offset);
3391 offset += get_type_size_bytes(entity_type);
3393 set_type_size_bytes(frame_type, offset);
3394 set_type_alignment_bytes(frame_type, align_all);
3395 set_type_state(frame_type, layout_fixed);
3400 static void create_global_variable(declaration_t *declaration)
3404 switch ((storage_class_tag_t)declaration->storage_class) {
3405 case STORAGE_CLASS_STATIC:
3406 vis = visibility_local;
3409 case STORAGE_CLASS_EXTERN:
3410 vis = visibility_external_allocated;
3413 case STORAGE_CLASS_NONE:
3414 vis = visibility_external_visible;
3417 case STORAGE_CLASS_THREAD:
3418 vis = visibility_external_visible;
3421 case STORAGE_CLASS_THREAD_EXTERN:
3422 vis = visibility_external_allocated;
3425 case STORAGE_CLASS_THREAD_STATIC:
3426 vis = visibility_local;
3430 var_type = get_tls_type();
3434 var_type = get_glob_type();
3438 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3440 set_entity_visibility(declaration->v.entity, vis);
3442 current_ir_graph = get_const_code_irg();
3443 create_initializer(declaration);
3446 case STORAGE_CLASS_TYPEDEF:
3447 case STORAGE_CLASS_AUTO:
3448 case STORAGE_CLASS_REGISTER:
3449 case STORAGE_CLASS_ENUM_ENTRY:
3452 panic("Invalid storage class for global variable");
3455 static void context_to_firm(context_t *context)
3457 /* first pass: create declarations */
3458 declaration_t *declaration = context->declarations;
3459 for( ; declaration != NULL; declaration = declaration->next) {
3460 if(declaration->namespc != NAMESPACE_NORMAL)
3462 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3463 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3465 if(declaration->symbol == NULL)
3468 type_t *type = skip_typeref(declaration->type);
3469 if(is_type_function(type)) {
3470 get_function_entity(declaration);
3472 create_global_variable(declaration);
3476 /* second pass: create code */
3477 declaration = context->declarations;
3478 for( ; declaration != NULL; declaration = declaration->next) {
3479 if(declaration->namespc != NAMESPACE_NORMAL)
3481 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3482 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3484 if(declaration->symbol == NULL)
3487 type_t *type = declaration->type;
3488 if(type->type != TYPE_FUNCTION)
3491 create_function(declaration);
3495 void init_ast2firm(void)
3497 obstack_init(&asm_obst);
3498 init_atomic_modes();
3501 void exit_ast2firm(void)
3503 obstack_free(&asm_obst, NULL);
3506 void translation_unit_to_firm(translation_unit_t *unit)
3508 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3509 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3510 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3512 ir_type_int = get_ir_type(type_int);
3513 ir_type_const_char = get_ir_type(type_const_char);
3514 ir_type_wchar_t = get_ir_type(type_wchar_t);
3515 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3518 type_void->base.firm_type = ir_type_void;
3520 /* just to be sure */
3521 continue_label = NULL;
3523 current_switch_cond = NULL;
3525 context_to_firm(& unit->context);