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