8 #include <libfirm/firm.h>
9 #include <libfirm/adt/obst.h>
11 #include "adt/error.h"
16 static ir_type *ir_type_const_char;
17 static ir_type *ir_type_void;
18 static ir_type *ir_type_int;
19 static ir_type *ir_type_void_ptr;
21 static type_t *type_const_char;
22 static type_t *type_void;
23 static type_t *type_int;
25 typedef struct type2firm_env_t type2firm_env_t;
26 struct type2firm_env_t {
27 int can_cache; /* nonzero if type can safely be cached because
28 no typevariables are in the hierarchy */
31 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type);
32 static ir_type *get_ir_type(type_t *type);
34 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
38 const declaration_t *declaration = & value_numbers[pos]->declaration;
40 print_warning_prefix(declaration->source_position);
41 fprintf(stderr, "variable '%s' might be used uninitialized\n",
42 declaration->symbol->string);
44 fprintf(stderr, "Some variable might be used uninitialized\n");
45 return new_r_Unknown(irg, mode);
48 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
50 const source_position_t *pos = (const source_position_t*) dbg;
53 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
57 const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
59 const source_position_t *pos = (const source_position_t*) dbg;
64 return pos->input_name;
67 void init_ast2firm(void)
69 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
70 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
71 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
73 ir_type_int = get_ir_type(type_int);
74 ir_type_const_char = get_ir_type(type_const_char);
75 ir_type_void = get_ir_type(type_int); /* we don't have a real void
77 ir_type_void_ptr = new_type_pointer(new_id_from_str("void_ptr"),
78 ir_type_void, mode_P_data);
81 void exit_ast2firm(void)
85 static unsigned unique_id = 0;
87 static ident *unique_ident(const char *tag)
91 snprintf(buf, sizeof(buf), "%s.%d", tag, unique_id);
93 return new_id_from_str(buf);
97 static symbol_t *unique_symbol(const char *tag)
99 obstack_printf(&symbol_obstack, "%s.%d", tag, unique_id);
102 const char *string = obstack_finish(&symbol_obstack);
103 symbol_t *symbol = symbol_table_insert(string);
105 assert(symbol->string == string);
111 static type_t *skip_typeref(type_t *type)
116 const typedef_type_t *typedef_type = (const typedef_type_t*) type;
117 type = typedef_type->declaration->type;
121 const typeof_type_t *typeof_type = (const typeof_type_t *) type;
122 if(typeof_type->typeof_type != NULL) {
123 type = typeof_type->typeof_type;
125 type = typeof_type->expression->datatype;
138 static ir_mode *get_atomic_mode(const atomic_type_t* atomic_type)
140 switch(atomic_type->atype) {
141 case ATOMIC_TYPE_SCHAR:
142 case ATOMIC_TYPE_CHAR:
144 case ATOMIC_TYPE_UCHAR:
146 case ATOMIC_TYPE_SHORT:
148 case ATOMIC_TYPE_USHORT:
150 case ATOMIC_TYPE_LONG:
151 case ATOMIC_TYPE_INT:
153 case ATOMIC_TYPE_ULONG:
154 case ATOMIC_TYPE_UINT:
156 case ATOMIC_TYPE_LONGLONG:
158 case ATOMIC_TYPE_ULONGLONG:
160 case ATOMIC_TYPE_FLOAT:
162 case ATOMIC_TYPE_DOUBLE:
164 case ATOMIC_TYPE_LONG_DOUBLE:
166 case ATOMIC_TYPE_BOOL:
168 #ifdef PROVIDE_COMPLEX
169 case ATOMIC_TYPE_FLOAT_COMPLEX:
170 case ATOMIC_TYPE_DOUBLE_COMPLEX:
171 case ATOMIC_TYPE_LONG_DOUBLE_COMPLEX:
172 panic("complex lowering not implemented yet");
174 case ATOMIC_TYPE_FLOAT_IMAGINARY:
175 case ATOMIC_TYPE_DOUBLE_IMAGINARY:
176 case ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY:
177 panic("imaginary lowering not implemented yet");
180 case ATOMIC_TYPE_VOID:
181 panic("tried to get mode from void type");
183 case ATOMIC_TYPE_INVALID:
186 panic("Encountered unknown atomic type");
190 static unsigned get_type_size(type_t *type);
192 static unsigned get_atomic_type_size(const atomic_type_t *type)
194 switch(type->atype) {
195 case ATOMIC_TYPE_CHAR:
196 case ATOMIC_TYPE_SCHAR:
197 case ATOMIC_TYPE_UCHAR:
200 case ATOMIC_TYPE_SHORT:
201 case ATOMIC_TYPE_USHORT:
204 case ATOMIC_TYPE_BOOL:
205 case ATOMIC_TYPE_INT:
206 case ATOMIC_TYPE_UINT:
207 case ATOMIC_TYPE_LONG:
208 case ATOMIC_TYPE_ULONG:
209 case ATOMIC_TYPE_FLOAT:
212 case ATOMIC_TYPE_LONGLONG:
213 case ATOMIC_TYPE_ULONGLONG:
214 case ATOMIC_TYPE_DOUBLE:
217 case ATOMIC_TYPE_LONG_DOUBLE:
220 case ATOMIC_TYPE_VOID:
223 case ATOMIC_TYPE_INVALID:
226 panic("Trying to determine size of invalid atomic type");
229 static unsigned get_compound_type_size(compound_type_t *type)
231 ir_type *irtype = get_ir_type(&type->type);
232 return get_type_size_bytes(irtype);
235 static unsigned get_array_type_size(array_type_t *type)
237 ir_type *irtype = get_ir_type(&type->type);
238 return get_type_size_bytes(irtype);
241 static unsigned get_type_size(type_t *type)
243 type = skip_typeref(type);
247 return get_atomic_type_size((const atomic_type_t*) type);
249 return get_mode_size_bytes(mode_Is);
250 case TYPE_COMPOUND_UNION:
251 case TYPE_COMPOUND_STRUCT:
252 return get_compound_type_size((compound_type_t*) type);
254 /* just a pointer to the function */
255 return get_mode_size_bytes(mode_P_code);
257 return get_mode_size_bytes(mode_P_data);
259 return get_array_type_size((array_type_t*) type);
266 panic("Trying to determine size of invalid type");
269 static unsigned count_parameters(const function_type_t *function_type)
273 function_parameter_t *parameter = function_type->parameters;
274 for ( ; parameter != NULL; parameter = parameter->next) {
284 static ir_type *get_atomic_type(type2firm_env_t *env, const atomic_type_t *type)
287 ir_mode *mode = get_atomic_mode(type);
288 ident *id = get_mode_ident(mode);
289 ir_type *irtype = new_type_primitive(id, mode);
294 static ir_type *get_method_type(type2firm_env_t *env,
295 const function_type_t *function_type)
297 type_t *result_type = function_type->result_type;
299 ident *id = unique_ident("functiontype");
300 int n_parameters = count_parameters(function_type);
301 int n_results = result_type == type_void ? 0 : 1;
302 ir_type *irtype = new_type_method(id, n_parameters, n_results);
304 if(result_type != type_void) {
305 ir_type *restype = _get_ir_type(env, result_type);
306 set_method_res_type(irtype, 0, restype);
309 function_parameter_t *parameter = function_type->parameters;
311 for( ; parameter != NULL; parameter = parameter->next) {
312 ir_type *p_irtype = _get_ir_type(env, parameter->type);
313 set_method_param_type(irtype, n, p_irtype);
317 if(function_type->variadic) {
318 set_method_variadicity(irtype, variadicity_variadic);
324 static ir_type *get_pointer_type(type2firm_env_t *env, pointer_type_t *type)
326 type_t *points_to = type->points_to;
327 ir_type *ir_points_to;
328 /* Avoid endless recursion if the points_to type contains this poiner type
329 * again (might be a struct). We therefore first create a void* pointer
330 * and then set the real points_to type
332 ir_type *ir_type_void = get_ir_type(type_void);
333 ir_type *ir_type = new_type_pointer(unique_ident("pointer"),
334 ir_type_void, mode_P_data);
335 type->type.firm_type = ir_type;
337 ir_points_to = _get_ir_type(env, points_to);
338 set_pointer_points_to_type(ir_type, ir_points_to);
343 static ir_type *get_array_type(type2firm_env_t *env, array_type_t *type)
345 type_t *element_type = type->element_type;
346 ir_type *ir_element_type = _get_ir_type(env, element_type);
350 panic("TODO arraytpye size not implemented yet");
352 ir_type *ir_type = new_type_array(unique_ident("array"), 1, ir_element_type);
353 set_array_bounds_int(ir_type, 0, 0, n_elements);
355 size_t elemsize = get_type_size_bytes(ir_element_type);
356 int align = get_type_alignment_bytes(ir_element_type);
357 if(elemsize % align > 0) {
358 elemsize += align - (elemsize % align);
360 set_type_size_bytes(ir_type, n_elements * elemsize);
361 set_type_alignment_bytes(ir_type, align);
362 set_type_state(ir_type, layout_fixed);
367 #define INVALID_TYPE ((ir_type_ptr)-1)
369 static ir_type *get_struct_type(type2firm_env_t *env, compound_type_t *type)
371 symbol_t *symbol = type->declaration->symbol;
374 id = unique_ident(symbol->string);
376 id = unique_ident("__anonymous_struct");
378 ir_type *ir_type = new_type_struct(id);
380 type->type.firm_type = ir_type;
384 declaration_t *entry = type->declaration->context.declarations;
385 for( ; entry != NULL; entry = entry->next) {
386 ident *ident = new_id_from_str(entry->symbol->string);
387 ir_type_ptr entry_ir_type = _get_ir_type(env, entry->type);
389 int entry_size = get_type_size_bytes(entry_ir_type);
390 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
391 int misalign = offset % entry_alignment;
394 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
395 set_entity_offset(entity, offset);
396 add_struct_member(ir_type, entity);
397 entry->entity = entity;
399 offset += entry_size;
400 if(entry_alignment > align_all) {
401 if(entry_alignment % align_all != 0) {
402 panic("Uneven alignments not supported yet");
404 align_all = entry_alignment;
408 int misalign = offset % align_all;
410 set_type_alignment_bytes(ir_type, align_all);
411 set_type_size_bytes(ir_type, offset);
412 set_type_state(ir_type, layout_fixed);
417 static ir_type *get_union_type(type2firm_env_t *env, compound_type_t *type)
419 declaration_t *declaration = type->declaration;
420 symbol_t *symbol = declaration->symbol;
423 id = unique_ident(symbol->string);
425 id = unique_ident("__anonymous_union");
427 ir_type *ir_type = new_type_union(id);
429 type->type.firm_type = ir_type;
433 declaration_t *entry = declaration->context.declarations;
434 for( ; entry != NULL; entry = entry->next) {
435 ident *ident = new_id_from_str(entry->symbol->string);
436 ir_type_ptr entry_ir_type = _get_ir_type(env, entry->type);
438 int entry_size = get_type_size_bytes(entry_ir_type);
439 int entry_alignment = get_type_alignment_bytes(entry_ir_type);
441 ir_entity *entity = new_entity(ir_type, ident, entry_ir_type);
442 add_union_member(ir_type, entity);
443 set_entity_offset(entity, 0);
444 entry->entity = entity;
446 if(entry_size > size) {
449 if(entry_alignment > align_all) {
450 if(entry_alignment % align_all != 0) {
451 panic("Uneven alignments not supported yet");
453 align_all = entry_alignment;
457 set_type_alignment_bytes(ir_type, align_all);
458 set_type_size_bytes(ir_type, size);
459 set_type_state(ir_type, layout_fixed);
464 static ir_type *_get_ir_type(type2firm_env_t *env, type_t *type)
466 assert(type != NULL);
468 type = skip_typeref(type);
470 if(type->firm_type != NULL) {
471 assert(type->firm_type != INVALID_TYPE);
472 return type->firm_type;
475 ir_type *firm_type = NULL;
478 firm_type = get_atomic_type(env, (atomic_type_t*) type);
481 firm_type = get_method_type(env, (function_type_t*) type);
484 firm_type = get_pointer_type(env, (pointer_type_t*) type);
487 firm_type = get_array_type(env, (array_type_t*) type);
489 case TYPE_COMPOUND_STRUCT:
490 firm_type = get_struct_type(env, (compound_type_t*) type);
492 case TYPE_COMPOUND_UNION:
493 firm_type = get_union_type(env, (compound_type_t*) type);
496 firm_type = ir_type_int;
504 if(firm_type == NULL)
505 panic("unknown type found");
508 type->firm_type = firm_type;
514 static ir_type *get_ir_type(type_t *type)
519 return _get_ir_type(&env, type);
522 static inline ir_mode *get_ir_mode(type_t *type)
524 ir_type *irtype = get_ir_type(type);
525 ir_mode *mode = get_type_mode(irtype);
526 assert(mode != NULL);
530 static ir_entity* get_entity_function(declaration_t *declaration)
532 if(declaration->entity != NULL)
533 return declaration->entity;
535 symbol_t *symbol = declaration->symbol;
536 ident *id = new_id_from_str(symbol->string);
538 ir_type *global_type = get_glob_type();
539 ir_type *ir_type_method = get_ir_type(declaration->type);
540 assert(is_Method_type(ir_type_method));
542 type_t *type = declaration->type;
543 ir_entity *entity = new_entity(global_type, id, ir_type_method);
544 set_entity_ld_ident(entity, id);
545 if(declaration->storage_class & STORAGE_CLASS_STATIC
546 || type->qualifiers & TYPE_QUALIFIER_INLINE) {
547 set_entity_visibility(entity, visibility_local);
548 } else if(declaration->init.statement != NULL) {
549 set_entity_visibility(entity, visibility_external_visible);
551 set_entity_visibility(entity, visibility_external_allocated);
554 declaration->entity = entity;
560 static dbg_info *get_dbg_info(const source_position_t *pos)
562 return (dbg_info*) pos;
565 static ir_node *const_to_firm(const const_t *cnst)
567 dbg_info *dbgi = get_dbg_info(&cnst->expression.source_position);
568 ir_mode *mode = get_ir_mode(cnst->expression.datatype);
571 if(mode_is_float(mode)) {
572 tv = new_tarval_from_double(cnst->v.float_value, mode);
574 tv = new_tarval_from_long(cnst->v.int_value, mode);
577 return new_d_Const(dbgi, mode, tv);
580 static ir_node *string_literal_to_firm(const string_literal_t* literal)
582 ir_type *global_type = get_glob_type();
583 ir_type *type = new_type_array(unique_ident("strtype"), 1,
586 ir_entity *ent = new_entity(global_type, unique_ident("Lstr"), type);
587 set_entity_variability(ent, variability_constant);
589 ir_type *elem_type = ir_type_const_char;
590 ir_mode *mode = get_type_mode(elem_type);
592 const char *string = literal->value;
593 size_t slen = strlen(string) + 1;
595 set_array_lower_bound_int(type, 0, 0);
596 set_array_upper_bound_int(type, 0, slen);
597 set_type_size_bytes(type, slen);
598 set_type_state(type, layout_fixed);
600 tarval **tvs = xmalloc(slen * sizeof(tvs[0]));
601 for(size_t i = 0; i < slen; ++i) {
602 tvs[i] = new_tarval_from_long(string[i], mode);
605 set_array_entity_values(ent, tvs, slen);
608 dbg_info *dbgi = get_dbg_info(&literal->expression.source_position);
610 union symconst_symbol sym;
612 return new_d_SymConst(dbgi, sym, symconst_addr_ent);
616 static ir_node *call_expression_to_firm(const call_expression_t *call)
618 expression_t *function = call->function;
619 ir_node *callee = expression_to_firm(function);
621 assert(function->datatype->type == TYPE_FUNCTION);
622 pointer_type_t *pointer_type = (pointer_type_t*) function->datatype;
623 type_t *points_to = pointer_type->points_to;
625 assert(points_to->type == TYPE_FUNCTION);
626 method_type_t *method_type = (method_type_t*) points_to;
627 ir_type *ir_method_type = get_ir_type((type_t*) method_type);
628 ir_type *new_method_type = NULL;
630 int n_parameters = 0;
631 call_argument_t *argument = call->arguments;
632 while(argument != NULL) {
634 argument = argument->next;
637 if(method_type->variable_arguments) {
638 /* we need to construct a new method type matching the call
640 new_method_type = new_type_method(unique_ident("calltype"),
642 get_method_n_ress(ir_method_type));
643 set_method_calling_convention(new_method_type,
644 get_method_calling_convention(ir_method_type));
645 set_method_additional_properties(new_method_type,
646 get_method_additional_properties(ir_method_type));
648 for(int i = 0; i < get_method_n_ress(ir_method_type); ++i) {
649 set_method_res_type(new_method_type, i,
650 get_method_res_type(ir_method_type, i));
653 ir_node *in[n_parameters];
655 argument = call->arguments;
657 while(argument != NULL) {
658 expression_t *expression = argument->expression;
660 ir_node *arg_node = expression_to_firm(expression);
663 if(new_method_type != NULL) {
664 ir_type *irtype = get_ir_type(expression->datatype);
665 set_method_param_type(new_method_type, n, irtype);
668 argument = argument->next;
672 if(new_method_type != NULL)
673 ir_method_type = new_method_type;
675 dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
676 ir_node *store = get_store();
677 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
679 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
682 type_t *result_type = method_type->result_type;
683 ir_node *result = NULL;
684 if(result_type->type != TYPE_VOID) {
685 ir_mode *mode = get_ir_mode(result_type);
686 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
687 result = new_d_Proj(dbgi, resproj, mode, 0);
694 static ir_node *expression_to_firm(expression_t *expression)
696 switch(expression->type) {
698 return const_to_firm((const const_t*) expression);
699 case EXPR_STRING_LITERAL:
700 return string_literal_to_firm((const string_literal_t*) expression);
704 panic("unsupported expression found");
709 static void statement_to_firm(statement_t *statement);
711 static void return_statement_to_firm(return_statement_t *statement)
713 dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
716 if(statement->return_value != NULL) {
717 ir_node *retval = expression_to_firm(statement->return_value);
721 ret = new_d_Return(dbgi, get_store(), 1, in);
723 ret = new_d_Return(dbgi, get_store(), 0, NULL);
725 ir_node *end_block = get_irg_end_block(current_ir_graph);
726 add_immBlock_pred(end_block, ret);
731 static void compound_statement_to_firm(compound_statement_t *compound)
733 statement_t *statement = compound->statements;
734 for( ; statement != NULL; statement = statement->next) {
735 //context2firm(&statement->context);
736 statement_to_firm(statement);
740 static void statement_to_firm(statement_t *statement)
742 switch(statement->type) {
743 case STATEMENT_COMPOUND:
744 compound_statement_to_firm((compound_statement_t*) statement);
746 case STATEMENT_RETURN:
747 return_statement_to_firm((return_statement_t*) statement);
752 panic("Statement not implemented\n");
755 static int get_function_n_local_vars(declaration_t *declaration)
762 static void create_function(declaration_t *declaration)
764 ir_entity *entity = get_entity_function(declaration);
766 //context2firm(declaration->context);
768 if(declaration->init.statement == NULL)
771 int n_local_vars = get_function_n_local_vars(declaration);
772 ir_graph *irg = new_ir_graph(entity, n_local_vars);
773 ir_node *first_block = get_cur_block();
775 statement_to_firm(declaration->init.statement);
777 ir_node *end_block = get_irg_end_block(irg);
779 /* do we have a return statement yet? */
780 if(get_cur_block() != NULL) {
781 ir_node *ret = new_Return(get_store(), 0, NULL);
782 add_immBlock_pred(end_block, ret);
785 mature_immBlock(first_block);
786 mature_immBlock(end_block);
788 irg_finalize_cons(irg);
790 /* finalize the frame type */
791 ir_type *frame_type = get_irg_frame_type(irg);
792 int n = get_compound_n_members(frame_type);
795 for(int i = 0; i < n; ++i) {
796 ir_entity *entity = get_compound_member(frame_type, i);
797 ir_type *entity_type = get_entity_type(entity);
799 int align = get_type_alignment_bytes(entity_type);
800 if(align > align_all)
804 misalign = offset % align;
808 set_entity_offset(entity, offset);
809 offset += get_type_size_bytes(entity_type);
811 set_type_size_bytes(frame_type, offset);
812 set_type_alignment_bytes(frame_type, align_all);
813 set_type_state(frame_type, layout_fixed);
818 static void context_to_firm(context_t *context)
820 declaration_t *declaration = context->declarations;
821 for( ; declaration != NULL; declaration = declaration->next) {
822 type_t *type = declaration->type;
823 if(type->type == TYPE_FUNCTION) {
824 create_function(declaration);
831 void translation_unit_to_firm(translation_unit_t *unit)
833 /* remove me later TODO FIXME */
834 (void) get_type_size;
836 context_to_firm(& unit->context);