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