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