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