]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Perform arithmetic and other operations on values, for GDB. |
1bac305b | 2 | |
197e01b6 | 3 | Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, |
d067a990 MK |
4 | 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 |
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 | |
11 | the Free Software Foundation; either version 2 of the License, or | |
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 JM |
19 | You should have received a copy of the GNU General Public License |
20 | along with this program; if not, write to the Free Software | |
197e01b6 EZ |
21 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
22 | Boston, MA 02110-1301, USA. */ | |
c906108c SS |
23 | |
24 | #include "defs.h" | |
25 | #include "value.h" | |
26 | #include "symtab.h" | |
27 | #include "gdbtypes.h" | |
28 | #include "expression.h" | |
29 | #include "target.h" | |
30 | #include "language.h" | |
c906108c | 31 | #include "gdb_string.h" |
d16aafd8 | 32 | #include "doublest.h" |
c4093a6a | 33 | #include <math.h> |
04714b91 | 34 | #include "infcall.h" |
c906108c SS |
35 | |
36 | /* Define whether or not the C operator '/' truncates towards zero for | |
37 | differently signed operands (truncation direction is undefined in C). */ | |
38 | ||
39 | #ifndef TRUNCATION_TOWARDS_ZERO | |
40 | #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2) | |
41 | #endif | |
42 | ||
f23631e4 | 43 | static struct value *value_subscripted_rvalue (struct value *, struct value *, int); |
c906108c | 44 | |
a14ed312 | 45 | void _initialize_valarith (void); |
c906108c | 46 | \f |
c5aa993b | 47 | |
ca439ad2 JI |
48 | /* Given a pointer, return the size of its target. |
49 | If the pointer type is void *, then return 1. | |
50 | If the target type is incomplete, then error out. | |
51 | This isn't a general purpose function, but just a | |
52 | helper for value_sub & value_add. | |
53 | */ | |
54 | ||
55 | static LONGEST | |
56 | find_size_for_pointer_math (struct type *ptr_type) | |
57 | { | |
58 | LONGEST sz = -1; | |
59 | struct type *ptr_target; | |
60 | ||
61 | ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type)); | |
62 | ||
63 | sz = TYPE_LENGTH (ptr_target); | |
64 | if (sz == 0) | |
65 | { | |
66 | if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID) | |
67 | sz = 1; | |
68 | else | |
69 | { | |
70 | char *name; | |
71 | ||
72 | name = TYPE_NAME (ptr_target); | |
73 | if (name == NULL) | |
74 | name = TYPE_TAG_NAME (ptr_target); | |
75 | if (name == NULL) | |
8a3fe4f8 AC |
76 | error (_("Cannot perform pointer math on incomplete types, " |
77 | "try casting to a known type, or void *.")); | |
ca439ad2 | 78 | else |
8a3fe4f8 AC |
79 | error (_("Cannot perform pointer math on incomplete type \"%s\", " |
80 | "try casting to a known type, or void *."), name); | |
ca439ad2 JI |
81 | } |
82 | } | |
83 | return sz; | |
84 | } | |
85 | ||
f23631e4 AC |
86 | struct value * |
87 | value_add (struct value *arg1, struct value *arg2) | |
c906108c | 88 | { |
f23631e4 AC |
89 | struct value *valint; |
90 | struct value *valptr; | |
ca439ad2 | 91 | LONGEST sz; |
c906108c SS |
92 | struct type *type1, *type2, *valptrtype; |
93 | ||
994b9211 AC |
94 | arg1 = coerce_array (arg1); |
95 | arg2 = coerce_array (arg2); | |
df407dfe AC |
96 | type1 = check_typedef (value_type (arg1)); |
97 | type2 = check_typedef (value_type (arg2)); | |
c906108c SS |
98 | |
99 | if ((TYPE_CODE (type1) == TYPE_CODE_PTR | |
100 | || TYPE_CODE (type2) == TYPE_CODE_PTR) | |
101 | && | |
2de41bce | 102 | (is_integral_type (type1) || is_integral_type (type2))) |
c906108c SS |
103 | /* Exactly one argument is a pointer, and one is an integer. */ |
104 | { | |
f23631e4 | 105 | struct value *retval; |
c906108c SS |
106 | |
107 | if (TYPE_CODE (type1) == TYPE_CODE_PTR) | |
108 | { | |
109 | valptr = arg1; | |
110 | valint = arg2; | |
111 | valptrtype = type1; | |
112 | } | |
113 | else | |
114 | { | |
115 | valptr = arg2; | |
116 | valint = arg1; | |
117 | valptrtype = type2; | |
118 | } | |
ca439ad2 JI |
119 | |
120 | sz = find_size_for_pointer_math (valptrtype); | |
121 | ||
4478b372 | 122 | retval = value_from_pointer (valptrtype, |
1aa20aa8 | 123 | value_as_address (valptr) |
ca439ad2 | 124 | + (sz * value_as_long (valint))); |
c906108c SS |
125 | return retval; |
126 | } | |
127 | ||
128 | return value_binop (arg1, arg2, BINOP_ADD); | |
129 | } | |
130 | ||
f23631e4 AC |
131 | struct value * |
132 | value_sub (struct value *arg1, struct value *arg2) | |
c906108c SS |
133 | { |
134 | struct type *type1, *type2; | |
994b9211 AC |
135 | arg1 = coerce_array (arg1); |
136 | arg2 = coerce_array (arg2); | |
df407dfe AC |
137 | type1 = check_typedef (value_type (arg1)); |
138 | type2 = check_typedef (value_type (arg2)); | |
c906108c SS |
139 | |
140 | if (TYPE_CODE (type1) == TYPE_CODE_PTR) | |
141 | { | |
2de41bce | 142 | if (is_integral_type (type2)) |
c906108c SS |
143 | { |
144 | /* pointer - integer. */ | |
ca439ad2 JI |
145 | LONGEST sz = find_size_for_pointer_math (type1); |
146 | ||
dbbd9c57 | 147 | return value_from_pointer (type1, |
1aa20aa8 | 148 | (value_as_address (arg1) |
4478b372 | 149 | - (sz * value_as_long (arg2)))); |
c906108c SS |
150 | } |
151 | else if (TYPE_CODE (type2) == TYPE_CODE_PTR | |
3dd3139b MS |
152 | && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1))) |
153 | == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2)))) | |
c906108c SS |
154 | { |
155 | /* pointer to <type x> - pointer to <type x>. */ | |
156 | LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1))); | |
157 | return value_from_longest | |
c5aa993b | 158 | (builtin_type_long, /* FIXME -- should be ptrdiff_t */ |
c906108c SS |
159 | (value_as_long (arg1) - value_as_long (arg2)) / sz); |
160 | } | |
161 | else | |
162 | { | |
8a3fe4f8 | 163 | error (_("\ |
c906108c | 164 | First argument of `-' is a pointer and second argument is neither\n\ |
8a3fe4f8 | 165 | an integer nor a pointer of the same type.")); |
c906108c SS |
166 | } |
167 | } | |
168 | ||
169 | return value_binop (arg1, arg2, BINOP_SUB); | |
170 | } | |
171 | ||
172 | /* Return the value of ARRAY[IDX]. | |
173 | See comments in value_coerce_array() for rationale for reason for | |
174 | doing lower bounds adjustment here rather than there. | |
175 | FIXME: Perhaps we should validate that the index is valid and if | |
176 | verbosity is set, warn about invalid indices (but still use them). */ | |
177 | ||
f23631e4 AC |
178 | struct value * |
179 | value_subscript (struct value *array, struct value *idx) | |
c906108c | 180 | { |
f23631e4 | 181 | struct value *bound; |
c906108c SS |
182 | int c_style = current_language->c_style_arrays; |
183 | struct type *tarray; | |
184 | ||
994b9211 | 185 | array = coerce_ref (array); |
df407dfe | 186 | tarray = check_typedef (value_type (array)); |
c906108c SS |
187 | |
188 | if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY | |
189 | || TYPE_CODE (tarray) == TYPE_CODE_STRING) | |
190 | { | |
191 | struct type *range_type = TYPE_INDEX_TYPE (tarray); | |
192 | LONGEST lowerbound, upperbound; | |
193 | get_discrete_bounds (range_type, &lowerbound, &upperbound); | |
194 | ||
195 | if (VALUE_LVAL (array) != lval_memory) | |
196 | return value_subscripted_rvalue (array, idx, lowerbound); | |
197 | ||
198 | if (c_style == 0) | |
199 | { | |
200 | LONGEST index = value_as_long (idx); | |
201 | if (index >= lowerbound && index <= upperbound) | |
202 | return value_subscripted_rvalue (array, idx, lowerbound); | |
987504bb JJ |
203 | /* Emit warning unless we have an array of unknown size. |
204 | An array of unknown size has lowerbound 0 and upperbound -1. */ | |
205 | if (upperbound > -1) | |
8a3fe4f8 | 206 | warning (_("array or string index out of range")); |
c906108c SS |
207 | /* fall doing C stuff */ |
208 | c_style = 1; | |
209 | } | |
210 | ||
211 | if (lowerbound != 0) | |
212 | { | |
213 | bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound); | |
214 | idx = value_sub (idx, bound); | |
215 | } | |
216 | ||
217 | array = value_coerce_array (array); | |
218 | } | |
219 | ||
220 | if (TYPE_CODE (tarray) == TYPE_CODE_BITSTRING) | |
221 | { | |
222 | struct type *range_type = TYPE_INDEX_TYPE (tarray); | |
223 | LONGEST index = value_as_long (idx); | |
f23631e4 | 224 | struct value *v; |
c906108c SS |
225 | int offset, byte, bit_index; |
226 | LONGEST lowerbound, upperbound; | |
227 | get_discrete_bounds (range_type, &lowerbound, &upperbound); | |
228 | if (index < lowerbound || index > upperbound) | |
8a3fe4f8 | 229 | error (_("bitstring index out of range")); |
c906108c SS |
230 | index -= lowerbound; |
231 | offset = index / TARGET_CHAR_BIT; | |
0fd88904 | 232 | byte = *((char *) value_contents (array) + offset); |
c906108c SS |
233 | bit_index = index % TARGET_CHAR_BIT; |
234 | byte >>= (BITS_BIG_ENDIAN ? TARGET_CHAR_BIT - 1 - bit_index : bit_index); | |
235 | v = value_from_longest (LA_BOOL_TYPE, byte & 1); | |
9bbda503 AC |
236 | set_value_bitpos (v, bit_index); |
237 | set_value_bitsize (v, 1); | |
c906108c SS |
238 | VALUE_LVAL (v) = VALUE_LVAL (array); |
239 | if (VALUE_LVAL (array) == lval_internalvar) | |
240 | VALUE_LVAL (v) = lval_internalvar_component; | |
241 | VALUE_ADDRESS (v) = VALUE_ADDRESS (array); | |
65d3800a | 242 | VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array); |
f5cf64a7 | 243 | set_value_offset (v, offset + value_offset (array)); |
c906108c SS |
244 | return v; |
245 | } | |
246 | ||
247 | if (c_style) | |
248 | return value_ind (value_add (array, idx)); | |
249 | else | |
8a3fe4f8 | 250 | error (_("not an array or string")); |
c906108c SS |
251 | } |
252 | ||
253 | /* Return the value of EXPR[IDX], expr an aggregate rvalue | |
254 | (eg, a vector register). This routine used to promote floats | |
255 | to doubles, but no longer does. */ | |
256 | ||
f23631e4 AC |
257 | static struct value * |
258 | value_subscripted_rvalue (struct value *array, struct value *idx, int lowerbound) | |
c906108c | 259 | { |
df407dfe | 260 | struct type *array_type = check_typedef (value_type (array)); |
c906108c SS |
261 | struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type)); |
262 | unsigned int elt_size = TYPE_LENGTH (elt_type); | |
263 | LONGEST index = value_as_long (idx); | |
264 | unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound); | |
f23631e4 | 265 | struct value *v; |
c906108c SS |
266 | |
267 | if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type)) | |
8a3fe4f8 | 268 | error (_("no such vector element")); |
c906108c SS |
269 | |
270 | v = allocate_value (elt_type); | |
d69fe07e | 271 | if (value_lazy (array)) |
dfa52d88 | 272 | set_value_lazy (v, 1); |
c906108c | 273 | else |
0fd88904 AC |
274 | memcpy (value_contents_writeable (v), |
275 | value_contents (array) + elt_offs, elt_size); | |
c906108c SS |
276 | |
277 | if (VALUE_LVAL (array) == lval_internalvar) | |
278 | VALUE_LVAL (v) = lval_internalvar_component; | |
279 | else | |
280 | VALUE_LVAL (v) = VALUE_LVAL (array); | |
281 | VALUE_ADDRESS (v) = VALUE_ADDRESS (array); | |
9ee8fc9d | 282 | VALUE_REGNUM (v) = VALUE_REGNUM (array); |
65d3800a | 283 | VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array); |
f5cf64a7 | 284 | set_value_offset (v, value_offset (array) + elt_offs); |
c906108c SS |
285 | return v; |
286 | } | |
287 | \f | |
288 | /* Check to see if either argument is a structure. This is called so | |
289 | we know whether to go ahead with the normal binop or look for a | |
290 | user defined function instead. | |
291 | ||
292 | For now, we do not overload the `=' operator. */ | |
293 | ||
294 | int | |
f23631e4 | 295 | binop_user_defined_p (enum exp_opcode op, struct value *arg1, struct value *arg2) |
c906108c SS |
296 | { |
297 | struct type *type1, *type2; | |
298 | if (op == BINOP_ASSIGN || op == BINOP_CONCAT) | |
299 | return 0; | |
df407dfe AC |
300 | type1 = check_typedef (value_type (arg1)); |
301 | type2 = check_typedef (value_type (arg2)); | |
c906108c SS |
302 | return (TYPE_CODE (type1) == TYPE_CODE_STRUCT |
303 | || TYPE_CODE (type2) == TYPE_CODE_STRUCT | |
304 | || (TYPE_CODE (type1) == TYPE_CODE_REF | |
305 | && TYPE_CODE (TYPE_TARGET_TYPE (type1)) == TYPE_CODE_STRUCT) | |
306 | || (TYPE_CODE (type2) == TYPE_CODE_REF | |
307 | && TYPE_CODE (TYPE_TARGET_TYPE (type2)) == TYPE_CODE_STRUCT)); | |
308 | } | |
309 | ||
310 | /* Check to see if argument is a structure. This is called so | |
311 | we know whether to go ahead with the normal unop or look for a | |
312 | user defined function instead. | |
313 | ||
314 | For now, we do not overload the `&' operator. */ | |
315 | ||
c5aa993b | 316 | int |
f23631e4 | 317 | unop_user_defined_p (enum exp_opcode op, struct value *arg1) |
c906108c SS |
318 | { |
319 | struct type *type1; | |
320 | if (op == UNOP_ADDR) | |
321 | return 0; | |
df407dfe | 322 | type1 = check_typedef (value_type (arg1)); |
c906108c SS |
323 | for (;;) |
324 | { | |
325 | if (TYPE_CODE (type1) == TYPE_CODE_STRUCT) | |
326 | return 1; | |
327 | else if (TYPE_CODE (type1) == TYPE_CODE_REF) | |
328 | type1 = TYPE_TARGET_TYPE (type1); | |
329 | else | |
330 | return 0; | |
331 | } | |
332 | } | |
333 | ||
334 | /* We know either arg1 or arg2 is a structure, so try to find the right | |
335 | user defined function. Create an argument vector that calls | |
336 | arg1.operator @ (arg1,arg2) and return that value (where '@' is any | |
337 | binary operator which is legal for GNU C++). | |
338 | ||
339 | OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP | |
340 | is the opcode saying how to modify it. Otherwise, OTHEROP is | |
341 | unused. */ | |
342 | ||
f23631e4 AC |
343 | struct value * |
344 | value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op, | |
fba45db2 | 345 | enum exp_opcode otherop, enum noside noside) |
c906108c | 346 | { |
f23631e4 | 347 | struct value **argvec; |
c906108c SS |
348 | char *ptr; |
349 | char tstr[13]; | |
350 | int static_memfuncp; | |
351 | ||
994b9211 AC |
352 | arg1 = coerce_ref (arg1); |
353 | arg2 = coerce_ref (arg2); | |
354 | arg1 = coerce_enum (arg1); | |
355 | arg2 = coerce_enum (arg2); | |
c906108c SS |
356 | |
357 | /* now we know that what we have to do is construct our | |
358 | arg vector and find the right function to call it with. */ | |
359 | ||
df407dfe | 360 | if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT) |
8a3fe4f8 | 361 | error (_("Can't do that binary op on that type")); /* FIXME be explicit */ |
c906108c | 362 | |
f23631e4 | 363 | argvec = (struct value **) alloca (sizeof (struct value *) * 4); |
c906108c SS |
364 | argvec[1] = value_addr (arg1); |
365 | argvec[2] = arg2; | |
366 | argvec[3] = 0; | |
367 | ||
c5aa993b JM |
368 | /* make the right function name up */ |
369 | strcpy (tstr, "operator__"); | |
370 | ptr = tstr + 8; | |
c906108c SS |
371 | switch (op) |
372 | { | |
c5aa993b JM |
373 | case BINOP_ADD: |
374 | strcpy (ptr, "+"); | |
375 | break; | |
376 | case BINOP_SUB: | |
377 | strcpy (ptr, "-"); | |
378 | break; | |
379 | case BINOP_MUL: | |
380 | strcpy (ptr, "*"); | |
381 | break; | |
382 | case BINOP_DIV: | |
383 | strcpy (ptr, "/"); | |
384 | break; | |
385 | case BINOP_REM: | |
386 | strcpy (ptr, "%"); | |
387 | break; | |
388 | case BINOP_LSH: | |
389 | strcpy (ptr, "<<"); | |
390 | break; | |
391 | case BINOP_RSH: | |
392 | strcpy (ptr, ">>"); | |
393 | break; | |
394 | case BINOP_BITWISE_AND: | |
395 | strcpy (ptr, "&"); | |
396 | break; | |
397 | case BINOP_BITWISE_IOR: | |
398 | strcpy (ptr, "|"); | |
399 | break; | |
400 | case BINOP_BITWISE_XOR: | |
401 | strcpy (ptr, "^"); | |
402 | break; | |
403 | case BINOP_LOGICAL_AND: | |
404 | strcpy (ptr, "&&"); | |
405 | break; | |
406 | case BINOP_LOGICAL_OR: | |
407 | strcpy (ptr, "||"); | |
408 | break; | |
409 | case BINOP_MIN: | |
410 | strcpy (ptr, "<?"); | |
411 | break; | |
412 | case BINOP_MAX: | |
413 | strcpy (ptr, ">?"); | |
414 | break; | |
415 | case BINOP_ASSIGN: | |
416 | strcpy (ptr, "="); | |
417 | break; | |
418 | case BINOP_ASSIGN_MODIFY: | |
c906108c SS |
419 | switch (otherop) |
420 | { | |
c5aa993b JM |
421 | case BINOP_ADD: |
422 | strcpy (ptr, "+="); | |
423 | break; | |
424 | case BINOP_SUB: | |
425 | strcpy (ptr, "-="); | |
426 | break; | |
427 | case BINOP_MUL: | |
428 | strcpy (ptr, "*="); | |
429 | break; | |
430 | case BINOP_DIV: | |
431 | strcpy (ptr, "/="); | |
432 | break; | |
433 | case BINOP_REM: | |
434 | strcpy (ptr, "%="); | |
435 | break; | |
436 | case BINOP_BITWISE_AND: | |
437 | strcpy (ptr, "&="); | |
438 | break; | |
439 | case BINOP_BITWISE_IOR: | |
440 | strcpy (ptr, "|="); | |
441 | break; | |
442 | case BINOP_BITWISE_XOR: | |
443 | strcpy (ptr, "^="); | |
444 | break; | |
445 | case BINOP_MOD: /* invalid */ | |
c906108c | 446 | default: |
8a3fe4f8 | 447 | error (_("Invalid binary operation specified.")); |
c906108c SS |
448 | } |
449 | break; | |
c5aa993b JM |
450 | case BINOP_SUBSCRIPT: |
451 | strcpy (ptr, "[]"); | |
452 | break; | |
453 | case BINOP_EQUAL: | |
454 | strcpy (ptr, "=="); | |
455 | break; | |
456 | case BINOP_NOTEQUAL: | |
457 | strcpy (ptr, "!="); | |
458 | break; | |
459 | case BINOP_LESS: | |
460 | strcpy (ptr, "<"); | |
461 | break; | |
462 | case BINOP_GTR: | |
463 | strcpy (ptr, ">"); | |
464 | break; | |
465 | case BINOP_GEQ: | |
466 | strcpy (ptr, ">="); | |
467 | break; | |
468 | case BINOP_LEQ: | |
469 | strcpy (ptr, "<="); | |
470 | break; | |
471 | case BINOP_MOD: /* invalid */ | |
c906108c | 472 | default: |
8a3fe4f8 | 473 | error (_("Invalid binary operation specified.")); |
c906108c SS |
474 | } |
475 | ||
c5aa993b JM |
476 | argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure"); |
477 | ||
c906108c SS |
478 | if (argvec[0]) |
479 | { | |
480 | if (static_memfuncp) | |
481 | { | |
482 | argvec[1] = argvec[0]; | |
483 | argvec++; | |
484 | } | |
485 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
486 | { | |
487 | struct type *return_type; | |
488 | return_type | |
df407dfe | 489 | = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0]))); |
c906108c SS |
490 | return value_zero (return_type, VALUE_LVAL (arg1)); |
491 | } | |
492 | return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); | |
493 | } | |
8a3fe4f8 | 494 | error (_("member function %s not found"), tstr); |
c906108c SS |
495 | #ifdef lint |
496 | return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); | |
497 | #endif | |
498 | } | |
499 | ||
500 | /* We know that arg1 is a structure, so try to find a unary user | |
501 | defined operator that matches the operator in question. | |
502 | Create an argument vector that calls arg1.operator @ (arg1) | |
503 | and return that value (where '@' is (almost) any unary operator which | |
504 | is legal for GNU C++). */ | |
505 | ||
f23631e4 AC |
506 | struct value * |
507 | value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside) | |
c906108c | 508 | { |
f23631e4 | 509 | struct value **argvec; |
c906108c SS |
510 | char *ptr, *mangle_ptr; |
511 | char tstr[13], mangle_tstr[13]; | |
491b8946 | 512 | int static_memfuncp, nargs; |
c906108c | 513 | |
994b9211 AC |
514 | arg1 = coerce_ref (arg1); |
515 | arg1 = coerce_enum (arg1); | |
c906108c SS |
516 | |
517 | /* now we know that what we have to do is construct our | |
518 | arg vector and find the right function to call it with. */ | |
519 | ||
df407dfe | 520 | if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT) |
8a3fe4f8 | 521 | error (_("Can't do that unary op on that type")); /* FIXME be explicit */ |
c906108c | 522 | |
491b8946 | 523 | argvec = (struct value **) alloca (sizeof (struct value *) * 4); |
c906108c SS |
524 | argvec[1] = value_addr (arg1); |
525 | argvec[2] = 0; | |
526 | ||
491b8946 DJ |
527 | nargs = 1; |
528 | ||
c5aa993b JM |
529 | /* make the right function name up */ |
530 | strcpy (tstr, "operator__"); | |
531 | ptr = tstr + 8; | |
532 | strcpy (mangle_tstr, "__"); | |
533 | mangle_ptr = mangle_tstr + 2; | |
c906108c SS |
534 | switch (op) |
535 | { | |
c5aa993b JM |
536 | case UNOP_PREINCREMENT: |
537 | strcpy (ptr, "++"); | |
538 | break; | |
539 | case UNOP_PREDECREMENT: | |
491b8946 | 540 | strcpy (ptr, "--"); |
c5aa993b JM |
541 | break; |
542 | case UNOP_POSTINCREMENT: | |
543 | strcpy (ptr, "++"); | |
491b8946 DJ |
544 | argvec[2] = value_from_longest (builtin_type_int, 0); |
545 | argvec[3] = 0; | |
546 | nargs ++; | |
c5aa993b JM |
547 | break; |
548 | case UNOP_POSTDECREMENT: | |
491b8946 DJ |
549 | strcpy (ptr, "--"); |
550 | argvec[2] = value_from_longest (builtin_type_int, 0); | |
551 | argvec[3] = 0; | |
552 | nargs ++; | |
c5aa993b JM |
553 | break; |
554 | case UNOP_LOGICAL_NOT: | |
555 | strcpy (ptr, "!"); | |
556 | break; | |
557 | case UNOP_COMPLEMENT: | |
558 | strcpy (ptr, "~"); | |
559 | break; | |
560 | case UNOP_NEG: | |
561 | strcpy (ptr, "-"); | |
562 | break; | |
36e9969c NS |
563 | case UNOP_PLUS: |
564 | strcpy (ptr, "+"); | |
565 | break; | |
c5aa993b JM |
566 | case UNOP_IND: |
567 | strcpy (ptr, "*"); | |
568 | break; | |
c906108c | 569 | default: |
8a3fe4f8 | 570 | error (_("Invalid unary operation specified.")); |
c906108c SS |
571 | } |
572 | ||
c5aa993b | 573 | argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure"); |
c906108c SS |
574 | |
575 | if (argvec[0]) | |
576 | { | |
577 | if (static_memfuncp) | |
578 | { | |
579 | argvec[1] = argvec[0]; | |
491b8946 | 580 | nargs --; |
c906108c SS |
581 | argvec++; |
582 | } | |
583 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
584 | { | |
585 | struct type *return_type; | |
586 | return_type | |
df407dfe | 587 | = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0]))); |
c906108c SS |
588 | return value_zero (return_type, VALUE_LVAL (arg1)); |
589 | } | |
491b8946 | 590 | return call_function_by_hand (argvec[0], nargs, argvec + 1); |
c906108c | 591 | } |
8a3fe4f8 | 592 | error (_("member function %s not found"), tstr); |
c5aa993b | 593 | return 0; /* For lint -- never reached */ |
c906108c | 594 | } |
c906108c | 595 | \f |
c5aa993b | 596 | |
c906108c SS |
597 | /* Concatenate two values with the following conditions: |
598 | ||
c5aa993b JM |
599 | (1) Both values must be either bitstring values or character string |
600 | values and the resulting value consists of the concatenation of | |
601 | ARG1 followed by ARG2. | |
c906108c | 602 | |
c5aa993b | 603 | or |
c906108c | 604 | |
c5aa993b JM |
605 | One value must be an integer value and the other value must be |
606 | either a bitstring value or character string value, which is | |
607 | to be repeated by the number of times specified by the integer | |
608 | value. | |
c906108c SS |
609 | |
610 | ||
c5aa993b JM |
611 | (2) Boolean values are also allowed and are treated as bit string |
612 | values of length 1. | |
c906108c | 613 | |
c5aa993b JM |
614 | (3) Character values are also allowed and are treated as character |
615 | string values of length 1. | |
616 | */ | |
c906108c | 617 | |
f23631e4 AC |
618 | struct value * |
619 | value_concat (struct value *arg1, struct value *arg2) | |
c906108c | 620 | { |
f23631e4 AC |
621 | struct value *inval1; |
622 | struct value *inval2; | |
623 | struct value *outval = NULL; | |
c906108c SS |
624 | int inval1len, inval2len; |
625 | int count, idx; | |
626 | char *ptr; | |
627 | char inchar; | |
df407dfe AC |
628 | struct type *type1 = check_typedef (value_type (arg1)); |
629 | struct type *type2 = check_typedef (value_type (arg2)); | |
c906108c | 630 | |
c906108c SS |
631 | /* First figure out if we are dealing with two values to be concatenated |
632 | or a repeat count and a value to be repeated. INVAL1 is set to the | |
633 | first of two concatenated values, or the repeat count. INVAL2 is set | |
634 | to the second of the two concatenated values or the value to be | |
635 | repeated. */ | |
636 | ||
637 | if (TYPE_CODE (type2) == TYPE_CODE_INT) | |
638 | { | |
639 | struct type *tmp = type1; | |
640 | type1 = tmp; | |
641 | tmp = type2; | |
642 | inval1 = arg2; | |
643 | inval2 = arg1; | |
644 | } | |
645 | else | |
646 | { | |
647 | inval1 = arg1; | |
648 | inval2 = arg2; | |
649 | } | |
650 | ||
651 | /* Now process the input values. */ | |
652 | ||
653 | if (TYPE_CODE (type1) == TYPE_CODE_INT) | |
654 | { | |
655 | /* We have a repeat count. Validate the second value and then | |
c5aa993b | 656 | construct a value repeated that many times. */ |
c906108c SS |
657 | if (TYPE_CODE (type2) == TYPE_CODE_STRING |
658 | || TYPE_CODE (type2) == TYPE_CODE_CHAR) | |
659 | { | |
660 | count = longest_to_int (value_as_long (inval1)); | |
661 | inval2len = TYPE_LENGTH (type2); | |
662 | ptr = (char *) alloca (count * inval2len); | |
663 | if (TYPE_CODE (type2) == TYPE_CODE_CHAR) | |
664 | { | |
665 | inchar = (char) unpack_long (type2, | |
0fd88904 | 666 | value_contents (inval2)); |
c906108c SS |
667 | for (idx = 0; idx < count; idx++) |
668 | { | |
669 | *(ptr + idx) = inchar; | |
670 | } | |
671 | } | |
672 | else | |
673 | { | |
674 | for (idx = 0; idx < count; idx++) | |
675 | { | |
0fd88904 | 676 | memcpy (ptr + (idx * inval2len), value_contents (inval2), |
c906108c SS |
677 | inval2len); |
678 | } | |
679 | } | |
680 | outval = value_string (ptr, count * inval2len); | |
681 | } | |
682 | else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING | |
683 | || TYPE_CODE (type2) == TYPE_CODE_BOOL) | |
684 | { | |
8a3fe4f8 | 685 | error (_("unimplemented support for bitstring/boolean repeats")); |
c906108c SS |
686 | } |
687 | else | |
688 | { | |
8a3fe4f8 | 689 | error (_("can't repeat values of that type")); |
c906108c SS |
690 | } |
691 | } | |
692 | else if (TYPE_CODE (type1) == TYPE_CODE_STRING | |
c5aa993b | 693 | || TYPE_CODE (type1) == TYPE_CODE_CHAR) |
c906108c SS |
694 | { |
695 | /* We have two character strings to concatenate. */ | |
696 | if (TYPE_CODE (type2) != TYPE_CODE_STRING | |
697 | && TYPE_CODE (type2) != TYPE_CODE_CHAR) | |
698 | { | |
8a3fe4f8 | 699 | error (_("Strings can only be concatenated with other strings.")); |
c906108c SS |
700 | } |
701 | inval1len = TYPE_LENGTH (type1); | |
702 | inval2len = TYPE_LENGTH (type2); | |
703 | ptr = (char *) alloca (inval1len + inval2len); | |
704 | if (TYPE_CODE (type1) == TYPE_CODE_CHAR) | |
705 | { | |
0fd88904 | 706 | *ptr = (char) unpack_long (type1, value_contents (inval1)); |
c906108c SS |
707 | } |
708 | else | |
709 | { | |
0fd88904 | 710 | memcpy (ptr, value_contents (inval1), inval1len); |
c906108c SS |
711 | } |
712 | if (TYPE_CODE (type2) == TYPE_CODE_CHAR) | |
713 | { | |
c5aa993b | 714 | *(ptr + inval1len) = |
0fd88904 | 715 | (char) unpack_long (type2, value_contents (inval2)); |
c906108c SS |
716 | } |
717 | else | |
718 | { | |
0fd88904 | 719 | memcpy (ptr + inval1len, value_contents (inval2), inval2len); |
c906108c SS |
720 | } |
721 | outval = value_string (ptr, inval1len + inval2len); | |
722 | } | |
723 | else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING | |
724 | || TYPE_CODE (type1) == TYPE_CODE_BOOL) | |
725 | { | |
726 | /* We have two bitstrings to concatenate. */ | |
727 | if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING | |
728 | && TYPE_CODE (type2) != TYPE_CODE_BOOL) | |
729 | { | |
8a3fe4f8 | 730 | error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.")); |
c906108c | 731 | } |
8a3fe4f8 | 732 | error (_("unimplemented support for bitstring/boolean concatenation.")); |
c5aa993b | 733 | } |
c906108c SS |
734 | else |
735 | { | |
736 | /* We don't know how to concatenate these operands. */ | |
8a3fe4f8 | 737 | error (_("illegal operands for concatenation.")); |
c906108c SS |
738 | } |
739 | return (outval); | |
740 | } | |
c906108c SS |
741 | \f |
742 | ||
c5aa993b | 743 | |
c906108c SS |
744 | /* Perform a binary operation on two operands which have reasonable |
745 | representations as integers or floats. This includes booleans, | |
746 | characters, integers, or floats. | |
747 | Does not support addition and subtraction on pointers; | |
748 | use value_add or value_sub if you want to handle those possibilities. */ | |
749 | ||
f23631e4 AC |
750 | struct value * |
751 | value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) | |
c906108c | 752 | { |
f23631e4 | 753 | struct value *val; |
c906108c SS |
754 | struct type *type1, *type2; |
755 | ||
994b9211 AC |
756 | arg1 = coerce_ref (arg1); |
757 | arg2 = coerce_ref (arg2); | |
df407dfe AC |
758 | type1 = check_typedef (value_type (arg1)); |
759 | type2 = check_typedef (value_type (arg2)); | |
c906108c | 760 | |
2de41bce | 761 | if ((TYPE_CODE (type1) != TYPE_CODE_FLT && !is_integral_type (type1)) |
c906108c | 762 | || |
2de41bce | 763 | (TYPE_CODE (type2) != TYPE_CODE_FLT && !is_integral_type (type2))) |
8a3fe4f8 | 764 | error (_("Argument to arithmetic operation not a number or boolean.")); |
c906108c SS |
765 | |
766 | if (TYPE_CODE (type1) == TYPE_CODE_FLT | |
767 | || | |
768 | TYPE_CODE (type2) == TYPE_CODE_FLT) | |
769 | { | |
770 | /* FIXME-if-picky-about-floating-accuracy: Should be doing this | |
c5aa993b JM |
771 | in target format. real.c in GCC probably has the necessary |
772 | code. */ | |
c4093a6a | 773 | DOUBLEST v1, v2, v = 0; |
c906108c SS |
774 | v1 = value_as_double (arg1); |
775 | v2 = value_as_double (arg2); | |
776 | switch (op) | |
777 | { | |
778 | case BINOP_ADD: | |
779 | v = v1 + v2; | |
780 | break; | |
781 | ||
782 | case BINOP_SUB: | |
783 | v = v1 - v2; | |
784 | break; | |
785 | ||
786 | case BINOP_MUL: | |
787 | v = v1 * v2; | |
788 | break; | |
789 | ||
790 | case BINOP_DIV: | |
791 | v = v1 / v2; | |
792 | break; | |
793 | ||
bd49c137 WZ |
794 | case BINOP_EXP: |
795 | errno = 0; | |
796 | v = pow (v1, v2); | |
797 | if (errno) | |
798 | error (_("Cannot perform exponentiation: %s"), safe_strerror (errno)); | |
799 | break; | |
c4093a6a | 800 | |
c906108c | 801 | default: |
8a3fe4f8 | 802 | error (_("Integer-only operation on floating point number.")); |
c906108c SS |
803 | } |
804 | ||
805 | /* If either arg was long double, make sure that value is also long | |
c5aa993b | 806 | double. */ |
c906108c | 807 | |
c5aa993b JM |
808 | if (TYPE_LENGTH (type1) * 8 > TARGET_DOUBLE_BIT |
809 | || TYPE_LENGTH (type2) * 8 > TARGET_DOUBLE_BIT) | |
c906108c SS |
810 | val = allocate_value (builtin_type_long_double); |
811 | else | |
812 | val = allocate_value (builtin_type_double); | |
813 | ||
990a07ab | 814 | store_typed_floating (value_contents_raw (val), value_type (val), v); |
c906108c SS |
815 | } |
816 | else if (TYPE_CODE (type1) == TYPE_CODE_BOOL | |
817 | && | |
818 | TYPE_CODE (type2) == TYPE_CODE_BOOL) | |
c5aa993b | 819 | { |
c4093a6a | 820 | LONGEST v1, v2, v = 0; |
c5aa993b JM |
821 | v1 = value_as_long (arg1); |
822 | v2 = value_as_long (arg2); | |
823 | ||
824 | switch (op) | |
825 | { | |
826 | case BINOP_BITWISE_AND: | |
827 | v = v1 & v2; | |
828 | break; | |
829 | ||
830 | case BINOP_BITWISE_IOR: | |
831 | v = v1 | v2; | |
832 | break; | |
833 | ||
834 | case BINOP_BITWISE_XOR: | |
835 | v = v1 ^ v2; | |
c4093a6a JM |
836 | break; |
837 | ||
838 | case BINOP_EQUAL: | |
839 | v = v1 == v2; | |
840 | break; | |
841 | ||
842 | case BINOP_NOTEQUAL: | |
843 | v = v1 != v2; | |
c5aa993b JM |
844 | break; |
845 | ||
846 | default: | |
8a3fe4f8 | 847 | error (_("Invalid operation on booleans.")); |
c5aa993b JM |
848 | } |
849 | ||
850 | val = allocate_value (type1); | |
990a07ab | 851 | store_signed_integer (value_contents_raw (val), |
c5aa993b JM |
852 | TYPE_LENGTH (type1), |
853 | v); | |
854 | } | |
c906108c SS |
855 | else |
856 | /* Integral operations here. */ | |
857 | /* FIXME: Also mixed integral/booleans, with result an integer. */ | |
858 | /* FIXME: This implements ANSI C rules (also correct for C++). | |
1b831c93 | 859 | What about FORTRAN and (the deleted) chill ? */ |
c906108c SS |
860 | { |
861 | unsigned int promoted_len1 = TYPE_LENGTH (type1); | |
862 | unsigned int promoted_len2 = TYPE_LENGTH (type2); | |
863 | int is_unsigned1 = TYPE_UNSIGNED (type1); | |
864 | int is_unsigned2 = TYPE_UNSIGNED (type2); | |
865 | unsigned int result_len; | |
866 | int unsigned_operation; | |
867 | ||
868 | /* Determine type length and signedness after promotion for | |
c5aa993b | 869 | both operands. */ |
c906108c SS |
870 | if (promoted_len1 < TYPE_LENGTH (builtin_type_int)) |
871 | { | |
872 | is_unsigned1 = 0; | |
873 | promoted_len1 = TYPE_LENGTH (builtin_type_int); | |
874 | } | |
875 | if (promoted_len2 < TYPE_LENGTH (builtin_type_int)) | |
876 | { | |
877 | is_unsigned2 = 0; | |
878 | promoted_len2 = TYPE_LENGTH (builtin_type_int); | |
879 | } | |
880 | ||
881 | /* Determine type length of the result, and if the operation should | |
c5aa993b JM |
882 | be done unsigned. |
883 | Use the signedness of the operand with the greater length. | |
884 | If both operands are of equal length, use unsigned operation | |
885 | if one of the operands is unsigned. */ | |
0d059fca AS |
886 | if (op == BINOP_RSH || op == BINOP_LSH) |
887 | { | |
888 | /* In case of the shift operators the type of the result only | |
889 | depends on the type of the left operand. */ | |
890 | unsigned_operation = is_unsigned1; | |
891 | result_len = promoted_len1; | |
892 | } | |
893 | else if (promoted_len1 > promoted_len2) | |
c906108c SS |
894 | { |
895 | unsigned_operation = is_unsigned1; | |
896 | result_len = promoted_len1; | |
897 | } | |
898 | else if (promoted_len2 > promoted_len1) | |
899 | { | |
900 | unsigned_operation = is_unsigned2; | |
901 | result_len = promoted_len2; | |
902 | } | |
903 | else | |
904 | { | |
905 | unsigned_operation = is_unsigned1 || is_unsigned2; | |
906 | result_len = promoted_len1; | |
907 | } | |
908 | ||
909 | if (unsigned_operation) | |
910 | { | |
c4093a6a | 911 | ULONGEST v1, v2, v = 0; |
c906108c SS |
912 | v1 = (ULONGEST) value_as_long (arg1); |
913 | v2 = (ULONGEST) value_as_long (arg2); | |
914 | ||
915 | /* Truncate values to the type length of the result. */ | |
916 | if (result_len < sizeof (ULONGEST)) | |
917 | { | |
918 | v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1; | |
919 | v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1; | |
920 | } | |
c5aa993b | 921 | |
c906108c SS |
922 | switch (op) |
923 | { | |
924 | case BINOP_ADD: | |
925 | v = v1 + v2; | |
926 | break; | |
c5aa993b | 927 | |
c906108c SS |
928 | case BINOP_SUB: |
929 | v = v1 - v2; | |
930 | break; | |
c5aa993b | 931 | |
c906108c SS |
932 | case BINOP_MUL: |
933 | v = v1 * v2; | |
934 | break; | |
c5aa993b | 935 | |
c906108c SS |
936 | case BINOP_DIV: |
937 | v = v1 / v2; | |
938 | break; | |
c5aa993b | 939 | |
bd49c137 WZ |
940 | case BINOP_EXP: |
941 | errno = 0; | |
942 | v = pow (v1, v2); | |
943 | if (errno) | |
944 | error (_("Cannot perform exponentiation: %s"), safe_strerror (errno)); | |
945 | break; | |
c4093a6a | 946 | |
c906108c SS |
947 | case BINOP_REM: |
948 | v = v1 % v2; | |
949 | break; | |
c5aa993b | 950 | |
c906108c SS |
951 | case BINOP_MOD: |
952 | /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, | |
953 | v1 mod 0 has a defined value, v1. */ | |
c906108c SS |
954 | if (v2 == 0) |
955 | { | |
956 | v = v1; | |
957 | } | |
958 | else | |
959 | { | |
c5aa993b | 960 | v = v1 / v2; |
c906108c SS |
961 | /* Note floor(v1/v2) == v1/v2 for unsigned. */ |
962 | v = v1 - (v2 * v); | |
963 | } | |
964 | break; | |
c5aa993b | 965 | |
c906108c SS |
966 | case BINOP_LSH: |
967 | v = v1 << v2; | |
968 | break; | |
c5aa993b | 969 | |
c906108c SS |
970 | case BINOP_RSH: |
971 | v = v1 >> v2; | |
972 | break; | |
c5aa993b | 973 | |
c906108c SS |
974 | case BINOP_BITWISE_AND: |
975 | v = v1 & v2; | |
976 | break; | |
c5aa993b | 977 | |
c906108c SS |
978 | case BINOP_BITWISE_IOR: |
979 | v = v1 | v2; | |
980 | break; | |
c5aa993b | 981 | |
c906108c SS |
982 | case BINOP_BITWISE_XOR: |
983 | v = v1 ^ v2; | |
984 | break; | |
c5aa993b | 985 | |
c906108c SS |
986 | case BINOP_LOGICAL_AND: |
987 | v = v1 && v2; | |
988 | break; | |
c5aa993b | 989 | |
c906108c SS |
990 | case BINOP_LOGICAL_OR: |
991 | v = v1 || v2; | |
992 | break; | |
c5aa993b | 993 | |
c906108c SS |
994 | case BINOP_MIN: |
995 | v = v1 < v2 ? v1 : v2; | |
996 | break; | |
c5aa993b | 997 | |
c906108c SS |
998 | case BINOP_MAX: |
999 | v = v1 > v2 ? v1 : v2; | |
1000 | break; | |
1001 | ||
1002 | case BINOP_EQUAL: | |
1003 | v = v1 == v2; | |
1004 | break; | |
1005 | ||
c4093a6a JM |
1006 | case BINOP_NOTEQUAL: |
1007 | v = v1 != v2; | |
1008 | break; | |
1009 | ||
c906108c SS |
1010 | case BINOP_LESS: |
1011 | v = v1 < v2; | |
1012 | break; | |
c5aa993b | 1013 | |
c906108c | 1014 | default: |
8a3fe4f8 | 1015 | error (_("Invalid binary operation on numbers.")); |
c906108c SS |
1016 | } |
1017 | ||
1018 | /* This is a kludge to get around the fact that we don't | |
1019 | know how to determine the result type from the types of | |
1020 | the operands. (I'm not really sure how much we feel the | |
1021 | need to duplicate the exact rules of the current | |
1022 | language. They can get really hairy. But not to do so | |
1023 | makes it hard to document just what we *do* do). */ | |
1024 | ||
1025 | /* Can't just call init_type because we wouldn't know what | |
1026 | name to give the type. */ | |
1027 | val = allocate_value | |
1028 | (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT | |
1029 | ? builtin_type_unsigned_long_long | |
1030 | : builtin_type_unsigned_long); | |
990a07ab | 1031 | store_unsigned_integer (value_contents_raw (val), |
df407dfe | 1032 | TYPE_LENGTH (value_type (val)), |
c906108c SS |
1033 | v); |
1034 | } | |
1035 | else | |
1036 | { | |
c4093a6a | 1037 | LONGEST v1, v2, v = 0; |
c906108c SS |
1038 | v1 = value_as_long (arg1); |
1039 | v2 = value_as_long (arg2); | |
c5aa993b | 1040 | |
c906108c SS |
1041 | switch (op) |
1042 | { | |
1043 | case BINOP_ADD: | |
1044 | v = v1 + v2; | |
1045 | break; | |
c5aa993b | 1046 | |
c906108c SS |
1047 | case BINOP_SUB: |
1048 | v = v1 - v2; | |
1049 | break; | |
c5aa993b | 1050 | |
c906108c SS |
1051 | case BINOP_MUL: |
1052 | v = v1 * v2; | |
1053 | break; | |
c5aa993b | 1054 | |
c906108c | 1055 | case BINOP_DIV: |
399cfac6 DL |
1056 | if (v2 != 0) |
1057 | v = v1 / v2; | |
1058 | else | |
8a3fe4f8 | 1059 | error (_("Division by zero")); |
c4093a6a JM |
1060 | break; |
1061 | ||
bd49c137 WZ |
1062 | case BINOP_EXP: |
1063 | errno = 0; | |
1064 | v = pow (v1, v2); | |
1065 | if (errno) | |
1066 | error (_("Cannot perform exponentiation: %s"), safe_strerror (errno)); | |
c906108c | 1067 | break; |
c5aa993b | 1068 | |
c906108c | 1069 | case BINOP_REM: |
399cfac6 DL |
1070 | if (v2 != 0) |
1071 | v = v1 % v2; | |
1072 | else | |
8a3fe4f8 | 1073 | error (_("Division by zero")); |
c906108c | 1074 | break; |
c5aa993b | 1075 | |
c906108c SS |
1076 | case BINOP_MOD: |
1077 | /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, | |
1078 | X mod 0 has a defined value, X. */ | |
c906108c SS |
1079 | if (v2 == 0) |
1080 | { | |
1081 | v = v1; | |
1082 | } | |
1083 | else | |
1084 | { | |
c5aa993b | 1085 | v = v1 / v2; |
c906108c SS |
1086 | /* Compute floor. */ |
1087 | if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0)) | |
1088 | { | |
1089 | v--; | |
1090 | } | |
1091 | v = v1 - (v2 * v); | |
1092 | } | |
1093 | break; | |
c5aa993b | 1094 | |
c906108c SS |
1095 | case BINOP_LSH: |
1096 | v = v1 << v2; | |
1097 | break; | |
c5aa993b | 1098 | |
c906108c SS |
1099 | case BINOP_RSH: |
1100 | v = v1 >> v2; | |
1101 | break; | |
c5aa993b | 1102 | |
c906108c SS |
1103 | case BINOP_BITWISE_AND: |
1104 | v = v1 & v2; | |
1105 | break; | |
c5aa993b | 1106 | |
c906108c SS |
1107 | case BINOP_BITWISE_IOR: |
1108 | v = v1 | v2; | |
1109 | break; | |
c5aa993b | 1110 | |
c906108c SS |
1111 | case BINOP_BITWISE_XOR: |
1112 | v = v1 ^ v2; | |
1113 | break; | |
c5aa993b | 1114 | |
c906108c SS |
1115 | case BINOP_LOGICAL_AND: |
1116 | v = v1 && v2; | |
1117 | break; | |
c5aa993b | 1118 | |
c906108c SS |
1119 | case BINOP_LOGICAL_OR: |
1120 | v = v1 || v2; | |
1121 | break; | |
c5aa993b | 1122 | |
c906108c SS |
1123 | case BINOP_MIN: |
1124 | v = v1 < v2 ? v1 : v2; | |
1125 | break; | |
c5aa993b | 1126 | |
c906108c SS |
1127 | case BINOP_MAX: |
1128 | v = v1 > v2 ? v1 : v2; | |
1129 | break; | |
1130 | ||
1131 | case BINOP_EQUAL: | |
1132 | v = v1 == v2; | |
1133 | break; | |
1134 | ||
1135 | case BINOP_LESS: | |
1136 | v = v1 < v2; | |
1137 | break; | |
c5aa993b | 1138 | |
c906108c | 1139 | default: |
8a3fe4f8 | 1140 | error (_("Invalid binary operation on numbers.")); |
c906108c SS |
1141 | } |
1142 | ||
1143 | /* This is a kludge to get around the fact that we don't | |
1144 | know how to determine the result type from the types of | |
1145 | the operands. (I'm not really sure how much we feel the | |
1146 | need to duplicate the exact rules of the current | |
1147 | language. They can get really hairy. But not to do so | |
1148 | makes it hard to document just what we *do* do). */ | |
1149 | ||
1150 | /* Can't just call init_type because we wouldn't know what | |
1151 | name to give the type. */ | |
1152 | val = allocate_value | |
1153 | (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT | |
1154 | ? builtin_type_long_long | |
1155 | : builtin_type_long); | |
990a07ab | 1156 | store_signed_integer (value_contents_raw (val), |
df407dfe | 1157 | TYPE_LENGTH (value_type (val)), |
c906108c SS |
1158 | v); |
1159 | } | |
1160 | } | |
1161 | ||
1162 | return val; | |
1163 | } | |
1164 | \f | |
1165 | /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */ | |
1166 | ||
1167 | int | |
f23631e4 | 1168 | value_logical_not (struct value *arg1) |
c906108c | 1169 | { |
52f0bd74 | 1170 | int len; |
fc1a4b47 | 1171 | const gdb_byte *p; |
c906108c SS |
1172 | struct type *type1; |
1173 | ||
994b9211 | 1174 | arg1 = coerce_number (arg1); |
df407dfe | 1175 | type1 = check_typedef (value_type (arg1)); |
c906108c SS |
1176 | |
1177 | if (TYPE_CODE (type1) == TYPE_CODE_FLT) | |
1178 | return 0 == value_as_double (arg1); | |
1179 | ||
1180 | len = TYPE_LENGTH (type1); | |
0fd88904 | 1181 | p = value_contents (arg1); |
c906108c SS |
1182 | |
1183 | while (--len >= 0) | |
1184 | { | |
1185 | if (*p++) | |
1186 | break; | |
1187 | } | |
1188 | ||
1189 | return len < 0; | |
1190 | } | |
1191 | ||
c4093a6a JM |
1192 | /* Perform a comparison on two string values (whose content are not |
1193 | necessarily null terminated) based on their length */ | |
1194 | ||
1195 | static int | |
f23631e4 | 1196 | value_strcmp (struct value *arg1, struct value *arg2) |
c4093a6a | 1197 | { |
df407dfe AC |
1198 | int len1 = TYPE_LENGTH (value_type (arg1)); |
1199 | int len2 = TYPE_LENGTH (value_type (arg2)); | |
fc1a4b47 AC |
1200 | const gdb_byte *s1 = value_contents (arg1); |
1201 | const gdb_byte *s2 = value_contents (arg2); | |
c4093a6a JM |
1202 | int i, len = len1 < len2 ? len1 : len2; |
1203 | ||
1204 | for (i = 0; i < len; i++) | |
1205 | { | |
1206 | if (s1[i] < s2[i]) | |
1207 | return -1; | |
1208 | else if (s1[i] > s2[i]) | |
1209 | return 1; | |
1210 | else | |
1211 | continue; | |
1212 | } | |
1213 | ||
1214 | if (len1 < len2) | |
1215 | return -1; | |
1216 | else if (len1 > len2) | |
1217 | return 1; | |
1218 | else | |
1219 | return 0; | |
1220 | } | |
1221 | ||
c906108c SS |
1222 | /* Simulate the C operator == by returning a 1 |
1223 | iff ARG1 and ARG2 have equal contents. */ | |
1224 | ||
1225 | int | |
f23631e4 | 1226 | value_equal (struct value *arg1, struct value *arg2) |
c906108c | 1227 | { |
52f0bd74 | 1228 | int len; |
fc1a4b47 AC |
1229 | const gdb_byte *p1; |
1230 | const gdb_byte *p2; | |
c906108c SS |
1231 | struct type *type1, *type2; |
1232 | enum type_code code1; | |
1233 | enum type_code code2; | |
2de41bce | 1234 | int is_int1, is_int2; |
c906108c | 1235 | |
994b9211 AC |
1236 | arg1 = coerce_array (arg1); |
1237 | arg2 = coerce_array (arg2); | |
c906108c | 1238 | |
df407dfe AC |
1239 | type1 = check_typedef (value_type (arg1)); |
1240 | type2 = check_typedef (value_type (arg2)); | |
c906108c SS |
1241 | code1 = TYPE_CODE (type1); |
1242 | code2 = TYPE_CODE (type2); | |
2de41bce PH |
1243 | is_int1 = is_integral_type (type1); |
1244 | is_int2 = is_integral_type (type2); | |
c906108c | 1245 | |
2de41bce | 1246 | if (is_int1 && is_int2) |
c906108c SS |
1247 | return longest_to_int (value_as_long (value_binop (arg1, arg2, |
1248 | BINOP_EQUAL))); | |
2de41bce PH |
1249 | else if ((code1 == TYPE_CODE_FLT || is_int1) |
1250 | && (code2 == TYPE_CODE_FLT || is_int2)) | |
d067a990 MK |
1251 | { |
1252 | /* NOTE: kettenis/20050816: Avoid compiler bug on systems where | |
1253 | `long double' values are returned in static storage (m68k). */ | |
1254 | DOUBLEST d = value_as_double (arg1); | |
1255 | return d == value_as_double (arg2); | |
1256 | } | |
c906108c SS |
1257 | |
1258 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
1259 | is bigger. */ | |
2de41bce | 1260 | else if (code1 == TYPE_CODE_PTR && is_int2) |
1aa20aa8 | 1261 | return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2); |
2de41bce | 1262 | else if (code2 == TYPE_CODE_PTR && is_int1) |
1aa20aa8 | 1263 | return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2); |
c906108c SS |
1264 | |
1265 | else if (code1 == code2 | |
1266 | && ((len = (int) TYPE_LENGTH (type1)) | |
1267 | == (int) TYPE_LENGTH (type2))) | |
1268 | { | |
0fd88904 AC |
1269 | p1 = value_contents (arg1); |
1270 | p2 = value_contents (arg2); | |
c906108c SS |
1271 | while (--len >= 0) |
1272 | { | |
1273 | if (*p1++ != *p2++) | |
1274 | break; | |
1275 | } | |
1276 | return len < 0; | |
1277 | } | |
c4093a6a JM |
1278 | else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) |
1279 | { | |
1280 | return value_strcmp (arg1, arg2) == 0; | |
1281 | } | |
c906108c SS |
1282 | else |
1283 | { | |
8a3fe4f8 | 1284 | error (_("Invalid type combination in equality test.")); |
c5aa993b | 1285 | return 0; /* For lint -- never reached */ |
c906108c SS |
1286 | } |
1287 | } | |
1288 | ||
1289 | /* Simulate the C operator < by returning 1 | |
1290 | iff ARG1's contents are less than ARG2's. */ | |
1291 | ||
1292 | int | |
f23631e4 | 1293 | value_less (struct value *arg1, struct value *arg2) |
c906108c | 1294 | { |
52f0bd74 AC |
1295 | enum type_code code1; |
1296 | enum type_code code2; | |
c906108c | 1297 | struct type *type1, *type2; |
2de41bce | 1298 | int is_int1, is_int2; |
c906108c | 1299 | |
994b9211 AC |
1300 | arg1 = coerce_array (arg1); |
1301 | arg2 = coerce_array (arg2); | |
c906108c | 1302 | |
df407dfe AC |
1303 | type1 = check_typedef (value_type (arg1)); |
1304 | type2 = check_typedef (value_type (arg2)); | |
c906108c SS |
1305 | code1 = TYPE_CODE (type1); |
1306 | code2 = TYPE_CODE (type2); | |
2de41bce PH |
1307 | is_int1 = is_integral_type (type1); |
1308 | is_int2 = is_integral_type (type2); | |
c906108c | 1309 | |
2de41bce | 1310 | if (is_int1 && is_int2) |
c906108c SS |
1311 | return longest_to_int (value_as_long (value_binop (arg1, arg2, |
1312 | BINOP_LESS))); | |
2de41bce PH |
1313 | else if ((code1 == TYPE_CODE_FLT || is_int1) |
1314 | && (code2 == TYPE_CODE_FLT || is_int2)) | |
d067a990 MK |
1315 | { |
1316 | /* NOTE: kettenis/20050816: Avoid compiler bug on systems where | |
1317 | `long double' values are returned in static storage (m68k). */ | |
1318 | DOUBLEST d = value_as_double (arg1); | |
1319 | return d < value_as_double (arg2); | |
1320 | } | |
c906108c | 1321 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) |
1aa20aa8 | 1322 | return value_as_address (arg1) < value_as_address (arg2); |
c906108c SS |
1323 | |
1324 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
1325 | is bigger. */ | |
2de41bce | 1326 | else if (code1 == TYPE_CODE_PTR && is_int2) |
1aa20aa8 | 1327 | return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2); |
2de41bce | 1328 | else if (code2 == TYPE_CODE_PTR && is_int1) |
1aa20aa8 | 1329 | return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2); |
c4093a6a JM |
1330 | else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) |
1331 | return value_strcmp (arg1, arg2) < 0; | |
c906108c SS |
1332 | else |
1333 | { | |
8a3fe4f8 | 1334 | error (_("Invalid type combination in ordering comparison.")); |
c906108c SS |
1335 | return 0; |
1336 | } | |
1337 | } | |
1338 | \f | |
36e9969c NS |
1339 | /* The unary operators +, - and ~. They free the argument ARG1. */ |
1340 | ||
1341 | struct value * | |
1342 | value_pos (struct value *arg1) | |
1343 | { | |
1344 | struct type *type; | |
1345 | ||
1346 | arg1 = coerce_ref (arg1); | |
1347 | ||
1348 | type = check_typedef (value_type (arg1)); | |
1349 | ||
1350 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
1351 | return value_from_double (type, value_as_double (arg1)); | |
1352 | else if (is_integral_type (type)) | |
1353 | { | |
1354 | /* Perform integral promotion for ANSI C/C++. FIXME: What about | |
1355 | FORTRAN and (the deleted) chill ? */ | |
1356 | if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int)) | |
1357 | type = builtin_type_int; | |
1358 | ||
1359 | return value_from_longest (type, value_as_long (arg1)); | |
1360 | } | |
1361 | else | |
1362 | { | |
1363 | error ("Argument to positive operation not a number."); | |
1364 | return 0; /* For lint -- never reached */ | |
1365 | } | |
1366 | } | |
c906108c | 1367 | |
f23631e4 AC |
1368 | struct value * |
1369 | value_neg (struct value *arg1) | |
c906108c | 1370 | { |
52f0bd74 | 1371 | struct type *type; |
df407dfe | 1372 | struct type *result_type = value_type (arg1); |
c906108c | 1373 | |
994b9211 | 1374 | arg1 = coerce_ref (arg1); |
c906108c | 1375 | |
df407dfe | 1376 | type = check_typedef (value_type (arg1)); |
c906108c SS |
1377 | |
1378 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
c5aa993b | 1379 | return value_from_double (result_type, -value_as_double (arg1)); |
2de41bce | 1380 | else if (is_integral_type (type)) |
c906108c | 1381 | { |
db034ac5 | 1382 | /* Perform integral promotion for ANSI C/C++. FIXME: What about |
1b831c93 | 1383 | FORTRAN and (the deleted) chill ? */ |
c906108c SS |
1384 | if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int)) |
1385 | result_type = builtin_type_int; | |
1386 | ||
c5aa993b JM |
1387 | return value_from_longest (result_type, -value_as_long (arg1)); |
1388 | } | |
1389 | else | |
1390 | { | |
8a3fe4f8 | 1391 | error (_("Argument to negate operation not a number.")); |
c5aa993b | 1392 | return 0; /* For lint -- never reached */ |
c906108c | 1393 | } |
c906108c SS |
1394 | } |
1395 | ||
f23631e4 AC |
1396 | struct value * |
1397 | value_complement (struct value *arg1) | |
c906108c | 1398 | { |
52f0bd74 | 1399 | struct type *type; |
df407dfe | 1400 | struct type *result_type = value_type (arg1); |
c906108c | 1401 | |
994b9211 | 1402 | arg1 = coerce_ref (arg1); |
c906108c | 1403 | |
df407dfe | 1404 | type = check_typedef (value_type (arg1)); |
c906108c | 1405 | |
2de41bce | 1406 | if (!is_integral_type (type)) |
8a3fe4f8 | 1407 | error (_("Argument to complement operation not an integer or boolean.")); |
c906108c SS |
1408 | |
1409 | /* Perform integral promotion for ANSI C/C++. | |
1410 | FIXME: What about FORTRAN ? */ | |
1411 | if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int)) | |
1412 | result_type = builtin_type_int; | |
1413 | ||
c5aa993b | 1414 | return value_from_longest (result_type, ~value_as_long (arg1)); |
c906108c SS |
1415 | } |
1416 | \f | |
df407dfe | 1417 | /* The INDEX'th bit of SET value whose value_type is TYPE, |
0fd88904 | 1418 | and whose value_contents is valaddr. |
c906108c SS |
1419 | Return -1 if out of range, -2 other error. */ |
1420 | ||
1421 | int | |
fc1a4b47 | 1422 | value_bit_index (struct type *type, const gdb_byte *valaddr, int index) |
c906108c SS |
1423 | { |
1424 | LONGEST low_bound, high_bound; | |
1425 | LONGEST word; | |
1426 | unsigned rel_index; | |
1427 | struct type *range = TYPE_FIELD_TYPE (type, 0); | |
1428 | if (get_discrete_bounds (range, &low_bound, &high_bound) < 0) | |
1429 | return -2; | |
1430 | if (index < low_bound || index > high_bound) | |
1431 | return -1; | |
1432 | rel_index = index - low_bound; | |
1433 | word = unpack_long (builtin_type_unsigned_char, | |
1434 | valaddr + (rel_index / TARGET_CHAR_BIT)); | |
1435 | rel_index %= TARGET_CHAR_BIT; | |
1436 | if (BITS_BIG_ENDIAN) | |
1437 | rel_index = TARGET_CHAR_BIT - 1 - rel_index; | |
1438 | return (word >> rel_index) & 1; | |
1439 | } | |
1440 | ||
f23631e4 AC |
1441 | struct value * |
1442 | value_in (struct value *element, struct value *set) | |
c906108c SS |
1443 | { |
1444 | int member; | |
df407dfe AC |
1445 | struct type *settype = check_typedef (value_type (set)); |
1446 | struct type *eltype = check_typedef (value_type (element)); | |
c906108c SS |
1447 | if (TYPE_CODE (eltype) == TYPE_CODE_RANGE) |
1448 | eltype = TYPE_TARGET_TYPE (eltype); | |
1449 | if (TYPE_CODE (settype) != TYPE_CODE_SET) | |
8a3fe4f8 | 1450 | error (_("Second argument of 'IN' has wrong type")); |
c906108c SS |
1451 | if (TYPE_CODE (eltype) != TYPE_CODE_INT |
1452 | && TYPE_CODE (eltype) != TYPE_CODE_CHAR | |
1453 | && TYPE_CODE (eltype) != TYPE_CODE_ENUM | |
1454 | && TYPE_CODE (eltype) != TYPE_CODE_BOOL) | |
8a3fe4f8 | 1455 | error (_("First argument of 'IN' has wrong type")); |
0fd88904 | 1456 | member = value_bit_index (settype, value_contents (set), |
c906108c SS |
1457 | value_as_long (element)); |
1458 | if (member < 0) | |
8a3fe4f8 | 1459 | error (_("First argument of 'IN' not in range")); |
c906108c SS |
1460 | return value_from_longest (LA_BOOL_TYPE, member); |
1461 | } | |
1462 | ||
1463 | void | |
fba45db2 | 1464 | _initialize_valarith (void) |
c906108c SS |
1465 | { |
1466 | } |