more work on firm backend
[cparser] / ast2firm.c
1 #include <config.h>
2
3 #define _GNU_SOURCE
4
5 #include <assert.h>
6 #include <libfirm/firm.h>
7 #include <libfirm/adt/obst.h>
8
9 #include "adt/error.h"
10 #include "token_t.h"
11 #include "type_t.h"
12 #include "ast_t.h"
13
14 static ir_type *ir_type_const_char;
15 static ir_type *ir_type_void;
16 static ir_type *ir_type_int;
17 static ir_type *ir_type_void_ptr;
18
19 static type_t *type_const_char;
20 static type_t *type_void;
21 static type_t *type_int;
22
23 typedef struct type2firm_env_t type2firm_env_t;
24 struct type2firm_env_t {
25         int can_cache;       /* nonzero if type can safely be cached because
26                                 no typevariables are in the hierarchy */
27 };
28
29 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type);
30 static ir_type *get_ir_type(type_t *type);
31
32 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
33 {
34         (void) pos;
35 #if 0
36         const declaration_t *declaration = & value_numbers[pos]->declaration;
37
38         print_warning_prefix(declaration->source_position);
39         fprintf(stderr, "variable '%s' might be used uninitialized\n",
40                         declaration->symbol->string);
41 #endif
42         fprintf(stderr, "Some variable might be used uninitialized\n");
43         return new_r_Unknown(irg, mode);
44 }
45
46 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
47 {
48         const source_position_t *pos = (const source_position_t*) dbg;
49         if(pos == NULL)
50                 return 0;
51         return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
52                                    pos->linenr);
53 }
54
55 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
56 {
57         const source_position_t *pos = (const source_position_t*) dbg;
58         if(pos == NULL)
59                 return NULL;
60         if(line != NULL)
61                 *line = pos->linenr;
62         return pos->input_name;
63 }
64
65 void init_ast2firm(void)
66 {
67         type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
68         type_void       = make_atomic_type(ATOMIC_TYPE_VOID, 0);
69         type_int        = make_atomic_type(ATOMIC_TYPE_INT, 0);
70
71         ir_type_int        = get_ir_type(type_int);
72         ir_type_const_char = get_ir_type(type_const_char);
73         ir_type_void       = get_ir_type(type_int); /* we don't have a real void
74                                                        type in firm */
75         ir_type_void_ptr   = new_type_pointer(new_id_from_str("void_ptr"),
76                                               ir_type_void, mode_P_data);
77 }
78
79 void exit_ast2firm(void)
80 {
81 }
82
83 static unsigned unique_id = 0;
84
85 static ident *unique_ident(const char *tag)
86 {
87         char buf[256];
88
89         snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
90         unique_id++;
91         return new_id_from_str(buf);
92 }
93
94 #if 0
95 static symbol_t *unique_symbol(const char *tag)
96 {
97         obstack_printf(&symbol_obstack, "%s.%d", tag, unique_id);
98         unique_id++;
99
100         const char *string = obstack_finish(&symbol_obstack);
101         symbol_t   *symbol = symbol_table_insert(string);
102
103         assert(symbol->string == string);
104
105         return symbol;
106 }
107 #endif
108
109 static type_t *skip_typeref(type_t *type)
110 {
111         while(1) {
112                 switch(type->type) {
113                 case TYPE_TYPEDEF: {
114                         const typedef_type_t *typedef_type = (const typedef_type_t*) type;
115                         type = typedef_type->declaration->type;
116                         continue;
117                 }
118                 case TYPE_TYPEOF: {
119                         const typeof_type_t *typeof_type = (const typeof_type_t *) type;
120                         if(typeof_type->typeof_type != NULL) {
121                                 type = typeof_type->typeof_type;
122                         } else {
123                                 type = typeof_type->expression->datatype;
124                         }
125                         continue;
126                 }
127                 default:
128                         break;
129                 }
130                 break;
131         }
132
133         return type;
134 }
135
136 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
137 {
138         switch(atomic_type->atype) {
139         case ATOMIC_TYPE_SCHAR:
140         case ATOMIC_TYPE_CHAR:
141                 return mode_Bs;
142         case ATOMIC_TYPE_UCHAR:
143                 return mode_Bu;
144         case ATOMIC_TYPE_SHORT:
145                 return mode_Hs;
146         case ATOMIC_TYPE_USHORT:
147                 return mode_Hu;
148         case ATOMIC_TYPE_LONG:
149         case ATOMIC_TYPE_INT:
150                 return mode_Is;
151         case ATOMIC_TYPE_ULONG:
152         case ATOMIC_TYPE_UINT:
153                 return mode_Iu;
154         case ATOMIC_TYPE_LONGLONG:
155                 return mode_Ls;
156         case ATOMIC_TYPE_ULONGLONG:
157                 return mode_Lu;
158         case ATOMIC_TYPE_FLOAT:
159                 return mode_F;
160         case ATOMIC_TYPE_DOUBLE:
161                 return mode_D;
162         case ATOMIC_TYPE_LONG_DOUBLE:
163                 return mode_E;
164         case ATOMIC_TYPE_BOOL:
165                 return mode_b;
166 #ifdef PROVIDE_COMPLEX
167         case ATOMIC_TYPE_FLOAT_COMPLEX:
168         case ATOMIC_TYPE_DOUBLE_COMPLEX:
169         case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
170                 panic("complex lowering not implemented yet");
171                 break;
172         case ATOMIC_TYPE_FLOAT_IMAGINARY:
173         case ATOMIC_TYPE_DOUBLE_IMAGINARY:
174         case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
175                 panic("imaginary lowering not implemented yet");
176                 break;
177 #endif
178         case ATOMIC_TYPE_VOID:
179                 panic("tried to get mode from void type");
180                 break;
181         case ATOMIC_TYPE_INVALID:
182                 break;
183         }
184         panic("Encountered unknown atomic type");
185 }
186
187
188 static unsigned get_type_size(type_t *type);
189
190 static unsigned get_atomic_type_size(const atomic_type_t *type)
191 {
192         switch(type->atype) {
193         case ATOMIC_TYPE_CHAR:
194         case ATOMIC_TYPE_SCHAR:
195         case ATOMIC_TYPE_UCHAR:
196                 return 1;
197
198         case ATOMIC_TYPE_SHORT:
199         case ATOMIC_TYPE_USHORT:
200                 return 2;
201
202         case ATOMIC_TYPE_BOOL:
203         case ATOMIC_TYPE_INT:
204         case ATOMIC_TYPE_UINT:
205         case ATOMIC_TYPE_LONG:
206         case ATOMIC_TYPE_ULONG:
207         case ATOMIC_TYPE_FLOAT:
208                 return 4;
209
210         case ATOMIC_TYPE_LONGLONG:
211         case ATOMIC_TYPE_ULONGLONG:
212         case ATOMIC_TYPE_DOUBLE:
213                 return 8;
214
215         case ATOMIC_TYPE_LONG_DOUBLE:
216                 return 12;
217
218         case ATOMIC_TYPE_VOID:
219                 return 1;
220
221         case ATOMIC_TYPE_INVALID:
222                 break;
223         }
224         panic("Trying to determine size of invalid atomic type");
225 }
226
227 static unsigned get_compound_type_size(compound_type_t *type)
228 {
229         ir_type *irtype = get_ir_type(&type->type);
230         return get_type_size_bytes(irtype);
231 }
232
233 static unsigned get_array_type_size(array_type_t *type)
234 {
235         ir_type *irtype = get_ir_type(&type->type);
236         return get_type_size_bytes(irtype);
237 }
238
239 static unsigned get_type_size(type_t *type)
240 {
241         type = skip_typeref(type);
242
243         switch(type->type) {
244         case TYPE_ATOMIC:
245                 return get_atomic_type_size((const atomic_type_t*) type);
246         case TYPE_ENUM:
247                 return get_mode_size_bytes(mode_Is);
248         case TYPE_COMPOUND_UNION:
249         case TYPE_COMPOUND_STRUCT:
250                 return get_compound_type_size((compound_type_t*) type);
251         case TYPE_METHOD:
252                 /* just a pointer to the method */
253                 return get_mode_size_bytes(mode_P_code);
254         case TYPE_POINTER:
255                 return get_mode_size_bytes(mode_P_data);
256         case TYPE_ARRAY:
257                 return get_array_type_size((array_type_t*) type);
258         case TYPE_BUILTIN:
259         case TYPE_TYPEDEF:
260         case TYPE_TYPEOF:
261         case TYPE_INVALID:
262                 break;
263         }
264         panic("Trying to determine size of invalid type");
265 }
266
267 static unsigned count_parameters(const method_type_t *method_type)
268 {
269         unsigned count = 0;
270
271         method_parameter_t *parameter = method_type->parameters;
272         for ( ; parameter != NULL; parameter = parameter->next) {
273                 ++count;
274         }
275
276         return count;
277 }
278
279
280
281
282 static ir_type *get_atomic_type(type2firm_env_t *env, const atomic_type_t *type)
283 {
284         (void) env;
285         ir_mode *mode   = get_atomic_mode(type);
286         ident   *id     = get_mode_ident(mode);
287         ir_type *irtype = new_type_primitive(id, mode);
288
289         return irtype;
290 }
291
292 static ir_type *get_method_type(type2firm_env_t *env,
293                                 const method_type_t *method_type)
294 {
295         type_t  *result_type  = method_type->result_type;
296
297         ident   *id           = unique_ident("methodtype");
298         int      n_parameters = count_parameters(method_type);
299         int      n_results    = result_type == type_void ? 0 : 1;
300         ir_type *irtype       = new_type_method(id, n_parameters, n_results);
301
302         if(result_type != type_void) {
303                 ir_type *restype = _get_ir_type(env, result_type);
304                 set_method_res_type(irtype, 0, restype);
305         }
306
307         method_parameter_t *parameter = method_type->parameters;
308         int                 n         = 0;
309         for( ; parameter != NULL; parameter = parameter->next) {
310                 ir_type *p_irtype = _get_ir_type(env, parameter->type);
311                 set_method_param_type(irtype, n, p_irtype);
312                 ++n;
313         }
314
315         if(method_type->variadic) {
316                 set_method_variadicity(irtype, variadicity_variadic);
317         }
318
319         return irtype;
320 }
321
322 static ir_type *get_pointer_type(type2firm_env_t *env, pointer_type_t *type)
323 {
324         type_t  *points_to = type->points_to;
325         ir_type *ir_points_to;
326         /* Avoid endless recursion if the points_to type contains this poiner type
327          * again (might be a struct). We therefore first create a void* pointer
328          * and then set the real points_to type
329          */
330         ir_type *ir_type_void = get_ir_type(type_void);
331         ir_type *ir_type      = new_type_pointer(unique_ident("pointer"),
332                                              ir_type_void, mode_P_data);
333         type->type.firm_type  = ir_type;
334
335         ir_points_to = _get_ir_type(env, points_to);
336         set_pointer_points_to_type(ir_type, ir_points_to);
337
338         return ir_type;
339 }
340
341 static ir_type *get_array_type(type2firm_env_t *env, array_type_t *type)
342 {
343         type_t  *element_type    = type->element_type;
344         ir_type *ir_element_type = _get_ir_type(env, element_type);
345
346         /* TODO... */
347         int n_elements = 0;
348         panic("TODO arraytpye size not implemented yet");
349
350         ir_type *ir_type = new_type_array(unique_ident("array"), 1, ir_element_type);
351         set_array_bounds_int(ir_type, 0, 0, n_elements);
352
353         size_t elemsize = get_type_size_bytes(ir_element_type);
354         int align = get_type_alignment_bytes(ir_element_type);
355         if(elemsize % align > 0) {
356                 elemsize += align - (elemsize % align);
357         }
358         set_type_size_bytes(ir_type, n_elements * elemsize);
359         set_type_alignment_bytes(ir_type, align);
360         set_type_state(ir_type, layout_fixed);
361
362         return ir_type;
363 }
364
365 #define INVALID_TYPE ((ir_type_ptr)-1)
366
367 static ir_type *get_struct_type(type2firm_env_t *env, compound_type_t *type)
368 {
369         symbol_t *symbol = type->declaration->symbol;
370         ident    *id;
371         if(symbol != NULL) {
372                 id = unique_ident(symbol->string);
373         } else {
374                 id = unique_ident("__anonymous_struct");
375         }
376         ir_type *ir_type = new_type_struct(id);
377
378         type->type.firm_type = ir_type;
379
380         int align_all = 1;
381         int offset    = 0;
382         declaration_t *entry = type->declaration->context.declarations;
383         for( ; entry != NULL; entry = entry->next) {
384                 ident       *ident         = new_id_from_str(entry->symbol->string);
385                 ir_type_ptr  entry_ir_type = _get_ir_type(env, entry->type);
386
387                 int entry_size      = get_type_size_bytes(entry_ir_type);
388                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
389                 int misalign = offset % entry_alignment;
390                 offset += misalign;
391
392                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
393                 set_entity_offset(entity, offset);
394                 add_struct_member(ir_type, entity);
395                 entry->entity = entity;
396
397                 offset += entry_size;
398                 if(entry_alignment > align_all) {
399                         if(entry_alignment % align_all != 0) {
400                                 panic("Uneven alignments not supported yet");
401                         }
402                         align_all = entry_alignment;
403                 }
404         }
405
406         int misalign = offset % align_all;
407         offset += misalign;
408         set_type_alignment_bytes(ir_type, align_all);
409         set_type_size_bytes(ir_type, offset);
410         set_type_state(ir_type, layout_fixed);
411
412         return ir_type;
413 }
414
415 static ir_type *get_union_type(type2firm_env_t *env, compound_type_t *type)
416 {
417         declaration_t *declaration = type->declaration;
418         symbol_t      *symbol      = declaration->symbol;
419         ident         *id;
420         if(symbol != NULL) {
421                 id = unique_ident(symbol->string);
422         } else {
423                 id = unique_ident("__anonymous_union");
424         }
425         ir_type  *ir_type = new_type_union(id);
426
427         type->type.firm_type = ir_type;
428
429         int align_all = 1;
430         int size      = 0;
431         declaration_t *entry = declaration->context.declarations;
432         for( ; entry != NULL; entry = entry->next) {
433                 ident       *ident         = new_id_from_str(entry->symbol->string);
434                 ir_type_ptr  entry_ir_type = _get_ir_type(env, entry->type);
435
436                 int entry_size      = get_type_size_bytes(entry_ir_type);
437                 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
438
439                 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
440                 add_union_member(ir_type, entity);
441                 set_entity_offset(entity, 0);
442                 entry->entity = entity;
443
444                 if(entry_size > size) {
445                         size = entry_size;
446                 }
447                 if(entry_alignment > align_all) {
448                         if(entry_alignment % align_all != 0) {
449                                 panic("Uneven alignments not supported yet");
450                         }
451                         align_all = entry_alignment;
452                 }
453         }
454
455         set_type_alignment_bytes(ir_type, align_all);
456         set_type_size_bytes(ir_type, size);
457         set_type_state(ir_type, layout_fixed);
458
459         return ir_type;
460 }
461
462 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type)
463 {
464         assert(type != NULL);
465
466         type = skip_typeref(type);
467
468         if(type->firm_type != NULL) {
469                 assert(type->firm_type != INVALID_TYPE);
470                 return type->firm_type;
471         }
472
473         ir_type *firm_type = NULL;
474         switch(type->type) {
475         case TYPE_ATOMIC:
476                 firm_type = get_atomic_type(env, (atomic_type_t*) type);
477                 break;
478         case TYPE_METHOD:
479                 firm_type = get_method_type(env, (method_type_t*) type);
480                 break;
481         case TYPE_POINTER:
482                 firm_type = get_pointer_type(env, (pointer_type_t*) type);
483                 break;
484         case TYPE_ARRAY:
485                 firm_type = get_array_type(env, (array_type_t*) type);
486                 break;
487         case TYPE_COMPOUND_STRUCT:
488                 firm_type = get_struct_type(env, (compound_type_t*) type);
489                 break;
490         case TYPE_COMPOUND_UNION:
491                 firm_type = get_union_type(env, (compound_type_t*) type);
492                 break;
493         case TYPE_ENUM:
494                 firm_type = ir_type_int;
495                 break;
496         case TYPE_BUILTIN:
497         case TYPE_TYPEOF:
498         case TYPE_TYPEDEF:
499         case TYPE_INVALID:
500                 break;
501         }
502         if(firm_type == NULL)
503                 panic("unknown type found");
504
505         if(env->can_cache) {
506                 type->firm_type = firm_type;
507         }
508         return firm_type;
509
510 }
511
512 static ir_type *get_ir_type(type_t *type)
513 {
514         type2firm_env_t env;
515         env.can_cache = 1;
516
517         return _get_ir_type(&env, type);
518 }
519
520 static inline ir_mode *get_ir_mode(type_t *type)
521 {
522         ir_type *irtype = get_ir_type(type);
523         ir_mode *mode   = get_type_mode(irtype);
524         assert(mode != NULL);
525         return mode;
526 }
527
528 static ir_entity* get_entity_function(declaration_t *declaration)
529 {
530         if(declaration->entity != NULL)
531                 return declaration->entity;
532
533         symbol_t *symbol = declaration->symbol;
534         ident    *id     = new_id_from_str(symbol->string);
535
536         ir_type  *global_type    = get_glob_type();
537         ir_type  *ir_type_method = get_ir_type(declaration->type);
538         assert(is_Method_type(ir_type_method));
539
540         type_t    *type   = declaration->type;
541         ir_entity *entity = new_entity(global_type, id, ir_type_method);
542         set_entity_ld_ident(entity, id);
543         if(declaration->storage_class & STORAGE_CLASS_STATIC
544                         || type->qualifiers & TYPE_QUALIFIER_INLINE) {
545                 set_entity_visibility(entity, visibility_local);
546         } else if(declaration->init.statement != NULL) {
547                 set_entity_visibility(entity, visibility_external_visible);
548         } else {
549                 set_entity_visibility(entity, visibility_external_allocated);
550         }
551
552         declaration->entity = entity;
553         return entity;
554 }
555
556 static void statement_to_firm(statement_t *statement)
557 {
558         (void) statement;
559         (void) get_type_size;
560         /* TODO */
561 }
562
563 static int get_function_n_local_vars(declaration_t *declaration)
564 {
565         (void) declaration;
566         /* TODO */
567         return 30;
568 }
569
570 static void create_function(declaration_t *declaration)
571 {
572         ir_entity *entity = get_entity_function(declaration);
573
574         //context2firm(declaration->context);
575
576         if(declaration->init.statement) {
577                 int        n_local_vars = get_function_n_local_vars(declaration);
578                 ir_graph  *irg          = new_ir_graph(entity, n_local_vars);
579                 ir_node   *first_block  = get_cur_block();
580
581                 statement_to_firm(declaration->init.statement);
582
583                 ir_node *end_block = get_irg_end_block(irg);
584
585                 /* do we have a return statement yet? */
586                 if(get_cur_block() != NULL) {
587                         ir_node *ret = new_Return(get_store(), 0, NULL);
588                         add_immBlock_pred(end_block, ret);
589                 }
590
591                 mature_immBlock(first_block);
592                 mature_immBlock(end_block);
593         }
594 }
595
596 static void context_to_firm(context_t *context)
597 {
598         declaration_t *declaration = context->declarations;
599         for( ; declaration != NULL; declaration = declaration->next) {
600                 type_t *type = declaration->type;
601                 if(type->type == TYPE_METHOD) {
602                         create_function(declaration);
603                 } else {
604                         /* TODO... */
605                 }
606         }
607 }
608
609 void translation_unit_to_firm(translation_unit_t *unit)
610 {
611         context_to_firm(& unit->context);
612 }