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