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 | } | |
79afc5ef SW |
544 | throw_error (NOT_FOUND_ERROR, |
545 | _("member function %s not found"), tstr); | |
c906108c SS |
546 | #ifdef lint |
547 | return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); | |
548 | #endif | |
549 | } | |
550 | ||
551 | /* We know that arg1 is a structure, so try to find a unary user | |
552 | defined operator that matches the operator in question. | |
553 | Create an argument vector that calls arg1.operator @ (arg1) | |
554 | and return that value (where '@' is (almost) any unary operator which | |
555 | is legal for GNU C++). */ | |
556 | ||
f23631e4 AC |
557 | struct value * |
558 | value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside) | |
c906108c | 559 | { |
50810684 | 560 | struct gdbarch *gdbarch = get_type_arch (value_type (arg1)); |
f23631e4 | 561 | struct value **argvec; |
c906108c SS |
562 | char *ptr, *mangle_ptr; |
563 | char tstr[13], mangle_tstr[13]; | |
491b8946 | 564 | int static_memfuncp, nargs; |
c906108c | 565 | |
994b9211 | 566 | arg1 = coerce_ref (arg1); |
c906108c SS |
567 | |
568 | /* now we know that what we have to do is construct our | |
569 | arg vector and find the right function to call it with. */ | |
570 | ||
df407dfe | 571 | if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT) |
8a3fe4f8 | 572 | error (_("Can't do that unary op on that type")); /* FIXME be explicit */ |
c906108c | 573 | |
491b8946 | 574 | argvec = (struct value **) alloca (sizeof (struct value *) * 4); |
c906108c SS |
575 | argvec[1] = value_addr (arg1); |
576 | argvec[2] = 0; | |
577 | ||
491b8946 DJ |
578 | nargs = 1; |
579 | ||
c5aa993b JM |
580 | /* make the right function name up */ |
581 | strcpy (tstr, "operator__"); | |
582 | ptr = tstr + 8; | |
583 | strcpy (mangle_tstr, "__"); | |
584 | mangle_ptr = mangle_tstr + 2; | |
c906108c SS |
585 | switch (op) |
586 | { | |
c5aa993b JM |
587 | case UNOP_PREINCREMENT: |
588 | strcpy (ptr, "++"); | |
589 | break; | |
590 | case UNOP_PREDECREMENT: | |
491b8946 | 591 | strcpy (ptr, "--"); |
c5aa993b JM |
592 | break; |
593 | case UNOP_POSTINCREMENT: | |
594 | strcpy (ptr, "++"); | |
22601c15 | 595 | argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0); |
491b8946 DJ |
596 | argvec[3] = 0; |
597 | nargs ++; | |
c5aa993b JM |
598 | break; |
599 | case UNOP_POSTDECREMENT: | |
491b8946 | 600 | strcpy (ptr, "--"); |
22601c15 | 601 | argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0); |
491b8946 DJ |
602 | argvec[3] = 0; |
603 | nargs ++; | |
c5aa993b JM |
604 | break; |
605 | case UNOP_LOGICAL_NOT: | |
606 | strcpy (ptr, "!"); | |
607 | break; | |
608 | case UNOP_COMPLEMENT: | |
609 | strcpy (ptr, "~"); | |
610 | break; | |
611 | case UNOP_NEG: | |
612 | strcpy (ptr, "-"); | |
613 | break; | |
36e9969c NS |
614 | case UNOP_PLUS: |
615 | strcpy (ptr, "+"); | |
616 | break; | |
c5aa993b JM |
617 | case UNOP_IND: |
618 | strcpy (ptr, "*"); | |
619 | break; | |
79afc5ef SW |
620 | case STRUCTOP_PTR: |
621 | strcpy (ptr, "->"); | |
622 | break; | |
c906108c | 623 | default: |
8a3fe4f8 | 624 | error (_("Invalid unary operation specified.")); |
c906108c SS |
625 | } |
626 | ||
4c3376c8 SW |
627 | argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr, |
628 | &static_memfuncp, nargs); | |
c906108c SS |
629 | |
630 | if (argvec[0]) | |
631 | { | |
632 | if (static_memfuncp) | |
633 | { | |
634 | argvec[1] = argvec[0]; | |
491b8946 | 635 | nargs --; |
c906108c SS |
636 | argvec++; |
637 | } | |
638 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
639 | { | |
640 | struct type *return_type; | |
a109c7c1 | 641 | |
c906108c | 642 | return_type |
df407dfe | 643 | = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0]))); |
c906108c SS |
644 | return value_zero (return_type, VALUE_LVAL (arg1)); |
645 | } | |
491b8946 | 646 | return call_function_by_hand (argvec[0], nargs, argvec + 1); |
c906108c | 647 | } |
79afc5ef SW |
648 | throw_error (NOT_FOUND_ERROR, |
649 | _("member function %s not found"), tstr); | |
650 | ||
c5aa993b | 651 | return 0; /* For lint -- never reached */ |
c906108c | 652 | } |
c906108c | 653 | \f |
c5aa993b | 654 | |
c906108c SS |
655 | /* Concatenate two values with the following conditions: |
656 | ||
c5aa993b JM |
657 | (1) Both values must be either bitstring values or character string |
658 | values and the resulting value consists of the concatenation of | |
659 | ARG1 followed by ARG2. | |
c906108c | 660 | |
c5aa993b | 661 | or |
c906108c | 662 | |
c5aa993b JM |
663 | One value must be an integer value and the other value must be |
664 | either a bitstring value or character string value, which is | |
665 | to be repeated by the number of times specified by the integer | |
666 | value. | |
c906108c SS |
667 | |
668 | ||
c5aa993b JM |
669 | (2) Boolean values are also allowed and are treated as bit string |
670 | values of length 1. | |
c906108c | 671 | |
c5aa993b JM |
672 | (3) Character values are also allowed and are treated as character |
673 | string values of length 1. | |
674 | */ | |
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 | |
694 | repeated. */ | |
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 | ||
711 | /* Now process the input values. */ | |
712 | ||
713 | if (TYPE_CODE (type1) == TYPE_CODE_INT) | |
714 | { | |
715 | /* We have a repeat count. Validate the second value and then | |
c5aa993b | 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 SS |
758 | { |
759 | /* We have two character strings to concatenate. */ | |
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 | { | |
794 | /* We have two bitstrings to concatenate. */ | |
795 | if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING | |
796 | && TYPE_CODE (type2) != TYPE_CODE_BOOL) | |
797 | { | |
8a3fe4f8 | 798 | error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.")); |
c906108c | 799 | } |
8a3fe4f8 | 800 | error (_("unimplemented support for bitstring/boolean concatenation.")); |
c5aa993b | 801 | } |
c906108c SS |
802 | else |
803 | { | |
804 | /* We don't know how to concatenate these operands. */ | |
8a3fe4f8 | 805 | error (_("illegal operands for concatenation.")); |
c906108c SS |
806 | } |
807 | return (outval); | |
808 | } | |
c906108c | 809 | \f |
d118ef87 PH |
810 | /* Integer exponentiation: V1**V2, where both arguments are |
811 | integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */ | |
812 | static LONGEST | |
813 | integer_pow (LONGEST v1, LONGEST v2) | |
814 | { | |
815 | if (v2 < 0) | |
816 | { | |
817 | if (v1 == 0) | |
818 | error (_("Attempt to raise 0 to negative power.")); | |
819 | else | |
820 | return 0; | |
821 | } | |
822 | else | |
823 | { | |
824 | /* The Russian Peasant's Algorithm */ | |
825 | LONGEST v; | |
826 | ||
827 | v = 1; | |
828 | for (;;) | |
829 | { | |
830 | if (v2 & 1L) | |
831 | v *= v1; | |
832 | v2 >>= 1; | |
833 | if (v2 == 0) | |
834 | return v; | |
835 | v1 *= v1; | |
836 | } | |
837 | } | |
838 | } | |
839 | ||
840 | /* Integer exponentiation: V1**V2, where both arguments are | |
841 | integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */ | |
842 | static ULONGEST | |
843 | uinteger_pow (ULONGEST v1, LONGEST v2) | |
844 | { | |
845 | if (v2 < 0) | |
846 | { | |
847 | if (v1 == 0) | |
848 | error (_("Attempt to raise 0 to negative power.")); | |
849 | else | |
850 | return 0; | |
851 | } | |
852 | else | |
853 | { | |
854 | /* The Russian Peasant's Algorithm */ | |
855 | ULONGEST v; | |
856 | ||
857 | v = 1; | |
858 | for (;;) | |
859 | { | |
860 | if (v2 & 1L) | |
861 | v *= v1; | |
862 | v2 >>= 1; | |
863 | if (v2 == 0) | |
864 | return v; | |
865 | v1 *= v1; | |
866 | } | |
867 | } | |
868 | } | |
869 | ||
4ef30785 TJB |
870 | /* Obtain decimal value of arguments for binary operation, converting from |
871 | other types if one of them is not decimal floating point. */ | |
872 | static void | |
873 | value_args_as_decimal (struct value *arg1, struct value *arg2, | |
e17a4113 UW |
874 | gdb_byte *x, int *len_x, enum bfd_endian *byte_order_x, |
875 | gdb_byte *y, int *len_y, enum bfd_endian *byte_order_y) | |
4ef30785 TJB |
876 | { |
877 | struct type *type1, *type2; | |
878 | ||
879 | type1 = check_typedef (value_type (arg1)); | |
880 | type2 = check_typedef (value_type (arg2)); | |
881 | ||
882 | /* At least one of the arguments must be of decimal float type. */ | |
883 | gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT | |
884 | || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT); | |
885 | ||
886 | if (TYPE_CODE (type1) == TYPE_CODE_FLT | |
887 | || TYPE_CODE (type2) == TYPE_CODE_FLT) | |
888 | /* The DFP extension to the C language does not allow mixing of | |
889 | * decimal float types with other float types in expressions | |
890 | * (see WDTR 24732, page 12). */ | |
891 | error (_("Mixing decimal floating types with other floating types is not allowed.")); | |
892 | ||
893 | /* Obtain decimal value of arg1, converting from other types | |
894 | if necessary. */ | |
895 | ||
896 | if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT) | |
897 | { | |
e17a4113 | 898 | *byte_order_x = gdbarch_byte_order (get_type_arch (type1)); |
4ef30785 TJB |
899 | *len_x = TYPE_LENGTH (type1); |
900 | memcpy (x, value_contents (arg1), *len_x); | |
901 | } | |
902 | else if (is_integral_type (type1)) | |
903 | { | |
e17a4113 | 904 | *byte_order_x = gdbarch_byte_order (get_type_arch (type2)); |
4ef30785 | 905 | *len_x = TYPE_LENGTH (type2); |
e17a4113 | 906 | decimal_from_integral (arg1, x, *len_x, *byte_order_x); |
4ef30785 TJB |
907 | } |
908 | else | |
909 | error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1), | |
910 | TYPE_NAME (type2)); | |
911 | ||
912 | /* Obtain decimal value of arg2, converting from other types | |
913 | if necessary. */ | |
914 | ||
915 | if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT) | |
916 | { | |
e17a4113 | 917 | *byte_order_y = gdbarch_byte_order (get_type_arch (type2)); |
4ef30785 TJB |
918 | *len_y = TYPE_LENGTH (type2); |
919 | memcpy (y, value_contents (arg2), *len_y); | |
920 | } | |
921 | else if (is_integral_type (type2)) | |
922 | { | |
e17a4113 | 923 | *byte_order_y = gdbarch_byte_order (get_type_arch (type1)); |
4ef30785 | 924 | *len_y = TYPE_LENGTH (type1); |
e17a4113 | 925 | decimal_from_integral (arg2, y, *len_y, *byte_order_y); |
4ef30785 TJB |
926 | } |
927 | else | |
928 | error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1), | |
929 | TYPE_NAME (type2)); | |
930 | } | |
c5aa993b | 931 | |
c906108c SS |
932 | /* Perform a binary operation on two operands which have reasonable |
933 | representations as integers or floats. This includes booleans, | |
934 | characters, integers, or floats. | |
935 | Does not support addition and subtraction on pointers; | |
89eef114 | 936 | use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */ |
c906108c | 937 | |
7346b668 KW |
938 | static struct value * |
939 | scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) | |
c906108c | 940 | { |
f23631e4 | 941 | struct value *val; |
4066e646 UW |
942 | struct type *type1, *type2, *result_type; |
943 | ||
994b9211 AC |
944 | arg1 = coerce_ref (arg1); |
945 | arg2 = coerce_ref (arg2); | |
c906108c | 946 | |
4066e646 UW |
947 | type1 = check_typedef (value_type (arg1)); |
948 | type2 = check_typedef (value_type (arg2)); | |
949 | ||
950 | if ((TYPE_CODE (type1) != TYPE_CODE_FLT | |
951 | && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT | |
952 | && !is_integral_type (type1)) | |
953 | || (TYPE_CODE (type2) != TYPE_CODE_FLT | |
954 | && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT | |
955 | && !is_integral_type (type2))) | |
956 | error (_("Argument to arithmetic operation not a number or boolean.")); | |
c906108c | 957 | |
4066e646 UW |
958 | if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT |
959 | || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT) | |
4ef30785 | 960 | { |
4ef30785 | 961 | int len_v1, len_v2, len_v; |
e17a4113 | 962 | enum bfd_endian byte_order_v1, byte_order_v2, byte_order_v; |
4ef30785 TJB |
963 | gdb_byte v1[16], v2[16]; |
964 | gdb_byte v[16]; | |
965 | ||
289bd67a UW |
966 | /* If only one type is decimal float, use its type. |
967 | Otherwise use the bigger type. */ | |
968 | if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT) | |
969 | result_type = type2; | |
970 | else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT) | |
971 | result_type = type1; | |
972 | else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1)) | |
973 | result_type = type2; | |
974 | else | |
975 | result_type = type1; | |
976 | ||
977 | len_v = TYPE_LENGTH (result_type); | |
e17a4113 | 978 | byte_order_v = gdbarch_byte_order (get_type_arch (result_type)); |
289bd67a | 979 | |
e17a4113 UW |
980 | value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1, |
981 | v2, &len_v2, &byte_order_v2); | |
4ef30785 TJB |
982 | |
983 | switch (op) | |
984 | { | |
985 | case BINOP_ADD: | |
986 | case BINOP_SUB: | |
987 | case BINOP_MUL: | |
988 | case BINOP_DIV: | |
989 | case BINOP_EXP: | |
e17a4113 UW |
990 | decimal_binop (op, v1, len_v1, byte_order_v1, |
991 | v2, len_v2, byte_order_v2, | |
992 | v, len_v, byte_order_v); | |
4ef30785 TJB |
993 | break; |
994 | ||
995 | default: | |
996 | error (_("Operation not valid for decimal floating point number.")); | |
997 | } | |
998 | ||
301f0ecf | 999 | val = value_from_decfloat (result_type, v); |
4ef30785 | 1000 | } |
4066e646 UW |
1001 | else if (TYPE_CODE (type1) == TYPE_CODE_FLT |
1002 | || TYPE_CODE (type2) == TYPE_CODE_FLT) | |
c906108c SS |
1003 | { |
1004 | /* FIXME-if-picky-about-floating-accuracy: Should be doing this | |
c5aa993b JM |
1005 | in target format. real.c in GCC probably has the necessary |
1006 | code. */ | |
c4093a6a | 1007 | DOUBLEST v1, v2, v = 0; |
a109c7c1 | 1008 | |
c906108c SS |
1009 | v1 = value_as_double (arg1); |
1010 | v2 = value_as_double (arg2); | |
301f0ecf | 1011 | |
c906108c SS |
1012 | switch (op) |
1013 | { | |
1014 | case BINOP_ADD: | |
1015 | v = v1 + v2; | |
1016 | break; | |
1017 | ||
1018 | case BINOP_SUB: | |
1019 | v = v1 - v2; | |
1020 | break; | |
1021 | ||
1022 | case BINOP_MUL: | |
1023 | v = v1 * v2; | |
1024 | break; | |
1025 | ||
1026 | case BINOP_DIV: | |
1027 | v = v1 / v2; | |
1028 | break; | |
1029 | ||
bd49c137 WZ |
1030 | case BINOP_EXP: |
1031 | errno = 0; | |
1032 | v = pow (v1, v2); | |
1033 | if (errno) | |
1034 | error (_("Cannot perform exponentiation: %s"), safe_strerror (errno)); | |
1035 | break; | |
c4093a6a | 1036 | |
d118ef87 PH |
1037 | case BINOP_MIN: |
1038 | v = v1 < v2 ? v1 : v2; | |
1039 | break; | |
1040 | ||
1041 | case BINOP_MAX: | |
1042 | v = v1 > v2 ? v1 : v2; | |
1043 | break; | |
1044 | ||
c906108c | 1045 | default: |
8a3fe4f8 | 1046 | error (_("Integer-only operation on floating point number.")); |
c906108c SS |
1047 | } |
1048 | ||
4066e646 UW |
1049 | /* If only one type is float, use its type. |
1050 | Otherwise use the bigger type. */ | |
1051 | if (TYPE_CODE (type1) != TYPE_CODE_FLT) | |
1052 | result_type = type2; | |
1053 | else if (TYPE_CODE (type2) != TYPE_CODE_FLT) | |
1054 | result_type = type1; | |
1055 | else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1)) | |
1056 | result_type = type2; | |
1057 | else | |
1058 | result_type = type1; | |
1059 | ||
301f0ecf | 1060 | val = allocate_value (result_type); |
990a07ab | 1061 | store_typed_floating (value_contents_raw (val), value_type (val), v); |
c906108c | 1062 | } |
4066e646 UW |
1063 | else if (TYPE_CODE (type1) == TYPE_CODE_BOOL |
1064 | || TYPE_CODE (type2) == TYPE_CODE_BOOL) | |
c5aa993b | 1065 | { |
c4093a6a | 1066 | LONGEST v1, v2, v = 0; |
a109c7c1 | 1067 | |
c5aa993b JM |
1068 | v1 = value_as_long (arg1); |
1069 | v2 = value_as_long (arg2); | |
1070 | ||
1071 | switch (op) | |
1072 | { | |
1073 | case BINOP_BITWISE_AND: | |
1074 | v = v1 & v2; | |
1075 | break; | |
1076 | ||
1077 | case BINOP_BITWISE_IOR: | |
1078 | v = v1 | v2; | |
1079 | break; | |
1080 | ||
1081 | case BINOP_BITWISE_XOR: | |
1082 | v = v1 ^ v2; | |
c4093a6a JM |
1083 | break; |
1084 | ||
1085 | case BINOP_EQUAL: | |
1086 | v = v1 == v2; | |
1087 | break; | |
1088 | ||
1089 | case BINOP_NOTEQUAL: | |
1090 | v = v1 != v2; | |
c5aa993b JM |
1091 | break; |
1092 | ||
1093 | default: | |
8a3fe4f8 | 1094 | error (_("Invalid operation on booleans.")); |
c5aa993b JM |
1095 | } |
1096 | ||
4066e646 UW |
1097 | result_type = type1; |
1098 | ||
301f0ecf | 1099 | val = allocate_value (result_type); |
990a07ab | 1100 | store_signed_integer (value_contents_raw (val), |
301f0ecf | 1101 | TYPE_LENGTH (result_type), |
e17a4113 | 1102 | gdbarch_byte_order (get_type_arch (result_type)), |
c5aa993b JM |
1103 | v); |
1104 | } | |
c906108c SS |
1105 | else |
1106 | /* Integral operations here. */ | |
c906108c | 1107 | { |
4066e646 UW |
1108 | /* Determine type length of the result, and if the operation should |
1109 | be done unsigned. For exponentiation and shift operators, | |
1110 | use the length and type of the left operand. Otherwise, | |
1111 | use the signedness of the operand with the greater length. | |
1112 | If both operands are of equal length, use unsigned operation | |
1113 | if one of the operands is unsigned. */ | |
1114 | if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP) | |
1115 | result_type = type1; | |
1116 | else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)) | |
1117 | result_type = type1; | |
1118 | else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1)) | |
1119 | result_type = type2; | |
1120 | else if (TYPE_UNSIGNED (type1)) | |
1121 | result_type = type1; | |
1122 | else if (TYPE_UNSIGNED (type2)) | |
1123 | result_type = type2; | |
1124 | else | |
1125 | result_type = type1; | |
c906108c | 1126 | |
4066e646 | 1127 | if (TYPE_UNSIGNED (result_type)) |
c906108c | 1128 | { |
d118ef87 | 1129 | LONGEST v2_signed = value_as_long (arg2); |
c4093a6a | 1130 | ULONGEST v1, v2, v = 0; |
a109c7c1 | 1131 | |
c906108c | 1132 | v1 = (ULONGEST) value_as_long (arg1); |
d118ef87 | 1133 | v2 = (ULONGEST) v2_signed; |
c906108c | 1134 | |
c906108c SS |
1135 | switch (op) |
1136 | { | |
1137 | case BINOP_ADD: | |
1138 | v = v1 + v2; | |
1139 | break; | |
c5aa993b | 1140 | |
c906108c SS |
1141 | case BINOP_SUB: |
1142 | v = v1 - v2; | |
1143 | break; | |
c5aa993b | 1144 | |
c906108c SS |
1145 | case BINOP_MUL: |
1146 | v = v1 * v2; | |
1147 | break; | |
c5aa993b | 1148 | |
c906108c | 1149 | case BINOP_DIV: |
ef80d18e | 1150 | case BINOP_INTDIV: |
c3940723 PM |
1151 | if (v2 != 0) |
1152 | v = v1 / v2; | |
1153 | else | |
1154 | error (_("Division by zero")); | |
c906108c | 1155 | break; |
c5aa993b | 1156 | |
bd49c137 | 1157 | case BINOP_EXP: |
d118ef87 | 1158 | v = uinteger_pow (v1, v2_signed); |
bd49c137 | 1159 | break; |
c4093a6a | 1160 | |
c906108c | 1161 | case BINOP_REM: |
f8597ac3 DE |
1162 | if (v2 != 0) |
1163 | v = v1 % v2; | |
1164 | else | |
1165 | error (_("Division by zero")); | |
c906108c | 1166 | break; |
c5aa993b | 1167 | |
c906108c SS |
1168 | case BINOP_MOD: |
1169 | /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, | |
1170 | v1 mod 0 has a defined value, v1. */ | |
c906108c SS |
1171 | if (v2 == 0) |
1172 | { | |
1173 | v = v1; | |
1174 | } | |
1175 | else | |
1176 | { | |
c5aa993b | 1177 | v = v1 / v2; |
c906108c SS |
1178 | /* Note floor(v1/v2) == v1/v2 for unsigned. */ |
1179 | v = v1 - (v2 * v); | |
1180 | } | |
1181 | break; | |
c5aa993b | 1182 | |
c906108c SS |
1183 | case BINOP_LSH: |
1184 | v = v1 << v2; | |
1185 | break; | |
c5aa993b | 1186 | |
c906108c SS |
1187 | case BINOP_RSH: |
1188 | v = v1 >> v2; | |
1189 | break; | |
c5aa993b | 1190 | |
c906108c SS |
1191 | case BINOP_BITWISE_AND: |
1192 | v = v1 & v2; | |
1193 | break; | |
c5aa993b | 1194 | |
c906108c SS |
1195 | case BINOP_BITWISE_IOR: |
1196 | v = v1 | v2; | |
1197 | break; | |
c5aa993b | 1198 | |
c906108c SS |
1199 | case BINOP_BITWISE_XOR: |
1200 | v = v1 ^ v2; | |
1201 | break; | |
c5aa993b | 1202 | |
c906108c SS |
1203 | case BINOP_LOGICAL_AND: |
1204 | v = v1 && v2; | |
1205 | break; | |
c5aa993b | 1206 | |
c906108c SS |
1207 | case BINOP_LOGICAL_OR: |
1208 | v = v1 || v2; | |
1209 | break; | |
c5aa993b | 1210 | |
c906108c SS |
1211 | case BINOP_MIN: |
1212 | v = v1 < v2 ? v1 : v2; | |
1213 | break; | |
c5aa993b | 1214 | |
c906108c SS |
1215 | case BINOP_MAX: |
1216 | v = v1 > v2 ? v1 : v2; | |
1217 | break; | |
1218 | ||
1219 | case BINOP_EQUAL: | |
1220 | v = v1 == v2; | |
1221 | break; | |
1222 | ||
c4093a6a JM |
1223 | case BINOP_NOTEQUAL: |
1224 | v = v1 != v2; | |
1225 | break; | |
1226 | ||
c906108c SS |
1227 | case BINOP_LESS: |
1228 | v = v1 < v2; | |
1229 | break; | |
c5aa993b | 1230 | |
b966cb8a TT |
1231 | case BINOP_GTR: |
1232 | v = v1 > v2; | |
1233 | break; | |
1234 | ||
1235 | case BINOP_LEQ: | |
1236 | v = v1 <= v2; | |
1237 | break; | |
1238 | ||
1239 | case BINOP_GEQ: | |
1240 | v = v1 >= v2; | |
1241 | break; | |
1242 | ||
c906108c | 1243 | default: |
8a3fe4f8 | 1244 | error (_("Invalid binary operation on numbers.")); |
c906108c SS |
1245 | } |
1246 | ||
301f0ecf | 1247 | val = allocate_value (result_type); |
990a07ab | 1248 | store_unsigned_integer (value_contents_raw (val), |
df407dfe | 1249 | TYPE_LENGTH (value_type (val)), |
e17a4113 UW |
1250 | gdbarch_byte_order |
1251 | (get_type_arch (result_type)), | |
c906108c SS |
1252 | v); |
1253 | } | |
1254 | else | |
1255 | { | |
c4093a6a | 1256 | LONGEST v1, v2, v = 0; |
a109c7c1 | 1257 | |
c906108c SS |
1258 | v1 = value_as_long (arg1); |
1259 | v2 = value_as_long (arg2); | |
c5aa993b | 1260 | |
c906108c SS |
1261 | switch (op) |
1262 | { | |
1263 | case BINOP_ADD: | |
1264 | v = v1 + v2; | |
1265 | break; | |
c5aa993b | 1266 | |
c906108c SS |
1267 | case BINOP_SUB: |
1268 | v = v1 - v2; | |
1269 | break; | |
c5aa993b | 1270 | |
c906108c SS |
1271 | case BINOP_MUL: |
1272 | v = v1 * v2; | |
1273 | break; | |
c5aa993b | 1274 | |
c906108c | 1275 | case BINOP_DIV: |
ef80d18e | 1276 | case BINOP_INTDIV: |
399cfac6 DL |
1277 | if (v2 != 0) |
1278 | v = v1 / v2; | |
1279 | else | |
8a3fe4f8 | 1280 | error (_("Division by zero")); |
c4093a6a JM |
1281 | break; |
1282 | ||
bd49c137 | 1283 | case BINOP_EXP: |
d118ef87 | 1284 | v = integer_pow (v1, v2); |
c906108c | 1285 | break; |
c5aa993b | 1286 | |
c906108c | 1287 | case BINOP_REM: |
399cfac6 DL |
1288 | if (v2 != 0) |
1289 | v = v1 % v2; | |
1290 | else | |
8a3fe4f8 | 1291 | error (_("Division by zero")); |
c906108c | 1292 | break; |
c5aa993b | 1293 | |
c906108c SS |
1294 | case BINOP_MOD: |
1295 | /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, | |
1296 | X mod 0 has a defined value, X. */ | |
c906108c SS |
1297 | if (v2 == 0) |
1298 | { | |
1299 | v = v1; | |
1300 | } | |
1301 | else | |
1302 | { | |
c5aa993b | 1303 | v = v1 / v2; |
c906108c SS |
1304 | /* Compute floor. */ |
1305 | if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0)) | |
1306 | { | |
1307 | v--; | |
1308 | } | |
1309 | v = v1 - (v2 * v); | |
1310 | } | |
1311 | break; | |
c5aa993b | 1312 | |
c906108c SS |
1313 | case BINOP_LSH: |
1314 | v = v1 << v2; | |
1315 | break; | |
c5aa993b | 1316 | |
c906108c SS |
1317 | case BINOP_RSH: |
1318 | v = v1 >> v2; | |
1319 | break; | |
c5aa993b | 1320 | |
c906108c SS |
1321 | case BINOP_BITWISE_AND: |
1322 | v = v1 & v2; | |
1323 | break; | |
c5aa993b | 1324 | |
c906108c SS |
1325 | case BINOP_BITWISE_IOR: |
1326 | v = v1 | v2; | |
1327 | break; | |
c5aa993b | 1328 | |
c906108c SS |
1329 | case BINOP_BITWISE_XOR: |
1330 | v = v1 ^ v2; | |
1331 | break; | |
c5aa993b | 1332 | |
c906108c SS |
1333 | case BINOP_LOGICAL_AND: |
1334 | v = v1 && v2; | |
1335 | break; | |
c5aa993b | 1336 | |
c906108c SS |
1337 | case BINOP_LOGICAL_OR: |
1338 | v = v1 || v2; | |
1339 | break; | |
c5aa993b | 1340 | |
c906108c SS |
1341 | case BINOP_MIN: |
1342 | v = v1 < v2 ? v1 : v2; | |
1343 | break; | |
c5aa993b | 1344 | |
c906108c SS |
1345 | case BINOP_MAX: |
1346 | v = v1 > v2 ? v1 : v2; | |
1347 | break; | |
1348 | ||
1349 | case BINOP_EQUAL: | |
1350 | v = v1 == v2; | |
1351 | break; | |
1352 | ||
b966cb8a TT |
1353 | case BINOP_NOTEQUAL: |
1354 | v = v1 != v2; | |
1355 | break; | |
1356 | ||
c906108c SS |
1357 | case BINOP_LESS: |
1358 | v = v1 < v2; | |
1359 | break; | |
c5aa993b | 1360 | |
b966cb8a TT |
1361 | case BINOP_GTR: |
1362 | v = v1 > v2; | |
1363 | break; | |
1364 | ||
1365 | case BINOP_LEQ: | |
1366 | v = v1 <= v2; | |
1367 | break; | |
1368 | ||
1369 | case BINOP_GEQ: | |
1370 | v = v1 >= v2; | |
1371 | break; | |
1372 | ||
c906108c | 1373 | default: |
8a3fe4f8 | 1374 | error (_("Invalid binary operation on numbers.")); |
c906108c SS |
1375 | } |
1376 | ||
301f0ecf | 1377 | val = allocate_value (result_type); |
990a07ab | 1378 | store_signed_integer (value_contents_raw (val), |
df407dfe | 1379 | TYPE_LENGTH (value_type (val)), |
e17a4113 UW |
1380 | gdbarch_byte_order |
1381 | (get_type_arch (result_type)), | |
c906108c SS |
1382 | v); |
1383 | } | |
1384 | } | |
1385 | ||
1386 | return val; | |
1387 | } | |
7346b668 KW |
1388 | |
1389 | /* Performs a binary operation on two vector operands by calling scalar_binop | |
1390 | for each pair of vector components. */ | |
1391 | ||
1392 | static struct value * | |
1393 | vector_binop (struct value *val1, struct value *val2, enum exp_opcode op) | |
1394 | { | |
1395 | struct value *val, *tmp, *mark; | |
1396 | struct type *type1, *type2, *eltype1, *eltype2, *result_type; | |
dbc98a8b KW |
1397 | int t1_is_vec, t2_is_vec, elsize, i; |
1398 | LONGEST low_bound1, high_bound1, low_bound2, high_bound2; | |
7346b668 KW |
1399 | |
1400 | type1 = check_typedef (value_type (val1)); | |
1401 | type2 = check_typedef (value_type (val2)); | |
1402 | ||
1403 | t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY | |
1404 | && TYPE_VECTOR (type1)) ? 1 : 0; | |
1405 | t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY | |
1406 | && TYPE_VECTOR (type2)) ? 1 : 0; | |
1407 | ||
1408 | if (!t1_is_vec || !t2_is_vec) | |
1409 | error (_("Vector operations are only supported among vectors")); | |
1410 | ||
dbc98a8b KW |
1411 | if (!get_array_bounds (type1, &low_bound1, &high_bound1) |
1412 | || !get_array_bounds (type2, &low_bound2, &high_bound2)) | |
1413 | error (_("Could not determine the vector bounds")); | |
1414 | ||
7346b668 KW |
1415 | eltype1 = check_typedef (TYPE_TARGET_TYPE (type1)); |
1416 | eltype2 = check_typedef (TYPE_TARGET_TYPE (type2)); | |
dbc98a8b | 1417 | elsize = TYPE_LENGTH (eltype1); |
7346b668 KW |
1418 | |
1419 | if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2) | |
dbc98a8b KW |
1420 | || elsize != TYPE_LENGTH (eltype2) |
1421 | || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2) | |
1422 | || low_bound1 != low_bound2 || high_bound1 != high_bound2) | |
7346b668 KW |
1423 | error (_("Cannot perform operation on vectors with different types")); |
1424 | ||
7346b668 KW |
1425 | val = allocate_value (type1); |
1426 | mark = value_mark (); | |
dbc98a8b | 1427 | for (i = 0; i < high_bound1 - low_bound1 + 1; i++) |
7346b668 KW |
1428 | { |
1429 | tmp = value_binop (value_subscript (val1, i), | |
1430 | value_subscript (val2, i), op); | |
1431 | memcpy (value_contents_writeable (val) + i * elsize, | |
1432 | value_contents_all (tmp), | |
1433 | elsize); | |
1434 | } | |
1435 | value_free_to_mark (mark); | |
1436 | ||
1437 | return val; | |
1438 | } | |
1439 | ||
1440 | /* Perform a binary operation on two operands. */ | |
1441 | ||
1442 | struct value * | |
1443 | value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) | |
1444 | { | |
3bdf2bbd | 1445 | struct value *val; |
7346b668 KW |
1446 | struct type *type1 = check_typedef (value_type (arg1)); |
1447 | struct type *type2 = check_typedef (value_type (arg2)); | |
3bdf2bbd KW |
1448 | int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY |
1449 | && TYPE_VECTOR (type1)); | |
1450 | int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY | |
1451 | && TYPE_VECTOR (type2)); | |
1452 | ||
1453 | if (!t1_is_vec && !t2_is_vec) | |
1454 | val = scalar_binop (arg1, arg2, op); | |
1455 | else if (t1_is_vec && t2_is_vec) | |
1456 | val = vector_binop (arg1, arg2, op); | |
7346b668 | 1457 | else |
3bdf2bbd KW |
1458 | { |
1459 | /* Widen the scalar operand to a vector. */ | |
1460 | struct value **v = t1_is_vec ? &arg2 : &arg1; | |
1461 | struct type *t = t1_is_vec ? type2 : type1; | |
1462 | ||
1463 | if (TYPE_CODE (t) != TYPE_CODE_FLT | |
1464 | && TYPE_CODE (t) != TYPE_CODE_DECFLOAT | |
1465 | && !is_integral_type (t)) | |
1466 | error (_("Argument to operation not a number or boolean.")); | |
1467 | ||
1468 | *v = value_cast (t1_is_vec ? type1 : type2, *v); | |
1469 | val = vector_binop (arg1, arg2, op); | |
1470 | } | |
1471 | ||
1472 | return val; | |
7346b668 | 1473 | } |
c906108c SS |
1474 | \f |
1475 | /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */ | |
1476 | ||
1477 | int | |
f23631e4 | 1478 | value_logical_not (struct value *arg1) |
c906108c | 1479 | { |
52f0bd74 | 1480 | int len; |
fc1a4b47 | 1481 | const gdb_byte *p; |
c906108c SS |
1482 | struct type *type1; |
1483 | ||
0ab7ba45 | 1484 | arg1 = coerce_array (arg1); |
df407dfe | 1485 | type1 = check_typedef (value_type (arg1)); |
c906108c SS |
1486 | |
1487 | if (TYPE_CODE (type1) == TYPE_CODE_FLT) | |
1488 | return 0 == value_as_double (arg1); | |
4ef30785 | 1489 | else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT) |
e17a4113 UW |
1490 | return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1), |
1491 | gdbarch_byte_order (get_type_arch (type1))); | |
c906108c SS |
1492 | |
1493 | len = TYPE_LENGTH (type1); | |
0fd88904 | 1494 | p = value_contents (arg1); |
c906108c SS |
1495 | |
1496 | while (--len >= 0) | |
1497 | { | |
1498 | if (*p++) | |
1499 | break; | |
1500 | } | |
1501 | ||
1502 | return len < 0; | |
1503 | } | |
1504 | ||
c4093a6a JM |
1505 | /* Perform a comparison on two string values (whose content are not |
1506 | necessarily null terminated) based on their length */ | |
1507 | ||
1508 | static int | |
f23631e4 | 1509 | value_strcmp (struct value *arg1, struct value *arg2) |
c4093a6a | 1510 | { |
df407dfe AC |
1511 | int len1 = TYPE_LENGTH (value_type (arg1)); |
1512 | int len2 = TYPE_LENGTH (value_type (arg2)); | |
fc1a4b47 AC |
1513 | const gdb_byte *s1 = value_contents (arg1); |
1514 | const gdb_byte *s2 = value_contents (arg2); | |
c4093a6a JM |
1515 | int i, len = len1 < len2 ? len1 : len2; |
1516 | ||
1517 | for (i = 0; i < len; i++) | |
1518 | { | |
1519 | if (s1[i] < s2[i]) | |
1520 | return -1; | |
1521 | else if (s1[i] > s2[i]) | |
1522 | return 1; | |
1523 | else | |
1524 | continue; | |
1525 | } | |
1526 | ||
1527 | if (len1 < len2) | |
1528 | return -1; | |
1529 | else if (len1 > len2) | |
1530 | return 1; | |
1531 | else | |
1532 | return 0; | |
1533 | } | |
1534 | ||
c906108c SS |
1535 | /* Simulate the C operator == by returning a 1 |
1536 | iff ARG1 and ARG2 have equal contents. */ | |
1537 | ||
1538 | int | |
f23631e4 | 1539 | value_equal (struct value *arg1, struct value *arg2) |
c906108c | 1540 | { |
52f0bd74 | 1541 | int len; |
fc1a4b47 AC |
1542 | const gdb_byte *p1; |
1543 | const gdb_byte *p2; | |
c906108c SS |
1544 | struct type *type1, *type2; |
1545 | enum type_code code1; | |
1546 | enum type_code code2; | |
2de41bce | 1547 | int is_int1, is_int2; |
c906108c | 1548 | |
994b9211 AC |
1549 | arg1 = coerce_array (arg1); |
1550 | arg2 = coerce_array (arg2); | |
c906108c | 1551 | |
df407dfe AC |
1552 | type1 = check_typedef (value_type (arg1)); |
1553 | type2 = check_typedef (value_type (arg2)); | |
c906108c SS |
1554 | code1 = TYPE_CODE (type1); |
1555 | code2 = TYPE_CODE (type2); | |
2de41bce PH |
1556 | is_int1 = is_integral_type (type1); |
1557 | is_int2 = is_integral_type (type2); | |
c906108c | 1558 | |
2de41bce | 1559 | if (is_int1 && is_int2) |
c906108c SS |
1560 | return longest_to_int (value_as_long (value_binop (arg1, arg2, |
1561 | BINOP_EQUAL))); | |
2de41bce PH |
1562 | else if ((code1 == TYPE_CODE_FLT || is_int1) |
1563 | && (code2 == TYPE_CODE_FLT || is_int2)) | |
d067a990 MK |
1564 | { |
1565 | /* NOTE: kettenis/20050816: Avoid compiler bug on systems where | |
1566 | `long double' values are returned in static storage (m68k). */ | |
1567 | DOUBLEST d = value_as_double (arg1); | |
a109c7c1 | 1568 | |
d067a990 MK |
1569 | return d == value_as_double (arg2); |
1570 | } | |
4ef30785 TJB |
1571 | else if ((code1 == TYPE_CODE_DECFLOAT || is_int1) |
1572 | && (code2 == TYPE_CODE_DECFLOAT || is_int2)) | |
1573 | { | |
1574 | gdb_byte v1[16], v2[16]; | |
1575 | int len_v1, len_v2; | |
e17a4113 | 1576 | enum bfd_endian byte_order_v1, byte_order_v2; |
4ef30785 | 1577 | |
e17a4113 UW |
1578 | value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1, |
1579 | v2, &len_v2, &byte_order_v2); | |
4ef30785 | 1580 | |
e17a4113 UW |
1581 | return decimal_compare (v1, len_v1, byte_order_v1, |
1582 | v2, len_v2, byte_order_v2) == 0; | |
4ef30785 | 1583 | } |
c906108c SS |
1584 | |
1585 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
1586 | is bigger. */ | |
2de41bce | 1587 | else if (code1 == TYPE_CODE_PTR && is_int2) |
1aa20aa8 | 1588 | return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2); |
2de41bce | 1589 | else if (code2 == TYPE_CODE_PTR && is_int1) |
1aa20aa8 | 1590 | return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2); |
c906108c SS |
1591 | |
1592 | else if (code1 == code2 | |
1593 | && ((len = (int) TYPE_LENGTH (type1)) | |
1594 | == (int) TYPE_LENGTH (type2))) | |
1595 | { | |
0fd88904 AC |
1596 | p1 = value_contents (arg1); |
1597 | p2 = value_contents (arg2); | |
c906108c SS |
1598 | while (--len >= 0) |
1599 | { | |
1600 | if (*p1++ != *p2++) | |
1601 | break; | |
1602 | } | |
1603 | return len < 0; | |
1604 | } | |
c4093a6a JM |
1605 | else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) |
1606 | { | |
1607 | return value_strcmp (arg1, arg2) == 0; | |
1608 | } | |
c906108c SS |
1609 | else |
1610 | { | |
8a3fe4f8 | 1611 | error (_("Invalid type combination in equality test.")); |
c5aa993b | 1612 | return 0; /* For lint -- never reached */ |
c906108c SS |
1613 | } |
1614 | } | |
1615 | ||
218d2fc6 TJB |
1616 | /* Compare values based on their raw contents. Useful for arrays since |
1617 | value_equal coerces them to pointers, thus comparing just the address | |
1618 | of the array instead of its contents. */ | |
1619 | ||
1620 | int | |
1621 | value_equal_contents (struct value *arg1, struct value *arg2) | |
1622 | { | |
1623 | struct type *type1, *type2; | |
1624 | ||
1625 | type1 = check_typedef (value_type (arg1)); | |
1626 | type2 = check_typedef (value_type (arg2)); | |
1627 | ||
1628 | return (TYPE_CODE (type1) == TYPE_CODE (type2) | |
1629 | && TYPE_LENGTH (type1) == TYPE_LENGTH (type2) | |
1630 | && memcmp (value_contents (arg1), value_contents (arg2), | |
1631 | TYPE_LENGTH (type1)) == 0); | |
1632 | } | |
1633 | ||
c906108c SS |
1634 | /* Simulate the C operator < by returning 1 |
1635 | iff ARG1's contents are less than ARG2's. */ | |
1636 | ||
1637 | int | |
f23631e4 | 1638 | value_less (struct value *arg1, struct value *arg2) |
c906108c | 1639 | { |
52f0bd74 AC |
1640 | enum type_code code1; |
1641 | enum type_code code2; | |
c906108c | 1642 | struct type *type1, *type2; |
2de41bce | 1643 | int is_int1, is_int2; |
c906108c | 1644 | |
994b9211 AC |
1645 | arg1 = coerce_array (arg1); |
1646 | arg2 = coerce_array (arg2); | |
c906108c | 1647 | |
df407dfe AC |
1648 | type1 = check_typedef (value_type (arg1)); |
1649 | type2 = check_typedef (value_type (arg2)); | |
c906108c SS |
1650 | code1 = TYPE_CODE (type1); |
1651 | code2 = TYPE_CODE (type2); | |
2de41bce PH |
1652 | is_int1 = is_integral_type (type1); |
1653 | is_int2 = is_integral_type (type2); | |
c906108c | 1654 | |
2de41bce | 1655 | if (is_int1 && is_int2) |
c906108c SS |
1656 | return longest_to_int (value_as_long (value_binop (arg1, arg2, |
1657 | BINOP_LESS))); | |
2de41bce PH |
1658 | else if ((code1 == TYPE_CODE_FLT || is_int1) |
1659 | && (code2 == TYPE_CODE_FLT || is_int2)) | |
d067a990 MK |
1660 | { |
1661 | /* NOTE: kettenis/20050816: Avoid compiler bug on systems where | |
1662 | `long double' values are returned in static storage (m68k). */ | |
1663 | DOUBLEST d = value_as_double (arg1); | |
a109c7c1 | 1664 | |
d067a990 MK |
1665 | return d < value_as_double (arg2); |
1666 | } | |
4ef30785 TJB |
1667 | else if ((code1 == TYPE_CODE_DECFLOAT || is_int1) |
1668 | && (code2 == TYPE_CODE_DECFLOAT || is_int2)) | |
1669 | { | |
1670 | gdb_byte v1[16], v2[16]; | |
1671 | int len_v1, len_v2; | |
e17a4113 | 1672 | enum bfd_endian byte_order_v1, byte_order_v2; |
4ef30785 | 1673 | |
e17a4113 UW |
1674 | value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1, |
1675 | v2, &len_v2, &byte_order_v2); | |
4ef30785 | 1676 | |
e17a4113 UW |
1677 | return decimal_compare (v1, len_v1, byte_order_v1, |
1678 | v2, len_v2, byte_order_v2) == -1; | |
4ef30785 | 1679 | } |
c906108c | 1680 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) |
1aa20aa8 | 1681 | return value_as_address (arg1) < value_as_address (arg2); |
c906108c SS |
1682 | |
1683 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
1684 | is bigger. */ | |
2de41bce | 1685 | else if (code1 == TYPE_CODE_PTR && is_int2) |
1aa20aa8 | 1686 | return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2); |
2de41bce | 1687 | else if (code2 == TYPE_CODE_PTR && is_int1) |
1aa20aa8 | 1688 | return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2); |
c4093a6a JM |
1689 | else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) |
1690 | return value_strcmp (arg1, arg2) < 0; | |
c906108c SS |
1691 | else |
1692 | { | |
8a3fe4f8 | 1693 | error (_("Invalid type combination in ordering comparison.")); |
c906108c SS |
1694 | return 0; |
1695 | } | |
1696 | } | |
1697 | \f | |
36e9969c NS |
1698 | /* The unary operators +, - and ~. They free the argument ARG1. */ |
1699 | ||
1700 | struct value * | |
1701 | value_pos (struct value *arg1) | |
1702 | { | |
1703 | struct type *type; | |
4066e646 | 1704 | |
36e9969c | 1705 | arg1 = coerce_ref (arg1); |
36e9969c NS |
1706 | type = check_typedef (value_type (arg1)); |
1707 | ||
1708 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
4066e646 | 1709 | return value_from_double (type, value_as_double (arg1)); |
4ef30785 | 1710 | else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) |
4066e646 | 1711 | return value_from_decfloat (type, value_contents (arg1)); |
36e9969c NS |
1712 | else if (is_integral_type (type)) |
1713 | { | |
4066e646 | 1714 | return value_from_longest (type, value_as_long (arg1)); |
36e9969c | 1715 | } |
120bd360 KW |
1716 | else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)) |
1717 | { | |
1718 | struct value *val = allocate_value (type); | |
1719 | ||
1720 | memcpy (value_contents_raw (val), value_contents (arg1), | |
1721 | TYPE_LENGTH (type)); | |
1722 | return val; | |
1723 | } | |
36e9969c NS |
1724 | else |
1725 | { | |
1726 | error ("Argument to positive operation not a number."); | |
1727 | return 0; /* For lint -- never reached */ | |
1728 | } | |
1729 | } | |
c906108c | 1730 | |
f23631e4 AC |
1731 | struct value * |
1732 | value_neg (struct value *arg1) | |
c906108c | 1733 | { |
52f0bd74 | 1734 | struct type *type; |
4066e646 | 1735 | |
994b9211 | 1736 | arg1 = coerce_ref (arg1); |
df407dfe | 1737 | type = check_typedef (value_type (arg1)); |
c906108c | 1738 | |
27bc4d80 TJB |
1739 | if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) |
1740 | { | |
4066e646 | 1741 | struct value *val = allocate_value (type); |
27bc4d80 TJB |
1742 | int len = TYPE_LENGTH (type); |
1743 | gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long */ | |
1744 | ||
4ef30785 | 1745 | memcpy (decbytes, value_contents (arg1), len); |
27bc4d80 | 1746 | |
50810684 | 1747 | if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE) |
27bc4d80 TJB |
1748 | decbytes[len-1] = decbytes[len - 1] | 0x80; |
1749 | else | |
1750 | decbytes[0] = decbytes[0] | 0x80; | |
1751 | ||
1752 | memcpy (value_contents_raw (val), decbytes, len); | |
1753 | return val; | |
1754 | } | |
301f0ecf | 1755 | else if (TYPE_CODE (type) == TYPE_CODE_FLT) |
4066e646 | 1756 | return value_from_double (type, -value_as_double (arg1)); |
2de41bce | 1757 | else if (is_integral_type (type)) |
c906108c | 1758 | { |
4066e646 | 1759 | return value_from_longest (type, -value_as_long (arg1)); |
c5aa993b | 1760 | } |
120bd360 KW |
1761 | else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)) |
1762 | { | |
1763 | struct value *tmp, *val = allocate_value (type); | |
1764 | struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type)); | |
1765 | int i, n = TYPE_LENGTH (type) / TYPE_LENGTH (eltype); | |
1766 | ||
1767 | for (i = 0; i < n; i++) | |
1768 | { | |
1769 | tmp = value_neg (value_subscript (arg1, i)); | |
1770 | memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype), | |
1771 | value_contents_all (tmp), TYPE_LENGTH (eltype)); | |
1772 | } | |
1773 | return val; | |
1774 | } | |
c5aa993b JM |
1775 | else |
1776 | { | |
8a3fe4f8 | 1777 | error (_("Argument to negate operation not a number.")); |
c5aa993b | 1778 | return 0; /* For lint -- never reached */ |
c906108c | 1779 | } |
c906108c SS |
1780 | } |
1781 | ||
f23631e4 AC |
1782 | struct value * |
1783 | value_complement (struct value *arg1) | |
c906108c | 1784 | { |
52f0bd74 | 1785 | struct type *type; |
120bd360 | 1786 | struct value *val; |
4066e646 | 1787 | |
994b9211 | 1788 | arg1 = coerce_ref (arg1); |
df407dfe | 1789 | type = check_typedef (value_type (arg1)); |
c906108c | 1790 | |
120bd360 KW |
1791 | if (is_integral_type (type)) |
1792 | val = value_from_longest (type, ~value_as_long (arg1)); | |
1793 | else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)) | |
1794 | { | |
1795 | struct value *tmp; | |
1796 | struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type)); | |
1797 | int i, n = TYPE_LENGTH (type) / TYPE_LENGTH (eltype); | |
1798 | ||
1799 | val = allocate_value (type); | |
1800 | for (i = 0; i < n; i++) | |
1801 | { | |
1802 | tmp = value_complement (value_subscript (arg1, i)); | |
1803 | memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype), | |
1804 | value_contents_all (tmp), TYPE_LENGTH (eltype)); | |
1805 | } | |
1806 | } | |
1807 | else | |
1808 | error (_("Argument to complement operation not an integer, boolean.")); | |
c906108c | 1809 | |
120bd360 | 1810 | return val; |
c906108c SS |
1811 | } |
1812 | \f | |
df407dfe | 1813 | /* The INDEX'th bit of SET value whose value_type is TYPE, |
0fd88904 | 1814 | and whose value_contents is valaddr. |
c906108c SS |
1815 | Return -1 if out of range, -2 other error. */ |
1816 | ||
1817 | int | |
fc1a4b47 | 1818 | value_bit_index (struct type *type, const gdb_byte *valaddr, int index) |
c906108c | 1819 | { |
50810684 | 1820 | struct gdbarch *gdbarch = get_type_arch (type); |
c906108c SS |
1821 | LONGEST low_bound, high_bound; |
1822 | LONGEST word; | |
1823 | unsigned rel_index; | |
262452ec | 1824 | struct type *range = TYPE_INDEX_TYPE (type); |
a109c7c1 | 1825 | |
c906108c SS |
1826 | if (get_discrete_bounds (range, &low_bound, &high_bound) < 0) |
1827 | return -2; | |
1828 | if (index < low_bound || index > high_bound) | |
1829 | return -1; | |
1830 | rel_index = index - low_bound; | |
e17a4113 UW |
1831 | word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1, |
1832 | gdbarch_byte_order (gdbarch)); | |
c906108c | 1833 | rel_index %= TARGET_CHAR_BIT; |
50810684 | 1834 | if (gdbarch_bits_big_endian (gdbarch)) |
c906108c SS |
1835 | rel_index = TARGET_CHAR_BIT - 1 - rel_index; |
1836 | return (word >> rel_index) & 1; | |
1837 | } | |
1838 | ||
fbb06eb1 | 1839 | int |
f23631e4 | 1840 | value_in (struct value *element, struct value *set) |
c906108c SS |
1841 | { |
1842 | int member; | |
df407dfe AC |
1843 | struct type *settype = check_typedef (value_type (set)); |
1844 | struct type *eltype = check_typedef (value_type (element)); | |
a109c7c1 | 1845 | |
c906108c SS |
1846 | if (TYPE_CODE (eltype) == TYPE_CODE_RANGE) |
1847 | eltype = TYPE_TARGET_TYPE (eltype); | |
1848 | if (TYPE_CODE (settype) != TYPE_CODE_SET) | |
8a3fe4f8 | 1849 | error (_("Second argument of 'IN' has wrong type")); |
c906108c SS |
1850 | if (TYPE_CODE (eltype) != TYPE_CODE_INT |
1851 | && TYPE_CODE (eltype) != TYPE_CODE_CHAR | |
1852 | && TYPE_CODE (eltype) != TYPE_CODE_ENUM | |
1853 | && TYPE_CODE (eltype) != TYPE_CODE_BOOL) | |
8a3fe4f8 | 1854 | error (_("First argument of 'IN' has wrong type")); |
0fd88904 | 1855 | member = value_bit_index (settype, value_contents (set), |
c906108c SS |
1856 | value_as_long (element)); |
1857 | if (member < 0) | |
8a3fe4f8 | 1858 | error (_("First argument of 'IN' not in range")); |
fbb06eb1 | 1859 | return member; |
c906108c SS |
1860 | } |
1861 | ||
1862 | void | |
fba45db2 | 1863 | _initialize_valarith (void) |
c906108c SS |
1864 | { |
1865 | } |