2010-05-24 Michael Snyder <msnyder@vmware.com>
[binutils-gdb.git] / gdb / dwarf2expr.c
index 9e5ce6154e8071875e8aa430999382e9305014a4..723293bd747656b2badbb5819f1b24b57e1fae39 100644 (file)
@@ -41,6 +41,7 @@ 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;
@@ -87,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;
@@ -141,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);
@@ -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,
@@ -849,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 "