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