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