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