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