03bc1e1e923918ce46569556e3fff816a87c6beb
1 /* Perform arithmetic and other operations on values, for GDB.
2 Copyright (C) 1986 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
22 #include "initialize.h"
26 #include "expression.h"
31 value_add (arg1
, arg2
)
34 register value val
, valint
, valptr
;
40 if ((TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
41 || TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_PTR
)
43 (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
44 || TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_INT
))
45 /* Exactly one argument is a pointer, and one is an integer. */
47 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
)
57 len
= TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (valptr
)));
58 if (len
== 0) len
= 1; /* For (void *) */
59 val
= value_from_long (builtin_type_long
,
60 value_as_long (valptr
)
61 + (len
* value_as_long (valint
)));
62 VALUE_TYPE (val
) = VALUE_TYPE (valptr
);
66 return value_binop (arg1
, arg2
, BINOP_ADD
);
70 value_sub (arg1
, arg2
)
78 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
80 TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_INT
)
82 val
= value_from_long (builtin_type_long
,
84 - TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
))) * value_as_long (arg2
));
85 VALUE_TYPE (val
) = VALUE_TYPE (arg1
);
89 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
91 VALUE_TYPE (arg1
) == VALUE_TYPE (arg2
))
93 val
= value_from_long (builtin_type_long
,
94 (value_as_long (arg1
) - value_as_long (arg2
))
95 / TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
))));
99 return value_binop (arg1
, arg2
, BINOP_SUB
);
102 /* Return the value of ARRAY[IDX]. */
105 value_subscript (array
, idx
)
108 return value_ind (value_add (array
, idx
));
111 /* Perform a binary operation on two integers or two floats.
112 Does not support addition and subtraction on pointers;
113 use value_add or value_sub if you want to handle those possibilities. */
116 value_binop (arg1
, arg2
, op
)
125 if ((TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_FLT
127 TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_INT
)
129 (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_FLT
131 TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
))
132 error ("Argument to arithmetic operation not a number.");
134 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_FLT
136 TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_FLT
)
139 v1
= value_as_double (arg1
);
140 v2
= value_as_double (arg2
);
160 error ("Integer-only operation on floating point number.");
163 val
= allocate_value (builtin_type_double
);
164 *(double *) VALUE_CONTENTS (val
) = v
;
169 v1
= value_as_long (arg1
);
170 v2
= value_as_long (arg2
);
223 error ("Invalid binary operation on numbers.");
226 val
= allocate_value (builtin_type_long
);
227 *(long *) VALUE_CONTENTS (val
) = v
;
233 /* Simulate the C operator ! -- return 1 if ARG1 contains zeros. */
244 len
= TYPE_LENGTH (VALUE_TYPE (arg1
));
245 p
= VALUE_CONTENTS (arg1
);
256 /* Simulate the C operator == by returning a 1
257 iff ARG1 and ARG2 have equal contents. */
260 value_equal (arg1
, arg2
)
261 register value arg1
, arg2
;
265 register char *p1
, *p2
;
266 enum type_code code1
;
267 enum type_code code2
;
272 code1
= TYPE_CODE (VALUE_TYPE (arg1
));
273 code2
= TYPE_CODE (VALUE_TYPE (arg2
));
275 if (code1
== TYPE_CODE_INT
&& code2
== TYPE_CODE_INT
)
276 return value_as_long (arg1
) == value_as_long (arg2
);
277 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
)
278 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
))
279 return value_as_double (arg1
) == value_as_double (arg2
);
280 else if ((code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_INT
)
281 || (code2
== TYPE_CODE_PTR
&& code1
== TYPE_CODE_INT
))
282 return value_as_long (arg1
) == value_as_long (arg2
);
283 else if (code1
== code2
284 && ((len
= TYPE_LENGTH (VALUE_TYPE (arg1
)))
285 == TYPE_LENGTH (VALUE_TYPE (arg2
))))
287 p1
= VALUE_CONTENTS (arg1
);
288 p2
= VALUE_CONTENTS (arg2
);
297 error ("Invalid type combination in equality test.");
300 /* Simulate the C operator < by returning 1
301 iff ARG1's contents are less than ARG2's. */
304 value_less (arg1
, arg2
)
305 register value arg1
, arg2
;
307 register enum type_code code1
;
308 register enum type_code code2
;
313 code1
= TYPE_CODE (VALUE_TYPE (arg1
));
314 code2
= TYPE_CODE (VALUE_TYPE (arg2
));
316 if (code1
== TYPE_CODE_INT
&& code2
== TYPE_CODE_INT
)
317 return value_as_long (arg1
) < value_as_long (arg2
);
318 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
)
319 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
))
320 return value_as_double (arg1
) < value_as_double (arg2
);
321 else if ((code1
== TYPE_CODE_PTR
|| code1
== TYPE_CODE_INT
)
322 && (code2
== TYPE_CODE_PTR
|| code2
== TYPE_CODE_INT
))
323 return value_as_long (arg1
) < value_as_long (arg2
);
325 error ("Invalid type combination in ordering comparison.");
328 /* The unary operators - and ~. Both free the argument ARG1. */
334 register struct type
*type
;
338 type
= VALUE_TYPE (arg1
);
340 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
341 return value_from_double (type
, - value_as_double (arg1
));
342 else if (TYPE_CODE (type
) == TYPE_CODE_INT
)
343 return value_from_long (type
, - value_as_long (arg1
));
345 error ("Argument to negate operation not a number.");
354 if (TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_INT
)
355 error ("Argument to complement operation not an integer.");
357 return value_from_long (VALUE_TYPE (arg1
), ~ value_as_long (arg1
));
This page took 0.035968 seconds and 4 git commands to generate.