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