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"
21 #include "driver/firm_opt.h"
22 #include "driver/firm_cmdline.h"
24 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
26 static ir_type *ir_type_const_char;
27 static ir_type *ir_type_wchar_t;
28 static ir_type *ir_type_void;
29 static ir_type *ir_type_int;
31 static type_t *type_const_char;
32 static type_t *type_void;
33 static type_t *type_int;
35 static int next_value_number_function;
36 static ir_node *continue_label;
37 static ir_node *break_label;
38 static ir_node *current_switch_cond;
39 static bool saw_default_label;
40 static ir_node **imature_blocks;
42 static const declaration_t *current_function_decl;
43 static ir_node *current_function_name;
45 static struct obstack asm_obst;
47 typedef enum declaration_type_t {
48 DECLARATION_TYPE_UNKNOWN,
49 DECLARATION_TYPE_FUNCTION,
50 DECLARATION_TYPE_GLOBAL_VARIABLE,
51 DECLARATION_TYPE_LOCAL_VARIABLE,
52 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
53 DECLARATION_TYPE_COMPOUND_MEMBER,
54 DECLARATION_TYPE_LABEL_BLOCK,
55 DECLARATION_TYPE_ENUM_ENTRY
58 static ir_type *get_ir_type(type_t *type);
59 static int count_decls_in_stmts(const statement_t *stmt);
61 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
65 const declaration_t *declaration = & value_numbers[pos]->declaration;
67 print_warning_prefix(declaration->source_position);
68 fprintf(stderr, "variable '%s' might be used uninitialized\n",
69 declaration->symbol->string);
71 fprintf(stderr, "Some variable might be used uninitialized\n");
72 return new_r_Unknown(irg, mode);
75 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
77 const source_position_t *pos = (const source_position_t*) dbg;
80 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
84 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
86 const source_position_t *pos = (const source_position_t*) dbg;
91 return pos->input_name;
94 static dbg_info *get_dbg_info(const source_position_t *pos)
96 return (dbg_info*) pos;
99 static unsigned unique_id = 0;
101 static ident *unique_ident(const char *tag)
105 snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
107 return new_id_from_str(buf);
111 * Return the signed integer mode of size bytes.
113 * @param size the size
115 static ir_mode *get_smode(unsigned size)
117 static ir_mode *s_modes[16 + 1] = {0, };
120 if (size <= 0 || size > 16)
129 snprintf(name, sizeof(name), "i%u", bits);
130 res = new_ir_mode(name, irms_int_number, bits, 1, irma_twos_complement,
131 bits <= machine_size ? machine_size : bits );
139 * Return the unsigned integer mode of size bytes.
141 * @param size the size
143 static ir_mode *get_umode(unsigned size)
145 static ir_mode *u_modes[16 + 1] = {0, };
148 if (size <= 0 || size > 16)
157 snprintf(name, sizeof(name), "u%u", bits);
158 res = new_ir_mode(name, irms_int_number, bits, 0, irma_twos_complement,
159 bits <= machine_size ? machine_size : bits );
167 * Return the pointer mode of size bytes.
169 * @param size the size
171 static ir_mode *get_ptrmode(unsigned size, char *name)
173 static ir_mode *p_modes[16 + 1] = {0, };
176 if (size <= 0 || size > 16)
186 snprintf(buf, sizeof(buf), "p%u", bits);
189 res = new_ir_mode(name, irms_reference, bits, 0, irma_twos_complement,
190 bits <= machine_size ? machine_size : bits);
194 set_reference_mode_signed_eq(res, get_smode(size));
195 set_reference_mode_unsigned_eq(res, get_umode(size));
200 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST];
202 static ir_mode *mode_int, *mode_uint;
205 * Initialises the atomic modes depending on the machine size.
207 static void init_atomic_modes(void) {
208 unsigned int_size = machine_size < 32 ? 2 : 4;
209 unsigned long_size = machine_size < 64 ? 4 : 8;
210 unsigned llong_size = machine_size < 32 ? 4 : 8;
212 /* firm has no real void... */
213 _atomic_modes[ATOMIC_TYPE_VOID] = mode_T;
214 _atomic_modes[ATOMIC_TYPE_CHAR] = char_is_signed ? get_smode(1) : get_umode(1);
215 _atomic_modes[ATOMIC_TYPE_SCHAR] = get_smode(1);
216 _atomic_modes[ATOMIC_TYPE_UCHAR] = get_umode(1);
217 _atomic_modes[ATOMIC_TYPE_SHORT] = get_smode(2);
218 _atomic_modes[ATOMIC_TYPE_USHORT] = get_umode(2);
219 _atomic_modes[ATOMIC_TYPE_INT] = get_smode(int_size);
220 _atomic_modes[ATOMIC_TYPE_UINT] = get_umode(int_size);
221 _atomic_modes[ATOMIC_TYPE_LONG] = get_smode(long_size);
222 _atomic_modes[ATOMIC_TYPE_ULONG] = get_umode(long_size);
223 _atomic_modes[ATOMIC_TYPE_LONGLONG] = get_smode(llong_size);
224 _atomic_modes[ATOMIC_TYPE_ULONGLONG] = get_umode(llong_size);
225 _atomic_modes[ATOMIC_TYPE_FLOAT] = mode_F;
226 _atomic_modes[ATOMIC_TYPE_DOUBLE] = mode_D;
227 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = mode_E;
228 _atomic_modes[ATOMIC_TYPE_BOOL] = get_umode(int_size);
230 #ifdef PROVIDE_COMPLEX
231 _atomic_modes[ATOMIC_TYPE_BOOL] = _atomic_modes[ATOMIC_TYPE_INT];
232 _atomic_modes[ATOMIC_TYPE_FLOAT_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_FLOAT];
233 _atomic_modes[ATOMIC_TYPE_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_DOUBLE];
234 _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE];
237 /* Hmm, pointers should be machine size */
238 set_modeP_data(get_ptrmode(machine_size >> 3, NULL));
239 set_modeP_code(get_ptrmode(machine_size >> 3, NULL));
241 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
242 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
245 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
248 if ((unsigned)atomic_type->atype < (unsigned)ATOMIC_TYPE_LAST)
249 res = _atomic_modes[(unsigned)atomic_type->atype];
251 panic("Encountered unknown atomic type");
255 static unsigned get_type_size(type_t *type);
257 static unsigned get_atomic_type_size(const atomic_type_t *type)
259 switch(type->atype) {
260 case ATOMIC_TYPE_CHAR:
261 case ATOMIC_TYPE_SCHAR:
262 case ATOMIC_TYPE_UCHAR:
265 case ATOMIC_TYPE_SHORT:
266 case ATOMIC_TYPE_USHORT:
269 case ATOMIC_TYPE_BOOL:
270 case ATOMIC_TYPE_INT:
271 case ATOMIC_TYPE_UINT:
272 return machine_size >> 3;
274 case ATOMIC_TYPE_LONG:
275 case ATOMIC_TYPE_ULONG:
276 return machine_size > 16 ? machine_size >> 3 : 4;
278 case ATOMIC_TYPE_LONGLONG:
279 case ATOMIC_TYPE_ULONGLONG:
280 return machine_size > 16 ? 8 : 4;
282 case ATOMIC_TYPE_FLOAT:
285 case ATOMIC_TYPE_DOUBLE:
288 case ATOMIC_TYPE_LONG_DOUBLE:
291 case ATOMIC_TYPE_VOID:
294 case ATOMIC_TYPE_INVALID:
295 case ATOMIC_TYPE_LAST:
298 panic("Trying to determine size of invalid atomic type");
301 static unsigned get_compound_type_size(compound_type_t *type)
303 ir_type *irtype = get_ir_type((type_t*) type);
304 return get_type_size_bytes(irtype);
307 static unsigned get_array_type_size(array_type_t *type)
309 ir_type *irtype = get_ir_type((type_t*) type);
310 return get_type_size_bytes(irtype);
314 static unsigned get_type_size(type_t *type)
316 type = skip_typeref(type);
320 return get_atomic_type_size(&type->atomic);
322 return get_mode_size_bytes(mode_int);
323 case TYPE_COMPOUND_UNION:
324 case TYPE_COMPOUND_STRUCT:
325 return get_compound_type_size(&type->compound);
327 /* just a pointer to the function */
328 return get_mode_size_bytes(mode_P_code);
330 return get_mode_size_bytes(mode_P_data);
332 return get_array_type_size(&type->array);
334 return get_type_size(type->builtin.real_type);
340 panic("Trying to determine size of invalid type");
343 static unsigned count_parameters(const function_type_t *function_type)
347 function_parameter_t *parameter = function_type->parameters;
348 for ( ; parameter != NULL; parameter = parameter->next) {
358 static long fold_constant(const expression_t *expression);
360 static ir_type *create_atomic_type(const atomic_type_t *type)
362 ir_mode *mode = get_atomic_mode(type);
363 ident *id = get_mode_ident(mode);
364 ir_type *irtype = new_type_primitive(id, mode);
366 if(type->atype == ATOMIC_TYPE_LONG_DOUBLE) {
367 set_type_alignment_bytes(irtype, 4);
373 static ir_type *create_method_type(const function_type_t *function_type)
375 type_t *return_type = function_type->return_type;
377 ident *id = unique_ident("functiontype");
378 int n_parameters = count_parameters(function_type);
379 int n_results = return_type == type_void ? 0 : 1;
380 ir_type *irtype = new_type_method(id, n_parameters, n_results);
382 if(return_type != type_void) {
383 ir_type *restype = get_ir_type(return_type);
384 set_method_res_type(irtype, 0, restype);
387 function_parameter_t *parameter = function_type->parameters;
389 for( ; parameter != NULL; parameter = parameter->next) {
390 ir_type *p_irtype = get_ir_type(parameter->type);
391 set_method_param_type(irtype, n, p_irtype);
395 if(function_type->variadic || function_type->unspecified_parameters) {
396 set_method_variadicity(irtype, variadicity_variadic);
402 static ir_type *create_pointer_type(pointer_type_t *type)
404 type_t *points_to = type->points_to;
405 ir_type *ir_points_to;
406 /* Avoid endless recursion if the points_to type contains this poiner type
407 * again (might be a struct). We therefore first create a void* pointer
408 * and then set the real points_to type
410 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
411 ir_type_void, mode_P_data);
412 type->type.firm_type = ir_type;
414 ir_points_to = get_ir_type(points_to);
415 set_pointer_points_to_type(ir_type, ir_points_to);
420 static ir_type *create_array_type(array_type_t *type)
422 type_t *element_type = type->element_type;
423 ir_type *ir_element_type = get_ir_type(element_type);
425 ident *id = unique_ident("array");
426 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
428 if(type->size != NULL) {
429 int n_elements = fold_constant(type->size);
431 set_array_bounds_int(ir_type, 0, 0, n_elements);
433 size_t elemsize = get_type_size_bytes(ir_element_type);
434 int align = get_type_alignment_bytes(ir_element_type);
435 if(elemsize % align > 0) {
436 elemsize += align - (elemsize % align);
438 set_type_size_bytes(ir_type, n_elements * elemsize);
439 set_type_alignment_bytes(ir_type, align);
440 set_type_state(ir_type, layout_fixed);
442 set_array_lower_bound_int(ir_type, 0, 0);
448 #define INVALID_TYPE ((ir_type_ptr)-1)
450 static ir_type *create_struct_type(compound_type_t *type)
452 symbol_t *symbol = type->declaration->symbol;
455 id = unique_ident(symbol->string);
457 id = unique_ident("__anonymous_struct");
459 ir_type *ir_type = new_type_struct(id);
461 type->type.firm_type = ir_type;
465 declaration_t *entry = type->declaration->context.declarations;
466 for( ; entry != NULL; entry = entry->next) {
467 if(entry->namespc != NAMESPACE_NORMAL)
470 ident *ident = new_id_from_str(entry->symbol->string);
471 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
473 int entry_size = get_type_size_bytes(entry_ir_type);
474 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
475 int misalign = offset % entry_alignment;
477 offset += entry_alignment - misalign;
479 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
480 ir_entity *const entity = new_d_entity(ir_type, ident, entry_ir_type, dbgi);
481 set_entity_offset(entity, offset);
482 add_struct_member(ir_type, entity);
483 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
484 entry->v.entity = entity;
486 offset += entry_size;
487 if(entry_alignment > align_all) {
488 if(entry_alignment % align_all != 0) {
489 panic("Uneven alignments not supported yet");
491 align_all = entry_alignment;
495 int misalign = offset % align_all;
497 set_type_alignment_bytes(ir_type, align_all);
498 set_type_size_bytes(ir_type, offset);
499 set_type_state(ir_type, layout_fixed);
504 static ir_type *create_union_type(compound_type_t *type)
506 declaration_t *declaration = type->declaration;
507 symbol_t *symbol = declaration->symbol;
510 id = unique_ident(symbol->string);
512 id = unique_ident("__anonymous_union");
514 ir_type *ir_type = new_type_union(id);
516 type->type.firm_type = ir_type;
520 declaration_t *entry = declaration->context.declarations;
521 for( ; entry != NULL; entry = entry->next) {
522 if(entry->namespc != NAMESPACE_NORMAL)
525 ident *ident = new_id_from_str(entry->symbol->string);
526 ir_type_ptr entry_ir_type = get_ir_type(entry->type);
528 int entry_size = get_type_size_bytes(entry_ir_type);
529 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
531 dbg_info *const dbgi = get_dbg_info(&entry->source_position);
532 ir_entity *const entity = new_d_entity(ir_type, ident, entry_ir_type, dbgi);
533 add_union_member(ir_type, entity);
534 set_entity_offset(entity, 0);
535 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
536 entry->v.entity = entity;
538 if(entry_size > size) {
541 if(entry_alignment > align_all) {
542 if(entry_alignment % align_all != 0) {
543 panic("Uneven alignments not supported yet");
545 align_all = entry_alignment;
549 set_type_alignment_bytes(ir_type, align_all);
550 set_type_size_bytes(ir_type, size);
551 set_type_state(ir_type, layout_fixed);
556 static ir_node *expression_to_firm(const expression_t *expression);
557 static inline ir_mode *get_ir_mode(type_t *type);
559 static ir_type *create_enum_type(enum_type_t *const type)
561 type->type.firm_type = ir_type_int;
563 ir_mode *const mode = get_ir_mode((type_t*) type);
564 tarval *const one = get_mode_one(mode);
565 tarval * tv_next = get_tarval_null(mode);
567 declaration_t *declaration = type->declaration->next;
568 for (; declaration != NULL; declaration = declaration->next) {
569 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
572 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
574 expression_t *const init = declaration->init.enum_value;
576 ir_node *const cnst = expression_to_firm(init);
577 if (!is_Const(cnst)) {
578 panic("couldn't fold constant");
580 tv_next = get_Const_tarval(cnst);
582 declaration->v.enum_val = tv_next;
583 tv_next = tarval_add(tv_next, one);
589 static ir_type *get_ir_type(type_t *type)
591 assert(type != NULL);
593 type = skip_typeref(type);
595 if(type->base.firm_type != NULL) {
596 assert(type->base.firm_type != INVALID_TYPE);
597 return type->base.firm_type;
600 ir_type *firm_type = NULL;
603 firm_type = create_atomic_type(&type->atomic);
606 firm_type = create_method_type(&type->function);
609 firm_type = create_pointer_type(&type->pointer);
612 firm_type = create_array_type(&type->array);
614 case TYPE_COMPOUND_STRUCT:
615 firm_type = create_struct_type(&type->compound);
617 case TYPE_COMPOUND_UNION:
618 firm_type = create_union_type(&type->compound);
621 firm_type = create_enum_type(&type->enumt);
624 firm_type = get_ir_type(type->builtin.real_type);
631 if(firm_type == NULL)
632 panic("unknown type found");
634 type->base.firm_type = firm_type;
638 static inline ir_mode *get_ir_mode(type_t *type)
640 ir_type *irtype = get_ir_type(type);
642 /* firm doesn't report a mode for arrays somehow... */
643 if(is_Array_type(irtype)) {
647 ir_mode *mode = get_type_mode(irtype);
648 assert(mode != NULL);
652 static ident *predef_idents[rts_max];
654 /** Names of the runtime functions. */
655 static const struct {
656 int id; /**< the rts id */
657 int n_res; /**< number of return values */
658 const char *name; /**< the name of the rts function */
659 int n_params; /**< number of parameters */
660 unsigned flags; /**< language flags */
662 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
663 { rts_abort, 0, "abort", 0, _C89 },
664 { rts_abs, 1, "abs", 1, _C89 },
665 { rts_labs, 1, "labs", 1, _C89 },
666 { rts_llabs, 1, "llabs", 1, _C99 },
667 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
669 { rts_fabs, 1, "fabs", 1, _C89 },
670 { rts_sqrt, 1, "sqrt", 1, _C89 },
671 { rts_cbrt, 1, "cbrt", 1, _C99 },
672 { rts_exp, 1, "exp", 1, _C89 },
673 { rts_exp2, 1, "exp2", 1, _C89 },
674 { rts_exp10, 1, "exp10", 1, _GNUC },
675 { rts_log, 1, "log", 1, _C89 },
676 { rts_log2, 1, "log2", 1, _C89 },
677 { rts_log10, 1, "log10", 1, _C89 },
678 { rts_pow, 1, "pow", 2, _C89 },
679 { rts_sin, 1, "sin", 1, _C89 },
680 { rts_cos, 1, "cos", 1, _C89 },
681 { rts_tan, 1, "tan", 1, _C89 },
682 { rts_asin, 1, "asin", 1, _C89 },
683 { rts_acos, 1, "acos", 1, _C89 },
684 { rts_atan, 1, "atan", 1, _C89 },
685 { rts_sinh, 1, "sinh", 1, _C89 },
686 { rts_cosh, 1, "cosh", 1, _C89 },
687 { rts_tanh, 1, "tanh", 1, _C89 },
689 { rts_fabsf, 1, "fabsf", 1, _C99 },
690 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
691 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
692 { rts_expf, 1, "expf", 1, _C99 },
693 { rts_exp2f, 1, "exp2f", 1, _C99 },
694 { rts_exp10f, 1, "exp10f", 1, _GNUC },
695 { rts_logf, 1, "logf", 1, _C99 },
696 { rts_log2f, 1, "log2f", 1, _C99 },
697 { rts_log10f, 1, "log10f", 1, _C99 },
698 { rts_powf, 1, "powf", 2, _C99 },
699 { rts_sinf, 1, "sinf", 1, _C99 },
700 { rts_cosf, 1, "cosf", 1, _C99 },
701 { rts_tanf, 1, "tanf", 1, _C99 },
702 { rts_asinf, 1, "asinf", 1, _C99 },
703 { rts_acosf, 1, "acosf", 1, _C99 },
704 { rts_atanf, 1, "atanf", 1, _C99 },
705 { rts_sinhf, 1, "sinhf", 1, _C99 },
706 { rts_coshf, 1, "coshf", 1, _C99 },
707 { rts_tanhf, 1, "tanhf", 1, _C99 },
709 { rts_fabsl, 1, "fabsl", 1, _C99 },
710 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
711 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
712 { rts_expl, 1, "expl", 1, _C99 },
713 { rts_exp2l, 1, "exp2l", 1, _C99 },
714 { rts_exp10l, 1, "exp10l", 1, _GNUC },
715 { rts_logl, 1, "logl", 1, _C99 },
716 { rts_log2l, 1, "log2l", 1, _C99 },
717 { rts_log10l, 1, "log10l", 1, _C99 },
718 { rts_powl, 1, "powl", 2, _C99 },
719 { rts_sinl, 1, "sinl", 1, _C99 },
720 { rts_cosl, 1, "cosl", 1, _C99 },
721 { rts_tanl, 1, "tanl", 1, _C99 },
722 { rts_asinl, 1, "asinl", 1, _C99 },
723 { rts_acosl, 1, "acosl", 1, _C99 },
724 { rts_atanl, 1, "atanl", 1, _C99 },
725 { rts_sinhl, 1, "sinhl", 1, _C99 },
726 { rts_coshl, 1, "coshl", 1, _C99 },
727 { rts_tanhl, 1, "tanhl", 1, _C99 },
729 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
730 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
731 { rts_strcmp, 1, "strcmp", 2, _C89 },
732 { rts_strncmp, 1, "strncmp", 3, _C89 }
735 static ir_entity* get_function_entity(declaration_t *declaration)
737 if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
738 return declaration->v.entity;
739 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
741 symbol_t *symbol = declaration->symbol;
742 ident *id = new_id_from_str(symbol->string);
744 ir_type *global_type = get_glob_type();
745 ir_type *ir_type_method = get_ir_type(declaration->type);
746 assert(is_Method_type(ir_type_method));
748 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
749 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
750 set_entity_ld_ident(entity, id);
751 if(declaration->storage_class == STORAGE_CLASS_STATIC
752 || declaration->is_inline) {
753 set_entity_visibility(entity, visibility_local);
754 } else if(declaration->init.statement != NULL) {
755 set_entity_visibility(entity, visibility_external_visible);
757 set_entity_visibility(entity, visibility_external_allocated);
759 /* We should check for file scope here, but as long as we compile C only
760 this is not needed. */
761 int n_params = get_method_n_params(ir_type_method);
762 int n_res = get_method_n_ress(ir_type_method);
763 ir_ident_ptr id = get_entity_ident(entity);
766 if (n_params == 0 && n_res == 0 && id == predef_idents[rts_abort]) {
767 /* found abort(), store for later */
771 if (! firm_opt.freestanding) {
772 /* check for a known runtime function */
773 for (i = 0; i < rts_max; ++i) {
774 /* ignore those rts functions not necessary needed for current mode */
775 if ((c_mode & rts_data[i].flags) == 0)
777 if (n_params == rts_data[i].n_params && n_res == rts_data[i].n_res &&
778 id == predef_idents[rts_data[i].id])
779 rts_entities[rts_data[i].id] = entity;
784 set_entity_allocation(entity, allocation_static);
786 declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
787 declaration->v.entity = entity;
792 static ir_node *const_to_firm(const const_expression_t *cnst)
794 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
795 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
800 if(mode_is_float(mode)) {
801 tv = new_tarval_from_double(cnst->v.float_value, mode);
803 if(mode_is_signed(mode)) {
804 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
806 len = snprintf(buf, sizeof(buf), "%llu",
807 (unsigned long long) cnst->v.int_value);
809 tv = new_tarval_from_str(buf, len, mode);
812 return new_d_Const(dbgi, mode, tv);
815 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
817 assert(entity != NULL);
818 union symconst_symbol sym;
819 sym.entity_p = entity;
820 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
823 static ir_node *string_to_firm(const source_position_t *const src_pos,
824 const char *const id_prefix,
825 const char *const string)
827 ir_type *const global_type = get_glob_type();
828 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
831 ident *const id = unique_ident(id_prefix);
832 dbg_info *const dbgi = get_dbg_info(src_pos);
833 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
834 set_entity_ld_ident(entity, id);
835 set_entity_variability(entity, variability_constant);
836 set_entity_allocation(entity, allocation_static);
838 ir_type *const elem_type = ir_type_const_char;
839 ir_mode *const mode = get_type_mode(elem_type);
841 const size_t slen = strlen(string) + 1;
843 set_array_lower_bound_int(type, 0, 0);
844 set_array_upper_bound_int(type, 0, slen);
845 set_type_size_bytes(type, slen);
846 set_type_state(type, layout_fixed);
848 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
849 for(size_t i = 0; i < slen; ++i) {
850 tvs[i] = new_tarval_from_long(string[i], mode);
853 set_array_entity_values(entity, tvs, slen);
856 return create_symconst(dbgi, entity);
859 static ir_node *string_literal_to_firm(
860 const string_literal_expression_t* literal)
862 return string_to_firm(&literal->expression.source_position, "Lstr",
866 static ir_node *wide_string_literal_to_firm(
867 const wide_string_literal_expression_t* const literal)
869 ir_type *const global_type = get_glob_type();
870 ir_type *const elem_type = ir_type_wchar_t;
871 ir_type *const type = new_type_array(unique_ident("strtype"), 1,
874 ident *const id = unique_ident("Lstr");
875 dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
876 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
877 set_entity_ld_ident(entity, id);
878 set_entity_variability(entity, variability_constant);
879 set_entity_allocation(entity, allocation_static);
881 ir_mode *const mode = get_type_mode(elem_type);
883 const wchar_rep_t *const string = literal->value.begin;
884 const size_t slen = literal->value.size;
886 set_array_lower_bound_int(type, 0, 0);
887 set_array_upper_bound_int(type, 0, slen);
888 set_type_size_bytes(type, slen);
889 set_type_state(type, layout_fixed);
891 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
892 for(size_t i = 0; i < slen; ++i) {
893 tvs[i] = new_tarval_from_long(string[i], mode);
896 set_array_entity_values(entity, tvs, slen);
899 return create_symconst(dbgi, entity);
902 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
903 dbg_info *const dbgi)
905 if(is_compound_type(irtype) || is_Array_type(irtype)) {
909 ir_mode *const mode = get_type_mode(irtype);
910 ir_node *const memory = get_store();
911 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
912 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
913 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
918 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
920 ir_mode *mode = get_irn_mode(node);
922 if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
924 if(!mode_is_float(mode))
927 /* check if there is already a Conv */
928 if (get_irn_op(node) == op_Conv) {
929 /* convert it into a strict Conv */
930 set_Conv_strict(node, 1);
934 /* otherwise create a new one */
935 return new_d_strictConv(dbgi, node, mode);
938 static ir_node *get_global_var_address(dbg_info *const dbgi,
939 const declaration_t *const decl)
941 assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
943 ir_entity *const entity = decl->v.entity;
944 switch ((storage_class_tag_t)decl->storage_class) {
945 case STORAGE_CLASS_THREAD:
946 case STORAGE_CLASS_THREAD_EXTERN:
947 case STORAGE_CLASS_THREAD_STATIC: {
948 ir_node *const no_mem = new_NoMem();
949 ir_node *const tls = get_irg_tls(current_ir_graph);
950 return new_d_simpleSel(dbgi, no_mem, tls, entity);
954 return create_symconst(dbgi, entity);
958 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
960 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
961 declaration_t *declaration = ref->declaration;
962 type_t *type = skip_typeref(declaration->type);
964 switch((declaration_type_t) declaration->declaration_type) {
965 case DECLARATION_TYPE_UNKNOWN:
966 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
972 case DECLARATION_TYPE_ENUM_ENTRY: {
973 ir_mode *const mode = get_ir_mode(type);
974 return new_Const(mode, declaration->v.enum_val);
977 case DECLARATION_TYPE_LOCAL_VARIABLE: {
978 ir_mode *mode = get_ir_mode(type);
979 return get_value(declaration->v.value_number, mode);
981 case DECLARATION_TYPE_FUNCTION: {
982 return create_symconst(dbgi, declaration->v.entity);
984 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
985 ir_node *const addr = get_global_var_address(dbgi, declaration);
986 ir_type *const irtype = get_entity_type(declaration->v.entity);
987 return deref_address(irtype, addr, dbgi);
990 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
991 ir_entity *entity = declaration->v.entity;
992 ir_node *frame = get_irg_frame(current_ir_graph);
993 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
994 ir_type *irtype = get_entity_type(entity);
995 return deref_address(irtype, sel, dbgi);
998 case DECLARATION_TYPE_COMPOUND_MEMBER:
999 case DECLARATION_TYPE_LABEL_BLOCK:
1000 panic("not implemented reference type");
1003 panic("reference to declaration with unknown type found");
1006 static ir_node *reference_addr(const reference_expression_t *ref)
1008 dbg_info *dbgi = get_dbg_info(&ref->expression.source_position);
1009 declaration_t *declaration = ref->declaration;
1011 switch((declaration_type_t) declaration->declaration_type) {
1012 case DECLARATION_TYPE_UNKNOWN:
1014 case DECLARATION_TYPE_LOCAL_VARIABLE:
1015 panic("local variable without entity has no address");
1016 case DECLARATION_TYPE_FUNCTION: {
1017 return create_symconst(dbgi, declaration->v.entity);
1019 case DECLARATION_TYPE_GLOBAL_VARIABLE: {
1020 ir_node *const addr = get_global_var_address(dbgi, declaration);
1023 case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
1024 ir_entity *entity = declaration->v.entity;
1025 ir_node *frame = get_irg_frame(current_ir_graph);
1026 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1031 case DECLARATION_TYPE_ENUM_ENTRY:
1032 panic("trying to reference enum entry");
1034 case DECLARATION_TYPE_COMPOUND_MEMBER:
1035 case DECLARATION_TYPE_LABEL_BLOCK:
1036 panic("not implemented reference type");
1039 panic("reference to declaration with unknown type found");
1042 static ir_node *process_builtin_call(const call_expression_t *call)
1044 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1046 assert(call->function->type == EXPR_BUILTIN_SYMBOL);
1047 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1049 type_t *type = skip_typeref(builtin->expression.datatype);
1050 assert(is_type_pointer(type));
1052 type_t *function_type = skip_typeref(type->pointer.points_to);
1053 symbol_t *symbol = builtin->symbol;
1055 switch(symbol->ID) {
1056 case T___builtin_alloca: {
1057 if(call->arguments == NULL || call->arguments->next != NULL) {
1058 panic("invalid number of parameters on __builtin_alloca");
1060 expression_t *argument = call->arguments->expression;
1061 ir_node *size = expression_to_firm(argument);
1063 ir_node *store = get_store();
1064 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1066 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1068 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1072 case T___builtin_nan:
1073 case T___builtin_nanf:
1074 case T___builtin_nand: {
1075 /* Ignore string for now... */
1076 assert(is_type_function(function_type));
1077 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1078 tarval *tv = get_mode_NAN(mode);
1079 ir_node *res = new_d_Const(dbgi, mode, tv);
1082 case T___builtin_va_end:
1085 panic("Unsupported builtin found\n");
1089 static ir_node *call_expression_to_firm(const call_expression_t *call)
1091 assert(get_cur_block() != NULL);
1093 expression_t *function = call->function;
1094 if(function->type == EXPR_BUILTIN_SYMBOL) {
1095 return process_builtin_call(call);
1097 ir_node *callee = expression_to_firm(function);
1099 type_t *type = skip_typeref(function->base.datatype);
1100 assert(is_type_pointer(type));
1101 pointer_type_t *pointer_type = &type->pointer;
1102 type_t *points_to = skip_typeref(pointer_type->points_to);
1103 assert(is_type_function(points_to));
1104 function_type_t *function_type = &points_to->function;
1106 int n_parameters = 0;
1107 call_argument_t *argument = call->arguments;
1108 for( ; argument != NULL; argument = argument->next) {
1112 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
1114 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1115 ir_type *new_method_type = NULL;
1116 if(function_type->variadic || function_type->unspecified_parameters) {
1117 /* we need to construct a new method type matching the call
1119 int n_res = get_method_n_ress(ir_method_type);
1120 new_method_type = new_type_method(unique_ident("calltype"),
1121 n_parameters, n_res);
1122 set_method_calling_convention(new_method_type,
1123 get_method_calling_convention(ir_method_type));
1124 set_method_additional_properties(new_method_type,
1125 get_method_additional_properties(ir_method_type));
1127 for(int i = 0; i < n_res; ++i) {
1128 set_method_res_type(new_method_type, i,
1129 get_method_res_type(ir_method_type, i));
1132 ir_node *in[n_parameters];
1134 argument = call->arguments;
1136 for( ; argument != NULL; argument = argument->next) {
1137 expression_t *expression = argument->expression;
1138 ir_node *arg_node = expression_to_firm(expression);
1140 arg_node = do_strict_conv(dbgi, arg_node);
1143 if(new_method_type != NULL) {
1144 ir_type *irtype = get_ir_type(expression->base.datatype);
1145 set_method_param_type(new_method_type, n, irtype);
1150 assert(n == n_parameters);
1152 if(new_method_type != NULL)
1153 ir_method_type = new_method_type;
1155 ir_node *store = get_store();
1156 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1158 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1161 type_t *return_type = skip_typeref(function_type->return_type);
1162 ir_node *result = NULL;
1164 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1166 if(is_type_scalar(return_type)) {
1167 mode = get_ir_mode(return_type);
1171 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1172 result = new_d_Proj(dbgi, resproj, mode, 0);
1178 static void statement_to_firm(statement_t *statement);
1179 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1181 static ir_node *expression_to_addr(const expression_t *expression);
1182 static void create_condition_evaluation(const expression_t *expression,
1183 ir_node *true_block,
1184 ir_node *false_block);
1186 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1189 value = do_strict_conv(dbgi, value);
1191 ir_node *memory = get_store();
1193 if(is_type_scalar(type)) {
1194 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1195 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1196 set_store(store_mem);
1198 ir_type *irtype = get_ir_type(type);
1199 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1200 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1201 set_store(copyb_mem);
1205 static void set_value_for_expression(const expression_t *expression,
1208 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1209 value = do_strict_conv(dbgi, value);
1211 if(expression->type == EXPR_REFERENCE) {
1212 reference_expression_t *ref = (reference_expression_t*) expression;
1214 declaration_t *declaration = ref->declaration;
1215 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
1216 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1217 set_value(declaration->v.value_number, value);
1222 ir_node *addr = expression_to_addr(expression);
1223 type_t *type = skip_typeref(expression->base.datatype);
1224 assign_value(dbgi, addr, type, value);
1227 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1229 ir_mode *value_mode = get_irn_mode(value);
1231 if (value_mode == dest_mode || is_Bad(value))
1234 if(dest_mode == mode_b) {
1235 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1236 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1237 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1241 return new_d_Conv(dbgi, value, dest_mode);
1244 static ir_node *create_incdec(const unary_expression_t *expression)
1246 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1247 type_t *type = skip_typeref(expression->expression.datatype);
1248 ir_mode *mode = get_ir_mode(type);
1249 expression_t *value = expression->value;
1251 ir_node *value_node = expression_to_firm(value);
1254 if(is_type_pointer(type)) {
1255 pointer_type_t *pointer_type = &type->pointer;
1256 unsigned elem_size = get_type_size(pointer_type->points_to);
1257 offset = new_Const_long(mode_int, elem_size);
1259 assert(is_type_arithmetic(type));
1260 offset = new_Const(mode, get_mode_one(mode));
1263 switch(expression->expression.type) {
1264 case EXPR_UNARY_POSTFIX_INCREMENT: {
1265 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1266 set_value_for_expression(value, new_value);
1269 case EXPR_UNARY_POSTFIX_DECREMENT: {
1270 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1271 set_value_for_expression(value, new_value);
1274 case EXPR_UNARY_PREFIX_INCREMENT: {
1275 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1276 set_value_for_expression(value, new_value);
1279 case EXPR_UNARY_PREFIX_DECREMENT: {
1280 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1281 set_value_for_expression(value, new_value);
1285 panic("no incdec expr in create_incdec");
1290 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1292 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1293 type_t *type = skip_typeref(expression->expression.datatype);
1295 if(expression->expression.type == EXPR_UNARY_TAKE_ADDRESS)
1296 return expression_to_addr(expression->value);
1298 const expression_t *value = expression->value;
1299 ir_node *value_node = expression_to_firm(value);
1301 switch(expression->expression.type) {
1302 case EXPR_UNARY_NEGATE: {
1303 ir_mode *mode = get_ir_mode(type);
1304 return new_d_Minus(dbgi, value_node, mode);
1306 case EXPR_UNARY_PLUS:
1308 case EXPR_UNARY_BITWISE_NEGATE: {
1309 ir_mode *mode = get_ir_mode(type);
1310 return new_d_Not(dbgi, value_node, mode);
1312 case EXPR_UNARY_NOT: {
1313 ir_mode *mode = get_ir_mode(type);
1314 if(get_irn_mode(value_node) != mode_b) {
1315 value_node = create_conv(dbgi, value_node, mode_b);
1317 value_node = new_d_Not(dbgi, value_node, mode_b);
1318 if(mode != mode_b) {
1319 value_node = create_conv(dbgi, value_node, mode);
1323 case EXPR_UNARY_DEREFERENCE: {
1324 type_t *value_type = skip_typeref(value->base.datatype);
1325 ir_type *irtype = get_ir_type(value_type);
1326 assert(is_Pointer_type(irtype));
1327 ir_type *points_to = get_pointer_points_to_type(irtype);
1328 return deref_address(points_to, value_node, dbgi);
1330 case EXPR_UNARY_POSTFIX_INCREMENT:
1331 case EXPR_UNARY_POSTFIX_DECREMENT:
1332 case EXPR_UNARY_PREFIX_INCREMENT:
1333 case EXPR_UNARY_PREFIX_DECREMENT:
1334 return create_incdec(expression);
1335 case EXPR_UNARY_CAST: {
1336 ir_mode *mode = get_ir_mode(type);
1337 ir_node *node = create_conv(dbgi, value_node, mode);
1338 node = do_strict_conv(dbgi, node);
1341 case EXPR_UNARY_CAST_IMPLICIT: {
1342 ir_mode *mode = get_ir_mode(type);
1343 return create_conv(dbgi, value_node, mode);
1349 panic("invalid UNEXPR type found");
1352 static long get_pnc(const expression_type_t type)
1355 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1356 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1357 case EXPR_BINARY_NOTEQUAL: return pn_Cmp_Ne;
1358 case EXPR_BINARY_ISLESS:
1359 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1360 case EXPR_BINARY_ISLESSEQUAL:
1361 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1362 case EXPR_BINARY_ISGREATER:
1363 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1364 case EXPR_BINARY_ISGREATEREQUAL:
1365 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1366 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1371 panic("trying to get pn_Cmp from non-comparison binexpr type");
1374 static ir_node *create_lazy_op(const binary_expression_t *expression)
1376 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1377 type_t *type = expression->expression.datatype;
1378 ir_mode *mode = get_ir_mode(type);
1380 if(is_constant_expression(expression->left)) {
1381 long val = fold_constant(expression->left);
1382 expression_type_t etype = expression->expression.type;
1383 if((etype == EXPR_BINARY_LOGICAL_AND && val != 0)
1384 || (etype == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1385 return expression_to_firm(expression->right);
1387 assert((etype == EXPR_BINARY_LOGICAL_AND && val == 0)
1388 || (etype == EXPR_BINARY_LOGICAL_OR && val != 0));
1389 return new_Const(mode, get_mode_one(mode));
1393 ir_node *cur_block = get_cur_block();
1395 ir_node *one_block = new_immBlock();
1396 ir_node *one = new_Const(mode, get_mode_one(mode));
1397 ir_node *jmp_one = new_d_Jmp(dbgi);
1399 ir_node *zero_block = new_immBlock();
1400 ir_node *zero = new_Const(mode, get_mode_null(mode));
1401 ir_node *jmp_zero = new_d_Jmp(dbgi);
1403 set_cur_block(cur_block);
1404 create_condition_evaluation((const expression_t*) expression,
1405 one_block, zero_block);
1406 mature_immBlock(one_block);
1407 mature_immBlock(zero_block);
1409 ir_node *common_block = new_immBlock();
1410 add_immBlock_pred(common_block, jmp_one);
1411 add_immBlock_pred(common_block, jmp_zero);
1412 mature_immBlock(common_block);
1414 ir_node *in[2] = { one, zero };
1415 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1420 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1421 ir_node *right, ir_mode *mode);
1423 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1424 create_arithmetic_func func)
1426 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1427 ir_node *left = expression_to_firm(expression->left);
1428 ir_node *right = expression_to_firm(expression->right);
1429 type_t *type = expression->right->base.datatype;
1430 /* be careful with the modes, because in arithmetic assign nodes only
1431 * the right operand has the mode of the arithmetic already */
1432 ir_mode *mode = get_ir_mode(type);
1433 left = create_conv(dbgi, left, mode);
1434 ir_node *res = func(dbgi, left, right, mode);
1439 static ir_node *pointer_arithmetic(ir_node *const pointer,
1442 dbg_info *const dbgi,
1443 const create_arithmetic_func func)
1445 pointer_type_t *const pointer_type = (pointer_type_t*)type;
1446 type_t *const points_to = pointer_type->points_to;
1447 const unsigned elem_size = get_type_size(points_to);
1449 assert(elem_size >= 1);
1450 if (elem_size > 1) {
1451 integer = create_conv(dbgi, integer, mode_int);
1452 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1453 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
1457 ir_mode *const mode = get_ir_mode(type);
1458 return func(dbgi, pointer, integer, mode);
1461 static ir_node *create_arithmetic_assign_binop(
1462 const binary_expression_t *expression, create_arithmetic_func func)
1464 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1465 type_t *const type = skip_typeref(expression->expression.datatype);
1468 if (is_type_pointer(type)) {
1469 ir_node *const pointer = expression_to_firm(expression->left);
1470 ir_node * integer = expression_to_firm(expression->right);
1471 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1473 value = create_arithmetic_binop(expression, func);
1476 ir_mode *const mode = get_ir_mode(type);
1477 value = create_conv(dbgi, value, mode);
1478 set_value_for_expression(expression->left, value);
1483 static ir_node *create_add(const binary_expression_t *expression)
1485 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1486 ir_node *left = expression_to_firm(expression->left);
1487 ir_node *right = expression_to_firm(expression->right);
1488 type_t *type = expression->expression.datatype;
1490 expression_t *expr_left = expression->left;
1491 expression_t *expr_right = expression->right;
1492 type_t *type_left = skip_typeref(expr_left->base.datatype);
1493 type_t *type_right = skip_typeref(expr_right->base.datatype);
1495 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1496 ir_mode *const mode = get_ir_mode(type);
1497 return new_d_Add(dbgi, left, right, mode);
1500 if (is_type_pointer(type_left)) {
1501 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1503 assert(is_type_pointer(type_right));
1504 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1508 static ir_node *create_sub(const binary_expression_t *expression)
1510 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1511 expression_t *const expr_left = expression->left;
1512 expression_t *const expr_right = expression->right;
1513 ir_node *const left = expression_to_firm(expr_left);
1514 ir_node *const right = expression_to_firm(expr_right);
1515 type_t *const type = expression->expression.datatype;
1516 type_t *const type_left = skip_typeref(expr_left->base.datatype);
1517 type_t *const type_right = skip_typeref(expr_right->base.datatype);
1519 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1520 ir_mode *const mode = get_ir_mode(type);
1521 return new_d_Sub(dbgi, left, right, mode);
1522 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1523 const pointer_type_t *const ptr_type = &type_left->pointer;
1524 const unsigned elem_size = get_type_size(ptr_type->points_to);
1525 ir_mode *const mode = get_ir_mode(type);
1526 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
1527 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1528 ir_node *const no_mem = new_NoMem();
1529 ir_node *const div = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1530 op_pin_state_floats);
1531 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1534 assert(is_type_pointer(type_left));
1535 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1538 static ir_node *create_shift(const binary_expression_t *expression)
1540 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1541 ir_node *left = expression_to_firm(expression->left);
1542 ir_node *right = expression_to_firm(expression->right);
1543 type_t *type = expression->expression.datatype;
1544 ir_mode *mode = get_ir_mode(type);
1546 /* firm always wants the shift count to be unsigned */
1547 right = create_conv(dbgi, right, mode_uint);
1551 switch(expression->expression.type) {
1552 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1553 case EXPR_BINARY_SHIFTLEFT:
1554 res = new_d_Shl(dbgi, left, right, mode);
1556 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1557 case EXPR_BINARY_SHIFTRIGHT: {
1558 expression_t *expr_left = expression->left;
1559 type_t *type_left = skip_typeref(expr_left->base.datatype);
1561 if(is_type_signed(type_left)) {
1562 res = new_d_Shrs(dbgi, left, right, mode);
1564 res = new_d_Shr(dbgi, left, right, mode);
1569 panic("create shift op called for non-shift op");
1576 static ir_node *create_divmod(const binary_expression_t *expression)
1578 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1579 ir_node *left = expression_to_firm(expression->left);
1580 ir_node *right = expression_to_firm(expression->right);
1581 ir_node *pin = new_Pin(new_NoMem());
1582 /* be careful with the modes, because in arithmetic assign nodes only
1583 * the right operand has the mode of the arithmetic already */
1584 type_t *type = expression->right->base.datatype;
1585 ir_mode *mode = get_ir_mode(type);
1586 left = create_conv(dbgi, left, mode);
1590 switch (expression->expression.type) {
1591 case EXPR_BINARY_DIV:
1592 case EXPR_BINARY_DIV_ASSIGN:
1593 if(mode_is_float(mode)) {
1594 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1595 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1597 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1598 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1602 case EXPR_BINARY_MOD:
1603 case EXPR_BINARY_MOD_ASSIGN:
1604 assert(!mode_is_float(mode));
1605 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1606 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1609 default: panic("unexpected binary expression type in create_divmod()");
1615 static ir_node *create_arithmetic_assign_divmod(
1616 const binary_expression_t *expression)
1618 ir_node * value = create_divmod(expression);
1619 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1620 type_t *const type = expression->expression.datatype;
1621 ir_mode *const mode = get_ir_mode(type);
1623 assert(type->type != TYPE_POINTER);
1625 value = create_conv(dbgi, value, mode);
1626 set_value_for_expression(expression->left, value);
1631 static ir_node *create_arithmetic_assign_shift(
1632 const binary_expression_t *expression)
1634 ir_node * value = create_shift(expression);
1635 dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1636 type_t *const type = expression->expression.datatype;
1637 ir_mode *const mode = get_ir_mode(type);
1639 value = create_conv(dbgi, value, mode);
1640 set_value_for_expression(expression->left, value);
1645 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1647 expression_type_t type = expression->expression.type;
1650 case EXPR_BINARY_EQUAL:
1651 case EXPR_BINARY_NOTEQUAL:
1652 case EXPR_BINARY_LESS:
1653 case EXPR_BINARY_LESSEQUAL:
1654 case EXPR_BINARY_GREATER:
1655 case EXPR_BINARY_GREATEREQUAL:
1656 case EXPR_BINARY_ISGREATER:
1657 case EXPR_BINARY_ISGREATEREQUAL:
1658 case EXPR_BINARY_ISLESS:
1659 case EXPR_BINARY_ISLESSEQUAL:
1660 case EXPR_BINARY_ISLESSGREATER:
1661 case EXPR_BINARY_ISUNORDERED: {
1662 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1663 ir_node *left = expression_to_firm(expression->left);
1664 ir_node *right = expression_to_firm(expression->right);
1665 ir_node *cmp = new_d_Cmp(dbgi, left, right);
1666 long pnc = get_pnc(type);
1667 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
1670 case EXPR_BINARY_ASSIGN: {
1671 ir_node *right = expression_to_firm(expression->right);
1672 set_value_for_expression(expression->left, right);
1676 case EXPR_BINARY_ADD:
1677 return create_add(expression);
1678 case EXPR_BINARY_SUB:
1679 return create_sub(expression);
1680 case EXPR_BINARY_MUL:
1681 return create_arithmetic_binop(expression, new_d_Mul);
1682 case EXPR_BINARY_BITWISE_AND:
1683 return create_arithmetic_binop(expression, new_d_And);
1684 case EXPR_BINARY_BITWISE_OR:
1685 return create_arithmetic_binop(expression, new_d_Or);
1686 case EXPR_BINARY_BITWISE_XOR:
1687 return create_arithmetic_binop(expression, new_d_Eor);
1688 case EXPR_BINARY_SHIFTLEFT:
1689 case EXPR_BINARY_SHIFTRIGHT:
1690 return create_shift(expression);
1691 case EXPR_BINARY_DIV:
1692 case EXPR_BINARY_MOD:
1693 return create_divmod(expression);
1694 case EXPR_BINARY_LOGICAL_AND:
1695 case EXPR_BINARY_LOGICAL_OR:
1696 return create_lazy_op(expression);
1697 case EXPR_BINARY_COMMA:
1698 expression_to_firm(expression->left);
1699 return expression_to_firm(expression->right);
1700 case EXPR_BINARY_ADD_ASSIGN:
1701 return create_arithmetic_assign_binop(expression, new_d_Add);
1702 case EXPR_BINARY_SUB_ASSIGN:
1703 return create_arithmetic_assign_binop(expression, new_d_Sub);
1704 case EXPR_BINARY_MUL_ASSIGN:
1705 return create_arithmetic_assign_binop(expression, new_d_Mul);
1706 case EXPR_BINARY_DIV_ASSIGN:
1707 return create_arithmetic_assign_divmod(expression);
1708 case EXPR_BINARY_BITWISE_AND_ASSIGN:
1709 return create_arithmetic_assign_binop(expression, new_d_And);
1710 case EXPR_BINARY_BITWISE_OR_ASSIGN:
1711 return create_arithmetic_assign_binop(expression, new_d_Or);
1712 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1713 return create_arithmetic_assign_binop(expression, new_d_Eor);
1714 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1715 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1716 return create_arithmetic_assign_shift(expression);
1717 case EXPR_BINARY_BUILTIN_EXPECT:
1718 return expression_to_firm(expression->left);
1720 panic("TODO binexpr type");
1724 static ir_node *array_access_addr(const array_access_expression_t *expression)
1726 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1727 ir_node *base_addr = expression_to_firm(expression->array_ref);
1728 ir_node *offset = expression_to_firm(expression->index);
1729 offset = create_conv(dbgi, offset, mode_uint);
1731 type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1732 assert(is_type_pointer(ref_type));
1733 pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1735 unsigned elem_size = get_type_size(pointer_type->points_to);
1736 ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
1737 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
1739 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
1744 static ir_node *array_access_to_firm(
1745 const array_access_expression_t *expression)
1747 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1748 ir_node *addr = array_access_addr(expression);
1749 type_t *type = revert_automatic_type_conversion(
1750 (const expression_t*) expression);
1751 type = skip_typeref(type);
1752 ir_type *irtype = get_ir_type(type);
1754 return deref_address(irtype, addr, dbgi);
1757 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1759 type_t *type = expression->type;
1761 type = expression->size_expression->base.datatype;
1762 assert(type != NULL);
1765 ir_mode *mode = get_ir_mode(expression->expression.datatype);
1766 unsigned size = get_type_size(type);
1767 ir_node *size_node = new_Const_long(mode, size);
1772 static long fold_constant(const expression_t *expression)
1774 assert(is_constant_expression(expression));
1776 ir_graph *old_current_ir_graph = current_ir_graph;
1777 if(current_ir_graph == NULL) {
1778 current_ir_graph = get_const_code_irg();
1781 ir_node *cnst = expression_to_firm(expression);
1782 current_ir_graph = old_current_ir_graph;
1784 if(!is_Const(cnst)) {
1785 panic("couldn't fold constant\n");
1788 tarval *tv = get_Const_tarval(cnst);
1789 if(!tarval_is_long(tv)) {
1790 panic("result of constant folding is not integer\n");
1793 return get_tarval_long(tv);
1796 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1798 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1800 /* first try to fold a constant condition */
1801 if(is_constant_expression(expression->condition)) {
1802 long val = fold_constant(expression->condition);
1804 return expression_to_firm(expression->true_expression);
1806 return expression_to_firm(expression->false_expression);
1810 ir_node *cur_block = get_cur_block();
1812 /* create the true block */
1813 ir_node *true_block = new_immBlock();
1815 ir_node *true_val = expression_to_firm(expression->true_expression);
1816 ir_node *true_jmp = new_Jmp();
1818 /* create the false block */
1819 ir_node *false_block = new_immBlock();
1821 ir_node *false_val = expression_to_firm(expression->false_expression);
1822 ir_node *false_jmp = new_Jmp();
1824 /* create the condition evaluation */
1825 set_cur_block(cur_block);
1826 create_condition_evaluation(expression->condition, true_block, false_block);
1827 mature_immBlock(true_block);
1828 mature_immBlock(false_block);
1830 /* create the common block */
1831 ir_node *common_block = new_immBlock();
1832 add_immBlock_pred(common_block, true_jmp);
1833 add_immBlock_pred(common_block, false_jmp);
1834 mature_immBlock(common_block);
1836 /* TODO improve static semantics, so either both or no values are NULL */
1837 if (true_val == NULL || false_val == NULL)
1840 ir_node *in[2] = { true_val, false_val };
1841 ir_mode *mode = get_irn_mode(true_val);
1842 assert(get_irn_mode(false_val) == mode);
1843 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1848 static ir_node *select_addr(const select_expression_t *expression)
1850 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1852 ir_node *compound_addr = expression_to_firm(expression->compound);
1854 declaration_t *entry = expression->compound_entry;
1855 assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1856 ir_entity *entity = entry->v.entity;
1858 assert(entity != NULL);
1860 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1865 static ir_node *select_to_firm(const select_expression_t *expression)
1867 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1868 ir_node *addr = select_addr(expression);
1869 type_t *type = revert_automatic_type_conversion(
1870 (const expression_t*) expression);
1871 type = skip_typeref(type);
1872 ir_type *irtype = get_ir_type(type);
1874 return deref_address(irtype, addr, dbgi);
1877 /* Values returned by __builtin_classify_type. */
1878 typedef enum gcc_type_class
1884 enumeral_type_class,
1887 reference_type_class,
1891 function_type_class,
1902 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1904 const type_t *const type = expr->type_expression->base.datatype;
1910 const atomic_type_t *const atomic_type = &type->atomic;
1911 switch (atomic_type->atype) {
1912 /* should not be reached */
1913 case ATOMIC_TYPE_INVALID:
1917 /* gcc cannot do that */
1918 case ATOMIC_TYPE_VOID:
1919 tc = void_type_class;
1922 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
1923 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
1924 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
1925 case ATOMIC_TYPE_SHORT:
1926 case ATOMIC_TYPE_USHORT:
1927 case ATOMIC_TYPE_INT:
1928 case ATOMIC_TYPE_UINT:
1929 case ATOMIC_TYPE_LONG:
1930 case ATOMIC_TYPE_ULONG:
1931 case ATOMIC_TYPE_LONGLONG:
1932 case ATOMIC_TYPE_ULONGLONG:
1933 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
1934 tc = integer_type_class;
1937 case ATOMIC_TYPE_FLOAT:
1938 case ATOMIC_TYPE_DOUBLE:
1939 case ATOMIC_TYPE_LONG_DOUBLE:
1940 tc = real_type_class;
1943 #ifdef PROVIDE_COMPLEX
1944 case ATOMIC_TYPE_FLOAT_COMPLEX:
1945 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1946 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1947 tc = complex_type_class;
1949 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1950 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1951 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1952 tc = complex_type_class;
1957 panic("Unimplemented case in classify_type_to_firm().");
1962 case TYPE_ARRAY: /* gcc handles this as pointer */
1963 case TYPE_FUNCTION: /* gcc handles this as pointer */
1964 case TYPE_POINTER: tc = pointer_type_class; break;
1965 case TYPE_COMPOUND_STRUCT: tc = record_type_class; break;
1966 case TYPE_COMPOUND_UNION: tc = union_type_class; break;
1968 /* gcc handles this as integer */
1969 case TYPE_ENUM: tc = integer_type_class; break;
1972 panic("Unimplemented case in classify_type_to_firm().");
1975 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1976 ir_mode *const mode = mode_int;
1977 tarval *const tv = new_tarval_from_long(tc, mode);
1978 return new_d_Const(dbgi, mode, tv);
1981 static ir_node *function_name_to_firm(
1982 const string_literal_expression_t *const expr)
1984 if (current_function_name == NULL) {
1985 const source_position_t *const src_pos =
1986 &expr->expression.source_position;
1987 const char *const name = current_function_decl->symbol->string;
1988 current_function_name = string_to_firm(src_pos, "__func__", name);
1991 return current_function_name;
1994 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1996 statement_t *statement = expr->statement;
1998 assert(statement->type == STATEMENT_COMPOUND);
1999 return compound_statement_to_firm((compound_statement_t*) statement);
2002 static ir_node *va_start_expression_to_firm(
2003 const va_start_expression_t *const expr)
2005 ir_type *const method_type = get_ir_type(current_function_decl->type);
2006 int const n = get_method_n_params(method_type) - 1;
2007 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2008 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2009 dbg_info *const dbgi =
2010 get_dbg_info(&expr->expression.source_position);
2011 ir_node *const no_mem = new_NoMem();
2012 ir_node *const arg_sel =
2013 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2015 size_t const parm_size = get_type_size(expr->parameter->type);
2016 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2017 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2018 set_value_for_expression(expr->ap, add);
2023 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2025 ir_type *const irtype = get_ir_type(expr->expression.datatype);
2026 ir_node *const ap = expression_to_firm(expr->ap);
2027 dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
2028 ir_node *const res = deref_address(irtype, ap, dbgi);
2030 size_t const parm_size = get_type_size(expr->expression.datatype);
2031 ir_node *const cnst = new_Const_long(mode_uint, parm_size);
2032 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2033 set_value_for_expression(expr->ap, add);
2038 static ir_node *dereference_addr(const unary_expression_t *const expression)
2040 assert(expression->expression.type == EXPR_UNARY_DEREFERENCE);
2041 return expression_to_firm(expression->value);
2044 static ir_node *expression_to_addr(const expression_t *expression)
2046 switch(expression->type) {
2047 case EXPR_REFERENCE:
2048 return reference_addr(&expression->reference);
2049 case EXPR_ARRAY_ACCESS:
2050 return array_access_addr(&expression->array_access);
2052 return select_addr(&expression->select);
2054 return call_expression_to_firm(&expression->call);
2055 case EXPR_UNARY_DEREFERENCE: {
2056 return dereference_addr(&expression->unary);
2061 panic("trying to get address of non-lvalue");
2064 static ir_node *_expression_to_firm(const expression_t *expression)
2066 switch(expression->type) {
2068 return const_to_firm(&expression->conste);
2069 case EXPR_STRING_LITERAL:
2070 return string_literal_to_firm(&expression->string);
2071 case EXPR_WIDE_STRING_LITERAL:
2072 return wide_string_literal_to_firm(&expression->wide_string);
2073 case EXPR_REFERENCE:
2074 return reference_expression_to_firm(&expression->reference);
2076 return call_expression_to_firm(&expression->call);
2078 return unary_expression_to_firm(&expression->unary);
2080 return binary_expression_to_firm(&expression->binary);
2081 case EXPR_ARRAY_ACCESS:
2082 return array_access_to_firm(&expression->array_access);
2084 return sizeof_to_firm(&expression->sizeofe);
2085 case EXPR_CONDITIONAL:
2086 return conditional_to_firm(&expression->conditional);
2088 return select_to_firm(&expression->select);
2089 case EXPR_CLASSIFY_TYPE:
2090 return classify_type_to_firm(&expression->classify_type);
2092 case EXPR_PRETTY_FUNCTION:
2093 return function_name_to_firm(&expression->string);
2094 case EXPR_STATEMENT:
2095 return statement_expression_to_firm(&expression->statement);
2097 return va_start_expression_to_firm(&expression->va_starte);
2099 return va_arg_expression_to_firm(&expression->va_arge);
2101 case EXPR_BUILTIN_SYMBOL:
2102 panic("unimplemented expression found");
2108 panic("invalid expression found");
2111 static ir_node *expression_to_firm(const expression_t *expression)
2113 ir_node *res = _expression_to_firm(expression);
2115 if(res != NULL && get_irn_mode(res) == mode_b) {
2116 ir_mode *mode = get_ir_mode(expression->base.datatype);
2117 res = create_conv(NULL, res, mode);
2123 static ir_node *expression_to_modeb(const expression_t *expression)
2125 ir_node *res = _expression_to_firm(expression);
2126 res = create_conv(NULL, res, mode_b);
2132 * create a short-circuit expression evaluation that tries to construct
2133 * efficient control flow structures for &&, || and ! expressions
2135 static void create_condition_evaluation(const expression_t *expression,
2136 ir_node *true_block,
2137 ir_node *false_block)
2139 switch(expression->type) {
2140 case EXPR_UNARY_NOT: {
2141 const unary_expression_t *unary_expression = &expression->unary;
2142 create_condition_evaluation(unary_expression->value, false_block,
2146 case EXPR_BINARY_LOGICAL_AND: {
2147 const binary_expression_t *binary_expression = &expression->binary;
2149 ir_node *cur_block = get_cur_block();
2150 ir_node *extra_block = new_immBlock();
2151 set_cur_block(cur_block);
2152 create_condition_evaluation(binary_expression->left, extra_block,
2154 mature_immBlock(extra_block);
2155 set_cur_block(extra_block);
2156 create_condition_evaluation(binary_expression->right, true_block,
2160 case EXPR_BINARY_LOGICAL_OR: {
2161 const binary_expression_t *binary_expression = &expression->binary;
2163 ir_node *cur_block = get_cur_block();
2164 ir_node *extra_block = new_immBlock();
2165 set_cur_block(cur_block);
2166 create_condition_evaluation(binary_expression->left, true_block,
2168 mature_immBlock(extra_block);
2169 set_cur_block(extra_block);
2170 create_condition_evaluation(binary_expression->right, true_block,
2178 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2179 ir_node *condition = expression_to_modeb(expression);
2180 ir_node *cond = new_d_Cond(dbgi, condition);
2181 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2182 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2184 add_immBlock_pred(true_block, true_proj);
2185 add_immBlock_pred(false_block, false_proj);
2187 set_cur_block(NULL);
2192 static void create_declaration_entity(declaration_t *declaration,
2193 declaration_type_t declaration_type,
2194 ir_type *parent_type)
2196 ident *const id = new_id_from_str(declaration->symbol->string);
2197 ir_type *const irtype = get_ir_type(declaration->type);
2198 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2199 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2200 set_entity_ld_ident(entity, id);
2202 declaration->declaration_type = (unsigned char) declaration_type;
2203 declaration->v.entity = entity;
2204 set_entity_variability(entity, variability_uninitialized);
2205 if(parent_type == get_tls_type())
2206 set_entity_allocation(entity, allocation_automatic);
2207 else if(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE)
2208 set_entity_allocation(entity, allocation_static);
2209 /* TODO: visibility? */
2212 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2214 enum compound_graph_entry_type_t {
2215 COMPOUND_GRAPH_ENTRY_ARRAY,
2216 COMPOUND_GRAPH_ENTRY_COMPOUND
2219 struct compound_graph_path_entry_t {
2225 compound_graph_path_entry_t *prev;
2228 static void create_initializer_object(initializer_t *initializer, type_t *type,
2229 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2231 static compound_graph_path *create_compound_path(ir_type *type,
2232 compound_graph_path_entry_t *entry, int len)
2234 compound_graph_path *path = new_compound_graph_path(type, len);
2237 for( ; entry != NULL; entry = entry->prev, --i) {
2239 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2240 set_compound_graph_path_node(path, i, entry->v.entity);
2242 assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2243 set_compound_graph_path_array_index(path, i, entry->v.array_index);
2251 static void create_initializer_value(initializer_value_t *initializer,
2253 compound_graph_path_entry_t *entry,
2256 ir_node *node = expression_to_firm(initializer->value);
2257 ir_type *type = get_entity_type(entity);
2258 compound_graph_path *path = create_compound_path(type, entry, len);
2259 add_compound_ent_value_w_path(entity, node, path);
2262 static void create_initializer_compound(initializer_list_t *initializer,
2263 compound_type_t *type,
2265 compound_graph_path_entry_t *last_entry,
2268 declaration_t *compound_declaration = type->declaration;
2270 declaration_t *compound_entry = compound_declaration->context.declarations;
2272 compound_graph_path_entry_t entry;
2273 entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2274 entry.prev = last_entry;
2278 for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2279 if(compound_entry->symbol == NULL)
2281 if(compound_entry->namespc != NAMESPACE_NORMAL)
2284 if(i >= initializer->len)
2287 entry.v.entity = compound_entry->v.entity;
2289 initializer_t *sub_initializer = initializer->initializers[i];
2291 assert(compound_entry != NULL);
2292 assert(compound_entry->declaration_type
2293 == DECLARATION_TYPE_COMPOUND_MEMBER);
2295 if(sub_initializer->type == INITIALIZER_VALUE) {
2296 create_initializer_value(&sub_initializer->value,
2297 entity, &entry, len);
2299 type_t *entry_type = skip_typeref(compound_entry->type);
2300 create_initializer_object(sub_initializer, entry_type, entity,
2308 static void create_initializer_array(initializer_list_t *initializer,
2309 array_type_t *type, ir_entity *entity,
2310 compound_graph_path_entry_t *last_entry,
2313 type_t *element_type = type->element_type;
2314 element_type = skip_typeref(element_type);
2316 compound_graph_path_entry_t entry;
2317 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2318 entry.prev = last_entry;
2322 for(i = 0; i < initializer->len; ++i) {
2323 entry.v.array_index = i;
2325 initializer_t *sub_initializer = initializer->initializers[i];
2327 if(sub_initializer->type == INITIALIZER_VALUE) {
2328 create_initializer_value(&sub_initializer->value,
2329 entity, &entry, len);
2331 create_initializer_object(sub_initializer, element_type, entity,
2337 /* TODO: initialize rest... */
2338 if(type->size_expression != NULL) {
2339 size_t array_len = fold_constant(type->size_expression);
2340 for( ; i < array_len; ++i) {
2347 static void create_initializer_string(initializer_string_t *initializer,
2348 array_type_t *type, ir_entity *entity,
2349 compound_graph_path_entry_t *last_entry,
2352 type_t *element_type = type->element_type;
2353 element_type = skip_typeref(element_type);
2355 compound_graph_path_entry_t entry;
2356 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2357 entry.prev = last_entry;
2360 ir_type *irtype = get_entity_type(entity);
2361 size_t arr_len = get_array_type_size(type);
2362 const char *p = initializer->string;
2364 for(i = 0; i < arr_len; ++i, ++p) {
2365 entry.v.array_index = i;
2367 ir_node *node = new_Const_long(mode_Bs, *p);
2368 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2369 add_compound_ent_value_w_path(entity, node, path);
2376 static void create_initializer_wide_string(
2377 const initializer_wide_string_t *const initializer, array_type_t *const type,
2378 ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2381 type_t *element_type = type->element_type;
2382 element_type = skip_typeref(element_type);
2384 compound_graph_path_entry_t entry;
2385 entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2386 entry.prev = last_entry;
2389 ir_type *const irtype = get_entity_type(entity);
2390 const size_t arr_len = get_array_type_size(type);
2391 const wchar_rep_t * p = initializer->string.begin;
2392 const wchar_rep_t *const end = p + initializer->string.size;
2393 for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2394 entry.v.array_index = i;
2396 ir_node *node = new_Const_long(mode_int, *p);
2397 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2398 add_compound_ent_value_w_path(entity, node, path);
2402 static void create_initializer_object(initializer_t *initializer, type_t *type,
2403 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2405 if(is_type_array(type)) {
2406 array_type_t *array_type = &type->array;
2408 switch (initializer->type) {
2409 case INITIALIZER_STRING: {
2410 initializer_string_t *const string = &initializer->string;
2411 create_initializer_string(string, array_type, entity, entry, len);
2415 case INITIALIZER_WIDE_STRING: {
2416 initializer_wide_string_t *const string = &initializer->wide_string;
2417 create_initializer_wide_string(string, array_type, entity, entry, len);
2421 case INITIALIZER_LIST: {
2422 initializer_list_t *const list = &initializer->list;
2423 create_initializer_array(list, array_type, entity, entry, len);
2427 case INITIALIZER_VALUE:
2430 panic("Unhandled initializer");
2432 assert(initializer->type == INITIALIZER_LIST);
2433 initializer_list_t *list = &initializer->list;
2435 assert(is_type_compound(type));
2436 compound_type_t *compound_type = &type->compound;
2437 create_initializer_compound(list, compound_type, entity, entry, len);
2441 static void create_initializer_local_variable_entity(declaration_t *declaration)
2443 initializer_t *initializer = declaration->init.initializer;
2444 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
2445 ir_entity *entity = declaration->v.entity;
2446 ir_node *memory = get_store();
2447 ir_node *nomem = new_NoMem();
2448 ir_node *frame = get_irg_frame(current_ir_graph);
2449 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
2451 if(initializer->type == INITIALIZER_VALUE) {
2452 initializer_value_t *initializer_value = &initializer->value;
2454 ir_node *value = expression_to_firm(initializer_value->value);
2455 type_t *type = skip_typeref(declaration->type);
2456 assign_value(dbgi, addr, type, value);
2460 /* create a "template" entity which is copied to the entity on the stack */
2461 ident *const id = unique_ident("initializer");
2462 ir_type *const irtype = get_ir_type(declaration->type);
2463 ir_type *const global_type = get_glob_type();
2464 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
2465 set_entity_ld_ident(init_entity, id);
2467 set_entity_variability(init_entity, variability_initialized);
2468 set_entity_visibility(init_entity, visibility_local);
2469 set_entity_allocation(init_entity, allocation_static);
2471 ir_graph *const old_current_ir_graph = current_ir_graph;
2472 current_ir_graph = get_const_code_irg();
2474 type_t *const type = skip_typeref(declaration->type);
2475 create_initializer_object(initializer, type, init_entity, NULL, 0);
2477 assert(current_ir_graph == get_const_code_irg());
2478 current_ir_graph = old_current_ir_graph;
2480 ir_node *const src_addr = create_symconst(dbgi, init_entity);
2481 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2483 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2484 set_store(copyb_mem);
2487 static void create_initializer(declaration_t *declaration)
2489 initializer_t *initializer = declaration->init.initializer;
2490 if(initializer == NULL)
2493 declaration_type_t declaration_type
2494 = (declaration_type_t) declaration->declaration_type;
2495 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2496 create_initializer_local_variable_entity(declaration);
2500 if(initializer->type == INITIALIZER_VALUE) {
2501 initializer_value_t *initializer_value = &initializer->value;
2503 ir_node *value = expression_to_firm(initializer_value->value);
2505 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2506 set_value(declaration->v.value_number, value);
2508 assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2510 ir_entity *entity = declaration->v.entity;
2512 set_entity_variability(entity, variability_initialized);
2513 set_atomic_ent_value(entity, value);
2516 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2517 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2519 ir_entity *entity = declaration->v.entity;
2520 set_entity_variability(entity, variability_initialized);
2522 type_t *type = skip_typeref(declaration->type);
2523 create_initializer_object(initializer, type, entity, NULL, 0);
2527 static void create_local_variable(declaration_t *declaration)
2529 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2531 bool needs_entity = declaration->address_taken;
2532 type_t *type = skip_typeref(declaration->type);
2534 if(is_type_array(type) || is_type_compound(type)) {
2535 needs_entity = true;
2539 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2540 create_declaration_entity(declaration,
2541 DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2544 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2545 declaration->v.value_number = next_value_number_function;
2546 ++next_value_number_function;
2549 create_initializer(declaration);
2552 static void create_local_static_variable(declaration_t *declaration)
2554 assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2556 type_t *const type = skip_typeref(declaration->type);
2557 ir_type *const global_type = get_glob_type();
2558 ident *const id = unique_ident(declaration->symbol->string);
2559 ir_type *const irtype = get_ir_type(type);
2560 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2561 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
2562 set_entity_ld_ident(entity, id);
2564 declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2565 declaration->v.entity = entity;
2566 set_entity_variability(entity, variability_uninitialized);
2567 set_entity_visibility(entity, visibility_local);
2568 set_entity_allocation(entity, allocation_static);
2570 ir_graph *const old_current_ir_graph = current_ir_graph;
2571 current_ir_graph = get_const_code_irg();
2573 create_initializer(declaration);
2575 assert(current_ir_graph == get_const_code_irg());
2576 current_ir_graph = old_current_ir_graph;
2581 static void return_statement_to_firm(return_statement_t *statement)
2583 if(get_cur_block() == NULL)
2586 ir_type *func_irtype = get_ir_type(current_function_decl->type);
2588 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2592 if(get_method_n_ress(func_irtype) > 0) {
2593 ir_type *res_type = get_method_res_type(func_irtype, 0);
2595 if(statement->return_value != NULL) {
2596 ir_node *node = expression_to_firm(statement->return_value);
2597 node = do_strict_conv(dbgi, node);
2601 if(is_compound_type(res_type)) {
2604 mode = get_type_mode(res_type);
2606 in[0] = new_Unknown(mode);
2610 /* build return_value for its side effects */
2611 if(statement->return_value != NULL) {
2612 expression_to_firm(statement->return_value);
2617 ir_node *store = get_store();
2618 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
2620 ir_node *end_block = get_irg_end_block(current_ir_graph);
2621 add_immBlock_pred(end_block, ret);
2623 set_cur_block(NULL);
2626 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
2628 if(get_cur_block() == NULL)
2631 return expression_to_firm(statement->expression);
2634 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
2636 ir_node *result = NULL;
2637 statement_t *statement = compound->statements;
2638 for( ; statement != NULL; statement = statement->base.next) {
2639 //context2firm(&statement->context);
2641 if(statement->base.next == NULL
2642 && statement->type == STATEMENT_EXPRESSION) {
2643 result = expression_statement_to_firm(
2644 (expression_statement_t*) statement);
2647 statement_to_firm(statement);
2653 static void create_local_declaration(declaration_t *declaration)
2655 type_t *type = skip_typeref(declaration->type);
2657 switch ((storage_class_tag_t) declaration->storage_class) {
2658 case STORAGE_CLASS_STATIC:
2659 create_local_static_variable(declaration);
2661 case STORAGE_CLASS_ENUM_ENTRY:
2662 panic("enum entry declaration in local block found");
2663 case STORAGE_CLASS_EXTERN:
2664 panic("extern declaration in local block found");
2665 case STORAGE_CLASS_NONE:
2666 case STORAGE_CLASS_AUTO:
2667 case STORAGE_CLASS_REGISTER:
2668 if(is_type_function(type)) {
2669 panic("nested functions not supported yet");
2671 create_local_variable(declaration);
2674 case STORAGE_CLASS_TYPEDEF:
2675 case STORAGE_CLASS_THREAD:
2676 case STORAGE_CLASS_THREAD_EXTERN:
2677 case STORAGE_CLASS_THREAD_STATIC:
2680 panic("invalid storage class found");
2683 static void declaration_statement_to_firm(declaration_statement_t *statement)
2685 declaration_t *declaration = statement->declarations_begin;
2686 declaration_t *end = statement->declarations_end->next;
2687 for( ; declaration != end; declaration = declaration->next) {
2688 create_local_variable(declaration);
2692 static void if_statement_to_firm(if_statement_t *statement)
2694 ir_node *cur_block = get_cur_block();
2696 ir_node *fallthrough_block = new_immBlock();
2698 /* the true (blocks) */
2699 ir_node *true_block;
2700 if (statement->true_statement != NULL) {
2701 true_block = new_immBlock();
2702 statement_to_firm(statement->true_statement);
2703 if(get_cur_block() != NULL) {
2704 ir_node *jmp = new_Jmp();
2705 add_immBlock_pred(fallthrough_block, jmp);
2708 true_block = fallthrough_block;
2711 /* the false (blocks) */
2712 ir_node *false_block;
2713 if(statement->false_statement != NULL) {
2714 false_block = new_immBlock();
2716 statement_to_firm(statement->false_statement);
2717 if(get_cur_block() != NULL) {
2718 ir_node *jmp = new_Jmp();
2719 add_immBlock_pred(fallthrough_block, jmp);
2722 false_block = fallthrough_block;
2725 /* create the condition */
2726 if(cur_block != NULL) {
2727 set_cur_block(cur_block);
2728 create_condition_evaluation(statement->condition, true_block,
2732 mature_immBlock(true_block);
2733 if(false_block != fallthrough_block) {
2734 mature_immBlock(false_block);
2736 mature_immBlock(fallthrough_block);
2738 set_cur_block(fallthrough_block);
2741 static void while_statement_to_firm(while_statement_t *statement)
2743 ir_node *jmp = NULL;
2744 if(get_cur_block() != NULL) {
2748 /* create the header block */
2749 ir_node *header_block = new_immBlock();
2751 add_immBlock_pred(header_block, jmp);
2754 /* the false block */
2755 ir_node *false_block = new_immBlock();
2758 ir_node *body_block;
2759 if (statement->body != NULL) {
2760 ir_node *old_continue_label = continue_label;
2761 ir_node *old_break_label = break_label;
2762 continue_label = header_block;
2763 break_label = false_block;
2765 body_block = new_immBlock();
2766 statement_to_firm(statement->body);
2768 assert(continue_label == header_block);
2769 assert(break_label == false_block);
2770 continue_label = old_continue_label;
2771 break_label = old_break_label;
2773 if(get_cur_block() != NULL) {
2775 add_immBlock_pred(header_block, jmp);
2778 body_block = header_block;
2781 /* create the condition */
2782 set_cur_block(header_block);
2784 create_condition_evaluation(statement->condition, body_block, false_block);
2785 mature_immBlock(body_block);
2786 mature_immBlock(false_block);
2787 mature_immBlock(header_block);
2789 set_cur_block(false_block);
2792 static void do_while_statement_to_firm(do_while_statement_t *statement)
2794 ir_node *jmp = NULL;
2795 if(get_cur_block() != NULL) {
2799 /* create the header block */
2800 ir_node *header_block = new_immBlock();
2802 /* the false block */
2803 ir_node *false_block = new_immBlock();
2806 ir_node *body_block = new_immBlock();
2808 add_immBlock_pred(body_block, jmp);
2811 if (statement->body != NULL) {
2812 ir_node *old_continue_label = continue_label;
2813 ir_node *old_break_label = break_label;
2814 continue_label = header_block;
2815 break_label = false_block;
2817 statement_to_firm(statement->body);
2819 assert(continue_label == header_block);
2820 assert(break_label == false_block);
2821 continue_label = old_continue_label;
2822 break_label = old_break_label;
2824 if (get_cur_block() == NULL) {
2825 mature_immBlock(header_block);
2826 mature_immBlock(body_block);
2827 mature_immBlock(false_block);
2832 ir_node *body_jmp = new_Jmp();
2833 add_immBlock_pred(header_block, body_jmp);
2834 mature_immBlock(header_block);
2836 /* create the condition */
2837 set_cur_block(header_block);
2839 create_condition_evaluation(statement->condition, body_block, false_block);
2840 mature_immBlock(body_block);
2841 mature_immBlock(false_block);
2842 mature_immBlock(header_block);
2844 set_cur_block(false_block);
2847 static void for_statement_to_firm(for_statement_t *statement)
2849 ir_node *jmp = NULL;
2850 if (get_cur_block() != NULL) {
2851 if(statement->initialisation != NULL) {
2852 expression_to_firm(statement->initialisation);
2855 /* create declarations */
2856 declaration_t *declaration = statement->context.declarations;
2857 for( ; declaration != NULL; declaration = declaration->next) {
2858 create_local_declaration(declaration);
2865 /* create the step block */
2866 ir_node *const step_block = new_immBlock();
2867 if (statement->step != NULL) {
2868 expression_to_firm(statement->step);
2870 ir_node *const step_jmp = new_Jmp();
2872 /* create the header block */
2873 ir_node *const header_block = new_immBlock();
2875 add_immBlock_pred(header_block, jmp);
2877 add_immBlock_pred(header_block, step_jmp);
2879 /* the false block */
2880 ir_node *const false_block = new_immBlock();
2883 ir_node * body_block;
2884 if (statement->body != NULL) {
2885 ir_node *const old_continue_label = continue_label;
2886 ir_node *const old_break_label = break_label;
2887 continue_label = step_block;
2888 break_label = false_block;
2890 body_block = new_immBlock();
2891 statement_to_firm(statement->body);
2893 assert(continue_label == step_block);
2894 assert(break_label == false_block);
2895 continue_label = old_continue_label;
2896 break_label = old_break_label;
2898 if (get_cur_block() != NULL) {
2900 add_immBlock_pred(step_block, jmp);
2903 body_block = step_block;
2906 /* create the condition */
2907 set_cur_block(header_block);
2908 if (statement->condition != NULL) {
2909 create_condition_evaluation(statement->condition, body_block,
2912 keep_alive(header_block);
2914 add_immBlock_pred(body_block, jmp);
2917 mature_immBlock(body_block);
2918 mature_immBlock(false_block);
2919 mature_immBlock(step_block);
2920 mature_immBlock(header_block);
2921 mature_immBlock(false_block);
2923 set_cur_block(false_block);
2926 static void create_jump_statement(const statement_t *statement,
2927 ir_node *target_block)
2929 if(get_cur_block() == NULL)
2932 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2933 ir_node *jump = new_d_Jmp(dbgi);
2934 add_immBlock_pred(target_block, jump);
2936 set_cur_block(NULL);
2939 static void switch_statement_to_firm(const switch_statement_t *statement)
2941 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2943 ir_node *expression = expression_to_firm(statement->expression);
2944 ir_node *cond = new_d_Cond(dbgi, expression);
2945 ir_node *break_block = new_immBlock();
2947 set_cur_block(NULL);
2949 ir_node *const old_switch_cond = current_switch_cond;
2950 ir_node *const old_break_label = break_label;
2951 const bool old_saw_default_label = saw_default_label;
2952 current_switch_cond = cond;
2953 break_label = break_block;
2955 statement_to_firm(statement->body);
2957 if(get_cur_block() != NULL) {
2958 ir_node *jmp = new_Jmp();
2959 add_immBlock_pred(break_block, jmp);
2962 if (!saw_default_label) {
2963 set_cur_block(get_nodes_block(cond));
2964 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2965 MAGIC_DEFAULT_PN_NUMBER);
2966 add_immBlock_pred(break_block, proj);
2969 assert(current_switch_cond == cond);
2970 assert(break_label == break_block);
2971 current_switch_cond = old_switch_cond;
2972 break_label = old_break_label;
2973 saw_default_label = old_saw_default_label;
2975 mature_immBlock(break_block);
2976 set_cur_block(break_block);
2979 static void case_label_to_firm(const case_label_statement_t *statement)
2981 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2983 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2985 /* let's create a node and hope firm constant folding creates a Const
2988 set_cur_block(get_nodes_block(current_switch_cond));
2989 if(statement->expression) {
2990 long pn = fold_constant(statement->expression);
2991 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2992 /* oops someone detected our cheating... */
2993 panic("magic default pn used");
2995 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2997 saw_default_label = true;
2998 proj = new_d_defaultProj(dbgi, current_switch_cond,
2999 MAGIC_DEFAULT_PN_NUMBER);
3002 ir_node *block = new_immBlock();
3003 if (fallthrough != NULL) {
3004 add_immBlock_pred(block, fallthrough);
3006 add_immBlock_pred(block, proj);
3007 mature_immBlock(block);
3009 if(statement->label_statement != NULL) {
3010 statement_to_firm(statement->label_statement);
3014 static ir_node *get_label_block(declaration_t *label)
3016 assert(label->namespc == NAMESPACE_LABEL);
3018 if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
3019 return label->v.block;
3021 assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
3023 ir_node *old_cur_block = get_cur_block();
3024 ir_node *block = new_immBlock();
3025 set_cur_block(old_cur_block);
3027 label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
3028 label->v.block = block;
3030 ARR_APP1(ir_node *, imature_blocks, block);
3035 static void label_to_firm(const label_statement_t *statement)
3037 ir_node *block = get_label_block(statement->label);
3039 if(get_cur_block() != NULL) {
3040 ir_node *jmp = new_Jmp();
3041 add_immBlock_pred(block, jmp);
3044 set_cur_block(block);
3047 if(statement->label_statement != NULL) {
3048 statement_to_firm(statement->label_statement);
3052 static void goto_to_firm(const goto_statement_t *statement)
3054 if(get_cur_block() == NULL)
3057 ir_node *block = get_label_block(statement->label);
3058 ir_node *jmp = new_Jmp();
3059 add_immBlock_pred(block, jmp);
3061 set_cur_block(NULL);
3064 typedef enum modifier_t {
3065 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
3066 ASM_MODIFIER_READ_WRITE = 1 << 1,
3067 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
3068 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
3072 static void asm_statement_to_firm(const asm_statement_t *statement)
3074 bool needs_memory = false;
3076 size_t n_clobbers = 0;
3077 asm_clobber_t *clobber = statement->clobbers;
3078 for( ; clobber != NULL; clobber = clobber->next) {
3079 if(strcmp(clobber->clobber, "memory") == 0) {
3080 needs_memory = true;
3084 ident *id = new_id_from_str(clobber->clobber);
3085 obstack_ptr_grow(&asm_obst, id);
3088 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
3089 ident **clobbers = NULL;
3090 if(n_clobbers > 0) {
3091 clobbers = obstack_finish(&asm_obst);
3094 /* find and count input and output constraints */
3095 asm_constraint_t *constraint = statement->inputs;
3096 for( ; constraint != NULL; constraint = constraint->next) {
3098 bool supports_memop = false;
3099 for(const char *c = constraint->constraints; *c != 0; ++c) {
3100 /* TODO: improve error messages */
3104 panic("multiple alternative assembler constraints not "
3112 supports_memop = true;
3113 obstack_1grow(&asm_obst, *c);
3116 if(modifiers & ASM_MODIFIER_READ_WRITE)
3117 panic("inconsistent register constraints");
3118 modifiers |= ASM_MODIFIER_WRITE_ONLY;
3121 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
3122 panic("inconsistent register constraints");
3123 modifiers |= ASM_MODIFIER_READ_WRITE;
3126 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
3127 panic("early clobber assembler constraint not supported yet");
3130 modifiers |= ASM_MODIFIER_COMMUTATIVE;
3131 panic("commutative assembler constraint not supported yet");
3134 /* skip register preferences stuff... */
3135 while(*c != 0 && *c != ',')
3139 /* skip register preferences stuff... */
3143 obstack_1grow(&asm_obst, *c);
3147 obstack_1grow(&asm_obst, '\0');
3148 const char *constraint_string = obstack_finish(&asm_obst);
3150 needs_memory |= supports_memop;
3151 if(supports_memop) {
3159 static void statement_to_firm(statement_t *statement)
3161 switch(statement->type) {
3162 case STATEMENT_INVALID:
3163 panic("invalid statement found");
3164 case STATEMENT_COMPOUND:
3165 compound_statement_to_firm(&statement->compound);
3167 case STATEMENT_RETURN:
3168 return_statement_to_firm(&statement->returns);
3170 case STATEMENT_EXPRESSION:
3171 expression_statement_to_firm(&statement->expression);
3174 if_statement_to_firm(&statement->ifs);
3176 case STATEMENT_WHILE:
3177 while_statement_to_firm(&statement->whiles);
3179 case STATEMENT_DO_WHILE:
3180 do_while_statement_to_firm(&statement->do_while);
3182 case STATEMENT_DECLARATION:
3183 declaration_statement_to_firm(&statement->declaration);
3185 case STATEMENT_BREAK:
3186 create_jump_statement(statement, break_label);
3188 case STATEMENT_CONTINUE:
3189 create_jump_statement(statement, continue_label);
3191 case STATEMENT_SWITCH:
3192 switch_statement_to_firm(&statement->switchs);
3194 case STATEMENT_CASE_LABEL:
3195 case_label_to_firm(&statement->case_label);
3198 for_statement_to_firm(&statement->fors);
3200 case STATEMENT_LABEL:
3201 label_to_firm(&statement->label);
3203 case STATEMENT_GOTO:
3204 goto_to_firm(&statement->gotos);
3207 //asm_statement_to_firm(&statement->asms);
3211 panic("Statement not implemented\n");
3214 static int count_decls_in_expression(const expression_t *expression);
3216 static int count_local_declarations(const declaration_t * decl,
3217 const declaration_t *const end)
3220 for (; decl != end; decl = decl->next) {
3221 const type_t *type = skip_typeref(decl->type);
3222 switch (type->type) {
3226 if (!decl->address_taken)
3232 const initializer_t *initializer = decl->init.initializer;
3233 /* FIXME: should walk initializer hierarchies... */
3234 if(initializer != NULL && initializer->type == INITIALIZER_VALUE) {
3235 count += count_decls_in_expression(initializer->value.value);
3241 static int count_decls_in_expression(const expression_t *expression) {
3242 if(expression == NULL)
3245 switch(expression->base.type) {
3246 case EXPR_STATEMENT:
3247 return count_decls_in_stmts(expression->statement.statement);
3249 return count_decls_in_expression(expression->binary.left)
3250 + count_decls_in_expression(expression->binary.right);
3252 return count_decls_in_expression(expression->unary.value);
3258 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
3259 * (or implement all the missing expressions here/implement a walker)
3265 static int count_decls_in_stmts(const statement_t *stmt)
3268 for (; stmt != NULL; stmt = stmt->base.next) {
3269 switch (stmt->type) {
3270 case STATEMENT_DECLARATION: {
3271 const declaration_statement_t *const decl_stmt = &stmt->declaration;
3272 count += count_local_declarations(decl_stmt->declarations_begin,
3273 decl_stmt->declarations_end->next);
3277 case STATEMENT_COMPOUND: {
3278 const compound_statement_t *const comp =
3279 (const compound_statement_t*)stmt;
3280 count += count_decls_in_stmts(comp->statements);
3284 case STATEMENT_IF: {
3285 const if_statement_t *const if_stmt = &stmt->ifs;
3286 count += count_decls_in_expression(if_stmt->condition);
3287 count += count_decls_in_stmts(if_stmt->true_statement);
3288 count += count_decls_in_stmts(if_stmt->false_statement);
3292 case STATEMENT_SWITCH: {
3293 const switch_statement_t *const switch_stmt = &stmt->switchs;
3294 count += count_decls_in_expression(switch_stmt->expression);
3295 count += count_decls_in_stmts(switch_stmt->body);
3299 case STATEMENT_LABEL: {
3300 const label_statement_t *const label_stmt = &stmt->label;
3301 count += count_decls_in_stmts(label_stmt->label_statement);
3305 case STATEMENT_WHILE: {
3306 const while_statement_t *const while_stmt = &stmt->whiles;
3307 count += count_decls_in_expression(while_stmt->condition);
3308 count += count_decls_in_stmts(while_stmt->body);
3312 case STATEMENT_DO_WHILE: {
3313 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
3314 count += count_decls_in_expression(do_while_stmt->condition);
3315 count += count_decls_in_stmts(do_while_stmt->body);
3319 case STATEMENT_FOR: {
3320 const for_statement_t *const for_stmt = &stmt->fors;
3321 count += count_local_declarations(for_stmt->context.declarations, NULL);
3322 count += count_decls_in_expression(for_stmt->initialisation);
3323 count += count_decls_in_expression(for_stmt->condition);
3324 count += count_decls_in_expression(for_stmt->step);
3325 count += count_decls_in_stmts(for_stmt->body);
3329 case STATEMENT_CASE_LABEL: {
3330 const case_label_statement_t *label = &stmt->case_label;
3331 count += count_decls_in_expression(label->expression);
3332 count += count_decls_in_stmts(label->label_statement);
3337 case STATEMENT_BREAK:
3338 case STATEMENT_CONTINUE:
3341 case STATEMENT_EXPRESSION: {
3342 const expression_statement_t *expr_stmt = &stmt->expression;
3343 count += count_decls_in_expression(expr_stmt->expression);
3347 case STATEMENT_GOTO:
3348 case STATEMENT_INVALID:
3351 case STATEMENT_RETURN: {
3352 const return_statement_t *ret_stmt = &stmt->returns;
3353 count += count_decls_in_expression(ret_stmt->return_value);
3361 static int get_function_n_local_vars(declaration_t *declaration)
3365 /* count parameters */
3366 count += count_local_declarations(declaration->context.declarations, NULL);
3368 /* count local variables declared in body */
3369 count += count_decls_in_stmts(declaration->init.statement);
3374 static void initialize_function_parameters(declaration_t *declaration)
3376 ir_graph *irg = current_ir_graph;
3377 ir_node *args = get_irg_args(irg);
3378 ir_node *start_block = get_irg_start_block(irg);
3379 ir_type *function_irtype = get_ir_type(declaration->type);
3382 declaration_t *parameter = declaration->context.declarations;
3383 for( ; parameter != NULL; parameter = parameter->next, ++n) {
3384 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
3385 type_t *type = skip_typeref(parameter->type);
3387 bool needs_entity = parameter->address_taken;
3388 assert(!is_type_array(type));
3389 if(is_type_compound(type)) {
3390 needs_entity = true;
3394 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3395 ident *id = new_id_from_str(parameter->symbol->string);
3396 set_entity_ident(entity, id);
3398 parameter->declaration_type
3399 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
3400 parameter->v.entity = entity;
3404 ir_mode *mode = get_ir_mode(parameter->type);
3406 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3408 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
3409 parameter->v.value_number = next_value_number_function;
3410 ++next_value_number_function;
3412 set_value(parameter->v.value_number, proj);
3417 * Handle additional decl modifiers for IR-graphs
3419 * @param irg the IR-graph
3420 * @param dec_modifiers additional modifiers
3422 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
3424 if (decl_modifiers & DM_NORETURN) {
3425 /* TRUE if the declaration includes the Microsoft
3426 __declspec(noreturn) specifier. */
3427 set_irg_additional_property(irg, mtp_property_noreturn);
3429 if (decl_modifiers & DM_NOTHROW) {
3430 /* TRUE if the declaration includes the Microsoft
3431 __declspec(nothrow) specifier. */
3432 set_irg_additional_property(irg, mtp_property_nothrow);
3434 if (decl_modifiers & DM_NAKED) {
3435 /* TRUE if the declaration includes the Microsoft
3436 __declspec(naked) specifier. */
3437 set_irg_additional_property(irg, mtp_property_naked);
3439 if (decl_modifiers & DM_FORCEINLINE) {
3440 /* TRUE if the declaration includes the
3441 Microsoft __forceinline specifier. */
3442 set_irg_inline_property(irg, irg_inline_forced);
3444 if (decl_modifiers & DM_NOINLINE) {
3445 /* TRUE if the declaration includes the Microsoft
3446 __declspec(noinline) specifier. */
3447 set_irg_inline_property(irg, irg_inline_forbidden);
3451 static void create_function(declaration_t *declaration)
3453 ir_entity *function_entity = get_function_entity(declaration);
3455 if(declaration->init.statement == NULL)
3458 current_function_decl = declaration;
3459 current_function_name = NULL;
3461 assert(imature_blocks == NULL);
3462 imature_blocks = NEW_ARR_F(ir_node*, 0);
3464 int n_local_vars = get_function_n_local_vars(declaration);
3465 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
3466 ir_node *first_block = get_cur_block();
3468 /* set inline flags */
3469 if (declaration->is_inline)
3470 set_irg_inline_property(irg, irg_inline_recomended);
3471 handle_decl_modifier_irg(irg, declaration->decl_modifiers);
3473 next_value_number_function = 0;
3474 initialize_function_parameters(declaration);
3476 statement_to_firm(declaration->init.statement);
3478 ir_node *end_block = get_irg_end_block(irg);
3480 /* do we have a return statement yet? */
3481 if(get_cur_block() != NULL) {
3482 type_t *type = skip_typeref(declaration->type);
3483 assert(is_type_function(type));
3484 const function_type_t *func_type = &type->function;
3485 const type_t *return_type
3486 = skip_typeref(func_type->return_type);
3489 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3490 ret = new_Return(get_store(), 0, NULL);
3493 if(is_type_scalar(return_type)) {
3494 mode = get_ir_mode(func_type->return_type);
3500 /* ยง5.1.2.2.3 main implicitly returns 0 */
3501 if (strcmp(declaration->symbol->string, "main") == 0) {
3502 in[0] = new_Const(mode, get_mode_null(mode));
3504 in[0] = new_Unknown(mode);
3506 ret = new_Return(get_store(), 1, in);
3508 add_immBlock_pred(end_block, ret);
3511 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3512 mature_immBlock(imature_blocks[i]);
3514 DEL_ARR_F(imature_blocks);
3515 imature_blocks = NULL;
3517 mature_immBlock(first_block);
3518 mature_immBlock(end_block);
3520 irg_finalize_cons(irg);
3522 /* finalize the frame type */
3523 ir_type *frame_type = get_irg_frame_type(irg);
3524 int n = get_compound_n_members(frame_type);
3527 for(int i = 0; i < n; ++i) {
3528 ir_entity *entity = get_compound_member(frame_type, i);
3529 ir_type *entity_type = get_entity_type(entity);
3531 int align = get_type_alignment_bytes(entity_type);
3532 if(align > align_all)
3536 misalign = offset % align;
3538 offset += align - misalign;
3542 set_entity_offset(entity, offset);
3543 offset += get_type_size_bytes(entity_type);
3545 set_type_size_bytes(frame_type, offset);
3546 set_type_alignment_bytes(frame_type, align_all);
3547 set_type_state(frame_type, layout_fixed);
3552 static void create_global_variable(declaration_t *declaration)
3556 switch ((storage_class_tag_t)declaration->storage_class) {
3557 case STORAGE_CLASS_STATIC:
3558 vis = visibility_local;
3561 case STORAGE_CLASS_EXTERN:
3562 vis = visibility_external_allocated;
3565 case STORAGE_CLASS_NONE:
3566 vis = visibility_external_visible;
3569 case STORAGE_CLASS_THREAD:
3570 vis = visibility_external_visible;
3573 case STORAGE_CLASS_THREAD_EXTERN:
3574 vis = visibility_external_allocated;
3577 case STORAGE_CLASS_THREAD_STATIC:
3578 vis = visibility_local;
3582 var_type = get_tls_type();
3586 var_type = get_glob_type();
3590 create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3592 set_entity_visibility(declaration->v.entity, vis);
3594 current_ir_graph = get_const_code_irg();
3595 create_initializer(declaration);
3598 case STORAGE_CLASS_TYPEDEF:
3599 case STORAGE_CLASS_AUTO:
3600 case STORAGE_CLASS_REGISTER:
3601 case STORAGE_CLASS_ENUM_ENTRY:
3604 panic("Invalid storage class for global variable");
3607 static void context_to_firm(context_t *context)
3609 /* first pass: create declarations */
3610 declaration_t *declaration = context->declarations;
3611 for( ; declaration != NULL; declaration = declaration->next) {
3612 if(declaration->namespc != NAMESPACE_NORMAL)
3614 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3615 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3617 if(declaration->symbol == NULL)
3620 type_t *type = skip_typeref(declaration->type);
3621 if(is_type_function(type)) {
3622 get_function_entity(declaration);
3624 create_global_variable(declaration);
3628 /* second pass: create code */
3629 declaration = context->declarations;
3630 for( ; declaration != NULL; declaration = declaration->next) {
3631 if(declaration->namespc != NAMESPACE_NORMAL)
3633 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3634 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3636 if(declaration->symbol == NULL)
3639 type_t *type = declaration->type;
3640 if(type->type != TYPE_FUNCTION)
3643 create_function(declaration);
3647 void init_ast2firm(void)
3649 obstack_init(&asm_obst);
3650 init_atomic_modes();
3652 /* create idents for all known runtime functions */
3653 for (int i = 0; i < sizeof(rts_data)/sizeof(rts_data[0]); ++i) {
3654 predef_idents[rts_data[i].id] = new_id_from_str(rts_data[i].name);
3658 void exit_ast2firm(void)
3660 obstack_free(&asm_obst, NULL);
3663 void translation_unit_to_firm(translation_unit_t *unit)
3665 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3666 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3667 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
3669 ir_type_int = get_ir_type(type_int);
3670 ir_type_const_char = get_ir_type(type_const_char);
3671 ir_type_wchar_t = get_ir_type(type_wchar_t);
3672 ir_type_void = get_ir_type(type_int); /* we don't have a real void
3675 type_void->base.firm_type = ir_type_void;
3677 /* just to be sure */
3678 continue_label = NULL;
3680 current_switch_cond = NULL;
3682 context_to_firm(&unit->context);