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