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