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