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