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