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