Add support to GDB for the Renesas rl78 architecture.
[external/binutils.git] / gdb / dwarf2expr.c
index 21a8363..222fcc3 100644 (file)
@@ -1,7 +1,7 @@
 /* DWARF 2 Expression Evaluator.
 
-   Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010, 2011
-   Free Software Foundation, Inc.
+   Copyright (C) 2001-2003, 2005, 2007-2012 Free Software Foundation,
+   Inc.
 
    Contributed by Daniel Berlin (dan@dberlin.org)
 
@@ -371,7 +371,7 @@ dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
 
 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
    by R, and return the new value of BUF.  Verify that it doesn't extend
-   past BUF_END.  */
+   past BUF_END.  R can be NULL, the constant is then only skipped.  */
 
 const gdb_byte *
 read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end, ULONGEST * r)
@@ -391,13 +391,14 @@ read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end, ULONGEST * r)
        break;
       shift += 7;
     }
-  *r = result;
+  if (r)
+    *r = result;
   return buf;
 }
 
 /* Decode the signed LEB128 constant at BUF into the variable pointed to
    by R, and return the new value of BUF.  Verify that it doesn't extend
-   past BUF_END.  */
+   past BUF_END.  R can be NULL, the constant is then only skipped.  */
 
 const gdb_byte *
 read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end, LONGEST * r)
@@ -420,7 +421,8 @@ read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end, LONGEST * r)
   if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
     result |= -(((LONGEST) 1) << shift);
 
-  *r = result;
+  if (r)
+    *r = result;
   return buf;
 }
 \f
@@ -466,9 +468,9 @@ dwarf_get_base_type (struct dwarf_expr_context *ctx, ULONGEST die, int size)
 {
   struct type *result;
 
-  if (ctx->get_base_type)
+  if (ctx->funcs->get_base_type)
     {
-      result = ctx->get_base_type (ctx, die);
+      result = ctx->funcs->get_base_type (ctx, die);
       if (result == NULL)
        error (_("Could not find type for DW_OP_GNU_const_type"));
       if (size != 0 && TYPE_LENGTH (result) != size)
@@ -481,6 +483,160 @@ dwarf_get_base_type (struct dwarf_expr_context *ctx, ULONGEST die, int size)
   return result;
 }
 
+/* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
+   DWARF register number.  Otherwise return -1.  */
+
+int
+dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
+{
+  ULONGEST dwarf_reg;
+
+  if (buf_end <= buf)
+    return -1;
+  if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
+    {
+      if (buf_end - buf != 1)
+       return -1;
+      return *buf - DW_OP_reg0;
+    }
+
+  if (*buf == DW_OP_GNU_regval_type)
+    {
+      buf++;
+      buf = read_uleb128 (buf, buf_end, &dwarf_reg);
+      buf = read_uleb128 (buf, buf_end, NULL);
+    }
+  else if (*buf == DW_OP_regx)
+    {
+      buf++;
+      buf = read_uleb128 (buf, buf_end, &dwarf_reg);
+    }
+  else
+    return -1;
+  if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
+    return -1;
+  return dwarf_reg;
+}
+
+/* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
+   DW_OP_deref* return the DWARF register number.  Otherwise return -1.
+   DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
+   size from DW_OP_deref_size.  */
+
+int
+dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
+                               CORE_ADDR *deref_size_return)
+{
+  ULONGEST dwarf_reg;
+  LONGEST offset;
+
+  if (buf_end <= buf)
+    return -1;
+  if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
+    {
+      dwarf_reg = *buf - DW_OP_breg0;
+      buf++;
+    }
+  else if (*buf == DW_OP_bregx)
+    {
+      buf++;
+      buf = read_uleb128 (buf, buf_end, &dwarf_reg);
+      if ((int) dwarf_reg != dwarf_reg)
+       return -1;
+    }
+  else
+    return -1;
+
+  buf = read_sleb128 (buf, buf_end, &offset);
+  if (offset != 0)
+    return -1;
+
+  if (buf >= buf_end)
+    return -1;
+
+  if (*buf == DW_OP_deref)
+    {
+      buf++;
+      *deref_size_return = -1;
+    }
+  else if (*buf == DW_OP_deref_size)
+    {
+      buf++;
+      if (buf >= buf_end)
+       return -1;
+      *deref_size_return = *buf++;
+    }
+  else
+    return -1;
+
+  if (buf != buf_end)
+    return -1;
+
+  return dwarf_reg;
+}
+
+/* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
+   in FB_OFFSET_RETURN with the X offset and return 1.  Otherwise return 0.  */
+
+int
+dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
+                         CORE_ADDR *fb_offset_return)
+{
+  LONGEST fb_offset;
+
+  if (buf_end <= buf)
+    return 0;
+
+  if (*buf != DW_OP_fbreg)
+    return 0;
+  buf++;
+
+  buf = read_sleb128 (buf, buf_end, &fb_offset);
+  *fb_offset_return = fb_offset;
+  if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
+    return 0;
+
+  return 1;
+}
+
+/* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
+   in SP_OFFSET_RETURN with the X offset and return 1.  Otherwise return 0.
+   The matched SP register number depends on GDBARCH.  */
+
+int
+dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
+                         const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
+{
+  ULONGEST dwarf_reg;
+  LONGEST sp_offset;
+
+  if (buf_end <= buf)
+    return 0;
+  if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
+    {
+      dwarf_reg = *buf - DW_OP_breg0;
+      buf++;
+    }
+  else
+    {
+      if (*buf != DW_OP_bregx)
+       return 0;
+      buf++;
+      buf = read_uleb128 (buf, buf_end, &dwarf_reg);
+    }
+
+  if (gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_reg)
+      != gdbarch_sp_regnum (gdbarch))
+    return 0;
+
+  buf = read_sleb128 (buf, buf_end, &sp_offset);
+  *sp_offset_return = sp_offset;
+  if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
+    return 0;
+
+  return 1;
+}
+
 /* The engine for the expression evaluator.  Using the context in CTX,
    evaluate the expression between OP_PTR and OP_END.  */
 
@@ -709,10 +865,14 @@ execute_stack_op (struct dwarf_expr_context *ctx,
            ULONGEST die;
            LONGEST len;
 
+           if (ctx->ref_addr_size == -1)
+             error (_("DWARF-2 expression error: DW_OP_GNU_implicit_pointer "
+                      "is not allowed in frame context"));
+
            /* The referred-to DIE.  */
-           ctx->len = extract_unsigned_integer (op_ptr, ctx->addr_size,
+           ctx->len = extract_unsigned_integer (op_ptr, ctx->ref_addr_size,
                                                 byte_order);
-           op_ptr += ctx->addr_size;
+           op_ptr += ctx->ref_addr_size;
 
            /* The byte offset into the data.  */
            op_ptr = read_sleb128 (op_ptr, op_end, &len);
@@ -759,7 +919,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
        case DW_OP_breg31:
          {
            op_ptr = read_sleb128 (op_ptr, op_end, &offset);
-           result = (ctx->read_reg) (ctx->baton, op - DW_OP_breg0);
+           result = (ctx->funcs->read_reg) (ctx->baton, op - DW_OP_breg0);
            result += offset;
            result_val = value_from_ulongest (address_type, result);
          }
@@ -768,7 +928,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
          {
            op_ptr = read_uleb128 (op_ptr, op_end, &reg);
            op_ptr = read_sleb128 (op_ptr, op_end, &offset);
-           result = (ctx->read_reg) (ctx->baton, reg);
+           result = (ctx->funcs->read_reg) (ctx->baton, reg);
            result += offset;
            result_val = value_from_ulongest (address_type, result);
          }
@@ -788,14 +948,13 @@ execute_stack_op (struct dwarf_expr_context *ctx,
            /* FIXME: cagney/2003-03-26: This code should be using
                get_frame_base_address(), and then implement a dwarf2
                specific this_base method.  */
-           (ctx->get_frame_base) (ctx->baton, &datastart, &datalen);
+           (ctx->funcs->get_frame_base) (ctx->baton, &datastart, &datalen);
            dwarf_expr_eval (ctx, datastart, datalen);
            if (ctx->location == DWARF_VALUE_MEMORY)
              result = dwarf_expr_fetch_address (ctx, 0);
            else if (ctx->location == DWARF_VALUE_REGISTER)
-             result
-               = (ctx->read_reg) (ctx->baton,
-                                  value_as_long (dwarf_expr_fetch (ctx, 0)));
+             result = (ctx->funcs->read_reg) (ctx->baton,
+                                    value_as_long (dwarf_expr_fetch (ctx, 0)));
            else
              error (_("Not implemented: computing frame "
                       "base using explicit value operator"));
@@ -880,7 +1039,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
            else
              type = address_type;
 
-           (ctx->read_mem) (ctx->baton, buf, addr, addr_size);
+           (ctx->funcs->read_mem) (ctx->baton, buf, addr, addr_size);
 
            /* If the size of the object read from memory is different
               from the type length, we need to zero-extend it.  */
@@ -1089,7 +1248,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
          break;
 
        case DW_OP_call_frame_cfa:
-         result = (ctx->get_frame_cfa) (ctx->baton);
+         result = (ctx->funcs->get_frame_cfa) (ctx->baton);
          result_val = value_from_ulongest (address_type, result);
          in_stack_memory = 1;
          break;
@@ -1105,7 +1264,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
          returned.  */
          result = value_as_long (dwarf_expr_fetch (ctx, 0));
          dwarf_expr_pop (ctx);
-         result = (ctx->get_tls_address) (ctx->baton, result);
+         result = (ctx->funcs->get_tls_address) (ctx->baton, result);
          result_val = value_from_ulongest (address_type, result);
          break;
 
@@ -1178,21 +1337,52 @@ execute_stack_op (struct dwarf_expr_context *ctx,
        case DW_OP_call2:
          result = extract_unsigned_integer (op_ptr, 2, byte_order);
          op_ptr += 2;
-         ctx->dwarf_call (ctx, result);
+         ctx->funcs->dwarf_call (ctx, result);
          goto no_push;
 
        case DW_OP_call4:
          result = extract_unsigned_integer (op_ptr, 4, byte_order);
          op_ptr += 4;
-         ctx->dwarf_call (ctx, result);
+         ctx->funcs->dwarf_call (ctx, result);
          goto no_push;
        
        case DW_OP_GNU_entry_value:
-         /* This operation is not yet supported by GDB.  */
-         ctx->location = DWARF_VALUE_OPTIMIZED_OUT;
-         ctx->stack_len = 0;
-         ctx->num_pieces = 0;
-         goto abort_expression;
+         {
+           ULONGEST len;
+           int dwarf_reg;
+           CORE_ADDR deref_size;
+
+           op_ptr = read_uleb128 (op_ptr, op_end, &len);
+           if (op_ptr + len > op_end)
+             error (_("DW_OP_GNU_entry_value: too few bytes available."));
+
+           dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
+           if (dwarf_reg != -1)
+             {
+               op_ptr += len;
+               ctx->funcs->push_dwarf_reg_entry_value (ctx, dwarf_reg,
+                                                       0 /* unused */,
+                                                       -1 /* deref_size */);
+               goto no_push;
+             }
+
+           dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr, op_ptr + len,
+                                                       &deref_size);
+           if (dwarf_reg != -1)
+             {
+               if (deref_size == -1)
+                 deref_size = ctx->addr_size;
+               op_ptr += len;
+               ctx->funcs->push_dwarf_reg_entry_value (ctx, dwarf_reg,
+                                                       0 /* unused */,
+                                                       deref_size);
+               goto no_push;
+             }
+
+           error (_("DWARF-2 expression error: DW_OP_GNU_entry_value is "
+                    "supported only for single DW_OP_reg* "
+                    "or for DW_OP_breg*(0)+DW_OP_deref*"));
+         }
 
        case DW_OP_GNU_const_type:
          {
@@ -1220,7 +1410,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
            op_ptr = read_uleb128 (op_ptr, op_end, &type_die);
 
            type = dwarf_get_base_type (ctx, type_die, 0);
-           result = (ctx->read_reg) (ctx->baton, reg);
+           result = (ctx->funcs->read_reg) (ctx->baton, reg);
            result_val = value_from_ulongest (address_type, result);
            result_val = value_from_contents (type,
                                              value_contents_all (result_val));
@@ -1281,6 +1471,66 @@ abort_expression:
   gdb_assert (ctx->recursion_depth >= 0);
 }
 
+/* Stub dwarf_expr_context_funcs.get_frame_base implementation.  */
+
+void
+ctx_no_get_frame_base (void *baton, const gdb_byte **start, size_t *length)
+{
+  error (_("%s is invalid in this context"), "DW_OP_fbreg");
+}
+
+/* Stub dwarf_expr_context_funcs.get_frame_cfa implementation.  */
+
+CORE_ADDR
+ctx_no_get_frame_cfa (void *baton)
+{
+  error (_("%s is invalid in this context"), "DW_OP_call_frame_cfa");
+}
+
+/* Stub dwarf_expr_context_funcs.get_frame_pc implementation.  */
+
+CORE_ADDR
+ctx_no_get_frame_pc (void *baton)
+{
+  error (_("%s is invalid in this context"), "DW_OP_GNU_implicit_pointer");
+}
+
+/* Stub dwarf_expr_context_funcs.get_tls_address implementation.  */
+
+CORE_ADDR
+ctx_no_get_tls_address (void *baton, CORE_ADDR offset)
+{
+  error (_("%s is invalid in this context"), "DW_OP_GNU_push_tls_address");
+}
+
+/* Stub dwarf_expr_context_funcs.dwarf_call implementation.  */
+
+void
+ctx_no_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
+{
+  error (_("%s is invalid in this context"), "DW_OP_call*");
+}
+
+/* Stub dwarf_expr_context_funcs.get_base_type implementation.  */
+
+struct type *
+ctx_no_get_base_type (struct dwarf_expr_context *ctx, size_t die)
+{
+  error (_("Support for typed DWARF is not supported in this context"));
+}
+
+/* Stub dwarf_expr_context_funcs.push_dwarf_block_entry_value
+   implementation.  */
+
+void
+ctx_no_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
+                                  int dwarf_reg, CORE_ADDR fb_offset,
+                                  int deref_size)
+{
+  internal_error (__FILE__, __LINE__,
+                 _("Support for DW_OP_GNU_entry_value is unimplemented"));
+}
+
 void
 _initialize_dwarf2expr (void)
 {