fix cond block in do-while loops
[cparser] / ast2firm.c
1 #include <config.h>
2
3 #define _GNU_SOURCE
4
5 #include <assert.h>
6 #include <string.h>
7 #include <stdbool.h>
8
9 #include <libfirm/firm.h>
10 #include <libfirm/adt/obst.h>
11
12 #include "ast2firm.h"
13
14 #include "adt/error.h"
15 #include "adt/array.h"
16 #include "token_t.h"
17 #include "type_t.h"
18 #include "ast_t.h"
19
20 #define MAGIC_DEFAULT_PN_NUMBER     (long) -314159265
21
22 static ir_type *ir_type_const_char;
23 static ir_type *ir_type_void;
24 static ir_type *ir_type_int;
25 static ir_type *ir_type_void_ptr;
26
27 static type_t *type_const_char;
28 static type_t *type_void;
29 static type_t *type_int;
30
31 static int       next_value_number_function;
32 static ir_node  *continue_label;
33 static ir_node  *break_label;
34 static ir_node  *current_switch_cond;
35 static ir_node **imature_blocks;
36
37 typedef enum declaration_type_t {
38         DECLARATION_TYPE_UNKNOWN,
39         DECLARATION_TYPE_FUNCTION,
40         DECLARATION_TYPE_GLOBAL_VARIABLE,
41         DECLARATION_TYPE_LOCAL_VARIABLE,
42         DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
43         DECLARATION_TYPE_COMPOUND_MEMBER,
44         DECLARATION_TYPE_LABEL_BLOCK,
45 } declaration_type_t;
46
47 static ir_type *get_ir_type(type_t *type);
48
49 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
50 {
51         (void) pos;
52 #if 0
53         const declaration_t *declaration = & value_numbers[pos]->declaration;
54
55         print_warning_prefix(declaration->source_position);
56         fprintf(stderr, "variable '%s' might be used uninitialized\n",
57                         declaration->symbol->string);
58 #endif
59         fprintf(stderr, "Some variable might be used uninitialized\n");
60         return new_r_Unknown(irg, mode);
61 }
62
63 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
64 {
65         const source_position_t *pos = (const source_position_t*) dbg;
66         if(pos == NULL)
67                 return 0;
68         return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
69                                    pos->linenr);
70 }
71
72 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
73 {
74         const source_position_t *pos = (const source_position_t*) dbg;
75         if(pos == NULL)
76                 return NULL;
77         if(line != NULL)
78                 *line = pos->linenr;
79         return pos->input_name;
80 }
81
82 void init_ast2firm(void)
83 {
84         type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
85         type_void       = make_atomic_type(ATOMIC_TYPE_VOID, 0);
86         type_int        = make_atomic_type(ATOMIC_TYPE_INT, 0);
87
88         ir_type_int        = get_ir_type(type_int);
89         ir_type_const_char = get_ir_type(type_const_char);
90         ir_type_void       = get_ir_type(type_int); /* we don't have a real void
91                                                        type in firm */
92         ir_type_void_ptr   = new_type_pointer(new_id_from_str("void_ptr"),
93                                               ir_type_void, mode_P_data);
94
95         type_void->firm_type = ir_type_void;
96 }
97
98 void exit_ast2firm(void)
99 {
100 }
101
102 static unsigned unique_id = 0;
103
104 static ident *unique_ident(const char *tag)
105 {
106         char buf[256];
107
108         snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
109         unique_id++;
110         return new_id_from_str(buf);
111 }
112
113 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
114 {
115         switch(atomic_type->atype) {
116         case ATOMIC_TYPE_SCHAR:
117         case ATOMIC_TYPE_CHAR:
118                 return mode_Bs;
119         case ATOMIC_TYPE_UCHAR:
120                 return mode_Bu;
121         case ATOMIC_TYPE_SHORT:
122                 return mode_Hs;
123         case ATOMIC_TYPE_USHORT:
124                 return mode_Hu;
125         case ATOMIC_TYPE_LONG:
126         case ATOMIC_TYPE_INT:
127                 return mode_Is;
128         case ATOMIC_TYPE_ULONG:
129         case ATOMIC_TYPE_UINT:
130                 return mode_Iu;
131         case ATOMIC_TYPE_LONGLONG:
132                 return mode_Ls;
133         case ATOMIC_TYPE_ULONGLONG:
134                 return mode_Lu;
135         case ATOMIC_TYPE_FLOAT:
136                 return mode_F;
137         case ATOMIC_TYPE_DOUBLE:
138                 return mode_D;
139         case ATOMIC_TYPE_LONG_DOUBLE:
140                 return mode_E;
141         case ATOMIC_TYPE_BOOL:
142                 return mode_b;
143 #ifdef PROVIDE_COMPLEX
144         case ATOMIC_TYPE_FLOAT_COMPLEX:
145         case ATOMIC_TYPE_DOUBLE_COMPLEX:
146         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
147                 panic("complex lowering not implemented yet");
148                 break;
149         case ATOMIC_TYPE_FLOAT_IMAGINARY:
150         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
151         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
152                 panic("imaginary lowering not implemented yet");
153                 break;
154 #endif
155         case ATOMIC_TYPE_VOID:
156                 /* firm has no real void... */
157                 return mode_Is;
158         case ATOMIC_TYPE_INVALID:
159                 break;
160         }
161         panic("Encountered unknown atomic type");
162 }
163
164
165 static unsigned get_type_size(type_t *type);
166
167 static unsigned get_atomic_type_size(const atomic_type_t *type)
168 {
169         switch(type->atype) {
170         case ATOMIC_TYPE_CHAR:
171         case ATOMIC_TYPE_SCHAR:
172         case ATOMIC_TYPE_UCHAR:
173                 return 1;
174
175         case ATOMIC_TYPE_SHORT:
176         case ATOMIC_TYPE_USHORT:
177                 return 2;
178
179         case ATOMIC_TYPE_BOOL:
180         case ATOMIC_TYPE_INT:
181         case ATOMIC_TYPE_UINT:
182         case ATOMIC_TYPE_LONG:
183         case ATOMIC_TYPE_ULONG:
184         case ATOMIC_TYPE_FLOAT:
185                 return 4;
186
187         case ATOMIC_TYPE_LONGLONG:
188         case ATOMIC_TYPE_ULONGLONG:
189         case ATOMIC_TYPE_DOUBLE:
190                 return 8;
191
192         case ATOMIC_TYPE_LONG_DOUBLE:
193                 return 12;
194
195         case ATOMIC_TYPE_VOID:
196                 return 1;
197
198         case ATOMIC_TYPE_INVALID:
199                 break;
200         }
201         panic("Trying to determine size of invalid atomic type");
202 }
203
204 static unsigned get_compound_type_size(compound_type_t *type)
205 {
206         ir_type *irtype = get_ir_type(&type->type);
207         return get_type_size_bytes(irtype);
208 }
209
210 static unsigned get_array_type_size(array_type_t *type)
211 {
212         ir_type *irtype = get_ir_type(&type->type);
213         return get_type_size_bytes(irtype);
214 }
215
216 static unsigned get_type_size(type_t *type)
217 {
218         type = skip_typeref(type);
219
220         switch(type->type) {
221         case TYPE_ATOMIC:
222                 return get_atomic_type_size((const atomic_type_t*) type);
223         case TYPE_ENUM:
224                 return get_mode_size_bytes(mode_Is);
225         case TYPE_COMPOUND_UNION:
226         case TYPE_COMPOUND_STRUCT:
227                 return get_compound_type_size((compound_type_t*) type);
228         case TYPE_FUNCTION:
229                 /* just a pointer to the function */
230                 return get_mode_size_bytes(mode_P_code);
231         case TYPE_POINTER:
232                 return get_mode_size_bytes(mode_P_data);
233         case TYPE_ARRAY:
234                 return get_array_type_size((array_type_t*) type);
235         case TYPE_BUILTIN:
236         case TYPE_TYPEDEF:
237         case TYPE_TYPEOF:
238         case TYPE_INVALID:
239                 break;
240         }
241         panic("Trying to determine size of invalid type");
242 }
243
244 static unsigned count_parameters(const function_type_t *function_type)
245 {
246         unsigned count = 0;
247
248         function_parameter_t *parameter = function_type->parameters;
249         for ( ; parameter != NULL; parameter = parameter->next) {
250                 ++count;
251         }
252
253         return count;
254 }
255
256
257
258
259 static long fold_constant(const expression_t *expression);
260
261 static ir_type *create_atomic_type(const atomic_type_t *type)
262 {
263         ir_mode *mode   = get_atomic_mode(type);
264         ident   *id     = get_mode_ident(mode);
265         ir_type *irtype = new_type_primitive(id, mode);
266
267         return irtype;
268 }
269
270 static ir_type *create_method_type(const function_type_t *function_type)
271 {
272         type_t  *result_type  = function_type->result_type;
273
274         ident   *id           = unique_ident("functiontype");
275         int      n_parameters = count_parameters(function_type);
276         int      n_results    = result_type == type_void ? 0 : 1;
277         ir_type *irtype       = new_type_method(id, n_parameters, n_results);
278
279         if(result_type != type_void) {
280                 ir_type *restype = get_ir_type(result_type);
281                 set_method_res_type(irtype, 0, restype);
282         }
283
284         function_parameter_t *parameter = function_type->parameters;
285         int                   n         = 0;
286         for( ; parameter != NULL; parameter = parameter->next) {
287                 ir_type *p_irtype = get_ir_type(parameter->type);
288                 set_method_param_type(irtype, n, p_irtype);
289                 ++n;
290         }
291
292         if(function_type->variadic || function_type->unspecified_parameters) {
293                 set_method_variadicity(irtype, variadicity_variadic);
294         }
295
296         return irtype;
297 }
298
299 static ir_type *create_pointer_type(pointer_type_t *type)
300 {
301         type_t  *points_to = type->points_to;
302         ir_type *ir_points_to;
303         /* Avoid endless recursion if the points_to type contains this poiner type
304          * again (might be a struct). We therefore first create a void* pointer
305          * and then set the real points_to type
306          */
307         ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
308                                             ir_type_void, mode_P_data);
309         type->type.firm_type  = ir_type;
310
311         ir_points_to = get_ir_type(points_to);
312         set_pointer_points_to_type(ir_type, ir_points_to);
313
314         return ir_type;
315 }
316
317 static ir_type *create_array_type(array_type_t *type)
318 {
319         type_t  *element_type    = type->element_type;
320         ir_type *ir_element_type = get_ir_type(element_type);
321
322         ident   *id      = unique_ident("array");
323         ir_type *ir_type = new_type_array(id, 1, ir_element_type);
324
325         if(type->size != NULL) {
326                 int n_elements = fold_constant(type->size);
327
328                 set_array_bounds_int(ir_type, 0, 0, n_elements);
329
330                 size_t elemsize = get_type_size_bytes(ir_element_type);
331                 int align = get_type_alignment_bytes(ir_element_type);
332                 if(elemsize % align > 0) {
333                         elemsize += align - (elemsize % align);
334                 }
335                 set_type_size_bytes(ir_type, n_elements * elemsize);
336                 set_type_alignment_bytes(ir_type, align);
337                 set_type_state(ir_type, layout_fixed);
338         }
339
340         return ir_type;
341 }
342
343 #define INVALID_TYPE ((ir_type_ptr)-1)
344
345 static ir_type *create_struct_type(compound_type_t *type)
346 {
347         symbol_t *symbol = type->declaration->symbol;
348         ident    *id;
349         if(symbol != NULL) {
350                 id = unique_ident(symbol->string);
351         } else {
352                 id = unique_ident("__anonymous_struct");
353         }
354         ir_type *ir_type = new_type_struct(id);
355
356         type->type.firm_type = ir_type;
357
358         int align_all = 1;
359         int offset    = 0;
360         declaration_t *entry = type->declaration->context.declarations;
361         for( ; entry != NULL; entry = entry->next) {
362                 if(entry->namespace != NAMESPACE_NORMAL)
363                         continue;
364
365                 ident       *ident         = new_id_from_str(entry->symbol->string);
366                 ir_type_ptr  entry_ir_type = get_ir_type(entry->type);
367
368                 int entry_size      = get_type_size_bytes(entry_ir_type);
369                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
370                 int misalign = offset % entry_alignment;
371                 offset += misalign;
372
373                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
374                 set_entity_offset(entity, offset);
375                 add_struct_member(ir_type, entity);
376                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
377                 entry->v.entity         = entity;
378
379                 offset += entry_size;
380                 if(entry_alignment > align_all) {
381                         if(entry_alignment % align_all != 0) {
382                                 panic("Uneven alignments not supported yet");
383                         }
384                         align_all = entry_alignment;
385                 }
386         }
387
388         int misalign = offset % align_all;
389         offset += misalign;
390         set_type_alignment_bytes(ir_type, align_all);
391         set_type_size_bytes(ir_type, offset);
392         set_type_state(ir_type, layout_fixed);
393
394         return ir_type;
395 }
396
397 static ir_type *create_union_type(compound_type_t *type)
398 {
399         declaration_t *declaration = type->declaration;
400         symbol_t      *symbol      = declaration->symbol;
401         ident         *id;
402         if(symbol != NULL) {
403                 id = unique_ident(symbol->string);
404         } else {
405                 id = unique_ident("__anonymous_union");
406         }
407         ir_type  *ir_type = new_type_union(id);
408
409         type->type.firm_type = ir_type;
410
411         int align_all = 1;
412         int size      = 0;
413         declaration_t *entry = declaration->context.declarations;
414         for( ; entry != NULL; entry = entry->next) {
415                 if(entry->namespace != NAMESPACE_NORMAL)
416                         continue;
417
418                 ident       *ident         = new_id_from_str(entry->symbol->string);
419                 ir_type_ptr  entry_ir_type = get_ir_type(entry->type);
420
421                 int entry_size      = get_type_size_bytes(entry_ir_type);
422                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
423
424                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
425                 add_union_member(ir_type, entity);
426                 set_entity_offset(entity, 0);
427                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
428                 entry->v.entity         = entity;
429
430                 if(entry_size > size) {
431                         size = entry_size;
432                 }
433                 if(entry_alignment > align_all) {
434                         if(entry_alignment % align_all != 0) {
435                                 panic("Uneven alignments not supported yet");
436                         }
437                         align_all = entry_alignment;
438                 }
439         }
440
441         set_type_alignment_bytes(ir_type, align_all);
442         set_type_size_bytes(ir_type, size);
443         set_type_state(ir_type, layout_fixed);
444
445         return ir_type;
446 }
447
448 static ir_type *get_ir_type(type_t *type)
449 {
450         assert(type != NULL);
451
452         type = skip_typeref(type);
453
454         if(type->firm_type != NULL) {
455                 assert(type->firm_type != INVALID_TYPE);
456                 return type->firm_type;
457         }
458
459         ir_type *firm_type = NULL;
460         switch(type->type) {
461         case TYPE_ATOMIC:
462                 firm_type = create_atomic_type((atomic_type_t*) type);
463                 break;
464         case TYPE_FUNCTION:
465                 firm_type = create_method_type((function_type_t*) type);
466                 break;
467         case TYPE_POINTER:
468                 firm_type = create_pointer_type((pointer_type_t*) type);
469                 break;
470         case TYPE_ARRAY:
471                 firm_type = create_array_type((array_type_t*) type);
472                 break;
473         case TYPE_COMPOUND_STRUCT:
474                 firm_type = create_struct_type((compound_type_t*) type);
475                 break;
476         case TYPE_COMPOUND_UNION:
477                 firm_type = create_union_type((compound_type_t*) type);
478                 break;
479         case TYPE_ENUM:
480                 firm_type = ir_type_int;
481                 break;
482         case TYPE_BUILTIN:
483         case TYPE_TYPEOF:
484         case TYPE_TYPEDEF:
485         case TYPE_INVALID:
486                 break;
487         }
488         if(firm_type == NULL)
489                 panic("unknown type found");
490
491         type->firm_type = firm_type;
492         return firm_type;
493 }
494
495 static inline ir_mode *get_ir_mode(type_t *type)
496 {
497         ir_type *irtype = get_ir_type(type);
498
499         /* firm doesn't report a mode for arrays somehow... */
500         if(is_Array_type(irtype)) {
501                 return mode_P;
502         }
503
504         ir_mode *mode   = get_type_mode(irtype);
505         assert(mode != NULL);
506         return mode;
507 }
508
509 static ir_entity* get_function_entity(declaration_t *declaration)
510 {
511         if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
512                 return declaration->v.entity;
513         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
514
515         symbol_t *symbol = declaration->symbol;
516         ident    *id     = new_id_from_str(symbol->string);
517
518         ir_type  *global_type    = get_glob_type();
519         ir_type  *ir_type_method = get_ir_type(declaration->type);
520         assert(is_Method_type(ir_type_method));
521
522         ir_entity *entity = new_entity(global_type, id, ir_type_method);
523         set_entity_ld_ident(entity, id);
524         if(declaration->storage_class == STORAGE_CLASS_STATIC
525                         || declaration->is_inline) {
526                 set_entity_visibility(entity, visibility_local);
527         } else if(declaration->init.statement != NULL) {
528                 set_entity_visibility(entity, visibility_external_visible);
529         } else {
530                 set_entity_visibility(entity, visibility_external_allocated);
531         }
532
533         declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
534         declaration->v.entity         = entity;
535
536         return entity;
537 }
538
539
540
541 static ir_node *expression_to_firm(const expression_t *expression);
542 static ir_node *expression_to_modeb(const expression_t *expression);
543
544 static dbg_info *get_dbg_info(const source_position_t *pos)
545 {
546         return (dbg_info*) pos;
547 }
548
549 static ir_node *const_to_firm(const const_t *cnst)
550 {
551         dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
552         ir_mode  *mode = get_ir_mode(cnst->expression.datatype);
553
554         tarval   *tv;
555         if(mode_is_float(mode)) {
556                 tv = new_tarval_from_double(cnst->v.float_value, mode);
557         } else {
558                 tv = new_tarval_from_long(cnst->v.int_value, mode);
559         }
560
561         return new_d_Const(dbgi, mode, tv);
562 }
563
564 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
565 {
566         assert(entity != NULL);
567         union symconst_symbol sym;
568         sym.entity_p = entity;
569         return new_d_SymConst(dbgi, sym, symconst_addr_ent);
570 }
571
572 static ir_node *string_literal_to_firm(const string_literal_t* literal)
573 {
574         ir_type   *global_type = get_glob_type();
575         ir_type   *type        = new_type_array(unique_ident("strtype"), 1,
576                                                 ir_type_const_char);
577
578         ident     *id     = unique_ident("Lstr");
579         ir_entity *entity = new_entity(global_type, id, type);
580         set_entity_ld_ident(entity, id);
581         set_entity_variability(entity, variability_constant);
582
583         ir_type    *elem_type = ir_type_const_char;
584         ir_mode    *mode      = get_type_mode(elem_type);
585
586         const char *string = literal->value;
587         size_t      slen   = strlen(string) + 1;
588
589         set_array_lower_bound_int(type, 0, 0);
590         set_array_upper_bound_int(type, 0, slen);
591         set_type_size_bytes(type, slen);
592         set_type_state(type, layout_fixed);
593
594         tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
595         for(size_t i = 0; i < slen; ++i) {
596                 tvs[i] = new_tarval_from_long(string[i], mode);
597         }
598
599         set_array_entity_values(entity, tvs, slen);
600         free(tvs);
601
602         dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
603
604         return create_symconst(dbgi, entity);
605 }
606
607 static ir_node *load_from_expression_addr(type_t *type, ir_node *addr,
608                                           dbg_info *dbgi)
609 {
610         ir_mode *mode     = get_ir_mode(type);
611         ir_node *memory   = get_store();
612         ir_node *load     = new_d_Load(dbgi, memory, addr, mode);
613         ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
614         ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
615         set_store(load_mem);
616
617         return load_res;
618 }
619
620 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
621 {
622         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
623         declaration_t *declaration = ref->declaration;
624         type_t        *type        = skip_typeref(declaration->type);
625         ir_mode       *mode        = get_ir_mode(type);
626
627         switch((declaration_type_t) declaration->declaration_type) {
628         case DECLARATION_TYPE_UNKNOWN:
629                 break;
630         case DECLARATION_TYPE_LOCAL_VARIABLE:
631                 return get_value(declaration->v.value_number, mode);
632         case DECLARATION_TYPE_FUNCTION: {
633                 return create_symconst(dbgi, declaration->v.entity);
634         }
635         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
636                 ir_entity *entity   = declaration->v.entity;
637                 ir_node   *symconst = create_symconst(dbgi, entity);
638
639                 if(type->type == TYPE_ARRAY) {
640                         return symconst;
641                 } else {
642                         return load_from_expression_addr(type, symconst, dbgi);
643                 }
644         }
645         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
646                 ir_entity *entity = declaration->v.entity;
647                 ir_node   *frame  = get_irg_frame(current_ir_graph);
648                 ir_node   *store  = get_store();
649                 ir_node   *sel    = new_d_simpleSel(dbgi, store, frame, entity);
650
651                 if(type->type == TYPE_ARRAY) {
652                         return sel;
653                 } else {
654                         return load_from_expression_addr(type, sel, dbgi);
655                 }
656         }
657
658         case DECLARATION_TYPE_COMPOUND_MEMBER:
659         case DECLARATION_TYPE_LABEL_BLOCK:
660                 panic("not implemented reference type");
661         }
662
663         panic("reference to declaration with unknown type found");
664 }
665
666 static ir_node *reference_addr(const reference_expression_t *ref)
667 {
668         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
669         declaration_t *declaration = ref->declaration;
670
671         switch((declaration_type_t) declaration->declaration_type) {
672         case DECLARATION_TYPE_UNKNOWN:
673                 break;
674         case DECLARATION_TYPE_LOCAL_VARIABLE:
675                 panic("local variable without entity has no address");
676         case DECLARATION_TYPE_FUNCTION: {
677                 return create_symconst(dbgi, declaration->v.entity);
678         }
679         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
680                 ir_entity *entity   = declaration->v.entity;
681                 ir_node   *symconst = create_symconst(dbgi, entity);
682                 return symconst;
683         }
684         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
685         case DECLARATION_TYPE_COMPOUND_MEMBER:
686         case DECLARATION_TYPE_LABEL_BLOCK:
687                 panic("not implemented reference type");
688         }
689
690         panic("reference to declaration with unknown type found");
691 }
692
693 static ir_node *call_expression_to_firm(const call_expression_t *call)
694 {
695         assert(get_cur_block() != NULL);
696
697         expression_t  *function = call->function;
698         ir_node       *callee   = expression_to_firm(function);
699
700         assert(function->datatype->type == TYPE_FUNCTION);
701         function_type_t *function_type = (function_type_t*) function->datatype;
702
703         int              n_parameters = 0;
704         call_argument_t *argument     = call->arguments;
705         for( ; argument != NULL; argument = argument->next) {
706                 ++n_parameters;
707         }
708
709         ir_type *ir_method_type  = get_ir_type((type_t*) function_type);
710         ir_type *new_method_type = NULL;
711         if(function_type->variadic || function_type->unspecified_parameters) {
712                 /* we need to construct a new method type matching the call
713                  * arguments... */
714                 int n_res       = get_method_n_ress(ir_method_type);
715                 new_method_type = new_type_method(unique_ident("calltype"),
716                                                   n_parameters, n_res);
717                 set_method_calling_convention(new_method_type,
718                                get_method_calling_convention(ir_method_type));
719                 set_method_additional_properties(new_method_type,
720                                get_method_additional_properties(ir_method_type));
721
722                 for(int i = 0; i < n_res; ++i) {
723                         set_method_res_type(new_method_type, i,
724                                             get_method_res_type(ir_method_type, i));
725                 }
726         }
727         ir_node *in[n_parameters];
728
729         argument = call->arguments;
730         int n = 0;
731         for( ; argument != NULL; argument = argument->next) {
732                 expression_t *expression = argument->expression;
733                 ir_node      *arg_node   = expression_to_firm(expression);
734
735                 in[n] = arg_node;
736                 if(new_method_type != NULL) {
737                         ir_type *irtype = get_ir_type(expression->datatype);
738                         set_method_param_type(new_method_type, n, irtype);
739                 }
740
741                 n++;
742         }
743         assert(n == n_parameters);
744
745         if(new_method_type != NULL)
746                 ir_method_type = new_method_type;
747
748         dbg_info *dbgi  = get_dbg_info(&call->expression.source_position);
749         ir_node  *store = get_store();
750         ir_node  *node  = new_d_Call(dbgi, store, callee, n_parameters, in,
751                                      ir_method_type);
752         ir_node  *mem   = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
753         set_store(mem);
754
755         type_t  *result_type = function_type->result_type;
756         ir_node *result      = NULL;
757         if(result_type != type_void) {
758                 ir_mode *mode    = get_ir_mode(result_type);
759                 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
760                 result           = new_d_Proj(dbgi, resproj, mode, 0);
761         }
762
763         return result;
764 }
765
766 static ir_node *expression_to_addr(const expression_t *expression);
767
768 static void set_value_for_expression(const expression_t *expression,
769                                      ir_node *value)
770 {
771         if(expression->type == EXPR_REFERENCE) {
772                 reference_expression_t *ref = (reference_expression_t*) expression;
773
774                 declaration_t *declaration = ref->declaration;
775                 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
776                 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
777                         set_value(declaration->v.value_number, value);
778                         return;
779                 }
780         }
781
782         dbg_info *dbgi      = get_dbg_info(&expression->source_position);
783         ir_node  *addr      = expression_to_addr(expression);
784         assert(get_irn_mode(value) == get_ir_mode(expression->datatype));
785         ir_node  *memory    = get_store();
786         ir_node  *store     = new_d_Store(dbgi, memory, addr, value);
787         ir_node  *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
788         set_store(store_mem);
789 }
790
791 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
792 {
793         ir_mode *value_mode = get_irn_mode(value);
794
795         if(value_mode == dest_mode)
796                 return value;
797
798         if(dest_mode == mode_b) {
799                 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
800                 ir_node *cmp  = new_d_Cmp(dbgi, value, zero);
801                 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
802                 return proj;
803         }
804
805         return new_d_Conv(dbgi, value, dest_mode);
806 }
807
808 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
809 {
810         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
811         type_t   *type = expression->expression.datatype;
812         ir_mode  *mode = get_ir_mode(type);
813
814         if(expression->type == UNEXPR_TAKE_ADDRESS)
815                 return expression_to_addr(expression->value);
816
817         const expression_t *value      = expression->value;
818         ir_node            *value_node = expression_to_firm(value);
819
820         switch(expression->type) {
821         case UNEXPR_NEGATE:
822                 return new_d_Minus(dbgi, value_node, mode);
823         case UNEXPR_PLUS:
824                 return value_node;
825         case UNEXPR_BITWISE_NEGATE:
826                 return new_d_Not(dbgi, value_node, mode);
827         case UNEXPR_NOT:
828                 if(get_irn_mode(value_node) != mode_b) {
829                         value_node = create_conv(dbgi, value_node, mode_b);
830                 }
831                 value_node = new_d_Not(dbgi, value_node, mode_b);
832                 if(mode != mode_b) {
833                         value_node = create_conv(dbgi, value_node, mode);
834                 }
835                 return value_node;
836         case UNEXPR_DEREFERENCE:
837                 return load_from_expression_addr(type, value_node, dbgi);
838         case UNEXPR_POSTFIX_INCREMENT: {
839                 ir_node *one       = new_Const(mode, get_mode_one(mode));
840                 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
841                 set_value_for_expression(value, new_value);
842                 return value_node;
843         }
844         case UNEXPR_POSTFIX_DECREMENT: {
845                 ir_node *one       = new_Const(mode, get_mode_one(mode));
846                 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
847                 set_value_for_expression(value, new_value);
848                 return value_node;
849         }
850         case UNEXPR_PREFIX_INCREMENT: {
851                 ir_node *one       = new_Const(mode, get_mode_one(mode));
852                 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
853                 set_value_for_expression(value, new_value);
854                 return new_value;
855         }
856         case UNEXPR_PREFIX_DECREMENT: {
857                 ir_node *one       = new_Const(mode, get_mode_one(mode));
858                 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
859                 set_value_for_expression(value, new_value);
860                 return new_value;
861         }
862         case UNEXPR_CAST:
863                 return create_conv(dbgi, value_node, mode);
864
865         case UNEXPR_TAKE_ADDRESS:
866         case UNEXPR_INVALID:
867                 break;
868         }
869         panic("invalid UNEXPR type found");
870 }
871
872 static long get_pnc(binary_expression_type_t type)
873 {
874         switch(type) {
875         case BINEXPR_EQUAL:        return pn_Cmp_Eq;
876         case BINEXPR_NOTEQUAL:     return pn_Cmp_Lg;
877         case BINEXPR_LESS:         return pn_Cmp_Lt;
878         case BINEXPR_LESSEQUAL:    return pn_Cmp_Le;
879         case BINEXPR_GREATER:      return pn_Cmp_Gt;
880         case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
881         default:
882                 break;
883         }
884         panic("trying to get pn_Cmp from non-comparison binexpr type");
885 }
886
887 static ir_node *create_lazy_op(const binary_expression_t *expression)
888 {
889         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
890
891         bool is_or = (expression->type == BINEXPR_LOGICAL_OR);
892         assert(is_or || expression->type == BINEXPR_LOGICAL_AND);
893
894         ir_node  *val1       = expression_to_modeb(expression->left);
895         ir_node  *cond       = new_d_Cond(dbgi, val1);
896         ir_node  *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
897         ir_node  *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
898
899         ir_node *fallthrough_block = new_immBlock();
900
901         /* the true case */
902         ir_node *calc_val2_block = new_immBlock();
903         if(is_or) {
904                 add_immBlock_pred(calc_val2_block, false_proj);
905         } else {
906                 add_immBlock_pred(calc_val2_block, true_proj);
907         }
908
909         mature_immBlock(calc_val2_block);
910
911         ir_node *val2 = expression_to_modeb(expression->right);
912         if(get_cur_block() != NULL) {
913                 ir_node *jmp = new_d_Jmp(dbgi);
914                 add_immBlock_pred(fallthrough_block, jmp);
915         }
916
917         /* fallthrough */
918         ir_node *constb;
919         if(is_or) {
920                 constb = new_d_Const(dbgi, mode_b, get_tarval_b_true());
921                 add_immBlock_pred(fallthrough_block, true_proj);
922         } else {
923                 constb = new_d_Const(dbgi, mode_b, get_tarval_b_false());
924                 add_immBlock_pred(fallthrough_block, false_proj);
925         }
926         mature_immBlock(fallthrough_block);
927
928         set_cur_block(fallthrough_block);
929
930         ir_node *in[2] = { val2, constb };
931         ir_node *val   = new_d_Phi(dbgi, 2, in, mode_b);
932
933         return val;
934 }
935
936 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
937                                             ir_node *right, ir_mode *mode);
938
939 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
940                                         create_arithmetic_func func)
941 {
942         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
943         ir_node  *left  = expression_to_firm(expression->left);
944         ir_node  *right = expression_to_firm(expression->right);
945         type_t   *type  = expression->right->datatype;
946         /* be careful with the modes, because in asithmetic assign nodes only
947          * the right operand has the mode of the arithmetic alread */
948         ir_mode  *mode  = get_ir_mode(type);
949         left            = create_conv(dbgi, left, mode);
950         ir_node  *res   = func(dbgi, left, right, mode);
951
952         return res;
953 }
954
955 static ir_node *create_arithmetic_assign_binop(
956                 const binary_expression_t *expression, create_arithmetic_func func)
957 {
958         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
959         ir_node  *value = create_arithmetic_binop(expression, func);
960         type_t   *type  = expression->expression.datatype;
961         ir_mode  *mode  = get_ir_mode(type);
962
963         assert(type->type != TYPE_POINTER);
964
965         value = create_conv(dbgi, value, mode);
966         set_value_for_expression(expression->left, value);
967
968         return value;
969 }
970
971 static ir_node *create_add(const binary_expression_t *expression)
972 {
973         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
974         ir_node  *left  = expression_to_firm(expression->left);
975         ir_node  *right = expression_to_firm(expression->right);
976         type_t   *type  = expression->expression.datatype;
977         ir_mode  *mode  = get_ir_mode(type);
978
979         expression_t *expr_left  = expression->left;
980         expression_t *expr_right = expression->right;
981         type_t       *type_left  = skip_typeref(expr_left->datatype);
982         type_t       *type_right = skip_typeref(expr_right->datatype);
983
984         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
985                 return new_d_Add(dbgi, left, right, mode);
986         }
987
988         ir_node        *pointer;
989         ir_node        *integer;
990         pointer_type_t *pointer_type;
991         if(type_left->type == TYPE_POINTER) {
992                 pointer      = left;
993                 integer      = right;
994                 pointer_type = (pointer_type_t*) type_left;
995         } else {
996                 assert(type_right->type == TYPE_POINTER);
997                 pointer      = right;
998                 integer      = left;
999                 pointer_type = (pointer_type_t*) type_right;
1000         }
1001
1002         type_t   *points_to = pointer_type->points_to;
1003         unsigned  elem_size = get_type_size(points_to);
1004
1005         assert(elem_size >= 1);
1006         if(elem_size > 1) {
1007                 integer       = create_conv(dbgi, integer, mode_Is);
1008                 ir_node *cnst = new_Const_long(mode_Is, (int) elem_size);
1009                 ir_node *mul  = new_d_Mul(dbgi, integer, cnst, mode_Is);
1010                 integer = mul;
1011         }
1012
1013         ir_node *res = new_d_Add(dbgi, pointer, integer, mode);
1014
1015         return res;
1016 }
1017
1018 static ir_node *create_sub(const binary_expression_t *expression)
1019 {
1020         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1021         ir_node  *left  = expression_to_firm(expression->left);
1022         ir_node  *right = expression_to_firm(expression->right);
1023         type_t   *type  = expression->expression.datatype;
1024         ir_mode  *mode  = get_ir_mode(type);
1025
1026         expression_t *expr_left  = expression->left;
1027         expression_t *expr_right = expression->right;
1028         type_t       *type_left  = skip_typeref(expr_left->datatype);
1029         type_t       *type_right = skip_typeref(expr_right->datatype);
1030
1031         if((is_type_arithmetic(type_left) && is_type_arithmetic(type_right))
1032                         || (type_left->type == TYPE_POINTER
1033                                 && type_right->type == TYPE_POINTER)) {
1034                 return new_d_Sub(dbgi, left, right, mode);
1035         }
1036
1037         assert(type_right->type == TYPE_POINTER);
1038         ir_node        *pointer      = left;
1039         ir_node        *integer      = right;
1040         pointer_type_t *pointer_type = (pointer_type_t*) type_right;
1041
1042         type_t   *points_to = pointer_type->points_to;
1043         unsigned  elem_size = get_type_size(points_to);
1044
1045         assert(elem_size >= 1);
1046         if(elem_size > 1) {
1047                 ir_node *cnst = new_Const_long(mode_Iu, elem_size);
1048                 ir_node *mul  = new_d_Mul(dbgi, integer, cnst, mode_Iu);
1049                 integer = mul;
1050         }
1051
1052         ir_node *res = new_d_Sub(dbgi, pointer, integer, mode);
1053
1054         return res;
1055 }
1056
1057 static ir_node *create_shift(const binary_expression_t *expression)
1058 {
1059         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1060         ir_node  *left  = expression_to_firm(expression->left);
1061         ir_node  *right = expression_to_firm(expression->right);
1062         type_t   *type  = expression->expression.datatype;
1063         ir_mode  *mode  = get_ir_mode(type);
1064
1065         /* firm always wants the shift count to be unsigned */
1066         right = create_conv(dbgi, right, mode_Iu);
1067
1068         ir_node *res;
1069
1070         switch(expression->type) {
1071         case BINEXPR_SHIFTLEFT:
1072                 res = new_d_Shl(dbgi, left, right, mode);
1073                 break;
1074         case BINEXPR_SHIFTRIGHT: {
1075                  expression_t *expr_left = expression->left;
1076                  type_t       *type_left = skip_typeref(expr_left->datatype);
1077
1078                  if(is_type_signed(type_left)) {
1079                         res = new_d_Shrs(dbgi, left, right, mode);
1080                  } else {
1081                          res = new_d_Shr(dbgi, left, right, mode);
1082                  }
1083                  break;
1084         }
1085         default:
1086                 panic("create shift op called for non-shift op");
1087         }
1088
1089         return res;
1090 }
1091
1092
1093 static ir_node *create_divmod(const binary_expression_t *expression)
1094 {
1095         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1096         ir_node  *left  = expression_to_firm(expression->left);
1097         ir_node  *right = expression_to_firm(expression->right);
1098         ir_node  *pin   = new_Pin(new_NoMem());
1099         type_t   *type  = expression->expression.datatype;
1100         ir_mode  *mode  = get_ir_mode(type);
1101         ir_node  *op;
1102         ir_node  *res;
1103
1104         if(expression->type == BINEXPR_DIV) {
1105                 if(mode_is_float(mode)) {
1106                         op  = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1107                         res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1108                 } else {
1109                         op  = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1110                         res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1111                 }
1112         } else {
1113                 assert(expression->type == BINEXPR_MOD);
1114                 assert(!mode_is_float(mode));
1115                 op  = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1116                 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1117         }
1118
1119         return res;
1120 }
1121
1122
1123
1124 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1125 {
1126         binary_expression_type_t type = expression->type;
1127         switch(type) {
1128         case BINEXPR_EQUAL:
1129         case BINEXPR_NOTEQUAL:
1130         case BINEXPR_LESS:
1131         case BINEXPR_LESSEQUAL:
1132         case BINEXPR_GREATER:
1133         case BINEXPR_GREATEREQUAL: {
1134                 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1135                 ir_node *left  = expression_to_firm(expression->left);
1136                 ir_node *right = expression_to_firm(expression->right);
1137                 ir_node *cmp   = new_d_Cmp(dbgi, left, right);
1138                 long     pnc   = get_pnc(type);
1139                 ir_node *proj  = new_d_Proj(dbgi, cmp, mode_b, pnc);
1140                 return proj;
1141         }
1142         case BINEXPR_ASSIGN: {
1143                 ir_node *right = expression_to_firm(expression->right);
1144                 set_value_for_expression(expression->left, right);
1145                 return right;
1146         }
1147         case BINEXPR_ADD:
1148                 return create_add(expression);
1149         case BINEXPR_SUB:
1150                 return create_sub(expression);
1151         case BINEXPR_MUL:
1152                 return create_arithmetic_binop(expression, new_d_Mul);
1153         case BINEXPR_BITWISE_AND:
1154                 return create_arithmetic_binop(expression, new_d_And);
1155         case BINEXPR_BITWISE_OR:
1156                 return create_arithmetic_binop(expression, new_d_Or);
1157         case BINEXPR_BITWISE_XOR:
1158                 return create_arithmetic_binop(expression, new_d_Eor);
1159         case BINEXPR_SHIFTLEFT:
1160         case BINEXPR_SHIFTRIGHT:
1161                 return create_shift(expression);
1162         case BINEXPR_DIV:
1163         case BINEXPR_MOD:
1164                 return create_divmod(expression);
1165         case BINEXPR_LOGICAL_AND:
1166         case BINEXPR_LOGICAL_OR:
1167                 return create_lazy_op(expression);
1168         case BINEXPR_COMMA:
1169                 expression_to_firm(expression->left);
1170                 return expression_to_firm(expression->right);
1171         case BINEXPR_ADD_ASSIGN:
1172                 return create_arithmetic_assign_binop(expression, new_d_Add);
1173         case BINEXPR_SUB_ASSIGN:
1174                 return create_arithmetic_assign_binop(expression, new_d_Sub);
1175         case BINEXPR_MUL_ASSIGN:
1176                 return create_arithmetic_assign_binop(expression, new_d_Mul);
1177         case BINEXPR_BITWISE_AND_ASSIGN:
1178                 return create_arithmetic_assign_binop(expression, new_d_And);
1179         case BINEXPR_BITWISE_OR_ASSIGN:
1180                 return create_arithmetic_assign_binop(expression, new_d_Or);
1181         case BINEXPR_BITWISE_XOR_ASSIGN:
1182                 return create_arithmetic_assign_binop(expression, new_d_Eor);
1183         case BINEXPR_SHIFTLEFT_ASSIGN:
1184                 return create_arithmetic_assign_binop(expression, new_d_Shl);
1185         case BINEXPR_SHIFTRIGHT_ASSIGN:
1186                 return create_arithmetic_assign_binop(expression, new_d_Shr);
1187         default:
1188                 panic("TODO binexpr type");
1189         }
1190 }
1191
1192 static ir_node *array_access_addr(const array_access_expression_t *expression)
1193 {
1194         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1195
1196         ir_node *base_addr = expression_to_firm(expression->array_ref);
1197         ir_node *offset    = expression_to_firm(expression->index);
1198         offset             = create_conv(dbgi, offset, mode_Iu);
1199
1200         unsigned elem_size       = get_type_size(expression->expression.datatype);
1201         ir_node *elem_size_const = new_Const_long(mode_Iu, elem_size);
1202         ir_node *real_offset     = new_d_Mul(dbgi, offset, elem_size_const,
1203                                              mode_Iu);
1204         ir_node *result          = new_d_Add(dbgi, base_addr, real_offset, mode_P);
1205
1206         return result;
1207 }
1208
1209 static ir_node *array_access_to_firm(
1210                 const array_access_expression_t *expression)
1211 {
1212         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1213         ir_node  *addr = array_access_addr(expression);
1214         type_t   *type = expression->expression.datatype;
1215
1216         return load_from_expression_addr(type, addr, dbgi);
1217 }
1218
1219 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1220 {
1221         type_t *type = expression->type;
1222         if(type == NULL) {
1223                 type = expression->size_expression->datatype;
1224                 assert(type != NULL);
1225         }
1226
1227         ir_mode  *mode      = get_ir_mode(expression->expression.datatype);
1228         unsigned  size      = get_type_size(type);
1229         ir_node  *size_node = new_Const_long(mode, size);
1230
1231         return size_node;
1232 }
1233
1234 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1235 {
1236         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1237
1238         ir_node *condition  = expression_to_modeb(expression->condition);
1239         ir_node *cond       = new_d_Cond(dbgi, condition);
1240         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1241         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1242
1243         /* create the true block */
1244         ir_node *true_block = new_immBlock();
1245         add_immBlock_pred(true_block, true_proj);
1246         mature_immBlock(true_block);
1247
1248         ir_node *true_val = expression_to_firm(expression->true_expression);
1249         ir_node *true_jmp = new_Jmp();
1250
1251         /* create the false block */
1252         ir_node *false_block = new_immBlock();
1253         add_immBlock_pred(false_block, false_proj);
1254         mature_immBlock(false_block);
1255
1256         ir_node *false_val = expression_to_firm(expression->false_expression);
1257         ir_node *false_jmp = new_Jmp();
1258
1259         /* create the common block */
1260         ir_node *common_block = new_immBlock();
1261         add_immBlock_pred(common_block, true_jmp);
1262         add_immBlock_pred(common_block, false_jmp);
1263         mature_immBlock(common_block);
1264
1265         ir_node *in[2] = { true_val, false_val };
1266         ir_mode *mode  = get_irn_mode(true_val);
1267         assert(get_irn_mode(false_val) == mode);
1268         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
1269
1270         return val;
1271 }
1272
1273 static ir_node *expression_to_addr(const expression_t *expression)
1274 {
1275         switch(expression->type) {
1276         case EXPR_REFERENCE:
1277                 return reference_addr((const reference_expression_t*) expression);
1278         case EXPR_ARRAY_ACCESS:
1279                 return array_access_addr((const array_access_expression_t*) expression);
1280         default:
1281                 break;
1282         }
1283         panic("trying to get address of non-lvalue");
1284 }
1285
1286 static ir_node *_expression_to_firm(const expression_t *expression)
1287 {
1288         switch(expression->type) {
1289         case EXPR_CONST:
1290                 return const_to_firm((const const_t*) expression);
1291         case EXPR_STRING_LITERAL:
1292                 return string_literal_to_firm((const string_literal_t*) expression);
1293         case EXPR_REFERENCE:
1294                 return reference_expression_to_firm(
1295                                 (const reference_expression_t*) expression);
1296         case EXPR_CALL:
1297                 return call_expression_to_firm((const call_expression_t*) expression);
1298         case EXPR_UNARY:
1299                 return unary_expression_to_firm((const unary_expression_t*) expression);
1300         case EXPR_BINARY:
1301                 return binary_expression_to_firm(
1302                                 (const binary_expression_t*) expression);
1303         case EXPR_ARRAY_ACCESS:
1304                 return array_access_to_firm(
1305                                 (const array_access_expression_t*) expression);
1306         case EXPR_SIZEOF:
1307                 return sizeof_to_firm((const sizeof_expression_t*) expression);
1308         case EXPR_CONDITIONAL:
1309                 return conditional_to_firm((const conditional_expression_t*)expression);
1310         default:
1311                 break;
1312         }
1313         panic("unsupported expression found");
1314 }
1315
1316 static ir_node *expression_to_firm(const expression_t *expression)
1317 {
1318         ir_node *res  = _expression_to_firm(expression);
1319
1320         if(expression->datatype == type_void)
1321                 return NULL;
1322
1323         ir_mode *mode = get_ir_mode(expression->datatype);
1324         res           = create_conv(NULL, res, mode);
1325         return res;
1326 }
1327
1328 static ir_node *expression_to_modeb(const expression_t *expression)
1329 {
1330         ir_node *res = _expression_to_firm(expression);
1331         res          = create_conv(NULL, res, mode_b);
1332
1333         return res;
1334 }
1335
1336 static void statement_to_firm(statement_t *statement);
1337
1338 static void return_statement_to_firm(return_statement_t *statement)
1339 {
1340         if(get_cur_block() == NULL)
1341                 return;
1342
1343         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1344         ir_node  *ret;
1345
1346         if(statement->return_value != NULL) {
1347                 ir_node *retval = expression_to_firm(statement->return_value);
1348                 ir_node *in[1];
1349
1350                 in[0] = retval;
1351                 ret   = new_d_Return(dbgi, get_store(), 1, in);
1352         } else {
1353                 ret   = new_d_Return(dbgi, get_store(), 0, NULL);
1354         }
1355         ir_node *end_block = get_irg_end_block(current_ir_graph);
1356         add_immBlock_pred(end_block, ret);
1357
1358         set_cur_block(NULL);
1359 }
1360
1361 static void compound_statement_to_firm(compound_statement_t *compound)
1362 {
1363         statement_t *statement = compound->statements;
1364         for( ; statement != NULL; statement = statement->next) {
1365                 //context2firm(&statement->context);
1366                 statement_to_firm(statement);
1367         }
1368 }
1369
1370 static void expression_statement_to_firm(expression_statement_t *statement)
1371 {
1372         if(get_cur_block() == NULL)
1373                 return;
1374
1375         expression_to_firm(statement->expression);
1376 }
1377
1378 static void if_statement_to_firm(if_statement_t *statement)
1379 {
1380         dbg_info *dbgi      = get_dbg_info(&statement->statement.source_position);
1381         ir_node  *condition = expression_to_modeb(statement->condition);
1382
1383         /* make sure we have a mode_b condition */
1384         ir_node *cond       = new_d_Cond(dbgi, condition);
1385         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1386         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1387
1388         ir_node *fallthrough_block = new_immBlock();
1389
1390         /* the true (blocks) */
1391         ir_node *true_block = new_immBlock();
1392         add_immBlock_pred(true_block, true_proj);
1393         mature_immBlock(true_block);
1394
1395         statement_to_firm(statement->true_statement);
1396         if(get_cur_block() != NULL) {
1397                 ir_node *jmp = new_Jmp();
1398                 add_immBlock_pred(fallthrough_block, jmp);
1399         }
1400
1401         /* the false (blocks) */
1402         if(statement->false_statement != NULL) {
1403                 ir_node *false_block = new_immBlock();
1404                 add_immBlock_pred(false_block, false_proj);
1405                 mature_immBlock(false_block);
1406
1407                 statement_to_firm(statement->false_statement);
1408                 if(get_cur_block() != NULL) {
1409                         ir_node *jmp = new_Jmp();
1410                         add_immBlock_pred(fallthrough_block, jmp);
1411                 }
1412         } else {
1413                 add_immBlock_pred(fallthrough_block, false_proj);
1414         }
1415         mature_immBlock(fallthrough_block);
1416
1417         set_cur_block(fallthrough_block);
1418 }
1419
1420 static void while_statement_to_firm(while_statement_t *statement)
1421 {
1422         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1423
1424         ir_node *jmp = NULL;
1425         if(get_cur_block() != NULL) {
1426                 jmp = new_Jmp();
1427         }
1428
1429         /* create the header block */
1430         ir_node *header_block = new_immBlock();
1431         if(jmp != NULL) {
1432                 add_immBlock_pred(header_block, jmp);
1433         }
1434
1435         /* create the condition */
1436         ir_node *condition  = expression_to_modeb(statement->condition);
1437         ir_node *cond       = new_d_Cond(dbgi, condition);
1438         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1439         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1440
1441         /* the false block */
1442         ir_node *false_block = new_immBlock();
1443         add_immBlock_pred(false_block, false_proj);
1444
1445         /* the loop body */
1446         ir_node *body_block = new_immBlock();
1447         add_immBlock_pred(body_block, true_proj);
1448         mature_immBlock(body_block);
1449
1450         ir_node *old_continue_label = continue_label;
1451         ir_node *old_break_label    = break_label;
1452         continue_label              = header_block;
1453         break_label                 = false_block;
1454
1455         statement_to_firm(statement->body);
1456
1457         assert(continue_label == header_block);
1458         assert(break_label    == false_block);
1459         continue_label = old_continue_label;
1460         break_label    = old_break_label;
1461
1462         if(get_cur_block() != NULL) {
1463                 ir_node *jmp = new_Jmp();
1464                 add_immBlock_pred(header_block, jmp);
1465         }
1466
1467         mature_immBlock(header_block);
1468         mature_immBlock(false_block);
1469
1470         set_cur_block(false_block);
1471 }
1472
1473 static void do_while_statement_to_firm(do_while_statement_t *statement)
1474 {
1475         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1476
1477         ir_node *jmp = NULL;
1478         if(get_cur_block() != NULL) {
1479                 jmp = new_Jmp();
1480         }
1481
1482         /* create the header block */
1483         ir_node *header_block = new_immBlock();
1484
1485         /* the false block */
1486         ir_node *false_block = new_immBlock();
1487
1488         /* the loop body */
1489         ir_node *body_block = new_immBlock();
1490         if(jmp != NULL) {
1491                 add_immBlock_pred(body_block, jmp);
1492         }
1493
1494         ir_node *old_continue_label = continue_label;
1495         ir_node *old_break_label    = break_label;
1496         continue_label              = header_block;
1497         break_label                 = false_block;
1498
1499         statement_to_firm(statement->body);
1500
1501         assert(continue_label == header_block);
1502         assert(break_label    == false_block);
1503         continue_label = old_continue_label;
1504         break_label    = old_break_label;
1505
1506         if(get_cur_block() == NULL) {
1507                 mature_immBlock(header_block);
1508                 mature_immBlock(body_block);
1509                 mature_immBlock(false_block);
1510                 return;
1511         }
1512
1513         ir_node *body_jmp = new_Jmp();
1514         add_immBlock_pred(header_block, body_jmp);
1515         mature_immBlock(header_block);
1516
1517         /* create the condition */
1518         set_cur_block(header_block);
1519         ir_node *condition  = expression_to_modeb(statement->condition);
1520         ir_node *cond       = new_d_Cond(dbgi, condition);
1521         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1522         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1523
1524         add_immBlock_pred(body_block, true_proj);
1525         mature_immBlock(body_block);
1526
1527         add_immBlock_pred(false_block, false_proj);
1528         mature_immBlock(false_block);
1529
1530         set_cur_block(false_block);
1531 }
1532
1533 static void for_statement_to_firm(for_statement_t *statement)
1534 {
1535         dbg_info *const dbgi = get_dbg_info(&statement->statement.source_position);
1536
1537         ir_node *jmp = NULL;
1538         if (get_cur_block() != NULL) {
1539                 if(statement->initialisation != NULL) {
1540                         expression_to_firm(statement->initialisation);
1541                 }
1542                 jmp = new_Jmp();
1543         }
1544
1545         /* create the step block */
1546         ir_node *const step_block = new_immBlock();
1547         if (statement->step != NULL) {
1548                 expression_to_firm(statement->step);
1549         }
1550         ir_node *const step_jmp   = new_Jmp();
1551
1552         /* create the header block */
1553         ir_node *const header_block = new_immBlock();
1554         if (jmp != NULL) {
1555                 add_immBlock_pred(header_block, jmp);
1556         }
1557         add_immBlock_pred(header_block, step_jmp);
1558
1559         /* create the condition */
1560         ir_node *true_proj;
1561         ir_node *false_proj;
1562         if (statement->condition != NULL) {
1563                 ir_node *const condition  = expression_to_modeb(statement->condition);
1564                 ir_node *const cond       = new_d_Cond(dbgi, condition);
1565                 true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1566                 false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1567         } else {
1568                 keep_alive(header_block);
1569                 true_proj  = new_Jmp();
1570                 false_proj = NULL;
1571         }
1572
1573         /* the false block */
1574         ir_node *const false_block = new_immBlock();
1575         if (false_proj != NULL) {
1576                 add_immBlock_pred(false_block, false_proj);
1577         }
1578
1579         /* the loop body */
1580         ir_node *const body_block = new_immBlock();
1581         add_immBlock_pred(body_block, true_proj);
1582         mature_immBlock(body_block);
1583
1584         ir_node *const old_continue_label = continue_label;
1585         ir_node *const old_break_label    = break_label;
1586         continue_label = step_block;
1587         break_label    = false_block;
1588
1589         statement_to_firm(statement->body);
1590
1591         assert(continue_label == step_block);
1592         assert(break_label    == false_block);
1593         continue_label = old_continue_label;
1594         break_label    = old_break_label;
1595
1596         if (get_cur_block() != NULL) {
1597                 ir_node *const jmp = new_Jmp();
1598                 add_immBlock_pred(step_block, jmp);
1599         }
1600
1601         mature_immBlock(step_block);
1602         mature_immBlock(header_block);
1603         mature_immBlock(false_block);
1604
1605         set_cur_block(false_block);
1606 }
1607
1608 static void create_declaration_entity(declaration_t *declaration,
1609                                       declaration_type_t declaration_type,
1610                                       ir_type *parent_type)
1611 {
1612         ident     *id     = new_id_from_str(declaration->symbol->string);
1613         ir_type   *irtype = get_ir_type(declaration->type);
1614         ir_entity *entity = new_entity(parent_type, id, irtype);
1615         set_entity_ld_ident(entity, id);
1616
1617         declaration->declaration_type = declaration_type;
1618         declaration->v.entity         = entity;
1619         set_entity_variability(entity, variability_uninitialized);
1620         /* TODO: visibility? */
1621 }
1622
1623 static void create_initializer(declaration_t *declaration)
1624 {
1625         initializer_t *initializer = declaration->init.initializer;
1626         if(initializer == NULL)
1627                 return;
1628
1629         if(initializer->type == INITIALIZER_VALUE) {
1630                 assert(initializer->designator == NULL);
1631                 assert(initializer->next == NULL);
1632                 ir_node *init_node = expression_to_firm(initializer->v.value);
1633
1634                 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1635                         set_value(declaration->v.value_number, init_node);
1636                 } else {
1637                         ir_entity *entity = declaration->v.entity;
1638
1639                         set_entity_variability(entity, variability_initialized);
1640                         set_atomic_ent_value(entity, init_node);
1641                 }
1642         } else {
1643                 assert(initializer->type == INITIALIZER_LIST);
1644                 panic("list initializer not supported yet");
1645         }
1646 }
1647
1648 static void create_local_variable(declaration_t *declaration)
1649 {
1650         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1651
1652         bool needs_entity = declaration->address_taken;
1653         type_t *type = declaration->type;
1654         if(type->type == TYPE_ARRAY
1655                         || type->type == TYPE_COMPOUND_STRUCT
1656                         || type->type == TYPE_COMPOUND_UNION) {
1657                 needs_entity = true;
1658         }
1659
1660         if(needs_entity) {
1661                 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1662                 create_declaration_entity(declaration,
1663                                           DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1664                                           frame_type);
1665         } else {
1666                 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1667                 declaration->v.value_number   = next_value_number_function;
1668                 ++next_value_number_function;
1669         }
1670
1671         create_initializer(declaration);
1672 }
1673
1674 static void declaration_statement_to_firm(declaration_statement_t *statement)
1675 {
1676         declaration_t *declaration = statement->declarations_begin;
1677         declaration_t *end         = statement->declarations_end->next;
1678         for( ; declaration != end; declaration = declaration->next) {
1679                 type_t *type = declaration->type;
1680
1681                 switch(declaration->storage_class) {
1682                 case STORAGE_CLASS_TYPEDEF:
1683                         continue;
1684                 case STORAGE_CLASS_STATIC:
1685                         panic("static local vars not implemented yet");
1686                 case STORAGE_CLASS_ENUM_ENTRY:
1687                         panic("enum entry declaration in local block found");
1688                 case STORAGE_CLASS_EXTERN:
1689                         panic("extern declaration in local block found");
1690                 case STORAGE_CLASS_NONE:
1691                 case STORAGE_CLASS_AUTO:
1692                 case STORAGE_CLASS_REGISTER:
1693                         if(type->type == TYPE_FUNCTION) {
1694                                 panic("nested functions not supported yet");
1695                         } else {
1696                                 create_local_variable(declaration);
1697                         }
1698                         continue;
1699                 }
1700                 panic("invalid storage class found");
1701         }
1702 }
1703
1704 static void create_jump_statement(const statement_t *statement,
1705                                   ir_node *target_block)
1706 {
1707         if(get_cur_block() == NULL)
1708                 return;
1709
1710         dbg_info *dbgi = get_dbg_info(&statement->source_position);
1711         ir_node  *jump = new_d_Jmp(dbgi);
1712         add_immBlock_pred(target_block, jump);
1713
1714         set_cur_block(NULL);
1715 }
1716
1717 static void switch_statement_to_firm(const switch_statement_t *statement)
1718 {
1719         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1720
1721         ir_node *expression  = expression_to_firm(statement->expression);
1722         ir_node *cond        = new_d_Cond(dbgi, expression);
1723         ir_node *break_block = new_immBlock();
1724
1725         set_cur_block(NULL);
1726
1727         ir_node *old_switch_cond = current_switch_cond;
1728         ir_node *old_break_label = break_label;
1729         current_switch_cond      = cond;
1730         break_label              = break_block;
1731
1732         statement_to_firm(statement->body);
1733
1734         if(get_cur_block() != NULL) {
1735                 ir_node *jmp = new_Jmp();
1736                 add_immBlock_pred(break_block, jmp);
1737         }
1738
1739         assert(current_switch_cond == cond);
1740         assert(break_label         == break_block);
1741         current_switch_cond = old_switch_cond;
1742         break_label         = old_break_label;
1743
1744         mature_immBlock(break_block);
1745         set_cur_block(break_block);
1746 }
1747
1748 static long fold_constant(const expression_t *expression)
1749 {
1750         ir_graph *old_current_ir_graph = current_ir_graph;
1751         current_ir_graph = get_const_code_irg();
1752
1753         ir_node *cnst = expression_to_firm(expression);
1754         if(!is_Const(cnst)) {
1755                 panic("couldn't fold constantl");
1756         }
1757         tarval *tv = get_Const_tarval(cnst);
1758         if(!tarval_is_long(tv)) {
1759                 panic("folded constant not an integer");
1760         }
1761
1762         long res = get_tarval_long(tv);
1763
1764         current_ir_graph = old_current_ir_graph;
1765         return res;
1766 }
1767
1768 static void case_label_to_firm(const case_label_statement_t *statement)
1769 {
1770         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
1771
1772         /* let's create a node and hope firm constant folding creates a Const
1773          * node... */
1774         ir_node *proj;
1775         set_cur_block(get_nodes_block(current_switch_cond));
1776         if(statement->expression) {
1777                 long pn = fold_constant(statement->expression);
1778                 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
1779                         /* oops someone detected our cheating... */
1780                         panic("magic default pn used");
1781                 }
1782                 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
1783         } else {
1784                 proj = new_d_defaultProj(dbgi, current_switch_cond,
1785                                          MAGIC_DEFAULT_PN_NUMBER);
1786         }
1787
1788         ir_node *block = new_immBlock();
1789         add_immBlock_pred(block, proj);
1790         mature_immBlock(block);
1791 }
1792
1793 static ir_node *get_label_block(declaration_t *label)
1794 {
1795         assert(label->namespace == NAMESPACE_LABEL);
1796
1797         if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
1798                 return label->v.block;
1799         }
1800         assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
1801
1802         ir_node *old_cur_block = get_cur_block();
1803         ir_node *block         = new_immBlock();
1804         set_cur_block(old_cur_block);
1805
1806         label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
1807         label->v.block          = block;
1808
1809         ARR_APP1(imature_blocks, block);
1810
1811         return block;
1812 }
1813
1814 static void label_to_firm(const label_statement_t *statement)
1815 {
1816         ir_node *block = get_label_block(statement->label);
1817
1818         if(get_cur_block() != NULL) {
1819                 ir_node *jmp = new_Jmp();
1820                 add_immBlock_pred(block, jmp);
1821         }
1822
1823         set_cur_block(block);
1824         keep_alive(block);
1825
1826         statement_to_firm(statement->label_statement);
1827 }
1828
1829 static void goto_to_firm(const goto_statement_t *statement)
1830 {
1831         if(get_cur_block() == NULL)
1832                 return;
1833
1834         ir_node *block = get_label_block(statement->label);
1835         ir_node *jmp   = new_Jmp();
1836         add_immBlock_pred(block, jmp);
1837
1838         set_cur_block(NULL);
1839 }
1840
1841 static void statement_to_firm(statement_t *statement)
1842 {
1843         switch(statement->type) {
1844         case STATEMENT_COMPOUND:
1845                 compound_statement_to_firm((compound_statement_t*) statement);
1846                 return;
1847         case STATEMENT_RETURN:
1848                 return_statement_to_firm((return_statement_t*) statement);
1849                 return;
1850         case STATEMENT_EXPRESSION:
1851                 expression_statement_to_firm((expression_statement_t*) statement);
1852                 return;
1853         case STATEMENT_IF:
1854                 if_statement_to_firm((if_statement_t*) statement);
1855                 return;
1856         case STATEMENT_WHILE:
1857                 while_statement_to_firm((while_statement_t*) statement);
1858                 return;
1859         case STATEMENT_DO_WHILE:
1860                 do_while_statement_to_firm((do_while_statement_t*) statement);
1861                 return;
1862         case STATEMENT_DECLARATION:
1863                 declaration_statement_to_firm((declaration_statement_t*) statement);
1864                 return;
1865         case STATEMENT_BREAK:
1866                 create_jump_statement(statement, break_label);
1867                 return;
1868         case STATEMENT_CONTINUE:
1869                 create_jump_statement(statement, continue_label);
1870                 return;
1871         case STATEMENT_SWITCH:
1872                 switch_statement_to_firm((switch_statement_t*) statement);
1873                 return;
1874         case STATEMENT_CASE_LABEL:
1875                 case_label_to_firm((case_label_statement_t*) statement);
1876                 return;
1877         case STATEMENT_FOR:
1878                 for_statement_to_firm((for_statement_t*) statement);
1879                 return;
1880         case STATEMENT_LABEL:
1881                 label_to_firm((label_statement_t*) statement);
1882                 return;
1883         case STATEMENT_GOTO:
1884                 goto_to_firm((goto_statement_t*) statement);
1885                 return;
1886         default:
1887                 break;
1888         }
1889         panic("Statement not implemented\n");
1890 }
1891
1892 static int get_function_n_local_vars(declaration_t *declaration)
1893 {
1894         (void) declaration;
1895         /* TODO */
1896         return 30;
1897 }
1898
1899 static void initialize_function_parameters(declaration_t *declaration)
1900 {
1901         ir_graph *irg         = current_ir_graph;
1902         ir_node  *args        = get_irg_args(irg);
1903         ir_node  *start_block = get_irg_start_block(irg);
1904
1905         int            n         = 0;
1906         declaration_t *parameter = declaration->context.declarations;
1907         for( ; parameter != NULL; parameter = parameter->next) {
1908                 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
1909
1910                 if(parameter->address_taken) {
1911                         panic("address take from parameter not implemented yet");
1912                 }
1913
1914                 ir_mode *mode = get_ir_mode(parameter->type);
1915                 long     pn   = n;
1916                 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
1917                 ++n;
1918
1919                 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1920                 parameter->v.value_number   = next_value_number_function;
1921                 ++next_value_number_function;
1922
1923                 set_value(parameter->v.value_number, proj);
1924         }
1925 }
1926
1927 static void create_function(declaration_t *declaration)
1928 {
1929         ir_entity *entity = get_function_entity(declaration);
1930
1931         if(declaration->init.statement == NULL)
1932                 return;
1933
1934         assert(imature_blocks == NULL);
1935         imature_blocks = NEW_ARR_F(ir_node*, 0);
1936
1937         int       n_local_vars = get_function_n_local_vars(declaration);
1938         ir_graph *irg          = new_ir_graph(entity, n_local_vars);
1939         ir_node  *first_block  = get_cur_block();
1940
1941         next_value_number_function = 0;
1942         initialize_function_parameters(declaration);
1943
1944         statement_to_firm(declaration->init.statement);
1945
1946         ir_node *end_block = get_irg_end_block(irg);
1947
1948         /* do we have a return statement yet? */
1949         if(get_cur_block() != NULL) {
1950                 assert(declaration->type->type == TYPE_FUNCTION);
1951                 const function_type_t* const func_type
1952                         = (const function_type_t*) declaration->type;
1953                 ir_node *ret;
1954                 if (func_type->result_type == type_void) {
1955                         ret = new_Return(get_store(), 0, NULL);
1956                 } else {
1957                         ir_mode *const mode = get_ir_mode(func_type->result_type);
1958                         ir_node *      in[1];
1959                         // ยง5.1.2.2.3 main implicitly returns 0
1960                         if (strcmp(declaration->symbol->string, "main") == 0) {
1961                                 in[0] = new_Const(mode, get_mode_null(mode));
1962                         } else {
1963                                 in[0] = new_Unknown(mode);
1964                         }
1965                         ret = new_Return(get_store(), 1, in);
1966                 }
1967                 add_immBlock_pred(end_block, ret);
1968         }
1969
1970         for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
1971                 mature_immBlock(imature_blocks[i]);
1972         }
1973         DEL_ARR_F(imature_blocks);
1974         imature_blocks = NULL;
1975
1976         mature_immBlock(first_block);
1977         mature_immBlock(end_block);
1978
1979         irg_finalize_cons(irg);
1980
1981         /* finalize the frame type */
1982         ir_type *frame_type = get_irg_frame_type(irg);
1983         int      n          = get_compound_n_members(frame_type);
1984         int      align_all  = 4;
1985         int      offset     = 0;
1986         for(int i = 0; i < n; ++i) {
1987                 ir_entity *entity      = get_compound_member(frame_type, i);
1988                 ir_type   *entity_type = get_entity_type(entity);
1989
1990                 int align = get_type_alignment_bytes(entity_type);
1991                 if(align > align_all)
1992                         align_all = align;
1993                 int misalign = 0;
1994                 if(align > 0) {
1995                         misalign  = offset % align;
1996                         offset   += misalign;
1997                 }
1998
1999                 set_entity_offset(entity, offset);
2000                 offset += get_type_size_bytes(entity_type);
2001         }
2002         set_type_size_bytes(frame_type, offset);
2003         set_type_alignment_bytes(frame_type, align_all);
2004         set_type_state(frame_type, layout_fixed);
2005
2006         irg_vrfy(irg);
2007 }
2008
2009 static void create_global_variable(declaration_t *declaration)
2010 {
2011         ir_type   *global_type = get_glob_type();
2012         create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
2013                                   global_type);
2014
2015         ir_entity *entity = declaration->v.entity;
2016         if(declaration->storage_class == STORAGE_CLASS_STATIC) {
2017                 set_entity_visibility(entity, visibility_local);
2018         } else if(declaration->storage_class == STORAGE_CLASS_EXTERN) {
2019                 set_entity_visibility(entity, visibility_external_allocated);
2020         } else {
2021                 set_entity_visibility(entity, visibility_external_visible);
2022         }
2023         current_ir_graph = get_const_code_irg();
2024         create_initializer(declaration);
2025 }
2026
2027 static void context_to_firm(context_t *context)
2028 {
2029         declaration_t *declaration = context->declarations;
2030         for( ; declaration != NULL; declaration = declaration->next) {
2031                 if(declaration->namespace != NAMESPACE_NORMAL)
2032                         continue;
2033                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
2034                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
2035                         continue;
2036                 if(declaration->symbol == NULL)
2037                         continue;
2038
2039                 type_t *type = declaration->type;
2040                 if(type->type == TYPE_FUNCTION) {
2041                         create_function(declaration);
2042                 } else {
2043                         create_global_variable(declaration);
2044                 }
2045         }
2046 }
2047
2048 void translation_unit_to_firm(translation_unit_t *unit)
2049 {
2050         /* remove me later TODO FIXME */
2051         (void) get_type_size;
2052
2053         /* just to be sure */
2054         continue_label      = NULL;
2055         break_label         = NULL;
2056         current_switch_cond = NULL;
2057
2058         context_to_firm(& unit->context);
2059 }