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