improve error message a bit
[cparser] / ast2firm.c
index 52c345a..509f302 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <libfirm/firm.h>
 #include <libfirm/adt/obst.h>
+#include <libfirm/be.h>
 
 #include "ast2firm.h"
 
@@ -927,7 +928,7 @@ static ident *create_ld_ident_win32(ir_entity *ent, declaration_t *declaration)
                id = mangle(id_underscore, get_entity_ident(ent));
        }
 
-       decl_modifiers_t decl_modifiers = declaration->decl_modifiers;
+       decl_modifiers_t decl_modifiers = declaration->modifiers;
        if (decl_modifiers & DM_DLLIMPORT) {
                /* add prefix for imported symbols */
                id = mangle(id_imp, id);
@@ -996,13 +997,13 @@ static ir_entity *get_function_entity(declaration_t *declaration)
        dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
        entity               = new_d_entity(global_type, id, ir_type_method, dbgi);
        set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
-       if (declaration->storage_class == STORAGE_CLASS_STATIC &&
-               declaration->init.statement == NULL) {
-               /* this entity was declared, but never defined */
-               set_entity_peculiarity(entity, peculiarity_description);
-       }
+
        if (declaration->storage_class == STORAGE_CLASS_STATIC
                        || declaration->is_inline) {
+               if (declaration->init.statement == NULL) {
+                       /* this entity was declared, but is defined nowhere */
+                       set_entity_peculiarity(entity, peculiarity_description);
+               }
                set_entity_visibility(entity, visibility_local);
        } else if (declaration->init.statement != NULL) {
                set_entity_visibility(entity, visibility_external_visible);
@@ -2072,7 +2073,12 @@ static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
 {
        pointer_type_t *const pointer_type = &type->pointer;
        type_t         *const points_to    = pointer_type->points_to;
-       const unsigned        elem_size    = get_type_size_const(points_to);
+       unsigned              elem_size    = get_type_size_const(points_to);
+
+       /* gcc extension */
+       if (elem_size == 0 && is_type_atomic(points_to, ATOMIC_TYPE_VOID)) {
+               elem_size = 1;
+       }
 
        assert(elem_size >= 1);
        if (elem_size == 1)
@@ -4445,29 +4451,31 @@ static void goto_to_firm(const goto_statement_t *statement)
        set_cur_block(NULL);
 }
 
-typedef enum modifier_t {
-       ASM_MODIFIER_WRITE_ONLY   = 1 << 0,
-       ASM_MODIFIER_READ_WRITE   = 1 << 1,
-       ASM_MODIFIER_COMMUTATIVE  = 1 << 2,
-       ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
-} modifier_t;
-
 static void asm_statement_to_firm(const asm_statement_t *statement)
 {
-       (void) statement;
-       fprintf(stderr, "WARNING asm not implemented yet!\n");
-#if 0
        bool needs_memory = false;
 
+       if (statement->is_volatile) {
+               needs_memory = true;
+       }
+
        size_t         n_clobbers = 0;
        asm_clobber_t *clobber    = statement->clobbers;
        for( ; clobber != NULL; clobber = clobber->next) {
-               if (strcmp(clobber->clobber, "memory") == 0) {
+               const char *clobber_str = clobber->clobber.begin;
+
+               if (!be_is_valid_clobber(clobber_str)) {
+                       errorf(&statement->base.source_position,
+                                  "invalid clobber '%s' specified", clobber->clobber);
+                       continue;
+               }
+
+               if (strcmp(clobber_str, "memory") == 0) {
                        needs_memory = true;
                        continue;
                }
 
-               ident *id = new_id_from_str(clobber->clobber);
+               ident *id = new_id_from_str(clobber_str);
                obstack_ptr_grow(&asm_obst, id);
                ++n_clobbers;
        }
@@ -4477,68 +4485,215 @@ static void asm_statement_to_firm(const asm_statement_t *statement)
                clobbers = obstack_finish(&asm_obst);
        }
 
-       /* find and count input and output constraints */
-       asm_constraint_t *constraint = statement->inputs;
-       for( ; constraint != NULL; constraint = constraint->next) {
-               int  modifiers      = 0;
-               bool supports_memop = false;
-               for(const char *c = constraint->constraints; *c != 0; ++c) {
-                       /* TODO: improve error messages */
-                       switch(*c) {
-                       case '?':
-                       case '!':
-                               panic("multiple alternative assembler constraints not "
-                                     "supported");
-                       case 'm':
-                       case 'o':
-                       case 'V':
-                       case '<':
-                       case '>':
-                       case 'X':
-                               supports_memop = true;
-                               obstack_1grow(&asm_obst, *c);
-                               break;
-                       case '=':
-                               if (modifiers & ASM_MODIFIER_READ_WRITE)
-                                       panic("inconsistent register constraints");
-                               modifiers |= ASM_MODIFIER_WRITE_ONLY;
-                               break;
-                       case '+':
-                               if (modifiers & ASM_MODIFIER_WRITE_ONLY)
-                                       panic("inconsistent register constraints");
-                               modifiers |= ASM_MODIFIER_READ_WRITE;
-                               break;
-                       case '&':
-                               modifiers |= ASM_MODIFIER_EARLYCLOBBER;
-                               panic("early clobber assembler constraint not supported yet");
-                               break;
-                       case '%':
-                               modifiers |= ASM_MODIFIER_COMMUTATIVE;
-                               panic("commutative assembler constraint not supported yet");
-                               break;
-                       case '#':
-                               /* skip register preferences stuff... */
-                               while(*c != 0 && *c != ',')
-                                       ++c;
-                               break;
-                       case '*':
-                               /* skip register preferences stuff... */
-                               ++c;
-                               break;
-                       default:
-                               obstack_1grow(&asm_obst, *c);
-                               break;
+       size_t n_inputs  = 0;
+       asm_argument_t *argument = statement->inputs;
+       for ( ; argument != NULL; argument = argument->next)
+               n_inputs++;
+       size_t n_outputs = 0;
+       argument = statement->outputs;
+       for ( ; argument != NULL; argument = argument->next)
+               n_outputs++;
+
+       unsigned next_pos = 0;
+
+       ir_node *ins[n_inputs + n_outputs + 1];
+       size_t   in_size = 0;
+
+       ir_asm_constraint *tmp_in_constraints
+               = xmalloc(n_outputs * sizeof(tmp_in_constraints[0]));
+
+       const expression_t *out_exprs[n_outputs];
+       ir_node            *out_addrs[n_outputs];
+       size_t              out_size = 0;
+
+       argument = statement->outputs;
+       for ( ; argument != NULL; argument = argument->next) {
+               const char *constraints = argument->constraints.begin;
+               asm_constraint_flags_t asm_flags
+                       = be_parse_asm_constraints(constraints);
+
+               if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
+                       errorf(&statement->base.source_position,
+                              "some constraints in '%s' are not supported", constraints);
+                       continue;
+               }
+               if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
+                       errorf(&statement->base.source_position,
+                              "some constraints in '%s' are invalid", constraints);
+                       continue;
+               }
+               if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
+                       errorf(&statement->base.source_position,
+                              "no write flag specified for output constraints '%s'",
+                              constraints);
+                       continue;
+               }
+
+               unsigned pos = next_pos++;
+               if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
+                               || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
+                       expression_t *expr = argument->expression;
+                       ir_node      *addr = expression_to_addr(expr);
+                       /* in+output, construct an artifical same_as constraint on the
+                        * input */
+                       if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
+                               char     buf[64];
+                               ir_node *value = get_value_from_lvalue(expr, addr);
+
+                               snprintf(buf, sizeof(buf), "%d", pos);
+
+                               ir_asm_constraint constraint;
+                               constraint.pos              = pos;
+                               constraint.constraint       = new_id_from_str(buf);
+                               constraint.mode             = get_ir_mode(expr->base.type);
+                               tmp_in_constraints[in_size] = constraint;
+                               ins[in_size] = value;
+
+                               ++in_size;
                        }
+
+                       out_exprs[out_size] = expr;
+                       out_addrs[out_size] = addr;
+                       ++out_size;
+               } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
+                       /* pure memory ops need no input (but we have to make sure we
+                        * attach to the memory) */
+                       assert(! (asm_flags &
+                                               (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
+                                                | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
+                       needs_memory = true;
+
+                       /* we need to attach the address to the inputs */
+                       expression_t *expr = argument->expression;
+
+                       ir_asm_constraint constraint;
+                       constraint.pos              = pos;
+                       constraint.constraint       = new_id_from_str(constraints);
+                       constraint.mode             = NULL;
+                       tmp_in_constraints[in_size] = constraint;
+
+                       ins[in_size]          = expression_to_addr(expr);
+                       ++in_size;
+                       continue;
+               } else {
+                       errorf(&statement->base.source_position,
+                              "only modifiers but no place set in constraints '%s'",
+                              constraints);
+                       continue;
                }
-               obstack_1grow(&asm_obst, '\0');
-               const char *constraint_string = obstack_finish(&asm_obst);
 
-               needs_memory |= supports_memop;
-               if (supports_memop) {
+               ir_asm_constraint constraint;
+               constraint.pos        = pos;
+               constraint.constraint = new_id_from_str(constraints);
+               constraint.mode       = get_ir_mode(argument->expression->base.type);
 
+               obstack_grow(&asm_obst, &constraint, sizeof(constraint));
+       }
+       assert(obstack_object_size(&asm_obst)
+                       == out_size * sizeof(ir_asm_constraint));
+       ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
+
+
+       obstack_grow(&asm_obst, tmp_in_constraints,
+                    in_size * sizeof(tmp_in_constraints[0]));
+       free(tmp_in_constraints);
+       /* find and count input and output arguments */
+       argument = statement->inputs;
+       for( ; argument != NULL; argument = argument->next) {
+               const char *constraints = argument->constraints.begin;
+               asm_constraint_flags_t asm_flags
+                       = be_parse_asm_constraints(constraints);
+
+               if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
+                       errorf(&statement->base.source_position,
+                              "some constraints in '%s' are not supported", constraints);
+                       continue;
+               }
+               if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
+                       errorf(&statement->base.source_position,
+                              "some constraints in '%s' are invalid", constraints);
+                       continue;
+               }
+               if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
+                       errorf(&statement->base.source_position,
+                              "write flag specified for input constraints '%s'",
+                              constraints);
+                       continue;
+               }
+
+               ir_node *input;
+               if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
+                               || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
+                       /* we can treat this as "normal" input */
+                       input = expression_to_firm(argument->expression);
+               } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
+                       /* pure memory ops need no input (but we have to make sure we
+                        * attach to the memory) */
+                       assert(! (asm_flags &
+                                               (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
+                                                | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
+                       needs_memory = true;
+                       input = expression_to_addr(argument->expression);
+               } else {
+                       errorf(&statement->base.source_position,
+                              "only modifiers but no place set in constraints '%s'",
+                              constraints);
+                       continue;
                }
+
+               ir_asm_constraint constraint;
+               constraint.pos        = next_pos++;
+               constraint.constraint = new_id_from_str(constraints);
+               constraint.mode       = get_irn_mode(input);
+
+               obstack_grow(&asm_obst, &constraint, sizeof(constraint));
+               ins[in_size++] = input;
+       }
+
+       if (needs_memory) {
+               ir_asm_constraint constraint;
+               constraint.pos        = next_pos++;
+               constraint.constraint = new_id_from_str("");
+               constraint.mode       = mode_M;
+
+               obstack_grow(&asm_obst, &constraint, sizeof(constraint));
+               ins[in_size++] = get_store();
+       }
+
+       assert(obstack_object_size(&asm_obst)
+                       == in_size * sizeof(ir_asm_constraint));
+       ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
+
+       /* create asm node */
+       dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
+
+       ident *asm_text = new_id_from_str(statement->asm_text.begin);
+
+       ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
+                                 out_size, output_constraints,
+                                 n_clobbers, clobbers, asm_text);
+
+       if (statement->is_volatile) {
+               set_irn_pinned(node, op_pin_state_pinned);
+       } else {
+               set_irn_pinned(node, op_pin_state_floats);
+       }
+
+       /* create output projs & connect them */
+       size_t i;
+       for (i = 0; i < out_size; ++i) {
+               const expression_t *out_expr = out_exprs[i];
+               long                pn       = i;
+               ir_mode            *mode     = get_ir_mode(out_expr->base.type);
+               ir_node            *proj     = new_Proj(node, mode, pn);
+               ir_node            *addr     = out_addrs[i];
+
+               set_value_for_expression_addr(out_expr, proj, addr);
+       }
+       if (needs_memory) {
+               ir_node *projm = new_Proj(node, mode_M, i);
+               set_store(projm);
        }
-#endif
 }
 
 static void    ms_try_statement_to_firm(ms_try_statement_t *statement) {
@@ -4944,6 +5099,24 @@ static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_mod
        }
 }
 
+static void add_function_pointer(ir_type *segment, ir_entity *method,
+                                 const char *unique_template)
+{
+       ir_type   *method_type  = get_entity_type(method);
+       ident     *id           = id_unique(unique_template);
+       ir_type   *ptr_type     = new_type_pointer(id, method_type, mode_P_code);
+
+       ident     *ide          = id_unique(unique_template);
+       ir_entity *ptr          = new_entity(segment, ide, ptr_type);
+       ir_graph  *irg          = get_const_code_irg();
+       ir_node   *val          = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
+                                                          method, NULL);
+
+       set_entity_compiler_generated(ptr, 1);
+       set_entity_variability(ptr, variability_constant);
+       set_atomic_ent_value(ptr, val);
+}
+
 /**
  * Create code for a function.
  */
@@ -4954,6 +5127,15 @@ static void create_function(declaration_t *declaration)
        if (declaration->init.statement == NULL)
                return;
 
+       if (declaration->modifiers & DM_CONSTRUCTOR) {
+               ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
+               add_function_pointer(segment, function_entity, "constructor_ptr.%u");
+       }
+       if (declaration->modifiers & DM_DESTRUCTOR) {
+               ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
+               add_function_pointer(segment, function_entity, "destructor_ptr.%u");
+       }
+
        current_function_decl = declaration;
        current_function_name = NULL;
        current_funcsig       = NULL;
@@ -4973,7 +5155,7 @@ static void create_function(declaration_t *declaration)
        /* set inline flags */
        if (declaration->is_inline)
                set_irg_inline_property(irg, irg_inline_recomended);
-       handle_decl_modifier_irg(irg, declaration->decl_modifiers);
+       handle_decl_modifier_irg(irg, declaration->modifiers);
 
        next_value_number_function = 0;
        initialize_function_parameters(declaration);