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