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