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