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