2c53676f1c164b24ab4ec1238f32f86a8492743c
[cparser] / ast2firm.c
1 #include <config.h>
2
3 #define _GNU_SOURCE
4
5 #include <assert.h>
6 #include <string.h>
7 #include <stdbool.h>
8
9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
11
12 #include "ast2firm.h"
13
14 #include "adt/error.h"
15 #include "adt/array.h"
16 #include "token_t.h"
17 #include "type_t.h"
18 #include "ast_t.h"
19 #include "parser.h"
20
21 #define MAGIC_DEFAULT_PN_NUMBER     (long) -314159265
22
23 static ir_type *ir_type_const_char;
24 static ir_type *ir_type_wchar_t;
25 static ir_type *ir_type_void;
26 static ir_type *ir_type_int;
27
28 static type_t *type_const_char;
29 static type_t *type_void;
30 static type_t *type_int;
31
32 static int       next_value_number_function;
33 static ir_node  *continue_label;
34 static ir_node  *break_label;
35 static ir_node  *current_switch_cond;
36 static bool      saw_default_label;
37 static ir_node **imature_blocks;
38
39 static const declaration_t *current_function_decl;
40 static ir_node             *current_function_name;
41
42 typedef enum declaration_type_t {
43         DECLARATION_TYPE_UNKNOWN,
44         DECLARATION_TYPE_FUNCTION,
45         DECLARATION_TYPE_GLOBAL_VARIABLE,
46         DECLARATION_TYPE_LOCAL_VARIABLE,
47         DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
48         DECLARATION_TYPE_COMPOUND_MEMBER,
49         DECLARATION_TYPE_LABEL_BLOCK,
50         DECLARATION_TYPE_ENUM_ENTRY
51 } declaration_type_t;
52
53 static ir_type *get_ir_type(type_t *type);
54
55 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
56 {
57         (void) pos;
58 #if 0
59         const declaration_t *declaration = & value_numbers[pos]->declaration;
60
61         print_warning_prefix(declaration->source_position);
62         fprintf(stderr, "variable '%s' might be used uninitialized\n",
63                         declaration->symbol->string);
64 #endif
65         fprintf(stderr, "Some variable might be used uninitialized\n");
66         return new_r_Unknown(irg, mode);
67 }
68
69 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
70 {
71         const source_position_t *pos = (const source_position_t*) dbg;
72         if(pos == NULL)
73                 return 0;
74         return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
75                                    pos->linenr);
76 }
77
78 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
79 {
80         const source_position_t *pos = (const source_position_t*) dbg;
81         if(pos == NULL)
82                 return NULL;
83         if(line != NULL)
84                 *line = pos->linenr;
85         return pos->input_name;
86 }
87
88 void init_ast2firm(void)
89 {
90 }
91
92 void exit_ast2firm(void)
93 {
94 }
95
96 static unsigned unique_id = 0;
97
98 static ident *unique_ident(const char *tag)
99 {
100         char buf[256];
101
102         snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
103         unique_id++;
104         return new_id_from_str(buf);
105 }
106
107 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
108 {
109         switch(atomic_type->atype) {
110         case ATOMIC_TYPE_SCHAR:
111         case ATOMIC_TYPE_CHAR:
112                 return mode_Bs;
113         case ATOMIC_TYPE_UCHAR:
114                 return mode_Bu;
115         case ATOMIC_TYPE_SHORT:
116                 return mode_Hs;
117         case ATOMIC_TYPE_USHORT:
118                 return mode_Hu;
119         case ATOMIC_TYPE_LONG:
120         case ATOMIC_TYPE_INT:
121                 return mode_Is;
122         case ATOMIC_TYPE_ULONG:
123         case ATOMIC_TYPE_UINT:
124                 return mode_Iu;
125         case ATOMIC_TYPE_LONGLONG:
126                 return mode_Ls;
127         case ATOMIC_TYPE_ULONGLONG:
128                 return mode_Lu;
129         case ATOMIC_TYPE_FLOAT:
130                 return mode_F;
131         case ATOMIC_TYPE_DOUBLE:
132                 return mode_D;
133         case ATOMIC_TYPE_LONG_DOUBLE:
134                 return mode_E;
135         case ATOMIC_TYPE_BOOL:
136                 return mode_b;
137 #ifdef PROVIDE_COMPLEX
138         case ATOMIC_TYPE_FLOAT_COMPLEX:
139         case ATOMIC_TYPE_DOUBLE_COMPLEX:
140         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
141                 panic("complex lowering not implemented yet");
142                 break;
143         case ATOMIC_TYPE_FLOAT_IMAGINARY:
144         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
145         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
146                 panic("imaginary lowering not implemented yet");
147                 break;
148 #endif
149         case ATOMIC_TYPE_VOID:
150                 /* firm has no real void... */
151                 return mode_Is;
152         case ATOMIC_TYPE_INVALID:
153                 break;
154         }
155         panic("Encountered unknown atomic type");
156 }
157
158
159 static unsigned get_type_size(type_t *type);
160
161 static unsigned get_atomic_type_size(const atomic_type_t *type)
162 {
163         switch(type->atype) {
164         case ATOMIC_TYPE_CHAR:
165         case ATOMIC_TYPE_SCHAR:
166         case ATOMIC_TYPE_UCHAR:
167                 return 1;
168
169         case ATOMIC_TYPE_SHORT:
170         case ATOMIC_TYPE_USHORT:
171                 return 2;
172
173         case ATOMIC_TYPE_BOOL:
174         case ATOMIC_TYPE_INT:
175         case ATOMIC_TYPE_UINT:
176         case ATOMIC_TYPE_LONG:
177         case ATOMIC_TYPE_ULONG:
178         case ATOMIC_TYPE_FLOAT:
179                 return 4;
180
181         case ATOMIC_TYPE_LONGLONG:
182         case ATOMIC_TYPE_ULONGLONG:
183         case ATOMIC_TYPE_DOUBLE:
184                 return 8;
185
186         case ATOMIC_TYPE_LONG_DOUBLE:
187                 return 12;
188
189         case ATOMIC_TYPE_VOID:
190                 return 1;
191
192         case ATOMIC_TYPE_INVALID:
193                 break;
194         }
195         panic("Trying to determine size of invalid atomic type");
196 }
197
198 static unsigned get_compound_type_size(compound_type_t *type)
199 {
200         ir_type *irtype = get_ir_type((type_t*) type);
201         return get_type_size_bytes(irtype);
202 }
203
204 static unsigned get_array_type_size(array_type_t *type)
205 {
206         ir_type *irtype = get_ir_type((type_t*) type);
207         return get_type_size_bytes(irtype);
208 }
209
210 static unsigned get_type_size(type_t *type)
211 {
212         type = skip_typeref(type);
213
214         switch(type->type) {
215         case TYPE_ATOMIC:
216                 return get_atomic_type_size(&type->atomic);
217         case TYPE_ENUM:
218                 return get_mode_size_bytes(mode_Is);
219         case TYPE_COMPOUND_UNION:
220         case TYPE_COMPOUND_STRUCT:
221                 return get_compound_type_size(&type->compound);
222         case TYPE_FUNCTION:
223                 /* just a pointer to the function */
224                 return get_mode_size_bytes(mode_P_code);
225         case TYPE_POINTER:
226                 return get_mode_size_bytes(mode_P_data);
227         case TYPE_ARRAY:
228                 return get_array_type_size(&type->array);
229         case TYPE_BUILTIN:
230                 return get_type_size(type->builtin.real_type);
231         case TYPE_TYPEDEF:
232         case TYPE_TYPEOF:
233         case TYPE_INVALID:
234                 break;
235         }
236         panic("Trying to determine size of invalid type");
237 }
238
239 static unsigned count_parameters(const function_type_t *function_type)
240 {
241         unsigned count = 0;
242
243         function_parameter_t *parameter = function_type->parameters;
244         for ( ; parameter != NULL; parameter = parameter->next) {
245                 ++count;
246         }
247
248         return count;
249 }
250
251
252
253
254 static long fold_constant(const expression_t *expression);
255
256 static ir_type *create_atomic_type(const atomic_type_t *type)
257 {
258         ir_mode *mode   = get_atomic_mode(type);
259         ident   *id     = get_mode_ident(mode);
260         ir_type *irtype = new_type_primitive(id, mode);
261
262         return irtype;
263 }
264
265 static ir_type *create_method_type(const function_type_t *function_type)
266 {
267         type_t  *return_type  = function_type->return_type;
268
269         ident   *id           = unique_ident("functiontype");
270         int      n_parameters = count_parameters(function_type);
271         int      n_results    = return_type == type_void ? 0 : 1;
272         ir_type *irtype       = new_type_method(id, n_parameters, n_results);
273
274         if(return_type != type_void) {
275                 ir_type *restype = get_ir_type(return_type);
276                 set_method_res_type(irtype, 0, restype);
277         }
278
279         function_parameter_t *parameter = function_type->parameters;
280         int                   n         = 0;
281         for( ; parameter != NULL; parameter = parameter->next) {
282                 ir_type *p_irtype = get_ir_type(parameter->type);
283                 set_method_param_type(irtype, n, p_irtype);
284                 ++n;
285         }
286
287         if(function_type->variadic || function_type->unspecified_parameters) {
288                 set_method_variadicity(irtype, variadicity_variadic);
289         }
290
291         return irtype;
292 }
293
294 static ir_type *create_pointer_type(pointer_type_t *type)
295 {
296         type_t  *points_to = type->points_to;
297         ir_type *ir_points_to;
298         /* Avoid endless recursion if the points_to type contains this poiner type
299          * again (might be a struct). We therefore first create a void* pointer
300          * and then set the real points_to type
301          */
302         ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
303                                             ir_type_void, mode_P_data);
304         type->type.firm_type  = ir_type;
305
306         ir_points_to = get_ir_type(points_to);
307         set_pointer_points_to_type(ir_type, ir_points_to);
308
309         return ir_type;
310 }
311
312 static ir_type *create_array_type(array_type_t *type)
313 {
314         type_t  *element_type    = type->element_type;
315         ir_type *ir_element_type = get_ir_type(element_type);
316
317         ident   *id      = unique_ident("array");
318         ir_type *ir_type = new_type_array(id, 1, ir_element_type);
319
320         if(type->size != NULL) {
321                 int n_elements = fold_constant(type->size);
322
323                 set_array_bounds_int(ir_type, 0, 0, n_elements);
324
325                 size_t elemsize = get_type_size_bytes(ir_element_type);
326                 int align = get_type_alignment_bytes(ir_element_type);
327                 if(elemsize % align > 0) {
328                         elemsize += align - (elemsize % align);
329                 }
330                 set_type_size_bytes(ir_type, n_elements * elemsize);
331                 set_type_alignment_bytes(ir_type, align);
332                 set_type_state(ir_type, layout_fixed);
333         }
334
335         return ir_type;
336 }
337
338 #define INVALID_TYPE ((ir_type_ptr)-1)
339
340 static ir_type *create_struct_type(compound_type_t *type)
341 {
342         symbol_t *symbol = type->declaration->symbol;
343         ident    *id;
344         if(symbol != NULL) {
345                 id = unique_ident(symbol->string);
346         } else {
347                 id = unique_ident("__anonymous_struct");
348         }
349         ir_type *ir_type = new_type_struct(id);
350
351         type->type.firm_type = ir_type;
352
353         int align_all = 1;
354         int offset    = 0;
355         declaration_t *entry = type->declaration->context.declarations;
356         for( ; entry != NULL; entry = entry->next) {
357                 if(entry->namespc != NAMESPACE_NORMAL)
358                         continue;
359
360                 ident       *ident         = new_id_from_str(entry->symbol->string);
361                 ir_type_ptr  entry_ir_type = get_ir_type(entry->type);
362
363                 int entry_size      = get_type_size_bytes(entry_ir_type);
364                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
365                 int misalign        = offset % entry_alignment;
366                 if (misalign != 0)
367                         offset += entry_alignment - misalign;
368
369                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
370                 set_entity_offset(entity, offset);
371                 add_struct_member(ir_type, entity);
372                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
373                 entry->v.entity         = entity;
374
375                 offset += entry_size;
376                 if(entry_alignment > align_all) {
377                         if(entry_alignment % align_all != 0) {
378                                 panic("Uneven alignments not supported yet");
379                         }
380                         align_all = entry_alignment;
381                 }
382         }
383
384         int misalign = offset % align_all;
385         offset += misalign;
386         set_type_alignment_bytes(ir_type, align_all);
387         set_type_size_bytes(ir_type, offset);
388         set_type_state(ir_type, layout_fixed);
389
390         return ir_type;
391 }
392
393 static ir_type *create_union_type(compound_type_t *type)
394 {
395         declaration_t *declaration = type->declaration;
396         symbol_t      *symbol      = declaration->symbol;
397         ident         *id;
398         if(symbol != NULL) {
399                 id = unique_ident(symbol->string);
400         } else {
401                 id = unique_ident("__anonymous_union");
402         }
403         ir_type  *ir_type = new_type_union(id);
404
405         type->type.firm_type = ir_type;
406
407         int align_all = 1;
408         int size      = 0;
409         declaration_t *entry = declaration->context.declarations;
410         for( ; entry != NULL; entry = entry->next) {
411                 if(entry->namespc != NAMESPACE_NORMAL)
412                         continue;
413
414                 ident       *ident         = new_id_from_str(entry->symbol->string);
415                 ir_type_ptr  entry_ir_type = get_ir_type(entry->type);
416
417                 int entry_size      = get_type_size_bytes(entry_ir_type);
418                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
419
420                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
421                 add_union_member(ir_type, entity);
422                 set_entity_offset(entity, 0);
423                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
424                 entry->v.entity         = entity;
425
426                 if(entry_size > size) {
427                         size = entry_size;
428                 }
429                 if(entry_alignment > align_all) {
430                         if(entry_alignment % align_all != 0) {
431                                 panic("Uneven alignments not supported yet");
432                         }
433                         align_all = entry_alignment;
434                 }
435         }
436
437         set_type_alignment_bytes(ir_type, align_all);
438         set_type_size_bytes(ir_type, size);
439         set_type_state(ir_type, layout_fixed);
440
441         return ir_type;
442 }
443
444 static ir_node *expression_to_firm(const expression_t *expression);
445 static inline ir_mode *get_ir_mode(type_t *type);
446
447 static ir_type *create_enum_type(enum_type_t *const type)
448 {
449         type->type.firm_type = ir_type_int;
450
451         ir_mode *const mode    = get_ir_mode((type_t*) type);
452         tarval  *const one     = get_mode_one(mode);
453         tarval  *      tv_next = get_tarval_null(mode);
454
455         declaration_t *declaration = type->declaration->next;
456         for (; declaration != NULL; declaration = declaration->next) {
457                 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
458                         break;
459
460                 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
461
462                 expression_t *const init = declaration->init.enum_value;
463                 if (init != NULL) {
464                         ir_node *const cnst = expression_to_firm(init);
465                         if (!is_Const(cnst)) {
466                                 panic("couldn't fold constant");
467                         }
468                         tv_next = get_Const_tarval(cnst);
469                 }
470                 declaration->v.enum_val = tv_next;
471                 tv_next = tarval_add(tv_next, one);
472         }
473
474         return ir_type_int;
475 }
476
477 static ir_type *get_ir_type(type_t *type)
478 {
479         assert(type != NULL);
480
481         type = skip_typeref(type);
482
483         if(type->base.firm_type != NULL) {
484                 assert(type->base.firm_type != INVALID_TYPE);
485                 return type->base.firm_type;
486         }
487
488         ir_type *firm_type = NULL;
489         switch(type->type) {
490         case TYPE_ATOMIC:
491                 firm_type = create_atomic_type(&type->atomic);
492                 break;
493         case TYPE_FUNCTION:
494                 firm_type = create_method_type(&type->function);
495                 break;
496         case TYPE_POINTER:
497                 firm_type = create_pointer_type(&type->pointer);
498                 break;
499         case TYPE_ARRAY:
500                 firm_type = create_array_type(&type->array);
501                 break;
502         case TYPE_COMPOUND_STRUCT:
503                 firm_type = create_struct_type(&type->compound);
504                 break;
505         case TYPE_COMPOUND_UNION:
506                 firm_type = create_union_type(&type->compound);
507                 break;
508         case TYPE_ENUM:
509                 firm_type = create_enum_type(&type->enumt);
510                 break;
511         case TYPE_BUILTIN:
512                 firm_type = get_ir_type(type->builtin.real_type);
513                 break;
514         case TYPE_TYPEOF:
515         case TYPE_TYPEDEF:
516         case TYPE_INVALID:
517                 break;
518         }
519         if(firm_type == NULL)
520                 panic("unknown type found");
521
522         type->base.firm_type = firm_type;
523         return firm_type;
524 }
525
526 static inline ir_mode *get_ir_mode(type_t *type)
527 {
528         ir_type *irtype = get_ir_type(type);
529
530         /* firm doesn't report a mode for arrays somehow... */
531         if(is_Array_type(irtype)) {
532                 return mode_P;
533         }
534
535         ir_mode *mode = get_type_mode(irtype);
536         assert(mode != NULL);
537         return mode;
538 }
539
540 static ir_entity* get_function_entity(declaration_t *declaration)
541 {
542         if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
543                 return declaration->v.entity;
544         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
545
546         symbol_t *symbol = declaration->symbol;
547         ident    *id     = new_id_from_str(symbol->string);
548
549         ir_type  *global_type    = get_glob_type();
550         ir_type  *ir_type_method = get_ir_type(declaration->type);
551         assert(is_Method_type(ir_type_method));
552
553         ir_entity *entity = new_entity(global_type, id, ir_type_method);
554         set_entity_ld_ident(entity, id);
555         if(declaration->storage_class == STORAGE_CLASS_STATIC
556                         || declaration->is_inline) {
557                 set_entity_visibility(entity, visibility_local);
558         } else if(declaration->init.statement != NULL) {
559                 set_entity_visibility(entity, visibility_external_visible);
560         } else {
561                 set_entity_visibility(entity, visibility_external_allocated);
562         }
563
564         declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
565         declaration->v.entity         = entity;
566
567         return entity;
568 }
569
570 static dbg_info *get_dbg_info(const source_position_t *pos)
571 {
572         return (dbg_info*) pos;
573 }
574
575 static ir_node *const_to_firm(const const_expression_t *cnst)
576 {
577         dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
578         ir_mode  *mode = get_ir_mode(cnst->expression.datatype);
579
580         char    buf[128];
581         tarval *tv;
582         size_t  len;
583         if(mode_is_float(mode)) {
584                 tv = new_tarval_from_double(cnst->v.float_value, mode);
585         } else {
586                 if(mode_is_signed(mode)) {
587                         len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
588                 } else {
589                         len = snprintf(buf, sizeof(buf), "%llu",
590                                        (unsigned long long) cnst->v.int_value);
591                 }
592                 tv = new_tarval_from_str(buf, len, mode);
593         }
594
595         return new_d_Const(dbgi, mode, tv);
596 }
597
598 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
599 {
600         assert(entity != NULL);
601         union symconst_symbol sym;
602         sym.entity_p = entity;
603         return new_d_SymConst(dbgi, sym, symconst_addr_ent);
604 }
605
606 static ir_node *string_to_firm(const source_position_t *const src_pos,
607                                const char *const id_prefix,
608                                const char *const string)
609 {
610         ir_type *const global_type = get_glob_type();
611         ir_type *const type        = new_type_array(unique_ident("strtype"), 1,
612                                                     ir_type_const_char);
613
614         ident     *const id     = unique_ident(id_prefix);
615         ir_entity *const entity = new_entity(global_type, id, type);
616         set_entity_ld_ident(entity, id);
617         set_entity_variability(entity, variability_constant);
618
619         ir_type *const elem_type = ir_type_const_char;
620         ir_mode *const mode      = get_type_mode(elem_type);
621
622         const size_t slen = strlen(string) + 1;
623
624         set_array_lower_bound_int(type, 0, 0);
625         set_array_upper_bound_int(type, 0, slen);
626         set_type_size_bytes(type, slen);
627         set_type_state(type, layout_fixed);
628
629         tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
630         for(size_t i = 0; i < slen; ++i) {
631                 tvs[i] = new_tarval_from_long(string[i], mode);
632         }
633
634         set_array_entity_values(entity, tvs, slen);
635         free(tvs);
636
637         dbg_info *const dbgi = get_dbg_info(src_pos);
638
639         return create_symconst(dbgi, entity);
640 }
641
642 static ir_node *string_literal_to_firm(
643                 const string_literal_expression_t* literal)
644 {
645         return string_to_firm(&literal->expression.source_position, "Lstr",
646                               literal->value);
647 }
648
649 static ir_node *wide_string_literal_to_firm(
650         const wide_string_literal_expression_t* const literal)
651 {
652         ir_type *const global_type = get_glob_type();
653         ir_type *const elem_type   = ir_type_wchar_t;
654         ir_type *const type        = new_type_array(unique_ident("strtype"), 1,
655                                                     elem_type);
656
657         ident     *const id     = unique_ident("Lstr");
658         ir_entity *const entity = new_entity(global_type, id, type);
659         set_entity_ld_ident(entity, id);
660         set_entity_variability(entity, variability_constant);
661
662         ir_mode *const mode      = get_type_mode(elem_type);
663
664         const wchar_rep_t *const string = literal->value.begin;
665         const size_t             slen   = literal->value.size;
666
667         set_array_lower_bound_int(type, 0, 0);
668         set_array_upper_bound_int(type, 0, slen);
669         set_type_size_bytes(type, slen);
670         set_type_state(type, layout_fixed);
671
672         tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
673         for(size_t i = 0; i < slen; ++i) {
674                 tvs[i] = new_tarval_from_long(string[i], mode);
675         }
676
677         set_array_entity_values(entity, tvs, slen);
678         free(tvs);
679
680         dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
681
682         return create_symconst(dbgi, entity);
683 }
684
685 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
686                               dbg_info *const dbgi)
687 {
688         if(is_compound_type(irtype) || is_Array_type(irtype)) {
689                 return addr;
690         }
691
692         ir_mode *const mode     = get_type_mode(irtype);
693         ir_node *const memory   = get_store();
694         ir_node *const load     = new_d_Load(dbgi, memory, addr, mode);
695         ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
696         ir_node *const load_res = new_d_Proj(dbgi, load, mode,   pn_Load_res);
697         set_store(load_mem);
698         return load_res;
699 }
700
701 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
702 {
703         ir_mode *mode = get_irn_mode(node);
704
705         if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
706                 return node;
707         if(!mode_is_float(mode))
708                 return node;
709
710         /* check if there is already a Conv */
711         if (get_irn_op(node) == op_Conv) {
712                 /* convert it into a strict Conv */
713                 set_Conv_strict(node, 1);
714                 return node;
715         }
716
717         /* otherwise create a new one */
718         return new_d_strictConv(dbgi, node, mode);
719 }
720
721 static ir_node *get_global_var_address(dbg_info *const dbgi,
722                                        const declaration_t *const decl)
723 {
724         assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
725
726         ir_entity *const entity = decl->v.entity;
727         switch ((storage_class_tag_t)decl->storage_class) {
728                 case STORAGE_CLASS_THREAD:
729                 case STORAGE_CLASS_THREAD_EXTERN:
730                 case STORAGE_CLASS_THREAD_STATIC: {
731                         ir_node *const no_mem = new_NoMem();
732                         ir_node *const tls    = get_irg_tls(current_ir_graph);
733                         return new_d_simpleSel(dbgi, no_mem, tls, entity);
734                 }
735
736                 default:
737                         return create_symconst(dbgi, entity);
738         }
739 }
740
741 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
742 {
743         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
744         declaration_t *declaration = ref->declaration;
745         type_t        *type        = skip_typeref(declaration->type);
746
747         switch((declaration_type_t) declaration->declaration_type) {
748         case DECLARATION_TYPE_UNKNOWN:
749                 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
750                         break;
751                 }
752                 get_ir_type(type);
753                 /* FALLTHROUGH */
754
755         case DECLARATION_TYPE_ENUM_ENTRY: {
756                 ir_mode *const mode = get_ir_mode(type);
757                 return new_Const(mode, declaration->v.enum_val);
758         }
759
760         case DECLARATION_TYPE_LOCAL_VARIABLE: {
761                 ir_mode *mode = get_ir_mode(type);
762                 return get_value(declaration->v.value_number, mode);
763         }
764         case DECLARATION_TYPE_FUNCTION: {
765                 return create_symconst(dbgi, declaration->v.entity);
766         }
767         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
768                 ir_node *const addr   = get_global_var_address(dbgi, declaration);
769                 ir_type *const irtype = get_entity_type(declaration->v.entity);
770                 return deref_address(irtype, addr, dbgi);
771         }
772
773         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
774                 ir_entity *entity = declaration->v.entity;
775                 ir_node   *frame  = get_irg_frame(current_ir_graph);
776                 ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
777                 ir_type   *irtype = get_entity_type(entity);
778                 return deref_address(irtype, sel, dbgi);
779         }
780
781         case DECLARATION_TYPE_COMPOUND_MEMBER:
782         case DECLARATION_TYPE_LABEL_BLOCK:
783                 panic("not implemented reference type");
784         }
785
786         panic("reference to declaration with unknown type found");
787 }
788
789 static ir_node *reference_addr(const reference_expression_t *ref)
790 {
791         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
792         declaration_t *declaration = ref->declaration;
793
794         switch((declaration_type_t) declaration->declaration_type) {
795         case DECLARATION_TYPE_UNKNOWN:
796                 break;
797         case DECLARATION_TYPE_LOCAL_VARIABLE:
798                 panic("local variable without entity has no address");
799         case DECLARATION_TYPE_FUNCTION: {
800                 return create_symconst(dbgi, declaration->v.entity);
801         }
802         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
803                 ir_node *const addr = get_global_var_address(dbgi, declaration);
804                 return addr;
805         }
806         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
807                 ir_entity *entity = declaration->v.entity;
808                 ir_node   *frame  = get_irg_frame(current_ir_graph);
809                 ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
810
811                 return sel;
812         }
813
814         case DECLARATION_TYPE_ENUM_ENTRY:
815                 panic("trying to reference enum entry");
816
817         case DECLARATION_TYPE_COMPOUND_MEMBER:
818         case DECLARATION_TYPE_LABEL_BLOCK:
819                 panic("not implemented reference type");
820         }
821
822         panic("reference to declaration with unknown type found");
823 }
824
825 static ir_node *process_builtin_call(const call_expression_t *call)
826 {
827         dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
828
829         assert(call->function->type == EXPR_BUILTIN_SYMBOL);
830         builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
831
832         type_t *type = skip_typeref(builtin->expression.datatype);
833         assert(is_type_pointer(type));
834
835         type_t   *function_type = skip_typeref(type->pointer.points_to);
836         symbol_t *symbol        = builtin->symbol;
837
838         switch(symbol->ID) {
839         case T___builtin_alloca: {
840                 if(call->arguments == NULL || call->arguments->next != NULL) {
841                         panic("invalid number of parameters on __builtin_alloca");
842                 }
843                 expression_t *argument = call->arguments->expression;
844                 ir_node      *size     = expression_to_firm(argument);
845
846                 ir_node *store  = get_store();
847                 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
848                                               stack_alloc);
849                 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
850                 set_store(proj_m);
851                 ir_node *res    = new_Proj(alloca, mode_P_data, pn_Alloc_res);
852
853                 return res;
854         }
855         case T___builtin_nan:
856         case T___builtin_nanf:
857         case T___builtin_nand: {
858                 /* Ignore string for now... */
859                 assert(is_type_function(function_type));
860                 ir_mode *mode = get_ir_mode(function_type->function.return_type);
861                 tarval  *tv   = get_mode_NAN(mode);
862                 ir_node *res  = new_d_Const(dbgi, mode, tv);
863                 return res;
864         }
865         case T___builtin_va_end:
866                 return NULL;
867         default:
868                 panic("Unsupported builtin found\n");
869         }
870 }
871
872 static ir_node *call_expression_to_firm(const call_expression_t *call)
873 {
874         assert(get_cur_block() != NULL);
875
876         expression_t *function = call->function;
877         if(function->type == EXPR_BUILTIN_SYMBOL) {
878                 return process_builtin_call(call);
879         }
880         ir_node *callee = expression_to_firm(function);
881
882         type_t *type = skip_typeref(function->base.datatype);
883         assert(is_type_pointer(type));
884         pointer_type_t *pointer_type = &type->pointer;
885         type_t         *points_to    = skip_typeref(pointer_type->points_to);
886         assert(is_type_function(points_to));
887         function_type_t *function_type = &points_to->function;
888
889         int              n_parameters = 0;
890         call_argument_t *argument     = call->arguments;
891         for( ; argument != NULL; argument = argument->next) {
892                 ++n_parameters;
893         }
894
895         dbg_info *dbgi  = get_dbg_info(&call->expression.source_position);
896
897         ir_type *ir_method_type  = get_ir_type((type_t*) function_type);
898         ir_type *new_method_type = NULL;
899         if(function_type->variadic || function_type->unspecified_parameters) {
900                 /* we need to construct a new method type matching the call
901                  * arguments... */
902                 int n_res       = get_method_n_ress(ir_method_type);
903                 new_method_type = new_type_method(unique_ident("calltype"),
904                                                   n_parameters, n_res);
905                 set_method_calling_convention(new_method_type,
906                                get_method_calling_convention(ir_method_type));
907                 set_method_additional_properties(new_method_type,
908                                get_method_additional_properties(ir_method_type));
909
910                 for(int i = 0; i < n_res; ++i) {
911                         set_method_res_type(new_method_type, i,
912                                             get_method_res_type(ir_method_type, i));
913                 }
914         }
915         ir_node *in[n_parameters];
916
917         argument = call->arguments;
918         int n = 0;
919         for( ; argument != NULL; argument = argument->next) {
920                 expression_t *expression = argument->expression;
921                 ir_node      *arg_node   = expression_to_firm(expression);
922
923                 arg_node = do_strict_conv(dbgi, arg_node);
924
925                 in[n] = arg_node;
926                 if(new_method_type != NULL) {
927                         ir_type *irtype = get_ir_type(expression->base.datatype);
928                         set_method_param_type(new_method_type, n, irtype);
929                 }
930
931                 n++;
932         }
933         assert(n == n_parameters);
934
935         if(new_method_type != NULL)
936                 ir_method_type = new_method_type;
937
938         ir_node  *store = get_store();
939         ir_node  *node  = new_d_Call(dbgi, store, callee, n_parameters, in,
940                                      ir_method_type);
941         ir_node  *mem   = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
942         set_store(mem);
943
944         type_t  *return_type = skip_typeref(function_type->return_type);
945         ir_node *result      = NULL;
946
947         if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
948                 ir_mode *mode;
949                 if(is_type_scalar(return_type)) {
950                         mode = get_ir_mode(return_type);
951                 } else {
952                         mode = mode_P_data;
953                 }
954                 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
955                 result           = new_d_Proj(dbgi, resproj, mode, 0);
956         }
957
958         return result;
959 }
960
961 static void statement_to_firm(statement_t *statement);
962 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
963
964 static ir_node *expression_to_addr(const expression_t *expression);
965 static void create_condition_evaluation(const expression_t *expression,
966                                         ir_node *true_block,
967                                         ir_node *false_block);
968
969 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
970                          ir_node *value)
971 {
972         value = do_strict_conv(dbgi, value);
973
974         ir_node  *memory = get_store();
975
976         if(is_type_scalar(type)) {
977                 ir_node  *store     = new_d_Store(dbgi, memory, addr, value);
978                 ir_node  *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
979                 set_store(store_mem);
980         } else {
981                 ir_type *irtype    = get_ir_type(type);
982                 ir_node *copyb     = new_d_CopyB(dbgi, memory, addr, value, irtype);
983                 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
984                 set_store(copyb_mem);
985         }
986 }
987
988 static void set_value_for_expression(const expression_t *expression,
989                                      ir_node *value)
990 {
991         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
992         value          = do_strict_conv(dbgi, value);
993
994         if(expression->type == EXPR_REFERENCE) {
995                 reference_expression_t *ref = (reference_expression_t*) expression;
996
997                 declaration_t *declaration = ref->declaration;
998                 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
999                 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1000                         set_value(declaration->v.value_number, value);
1001                         return;
1002                 }
1003         }
1004
1005         ir_node *addr = expression_to_addr(expression);
1006         type_t  *type = skip_typeref(expression->base.datatype);
1007         assign_value(dbgi, addr, type, value);
1008 }
1009
1010 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1011 {
1012         ir_mode *value_mode = get_irn_mode(value);
1013
1014         if (value_mode == dest_mode || is_Bad(value))
1015                 return value;
1016
1017         if(dest_mode == mode_b) {
1018                 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1019                 ir_node *cmp  = new_d_Cmp(dbgi, value, zero);
1020                 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1021                 return proj;
1022         }
1023
1024         return new_d_Conv(dbgi, value, dest_mode);
1025 }
1026
1027 static ir_node *create_incdec(const unary_expression_t *expression)
1028 {
1029         dbg_info     *dbgi  = get_dbg_info(&expression->expression.source_position);
1030         type_t       *type  = skip_typeref(expression->expression.datatype);
1031         ir_mode      *mode  = get_ir_mode(type);
1032         expression_t *value = expression->value;
1033
1034         ir_node *value_node = expression_to_firm(value);
1035
1036         ir_node *offset;
1037         if(is_type_pointer(type)) {
1038                 pointer_type_t *pointer_type = &type->pointer;
1039                 unsigned        elem_size    = get_type_size(pointer_type->points_to);
1040                 offset = new_Const_long(mode_Is, elem_size);
1041         } else {
1042                 assert(is_type_arithmetic(type));
1043                 offset = new_Const(mode, get_mode_one(mode));
1044         }
1045
1046         switch(expression->expression.type) {
1047         case EXPR_UNARY_POSTFIX_INCREMENT: {
1048                 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1049                 set_value_for_expression(value, new_value);
1050                 return value_node;
1051         }
1052         case EXPR_UNARY_POSTFIX_DECREMENT: {
1053                 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1054                 set_value_for_expression(value, new_value);
1055                 return value_node;
1056         }
1057         case EXPR_UNARY_PREFIX_INCREMENT: {
1058                 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1059                 set_value_for_expression(value, new_value);
1060                 return new_value;
1061         }
1062         case EXPR_UNARY_PREFIX_DECREMENT: {
1063                 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1064                 set_value_for_expression(value, new_value);
1065                 return new_value;
1066         }
1067         default:
1068                 panic("no incdec expr in create_incdec");
1069                 return NULL;
1070         }
1071 }
1072
1073 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1074 {
1075         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1076         type_t   *type = skip_typeref(expression->expression.datatype);
1077         ir_mode  *mode = get_ir_mode(type);
1078
1079         if(expression->expression.type == EXPR_UNARY_TAKE_ADDRESS)
1080                 return expression_to_addr(expression->value);
1081
1082         const expression_t *value      = expression->value;
1083         ir_node            *value_node = expression_to_firm(value);
1084
1085         switch(expression->expression.type) {
1086         case EXPR_UNARY_NEGATE:
1087                 return new_d_Minus(dbgi, value_node, mode);
1088         case EXPR_UNARY_PLUS:
1089                 return value_node;
1090         case EXPR_UNARY_BITWISE_NEGATE:
1091                 return new_d_Not(dbgi, value_node, mode);
1092         case EXPR_UNARY_NOT: {
1093                 if(get_irn_mode(value_node) != mode_b) {
1094                         value_node = create_conv(dbgi, value_node, mode_b);
1095                 }
1096                 value_node = new_d_Not(dbgi, value_node, mode_b);
1097                 if(mode != mode_b) {
1098                         value_node = create_conv(dbgi, value_node, mode);
1099                 }
1100                 return value_node;
1101         }
1102         case EXPR_UNARY_DEREFERENCE: {
1103                 type_t  *value_type = skip_typeref(value->base.datatype);
1104                 ir_type *irtype     = get_ir_type(value_type);
1105                 assert(is_Pointer_type(irtype));
1106                 ir_type *points_to  = get_pointer_points_to_type(irtype);
1107                 return deref_address(points_to, value_node, dbgi);
1108         }
1109         case EXPR_UNARY_POSTFIX_INCREMENT:
1110         case EXPR_UNARY_POSTFIX_DECREMENT:
1111         case EXPR_UNARY_PREFIX_INCREMENT:
1112         case EXPR_UNARY_PREFIX_DECREMENT:
1113                 return create_incdec(expression);
1114         case EXPR_UNARY_CAST: {
1115                 ir_node *node = create_conv(dbgi, value_node, get_ir_mode(type));
1116                 node = do_strict_conv(dbgi, node);
1117                 return node;
1118         }
1119         case EXPR_UNARY_CAST_IMPLICIT:
1120                 return create_conv(dbgi, value_node, get_ir_mode(type));
1121
1122         default:
1123                 break;
1124         }
1125         panic("invalid UNEXPR type found");
1126 }
1127
1128 static long get_pnc(expression_type_t type)
1129 {
1130         switch(type) {
1131         case EXPR_BINARY_EQUAL:        return pn_Cmp_Eq;
1132         case EXPR_BINARY_NOTEQUAL:     return pn_Cmp_Lg;
1133         case EXPR_BINARY_LESS:         return pn_Cmp_Lt;
1134         case EXPR_BINARY_LESSEQUAL:    return pn_Cmp_Le;
1135         case EXPR_BINARY_GREATER:      return pn_Cmp_Gt;
1136         case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1137         default:
1138                 break;
1139         }
1140         panic("trying to get pn_Cmp from non-comparison binexpr type");
1141 }
1142
1143 static ir_node *create_lazy_op(const binary_expression_t *expression)
1144 {
1145         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1146         type_t   *type = expression->expression.datatype;
1147         ir_mode  *mode = get_ir_mode(type);
1148
1149         ir_node *cur_block = get_cur_block();
1150
1151         ir_node *one_block = new_immBlock();
1152         ir_node *one       = new_Const(mode, get_mode_one(mode));
1153         ir_node *jmp_one   = new_d_Jmp(dbgi);
1154
1155         ir_node *zero_block = new_immBlock();
1156         ir_node *zero       = new_Const(mode, get_mode_null(mode));
1157         ir_node *jmp_zero   = new_d_Jmp(dbgi);
1158
1159         set_cur_block(cur_block);
1160         create_condition_evaluation((const expression_t*) expression,
1161                                     one_block, zero_block);
1162         mature_immBlock(one_block);
1163         mature_immBlock(zero_block);
1164
1165         ir_node *common_block = new_immBlock();
1166         add_immBlock_pred(common_block, jmp_one);
1167         add_immBlock_pred(common_block, jmp_zero);
1168         mature_immBlock(common_block);
1169
1170         ir_node *in[2] = { one, zero };
1171         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
1172
1173         return val;
1174 }
1175
1176 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1177                                             ir_node *right, ir_mode *mode);
1178
1179 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1180                                         create_arithmetic_func func)
1181 {
1182         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1183         ir_node  *left  = expression_to_firm(expression->left);
1184         ir_node  *right = expression_to_firm(expression->right);
1185         type_t   *type  = expression->right->base.datatype;
1186         /* be careful with the modes, because in arithmetic assign nodes only
1187          * the right operand has the mode of the arithmetic already */
1188         ir_mode  *mode  = get_ir_mode(type);
1189         left            = create_conv(dbgi, left, mode);
1190         ir_node  *res   = func(dbgi, left, right, mode);
1191
1192         return res;
1193 }
1194
1195 static ir_node *pointer_arithmetic(ir_node  *const pointer,
1196                                    ir_node  *      integer,
1197                                    type_t   *const type,
1198                                    dbg_info *const dbgi,
1199                                    const create_arithmetic_func func)
1200 {
1201         pointer_type_t *const pointer_type = (pointer_type_t*)type;
1202         type_t         *const points_to    = pointer_type->points_to;
1203         const unsigned        elem_size    = get_type_size(points_to);
1204
1205         assert(elem_size >= 1);
1206         if (elem_size > 1) {
1207                 integer             = create_conv(dbgi, integer, mode_Is);
1208                 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1209                 ir_node *const mul  = new_d_Mul(dbgi, integer, cnst, mode_Is);
1210                 integer = mul;
1211         }
1212
1213         ir_mode *const mode = get_ir_mode(type);
1214         return func(dbgi, pointer, integer, mode);
1215 }
1216
1217 static ir_node *create_arithmetic_assign_binop(
1218                 const binary_expression_t *expression, create_arithmetic_func func)
1219 {
1220         dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1221         type_t   *const type = skip_typeref(expression->expression.datatype);
1222         ir_node  *value;
1223
1224         if (is_type_pointer(type)) {
1225                 ir_node        *const pointer = expression_to_firm(expression->left);
1226                 ir_node        *      integer = expression_to_firm(expression->right);
1227                 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1228         } else {
1229                 value = create_arithmetic_binop(expression, func);
1230         }
1231
1232         ir_mode  *const mode = get_ir_mode(type);
1233         value = create_conv(dbgi, value, mode);
1234         set_value_for_expression(expression->left, value);
1235
1236         return value;
1237 }
1238
1239 static ir_node *create_add(const binary_expression_t *expression)
1240 {
1241         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1242         ir_node  *left  = expression_to_firm(expression->left);
1243         ir_node  *right = expression_to_firm(expression->right);
1244         type_t   *type  = expression->expression.datatype;
1245
1246         expression_t *expr_left  = expression->left;
1247         expression_t *expr_right = expression->right;
1248         type_t       *type_left  = skip_typeref(expr_left->base.datatype);
1249         type_t       *type_right = skip_typeref(expr_right->base.datatype);
1250
1251         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1252                 ir_mode *const mode = get_ir_mode(type);
1253                 return new_d_Add(dbgi, left, right, mode);
1254         }
1255
1256         if (is_type_pointer(type_left)) {
1257                 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1258         } else {
1259                 assert(is_type_pointer(type_right));
1260                 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1261         }
1262 }
1263
1264 static ir_node *create_sub(const binary_expression_t *expression)
1265 {
1266         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1267         expression_t *const expr_left  = expression->left;
1268         expression_t *const expr_right = expression->right;
1269         ir_node      *const left       = expression_to_firm(expr_left);
1270         ir_node      *const right      = expression_to_firm(expr_right);
1271         type_t       *const type       = expression->expression.datatype;
1272         type_t       *const type_left  = skip_typeref(expr_left->base.datatype);
1273         type_t       *const type_right = skip_typeref(expr_right->base.datatype);
1274
1275         if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1276                 ir_mode *const mode = get_ir_mode(type);
1277                 return new_d_Sub(dbgi, left, right, mode);
1278         } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1279                 const pointer_type_t *const ptr_type = &type_left->pointer;
1280                 const unsigned elem_size             = get_type_size(ptr_type->points_to);
1281                 ir_mode *const mode   = get_ir_mode(type);
1282                 ir_node *const sub    = new_d_Sub(dbgi, left, right, mode);
1283                 ir_node *const cnst   = new_Const_long(mode_Is, (long)elem_size);
1284                 ir_node *const no_mem = new_NoMem();
1285                 ir_node *const div    = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1286                                                   op_pin_state_floats);
1287                 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1288         }
1289
1290         assert(is_type_pointer(type_left));
1291         return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1292 }
1293
1294 static ir_node *create_shift(const binary_expression_t *expression)
1295 {
1296         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1297         ir_node  *left  = expression_to_firm(expression->left);
1298         ir_node  *right = expression_to_firm(expression->right);
1299         type_t   *type  = expression->expression.datatype;
1300         ir_mode  *mode  = get_ir_mode(type);
1301
1302         /* firm always wants the shift count to be unsigned */
1303         right = create_conv(dbgi, right, mode_Iu);
1304
1305         ir_node *res;
1306
1307         switch(expression->expression.type) {
1308         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1309         case EXPR_BINARY_SHIFTLEFT:
1310                 res = new_d_Shl(dbgi, left, right, mode);
1311                 break;
1312         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1313         case EXPR_BINARY_SHIFTRIGHT: {
1314                  expression_t *expr_left = expression->left;
1315                  type_t       *type_left = skip_typeref(expr_left->base.datatype);
1316
1317                  if(is_type_signed(type_left)) {
1318                         res = new_d_Shrs(dbgi, left, right, mode);
1319                  } else {
1320                          res = new_d_Shr(dbgi, left, right, mode);
1321                  }
1322                  break;
1323         }
1324         default:
1325                 panic("create shift op called for non-shift op");
1326         }
1327
1328         return res;
1329 }
1330
1331
1332 static ir_node *create_divmod(const binary_expression_t *expression)
1333 {
1334         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1335         ir_node  *left  = expression_to_firm(expression->left);
1336         ir_node  *right = expression_to_firm(expression->right);
1337         ir_node  *pin   = new_Pin(new_NoMem());
1338         /* be careful with the modes, because in arithmetic assign nodes only
1339          * the right operand has the mode of the arithmetic already */
1340         type_t   *type  = expression->right->base.datatype;
1341         ir_mode  *mode  = get_ir_mode(type);
1342         left            = create_conv(dbgi, left, mode);
1343         ir_node  *op;
1344         ir_node  *res;
1345
1346         switch (expression->expression.type) {
1347         case EXPR_BINARY_DIV:
1348         case EXPR_BINARY_DIV_ASSIGN:
1349                 if(mode_is_float(mode)) {
1350                         op  = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1351                         res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1352                 } else {
1353                         op  = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1354                         res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1355                 }
1356                 break;
1357
1358         case EXPR_BINARY_MOD:
1359         case EXPR_BINARY_MOD_ASSIGN:
1360                 assert(!mode_is_float(mode));
1361                 op  = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1362                 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1363                 break;
1364
1365         default: panic("unexpected binary expression type in create_divmod()");
1366         }
1367
1368         return res;
1369 }
1370
1371 static ir_node *create_arithmetic_assign_divmod(
1372                 const binary_expression_t *expression)
1373 {
1374         ir_node  *      value = create_divmod(expression);
1375         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1376         type_t   *const type  = expression->expression.datatype;
1377         ir_mode  *const mode  = get_ir_mode(type);
1378
1379         assert(type->type != TYPE_POINTER);
1380
1381         value = create_conv(dbgi, value, mode);
1382         set_value_for_expression(expression->left, value);
1383
1384         return value;
1385 }
1386
1387 static ir_node *create_arithmetic_assign_shift(
1388                 const binary_expression_t *expression)
1389 {
1390         ir_node  *      value = create_shift(expression);
1391         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1392         type_t   *const type  = expression->expression.datatype;
1393         ir_mode  *const mode  = get_ir_mode(type);
1394
1395         value = create_conv(dbgi, value, mode);
1396         set_value_for_expression(expression->left, value);
1397
1398         return value;
1399 }
1400
1401 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1402 {
1403         expression_type_t type = expression->expression.type;
1404
1405         switch(type) {
1406         case EXPR_BINARY_EQUAL:
1407         case EXPR_BINARY_NOTEQUAL:
1408         case EXPR_BINARY_LESS:
1409         case EXPR_BINARY_LESSEQUAL:
1410         case EXPR_BINARY_GREATER:
1411         case EXPR_BINARY_GREATEREQUAL: {
1412                 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1413                 ir_node *left  = expression_to_firm(expression->left);
1414                 ir_node *right = expression_to_firm(expression->right);
1415                 ir_node *cmp   = new_d_Cmp(dbgi, left, right);
1416                 long     pnc   = get_pnc(type);
1417                 ir_node *proj  = new_d_Proj(dbgi, cmp, mode_b, pnc);
1418                 return proj;
1419         }
1420         case EXPR_BINARY_ASSIGN: {
1421                 ir_node *right = expression_to_firm(expression->right);
1422                 set_value_for_expression(expression->left, right);
1423
1424                 return right;
1425         }
1426         case EXPR_BINARY_ADD:
1427                 return create_add(expression);
1428         case EXPR_BINARY_SUB:
1429                 return create_sub(expression);
1430         case EXPR_BINARY_MUL:
1431                 return create_arithmetic_binop(expression, new_d_Mul);
1432         case EXPR_BINARY_BITWISE_AND:
1433                 return create_arithmetic_binop(expression, new_d_And);
1434         case EXPR_BINARY_BITWISE_OR:
1435                 return create_arithmetic_binop(expression, new_d_Or);
1436         case EXPR_BINARY_BITWISE_XOR:
1437                 return create_arithmetic_binop(expression, new_d_Eor);
1438         case EXPR_BINARY_SHIFTLEFT:
1439         case EXPR_BINARY_SHIFTRIGHT:
1440                 return create_shift(expression);
1441         case EXPR_BINARY_DIV:
1442         case EXPR_BINARY_MOD:
1443                 return create_divmod(expression);
1444         case EXPR_BINARY_LOGICAL_AND:
1445         case EXPR_BINARY_LOGICAL_OR:
1446                 return create_lazy_op(expression);
1447         case EXPR_BINARY_COMMA:
1448                 expression_to_firm(expression->left);
1449                 return expression_to_firm(expression->right);
1450         case EXPR_BINARY_ADD_ASSIGN:
1451                 return create_arithmetic_assign_binop(expression, new_d_Add);
1452         case EXPR_BINARY_SUB_ASSIGN:
1453                 return create_arithmetic_assign_binop(expression, new_d_Sub);
1454         case EXPR_BINARY_MUL_ASSIGN:
1455                 return create_arithmetic_assign_binop(expression, new_d_Mul);
1456         case EXPR_BINARY_DIV_ASSIGN:
1457                 return create_arithmetic_assign_divmod(expression);
1458         case EXPR_BINARY_BITWISE_AND_ASSIGN:
1459                 return create_arithmetic_assign_binop(expression, new_d_And);
1460         case EXPR_BINARY_BITWISE_OR_ASSIGN:
1461                 return create_arithmetic_assign_binop(expression, new_d_Or);
1462         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1463                 return create_arithmetic_assign_binop(expression, new_d_Eor);
1464         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1465         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1466                 return create_arithmetic_assign_shift(expression);
1467         default:
1468                 panic("TODO binexpr type");
1469         }
1470 }
1471
1472 static ir_node *array_access_addr(const array_access_expression_t *expression)
1473 {
1474         dbg_info *dbgi      = get_dbg_info(&expression->expression.source_position);
1475         ir_node  *base_addr = expression_to_firm(expression->array_ref);
1476         ir_node  *offset    = expression_to_firm(expression->index);
1477         offset              = create_conv(dbgi, offset, mode_Iu);
1478
1479         type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1480         assert(is_type_pointer(ref_type));
1481         pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1482
1483         unsigned elem_size       = get_type_size(pointer_type->points_to);
1484         ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1485         ir_node *real_offset     = new_d_Mul(dbgi, offset, elem_size_const,
1486                                              mode_Iu);
1487         ir_node *result          = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1488
1489         return result;
1490 }
1491
1492 static ir_node *array_access_to_firm(
1493                 const array_access_expression_t *expression)
1494 {
1495         dbg_info *dbgi   = get_dbg_info(&expression->expression.source_position);
1496         ir_node  *addr   = array_access_addr(expression);
1497         type_t   *type   = revert_automatic_type_conversion(
1498                         (const expression_t*) expression);
1499         type             = skip_typeref(type);
1500         ir_type  *irtype = get_ir_type(type);
1501
1502         return deref_address(irtype, addr, dbgi);
1503 }
1504
1505 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1506 {
1507         type_t *type = expression->type;
1508         if(type == NULL) {
1509                 type = expression->size_expression->base.datatype;
1510                 assert(type != NULL);
1511         }
1512
1513         ir_mode  *mode      = get_ir_mode(expression->expression.datatype);
1514         unsigned  size      = get_type_size(type);
1515         ir_node  *size_node = new_Const_long(mode, size);
1516
1517         return size_node;
1518 }
1519
1520 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1521 {
1522         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1523
1524         ir_node *cur_block   = get_cur_block();
1525
1526         /* create the true block */
1527         ir_node *true_block  = new_immBlock();
1528
1529         ir_node *true_val = expression_to_firm(expression->true_expression);
1530         ir_node *true_jmp = new_Jmp();
1531
1532         /* create the false block */
1533         ir_node *false_block = new_immBlock();
1534
1535         ir_node *false_val = expression_to_firm(expression->false_expression);
1536         ir_node *false_jmp = new_Jmp();
1537
1538         /* create the condition evaluation */
1539         set_cur_block(cur_block);
1540         create_condition_evaluation(expression->condition, true_block, false_block);
1541         mature_immBlock(true_block);
1542         mature_immBlock(false_block);
1543
1544         /* create the common block */
1545         ir_node *common_block = new_immBlock();
1546         add_immBlock_pred(common_block, true_jmp);
1547         add_immBlock_pred(common_block, false_jmp);
1548         mature_immBlock(common_block);
1549
1550         /* TODO improve static semantics, so either both or no values are NULL */
1551         if (true_val == NULL || false_val == NULL)
1552                 return NULL;
1553
1554         ir_node *in[2] = { true_val, false_val };
1555         ir_mode *mode  = get_irn_mode(true_val);
1556         assert(get_irn_mode(false_val) == mode);
1557         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
1558
1559         return val;
1560 }
1561
1562 static ir_node *select_addr(const select_expression_t *expression)
1563 {
1564         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1565
1566         ir_node *compound_addr = expression_to_firm(expression->compound);
1567
1568         declaration_t *entry = expression->compound_entry;
1569         assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1570         ir_entity     *entity = entry->v.entity;
1571
1572         assert(entity != NULL);
1573
1574         ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1575
1576         return sel;
1577 }
1578
1579 static ir_node *select_to_firm(const select_expression_t *expression)
1580 {
1581         dbg_info *dbgi   = get_dbg_info(&expression->expression.source_position);
1582         ir_node  *addr   = select_addr(expression);
1583         type_t   *type   = revert_automatic_type_conversion(
1584                         (const expression_t*) expression);
1585         type             = skip_typeref(type);
1586         ir_type  *irtype = get_ir_type(type);
1587
1588         return deref_address(irtype, addr, dbgi);
1589 }
1590
1591 /* Values returned by __builtin_classify_type. */
1592 typedef enum gcc_type_class
1593 {
1594         no_type_class = -1,
1595         void_type_class,
1596         integer_type_class,
1597         char_type_class,
1598         enumeral_type_class,
1599         boolean_type_class,
1600         pointer_type_class,
1601         reference_type_class,
1602         offset_type_class,
1603         real_type_class,
1604         complex_type_class,
1605         function_type_class,
1606         method_type_class,
1607         record_type_class,
1608         union_type_class,
1609         array_type_class,
1610         string_type_class,
1611         set_type_class,
1612         file_type_class,
1613         lang_type_class
1614 } gcc_type_class;
1615
1616 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1617 {
1618         const type_t *const type = expr->type_expression->base.datatype;
1619
1620         gcc_type_class tc;
1621         switch (type->type)
1622         {
1623                 case TYPE_ATOMIC: {
1624                         const atomic_type_t *const atomic_type = &type->atomic;
1625                         switch (atomic_type->atype) {
1626                                 // should not be reached
1627                                 case ATOMIC_TYPE_INVALID:
1628                                         tc = no_type_class;
1629                                         break;
1630
1631                                 // gcc cannot do that
1632                                 case ATOMIC_TYPE_VOID:
1633                                         tc = void_type_class;
1634                                         break;
1635
1636                                 case ATOMIC_TYPE_CHAR:      // gcc handles this as integer
1637                                 case ATOMIC_TYPE_SCHAR:     // gcc handles this as integer
1638                                 case ATOMIC_TYPE_UCHAR:     // gcc handles this as integer
1639                                 case ATOMIC_TYPE_SHORT:
1640                                 case ATOMIC_TYPE_USHORT:
1641                                 case ATOMIC_TYPE_INT:
1642                                 case ATOMIC_TYPE_UINT:
1643                                 case ATOMIC_TYPE_LONG:
1644                                 case ATOMIC_TYPE_ULONG:
1645                                 case ATOMIC_TYPE_LONGLONG:
1646                                 case ATOMIC_TYPE_ULONGLONG:
1647                                 case ATOMIC_TYPE_BOOL:      // gcc handles this as integer
1648                                         tc = integer_type_class;
1649                                         break;
1650
1651                                 case ATOMIC_TYPE_FLOAT:
1652                                 case ATOMIC_TYPE_DOUBLE:
1653                                 case ATOMIC_TYPE_LONG_DOUBLE:
1654                                         tc = real_type_class;
1655                                         break;
1656
1657 #ifdef PROVIDE_COMPLEX
1658                                 case ATOMIC_TYPE_FLOAT_COMPLEX:
1659                                 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1660                                 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1661                                         tc = complex_type_class;
1662                                         break;
1663                                 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1664                                 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1665                                 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1666                                         tc = complex_type_class;
1667                                         break;
1668 #endif
1669
1670                                 default:
1671                                         panic("Unimplemented case in classify_type_to_firm().");
1672                         }
1673                         break;
1674                 }
1675
1676                 case TYPE_ARRAY:           // gcc handles this as pointer
1677                 case TYPE_FUNCTION:        // gcc handles this as pointer
1678                 case TYPE_POINTER:         tc = pointer_type_class; break;
1679                 case TYPE_COMPOUND_STRUCT: tc = record_type_class;  break;
1680                 case TYPE_COMPOUND_UNION:  tc = union_type_class;   break;
1681
1682                 // gcc handles this as integer
1683                 case TYPE_ENUM:            tc = integer_type_class; break;
1684
1685                 default:
1686                         panic("Unimplemented case in classify_type_to_firm().");
1687         }
1688
1689         dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1690         ir_mode  *const mode = mode_Is;
1691         tarval   *const tv   = new_tarval_from_long(tc, mode);
1692         return new_d_Const(dbgi, mode, tv);
1693 }
1694
1695 static ir_node *function_name_to_firm(
1696                 const string_literal_expression_t *const expr)
1697 {
1698         if (current_function_name == NULL) {
1699                 const source_position_t *const src_pos =
1700                         &expr->expression.source_position;
1701                 const char *const name = current_function_decl->symbol->string;
1702                 current_function_name = string_to_firm(src_pos, "__func__", name);
1703         }
1704
1705         return current_function_name;
1706 }
1707
1708 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1709 {
1710         statement_t *statement = expr->statement;
1711
1712         assert(statement->type == STATEMENT_COMPOUND);
1713         return compound_statement_to_firm((compound_statement_t*) statement);
1714 }
1715
1716 static ir_node *va_start_expression_to_firm(
1717         const va_start_expression_t *const expr)
1718 {
1719         ir_type   *const method_type = get_ir_type(current_function_decl->type);
1720         int        const n           = get_method_n_params(method_type) - 1;
1721         ir_entity *const parm_ent    = get_method_value_param_ent(method_type, n);
1722         ir_node   *const arg_base    = get_irg_value_param_base(current_ir_graph);
1723         dbg_info  *const dbgi        =
1724                 get_dbg_info(&expr->expression.source_position);
1725         ir_node   *const no_mem      = new_NoMem();
1726         ir_node   *const arg_sel     =
1727                 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
1728
1729         size_t     const parm_size   = get_type_size(expr->parameter->type);
1730         ir_node   *const cnst        = new_Const_long(mode_Iu, parm_size);
1731         ir_node   *const add         = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
1732         set_value_for_expression(expr->ap, add);
1733
1734         return NULL;
1735 }
1736
1737 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
1738 {
1739         ir_type  *const irtype = get_ir_type(expr->expression.datatype);
1740         ir_node  *const ap     = expression_to_firm(expr->ap);
1741         dbg_info *const dbgi   = get_dbg_info(&expr->expression.source_position);
1742         ir_node  *const res    = deref_address(irtype, ap, dbgi);
1743
1744         size_t     const parm_size   = get_type_size(expr->expression.datatype);
1745         ir_node   *const cnst        = new_Const_long(mode_Iu, parm_size);
1746         ir_node   *const add         = new_d_Add(dbgi, ap, cnst, mode_P_data);
1747         set_value_for_expression(expr->ap, add);
1748
1749         return res;
1750 }
1751
1752 static ir_node *dereference_addr(const unary_expression_t *const expression)
1753 {
1754         assert(expression->expression.type == EXPR_UNARY_DEREFERENCE);
1755         return expression_to_firm(expression->value);
1756 }
1757
1758 static ir_node *expression_to_addr(const expression_t *expression)
1759 {
1760         switch(expression->type) {
1761         case EXPR_REFERENCE:
1762                 return reference_addr(&expression->reference);
1763         case EXPR_ARRAY_ACCESS:
1764                 return array_access_addr(&expression->array_access);
1765         case EXPR_SELECT:
1766                 return select_addr(&expression->select);
1767         case EXPR_CALL:
1768                 return call_expression_to_firm(&expression->call);
1769         case EXPR_UNARY_DEREFERENCE: {
1770                 return dereference_addr(&expression->unary);
1771         }
1772         default:
1773                 break;
1774         }
1775         panic("trying to get address of non-lvalue");
1776 }
1777
1778 static ir_node *_expression_to_firm(const expression_t *expression)
1779 {
1780         switch(expression->type) {
1781         case EXPR_CONST:
1782                 return const_to_firm(&expression->conste);
1783         case EXPR_STRING_LITERAL:
1784                 return string_literal_to_firm(&expression->string);
1785         case EXPR_WIDE_STRING_LITERAL:
1786                 return wide_string_literal_to_firm(&expression->wide_string);
1787         case EXPR_REFERENCE:
1788                 return reference_expression_to_firm(&expression->reference);
1789         case EXPR_CALL:
1790                 return call_expression_to_firm(&expression->call);
1791         EXPR_UNARY_CASES
1792                 return unary_expression_to_firm(&expression->unary);
1793         EXPR_BINARY_CASES
1794                 return binary_expression_to_firm(&expression->binary);
1795         case EXPR_ARRAY_ACCESS:
1796                 return array_access_to_firm(&expression->array_access);
1797         case EXPR_SIZEOF:
1798                 return sizeof_to_firm(&expression->sizeofe);
1799         case EXPR_CONDITIONAL:
1800                 return conditional_to_firm(&expression->conditional);
1801         case EXPR_SELECT:
1802                 return select_to_firm(&expression->select);
1803         case EXPR_CLASSIFY_TYPE:
1804                 return classify_type_to_firm(&expression->classify_type);
1805         case EXPR_FUNCTION:
1806         case EXPR_PRETTY_FUNCTION:
1807                 return function_name_to_firm(&expression->string);
1808         case EXPR_STATEMENT:
1809                 return statement_expression_to_firm(&expression->statement);
1810         case EXPR_VA_START:
1811                 return va_start_expression_to_firm(&expression->va_starte);
1812         case EXPR_VA_ARG:
1813                 return va_arg_expression_to_firm(&expression->va_arge);
1814         case EXPR_OFFSETOF:
1815         case EXPR_BUILTIN_SYMBOL:
1816                 panic("unimplemented expression found");
1817
1818         case EXPR_UNKNOWN:
1819         case EXPR_INVALID:
1820                 break;
1821         }
1822         panic("invalid expression found");
1823 }
1824
1825 static ir_node *expression_to_firm(const expression_t *expression)
1826 {
1827         ir_node *res = _expression_to_firm(expression);
1828
1829         if(res != NULL && get_irn_mode(res) == mode_b) {
1830                 ir_mode *mode = get_ir_mode(expression->base.datatype);
1831                 res           = create_conv(NULL, res, mode);
1832         }
1833
1834         return res;
1835 }
1836
1837 static ir_node *expression_to_modeb(const expression_t *expression)
1838 {
1839         ir_node *res = _expression_to_firm(expression);
1840         res          = create_conv(NULL, res, mode_b);
1841
1842         return res;
1843 }
1844
1845 /**
1846  * create a short-circuit expression evaluation that tries to construct
1847  * efficient control flow structures for &&, || and ! expressions
1848  */
1849 static void create_condition_evaluation(const expression_t *expression,
1850                                         ir_node *true_block,
1851                                         ir_node *false_block)
1852 {
1853         switch(expression->type) {
1854         case EXPR_UNARY_NOT: {
1855                 const unary_expression_t *unary_expression = &expression->unary;
1856                 create_condition_evaluation(unary_expression->value, false_block,
1857                                             true_block);
1858                 return;
1859         }
1860         case EXPR_BINARY_LOGICAL_AND: {
1861                 const binary_expression_t *binary_expression = &expression->binary;
1862
1863                 ir_node *cur_block   = get_cur_block();
1864                 ir_node *extra_block = new_immBlock();
1865                 set_cur_block(cur_block);
1866                 create_condition_evaluation(binary_expression->left, extra_block,
1867                                             false_block);
1868                 mature_immBlock(extra_block);
1869                 set_cur_block(extra_block);
1870                 create_condition_evaluation(binary_expression->right, true_block,
1871                                             false_block);
1872                 return;
1873         }
1874         case EXPR_BINARY_LOGICAL_OR: {
1875                 const binary_expression_t *binary_expression = &expression->binary;
1876
1877                 ir_node *cur_block   = get_cur_block();
1878                 ir_node *extra_block = new_immBlock();
1879                 set_cur_block(cur_block);
1880                 create_condition_evaluation(binary_expression->left, true_block,
1881                                             extra_block);
1882                 mature_immBlock(extra_block);
1883                 set_cur_block(extra_block);
1884                 create_condition_evaluation(binary_expression->right, true_block,
1885                                             false_block);
1886                 return;
1887         }
1888         default:
1889                 break;
1890         }
1891
1892         dbg_info *dbgi       = get_dbg_info(&expression->base.source_position);
1893         ir_node  *condition  = expression_to_modeb(expression);
1894         ir_node  *cond       = new_d_Cond(dbgi, condition);
1895         ir_node  *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1896         ir_node  *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1897
1898         add_immBlock_pred(true_block, true_proj);
1899         add_immBlock_pred(false_block, false_proj);
1900
1901         set_cur_block(NULL);
1902 }
1903
1904 static void return_statement_to_firm(return_statement_t *statement)
1905 {
1906         if(get_cur_block() == NULL)
1907                 return;
1908
1909         ir_type *func_irtype = get_ir_type(current_function_decl->type);
1910
1911         dbg_info *dbgi  = get_dbg_info(&statement->statement.source_position);
1912
1913         ir_node *in[1];
1914         int      in_len;
1915         if(get_method_n_ress(func_irtype) > 0) {
1916                 ir_type *res_type = get_method_res_type(func_irtype, 0);
1917
1918                 if(statement->return_value != NULL) {
1919                         ir_node *node = expression_to_firm(statement->return_value);
1920                         node  = do_strict_conv(dbgi, node);
1921                         in[0] = node;
1922                 } else {
1923                         ir_mode *mode;
1924                         if(is_compound_type(res_type)) {
1925                                 mode = mode_P_data;
1926                         } else {
1927                                 mode = get_type_mode(res_type);
1928                         }
1929                         in[0] = new_Unknown(mode);
1930                 }
1931                 in_len = 1;
1932         } else {
1933                 /* build return_value for its side effects */
1934                 if(statement->return_value != NULL) {
1935                         expression_to_firm(statement->return_value);
1936                 }
1937                 in_len = 0;
1938         }
1939
1940         ir_node  *store = get_store();
1941         ir_node  *ret   = new_d_Return(dbgi, store, in_len, in);
1942
1943         ir_node *end_block = get_irg_end_block(current_ir_graph);
1944         add_immBlock_pred(end_block, ret);
1945
1946         set_cur_block(NULL);
1947 }
1948
1949 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1950 {
1951         if(get_cur_block() == NULL)
1952                 return NULL;
1953
1954         return expression_to_firm(statement->expression);
1955 }
1956
1957 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1958 {
1959         ir_node     *result    = NULL;
1960         statement_t *statement = compound->statements;
1961         for( ; statement != NULL; statement = statement->base.next) {
1962                 //context2firm(&statement->context);
1963
1964                 if(statement->base.next == NULL
1965                                 && statement->type == STATEMENT_EXPRESSION) {
1966                         result = expression_statement_to_firm(
1967                                         (expression_statement_t*) statement);
1968                         break;
1969                 }
1970                 statement_to_firm(statement);
1971         }
1972
1973         return result;
1974 }
1975
1976 static void if_statement_to_firm(if_statement_t *statement)
1977 {
1978         ir_node *cur_block = get_cur_block();
1979
1980         ir_node *fallthrough_block = new_immBlock();
1981
1982         /* the true (blocks) */
1983         ir_node *true_block;
1984         if (statement->true_statement != NULL) {
1985                 true_block = new_immBlock();
1986                 statement_to_firm(statement->true_statement);
1987                 if(get_cur_block() != NULL) {
1988                         ir_node *jmp = new_Jmp();
1989                         add_immBlock_pred(fallthrough_block, jmp);
1990                 }
1991         } else {
1992                 true_block = fallthrough_block;
1993         }
1994
1995         /* the false (blocks) */
1996         ir_node *false_block;
1997         if(statement->false_statement != NULL) {
1998                 false_block = new_immBlock();
1999
2000                 statement_to_firm(statement->false_statement);
2001                 if(get_cur_block() != NULL) {
2002                         ir_node *jmp = new_Jmp();
2003                         add_immBlock_pred(fallthrough_block, jmp);
2004                 }
2005         } else {
2006                 false_block = fallthrough_block;
2007         }
2008
2009         /* create the condition */
2010         if(cur_block != NULL) {
2011                 set_cur_block(cur_block);
2012                 create_condition_evaluation(statement->condition, true_block,
2013                                             false_block);
2014         }
2015
2016         mature_immBlock(true_block);
2017         if(false_block != fallthrough_block) {
2018                 mature_immBlock(false_block);
2019         }
2020         mature_immBlock(fallthrough_block);
2021
2022         set_cur_block(fallthrough_block);
2023 }
2024
2025 static void while_statement_to_firm(while_statement_t *statement)
2026 {
2027         ir_node *jmp = NULL;
2028         if(get_cur_block() != NULL) {
2029                 jmp = new_Jmp();
2030         }
2031
2032         /* create the header block */
2033         ir_node *header_block = new_immBlock();
2034         if(jmp != NULL) {
2035                 add_immBlock_pred(header_block, jmp);
2036         }
2037
2038         /* the false block */
2039         ir_node *false_block = new_immBlock();
2040
2041         /* the loop body */
2042         ir_node *body_block;
2043         if (statement->body != NULL) {
2044                 ir_node *old_continue_label = continue_label;
2045                 ir_node *old_break_label    = break_label;
2046                 continue_label              = header_block;
2047                 break_label                 = false_block;
2048
2049                 body_block = new_immBlock();
2050                 statement_to_firm(statement->body);
2051
2052                 assert(continue_label == header_block);
2053                 assert(break_label    == false_block);
2054                 continue_label = old_continue_label;
2055                 break_label    = old_break_label;
2056
2057                 if(get_cur_block() != NULL) {
2058                         jmp = new_Jmp();
2059                         add_immBlock_pred(header_block, jmp);
2060                 }
2061         } else {
2062                 body_block = header_block;
2063         }
2064
2065         /* create the condition */
2066         set_cur_block(header_block);
2067
2068         create_condition_evaluation(statement->condition, body_block, false_block);
2069         mature_immBlock(body_block);
2070         mature_immBlock(false_block);
2071         mature_immBlock(header_block);
2072
2073         set_cur_block(false_block);
2074 }
2075
2076 static void do_while_statement_to_firm(do_while_statement_t *statement)
2077 {
2078         ir_node *jmp = NULL;
2079         if(get_cur_block() != NULL) {
2080                 jmp = new_Jmp();
2081         }
2082
2083         /* create the header block */
2084         ir_node *header_block = new_immBlock();
2085
2086         /* the false block */
2087         ir_node *false_block = new_immBlock();
2088
2089         /* the loop body */
2090         ir_node *body_block = new_immBlock();
2091         if(jmp != NULL) {
2092                 add_immBlock_pred(body_block, jmp);
2093         }
2094
2095         if (statement->body != NULL) {
2096                 ir_node *old_continue_label = continue_label;
2097                 ir_node *old_break_label    = break_label;
2098                 continue_label              = header_block;
2099                 break_label                 = false_block;
2100
2101                 statement_to_firm(statement->body);
2102
2103                 assert(continue_label == header_block);
2104                 assert(break_label    == false_block);
2105                 continue_label = old_continue_label;
2106                 break_label    = old_break_label;
2107
2108                 if (get_cur_block() == NULL) {
2109                         mature_immBlock(header_block);
2110                         mature_immBlock(body_block);
2111                         mature_immBlock(false_block);
2112                         return;
2113                 }
2114         }
2115
2116         ir_node *body_jmp = new_Jmp();
2117         add_immBlock_pred(header_block, body_jmp);
2118         mature_immBlock(header_block);
2119
2120         /* create the condition */
2121         set_cur_block(header_block);
2122
2123         create_condition_evaluation(statement->condition, body_block, false_block);
2124         mature_immBlock(body_block);
2125         mature_immBlock(false_block);
2126         mature_immBlock(header_block);
2127
2128         set_cur_block(false_block);
2129 }
2130
2131 static void for_statement_to_firm(for_statement_t *statement)
2132 {
2133         ir_node *jmp = NULL;
2134         if (get_cur_block() != NULL) {
2135                 if(statement->initialisation != NULL) {
2136                         expression_to_firm(statement->initialisation);
2137                 }
2138                 jmp = new_Jmp();
2139         }
2140
2141         /* create the step block */
2142         ir_node *const step_block = new_immBlock();
2143         if (statement->step != NULL) {
2144                 expression_to_firm(statement->step);
2145         }
2146         ir_node *const step_jmp = new_Jmp();
2147
2148         /* create the header block */
2149         ir_node *const header_block = new_immBlock();
2150         if (jmp != NULL) {
2151                 add_immBlock_pred(header_block, jmp);
2152         }
2153         add_immBlock_pred(header_block, step_jmp);
2154
2155         /* the false block */
2156         ir_node *const false_block = new_immBlock();
2157
2158         /* the loop body */
2159         ir_node * body_block;
2160         if (statement->body != NULL) {
2161                 ir_node *const old_continue_label = continue_label;
2162                 ir_node *const old_break_label    = break_label;
2163                 continue_label = step_block;
2164                 break_label    = false_block;
2165
2166                 body_block = new_immBlock();
2167                 statement_to_firm(statement->body);
2168
2169                 assert(continue_label == step_block);
2170                 assert(break_label    == false_block);
2171                 continue_label = old_continue_label;
2172                 break_label    = old_break_label;
2173
2174                 if (get_cur_block() != NULL) {
2175                         jmp = new_Jmp();
2176                         add_immBlock_pred(step_block, jmp);
2177                 }
2178         } else {
2179                 body_block = step_block;
2180         }
2181
2182         /* create the condition */
2183         set_cur_block(header_block);
2184         if (statement->condition != NULL) {
2185                 create_condition_evaluation(statement->condition, body_block,
2186                                             false_block);
2187         } else {
2188                 keep_alive(header_block);
2189                 jmp = new_Jmp();
2190                 add_immBlock_pred(body_block, jmp);
2191         }
2192
2193         mature_immBlock(body_block);
2194         mature_immBlock(false_block);
2195         mature_immBlock(step_block);
2196         mature_immBlock(header_block);
2197         mature_immBlock(false_block);
2198
2199         set_cur_block(false_block);
2200 }
2201
2202 static void create_declaration_entity(declaration_t *declaration,
2203                                       declaration_type_t declaration_type,
2204                                       ir_type *parent_type)
2205 {
2206         ident     *id     = new_id_from_str(declaration->symbol->string);
2207         ir_type   *irtype = get_ir_type(declaration->type);
2208         ir_entity *entity = new_entity(parent_type, id, irtype);
2209         set_entity_ld_ident(entity, id);
2210
2211         declaration->declaration_type = (unsigned char) declaration_type;
2212         declaration->v.entity         = entity;
2213         set_entity_variability(entity, variability_uninitialized);
2214         /* TODO: visibility? */
2215 }
2216
2217 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2218
2219 enum compound_graph_entry_type_t {
2220         COMPOUND_GRAPH_ENTRY_ARRAY,
2221         COMPOUND_GRAPH_ENTRY_COMPOUND
2222 };
2223
2224 struct compound_graph_path_entry_t {
2225         int type;
2226         union {
2227                 ir_entity *entity;
2228                 int        array_index;
2229         } v;
2230         compound_graph_path_entry_t *prev;
2231 };
2232
2233 static void create_initializer_object(initializer_t *initializer, type_t *type,
2234                 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2235
2236 static compound_graph_path *create_compound_path(ir_type *type,
2237                 compound_graph_path_entry_t *entry, int len)
2238 {
2239         compound_graph_path *path = new_compound_graph_path(type, len);
2240
2241         int i = len - 1;
2242         for( ; entry != NULL; entry = entry->prev, --i) {
2243                 assert(i >= 0);
2244                 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2245                         set_compound_graph_path_node(path, i, entry->v.entity);
2246                 } else {
2247                         assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2248                         set_compound_graph_path_array_index(path, i, entry->v.array_index);
2249                 }
2250         }
2251         assert(i == -1);
2252
2253         return path;
2254 }
2255
2256 static void create_initializer_value(initializer_value_t *initializer,
2257                                      ir_entity *entity,
2258                                      compound_graph_path_entry_t *entry,
2259                                      int len)
2260 {
2261         ir_node             *node = expression_to_firm(initializer->value);
2262         ir_type             *type = get_entity_type(entity);
2263         compound_graph_path *path = create_compound_path(type, entry, len);
2264         add_compound_ent_value_w_path(entity, node, path);
2265 }
2266
2267 static void create_initializer_compound(initializer_list_t *initializer,
2268                                         compound_type_t *type,
2269                                         ir_entity *entity,
2270                                         compound_graph_path_entry_t *last_entry,
2271                                         int len)
2272 {
2273         declaration_t *compound_declaration = type->declaration;
2274
2275         declaration_t *compound_entry = compound_declaration->context.declarations;
2276
2277         compound_graph_path_entry_t entry;
2278         entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2279         entry.prev = last_entry;
2280         ++len;
2281
2282         size_t i = 0;
2283         for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2284                 if(compound_entry->symbol == NULL)
2285                         continue;
2286                 if(compound_entry->namespc != NAMESPACE_NORMAL)
2287                         continue;
2288
2289                 if(i >= initializer->len)
2290                         break;
2291
2292                 entry.v.entity = compound_entry->v.entity;
2293
2294                 initializer_t *sub_initializer = initializer->initializers[i];
2295
2296                 assert(compound_entry != NULL);
2297                 assert(compound_entry->declaration_type
2298                                 == DECLARATION_TYPE_COMPOUND_MEMBER);
2299
2300                 if(sub_initializer->type == INITIALIZER_VALUE) {
2301                         create_initializer_value(&sub_initializer->value,
2302                                                  entity, &entry, len);
2303                 } else {
2304                         type_t *entry_type = skip_typeref(compound_entry->type);
2305                         create_initializer_object(sub_initializer, entry_type, entity,
2306                                                   &entry, len);
2307                 }
2308
2309                 ++i;
2310         }
2311 }
2312
2313 static void create_initializer_array(initializer_list_t *initializer,
2314                                      array_type_t *type, ir_entity *entity,
2315                                      compound_graph_path_entry_t *last_entry,
2316                                      int len)
2317 {
2318         type_t *element_type = type->element_type;
2319         element_type         = skip_typeref(element_type);
2320
2321         compound_graph_path_entry_t entry;
2322         entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2323         entry.prev = last_entry;
2324         ++len;
2325
2326         size_t i;
2327         for(i = 0; i < initializer->len; ++i) {
2328                 entry.v.array_index = i;
2329
2330                 initializer_t *sub_initializer = initializer->initializers[i];
2331
2332                 if(sub_initializer->type == INITIALIZER_VALUE) {
2333                         create_initializer_value(&sub_initializer->value,
2334                                                  entity, &entry, len);
2335                 } else {
2336                         create_initializer_object(sub_initializer, element_type, entity,
2337                                                   &entry, len);
2338                 }
2339         }
2340
2341 #if 0
2342         /* TODO: initialize rest... */
2343         if(type->size_expression != NULL) {
2344                 size_t array_len = fold_constant(type->size_expression);
2345                 for( ; i < array_len; ++i) {
2346
2347                 }
2348         }
2349 #endif
2350 }
2351
2352 static void create_initializer_string(initializer_string_t *initializer,
2353                                       array_type_t *type, ir_entity *entity,
2354                                       compound_graph_path_entry_t *last_entry,
2355                                       int len)
2356 {
2357         type_t *element_type = type->element_type;
2358         element_type         = skip_typeref(element_type);
2359
2360         compound_graph_path_entry_t entry;
2361         entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2362         entry.prev = last_entry;
2363         ++len;
2364
2365         ir_type    *irtype  = get_entity_type(entity);
2366         size_t      arr_len = get_array_type_size(type);
2367         const char *p       = initializer->string;
2368         size_t      i       = 0;
2369         for(i = 0; i < arr_len; ++i, ++p) {
2370                 entry.v.array_index = i;
2371
2372                 ir_node             *node = new_Const_long(mode_Bs, *p);
2373                 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2374                 add_compound_ent_value_w_path(entity, node, path);
2375
2376                 if(*p == '\0')
2377                         break;
2378         }
2379 }
2380
2381 static void create_initializer_wide_string(
2382         const initializer_wide_string_t *const initializer, array_type_t *const type,
2383         ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2384         int len)
2385 {
2386         type_t *element_type = type->element_type;
2387         element_type         = skip_typeref(element_type);
2388
2389         compound_graph_path_entry_t entry;
2390         entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2391         entry.prev = last_entry;
2392         ++len;
2393
2394         ir_type           *const irtype  = get_entity_type(entity);
2395         const size_t             arr_len = get_array_type_size(type);
2396         const wchar_rep_t *      p       = initializer->string.begin;
2397         const wchar_rep_t *const end     = p + initializer->string.size;
2398         for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2399                 entry.v.array_index = i;
2400
2401                 ir_node             *node = new_Const_long(mode_Is, *p);
2402                 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2403                 add_compound_ent_value_w_path(entity, node, path);
2404         }
2405 }
2406
2407 static void create_initializer_object(initializer_t *initializer, type_t *type,
2408                 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2409 {
2410         if(is_type_array(type)) {
2411                 array_type_t *array_type = &type->array;
2412
2413                 switch (initializer->type) {
2414                         case INITIALIZER_STRING: {
2415                                 initializer_string_t *const string = &initializer->string;
2416                                 create_initializer_string(string, array_type, entity, entry, len);
2417                                 return;
2418                         }
2419
2420                         case INITIALIZER_WIDE_STRING: {
2421                                 initializer_wide_string_t *const string = &initializer->wide_string;
2422                                 create_initializer_wide_string(string, array_type, entity, entry, len);
2423                                 return;
2424                         }
2425
2426                         case INITIALIZER_LIST: {
2427                                 initializer_list_t *const list = &initializer->list;
2428                                 create_initializer_array(list, array_type, entity, entry, len);
2429                                 return;
2430                         }
2431
2432                         case INITIALIZER_VALUE:
2433                                 break;
2434                 }
2435                 panic("Unhandled initializer");
2436         } else {
2437                 assert(initializer->type == INITIALIZER_LIST);
2438                 initializer_list_t *list = &initializer->list;
2439
2440                 assert(is_type_compound(type));
2441                 compound_type_t *compound_type = &type->compound;
2442                 create_initializer_compound(list, compound_type, entity, entry, len);
2443         }
2444 }
2445
2446 static void create_initializer_local_variable_entity(declaration_t *declaration)
2447 {
2448         initializer_t *initializer = declaration->init.initializer;
2449         dbg_info      *dbgi        = get_dbg_info(&declaration->source_position);
2450         ir_entity     *entity      = declaration->v.entity;
2451         ir_node       *memory      = get_store();
2452         ir_node       *nomem       = new_NoMem();
2453         ir_node       *frame       = get_irg_frame(current_ir_graph);
2454         ir_node       *addr        = new_d_simpleSel(dbgi, nomem, frame, entity);
2455
2456         if(initializer->type == INITIALIZER_VALUE) {
2457                 initializer_value_t *initializer_value = &initializer->value;
2458
2459                 ir_node *value = expression_to_firm(initializer_value->value);
2460                 type_t  *type  = skip_typeref(declaration->type);
2461                 assign_value(dbgi, addr, type, value);
2462                 return;
2463         }
2464
2465         /* create a "template" entity which is copied to the entity on the stack */
2466         ident     *id          = unique_ident("initializer");
2467         ir_type   *irtype      = get_ir_type(declaration->type);
2468         ir_type   *global_type = get_glob_type();
2469         ir_entity *init_entity = new_entity(global_type, id, irtype);
2470         set_entity_ld_ident(init_entity, id);
2471
2472         set_entity_variability(init_entity, variability_initialized);
2473         set_entity_visibility(init_entity, visibility_local);
2474
2475         ir_graph *old_current_ir_graph = current_ir_graph;
2476         current_ir_graph = get_const_code_irg();
2477
2478         type_t *type = skip_typeref(declaration->type);
2479         create_initializer_object(initializer, type, init_entity, NULL, 0);
2480
2481         assert(current_ir_graph == get_const_code_irg());
2482         current_ir_graph = old_current_ir_graph;
2483
2484         ir_node *src_addr  = create_symconst(dbgi, init_entity);
2485         ir_node *copyb     = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2486
2487         ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2488         set_store(copyb_mem);
2489 }
2490
2491 static void create_initializer(declaration_t *declaration)
2492 {
2493         initializer_t *initializer = declaration->init.initializer;
2494         if(initializer == NULL)
2495                 return;
2496
2497         declaration_type_t declaration_type
2498                 = (declaration_type_t) declaration->declaration_type;
2499         if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2500                 create_initializer_local_variable_entity(declaration);
2501                 return;
2502         }
2503
2504         if(initializer->type == INITIALIZER_VALUE) {
2505                 initializer_value_t *initializer_value = &initializer->value;
2506
2507                 ir_node *value = expression_to_firm(initializer_value->value);
2508
2509                 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2510                         set_value(declaration->v.value_number, value);
2511                 } else {
2512                         assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2513
2514                         ir_entity *entity = declaration->v.entity;
2515
2516                         set_entity_variability(entity, variability_initialized);
2517                         set_atomic_ent_value(entity, value);
2518                 }
2519         } else {
2520                 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2521                                 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2522
2523                 ir_entity *entity = declaration->v.entity;
2524                 set_entity_variability(entity, variability_initialized);
2525
2526                 type_t *type = skip_typeref(declaration->type);
2527                 create_initializer_object(initializer, type, entity, NULL, 0);
2528         }
2529 }
2530
2531 static void create_local_variable(declaration_t *declaration)
2532 {
2533         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2534
2535         bool needs_entity = declaration->address_taken;
2536         type_t *type = skip_typeref(declaration->type);
2537
2538         if(is_type_array(type) || is_type_compound(type)) {
2539                 needs_entity = true;
2540         }
2541
2542         if(needs_entity) {
2543                 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2544                 create_declaration_entity(declaration,
2545                                           DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2546                                           frame_type);
2547         } else {
2548                 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2549                 declaration->v.value_number   = next_value_number_function;
2550                 ++next_value_number_function;
2551         }
2552
2553         create_initializer(declaration);
2554 }
2555
2556 static void create_local_static_variable(declaration_t *declaration)
2557 {
2558         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2559
2560         type_t    *type        = skip_typeref(declaration->type);
2561         ir_type   *global_type = get_glob_type();
2562         ident     *id          = unique_ident(declaration->symbol->string);
2563         ir_type   *irtype      = get_ir_type(type);
2564         ir_entity *entity      = new_entity(global_type, id, irtype);
2565         set_entity_ld_ident(entity, id);
2566
2567         declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2568         declaration->v.entity         = entity;
2569         set_entity_variability(entity, variability_uninitialized);
2570         set_entity_visibility(entity, visibility_local);
2571
2572         ir_graph *old_current_ir_graph = current_ir_graph;
2573         current_ir_graph = get_const_code_irg();
2574
2575         create_initializer(declaration);
2576
2577         assert(current_ir_graph == get_const_code_irg());
2578         current_ir_graph = old_current_ir_graph;
2579 }
2580
2581 static void declaration_statement_to_firm(declaration_statement_t *statement)
2582 {
2583         declaration_t *declaration = statement->declarations_begin;
2584         declaration_t *end         = statement->declarations_end->next;
2585         for( ; declaration != end; declaration = declaration->next) {
2586                 type_t *type = skip_typeref(declaration->type);
2587
2588                 switch ((storage_class_tag_t) declaration->storage_class) {
2589                 case STORAGE_CLASS_TYPEDEF:
2590                         continue;
2591                 case STORAGE_CLASS_STATIC:
2592                         create_local_static_variable(declaration);
2593                         continue;
2594                 case STORAGE_CLASS_ENUM_ENTRY:
2595                         panic("enum entry declaration in local block found");
2596                 case STORAGE_CLASS_EXTERN:
2597                         panic("extern declaration in local block found");
2598                 case STORAGE_CLASS_NONE:
2599                 case STORAGE_CLASS_AUTO:
2600                 case STORAGE_CLASS_REGISTER:
2601                         if(is_type_function(type)) {
2602                                 panic("nested functions not supported yet");
2603                         } else {
2604                                 create_local_variable(declaration);
2605                         }
2606                         continue;
2607                 case STORAGE_CLASS_THREAD:
2608                 case STORAGE_CLASS_THREAD_EXTERN:
2609                 case STORAGE_CLASS_THREAD_STATIC:
2610                         break;
2611                 }
2612                 panic("invalid storage class found");
2613         }
2614 }
2615
2616 static void create_jump_statement(const statement_t *statement,
2617                                   ir_node *target_block)
2618 {
2619         if(get_cur_block() == NULL)
2620                 return;
2621
2622         dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2623         ir_node  *jump = new_d_Jmp(dbgi);
2624         add_immBlock_pred(target_block, jump);
2625
2626         set_cur_block(NULL);
2627 }
2628
2629 static void switch_statement_to_firm(const switch_statement_t *statement)
2630 {
2631         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2632
2633         ir_node *expression  = expression_to_firm(statement->expression);
2634         ir_node *cond        = new_d_Cond(dbgi, expression);
2635         ir_node *break_block = new_immBlock();
2636
2637         set_cur_block(NULL);
2638
2639         ir_node *const old_switch_cond       = current_switch_cond;
2640         ir_node *const old_break_label       = break_label;
2641         const bool     old_saw_default_label = saw_default_label;
2642         current_switch_cond                  = cond;
2643         break_label                          = break_block;
2644
2645         statement_to_firm(statement->body);
2646
2647         if(get_cur_block() != NULL) {
2648                 ir_node *jmp = new_Jmp();
2649                 add_immBlock_pred(break_block, jmp);
2650         }
2651
2652         if (!saw_default_label) {
2653                 set_cur_block(get_nodes_block(cond));
2654                 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2655                                                         MAGIC_DEFAULT_PN_NUMBER);
2656                 add_immBlock_pred(break_block, proj);
2657         }
2658
2659         assert(current_switch_cond == cond);
2660         assert(break_label         == break_block);
2661         current_switch_cond = old_switch_cond;
2662         break_label         = old_break_label;
2663         saw_default_label   = old_saw_default_label;
2664
2665         mature_immBlock(break_block);
2666         set_cur_block(break_block);
2667 }
2668
2669 static long fold_constant(const expression_t *expression)
2670 {
2671         ir_graph *old_current_ir_graph = current_ir_graph;
2672         current_ir_graph = get_const_code_irg();
2673
2674         ir_node *cnst = expression_to_firm(expression);
2675         if(!is_Const(cnst)) {
2676                 panic("couldn't fold constantl");
2677         }
2678         tarval *tv = get_Const_tarval(cnst);
2679         if(!tarval_is_long(tv)) {
2680                 panic("folded constant not an integer");
2681         }
2682
2683         long res = get_tarval_long(tv);
2684
2685         current_ir_graph = old_current_ir_graph;
2686         return res;
2687 }
2688
2689 static void case_label_to_firm(const case_label_statement_t *statement)
2690 {
2691         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2692
2693         ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2694
2695         /* let's create a node and hope firm constant folding creates a Const
2696          * node... */
2697         ir_node *proj;
2698         set_cur_block(get_nodes_block(current_switch_cond));
2699         if(statement->expression) {
2700                 long pn = fold_constant(statement->expression);
2701                 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2702                         /* oops someone detected our cheating... */
2703                         panic("magic default pn used");
2704                 }
2705                 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2706         } else {
2707                 saw_default_label = true;
2708                 proj = new_d_defaultProj(dbgi, current_switch_cond,
2709                                          MAGIC_DEFAULT_PN_NUMBER);
2710         }
2711
2712         ir_node *block = new_immBlock();
2713         if (fallthrough != NULL) {
2714                 add_immBlock_pred(block, fallthrough);
2715         }
2716         add_immBlock_pred(block, proj);
2717         mature_immBlock(block);
2718
2719         statement_to_firm(statement->label_statement);
2720 }
2721
2722 static ir_node *get_label_block(declaration_t *label)
2723 {
2724         assert(label->namespc == NAMESPACE_LABEL);
2725
2726         if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2727                 return label->v.block;
2728         }
2729         assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2730
2731         ir_node *old_cur_block = get_cur_block();
2732         ir_node *block         = new_immBlock();
2733         set_cur_block(old_cur_block);
2734
2735         label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2736         label->v.block          = block;
2737
2738         ARR_APP1(ir_node *, imature_blocks, block);
2739
2740         return block;
2741 }
2742
2743 static void label_to_firm(const label_statement_t *statement)
2744 {
2745         ir_node *block = get_label_block(statement->label);
2746
2747         if(get_cur_block() != NULL) {
2748                 ir_node *jmp = new_Jmp();
2749                 add_immBlock_pred(block, jmp);
2750         }
2751
2752         set_cur_block(block);
2753         keep_alive(block);
2754
2755         statement_to_firm(statement->label_statement);
2756 }
2757
2758 static void goto_to_firm(const goto_statement_t *statement)
2759 {
2760         if(get_cur_block() == NULL)
2761                 return;
2762
2763         ir_node *block = get_label_block(statement->label);
2764         ir_node *jmp   = new_Jmp();
2765         add_immBlock_pred(block, jmp);
2766
2767         set_cur_block(NULL);
2768 }
2769
2770 static void statement_to_firm(statement_t *statement)
2771 {
2772         switch(statement->type) {
2773         case STATEMENT_INVALID:
2774                 panic("invalid statement found");
2775         case STATEMENT_COMPOUND:
2776                 compound_statement_to_firm(&statement->compound);
2777                 return;
2778         case STATEMENT_RETURN:
2779                 return_statement_to_firm(&statement->returns);
2780                 return;
2781         case STATEMENT_EXPRESSION:
2782                 expression_statement_to_firm(&statement->expression);
2783                 return;
2784         case STATEMENT_IF:
2785                 if_statement_to_firm(&statement->ifs);
2786                 return;
2787         case STATEMENT_WHILE:
2788                 while_statement_to_firm(&statement->whiles);
2789                 return;
2790         case STATEMENT_DO_WHILE:
2791                 do_while_statement_to_firm(&statement->do_while);
2792                 return;
2793         case STATEMENT_DECLARATION:
2794                 declaration_statement_to_firm(&statement->declaration);
2795                 return;
2796         case STATEMENT_BREAK:
2797                 create_jump_statement(statement, break_label);
2798                 return;
2799         case STATEMENT_CONTINUE:
2800                 create_jump_statement(statement, continue_label);
2801                 return;
2802         case STATEMENT_SWITCH:
2803                 switch_statement_to_firm(&statement->switchs);
2804                 return;
2805         case STATEMENT_CASE_LABEL:
2806                 case_label_to_firm(&statement->case_label);
2807                 return;
2808         case STATEMENT_FOR:
2809                 for_statement_to_firm(&statement->fors);
2810                 return;
2811         case STATEMENT_LABEL:
2812                 label_to_firm(&statement->label);
2813                 return;
2814         case STATEMENT_GOTO:
2815                 goto_to_firm(&statement->gotos);
2816                 return;
2817         case STATEMENT_ASM:
2818                 break;
2819         }
2820         panic("Statement not implemented\n");
2821 }
2822
2823 static int count_local_declarations(const declaration_t *      decl,
2824                                     const declaration_t *const end)
2825 {
2826         int count = 0;
2827         for (; decl != end; decl = decl->next) {
2828                 const type_t *type = skip_typeref(decl->type);
2829                 switch (type->type) {
2830                         case TYPE_ATOMIC:
2831                         case TYPE_ENUM:
2832                         case TYPE_POINTER:
2833                                 if (!decl->address_taken)
2834                                         ++count;
2835                                 break;
2836
2837                         default: break;
2838                 }
2839         }
2840         return count;
2841 }
2842
2843 static int count_decls_in_stmts(const statement_t *stmt)
2844 {
2845         int count = 0;
2846         for (; stmt != NULL; stmt = stmt->base.next) {
2847                 switch (stmt->type) {
2848                         case STATEMENT_DECLARATION: {
2849                                 const declaration_statement_t *const decl_stmt =
2850                                         (const declaration_statement_t*)stmt;
2851                                 count += count_local_declarations(decl_stmt->declarations_begin,
2852                                                                   decl_stmt->declarations_end->next);
2853                                 break;
2854                         }
2855
2856                         case STATEMENT_COMPOUND: {
2857                                 const compound_statement_t *const comp =
2858                                         (const compound_statement_t*)stmt;
2859                                 count += count_decls_in_stmts(comp->statements);
2860                                 break;
2861                         }
2862
2863                         case STATEMENT_IF: {
2864                                 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2865                                 count += count_decls_in_stmts(if_stmt->true_statement);
2866                                 count += count_decls_in_stmts(if_stmt->false_statement);
2867                                 break;
2868                         }
2869
2870                         case STATEMENT_SWITCH: {
2871                                 const switch_statement_t *const switch_stmt =
2872                                         (const switch_statement_t*)stmt;
2873                                 count += count_decls_in_stmts(switch_stmt->body);
2874                                 break;
2875                         }
2876
2877                         case STATEMENT_LABEL: {
2878                                 const label_statement_t *const label_stmt =
2879                                         (const label_statement_t*)stmt;
2880                                 count += count_decls_in_stmts(label_stmt->label_statement);
2881                                 break;
2882                         }
2883
2884                         case STATEMENT_WHILE: {
2885                                 const while_statement_t *const while_stmt =
2886                                         (const while_statement_t*)stmt;
2887                                 count += count_decls_in_stmts(while_stmt->body);
2888                                 break;
2889                         }
2890
2891                         case STATEMENT_DO_WHILE: {
2892                                 const do_while_statement_t *const do_while_stmt =
2893                                         (const do_while_statement_t*)stmt;
2894                                 count += count_decls_in_stmts(do_while_stmt->body);
2895                                 break;
2896                         }
2897
2898                         case STATEMENT_FOR: {
2899                                 const for_statement_t *const for_stmt =
2900                                         (const for_statement_t*)stmt;
2901                                 /* TODO initialisation */
2902                                 count += count_decls_in_stmts(for_stmt->body);
2903                                 break;
2904                         }
2905
2906                         case STATEMENT_ASM:
2907                         case STATEMENT_BREAK:
2908                         case STATEMENT_CASE_LABEL:
2909                         case STATEMENT_CONTINUE:
2910                         case STATEMENT_EXPRESSION:
2911                         case STATEMENT_GOTO:
2912                         case STATEMENT_INVALID:
2913                         case STATEMENT_RETURN:
2914                                 break;
2915                 }
2916         }
2917         return count;
2918 }
2919
2920 static int get_function_n_local_vars(declaration_t *declaration)
2921 {
2922         int count = 0;
2923
2924         /* count parameters */
2925         count += count_local_declarations(declaration->context.declarations, NULL);
2926
2927         /* count local variables declared in body */
2928         count += count_decls_in_stmts(declaration->init.statement);
2929
2930         /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2931          * for expression statements... */
2932         count += 10;
2933
2934         return count;
2935 }
2936
2937 static void initialize_function_parameters(declaration_t *declaration)
2938 {
2939         ir_graph        *irg             = current_ir_graph;
2940         ir_node         *args            = get_irg_args(irg);
2941         ir_node         *start_block     = get_irg_start_block(irg);
2942         ir_type         *function_irtype = get_ir_type(declaration->type);
2943
2944         int            n         = 0;
2945         declaration_t *parameter = declaration->context.declarations;
2946         for( ; parameter != NULL; parameter = parameter->next, ++n) {
2947                 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2948                 type_t *type = skip_typeref(parameter->type);
2949
2950                 bool needs_entity = parameter->address_taken;
2951                 assert(!is_type_array(type));
2952                 if(is_type_compound(type)) {
2953                         needs_entity = true;
2954                 }
2955
2956                 if(needs_entity) {
2957                         ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2958                         ident     *id     = new_id_from_str(parameter->symbol->string);
2959                         set_entity_ident(entity, id);
2960
2961                         parameter->declaration_type
2962                                 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2963                         parameter->v.entity = entity;
2964                         continue;
2965                 }
2966
2967                 ir_mode *mode = get_ir_mode(parameter->type);
2968                 long     pn   = n;
2969                 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2970
2971                 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2972                 parameter->v.value_number   = next_value_number_function;
2973                 ++next_value_number_function;
2974
2975                 set_value(parameter->v.value_number, proj);
2976         }
2977 }
2978
2979 static void create_function(declaration_t *declaration)
2980 {
2981         ir_entity *function_entity = get_function_entity(declaration);
2982
2983         if(declaration->init.statement == NULL)
2984                 return;
2985
2986         current_function_decl = declaration;
2987         current_function_name = NULL;
2988
2989         assert(imature_blocks == NULL);
2990         imature_blocks = NEW_ARR_F(ir_node*, 0);
2991
2992         int       n_local_vars = get_function_n_local_vars(declaration);
2993         ir_graph *irg          = new_ir_graph(function_entity, n_local_vars);
2994         ir_node  *first_block  = get_cur_block();
2995
2996         next_value_number_function = 0;
2997         initialize_function_parameters(declaration);
2998
2999         statement_to_firm(declaration->init.statement);
3000
3001         ir_node *end_block = get_irg_end_block(irg);
3002
3003         /* do we have a return statement yet? */
3004         if(get_cur_block() != NULL) {
3005                 type_t *type = skip_typeref(declaration->type);
3006                 assert(is_type_function(type));
3007                 const function_type_t *func_type   = &type->function;
3008                 const type_t          *return_type
3009                         = skip_typeref(func_type->return_type);
3010
3011                 ir_node *ret;
3012                 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3013                         ret = new_Return(get_store(), 0, NULL);
3014                 } else {
3015                         ir_mode *mode;
3016                         if(is_type_scalar(return_type)) {
3017                                 mode = get_ir_mode(func_type->return_type);
3018                         } else {
3019                                 mode = mode_P_data;
3020                         }
3021
3022                         ir_node *in[1];
3023                         // ยง5.1.2.2.3 main implicitly returns 0
3024                         if (strcmp(declaration->symbol->string, "main") == 0) {
3025                                 in[0] = new_Const(mode, get_mode_null(mode));
3026                         } else {
3027                                 in[0] = new_Unknown(mode);
3028                         }
3029                         ret = new_Return(get_store(), 1, in);
3030                 }
3031                 add_immBlock_pred(end_block, ret);
3032         }
3033
3034         for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3035                 mature_immBlock(imature_blocks[i]);
3036         }
3037         DEL_ARR_F(imature_blocks);
3038         imature_blocks = NULL;
3039
3040         mature_immBlock(first_block);
3041         mature_immBlock(end_block);
3042
3043         irg_finalize_cons(irg);
3044
3045         /* finalize the frame type */
3046         ir_type *frame_type = get_irg_frame_type(irg);
3047         int      n          = get_compound_n_members(frame_type);
3048         int      align_all  = 4;
3049         int      offset     = 0;
3050         for(int i = 0; i < n; ++i) {
3051                 ir_entity *entity      = get_compound_member(frame_type, i);
3052                 ir_type   *entity_type = get_entity_type(entity);
3053
3054                 int align = get_type_alignment_bytes(entity_type);
3055                 if(align > align_all)
3056                         align_all = align;
3057                 int misalign = 0;
3058                 if(align > 0) {
3059                         misalign  = offset % align;
3060                         if(misalign > 0) {
3061                                 offset += align - misalign;
3062                         }
3063                 }
3064
3065                 set_entity_offset(entity, offset);
3066                 offset += get_type_size_bytes(entity_type);
3067         }
3068         set_type_size_bytes(frame_type, offset);
3069         set_type_alignment_bytes(frame_type, align_all);
3070         set_type_state(frame_type, layout_fixed);
3071
3072         irg_vrfy(irg);
3073 }
3074
3075 static void create_global_variable(declaration_t *declaration)
3076 {
3077         ir_visibility  vis;
3078         ir_type       *var_type;
3079         switch ((storage_class_tag_t)declaration->storage_class) {
3080                 case STORAGE_CLASS_STATIC:
3081                         vis = visibility_local;
3082                         goto global_var;
3083
3084                 case STORAGE_CLASS_EXTERN:
3085                         vis = visibility_external_allocated;
3086                         goto global_var;
3087
3088                 case STORAGE_CLASS_NONE:
3089                         vis = visibility_external_visible;
3090                         goto global_var;
3091
3092                 case STORAGE_CLASS_THREAD:
3093                         vis = visibility_external_visible;
3094                         goto tls_var;
3095
3096                 case STORAGE_CLASS_THREAD_EXTERN:
3097                         vis = visibility_external_allocated;
3098                         goto tls_var;
3099
3100                 case STORAGE_CLASS_THREAD_STATIC:
3101                         vis = visibility_local;
3102                         goto tls_var;
3103
3104 tls_var:
3105                         var_type = get_tls_type();
3106                         goto create_var;
3107
3108 global_var:
3109                         var_type = get_glob_type();
3110                         goto create_var;
3111
3112 create_var:
3113                         create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3114                                                   var_type);
3115                         set_entity_visibility(declaration->v.entity, vis);
3116
3117                         current_ir_graph = get_const_code_irg();
3118                         create_initializer(declaration);
3119                         return;
3120
3121                 case STORAGE_CLASS_TYPEDEF:
3122                 case STORAGE_CLASS_AUTO:
3123                 case STORAGE_CLASS_REGISTER:
3124                 case STORAGE_CLASS_ENUM_ENTRY:
3125                         break;
3126         }
3127         panic("Invalid storage class for global variable");
3128 }
3129
3130 static void context_to_firm(context_t *context)
3131 {
3132         /* first pass: create declarations */
3133         declaration_t *declaration = context->declarations;
3134         for( ; declaration != NULL; declaration = declaration->next) {
3135                 if(declaration->namespc != NAMESPACE_NORMAL)
3136                         continue;
3137                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3138                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3139                         continue;
3140                 if(declaration->symbol == NULL)
3141                         continue;
3142
3143                 type_t *type = skip_typeref(declaration->type);
3144                 if(is_type_function(type)) {
3145                         get_function_entity(declaration);
3146                 } else {
3147                         create_global_variable(declaration);
3148                 }
3149         }
3150
3151         /* second pass: create code */
3152         declaration = context->declarations;
3153         for( ; declaration != NULL; declaration = declaration->next) {
3154                 if(declaration->namespc != NAMESPACE_NORMAL)
3155                         continue;
3156                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3157                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3158                         continue;
3159                 if(declaration->symbol == NULL)
3160                         continue;
3161
3162                 type_t *type = declaration->type;
3163                 if(type->type != TYPE_FUNCTION)
3164                         continue;
3165
3166                 create_function(declaration);
3167         }
3168 }
3169
3170 void translation_unit_to_firm(translation_unit_t *unit)
3171 {
3172         type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3173         type_void       = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3174         type_int        = make_atomic_type(ATOMIC_TYPE_INT,  TYPE_QUALIFIER_NONE);
3175
3176         ir_type_int        = get_ir_type(type_int);
3177         ir_type_const_char = get_ir_type(type_const_char);
3178         ir_type_wchar_t    = get_ir_type(type_wchar_t);
3179         ir_type_void       = get_ir_type(type_int); /* we don't have a real void
3180                                                        type in firm */
3181
3182         type_void->base.firm_type = ir_type_void;
3183
3184         /* just to be sure */
3185         continue_label      = NULL;
3186         break_label         = NULL;
3187         current_switch_cond = NULL;
3188
3189         context_to_firm(& unit->context);
3190 }