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