/* Perform non-arithmetic operations on values, for GDB.
- Copyright (C) 1986-2020 Free Software Foundation, Inc.
+ Copyright (C) 1986-2021 Free Software Foundation, Inc.
This file is part of GDB.
return arg2;
}
+/* See value.h. */
+
+gdb_mpq
+value_to_gdb_mpq (struct value *value)
+{
+ struct type *type = check_typedef (value_type (value));
+
+ gdb_mpq result;
+ if (is_floating_type (type))
+ {
+ double d = target_float_to_host_double (value_contents (value),
+ type);
+ mpq_set_d (result.val, d);
+ }
+ else
+ {
+ gdb_assert (is_integral_type (type)
+ || is_fixed_point_type (type));
+
+ gdb_mpz vz;
+ vz.read (gdb::make_array_view (value_contents (value),
+ TYPE_LENGTH (type)),
+ type_byte_order (type), type->is_unsigned ());
+ mpq_set_z (result.val, vz.val);
+
+ if (is_fixed_point_type (type))
+ mpq_mul (result.val, result.val,
+ type->fixed_point_scaling_factor ().val);
+ }
+
+ return result;
+}
+
/* Assuming that TO_TYPE is a fixed point type, return a value
corresponding to the cast of FROM_VAL to that type. */
if (from_type == to_type)
return from_val;
- gdb_mpq vq;
-
- /* Extract the value as a rational number. */
-
- if (is_floating_type (from_type))
- {
- double d = target_float_to_host_double (value_contents (from_val),
- from_type);
- mpq_set_d (vq.val, d);
- }
-
- else if (is_integral_type (from_type) || is_fixed_point_type (from_type))
- {
- gdb_mpz vz;
-
- vz.read (value_contents (from_val), TYPE_LENGTH (from_type),
- type_byte_order (from_type), from_type->is_unsigned ());
- mpq_set_z (vq.val, vz.val);
-
- if (is_fixed_point_type (from_type))
- mpq_mul (vq.val, vq.val, fixed_point_scaling_factor (from_type).val);
- }
-
- else
+ if (!is_floating_type (from_type)
+ && !is_integral_type (from_type)
+ && !is_fixed_point_type (from_type))
error (_("Invalid conversion from type %s to fixed point type %s"),
from_type->name (), to_type->name ());
+ gdb_mpq vq = value_to_gdb_mpq (from_val);
+
/* Divide that value by the scaling factor to obtain the unscaled
value, first in rational form, and then in integer form. */
- mpq_div (vq.val, vq.val, fixed_point_scaling_factor (to_type).val);
+ mpq_div (vq.val, vq.val, to_type->fixed_point_scaling_factor ().val);
gdb_mpz unscaled = vq.get_rounded ();
/* Finally, create the result value, and pack the unscaled value
in it. */
struct value *result = allocate_value (to_type);
- unscaled.write (value_contents_raw (result),
- TYPE_LENGTH (to_type), type_byte_order (to_type),
+ unscaled.write (gdb::make_array_view (value_contents_raw (result),
+ TYPE_LENGTH (to_type)),
+ type_byte_order (to_type),
to_type->is_unsigned ());
return result;
int convert_to_boolean = 0;
- if (value_type (arg2) == type)
- return arg2;
+ /* TYPE might be equal in meaning to the existing type of ARG2, but for
+ many reasons, might be a different type object (e.g. TYPE might be a
+ gdbarch owned type, while VALUE_TYPE (ARG2) could be an objfile owned
+ type).
+
+ In this case we want to preserve the LVAL of ARG2 as this allows the
+ resulting value to be used in more places. We do this by calling
+ VALUE_COPY if appropriate. */
+ if (types_deeply_equal (value_type (arg2), type))
+ {
+ /* If the types are exactly equal then we can avoid creating a new
+ value completely. */
+ if (value_type (arg2) != type)
+ {
+ arg2 = value_copy (arg2);
+ deprecated_set_value_type (arg2, type);
+ }
+ return arg2;
+ }
if (is_fixed_point_type (type))
return value_cast_to_fixed_point (type, arg2);
int val_length = TYPE_LENGTH (type2);
LONGEST low_bound, high_bound, new_length;
- if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
+ if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
low_bound = 0, high_bound = 0;
new_length = val_length / element_length;
if (val_length % element_length != 0)
gdb_mpq fp_val;
fp_val.read_fixed_point
- (value_contents (arg2), TYPE_LENGTH (type2),
+ (gdb::make_array_view (value_contents (arg2), TYPE_LENGTH (type2)),
type_byte_order (type2), type2->is_unsigned (),
- fixed_point_scaling_factor (type2));
+ type2->fixed_point_scaling_factor ());
struct value *v = allocate_value (to_type);
target_float_from_host_double (value_contents_raw (v),
otherwise occur when dealing with a target having two byte
pointers and four byte addresses. */
- int addr_bit = gdbarch_addr_bit (get_type_arch (type2));
+ int addr_bit = gdbarch_addr_bit (type2->arch ());
LONGEST longest = value_as_long (arg2);
if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
enum target_xfer_status status;
ULONGEST xfered_partial;
- status = target_xfer_partial (current_top_target (),
+ status = target_xfer_partial (current_inferior ()->top_target (),
object, NULL,
buffer + xfered_total * unit_size, NULL,
memaddr + xfered_total,
{
case lval_internalvar:
set_internalvar (VALUE_INTERNALVAR (toval), fromval);
- return value_of_internalvar (get_type_arch (type),
+ return value_of_internalvar (type->arch (),
VALUE_INTERNALVAR (toval));
case lval_internalvar_component:
{
struct value *parent = value_parent (toval);
LONGEST offset = value_offset (parent) + value_offset (toval);
- int changed_len;
+ size_t changed_len;
gdb_byte buffer[sizeof (LONGEST)];
int optim, unavail;
+ HOST_CHAR_BIT - 1)
/ HOST_CHAR_BIT;
- if (changed_len > (int) sizeof (LONGEST))
+ if (changed_len > sizeof (LONGEST))
error (_("Can't handle bitfields which "
"don't fit in a %d bit word."),
(int) sizeof (LONGEST) * HOST_CHAR_BIT);
if (!get_frame_register_bytes (frame, value_reg, offset,
- changed_len, buffer,
+ {buffer, changed_len},
&optim, &unavail))
{
if (optim)
value_bitpos (toval), value_bitsize (toval));
put_frame_register_bytes (frame, value_reg, offset,
- changed_len, buffer);
+ {buffer, changed_len});
}
else
{
}
else
{
+ gdb::array_view<const gdb_byte> contents
+ = gdb::make_array_view (value_contents (fromval),
+ TYPE_LENGTH (type));
put_frame_register_bytes (frame, value_reg,
value_offset (toval),
- TYPE_LENGTH (type),
- value_contents (fromval));
+ contents);
}
}
case lval_register:
case lval_computed:
- gdb::observers::target_changed.notify (current_top_target ());
+ gdb::observers::target_changed.notify
+ (current_inferior ()->top_target ());
/* Having destroyed the frame cache, restore the selected
frame. */
int name_len = strlen (name);
gdb_assert (type->code () == TYPE_CODE_ENUM
- && TYPE_DECLARED_CLASS (type));
+ && type->is_declared_class ());
for (i = TYPE_N_BASECLASSES (type); i < type->num_fields (); ++i)
{
error (_("array not associated"));
range_type = array_type->index_type ();
- if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
+ if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
error (_("slice from bad array or bitstring"));
if (lowbound < lowerbound || length < 0