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