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