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