/* Perform arithmetic and other operations on values, for GDB.
- Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
- 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009,
- 2010, 2011 Free Software Foundation, Inc.
+ Copyright (C) 1986, 1988-2005, 2007-2012 Free Software Foundation,
+ Inc.
This file is part of GDB.
#include "exceptions.h"
/* Define whether or not the C operator '/' truncates towards zero for
- differently signed operands (truncation direction is undefined in C). */
+ differently signed operands (truncation direction is undefined in C). */
#ifndef TRUNCATION_TOWARDS_ZERO
#define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
If the pointer type is void *, then return 1.
If the target type is incomplete, then error out.
This isn't a general purpose function, but just a
- helper for value_ptradd.
-*/
+ helper for value_ptradd. */
static LONGEST
find_size_for_pointer_math (struct type *ptr_type)
sz = 1;
else
{
- char *name;
+ const char *name;
name = TYPE_NAME (ptr_target);
if (name == NULL)
ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
current language supports C-style arrays, it may also be TYPE_CODE_PTR.
- To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript.
See comments in value_coerce_array() for rationale for reason for
doing lower bounds adjustment here rather than there.
FIXME: Perhaps we should validate that the index is valid and if
- verbosity is set, warn about invalid indices (but still use them). */
+ verbosity is set, warn about invalid indices (but still use them). */
struct value *
value_subscript (struct value *array, LONGEST index)
&& elt_offs >= TYPE_LENGTH (array_type)))
error (_("no such vector element"));
- v = allocate_value (elt_type);
if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
- set_value_lazy (v, 1);
+ v = allocate_value_lazy (elt_type);
else
- memcpy (value_contents_writeable (v),
- value_contents (array) + elt_offs, elt_size);
+ {
+ v = allocate_value (elt_type);
+ value_contents_copy (v, value_embedded_offset (v),
+ array, value_embedded_offset (array) + elt_offs,
+ elt_size);
+ }
set_value_component_location (v, array);
VALUE_REGNUM (v) = VALUE_REGNUM (array);
return v;
}
-/* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */
-
-struct value *
-value_bitstring_subscript (struct type *type,
- struct value *bitstring, LONGEST index)
-{
-
- struct type *bitstring_type, *range_type;
- struct value *v;
- int offset, byte, bit_index;
- LONGEST lowerbound, upperbound;
-
- bitstring_type = check_typedef (value_type (bitstring));
- gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING);
-
- range_type = TYPE_INDEX_TYPE (bitstring_type);
- get_discrete_bounds (range_type, &lowerbound, &upperbound);
- if (index < lowerbound || index > upperbound)
- error (_("bitstring index out of range"));
-
- index -= lowerbound;
- offset = index / TARGET_CHAR_BIT;
- byte = *((char *) value_contents (bitstring) + offset);
-
- bit_index = index % TARGET_CHAR_BIT;
- byte >>= (gdbarch_bits_big_endian (get_type_arch (bitstring_type)) ?
- TARGET_CHAR_BIT - 1 - bit_index : bit_index);
-
- v = value_from_longest (type, byte & 1);
-
- set_value_bitpos (v, bit_index);
- set_value_bitsize (v, 1);
- set_value_component_location (v, bitstring);
- VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring);
-
- set_value_offset (v, offset + value_offset (bitstring));
-
- return v;
-}
-
\f
/* Check to see if either argument is a structure, or a reference to
one. This is called so we know whether to go ahead with the normal
if (TYPE_CODE (type1) == TYPE_CODE_REF)
type1 = check_typedef (TYPE_TARGET_TYPE (type1));
- type2 = check_typedef (type1);
+ type2 = check_typedef (type2);
if (TYPE_CODE (type2) == TYPE_CODE_REF)
type2 = check_typedef (TYPE_TARGET_TYPE (type2));
if (op == UNOP_ADDR)
return 0;
type1 = check_typedef (value_type (arg1));
- for (;;)
- {
- if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
- return 1;
- else if (TYPE_CODE (type1) == TYPE_CODE_REF)
- type1 = TYPE_TARGET_TYPE (type1);
- else
- return 0;
- }
+ if (TYPE_CODE (type1) == TYPE_CODE_REF)
+ type1 = check_typedef (TYPE_TARGET_TYPE (type1));
+ return TYPE_CODE (type1) == TYPE_CODE_STRUCT;
}
/* Try to find an operator named OPERATOR which takes NARGS arguments
struct symbol *symp = NULL;
struct value *valp = NULL;
- struct type **arg_types;
- int i;
- arg_types = (struct type **) alloca (nargs * (sizeof (struct type *)));
- /* Prepare list of argument types for overload resolution */
- for (i = 0; i < nargs; i++)
- arg_types[i] = value_type (args[i]);
-
- find_overload_match (arg_types, nargs, operator, BOTH /* could be method */,
+ find_overload_match (args, nargs, operator, BOTH /* could be method */,
0 /* strict match */, &args[0], /* objp */
NULL /* pass NULL symbol since symbol is unknown */,
&valp, &symp, static_memfuncp, 0);
argvec[2] = arg2;
argvec[3] = 0;
- /* make the right function name up */
+ /* Make the right function name up. */
strcpy (tstr, "operator__");
ptr = tstr + 8;
switch (op)
}
/* We know that arg1 is a structure, so try to find a unary user
- defined operator that matches the operator in question.
+ defined operator that matches the operator in question.
Create an argument vector that calls arg1.operator @ (arg1)
and return that value (where '@' is (almost) any unary operator which
is legal for GNU C++). */
nargs = 1;
- /* make the right function name up */
+ /* Make the right function name up. */
strcpy (tstr, "operator__");
ptr = tstr + 8;
strcpy (mangle_tstr, "__");
values of length 1.
(3) Character values are also allowed and are treated as character
- string values of length 1.
- */
+ string values of length 1. */
struct value *
value_concat (struct value *arg1, struct value *arg2)
or a repeat count and a value to be repeated. INVAL1 is set to the
first of two concatenated values, or the repeat count. INVAL2 is set
to the second of the two concatenated values or the value to be
- repeated. */
+ repeated. */
if (TYPE_CODE (type2) == TYPE_CODE_INT)
{
inval2 = arg2;
}
- /* Now process the input values. */
+ /* Now process the input values. */
if (TYPE_CODE (type1) == TYPE_CODE_INT)
{
/* We have a repeat count. Validate the second value and then
- construct a value repeated that many times. */
+ construct a value repeated that many times. */
if (TYPE_CODE (type2) == TYPE_CODE_STRING
|| TYPE_CODE (type2) == TYPE_CODE_CHAR)
{
+ struct cleanup *back_to;
+
count = longest_to_int (value_as_long (inval1));
inval2len = TYPE_LENGTH (type2);
- ptr = (char *) alloca (count * inval2len);
+ ptr = (char *) xmalloc (count * inval2len);
+ back_to = make_cleanup (xfree, ptr);
if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
{
char_type = type2;
}
}
outval = value_string (ptr, count * inval2len, char_type);
+ do_cleanups (back_to);
}
- else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
- || TYPE_CODE (type2) == TYPE_CODE_BOOL)
+ else if (TYPE_CODE (type2) == TYPE_CODE_BOOL)
{
- error (_("unimplemented support for bitstring/boolean repeats"));
+ error (_("unimplemented support for boolean repeats"));
}
else
{
else if (TYPE_CODE (type1) == TYPE_CODE_STRING
|| TYPE_CODE (type1) == TYPE_CODE_CHAR)
{
- /* We have two character strings to concatenate. */
+ struct cleanup *back_to;
+
+ /* We have two character strings to concatenate. */
if (TYPE_CODE (type2) != TYPE_CODE_STRING
&& TYPE_CODE (type2) != TYPE_CODE_CHAR)
{
}
inval1len = TYPE_LENGTH (type1);
inval2len = TYPE_LENGTH (type2);
- ptr = (char *) alloca (inval1len + inval2len);
+ ptr = (char *) xmalloc (inval1len + inval2len);
+ back_to = make_cleanup (xfree, ptr);
if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
{
char_type = type1;
memcpy (ptr + inval1len, value_contents (inval2), inval2len);
}
outval = value_string (ptr, inval1len + inval2len, char_type);
+ do_cleanups (back_to);
}
- else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
- || TYPE_CODE (type1) == TYPE_CODE_BOOL)
+ else if (TYPE_CODE (type1) == TYPE_CODE_BOOL)
{
- /* We have two bitstrings to concatenate. */
- if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
- && TYPE_CODE (type2) != TYPE_CODE_BOOL)
+ /* We have two bitstrings to concatenate. */
+ if (TYPE_CODE (type2) != TYPE_CODE_BOOL)
{
- error (_("Bitstrings or booleans can only be concatenated "
+ error (_("Booleans can only be concatenated "
"with other bitstrings or booleans."));
}
- error (_("unimplemented support for bitstring/boolean concatenation."));
+ error (_("unimplemented support for boolean concatenation."));
}
else
{
- /* We don't know how to concatenate these operands. */
+ /* We don't know how to concatenate these operands. */
error (_("illegal operands for concatenation."));
}
return (outval);
\f
/* Integer exponentiation: V1**V2, where both arguments are
integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
+
static LONGEST
integer_pow (LONGEST v1, LONGEST v2)
{
}
else
{
- /* The Russian Peasant's Algorithm */
+ /* The Russian Peasant's Algorithm. */
LONGEST v;
v = 1;
/* Integer exponentiation: V1**V2, where both arguments are
integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
+
static ULONGEST
uinteger_pow (ULONGEST v1, LONGEST v2)
{
}
else
{
- /* The Russian Peasant's Algorithm */
+ /* The Russian Peasant's Algorithm. */
ULONGEST v;
v = 1;
case BINOP_MOD:
/* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
- v1 mod 0 has a defined value, v1. */
+ v1 mod 0 has a defined value, v1. */
if (v2 == 0)
{
v = v1;
else
{
v = v1 / v2;
- /* Note floor(v1/v2) == v1/v2 for unsigned. */
+ /* Note floor(v1/v2) == v1/v2 for unsigned. */
v = v1 - (v2 * v);
}
break;
case BINOP_MOD:
/* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
- X mod 0 has a defined value, X. */
+ X mod 0 has a defined value, X. */
if (v2 == 0)
{
v = v1;
else
{
v = v1 / v2;
- /* Compute floor. */
+ /* Compute floor. */
if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
{
v--;
vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
{
struct value *val, *tmp, *mark;
- struct type *type1, *type2, *eltype1, *eltype2, *result_type;
+ struct type *type1, *type2, *eltype1, *eltype2;
int t1_is_vec, t2_is_vec, elsize, i;
LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
}
/* Perform a comparison on two string values (whose content are not
- necessarily null terminated) based on their length */
+ necessarily null terminated) based on their length. */
static int
value_strcmp (struct value *arg1, struct value *arg2)
else
{
error (_("Invalid type combination in equality test."));
- return 0; /* For lint -- never reached */
+ return 0; /* For lint -- never reached. */
}
}
else
{
error (_("Argument to positive operation not a number."));
- return 0; /* For lint -- never reached */
+ return 0; /* For lint -- never reached. */
}
}
{
struct value *val = allocate_value (type);
int len = TYPE_LENGTH (type);
- gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long */
+ gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long. */
memcpy (decbytes, value_contents (arg1), len);
else
{
error (_("Argument to negate operation not a number."));
- return 0; /* For lint -- never reached */
+ return 0; /* For lint -- never reached. */
}
}
\f
/* The INDEX'th bit of SET value whose value_type is TYPE,
and whose value_contents is valaddr.
- Return -1 if out of range, -2 other error. */
+ Return -1 if out of range, -2 other error. */
int
value_bit_index (struct type *type, const gdb_byte *valaddr, int index)