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