2010-05-24 Michael Snyder <msnyder@vmware.com>
[binutils-gdb.git] / gdb / dwarf2expr.c
index 46bc9d758b267b44268b5b56edbb8dc3d8cc49d0..723293bd747656b2badbb5819f1b24b57e1fae39 100644 (file)
@@ -1,6 +1,6 @@
 /* DWARF 2 Expression Evaluator.
 
-   Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009
+   Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
 
    Contributed by Daniel Berlin (dan@dberlin.org)
@@ -41,10 +41,12 @@ struct dwarf_expr_context *
 new_dwarf_expr_context (void)
 {
   struct dwarf_expr_context *retval;
+
   retval = xcalloc (1, sizeof (struct dwarf_expr_context));
   retval->stack_len = 0;
   retval->stack_allocated = 10;
-  retval->stack = xmalloc (retval->stack_allocated * sizeof (CORE_ADDR));
+  retval->stack = xmalloc (retval->stack_allocated
+                          * sizeof (struct dwarf_stack_value));
   retval->num_pieces = 0;
   retval->pieces = 0;
   retval->max_recursion_depth = 0x100;
@@ -86,6 +88,7 @@ dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
   if (ctx->stack_len + need > ctx->stack_allocated)
     {
       size_t newlen = ctx->stack_len + need + 10;
+
       ctx->stack = xrealloc (ctx->stack,
                             newlen * sizeof (struct dwarf_stack_value));
       ctx->stack_allocated = newlen;
@@ -140,30 +143,45 @@ dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n)
 
 }
 
+/* Return true if the expression stack is empty.  */
+
+static int
+dwarf_expr_stack_empty_p (struct dwarf_expr_context *ctx)
+{
+  return ctx->stack_len == 0;
+}
+
 /* Add a new piece to CTX's piece list.  */
 static void
-add_piece (struct dwarf_expr_context *ctx, ULONGEST size)
+add_piece (struct dwarf_expr_context *ctx, ULONGEST size, ULONGEST offset)
 {
   struct dwarf_expr_piece *p;
 
   ctx->num_pieces++;
 
-  if (ctx->pieces)
-    ctx->pieces = xrealloc (ctx->pieces,
-                            (ctx->num_pieces
-                             * sizeof (struct dwarf_expr_piece)));
-  else
-    ctx->pieces = xmalloc (ctx->num_pieces
-                           * sizeof (struct dwarf_expr_piece));
+  ctx->pieces = xrealloc (ctx->pieces,
+                         (ctx->num_pieces
+                          * sizeof (struct dwarf_expr_piece)));
 
   p = &ctx->pieces[ctx->num_pieces - 1];
   p->location = ctx->location;
   p->size = size;
+  p->offset = offset;
+
   if (p->location == DWARF_VALUE_LITERAL)
     {
       p->v.literal.data = ctx->data;
       p->v.literal.length = ctx->len;
     }
+  else if (dwarf_expr_stack_empty_p (ctx))
+    {
+      p->location = DWARF_VALUE_OPTIMIZED_OUT;
+      /* Also reset the context's location, for our callers.  This is
+        a somewhat strange approach, but this lets us avoid setting
+        the location to DWARF_VALUE_MEMORY in all the individual
+        cases in the evaluator.  */
+      ctx->location = DWARF_VALUE_OPTIMIZED_OUT;
+    }
   else
     {
       p->v.expr.value = dwarf_expr_fetch (ctx, 0);
@@ -249,7 +267,6 @@ dwarf2_read_address (struct gdbarch *gdbarch, gdb_byte *buf,
                     gdb_byte *buf_end, int addr_size)
 {
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
-  CORE_ADDR result;
 
   if (buf_end - buf < addr_size)
     error (_("dwarf2_read_address: Corrupted DWARF expression."));
@@ -337,6 +354,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
                  gdb_byte *op_ptr, gdb_byte *op_end)
 {
   enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
+
   ctx->location = DWARF_VALUE_MEMORY;
   ctx->initialized = 1;  /* Default is initialized.  */
 
@@ -479,9 +497,11 @@ execute_stack_op (struct dwarf_expr_context *ctx,
        case DW_OP_reg31:
          if (op_ptr != op_end 
              && *op_ptr != DW_OP_piece
+             && *op_ptr != DW_OP_bit_piece
              && *op_ptr != DW_OP_GNU_uninit)
            error (_("DWARF-2 expression error: DW_OP_reg operations must be "
-                  "used either alone or in conjuction with DW_OP_piece."));
+                    "used either alone or in conjuction with DW_OP_piece "
+                    "or DW_OP_bit_piece."));
 
          result = op - DW_OP_reg0;
          ctx->location = DWARF_VALUE_REGISTER;
@@ -498,6 +518,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
        case DW_OP_implicit_value:
          {
            ULONGEST len;
+
            op_ptr = read_uleb128 (op_ptr, op_end, &len);
            if (op_ptr + len > op_end)
              error (_("DW_OP_implicit_value: too few bytes available."));
@@ -655,6 +676,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
            case DW_OP_deref:
              {
                gdb_byte *buf = alloca (ctx->addr_size);
+
                (ctx->read_mem) (ctx->baton, buf, result, ctx->addr_size);
                result = dwarf2_read_address (ctx->gdbarch,
                                              buf, buf + ctx->addr_size,
@@ -666,6 +688,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
              {
                int addr_size = *op_ptr++;
                gdb_byte *buf = alloca (addr_size);
+
                (ctx->read_mem) (ctx->baton, buf, result, addr_size);
                result = dwarf2_read_address (ctx->gdbarch,
                                              buf, buf + addr_size,
@@ -712,7 +735,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
               the right width.  */
            CORE_ADDR first, second;
            enum exp_opcode binop;
-           struct value *val1, *val2;
+           struct value *val1 = NULL, *val2 = NULL;
            struct type *stype, *utype;
 
            second = dwarf_expr_fetch (ctx, 0);
@@ -723,8 +746,6 @@ execute_stack_op (struct dwarf_expr_context *ctx,
 
            utype = unsigned_address_type (ctx->gdbarch, ctx->addr_size);
            stype = signed_address_type (ctx->gdbarch, ctx->addr_size);
-           val1 = value_from_longest (utype, first);
-           val2 = value_from_longest (utype, second);
 
            switch (op)
              {
@@ -733,6 +754,8 @@ execute_stack_op (struct dwarf_expr_context *ctx,
                break;
              case DW_OP_div:
                binop = BINOP_DIV;
+               val1 = value_from_longest (stype, first);
+               val2 = value_from_longest (stype, second);
                 break;
              case DW_OP_minus:
                binop = BINOP_SUB;
@@ -764,26 +787,45 @@ execute_stack_op (struct dwarf_expr_context *ctx,
                break;
              case DW_OP_le:
                binop = BINOP_LEQ;
+               val1 = value_from_longest (stype, first);
+               val2 = value_from_longest (stype, second);
                break;
              case DW_OP_ge:
                binop = BINOP_GEQ;
+               val1 = value_from_longest (stype, first);
+               val2 = value_from_longest (stype, second);
                break;
              case DW_OP_eq:
                binop = BINOP_EQUAL;
+               val1 = value_from_longest (stype, first);
+               val2 = value_from_longest (stype, second);
                break;
              case DW_OP_lt:
                binop = BINOP_LESS;
+               val1 = value_from_longest (stype, first);
+               val2 = value_from_longest (stype, second);
                break;
              case DW_OP_gt:
                binop = BINOP_GTR;
+               val1 = value_from_longest (stype, first);
+               val2 = value_from_longest (stype, second);
                break;
              case DW_OP_ne:
                binop = BINOP_NOTEQUAL;
+               val1 = value_from_longest (stype, first);
+               val2 = value_from_longest (stype, second);
                break;
              default:
                internal_error (__FILE__, __LINE__,
                                _("Can't be reached."));
              }
+
+           /* We use unsigned operands by default.  */
+           if (val1 == NULL)
+             val1 = value_from_longest (utype, first);
+           if (val2 == NULL)
+             val2 = value_from_longest (utype, second);
+
            result = value_as_long (value_binop (val1, val2, binop));
          }
          break;
@@ -830,16 +872,35 @@ execute_stack_op (struct dwarf_expr_context *ctx,
 
             /* Record the piece.  */
             op_ptr = read_uleb128 (op_ptr, op_end, &size);
-           add_piece (ctx, size);
+           add_piece (ctx, 8 * size, 0);
 
             /* Pop off the address/regnum, and reset the location
               type.  */
-           if (ctx->location != DWARF_VALUE_LITERAL)
+           if (ctx->location != DWARF_VALUE_LITERAL
+               && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
              dwarf_expr_pop (ctx);
             ctx->location = DWARF_VALUE_MEMORY;
           }
           goto no_push;
 
+       case DW_OP_bit_piece:
+         {
+           ULONGEST size, offset;
+
+            /* Record the piece.  */
+           op_ptr = read_uleb128 (op_ptr, op_end, &size);
+           op_ptr = read_uleb128 (op_ptr, op_end, &offset);
+           add_piece (ctx, size, offset);
+
+            /* Pop off the address/regnum, and reset the location
+              type.  */
+           if (ctx->location != DWARF_VALUE_LITERAL
+               && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
+             dwarf_expr_pop (ctx);
+            ctx->location = DWARF_VALUE_MEMORY;
+         }
+         goto no_push;
+
        case DW_OP_GNU_uninit:
          if (op_ptr != op_end)
            error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "