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