Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Perform arithmetic and other operations on values, for GDB. |
1bac305b | 2 | |
6aba47ca | 3 | Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, |
4c38e0a4 | 4 | 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, |
7b6bb8da | 5 | 2010, 2011 Free Software Foundation, Inc. |
c906108c | 6 | |
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 12 | (at your option) any later version. |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b | 19 | You should have received a copy of the GNU General Public License |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
21 | |
22 | #include "defs.h" | |
23 | #include "value.h" | |
24 | #include "symtab.h" | |
25 | #include "gdbtypes.h" | |
26 | #include "expression.h" | |
27 | #include "target.h" | |
28 | #include "language.h" | |
c906108c | 29 | #include "gdb_string.h" |
d16aafd8 | 30 | #include "doublest.h" |
4ef30785 | 31 | #include "dfp.h" |
c4093a6a | 32 | #include <math.h> |
04714b91 | 33 | #include "infcall.h" |
4c3376c8 | 34 | #include "exceptions.h" |
c906108c SS |
35 | |
36 | /* Define whether or not the C operator '/' truncates towards zero for | |
581e13c1 | 37 | differently signed operands (truncation direction is undefined in C). */ |
c906108c SS |
38 | |
39 | #ifndef TRUNCATION_TOWARDS_ZERO | |
40 | #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2) | |
41 | #endif | |
42 | ||
a14ed312 | 43 | void _initialize_valarith (void); |
c906108c | 44 | \f |
c5aa993b | 45 | |
ca439ad2 JI |
46 | /* Given a pointer, return the size of its target. |
47 | If the pointer type is void *, then return 1. | |
48 | If the target type is incomplete, then error out. | |
49 | This isn't a general purpose function, but just a | |
581e13c1 | 50 | helper for value_ptradd. */ |
ca439ad2 JI |
51 | |
52 | static LONGEST | |
53 | find_size_for_pointer_math (struct type *ptr_type) | |
54 | { | |
55 | LONGEST sz = -1; | |
56 | struct type *ptr_target; | |
57 | ||
89eef114 | 58 | gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR); |
ca439ad2 JI |
59 | ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type)); |
60 | ||
61 | sz = TYPE_LENGTH (ptr_target); | |
62 | if (sz == 0) | |
63 | { | |
64 | if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID) | |
65 | sz = 1; | |
66 | else | |
67 | { | |
68 | char *name; | |
69 | ||
70 | name = TYPE_NAME (ptr_target); | |
71 | if (name == NULL) | |
72 | name = TYPE_TAG_NAME (ptr_target); | |
73 | if (name == NULL) | |
8a3fe4f8 AC |
74 | error (_("Cannot perform pointer math on incomplete types, " |
75 | "try casting to a known type, or void *.")); | |
ca439ad2 | 76 | else |
8a3fe4f8 AC |
77 | error (_("Cannot perform pointer math on incomplete type \"%s\", " |
78 | "try casting to a known type, or void *."), name); | |
ca439ad2 JI |
79 | } |
80 | } | |
81 | return sz; | |
82 | } | |
83 | ||
89eef114 UW |
84 | /* Given a pointer ARG1 and an integral value ARG2, return the |
85 | result of C-style pointer arithmetic ARG1 + ARG2. */ | |
86 | ||
f23631e4 | 87 | struct value * |
2497b498 | 88 | value_ptradd (struct value *arg1, LONGEST arg2) |
c906108c | 89 | { |
89eef114 | 90 | struct type *valptrtype; |
ca439ad2 | 91 | LONGEST sz; |
8cf6f0b1 | 92 | struct value *result; |
c906108c | 93 | |
994b9211 | 94 | arg1 = coerce_array (arg1); |
89eef114 UW |
95 | valptrtype = check_typedef (value_type (arg1)); |
96 | sz = find_size_for_pointer_math (valptrtype); | |
c906108c | 97 | |
8cf6f0b1 TT |
98 | result = value_from_pointer (valptrtype, |
99 | value_as_address (arg1) + sz * arg2); | |
100 | if (VALUE_LVAL (result) != lval_internalvar) | |
101 | set_value_component_location (result, arg1); | |
102 | return result; | |
c906108c SS |
103 | } |
104 | ||
89eef114 UW |
105 | /* Given two compatible pointer values ARG1 and ARG2, return the |
106 | result of C-style pointer arithmetic ARG1 - ARG2. */ | |
107 | ||
108 | LONGEST | |
109 | value_ptrdiff (struct value *arg1, struct value *arg2) | |
c906108c SS |
110 | { |
111 | struct type *type1, *type2; | |
89eef114 UW |
112 | LONGEST sz; |
113 | ||
994b9211 AC |
114 | arg1 = coerce_array (arg1); |
115 | arg2 = coerce_array (arg2); | |
df407dfe AC |
116 | type1 = check_typedef (value_type (arg1)); |
117 | type2 = check_typedef (value_type (arg2)); | |
c906108c | 118 | |
89eef114 UW |
119 | gdb_assert (TYPE_CODE (type1) == TYPE_CODE_PTR); |
120 | gdb_assert (TYPE_CODE (type2) == TYPE_CODE_PTR); | |
ca439ad2 | 121 | |
89eef114 UW |
122 | if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1))) |
123 | != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2)))) | |
3e43a32a MS |
124 | error (_("First argument of `-' is a pointer and " |
125 | "second argument is neither\n" | |
126 | "an integer nor a pointer of the same type.")); | |
c906108c | 127 | |
89eef114 | 128 | sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1))); |
83b10087 CM |
129 | if (sz == 0) |
130 | { | |
131 | warning (_("Type size unknown, assuming 1. " | |
132 | "Try casting to a known type, or void *.")); | |
133 | sz = 1; | |
134 | } | |
135 | ||
89eef114 | 136 | return (value_as_long (arg1) - value_as_long (arg2)) / sz; |
c906108c SS |
137 | } |
138 | ||
139 | /* Return the value of ARRAY[IDX]. | |
afc05acb UW |
140 | |
141 | ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the | |
142 | current language supports C-style arrays, it may also be TYPE_CODE_PTR. | |
143 | To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript. | |
144 | ||
c906108c SS |
145 | See comments in value_coerce_array() for rationale for reason for |
146 | doing lower bounds adjustment here rather than there. | |
147 | FIXME: Perhaps we should validate that the index is valid and if | |
581e13c1 | 148 | verbosity is set, warn about invalid indices (but still use them). */ |
c906108c | 149 | |
f23631e4 | 150 | struct value * |
2497b498 | 151 | value_subscript (struct value *array, LONGEST index) |
c906108c | 152 | { |
c906108c SS |
153 | int c_style = current_language->c_style_arrays; |
154 | struct type *tarray; | |
155 | ||
994b9211 | 156 | array = coerce_ref (array); |
df407dfe | 157 | tarray = check_typedef (value_type (array)); |
c906108c SS |
158 | |
159 | if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY | |
160 | || TYPE_CODE (tarray) == TYPE_CODE_STRING) | |
161 | { | |
162 | struct type *range_type = TYPE_INDEX_TYPE (tarray); | |
163 | LONGEST lowerbound, upperbound; | |
c906108c | 164 | |
a109c7c1 | 165 | get_discrete_bounds (range_type, &lowerbound, &upperbound); |
c906108c | 166 | if (VALUE_LVAL (array) != lval_memory) |
2497b498 | 167 | return value_subscripted_rvalue (array, index, lowerbound); |
c906108c SS |
168 | |
169 | if (c_style == 0) | |
170 | { | |
c906108c | 171 | if (index >= lowerbound && index <= upperbound) |
2497b498 | 172 | return value_subscripted_rvalue (array, index, lowerbound); |
987504bb JJ |
173 | /* Emit warning unless we have an array of unknown size. |
174 | An array of unknown size has lowerbound 0 and upperbound -1. */ | |
175 | if (upperbound > -1) | |
8a3fe4f8 | 176 | warning (_("array or string index out of range")); |
c906108c SS |
177 | /* fall doing C stuff */ |
178 | c_style = 1; | |
179 | } | |
180 | ||
2497b498 | 181 | index -= lowerbound; |
c906108c SS |
182 | array = value_coerce_array (array); |
183 | } | |
184 | ||
c906108c | 185 | if (c_style) |
2497b498 | 186 | return value_ind (value_ptradd (array, index)); |
c906108c | 187 | else |
8a3fe4f8 | 188 | error (_("not an array or string")); |
c906108c SS |
189 | } |
190 | ||
191 | /* Return the value of EXPR[IDX], expr an aggregate rvalue | |
192 | (eg, a vector register). This routine used to promote floats | |
193 | to doubles, but no longer does. */ | |
194 | ||
9eec4d1e | 195 | struct value * |
2497b498 | 196 | value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound) |
c906108c | 197 | { |
df407dfe | 198 | struct type *array_type = check_typedef (value_type (array)); |
c906108c SS |
199 | struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type)); |
200 | unsigned int elt_size = TYPE_LENGTH (elt_type); | |
c906108c | 201 | unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound); |
f23631e4 | 202 | struct value *v; |
c906108c | 203 | |
bbb0eef6 JK |
204 | if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type) |
205 | && elt_offs >= TYPE_LENGTH (array_type))) | |
8a3fe4f8 | 206 | error (_("no such vector element")); |
c906108c | 207 | |
9214ee5f | 208 | if (VALUE_LVAL (array) == lval_memory && value_lazy (array)) |
41e8491f | 209 | v = allocate_value_lazy (elt_type); |
c906108c | 210 | else |
41e8491f JK |
211 | { |
212 | v = allocate_value (elt_type); | |
39d37385 PA |
213 | value_contents_copy (v, value_embedded_offset (v), |
214 | array, value_embedded_offset (array) + elt_offs, | |
215 | elt_size); | |
41e8491f | 216 | } |
c906108c | 217 | |
74bcbdf3 | 218 | set_value_component_location (v, array); |
9ee8fc9d | 219 | VALUE_REGNUM (v) = VALUE_REGNUM (array); |
65d3800a | 220 | VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array); |
f5cf64a7 | 221 | set_value_offset (v, value_offset (array) + elt_offs); |
c906108c SS |
222 | return v; |
223 | } | |
afc05acb UW |
224 | |
225 | /* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */ | |
226 | ||
227 | struct value * | |
228 | value_bitstring_subscript (struct type *type, | |
2497b498 | 229 | struct value *bitstring, LONGEST index) |
afc05acb UW |
230 | { |
231 | ||
232 | struct type *bitstring_type, *range_type; | |
afc05acb UW |
233 | struct value *v; |
234 | int offset, byte, bit_index; | |
235 | LONGEST lowerbound, upperbound; | |
236 | ||
237 | bitstring_type = check_typedef (value_type (bitstring)); | |
238 | gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING); | |
239 | ||
240 | range_type = TYPE_INDEX_TYPE (bitstring_type); | |
241 | get_discrete_bounds (range_type, &lowerbound, &upperbound); | |
242 | if (index < lowerbound || index > upperbound) | |
243 | error (_("bitstring index out of range")); | |
244 | ||
245 | index -= lowerbound; | |
246 | offset = index / TARGET_CHAR_BIT; | |
247 | byte = *((char *) value_contents (bitstring) + offset); | |
248 | ||
249 | bit_index = index % TARGET_CHAR_BIT; | |
50810684 | 250 | byte >>= (gdbarch_bits_big_endian (get_type_arch (bitstring_type)) ? |
afc05acb UW |
251 | TARGET_CHAR_BIT - 1 - bit_index : bit_index); |
252 | ||
253 | v = value_from_longest (type, byte & 1); | |
254 | ||
255 | set_value_bitpos (v, bit_index); | |
256 | set_value_bitsize (v, 1); | |
74bcbdf3 | 257 | set_value_component_location (v, bitstring); |
afc05acb UW |
258 | VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring); |
259 | ||
260 | set_value_offset (v, offset + value_offset (bitstring)); | |
261 | ||
262 | return v; | |
263 | } | |
264 | ||
c906108c | 265 | \f |
13d6656b JB |
266 | /* Check to see if either argument is a structure, or a reference to |
267 | one. This is called so we know whether to go ahead with the normal | |
268 | binop or look for a user defined function instead. | |
c906108c SS |
269 | |
270 | For now, we do not overload the `=' operator. */ | |
271 | ||
272 | int | |
be636754 PA |
273 | binop_types_user_defined_p (enum exp_opcode op, |
274 | struct type *type1, struct type *type2) | |
c906108c | 275 | { |
c906108c SS |
276 | if (op == BINOP_ASSIGN || op == BINOP_CONCAT) |
277 | return 0; | |
13d6656b | 278 | |
be636754 | 279 | type1 = check_typedef (type1); |
13d6656b JB |
280 | if (TYPE_CODE (type1) == TYPE_CODE_REF) |
281 | type1 = check_typedef (TYPE_TARGET_TYPE (type1)); | |
282 | ||
be636754 | 283 | type2 = check_typedef (type1); |
13d6656b JB |
284 | if (TYPE_CODE (type2) == TYPE_CODE_REF) |
285 | type2 = check_typedef (TYPE_TARGET_TYPE (type2)); | |
286 | ||
c906108c | 287 | return (TYPE_CODE (type1) == TYPE_CODE_STRUCT |
13d6656b | 288 | || TYPE_CODE (type2) == TYPE_CODE_STRUCT); |
c906108c SS |
289 | } |
290 | ||
be636754 PA |
291 | /* Check to see if either argument is a structure, or a reference to |
292 | one. This is called so we know whether to go ahead with the normal | |
293 | binop or look for a user defined function instead. | |
294 | ||
295 | For now, we do not overload the `=' operator. */ | |
296 | ||
297 | int | |
298 | binop_user_defined_p (enum exp_opcode op, | |
299 | struct value *arg1, struct value *arg2) | |
300 | { | |
301 | return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2)); | |
302 | } | |
303 | ||
c906108c SS |
304 | /* Check to see if argument is a structure. This is called so |
305 | we know whether to go ahead with the normal unop or look for a | |
306 | user defined function instead. | |
307 | ||
308 | For now, we do not overload the `&' operator. */ | |
309 | ||
c5aa993b | 310 | int |
f23631e4 | 311 | unop_user_defined_p (enum exp_opcode op, struct value *arg1) |
c906108c SS |
312 | { |
313 | struct type *type1; | |
a109c7c1 | 314 | |
c906108c SS |
315 | if (op == UNOP_ADDR) |
316 | return 0; | |
df407dfe | 317 | type1 = check_typedef (value_type (arg1)); |
eeaafae2 JK |
318 | if (TYPE_CODE (type1) == TYPE_CODE_REF) |
319 | type1 = check_typedef (TYPE_TARGET_TYPE (type1)); | |
320 | return TYPE_CODE (type1) == TYPE_CODE_STRUCT; | |
c906108c SS |
321 | } |
322 | ||
4c3376c8 SW |
323 | /* Try to find an operator named OPERATOR which takes NARGS arguments |
324 | specified in ARGS. If the operator found is a static member operator | |
325 | *STATIC_MEMFUNP will be set to 1, and otherwise 0. | |
326 | The search if performed through find_overload_match which will handle | |
327 | member operators, non member operators, operators imported implicitly or | |
328 | explicitly, and perform correct overload resolution in all of the above | |
329 | situations or combinations thereof. */ | |
330 | ||
331 | static struct value * | |
332 | value_user_defined_cpp_op (struct value **args, int nargs, char *operator, | |
333 | int *static_memfuncp) | |
334 | { | |
335 | ||
336 | struct symbol *symp = NULL; | |
337 | struct value *valp = NULL; | |
338 | struct type **arg_types; | |
339 | int i; | |
340 | ||
341 | arg_types = (struct type **) alloca (nargs * (sizeof (struct type *))); | |
581e13c1 | 342 | /* Prepare list of argument types for overload resolution. */ |
4c3376c8 SW |
343 | for (i = 0; i < nargs; i++) |
344 | arg_types[i] = value_type (args[i]); | |
345 | ||
346 | find_overload_match (arg_types, nargs, operator, BOTH /* could be method */, | |
347 | 0 /* strict match */, &args[0], /* objp */ | |
348 | NULL /* pass NULL symbol since symbol is unknown */, | |
349 | &valp, &symp, static_memfuncp, 0); | |
350 | ||
351 | if (valp) | |
352 | return valp; | |
353 | ||
354 | if (symp) | |
355 | { | |
356 | /* This is a non member function and does not | |
357 | expect a reference as its first argument | |
358 | rather the explicit structure. */ | |
359 | args[0] = value_ind (args[0]); | |
360 | return value_of_variable (symp, 0); | |
361 | } | |
362 | ||
363 | error (_("Could not find %s."), operator); | |
364 | } | |
365 | ||
366 | /* Lookup user defined operator NAME. Return a value representing the | |
367 | function, otherwise return NULL. */ | |
368 | ||
369 | static struct value * | |
370 | value_user_defined_op (struct value **argp, struct value **args, char *name, | |
371 | int *static_memfuncp, int nargs) | |
372 | { | |
373 | struct value *result = NULL; | |
374 | ||
375 | if (current_language->la_language == language_cplus) | |
376 | result = value_user_defined_cpp_op (args, nargs, name, static_memfuncp); | |
377 | else | |
378 | result = value_struct_elt (argp, args, name, static_memfuncp, | |
379 | "structure"); | |
380 | ||
381 | return result; | |
382 | } | |
383 | ||
c906108c SS |
384 | /* We know either arg1 or arg2 is a structure, so try to find the right |
385 | user defined function. Create an argument vector that calls | |
386 | arg1.operator @ (arg1,arg2) and return that value (where '@' is any | |
387 | binary operator which is legal for GNU C++). | |
388 | ||
389 | OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP | |
390 | is the opcode saying how to modify it. Otherwise, OTHEROP is | |
391 | unused. */ | |
392 | ||
f23631e4 AC |
393 | struct value * |
394 | value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op, | |
fba45db2 | 395 | enum exp_opcode otherop, enum noside noside) |
c906108c | 396 | { |
f23631e4 | 397 | struct value **argvec; |
c906108c SS |
398 | char *ptr; |
399 | char tstr[13]; | |
400 | int static_memfuncp; | |
401 | ||
994b9211 AC |
402 | arg1 = coerce_ref (arg1); |
403 | arg2 = coerce_ref (arg2); | |
c906108c SS |
404 | |
405 | /* now we know that what we have to do is construct our | |
406 | arg vector and find the right function to call it with. */ | |
407 | ||
df407dfe | 408 | if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT) |
8a3fe4f8 | 409 | error (_("Can't do that binary op on that type")); /* FIXME be explicit */ |
c906108c | 410 | |
f23631e4 | 411 | argvec = (struct value **) alloca (sizeof (struct value *) * 4); |
c906108c SS |
412 | argvec[1] = value_addr (arg1); |
413 | argvec[2] = arg2; | |
414 | argvec[3] = 0; | |
415 | ||
581e13c1 | 416 | /* Make the right function name up. */ |
c5aa993b JM |
417 | strcpy (tstr, "operator__"); |
418 | ptr = tstr + 8; | |
c906108c SS |
419 | switch (op) |
420 | { | |
c5aa993b JM |
421 | case BINOP_ADD: |
422 | strcpy (ptr, "+"); | |
423 | break; | |
424 | case BINOP_SUB: | |
425 | strcpy (ptr, "-"); | |
426 | break; | |
427 | case BINOP_MUL: | |
428 | strcpy (ptr, "*"); | |
429 | break; | |
430 | case BINOP_DIV: | |
431 | strcpy (ptr, "/"); | |
432 | break; | |
433 | case BINOP_REM: | |
434 | strcpy (ptr, "%"); | |
435 | break; | |
436 | case BINOP_LSH: | |
437 | strcpy (ptr, "<<"); | |
438 | break; | |
439 | case BINOP_RSH: | |
440 | strcpy (ptr, ">>"); | |
441 | break; | |
442 | case BINOP_BITWISE_AND: | |
443 | strcpy (ptr, "&"); | |
444 | break; | |
445 | case BINOP_BITWISE_IOR: | |
446 | strcpy (ptr, "|"); | |
447 | break; | |
448 | case BINOP_BITWISE_XOR: | |
449 | strcpy (ptr, "^"); | |
450 | break; | |
451 | case BINOP_LOGICAL_AND: | |
452 | strcpy (ptr, "&&"); | |
453 | break; | |
454 | case BINOP_LOGICAL_OR: | |
455 | strcpy (ptr, "||"); | |
456 | break; | |
457 | case BINOP_MIN: | |
458 | strcpy (ptr, "<?"); | |
459 | break; | |
460 | case BINOP_MAX: | |
461 | strcpy (ptr, ">?"); | |
462 | break; | |
463 | case BINOP_ASSIGN: | |
464 | strcpy (ptr, "="); | |
465 | break; | |
466 | case BINOP_ASSIGN_MODIFY: | |
c906108c SS |
467 | switch (otherop) |
468 | { | |
c5aa993b JM |
469 | case BINOP_ADD: |
470 | strcpy (ptr, "+="); | |
471 | break; | |
472 | case BINOP_SUB: | |
473 | strcpy (ptr, "-="); | |
474 | break; | |
475 | case BINOP_MUL: | |
476 | strcpy (ptr, "*="); | |
477 | break; | |
478 | case BINOP_DIV: | |
479 | strcpy (ptr, "/="); | |
480 | break; | |
481 | case BINOP_REM: | |
482 | strcpy (ptr, "%="); | |
483 | break; | |
484 | case BINOP_BITWISE_AND: | |
485 | strcpy (ptr, "&="); | |
486 | break; | |
487 | case BINOP_BITWISE_IOR: | |
488 | strcpy (ptr, "|="); | |
489 | break; | |
490 | case BINOP_BITWISE_XOR: | |
491 | strcpy (ptr, "^="); | |
492 | break; | |
493 | case BINOP_MOD: /* invalid */ | |
c906108c | 494 | default: |
8a3fe4f8 | 495 | error (_("Invalid binary operation specified.")); |
c906108c SS |
496 | } |
497 | break; | |
c5aa993b JM |
498 | case BINOP_SUBSCRIPT: |
499 | strcpy (ptr, "[]"); | |
500 | break; | |
501 | case BINOP_EQUAL: | |
502 | strcpy (ptr, "=="); | |
503 | break; | |
504 | case BINOP_NOTEQUAL: | |
505 | strcpy (ptr, "!="); | |
506 | break; | |
507 | case BINOP_LESS: | |
508 | strcpy (ptr, "<"); | |
509 | break; | |
510 | case BINOP_GTR: | |
511 | strcpy (ptr, ">"); | |
512 | break; | |
513 | case BINOP_GEQ: | |
514 | strcpy (ptr, ">="); | |
515 | break; | |
516 | case BINOP_LEQ: | |
517 | strcpy (ptr, "<="); | |
518 | break; | |
519 | case BINOP_MOD: /* invalid */ | |
c906108c | 520 | default: |
8a3fe4f8 | 521 | error (_("Invalid binary operation specified.")); |
c906108c SS |
522 | } |
523 | ||
4c3376c8 SW |
524 | argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr, |
525 | &static_memfuncp, 2); | |
c5aa993b | 526 | |
c906108c SS |
527 | if (argvec[0]) |
528 | { | |
529 | if (static_memfuncp) | |
530 | { | |
531 | argvec[1] = argvec[0]; | |
532 | argvec++; | |
533 | } | |
534 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
535 | { | |
536 | struct type *return_type; | |
a109c7c1 | 537 | |
c906108c | 538 | return_type |
df407dfe | 539 | = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0]))); |
c906108c SS |
540 | return value_zero (return_type, VALUE_LVAL (arg1)); |
541 | } | |
3e43a32a MS |
542 | return call_function_by_hand (argvec[0], 2 - static_memfuncp, |
543 | argvec + 1); | |
c906108c | 544 | } |
79afc5ef SW |
545 | throw_error (NOT_FOUND_ERROR, |
546 | _("member function %s not found"), tstr); | |
c906108c SS |
547 | #ifdef lint |
548 | return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); | |
549 | #endif | |
550 | } | |
551 | ||
552 | /* We know that arg1 is a structure, so try to find a unary user | |
581e13c1 | 553 | defined operator that matches the operator in question. |
c906108c SS |
554 | Create an argument vector that calls arg1.operator @ (arg1) |
555 | and return that value (where '@' is (almost) any unary operator which | |
556 | is legal for GNU C++). */ | |
557 | ||
f23631e4 AC |
558 | struct value * |
559 | value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside) | |
c906108c | 560 | { |
50810684 | 561 | struct gdbarch *gdbarch = get_type_arch (value_type (arg1)); |
f23631e4 | 562 | struct value **argvec; |
c906108c SS |
563 | char *ptr, *mangle_ptr; |
564 | char tstr[13], mangle_tstr[13]; | |
491b8946 | 565 | int static_memfuncp, nargs; |
c906108c | 566 | |
994b9211 | 567 | arg1 = coerce_ref (arg1); |
c906108c SS |
568 | |
569 | /* now we know that what we have to do is construct our | |
570 | arg vector and find the right function to call it with. */ | |
571 | ||
df407dfe | 572 | if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT) |
8a3fe4f8 | 573 | error (_("Can't do that unary op on that type")); /* FIXME be explicit */ |
c906108c | 574 | |
491b8946 | 575 | argvec = (struct value **) alloca (sizeof (struct value *) * 4); |
c906108c SS |
576 | argvec[1] = value_addr (arg1); |
577 | argvec[2] = 0; | |
578 | ||
491b8946 DJ |
579 | nargs = 1; |
580 | ||
581e13c1 | 581 | /* Make the right function name up. */ |
c5aa993b JM |
582 | strcpy (tstr, "operator__"); |
583 | ptr = tstr + 8; | |
584 | strcpy (mangle_tstr, "__"); | |
585 | mangle_ptr = mangle_tstr + 2; | |
c906108c SS |
586 | switch (op) |
587 | { | |
c5aa993b JM |
588 | case UNOP_PREINCREMENT: |
589 | strcpy (ptr, "++"); | |
590 | break; | |
591 | case UNOP_PREDECREMENT: | |
491b8946 | 592 | strcpy (ptr, "--"); |
c5aa993b JM |
593 | break; |
594 | case UNOP_POSTINCREMENT: | |
595 | strcpy (ptr, "++"); | |
22601c15 | 596 | argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0); |
491b8946 DJ |
597 | argvec[3] = 0; |
598 | nargs ++; | |
c5aa993b JM |
599 | break; |
600 | case UNOP_POSTDECREMENT: | |
491b8946 | 601 | strcpy (ptr, "--"); |
22601c15 | 602 | argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0); |
491b8946 DJ |
603 | argvec[3] = 0; |
604 | nargs ++; | |
c5aa993b JM |
605 | break; |
606 | case UNOP_LOGICAL_NOT: | |
607 | strcpy (ptr, "!"); | |
608 | break; | |
609 | case UNOP_COMPLEMENT: | |
610 | strcpy (ptr, "~"); | |
611 | break; | |
612 | case UNOP_NEG: | |
613 | strcpy (ptr, "-"); | |
614 | break; | |
36e9969c NS |
615 | case UNOP_PLUS: |
616 | strcpy (ptr, "+"); | |
617 | break; | |
c5aa993b JM |
618 | case UNOP_IND: |
619 | strcpy (ptr, "*"); | |
620 | break; | |
79afc5ef SW |
621 | case STRUCTOP_PTR: |
622 | strcpy (ptr, "->"); | |
623 | break; | |
c906108c | 624 | default: |
8a3fe4f8 | 625 | error (_("Invalid unary operation specified.")); |
c906108c SS |
626 | } |
627 | ||
4c3376c8 SW |
628 | argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr, |
629 | &static_memfuncp, nargs); | |
c906108c SS |
630 | |
631 | if (argvec[0]) | |
632 | { | |
633 | if (static_memfuncp) | |
634 | { | |
635 | argvec[1] = argvec[0]; | |
491b8946 | 636 | nargs --; |
c906108c SS |
637 | argvec++; |
638 | } | |
639 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
640 | { | |
641 | struct type *return_type; | |
a109c7c1 | 642 | |
c906108c | 643 | return_type |
df407dfe | 644 | = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0]))); |
c906108c SS |
645 | return value_zero (return_type, VALUE_LVAL (arg1)); |
646 | } | |
491b8946 | 647 | return call_function_by_hand (argvec[0], nargs, argvec + 1); |
c906108c | 648 | } |
79afc5ef SW |
649 | throw_error (NOT_FOUND_ERROR, |
650 | _("member function %s not found"), tstr); | |
651 | ||
c5aa993b | 652 | return 0; /* For lint -- never reached */ |
c906108c | 653 | } |
c906108c | 654 | \f |
c5aa993b | 655 | |
c906108c SS |
656 | /* Concatenate two values with the following conditions: |
657 | ||
c5aa993b JM |
658 | (1) Both values must be either bitstring values or character string |
659 | values and the resulting value consists of the concatenation of | |
660 | ARG1 followed by ARG2. | |
c906108c | 661 | |
c5aa993b | 662 | or |
c906108c | 663 | |
c5aa993b JM |
664 | One value must be an integer value and the other value must be |
665 | either a bitstring value or character string value, which is | |
666 | to be repeated by the number of times specified by the integer | |
667 | value. | |
c906108c SS |
668 | |
669 | ||
c5aa993b JM |
670 | (2) Boolean values are also allowed and are treated as bit string |
671 | values of length 1. | |
c906108c | 672 | |
c5aa993b | 673 | (3) Character values are also allowed and are treated as character |
581e13c1 | 674 | string values of length 1. */ |
c906108c | 675 | |
f23631e4 AC |
676 | struct value * |
677 | value_concat (struct value *arg1, struct value *arg2) | |
c906108c | 678 | { |
f23631e4 AC |
679 | struct value *inval1; |
680 | struct value *inval2; | |
681 | struct value *outval = NULL; | |
c906108c SS |
682 | int inval1len, inval2len; |
683 | int count, idx; | |
684 | char *ptr; | |
685 | char inchar; | |
df407dfe AC |
686 | struct type *type1 = check_typedef (value_type (arg1)); |
687 | struct type *type2 = check_typedef (value_type (arg2)); | |
3b7538c0 | 688 | struct type *char_type; |
c906108c | 689 | |
c906108c SS |
690 | /* First figure out if we are dealing with two values to be concatenated |
691 | or a repeat count and a value to be repeated. INVAL1 is set to the | |
692 | first of two concatenated values, or the repeat count. INVAL2 is set | |
693 | to the second of the two concatenated values or the value to be | |
581e13c1 | 694 | repeated. */ |
c906108c SS |
695 | |
696 | if (TYPE_CODE (type2) == TYPE_CODE_INT) | |
697 | { | |
698 | struct type *tmp = type1; | |
a109c7c1 | 699 | |
c906108c SS |
700 | type1 = tmp; |
701 | tmp = type2; | |
702 | inval1 = arg2; | |
703 | inval2 = arg1; | |
704 | } | |
705 | else | |
706 | { | |
707 | inval1 = arg1; | |
708 | inval2 = arg2; | |
709 | } | |
710 | ||
581e13c1 | 711 | /* Now process the input values. */ |
c906108c SS |
712 | |
713 | if (TYPE_CODE (type1) == TYPE_CODE_INT) | |
714 | { | |
715 | /* We have a repeat count. Validate the second value and then | |
581e13c1 | 716 | construct a value repeated that many times. */ |
c906108c SS |
717 | if (TYPE_CODE (type2) == TYPE_CODE_STRING |
718 | || TYPE_CODE (type2) == TYPE_CODE_CHAR) | |
719 | { | |
720 | count = longest_to_int (value_as_long (inval1)); | |
721 | inval2len = TYPE_LENGTH (type2); | |
722 | ptr = (char *) alloca (count * inval2len); | |
723 | if (TYPE_CODE (type2) == TYPE_CODE_CHAR) | |
724 | { | |
3b7538c0 | 725 | char_type = type2; |
a109c7c1 | 726 | |
c906108c | 727 | inchar = (char) unpack_long (type2, |
0fd88904 | 728 | value_contents (inval2)); |
c906108c SS |
729 | for (idx = 0; idx < count; idx++) |
730 | { | |
731 | *(ptr + idx) = inchar; | |
732 | } | |
733 | } | |
734 | else | |
735 | { | |
3b7538c0 | 736 | char_type = TYPE_TARGET_TYPE (type2); |
a109c7c1 | 737 | |
c906108c SS |
738 | for (idx = 0; idx < count; idx++) |
739 | { | |
0fd88904 | 740 | memcpy (ptr + (idx * inval2len), value_contents (inval2), |
c906108c SS |
741 | inval2len); |
742 | } | |
743 | } | |
3b7538c0 | 744 | outval = value_string (ptr, count * inval2len, char_type); |
c906108c SS |
745 | } |
746 | else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING | |
747 | || TYPE_CODE (type2) == TYPE_CODE_BOOL) | |
748 | { | |
8a3fe4f8 | 749 | error (_("unimplemented support for bitstring/boolean repeats")); |
c906108c SS |
750 | } |
751 | else | |
752 | { | |
8a3fe4f8 | 753 | error (_("can't repeat values of that type")); |
c906108c SS |
754 | } |
755 | } | |
756 | else if (TYPE_CODE (type1) == TYPE_CODE_STRING | |
c5aa993b | 757 | || TYPE_CODE (type1) == TYPE_CODE_CHAR) |
c906108c | 758 | { |
581e13c1 | 759 | /* We have two character strings to concatenate. */ |
c906108c SS |
760 | if (TYPE_CODE (type2) != TYPE_CODE_STRING |
761 | && TYPE_CODE (type2) != TYPE_CODE_CHAR) | |
762 | { | |
8a3fe4f8 | 763 | error (_("Strings can only be concatenated with other strings.")); |
c906108c SS |
764 | } |
765 | inval1len = TYPE_LENGTH (type1); | |
766 | inval2len = TYPE_LENGTH (type2); | |
767 | ptr = (char *) alloca (inval1len + inval2len); | |
768 | if (TYPE_CODE (type1) == TYPE_CODE_CHAR) | |
769 | { | |
3b7538c0 | 770 | char_type = type1; |
a109c7c1 | 771 | |
0fd88904 | 772 | *ptr = (char) unpack_long (type1, value_contents (inval1)); |
c906108c SS |
773 | } |
774 | else | |
775 | { | |
3b7538c0 | 776 | char_type = TYPE_TARGET_TYPE (type1); |
a109c7c1 | 777 | |
0fd88904 | 778 | memcpy (ptr, value_contents (inval1), inval1len); |
c906108c SS |
779 | } |
780 | if (TYPE_CODE (type2) == TYPE_CODE_CHAR) | |
781 | { | |
c5aa993b | 782 | *(ptr + inval1len) = |
0fd88904 | 783 | (char) unpack_long (type2, value_contents (inval2)); |
c906108c SS |
784 | } |
785 | else | |
786 | { | |
0fd88904 | 787 | memcpy (ptr + inval1len, value_contents (inval2), inval2len); |
c906108c | 788 | } |
3b7538c0 | 789 | outval = value_string (ptr, inval1len + inval2len, char_type); |
c906108c SS |
790 | } |
791 | else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING | |
792 | || TYPE_CODE (type1) == TYPE_CODE_BOOL) | |
793 | { | |
581e13c1 | 794 | /* We have two bitstrings to concatenate. */ |
c906108c SS |
795 | if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING |
796 | && TYPE_CODE (type2) != TYPE_CODE_BOOL) | |
797 | { | |
3e43a32a MS |
798 | error (_("Bitstrings or booleans can only be concatenated " |
799 | "with other bitstrings or booleans.")); | |
c906108c | 800 | } |
8a3fe4f8 | 801 | error (_("unimplemented support for bitstring/boolean concatenation.")); |
c5aa993b | 802 | } |
c906108c SS |
803 | else |
804 | { | |
581e13c1 | 805 | /* We don't know how to concatenate these operands. */ |
8a3fe4f8 | 806 | error (_("illegal operands for concatenation.")); |
c906108c SS |
807 | } |
808 | return (outval); | |
809 | } | |
c906108c | 810 | \f |
d118ef87 PH |
811 | /* Integer exponentiation: V1**V2, where both arguments are |
812 | integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */ | |
581e13c1 | 813 | |
d118ef87 PH |
814 | static LONGEST |
815 | integer_pow (LONGEST v1, LONGEST v2) | |
816 | { | |
817 | if (v2 < 0) | |
818 | { | |
819 | if (v1 == 0) | |
820 | error (_("Attempt to raise 0 to negative power.")); | |
821 | else | |
822 | return 0; | |
823 | } | |
824 | else | |
825 | { | |
581e13c1 | 826 | /* The Russian Peasant's Algorithm. */ |
d118ef87 PH |
827 | LONGEST v; |
828 | ||
829 | v = 1; | |
830 | for (;;) | |
831 | { | |
832 | if (v2 & 1L) | |
833 | v *= v1; | |
834 | v2 >>= 1; | |
835 | if (v2 == 0) | |
836 | return v; | |
837 | v1 *= v1; | |
838 | } | |
839 | } | |
840 | } | |
841 | ||
842 | /* Integer exponentiation: V1**V2, where both arguments are | |
843 | integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */ | |
581e13c1 | 844 | |
d118ef87 PH |
845 | static ULONGEST |
846 | uinteger_pow (ULONGEST v1, LONGEST v2) | |
847 | { | |
848 | if (v2 < 0) | |
849 | { | |
850 | if (v1 == 0) | |
851 | error (_("Attempt to raise 0 to negative power.")); | |
852 | else | |
853 | return 0; | |
854 | } | |
855 | else | |
856 | { | |
581e13c1 | 857 | /* The Russian Peasant's Algorithm. */ |
d118ef87 PH |
858 | ULONGEST v; |
859 | ||
860 | v = 1; | |
861 | for (;;) | |
862 | { | |
863 | if (v2 & 1L) | |
864 | v *= v1; | |
865 | v2 >>= 1; | |
866 | if (v2 == 0) | |
867 | return v; | |
868 | v1 *= v1; | |
869 | } | |
870 | } | |
871 | } | |
872 | ||
4ef30785 TJB |
873 | /* Obtain decimal value of arguments for binary operation, converting from |
874 | other types if one of them is not decimal floating point. */ | |
875 | static void | |
876 | value_args_as_decimal (struct value *arg1, struct value *arg2, | |
e17a4113 UW |
877 | gdb_byte *x, int *len_x, enum bfd_endian *byte_order_x, |
878 | gdb_byte *y, int *len_y, enum bfd_endian *byte_order_y) | |
4ef30785 TJB |
879 | { |
880 | struct type *type1, *type2; | |
881 | ||
882 | type1 = check_typedef (value_type (arg1)); | |
883 | type2 = check_typedef (value_type (arg2)); | |
884 | ||
885 | /* At least one of the arguments must be of decimal float type. */ | |
886 | gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT | |
887 | || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT); | |
888 | ||
889 | if (TYPE_CODE (type1) == TYPE_CODE_FLT | |
890 | || TYPE_CODE (type2) == TYPE_CODE_FLT) | |
891 | /* The DFP extension to the C language does not allow mixing of | |
892 | * decimal float types with other float types in expressions | |
893 | * (see WDTR 24732, page 12). */ | |
3e43a32a MS |
894 | error (_("Mixing decimal floating types with " |
895 | "other floating types is not allowed.")); | |
4ef30785 TJB |
896 | |
897 | /* Obtain decimal value of arg1, converting from other types | |
898 | if necessary. */ | |
899 | ||
900 | if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT) | |
901 | { | |
e17a4113 | 902 | *byte_order_x = gdbarch_byte_order (get_type_arch (type1)); |
4ef30785 TJB |
903 | *len_x = TYPE_LENGTH (type1); |
904 | memcpy (x, value_contents (arg1), *len_x); | |
905 | } | |
906 | else if (is_integral_type (type1)) | |
907 | { | |
e17a4113 | 908 | *byte_order_x = gdbarch_byte_order (get_type_arch (type2)); |
4ef30785 | 909 | *len_x = TYPE_LENGTH (type2); |
e17a4113 | 910 | decimal_from_integral (arg1, x, *len_x, *byte_order_x); |
4ef30785 TJB |
911 | } |
912 | else | |
913 | error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1), | |
914 | TYPE_NAME (type2)); | |
915 | ||
916 | /* Obtain decimal value of arg2, converting from other types | |
917 | if necessary. */ | |
918 | ||
919 | if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT) | |
920 | { | |
e17a4113 | 921 | *byte_order_y = gdbarch_byte_order (get_type_arch (type2)); |
4ef30785 TJB |
922 | *len_y = TYPE_LENGTH (type2); |
923 | memcpy (y, value_contents (arg2), *len_y); | |
924 | } | |
925 | else if (is_integral_type (type2)) | |
926 | { | |
e17a4113 | 927 | *byte_order_y = gdbarch_byte_order (get_type_arch (type1)); |
4ef30785 | 928 | *len_y = TYPE_LENGTH (type1); |
e17a4113 | 929 | decimal_from_integral (arg2, y, *len_y, *byte_order_y); |
4ef30785 TJB |
930 | } |
931 | else | |
932 | error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1), | |
933 | TYPE_NAME (type2)); | |
934 | } | |
c5aa993b | 935 | |
c906108c SS |
936 | /* Perform a binary operation on two operands which have reasonable |
937 | representations as integers or floats. This includes booleans, | |
938 | characters, integers, or floats. | |
939 | Does not support addition and subtraction on pointers; | |
89eef114 | 940 | use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */ |
c906108c | 941 | |
7346b668 KW |
942 | static struct value * |
943 | scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) | |
c906108c | 944 | { |
f23631e4 | 945 | struct value *val; |
4066e646 UW |
946 | struct type *type1, *type2, *result_type; |
947 | ||
994b9211 AC |
948 | arg1 = coerce_ref (arg1); |
949 | arg2 = coerce_ref (arg2); | |
c906108c | 950 | |
4066e646 UW |
951 | type1 = check_typedef (value_type (arg1)); |
952 | type2 = check_typedef (value_type (arg2)); | |
953 | ||
954 | if ((TYPE_CODE (type1) != TYPE_CODE_FLT | |
955 | && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT | |
956 | && !is_integral_type (type1)) | |
957 | || (TYPE_CODE (type2) != TYPE_CODE_FLT | |
958 | && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT | |
959 | && !is_integral_type (type2))) | |
960 | error (_("Argument to arithmetic operation not a number or boolean.")); | |
c906108c | 961 | |
4066e646 UW |
962 | if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT |
963 | || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT) | |
4ef30785 | 964 | { |
4ef30785 | 965 | int len_v1, len_v2, len_v; |
e17a4113 | 966 | enum bfd_endian byte_order_v1, byte_order_v2, byte_order_v; |
4ef30785 TJB |
967 | gdb_byte v1[16], v2[16]; |
968 | gdb_byte v[16]; | |
969 | ||
289bd67a UW |
970 | /* If only one type is decimal float, use its type. |
971 | Otherwise use the bigger type. */ | |
972 | if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT) | |
973 | result_type = type2; | |
974 | else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT) | |
975 | result_type = type1; | |
976 | else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1)) | |
977 | result_type = type2; | |
978 | else | |
979 | result_type = type1; | |
980 | ||
981 | len_v = TYPE_LENGTH (result_type); | |
e17a4113 | 982 | byte_order_v = gdbarch_byte_order (get_type_arch (result_type)); |
289bd67a | 983 | |
e17a4113 UW |
984 | value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1, |
985 | v2, &len_v2, &byte_order_v2); | |
4ef30785 TJB |
986 | |
987 | switch (op) | |
988 | { | |
989 | case BINOP_ADD: | |
990 | case BINOP_SUB: | |
991 | case BINOP_MUL: | |
992 | case BINOP_DIV: | |
993 | case BINOP_EXP: | |
e17a4113 UW |
994 | decimal_binop (op, v1, len_v1, byte_order_v1, |
995 | v2, len_v2, byte_order_v2, | |
996 | v, len_v, byte_order_v); | |
4ef30785 TJB |
997 | break; |
998 | ||
999 | default: | |
1000 | error (_("Operation not valid for decimal floating point number.")); | |
1001 | } | |
1002 | ||
301f0ecf | 1003 | val = value_from_decfloat (result_type, v); |
4ef30785 | 1004 | } |
4066e646 UW |
1005 | else if (TYPE_CODE (type1) == TYPE_CODE_FLT |
1006 | || TYPE_CODE (type2) == TYPE_CODE_FLT) | |
c906108c SS |
1007 | { |
1008 | /* FIXME-if-picky-about-floating-accuracy: Should be doing this | |
c5aa993b JM |
1009 | in target format. real.c in GCC probably has the necessary |
1010 | code. */ | |
c4093a6a | 1011 | DOUBLEST v1, v2, v = 0; |
a109c7c1 | 1012 | |
c906108c SS |
1013 | v1 = value_as_double (arg1); |
1014 | v2 = value_as_double (arg2); | |
301f0ecf | 1015 | |
c906108c SS |
1016 | switch (op) |
1017 | { | |
1018 | case BINOP_ADD: | |
1019 | v = v1 + v2; | |
1020 | break; | |
1021 | ||
1022 | case BINOP_SUB: | |
1023 | v = v1 - v2; | |
1024 | break; | |
1025 | ||
1026 | case BINOP_MUL: | |
1027 | v = v1 * v2; | |
1028 | break; | |
1029 | ||
1030 | case BINOP_DIV: | |
1031 | v = v1 / v2; | |
1032 | break; | |
1033 | ||
bd49c137 WZ |
1034 | case BINOP_EXP: |
1035 | errno = 0; | |
1036 | v = pow (v1, v2); | |
1037 | if (errno) | |
3e43a32a MS |
1038 | error (_("Cannot perform exponentiation: %s"), |
1039 | safe_strerror (errno)); | |
bd49c137 | 1040 | break; |
c4093a6a | 1041 | |
d118ef87 PH |
1042 | case BINOP_MIN: |
1043 | v = v1 < v2 ? v1 : v2; | |
1044 | break; | |
1045 | ||
1046 | case BINOP_MAX: | |
1047 | v = v1 > v2 ? v1 : v2; | |
1048 | break; | |
1049 | ||
c906108c | 1050 | default: |
8a3fe4f8 | 1051 | error (_("Integer-only operation on floating point number.")); |
c906108c SS |
1052 | } |
1053 | ||
4066e646 UW |
1054 | /* If only one type is float, use its type. |
1055 | Otherwise use the bigger type. */ | |
1056 | if (TYPE_CODE (type1) != TYPE_CODE_FLT) | |
1057 | result_type = type2; | |
1058 | else if (TYPE_CODE (type2) != TYPE_CODE_FLT) | |
1059 | result_type = type1; | |
1060 | else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1)) | |
1061 | result_type = type2; | |
1062 | else | |
1063 | result_type = type1; | |
1064 | ||
301f0ecf | 1065 | val = allocate_value (result_type); |
990a07ab | 1066 | store_typed_floating (value_contents_raw (val), value_type (val), v); |
c906108c | 1067 | } |
4066e646 UW |
1068 | else if (TYPE_CODE (type1) == TYPE_CODE_BOOL |
1069 | || TYPE_CODE (type2) == TYPE_CODE_BOOL) | |
c5aa993b | 1070 | { |
c4093a6a | 1071 | LONGEST v1, v2, v = 0; |
a109c7c1 | 1072 | |
c5aa993b JM |
1073 | v1 = value_as_long (arg1); |
1074 | v2 = value_as_long (arg2); | |
1075 | ||
1076 | switch (op) | |
1077 | { | |
1078 | case BINOP_BITWISE_AND: | |
1079 | v = v1 & v2; | |
1080 | break; | |
1081 | ||
1082 | case BINOP_BITWISE_IOR: | |
1083 | v = v1 | v2; | |
1084 | break; | |
1085 | ||
1086 | case BINOP_BITWISE_XOR: | |
1087 | v = v1 ^ v2; | |
c4093a6a JM |
1088 | break; |
1089 | ||
1090 | case BINOP_EQUAL: | |
1091 | v = v1 == v2; | |
1092 | break; | |
1093 | ||
1094 | case BINOP_NOTEQUAL: | |
1095 | v = v1 != v2; | |
c5aa993b JM |
1096 | break; |
1097 | ||
1098 | default: | |
8a3fe4f8 | 1099 | error (_("Invalid operation on booleans.")); |
c5aa993b JM |
1100 | } |
1101 | ||
4066e646 UW |
1102 | result_type = type1; |
1103 | ||
301f0ecf | 1104 | val = allocate_value (result_type); |
990a07ab | 1105 | store_signed_integer (value_contents_raw (val), |
301f0ecf | 1106 | TYPE_LENGTH (result_type), |
e17a4113 | 1107 | gdbarch_byte_order (get_type_arch (result_type)), |
c5aa993b JM |
1108 | v); |
1109 | } | |
c906108c SS |
1110 | else |
1111 | /* Integral operations here. */ | |
c906108c | 1112 | { |
4066e646 UW |
1113 | /* Determine type length of the result, and if the operation should |
1114 | be done unsigned. For exponentiation and shift operators, | |
1115 | use the length and type of the left operand. Otherwise, | |
1116 | use the signedness of the operand with the greater length. | |
1117 | If both operands are of equal length, use unsigned operation | |
1118 | if one of the operands is unsigned. */ | |
1119 | if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP) | |
1120 | result_type = type1; | |
1121 | else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)) | |
1122 | result_type = type1; | |
1123 | else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1)) | |
1124 | result_type = type2; | |
1125 | else if (TYPE_UNSIGNED (type1)) | |
1126 | result_type = type1; | |
1127 | else if (TYPE_UNSIGNED (type2)) | |
1128 | result_type = type2; | |
1129 | else | |
1130 | result_type = type1; | |
c906108c | 1131 | |
4066e646 | 1132 | if (TYPE_UNSIGNED (result_type)) |
c906108c | 1133 | { |
d118ef87 | 1134 | LONGEST v2_signed = value_as_long (arg2); |
c4093a6a | 1135 | ULONGEST v1, v2, v = 0; |
a109c7c1 | 1136 | |
c906108c | 1137 | v1 = (ULONGEST) value_as_long (arg1); |
d118ef87 | 1138 | v2 = (ULONGEST) v2_signed; |
c906108c | 1139 | |
c906108c SS |
1140 | switch (op) |
1141 | { | |
1142 | case BINOP_ADD: | |
1143 | v = v1 + v2; | |
1144 | break; | |
c5aa993b | 1145 | |
c906108c SS |
1146 | case BINOP_SUB: |
1147 | v = v1 - v2; | |
1148 | break; | |
c5aa993b | 1149 | |
c906108c SS |
1150 | case BINOP_MUL: |
1151 | v = v1 * v2; | |
1152 | break; | |
c5aa993b | 1153 | |
c906108c | 1154 | case BINOP_DIV: |
ef80d18e | 1155 | case BINOP_INTDIV: |
c3940723 PM |
1156 | if (v2 != 0) |
1157 | v = v1 / v2; | |
1158 | else | |
1159 | error (_("Division by zero")); | |
c906108c | 1160 | break; |
c5aa993b | 1161 | |
bd49c137 | 1162 | case BINOP_EXP: |
d118ef87 | 1163 | v = uinteger_pow (v1, v2_signed); |
bd49c137 | 1164 | break; |
c4093a6a | 1165 | |
c906108c | 1166 | case BINOP_REM: |
f8597ac3 DE |
1167 | if (v2 != 0) |
1168 | v = v1 % v2; | |
1169 | else | |
1170 | error (_("Division by zero")); | |
c906108c | 1171 | break; |
c5aa993b | 1172 | |
c906108c SS |
1173 | case BINOP_MOD: |
1174 | /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, | |
581e13c1 | 1175 | v1 mod 0 has a defined value, v1. */ |
c906108c SS |
1176 | if (v2 == 0) |
1177 | { | |
1178 | v = v1; | |
1179 | } | |
1180 | else | |
1181 | { | |
c5aa993b | 1182 | v = v1 / v2; |
581e13c1 | 1183 | /* Note floor(v1/v2) == v1/v2 for unsigned. */ |
c906108c SS |
1184 | v = v1 - (v2 * v); |
1185 | } | |
1186 | break; | |
c5aa993b | 1187 | |
c906108c SS |
1188 | case BINOP_LSH: |
1189 | v = v1 << v2; | |
1190 | break; | |
c5aa993b | 1191 | |
c906108c SS |
1192 | case BINOP_RSH: |
1193 | v = v1 >> v2; | |
1194 | break; | |
c5aa993b | 1195 | |
c906108c SS |
1196 | case BINOP_BITWISE_AND: |
1197 | v = v1 & v2; | |
1198 | break; | |
c5aa993b | 1199 | |
c906108c SS |
1200 | case BINOP_BITWISE_IOR: |
1201 | v = v1 | v2; | |
1202 | break; | |
c5aa993b | 1203 | |
c906108c SS |
1204 | case BINOP_BITWISE_XOR: |
1205 | v = v1 ^ v2; | |
1206 | break; | |
c5aa993b | 1207 | |
c906108c SS |
1208 | case BINOP_LOGICAL_AND: |
1209 | v = v1 && v2; | |
1210 | break; | |
c5aa993b | 1211 | |
c906108c SS |
1212 | case BINOP_LOGICAL_OR: |
1213 | v = v1 || v2; | |
1214 | break; | |
c5aa993b | 1215 | |
c906108c SS |
1216 | case BINOP_MIN: |
1217 | v = v1 < v2 ? v1 : v2; | |
1218 | break; | |
c5aa993b | 1219 | |
c906108c SS |
1220 | case BINOP_MAX: |
1221 | v = v1 > v2 ? v1 : v2; | |
1222 | break; | |
1223 | ||
1224 | case BINOP_EQUAL: | |
1225 | v = v1 == v2; | |
1226 | break; | |
1227 | ||
c4093a6a JM |
1228 | case BINOP_NOTEQUAL: |
1229 | v = v1 != v2; | |
1230 | break; | |
1231 | ||
c906108c SS |
1232 | case BINOP_LESS: |
1233 | v = v1 < v2; | |
1234 | break; | |
c5aa993b | 1235 | |
b966cb8a TT |
1236 | case BINOP_GTR: |
1237 | v = v1 > v2; | |
1238 | break; | |
1239 | ||
1240 | case BINOP_LEQ: | |
1241 | v = v1 <= v2; | |
1242 | break; | |
1243 | ||
1244 | case BINOP_GEQ: | |
1245 | v = v1 >= v2; | |
1246 | break; | |
1247 | ||
c906108c | 1248 | default: |
8a3fe4f8 | 1249 | error (_("Invalid binary operation on numbers.")); |
c906108c SS |
1250 | } |
1251 | ||
301f0ecf | 1252 | val = allocate_value (result_type); |
990a07ab | 1253 | store_unsigned_integer (value_contents_raw (val), |
df407dfe | 1254 | TYPE_LENGTH (value_type (val)), |
e17a4113 UW |
1255 | gdbarch_byte_order |
1256 | (get_type_arch (result_type)), | |
c906108c SS |
1257 | v); |
1258 | } | |
1259 | else | |
1260 | { | |
c4093a6a | 1261 | LONGEST v1, v2, v = 0; |
a109c7c1 | 1262 | |
c906108c SS |
1263 | v1 = value_as_long (arg1); |
1264 | v2 = value_as_long (arg2); | |
c5aa993b | 1265 | |
c906108c SS |
1266 | switch (op) |
1267 | { | |
1268 | case BINOP_ADD: | |
1269 | v = v1 + v2; | |
1270 | break; | |
c5aa993b | 1271 | |
c906108c SS |
1272 | case BINOP_SUB: |
1273 | v = v1 - v2; | |
1274 | break; | |
c5aa993b | 1275 | |
c906108c SS |
1276 | case BINOP_MUL: |
1277 | v = v1 * v2; | |
1278 | break; | |
c5aa993b | 1279 | |
c906108c | 1280 | case BINOP_DIV: |
ef80d18e | 1281 | case BINOP_INTDIV: |
399cfac6 DL |
1282 | if (v2 != 0) |
1283 | v = v1 / v2; | |
1284 | else | |
8a3fe4f8 | 1285 | error (_("Division by zero")); |
c4093a6a JM |
1286 | break; |
1287 | ||
bd49c137 | 1288 | case BINOP_EXP: |
d118ef87 | 1289 | v = integer_pow (v1, v2); |
c906108c | 1290 | break; |
c5aa993b | 1291 | |
c906108c | 1292 | case BINOP_REM: |
399cfac6 DL |
1293 | if (v2 != 0) |
1294 | v = v1 % v2; | |
1295 | else | |
8a3fe4f8 | 1296 | error (_("Division by zero")); |
c906108c | 1297 | break; |
c5aa993b | 1298 | |
c906108c SS |
1299 | case BINOP_MOD: |
1300 | /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, | |
581e13c1 | 1301 | X mod 0 has a defined value, X. */ |
c906108c SS |
1302 | if (v2 == 0) |
1303 | { | |
1304 | v = v1; | |
1305 | } | |
1306 | else | |
1307 | { | |
c5aa993b | 1308 | v = v1 / v2; |
581e13c1 | 1309 | /* Compute floor. */ |
c906108c SS |
1310 | if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0)) |
1311 | { | |
1312 | v--; | |
1313 | } | |
1314 | v = v1 - (v2 * v); | |
1315 | } | |
1316 | break; | |
c5aa993b | 1317 | |
c906108c SS |
1318 | case BINOP_LSH: |
1319 | v = v1 << v2; | |
1320 | break; | |
c5aa993b | 1321 | |
c906108c SS |
1322 | case BINOP_RSH: |
1323 | v = v1 >> v2; | |
1324 | break; | |
c5aa993b | 1325 | |
c906108c SS |
1326 | case BINOP_BITWISE_AND: |
1327 | v = v1 & v2; | |
1328 | break; | |
c5aa993b | 1329 | |
c906108c SS |
1330 | case BINOP_BITWISE_IOR: |
1331 | v = v1 | v2; | |
1332 | break; | |
c5aa993b | 1333 | |
c906108c SS |
1334 | case BINOP_BITWISE_XOR: |
1335 | v = v1 ^ v2; | |
1336 | break; | |
c5aa993b | 1337 | |
c906108c SS |
1338 | case BINOP_LOGICAL_AND: |
1339 | v = v1 && v2; | |
1340 | break; | |
c5aa993b | 1341 | |
c906108c SS |
1342 | case BINOP_LOGICAL_OR: |
1343 | v = v1 || v2; | |
1344 | break; | |
c5aa993b | 1345 | |
c906108c SS |
1346 | case BINOP_MIN: |
1347 | v = v1 < v2 ? v1 : v2; | |
1348 | break; | |
c5aa993b | 1349 | |
c906108c SS |
1350 | case BINOP_MAX: |
1351 | v = v1 > v2 ? v1 : v2; | |
1352 | break; | |
1353 | ||
1354 | case BINOP_EQUAL: | |
1355 | v = v1 == v2; | |
1356 | break; | |
1357 | ||
b966cb8a TT |
1358 | case BINOP_NOTEQUAL: |
1359 | v = v1 != v2; | |
1360 | break; | |
1361 | ||
c906108c SS |
1362 | case BINOP_LESS: |
1363 | v = v1 < v2; | |
1364 | break; | |
c5aa993b | 1365 | |
b966cb8a TT |
1366 | case BINOP_GTR: |
1367 | v = v1 > v2; | |
1368 | break; | |
1369 | ||
1370 | case BINOP_LEQ: | |
1371 | v = v1 <= v2; | |
1372 | break; | |
1373 | ||
1374 | case BINOP_GEQ: | |
1375 | v = v1 >= v2; | |
1376 | break; | |
1377 | ||
c906108c | 1378 | default: |
8a3fe4f8 | 1379 | error (_("Invalid binary operation on numbers.")); |
c906108c SS |
1380 | } |
1381 | ||
301f0ecf | 1382 | val = allocate_value (result_type); |
990a07ab | 1383 | store_signed_integer (value_contents_raw (val), |
df407dfe | 1384 | TYPE_LENGTH (value_type (val)), |
e17a4113 UW |
1385 | gdbarch_byte_order |
1386 | (get_type_arch (result_type)), | |
c906108c SS |
1387 | v); |
1388 | } | |
1389 | } | |
1390 | ||
1391 | return val; | |
1392 | } | |
7346b668 KW |
1393 | |
1394 | /* Performs a binary operation on two vector operands by calling scalar_binop | |
1395 | for each pair of vector components. */ | |
1396 | ||
1397 | static struct value * | |
1398 | vector_binop (struct value *val1, struct value *val2, enum exp_opcode op) | |
1399 | { | |
1400 | struct value *val, *tmp, *mark; | |
1401 | struct type *type1, *type2, *eltype1, *eltype2, *result_type; | |
dbc98a8b KW |
1402 | int t1_is_vec, t2_is_vec, elsize, i; |
1403 | LONGEST low_bound1, high_bound1, low_bound2, high_bound2; | |
7346b668 KW |
1404 | |
1405 | type1 = check_typedef (value_type (val1)); | |
1406 | type2 = check_typedef (value_type (val2)); | |
1407 | ||
1408 | t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY | |
1409 | && TYPE_VECTOR (type1)) ? 1 : 0; | |
1410 | t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY | |
1411 | && TYPE_VECTOR (type2)) ? 1 : 0; | |
1412 | ||
1413 | if (!t1_is_vec || !t2_is_vec) | |
1414 | error (_("Vector operations are only supported among vectors")); | |
1415 | ||
dbc98a8b KW |
1416 | if (!get_array_bounds (type1, &low_bound1, &high_bound1) |
1417 | || !get_array_bounds (type2, &low_bound2, &high_bound2)) | |
1418 | error (_("Could not determine the vector bounds")); | |
1419 | ||
7346b668 KW |
1420 | eltype1 = check_typedef (TYPE_TARGET_TYPE (type1)); |
1421 | eltype2 = check_typedef (TYPE_TARGET_TYPE (type2)); | |
dbc98a8b | 1422 | elsize = TYPE_LENGTH (eltype1); |
7346b668 KW |
1423 | |
1424 | if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2) | |
dbc98a8b KW |
1425 | || elsize != TYPE_LENGTH (eltype2) |
1426 | || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2) | |
1427 | || low_bound1 != low_bound2 || high_bound1 != high_bound2) | |
7346b668 KW |
1428 | error (_("Cannot perform operation on vectors with different types")); |
1429 | ||
7346b668 KW |
1430 | val = allocate_value (type1); |
1431 | mark = value_mark (); | |
dbc98a8b | 1432 | for (i = 0; i < high_bound1 - low_bound1 + 1; i++) |
7346b668 KW |
1433 | { |
1434 | tmp = value_binop (value_subscript (val1, i), | |
1435 | value_subscript (val2, i), op); | |
1436 | memcpy (value_contents_writeable (val) + i * elsize, | |
1437 | value_contents_all (tmp), | |
1438 | elsize); | |
1439 | } | |
1440 | value_free_to_mark (mark); | |
1441 | ||
1442 | return val; | |
1443 | } | |
1444 | ||
1445 | /* Perform a binary operation on two operands. */ | |
1446 | ||
1447 | struct value * | |
1448 | value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) | |
1449 | { | |
3bdf2bbd | 1450 | struct value *val; |
7346b668 KW |
1451 | struct type *type1 = check_typedef (value_type (arg1)); |
1452 | struct type *type2 = check_typedef (value_type (arg2)); | |
3bdf2bbd KW |
1453 | int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY |
1454 | && TYPE_VECTOR (type1)); | |
1455 | int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY | |
1456 | && TYPE_VECTOR (type2)); | |
1457 | ||
1458 | if (!t1_is_vec && !t2_is_vec) | |
1459 | val = scalar_binop (arg1, arg2, op); | |
1460 | else if (t1_is_vec && t2_is_vec) | |
1461 | val = vector_binop (arg1, arg2, op); | |
7346b668 | 1462 | else |
3bdf2bbd KW |
1463 | { |
1464 | /* Widen the scalar operand to a vector. */ | |
1465 | struct value **v = t1_is_vec ? &arg2 : &arg1; | |
1466 | struct type *t = t1_is_vec ? type2 : type1; | |
1467 | ||
1468 | if (TYPE_CODE (t) != TYPE_CODE_FLT | |
1469 | && TYPE_CODE (t) != TYPE_CODE_DECFLOAT | |
1470 | && !is_integral_type (t)) | |
1471 | error (_("Argument to operation not a number or boolean.")); | |
1472 | ||
1473 | *v = value_cast (t1_is_vec ? type1 : type2, *v); | |
1474 | val = vector_binop (arg1, arg2, op); | |
1475 | } | |
1476 | ||
1477 | return val; | |
7346b668 | 1478 | } |
c906108c SS |
1479 | \f |
1480 | /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */ | |
1481 | ||
1482 | int | |
f23631e4 | 1483 | value_logical_not (struct value *arg1) |
c906108c | 1484 | { |
52f0bd74 | 1485 | int len; |
fc1a4b47 | 1486 | const gdb_byte *p; |
c906108c SS |
1487 | struct type *type1; |
1488 | ||
0ab7ba45 | 1489 | arg1 = coerce_array (arg1); |
df407dfe | 1490 | type1 = check_typedef (value_type (arg1)); |
c906108c SS |
1491 | |
1492 | if (TYPE_CODE (type1) == TYPE_CODE_FLT) | |
1493 | return 0 == value_as_double (arg1); | |
4ef30785 | 1494 | else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT) |
e17a4113 UW |
1495 | return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1), |
1496 | gdbarch_byte_order (get_type_arch (type1))); | |
c906108c SS |
1497 | |
1498 | len = TYPE_LENGTH (type1); | |
0fd88904 | 1499 | p = value_contents (arg1); |
c906108c SS |
1500 | |
1501 | while (--len >= 0) | |
1502 | { | |
1503 | if (*p++) | |
1504 | break; | |
1505 | } | |
1506 | ||
1507 | return len < 0; | |
1508 | } | |
1509 | ||
c4093a6a | 1510 | /* Perform a comparison on two string values (whose content are not |
581e13c1 | 1511 | necessarily null terminated) based on their length. */ |
c4093a6a JM |
1512 | |
1513 | static int | |
f23631e4 | 1514 | value_strcmp (struct value *arg1, struct value *arg2) |
c4093a6a | 1515 | { |
df407dfe AC |
1516 | int len1 = TYPE_LENGTH (value_type (arg1)); |
1517 | int len2 = TYPE_LENGTH (value_type (arg2)); | |
fc1a4b47 AC |
1518 | const gdb_byte *s1 = value_contents (arg1); |
1519 | const gdb_byte *s2 = value_contents (arg2); | |
c4093a6a JM |
1520 | int i, len = len1 < len2 ? len1 : len2; |
1521 | ||
1522 | for (i = 0; i < len; i++) | |
1523 | { | |
1524 | if (s1[i] < s2[i]) | |
1525 | return -1; | |
1526 | else if (s1[i] > s2[i]) | |
1527 | return 1; | |
1528 | else | |
1529 | continue; | |
1530 | } | |
1531 | ||
1532 | if (len1 < len2) | |
1533 | return -1; | |
1534 | else if (len1 > len2) | |
1535 | return 1; | |
1536 | else | |
1537 | return 0; | |
1538 | } | |
1539 | ||
c906108c SS |
1540 | /* Simulate the C operator == by returning a 1 |
1541 | iff ARG1 and ARG2 have equal contents. */ | |
1542 | ||
1543 | int | |
f23631e4 | 1544 | value_equal (struct value *arg1, struct value *arg2) |
c906108c | 1545 | { |
52f0bd74 | 1546 | int len; |
fc1a4b47 AC |
1547 | const gdb_byte *p1; |
1548 | const gdb_byte *p2; | |
c906108c SS |
1549 | struct type *type1, *type2; |
1550 | enum type_code code1; | |
1551 | enum type_code code2; | |
2de41bce | 1552 | int is_int1, is_int2; |
c906108c | 1553 | |
994b9211 AC |
1554 | arg1 = coerce_array (arg1); |
1555 | arg2 = coerce_array (arg2); | |
c906108c | 1556 | |
df407dfe AC |
1557 | type1 = check_typedef (value_type (arg1)); |
1558 | type2 = check_typedef (value_type (arg2)); | |
c906108c SS |
1559 | code1 = TYPE_CODE (type1); |
1560 | code2 = TYPE_CODE (type2); | |
2de41bce PH |
1561 | is_int1 = is_integral_type (type1); |
1562 | is_int2 = is_integral_type (type2); | |
c906108c | 1563 | |
2de41bce | 1564 | if (is_int1 && is_int2) |
c906108c SS |
1565 | return longest_to_int (value_as_long (value_binop (arg1, arg2, |
1566 | BINOP_EQUAL))); | |
2de41bce PH |
1567 | else if ((code1 == TYPE_CODE_FLT || is_int1) |
1568 | && (code2 == TYPE_CODE_FLT || is_int2)) | |
d067a990 MK |
1569 | { |
1570 | /* NOTE: kettenis/20050816: Avoid compiler bug on systems where | |
1571 | `long double' values are returned in static storage (m68k). */ | |
1572 | DOUBLEST d = value_as_double (arg1); | |
a109c7c1 | 1573 | |
d067a990 MK |
1574 | return d == value_as_double (arg2); |
1575 | } | |
4ef30785 TJB |
1576 | else if ((code1 == TYPE_CODE_DECFLOAT || is_int1) |
1577 | && (code2 == TYPE_CODE_DECFLOAT || is_int2)) | |
1578 | { | |
1579 | gdb_byte v1[16], v2[16]; | |
1580 | int len_v1, len_v2; | |
e17a4113 | 1581 | enum bfd_endian byte_order_v1, byte_order_v2; |
4ef30785 | 1582 | |
e17a4113 UW |
1583 | value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1, |
1584 | v2, &len_v2, &byte_order_v2); | |
4ef30785 | 1585 | |
e17a4113 UW |
1586 | return decimal_compare (v1, len_v1, byte_order_v1, |
1587 | v2, len_v2, byte_order_v2) == 0; | |
4ef30785 | 1588 | } |
c906108c SS |
1589 | |
1590 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
1591 | is bigger. */ | |
2de41bce | 1592 | else if (code1 == TYPE_CODE_PTR && is_int2) |
1aa20aa8 | 1593 | return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2); |
2de41bce | 1594 | else if (code2 == TYPE_CODE_PTR && is_int1) |
1aa20aa8 | 1595 | return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2); |
c906108c SS |
1596 | |
1597 | else if (code1 == code2 | |
1598 | && ((len = (int) TYPE_LENGTH (type1)) | |
1599 | == (int) TYPE_LENGTH (type2))) | |
1600 | { | |
0fd88904 AC |
1601 | p1 = value_contents (arg1); |
1602 | p2 = value_contents (arg2); | |
c906108c SS |
1603 | while (--len >= 0) |
1604 | { | |
1605 | if (*p1++ != *p2++) | |
1606 | break; | |
1607 | } | |
1608 | return len < 0; | |
1609 | } | |
c4093a6a JM |
1610 | else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) |
1611 | { | |
1612 | return value_strcmp (arg1, arg2) == 0; | |
1613 | } | |
c906108c SS |
1614 | else |
1615 | { | |
8a3fe4f8 | 1616 | error (_("Invalid type combination in equality test.")); |
581e13c1 | 1617 | return 0; /* For lint -- never reached. */ |
c906108c SS |
1618 | } |
1619 | } | |
1620 | ||
218d2fc6 TJB |
1621 | /* Compare values based on their raw contents. Useful for arrays since |
1622 | value_equal coerces them to pointers, thus comparing just the address | |
1623 | of the array instead of its contents. */ | |
1624 | ||
1625 | int | |
1626 | value_equal_contents (struct value *arg1, struct value *arg2) | |
1627 | { | |
1628 | struct type *type1, *type2; | |
1629 | ||
1630 | type1 = check_typedef (value_type (arg1)); | |
1631 | type2 = check_typedef (value_type (arg2)); | |
1632 | ||
1633 | return (TYPE_CODE (type1) == TYPE_CODE (type2) | |
1634 | && TYPE_LENGTH (type1) == TYPE_LENGTH (type2) | |
1635 | && memcmp (value_contents (arg1), value_contents (arg2), | |
1636 | TYPE_LENGTH (type1)) == 0); | |
1637 | } | |
1638 | ||
c906108c SS |
1639 | /* Simulate the C operator < by returning 1 |
1640 | iff ARG1's contents are less than ARG2's. */ | |
1641 | ||
1642 | int | |
f23631e4 | 1643 | value_less (struct value *arg1, struct value *arg2) |
c906108c | 1644 | { |
52f0bd74 AC |
1645 | enum type_code code1; |
1646 | enum type_code code2; | |
c906108c | 1647 | struct type *type1, *type2; |
2de41bce | 1648 | int is_int1, is_int2; |
c906108c | 1649 | |
994b9211 AC |
1650 | arg1 = coerce_array (arg1); |
1651 | arg2 = coerce_array (arg2); | |
c906108c | 1652 | |
df407dfe AC |
1653 | type1 = check_typedef (value_type (arg1)); |
1654 | type2 = check_typedef (value_type (arg2)); | |
c906108c SS |
1655 | code1 = TYPE_CODE (type1); |
1656 | code2 = TYPE_CODE (type2); | |
2de41bce PH |
1657 | is_int1 = is_integral_type (type1); |
1658 | is_int2 = is_integral_type (type2); | |
c906108c | 1659 | |
2de41bce | 1660 | if (is_int1 && is_int2) |
c906108c SS |
1661 | return longest_to_int (value_as_long (value_binop (arg1, arg2, |
1662 | BINOP_LESS))); | |
2de41bce PH |
1663 | else if ((code1 == TYPE_CODE_FLT || is_int1) |
1664 | && (code2 == TYPE_CODE_FLT || is_int2)) | |
d067a990 MK |
1665 | { |
1666 | /* NOTE: kettenis/20050816: Avoid compiler bug on systems where | |
1667 | `long double' values are returned in static storage (m68k). */ | |
1668 | DOUBLEST d = value_as_double (arg1); | |
a109c7c1 | 1669 | |
d067a990 MK |
1670 | return d < value_as_double (arg2); |
1671 | } | |
4ef30785 TJB |
1672 | else if ((code1 == TYPE_CODE_DECFLOAT || is_int1) |
1673 | && (code2 == TYPE_CODE_DECFLOAT || is_int2)) | |
1674 | { | |
1675 | gdb_byte v1[16], v2[16]; | |
1676 | int len_v1, len_v2; | |
e17a4113 | 1677 | enum bfd_endian byte_order_v1, byte_order_v2; |
4ef30785 | 1678 | |
e17a4113 UW |
1679 | value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1, |
1680 | v2, &len_v2, &byte_order_v2); | |
4ef30785 | 1681 | |
e17a4113 UW |
1682 | return decimal_compare (v1, len_v1, byte_order_v1, |
1683 | v2, len_v2, byte_order_v2) == -1; | |
4ef30785 | 1684 | } |
c906108c | 1685 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) |
1aa20aa8 | 1686 | return value_as_address (arg1) < value_as_address (arg2); |
c906108c SS |
1687 | |
1688 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
1689 | is bigger. */ | |
2de41bce | 1690 | else if (code1 == TYPE_CODE_PTR && is_int2) |
1aa20aa8 | 1691 | return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2); |
2de41bce | 1692 | else if (code2 == TYPE_CODE_PTR && is_int1) |
1aa20aa8 | 1693 | return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2); |
c4093a6a JM |
1694 | else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) |
1695 | return value_strcmp (arg1, arg2) < 0; | |
c906108c SS |
1696 | else |
1697 | { | |
8a3fe4f8 | 1698 | error (_("Invalid type combination in ordering comparison.")); |
c906108c SS |
1699 | return 0; |
1700 | } | |
1701 | } | |
1702 | \f | |
36e9969c NS |
1703 | /* The unary operators +, - and ~. They free the argument ARG1. */ |
1704 | ||
1705 | struct value * | |
1706 | value_pos (struct value *arg1) | |
1707 | { | |
1708 | struct type *type; | |
4066e646 | 1709 | |
36e9969c | 1710 | arg1 = coerce_ref (arg1); |
36e9969c NS |
1711 | type = check_typedef (value_type (arg1)); |
1712 | ||
1713 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
4066e646 | 1714 | return value_from_double (type, value_as_double (arg1)); |
4ef30785 | 1715 | else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) |
4066e646 | 1716 | return value_from_decfloat (type, value_contents (arg1)); |
36e9969c NS |
1717 | else if (is_integral_type (type)) |
1718 | { | |
4066e646 | 1719 | return value_from_longest (type, value_as_long (arg1)); |
36e9969c | 1720 | } |
120bd360 KW |
1721 | else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)) |
1722 | { | |
1723 | struct value *val = allocate_value (type); | |
1724 | ||
1725 | memcpy (value_contents_raw (val), value_contents (arg1), | |
1726 | TYPE_LENGTH (type)); | |
1727 | return val; | |
1728 | } | |
36e9969c NS |
1729 | else |
1730 | { | |
a73c6dcd | 1731 | error (_("Argument to positive operation not a number.")); |
581e13c1 | 1732 | return 0; /* For lint -- never reached. */ |
36e9969c NS |
1733 | } |
1734 | } | |
c906108c | 1735 | |
f23631e4 AC |
1736 | struct value * |
1737 | value_neg (struct value *arg1) | |
c906108c | 1738 | { |
52f0bd74 | 1739 | struct type *type; |
4066e646 | 1740 | |
994b9211 | 1741 | arg1 = coerce_ref (arg1); |
df407dfe | 1742 | type = check_typedef (value_type (arg1)); |
c906108c | 1743 | |
27bc4d80 TJB |
1744 | if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) |
1745 | { | |
4066e646 | 1746 | struct value *val = allocate_value (type); |
27bc4d80 | 1747 | int len = TYPE_LENGTH (type); |
581e13c1 | 1748 | gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long. */ |
27bc4d80 | 1749 | |
4ef30785 | 1750 | memcpy (decbytes, value_contents (arg1), len); |
27bc4d80 | 1751 | |
50810684 | 1752 | if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE) |
27bc4d80 TJB |
1753 | decbytes[len-1] = decbytes[len - 1] | 0x80; |
1754 | else | |
1755 | decbytes[0] = decbytes[0] | 0x80; | |
1756 | ||
1757 | memcpy (value_contents_raw (val), decbytes, len); | |
1758 | return val; | |
1759 | } | |
301f0ecf | 1760 | else if (TYPE_CODE (type) == TYPE_CODE_FLT) |
4066e646 | 1761 | return value_from_double (type, -value_as_double (arg1)); |
2de41bce | 1762 | else if (is_integral_type (type)) |
c906108c | 1763 | { |
4066e646 | 1764 | return value_from_longest (type, -value_as_long (arg1)); |
c5aa993b | 1765 | } |
120bd360 KW |
1766 | else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)) |
1767 | { | |
1768 | struct value *tmp, *val = allocate_value (type); | |
1769 | struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type)); | |
cfa6f054 KW |
1770 | int i; |
1771 | LONGEST low_bound, high_bound; | |
120bd360 | 1772 | |
cfa6f054 KW |
1773 | if (!get_array_bounds (type, &low_bound, &high_bound)) |
1774 | error (_("Could not determine the vector bounds")); | |
1775 | ||
1776 | for (i = 0; i < high_bound - low_bound + 1; i++) | |
120bd360 KW |
1777 | { |
1778 | tmp = value_neg (value_subscript (arg1, i)); | |
1779 | memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype), | |
1780 | value_contents_all (tmp), TYPE_LENGTH (eltype)); | |
1781 | } | |
1782 | return val; | |
1783 | } | |
c5aa993b JM |
1784 | else |
1785 | { | |
8a3fe4f8 | 1786 | error (_("Argument to negate operation not a number.")); |
581e13c1 | 1787 | return 0; /* For lint -- never reached. */ |
c906108c | 1788 | } |
c906108c SS |
1789 | } |
1790 | ||
f23631e4 AC |
1791 | struct value * |
1792 | value_complement (struct value *arg1) | |
c906108c | 1793 | { |
52f0bd74 | 1794 | struct type *type; |
120bd360 | 1795 | struct value *val; |
4066e646 | 1796 | |
994b9211 | 1797 | arg1 = coerce_ref (arg1); |
df407dfe | 1798 | type = check_typedef (value_type (arg1)); |
c906108c | 1799 | |
120bd360 KW |
1800 | if (is_integral_type (type)) |
1801 | val = value_from_longest (type, ~value_as_long (arg1)); | |
1802 | else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)) | |
1803 | { | |
1804 | struct value *tmp; | |
1805 | struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type)); | |
cfa6f054 KW |
1806 | int i; |
1807 | LONGEST low_bound, high_bound; | |
1808 | ||
1809 | if (!get_array_bounds (type, &low_bound, &high_bound)) | |
1810 | error (_("Could not determine the vector bounds")); | |
120bd360 KW |
1811 | |
1812 | val = allocate_value (type); | |
cfa6f054 | 1813 | for (i = 0; i < high_bound - low_bound + 1; i++) |
120bd360 KW |
1814 | { |
1815 | tmp = value_complement (value_subscript (arg1, i)); | |
1816 | memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype), | |
1817 | value_contents_all (tmp), TYPE_LENGTH (eltype)); | |
1818 | } | |
1819 | } | |
1820 | else | |
1821 | error (_("Argument to complement operation not an integer, boolean.")); | |
c906108c | 1822 | |
120bd360 | 1823 | return val; |
c906108c SS |
1824 | } |
1825 | \f | |
df407dfe | 1826 | /* The INDEX'th bit of SET value whose value_type is TYPE, |
0fd88904 | 1827 | and whose value_contents is valaddr. |
581e13c1 | 1828 | Return -1 if out of range, -2 other error. */ |
c906108c SS |
1829 | |
1830 | int | |
fc1a4b47 | 1831 | value_bit_index (struct type *type, const gdb_byte *valaddr, int index) |
c906108c | 1832 | { |
50810684 | 1833 | struct gdbarch *gdbarch = get_type_arch (type); |
c906108c SS |
1834 | LONGEST low_bound, high_bound; |
1835 | LONGEST word; | |
1836 | unsigned rel_index; | |
262452ec | 1837 | struct type *range = TYPE_INDEX_TYPE (type); |
a109c7c1 | 1838 | |
c906108c SS |
1839 | if (get_discrete_bounds (range, &low_bound, &high_bound) < 0) |
1840 | return -2; | |
1841 | if (index < low_bound || index > high_bound) | |
1842 | return -1; | |
1843 | rel_index = index - low_bound; | |
e17a4113 UW |
1844 | word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1, |
1845 | gdbarch_byte_order (gdbarch)); | |
c906108c | 1846 | rel_index %= TARGET_CHAR_BIT; |
50810684 | 1847 | if (gdbarch_bits_big_endian (gdbarch)) |
c906108c SS |
1848 | rel_index = TARGET_CHAR_BIT - 1 - rel_index; |
1849 | return (word >> rel_index) & 1; | |
1850 | } | |
1851 | ||
fbb06eb1 | 1852 | int |
f23631e4 | 1853 | value_in (struct value *element, struct value *set) |
c906108c SS |
1854 | { |
1855 | int member; | |
df407dfe AC |
1856 | struct type *settype = check_typedef (value_type (set)); |
1857 | struct type *eltype = check_typedef (value_type (element)); | |
a109c7c1 | 1858 | |
c906108c SS |
1859 | if (TYPE_CODE (eltype) == TYPE_CODE_RANGE) |
1860 | eltype = TYPE_TARGET_TYPE (eltype); | |
1861 | if (TYPE_CODE (settype) != TYPE_CODE_SET) | |
8a3fe4f8 | 1862 | error (_("Second argument of 'IN' has wrong type")); |
c906108c SS |
1863 | if (TYPE_CODE (eltype) != TYPE_CODE_INT |
1864 | && TYPE_CODE (eltype) != TYPE_CODE_CHAR | |
1865 | && TYPE_CODE (eltype) != TYPE_CODE_ENUM | |
1866 | && TYPE_CODE (eltype) != TYPE_CODE_BOOL) | |
8a3fe4f8 | 1867 | error (_("First argument of 'IN' has wrong type")); |
0fd88904 | 1868 | member = value_bit_index (settype, value_contents (set), |
c906108c SS |
1869 | value_as_long (element)); |
1870 | if (member < 0) | |
8a3fe4f8 | 1871 | error (_("First argument of 'IN' not in range")); |
fbb06eb1 | 1872 | return member; |
c906108c SS |
1873 | } |
1874 | ||
1875 | void | |
fba45db2 | 1876 | _initialize_valarith (void) |
c906108c SS |
1877 | { |
1878 | } |