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