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