Add options:
[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 "adt/array.h"
16 #include "token_t.h"
17 #include "type_t.h"
18 #include "ast_t.h"
19 #include "parser.h"
20 #include "lang_features.h"
21
22 #define MAGIC_DEFAULT_PN_NUMBER     (long) -314159265
23
24 static ir_type *ir_type_const_char;
25 static ir_type *ir_type_wchar_t;
26 static ir_type *ir_type_void;
27 static ir_type *ir_type_int;
28
29 static type_t *type_const_char;
30 static type_t *type_void;
31 static type_t *type_int;
32
33 static int       next_value_number_function;
34 static ir_node  *continue_label;
35 static ir_node  *break_label;
36 static ir_node  *current_switch_cond;
37 static bool      saw_default_label;
38 static ir_node **imature_blocks;
39
40 static const declaration_t *current_function_decl;
41 static ir_node             *current_function_name;
42
43 static struct obstack asm_obst;
44
45 typedef enum declaration_type_t {
46         DECLARATION_TYPE_UNKNOWN,
47         DECLARATION_TYPE_FUNCTION,
48         DECLARATION_TYPE_GLOBAL_VARIABLE,
49         DECLARATION_TYPE_LOCAL_VARIABLE,
50         DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
51         DECLARATION_TYPE_COMPOUND_MEMBER,
52         DECLARATION_TYPE_LABEL_BLOCK,
53         DECLARATION_TYPE_ENUM_ENTRY
54 } declaration_type_t;
55
56 static ir_type *get_ir_type(type_t *type);
57 static int count_decls_in_stmts(const statement_t *stmt);
58
59 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
60 {
61         (void) pos;
62 #if 0
63         const declaration_t *declaration = & value_numbers[pos]->declaration;
64
65         print_warning_prefix(declaration->source_position);
66         fprintf(stderr, "variable '%s' might be used uninitialized\n",
67                         declaration->symbol->string);
68 #endif
69         fprintf(stderr, "Some variable might be used uninitialized\n");
70         return new_r_Unknown(irg, mode);
71 }
72
73 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
74 {
75         const source_position_t *pos = (const source_position_t*) dbg;
76         if(pos == NULL)
77                 return 0;
78         return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
79                                    pos->linenr);
80 }
81
82 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
83 {
84         const source_position_t *pos = (const source_position_t*) dbg;
85         if(pos == NULL)
86                 return NULL;
87         if(line != NULL)
88                 *line = pos->linenr;
89         return pos->input_name;
90 }
91
92 static dbg_info *get_dbg_info(const source_position_t *pos)
93 {
94         return (dbg_info*) pos;
95 }
96
97 static unsigned unique_id = 0;
98
99 static ident *unique_ident(const char *tag)
100 {
101         char buf[256];
102
103         snprintf(buf, sizeof(buf), "%s.%u", tag, unique_id);
104         unique_id++;
105         return new_id_from_str(buf);
106 }
107
108 /**
109  * Return the signed integer mode of size bytes.
110  *
111  * @param size   the size
112  */
113 static ir_mode *get_smode(unsigned size)
114 {
115         static ir_mode *s_modes[16 + 1] = {0, };
116         ir_mode *res;
117
118         if (size <= 0 || size > 16)
119                 return NULL;
120
121         res = s_modes[size];
122         if (res == NULL) {
123                 unsigned bits;
124         char name[32];
125
126         bits = size * 8;
127         snprintf(name, sizeof(name), "i%u", bits);
128         res = new_ir_mode(name, irms_int_number, bits, 1, irma_twos_complement,
129                                         bits <= machine_size ? machine_size : bits );
130
131                 s_modes[size] = res;
132         }
133         return res;
134 }
135
136 /**
137  * Return the unsigned integer mode of size bytes.
138  *
139  * @param size  the size
140  */
141 static ir_mode *get_umode(unsigned size)
142 {
143         static ir_mode *u_modes[16 + 1] = {0, };
144         ir_mode *res;
145
146         if (size <= 0 || size > 16)
147                 return NULL;
148
149         res = u_modes[size];
150         if (res == NULL) {
151                 unsigned bits;
152                 char name[32];
153
154                 bits = size * 8;
155                 snprintf(name, sizeof(name), "u%u", bits);
156                 res = new_ir_mode(name, irms_int_number, bits, 0, irma_twos_complement,
157                                                 bits <= machine_size ? machine_size : bits );
158
159                 u_modes[size] = res;
160         }
161         return res;
162 }
163
164 /**
165  * Return the pointer mode of size bytes.
166  *
167  * @param size  the size
168  */
169 static ir_mode *get_ptrmode(unsigned size, char *name)
170 {
171         static ir_mode *p_modes[16 + 1] = {0, };
172         ir_mode *res;
173
174         if (size <= 0 || size > 16)
175                 return NULL;
176
177         res = p_modes[size];
178         if (res == NULL) {
179                 unsigned bits;
180                 char buf[32];
181
182                 bits = size * 8;
183                 if (name == NULL) {
184                         snprintf(buf, sizeof(buf), "p%u", bits);
185                         name = buf;
186                 }
187                 res = new_ir_mode(name, irms_reference, bits, 0, irma_twos_complement,
188                                                 bits <= machine_size ? machine_size : bits);
189
190                 p_modes[size] = res;
191
192                 set_reference_mode_signed_eq(res, get_smode(size));
193                 set_reference_mode_unsigned_eq(res, get_umode(size));
194         }
195         return res;
196 }
197
198 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST];
199
200 static ir_mode *mode_int, *mode_uint;
201
202 /**
203  * Initialises the atomic modes depending on the machine size.
204  */
205 static void init_atomic_modes(void) {
206         unsigned int_size   = machine_size < 32 ? 2 : 4;
207         unsigned long_size  = machine_size < 64 ? 4 : 8;
208         unsigned llong_size = machine_size < 32 ? 4 : 8;
209
210         /* firm has no real void... */
211         _atomic_modes[ATOMIC_TYPE_VOID]        = mode_T;
212         _atomic_modes[ATOMIC_TYPE_CHAR]        = char_is_signed ? get_smode(1) : get_umode(1);
213         _atomic_modes[ATOMIC_TYPE_SCHAR]       = get_smode(1);
214         _atomic_modes[ATOMIC_TYPE_UCHAR]       = get_umode(1);
215         _atomic_modes[ATOMIC_TYPE_SHORT]       = get_smode(2);
216         _atomic_modes[ATOMIC_TYPE_USHORT]      = get_umode(2);
217         _atomic_modes[ATOMIC_TYPE_INT]         = get_smode(int_size);
218         _atomic_modes[ATOMIC_TYPE_UINT]        = get_umode(int_size);
219         _atomic_modes[ATOMIC_TYPE_LONG]        = get_smode(long_size);
220         _atomic_modes[ATOMIC_TYPE_ULONG]       = get_umode(long_size);
221         _atomic_modes[ATOMIC_TYPE_LONGLONG]    = get_smode(llong_size);
222         _atomic_modes[ATOMIC_TYPE_ULONGLONG]   = get_umode(llong_size);
223         _atomic_modes[ATOMIC_TYPE_FLOAT]       = mode_F;
224         _atomic_modes[ATOMIC_TYPE_DOUBLE]      = mode_D;
225         _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = mode_E;
226
227 #ifdef PROVIDE_COMPLEX
228         _atomic_modes[ATOMIC_TYPE_BOOL]                  = _atomic_modes[ATOMIC_TYPE_INT];
229         _atomic_modes[ATOMIC_TYPE_FLOAT_IMAGINARY]       = _atomic_modes[ATOMIC_TYPE_FLOAT];
230         _atomic_modes[ATOMIC_TYPE_DOUBLE_IMAGINARY]      = _atomic_modes[ATOMIC_TYPE_DOUBLE];
231         _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY] = _atomic_modes[ATOMIC_TYPE_LONG_DOUBLE];
232 #endif
233
234         /* Hmm, pointers should be machine size */
235         set_modeP_data(get_ptrmode(machine_size >> 3, NULL));
236         set_modeP_code(get_ptrmode(machine_size >> 3, NULL));
237
238         mode_int  = _atomic_modes[ATOMIC_TYPE_INT];
239         mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
240 }
241
242 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
243 {
244         ir_mode *res = NULL;
245         if ((unsigned)atomic_type->atype < (unsigned)ATOMIC_TYPE_LAST)
246                 res = _atomic_modes[(unsigned)atomic_type->atype];
247         if (res == NULL)
248                 panic("Encountered unknown atomic type");
249         return res;
250 }
251
252 static unsigned get_type_size(type_t *type);
253
254 static unsigned get_atomic_type_size(const atomic_type_t *type)
255 {
256         switch(type->atype) {
257         case ATOMIC_TYPE_CHAR:
258         case ATOMIC_TYPE_SCHAR:
259         case ATOMIC_TYPE_UCHAR:
260                 return 1;
261
262         case ATOMIC_TYPE_SHORT:
263         case ATOMIC_TYPE_USHORT:
264                 return 2;
265
266         case ATOMIC_TYPE_BOOL:
267         case ATOMIC_TYPE_INT:
268         case ATOMIC_TYPE_UINT:
269                 return machine_size >> 3;
270
271         case ATOMIC_TYPE_LONG:
272         case ATOMIC_TYPE_ULONG:
273                 return machine_size > 16 ? machine_size >> 3 : 4;
274
275         case ATOMIC_TYPE_LONGLONG:
276         case ATOMIC_TYPE_ULONGLONG:
277                 return machine_size > 16 ? 8 : 4;
278
279         case ATOMIC_TYPE_FLOAT:
280                 return 4;
281
282         case ATOMIC_TYPE_DOUBLE:
283                 return 8;
284
285         case ATOMIC_TYPE_LONG_DOUBLE:
286                 return 12;
287
288         case ATOMIC_TYPE_VOID:
289                 return 1;
290
291         case ATOMIC_TYPE_INVALID:
292         case ATOMIC_TYPE_LAST:
293                 break;
294         }
295         panic("Trying to determine size of invalid atomic type");
296 }
297
298 static unsigned get_compound_type_size(compound_type_t *type)
299 {
300         ir_type *irtype = get_ir_type((type_t*) type);
301         return get_type_size_bytes(irtype);
302 }
303
304 static unsigned get_array_type_size(array_type_t *type)
305 {
306         ir_type *irtype = get_ir_type((type_t*) type);
307         return get_type_size_bytes(irtype);
308 }
309
310
311 static unsigned get_type_size(type_t *type)
312 {
313         type = skip_typeref(type);
314
315         switch(type->type) {
316         case TYPE_ATOMIC:
317                 return get_atomic_type_size(&type->atomic);
318         case TYPE_ENUM:
319                 return get_mode_size_bytes(mode_int);
320         case TYPE_COMPOUND_UNION:
321         case TYPE_COMPOUND_STRUCT:
322                 return get_compound_type_size(&type->compound);
323         case TYPE_FUNCTION:
324                 /* just a pointer to the function */
325                 return get_mode_size_bytes(mode_P_code);
326         case TYPE_POINTER:
327                 return get_mode_size_bytes(mode_P_data);
328         case TYPE_ARRAY:
329                 return get_array_type_size(&type->array);
330         case TYPE_BUILTIN:
331                 return get_type_size(type->builtin.real_type);
332         case TYPE_TYPEDEF:
333         case TYPE_TYPEOF:
334         case TYPE_INVALID:
335                 break;
336         }
337         panic("Trying to determine size of invalid type");
338 }
339
340 static unsigned count_parameters(const function_type_t *function_type)
341 {
342         unsigned count = 0;
343
344         function_parameter_t *parameter = function_type->parameters;
345         for ( ; parameter != NULL; parameter = parameter->next) {
346                 ++count;
347         }
348
349         return count;
350 }
351
352
353
354
355 static long fold_constant(const expression_t *expression);
356
357 static ir_type *create_atomic_type(const atomic_type_t *type)
358 {
359         ir_mode *mode   = get_atomic_mode(type);
360         ident   *id     = get_mode_ident(mode);
361         ir_type *irtype = new_type_primitive(id, mode);
362
363         if(type->atype == ATOMIC_TYPE_LONG_DOUBLE) {
364                 set_type_alignment_bytes(irtype, 4);
365         }
366
367         return irtype;
368 }
369
370 static ir_type *create_method_type(const function_type_t *function_type)
371 {
372         type_t  *return_type  = function_type->return_type;
373
374         ident   *id           = unique_ident("functiontype");
375         int      n_parameters = count_parameters(function_type);
376         int      n_results    = return_type == type_void ? 0 : 1;
377         ir_type *irtype       = new_type_method(id, n_parameters, n_results);
378
379         if(return_type != type_void) {
380                 ir_type *restype = get_ir_type(return_type);
381                 set_method_res_type(irtype, 0, restype);
382         }
383
384         function_parameter_t *parameter = function_type->parameters;
385         int                   n         = 0;
386         for( ; parameter != NULL; parameter = parameter->next) {
387                 ir_type *p_irtype = get_ir_type(parameter->type);
388                 set_method_param_type(irtype, n, p_irtype);
389                 ++n;
390         }
391
392         if(function_type->variadic || function_type->unspecified_parameters) {
393                 set_method_variadicity(irtype, variadicity_variadic);
394         }
395
396         return irtype;
397 }
398
399 static ir_type *create_pointer_type(pointer_type_t *type)
400 {
401         type_t  *points_to = type->points_to;
402         ir_type *ir_points_to;
403         /* Avoid endless recursion if the points_to type contains this poiner type
404          * again (might be a struct). We therefore first create a void* pointer
405          * and then set the real points_to type
406          */
407         ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
408                                             ir_type_void, mode_P_data);
409         type->type.firm_type  = ir_type;
410
411         ir_points_to = get_ir_type(points_to);
412         set_pointer_points_to_type(ir_type, ir_points_to);
413
414         return ir_type;
415 }
416
417 static ir_type *create_array_type(array_type_t *type)
418 {
419         type_t  *element_type    = type->element_type;
420         ir_type *ir_element_type = get_ir_type(element_type);
421
422         ident   *id      = unique_ident("array");
423         ir_type *ir_type = new_type_array(id, 1, ir_element_type);
424
425         if(type->size != NULL) {
426                 int n_elements = fold_constant(type->size);
427
428                 set_array_bounds_int(ir_type, 0, 0, n_elements);
429
430                 size_t elemsize = get_type_size_bytes(ir_element_type);
431                 int align = get_type_alignment_bytes(ir_element_type);
432                 if(elemsize % align > 0) {
433                         elemsize += align - (elemsize % align);
434                 }
435                 set_type_size_bytes(ir_type, n_elements * elemsize);
436                 set_type_alignment_bytes(ir_type, align);
437                 set_type_state(ir_type, layout_fixed);
438         } else {
439                 set_array_lower_bound_int(ir_type, 0, 0);
440         }
441
442         return ir_type;
443 }
444
445 #define INVALID_TYPE ((ir_type_ptr)-1)
446
447 static ir_type *create_struct_type(compound_type_t *type)
448 {
449         symbol_t *symbol = type->declaration->symbol;
450         ident    *id;
451         if(symbol != NULL) {
452                 id = unique_ident(symbol->string);
453         } else {
454                 id = unique_ident("__anonymous_struct");
455         }
456         ir_type *ir_type = new_type_struct(id);
457
458         type->type.firm_type = ir_type;
459
460         int align_all = 1;
461         int offset    = 0;
462         declaration_t *entry = type->declaration->context.declarations;
463         for( ; entry != NULL; entry = entry->next) {
464                 if(entry->namespc != NAMESPACE_NORMAL)
465                         continue;
466
467                 ident       *ident         = new_id_from_str(entry->symbol->string);
468                 ir_type_ptr  entry_ir_type = get_ir_type(entry->type);
469
470                 int entry_size      = get_type_size_bytes(entry_ir_type);
471                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
472                 int misalign        = offset % entry_alignment;
473                 if (misalign != 0)
474                         offset += entry_alignment - misalign;
475
476                 dbg_info  *const dbgi   = get_dbg_info(&entry->source_position);
477                 ir_entity *const entity = new_d_entity(ir_type, ident, entry_ir_type, dbgi);
478                 set_entity_offset(entity, offset);
479                 add_struct_member(ir_type, entity);
480                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
481                 entry->v.entity         = entity;
482
483                 offset += entry_size;
484                 if(entry_alignment > align_all) {
485                         if(entry_alignment % align_all != 0) {
486                                 panic("Uneven alignments not supported yet");
487                         }
488                         align_all = entry_alignment;
489                 }
490         }
491
492         int misalign = offset % align_all;
493         offset += misalign;
494         set_type_alignment_bytes(ir_type, align_all);
495         set_type_size_bytes(ir_type, offset);
496         set_type_state(ir_type, layout_fixed);
497
498         return ir_type;
499 }
500
501 static ir_type *create_union_type(compound_type_t *type)
502 {
503         declaration_t *declaration = type->declaration;
504         symbol_t      *symbol      = declaration->symbol;
505         ident         *id;
506         if(symbol != NULL) {
507                 id = unique_ident(symbol->string);
508         } else {
509                 id = unique_ident("__anonymous_union");
510         }
511         ir_type  *ir_type = new_type_union(id);
512
513         type->type.firm_type = ir_type;
514
515         int align_all = 1;
516         int size      = 0;
517         declaration_t *entry = declaration->context.declarations;
518         for( ; entry != NULL; entry = entry->next) {
519                 if(entry->namespc != NAMESPACE_NORMAL)
520                         continue;
521
522                 ident       *ident         = new_id_from_str(entry->symbol->string);
523                 ir_type_ptr  entry_ir_type = get_ir_type(entry->type);
524
525                 int entry_size      = get_type_size_bytes(entry_ir_type);
526                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
527
528                 dbg_info  *const dbgi   = get_dbg_info(&entry->source_position);
529                 ir_entity *const entity = new_d_entity(ir_type, ident, entry_ir_type, dbgi);
530                 add_union_member(ir_type, entity);
531                 set_entity_offset(entity, 0);
532                 entry->declaration_type = DECLARATION_TYPE_COMPOUND_MEMBER;
533                 entry->v.entity         = entity;
534
535                 if(entry_size > size) {
536                         size = entry_size;
537                 }
538                 if(entry_alignment > align_all) {
539                         if(entry_alignment % align_all != 0) {
540                                 panic("Uneven alignments not supported yet");
541                         }
542                         align_all = entry_alignment;
543                 }
544         }
545
546         set_type_alignment_bytes(ir_type, align_all);
547         set_type_size_bytes(ir_type, size);
548         set_type_state(ir_type, layout_fixed);
549
550         return ir_type;
551 }
552
553 static ir_node *expression_to_firm(const expression_t *expression);
554 static inline ir_mode *get_ir_mode(type_t *type);
555
556 static ir_type *create_enum_type(enum_type_t *const type)
557 {
558         type->type.firm_type = ir_type_int;
559
560         ir_mode *const mode    = get_ir_mode((type_t*) type);
561         tarval  *const one     = get_mode_one(mode);
562         tarval  *      tv_next = get_tarval_null(mode);
563
564         declaration_t *declaration = type->declaration->next;
565         for (; declaration != NULL; declaration = declaration->next) {
566                 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
567                         break;
568
569                 declaration->declaration_type = DECLARATION_TYPE_ENUM_ENTRY;
570
571                 expression_t *const init = declaration->init.enum_value;
572                 if (init != NULL) {
573                         ir_node *const cnst = expression_to_firm(init);
574                         if (!is_Const(cnst)) {
575                                 panic("couldn't fold constant");
576                         }
577                         tv_next = get_Const_tarval(cnst);
578                 }
579                 declaration->v.enum_val = tv_next;
580                 tv_next = tarval_add(tv_next, one);
581         }
582
583         return ir_type_int;
584 }
585
586 static ir_type *get_ir_type(type_t *type)
587 {
588         assert(type != NULL);
589
590         type = skip_typeref(type);
591
592         if(type->base.firm_type != NULL) {
593                 assert(type->base.firm_type != INVALID_TYPE);
594                 return type->base.firm_type;
595         }
596
597         ir_type *firm_type = NULL;
598         switch(type->type) {
599         case TYPE_ATOMIC:
600                 firm_type = create_atomic_type(&type->atomic);
601                 break;
602         case TYPE_FUNCTION:
603                 firm_type = create_method_type(&type->function);
604                 break;
605         case TYPE_POINTER:
606                 firm_type = create_pointer_type(&type->pointer);
607                 break;
608         case TYPE_ARRAY:
609                 firm_type = create_array_type(&type->array);
610                 break;
611         case TYPE_COMPOUND_STRUCT:
612                 firm_type = create_struct_type(&type->compound);
613                 break;
614         case TYPE_COMPOUND_UNION:
615                 firm_type = create_union_type(&type->compound);
616                 break;
617         case TYPE_ENUM:
618                 firm_type = create_enum_type(&type->enumt);
619                 break;
620         case TYPE_BUILTIN:
621                 firm_type = get_ir_type(type->builtin.real_type);
622                 break;
623         case TYPE_TYPEOF:
624         case TYPE_TYPEDEF:
625         case TYPE_INVALID:
626                 break;
627         }
628         if(firm_type == NULL)
629                 panic("unknown type found");
630
631         type->base.firm_type = firm_type;
632         return firm_type;
633 }
634
635 static inline ir_mode *get_ir_mode(type_t *type)
636 {
637         ir_type *irtype = get_ir_type(type);
638
639         /* firm doesn't report a mode for arrays somehow... */
640         if(is_Array_type(irtype)) {
641                 return mode_P_data;
642         }
643
644         ir_mode *mode = get_type_mode(irtype);
645         assert(mode != NULL);
646         return mode;
647 }
648
649 static ir_entity* get_function_entity(declaration_t *declaration)
650 {
651         if(declaration->declaration_type == DECLARATION_TYPE_FUNCTION)
652                 return declaration->v.entity;
653         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
654
655         symbol_t *symbol = declaration->symbol;
656         ident    *id     = new_id_from_str(symbol->string);
657
658         ir_type  *global_type    = get_glob_type();
659         ir_type  *ir_type_method = get_ir_type(declaration->type);
660         assert(is_Method_type(ir_type_method));
661
662         dbg_info  *const dbgi   = get_dbg_info(&declaration->source_position);
663         ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
664         set_entity_ld_ident(entity, id);
665         if(declaration->storage_class == STORAGE_CLASS_STATIC
666                         || declaration->is_inline) {
667                 set_entity_visibility(entity, visibility_local);
668         } else if(declaration->init.statement != NULL) {
669                 set_entity_visibility(entity, visibility_external_visible);
670         } else {
671                 set_entity_visibility(entity, visibility_external_allocated);
672         }
673         set_entity_allocation(entity, allocation_static);
674
675         declaration->declaration_type = DECLARATION_TYPE_FUNCTION;
676         declaration->v.entity         = entity;
677
678         return entity;
679 }
680
681 static ir_node *const_to_firm(const const_expression_t *cnst)
682 {
683         dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
684         ir_mode  *mode = get_ir_mode(cnst->expression.datatype);
685
686         char    buf[128];
687         tarval *tv;
688         size_t  len;
689         if(mode_is_float(mode)) {
690                 tv = new_tarval_from_double(cnst->v.float_value, mode);
691         } else {
692                 if(mode_is_signed(mode)) {
693                         len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
694                 } else {
695                         len = snprintf(buf, sizeof(buf), "%llu",
696                                        (unsigned long long) cnst->v.int_value);
697                 }
698                 tv = new_tarval_from_str(buf, len, mode);
699         }
700
701         return new_d_Const(dbgi, mode, tv);
702 }
703
704 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
705 {
706         assert(entity != NULL);
707         union symconst_symbol sym;
708         sym.entity_p = entity;
709         return new_d_SymConst(dbgi, sym, symconst_addr_ent);
710 }
711
712 static ir_node *string_to_firm(const source_position_t *const src_pos,
713                                const char *const id_prefix,
714                                const char *const string)
715 {
716         ir_type *const global_type = get_glob_type();
717         ir_type *const type        = new_type_array(unique_ident("strtype"), 1,
718                                                     ir_type_const_char);
719
720         ident     *const id     = unique_ident(id_prefix);
721         dbg_info  *const dbgi   = get_dbg_info(src_pos);
722         ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
723         set_entity_ld_ident(entity, id);
724         set_entity_variability(entity, variability_constant);
725         set_entity_allocation(entity, allocation_static);
726
727         ir_type *const elem_type = ir_type_const_char;
728         ir_mode *const mode      = get_type_mode(elem_type);
729
730         const size_t slen = strlen(string) + 1;
731
732         set_array_lower_bound_int(type, 0, 0);
733         set_array_upper_bound_int(type, 0, slen);
734         set_type_size_bytes(type, slen);
735         set_type_state(type, layout_fixed);
736
737         tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
738         for(size_t i = 0; i < slen; ++i) {
739                 tvs[i] = new_tarval_from_long(string[i], mode);
740         }
741
742         set_array_entity_values(entity, tvs, slen);
743         free(tvs);
744
745         return create_symconst(dbgi, entity);
746 }
747
748 static ir_node *string_literal_to_firm(
749                 const string_literal_expression_t* literal)
750 {
751         return string_to_firm(&literal->expression.source_position, "Lstr",
752                               literal->value);
753 }
754
755 static ir_node *wide_string_literal_to_firm(
756         const wide_string_literal_expression_t* const literal)
757 {
758         ir_type *const global_type = get_glob_type();
759         ir_type *const elem_type   = ir_type_wchar_t;
760         ir_type *const type        = new_type_array(unique_ident("strtype"), 1,
761                                                     elem_type);
762
763         ident     *const id     = unique_ident("Lstr");
764         dbg_info  *const dbgi   = get_dbg_info(&literal->expression.source_position);
765         ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
766         set_entity_ld_ident(entity, id);
767         set_entity_variability(entity, variability_constant);
768         set_entity_allocation(entity, allocation_static);
769
770         ir_mode *const mode      = get_type_mode(elem_type);
771
772         const wchar_rep_t *const string = literal->value.begin;
773         const size_t             slen   = literal->value.size;
774
775         set_array_lower_bound_int(type, 0, 0);
776         set_array_upper_bound_int(type, 0, slen);
777         set_type_size_bytes(type, slen);
778         set_type_state(type, layout_fixed);
779
780         tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
781         for(size_t i = 0; i < slen; ++i) {
782                 tvs[i] = new_tarval_from_long(string[i], mode);
783         }
784
785         set_array_entity_values(entity, tvs, slen);
786         free(tvs);
787
788         return create_symconst(dbgi, entity);
789 }
790
791 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
792                               dbg_info *const dbgi)
793 {
794         if(is_compound_type(irtype) || is_Array_type(irtype)) {
795                 return addr;
796         }
797
798         ir_mode *const mode     = get_type_mode(irtype);
799         ir_node *const memory   = get_store();
800         ir_node *const load     = new_d_Load(dbgi, memory, addr, mode);
801         ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
802         ir_node *const load_res = new_d_Proj(dbgi, load, mode,   pn_Load_res);
803         set_store(load_mem);
804         return load_res;
805 }
806
807 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
808 {
809         ir_mode *mode = get_irn_mode(node);
810
811         if(!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
812                 return node;
813         if(!mode_is_float(mode))
814                 return node;
815
816         /* check if there is already a Conv */
817         if (get_irn_op(node) == op_Conv) {
818                 /* convert it into a strict Conv */
819                 set_Conv_strict(node, 1);
820                 return node;
821         }
822
823         /* otherwise create a new one */
824         return new_d_strictConv(dbgi, node, mode);
825 }
826
827 static ir_node *get_global_var_address(dbg_info *const dbgi,
828                                        const declaration_t *const decl)
829 {
830         assert(decl->declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
831
832         ir_entity *const entity = decl->v.entity;
833         switch ((storage_class_tag_t)decl->storage_class) {
834                 case STORAGE_CLASS_THREAD:
835                 case STORAGE_CLASS_THREAD_EXTERN:
836                 case STORAGE_CLASS_THREAD_STATIC: {
837                         ir_node *const no_mem = new_NoMem();
838                         ir_node *const tls    = get_irg_tls(current_ir_graph);
839                         return new_d_simpleSel(dbgi, no_mem, tls, entity);
840                 }
841
842                 default:
843                         return create_symconst(dbgi, entity);
844         }
845 }
846
847 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
848 {
849         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
850         declaration_t *declaration = ref->declaration;
851         type_t        *type        = skip_typeref(declaration->type);
852
853         switch((declaration_type_t) declaration->declaration_type) {
854         case DECLARATION_TYPE_UNKNOWN:
855                 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
856                         break;
857                 }
858                 get_ir_type(type);
859                 /* FALLTHROUGH */
860
861         case DECLARATION_TYPE_ENUM_ENTRY: {
862                 ir_mode *const mode = get_ir_mode(type);
863                 return new_Const(mode, declaration->v.enum_val);
864         }
865
866         case DECLARATION_TYPE_LOCAL_VARIABLE: {
867                 ir_mode *mode = get_ir_mode(type);
868                 return get_value(declaration->v.value_number, mode);
869         }
870         case DECLARATION_TYPE_FUNCTION: {
871                 return create_symconst(dbgi, declaration->v.entity);
872         }
873         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
874                 ir_node *const addr   = get_global_var_address(dbgi, declaration);
875                 ir_type *const irtype = get_entity_type(declaration->v.entity);
876                 return deref_address(irtype, addr, dbgi);
877         }
878
879         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
880                 ir_entity *entity = declaration->v.entity;
881                 ir_node   *frame  = get_irg_frame(current_ir_graph);
882                 ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
883                 ir_type   *irtype = get_entity_type(entity);
884                 return deref_address(irtype, sel, dbgi);
885         }
886
887         case DECLARATION_TYPE_COMPOUND_MEMBER:
888         case DECLARATION_TYPE_LABEL_BLOCK:
889                 panic("not implemented reference type");
890         }
891
892         panic("reference to declaration with unknown type found");
893 }
894
895 static ir_node *reference_addr(const reference_expression_t *ref)
896 {
897         dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
898         declaration_t *declaration = ref->declaration;
899
900         switch((declaration_type_t) declaration->declaration_type) {
901         case DECLARATION_TYPE_UNKNOWN:
902                 break;
903         case DECLARATION_TYPE_LOCAL_VARIABLE:
904                 panic("local variable without entity has no address");
905         case DECLARATION_TYPE_FUNCTION: {
906                 return create_symconst(dbgi, declaration->v.entity);
907         }
908         case DECLARATION_TYPE_GLOBAL_VARIABLE: {
909                 ir_node *const addr = get_global_var_address(dbgi, declaration);
910                 return addr;
911         }
912         case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
913                 ir_entity *entity = declaration->v.entity;
914                 ir_node   *frame  = get_irg_frame(current_ir_graph);
915                 ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
916
917                 return sel;
918         }
919
920         case DECLARATION_TYPE_ENUM_ENTRY:
921                 panic("trying to reference enum entry");
922
923         case DECLARATION_TYPE_COMPOUND_MEMBER:
924         case DECLARATION_TYPE_LABEL_BLOCK:
925                 panic("not implemented reference type");
926         }
927
928         panic("reference to declaration with unknown type found");
929 }
930
931 static ir_node *process_builtin_call(const call_expression_t *call)
932 {
933         dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
934
935         assert(call->function->type == EXPR_BUILTIN_SYMBOL);
936         builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
937
938         type_t *type = skip_typeref(builtin->expression.datatype);
939         assert(is_type_pointer(type));
940
941         type_t   *function_type = skip_typeref(type->pointer.points_to);
942         symbol_t *symbol        = builtin->symbol;
943
944         switch(symbol->ID) {
945         case T___builtin_alloca: {
946                 if(call->arguments == NULL || call->arguments->next != NULL) {
947                         panic("invalid number of parameters on __builtin_alloca");
948                 }
949                 expression_t *argument = call->arguments->expression;
950                 ir_node      *size     = expression_to_firm(argument);
951
952                 ir_node *store  = get_store();
953                 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
954                                               stack_alloc);
955                 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
956                 set_store(proj_m);
957                 ir_node *res    = new_Proj(alloca, mode_P_data, pn_Alloc_res);
958
959                 return res;
960         }
961         case T___builtin_nan:
962         case T___builtin_nanf:
963         case T___builtin_nand: {
964                 /* Ignore string for now... */
965                 assert(is_type_function(function_type));
966                 ir_mode *mode = get_ir_mode(function_type->function.return_type);
967                 tarval  *tv   = get_mode_NAN(mode);
968                 ir_node *res  = new_d_Const(dbgi, mode, tv);
969                 return res;
970         }
971         case T___builtin_va_end:
972                 return NULL;
973         default:
974                 panic("Unsupported builtin found\n");
975         }
976 }
977
978 static ir_node *call_expression_to_firm(const call_expression_t *call)
979 {
980         assert(get_cur_block() != NULL);
981
982         expression_t *function = call->function;
983         if(function->type == EXPR_BUILTIN_SYMBOL) {
984                 return process_builtin_call(call);
985         }
986         ir_node *callee = expression_to_firm(function);
987
988         type_t *type = skip_typeref(function->base.datatype);
989         assert(is_type_pointer(type));
990         pointer_type_t *pointer_type = &type->pointer;
991         type_t         *points_to    = skip_typeref(pointer_type->points_to);
992         assert(is_type_function(points_to));
993         function_type_t *function_type = &points_to->function;
994
995         int              n_parameters = 0;
996         call_argument_t *argument     = call->arguments;
997         for( ; argument != NULL; argument = argument->next) {
998                 ++n_parameters;
999         }
1000
1001         dbg_info *dbgi  = get_dbg_info(&call->expression.source_position);
1002
1003         ir_type *ir_method_type  = get_ir_type((type_t*) function_type);
1004         ir_type *new_method_type = NULL;
1005         if(function_type->variadic || function_type->unspecified_parameters) {
1006                 /* we need to construct a new method type matching the call
1007                  * arguments... */
1008                 int n_res       = get_method_n_ress(ir_method_type);
1009                 new_method_type = new_type_method(unique_ident("calltype"),
1010                                                   n_parameters, n_res);
1011                 set_method_calling_convention(new_method_type,
1012                                get_method_calling_convention(ir_method_type));
1013                 set_method_additional_properties(new_method_type,
1014                                get_method_additional_properties(ir_method_type));
1015
1016                 for(int i = 0; i < n_res; ++i) {
1017                         set_method_res_type(new_method_type, i,
1018                                             get_method_res_type(ir_method_type, i));
1019                 }
1020         }
1021         ir_node *in[n_parameters];
1022
1023         argument = call->arguments;
1024         int n = 0;
1025         for( ; argument != NULL; argument = argument->next) {
1026                 expression_t *expression = argument->expression;
1027                 ir_node      *arg_node   = expression_to_firm(expression);
1028
1029                 arg_node = do_strict_conv(dbgi, arg_node);
1030
1031                 in[n] = arg_node;
1032                 if(new_method_type != NULL) {
1033                         ir_type *irtype = get_ir_type(expression->base.datatype);
1034                         set_method_param_type(new_method_type, n, irtype);
1035                 }
1036
1037                 n++;
1038         }
1039         assert(n == n_parameters);
1040
1041         if(new_method_type != NULL)
1042                 ir_method_type = new_method_type;
1043
1044         ir_node  *store = get_store();
1045         ir_node  *node  = new_d_Call(dbgi, store, callee, n_parameters, in,
1046                                      ir_method_type);
1047         ir_node  *mem   = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1048         set_store(mem);
1049
1050         type_t  *return_type = skip_typeref(function_type->return_type);
1051         ir_node *result      = NULL;
1052
1053         if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1054                 ir_mode *mode;
1055                 if(is_type_scalar(return_type)) {
1056                         mode = get_ir_mode(return_type);
1057                 } else {
1058                         mode = mode_P_data;
1059                 }
1060                 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1061                 result           = new_d_Proj(dbgi, resproj, mode, 0);
1062         }
1063
1064         return result;
1065 }
1066
1067 static void statement_to_firm(statement_t *statement);
1068 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1069
1070 static ir_node *expression_to_addr(const expression_t *expression);
1071 static void create_condition_evaluation(const expression_t *expression,
1072                                         ir_node *true_block,
1073                                         ir_node *false_block);
1074
1075 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1076                          ir_node *value)
1077 {
1078         value = do_strict_conv(dbgi, value);
1079
1080         ir_node  *memory = get_store();
1081
1082         if(is_type_scalar(type)) {
1083                 ir_node  *store     = new_d_Store(dbgi, memory, addr, value);
1084                 ir_node  *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1085                 set_store(store_mem);
1086         } else {
1087                 ir_type *irtype    = get_ir_type(type);
1088                 ir_node *copyb     = new_d_CopyB(dbgi, memory, addr, value, irtype);
1089                 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1090                 set_store(copyb_mem);
1091         }
1092 }
1093
1094 static void set_value_for_expression(const expression_t *expression,
1095                                      ir_node *value)
1096 {
1097         dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1098         value          = do_strict_conv(dbgi, value);
1099
1100         if(expression->type == EXPR_REFERENCE) {
1101                 reference_expression_t *ref = (reference_expression_t*) expression;
1102
1103                 declaration_t *declaration = ref->declaration;
1104                 assert(declaration->declaration_type != DECLARATION_TYPE_UNKNOWN);
1105                 if(declaration->declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
1106                         set_value(declaration->v.value_number, value);
1107                         return;
1108                 }
1109         }
1110
1111         ir_node *addr = expression_to_addr(expression);
1112         type_t  *type = skip_typeref(expression->base.datatype);
1113         assign_value(dbgi, addr, type, value);
1114 }
1115
1116 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1117 {
1118         ir_mode *value_mode = get_irn_mode(value);
1119
1120         if (value_mode == dest_mode || is_Bad(value))
1121                 return value;
1122
1123         if(dest_mode == mode_b) {
1124                 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1125                 ir_node *cmp  = new_d_Cmp(dbgi, value, zero);
1126                 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1127                 return proj;
1128         }
1129
1130         return new_d_Conv(dbgi, value, dest_mode);
1131 }
1132
1133 static ir_node *create_incdec(const unary_expression_t *expression)
1134 {
1135         dbg_info     *dbgi  = get_dbg_info(&expression->expression.source_position);
1136         type_t       *type  = skip_typeref(expression->expression.datatype);
1137         ir_mode      *mode  = get_ir_mode(type);
1138         expression_t *value = expression->value;
1139
1140         ir_node *value_node = expression_to_firm(value);
1141
1142         ir_node *offset;
1143         if(is_type_pointer(type)) {
1144                 pointer_type_t *pointer_type = &type->pointer;
1145                 unsigned        elem_size    = get_type_size(pointer_type->points_to);
1146                 offset = new_Const_long(mode_int, elem_size);
1147         } else {
1148                 assert(is_type_arithmetic(type));
1149                 offset = new_Const(mode, get_mode_one(mode));
1150         }
1151
1152         switch(expression->expression.type) {
1153         case EXPR_UNARY_POSTFIX_INCREMENT: {
1154                 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1155                 set_value_for_expression(value, new_value);
1156                 return value_node;
1157         }
1158         case EXPR_UNARY_POSTFIX_DECREMENT: {
1159                 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1160                 set_value_for_expression(value, new_value);
1161                 return value_node;
1162         }
1163         case EXPR_UNARY_PREFIX_INCREMENT: {
1164                 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1165                 set_value_for_expression(value, new_value);
1166                 return new_value;
1167         }
1168         case EXPR_UNARY_PREFIX_DECREMENT: {
1169                 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1170                 set_value_for_expression(value, new_value);
1171                 return new_value;
1172         }
1173         default:
1174                 panic("no incdec expr in create_incdec");
1175                 return NULL;
1176         }
1177 }
1178
1179 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1180 {
1181         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1182         type_t   *type = skip_typeref(expression->expression.datatype);
1183
1184         if(expression->expression.type == EXPR_UNARY_TAKE_ADDRESS)
1185                 return expression_to_addr(expression->value);
1186
1187         const expression_t *value      = expression->value;
1188         ir_node            *value_node = expression_to_firm(value);
1189
1190         switch(expression->expression.type) {
1191         case EXPR_UNARY_NEGATE: {
1192                 ir_mode *mode = get_ir_mode(type);
1193                 return new_d_Minus(dbgi, value_node, mode);
1194         }
1195         case EXPR_UNARY_PLUS:
1196                 return value_node;
1197         case EXPR_UNARY_BITWISE_NEGATE: {
1198                 ir_mode *mode = get_ir_mode(type);
1199                 return new_d_Not(dbgi, value_node, mode);
1200         }
1201         case EXPR_UNARY_NOT: {
1202                 ir_mode *mode = get_ir_mode(type);
1203                 if(get_irn_mode(value_node) != mode_b) {
1204                         value_node = create_conv(dbgi, value_node, mode_b);
1205                 }
1206                 value_node = new_d_Not(dbgi, value_node, mode_b);
1207                 if(mode != mode_b) {
1208                         value_node = create_conv(dbgi, value_node, mode);
1209                 }
1210                 return value_node;
1211         }
1212         case EXPR_UNARY_DEREFERENCE: {
1213                 type_t  *value_type = skip_typeref(value->base.datatype);
1214                 ir_type *irtype     = get_ir_type(value_type);
1215                 assert(is_Pointer_type(irtype));
1216                 ir_type *points_to  = get_pointer_points_to_type(irtype);
1217                 return deref_address(points_to, value_node, dbgi);
1218         }
1219         case EXPR_UNARY_POSTFIX_INCREMENT:
1220         case EXPR_UNARY_POSTFIX_DECREMENT:
1221         case EXPR_UNARY_PREFIX_INCREMENT:
1222         case EXPR_UNARY_PREFIX_DECREMENT:
1223                 return create_incdec(expression);
1224         case EXPR_UNARY_CAST: {
1225                 ir_mode *mode = get_ir_mode(type);
1226                 ir_node *node = create_conv(dbgi, value_node, mode);
1227                 node = do_strict_conv(dbgi, node);
1228                 return node;
1229         }
1230         case EXPR_UNARY_CAST_IMPLICIT: {
1231                 ir_mode *mode = get_ir_mode(type);
1232                 return create_conv(dbgi, value_node, mode);
1233         }
1234
1235         default:
1236                 break;
1237         }
1238         panic("invalid UNEXPR type found");
1239 }
1240
1241 static long get_pnc(const expression_type_t type)
1242 {
1243         switch(type) {
1244         case EXPR_BINARY_EQUAL:         return pn_Cmp_Eq;
1245         case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1246         case EXPR_BINARY_NOTEQUAL:      return pn_Cmp_Ne;
1247         case EXPR_BINARY_ISLESS:
1248         case EXPR_BINARY_LESS:          return pn_Cmp_Lt;
1249         case EXPR_BINARY_ISLESSEQUAL:
1250         case EXPR_BINARY_LESSEQUAL:     return pn_Cmp_Le;
1251         case EXPR_BINARY_ISGREATER:
1252         case EXPR_BINARY_GREATER:       return pn_Cmp_Gt;
1253         case EXPR_BINARY_ISGREATEREQUAL:
1254         case EXPR_BINARY_GREATEREQUAL:  return pn_Cmp_Ge;
1255         case EXPR_BINARY_ISUNORDERED:   return pn_Cmp_Uo;
1256
1257         default:
1258                 break;
1259         }
1260         panic("trying to get pn_Cmp from non-comparison binexpr type");
1261 }
1262
1263 static ir_node *create_lazy_op(const binary_expression_t *expression)
1264 {
1265         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1266         type_t   *type = expression->expression.datatype;
1267         ir_mode  *mode = get_ir_mode(type);
1268
1269         ir_node *cur_block = get_cur_block();
1270
1271         ir_node *one_block = new_immBlock();
1272         ir_node *one       = new_Const(mode, get_mode_one(mode));
1273         ir_node *jmp_one   = new_d_Jmp(dbgi);
1274
1275         ir_node *zero_block = new_immBlock();
1276         ir_node *zero       = new_Const(mode, get_mode_null(mode));
1277         ir_node *jmp_zero   = new_d_Jmp(dbgi);
1278
1279         set_cur_block(cur_block);
1280         create_condition_evaluation((const expression_t*) expression,
1281                                     one_block, zero_block);
1282         mature_immBlock(one_block);
1283         mature_immBlock(zero_block);
1284
1285         ir_node *common_block = new_immBlock();
1286         add_immBlock_pred(common_block, jmp_one);
1287         add_immBlock_pred(common_block, jmp_zero);
1288         mature_immBlock(common_block);
1289
1290         ir_node *in[2] = { one, zero };
1291         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
1292
1293         return val;
1294 }
1295
1296 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1297                                             ir_node *right, ir_mode *mode);
1298
1299 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1300                                         create_arithmetic_func func)
1301 {
1302         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1303         ir_node  *left  = expression_to_firm(expression->left);
1304         ir_node  *right = expression_to_firm(expression->right);
1305         type_t   *type  = expression->right->base.datatype;
1306         /* be careful with the modes, because in arithmetic assign nodes only
1307          * the right operand has the mode of the arithmetic already */
1308         ir_mode  *mode  = get_ir_mode(type);
1309         left            = create_conv(dbgi, left, mode);
1310         ir_node  *res   = func(dbgi, left, right, mode);
1311
1312         return res;
1313 }
1314
1315 static ir_node *pointer_arithmetic(ir_node  *const pointer,
1316                                    ir_node  *      integer,
1317                                    type_t   *const type,
1318                                    dbg_info *const dbgi,
1319                                    const create_arithmetic_func func)
1320 {
1321         pointer_type_t *const pointer_type = (pointer_type_t*)type;
1322         type_t         *const points_to    = pointer_type->points_to;
1323         const unsigned        elem_size    = get_type_size(points_to);
1324
1325         assert(elem_size >= 1);
1326         if (elem_size > 1) {
1327                 integer             = create_conv(dbgi, integer, mode_int);
1328                 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1329                 ir_node *const mul  = new_d_Mul(dbgi, integer, cnst, mode_int);
1330                 integer = mul;
1331         }
1332
1333         ir_mode *const mode = get_ir_mode(type);
1334         return func(dbgi, pointer, integer, mode);
1335 }
1336
1337 static ir_node *create_arithmetic_assign_binop(
1338                 const binary_expression_t *expression, create_arithmetic_func func)
1339 {
1340         dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
1341         type_t   *const type = skip_typeref(expression->expression.datatype);
1342         ir_node  *value;
1343
1344         if (is_type_pointer(type)) {
1345                 ir_node        *const pointer = expression_to_firm(expression->left);
1346                 ir_node        *      integer = expression_to_firm(expression->right);
1347                 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
1348         } else {
1349                 value = create_arithmetic_binop(expression, func);
1350         }
1351
1352         ir_mode  *const mode = get_ir_mode(type);
1353         value = create_conv(dbgi, value, mode);
1354         set_value_for_expression(expression->left, value);
1355
1356         return value;
1357 }
1358
1359 static ir_node *create_add(const binary_expression_t *expression)
1360 {
1361         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1362         ir_node  *left  = expression_to_firm(expression->left);
1363         ir_node  *right = expression_to_firm(expression->right);
1364         type_t   *type  = expression->expression.datatype;
1365
1366         expression_t *expr_left  = expression->left;
1367         expression_t *expr_right = expression->right;
1368         type_t       *type_left  = skip_typeref(expr_left->base.datatype);
1369         type_t       *type_right = skip_typeref(expr_right->base.datatype);
1370
1371         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1372                 ir_mode *const mode = get_ir_mode(type);
1373                 return new_d_Add(dbgi, left, right, mode);
1374         }
1375
1376         if (is_type_pointer(type_left)) {
1377                 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
1378         } else {
1379                 assert(is_type_pointer(type_right));
1380                 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
1381         }
1382 }
1383
1384 static ir_node *create_sub(const binary_expression_t *expression)
1385 {
1386         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1387         expression_t *const expr_left  = expression->left;
1388         expression_t *const expr_right = expression->right;
1389         ir_node      *const left       = expression_to_firm(expr_left);
1390         ir_node      *const right      = expression_to_firm(expr_right);
1391         type_t       *const type       = expression->expression.datatype;
1392         type_t       *const type_left  = skip_typeref(expr_left->base.datatype);
1393         type_t       *const type_right = skip_typeref(expr_right->base.datatype);
1394
1395         if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
1396                 ir_mode *const mode = get_ir_mode(type);
1397                 return new_d_Sub(dbgi, left, right, mode);
1398         } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
1399                 const pointer_type_t *const ptr_type = &type_left->pointer;
1400                 const unsigned elem_size             = get_type_size(ptr_type->points_to);
1401                 ir_mode *const mode   = get_ir_mode(type);
1402                 ir_node *const sub    = new_d_Sub(dbgi, left, right, mode);
1403                 ir_node *const cnst   = new_Const_long(mode_int, (long)elem_size);
1404                 ir_node *const no_mem = new_NoMem();
1405                 ir_node *const div    = new_d_Div(dbgi, no_mem, sub, cnst, mode,
1406                                                   op_pin_state_floats);
1407                 return new_d_Proj(dbgi, div, mode, pn_Div_res);
1408         }
1409
1410         assert(is_type_pointer(type_left));
1411         return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
1412 }
1413
1414 static ir_node *create_shift(const binary_expression_t *expression)
1415 {
1416         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1417         ir_node  *left  = expression_to_firm(expression->left);
1418         ir_node  *right = expression_to_firm(expression->right);
1419         type_t   *type  = expression->expression.datatype;
1420         ir_mode  *mode  = get_ir_mode(type);
1421
1422         /* firm always wants the shift count to be unsigned */
1423         right = create_conv(dbgi, right, mode_uint);
1424
1425         ir_node *res;
1426
1427         switch(expression->expression.type) {
1428         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1429         case EXPR_BINARY_SHIFTLEFT:
1430                 res = new_d_Shl(dbgi, left, right, mode);
1431                 break;
1432         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1433         case EXPR_BINARY_SHIFTRIGHT: {
1434                  expression_t *expr_left = expression->left;
1435                  type_t       *type_left = skip_typeref(expr_left->base.datatype);
1436
1437                  if(is_type_signed(type_left)) {
1438                         res = new_d_Shrs(dbgi, left, right, mode);
1439                  } else {
1440                          res = new_d_Shr(dbgi, left, right, mode);
1441                  }
1442                  break;
1443         }
1444         default:
1445                 panic("create shift op called for non-shift op");
1446         }
1447
1448         return res;
1449 }
1450
1451
1452 static ir_node *create_divmod(const binary_expression_t *expression)
1453 {
1454         dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
1455         ir_node  *left  = expression_to_firm(expression->left);
1456         ir_node  *right = expression_to_firm(expression->right);
1457         ir_node  *pin   = new_Pin(new_NoMem());
1458         /* be careful with the modes, because in arithmetic assign nodes only
1459          * the right operand has the mode of the arithmetic already */
1460         type_t   *type  = expression->right->base.datatype;
1461         ir_mode  *mode  = get_ir_mode(type);
1462         left            = create_conv(dbgi, left, mode);
1463         ir_node  *op;
1464         ir_node  *res;
1465
1466         switch (expression->expression.type) {
1467         case EXPR_BINARY_DIV:
1468         case EXPR_BINARY_DIV_ASSIGN:
1469                 if(mode_is_float(mode)) {
1470                         op  = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
1471                         res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
1472                 } else {
1473                         op  = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
1474                         res = new_d_Proj(dbgi, op, mode, pn_Div_res);
1475                 }
1476                 break;
1477
1478         case EXPR_BINARY_MOD:
1479         case EXPR_BINARY_MOD_ASSIGN:
1480                 assert(!mode_is_float(mode));
1481                 op  = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
1482                 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
1483                 break;
1484
1485         default: panic("unexpected binary expression type in create_divmod()");
1486         }
1487
1488         return res;
1489 }
1490
1491 static ir_node *create_arithmetic_assign_divmod(
1492                 const binary_expression_t *expression)
1493 {
1494         ir_node  *      value = create_divmod(expression);
1495         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1496         type_t   *const type  = expression->expression.datatype;
1497         ir_mode  *const mode  = get_ir_mode(type);
1498
1499         assert(type->type != TYPE_POINTER);
1500
1501         value = create_conv(dbgi, value, mode);
1502         set_value_for_expression(expression->left, value);
1503
1504         return value;
1505 }
1506
1507 static ir_node *create_arithmetic_assign_shift(
1508                 const binary_expression_t *expression)
1509 {
1510         ir_node  *      value = create_shift(expression);
1511         dbg_info *const dbgi  = get_dbg_info(&expression->expression.source_position);
1512         type_t   *const type  = expression->expression.datatype;
1513         ir_mode  *const mode  = get_ir_mode(type);
1514
1515         value = create_conv(dbgi, value, mode);
1516         set_value_for_expression(expression->left, value);
1517
1518         return value;
1519 }
1520
1521 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
1522 {
1523         expression_type_t type = expression->expression.type;
1524
1525         switch(type) {
1526         case EXPR_BINARY_EQUAL:
1527         case EXPR_BINARY_NOTEQUAL:
1528         case EXPR_BINARY_LESS:
1529         case EXPR_BINARY_LESSEQUAL:
1530         case EXPR_BINARY_GREATER:
1531         case EXPR_BINARY_GREATEREQUAL:
1532         case EXPR_BINARY_ISGREATER:
1533         case EXPR_BINARY_ISGREATEREQUAL:
1534         case EXPR_BINARY_ISLESS:
1535         case EXPR_BINARY_ISLESSEQUAL:
1536         case EXPR_BINARY_ISLESSGREATER:
1537         case EXPR_BINARY_ISUNORDERED: {
1538                 dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1539                 ir_node *left  = expression_to_firm(expression->left);
1540                 ir_node *right = expression_to_firm(expression->right);
1541                 ir_node *cmp   = new_d_Cmp(dbgi, left, right);
1542                 long     pnc   = get_pnc(type);
1543                 ir_node *proj  = new_d_Proj(dbgi, cmp, mode_b, pnc);
1544                 return proj;
1545         }
1546         case EXPR_BINARY_ASSIGN: {
1547                 ir_node *right = expression_to_firm(expression->right);
1548                 set_value_for_expression(expression->left, right);
1549
1550                 return right;
1551         }
1552         case EXPR_BINARY_ADD:
1553                 return create_add(expression);
1554         case EXPR_BINARY_SUB:
1555                 return create_sub(expression);
1556         case EXPR_BINARY_MUL:
1557                 return create_arithmetic_binop(expression, new_d_Mul);
1558         case EXPR_BINARY_BITWISE_AND:
1559                 return create_arithmetic_binop(expression, new_d_And);
1560         case EXPR_BINARY_BITWISE_OR:
1561                 return create_arithmetic_binop(expression, new_d_Or);
1562         case EXPR_BINARY_BITWISE_XOR:
1563                 return create_arithmetic_binop(expression, new_d_Eor);
1564         case EXPR_BINARY_SHIFTLEFT:
1565         case EXPR_BINARY_SHIFTRIGHT:
1566                 return create_shift(expression);
1567         case EXPR_BINARY_DIV:
1568         case EXPR_BINARY_MOD:
1569                 return create_divmod(expression);
1570         case EXPR_BINARY_LOGICAL_AND:
1571         case EXPR_BINARY_LOGICAL_OR:
1572                 return create_lazy_op(expression);
1573         case EXPR_BINARY_COMMA:
1574                 expression_to_firm(expression->left);
1575                 return expression_to_firm(expression->right);
1576         case EXPR_BINARY_ADD_ASSIGN:
1577                 return create_arithmetic_assign_binop(expression, new_d_Add);
1578         case EXPR_BINARY_SUB_ASSIGN:
1579                 return create_arithmetic_assign_binop(expression, new_d_Sub);
1580         case EXPR_BINARY_MUL_ASSIGN:
1581                 return create_arithmetic_assign_binop(expression, new_d_Mul);
1582         case EXPR_BINARY_DIV_ASSIGN:
1583                 return create_arithmetic_assign_divmod(expression);
1584         case EXPR_BINARY_BITWISE_AND_ASSIGN:
1585                 return create_arithmetic_assign_binop(expression, new_d_And);
1586         case EXPR_BINARY_BITWISE_OR_ASSIGN:
1587                 return create_arithmetic_assign_binop(expression, new_d_Or);
1588         case EXPR_BINARY_BITWISE_XOR_ASSIGN:
1589                 return create_arithmetic_assign_binop(expression, new_d_Eor);
1590         case EXPR_BINARY_SHIFTLEFT_ASSIGN:
1591         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
1592                 return create_arithmetic_assign_shift(expression);
1593         default:
1594                 panic("TODO binexpr type");
1595         }
1596 }
1597
1598 static ir_node *array_access_addr(const array_access_expression_t *expression)
1599 {
1600         dbg_info *dbgi      = get_dbg_info(&expression->expression.source_position);
1601         ir_node  *base_addr = expression_to_firm(expression->array_ref);
1602         ir_node  *offset    = expression_to_firm(expression->index);
1603         offset              = create_conv(dbgi, offset, mode_uint);
1604
1605         type_t *ref_type = skip_typeref(expression->array_ref->base.datatype);
1606         assert(is_type_pointer(ref_type));
1607         pointer_type_t *pointer_type = (pointer_type_t*) ref_type;
1608
1609         unsigned elem_size       = get_type_size(pointer_type->points_to);
1610         ir_node *elem_size_const = new_Const_long(mode_uint, elem_size);
1611         ir_node *real_offset     = new_d_Mul(dbgi, offset, elem_size_const,
1612                                              mode_uint);
1613         ir_node *result          = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
1614
1615         return result;
1616 }
1617
1618 static ir_node *array_access_to_firm(
1619                 const array_access_expression_t *expression)
1620 {
1621         dbg_info *dbgi   = get_dbg_info(&expression->expression.source_position);
1622         ir_node  *addr   = array_access_addr(expression);
1623         type_t   *type   = revert_automatic_type_conversion(
1624                         (const expression_t*) expression);
1625         type             = skip_typeref(type);
1626         ir_type  *irtype = get_ir_type(type);
1627
1628         return deref_address(irtype, addr, dbgi);
1629 }
1630
1631 static ir_node *sizeof_to_firm(const sizeof_expression_t *expression)
1632 {
1633         type_t *type = expression->type;
1634         if(type == NULL) {
1635                 type = expression->size_expression->base.datatype;
1636                 assert(type != NULL);
1637         }
1638
1639         ir_mode  *mode      = get_ir_mode(expression->expression.datatype);
1640         unsigned  size      = get_type_size(type);
1641         ir_node  *size_node = new_Const_long(mode, size);
1642
1643         return size_node;
1644 }
1645
1646 static tarval *try_fold_constant(const expression_t *expression)
1647 {
1648         ir_graph *old_current_ir_graph = current_ir_graph;
1649         if(current_ir_graph == NULL) {
1650                 current_ir_graph = get_const_code_irg();
1651         }
1652
1653         ir_node *cnst = expression_to_firm(expression);
1654         current_ir_graph = old_current_ir_graph;
1655
1656         if(!is_Const(cnst)) {
1657                 return NULL;
1658         }
1659
1660         tarval *tv = get_Const_tarval(cnst);
1661         if(!tarval_is_long(tv)) {
1662                 return NULL;
1663         }
1664
1665         return tv;
1666 }
1667
1668 static long fold_constant(const expression_t *expression)
1669 {
1670         tarval *tv = try_fold_constant(expression);
1671         if(tv == NULL) {
1672                 panic("couldn't fold constantl");
1673         }
1674
1675         return get_tarval_long(tv);
1676 }
1677
1678 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
1679 {
1680         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1681
1682         /* first try to fold a constant condition */
1683         tarval *tv = try_fold_constant(expression->condition);
1684         if(tv != NULL) {
1685                 long val = get_tarval_long(tv);
1686                 if(val) {
1687                         return expression_to_firm(expression->true_expression);
1688                 } else {
1689                         return expression_to_firm(expression->false_expression);
1690                 }
1691         }
1692
1693         ir_node *cur_block   = get_cur_block();
1694
1695         /* create the true block */
1696         ir_node *true_block  = new_immBlock();
1697
1698         ir_node *true_val = expression_to_firm(expression->true_expression);
1699         ir_node *true_jmp = new_Jmp();
1700
1701         /* create the false block */
1702         ir_node *false_block = new_immBlock();
1703
1704         ir_node *false_val = expression_to_firm(expression->false_expression);
1705         ir_node *false_jmp = new_Jmp();
1706
1707         /* create the condition evaluation */
1708         set_cur_block(cur_block);
1709         create_condition_evaluation(expression->condition, true_block, false_block);
1710         mature_immBlock(true_block);
1711         mature_immBlock(false_block);
1712
1713         /* create the common block */
1714         ir_node *common_block = new_immBlock();
1715         add_immBlock_pred(common_block, true_jmp);
1716         add_immBlock_pred(common_block, false_jmp);
1717         mature_immBlock(common_block);
1718
1719         /* TODO improve static semantics, so either both or no values are NULL */
1720         if (true_val == NULL || false_val == NULL)
1721                 return NULL;
1722
1723         ir_node *in[2] = { true_val, false_val };
1724         ir_mode *mode  = get_irn_mode(true_val);
1725         assert(get_irn_mode(false_val) == mode);
1726         ir_node *val   = new_d_Phi(dbgi, 2, in, mode);
1727
1728         return val;
1729 }
1730
1731 static ir_node *select_addr(const select_expression_t *expression)
1732 {
1733         dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
1734
1735         ir_node *compound_addr = expression_to_firm(expression->compound);
1736
1737         declaration_t *entry = expression->compound_entry;
1738         assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
1739         ir_entity     *entity = entry->v.entity;
1740
1741         assert(entity != NULL);
1742
1743         ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
1744
1745         return sel;
1746 }
1747
1748 static ir_node *select_to_firm(const select_expression_t *expression)
1749 {
1750         dbg_info *dbgi   = get_dbg_info(&expression->expression.source_position);
1751         ir_node  *addr   = select_addr(expression);
1752         type_t   *type   = revert_automatic_type_conversion(
1753                         (const expression_t*) expression);
1754         type             = skip_typeref(type);
1755         ir_type  *irtype = get_ir_type(type);
1756
1757         return deref_address(irtype, addr, dbgi);
1758 }
1759
1760 /* Values returned by __builtin_classify_type. */
1761 typedef enum gcc_type_class
1762 {
1763         no_type_class = -1,
1764         void_type_class,
1765         integer_type_class,
1766         char_type_class,
1767         enumeral_type_class,
1768         boolean_type_class,
1769         pointer_type_class,
1770         reference_type_class,
1771         offset_type_class,
1772         real_type_class,
1773         complex_type_class,
1774         function_type_class,
1775         method_type_class,
1776         record_type_class,
1777         union_type_class,
1778         array_type_class,
1779         string_type_class,
1780         set_type_class,
1781         file_type_class,
1782         lang_type_class
1783 } gcc_type_class;
1784
1785 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
1786 {
1787         const type_t *const type = expr->type_expression->base.datatype;
1788
1789         gcc_type_class tc;
1790         switch (type->type)
1791         {
1792                 case TYPE_ATOMIC: {
1793                         const atomic_type_t *const atomic_type = &type->atomic;
1794                         switch (atomic_type->atype) {
1795                                 /* should not be reached */
1796                                 case ATOMIC_TYPE_INVALID:
1797                                         tc = no_type_class;
1798                                         break;
1799
1800                                 /* gcc cannot do that */
1801                                 case ATOMIC_TYPE_VOID:
1802                                         tc = void_type_class;
1803                                         break;
1804
1805                                 case ATOMIC_TYPE_CHAR:      /* gcc handles this as integer */
1806                                 case ATOMIC_TYPE_SCHAR:     /* gcc handles this as integer */
1807                                 case ATOMIC_TYPE_UCHAR:     /* gcc handles this as integer */
1808                                 case ATOMIC_TYPE_SHORT:
1809                                 case ATOMIC_TYPE_USHORT:
1810                                 case ATOMIC_TYPE_INT:
1811                                 case ATOMIC_TYPE_UINT:
1812                                 case ATOMIC_TYPE_LONG:
1813                                 case ATOMIC_TYPE_ULONG:
1814                                 case ATOMIC_TYPE_LONGLONG:
1815                                 case ATOMIC_TYPE_ULONGLONG:
1816                                 case ATOMIC_TYPE_BOOL:      /* gcc handles this as integer */
1817                                         tc = integer_type_class;
1818                                         break;
1819
1820                                 case ATOMIC_TYPE_FLOAT:
1821                                 case ATOMIC_TYPE_DOUBLE:
1822                                 case ATOMIC_TYPE_LONG_DOUBLE:
1823                                         tc = real_type_class;
1824                                         break;
1825
1826 #ifdef PROVIDE_COMPLEX
1827                                 case ATOMIC_TYPE_FLOAT_COMPLEX:
1828                                 case ATOMIC_TYPE_DOUBLE_COMPLEX:
1829                                 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
1830                                         tc = complex_type_class;
1831                                         break;
1832                                 case ATOMIC_TYPE_FLOAT_IMAGINARY:
1833                                 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
1834                                 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
1835                                         tc = complex_type_class;
1836                                         break;
1837 #endif
1838
1839                                 default:
1840                                         panic("Unimplemented case in classify_type_to_firm().");
1841                         }
1842                         break;
1843                 }
1844
1845                 case TYPE_ARRAY:           /* gcc handles this as pointer */
1846                 case TYPE_FUNCTION:        /* gcc handles this as pointer */
1847                 case TYPE_POINTER:         tc = pointer_type_class; break;
1848                 case TYPE_COMPOUND_STRUCT: tc = record_type_class;  break;
1849                 case TYPE_COMPOUND_UNION:  tc = union_type_class;   break;
1850
1851                 /* gcc handles this as integer */
1852                 case TYPE_ENUM:            tc = integer_type_class; break;
1853
1854                 default:
1855                         panic("Unimplemented case in classify_type_to_firm().");
1856         }
1857
1858         dbg_info *const dbgi = get_dbg_info(&expr->expression.source_position);
1859         ir_mode  *const mode = mode_int;
1860         tarval   *const tv   = new_tarval_from_long(tc, mode);
1861         return new_d_Const(dbgi, mode, tv);
1862 }
1863
1864 static ir_node *function_name_to_firm(
1865                 const string_literal_expression_t *const expr)
1866 {
1867         if (current_function_name == NULL) {
1868                 const source_position_t *const src_pos =
1869                         &expr->expression.source_position;
1870                 const char *const name = current_function_decl->symbol->string;
1871                 current_function_name = string_to_firm(src_pos, "__func__", name);
1872         }
1873
1874         return current_function_name;
1875 }
1876
1877 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
1878 {
1879         statement_t *statement = expr->statement;
1880
1881         assert(statement->type == STATEMENT_COMPOUND);
1882         return compound_statement_to_firm((compound_statement_t*) statement);
1883 }
1884
1885 static ir_node *va_start_expression_to_firm(
1886         const va_start_expression_t *const expr)
1887 {
1888         ir_type   *const method_type = get_ir_type(current_function_decl->type);
1889         int        const n           = get_method_n_params(method_type) - 1;
1890         ir_entity *const parm_ent    = get_method_value_param_ent(method_type, n);
1891         ir_node   *const arg_base    = get_irg_value_param_base(current_ir_graph);
1892         dbg_info  *const dbgi        =
1893                 get_dbg_info(&expr->expression.source_position);
1894         ir_node   *const no_mem      = new_NoMem();
1895         ir_node   *const arg_sel     =
1896                 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
1897
1898         size_t     const parm_size   = get_type_size(expr->parameter->type);
1899         ir_node   *const cnst        = new_Const_long(mode_uint, parm_size);
1900         ir_node   *const add         = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
1901         set_value_for_expression(expr->ap, add);
1902
1903         return NULL;
1904 }
1905
1906 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
1907 {
1908         ir_type  *const irtype = get_ir_type(expr->expression.datatype);
1909         ir_node  *const ap     = expression_to_firm(expr->ap);
1910         dbg_info *const dbgi   = get_dbg_info(&expr->expression.source_position);
1911         ir_node  *const res    = deref_address(irtype, ap, dbgi);
1912
1913         size_t     const parm_size   = get_type_size(expr->expression.datatype);
1914         ir_node   *const cnst        = new_Const_long(mode_uint, parm_size);
1915         ir_node   *const add         = new_d_Add(dbgi, ap, cnst, mode_P_data);
1916         set_value_for_expression(expr->ap, add);
1917
1918         return res;
1919 }
1920
1921 static ir_node *dereference_addr(const unary_expression_t *const expression)
1922 {
1923         assert(expression->expression.type == EXPR_UNARY_DEREFERENCE);
1924         return expression_to_firm(expression->value);
1925 }
1926
1927 static ir_node *expression_to_addr(const expression_t *expression)
1928 {
1929         switch(expression->type) {
1930         case EXPR_REFERENCE:
1931                 return reference_addr(&expression->reference);
1932         case EXPR_ARRAY_ACCESS:
1933                 return array_access_addr(&expression->array_access);
1934         case EXPR_SELECT:
1935                 return select_addr(&expression->select);
1936         case EXPR_CALL:
1937                 return call_expression_to_firm(&expression->call);
1938         case EXPR_UNARY_DEREFERENCE: {
1939                 return dereference_addr(&expression->unary);
1940         }
1941         default:
1942                 break;
1943         }
1944         panic("trying to get address of non-lvalue");
1945 }
1946
1947 static ir_node *_expression_to_firm(const expression_t *expression)
1948 {
1949         switch(expression->type) {
1950         case EXPR_CONST:
1951                 return const_to_firm(&expression->conste);
1952         case EXPR_STRING_LITERAL:
1953                 return string_literal_to_firm(&expression->string);
1954         case EXPR_WIDE_STRING_LITERAL:
1955                 return wide_string_literal_to_firm(&expression->wide_string);
1956         case EXPR_REFERENCE:
1957                 return reference_expression_to_firm(&expression->reference);
1958         case EXPR_CALL:
1959                 return call_expression_to_firm(&expression->call);
1960         EXPR_UNARY_CASES
1961                 return unary_expression_to_firm(&expression->unary);
1962         EXPR_BINARY_CASES
1963                 return binary_expression_to_firm(&expression->binary);
1964         case EXPR_ARRAY_ACCESS:
1965                 return array_access_to_firm(&expression->array_access);
1966         case EXPR_SIZEOF:
1967                 return sizeof_to_firm(&expression->sizeofe);
1968         case EXPR_CONDITIONAL:
1969                 return conditional_to_firm(&expression->conditional);
1970         case EXPR_SELECT:
1971                 return select_to_firm(&expression->select);
1972         case EXPR_CLASSIFY_TYPE:
1973                 return classify_type_to_firm(&expression->classify_type);
1974         case EXPR_FUNCTION:
1975         case EXPR_PRETTY_FUNCTION:
1976                 return function_name_to_firm(&expression->string);
1977         case EXPR_STATEMENT:
1978                 return statement_expression_to_firm(&expression->statement);
1979         case EXPR_VA_START:
1980                 return va_start_expression_to_firm(&expression->va_starte);
1981         case EXPR_VA_ARG:
1982                 return va_arg_expression_to_firm(&expression->va_arge);
1983         case EXPR_OFFSETOF:
1984         case EXPR_BUILTIN_SYMBOL:
1985                 panic("unimplemented expression found");
1986
1987         case EXPR_UNKNOWN:
1988         case EXPR_INVALID:
1989                 break;
1990         }
1991         panic("invalid expression found");
1992 }
1993
1994 static ir_node *expression_to_firm(const expression_t *expression)
1995 {
1996         ir_node *res = _expression_to_firm(expression);
1997
1998         if(res != NULL && get_irn_mode(res) == mode_b) {
1999                 ir_mode *mode = get_ir_mode(expression->base.datatype);
2000                 res           = create_conv(NULL, res, mode);
2001         }
2002
2003         return res;
2004 }
2005
2006 static ir_node *expression_to_modeb(const expression_t *expression)
2007 {
2008         ir_node *res = _expression_to_firm(expression);
2009         res          = create_conv(NULL, res, mode_b);
2010
2011         return res;
2012 }
2013
2014 /**
2015  * create a short-circuit expression evaluation that tries to construct
2016  * efficient control flow structures for &&, || and ! expressions
2017  */
2018 static void create_condition_evaluation(const expression_t *expression,
2019                                         ir_node *true_block,
2020                                         ir_node *false_block)
2021 {
2022         switch(expression->type) {
2023         case EXPR_UNARY_NOT: {
2024                 const unary_expression_t *unary_expression = &expression->unary;
2025                 create_condition_evaluation(unary_expression->value, false_block,
2026                                             true_block);
2027                 return;
2028         }
2029         case EXPR_BINARY_LOGICAL_AND: {
2030                 const binary_expression_t *binary_expression = &expression->binary;
2031
2032                 ir_node *cur_block   = get_cur_block();
2033                 ir_node *extra_block = new_immBlock();
2034                 set_cur_block(cur_block);
2035                 create_condition_evaluation(binary_expression->left, extra_block,
2036                                             false_block);
2037                 mature_immBlock(extra_block);
2038                 set_cur_block(extra_block);
2039                 create_condition_evaluation(binary_expression->right, true_block,
2040                                             false_block);
2041                 return;
2042         }
2043         case EXPR_BINARY_LOGICAL_OR: {
2044                 const binary_expression_t *binary_expression = &expression->binary;
2045
2046                 ir_node *cur_block   = get_cur_block();
2047                 ir_node *extra_block = new_immBlock();
2048                 set_cur_block(cur_block);
2049                 create_condition_evaluation(binary_expression->left, true_block,
2050                                             extra_block);
2051                 mature_immBlock(extra_block);
2052                 set_cur_block(extra_block);
2053                 create_condition_evaluation(binary_expression->right, true_block,
2054                                             false_block);
2055                 return;
2056         }
2057         default:
2058                 break;
2059         }
2060
2061         dbg_info *dbgi       = get_dbg_info(&expression->base.source_position);
2062         ir_node  *condition  = expression_to_modeb(expression);
2063         ir_node  *cond       = new_d_Cond(dbgi, condition);
2064         ir_node  *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2065         ir_node  *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2066
2067         add_immBlock_pred(true_block, true_proj);
2068         add_immBlock_pred(false_block, false_proj);
2069
2070         set_cur_block(NULL);
2071 }
2072
2073
2074
2075 static void create_declaration_entity(declaration_t *declaration,
2076                                       declaration_type_t declaration_type,
2077                                       ir_type *parent_type)
2078 {
2079         ident     *const id     = new_id_from_str(declaration->symbol->string);
2080         ir_type   *const irtype = get_ir_type(declaration->type);
2081         dbg_info  *const dbgi   = get_dbg_info(&declaration->source_position);
2082         ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2083         set_entity_ld_ident(entity, id);
2084
2085         declaration->declaration_type = (unsigned char) declaration_type;
2086         declaration->v.entity         = entity;
2087         set_entity_variability(entity, variability_uninitialized);
2088         if(parent_type == get_tls_type())
2089                 set_entity_allocation(entity, allocation_automatic);
2090         else if(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE)
2091                 set_entity_allocation(entity, allocation_static);
2092         /* TODO: visibility? */
2093 }
2094
2095 typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
2096
2097 enum compound_graph_entry_type_t {
2098         COMPOUND_GRAPH_ENTRY_ARRAY,
2099         COMPOUND_GRAPH_ENTRY_COMPOUND
2100 };
2101
2102 struct compound_graph_path_entry_t {
2103         int type;
2104         union {
2105                 ir_entity *entity;
2106                 int        array_index;
2107         } v;
2108         compound_graph_path_entry_t *prev;
2109 };
2110
2111 static void create_initializer_object(initializer_t *initializer, type_t *type,
2112                 ir_entity *entity, compound_graph_path_entry_t *entry, int len);
2113
2114 static compound_graph_path *create_compound_path(ir_type *type,
2115                 compound_graph_path_entry_t *entry, int len)
2116 {
2117         compound_graph_path *path = new_compound_graph_path(type, len);
2118
2119         int i = len - 1;
2120         for( ; entry != NULL; entry = entry->prev, --i) {
2121                 assert(i >= 0);
2122                 if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
2123                         set_compound_graph_path_node(path, i, entry->v.entity);
2124                 } else {
2125                         assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
2126                         set_compound_graph_path_array_index(path, i, entry->v.array_index);
2127                 }
2128         }
2129         assert(i == -1);
2130
2131         return path;
2132 }
2133
2134 static void create_initializer_value(initializer_value_t *initializer,
2135                                      ir_entity *entity,
2136                                      compound_graph_path_entry_t *entry,
2137                                      int len)
2138 {
2139         ir_node             *node = expression_to_firm(initializer->value);
2140         ir_type             *type = get_entity_type(entity);
2141         compound_graph_path *path = create_compound_path(type, entry, len);
2142         add_compound_ent_value_w_path(entity, node, path);
2143 }
2144
2145 static void create_initializer_compound(initializer_list_t *initializer,
2146                                         compound_type_t *type,
2147                                         ir_entity *entity,
2148                                         compound_graph_path_entry_t *last_entry,
2149                                         int len)
2150 {
2151         declaration_t *compound_declaration = type->declaration;
2152
2153         declaration_t *compound_entry = compound_declaration->context.declarations;
2154
2155         compound_graph_path_entry_t entry;
2156         entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
2157         entry.prev = last_entry;
2158         ++len;
2159
2160         size_t i = 0;
2161         for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
2162                 if(compound_entry->symbol == NULL)
2163                         continue;
2164                 if(compound_entry->namespc != NAMESPACE_NORMAL)
2165                         continue;
2166
2167                 if(i >= initializer->len)
2168                         break;
2169
2170                 entry.v.entity = compound_entry->v.entity;
2171
2172                 initializer_t *sub_initializer = initializer->initializers[i];
2173
2174                 assert(compound_entry != NULL);
2175                 assert(compound_entry->declaration_type
2176                                 == DECLARATION_TYPE_COMPOUND_MEMBER);
2177
2178                 if(sub_initializer->type == INITIALIZER_VALUE) {
2179                         create_initializer_value(&sub_initializer->value,
2180                                                  entity, &entry, len);
2181                 } else {
2182                         type_t *entry_type = skip_typeref(compound_entry->type);
2183                         create_initializer_object(sub_initializer, entry_type, entity,
2184                                                   &entry, len);
2185                 }
2186
2187                 ++i;
2188         }
2189 }
2190
2191 static void create_initializer_array(initializer_list_t *initializer,
2192                                      array_type_t *type, ir_entity *entity,
2193                                      compound_graph_path_entry_t *last_entry,
2194                                      int len)
2195 {
2196         type_t *element_type = type->element_type;
2197         element_type         = skip_typeref(element_type);
2198
2199         compound_graph_path_entry_t entry;
2200         entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2201         entry.prev = last_entry;
2202         ++len;
2203
2204         size_t i;
2205         for(i = 0; i < initializer->len; ++i) {
2206                 entry.v.array_index = i;
2207
2208                 initializer_t *sub_initializer = initializer->initializers[i];
2209
2210                 if(sub_initializer->type == INITIALIZER_VALUE) {
2211                         create_initializer_value(&sub_initializer->value,
2212                                                  entity, &entry, len);
2213                 } else {
2214                         create_initializer_object(sub_initializer, element_type, entity,
2215                                                   &entry, len);
2216                 }
2217         }
2218
2219 #if 0
2220         /* TODO: initialize rest... */
2221         if(type->size_expression != NULL) {
2222                 size_t array_len = fold_constant(type->size_expression);
2223                 for( ; i < array_len; ++i) {
2224
2225                 }
2226         }
2227 #endif
2228 }
2229
2230 static void create_initializer_string(initializer_string_t *initializer,
2231                                       array_type_t *type, ir_entity *entity,
2232                                       compound_graph_path_entry_t *last_entry,
2233                                       int len)
2234 {
2235         type_t *element_type = type->element_type;
2236         element_type         = skip_typeref(element_type);
2237
2238         compound_graph_path_entry_t entry;
2239         entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2240         entry.prev = last_entry;
2241         ++len;
2242
2243         ir_type    *irtype  = get_entity_type(entity);
2244         size_t      arr_len = get_array_type_size(type);
2245         const char *p       = initializer->string;
2246         size_t      i       = 0;
2247         for(i = 0; i < arr_len; ++i, ++p) {
2248                 entry.v.array_index = i;
2249
2250                 ir_node             *node = new_Const_long(mode_Bs, *p);
2251                 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2252                 add_compound_ent_value_w_path(entity, node, path);
2253
2254                 if(*p == '\0')
2255                         break;
2256         }
2257 }
2258
2259 static void create_initializer_wide_string(
2260         const initializer_wide_string_t *const initializer, array_type_t *const type,
2261         ir_entity *const entity, compound_graph_path_entry_t *const last_entry,
2262         int len)
2263 {
2264         type_t *element_type = type->element_type;
2265         element_type         = skip_typeref(element_type);
2266
2267         compound_graph_path_entry_t entry;
2268         entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
2269         entry.prev = last_entry;
2270         ++len;
2271
2272         ir_type           *const irtype  = get_entity_type(entity);
2273         const size_t             arr_len = get_array_type_size(type);
2274         const wchar_rep_t *      p       = initializer->string.begin;
2275         const wchar_rep_t *const end     = p + initializer->string.size;
2276         for (size_t i = 0; i < arr_len && p != end; ++i, ++p) {
2277                 entry.v.array_index = i;
2278
2279                 ir_node             *node = new_Const_long(mode_int, *p);
2280                 compound_graph_path *path = create_compound_path(irtype, &entry, len);
2281                 add_compound_ent_value_w_path(entity, node, path);
2282         }
2283 }
2284
2285 static void create_initializer_object(initializer_t *initializer, type_t *type,
2286                 ir_entity *entity, compound_graph_path_entry_t *entry, int len)
2287 {
2288         if(is_type_array(type)) {
2289                 array_type_t *array_type = &type->array;
2290
2291                 switch (initializer->type) {
2292                         case INITIALIZER_STRING: {
2293                                 initializer_string_t *const string = &initializer->string;
2294                                 create_initializer_string(string, array_type, entity, entry, len);
2295                                 return;
2296                         }
2297
2298                         case INITIALIZER_WIDE_STRING: {
2299                                 initializer_wide_string_t *const string = &initializer->wide_string;
2300                                 create_initializer_wide_string(string, array_type, entity, entry, len);
2301                                 return;
2302                         }
2303
2304                         case INITIALIZER_LIST: {
2305                                 initializer_list_t *const list = &initializer->list;
2306                                 create_initializer_array(list, array_type, entity, entry, len);
2307                                 return;
2308                         }
2309
2310                         case INITIALIZER_VALUE:
2311                                 break;
2312                 }
2313                 panic("Unhandled initializer");
2314         } else {
2315                 assert(initializer->type == INITIALIZER_LIST);
2316                 initializer_list_t *list = &initializer->list;
2317
2318                 assert(is_type_compound(type));
2319                 compound_type_t *compound_type = &type->compound;
2320                 create_initializer_compound(list, compound_type, entity, entry, len);
2321         }
2322 }
2323
2324 static void create_initializer_local_variable_entity(declaration_t *declaration)
2325 {
2326         initializer_t *initializer = declaration->init.initializer;
2327         dbg_info      *dbgi        = get_dbg_info(&declaration->source_position);
2328         ir_entity     *entity      = declaration->v.entity;
2329         ir_node       *memory      = get_store();
2330         ir_node       *nomem       = new_NoMem();
2331         ir_node       *frame       = get_irg_frame(current_ir_graph);
2332         ir_node       *addr        = new_d_simpleSel(dbgi, nomem, frame, entity);
2333
2334         if(initializer->type == INITIALIZER_VALUE) {
2335                 initializer_value_t *initializer_value = &initializer->value;
2336
2337                 ir_node *value = expression_to_firm(initializer_value->value);
2338                 type_t  *type  = skip_typeref(declaration->type);
2339                 assign_value(dbgi, addr, type, value);
2340                 return;
2341         }
2342
2343         /* create a "template" entity which is copied to the entity on the stack */
2344         ident     *const id          = unique_ident("initializer");
2345         ir_type   *const irtype      = get_ir_type(declaration->type);
2346         ir_type   *const global_type = get_glob_type();
2347         ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
2348         set_entity_ld_ident(init_entity, id);
2349
2350         set_entity_variability(init_entity, variability_initialized);
2351         set_entity_visibility(init_entity, visibility_local);
2352         set_entity_allocation(init_entity, allocation_static);
2353
2354         ir_graph *const old_current_ir_graph = current_ir_graph;
2355         current_ir_graph = get_const_code_irg();
2356
2357         type_t *const type = skip_typeref(declaration->type);
2358         create_initializer_object(initializer, type, init_entity, NULL, 0);
2359
2360         assert(current_ir_graph == get_const_code_irg());
2361         current_ir_graph = old_current_ir_graph;
2362
2363         ir_node *const src_addr  = create_symconst(dbgi, init_entity);
2364         ir_node *const copyb     = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
2365
2366         ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2367         set_store(copyb_mem);
2368 }
2369
2370 static void create_initializer(declaration_t *declaration)
2371 {
2372         initializer_t *initializer = declaration->init.initializer;
2373         if(initializer == NULL)
2374                 return;
2375
2376         declaration_type_t declaration_type
2377                 = (declaration_type_t) declaration->declaration_type;
2378         if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY) {
2379                 create_initializer_local_variable_entity(declaration);
2380                 return;
2381         }
2382
2383         if(initializer->type == INITIALIZER_VALUE) {
2384                 initializer_value_t *initializer_value = &initializer->value;
2385
2386                 ir_node *value = expression_to_firm(initializer_value->value);
2387
2388                 if(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE) {
2389                         set_value(declaration->v.value_number, value);
2390                 } else {
2391                         assert(declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2392
2393                         ir_entity *entity = declaration->v.entity;
2394
2395                         set_entity_variability(entity, variability_initialized);
2396                         set_atomic_ent_value(entity, value);
2397                 }
2398         } else {
2399                 assert(declaration_type == DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY
2400                                 || declaration_type == DECLARATION_TYPE_GLOBAL_VARIABLE);
2401
2402                 ir_entity *entity = declaration->v.entity;
2403                 set_entity_variability(entity, variability_initialized);
2404
2405                 type_t *type = skip_typeref(declaration->type);
2406                 create_initializer_object(initializer, type, entity, NULL, 0);
2407         }
2408 }
2409
2410 static void create_local_variable(declaration_t *declaration)
2411 {
2412         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2413
2414         bool needs_entity = declaration->address_taken;
2415         type_t *type = skip_typeref(declaration->type);
2416
2417         if(is_type_array(type) || is_type_compound(type)) {
2418                 needs_entity = true;
2419         }
2420
2421         if(needs_entity) {
2422                 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2423                 create_declaration_entity(declaration,
2424                                           DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
2425                                           frame_type);
2426         } else {
2427                 declaration->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
2428                 declaration->v.value_number   = next_value_number_function;
2429                 ++next_value_number_function;
2430         }
2431
2432         create_initializer(declaration);
2433 }
2434
2435 static void create_local_static_variable(declaration_t *declaration)
2436 {
2437         assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
2438
2439         type_t    *const type        = skip_typeref(declaration->type);
2440         ir_type   *const global_type = get_glob_type();
2441         ident     *const id          = unique_ident(declaration->symbol->string);
2442         ir_type   *const irtype      = get_ir_type(type);
2443         dbg_info  *const dbgi        = get_dbg_info(&declaration->source_position);
2444         ir_entity *const entity      = new_d_entity(global_type, id, irtype, dbgi);
2445         set_entity_ld_ident(entity, id);
2446
2447         declaration->declaration_type = DECLARATION_TYPE_GLOBAL_VARIABLE;
2448         declaration->v.entity         = entity;
2449         set_entity_variability(entity, variability_uninitialized);
2450         set_entity_visibility(entity, visibility_local);
2451         set_entity_allocation(entity, allocation_static);
2452
2453         ir_graph *const old_current_ir_graph = current_ir_graph;
2454         current_ir_graph = get_const_code_irg();
2455
2456         create_initializer(declaration);
2457
2458         assert(current_ir_graph == get_const_code_irg());
2459         current_ir_graph = old_current_ir_graph;
2460 }
2461
2462
2463
2464 static void return_statement_to_firm(return_statement_t *statement)
2465 {
2466         if(get_cur_block() == NULL)
2467                 return;
2468
2469         ir_type *func_irtype = get_ir_type(current_function_decl->type);
2470
2471         dbg_info *dbgi  = get_dbg_info(&statement->statement.source_position);
2472
2473         ir_node *in[1];
2474         int      in_len;
2475         if(get_method_n_ress(func_irtype) > 0) {
2476                 ir_type *res_type = get_method_res_type(func_irtype, 0);
2477
2478                 if(statement->return_value != NULL) {
2479                         ir_node *node = expression_to_firm(statement->return_value);
2480                         node  = do_strict_conv(dbgi, node);
2481                         in[0] = node;
2482                 } else {
2483                         ir_mode *mode;
2484                         if(is_compound_type(res_type)) {
2485                                 mode = mode_P_data;
2486                         } else {
2487                                 mode = get_type_mode(res_type);
2488                         }
2489                         in[0] = new_Unknown(mode);
2490                 }
2491                 in_len = 1;
2492         } else {
2493                 /* build return_value for its side effects */
2494                 if(statement->return_value != NULL) {
2495                         expression_to_firm(statement->return_value);
2496                 }
2497                 in_len = 0;
2498         }
2499
2500         ir_node  *store = get_store();
2501         ir_node  *ret   = new_d_Return(dbgi, store, in_len, in);
2502
2503         ir_node *end_block = get_irg_end_block(current_ir_graph);
2504         add_immBlock_pred(end_block, ret);
2505
2506         set_cur_block(NULL);
2507 }
2508
2509 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
2510 {
2511         if(get_cur_block() == NULL)
2512                 return NULL;
2513
2514         return expression_to_firm(statement->expression);
2515 }
2516
2517 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
2518 {
2519         ir_node     *result    = NULL;
2520         statement_t *statement = compound->statements;
2521         for( ; statement != NULL; statement = statement->base.next) {
2522                 //context2firm(&statement->context);
2523
2524                 if(statement->base.next == NULL
2525                                 && statement->type == STATEMENT_EXPRESSION) {
2526                         result = expression_statement_to_firm(
2527                                         (expression_statement_t*) statement);
2528                         break;
2529                 }
2530                 statement_to_firm(statement);
2531         }
2532
2533         return result;
2534 }
2535
2536 static void create_local_declaration(declaration_t *declaration)
2537 {
2538         type_t *type = skip_typeref(declaration->type);
2539
2540         switch ((storage_class_tag_t) declaration->storage_class) {
2541         case STORAGE_CLASS_STATIC:
2542                 create_local_static_variable(declaration);
2543                 return;
2544         case STORAGE_CLASS_ENUM_ENTRY:
2545                 panic("enum entry declaration in local block found");
2546         case STORAGE_CLASS_EXTERN:
2547                 panic("extern declaration in local block found");
2548         case STORAGE_CLASS_NONE:
2549         case STORAGE_CLASS_AUTO:
2550         case STORAGE_CLASS_REGISTER:
2551                 if(is_type_function(type)) {
2552                         panic("nested functions not supported yet");
2553                 } else {
2554                         create_local_variable(declaration);
2555                 }
2556                 return;
2557         case STORAGE_CLASS_TYPEDEF:
2558         case STORAGE_CLASS_THREAD:
2559         case STORAGE_CLASS_THREAD_EXTERN:
2560         case STORAGE_CLASS_THREAD_STATIC:
2561                 return;
2562         }
2563         panic("invalid storage class found");
2564 }
2565
2566 static void declaration_statement_to_firm(declaration_statement_t *statement)
2567 {
2568         declaration_t *declaration = statement->declarations_begin;
2569         declaration_t *end         = statement->declarations_end->next;
2570         for( ; declaration != end; declaration = declaration->next) {
2571                 create_local_variable(declaration);
2572         }
2573 }
2574
2575 static void if_statement_to_firm(if_statement_t *statement)
2576 {
2577         ir_node *cur_block = get_cur_block();
2578
2579         ir_node *fallthrough_block = new_immBlock();
2580
2581         /* the true (blocks) */
2582         ir_node *true_block;
2583         if (statement->true_statement != NULL) {
2584                 true_block = new_immBlock();
2585                 statement_to_firm(statement->true_statement);
2586                 if(get_cur_block() != NULL) {
2587                         ir_node *jmp = new_Jmp();
2588                         add_immBlock_pred(fallthrough_block, jmp);
2589                 }
2590         } else {
2591                 true_block = fallthrough_block;
2592         }
2593
2594         /* the false (blocks) */
2595         ir_node *false_block;
2596         if(statement->false_statement != NULL) {
2597                 false_block = new_immBlock();
2598
2599                 statement_to_firm(statement->false_statement);
2600                 if(get_cur_block() != NULL) {
2601                         ir_node *jmp = new_Jmp();
2602                         add_immBlock_pred(fallthrough_block, jmp);
2603                 }
2604         } else {
2605                 false_block = fallthrough_block;
2606         }
2607
2608         /* create the condition */
2609         if(cur_block != NULL) {
2610                 set_cur_block(cur_block);
2611                 create_condition_evaluation(statement->condition, true_block,
2612                                             false_block);
2613         }
2614
2615         mature_immBlock(true_block);
2616         if(false_block != fallthrough_block) {
2617                 mature_immBlock(false_block);
2618         }
2619         mature_immBlock(fallthrough_block);
2620
2621         set_cur_block(fallthrough_block);
2622 }
2623
2624 static void while_statement_to_firm(while_statement_t *statement)
2625 {
2626         ir_node *jmp = NULL;
2627         if(get_cur_block() != NULL) {
2628                 jmp = new_Jmp();
2629         }
2630
2631         /* create the header block */
2632         ir_node *header_block = new_immBlock();
2633         if(jmp != NULL) {
2634                 add_immBlock_pred(header_block, jmp);
2635         }
2636
2637         /* the false block */
2638         ir_node *false_block = new_immBlock();
2639
2640         /* the loop body */
2641         ir_node *body_block;
2642         if (statement->body != NULL) {
2643                 ir_node *old_continue_label = continue_label;
2644                 ir_node *old_break_label    = break_label;
2645                 continue_label              = header_block;
2646                 break_label                 = false_block;
2647
2648                 body_block = new_immBlock();
2649                 statement_to_firm(statement->body);
2650
2651                 assert(continue_label == header_block);
2652                 assert(break_label    == false_block);
2653                 continue_label = old_continue_label;
2654                 break_label    = old_break_label;
2655
2656                 if(get_cur_block() != NULL) {
2657                         jmp = new_Jmp();
2658                         add_immBlock_pred(header_block, jmp);
2659                 }
2660         } else {
2661                 body_block = header_block;
2662         }
2663
2664         /* create the condition */
2665         set_cur_block(header_block);
2666
2667         create_condition_evaluation(statement->condition, body_block, false_block);
2668         mature_immBlock(body_block);
2669         mature_immBlock(false_block);
2670         mature_immBlock(header_block);
2671
2672         set_cur_block(false_block);
2673 }
2674
2675 static void do_while_statement_to_firm(do_while_statement_t *statement)
2676 {
2677         ir_node *jmp = NULL;
2678         if(get_cur_block() != NULL) {
2679                 jmp = new_Jmp();
2680         }
2681
2682         /* create the header block */
2683         ir_node *header_block = new_immBlock();
2684
2685         /* the false block */
2686         ir_node *false_block = new_immBlock();
2687
2688         /* the loop body */
2689         ir_node *body_block = new_immBlock();
2690         if(jmp != NULL) {
2691                 add_immBlock_pred(body_block, jmp);
2692         }
2693
2694         if (statement->body != NULL) {
2695                 ir_node *old_continue_label = continue_label;
2696                 ir_node *old_break_label    = break_label;
2697                 continue_label              = header_block;
2698                 break_label                 = false_block;
2699
2700                 statement_to_firm(statement->body);
2701
2702                 assert(continue_label == header_block);
2703                 assert(break_label    == false_block);
2704                 continue_label = old_continue_label;
2705                 break_label    = old_break_label;
2706
2707                 if (get_cur_block() == NULL) {
2708                         mature_immBlock(header_block);
2709                         mature_immBlock(body_block);
2710                         mature_immBlock(false_block);
2711                         return;
2712                 }
2713         }
2714
2715         ir_node *body_jmp = new_Jmp();
2716         add_immBlock_pred(header_block, body_jmp);
2717         mature_immBlock(header_block);
2718
2719         /* create the condition */
2720         set_cur_block(header_block);
2721
2722         create_condition_evaluation(statement->condition, body_block, false_block);
2723         mature_immBlock(body_block);
2724         mature_immBlock(false_block);
2725         mature_immBlock(header_block);
2726
2727         set_cur_block(false_block);
2728 }
2729
2730 static void for_statement_to_firm(for_statement_t *statement)
2731 {
2732         ir_node *jmp = NULL;
2733         if (get_cur_block() != NULL) {
2734                 if(statement->initialisation != NULL) {
2735                         expression_to_firm(statement->initialisation);
2736                 }
2737
2738                 /* create declarations */
2739                 declaration_t *declaration = statement->context.declarations;
2740                 for( ; declaration != NULL; declaration = declaration->next) {
2741                         create_local_declaration(declaration);
2742                 }
2743
2744                 jmp = new_Jmp();
2745         }
2746
2747
2748         /* create the step block */
2749         ir_node *const step_block = new_immBlock();
2750         if (statement->step != NULL) {
2751                 expression_to_firm(statement->step);
2752         }
2753         ir_node *const step_jmp = new_Jmp();
2754
2755         /* create the header block */
2756         ir_node *const header_block = new_immBlock();
2757         if (jmp != NULL) {
2758                 add_immBlock_pred(header_block, jmp);
2759         }
2760         add_immBlock_pred(header_block, step_jmp);
2761
2762         /* the false block */
2763         ir_node *const false_block = new_immBlock();
2764
2765         /* the loop body */
2766         ir_node * body_block;
2767         if (statement->body != NULL) {
2768                 ir_node *const old_continue_label = continue_label;
2769                 ir_node *const old_break_label    = break_label;
2770                 continue_label = step_block;
2771                 break_label    = false_block;
2772
2773                 body_block = new_immBlock();
2774                 statement_to_firm(statement->body);
2775
2776                 assert(continue_label == step_block);
2777                 assert(break_label    == false_block);
2778                 continue_label = old_continue_label;
2779                 break_label    = old_break_label;
2780
2781                 if (get_cur_block() != NULL) {
2782                         jmp = new_Jmp();
2783                         add_immBlock_pred(step_block, jmp);
2784                 }
2785         } else {
2786                 body_block = step_block;
2787         }
2788
2789         /* create the condition */
2790         set_cur_block(header_block);
2791         if (statement->condition != NULL) {
2792                 create_condition_evaluation(statement->condition, body_block,
2793                                             false_block);
2794         } else {
2795                 keep_alive(header_block);
2796                 jmp = new_Jmp();
2797                 add_immBlock_pred(body_block, jmp);
2798         }
2799
2800         mature_immBlock(body_block);
2801         mature_immBlock(false_block);
2802         mature_immBlock(step_block);
2803         mature_immBlock(header_block);
2804         mature_immBlock(false_block);
2805
2806         set_cur_block(false_block);
2807 }
2808
2809 static void create_jump_statement(const statement_t *statement,
2810                                   ir_node *target_block)
2811 {
2812         if(get_cur_block() == NULL)
2813                 return;
2814
2815         dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
2816         ir_node  *jump = new_d_Jmp(dbgi);
2817         add_immBlock_pred(target_block, jump);
2818
2819         set_cur_block(NULL);
2820 }
2821
2822 static void switch_statement_to_firm(const switch_statement_t *statement)
2823 {
2824         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2825
2826         ir_node *expression  = expression_to_firm(statement->expression);
2827         ir_node *cond        = new_d_Cond(dbgi, expression);
2828         ir_node *break_block = new_immBlock();
2829
2830         set_cur_block(NULL);
2831
2832         ir_node *const old_switch_cond       = current_switch_cond;
2833         ir_node *const old_break_label       = break_label;
2834         const bool     old_saw_default_label = saw_default_label;
2835         current_switch_cond                  = cond;
2836         break_label                          = break_block;
2837
2838         statement_to_firm(statement->body);
2839
2840         if(get_cur_block() != NULL) {
2841                 ir_node *jmp = new_Jmp();
2842                 add_immBlock_pred(break_block, jmp);
2843         }
2844
2845         if (!saw_default_label) {
2846                 set_cur_block(get_nodes_block(cond));
2847                 ir_node *const proj = new_d_defaultProj(dbgi, cond,
2848                                                         MAGIC_DEFAULT_PN_NUMBER);
2849                 add_immBlock_pred(break_block, proj);
2850         }
2851
2852         assert(current_switch_cond == cond);
2853         assert(break_label         == break_block);
2854         current_switch_cond = old_switch_cond;
2855         break_label         = old_break_label;
2856         saw_default_label   = old_saw_default_label;
2857
2858         mature_immBlock(break_block);
2859         set_cur_block(break_block);
2860 }
2861
2862 static void case_label_to_firm(const case_label_statement_t *statement)
2863 {
2864         dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
2865
2866         ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
2867
2868         /* let's create a node and hope firm constant folding creates a Const
2869          * node... */
2870         ir_node *proj;
2871         set_cur_block(get_nodes_block(current_switch_cond));
2872         if(statement->expression) {
2873                 long pn = fold_constant(statement->expression);
2874                 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
2875                         /* oops someone detected our cheating... */
2876                         panic("magic default pn used");
2877                 }
2878                 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
2879         } else {
2880                 saw_default_label = true;
2881                 proj = new_d_defaultProj(dbgi, current_switch_cond,
2882                                          MAGIC_DEFAULT_PN_NUMBER);
2883         }
2884
2885         ir_node *block = new_immBlock();
2886         if (fallthrough != NULL) {
2887                 add_immBlock_pred(block, fallthrough);
2888         }
2889         add_immBlock_pred(block, proj);
2890         mature_immBlock(block);
2891
2892         statement_to_firm(statement->label_statement);
2893 }
2894
2895 static ir_node *get_label_block(declaration_t *label)
2896 {
2897         assert(label->namespc == NAMESPACE_LABEL);
2898
2899         if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
2900                 return label->v.block;
2901         }
2902         assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
2903
2904         ir_node *old_cur_block = get_cur_block();
2905         ir_node *block         = new_immBlock();
2906         set_cur_block(old_cur_block);
2907
2908         label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
2909         label->v.block          = block;
2910
2911         ARR_APP1(ir_node *, imature_blocks, block);
2912
2913         return block;
2914 }
2915
2916 static void label_to_firm(const label_statement_t *statement)
2917 {
2918         ir_node *block = get_label_block(statement->label);
2919
2920         if(get_cur_block() != NULL) {
2921                 ir_node *jmp = new_Jmp();
2922                 add_immBlock_pred(block, jmp);
2923         }
2924
2925         set_cur_block(block);
2926         keep_alive(block);
2927
2928         statement_to_firm(statement->label_statement);
2929 }
2930
2931 static void goto_to_firm(const goto_statement_t *statement)
2932 {
2933         if(get_cur_block() == NULL)
2934                 return;
2935
2936         ir_node *block = get_label_block(statement->label);
2937         ir_node *jmp   = new_Jmp();
2938         add_immBlock_pred(block, jmp);
2939
2940         set_cur_block(NULL);
2941 }
2942
2943 typedef enum modifier_t {
2944         ASM_MODIFIER_WRITE_ONLY   = 1 << 0,
2945         ASM_MODIFIER_READ_WRITE   = 1 << 1,
2946         ASM_MODIFIER_COMMUTATIVE  = 1 << 2,
2947         ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
2948 } modifier_t;
2949
2950 #if 0
2951 static void asm_statement_to_firm(const asm_statement_t *statement)
2952 {
2953         bool needs_memory = false;
2954
2955         size_t         n_clobbers = 0;
2956         asm_clobber_t *clobber    = statement->clobbers;
2957         for( ; clobber != NULL; clobber = clobber->next) {
2958                 if(strcmp(clobber->clobber, "memory") == 0) {
2959                         needs_memory = true;
2960                         continue;
2961                 }
2962
2963                 ident *id = new_id_from_str(clobber->clobber);
2964                 obstack_ptr_grow(&asm_obst, id);
2965                 ++n_clobbers;
2966         }
2967         assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
2968         ident **clobbers = NULL;
2969         if(n_clobbers > 0) {
2970                 clobbers = obstack_finish(&asm_obst);
2971         }
2972
2973         /* find and count input and output constraints */
2974         asm_constraint_t *constraint = statement->inputs;
2975         for( ; constraint != NULL; constraint = constraint->next) {
2976                 int  modifiers      = 0;
2977                 bool supports_memop = false;
2978                 for(const char *c = constraint->constraints; *c != 0; ++c) {
2979                         /* TODO: improve error messages */
2980                         switch(*c) {
2981                         case '?':
2982                         case '!':
2983                                 panic("multiple alternative assembler constraints not "
2984                                       "supported");
2985                         case 'm':
2986                         case 'o':
2987                         case 'V':
2988                         case '<':
2989                         case '>':
2990                         case 'X':
2991                                 supports_memop = true;
2992                                 obstack_1grow(&asm_obst, *c);
2993                                 break;
2994                         case '=':
2995                                 if(modifiers & ASM_MODIFIER_READ_WRITE)
2996                                         panic("inconsistent register constraints");
2997                                 modifiers |= ASM_MODIFIER_WRITE_ONLY;
2998                                 break;
2999                         case '+':
3000                                 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
3001                                         panic("inconsistent register constraints");
3002                                 modifiers |= ASM_MODIFIER_READ_WRITE;
3003                                 break;
3004                         case '&':
3005                                 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
3006                                 panic("early clobber assembler constraint not supported yet");
3007                                 break;
3008                         case '%':
3009                                 modifiers |= ASM_MODIFIER_COMMUTATIVE;
3010                                 panic("commutative assembler constraint not supported yet");
3011                                 break;
3012                         case '#':
3013                                 /* skip register preferences stuff... */
3014                                 while(*c != 0 && *c != ',')
3015                                         ++c;
3016                                 break;
3017                         case '*':
3018                                 /* skip register preferences stuff... */
3019                                 ++c;
3020                                 break;
3021                         default:
3022                                 obstack_1grow(&asm_obst, *c);
3023                                 break;
3024                         }
3025                 }
3026                 obstack_1grow(&asm_obst, '\0');
3027                 const char *constraint_string = obstack_finish(&asm_obst);
3028
3029                 needs_memory |= supports_memop;
3030                 if(supports_memop) {
3031
3032                 }
3033         }
3034
3035 }
3036 #endif
3037
3038 static void statement_to_firm(statement_t *statement)
3039 {
3040         switch(statement->type) {
3041         case STATEMENT_INVALID:
3042                 panic("invalid statement found");
3043         case STATEMENT_COMPOUND:
3044                 compound_statement_to_firm(&statement->compound);
3045                 return;
3046         case STATEMENT_RETURN:
3047                 return_statement_to_firm(&statement->returns);
3048                 return;
3049         case STATEMENT_EXPRESSION:
3050                 expression_statement_to_firm(&statement->expression);
3051                 return;
3052         case STATEMENT_IF:
3053                 if_statement_to_firm(&statement->ifs);
3054                 return;
3055         case STATEMENT_WHILE:
3056                 while_statement_to_firm(&statement->whiles);
3057                 return;
3058         case STATEMENT_DO_WHILE:
3059                 do_while_statement_to_firm(&statement->do_while);
3060                 return;
3061         case STATEMENT_DECLARATION:
3062                 declaration_statement_to_firm(&statement->declaration);
3063                 return;
3064         case STATEMENT_BREAK:
3065                 create_jump_statement(statement, break_label);
3066                 return;
3067         case STATEMENT_CONTINUE:
3068                 create_jump_statement(statement, continue_label);
3069                 return;
3070         case STATEMENT_SWITCH:
3071                 switch_statement_to_firm(&statement->switchs);
3072                 return;
3073         case STATEMENT_CASE_LABEL:
3074                 case_label_to_firm(&statement->case_label);
3075                 return;
3076         case STATEMENT_FOR:
3077                 for_statement_to_firm(&statement->fors);
3078                 return;
3079         case STATEMENT_LABEL:
3080                 label_to_firm(&statement->label);
3081                 return;
3082         case STATEMENT_GOTO:
3083                 goto_to_firm(&statement->gotos);
3084                 return;
3085         case STATEMENT_ASM:
3086                 //asm_statement_to_firm(&statement->asms);
3087                 //return;
3088                 break;
3089         }
3090         panic("Statement not implemented\n");
3091 }
3092
3093 static int count_local_declarations(const declaration_t *      decl,
3094                                     const declaration_t *const end)
3095 {
3096         int count = 0;
3097         for (; decl != end; decl = decl->next) {
3098                 const type_t *type = skip_typeref(decl->type);
3099                 switch (type->type) {
3100                         case TYPE_ATOMIC:
3101                         case TYPE_ENUM:
3102                         case TYPE_POINTER:
3103                                 if (!decl->address_taken)
3104                                         ++count;
3105                                 break;
3106
3107                         default: break;
3108                 }
3109         }
3110         return count;
3111 }
3112
3113 static int count_decls_in_expr(const expression_t *expr) {
3114         if (expr->base.type == EXPR_STATEMENT)
3115                 return count_decls_in_stmts(expr->statement.statement);
3116         return 0;
3117 }
3118
3119 static int count_decls_in_stmts(const statement_t *stmt)
3120 {
3121         int count = 0;
3122         for (; stmt != NULL; stmt = stmt->base.next) {
3123                 switch (stmt->type) {
3124                         case STATEMENT_DECLARATION: {
3125                                 const declaration_statement_t *const decl_stmt = &stmt->declaration;
3126                                 count += count_local_declarations(decl_stmt->declarations_begin,
3127                                                                   decl_stmt->declarations_end->next);
3128                                 break;
3129                         }
3130
3131                         case STATEMENT_COMPOUND: {
3132                                 const compound_statement_t *const comp =
3133                                         (const compound_statement_t*)stmt;
3134                                 count += count_decls_in_stmts(comp->statements);
3135                                 break;
3136                         }
3137
3138                         case STATEMENT_IF: {
3139                                 const if_statement_t *const if_stmt = &stmt->ifs;
3140                                 count += count_decls_in_expr(if_stmt->condition);
3141                                 count += count_decls_in_stmts(if_stmt->true_statement);
3142                                 count += count_decls_in_stmts(if_stmt->false_statement);
3143                                 break;
3144                         }
3145
3146                         case STATEMENT_SWITCH: {
3147                                 const switch_statement_t *const switch_stmt = &stmt->switchs;
3148                                 count += count_decls_in_expr(switch_stmt->expression);
3149                                 count += count_decls_in_stmts(switch_stmt->body);
3150                                 break;
3151                         }
3152
3153                         case STATEMENT_LABEL: {
3154                                 const label_statement_t *const label_stmt = &stmt->label;
3155                                 count += count_decls_in_stmts(label_stmt->label_statement);
3156                                 break;
3157                         }
3158
3159                         case STATEMENT_WHILE: {
3160                                 const while_statement_t *const while_stmt = &stmt->whiles;
3161                                 count += count_decls_in_expr(while_stmt->condition);
3162                                 count += count_decls_in_stmts(while_stmt->body);
3163                                 break;
3164                         }
3165
3166                         case STATEMENT_DO_WHILE: {
3167                                 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
3168                                 count += count_decls_in_expr(do_while_stmt->condition);
3169                                 count += count_decls_in_stmts(do_while_stmt->body);
3170                                 break;
3171                         }
3172
3173                         case STATEMENT_FOR: {
3174                                 const for_statement_t *const for_stmt = &stmt->fors;
3175                                 count += count_local_declarations(for_stmt->context.declarations, NULL);
3176                                 count += count_decls_in_expr(for_stmt->initialisation);
3177                                 count += count_decls_in_expr(for_stmt->condition);
3178                                 count += count_decls_in_expr(for_stmt->step);
3179                                 count += count_decls_in_stmts(for_stmt->body);
3180                                 break;
3181                         }
3182
3183                         case STATEMENT_ASM:
3184                         case STATEMENT_BREAK:
3185                         case STATEMENT_CASE_LABEL:
3186                         case STATEMENT_CONTINUE:
3187                                 break;
3188
3189                         case STATEMENT_EXPRESSION: {
3190                                 const expression_statement_t *expr_stmt = &stmt->expression;
3191                                 count += count_decls_in_expr(expr_stmt->expression);
3192                                 break;
3193                         }
3194
3195                         case STATEMENT_GOTO:
3196                         case STATEMENT_INVALID:
3197                                 break;
3198
3199                         case STATEMENT_RETURN: {
3200                                 const return_statement_t *ret_stmt = &stmt->returns;
3201                                 count += count_decls_in_expr(ret_stmt->return_value);
3202                                 break;
3203                         }
3204                 }
3205         }
3206         return count;
3207 }
3208
3209 static int get_function_n_local_vars(declaration_t *declaration)
3210 {
3211         int count = 0;
3212
3213         /* count parameters */
3214         count += count_local_declarations(declaration->context.declarations, NULL);
3215
3216         /* count local variables declared in body */
3217         count += count_decls_in_stmts(declaration->init.statement);
3218
3219         return count;
3220 }
3221
3222 static void initialize_function_parameters(declaration_t *declaration)
3223 {
3224         ir_graph        *irg             = current_ir_graph;
3225         ir_node         *args            = get_irg_args(irg);
3226         ir_node         *start_block     = get_irg_start_block(irg);
3227         ir_type         *function_irtype = get_ir_type(declaration->type);
3228
3229         int            n         = 0;
3230         declaration_t *parameter = declaration->context.declarations;
3231         for( ; parameter != NULL; parameter = parameter->next, ++n) {
3232                 assert(parameter->declaration_type == DECLARATION_TYPE_UNKNOWN);
3233                 type_t *type = skip_typeref(parameter->type);
3234
3235                 bool needs_entity = parameter->address_taken;
3236                 assert(!is_type_array(type));
3237                 if(is_type_compound(type)) {
3238                         needs_entity = true;
3239                 }
3240
3241                 if(needs_entity) {
3242                         ir_entity *entity = get_method_value_param_ent(function_irtype, n);
3243                         ident     *id     = new_id_from_str(parameter->symbol->string);
3244                         set_entity_ident(entity, id);
3245
3246                         parameter->declaration_type
3247                                 = DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY;
3248                         parameter->v.entity = entity;
3249                         continue;
3250                 }
3251
3252                 ir_mode *mode = get_ir_mode(parameter->type);
3253                 long     pn   = n;
3254                 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
3255
3256                 parameter->declaration_type = DECLARATION_TYPE_LOCAL_VARIABLE;
3257                 parameter->v.value_number   = next_value_number_function;
3258                 ++next_value_number_function;
3259
3260                 set_value(parameter->v.value_number, proj);
3261         }
3262 }
3263
3264 /**
3265  * Handle additional decl modifiers for IR-graphs
3266  *
3267  * @param irg            the IR-graph
3268  * @param dec_modifiers  additional modifiers
3269  */
3270 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
3271 {
3272         if (decl_modifiers & DM_NORETURN) {
3273                 /* TRUE if the declaration includes the Microsoft
3274                    __declspec(noreturn) specifier. */
3275                 set_irg_additional_property(irg, mtp_property_noreturn);
3276         }
3277         if (decl_modifiers & DM_NOTHROW) {
3278                 /* TRUE if the declaration includes the Microsoft
3279                    __declspec(nothrow) specifier. */
3280                 set_irg_additional_property(irg, mtp_property_nothrow);
3281         }
3282         if (decl_modifiers & DM_NAKED) {
3283                 /* TRUE if the declaration includes the Microsoft
3284                    __declspec(naked) specifier. */
3285                 set_irg_additional_property(irg, mtp_property_naked);
3286         }
3287         if (decl_modifiers & DM_FORCEINLINE) {
3288                 /* TRUE if the declaration includes the
3289                    Microsoft __forceinline specifier. */
3290                 set_irg_inline_property(irg, irg_inline_forced);
3291         }
3292         if (decl_modifiers & DM_NOINLINE) {
3293                 /* TRUE if the declaration includes the Microsoft
3294                    __declspec(noinline) specifier. */
3295                 set_irg_inline_property(irg, irg_inline_forbidden);
3296         }
3297 }
3298
3299 static void create_function(declaration_t *declaration)
3300 {
3301         ir_entity *function_entity = get_function_entity(declaration);
3302
3303         if(declaration->init.statement == NULL)
3304                 return;
3305
3306         current_function_decl = declaration;
3307         current_function_name = NULL;
3308
3309         assert(imature_blocks == NULL);
3310         imature_blocks = NEW_ARR_F(ir_node*, 0);
3311
3312         int       n_local_vars = get_function_n_local_vars(declaration);
3313         ir_graph *irg          = new_ir_graph(function_entity, n_local_vars);
3314         ir_node  *first_block  = get_cur_block();
3315
3316         /* set inline flags */
3317         if (declaration->is_inline)
3318         set_irg_inline_property(irg, irg_inline_recomended);
3319     handle_decl_modifier_irg(irg, declaration->decl_modifiers);
3320
3321         next_value_number_function = 0;
3322         initialize_function_parameters(declaration);
3323
3324         statement_to_firm(declaration->init.statement);
3325
3326         ir_node *end_block = get_irg_end_block(irg);
3327
3328         /* do we have a return statement yet? */
3329         if(get_cur_block() != NULL) {
3330                 type_t *type = skip_typeref(declaration->type);
3331                 assert(is_type_function(type));
3332                 const function_type_t *func_type   = &type->function;
3333                 const type_t          *return_type
3334                         = skip_typeref(func_type->return_type);
3335
3336                 ir_node *ret;
3337                 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
3338                         ret = new_Return(get_store(), 0, NULL);
3339                 } else {
3340                         ir_mode *mode;
3341                         if(is_type_scalar(return_type)) {
3342                                 mode = get_ir_mode(func_type->return_type);
3343                         } else {
3344                                 mode = mode_P_data;
3345                         }
3346
3347                         ir_node *in[1];
3348                         /* ยง5.1.2.2.3 main implicitly returns 0 */
3349                         if (strcmp(declaration->symbol->string, "main") == 0) {
3350                                 in[0] = new_Const(mode, get_mode_null(mode));
3351                         } else {
3352                                 in[0] = new_Unknown(mode);
3353                         }
3354                         ret = new_Return(get_store(), 1, in);
3355                 }
3356                 add_immBlock_pred(end_block, ret);
3357         }
3358
3359         for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
3360                 mature_immBlock(imature_blocks[i]);
3361         }
3362         DEL_ARR_F(imature_blocks);
3363         imature_blocks = NULL;
3364
3365         mature_immBlock(first_block);
3366         mature_immBlock(end_block);
3367
3368         irg_finalize_cons(irg);
3369
3370         /* finalize the frame type */
3371         ir_type *frame_type = get_irg_frame_type(irg);
3372         int      n          = get_compound_n_members(frame_type);
3373         int      align_all  = 4;
3374         int      offset     = 0;
3375         for(int i = 0; i < n; ++i) {
3376                 ir_entity *entity      = get_compound_member(frame_type, i);
3377                 ir_type   *entity_type = get_entity_type(entity);
3378
3379                 int align = get_type_alignment_bytes(entity_type);
3380                 if(align > align_all)
3381                         align_all = align;
3382                 int misalign = 0;
3383                 if(align > 0) {
3384                         misalign  = offset % align;
3385                         if(misalign > 0) {
3386                                 offset += align - misalign;
3387                         }
3388                 }
3389
3390                 set_entity_offset(entity, offset);
3391                 offset += get_type_size_bytes(entity_type);
3392         }
3393         set_type_size_bytes(frame_type, offset);
3394         set_type_alignment_bytes(frame_type, align_all);
3395         set_type_state(frame_type, layout_fixed);
3396
3397         irg_vrfy(irg);
3398 }
3399
3400 static void create_global_variable(declaration_t *declaration)
3401 {
3402         ir_visibility  vis;
3403         ir_type       *var_type;
3404         switch ((storage_class_tag_t)declaration->storage_class) {
3405                 case STORAGE_CLASS_STATIC:
3406                         vis = visibility_local;
3407                         goto global_var;
3408
3409                 case STORAGE_CLASS_EXTERN:
3410                         vis = visibility_external_allocated;
3411                         goto global_var;
3412
3413                 case STORAGE_CLASS_NONE:
3414                         vis = visibility_external_visible;
3415                         goto global_var;
3416
3417                 case STORAGE_CLASS_THREAD:
3418                         vis = visibility_external_visible;
3419                         goto tls_var;
3420
3421                 case STORAGE_CLASS_THREAD_EXTERN:
3422                         vis = visibility_external_allocated;
3423                         goto tls_var;
3424
3425                 case STORAGE_CLASS_THREAD_STATIC:
3426                         vis = visibility_local;
3427                         goto tls_var;
3428
3429 tls_var:
3430                         var_type = get_tls_type();
3431                         goto create_var;
3432
3433 global_var:
3434                         var_type = get_glob_type();
3435                         goto create_var;
3436
3437 create_var:
3438                         create_declaration_entity(declaration, DECLARATION_TYPE_GLOBAL_VARIABLE,
3439                                                   var_type);
3440                         set_entity_visibility(declaration->v.entity, vis);
3441
3442                         current_ir_graph = get_const_code_irg();
3443                         create_initializer(declaration);
3444                         return;
3445
3446                 case STORAGE_CLASS_TYPEDEF:
3447                 case STORAGE_CLASS_AUTO:
3448                 case STORAGE_CLASS_REGISTER:
3449                 case STORAGE_CLASS_ENUM_ENTRY:
3450                         break;
3451         }
3452         panic("Invalid storage class for global variable");
3453 }
3454
3455 static void context_to_firm(context_t *context)
3456 {
3457         /* first pass: create declarations */
3458         declaration_t *declaration = context->declarations;
3459         for( ; declaration != NULL; declaration = declaration->next) {
3460                 if(declaration->namespc != NAMESPACE_NORMAL)
3461                         continue;
3462                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3463                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3464                         continue;
3465                 if(declaration->symbol == NULL)
3466                         continue;
3467
3468                 type_t *type = skip_typeref(declaration->type);
3469                 if(is_type_function(type)) {
3470                         get_function_entity(declaration);
3471                 } else {
3472                         create_global_variable(declaration);
3473                 }
3474         }
3475
3476         /* second pass: create code */
3477         declaration = context->declarations;
3478         for( ; declaration != NULL; declaration = declaration->next) {
3479                 if(declaration->namespc != NAMESPACE_NORMAL)
3480                         continue;
3481                 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
3482                                 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
3483                         continue;
3484                 if(declaration->symbol == NULL)
3485                         continue;
3486
3487                 type_t *type = declaration->type;
3488                 if(type->type != TYPE_FUNCTION)
3489                         continue;
3490
3491                 create_function(declaration);
3492         }
3493 }
3494
3495 void init_ast2firm(void)
3496 {
3497         obstack_init(&asm_obst);
3498         init_atomic_modes();
3499 }
3500
3501 void exit_ast2firm(void)
3502 {
3503         obstack_free(&asm_obst, NULL);
3504 }
3505
3506 void translation_unit_to_firm(translation_unit_t *unit)
3507 {
3508         type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3509         type_void       = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
3510         type_int        = make_atomic_type(ATOMIC_TYPE_INT,  TYPE_QUALIFIER_NONE);
3511
3512         ir_type_int        = get_ir_type(type_int);
3513         ir_type_const_char = get_ir_type(type_const_char);
3514         ir_type_wchar_t    = get_ir_type(type_wchar_t);
3515         ir_type_void       = get_ir_type(type_int); /* we don't have a real void
3516                                                        type in firm */
3517
3518         type_void->base.firm_type = ir_type_void;
3519
3520         /* just to be sure */
3521         continue_label      = NULL;
3522         break_label         = NULL;
3523         current_switch_cond = NULL;
3524
3525         context_to_firm(& unit->context);
3526 }