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