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