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