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