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