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