more work on local variable support
[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 static ir_type *ir_type_const_char;
20 static ir_type *ir_type_void;
21 static ir_type *ir_type_int;
22 static ir_type *ir_type_void_ptr;
23
24 static type_t *type_const_char;
25 static type_t *type_void;
26 static type_t *type_int;
27
28 static int next_value_number_function;
29
30 typedef enum declaration_type_t {
31         DECLARATION_TYPE_UNKNOWN,
32         DECLARATION_TYPE_FUNCTION,
33         DECLARATION_TYPE_GLOBAL_VARIABLE,
34         DECLARATION_TYPE_LOCAL_VARIABLE,
35         DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
36         DECLARATION_TYPE_COMPOUND_MEMBER
37 } declaration_type_t;
38
39 typedef struct type2firm_env_t type2firm_env_t;
40 struct type2firm_env_t {
41         int can_cache;       /* nonzero if type can safely be cached because
42                                 no typevariables are in the hierarchy */
43 };
44
45 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type);
46 static ir_type *get_ir_type(type_t *type);
47
48 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
49 {
50         (void) pos;
51 #if 0
52         const declaration_t *declaration = & value_numbers[pos]->declaration;
53
54         print_warning_prefix(declaration->source_position);
55         fprintf(stderr, "variable '%s' might be used uninitialized\n",
56                         declaration->symbol->string);
57 #endif
58         fprintf(stderr, "Some variable might be used uninitialized\n");
59         return new_r_Unknown(irg, mode);
60 }
61
62 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
63 {
64         const source_position_t *pos = (const source_position_t*) dbg;
65         if(pos == NULL)
66                 return 0;
67         return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
68                                    pos->linenr);
69 }
70
71 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
72 {
73         const source_position_t *pos = (const source_position_t*) dbg;
74         if(pos == NULL)
75                 return NULL;
76         if(line != NULL)
77                 *line = pos->linenr;
78         return pos->input_name;
79 }
80
81 void init_ast2firm(void)
82 {
83         type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
84         type_void       = make_atomic_type(ATOMIC_TYPE_VOID, 0);
85         type_int        = make_atomic_type(ATOMIC_TYPE_INT, 0);
86
87         ir_type_int        = get_ir_type(type_int);
88         ir_type_const_char = get_ir_type(type_const_char);
89         ir_type_void       = get_ir_type(type_int); /* we don't have a real void
90                                                        type in firm */
91         ir_type_void_ptr   = new_type_pointer(new_id_from_str("void_ptr"),
92                                               ir_type_void, mode_P_data);
93
94         type_void->firm_type = ir_type_void;
95 }
96
97 void exit_ast2firm(void)
98 {
99 }
100
101 static unsigned unique_id = 0;
102
103 static ident *unique_ident(const char *tag)
104 {
105         char buf[256];
106
107         snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
108         unique_id++;
109         return new_id_from_str(buf);
110 }
111
112 static type_t *skip_typeref(type_t *type)
113 {
114         while(1) {
115                 switch(type->type) {
116                 case TYPE_TYPEDEF: {
117                         const typedef_type_t *typedef_type = (const typedef_type_t*) type;
118                         type = typedef_type->declaration->type;
119                         continue;
120                 }
121                 case TYPE_TYPEOF: {
122                         const typeof_type_t *typeof_type = (const typeof_type_t *) type;
123                         if(typeof_type->typeof_type != NULL) {
124                                 type = typeof_type->typeof_type;
125                         } else {
126                                 type = typeof_type->expression->datatype;
127                         }
128                         continue;
129                 }
130                 default:
131                         break;
132                 }
133                 break;
134         }
135
136         return type;
137 }
138
139 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
140 {
141         switch(atomic_type->atype) {
142         case ATOMIC_TYPE_SCHAR:
143         case ATOMIC_TYPE_CHAR:
144                 return mode_Bs;
145         case ATOMIC_TYPE_UCHAR:
146                 return mode_Bu;
147         case ATOMIC_TYPE_SHORT:
148                 return mode_Hs;
149         case ATOMIC_TYPE_USHORT:
150                 return mode_Hu;
151         case ATOMIC_TYPE_LONG:
152         case ATOMIC_TYPE_INT:
153                 return mode_Is;
154         case ATOMIC_TYPE_ULONG:
155         case ATOMIC_TYPE_UINT:
156                 return mode_Iu;
157         case ATOMIC_TYPE_LONGLONG:
158                 return mode_Ls;
159         case ATOMIC_TYPE_ULONGLONG:
160                 return mode_Lu;
161         case ATOMIC_TYPE_FLOAT:
162                 return mode_F;
163         case ATOMIC_TYPE_DOUBLE:
164                 return mode_D;
165         case ATOMIC_TYPE_LONG_DOUBLE:
166                 return mode_E;
167         case ATOMIC_TYPE_BOOL:
168                 return mode_b;
169 #ifdef PROVIDE_COMPLEX
170         case ATOMIC_TYPE_FLOAT_COMPLEX:
171         case ATOMIC_TYPE_DOUBLE_COMPLEX:
172         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
173                 panic("complex lowering not implemented yet");
174                 break;
175         case ATOMIC_TYPE_FLOAT_IMAGINARY:
176         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
177         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
178                 panic("imaginary lowering not implemented yet");
179                 break;
180 #endif
181         case ATOMIC_TYPE_VOID:
182                 panic("tried to get mode from void type");
183                 break;
184         case ATOMIC_TYPE_INVALID:
185                 break;
186         }
187         panic("Encountered unknown atomic type");
188 }
189
190
191 static unsigned get_type_size(type_t *type);
192
193 static unsigned get_atomic_type_size(const atomic_type_t *type)
194 {
195         switch(type->atype) {
196         case ATOMIC_TYPE_CHAR:
197         case ATOMIC_TYPE_SCHAR:
198         case ATOMIC_TYPE_UCHAR:
199                 return 1;
200
201         case ATOMIC_TYPE_SHORT:
202         case ATOMIC_TYPE_USHORT:
203                 return 2;
204
205         case ATOMIC_TYPE_BOOL:
206         case ATOMIC_TYPE_INT:
207         case ATOMIC_TYPE_UINT:
208         case ATOMIC_TYPE_LONG:
209         case ATOMIC_TYPE_ULONG:
210         case ATOMIC_TYPE_FLOAT:
211                 return 4;
212
213         case ATOMIC_TYPE_LONGLONG:
214         case ATOMIC_TYPE_ULONGLONG:
215         case ATOMIC_TYPE_DOUBLE:
216                 return 8;
217
218         case ATOMIC_TYPE_LONG_DOUBLE:
219                 return 12;
220
221         case ATOMIC_TYPE_VOID:
222                 return 1;
223
224         case ATOMIC_TYPE_INVALID:
225                 break;
226         }
227         panic("Trying to determine size of invalid atomic type");
228 }
229
230 static unsigned get_compound_type_size(compound_type_t *type)
231 {
232         ir_type *irtype = get_ir_type(&type->type);
233         return get_type_size_bytes(irtype);
234 }
235
236 static unsigned get_array_type_size(array_type_t *type)
237 {
238         ir_type *irtype = get_ir_type(&type->type);
239         return get_type_size_bytes(irtype);
240 }
241
242 static unsigned get_type_size(type_t *type)
243 {
244         type = skip_typeref(type);
245
246         switch(type->type) {
247         case TYPE_ATOMIC:
248                 return get_atomic_type_size((const atomic_type_t*) type);
249         case TYPE_ENUM:
250                 return get_mode_size_bytes(mode_Is);
251         case TYPE_COMPOUND_UNION:
252         case TYPE_COMPOUND_STRUCT:
253                 return get_compound_type_size((compound_type_t*) type);
254         case TYPE_FUNCTION:
255                 /* just a pointer to the function */
256                 return get_mode_size_bytes(mode_P_code);
257         case TYPE_POINTER:
258                 return get_mode_size_bytes(mode_P_data);
259         case TYPE_ARRAY:
260                 return get_array_type_size((array_type_t*) type);
261         case TYPE_BUILTIN:
262         case TYPE_TYPEDEF:
263         case TYPE_TYPEOF:
264         case TYPE_INVALID:
265                 break;
266         }
267         panic("Trying to determine size of invalid type");
268 }
269
270 static unsigned count_parameters(const function_type_t *function_type)
271 {
272         unsigned count = 0;
273
274         function_parameter_t *parameter = function_type->parameters;
275         for ( ; parameter != NULL; parameter = parameter->next) {
276                 ++count;
277         }
278
279         return count;
280 }
281
282
283
284
285 static ir_type *create_atomic_type(type2firm_env_t *env,
286                                    const atomic_type_t *type)
287 {
288         (void) env;
289         ir_mode *mode   = get_atomic_mode(type);
290         ident   *id     = get_mode_ident(mode);
291         ir_type *irtype = new_type_primitive(id, mode);
292
293         return irtype;
294 }
295
296 static ir_type *create_method_type(type2firm_env_t *env,
297                                    const function_type_t *function_type)
298 {
299         type_t  *result_type  = function_type->result_type;
300
301         ident   *id           = unique_ident("functiontype");
302         int      n_parameters = count_parameters(function_type);
303         int      n_results    = result_type == type_void ? 0 : 1;
304         ir_type *irtype       = new_type_method(id, n_parameters, n_results);
305
306         if(result_type != type_void) {
307                 ir_type *restype = _get_ir_type(env, result_type);
308                 set_method_res_type(irtype, 0, restype);
309         }
310
311         function_parameter_t *parameter = function_type->parameters;
312         int                   n         = 0;
313         for( ; parameter != NULL; parameter = parameter->next) {
314                 ir_type *p_irtype = _get_ir_type(env, parameter->type);
315                 set_method_param_type(irtype, n, p_irtype);
316                 ++n;
317         }
318
319         if(function_type->variadic) {
320                 set_method_variadicity(irtype, variadicity_variadic);
321         }
322
323         return irtype;
324 }
325
326 static ir_type *create_pointer_type(type2firm_env_t *env, pointer_type_t *type)
327 {
328         type_t  *points_to = type->points_to;
329         ir_type *ir_points_to;
330         /* Avoid endless recursion if the points_to type contains this poiner type
331          * again (might be a struct). We therefore first create a void* pointer
332          * and then set the real points_to type
333          */
334         ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
335                                             ir_type_void, mode_P_data);
336         type->type.firm_type  = ir_type;
337
338         ir_points_to = _get_ir_type(env, points_to);
339         set_pointer_points_to_type(ir_type, ir_points_to);
340
341         return ir_type;
342 }
343
344 static ir_type *create_array_type(type2firm_env_t *env, array_type_t *type)
345 {
346         type_t  *element_type    = type->element_type;
347         ir_type *ir_element_type = _get_ir_type(env, element_type);
348
349         /* TODO... */
350         int n_elements = 0;
351         panic("TODO arraytpye size not implemented yet");
352
353         ir_type *ir_type = new_type_array(unique_ident("array"), 1, ir_element_type);
354         set_array_bounds_int(ir_type, 0, 0, n_elements);
355
356         size_t elemsize = get_type_size_bytes(ir_element_type);
357         int align = get_type_alignment_bytes(ir_element_type);
358         if(elemsize % align > 0) {
359                 elemsize += align - (elemsize % align);
360         }
361         set_type_size_bytes(ir_type, n_elements * elemsize);
362         set_type_alignment_bytes(ir_type, align);
363         set_type_state(ir_type, layout_fixed);
364
365         return ir_type;
366 }
367
368 #define INVALID_TYPE ((ir_type_ptr)-1)
369
370 static ir_type *create_struct_type(type2firm_env_t *env, compound_type_t *type)
371 {
372         symbol_t *symbol = type->declaration->symbol;
373         ident    *id;
374         if(symbol != NULL) {
375                 id = unique_ident(symbol->string);
376         } else {
377                 id = unique_ident("__anonymous_struct");
378         }
379         ir_type *ir_type = new_type_struct(id);
380
381         type->type.firm_type = ir_type;
382
383         int align_all = 1;
384         int offset    = 0;
385         declaration_t *entry = type->declaration->context.declarations;
386         for( ; entry != NULL; entry = entry->next) {
387                 ident       *ident         = new_id_from_str(entry->symbol->string);
388                 ir_type_ptr  entry_ir_type = _get_ir_type(env, entry->type);
389
390                 int entry_size      = get_type_size_bytes(entry_ir_type);
391                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
392                 int misalign = offset % entry_alignment;
393                 offset += misalign;
394
395                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
396                 set_entity_offset(entity, offset);
397                 add_struct_member(ir_type, entity);
398                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
399                 entry->v.entity         = entity;
400
401                 offset += entry_size;
402                 if(entry_alignment > align_all) {
403                         if(entry_alignment % align_all != 0) {
404                                 panic("Uneven alignments not supported yet");
405                         }
406                         align_all = entry_alignment;
407                 }
408         }
409
410         int misalign = offset % align_all;
411         offset += misalign;
412         set_type_alignment_bytes(ir_type, align_all);
413         set_type_size_bytes(ir_type, offset);
414         set_type_state(ir_type, layout_fixed);
415
416         return ir_type;
417 }
418
419 static ir_type *create_union_type(type2firm_env_t *env, compound_type_t *type)
420 {
421         declaration_t *declaration = type->declaration;
422         symbol_t      *symbol      = declaration->symbol;
423         ident         *id;
424         if(symbol != NULL) {
425                 id = unique_ident(symbol->string);
426         } else {
427                 id = unique_ident("__anonymous_union");
428         }
429         ir_type  *ir_type = new_type_union(id);
430
431         type->type.firm_type = ir_type;
432
433         int align_all = 1;
434         int size      = 0;
435         declaration_t *entry = declaration->context.declarations;
436         for( ; entry != NULL; entry = entry->next) {
437                 ident       *ident         = new_id_from_str(entry->symbol->string);
438                 ir_type_ptr  entry_ir_type = _get_ir_type(env, entry->type);
439
440                 int entry_size      = get_type_size_bytes(entry_ir_type);
441                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
442
443                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
444                 add_union_member(ir_type, entity);
445                 set_entity_offset(entity, 0);
446                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
447                 entry->v.entity         = entity;
448
449                 if(entry_size > size) {
450                         size = entry_size;
451                 }
452                 if(entry_alignment > align_all) {
453                         if(entry_alignment % align_all != 0) {
454                                 panic("Uneven alignments not supported yet");
455                         }
456                         align_all = entry_alignment;
457                 }
458         }
459
460         set_type_alignment_bytes(ir_type, align_all);
461         set_type_size_bytes(ir_type, size);
462         set_type_state(ir_type, layout_fixed);
463
464         return ir_type;
465 }
466
467 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type)
468 {
469         assert(type != NULL);
470
471         type = skip_typeref(type);
472
473         if(type->firm_type != NULL) {
474                 assert(type->firm_type != INVALID_TYPE);
475                 return type->firm_type;
476         }
477
478         ir_type *firm_type = NULL;
479         switch(type->type) {
480         case TYPE_ATOMIC:
481                 firm_type = create_atomic_type(env, (atomic_type_t*) type);
482                 break;
483         case TYPE_FUNCTION:
484                 firm_type = create_method_type(env, (function_type_t*) type);
485                 break;
486         case TYPE_POINTER:
487                 firm_type = create_pointer_type(env, (pointer_type_t*) type);
488                 break;
489         case TYPE_ARRAY:
490                 firm_type = create_array_type(env, (array_type_t*) type);
491                 break;
492         case TYPE_COMPOUND_STRUCT:
493                 firm_type = create_struct_type(env, (compound_type_t*) type);
494                 break;
495         case TYPE_COMPOUND_UNION:
496                 firm_type = create_union_type(env, (compound_type_t*) type);
497                 break;
498         case TYPE_ENUM:
499                 firm_type = ir_type_int;
500                 break;
501         case TYPE_BUILTIN:
502         case TYPE_TYPEOF:
503         case TYPE_TYPEDEF:
504         case TYPE_INVALID:
505                 break;
506         }
507         if(firm_type == NULL)
508                 panic("unknown type found");
509
510         if(env->can_cache) {
511                 type->firm_type = firm_type;
512         }
513         return firm_type;
514
515 }
516
517 static ir_type *get_ir_type(type_t *type)
518 {
519         type2firm_env_t env;
520         env.can_cache = 1;
521
522         return _get_ir_type(&env, type);
523 }
524
525 static inline ir_mode *get_ir_mode(type_t *type)
526 {
527         ir_type *irtype = get_ir_type(type);
528         ir_mode *mode   = get_type_mode(irtype);
529         assert(mode != NULL);
530         return mode;
531 }
532
533 static ir_entity* get_function_entity(declaration_t *declaration)
534 {
535         if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
536                 return declaration->v.entity;
537         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
538
539         symbol_t *symbol = declaration->symbol;
540         ident    *id     = new_id_from_str(symbol->string);
541
542         ir_type  *global_type    = get_glob_type();
543         ir_type  *ir_type_method = get_ir_type(declaration->type);
544         assert(is_Method_type(ir_type_method));
545
546         type_t    *type   = declaration->type;
547         ir_entity *entity = new_entity(global_type, id, ir_type_method);
548         set_entity_ld_ident(entity, id);
549         if(declaration->storage_class & STORAGE_CLASS_STATIC
550                         || type->qualifiers & TYPE_QUALIFIER_INLINE) {
551                 set_entity_visibility(entity, visibility_local);
552         } else if(declaration->init.statement != NULL) {
553                 set_entity_visibility(entity, visibility_external_visible);
554         } else {
555                 set_entity_visibility(entity, visibility_external_allocated);
556         }
557
558         declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
559         declaration->v.entity         = entity;
560
561         return entity;
562 }
563
564
565
566 static ir_node *expression_to_firm(const expression_t *expression);
567
568 static dbg_info *get_dbg_info(const source_position_t *pos)
569 {
570         return (dbg_info*) pos;
571 }
572
573 static ir_node *const_to_firm(const const_t *cnst)
574 {
575         dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
576         ir_mode  *mode = get_ir_mode(cnst->expression.datatype);
577
578         tarval   *tv;
579         if(mode_is_float(mode)) {
580                 tv = new_tarval_from_double(cnst->v.float_value, mode);
581         } else {
582                 tv = new_tarval_from_long(cnst->v.int_value, mode);
583         }
584
585         return new_d_Const(dbgi, mode, tv);
586 }
587
588 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
589 {
590         assert(entity != NULL);
591         union symconst_symbol sym;
592         sym.entity_p = entity;
593         return new_d_SymConst(dbgi, sym, symconst_addr_ent);
594 }
595
596 static ir_node *string_literal_to_firm(const string_literal_t* literal)
597 {
598         ir_type   *global_type = get_glob_type();
599         ir_type   *type        = new_type_array(unique_ident("strtype"), 1,
600                                                 ir_type_const_char);
601
602         ident     *id     = unique_ident("Lstr");
603         ir_entity *entity = new_entity(global_type, id, type);
604         set_entity_ld_ident(entity, id);
605         set_entity_variability(entity, variability_constant);
606
607         ir_type    *elem_type = ir_type_const_char;
608         ir_mode    *mode      = get_type_mode(elem_type);
609
610         const char *string = literal->value;
611         size_t      slen   = strlen(string) + 1;
612
613         set_array_lower_bound_int(type, 0, 0);
614         set_array_upper_bound_int(type, 0, slen);
615         set_type_size_bytes(type, slen);
616         set_type_state(type, layout_fixed);
617
618         tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
619         for(size_t i = 0; i < slen; ++i) {
620                 tvs[i] = new_tarval_from_long(string[i], mode);
621         }
622
623         set_array_entity_values(entity, tvs, slen);
624         free(tvs);
625
626         dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
627
628         return create_symconst(dbgi, entity);
629 }
630
631 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
632 {
633         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
634         declaration_t *declaration = ref->declaration;
635         type_t        *type        = declaration->type;
636         ir_mode       *mode        = get_ir_mode(type);
637
638         switch((declaration_type_t) declaration->declaration_type) {
639         case DECLARATION_TYPE_UNKNOWN:
640                 break;
641         case DECLARATION_TYPE_LOCAL_VARIABLE:
642                 return get_value(declaration->v.value_number, mode);
643         case DECLARATION_TYPE_FUNCTION: {
644                 return create_symconst(dbgi, declaration->v.entity);
645         }
646         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
647         case DECLARATION_TYPE_GLOBAL_VARIABLE:
648         case DECLARATION_TYPE_COMPOUND_MEMBER:
649                 panic("not implemented reference type");
650         }
651
652         panic("reference to declaration with unknown type found");
653 }
654
655 static ir_node *call_expression_to_firm(const call_expression_t *call)
656 {
657         expression_t  *function = call->function;
658         ir_node       *callee   = expression_to_firm(function);
659
660         assert(function->datatype->type == TYPE_FUNCTION);
661         function_type_t *function_type = (function_type_t*) function->datatype;
662
663         int              n_parameters = 0;
664         call_argument_t *argument     = call->arguments;
665         for( ; argument != NULL; argument = argument->next) {
666                 ++n_parameters;
667         }
668
669         ir_type *ir_method_type  = get_ir_type((type_t*) function_type);
670         ir_type *new_method_type = NULL;
671         if(function_type->variadic) {
672                 /* we need to construct a new method type matching the call
673                  * arguments... */
674                 int n_res       = get_method_n_ress(ir_method_type);
675                 new_method_type = new_type_method(unique_ident("calltype"),
676                                                   n_parameters, n_res);
677                 set_method_calling_convention(new_method_type,
678                                get_method_calling_convention(ir_method_type));
679                 set_method_additional_properties(new_method_type,
680                                get_method_additional_properties(ir_method_type));
681
682                 for(int i = 0; i < n_res; ++i) {
683                         set_method_res_type(new_method_type, i,
684                                             get_method_res_type(ir_method_type, i));
685                 }
686         }
687         ir_node *in[n_parameters];
688
689         argument = call->arguments;
690         int n = 0;
691         for( ; argument != NULL; argument = argument->next) {
692                 expression_t *expression = argument->expression;
693                 ir_node      *arg_node   = expression_to_firm(expression);
694
695                 in[n] = arg_node;
696                 if(new_method_type != NULL) {
697                         ir_type *irtype = get_ir_type(expression->datatype);
698                         set_method_param_type(new_method_type, n, irtype);
699                 }
700
701                 n++;
702         }
703         assert(n == n_parameters);
704
705         if(new_method_type != NULL)
706                 ir_method_type = new_method_type;
707
708         dbg_info *dbgi  = get_dbg_info(&call->expression.source_position);
709         ir_node  *store = get_store();
710         ir_node  *node  = new_d_Call(dbgi, store, callee, n_parameters, in,
711                                      ir_method_type);
712         ir_node  *mem   = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
713         set_store(mem);
714
715         type_t  *result_type = function_type->result_type;
716         ir_node *result      = NULL;
717         if(result_type != type_void) {
718                 ir_mode *mode    = get_ir_mode(result_type);
719                 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
720                 result           = new_d_Proj(dbgi, resproj, mode, 0);
721         }
722
723         return result;
724 }
725
726 static ir_node *load_from_expression_addr(type_t *type, ir_node *addr,
727                                           dbg_info *dbgi)
728 {
729         ir_mode *mode     = get_ir_mode(type);
730         ir_node *memory   = get_store();
731         ir_node *load     = new_d_Load(dbgi, memory, addr, mode);
732         ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
733         ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
734         set_store(load_mem);
735
736         return load_res;
737 }
738
739 static ir_node *expression_addr(const expression_t *expression)
740 {
741         (void) expression;
742         panic("expression_addr not implemented yet");
743         return NULL;
744 }
745
746 static void set_value_for_expression(const expression_t *expression,
747                                      ir_node *value)
748 {
749         if(expression->type == EXPR_REFERENCE) {
750                 reference_expression_t *ref = (reference_expression_t*) expression;
751
752                 declaration_t *declaration = ref->declaration;
753                 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
754                 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
755                         set_value(declaration->v.value_number, value);
756                         return;
757                 }
758         }
759         panic("set_value_for_expression not implemented yet");
760 }
761
762 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
763 {
764         ir_mode *value_mode = get_irn_mode(value);
765
766         if(value_mode == dest_mode)
767                 return value;
768
769         if(dest_mode == mode_b) {
770                 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
771                 ir_node *cmp  = new_d_Cmp(dbgi, value, zero);
772                 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
773                 return proj;
774         }
775
776         return new_d_Conv(dbgi, value, dest_mode);
777 }
778
779 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
780 {
781         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
782         type_t   *type = expression->expression.datatype;
783         ir_mode  *mode = get_ir_mode(type);
784
785         if(expression->type == UNEXPR_TAKE_ADDRESS)
786                 return expression_addr(expression->value);
787
788         const expression_t *value      = expression->value;
789         ir_node            *value_node = expression_to_firm(value);
790
791         switch(expression->type) {
792         case UNEXPR_NEGATE:
793                 return new_d_Minus(dbgi, value_node, mode);
794         case UNEXPR_PLUS:
795                 return value_node;
796         case UNEXPR_BITWISE_NEGATE:
797                 return new_d_Not(dbgi, value_node, mode);
798         case UNEXPR_NOT:
799                 if(get_irn_mode(value_node) != mode_b) {
800                         value_node = create_conv(dbgi, value_node, mode_b);
801                 }
802                 value_node = new_d_Not(dbgi, value_node, mode_b);
803                 if(mode != mode_b) {
804                         value_node = create_conv(dbgi, value_node, mode);
805                 }
806                 return value_node;
807         case UNEXPR_DEREFERENCE:
808                 return load_from_expression_addr(type, value_node, dbgi);
809         case UNEXPR_POSTFIX_INCREMENT: {
810                 ir_node *one       = new_Const(mode, get_mode_one(mode));
811                 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
812                 set_value_for_expression(value, new_value);
813                 return value_node;
814         }
815         case UNEXPR_POSTFIX_DECREMENT: {
816                 ir_node *one       = new_Const(mode, get_mode_one(mode));
817                 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
818                 set_value_for_expression(value, new_value);
819                 return value_node;
820         }
821         case UNEXPR_PREFIX_INCREMENT: {
822                 ir_node *one       = new_Const(mode, get_mode_one(mode));
823                 ir_node *new_value = new_d_Add(dbgi, value_node, one, mode);
824                 set_value_for_expression(value, new_value);
825                 return new_value;
826         }
827         case UNEXPR_PREFIX_DECREMENT: {
828                 ir_node *one       = new_Const(mode, get_mode_one(mode));
829                 ir_node *new_value = new_d_Sub(dbgi, value_node, one, mode);
830                 set_value_for_expression(value, new_value);
831                 return new_value;
832         }
833         case UNEXPR_CAST:
834                 return create_conv(dbgi, value_node, mode);
835
836         case UNEXPR_TAKE_ADDRESS:
837         case UNEXPR_INVALID:
838                 break;
839         }
840         panic("invalid UNEXPR type found");
841 }
842
843 static long get_pnc(binary_expression_type_t type)
844 {
845         switch(type) {
846         case BINEXPR_EQUAL:        return pn_Cmp_Eq;
847         case BINEXPR_NOTEQUAL:     return pn_Cmp_Lg;
848         case BINEXPR_LESS:         return pn_Cmp_Lt;
849         case BINEXPR_LESSEQUAL:    return pn_Cmp_Le;
850         case BINEXPR_GREATER:      return pn_Cmp_Gt;
851         case BINEXPR_GREATEREQUAL: return pn_Cmp_Ge;
852         default:
853                 break;
854         }
855         panic("trying to get pn_Cmp from non-comparison binexpr type");
856 }
857
858 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
859 {
860         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
861
862         binary_expression_type_t type = expression->type;
863         switch(type) {
864         case BINEXPR_EQUAL:
865         case BINEXPR_NOTEQUAL:
866         case BINEXPR_LESS:
867         case BINEXPR_LESSEQUAL:
868         case BINEXPR_GREATER:
869         case BINEXPR_GREATEREQUAL: {
870                 ir_node *left  = expression_to_firm(expression->left);
871                 ir_node *right = expression_to_firm(expression->right);
872                 ir_node *cmp   = new_d_Cmp(dbgi, left, right);
873                 long     pnc   = get_pnc(type);
874                 ir_node *proj  = new_d_Proj(dbgi, cmp, mode_b, pnc);
875                 return proj;
876         }
877         case BINEXPR_ASSIGN: {
878                 ir_node *right = expression_to_firm(expression->right);
879                 set_value_for_expression(expression->left, right);
880                 return right;
881         }
882         default:
883                 panic("TODO binexpr type");
884         }
885 }
886
887 static ir_node *expression_to_firm(const expression_t *expression)
888 {
889         switch(expression->type) {
890         case EXPR_CONST:
891                 return const_to_firm((const const_t*) expression);
892         case EXPR_STRING_LITERAL:
893                 return string_literal_to_firm((const string_literal_t*) expression);
894         case EXPR_REFERENCE:
895                 return reference_expression_to_firm(
896                                 (const reference_expression_t*) expression);
897         case EXPR_CALL:
898                 return call_expression_to_firm((const call_expression_t*) expression);
899         case EXPR_UNARY:
900                 return unary_expression_to_firm((const unary_expression_t*) expression);
901         case EXPR_BINARY:
902                 return binary_expression_to_firm(
903                                 (const binary_expression_t*) expression);
904         default:
905                 break;
906         }
907         panic("unsupported expression found");
908 }
909
910
911
912 static void statement_to_firm(statement_t *statement);
913
914 static void return_statement_to_firm(return_statement_t *statement)
915 {
916         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
917         ir_node  *ret;
918
919         if(statement->return_value != NULL) {
920                 ir_node *retval = expression_to_firm(statement->return_value);
921                 ir_node *in[1];
922
923                 in[0] = retval;
924                 ret   = new_d_Return(dbgi, get_store(), 1, in);
925         } else {
926                 ret   = new_d_Return(dbgi, get_store(), 0, NULL);
927         }
928         ir_node *end_block = get_irg_end_block(current_ir_graph);
929         add_immBlock_pred(end_block, ret);
930
931         set_cur_block(NULL);
932 }
933
934 static void compound_statement_to_firm(compound_statement_t *compound)
935 {
936         statement_t *statement = compound->statements;
937         for( ; statement != NULL; statement = statement->next) {
938                 //context2firm(&statement->context);
939                 statement_to_firm(statement);
940         }
941 }
942
943 static void expression_statement_to_firm(expression_statement_t *statement)
944 {
945         expression_to_firm(statement->expression);
946 }
947
948 static void if_statement_to_firm(if_statement_t *statement)
949 {
950         dbg_info *dbgi      = get_dbg_info(&statement->statement.source_position);
951         ir_node  *condition = expression_to_firm(statement->condition);
952         assert(condition != NULL);
953
954         /* make sure we have a mode_b condition */
955         condition = create_conv(dbgi, condition, mode_b);
956
957         ir_node *cond       = new_d_Cond(dbgi, condition);
958         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
959         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
960
961         ir_node *fallthrough_block = new_immBlock();
962
963         /* the true (blocks) */
964         ir_node *true_block = new_immBlock();
965         add_immBlock_pred(true_block, true_proj);
966         mature_immBlock(true_block);
967
968         statement_to_firm(statement->true_statement);
969         if(get_cur_block() != NULL) {
970                 ir_node *jmp = new_Jmp();
971                 add_immBlock_pred(fallthrough_block, jmp);
972         }
973
974         /* the false (blocks) */
975         if(statement->false_statement != NULL) {
976                 ir_node *false_block = new_immBlock();
977                 add_immBlock_pred(false_block, false_proj);
978                 mature_immBlock(false_block);
979
980                 statement_to_firm(statement->false_statement);
981                 if(get_cur_block() != NULL) {
982                         ir_node *jmp = new_Jmp();
983                         add_immBlock_pred(fallthrough_block, jmp);
984                 }
985         } else {
986                 add_immBlock_pred(fallthrough_block, false_proj);
987         }
988         mature_immBlock(fallthrough_block);
989
990         set_cur_block(fallthrough_block);
991 }
992
993 static void while_statement_to_firm(while_statement_t *statement)
994 {
995         dbg_info *dbgi      = get_dbg_info(&statement->statement.source_position);
996
997         /* create the header block */
998         ir_node *jmp          = new_Jmp();
999         ir_node *header_block = new_immBlock();
1000         add_immBlock_pred(header, jmp);
1001
1002         /* create the condition */
1003         ir_node  *condition = expression_to_firm(statement->condition);
1004         assert(condition != NULL);
1005         /* make sure we have a mode_b condition */
1006         condition = create_conv(dbgi, condition, mode_b);
1007
1008         ir_node *cond       = new_d_Cond(dbgi, condition);
1009         ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
1010         ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
1011
1012         /* the loop body */
1013         ir_node *body_block = new_immBlock();
1014         add_immBlock_pred(body_block, true_proj);
1015         mature_immBlock(true_block);
1016
1017         statement_to_firm(statement->true_statement);
1018         if(get_cur_block() != NULL) {
1019                 ir_node *jmp = new_Jmp();
1020                 add_immBlock_pred(fallthrough_block, jmp);
1021         }
1022
1023         /* the false (blocks) */
1024         if(statement->false_statement != NULL) {
1025                 ir_node *false_block = new_immBlock();
1026                 add_immBlock_pred(false_block, false_proj);
1027                 mature_immBlock(false_block);
1028
1029                 statement_to_firm(statement->false_statement);
1030                 if(get_cur_block() != NULL) {
1031                         ir_node *jmp = new_Jmp();
1032                         add_immBlock_pred(fallthrough_block, jmp);
1033                 }
1034         } else {
1035                 add_immBlock_pred(fallthrough_block, false_proj);
1036         }
1037         mature_immBlock(fallthrough_block);
1038
1039         set_cur_block(fallthrough_block);
1040 }
1041
1042 static void create_declaration_entity(declaration_t *declaration,
1043                                       declaration_type_t declaration_type,
1044                                       ir_type *parent_type)
1045 {
1046         ident     *id     = new_id_from_str(declaration->symbol->string);
1047         ir_type   *irtype = get_ir_type(declaration->type);
1048         ir_entity *entity = new_entity(parent_type, id, irtype);
1049         set_entity_ld_ident(entity, id);
1050
1051         declaration->declaration_type = declaration_type;
1052         declaration->v.entity         = entity;
1053         set_entity_variability(entity, variability_uninitialized);
1054         /* TODO: visibility? */
1055 }
1056
1057 static void create_initializer(declaration_t *declaration)
1058 {
1059         initializer_t *initializer = declaration->init.initializer;
1060         if(initializer == NULL)
1061                 return;
1062
1063         if(initializer->type == INITIALIZER_VALUE) {
1064                 assert(initializer->designator == NULL);
1065                 assert(initializer->next == NULL);
1066                 ir_node *init_node = expression_to_firm(initializer->v.value);
1067
1068                 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1069                         set_value(declaration->v.value_number, init_node);
1070                 } else {
1071                         panic("initializer not completely implemented yet");
1072                 }
1073         } else {
1074                 assert(initializer->type == INITIALIZER_LIST);
1075                 panic("list initializer not supported yet");
1076         }
1077 }
1078
1079 static void create_local_variable(declaration_t *declaration)
1080 {
1081         bool needs_entity = declaration->address_taken;
1082
1083         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
1084
1085         if(needs_entity) {
1086                 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
1087                 create_declaration_entity(declaration,
1088                                           DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
1089                                           frame_type);
1090         } else {
1091                 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1092                 declaration->v.value_number   = next_value_number_function;
1093                 ++next_value_number_function;
1094         }
1095
1096         create_initializer(declaration);
1097 }
1098
1099 static void declaration_statement_to_firm(declaration_statement_t *statement)
1100 {
1101         declaration_t *declaration = statement->declarations_begin;
1102         declaration_t *end         = statement->declarations_end->next;
1103         for( ; declaration != end; declaration = declaration->next) {
1104                 type_t *type = declaration->type;
1105
1106                 switch(declaration->storage_class) {
1107                 case STORAGE_CLASS_TYPEDEF:
1108                         continue;
1109                 case STORAGE_CLASS_STATIC:
1110                         panic("static local vars not implemented yet");
1111                 case STORAGE_CLASS_ENUM_ENTRY:
1112                         panic("enum entry declaration in local block found");
1113                 case STORAGE_CLASS_EXTERN:
1114                         panic("extern declaration in local block found");
1115                 case STORAGE_CLASS_NONE:
1116                 case STORAGE_CLASS_AUTO:
1117                 case STORAGE_CLASS_REGISTER:
1118                         if(type->type == TYPE_FUNCTION) {
1119                                 panic("nested functions not supported yet");
1120                         } else {
1121                                 create_local_variable(declaration);
1122                         }
1123                         continue;
1124                 }
1125                 panic("invalid storage class found");
1126         }
1127 }
1128
1129 static void statement_to_firm(statement_t *statement)
1130 {
1131         switch(statement->type) {
1132         case STATEMENT_COMPOUND:
1133                 compound_statement_to_firm((compound_statement_t*) statement);
1134                 return;
1135         case STATEMENT_RETURN:
1136                 return_statement_to_firm((return_statement_t*) statement);
1137                 return;
1138         case STATEMENT_EXPRESSION:
1139                 expression_statement_to_firm((expression_statement_t*) statement);
1140                 return;
1141         case STATEMENT_IF:
1142                 if_statement_to_firm((if_statement_t*) statement);
1143                 return;
1144         case STATEMENT_DECLARATION:
1145                 declaration_statement_to_firm((declaration_statement_t*) statement);
1146                 return;
1147         default:
1148                 break;
1149         }
1150         panic("Statement not implemented\n");
1151 }
1152
1153 static int get_function_n_local_vars(declaration_t *declaration)
1154 {
1155         (void) declaration;
1156         /* TODO */
1157         return 30;
1158 }
1159
1160 static void initialize_function_parameters(declaration_t *declaration)
1161 {
1162         ir_graph *irg         = current_ir_graph;
1163         ir_node  *args        = get_irg_args(irg);
1164         ir_node  *start_block = get_irg_start_block(irg);
1165
1166         int            n         = 0;
1167         declaration_t *parameter = declaration->context.declarations;
1168         for( ; parameter != NULL; parameter = parameter->next) {
1169                 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
1170
1171                 if(parameter->address_taken) {
1172                         panic("address take from parameter not implemented yet");
1173                 }
1174
1175                 ir_mode *mode = get_ir_mode(parameter->type);
1176                 long     pn   = n;
1177                 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
1178                 ++n;
1179
1180                 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
1181                 parameter->v.value_number   = next_value_number_function;
1182                 ++next_value_number_function;
1183
1184                 set_value(parameter->v.value_number, proj);
1185         }
1186 }
1187
1188 static void create_function(declaration_t *declaration)
1189 {
1190         ir_entity *entity = get_function_entity(declaration);
1191
1192         if(declaration->init.statement == NULL)
1193                 return;
1194
1195         int        n_local_vars = get_function_n_local_vars(declaration);
1196         ir_graph  *irg          = new_ir_graph(entity, n_local_vars);
1197         ir_node   *first_block  = get_cur_block();
1198
1199         next_value_number_function = 0;
1200         initialize_function_parameters(declaration);
1201
1202         statement_to_firm(declaration->init.statement);
1203
1204         ir_node *end_block = get_irg_end_block(irg);
1205
1206         /* do we have a return statement yet? */
1207         if(get_cur_block() != NULL) {
1208                 ir_node *ret = new_Return(get_store(), 0, NULL);
1209                 add_immBlock_pred(end_block, ret);
1210         }
1211
1212         mature_immBlock(first_block);
1213         mature_immBlock(end_block);
1214
1215         irg_finalize_cons(irg);
1216
1217         /* finalize the frame type */
1218         ir_type *frame_type = get_irg_frame_type(irg);
1219         int      n          = get_compound_n_members(frame_type);
1220         int      align_all  = 4;
1221         int      offset     = 0;
1222         for(int i = 0; i < n; ++i) {
1223                 ir_entity *entity      = get_compound_member(frame_type, i);
1224                 ir_type   *entity_type = get_entity_type(entity);
1225
1226                 int align = get_type_alignment_bytes(entity_type);
1227                 if(align > align_all)
1228                         align_all = align;
1229                 int misalign = 0;
1230                 if(align > 0) {
1231                         misalign  = offset % align;
1232                         offset   += misalign;
1233                 }
1234
1235                 set_entity_offset(entity, offset);
1236                 offset += get_type_size_bytes(entity_type);
1237         }
1238         set_type_size_bytes(frame_type, offset);
1239         set_type_alignment_bytes(frame_type, align_all);
1240         set_type_state(frame_type, layout_fixed);
1241
1242         irg_vrfy(irg);
1243 }
1244
1245 static void context_to_firm(context_t *context)
1246 {
1247         declaration_t *declaration = context->declarations;
1248         for( ; declaration != NULL; declaration = declaration->next) {
1249                 type_t *type = declaration->type;
1250                 if(type->type == TYPE_FUNCTION) {
1251                         create_function(declaration);
1252                 } else {
1253                         /* TODO... */
1254                 }
1255         }
1256 }
1257
1258 void translation_unit_to_firm(translation_unit_t *unit)
1259 {
1260         /* remove me later TODO FIXME */
1261         (void) get_type_size;
1262
1263         context_to_firm(& unit->context);
1264 }