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