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