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