implement builtin_isgreater and friends
[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(const expression_type_t type)
1129 {
1130         switch(type) {
1131         case EXPR_BINARY_EQUAL:         return pn_Cmp_Eq;
1132         case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1133         case EXPR_BINARY_NOTEQUAL:      return pn_Cmp_Ne;
1134         case EXPR_BINARY_ISLESS:
1135         case EXPR_BINARY_LESS:          return pn_Cmp_Lt;
1136         case EXPR_BINARY_ISLESSEQUAL:
1137         case EXPR_BINARY_LESSEQUAL:     return pn_Cmp_Le;
1138         case EXPR_BINARY_ISGREATER:
1139         case EXPR_BINARY_GREATER:       return pn_Cmp_Gt;
1140         case EXPR_BINARY_ISGREATEREQUAL:
1141         case EXPR_BINARY_GREATEREQUAL:  return pn_Cmp_Ge;
1142         case EXPR_BINARY_ISUNORDERED:   return pn_Cmp_Uo;
1143
1144         default:
1145                 break;
1146         }
1147         panic("trying to get pn_Cmp from non-comparison binexpr type");
1148 }
1149
1150 static ir_node *create_lazy_op(const binary_expression_t *expression)
1151 {
1152         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1153         type_t   *type = expression->expression.datatype;
1154         ir_mode  *mode = get_ir_mode(type);
1155
1156         ir_node *cur_block = get_cur_block();
1157
1158         ir_node *one_block = new_immBlock();
1159         ir_node *one       = new_Const(mode, get_mode_one(mode));
1160         ir_node *jmp_one   = new_d_Jmp(dbgi);
1161
1162         ir_node *zero_block = new_immBlock();
1163         ir_node *zero       = new_Const(mode, get_mode_null(mode));
1164         ir_node *jmp_zero   = new_d_Jmp(dbgi);
1165
1166         set_cur_block(cur_block);
1167         create_condition_evaluation((const expression_t*) expression,
1168                                     one_block, zero_block);
1169         mature_immBlock(one_block);
1170         mature_immBlock(zero_block);
1171
1172         ir_node *common_block = new_immBlock();
1173         add_immBlock_pred(common_block, jmp_one);
1174         add_immBlock_pred(common_block, jmp_zero);
1175         mature_immBlock(common_block);
1176
1177         ir_node *in[2] = { one, zero };
1178         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
1179
1180         return val;
1181 }
1182
1183 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1184                                             ir_node *right, ir_mode *mode);
1185
1186 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1187                                         create_arithmetic_func func)
1188 {
1189         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1190         ir_node  *left  = expression_to_firm(expression->left);
1191         ir_node  *right = expression_to_firm(expression->right);
1192         type_t   *type  = expression->right->base.datatype;
1193         /* be careful with the modes, because in arithmetic assign nodes only
1194          * the right operand has the mode of the arithmetic already */
1195         ir_mode  *mode  = get_ir_mode(type);
1196         left            = create_conv(dbgi, left, mode);
1197         ir_node  *res   = func(dbgi, left, right, mode);
1198
1199         return res;
1200 }
1201
1202 static ir_node *pointer_arithmetic(ir_node  *const pointer,
1203                                    ir_node  *      integer,
1204                                    type_t   *const type,
1205                                    dbg_info *const dbgi,
1206                                    const create_arithmetic_func func)
1207 {
1208         pointer_type_t *const pointer_type = (pointer_type_t*)type;
1209         type_t         *const points_to    = pointer_type->points_to;
1210         const unsigned        elem_size    = get_type_size(points_to);
1211
1212         assert(elem_size >= 1);
1213         if (elem_size > 1) {
1214                 integer             = create_conv(dbgi, integer, mode_Is);
1215                 ir_node *const cnst = new_Const_long(mode_Is, (long)elem_size);
1216                 ir_node *const mul  = new_d_Mul(dbgi, integer, cnst, mode_Is);
1217                 integer = mul;
1218         }
1219
1220         ir_mode *const mode = get_ir_mode(type);
1221         return func(dbgi, pointer, integer, mode);
1222 }
1223
1224 static ir_node *create_arithmetic_assign_binop(
1225                 const binary_expression_t *expression, create_arithmetic_func func)
1226 {
1227         dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1228         type_t   *const type = skip_typeref(expression->expression.datatype);
1229         ir_node  *value;
1230
1231         if (is_type_pointer(type)) {
1232                 ir_node        *const pointer = expression_to_firm(expression->left);
1233                 ir_node        *      integer = expression_to_firm(expression->right);
1234                 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1235         } else {
1236                 value = create_arithmetic_binop(expression, func);
1237         }
1238
1239         ir_mode  *const mode = get_ir_mode(type);
1240         value = create_conv(dbgi, value, mode);
1241         set_value_for_expression(expression->left, value);
1242
1243         return value;
1244 }
1245
1246 static ir_node *create_add(const binary_expression_t *expression)
1247 {
1248         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1249         ir_node  *left  = expression_to_firm(expression->left);
1250         ir_node  *right = expression_to_firm(expression->right);
1251         type_t   *type  = expression->expression.datatype;
1252
1253         expression_t *expr_left  = expression->left;
1254         expression_t *expr_right = expression->right;
1255         type_t       *type_left  = skip_typeref(expr_left->base.datatype);
1256         type_t       *type_right = skip_typeref(expr_right->base.datatype);
1257
1258         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1259                 ir_mode *const mode = get_ir_mode(type);
1260                 return new_d_Add(dbgi, left, right, mode);
1261         }
1262
1263         if (is_type_pointer(type_left)) {
1264                 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1265         } else {
1266                 assert(is_type_pointer(type_right));
1267                 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1268         }
1269 }
1270
1271 static ir_node *create_sub(const binary_expression_t *expression)
1272 {
1273         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1274         expression_t *const expr_left  = expression->left;
1275         expression_t *const expr_right = expression->right;
1276         ir_node      *const left       = expression_to_firm(expr_left);
1277         ir_node      *const right      = expression_to_firm(expr_right);
1278         type_t       *const type       = expression->expression.datatype;
1279         type_t       *const type_left  = skip_typeref(expr_left->base.datatype);
1280         type_t       *const type_right = skip_typeref(expr_right->base.datatype);
1281
1282         if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1283                 ir_mode *const mode = get_ir_mode(type);
1284                 return new_d_Sub(dbgi, left, right, mode);
1285         } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1286                 const pointer_type_t *const ptr_type = &type_left->pointer;
1287                 const unsigned elem_size             = get_type_size(ptr_type->points_to);
1288                 ir_mode *const mode   = get_ir_mode(type);
1289                 ir_node *const sub    = new_d_Sub(dbgi, left, right, mode);
1290                 ir_node *const cnst   = new_Const_long(mode_Is, (long)elem_size);
1291                 ir_node *const no_mem = new_NoMem();
1292                 ir_node *const div    = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1293                                                   op_pin_state_floats);
1294                 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1295         }
1296
1297         assert(is_type_pointer(type_left));
1298         return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1299 }
1300
1301 static ir_node *create_shift(const binary_expression_t *expression)
1302 {
1303         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1304         ir_node  *left  = expression_to_firm(expression->left);
1305         ir_node  *right = expression_to_firm(expression->right);
1306         type_t   *type  = expression->expression.datatype;
1307         ir_mode  *mode  = get_ir_mode(type);
1308
1309         /* firm always wants the shift count to be unsigned */
1310         right = create_conv(dbgi, right, mode_Iu);
1311
1312         ir_node *res;
1313
1314         switch(expression->expression.type) {
1315         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1316         case EXPR_BINARY_SHIFTLEFT:
1317                 res = new_d_Shl(dbgi, left, right, mode);
1318                 break;
1319         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1320         case EXPR_BINARY_SHIFTRIGHT: {
1321                  expression_t *expr_left = expression->left;
1322                  type_t       *type_left = skip_typeref(expr_left->base.datatype);
1323
1324                  if(is_type_signed(type_left)) {
1325                         res = new_d_Shrs(dbgi, left, right, mode);
1326                  } else {
1327                          res = new_d_Shr(dbgi, left, right, mode);
1328                  }
1329                  break;
1330         }
1331         default:
1332                 panic("create shift op called for non-shift op");
1333         }
1334
1335         return res;
1336 }
1337
1338
1339 static ir_node *create_divmod(const binary_expression_t *expression)
1340 {
1341         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1342         ir_node  *left  = expression_to_firm(expression->left);
1343         ir_node  *right = expression_to_firm(expression->right);
1344         ir_node  *pin   = new_Pin(new_NoMem());
1345         /* be careful with the modes, because in arithmetic assign nodes only
1346          * the right operand has the mode of the arithmetic already */
1347         type_t   *type  = expression->right->base.datatype;
1348         ir_mode  *mode  = get_ir_mode(type);
1349         left            = create_conv(dbgi, left, mode);
1350         ir_node  *op;
1351         ir_node  *res;
1352
1353         switch (expression->expression.type) {
1354         case EXPR_BINARY_DIV:
1355         case EXPR_BINARY_DIV_ASSIGN:
1356                 if(mode_is_float(mode)) {
1357                         op  = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1358                         res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1359                 } else {
1360                         op  = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1361                         res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1362                 }
1363                 break;
1364
1365         case EXPR_BINARY_MOD:
1366         case EXPR_BINARY_MOD_ASSIGN:
1367                 assert(!mode_is_float(mode));
1368                 op  = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1369                 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1370                 break;
1371
1372         default: panic("unexpected binary expression type in create_divmod()");
1373         }
1374
1375         return res;
1376 }
1377
1378 static ir_node *create_arithmetic_assign_divmod(
1379                 const binary_expression_t *expression)
1380 {
1381         ir_node  *      value = create_divmod(expression);
1382         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1383         type_t   *const type  = expression->expression.datatype;
1384         ir_mode  *const mode  = get_ir_mode(type);
1385
1386         assert(type->type != TYPE_POINTER);
1387
1388         value = create_conv(dbgi, value, mode);
1389         set_value_for_expression(expression->left, value);
1390
1391         return value;
1392 }
1393
1394 static ir_node *create_arithmetic_assign_shift(
1395                 const binary_expression_t *expression)
1396 {
1397         ir_node  *      value = create_shift(expression);
1398         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1399         type_t   *const type  = expression->expression.datatype;
1400         ir_mode  *const mode  = get_ir_mode(type);
1401
1402         value = create_conv(dbgi, value, mode);
1403         set_value_for_expression(expression->left, value);
1404
1405         return value;
1406 }
1407
1408 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1409 {
1410         expression_type_t type = expression->expression.type;
1411
1412         switch(type) {
1413         case EXPR_BINARY_EQUAL:
1414         case EXPR_BINARY_NOTEQUAL:
1415         case EXPR_BINARY_LESS:
1416         case EXPR_BINARY_LESSEQUAL:
1417         case EXPR_BINARY_GREATER:
1418         case EXPR_BINARY_GREATEREQUAL:
1419         case EXPR_BINARY_ISGREATER:
1420         case EXPR_BINARY_ISGREATEREQUAL:
1421         case EXPR_BINARY_ISLESS:
1422         case EXPR_BINARY_ISLESSEQUAL:
1423         case EXPR_BINARY_ISLESSGREATER:
1424         case EXPR_BINARY_ISUNORDERED: {
1425                 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1426                 ir_node *left  = expression_to_firm(expression->left);
1427                 ir_node *right = expression_to_firm(expression->right);
1428                 ir_node *cmp   = new_d_Cmp(dbgi, left, right);
1429                 long     pnc   = get_pnc(type);
1430                 ir_node *proj  = new_d_Proj(dbgi, cmp, mode_b, pnc);
1431                 return proj;
1432         }
1433         case EXPR_BINARY_ASSIGN: {
1434                 ir_node *right = expression_to_firm(expression->right);
1435                 set_value_for_expression(expression->left, right);
1436
1437                 return right;
1438         }
1439         case EXPR_BINARY_ADD:
1440                 return create_add(expression);
1441         case EXPR_BINARY_SUB:
1442                 return create_sub(expression);
1443         case EXPR_BINARY_MUL:
1444                 return create_arithmetic_binop(expression, new_d_Mul);
1445         case EXPR_BINARY_BITWISE_AND:
1446                 return create_arithmetic_binop(expression, new_d_And);
1447         case EXPR_BINARY_BITWISE_OR:
1448                 return create_arithmetic_binop(expression, new_d_Or);
1449         case EXPR_BINARY_BITWISE_XOR:
1450                 return create_arithmetic_binop(expression, new_d_Eor);
1451         case EXPR_BINARY_SHIFTLEFT:
1452         case EXPR_BINARY_SHIFTRIGHT:
1453                 return create_shift(expression);
1454         case EXPR_BINARY_DIV:
1455         case EXPR_BINARY_MOD:
1456                 return create_divmod(expression);
1457         case EXPR_BINARY_LOGICAL_AND:
1458         case EXPR_BINARY_LOGICAL_OR:
1459                 return create_lazy_op(expression);
1460         case EXPR_BINARY_COMMA:
1461                 expression_to_firm(expression->left);
1462                 return expression_to_firm(expression->right);
1463         case EXPR_BINARY_ADD_ASSIGN:
1464                 return create_arithmetic_assign_binop(expression, new_d_Add);
1465         case EXPR_BINARY_SUB_ASSIGN:
1466                 return create_arithmetic_assign_binop(expression, new_d_Sub);
1467         case EXPR_BINARY_MUL_ASSIGN:
1468                 return create_arithmetic_assign_binop(expression, new_d_Mul);
1469         case EXPR_BINARY_DIV_ASSIGN:
1470                 return create_arithmetic_assign_divmod(expression);
1471         case EXPR_BINARY_BITWISE_AND_ASSIGN:
1472                 return create_arithmetic_assign_binop(expression, new_d_And);
1473         case EXPR_BINARY_BITWISE_OR_ASSIGN:
1474                 return create_arithmetic_assign_binop(expression, new_d_Or);
1475         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1476                 return create_arithmetic_assign_binop(expression, new_d_Eor);
1477         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1478         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1479                 return create_arithmetic_assign_shift(expression);
1480         default:
1481                 panic("TODO binexpr type");
1482         }
1483 }
1484
1485 static ir_node *array_access_addr(const array_access_expression_t *expression)
1486 {
1487         dbg_info *dbgi      = get_dbg_info(&expression->expression.source_position);
1488         ir_node  *base_addr = expression_to_firm(expression->array_ref);
1489         ir_node  *offset    = expression_to_firm(expression->index);
1490         offset              = create_conv(dbgi, offset, mode_Iu);
1491
1492         type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1493         assert(is_type_pointer(ref_type));
1494         pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1495
1496         unsigned elem_size       = get_type_size(pointer_type->points_to);
1497         ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1498         ir_node *real_offset     = new_d_Mul(dbgi, offset, elem_size_const,
1499                                              mode_Iu);
1500         ir_node *result          = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1501
1502         return result;
1503 }
1504
1505 static ir_node *array_access_to_firm(
1506                 const array_access_expression_t *expression)
1507 {
1508         dbg_info *dbgi   = get_dbg_info(&expression->expression.source_position);
1509         ir_node  *addr   = array_access_addr(expression);
1510         type_t   *type   = revert_automatic_type_conversion(
1511                         (const expression_t*) expression);
1512         type             = skip_typeref(type);
1513         ir_type  *irtype = get_ir_type(type);
1514
1515         return deref_address(irtype, addr, dbgi);
1516 }
1517
1518 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1519 {
1520         type_t *type = expression->type;
1521         if(type == NULL) {
1522                 type = expression->size_expression->base.datatype;
1523                 assert(type != NULL);
1524         }
1525
1526         ir_mode  *mode      = get_ir_mode(expression->expression.datatype);
1527         unsigned  size      = get_type_size(type);
1528         ir_node  *size_node = new_Const_long(mode, size);
1529
1530         return size_node;
1531 }
1532
1533 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1534 {
1535         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1536
1537         ir_node *cur_block   = get_cur_block();
1538
1539         /* create the true block */
1540         ir_node *true_block  = new_immBlock();
1541
1542         ir_node *true_val = expression_to_firm(expression->true_expression);
1543         ir_node *true_jmp = new_Jmp();
1544
1545         /* create the false block */
1546         ir_node *false_block = new_immBlock();
1547
1548         ir_node *false_val = expression_to_firm(expression->false_expression);
1549         ir_node *false_jmp = new_Jmp();
1550
1551         /* create the condition evaluation */
1552         set_cur_block(cur_block);
1553         create_condition_evaluation(expression->condition, true_block, false_block);
1554         mature_immBlock(true_block);
1555         mature_immBlock(false_block);
1556
1557         /* create the common block */
1558         ir_node *common_block = new_immBlock();
1559         add_immBlock_pred(common_block, true_jmp);
1560         add_immBlock_pred(common_block, false_jmp);
1561         mature_immBlock(common_block);
1562
1563         /* TODO improve static semantics, so either both or no values are NULL */
1564         if (true_val == NULL || false_val == NULL)
1565                 return NULL;
1566
1567         ir_node *in[2] = { true_val, false_val };
1568         ir_mode *mode  = get_irn_mode(true_val);
1569         assert(get_irn_mode(false_val) == mode);
1570         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
1571
1572         return val;
1573 }
1574
1575 static ir_node *select_addr(const select_expression_t *expression)
1576 {
1577         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1578
1579         ir_node *compound_addr = expression_to_firm(expression->compound);
1580
1581         declaration_t *entry = expression->compound_entry;
1582         assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1583         ir_entity     *entity = entry->v.entity;
1584
1585         assert(entity != NULL);
1586
1587         ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1588
1589         return sel;
1590 }
1591
1592 static ir_node *select_to_firm(const select_expression_t *expression)
1593 {
1594         dbg_info *dbgi   = get_dbg_info(&expression->expression.source_position);
1595         ir_node  *addr   = select_addr(expression);
1596         type_t   *type   = revert_automatic_type_conversion(
1597                         (const expression_t*) expression);
1598         type             = skip_typeref(type);
1599         ir_type  *irtype = get_ir_type(type);
1600
1601         return deref_address(irtype, addr, dbgi);
1602 }
1603
1604 /* Values returned by __builtin_classify_type. */
1605 typedef enum gcc_type_class
1606 {
1607         no_type_class = -1,
1608         void_type_class,
1609         integer_type_class,
1610         char_type_class,
1611         enumeral_type_class,
1612         boolean_type_class,
1613         pointer_type_class,
1614         reference_type_class,
1615         offset_type_class,
1616         real_type_class,
1617         complex_type_class,
1618         function_type_class,
1619         method_type_class,
1620         record_type_class,
1621         union_type_class,
1622         array_type_class,
1623         string_type_class,
1624         set_type_class,
1625         file_type_class,
1626         lang_type_class
1627 } gcc_type_class;
1628
1629 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1630 {
1631         const type_t *const type = expr->type_expression->base.datatype;
1632
1633         gcc_type_class tc;
1634         switch (type->type)
1635         {
1636                 case TYPE_ATOMIC: {
1637                         const atomic_type_t *const atomic_type = &type->atomic;
1638                         switch (atomic_type->atype) {
1639                                 // should not be reached
1640                                 case ATOMIC_TYPE_INVALID:
1641                                         tc = no_type_class;
1642                                         break;
1643
1644                                 // gcc cannot do that
1645                                 case ATOMIC_TYPE_VOID:
1646                                         tc = void_type_class;
1647                                         break;
1648
1649                                 case ATOMIC_TYPE_CHAR:      // gcc handles this as integer
1650                                 case ATOMIC_TYPE_SCHAR:     // gcc handles this as integer
1651                                 case ATOMIC_TYPE_UCHAR:     // gcc handles this as integer
1652                                 case ATOMIC_TYPE_SHORT:
1653                                 case ATOMIC_TYPE_USHORT:
1654                                 case ATOMIC_TYPE_INT:
1655                                 case ATOMIC_TYPE_UINT:
1656                                 case ATOMIC_TYPE_LONG:
1657                                 case ATOMIC_TYPE_ULONG:
1658                                 case ATOMIC_TYPE_LONGLONG:
1659                                 case ATOMIC_TYPE_ULONGLONG:
1660                                 case ATOMIC_TYPE_BOOL:      // gcc handles this as integer
1661                                         tc = integer_type_class;
1662                                         break;
1663
1664                                 case ATOMIC_TYPE_FLOAT:
1665                                 case ATOMIC_TYPE_DOUBLE:
1666                                 case ATOMIC_TYPE_LONG_DOUBLE:
1667                                         tc = real_type_class;
1668                                         break;
1669
1670 #ifdef PROVIDE_COMPLEX
1671                                 case ATOMIC_TYPE_FLOAT_COMPLEX:
1672                                 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1673                                 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1674                                         tc = complex_type_class;
1675                                         break;
1676                                 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1677                                 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1678                                 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1679                                         tc = complex_type_class;
1680                                         break;
1681 #endif
1682
1683                                 default:
1684                                         panic("Unimplemented case in classify_type_to_firm().");
1685                         }
1686                         break;
1687                 }
1688
1689                 case TYPE_ARRAY:           // gcc handles this as pointer
1690                 case TYPE_FUNCTION:        // gcc handles this as pointer
1691                 case TYPE_POINTER:         tc = pointer_type_class; break;
1692                 case TYPE_COMPOUND_STRUCT: tc = record_type_class;  break;
1693                 case TYPE_COMPOUND_UNION:  tc = union_type_class;   break;
1694
1695                 // gcc handles this as integer
1696                 case TYPE_ENUM:            tc = integer_type_class; break;
1697
1698                 default:
1699                         panic("Unimplemented case in classify_type_to_firm().");
1700         }
1701
1702         dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1703         ir_mode  *const mode = mode_Is;
1704         tarval   *const tv   = new_tarval_from_long(tc, mode);
1705         return new_d_Const(dbgi, mode, tv);
1706 }
1707
1708 static ir_node *function_name_to_firm(
1709                 const string_literal_expression_t *const expr)
1710 {
1711         if (current_function_name == NULL) {
1712                 const source_position_t *const src_pos =
1713                         &expr->expression.source_position;
1714                 const char *const name = current_function_decl->symbol->string;
1715                 current_function_name = string_to_firm(src_pos, "__func__", name);
1716         }
1717
1718         return current_function_name;
1719 }
1720
1721 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1722 {
1723         statement_t *statement = expr->statement;
1724
1725         assert(statement->type == STATEMENT_COMPOUND);
1726         return compound_statement_to_firm((compound_statement_t*) statement);
1727 }
1728
1729 static ir_node *va_start_expression_to_firm(
1730         const va_start_expression_t *const expr)
1731 {
1732         ir_type   *const method_type = get_ir_type(current_function_decl->type);
1733         int        const n           = get_method_n_params(method_type) - 1;
1734         ir_entity *const parm_ent    = get_method_value_param_ent(method_type, n);
1735         ir_node   *const arg_base    = get_irg_value_param_base(current_ir_graph);
1736         dbg_info  *const dbgi        =
1737                 get_dbg_info(&expr->expression.source_position);
1738         ir_node   *const no_mem      = new_NoMem();
1739         ir_node   *const arg_sel     =
1740                 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
1741
1742         size_t     const parm_size   = get_type_size(expr->parameter->type);
1743         ir_node   *const cnst        = new_Const_long(mode_Iu, parm_size);
1744         ir_node   *const add         = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
1745         set_value_for_expression(expr->ap, add);
1746
1747         return NULL;
1748 }
1749
1750 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
1751 {
1752         ir_type  *const irtype = get_ir_type(expr->expression.datatype);
1753         ir_node  *const ap     = expression_to_firm(expr->ap);
1754         dbg_info *const dbgi   = get_dbg_info(&expr->expression.source_position);
1755         ir_node  *const res    = deref_address(irtype, ap, dbgi);
1756
1757         size_t     const parm_size   = get_type_size(expr->expression.datatype);
1758         ir_node   *const cnst        = new_Const_long(mode_Iu, parm_size);
1759         ir_node   *const add         = new_d_Add(dbgi, ap, cnst, mode_P_data);
1760         set_value_for_expression(expr->ap, add);
1761
1762         return res;
1763 }
1764
1765 static ir_node *dereference_addr(const unary_expression_t *const expression)
1766 {
1767         assert(expression->expression.type == EXPR_UNARY_DEREFERENCE);
1768         return expression_to_firm(expression->value);
1769 }
1770
1771 static ir_node *expression_to_addr(const expression_t *expression)
1772 {
1773         switch(expression->type) {
1774         case EXPR_REFERENCE:
1775                 return reference_addr(&expression->reference);
1776         case EXPR_ARRAY_ACCESS:
1777                 return array_access_addr(&expression->array_access);
1778         case EXPR_SELECT:
1779                 return select_addr(&expression->select);
1780         case EXPR_CALL:
1781                 return call_expression_to_firm(&expression->call);
1782         case EXPR_UNARY_DEREFERENCE: {
1783                 return dereference_addr(&expression->unary);
1784         }
1785         default:
1786                 break;
1787         }
1788         panic("trying to get address of non-lvalue");
1789 }
1790
1791 static ir_node *_expression_to_firm(const expression_t *expression)
1792 {
1793         switch(expression->type) {
1794         case EXPR_CONST:
1795                 return const_to_firm(&expression->conste);
1796         case EXPR_STRING_LITERAL:
1797                 return string_literal_to_firm(&expression->string);
1798         case EXPR_WIDE_STRING_LITERAL:
1799                 return wide_string_literal_to_firm(&expression->wide_string);
1800         case EXPR_REFERENCE:
1801                 return reference_expression_to_firm(&expression->reference);
1802         case EXPR_CALL:
1803                 return call_expression_to_firm(&expression->call);
1804         EXPR_UNARY_CASES
1805                 return unary_expression_to_firm(&expression->unary);
1806         EXPR_BINARY_CASES
1807                 return binary_expression_to_firm(&expression->binary);
1808         case EXPR_ARRAY_ACCESS:
1809                 return array_access_to_firm(&expression->array_access);
1810         case EXPR_SIZEOF:
1811                 return sizeof_to_firm(&expression->sizeofe);
1812         case EXPR_CONDITIONAL:
1813                 return conditional_to_firm(&expression->conditional);
1814         case EXPR_SELECT:
1815                 return select_to_firm(&expression->select);
1816         case EXPR_CLASSIFY_TYPE:
1817                 return classify_type_to_firm(&expression->classify_type);
1818         case EXPR_FUNCTION:
1819         case EXPR_PRETTY_FUNCTION:
1820                 return function_name_to_firm(&expression->string);
1821         case EXPR_STATEMENT:
1822                 return statement_expression_to_firm(&expression->statement);
1823         case EXPR_VA_START:
1824                 return va_start_expression_to_firm(&expression->va_starte);
1825         case EXPR_VA_ARG:
1826                 return va_arg_expression_to_firm(&expression->va_arge);
1827         case EXPR_OFFSETOF:
1828         case EXPR_BUILTIN_SYMBOL:
1829                 panic("unimplemented expression found");
1830
1831         case EXPR_UNKNOWN:
1832         case EXPR_INVALID:
1833                 break;
1834         }
1835         panic("invalid expression found");
1836 }
1837
1838 static ir_node *expression_to_firm(const expression_t *expression)
1839 {
1840         ir_node *res = _expression_to_firm(expression);
1841
1842         if(res != NULL && get_irn_mode(res) == mode_b) {
1843                 ir_mode *mode = get_ir_mode(expression->base.datatype);
1844                 res           = create_conv(NULL, res, mode);
1845         }
1846
1847         return res;
1848 }
1849
1850 static ir_node *expression_to_modeb(const expression_t *expression)
1851 {
1852         ir_node *res = _expression_to_firm(expression);
1853         res          = create_conv(NULL, res, mode_b);
1854
1855         return res;
1856 }
1857
1858 /**
1859  * create a short-circuit expression evaluation that tries to construct
1860  * efficient control flow structures for &&, || and ! expressions
1861  */
1862 static void create_condition_evaluation(const expression_t *expression,
1863                                         ir_node *true_block,
1864                                         ir_node *false_block)
1865 {
1866         switch(expression->type) {
1867         case EXPR_UNARY_NOT: {
1868                 const unary_expression_t *unary_expression = &expression->unary;
1869                 create_condition_evaluation(unary_expression->value, false_block,
1870                                             true_block);
1871                 return;
1872         }
1873         case EXPR_BINARY_LOGICAL_AND: {
1874                 const binary_expression_t *binary_expression = &expression->binary;
1875
1876                 ir_node *cur_block   = get_cur_block();
1877                 ir_node *extra_block = new_immBlock();
1878                 set_cur_block(cur_block);
1879                 create_condition_evaluation(binary_expression->left, extra_block,
1880                                             false_block);
1881                 mature_immBlock(extra_block);
1882                 set_cur_block(extra_block);
1883                 create_condition_evaluation(binary_expression->right, true_block,
1884                                             false_block);
1885                 return;
1886         }
1887         case EXPR_BINARY_LOGICAL_OR: {
1888                 const binary_expression_t *binary_expression = &expression->binary;
1889
1890                 ir_node *cur_block   = get_cur_block();
1891                 ir_node *extra_block = new_immBlock();
1892                 set_cur_block(cur_block);
1893                 create_condition_evaluation(binary_expression->left, true_block,
1894                                             extra_block);
1895                 mature_immBlock(extra_block);
1896                 set_cur_block(extra_block);
1897                 create_condition_evaluation(binary_expression->right, true_block,
1898                                             false_block);
1899                 return;
1900         }
1901         default:
1902                 break;
1903         }
1904
1905         dbg_info *dbgi       = get_dbg_info(&expression->base.source_position);
1906         ir_node  *condition  = expression_to_modeb(expression);
1907         ir_node  *cond       = new_d_Cond(dbgi, condition);
1908         ir_node  *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1909         ir_node  *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1910
1911         add_immBlock_pred(true_block, true_proj);
1912         add_immBlock_pred(false_block, false_proj);
1913
1914         set_cur_block(NULL);
1915 }
1916
1917 static void return_statement_to_firm(return_statement_t *statement)
1918 {
1919         if(get_cur_block() == NULL)
1920                 return;
1921
1922         ir_type *func_irtype = get_ir_type(current_function_decl->type);
1923
1924         dbg_info *dbgi  = get_dbg_info(&statement->statement.source_position);
1925
1926         ir_node *in[1];
1927         int      in_len;
1928         if(get_method_n_ress(func_irtype) > 0) {
1929                 ir_type *res_type = get_method_res_type(func_irtype, 0);
1930
1931                 if(statement->return_value != NULL) {
1932                         ir_node *node = expression_to_firm(statement->return_value);
1933                         node  = do_strict_conv(dbgi, node);
1934                         in[0] = node;
1935                 } else {
1936                         ir_mode *mode;
1937                         if(is_compound_type(res_type)) {
1938                                 mode = mode_P_data;
1939                         } else {
1940                                 mode = get_type_mode(res_type);
1941                         }
1942                         in[0] = new_Unknown(mode);
1943                 }
1944                 in_len = 1;
1945         } else {
1946                 /* build return_value for its side effects */
1947                 if(statement->return_value != NULL) {
1948                         expression_to_firm(statement->return_value);
1949                 }
1950                 in_len = 0;
1951         }
1952
1953         ir_node  *store = get_store();
1954         ir_node  *ret   = new_d_Return(dbgi, store, in_len, in);
1955
1956         ir_node *end_block = get_irg_end_block(current_ir_graph);
1957         add_immBlock_pred(end_block, ret);
1958
1959         set_cur_block(NULL);
1960 }
1961
1962 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
1963 {
1964         if(get_cur_block() == NULL)
1965                 return NULL;
1966
1967         return expression_to_firm(statement->expression);
1968 }
1969
1970 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
1971 {
1972         ir_node     *result    = NULL;
1973         statement_t *statement = compound->statements;
1974         for( ; statement != NULL; statement = statement->base.next) {
1975                 //context2firm(&statement->context);
1976
1977                 if(statement->base.next == NULL
1978                                 && statement->type == STATEMENT_EXPRESSION) {
1979                         result = expression_statement_to_firm(
1980                                         (expression_statement_t*) statement);
1981                         break;
1982                 }
1983                 statement_to_firm(statement);
1984         }
1985
1986         return result;
1987 }
1988
1989 static void if_statement_to_firm(if_statement_t *statement)
1990 {
1991         ir_node *cur_block = get_cur_block();
1992
1993         ir_node *fallthrough_block = new_immBlock();
1994
1995         /* the true (blocks) */
1996         ir_node *true_block;
1997         if (statement->true_statement != NULL) {
1998                 true_block = new_immBlock();
1999                 statement_to_firm(statement->true_statement);
2000                 if(get_cur_block() != NULL) {
2001                         ir_node *jmp = new_Jmp();
2002                         add_immBlock_pred(fallthrough_block, jmp);
2003                 }
2004         } else {
2005                 true_block = fallthrough_block;
2006         }
2007
2008         /* the false (blocks) */
2009         ir_node *false_block;
2010         if(statement->false_statement != NULL) {
2011                 false_block = new_immBlock();
2012
2013                 statement_to_firm(statement->false_statement);
2014                 if(get_cur_block() != NULL) {
2015                         ir_node *jmp = new_Jmp();
2016                         add_immBlock_pred(fallthrough_block, jmp);
2017                 }
2018         } else {
2019                 false_block = fallthrough_block;
2020         }
2021
2022         /* create the condition */
2023         if(cur_block != NULL) {
2024                 set_cur_block(cur_block);
2025                 create_condition_evaluation(statement->condition, true_block,
2026                                             false_block);
2027         }
2028
2029         mature_immBlock(true_block);
2030         if(false_block != fallthrough_block) {
2031                 mature_immBlock(false_block);
2032         }
2033         mature_immBlock(fallthrough_block);
2034
2035         set_cur_block(fallthrough_block);
2036 }
2037
2038 static void while_statement_to_firm(while_statement_t *statement)
2039 {
2040         ir_node *jmp = NULL;
2041         if(get_cur_block() != NULL) {
2042                 jmp = new_Jmp();
2043         }
2044
2045         /* create the header block */
2046         ir_node *header_block = new_immBlock();
2047         if(jmp != NULL) {
2048                 add_immBlock_pred(header_block, jmp);
2049         }
2050
2051         /* the false block */
2052         ir_node *false_block = new_immBlock();
2053
2054         /* the loop body */
2055         ir_node *body_block;
2056         if (statement->body != NULL) {
2057                 ir_node *old_continue_label = continue_label;
2058                 ir_node *old_break_label    = break_label;
2059                 continue_label              = header_block;
2060                 break_label                 = false_block;
2061
2062                 body_block = new_immBlock();
2063                 statement_to_firm(statement->body);
2064
2065                 assert(continue_label == header_block);
2066                 assert(break_label    == false_block);
2067                 continue_label = old_continue_label;
2068                 break_label    = old_break_label;
2069
2070                 if(get_cur_block() != NULL) {
2071                         jmp = new_Jmp();
2072                         add_immBlock_pred(header_block, jmp);
2073                 }
2074         } else {
2075                 body_block = header_block;
2076         }
2077
2078         /* create the condition */
2079         set_cur_block(header_block);
2080
2081         create_condition_evaluation(statement->condition, body_block, false_block);
2082         mature_immBlock(body_block);
2083         mature_immBlock(false_block);
2084         mature_immBlock(header_block);
2085
2086         set_cur_block(false_block);
2087 }
2088
2089 static void do_while_statement_to_firm(do_while_statement_t *statement)
2090 {
2091         ir_node *jmp = NULL;
2092         if(get_cur_block() != NULL) {
2093                 jmp = new_Jmp();
2094         }
2095
2096         /* create the header block */
2097         ir_node *header_block = new_immBlock();
2098
2099         /* the false block */
2100         ir_node *false_block = new_immBlock();
2101
2102         /* the loop body */
2103         ir_node *body_block = new_immBlock();
2104         if(jmp != NULL) {
2105                 add_immBlock_pred(body_block, jmp);
2106         }
2107
2108         if (statement->body != NULL) {
2109                 ir_node *old_continue_label = continue_label;
2110                 ir_node *old_break_label    = break_label;
2111                 continue_label              = header_block;
2112                 break_label                 = false_block;
2113
2114                 statement_to_firm(statement->body);
2115
2116                 assert(continue_label == header_block);
2117                 assert(break_label    == false_block);
2118                 continue_label = old_continue_label;
2119                 break_label    = old_break_label;
2120
2121                 if (get_cur_block() == NULL) {
2122                         mature_immBlock(header_block);
2123                         mature_immBlock(body_block);
2124                         mature_immBlock(false_block);
2125                         return;
2126                 }
2127         }
2128
2129         ir_node *body_jmp = new_Jmp();
2130         add_immBlock_pred(header_block, body_jmp);
2131         mature_immBlock(header_block);
2132
2133         /* create the condition */
2134         set_cur_block(header_block);
2135
2136         create_condition_evaluation(statement->condition, body_block, false_block);
2137         mature_immBlock(body_block);
2138         mature_immBlock(false_block);
2139         mature_immBlock(header_block);
2140
2141         set_cur_block(false_block);
2142 }
2143
2144 static void for_statement_to_firm(for_statement_t *statement)
2145 {
2146         ir_node *jmp = NULL;
2147         if (get_cur_block() != NULL) {
2148                 if(statement->initialisation != NULL) {
2149                         expression_to_firm(statement->initialisation);
2150                 }
2151                 jmp = new_Jmp();
2152         }
2153
2154         /* create the step block */
2155         ir_node *const step_block = new_immBlock();
2156         if (statement->step != NULL) {
2157                 expression_to_firm(statement->step);
2158         }
2159         ir_node *const step_jmp = new_Jmp();
2160
2161         /* create the header block */
2162         ir_node *const header_block = new_immBlock();
2163         if (jmp != NULL) {
2164                 add_immBlock_pred(header_block, jmp);
2165         }
2166         add_immBlock_pred(header_block, step_jmp);
2167
2168         /* the false block */
2169         ir_node *const false_block = new_immBlock();
2170
2171         /* the loop body */
2172         ir_node * body_block;
2173         if (statement->body != NULL) {
2174                 ir_node *const old_continue_label = continue_label;
2175                 ir_node *const old_break_label    = break_label;
2176                 continue_label = step_block;
2177                 break_label    = false_block;
2178
2179                 body_block = new_immBlock();
2180                 statement_to_firm(statement->body);
2181
2182                 assert(continue_label == step_block);
2183                 assert(break_label    == false_block);
2184                 continue_label = old_continue_label;
2185                 break_label    = old_break_label;
2186
2187                 if (get_cur_block() != NULL) {
2188                         jmp = new_Jmp();
2189                         add_immBlock_pred(step_block, jmp);
2190                 }
2191         } else {
2192                 body_block = step_block;
2193         }
2194
2195         /* create the condition */
2196         set_cur_block(header_block);
2197         if (statement->condition != NULL) {
2198                 create_condition_evaluation(statement->condition, body_block,
2199                                             false_block);
2200         } else {
2201                 keep_alive(header_block);
2202                 jmp = new_Jmp();
2203                 add_immBlock_pred(body_block, jmp);
2204         }
2205
2206         mature_immBlock(body_block);
2207         mature_immBlock(false_block);
2208         mature_immBlock(step_block);
2209         mature_immBlock(header_block);
2210         mature_immBlock(false_block);
2211
2212         set_cur_block(false_block);
2213 }
2214
2215 static void create_declaration_entity(declaration_t *declaration,
2216                                       declaration_type_t declaration_type,
2217                                       ir_type *parent_type)
2218 {
2219         ident     *id     = new_id_from_str(declaration->symbol->string);
2220         ir_type   *irtype = get_ir_type(declaration->type);
2221         ir_entity *entity = new_entity(parent_type, id, irtype);
2222         set_entity_ld_ident(entity, id);
2223
2224         declaration->declaration_type = (unsigned char) declaration_type;
2225         declaration->v.entity         = entity;
2226         set_entity_variability(entity, variability_uninitialized);
2227         /* TODO: visibility? */
2228 }
2229
2230 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2231
2232 enum compound_graph_entry_type_t {
2233         COMPOUND_GRAPH_ENTRY_ARRAY,
2234         COMPOUND_GRAPH_ENTRY_COMPOUND
2235 };
2236
2237 struct compound_graph_path_entry_t {
2238         int type;
2239         union {
2240                 ir_entity *entity;
2241                 int        array_index;
2242         } v;
2243         compound_graph_path_entry_t *prev;
2244 };
2245
2246 static void create_initializer_object(initializer_t *initializer, type_t *type,
2247                 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2248
2249 static compound_graph_path *create_compound_path(ir_type *type,
2250                 compound_graph_path_entry_t *entry, int len)
2251 {
2252         compound_graph_path *path = new_compound_graph_path(type, len);
2253
2254         int i = len - 1;
2255         for( ; entry != NULL; entry = entry->prev, --i) {
2256                 assert(i >= 0);
2257                 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2258                         set_compound_graph_path_node(path, i, entry->v.entity);
2259                 } else {
2260                         assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2261                         set_compound_graph_path_array_index(path, i, entry->v.array_index);
2262                 }
2263         }
2264         assert(i == -1);
2265
2266         return path;
2267 }
2268
2269 static void create_initializer_value(initializer_value_t *initializer,
2270                                      ir_entity *entity,
2271                                      compound_graph_path_entry_t *entry,
2272                                      int len)
2273 {
2274         ir_node             *node = expression_to_firm(initializer->value);
2275         ir_type             *type = get_entity_type(entity);
2276         compound_graph_path *path = create_compound_path(type, entry, len);
2277         add_compound_ent_value_w_path(entity, node, path);
2278 }
2279
2280 static void create_initializer_compound(initializer_list_t *initializer,
2281                                         compound_type_t *type,
2282                                         ir_entity *entity,
2283                                         compound_graph_path_entry_t *last_entry,
2284                                         int len)
2285 {
2286         declaration_t *compound_declaration = type->declaration;
2287
2288         declaration_t *compound_entry = compound_declaration->context.declarations;
2289
2290         compound_graph_path_entry_t entry;
2291         entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2292         entry.prev = last_entry;
2293         ++len;
2294
2295         size_t i = 0;
2296         for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2297                 if(compound_entry->symbol == NULL)
2298                         continue;
2299                 if(compound_entry->namespc != NAMESPACE_NORMAL)
2300                         continue;
2301
2302                 if(i >= initializer->len)
2303                         break;
2304
2305                 entry.v.entity = compound_entry->v.entity;
2306
2307                 initializer_t *sub_initializer = initializer->initializers[i];
2308
2309                 assert(compound_entry != NULL);
2310                 assert(compound_entry->declaration_type
2311                                 == DECLARATION_TYPE_COMPOUND_MEMBER);
2312
2313                 if(sub_initializer->type == INITIALIZER_VALUE) {
2314                         create_initializer_value(&sub_initializer->value,
2315                                                  entity, &entry, len);
2316                 } else {
2317                         type_t *entry_type = skip_typeref(compound_entry->type);
2318                         create_initializer_object(sub_initializer, entry_type, entity,
2319                                                   &entry, len);
2320                 }
2321
2322                 ++i;
2323         }
2324 }
2325
2326 static void create_initializer_array(initializer_list_t *initializer,
2327                                      array_type_t *type, ir_entity *entity,
2328                                      compound_graph_path_entry_t *last_entry,
2329                                      int len)
2330 {
2331         type_t *element_type = type->element_type;
2332         element_type         = skip_typeref(element_type);
2333
2334         compound_graph_path_entry_t entry;
2335         entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2336         entry.prev = last_entry;
2337         ++len;
2338
2339         size_t i;
2340         for(i = 0; i < initializer->len; ++i) {
2341                 entry.v.array_index = i;
2342
2343                 initializer_t *sub_initializer = initializer->initializers[i];
2344
2345                 if(sub_initializer->type == INITIALIZER_VALUE) {
2346                         create_initializer_value(&sub_initializer->value,
2347                                                  entity, &entry, len);
2348                 } else {
2349                         create_initializer_object(sub_initializer, element_type, entity,
2350                                                   &entry, len);
2351                 }
2352         }
2353
2354 #if 0
2355         /* TODO: initialize rest... */
2356         if(type->size_expression != NULL) {
2357                 size_t array_len = fold_constant(type->size_expression);
2358                 for( ; i < array_len; ++i) {
2359
2360                 }
2361         }
2362 #endif
2363 }
2364
2365 static void create_initializer_string(initializer_string_t *initializer,
2366                                       array_type_t *type, ir_entity *entity,
2367                                       compound_graph_path_entry_t *last_entry,
2368                                       int len)
2369 {
2370         type_t *element_type = type->element_type;
2371         element_type         = skip_typeref(element_type);
2372
2373         compound_graph_path_entry_t entry;
2374         entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2375         entry.prev = last_entry;
2376         ++len;
2377
2378         ir_type    *irtype  = get_entity_type(entity);
2379         size_t      arr_len = get_array_type_size(type);
2380         const char *p       = initializer->string;
2381         size_t      i       = 0;
2382         for(i = 0; i < arr_len; ++i, ++p) {
2383                 entry.v.array_index = i;
2384
2385                 ir_node             *node = new_Const_long(mode_Bs, *p);
2386                 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2387                 add_compound_ent_value_w_path(entity, node, path);
2388
2389                 if(*p == '\0')
2390                         break;
2391         }
2392 }
2393
2394 static void create_initializer_wide_string(
2395         const initializer_wide_string_t *const initializer, array_type_t *const type,
2396         ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2397         int len)
2398 {
2399         type_t *element_type = type->element_type;
2400         element_type         = skip_typeref(element_type);
2401
2402         compound_graph_path_entry_t entry;
2403         entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2404         entry.prev = last_entry;
2405         ++len;
2406
2407         ir_type           *const irtype  = get_entity_type(entity);
2408         const size_t             arr_len = get_array_type_size(type);
2409         const wchar_rep_t *      p       = initializer->string.begin;
2410         const wchar_rep_t *const end     = p + initializer->string.size;
2411         for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2412                 entry.v.array_index = i;
2413
2414                 ir_node             *node = new_Const_long(mode_Is, *p);
2415                 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2416                 add_compound_ent_value_w_path(entity, node, path);
2417         }
2418 }
2419
2420 static void create_initializer_object(initializer_t *initializer, type_t *type,
2421                 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2422 {
2423         if(is_type_array(type)) {
2424                 array_type_t *array_type = &type->array;
2425
2426                 switch (initializer->type) {
2427                         case INITIALIZER_STRING: {
2428                                 initializer_string_t *const string = &initializer->string;
2429                                 create_initializer_string(string, array_type, entity, entry, len);
2430                                 return;
2431                         }
2432
2433                         case INITIALIZER_WIDE_STRING: {
2434                                 initializer_wide_string_t *const string = &initializer->wide_string;
2435                                 create_initializer_wide_string(string, array_type, entity, entry, len);
2436                                 return;
2437                         }
2438
2439                         case INITIALIZER_LIST: {
2440                                 initializer_list_t *const list = &initializer->list;
2441                                 create_initializer_array(list, array_type, entity, entry, len);
2442                                 return;
2443                         }
2444
2445                         case INITIALIZER_VALUE:
2446                                 break;
2447                 }
2448                 panic("Unhandled initializer");
2449         } else {
2450                 assert(initializer->type == INITIALIZER_LIST);
2451                 initializer_list_t *list = &initializer->list;
2452
2453                 assert(is_type_compound(type));
2454                 compound_type_t *compound_type = &type->compound;
2455                 create_initializer_compound(list, compound_type, entity, entry, len);
2456         }
2457 }
2458
2459 static void create_initializer_local_variable_entity(declaration_t *declaration)
2460 {
2461         initializer_t *initializer = declaration->init.initializer;
2462         dbg_info      *dbgi        = get_dbg_info(&declaration->source_position);
2463         ir_entity     *entity      = declaration->v.entity;
2464         ir_node       *memory      = get_store();
2465         ir_node       *nomem       = new_NoMem();
2466         ir_node       *frame       = get_irg_frame(current_ir_graph);
2467         ir_node       *addr        = new_d_simpleSel(dbgi, nomem, frame, entity);
2468
2469         if(initializer->type == INITIALIZER_VALUE) {
2470                 initializer_value_t *initializer_value = &initializer->value;
2471
2472                 ir_node *value = expression_to_firm(initializer_value->value);
2473                 type_t  *type  = skip_typeref(declaration->type);
2474                 assign_value(dbgi, addr, type, value);
2475                 return;
2476         }
2477
2478         /* create a "template" entity which is copied to the entity on the stack */
2479         ident     *id          = unique_ident("initializer");
2480         ir_type   *irtype      = get_ir_type(declaration->type);
2481         ir_type   *global_type = get_glob_type();
2482         ir_entity *init_entity = new_entity(global_type, id, irtype);
2483         set_entity_ld_ident(init_entity, id);
2484
2485         set_entity_variability(init_entity, variability_initialized);
2486         set_entity_visibility(init_entity, visibility_local);
2487
2488         ir_graph *old_current_ir_graph = current_ir_graph;
2489         current_ir_graph = get_const_code_irg();
2490
2491         type_t *type = skip_typeref(declaration->type);
2492         create_initializer_object(initializer, type, init_entity, NULL, 0);
2493
2494         assert(current_ir_graph == get_const_code_irg());
2495         current_ir_graph = old_current_ir_graph;
2496
2497         ir_node *src_addr  = create_symconst(dbgi, init_entity);
2498         ir_node *copyb     = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2499
2500         ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2501         set_store(copyb_mem);
2502 }
2503
2504 static void create_initializer(declaration_t *declaration)
2505 {
2506         initializer_t *initializer = declaration->init.initializer;
2507         if(initializer == NULL)
2508                 return;
2509
2510         declaration_type_t declaration_type
2511                 = (declaration_type_t) declaration->declaration_type;
2512         if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2513                 create_initializer_local_variable_entity(declaration);
2514                 return;
2515         }
2516
2517         if(initializer->type == INITIALIZER_VALUE) {
2518                 initializer_value_t *initializer_value = &initializer->value;
2519
2520                 ir_node *value = expression_to_firm(initializer_value->value);
2521
2522                 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2523                         set_value(declaration->v.value_number, value);
2524                 } else {
2525                         assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2526
2527                         ir_entity *entity = declaration->v.entity;
2528
2529                         set_entity_variability(entity, variability_initialized);
2530                         set_atomic_ent_value(entity, value);
2531                 }
2532         } else {
2533                 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2534                                 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2535
2536                 ir_entity *entity = declaration->v.entity;
2537                 set_entity_variability(entity, variability_initialized);
2538
2539                 type_t *type = skip_typeref(declaration->type);
2540                 create_initializer_object(initializer, type, entity, NULL, 0);
2541         }
2542 }
2543
2544 static void create_local_variable(declaration_t *declaration)
2545 {
2546         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2547
2548         bool needs_entity = declaration->address_taken;
2549         type_t *type = skip_typeref(declaration->type);
2550
2551         if(is_type_array(type) || is_type_compound(type)) {
2552                 needs_entity = true;
2553         }
2554
2555         if(needs_entity) {
2556                 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2557                 create_declaration_entity(declaration,
2558                                           DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2559                                           frame_type);
2560         } else {
2561                 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2562                 declaration->v.value_number   = next_value_number_function;
2563                 ++next_value_number_function;
2564         }
2565
2566         create_initializer(declaration);
2567 }
2568
2569 static void create_local_static_variable(declaration_t *declaration)
2570 {
2571         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2572
2573         type_t    *type        = skip_typeref(declaration->type);
2574         ir_type   *global_type = get_glob_type();
2575         ident     *id          = unique_ident(declaration->symbol->string);
2576         ir_type   *irtype      = get_ir_type(type);
2577         ir_entity *entity      = new_entity(global_type, id, irtype);
2578         set_entity_ld_ident(entity, id);
2579
2580         declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2581         declaration->v.entity         = entity;
2582         set_entity_variability(entity, variability_uninitialized);
2583         set_entity_visibility(entity, visibility_local);
2584
2585         ir_graph *old_current_ir_graph = current_ir_graph;
2586         current_ir_graph = get_const_code_irg();
2587
2588         create_initializer(declaration);
2589
2590         assert(current_ir_graph == get_const_code_irg());
2591         current_ir_graph = old_current_ir_graph;
2592 }
2593
2594 static void declaration_statement_to_firm(declaration_statement_t *statement)
2595 {
2596         declaration_t *declaration = statement->declarations_begin;
2597         declaration_t *end         = statement->declarations_end->next;
2598         for( ; declaration != end; declaration = declaration->next) {
2599                 type_t *type = skip_typeref(declaration->type);
2600
2601                 switch ((storage_class_tag_t) declaration->storage_class) {
2602                 case STORAGE_CLASS_TYPEDEF:
2603                         continue;
2604                 case STORAGE_CLASS_STATIC:
2605                         create_local_static_variable(declaration);
2606                         continue;
2607                 case STORAGE_CLASS_ENUM_ENTRY:
2608                         panic("enum entry declaration in local block found");
2609                 case STORAGE_CLASS_EXTERN:
2610                         panic("extern declaration in local block found");
2611                 case STORAGE_CLASS_NONE:
2612                 case STORAGE_CLASS_AUTO:
2613                 case STORAGE_CLASS_REGISTER:
2614                         if(is_type_function(type)) {
2615                                 panic("nested functions not supported yet");
2616                         } else {
2617                                 create_local_variable(declaration);
2618                         }
2619                         continue;
2620                 case STORAGE_CLASS_THREAD:
2621                 case STORAGE_CLASS_THREAD_EXTERN:
2622                 case STORAGE_CLASS_THREAD_STATIC:
2623                         break;
2624                 }
2625                 panic("invalid storage class found");
2626         }
2627 }
2628
2629 static void create_jump_statement(const statement_t *statement,
2630                                   ir_node *target_block)
2631 {
2632         if(get_cur_block() == NULL)
2633                 return;
2634
2635         dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2636         ir_node  *jump = new_d_Jmp(dbgi);
2637         add_immBlock_pred(target_block, jump);
2638
2639         set_cur_block(NULL);
2640 }
2641
2642 static void switch_statement_to_firm(const switch_statement_t *statement)
2643 {
2644         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2645
2646         ir_node *expression  = expression_to_firm(statement->expression);
2647         ir_node *cond        = new_d_Cond(dbgi, expression);
2648         ir_node *break_block = new_immBlock();
2649
2650         set_cur_block(NULL);
2651
2652         ir_node *const old_switch_cond       = current_switch_cond;
2653         ir_node *const old_break_label       = break_label;
2654         const bool     old_saw_default_label = saw_default_label;
2655         current_switch_cond                  = cond;
2656         break_label                          = break_block;
2657
2658         statement_to_firm(statement->body);
2659
2660         if(get_cur_block() != NULL) {
2661                 ir_node *jmp = new_Jmp();
2662                 add_immBlock_pred(break_block, jmp);
2663         }
2664
2665         if (!saw_default_label) {
2666                 set_cur_block(get_nodes_block(cond));
2667                 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2668                                                         MAGIC_DEFAULT_PN_NUMBER);
2669                 add_immBlock_pred(break_block, proj);
2670         }
2671
2672         assert(current_switch_cond == cond);
2673         assert(break_label         == break_block);
2674         current_switch_cond = old_switch_cond;
2675         break_label         = old_break_label;
2676         saw_default_label   = old_saw_default_label;
2677
2678         mature_immBlock(break_block);
2679         set_cur_block(break_block);
2680 }
2681
2682 static long fold_constant(const expression_t *expression)
2683 {
2684         ir_graph *old_current_ir_graph = current_ir_graph;
2685         current_ir_graph = get_const_code_irg();
2686
2687         ir_node *cnst = expression_to_firm(expression);
2688         if(!is_Const(cnst)) {
2689                 panic("couldn't fold constantl");
2690         }
2691         tarval *tv = get_Const_tarval(cnst);
2692         if(!tarval_is_long(tv)) {
2693                 panic("folded constant not an integer");
2694         }
2695
2696         long res = get_tarval_long(tv);
2697
2698         current_ir_graph = old_current_ir_graph;
2699         return res;
2700 }
2701
2702 static void case_label_to_firm(const case_label_statement_t *statement)
2703 {
2704         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2705
2706         ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2707
2708         /* let's create a node and hope firm constant folding creates a Const
2709          * node... */
2710         ir_node *proj;
2711         set_cur_block(get_nodes_block(current_switch_cond));
2712         if(statement->expression) {
2713                 long pn = fold_constant(statement->expression);
2714                 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2715                         /* oops someone detected our cheating... */
2716                         panic("magic default pn used");
2717                 }
2718                 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2719         } else {
2720                 saw_default_label = true;
2721                 proj = new_d_defaultProj(dbgi, current_switch_cond,
2722                                          MAGIC_DEFAULT_PN_NUMBER);
2723         }
2724
2725         ir_node *block = new_immBlock();
2726         if (fallthrough != NULL) {
2727                 add_immBlock_pred(block, fallthrough);
2728         }
2729         add_immBlock_pred(block, proj);
2730         mature_immBlock(block);
2731
2732         statement_to_firm(statement->label_statement);
2733 }
2734
2735 static ir_node *get_label_block(declaration_t *label)
2736 {
2737         assert(label->namespc == NAMESPACE_LABEL);
2738
2739         if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2740                 return label->v.block;
2741         }
2742         assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2743
2744         ir_node *old_cur_block = get_cur_block();
2745         ir_node *block         = new_immBlock();
2746         set_cur_block(old_cur_block);
2747
2748         label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2749         label->v.block          = block;
2750
2751         ARR_APP1(ir_node *, imature_blocks, block);
2752
2753         return block;
2754 }
2755
2756 static void label_to_firm(const label_statement_t *statement)
2757 {
2758         ir_node *block = get_label_block(statement->label);
2759
2760         if(get_cur_block() != NULL) {
2761                 ir_node *jmp = new_Jmp();
2762                 add_immBlock_pred(block, jmp);
2763         }
2764
2765         set_cur_block(block);
2766         keep_alive(block);
2767
2768         statement_to_firm(statement->label_statement);
2769 }
2770
2771 static void goto_to_firm(const goto_statement_t *statement)
2772 {
2773         if(get_cur_block() == NULL)
2774                 return;
2775
2776         ir_node *block = get_label_block(statement->label);
2777         ir_node *jmp   = new_Jmp();
2778         add_immBlock_pred(block, jmp);
2779
2780         set_cur_block(NULL);
2781 }
2782
2783 static void statement_to_firm(statement_t *statement)
2784 {
2785         switch(statement->type) {
2786         case STATEMENT_INVALID:
2787                 panic("invalid statement found");
2788         case STATEMENT_COMPOUND:
2789                 compound_statement_to_firm(&statement->compound);
2790                 return;
2791         case STATEMENT_RETURN:
2792                 return_statement_to_firm(&statement->returns);
2793                 return;
2794         case STATEMENT_EXPRESSION:
2795                 expression_statement_to_firm(&statement->expression);
2796                 return;
2797         case STATEMENT_IF:
2798                 if_statement_to_firm(&statement->ifs);
2799                 return;
2800         case STATEMENT_WHILE:
2801                 while_statement_to_firm(&statement->whiles);
2802                 return;
2803         case STATEMENT_DO_WHILE:
2804                 do_while_statement_to_firm(&statement->do_while);
2805                 return;
2806         case STATEMENT_DECLARATION:
2807                 declaration_statement_to_firm(&statement->declaration);
2808                 return;
2809         case STATEMENT_BREAK:
2810                 create_jump_statement(statement, break_label);
2811                 return;
2812         case STATEMENT_CONTINUE:
2813                 create_jump_statement(statement, continue_label);
2814                 return;
2815         case STATEMENT_SWITCH:
2816                 switch_statement_to_firm(&statement->switchs);
2817                 return;
2818         case STATEMENT_CASE_LABEL:
2819                 case_label_to_firm(&statement->case_label);
2820                 return;
2821         case STATEMENT_FOR:
2822                 for_statement_to_firm(&statement->fors);
2823                 return;
2824         case STATEMENT_LABEL:
2825                 label_to_firm(&statement->label);
2826                 return;
2827         case STATEMENT_GOTO:
2828                 goto_to_firm(&statement->gotos);
2829                 return;
2830         case STATEMENT_ASM:
2831                 break;
2832         }
2833         panic("Statement not implemented\n");
2834 }
2835
2836 static int count_local_declarations(const declaration_t *      decl,
2837                                     const declaration_t *const end)
2838 {
2839         int count = 0;
2840         for (; decl != end; decl = decl->next) {
2841                 const type_t *type = skip_typeref(decl->type);
2842                 switch (type->type) {
2843                         case TYPE_ATOMIC:
2844                         case TYPE_ENUM:
2845                         case TYPE_POINTER:
2846                                 if (!decl->address_taken)
2847                                         ++count;
2848                                 break;
2849
2850                         default: break;
2851                 }
2852         }
2853         return count;
2854 }
2855
2856 static int count_decls_in_stmts(const statement_t *stmt)
2857 {
2858         int count = 0;
2859         for (; stmt != NULL; stmt = stmt->base.next) {
2860                 switch (stmt->type) {
2861                         case STATEMENT_DECLARATION: {
2862                                 const declaration_statement_t *const decl_stmt =
2863                                         (const declaration_statement_t*)stmt;
2864                                 count += count_local_declarations(decl_stmt->declarations_begin,
2865                                                                   decl_stmt->declarations_end->next);
2866                                 break;
2867                         }
2868
2869                         case STATEMENT_COMPOUND: {
2870                                 const compound_statement_t *const comp =
2871                                         (const compound_statement_t*)stmt;
2872                                 count += count_decls_in_stmts(comp->statements);
2873                                 break;
2874                         }
2875
2876                         case STATEMENT_IF: {
2877                                 const if_statement_t *const if_stmt = (const if_statement_t*)stmt;
2878                                 count += count_decls_in_stmts(if_stmt->true_statement);
2879                                 count += count_decls_in_stmts(if_stmt->false_statement);
2880                                 break;
2881                         }
2882
2883                         case STATEMENT_SWITCH: {
2884                                 const switch_statement_t *const switch_stmt =
2885                                         (const switch_statement_t*)stmt;
2886                                 count += count_decls_in_stmts(switch_stmt->body);
2887                                 break;
2888                         }
2889
2890                         case STATEMENT_LABEL: {
2891                                 const label_statement_t *const label_stmt =
2892                                         (const label_statement_t*)stmt;
2893                                 count += count_decls_in_stmts(label_stmt->label_statement);
2894                                 break;
2895                         }
2896
2897                         case STATEMENT_WHILE: {
2898                                 const while_statement_t *const while_stmt =
2899                                         (const while_statement_t*)stmt;
2900                                 count += count_decls_in_stmts(while_stmt->body);
2901                                 break;
2902                         }
2903
2904                         case STATEMENT_DO_WHILE: {
2905                                 const do_while_statement_t *const do_while_stmt =
2906                                         (const do_while_statement_t*)stmt;
2907                                 count += count_decls_in_stmts(do_while_stmt->body);
2908                                 break;
2909                         }
2910
2911                         case STATEMENT_FOR: {
2912                                 const for_statement_t *const for_stmt =
2913                                         (const for_statement_t*)stmt;
2914                                 /* TODO initialisation */
2915                                 count += count_decls_in_stmts(for_stmt->body);
2916                                 break;
2917                         }
2918
2919                         case STATEMENT_ASM:
2920                         case STATEMENT_BREAK:
2921                         case STATEMENT_CASE_LABEL:
2922                         case STATEMENT_CONTINUE:
2923                         case STATEMENT_EXPRESSION:
2924                         case STATEMENT_GOTO:
2925                         case STATEMENT_INVALID:
2926                         case STATEMENT_RETURN:
2927                                 break;
2928                 }
2929         }
2930         return count;
2931 }
2932
2933 static int get_function_n_local_vars(declaration_t *declaration)
2934 {
2935         int count = 0;
2936
2937         /* count parameters */
2938         count += count_local_declarations(declaration->context.declarations, NULL);
2939
2940         /* count local variables declared in body */
2941         count += count_decls_in_stmts(declaration->init.statement);
2942
2943         /* TODO FIXME: Matze: I'm lazy don't want to scan all expressions
2944          * for expression statements... */
2945         count += 10;
2946
2947         return count;
2948 }
2949
2950 static void initialize_function_parameters(declaration_t *declaration)
2951 {
2952         ir_graph        *irg             = current_ir_graph;
2953         ir_node         *args            = get_irg_args(irg);
2954         ir_node         *start_block     = get_irg_start_block(irg);
2955         ir_type         *function_irtype = get_ir_type(declaration->type);
2956
2957         int            n         = 0;
2958         declaration_t *parameter = declaration->context.declarations;
2959         for( ; parameter != NULL; parameter = parameter->next, ++n) {
2960                 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
2961                 type_t *type = skip_typeref(parameter->type);
2962
2963                 bool needs_entity = parameter->address_taken;
2964                 assert(!is_type_array(type));
2965                 if(is_type_compound(type)) {
2966                         needs_entity = true;
2967                 }
2968
2969                 if(needs_entity) {
2970                         ir_entity *entity = get_method_value_param_ent(function_irtype, n);
2971                         ident     *id     = new_id_from_str(parameter->symbol->string);
2972                         set_entity_ident(entity, id);
2973
2974                         parameter->declaration_type
2975                                 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
2976                         parameter->v.entity = entity;
2977                         continue;
2978                 }
2979
2980                 ir_mode *mode = get_ir_mode(parameter->type);
2981                 long     pn   = n;
2982                 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
2983
2984                 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2985                 parameter->v.value_number   = next_value_number_function;
2986                 ++next_value_number_function;
2987
2988                 set_value(parameter->v.value_number, proj);
2989         }
2990 }
2991
2992 static void create_function(declaration_t *declaration)
2993 {
2994         ir_entity *function_entity = get_function_entity(declaration);
2995
2996         if(declaration->init.statement == NULL)
2997                 return;
2998
2999         current_function_decl = declaration;
3000         current_function_name = NULL;
3001
3002         assert(imature_blocks == NULL);
3003         imature_blocks = NEW_ARR_F(ir_node*, 0);
3004
3005         int       n_local_vars = get_function_n_local_vars(declaration);
3006         ir_graph *irg          = new_ir_graph(function_entity, n_local_vars);
3007         ir_node  *first_block  = get_cur_block();
3008
3009         next_value_number_function = 0;
3010         initialize_function_parameters(declaration);
3011
3012         statement_to_firm(declaration->init.statement);
3013
3014         ir_node *end_block = get_irg_end_block(irg);
3015
3016         /* do we have a return statement yet? */
3017         if(get_cur_block() != NULL) {
3018                 type_t *type = skip_typeref(declaration->type);
3019                 assert(is_type_function(type));
3020                 const function_type_t *func_type   = &type->function;
3021                 const type_t          *return_type
3022                         = skip_typeref(func_type->return_type);
3023
3024                 ir_node *ret;
3025                 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3026                         ret = new_Return(get_store(), 0, NULL);
3027                 } else {
3028                         ir_mode *mode;
3029                         if(is_type_scalar(return_type)) {
3030                                 mode = get_ir_mode(func_type->return_type);
3031                         } else {
3032                                 mode = mode_P_data;
3033                         }
3034
3035                         ir_node *in[1];
3036                         // ยง5.1.2.2.3 main implicitly returns 0
3037                         if (strcmp(declaration->symbol->string, "main") == 0) {
3038                                 in[0] = new_Const(mode, get_mode_null(mode));
3039                         } else {
3040                                 in[0] = new_Unknown(mode);
3041                         }
3042                         ret = new_Return(get_store(), 1, in);
3043                 }
3044                 add_immBlock_pred(end_block, ret);
3045         }
3046
3047         for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3048                 mature_immBlock(imature_blocks[i]);
3049         }
3050         DEL_ARR_F(imature_blocks);
3051         imature_blocks = NULL;
3052
3053         mature_immBlock(first_block);
3054         mature_immBlock(end_block);
3055
3056         irg_finalize_cons(irg);
3057
3058         /* finalize the frame type */
3059         ir_type *frame_type = get_irg_frame_type(irg);
3060         int      n          = get_compound_n_members(frame_type);
3061         int      align_all  = 4;
3062         int      offset     = 0;
3063         for(int i = 0; i < n; ++i) {
3064                 ir_entity *entity      = get_compound_member(frame_type, i);
3065                 ir_type   *entity_type = get_entity_type(entity);
3066
3067                 int align = get_type_alignment_bytes(entity_type);
3068                 if(align > align_all)
3069                         align_all = align;
3070                 int misalign = 0;
3071                 if(align > 0) {
3072                         misalign  = offset % align;
3073                         if(misalign > 0) {
3074                                 offset += align - misalign;
3075                         }
3076                 }
3077
3078                 set_entity_offset(entity, offset);
3079                 offset += get_type_size_bytes(entity_type);
3080         }
3081         set_type_size_bytes(frame_type, offset);
3082         set_type_alignment_bytes(frame_type, align_all);
3083         set_type_state(frame_type, layout_fixed);
3084
3085         irg_vrfy(irg);
3086 }
3087
3088 static void create_global_variable(declaration_t *declaration)
3089 {
3090         ir_visibility  vis;
3091         ir_type       *var_type;
3092         switch ((storage_class_tag_t)declaration->storage_class) {
3093                 case STORAGE_CLASS_STATIC:
3094                         vis = visibility_local;
3095                         goto global_var;
3096
3097                 case STORAGE_CLASS_EXTERN:
3098                         vis = visibility_external_allocated;
3099                         goto global_var;
3100
3101                 case STORAGE_CLASS_NONE:
3102                         vis = visibility_external_visible;
3103                         goto global_var;
3104
3105                 case STORAGE_CLASS_THREAD:
3106                         vis = visibility_external_visible;
3107                         goto tls_var;
3108
3109                 case STORAGE_CLASS_THREAD_EXTERN:
3110                         vis = visibility_external_allocated;
3111                         goto tls_var;
3112
3113                 case STORAGE_CLASS_THREAD_STATIC:
3114                         vis = visibility_local;
3115                         goto tls_var;
3116
3117 tls_var:
3118                         var_type = get_tls_type();
3119                         goto create_var;
3120
3121 global_var:
3122                         var_type = get_glob_type();
3123                         goto create_var;
3124
3125 create_var:
3126                         create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3127                                                   var_type);
3128                         set_entity_visibility(declaration->v.entity, vis);
3129
3130                         current_ir_graph = get_const_code_irg();
3131                         create_initializer(declaration);
3132                         return;
3133
3134                 case STORAGE_CLASS_TYPEDEF:
3135                 case STORAGE_CLASS_AUTO:
3136                 case STORAGE_CLASS_REGISTER:
3137                 case STORAGE_CLASS_ENUM_ENTRY:
3138                         break;
3139         }
3140         panic("Invalid storage class for global variable");
3141 }
3142
3143 static void context_to_firm(context_t *context)
3144 {
3145         /* first pass: create declarations */
3146         declaration_t *declaration = context->declarations;
3147         for( ; declaration != NULL; declaration = declaration->next) {
3148                 if(declaration->namespc != NAMESPACE_NORMAL)
3149                         continue;
3150                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3151                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3152                         continue;
3153                 if(declaration->symbol == NULL)
3154                         continue;
3155
3156                 type_t *type = skip_typeref(declaration->type);
3157                 if(is_type_function(type)) {
3158                         get_function_entity(declaration);
3159                 } else {
3160                         create_global_variable(declaration);
3161                 }
3162         }
3163
3164         /* second pass: create code */
3165         declaration = context->declarations;
3166         for( ; declaration != NULL; declaration = declaration->next) {
3167                 if(declaration->namespc != NAMESPACE_NORMAL)
3168                         continue;
3169                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3170                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3171                         continue;
3172                 if(declaration->symbol == NULL)
3173                         continue;
3174
3175                 type_t *type = declaration->type;
3176                 if(type->type != TYPE_FUNCTION)
3177                         continue;
3178
3179                 create_function(declaration);
3180         }
3181 }
3182
3183 void translation_unit_to_firm(translation_unit_t *unit)
3184 {
3185         type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3186         type_void       = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3187         type_int        = make_atomic_type(ATOMIC_TYPE_INT,  TYPE_QUALIFIER_NONE);
3188
3189         ir_type_int        = get_ir_type(type_int);
3190         ir_type_const_char = get_ir_type(type_const_char);
3191         ir_type_wchar_t    = get_ir_type(type_wchar_t);
3192         ir_type_void       = get_ir_type(type_int); /* we don't have a real void
3193                                                        type in firm */
3194
3195         type_void->base.firm_type = ir_type_void;
3196
3197         /* just to be sure */
3198         continue_label      = NULL;
3199         break_label         = NULL;
3200         current_switch_cond = NULL;
3201
3202         context_to_firm(& unit->context);
3203 }