support functions declared in local scope
[cparser] / ast2firm.c
index 27e502f..d063329 100644 (file)
@@ -430,22 +430,23 @@ static ir_type *create_array_type(array_type_t *type)
        dbg_info *dbgi   = get_dbg_info(&type->type.source_position);
        ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
 
+       const int align = get_type_alignment_bytes(ir_element_type);
+       set_type_alignment_bytes(ir_type, align);
+
        if(type->size != NULL) {
                int n_elements = fold_constant(type->size);
 
                set_array_bounds_int(ir_type, 0, 0, n_elements);
 
                size_t elemsize = get_type_size_bytes(ir_element_type);
-               int align = get_type_alignment_bytes(ir_element_type);
                if(elemsize % align > 0) {
                        elemsize += align - (elemsize % align);
                }
                set_type_size_bytes(ir_type, n_elements * elemsize);
-               set_type_alignment_bytes(ir_type, align);
-               set_type_state(ir_type, layout_fixed);
        } else {
                set_array_lower_bound_int(ir_type, 0, 0);
        }
+       set_type_state(ir_type, layout_fixed);
 
        return ir_type;
 }
@@ -539,35 +540,65 @@ static ir_type *create_bitfield_type(bitfield_type_t *const type)
 
 #define INVALID_TYPE ((ir_type_ptr)-1)
 
-static ir_type *create_struct_type(compound_type_t *type)
+static ir_type *create_union_type(compound_type_t *type, ir_type *irtype,
+                                  size_t *outer_offset, size_t *outer_align);
+
+static ir_type *create_struct_type(compound_type_t *type, ir_type *irtype,
+                                   size_t *outer_offset, size_t *outer_align)
 {
        declaration_t *declaration = type->declaration;
        if(declaration->v.irtype != NULL) {
                return declaration->v.irtype;
        }
 
-       symbol_t *symbol = declaration->symbol;
-       ident    *id;
-       if(symbol != NULL) {
-               id = unique_ident(symbol->string);
-       } else {
-               id = unique_ident("__anonymous_struct");
-       }
-       dbg_info *dbgi  = get_dbg_info(&type->type.source_position);
-       ir_type *irtype = new_d_type_struct(id, dbgi);
-
-       declaration->v.irtype = irtype;
-       type->type.firm_type  = irtype;
-
        size_t align_all  = 1;
        size_t offset     = 0;
        size_t bit_offset = 0;
+       if(irtype == NULL) {
+               symbol_t *symbol = declaration->symbol;
+               ident    *id;
+               if(symbol != NULL) {
+                       id = unique_ident(symbol->string);
+               } else {
+                       id = unique_ident("__anonymous_struct");
+               }
+               dbg_info *dbgi  = get_dbg_info(&type->type.source_position);
+
+               irtype = new_d_type_struct(id, dbgi);
+
+               declaration->v.irtype = irtype;
+               type->type.firm_type  = irtype;
+       } else {
+               offset    = *outer_offset;
+               align_all = *outer_align;
+       }
+
        declaration_t *entry = declaration->scope.declarations;
        for( ; entry != NULL; entry = entry->next) {
                if(entry->namespc != NAMESPACE_NORMAL)
                        continue;
 
-               type_t  *entry_type  = skip_typeref(entry->type);
+               symbol_t *symbol     = entry->symbol;
+               type_t   *entry_type = skip_typeref(entry->type);
+               dbg_info *dbgi       = get_dbg_info(&entry->source_position);
+               ident    *ident;
+               if(symbol != NULL) {
+                       ident = new_id_from_str(symbol->string);
+               } else {
+                       if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
+                               create_struct_type(&entry_type->compound, irtype, &offset,
+                                                  &align_all);
+                               continue;
+                       } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
+                               create_union_type(&entry_type->compound, irtype, &offset,
+                                                 &align_all);
+                               continue;
+                       } else {
+                               assert(entry_type->kind == TYPE_BITFIELD);
+                       }
+                       ident = unique_ident("anon");
+               }
+
                ir_type *base_irtype;
                if(entry_type->kind == TYPE_BITFIELD) {
                        base_irtype = get_ir_type(entry_type->bitfield.base);
@@ -578,16 +609,8 @@ static ir_type *create_struct_type(compound_type_t *type)
                size_t entry_alignment = get_type_alignment_bytes(base_irtype);
                size_t misalign        = offset % entry_alignment;
 
-               dbg_info  *dbgi   = get_dbg_info(&entry->source_position);
-               ir_entity *entity = NULL;
-               if(entry->symbol != NULL) {
-                       ident   *ident        = new_id_from_str(entry->symbol->string);
-                       ir_type *entry_irtype = get_ir_type(entry_type);
-                       entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
-               } else {
-                       /* only bitfields are allowed to be anonymous */
-                       assert(entry_type->kind == TYPE_BITFIELD);
-               }
+               ir_type   *entry_irtype = get_ir_type(entry_type);
+               ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
 
                size_t base;
                size_t bits_remainder;
@@ -628,64 +651,100 @@ static ir_type *create_struct_type(compound_type_t *type)
                        align_all = entry_alignment;
                }
 
-               if(entity != NULL) {
-                       set_entity_offset(entity, base);
-                       set_entity_offset_bits_remainder(entity,
-                                                        (unsigned char) bits_remainder);
-                       add_struct_member(irtype, entity);
-                       entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
-                       assert(entry->v.entity == NULL);
-                       entry->v.entity         = entity;
-               }
+               set_entity_offset(entity, base);
+               set_entity_offset_bits_remainder(entity,
+                                                (unsigned char) bits_remainder);
+               //add_struct_member(irtype, entity);
+               entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
+               assert(entry->v.entity == NULL);
+               entry->v.entity         = entity;
        }
 
        size_t misalign = offset % align_all;
        if(misalign > 0 || bit_offset > 0) {
                offset += align_all - misalign;
        }
-       set_type_alignment_bytes(irtype, align_all);
-       set_type_size_bytes(irtype, offset);
-       set_type_state(irtype, layout_fixed);
+
+       if(outer_offset != NULL) {
+               *outer_offset = offset;
+               *outer_align  = align_all;
+       } else {
+               set_type_alignment_bytes(irtype, align_all);
+               set_type_size_bytes(irtype, offset);
+               set_type_state(irtype, layout_fixed);
+       }
 
        return irtype;
 }
 
-static ir_type *create_union_type(compound_type_t *type)
+static ir_type *create_union_type(compound_type_t *type, ir_type *irtype,
+                                  size_t *outer_offset, size_t *outer_align)
 {
        declaration_t *declaration = type->declaration;
        if(declaration->v.irtype != NULL) {
                return declaration->v.irtype;
        }
 
-       symbol_t      *symbol      = declaration->symbol;
-       ident         *id;
-       if(symbol != NULL) {
-               id = unique_ident(symbol->string);
+       size_t offset    = 0;
+       size_t align_all = 1;
+       size_t size      = 0;
+
+       if(irtype == NULL) {
+               symbol_t      *symbol      = declaration->symbol;
+               ident         *id;
+               if(symbol != NULL) {
+                       id = unique_ident(symbol->string);
+               } else {
+                       id = unique_ident("__anonymous_union");
+               }
+               dbg_info *dbgi = get_dbg_info(&type->type.source_position);
+
+               irtype = new_d_type_union(id, dbgi);
        } else {
-               id = unique_ident("__anonymous_union");
+               offset    = *outer_offset;
+               align_all = *outer_align;
        }
-       dbg_info *dbgi  = get_dbg_info(&type->type.source_position);
-       ir_type *irtype = new_d_type_union(id, dbgi);
 
        type->type.firm_type = irtype;
 
-       int align_all = 1;
-       int size      = 0;
        declaration_t *entry = declaration->scope.declarations;
        for( ; entry != NULL; entry = entry->next) {
                if(entry->namespc != NAMESPACE_NORMAL)
                        continue;
 
-               ident   *ident         = new_id_from_str(entry->symbol->string);
-               ir_type *entry_ir_type = get_ir_type(entry->type);
+               type_t  *entry_type    = skip_typeref(entry->type);
+               ir_type *entry_ir_type = get_ir_type(entry_type);
 
-               int entry_size      = get_type_size_bytes(entry_ir_type);
-               int entry_alignment = get_type_alignment_bytes(entry_ir_type);
+               ident *ident;
+               if(entry->symbol != NULL) {
+                       ident = new_id_from_str(entry->symbol->string);
+               } else {
+                       size_t offs = offset;
+                       if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
+                               create_struct_type(&entry_type->compound, irtype, &offs,
+                                                  &align_all);
+                               continue;
+                       } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
+                               create_union_type(&entry_type->compound, irtype, &offs,
+                                                 &align_all);
+                               continue;
+                       } else {
+                               panic("anonymous union member must be struct or union");
+                       }
+                       size_t entry_size = offs - offset;
+                       if(entry_size > size) {
+                               size = entry_size;
+                       }
+                       ident = unique_ident("anon");
+               }
+
+               size_t entry_size      = get_type_size_bytes(entry_ir_type);
+               size_t entry_alignment = get_type_alignment_bytes(entry_ir_type);
 
                dbg_info  *const dbgi   = get_dbg_info(&entry->source_position);
                ir_entity *const entity = new_d_entity(irtype, ident, entry_ir_type,
                                                       dbgi);
-               add_union_member(irtype, entity);
+               //add_union_member(irtype, entity);
                set_entity_offset(entity, 0);
                entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
                assert(entry->v.entity == NULL);
@@ -702,11 +761,26 @@ static ir_type *create_union_type(compound_type_t *type)
                }
        }
 
-       set_type_alignment_bytes(irtype, align_all);
-       set_type_size_bytes(irtype, size);
-       set_type_state(irtype, layout_fixed);
+       if(outer_offset != NULL) {
+               assert(*outer_offset == offset);
+
+               size_t misalign = offset % align_all;
+               if (misalign != 0)
+                       size += align_all - misalign;
+               *outer_offset += size;
 
-       declaration->v.irtype = irtype;
+               if(align_all > *outer_align) {
+                       if(align_all % *outer_align != 0) {
+                               panic("uneven alignments not supported yet");
+                       }
+                       *outer_align = align_all;
+               }
+       } else {
+               set_type_alignment_bytes(irtype, align_all);
+               set_type_size_bytes(irtype, size);
+               set_type_state(irtype, layout_fixed);
+               declaration->v.irtype = irtype;
+       }
 
        return irtype;
 }
@@ -772,10 +846,10 @@ static ir_type *get_ir_type(type_t *type)
                firm_type = create_array_type(&type->array);
                break;
        case TYPE_COMPOUND_STRUCT:
-               firm_type = create_struct_type(&type->compound);
+               firm_type = create_struct_type(&type->compound, NULL, NULL, NULL);
                break;
        case TYPE_COMPOUND_UNION:
-               firm_type = create_union_type(&type->compound);
+               firm_type = create_union_type(&type->compound, NULL, NULL, NULL);
                break;
        case TYPE_ENUM:
                firm_type = create_enum_type(&type->enumt);
@@ -1567,12 +1641,13 @@ static bool is_local_variable(expression_t *expression)
        return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
 }
 
-static pn_Cmp get_pnc(const expression_kind_t kind)
+static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
 {
        switch(kind) {
        case EXPR_BINARY_EQUAL:         return pn_Cmp_Eq;
        case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
-       case EXPR_BINARY_NOTEQUAL:      return pn_Cmp_Ne;
+       case EXPR_BINARY_NOTEQUAL:
+               return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
        case EXPR_BINARY_ISLESS:
        case EXPR_BINARY_LESS:          return pn_Cmp_Lt;
        case EXPR_BINARY_ISLESSEQUAL:
@@ -1610,7 +1685,7 @@ static ir_node *handle_assume_compare(dbg_info *dbi,
        ir_node       *res = NULL;
        pn_Cmp         cmp_val;
 
-       cmp_val = get_pnc(expression->base.kind);
+       cmp_val = get_pnc(expression->base.kind, op1->base.type);
 
        if (is_local_variable(op1) && is_local_variable(op2)) {
        var  = op1->reference.declaration;
@@ -2090,7 +2165,7 @@ static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
                ir_node *left  = expression_to_firm(expression->left);
                ir_node *right = expression_to_firm(expression->right);
                ir_node *cmp   = new_d_Cmp(dbgi, left, right);
-               long     pnc   = get_pnc(kind);
+               long     pnc   = get_pnc(kind, expression->left->base.type);
                ir_node *proj  = new_d_Proj(dbgi, cmp, mode_b, pnc);
                return proj;
        }
@@ -3169,7 +3244,11 @@ static void create_local_declaration(declaration_t *declaration)
        case STORAGE_CLASS_AUTO:
        case STORAGE_CLASS_REGISTER:
                if(is_type_function(type)) {
-                       panic("nested functions not supported yet");
+                       if(declaration->init.statement != NULL) {
+                               panic("nested functions not supported yet");
+                       } else {
+                               get_function_entity(declaration);
+                       }
                } else {
                        create_local_variable(declaration);
                }
@@ -3766,6 +3845,14 @@ static int count_decls_in_expression(const expression_t *expression) {
        }
        EXPR_UNARY_CASES
                return count_decls_in_expression(expression->unary.value);
+       case EXPR_CALL: {
+               int count = 0;
+               call_argument_t *argument = expression->call.arguments;
+               for( ; argument != NULL; argument = argument->next) {
+                       count += count_decls_in_expression(argument->expression);
+               }
+               return count;
+       }
 
        default:
                break;
@@ -4113,8 +4200,6 @@ create_var:
                                                  var_type);
                        set_entity_visibility(declaration->v.entity, vis);
 
-                       current_ir_graph = get_const_code_irg();
-                       create_initializer(declaration);
                        return;
 
                case STORAGE_CLASS_TYPEDEF:
@@ -4147,7 +4232,7 @@ static void scope_to_firm(scope_t *scope)
                }
        }
 
-       /* second pass: create code */
+       /* second pass: create code/initializers */
        declaration = scope->declarations;
        for( ; declaration != NULL; declaration = declaration->next) {
                if(declaration->namespc != NAMESPACE_NORMAL)
@@ -4159,10 +4244,14 @@ static void scope_to_firm(scope_t *scope)
                        continue;
 
                type_t *type = declaration->type;
-               if(type->kind != TYPE_FUNCTION)
-                       continue;
-
-               create_function(declaration);
+               if(type->kind == TYPE_FUNCTION) {
+                       create_function(declaration);
+               } else {
+                       assert(declaration->declaration_kind
+                                       == DECLARATION_KIND_GLOBAL_VARIABLE);
+                       current_ir_graph = get_const_code_irg();
+                       create_initializer(declaration);
+               }
        }
 }