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