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