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