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