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