Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Perform arithmetic and other operations on values, for GDB. |
1bac305b | 2 | |
f23631e4 | 3 | Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, |
2de41bce | 4 | 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software |
1bac305b | 5 | Foundation, Inc. |
c906108c | 6 | |
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b JM |
19 | You should have received a copy of the GNU General Public License |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
23 | |
24 | #include "defs.h" | |
25 | #include "value.h" | |
26 | #include "symtab.h" | |
27 | #include "gdbtypes.h" | |
28 | #include "expression.h" | |
29 | #include "target.h" | |
30 | #include "language.h" | |
c906108c | 31 | #include "gdb_string.h" |
d16aafd8 | 32 | #include "doublest.h" |
c4093a6a | 33 | #include <math.h> |
04714b91 | 34 | #include "infcall.h" |
c906108c SS |
35 | |
36 | /* Define whether or not the C operator '/' truncates towards zero for | |
37 | differently signed operands (truncation direction is undefined in C). */ | |
38 | ||
39 | #ifndef TRUNCATION_TOWARDS_ZERO | |
40 | #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2) | |
41 | #endif | |
42 | ||
f23631e4 | 43 | static struct value *value_subscripted_rvalue (struct value *, struct value *, int); |
c906108c | 44 | |
a14ed312 | 45 | void _initialize_valarith (void); |
c906108c | 46 | \f |
c5aa993b | 47 | |
ca439ad2 JI |
48 | /* Given a pointer, return the size of its target. |
49 | If the pointer type is void *, then return 1. | |
50 | If the target type is incomplete, then error out. | |
51 | This isn't a general purpose function, but just a | |
52 | helper for value_sub & value_add. | |
53 | */ | |
54 | ||
55 | static LONGEST | |
56 | find_size_for_pointer_math (struct type *ptr_type) | |
57 | { | |
58 | LONGEST sz = -1; | |
59 | struct type *ptr_target; | |
60 | ||
61 | ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type)); | |
62 | ||
63 | sz = TYPE_LENGTH (ptr_target); | |
64 | if (sz == 0) | |
65 | { | |
66 | if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID) | |
67 | sz = 1; | |
68 | else | |
69 | { | |
70 | char *name; | |
71 | ||
72 | name = TYPE_NAME (ptr_target); | |
73 | if (name == NULL) | |
74 | name = TYPE_TAG_NAME (ptr_target); | |
75 | if (name == NULL) | |
76 | error ("Cannot perform pointer math on incomplete types, " | |
77 | "try casting to a known type, or void *."); | |
78 | else | |
79 | error ("Cannot perform pointer math on incomplete type \"%s\", " | |
80 | "try casting to a known type, or void *.", name); | |
81 | } | |
82 | } | |
83 | return sz; | |
84 | } | |
85 | ||
f23631e4 AC |
86 | struct value * |
87 | value_add (struct value *arg1, struct value *arg2) | |
c906108c | 88 | { |
f23631e4 AC |
89 | struct value *valint; |
90 | struct value *valptr; | |
ca439ad2 | 91 | LONGEST sz; |
c906108c SS |
92 | struct type *type1, *type2, *valptrtype; |
93 | ||
2de41bce PH |
94 | COERCE_ARRAY (arg1); |
95 | COERCE_ARRAY (arg2); | |
c906108c SS |
96 | type1 = check_typedef (VALUE_TYPE (arg1)); |
97 | type2 = check_typedef (VALUE_TYPE (arg2)); | |
98 | ||
99 | if ((TYPE_CODE (type1) == TYPE_CODE_PTR | |
100 | || TYPE_CODE (type2) == TYPE_CODE_PTR) | |
101 | && | |
2de41bce | 102 | (is_integral_type (type1) || is_integral_type (type2))) |
c906108c SS |
103 | /* Exactly one argument is a pointer, and one is an integer. */ |
104 | { | |
f23631e4 | 105 | struct value *retval; |
c906108c SS |
106 | |
107 | if (TYPE_CODE (type1) == TYPE_CODE_PTR) | |
108 | { | |
109 | valptr = arg1; | |
110 | valint = arg2; | |
111 | valptrtype = type1; | |
112 | } | |
113 | else | |
114 | { | |
115 | valptr = arg2; | |
116 | valint = arg1; | |
117 | valptrtype = type2; | |
118 | } | |
ca439ad2 JI |
119 | |
120 | sz = find_size_for_pointer_math (valptrtype); | |
121 | ||
4478b372 | 122 | retval = value_from_pointer (valptrtype, |
1aa20aa8 | 123 | value_as_address (valptr) |
ca439ad2 | 124 | + (sz * value_as_long (valint))); |
c906108c SS |
125 | VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (valptr); |
126 | return retval; | |
127 | } | |
128 | ||
129 | return value_binop (arg1, arg2, BINOP_ADD); | |
130 | } | |
131 | ||
f23631e4 AC |
132 | struct value * |
133 | value_sub (struct value *arg1, struct value *arg2) | |
c906108c SS |
134 | { |
135 | struct type *type1, *type2; | |
2de41bce PH |
136 | COERCE_ARRAY (arg1); |
137 | COERCE_ARRAY (arg2); | |
c906108c SS |
138 | type1 = check_typedef (VALUE_TYPE (arg1)); |
139 | type2 = check_typedef (VALUE_TYPE (arg2)); | |
140 | ||
141 | if (TYPE_CODE (type1) == TYPE_CODE_PTR) | |
142 | { | |
2de41bce | 143 | if (is_integral_type (type2)) |
c906108c SS |
144 | { |
145 | /* pointer - integer. */ | |
ca439ad2 JI |
146 | LONGEST sz = find_size_for_pointer_math (type1); |
147 | ||
dbbd9c57 | 148 | return value_from_pointer (type1, |
1aa20aa8 | 149 | (value_as_address (arg1) |
4478b372 | 150 | - (sz * value_as_long (arg2)))); |
c906108c SS |
151 | } |
152 | else if (TYPE_CODE (type2) == TYPE_CODE_PTR | |
3dd3139b MS |
153 | && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1))) |
154 | == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2)))) | |
c906108c SS |
155 | { |
156 | /* pointer to <type x> - pointer to <type x>. */ | |
157 | LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1))); | |
158 | return value_from_longest | |
c5aa993b | 159 | (builtin_type_long, /* FIXME -- should be ptrdiff_t */ |
c906108c SS |
160 | (value_as_long (arg1) - value_as_long (arg2)) / sz); |
161 | } | |
162 | else | |
163 | { | |
164 | error ("\ | |
165 | First argument of `-' is a pointer and second argument is neither\n\ | |
166 | an integer nor a pointer of the same type."); | |
167 | } | |
168 | } | |
169 | ||
170 | return value_binop (arg1, arg2, BINOP_SUB); | |
171 | } | |
172 | ||
173 | /* Return the value of ARRAY[IDX]. | |
174 | See comments in value_coerce_array() for rationale for reason for | |
175 | doing lower bounds adjustment here rather than there. | |
176 | FIXME: Perhaps we should validate that the index is valid and if | |
177 | verbosity is set, warn about invalid indices (but still use them). */ | |
178 | ||
f23631e4 AC |
179 | struct value * |
180 | value_subscript (struct value *array, struct value *idx) | |
c906108c | 181 | { |
f23631e4 | 182 | struct value *bound; |
c906108c SS |
183 | int c_style = current_language->c_style_arrays; |
184 | struct type *tarray; | |
185 | ||
186 | COERCE_REF (array); | |
187 | tarray = check_typedef (VALUE_TYPE (array)); | |
188 | COERCE_VARYING_ARRAY (array, tarray); | |
189 | ||
190 | if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY | |
191 | || TYPE_CODE (tarray) == TYPE_CODE_STRING) | |
192 | { | |
193 | struct type *range_type = TYPE_INDEX_TYPE (tarray); | |
194 | LONGEST lowerbound, upperbound; | |
195 | get_discrete_bounds (range_type, &lowerbound, &upperbound); | |
196 | ||
197 | if (VALUE_LVAL (array) != lval_memory) | |
198 | return value_subscripted_rvalue (array, idx, lowerbound); | |
199 | ||
200 | if (c_style == 0) | |
201 | { | |
202 | LONGEST index = value_as_long (idx); | |
203 | if (index >= lowerbound && index <= upperbound) | |
204 | return value_subscripted_rvalue (array, idx, lowerbound); | |
987504bb JJ |
205 | /* Emit warning unless we have an array of unknown size. |
206 | An array of unknown size has lowerbound 0 and upperbound -1. */ | |
207 | if (upperbound > -1) | |
208 | warning ("array or string index out of range"); | |
c906108c SS |
209 | /* fall doing C stuff */ |
210 | c_style = 1; | |
211 | } | |
212 | ||
213 | if (lowerbound != 0) | |
214 | { | |
215 | bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound); | |
216 | idx = value_sub (idx, bound); | |
217 | } | |
218 | ||
219 | array = value_coerce_array (array); | |
220 | } | |
221 | ||
222 | if (TYPE_CODE (tarray) == TYPE_CODE_BITSTRING) | |
223 | { | |
224 | struct type *range_type = TYPE_INDEX_TYPE (tarray); | |
225 | LONGEST index = value_as_long (idx); | |
f23631e4 | 226 | struct value *v; |
c906108c SS |
227 | int offset, byte, bit_index; |
228 | LONGEST lowerbound, upperbound; | |
229 | get_discrete_bounds (range_type, &lowerbound, &upperbound); | |
230 | if (index < lowerbound || index > upperbound) | |
231 | error ("bitstring index out of range"); | |
232 | index -= lowerbound; | |
233 | offset = index / TARGET_CHAR_BIT; | |
c5aa993b | 234 | byte = *((char *) VALUE_CONTENTS (array) + offset); |
c906108c SS |
235 | bit_index = index % TARGET_CHAR_BIT; |
236 | byte >>= (BITS_BIG_ENDIAN ? TARGET_CHAR_BIT - 1 - bit_index : bit_index); | |
237 | v = value_from_longest (LA_BOOL_TYPE, byte & 1); | |
238 | VALUE_BITPOS (v) = bit_index; | |
239 | VALUE_BITSIZE (v) = 1; | |
240 | VALUE_LVAL (v) = VALUE_LVAL (array); | |
241 | if (VALUE_LVAL (array) == lval_internalvar) | |
242 | VALUE_LVAL (v) = lval_internalvar_component; | |
243 | VALUE_ADDRESS (v) = VALUE_ADDRESS (array); | |
244 | VALUE_OFFSET (v) = offset + VALUE_OFFSET (array); | |
245 | return v; | |
246 | } | |
247 | ||
248 | if (c_style) | |
249 | return value_ind (value_add (array, idx)); | |
250 | else | |
251 | error ("not an array or string"); | |
252 | } | |
253 | ||
254 | /* Return the value of EXPR[IDX], expr an aggregate rvalue | |
255 | (eg, a vector register). This routine used to promote floats | |
256 | to doubles, but no longer does. */ | |
257 | ||
f23631e4 AC |
258 | static struct value * |
259 | value_subscripted_rvalue (struct value *array, struct value *idx, int lowerbound) | |
c906108c SS |
260 | { |
261 | struct type *array_type = check_typedef (VALUE_TYPE (array)); | |
262 | struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type)); | |
263 | unsigned int elt_size = TYPE_LENGTH (elt_type); | |
264 | LONGEST index = value_as_long (idx); | |
265 | unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound); | |
f23631e4 | 266 | struct value *v; |
c906108c SS |
267 | |
268 | if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type)) | |
269 | error ("no such vector element"); | |
270 | ||
271 | v = allocate_value (elt_type); | |
272 | if (VALUE_LAZY (array)) | |
273 | VALUE_LAZY (v) = 1; | |
274 | else | |
275 | memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size); | |
276 | ||
277 | if (VALUE_LVAL (array) == lval_internalvar) | |
278 | VALUE_LVAL (v) = lval_internalvar_component; | |
279 | else | |
280 | VALUE_LVAL (v) = VALUE_LVAL (array); | |
281 | VALUE_ADDRESS (v) = VALUE_ADDRESS (array); | |
f49bacc8 | 282 | VALUE_REGNO (v) = VALUE_REGNO (array); |
c906108c SS |
283 | VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs; |
284 | return v; | |
285 | } | |
286 | \f | |
287 | /* Check to see if either argument is a structure. This is called so | |
288 | we know whether to go ahead with the normal binop or look for a | |
289 | user defined function instead. | |
290 | ||
291 | For now, we do not overload the `=' operator. */ | |
292 | ||
293 | int | |
f23631e4 | 294 | binop_user_defined_p (enum exp_opcode op, struct value *arg1, struct value *arg2) |
c906108c SS |
295 | { |
296 | struct type *type1, *type2; | |
297 | if (op == BINOP_ASSIGN || op == BINOP_CONCAT) | |
298 | return 0; | |
299 | type1 = check_typedef (VALUE_TYPE (arg1)); | |
300 | type2 = check_typedef (VALUE_TYPE (arg2)); | |
301 | return (TYPE_CODE (type1) == TYPE_CODE_STRUCT | |
302 | || TYPE_CODE (type2) == TYPE_CODE_STRUCT | |
303 | || (TYPE_CODE (type1) == TYPE_CODE_REF | |
304 | && TYPE_CODE (TYPE_TARGET_TYPE (type1)) == TYPE_CODE_STRUCT) | |
305 | || (TYPE_CODE (type2) == TYPE_CODE_REF | |
306 | && TYPE_CODE (TYPE_TARGET_TYPE (type2)) == TYPE_CODE_STRUCT)); | |
307 | } | |
308 | ||
309 | /* Check to see if argument is a structure. This is called so | |
310 | we know whether to go ahead with the normal unop or look for a | |
311 | user defined function instead. | |
312 | ||
313 | For now, we do not overload the `&' operator. */ | |
314 | ||
c5aa993b | 315 | int |
f23631e4 | 316 | unop_user_defined_p (enum exp_opcode op, struct value *arg1) |
c906108c SS |
317 | { |
318 | struct type *type1; | |
319 | if (op == UNOP_ADDR) | |
320 | return 0; | |
321 | type1 = check_typedef (VALUE_TYPE (arg1)); | |
322 | for (;;) | |
323 | { | |
324 | if (TYPE_CODE (type1) == TYPE_CODE_STRUCT) | |
325 | return 1; | |
326 | else if (TYPE_CODE (type1) == TYPE_CODE_REF) | |
327 | type1 = TYPE_TARGET_TYPE (type1); | |
328 | else | |
329 | return 0; | |
330 | } | |
331 | } | |
332 | ||
333 | /* We know either arg1 or arg2 is a structure, so try to find the right | |
334 | user defined function. Create an argument vector that calls | |
335 | arg1.operator @ (arg1,arg2) and return that value (where '@' is any | |
336 | binary operator which is legal for GNU C++). | |
337 | ||
338 | OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP | |
339 | is the opcode saying how to modify it. Otherwise, OTHEROP is | |
340 | unused. */ | |
341 | ||
f23631e4 AC |
342 | struct value * |
343 | value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op, | |
fba45db2 | 344 | enum exp_opcode otherop, enum noside noside) |
c906108c | 345 | { |
f23631e4 | 346 | struct value **argvec; |
c906108c SS |
347 | char *ptr; |
348 | char tstr[13]; | |
349 | int static_memfuncp; | |
350 | ||
351 | COERCE_REF (arg1); | |
352 | COERCE_REF (arg2); | |
353 | COERCE_ENUM (arg1); | |
354 | COERCE_ENUM (arg2); | |
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 | ||
359 | if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT) | |
c5aa993b | 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 | ||
c5aa993b JM |
367 | /* make the right function name up */ |
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 SS |
445 | default: |
446 | error ("Invalid binary operation specified."); | |
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 SS |
471 | default: |
472 | error ("Invalid binary operation specified."); | |
473 | } | |
474 | ||
c5aa993b JM |
475 | argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure"); |
476 | ||
c906108c SS |
477 | if (argvec[0]) |
478 | { | |
479 | if (static_memfuncp) | |
480 | { | |
481 | argvec[1] = argvec[0]; | |
482 | argvec++; | |
483 | } | |
484 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
485 | { | |
486 | struct type *return_type; | |
487 | return_type | |
488 | = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0]))); | |
489 | return value_zero (return_type, VALUE_LVAL (arg1)); | |
490 | } | |
491 | return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); | |
492 | } | |
493 | error ("member function %s not found", tstr); | |
494 | #ifdef lint | |
495 | return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); | |
496 | #endif | |
497 | } | |
498 | ||
499 | /* We know that arg1 is a structure, so try to find a unary user | |
500 | defined operator that matches the operator in question. | |
501 | Create an argument vector that calls arg1.operator @ (arg1) | |
502 | and return that value (where '@' is (almost) any unary operator which | |
503 | is legal for GNU C++). */ | |
504 | ||
f23631e4 AC |
505 | struct value * |
506 | value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside) | |
c906108c | 507 | { |
f23631e4 | 508 | struct value **argvec; |
c906108c SS |
509 | char *ptr, *mangle_ptr; |
510 | char tstr[13], mangle_tstr[13]; | |
491b8946 | 511 | int static_memfuncp, nargs; |
c906108c SS |
512 | |
513 | COERCE_REF (arg1); | |
514 | COERCE_ENUM (arg1); | |
515 | ||
516 | /* now we know that what we have to do is construct our | |
517 | arg vector and find the right function to call it with. */ | |
518 | ||
519 | if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT) | |
c5aa993b | 520 | error ("Can't do that unary op on that type"); /* FIXME be explicit */ |
c906108c | 521 | |
491b8946 | 522 | argvec = (struct value **) alloca (sizeof (struct value *) * 4); |
c906108c SS |
523 | argvec[1] = value_addr (arg1); |
524 | argvec[2] = 0; | |
525 | ||
491b8946 DJ |
526 | nargs = 1; |
527 | ||
c5aa993b JM |
528 | /* make the right function name up */ |
529 | strcpy (tstr, "operator__"); | |
530 | ptr = tstr + 8; | |
531 | strcpy (mangle_tstr, "__"); | |
532 | mangle_ptr = mangle_tstr + 2; | |
c906108c SS |
533 | switch (op) |
534 | { | |
c5aa993b JM |
535 | case UNOP_PREINCREMENT: |
536 | strcpy (ptr, "++"); | |
537 | break; | |
538 | case UNOP_PREDECREMENT: | |
491b8946 | 539 | strcpy (ptr, "--"); |
c5aa993b JM |
540 | break; |
541 | case UNOP_POSTINCREMENT: | |
542 | strcpy (ptr, "++"); | |
491b8946 DJ |
543 | argvec[2] = value_from_longest (builtin_type_int, 0); |
544 | argvec[3] = 0; | |
545 | nargs ++; | |
c5aa993b JM |
546 | break; |
547 | case UNOP_POSTDECREMENT: | |
491b8946 DJ |
548 | strcpy (ptr, "--"); |
549 | argvec[2] = value_from_longest (builtin_type_int, 0); | |
550 | argvec[3] = 0; | |
551 | nargs ++; | |
c5aa993b JM |
552 | break; |
553 | case UNOP_LOGICAL_NOT: | |
554 | strcpy (ptr, "!"); | |
555 | break; | |
556 | case UNOP_COMPLEMENT: | |
557 | strcpy (ptr, "~"); | |
558 | break; | |
559 | case UNOP_NEG: | |
560 | strcpy (ptr, "-"); | |
561 | break; | |
562 | case UNOP_IND: | |
563 | strcpy (ptr, "*"); | |
564 | break; | |
c906108c SS |
565 | default: |
566 | error ("Invalid unary operation specified."); | |
567 | } | |
568 | ||
c5aa993b | 569 | argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure"); |
c906108c SS |
570 | |
571 | if (argvec[0]) | |
572 | { | |
573 | if (static_memfuncp) | |
574 | { | |
575 | argvec[1] = argvec[0]; | |
491b8946 | 576 | nargs --; |
c906108c SS |
577 | argvec++; |
578 | } | |
579 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
580 | { | |
581 | struct type *return_type; | |
582 | return_type | |
583 | = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0]))); | |
584 | return value_zero (return_type, VALUE_LVAL (arg1)); | |
585 | } | |
491b8946 | 586 | return call_function_by_hand (argvec[0], nargs, argvec + 1); |
c906108c SS |
587 | } |
588 | error ("member function %s not found", tstr); | |
c5aa993b | 589 | return 0; /* For lint -- never reached */ |
c906108c | 590 | } |
c906108c | 591 | \f |
c5aa993b | 592 | |
c906108c SS |
593 | /* Concatenate two values with the following conditions: |
594 | ||
c5aa993b JM |
595 | (1) Both values must be either bitstring values or character string |
596 | values and the resulting value consists of the concatenation of | |
597 | ARG1 followed by ARG2. | |
c906108c | 598 | |
c5aa993b | 599 | or |
c906108c | 600 | |
c5aa993b JM |
601 | One value must be an integer value and the other value must be |
602 | either a bitstring value or character string value, which is | |
603 | to be repeated by the number of times specified by the integer | |
604 | value. | |
c906108c SS |
605 | |
606 | ||
c5aa993b JM |
607 | (2) Boolean values are also allowed and are treated as bit string |
608 | values of length 1. | |
c906108c | 609 | |
c5aa993b JM |
610 | (3) Character values are also allowed and are treated as character |
611 | string values of length 1. | |
612 | */ | |
c906108c | 613 | |
f23631e4 AC |
614 | struct value * |
615 | value_concat (struct value *arg1, struct value *arg2) | |
c906108c | 616 | { |
f23631e4 AC |
617 | struct value *inval1; |
618 | struct value *inval2; | |
619 | struct value *outval = NULL; | |
c906108c SS |
620 | int inval1len, inval2len; |
621 | int count, idx; | |
622 | char *ptr; | |
623 | char inchar; | |
624 | struct type *type1 = check_typedef (VALUE_TYPE (arg1)); | |
625 | struct type *type2 = check_typedef (VALUE_TYPE (arg2)); | |
626 | ||
627 | COERCE_VARYING_ARRAY (arg1, type1); | |
628 | COERCE_VARYING_ARRAY (arg2, type2); | |
629 | ||
630 | /* First figure out if we are dealing with two values to be concatenated | |
631 | or a repeat count and a value to be repeated. INVAL1 is set to the | |
632 | first of two concatenated values, or the repeat count. INVAL2 is set | |
633 | to the second of the two concatenated values or the value to be | |
634 | repeated. */ | |
635 | ||
636 | if (TYPE_CODE (type2) == TYPE_CODE_INT) | |
637 | { | |
638 | struct type *tmp = type1; | |
639 | type1 = tmp; | |
640 | tmp = type2; | |
641 | inval1 = arg2; | |
642 | inval2 = arg1; | |
643 | } | |
644 | else | |
645 | { | |
646 | inval1 = arg1; | |
647 | inval2 = arg2; | |
648 | } | |
649 | ||
650 | /* Now process the input values. */ | |
651 | ||
652 | if (TYPE_CODE (type1) == TYPE_CODE_INT) | |
653 | { | |
654 | /* We have a repeat count. Validate the second value and then | |
c5aa993b | 655 | construct a value repeated that many times. */ |
c906108c SS |
656 | if (TYPE_CODE (type2) == TYPE_CODE_STRING |
657 | || TYPE_CODE (type2) == TYPE_CODE_CHAR) | |
658 | { | |
659 | count = longest_to_int (value_as_long (inval1)); | |
660 | inval2len = TYPE_LENGTH (type2); | |
661 | ptr = (char *) alloca (count * inval2len); | |
662 | if (TYPE_CODE (type2) == TYPE_CODE_CHAR) | |
663 | { | |
664 | inchar = (char) unpack_long (type2, | |
665 | VALUE_CONTENTS (inval2)); | |
666 | for (idx = 0; idx < count; idx++) | |
667 | { | |
668 | *(ptr + idx) = inchar; | |
669 | } | |
670 | } | |
671 | else | |
672 | { | |
673 | for (idx = 0; idx < count; idx++) | |
674 | { | |
675 | memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2), | |
676 | inval2len); | |
677 | } | |
678 | } | |
679 | outval = value_string (ptr, count * inval2len); | |
680 | } | |
681 | else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING | |
682 | || TYPE_CODE (type2) == TYPE_CODE_BOOL) | |
683 | { | |
684 | error ("unimplemented support for bitstring/boolean repeats"); | |
685 | } | |
686 | else | |
687 | { | |
688 | error ("can't repeat values of that type"); | |
689 | } | |
690 | } | |
691 | else if (TYPE_CODE (type1) == TYPE_CODE_STRING | |
c5aa993b | 692 | || TYPE_CODE (type1) == TYPE_CODE_CHAR) |
c906108c SS |
693 | { |
694 | /* We have two character strings to concatenate. */ | |
695 | if (TYPE_CODE (type2) != TYPE_CODE_STRING | |
696 | && TYPE_CODE (type2) != TYPE_CODE_CHAR) | |
697 | { | |
698 | error ("Strings can only be concatenated with other strings."); | |
699 | } | |
700 | inval1len = TYPE_LENGTH (type1); | |
701 | inval2len = TYPE_LENGTH (type2); | |
702 | ptr = (char *) alloca (inval1len + inval2len); | |
703 | if (TYPE_CODE (type1) == TYPE_CODE_CHAR) | |
704 | { | |
705 | *ptr = (char) unpack_long (type1, VALUE_CONTENTS (inval1)); | |
706 | } | |
707 | else | |
708 | { | |
709 | memcpy (ptr, VALUE_CONTENTS (inval1), inval1len); | |
710 | } | |
711 | if (TYPE_CODE (type2) == TYPE_CODE_CHAR) | |
712 | { | |
c5aa993b | 713 | *(ptr + inval1len) = |
c906108c SS |
714 | (char) unpack_long (type2, VALUE_CONTENTS (inval2)); |
715 | } | |
716 | else | |
717 | { | |
718 | memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len); | |
719 | } | |
720 | outval = value_string (ptr, inval1len + inval2len); | |
721 | } | |
722 | else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING | |
723 | || TYPE_CODE (type1) == TYPE_CODE_BOOL) | |
724 | { | |
725 | /* We have two bitstrings to concatenate. */ | |
726 | if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING | |
727 | && TYPE_CODE (type2) != TYPE_CODE_BOOL) | |
728 | { | |
729 | error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."); | |
730 | } | |
731 | error ("unimplemented support for bitstring/boolean concatenation."); | |
c5aa993b | 732 | } |
c906108c SS |
733 | else |
734 | { | |
735 | /* We don't know how to concatenate these operands. */ | |
736 | error ("illegal operands for concatenation."); | |
737 | } | |
738 | return (outval); | |
739 | } | |
c906108c SS |
740 | \f |
741 | ||
c5aa993b | 742 | |
c906108c SS |
743 | /* Perform a binary operation on two operands which have reasonable |
744 | representations as integers or floats. This includes booleans, | |
745 | characters, integers, or floats. | |
746 | Does not support addition and subtraction on pointers; | |
747 | use value_add or value_sub if you want to handle those possibilities. */ | |
748 | ||
f23631e4 AC |
749 | struct value * |
750 | value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) | |
c906108c | 751 | { |
f23631e4 | 752 | struct value *val; |
c906108c SS |
753 | struct type *type1, *type2; |
754 | ||
755 | COERCE_REF (arg1); | |
756 | COERCE_REF (arg2); | |
c906108c SS |
757 | type1 = check_typedef (VALUE_TYPE (arg1)); |
758 | type2 = check_typedef (VALUE_TYPE (arg2)); | |
759 | ||
2de41bce | 760 | if ((TYPE_CODE (type1) != TYPE_CODE_FLT && !is_integral_type (type1)) |
c906108c | 761 | || |
2de41bce | 762 | (TYPE_CODE (type2) != TYPE_CODE_FLT && !is_integral_type (type2))) |
c906108c SS |
763 | error ("Argument to arithmetic operation not a number or boolean."); |
764 | ||
765 | if (TYPE_CODE (type1) == TYPE_CODE_FLT | |
766 | || | |
767 | TYPE_CODE (type2) == TYPE_CODE_FLT) | |
768 | { | |
769 | /* FIXME-if-picky-about-floating-accuracy: Should be doing this | |
c5aa993b JM |
770 | in target format. real.c in GCC probably has the necessary |
771 | code. */ | |
c4093a6a | 772 | DOUBLEST v1, v2, v = 0; |
c906108c SS |
773 | v1 = value_as_double (arg1); |
774 | v2 = value_as_double (arg2); | |
775 | switch (op) | |
776 | { | |
777 | case BINOP_ADD: | |
778 | v = v1 + v2; | |
779 | break; | |
780 | ||
781 | case BINOP_SUB: | |
782 | v = v1 - v2; | |
783 | break; | |
784 | ||
785 | case BINOP_MUL: | |
786 | v = v1 * v2; | |
787 | break; | |
788 | ||
789 | case BINOP_DIV: | |
790 | v = v1 / v2; | |
791 | break; | |
792 | ||
c4093a6a JM |
793 | case BINOP_EXP: |
794 | v = pow (v1, v2); | |
795 | if (errno) | |
dc672865 | 796 | error ("Cannot perform exponentiation: %s", safe_strerror (errno)); |
c4093a6a JM |
797 | break; |
798 | ||
c906108c SS |
799 | default: |
800 | error ("Integer-only operation on floating point number."); | |
801 | } | |
802 | ||
803 | /* If either arg was long double, make sure that value is also long | |
c5aa993b | 804 | double. */ |
c906108c | 805 | |
c5aa993b JM |
806 | if (TYPE_LENGTH (type1) * 8 > TARGET_DOUBLE_BIT |
807 | || TYPE_LENGTH (type2) * 8 > TARGET_DOUBLE_BIT) | |
c906108c SS |
808 | val = allocate_value (builtin_type_long_double); |
809 | else | |
810 | val = allocate_value (builtin_type_double); | |
811 | ||
96d2f608 | 812 | store_typed_floating (VALUE_CONTENTS_RAW (val), VALUE_TYPE (val), v); |
c906108c SS |
813 | } |
814 | else if (TYPE_CODE (type1) == TYPE_CODE_BOOL | |
815 | && | |
816 | TYPE_CODE (type2) == TYPE_CODE_BOOL) | |
c5aa993b | 817 | { |
c4093a6a | 818 | LONGEST v1, v2, v = 0; |
c5aa993b JM |
819 | v1 = value_as_long (arg1); |
820 | v2 = value_as_long (arg2); | |
821 | ||
822 | switch (op) | |
823 | { | |
824 | case BINOP_BITWISE_AND: | |
825 | v = v1 & v2; | |
826 | break; | |
827 | ||
828 | case BINOP_BITWISE_IOR: | |
829 | v = v1 | v2; | |
830 | break; | |
831 | ||
832 | case BINOP_BITWISE_XOR: | |
833 | v = v1 ^ v2; | |
c4093a6a JM |
834 | break; |
835 | ||
836 | case BINOP_EQUAL: | |
837 | v = v1 == v2; | |
838 | break; | |
839 | ||
840 | case BINOP_NOTEQUAL: | |
841 | v = v1 != v2; | |
c5aa993b JM |
842 | break; |
843 | ||
844 | default: | |
845 | error ("Invalid operation on booleans."); | |
846 | } | |
847 | ||
848 | val = allocate_value (type1); | |
849 | store_signed_integer (VALUE_CONTENTS_RAW (val), | |
850 | TYPE_LENGTH (type1), | |
851 | v); | |
852 | } | |
c906108c SS |
853 | else |
854 | /* Integral operations here. */ | |
855 | /* FIXME: Also mixed integral/booleans, with result an integer. */ | |
856 | /* FIXME: This implements ANSI C rules (also correct for C++). | |
1b831c93 | 857 | What about FORTRAN and (the deleted) chill ? */ |
c906108c SS |
858 | { |
859 | unsigned int promoted_len1 = TYPE_LENGTH (type1); | |
860 | unsigned int promoted_len2 = TYPE_LENGTH (type2); | |
861 | int is_unsigned1 = TYPE_UNSIGNED (type1); | |
862 | int is_unsigned2 = TYPE_UNSIGNED (type2); | |
863 | unsigned int result_len; | |
864 | int unsigned_operation; | |
865 | ||
866 | /* Determine type length and signedness after promotion for | |
c5aa993b | 867 | both operands. */ |
c906108c SS |
868 | if (promoted_len1 < TYPE_LENGTH (builtin_type_int)) |
869 | { | |
870 | is_unsigned1 = 0; | |
871 | promoted_len1 = TYPE_LENGTH (builtin_type_int); | |
872 | } | |
873 | if (promoted_len2 < TYPE_LENGTH (builtin_type_int)) | |
874 | { | |
875 | is_unsigned2 = 0; | |
876 | promoted_len2 = TYPE_LENGTH (builtin_type_int); | |
877 | } | |
878 | ||
879 | /* Determine type length of the result, and if the operation should | |
c5aa993b JM |
880 | be done unsigned. |
881 | Use the signedness of the operand with the greater length. | |
882 | If both operands are of equal length, use unsigned operation | |
883 | if one of the operands is unsigned. */ | |
c906108c SS |
884 | if (promoted_len1 > promoted_len2) |
885 | { | |
886 | unsigned_operation = is_unsigned1; | |
887 | result_len = promoted_len1; | |
888 | } | |
889 | else if (promoted_len2 > promoted_len1) | |
890 | { | |
891 | unsigned_operation = is_unsigned2; | |
892 | result_len = promoted_len2; | |
893 | } | |
894 | else | |
895 | { | |
896 | unsigned_operation = is_unsigned1 || is_unsigned2; | |
897 | result_len = promoted_len1; | |
898 | } | |
899 | ||
900 | if (unsigned_operation) | |
901 | { | |
c4093a6a | 902 | ULONGEST v1, v2, v = 0; |
c906108c SS |
903 | v1 = (ULONGEST) value_as_long (arg1); |
904 | v2 = (ULONGEST) value_as_long (arg2); | |
905 | ||
906 | /* Truncate values to the type length of the result. */ | |
907 | if (result_len < sizeof (ULONGEST)) | |
908 | { | |
909 | v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1; | |
910 | v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1; | |
911 | } | |
c5aa993b | 912 | |
c906108c SS |
913 | switch (op) |
914 | { | |
915 | case BINOP_ADD: | |
916 | v = v1 + v2; | |
917 | break; | |
c5aa993b | 918 | |
c906108c SS |
919 | case BINOP_SUB: |
920 | v = v1 - v2; | |
921 | break; | |
c5aa993b | 922 | |
c906108c SS |
923 | case BINOP_MUL: |
924 | v = v1 * v2; | |
925 | break; | |
c5aa993b | 926 | |
c906108c SS |
927 | case BINOP_DIV: |
928 | v = v1 / v2; | |
929 | break; | |
c5aa993b | 930 | |
c4093a6a JM |
931 | case BINOP_EXP: |
932 | v = pow (v1, v2); | |
933 | if (errno) | |
dc672865 | 934 | error ("Cannot perform exponentiation: %s", safe_strerror (errno)); |
c4093a6a JM |
935 | break; |
936 | ||
c906108c SS |
937 | case BINOP_REM: |
938 | v = v1 % v2; | |
939 | break; | |
c5aa993b | 940 | |
c906108c SS |
941 | case BINOP_MOD: |
942 | /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, | |
943 | v1 mod 0 has a defined value, v1. */ | |
c906108c SS |
944 | if (v2 == 0) |
945 | { | |
946 | v = v1; | |
947 | } | |
948 | else | |
949 | { | |
c5aa993b | 950 | v = v1 / v2; |
c906108c SS |
951 | /* Note floor(v1/v2) == v1/v2 for unsigned. */ |
952 | v = v1 - (v2 * v); | |
953 | } | |
954 | break; | |
c5aa993b | 955 | |
c906108c SS |
956 | case BINOP_LSH: |
957 | v = v1 << v2; | |
958 | break; | |
c5aa993b | 959 | |
c906108c SS |
960 | case BINOP_RSH: |
961 | v = v1 >> v2; | |
962 | break; | |
c5aa993b | 963 | |
c906108c SS |
964 | case BINOP_BITWISE_AND: |
965 | v = v1 & v2; | |
966 | break; | |
c5aa993b | 967 | |
c906108c SS |
968 | case BINOP_BITWISE_IOR: |
969 | v = v1 | v2; | |
970 | break; | |
c5aa993b | 971 | |
c906108c SS |
972 | case BINOP_BITWISE_XOR: |
973 | v = v1 ^ v2; | |
974 | break; | |
c5aa993b | 975 | |
c906108c SS |
976 | case BINOP_LOGICAL_AND: |
977 | v = v1 && v2; | |
978 | break; | |
c5aa993b | 979 | |
c906108c SS |
980 | case BINOP_LOGICAL_OR: |
981 | v = v1 || v2; | |
982 | break; | |
c5aa993b | 983 | |
c906108c SS |
984 | case BINOP_MIN: |
985 | v = v1 < v2 ? v1 : v2; | |
986 | break; | |
c5aa993b | 987 | |
c906108c SS |
988 | case BINOP_MAX: |
989 | v = v1 > v2 ? v1 : v2; | |
990 | break; | |
991 | ||
992 | case BINOP_EQUAL: | |
993 | v = v1 == v2; | |
994 | break; | |
995 | ||
c4093a6a JM |
996 | case BINOP_NOTEQUAL: |
997 | v = v1 != v2; | |
998 | break; | |
999 | ||
c906108c SS |
1000 | case BINOP_LESS: |
1001 | v = v1 < v2; | |
1002 | break; | |
c5aa993b | 1003 | |
c906108c SS |
1004 | default: |
1005 | error ("Invalid binary operation on numbers."); | |
1006 | } | |
1007 | ||
1008 | /* This is a kludge to get around the fact that we don't | |
1009 | know how to determine the result type from the types of | |
1010 | the operands. (I'm not really sure how much we feel the | |
1011 | need to duplicate the exact rules of the current | |
1012 | language. They can get really hairy. But not to do so | |
1013 | makes it hard to document just what we *do* do). */ | |
1014 | ||
1015 | /* Can't just call init_type because we wouldn't know what | |
1016 | name to give the type. */ | |
1017 | val = allocate_value | |
1018 | (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT | |
1019 | ? builtin_type_unsigned_long_long | |
1020 | : builtin_type_unsigned_long); | |
1021 | store_unsigned_integer (VALUE_CONTENTS_RAW (val), | |
1022 | TYPE_LENGTH (VALUE_TYPE (val)), | |
1023 | v); | |
1024 | } | |
1025 | else | |
1026 | { | |
c4093a6a | 1027 | LONGEST v1, v2, v = 0; |
c906108c SS |
1028 | v1 = value_as_long (arg1); |
1029 | v2 = value_as_long (arg2); | |
c5aa993b | 1030 | |
c906108c SS |
1031 | switch (op) |
1032 | { | |
1033 | case BINOP_ADD: | |
1034 | v = v1 + v2; | |
1035 | break; | |
c5aa993b | 1036 | |
c906108c SS |
1037 | case BINOP_SUB: |
1038 | v = v1 - v2; | |
1039 | break; | |
c5aa993b | 1040 | |
c906108c SS |
1041 | case BINOP_MUL: |
1042 | v = v1 * v2; | |
1043 | break; | |
c5aa993b | 1044 | |
c906108c | 1045 | case BINOP_DIV: |
399cfac6 DL |
1046 | if (v2 != 0) |
1047 | v = v1 / v2; | |
1048 | else | |
1049 | error ("Division by zero"); | |
c4093a6a JM |
1050 | break; |
1051 | ||
1052 | case BINOP_EXP: | |
1053 | v = pow (v1, v2); | |
1054 | if (errno) | |
dc672865 | 1055 | error ("Cannot perform exponentiation: %s", safe_strerror (errno)); |
c906108c | 1056 | break; |
c5aa993b | 1057 | |
c906108c | 1058 | case BINOP_REM: |
399cfac6 DL |
1059 | if (v2 != 0) |
1060 | v = v1 % v2; | |
1061 | else | |
1062 | error ("Division by zero"); | |
c906108c | 1063 | break; |
c5aa993b | 1064 | |
c906108c SS |
1065 | case BINOP_MOD: |
1066 | /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, | |
1067 | X mod 0 has a defined value, X. */ | |
c906108c SS |
1068 | if (v2 == 0) |
1069 | { | |
1070 | v = v1; | |
1071 | } | |
1072 | else | |
1073 | { | |
c5aa993b | 1074 | v = v1 / v2; |
c906108c SS |
1075 | /* Compute floor. */ |
1076 | if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0)) | |
1077 | { | |
1078 | v--; | |
1079 | } | |
1080 | v = v1 - (v2 * v); | |
1081 | } | |
1082 | break; | |
c5aa993b | 1083 | |
c906108c SS |
1084 | case BINOP_LSH: |
1085 | v = v1 << v2; | |
1086 | break; | |
c5aa993b | 1087 | |
c906108c SS |
1088 | case BINOP_RSH: |
1089 | v = v1 >> v2; | |
1090 | break; | |
c5aa993b | 1091 | |
c906108c SS |
1092 | case BINOP_BITWISE_AND: |
1093 | v = v1 & v2; | |
1094 | break; | |
c5aa993b | 1095 | |
c906108c SS |
1096 | case BINOP_BITWISE_IOR: |
1097 | v = v1 | v2; | |
1098 | break; | |
c5aa993b | 1099 | |
c906108c SS |
1100 | case BINOP_BITWISE_XOR: |
1101 | v = v1 ^ v2; | |
1102 | break; | |
c5aa993b | 1103 | |
c906108c SS |
1104 | case BINOP_LOGICAL_AND: |
1105 | v = v1 && v2; | |
1106 | break; | |
c5aa993b | 1107 | |
c906108c SS |
1108 | case BINOP_LOGICAL_OR: |
1109 | v = v1 || v2; | |
1110 | break; | |
c5aa993b | 1111 | |
c906108c SS |
1112 | case BINOP_MIN: |
1113 | v = v1 < v2 ? v1 : v2; | |
1114 | break; | |
c5aa993b | 1115 | |
c906108c SS |
1116 | case BINOP_MAX: |
1117 | v = v1 > v2 ? v1 : v2; | |
1118 | break; | |
1119 | ||
1120 | case BINOP_EQUAL: | |
1121 | v = v1 == v2; | |
1122 | break; | |
1123 | ||
1124 | case BINOP_LESS: | |
1125 | v = v1 < v2; | |
1126 | break; | |
c5aa993b | 1127 | |
c906108c SS |
1128 | default: |
1129 | error ("Invalid binary operation on numbers."); | |
1130 | } | |
1131 | ||
1132 | /* This is a kludge to get around the fact that we don't | |
1133 | know how to determine the result type from the types of | |
1134 | the operands. (I'm not really sure how much we feel the | |
1135 | need to duplicate the exact rules of the current | |
1136 | language. They can get really hairy. But not to do so | |
1137 | makes it hard to document just what we *do* do). */ | |
1138 | ||
1139 | /* Can't just call init_type because we wouldn't know what | |
1140 | name to give the type. */ | |
1141 | val = allocate_value | |
1142 | (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT | |
1143 | ? builtin_type_long_long | |
1144 | : builtin_type_long); | |
1145 | store_signed_integer (VALUE_CONTENTS_RAW (val), | |
1146 | TYPE_LENGTH (VALUE_TYPE (val)), | |
1147 | v); | |
1148 | } | |
1149 | } | |
1150 | ||
1151 | return val; | |
1152 | } | |
1153 | \f | |
1154 | /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */ | |
1155 | ||
1156 | int | |
f23631e4 | 1157 | value_logical_not (struct value *arg1) |
c906108c | 1158 | { |
52f0bd74 AC |
1159 | int len; |
1160 | char *p; | |
c906108c SS |
1161 | struct type *type1; |
1162 | ||
1163 | COERCE_NUMBER (arg1); | |
1164 | type1 = check_typedef (VALUE_TYPE (arg1)); | |
1165 | ||
1166 | if (TYPE_CODE (type1) == TYPE_CODE_FLT) | |
1167 | return 0 == value_as_double (arg1); | |
1168 | ||
1169 | len = TYPE_LENGTH (type1); | |
1170 | p = VALUE_CONTENTS (arg1); | |
1171 | ||
1172 | while (--len >= 0) | |
1173 | { | |
1174 | if (*p++) | |
1175 | break; | |
1176 | } | |
1177 | ||
1178 | return len < 0; | |
1179 | } | |
1180 | ||
c4093a6a JM |
1181 | /* Perform a comparison on two string values (whose content are not |
1182 | necessarily null terminated) based on their length */ | |
1183 | ||
1184 | static int | |
f23631e4 | 1185 | value_strcmp (struct value *arg1, struct value *arg2) |
c4093a6a JM |
1186 | { |
1187 | int len1 = TYPE_LENGTH (VALUE_TYPE (arg1)); | |
1188 | int len2 = TYPE_LENGTH (VALUE_TYPE (arg2)); | |
1189 | char *s1 = VALUE_CONTENTS (arg1); | |
1190 | char *s2 = VALUE_CONTENTS (arg2); | |
1191 | int i, len = len1 < len2 ? len1 : len2; | |
1192 | ||
1193 | for (i = 0; i < len; i++) | |
1194 | { | |
1195 | if (s1[i] < s2[i]) | |
1196 | return -1; | |
1197 | else if (s1[i] > s2[i]) | |
1198 | return 1; | |
1199 | else | |
1200 | continue; | |
1201 | } | |
1202 | ||
1203 | if (len1 < len2) | |
1204 | return -1; | |
1205 | else if (len1 > len2) | |
1206 | return 1; | |
1207 | else | |
1208 | return 0; | |
1209 | } | |
1210 | ||
c906108c SS |
1211 | /* Simulate the C operator == by returning a 1 |
1212 | iff ARG1 and ARG2 have equal contents. */ | |
1213 | ||
1214 | int | |
f23631e4 | 1215 | value_equal (struct value *arg1, struct value *arg2) |
c906108c | 1216 | { |
52f0bd74 AC |
1217 | int len; |
1218 | char *p1, *p2; | |
c906108c SS |
1219 | struct type *type1, *type2; |
1220 | enum type_code code1; | |
1221 | enum type_code code2; | |
2de41bce | 1222 | int is_int1, is_int2; |
c906108c | 1223 | |
2de41bce PH |
1224 | COERCE_ARRAY (arg1); |
1225 | COERCE_ARRAY (arg2); | |
c906108c SS |
1226 | |
1227 | type1 = check_typedef (VALUE_TYPE (arg1)); | |
1228 | type2 = check_typedef (VALUE_TYPE (arg2)); | |
1229 | code1 = TYPE_CODE (type1); | |
1230 | code2 = TYPE_CODE (type2); | |
2de41bce PH |
1231 | is_int1 = is_integral_type (type1); |
1232 | is_int2 = is_integral_type (type2); | |
c906108c | 1233 | |
2de41bce | 1234 | if (is_int1 && is_int2) |
c906108c SS |
1235 | return longest_to_int (value_as_long (value_binop (arg1, arg2, |
1236 | BINOP_EQUAL))); | |
2de41bce PH |
1237 | else if ((code1 == TYPE_CODE_FLT || is_int1) |
1238 | && (code2 == TYPE_CODE_FLT || is_int2)) | |
c906108c SS |
1239 | return value_as_double (arg1) == value_as_double (arg2); |
1240 | ||
1241 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
1242 | is bigger. */ | |
2de41bce | 1243 | else if (code1 == TYPE_CODE_PTR && is_int2) |
1aa20aa8 | 1244 | return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2); |
2de41bce | 1245 | else if (code2 == TYPE_CODE_PTR && is_int1) |
1aa20aa8 | 1246 | return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2); |
c906108c SS |
1247 | |
1248 | else if (code1 == code2 | |
1249 | && ((len = (int) TYPE_LENGTH (type1)) | |
1250 | == (int) TYPE_LENGTH (type2))) | |
1251 | { | |
1252 | p1 = VALUE_CONTENTS (arg1); | |
1253 | p2 = VALUE_CONTENTS (arg2); | |
1254 | while (--len >= 0) | |
1255 | { | |
1256 | if (*p1++ != *p2++) | |
1257 | break; | |
1258 | } | |
1259 | return len < 0; | |
1260 | } | |
c4093a6a JM |
1261 | else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) |
1262 | { | |
1263 | return value_strcmp (arg1, arg2) == 0; | |
1264 | } | |
c906108c SS |
1265 | else |
1266 | { | |
1267 | error ("Invalid type combination in equality test."); | |
c5aa993b | 1268 | return 0; /* For lint -- never reached */ |
c906108c SS |
1269 | } |
1270 | } | |
1271 | ||
1272 | /* Simulate the C operator < by returning 1 | |
1273 | iff ARG1's contents are less than ARG2's. */ | |
1274 | ||
1275 | int | |
f23631e4 | 1276 | value_less (struct value *arg1, struct value *arg2) |
c906108c | 1277 | { |
52f0bd74 AC |
1278 | enum type_code code1; |
1279 | enum type_code code2; | |
c906108c | 1280 | struct type *type1, *type2; |
2de41bce | 1281 | int is_int1, is_int2; |
c906108c | 1282 | |
2de41bce PH |
1283 | COERCE_ARRAY (arg1); |
1284 | COERCE_ARRAY (arg2); | |
c906108c SS |
1285 | |
1286 | type1 = check_typedef (VALUE_TYPE (arg1)); | |
1287 | type2 = check_typedef (VALUE_TYPE (arg2)); | |
1288 | code1 = TYPE_CODE (type1); | |
1289 | code2 = TYPE_CODE (type2); | |
2de41bce PH |
1290 | is_int1 = is_integral_type (type1); |
1291 | is_int2 = is_integral_type (type2); | |
c906108c | 1292 | |
2de41bce | 1293 | if (is_int1 && is_int2) |
c906108c SS |
1294 | return longest_to_int (value_as_long (value_binop (arg1, arg2, |
1295 | BINOP_LESS))); | |
2de41bce PH |
1296 | else if ((code1 == TYPE_CODE_FLT || is_int1) |
1297 | && (code2 == TYPE_CODE_FLT || is_int2)) | |
c906108c SS |
1298 | return value_as_double (arg1) < value_as_double (arg2); |
1299 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) | |
1aa20aa8 | 1300 | return value_as_address (arg1) < value_as_address (arg2); |
c906108c SS |
1301 | |
1302 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
1303 | is bigger. */ | |
2de41bce | 1304 | else if (code1 == TYPE_CODE_PTR && is_int2) |
1aa20aa8 | 1305 | return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2); |
2de41bce | 1306 | else if (code2 == TYPE_CODE_PTR && is_int1) |
1aa20aa8 | 1307 | return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2); |
c4093a6a JM |
1308 | else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) |
1309 | return value_strcmp (arg1, arg2) < 0; | |
c906108c SS |
1310 | else |
1311 | { | |
1312 | error ("Invalid type combination in ordering comparison."); | |
1313 | return 0; | |
1314 | } | |
1315 | } | |
1316 | \f | |
1317 | /* The unary operators - and ~. Both free the argument ARG1. */ | |
1318 | ||
f23631e4 AC |
1319 | struct value * |
1320 | value_neg (struct value *arg1) | |
c906108c | 1321 | { |
52f0bd74 AC |
1322 | struct type *type; |
1323 | struct type *result_type = VALUE_TYPE (arg1); | |
c906108c SS |
1324 | |
1325 | COERCE_REF (arg1); | |
c906108c SS |
1326 | |
1327 | type = check_typedef (VALUE_TYPE (arg1)); | |
1328 | ||
1329 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
c5aa993b | 1330 | return value_from_double (result_type, -value_as_double (arg1)); |
2de41bce | 1331 | else if (is_integral_type (type)) |
c906108c | 1332 | { |
db034ac5 | 1333 | /* Perform integral promotion for ANSI C/C++. FIXME: What about |
1b831c93 | 1334 | FORTRAN and (the deleted) chill ? */ |
c906108c SS |
1335 | if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int)) |
1336 | result_type = builtin_type_int; | |
1337 | ||
c5aa993b JM |
1338 | return value_from_longest (result_type, -value_as_long (arg1)); |
1339 | } | |
1340 | else | |
1341 | { | |
1342 | error ("Argument to negate operation not a number."); | |
1343 | return 0; /* For lint -- never reached */ | |
c906108c | 1344 | } |
c906108c SS |
1345 | } |
1346 | ||
f23631e4 AC |
1347 | struct value * |
1348 | value_complement (struct value *arg1) | |
c906108c | 1349 | { |
52f0bd74 AC |
1350 | struct type *type; |
1351 | struct type *result_type = VALUE_TYPE (arg1); | |
c906108c SS |
1352 | |
1353 | COERCE_REF (arg1); | |
c906108c SS |
1354 | |
1355 | type = check_typedef (VALUE_TYPE (arg1)); | |
1356 | ||
2de41bce | 1357 | if (!is_integral_type (type)) |
c906108c SS |
1358 | error ("Argument to complement operation not an integer or boolean."); |
1359 | ||
1360 | /* Perform integral promotion for ANSI C/C++. | |
1361 | FIXME: What about FORTRAN ? */ | |
1362 | if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int)) | |
1363 | result_type = builtin_type_int; | |
1364 | ||
c5aa993b | 1365 | return value_from_longest (result_type, ~value_as_long (arg1)); |
c906108c SS |
1366 | } |
1367 | \f | |
1368 | /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE, | |
1369 | and whose VALUE_CONTENTS is valaddr. | |
1370 | Return -1 if out of range, -2 other error. */ | |
1371 | ||
1372 | int | |
fba45db2 | 1373 | value_bit_index (struct type *type, char *valaddr, int index) |
c906108c SS |
1374 | { |
1375 | LONGEST low_bound, high_bound; | |
1376 | LONGEST word; | |
1377 | unsigned rel_index; | |
1378 | struct type *range = TYPE_FIELD_TYPE (type, 0); | |
1379 | if (get_discrete_bounds (range, &low_bound, &high_bound) < 0) | |
1380 | return -2; | |
1381 | if (index < low_bound || index > high_bound) | |
1382 | return -1; | |
1383 | rel_index = index - low_bound; | |
1384 | word = unpack_long (builtin_type_unsigned_char, | |
1385 | valaddr + (rel_index / TARGET_CHAR_BIT)); | |
1386 | rel_index %= TARGET_CHAR_BIT; | |
1387 | if (BITS_BIG_ENDIAN) | |
1388 | rel_index = TARGET_CHAR_BIT - 1 - rel_index; | |
1389 | return (word >> rel_index) & 1; | |
1390 | } | |
1391 | ||
f23631e4 AC |
1392 | struct value * |
1393 | value_in (struct value *element, struct value *set) | |
c906108c SS |
1394 | { |
1395 | int member; | |
1396 | struct type *settype = check_typedef (VALUE_TYPE (set)); | |
1397 | struct type *eltype = check_typedef (VALUE_TYPE (element)); | |
1398 | if (TYPE_CODE (eltype) == TYPE_CODE_RANGE) | |
1399 | eltype = TYPE_TARGET_TYPE (eltype); | |
1400 | if (TYPE_CODE (settype) != TYPE_CODE_SET) | |
1401 | error ("Second argument of 'IN' has wrong type"); | |
1402 | if (TYPE_CODE (eltype) != TYPE_CODE_INT | |
1403 | && TYPE_CODE (eltype) != TYPE_CODE_CHAR | |
1404 | && TYPE_CODE (eltype) != TYPE_CODE_ENUM | |
1405 | && TYPE_CODE (eltype) != TYPE_CODE_BOOL) | |
1406 | error ("First argument of 'IN' has wrong type"); | |
1407 | member = value_bit_index (settype, VALUE_CONTENTS (set), | |
1408 | value_as_long (element)); | |
1409 | if (member < 0) | |
1410 | error ("First argument of 'IN' not in range"); | |
1411 | return value_from_longest (LA_BOOL_TYPE, member); | |
1412 | } | |
1413 | ||
1414 | void | |
fba45db2 | 1415 | _initialize_valarith (void) |
c906108c SS |
1416 | { |
1417 | } |