6 #include <libfirm/firm.h>
7 #include <libfirm/adt/obst.h>
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;
19 static type_t *type_const_char;
20 static type_t *type_void;
21 static type_t *type_int;
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 */
29 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type);
30 static ir_type *get_ir_type(type_t *type);
32 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
36 const declaration_t *declaration = & value_numbers[pos]->declaration;
38 print_warning_prefix(declaration->source_position);
39 fprintf(stderr, "variable '%s' might be used uninitialized\n",
40 declaration->symbol->string);
42 fprintf(stderr, "Some variable might be used uninitialized\n");
43 return new_r_Unknown(irg, mode);
46 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
48 const source_position_t *pos = (const source_position_t*) dbg;
51 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
55 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
57 const source_position_t *pos = (const source_position_t*) dbg;
62 return pos->input_name;
65 void init_ast2firm(void)
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);
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
75 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
76 ir_type_void, mode_P_data);
79 void exit_ast2firm(void)
83 static unsigned unique_id = 0;
85 static ident *unique_ident(const char *tag)
89 snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
91 return new_id_from_str(buf);
95 static symbol_t *unique_symbol(const char *tag)
97 obstack_printf(&symbol_obstack, "%s.%d", tag, unique_id);
100 const char *string = obstack_finish(&symbol_obstack);
101 symbol_t *symbol = symbol_table_insert(string);
103 assert(symbol->string == string);
109 static type_t *skip_typeref(type_t *type)
114 const typedef_type_t *typedef_type = (const typedef_type_t*) type;
115 type = typedef_type->declaration->type;
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;
123 type = typeof_type->expression->datatype;
136 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
138 switch(atomic_type->atype) {
139 case ATOMIC_TYPE_SCHAR:
140 case ATOMIC_TYPE_CHAR:
142 case ATOMIC_TYPE_UCHAR:
144 case ATOMIC_TYPE_SHORT:
146 case ATOMIC_TYPE_USHORT:
148 case ATOMIC_TYPE_LONG:
149 case ATOMIC_TYPE_INT:
151 case ATOMIC_TYPE_ULONG:
152 case ATOMIC_TYPE_UINT:
154 case ATOMIC_TYPE_LONGLONG:
156 case ATOMIC_TYPE_ULONGLONG:
158 case ATOMIC_TYPE_FLOAT:
160 case ATOMIC_TYPE_DOUBLE:
162 case ATOMIC_TYPE_LONG_DOUBLE:
164 case ATOMIC_TYPE_BOOL:
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");
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");
178 case ATOMIC_TYPE_VOID:
179 panic("tried to get mode from void type");
181 case ATOMIC_TYPE_INVALID:
184 panic("Encountered unknown atomic type");
188 static unsigned get_type_size(type_t *type);
190 static unsigned get_atomic_type_size(const atomic_type_t *type)
192 switch(type->atype) {
193 case ATOMIC_TYPE_CHAR:
194 case ATOMIC_TYPE_SCHAR:
195 case ATOMIC_TYPE_UCHAR:
198 case ATOMIC_TYPE_SHORT:
199 case ATOMIC_TYPE_USHORT:
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:
210 case ATOMIC_TYPE_LONGLONG:
211 case ATOMIC_TYPE_ULONGLONG:
212 case ATOMIC_TYPE_DOUBLE:
215 case ATOMIC_TYPE_LONG_DOUBLE:
218 case ATOMIC_TYPE_VOID:
221 case ATOMIC_TYPE_INVALID:
224 panic("Trying to determine size of invalid atomic type");
227 static unsigned get_compound_type_size(compound_type_t *type)
229 ir_type *irtype = get_ir_type(&type->type);
230 return get_type_size_bytes(irtype);
233 static unsigned get_array_type_size(array_type_t *type)
235 ir_type *irtype = get_ir_type(&type->type);
236 return get_type_size_bytes(irtype);
239 static unsigned get_type_size(type_t *type)
241 type = skip_typeref(type);
245 return get_atomic_type_size((const atomic_type_t*) type);
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);
252 /* just a pointer to the method */
253 return get_mode_size_bytes(mode_P_code);
255 return get_mode_size_bytes(mode_P_data);
257 return get_array_type_size((array_type_t*) type);
264 panic("Trying to determine size of invalid type");
267 static unsigned count_parameters(const method_type_t *method_type)
271 method_parameter_t *parameter = method_type->parameters;
272 for ( ; parameter != NULL; parameter = parameter->next) {
282 static ir_type *get_atomic_type(type2firm_env_t *env, const atomic_type_t *type)
285 ir_mode *mode = get_atomic_mode(type);
286 ident *id = get_mode_ident(mode);
287 ir_type *irtype = new_type_primitive(id, mode);
292 static ir_type *get_method_type(type2firm_env_t *env,
293 const method_type_t *method_type)
295 type_t *result_type = method_type->result_type;
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);
302 if(result_type != type_void) {
303 ir_type *restype = _get_ir_type(env, result_type);
304 set_method_res_type(irtype, 0, restype);
307 method_parameter_t *parameter = method_type->parameters;
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);
315 if(method_type->variadic) {
316 set_method_variadicity(irtype, variadicity_variadic);
322 static ir_type *get_pointer_type(type2firm_env_t *env, pointer_type_t *type)
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
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;
335 ir_points_to = _get_ir_type(env, points_to);
336 set_pointer_points_to_type(ir_type, ir_points_to);
341 static ir_type *get_array_type(type2firm_env_t *env, array_type_t *type)
343 type_t *element_type = type->element_type;
344 ir_type *ir_element_type = _get_ir_type(env, element_type);
348 panic("TODO arraytpye size not implemented yet");
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);
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);
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);
365 #define INVALID_TYPE ((ir_type_ptr)-1)
367 static ir_type *get_struct_type(type2firm_env_t *env, compound_type_t *type)
369 symbol_t *symbol = type->declaration->symbol;
372 id = unique_ident(symbol->string);
374 id = unique_ident("__anonymous_struct");
376 ir_type *ir_type = new_type_struct(id);
378 type->type.firm_type = ir_type;
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);
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;
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;
397 offset += entry_size;
398 if(entry_alignment > align_all) {
399 if(entry_alignment % align_all != 0) {
400 panic("Uneven alignments not supported yet");
402 align_all = entry_alignment;
406 int misalign = offset % align_all;
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);
415 static ir_type *get_union_type(type2firm_env_t *env, compound_type_t *type)
417 declaration_t *declaration = type->declaration;
418 symbol_t *symbol = declaration->symbol;
421 id = unique_ident(symbol->string);
423 id = unique_ident("__anonymous_union");
425 ir_type *ir_type = new_type_union(id);
427 type->type.firm_type = ir_type;
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);
436 int entry_size = get_type_size_bytes(entry_ir_type);
437 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
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;
444 if(entry_size > size) {
447 if(entry_alignment > align_all) {
448 if(entry_alignment % align_all != 0) {
449 panic("Uneven alignments not supported yet");
451 align_all = entry_alignment;
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);
462 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type)
464 assert(type != NULL);
466 type = skip_typeref(type);
468 if(type->firm_type != NULL) {
469 assert(type->firm_type != INVALID_TYPE);
470 return type->firm_type;
473 ir_type *firm_type = NULL;
476 firm_type = get_atomic_type(env, (atomic_type_t*) type);
479 firm_type = get_method_type(env, (method_type_t*) type);
482 firm_type = get_pointer_type(env, (pointer_type_t*) type);
485 firm_type = get_array_type(env, (array_type_t*) type);
487 case TYPE_COMPOUND_STRUCT:
488 firm_type = get_struct_type(env, (compound_type_t*) type);
490 case TYPE_COMPOUND_UNION:
491 firm_type = get_union_type(env, (compound_type_t*) type);
494 firm_type = ir_type_int;
502 if(firm_type == NULL)
503 panic("unknown type found");
506 type->firm_type = firm_type;
512 static ir_type *get_ir_type(type_t *type)
517 return _get_ir_type(&env, type);
520 static inline ir_mode *get_ir_mode(type_t *type)
522 ir_type *irtype = get_ir_type(type);
523 ir_mode *mode = get_type_mode(irtype);
524 assert(mode != NULL);
528 static ir_entity* get_entity_function(declaration_t *declaration)
530 if(declaration->entity != NULL)
531 return declaration->entity;
533 symbol_t *symbol = declaration->symbol;
534 ident *id = new_id_from_str(symbol->string);
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));
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);
549 set_entity_visibility(entity, visibility_external_allocated);
552 declaration->entity = entity;
556 static void statement_to_firm(statement_t *statement)
559 (void) get_type_size;
563 static int get_function_n_local_vars(declaration_t *declaration)
570 static void create_function(declaration_t *declaration)
572 ir_entity *entity = get_entity_function(declaration);
574 //context2firm(declaration->context);
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();
581 statement_to_firm(declaration->init.statement);
583 ir_node *end_block = get_irg_end_block(irg);
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);
591 mature_immBlock(first_block);
592 mature_immBlock(end_block);
596 static void context_to_firm(context_t *context)
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);
609 void translation_unit_to_firm(translation_unit_t *unit)
611 context_to_firm(& unit->context);