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