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