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