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