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