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]; | |
467 | int static_memfuncp; | |
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 | |
f23631e4 | 478 | argvec = (struct value **) alloca (sizeof (struct value *) * 3); |
c906108c SS |
479 | argvec[1] = value_addr (arg1); |
480 | argvec[2] = 0; | |
481 | ||
c5aa993b JM |
482 | /* make the right function name up */ |
483 | strcpy (tstr, "operator__"); | |
484 | ptr = tstr + 8; | |
485 | strcpy (mangle_tstr, "__"); | |
486 | mangle_ptr = mangle_tstr + 2; | |
c906108c SS |
487 | switch (op) |
488 | { | |
c5aa993b JM |
489 | case UNOP_PREINCREMENT: |
490 | strcpy (ptr, "++"); | |
491 | break; | |
492 | case UNOP_PREDECREMENT: | |
493 | strcpy (ptr, "++"); | |
494 | break; | |
495 | case UNOP_POSTINCREMENT: | |
496 | strcpy (ptr, "++"); | |
497 | break; | |
498 | case UNOP_POSTDECREMENT: | |
499 | strcpy (ptr, "++"); | |
500 | break; | |
501 | case UNOP_LOGICAL_NOT: | |
502 | strcpy (ptr, "!"); | |
503 | break; | |
504 | case UNOP_COMPLEMENT: | |
505 | strcpy (ptr, "~"); | |
506 | break; | |
507 | case UNOP_NEG: | |
508 | strcpy (ptr, "-"); | |
509 | break; | |
510 | case UNOP_IND: | |
511 | strcpy (ptr, "*"); | |
512 | break; | |
c906108c SS |
513 | default: |
514 | error ("Invalid unary operation specified."); | |
515 | } | |
516 | ||
c5aa993b | 517 | argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure"); |
c906108c SS |
518 | |
519 | if (argvec[0]) | |
520 | { | |
521 | if (static_memfuncp) | |
522 | { | |
523 | argvec[1] = argvec[0]; | |
524 | argvec++; | |
525 | } | |
526 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
527 | { | |
528 | struct type *return_type; | |
529 | return_type | |
530 | = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0]))); | |
531 | return value_zero (return_type, VALUE_LVAL (arg1)); | |
532 | } | |
533 | return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1); | |
534 | } | |
535 | error ("member function %s not found", tstr); | |
c5aa993b | 536 | return 0; /* For lint -- never reached */ |
c906108c | 537 | } |
c906108c | 538 | \f |
c5aa993b | 539 | |
c906108c SS |
540 | /* Concatenate two values with the following conditions: |
541 | ||
c5aa993b JM |
542 | (1) Both values must be either bitstring values or character string |
543 | values and the resulting value consists of the concatenation of | |
544 | ARG1 followed by ARG2. | |
c906108c | 545 | |
c5aa993b | 546 | or |
c906108c | 547 | |
c5aa993b JM |
548 | One value must be an integer value and the other value must be |
549 | either a bitstring value or character string value, which is | |
550 | to be repeated by the number of times specified by the integer | |
551 | value. | |
c906108c SS |
552 | |
553 | ||
c5aa993b JM |
554 | (2) Boolean values are also allowed and are treated as bit string |
555 | values of length 1. | |
c906108c | 556 | |
c5aa993b JM |
557 | (3) Character values are also allowed and are treated as character |
558 | string values of length 1. | |
559 | */ | |
c906108c | 560 | |
f23631e4 AC |
561 | struct value * |
562 | value_concat (struct value *arg1, struct value *arg2) | |
c906108c | 563 | { |
f23631e4 AC |
564 | struct value *inval1; |
565 | struct value *inval2; | |
566 | struct value *outval = NULL; | |
c906108c SS |
567 | int inval1len, inval2len; |
568 | int count, idx; | |
569 | char *ptr; | |
570 | char inchar; | |
571 | struct type *type1 = check_typedef (VALUE_TYPE (arg1)); | |
572 | struct type *type2 = check_typedef (VALUE_TYPE (arg2)); | |
573 | ||
574 | COERCE_VARYING_ARRAY (arg1, type1); | |
575 | COERCE_VARYING_ARRAY (arg2, type2); | |
576 | ||
577 | /* First figure out if we are dealing with two values to be concatenated | |
578 | or a repeat count and a value to be repeated. INVAL1 is set to the | |
579 | first of two concatenated values, or the repeat count. INVAL2 is set | |
580 | to the second of the two concatenated values or the value to be | |
581 | repeated. */ | |
582 | ||
583 | if (TYPE_CODE (type2) == TYPE_CODE_INT) | |
584 | { | |
585 | struct type *tmp = type1; | |
586 | type1 = tmp; | |
587 | tmp = type2; | |
588 | inval1 = arg2; | |
589 | inval2 = arg1; | |
590 | } | |
591 | else | |
592 | { | |
593 | inval1 = arg1; | |
594 | inval2 = arg2; | |
595 | } | |
596 | ||
597 | /* Now process the input values. */ | |
598 | ||
599 | if (TYPE_CODE (type1) == TYPE_CODE_INT) | |
600 | { | |
601 | /* We have a repeat count. Validate the second value and then | |
c5aa993b | 602 | construct a value repeated that many times. */ |
c906108c SS |
603 | if (TYPE_CODE (type2) == TYPE_CODE_STRING |
604 | || TYPE_CODE (type2) == TYPE_CODE_CHAR) | |
605 | { | |
606 | count = longest_to_int (value_as_long (inval1)); | |
607 | inval2len = TYPE_LENGTH (type2); | |
608 | ptr = (char *) alloca (count * inval2len); | |
609 | if (TYPE_CODE (type2) == TYPE_CODE_CHAR) | |
610 | { | |
611 | inchar = (char) unpack_long (type2, | |
612 | VALUE_CONTENTS (inval2)); | |
613 | for (idx = 0; idx < count; idx++) | |
614 | { | |
615 | *(ptr + idx) = inchar; | |
616 | } | |
617 | } | |
618 | else | |
619 | { | |
620 | for (idx = 0; idx < count; idx++) | |
621 | { | |
622 | memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2), | |
623 | inval2len); | |
624 | } | |
625 | } | |
626 | outval = value_string (ptr, count * inval2len); | |
627 | } | |
628 | else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING | |
629 | || TYPE_CODE (type2) == TYPE_CODE_BOOL) | |
630 | { | |
631 | error ("unimplemented support for bitstring/boolean repeats"); | |
632 | } | |
633 | else | |
634 | { | |
635 | error ("can't repeat values of that type"); | |
636 | } | |
637 | } | |
638 | else if (TYPE_CODE (type1) == TYPE_CODE_STRING | |
c5aa993b | 639 | || TYPE_CODE (type1) == TYPE_CODE_CHAR) |
c906108c SS |
640 | { |
641 | /* We have two character strings to concatenate. */ | |
642 | if (TYPE_CODE (type2) != TYPE_CODE_STRING | |
643 | && TYPE_CODE (type2) != TYPE_CODE_CHAR) | |
644 | { | |
645 | error ("Strings can only be concatenated with other strings."); | |
646 | } | |
647 | inval1len = TYPE_LENGTH (type1); | |
648 | inval2len = TYPE_LENGTH (type2); | |
649 | ptr = (char *) alloca (inval1len + inval2len); | |
650 | if (TYPE_CODE (type1) == TYPE_CODE_CHAR) | |
651 | { | |
652 | *ptr = (char) unpack_long (type1, VALUE_CONTENTS (inval1)); | |
653 | } | |
654 | else | |
655 | { | |
656 | memcpy (ptr, VALUE_CONTENTS (inval1), inval1len); | |
657 | } | |
658 | if (TYPE_CODE (type2) == TYPE_CODE_CHAR) | |
659 | { | |
c5aa993b | 660 | *(ptr + inval1len) = |
c906108c SS |
661 | (char) unpack_long (type2, VALUE_CONTENTS (inval2)); |
662 | } | |
663 | else | |
664 | { | |
665 | memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len); | |
666 | } | |
667 | outval = value_string (ptr, inval1len + inval2len); | |
668 | } | |
669 | else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING | |
670 | || TYPE_CODE (type1) == TYPE_CODE_BOOL) | |
671 | { | |
672 | /* We have two bitstrings to concatenate. */ | |
673 | if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING | |
674 | && TYPE_CODE (type2) != TYPE_CODE_BOOL) | |
675 | { | |
676 | error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."); | |
677 | } | |
678 | error ("unimplemented support for bitstring/boolean concatenation."); | |
c5aa993b | 679 | } |
c906108c SS |
680 | else |
681 | { | |
682 | /* We don't know how to concatenate these operands. */ | |
683 | error ("illegal operands for concatenation."); | |
684 | } | |
685 | return (outval); | |
686 | } | |
c906108c SS |
687 | \f |
688 | ||
c5aa993b | 689 | |
c906108c SS |
690 | /* Perform a binary operation on two operands which have reasonable |
691 | representations as integers or floats. This includes booleans, | |
692 | characters, integers, or floats. | |
693 | Does not support addition and subtraction on pointers; | |
694 | use value_add or value_sub if you want to handle those possibilities. */ | |
695 | ||
f23631e4 AC |
696 | struct value * |
697 | value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) | |
c906108c | 698 | { |
f23631e4 | 699 | struct value *val; |
c906108c SS |
700 | struct type *type1, *type2; |
701 | ||
702 | COERCE_REF (arg1); | |
703 | COERCE_REF (arg2); | |
704 | COERCE_ENUM (arg1); | |
705 | COERCE_ENUM (arg2); | |
706 | type1 = check_typedef (VALUE_TYPE (arg1)); | |
707 | type2 = check_typedef (VALUE_TYPE (arg2)); | |
708 | ||
709 | if ((TYPE_CODE (type1) != TYPE_CODE_FLT | |
710 | && TYPE_CODE (type1) != TYPE_CODE_CHAR | |
711 | && TYPE_CODE (type1) != TYPE_CODE_INT | |
712 | && TYPE_CODE (type1) != TYPE_CODE_BOOL | |
713 | && TYPE_CODE (type1) != TYPE_CODE_RANGE) | |
714 | || | |
715 | (TYPE_CODE (type2) != TYPE_CODE_FLT | |
716 | && TYPE_CODE (type2) != TYPE_CODE_CHAR | |
717 | && TYPE_CODE (type2) != TYPE_CODE_INT | |
718 | && TYPE_CODE (type2) != TYPE_CODE_BOOL | |
719 | && TYPE_CODE (type2) != TYPE_CODE_RANGE)) | |
720 | error ("Argument to arithmetic operation not a number or boolean."); | |
721 | ||
722 | if (TYPE_CODE (type1) == TYPE_CODE_FLT | |
723 | || | |
724 | TYPE_CODE (type2) == TYPE_CODE_FLT) | |
725 | { | |
726 | /* FIXME-if-picky-about-floating-accuracy: Should be doing this | |
c5aa993b JM |
727 | in target format. real.c in GCC probably has the necessary |
728 | code. */ | |
c4093a6a | 729 | DOUBLEST v1, v2, v = 0; |
c906108c SS |
730 | v1 = value_as_double (arg1); |
731 | v2 = value_as_double (arg2); | |
732 | switch (op) | |
733 | { | |
734 | case BINOP_ADD: | |
735 | v = v1 + v2; | |
736 | break; | |
737 | ||
738 | case BINOP_SUB: | |
739 | v = v1 - v2; | |
740 | break; | |
741 | ||
742 | case BINOP_MUL: | |
743 | v = v1 * v2; | |
744 | break; | |
745 | ||
746 | case BINOP_DIV: | |
747 | v = v1 / v2; | |
748 | break; | |
749 | ||
c4093a6a JM |
750 | case BINOP_EXP: |
751 | v = pow (v1, v2); | |
752 | if (errno) | |
753 | error ("Cannot perform exponentiation: %s", strerror (errno)); | |
754 | break; | |
755 | ||
c906108c SS |
756 | default: |
757 | error ("Integer-only operation on floating point number."); | |
758 | } | |
759 | ||
760 | /* If either arg was long double, make sure that value is also long | |
c5aa993b | 761 | double. */ |
c906108c | 762 | |
c5aa993b JM |
763 | if (TYPE_LENGTH (type1) * 8 > TARGET_DOUBLE_BIT |
764 | || TYPE_LENGTH (type2) * 8 > TARGET_DOUBLE_BIT) | |
c906108c SS |
765 | val = allocate_value (builtin_type_long_double); |
766 | else | |
767 | val = allocate_value (builtin_type_double); | |
768 | ||
96d2f608 | 769 | store_typed_floating (VALUE_CONTENTS_RAW (val), VALUE_TYPE (val), v); |
c906108c SS |
770 | } |
771 | else if (TYPE_CODE (type1) == TYPE_CODE_BOOL | |
772 | && | |
773 | TYPE_CODE (type2) == TYPE_CODE_BOOL) | |
c5aa993b | 774 | { |
c4093a6a | 775 | LONGEST v1, v2, v = 0; |
c5aa993b JM |
776 | v1 = value_as_long (arg1); |
777 | v2 = value_as_long (arg2); | |
778 | ||
779 | switch (op) | |
780 | { | |
781 | case BINOP_BITWISE_AND: | |
782 | v = v1 & v2; | |
783 | break; | |
784 | ||
785 | case BINOP_BITWISE_IOR: | |
786 | v = v1 | v2; | |
787 | break; | |
788 | ||
789 | case BINOP_BITWISE_XOR: | |
790 | v = v1 ^ v2; | |
c4093a6a JM |
791 | break; |
792 | ||
793 | case BINOP_EQUAL: | |
794 | v = v1 == v2; | |
795 | break; | |
796 | ||
797 | case BINOP_NOTEQUAL: | |
798 | v = v1 != v2; | |
c5aa993b JM |
799 | break; |
800 | ||
801 | default: | |
802 | error ("Invalid operation on booleans."); | |
803 | } | |
804 | ||
805 | val = allocate_value (type1); | |
806 | store_signed_integer (VALUE_CONTENTS_RAW (val), | |
807 | TYPE_LENGTH (type1), | |
808 | v); | |
809 | } | |
c906108c SS |
810 | else |
811 | /* Integral operations here. */ | |
812 | /* FIXME: Also mixed integral/booleans, with result an integer. */ | |
813 | /* FIXME: This implements ANSI C rules (also correct for C++). | |
814 | What about FORTRAN and chill? */ | |
815 | { | |
816 | unsigned int promoted_len1 = TYPE_LENGTH (type1); | |
817 | unsigned int promoted_len2 = TYPE_LENGTH (type2); | |
818 | int is_unsigned1 = TYPE_UNSIGNED (type1); | |
819 | int is_unsigned2 = TYPE_UNSIGNED (type2); | |
820 | unsigned int result_len; | |
821 | int unsigned_operation; | |
822 | ||
823 | /* Determine type length and signedness after promotion for | |
c5aa993b | 824 | both operands. */ |
c906108c SS |
825 | if (promoted_len1 < TYPE_LENGTH (builtin_type_int)) |
826 | { | |
827 | is_unsigned1 = 0; | |
828 | promoted_len1 = TYPE_LENGTH (builtin_type_int); | |
829 | } | |
830 | if (promoted_len2 < TYPE_LENGTH (builtin_type_int)) | |
831 | { | |
832 | is_unsigned2 = 0; | |
833 | promoted_len2 = TYPE_LENGTH (builtin_type_int); | |
834 | } | |
835 | ||
836 | /* Determine type length of the result, and if the operation should | |
c5aa993b JM |
837 | be done unsigned. |
838 | Use the signedness of the operand with the greater length. | |
839 | If both operands are of equal length, use unsigned operation | |
840 | if one of the operands is unsigned. */ | |
c906108c SS |
841 | if (promoted_len1 > promoted_len2) |
842 | { | |
843 | unsigned_operation = is_unsigned1; | |
844 | result_len = promoted_len1; | |
845 | } | |
846 | else if (promoted_len2 > promoted_len1) | |
847 | { | |
848 | unsigned_operation = is_unsigned2; | |
849 | result_len = promoted_len2; | |
850 | } | |
851 | else | |
852 | { | |
853 | unsigned_operation = is_unsigned1 || is_unsigned2; | |
854 | result_len = promoted_len1; | |
855 | } | |
856 | ||
857 | if (unsigned_operation) | |
858 | { | |
c4093a6a | 859 | ULONGEST v1, v2, v = 0; |
c906108c SS |
860 | v1 = (ULONGEST) value_as_long (arg1); |
861 | v2 = (ULONGEST) value_as_long (arg2); | |
862 | ||
863 | /* Truncate values to the type length of the result. */ | |
864 | if (result_len < sizeof (ULONGEST)) | |
865 | { | |
866 | v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1; | |
867 | v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1; | |
868 | } | |
c5aa993b | 869 | |
c906108c SS |
870 | switch (op) |
871 | { | |
872 | case BINOP_ADD: | |
873 | v = v1 + v2; | |
874 | break; | |
c5aa993b | 875 | |
c906108c SS |
876 | case BINOP_SUB: |
877 | v = v1 - v2; | |
878 | break; | |
c5aa993b | 879 | |
c906108c SS |
880 | case BINOP_MUL: |
881 | v = v1 * v2; | |
882 | break; | |
c5aa993b | 883 | |
c906108c SS |
884 | case BINOP_DIV: |
885 | v = v1 / v2; | |
886 | break; | |
c5aa993b | 887 | |
c4093a6a JM |
888 | case BINOP_EXP: |
889 | v = pow (v1, v2); | |
890 | if (errno) | |
891 | error ("Cannot perform exponentiation: %s", strerror (errno)); | |
892 | break; | |
893 | ||
c906108c SS |
894 | case BINOP_REM: |
895 | v = v1 % v2; | |
896 | break; | |
c5aa993b | 897 | |
c906108c SS |
898 | case BINOP_MOD: |
899 | /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, | |
900 | v1 mod 0 has a defined value, v1. */ | |
901 | /* Chill specifies that v2 must be > 0, so check for that. */ | |
c5aa993b | 902 | if (current_language->la_language == language_chill |
c906108c SS |
903 | && value_as_long (arg2) <= 0) |
904 | { | |
905 | error ("Second operand of MOD must be greater than zero."); | |
906 | } | |
907 | if (v2 == 0) | |
908 | { | |
909 | v = v1; | |
910 | } | |
911 | else | |
912 | { | |
c5aa993b | 913 | v = v1 / v2; |
c906108c SS |
914 | /* Note floor(v1/v2) == v1/v2 for unsigned. */ |
915 | v = v1 - (v2 * v); | |
916 | } | |
917 | break; | |
c5aa993b | 918 | |
c906108c SS |
919 | case BINOP_LSH: |
920 | v = v1 << v2; | |
921 | break; | |
c5aa993b | 922 | |
c906108c SS |
923 | case BINOP_RSH: |
924 | v = v1 >> v2; | |
925 | break; | |
c5aa993b | 926 | |
c906108c SS |
927 | case BINOP_BITWISE_AND: |
928 | v = v1 & v2; | |
929 | break; | |
c5aa993b | 930 | |
c906108c SS |
931 | case BINOP_BITWISE_IOR: |
932 | v = v1 | v2; | |
933 | break; | |
c5aa993b | 934 | |
c906108c SS |
935 | case BINOP_BITWISE_XOR: |
936 | v = v1 ^ v2; | |
937 | break; | |
c5aa993b | 938 | |
c906108c SS |
939 | case BINOP_LOGICAL_AND: |
940 | v = v1 && v2; | |
941 | break; | |
c5aa993b | 942 | |
c906108c SS |
943 | case BINOP_LOGICAL_OR: |
944 | v = v1 || v2; | |
945 | break; | |
c5aa993b | 946 | |
c906108c SS |
947 | case BINOP_MIN: |
948 | v = v1 < v2 ? v1 : v2; | |
949 | break; | |
c5aa993b | 950 | |
c906108c SS |
951 | case BINOP_MAX: |
952 | v = v1 > v2 ? v1 : v2; | |
953 | break; | |
954 | ||
955 | case BINOP_EQUAL: | |
956 | v = v1 == v2; | |
957 | break; | |
958 | ||
c4093a6a JM |
959 | case BINOP_NOTEQUAL: |
960 | v = v1 != v2; | |
961 | break; | |
962 | ||
c906108c SS |
963 | case BINOP_LESS: |
964 | v = v1 < v2; | |
965 | break; | |
c5aa993b | 966 | |
c906108c SS |
967 | default: |
968 | error ("Invalid binary operation on numbers."); | |
969 | } | |
970 | ||
971 | /* This is a kludge to get around the fact that we don't | |
972 | know how to determine the result type from the types of | |
973 | the operands. (I'm not really sure how much we feel the | |
974 | need to duplicate the exact rules of the current | |
975 | language. They can get really hairy. But not to do so | |
976 | makes it hard to document just what we *do* do). */ | |
977 | ||
978 | /* Can't just call init_type because we wouldn't know what | |
979 | name to give the type. */ | |
980 | val = allocate_value | |
981 | (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT | |
982 | ? builtin_type_unsigned_long_long | |
983 | : builtin_type_unsigned_long); | |
984 | store_unsigned_integer (VALUE_CONTENTS_RAW (val), | |
985 | TYPE_LENGTH (VALUE_TYPE (val)), | |
986 | v); | |
987 | } | |
988 | else | |
989 | { | |
c4093a6a | 990 | LONGEST v1, v2, v = 0; |
c906108c SS |
991 | v1 = value_as_long (arg1); |
992 | v2 = value_as_long (arg2); | |
c5aa993b | 993 | |
c906108c SS |
994 | switch (op) |
995 | { | |
996 | case BINOP_ADD: | |
997 | v = v1 + v2; | |
998 | break; | |
c5aa993b | 999 | |
c906108c SS |
1000 | case BINOP_SUB: |
1001 | v = v1 - v2; | |
1002 | break; | |
c5aa993b | 1003 | |
c906108c SS |
1004 | case BINOP_MUL: |
1005 | v = v1 * v2; | |
1006 | break; | |
c5aa993b | 1007 | |
c906108c SS |
1008 | case BINOP_DIV: |
1009 | v = v1 / v2; | |
c4093a6a JM |
1010 | break; |
1011 | ||
1012 | case BINOP_EXP: | |
1013 | v = pow (v1, v2); | |
1014 | if (errno) | |
1015 | error ("Cannot perform exponentiation: %s", strerror (errno)); | |
c906108c | 1016 | break; |
c5aa993b | 1017 | |
c906108c SS |
1018 | case BINOP_REM: |
1019 | v = v1 % v2; | |
1020 | break; | |
c5aa993b | 1021 | |
c906108c SS |
1022 | case BINOP_MOD: |
1023 | /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, | |
1024 | X mod 0 has a defined value, X. */ | |
1025 | /* Chill specifies that v2 must be > 0, so check for that. */ | |
c5aa993b | 1026 | if (current_language->la_language == language_chill |
c906108c SS |
1027 | && v2 <= 0) |
1028 | { | |
1029 | error ("Second operand of MOD must be greater than zero."); | |
1030 | } | |
1031 | if (v2 == 0) | |
1032 | { | |
1033 | v = v1; | |
1034 | } | |
1035 | else | |
1036 | { | |
c5aa993b | 1037 | v = v1 / v2; |
c906108c SS |
1038 | /* Compute floor. */ |
1039 | if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0)) | |
1040 | { | |
1041 | v--; | |
1042 | } | |
1043 | v = v1 - (v2 * v); | |
1044 | } | |
1045 | break; | |
c5aa993b | 1046 | |
c906108c SS |
1047 | case BINOP_LSH: |
1048 | v = v1 << v2; | |
1049 | break; | |
c5aa993b | 1050 | |
c906108c SS |
1051 | case BINOP_RSH: |
1052 | v = v1 >> v2; | |
1053 | break; | |
c5aa993b | 1054 | |
c906108c SS |
1055 | case BINOP_BITWISE_AND: |
1056 | v = v1 & v2; | |
1057 | break; | |
c5aa993b | 1058 | |
c906108c SS |
1059 | case BINOP_BITWISE_IOR: |
1060 | v = v1 | v2; | |
1061 | break; | |
c5aa993b | 1062 | |
c906108c SS |
1063 | case BINOP_BITWISE_XOR: |
1064 | v = v1 ^ v2; | |
1065 | break; | |
c5aa993b | 1066 | |
c906108c SS |
1067 | case BINOP_LOGICAL_AND: |
1068 | v = v1 && v2; | |
1069 | break; | |
c5aa993b | 1070 | |
c906108c SS |
1071 | case BINOP_LOGICAL_OR: |
1072 | v = v1 || v2; | |
1073 | break; | |
c5aa993b | 1074 | |
c906108c SS |
1075 | case BINOP_MIN: |
1076 | v = v1 < v2 ? v1 : v2; | |
1077 | break; | |
c5aa993b | 1078 | |
c906108c SS |
1079 | case BINOP_MAX: |
1080 | v = v1 > v2 ? v1 : v2; | |
1081 | break; | |
1082 | ||
1083 | case BINOP_EQUAL: | |
1084 | v = v1 == v2; | |
1085 | break; | |
1086 | ||
1087 | case BINOP_LESS: | |
1088 | v = v1 < v2; | |
1089 | break; | |
c5aa993b | 1090 | |
c906108c SS |
1091 | default: |
1092 | error ("Invalid binary operation on numbers."); | |
1093 | } | |
1094 | ||
1095 | /* This is a kludge to get around the fact that we don't | |
1096 | know how to determine the result type from the types of | |
1097 | the operands. (I'm not really sure how much we feel the | |
1098 | need to duplicate the exact rules of the current | |
1099 | language. They can get really hairy. But not to do so | |
1100 | makes it hard to document just what we *do* do). */ | |
1101 | ||
1102 | /* Can't just call init_type because we wouldn't know what | |
1103 | name to give the type. */ | |
1104 | val = allocate_value | |
1105 | (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT | |
1106 | ? builtin_type_long_long | |
1107 | : builtin_type_long); | |
1108 | store_signed_integer (VALUE_CONTENTS_RAW (val), | |
1109 | TYPE_LENGTH (VALUE_TYPE (val)), | |
1110 | v); | |
1111 | } | |
1112 | } | |
1113 | ||
1114 | return val; | |
1115 | } | |
1116 | \f | |
1117 | /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */ | |
1118 | ||
1119 | int | |
f23631e4 | 1120 | value_logical_not (struct value *arg1) |
c906108c SS |
1121 | { |
1122 | register int len; | |
1123 | register char *p; | |
1124 | struct type *type1; | |
1125 | ||
1126 | COERCE_NUMBER (arg1); | |
1127 | type1 = check_typedef (VALUE_TYPE (arg1)); | |
1128 | ||
1129 | if (TYPE_CODE (type1) == TYPE_CODE_FLT) | |
1130 | return 0 == value_as_double (arg1); | |
1131 | ||
1132 | len = TYPE_LENGTH (type1); | |
1133 | p = VALUE_CONTENTS (arg1); | |
1134 | ||
1135 | while (--len >= 0) | |
1136 | { | |
1137 | if (*p++) | |
1138 | break; | |
1139 | } | |
1140 | ||
1141 | return len < 0; | |
1142 | } | |
1143 | ||
c4093a6a JM |
1144 | /* Perform a comparison on two string values (whose content are not |
1145 | necessarily null terminated) based on their length */ | |
1146 | ||
1147 | static int | |
f23631e4 | 1148 | value_strcmp (struct value *arg1, struct value *arg2) |
c4093a6a JM |
1149 | { |
1150 | int len1 = TYPE_LENGTH (VALUE_TYPE (arg1)); | |
1151 | int len2 = TYPE_LENGTH (VALUE_TYPE (arg2)); | |
1152 | char *s1 = VALUE_CONTENTS (arg1); | |
1153 | char *s2 = VALUE_CONTENTS (arg2); | |
1154 | int i, len = len1 < len2 ? len1 : len2; | |
1155 | ||
1156 | for (i = 0; i < len; i++) | |
1157 | { | |
1158 | if (s1[i] < s2[i]) | |
1159 | return -1; | |
1160 | else if (s1[i] > s2[i]) | |
1161 | return 1; | |
1162 | else | |
1163 | continue; | |
1164 | } | |
1165 | ||
1166 | if (len1 < len2) | |
1167 | return -1; | |
1168 | else if (len1 > len2) | |
1169 | return 1; | |
1170 | else | |
1171 | return 0; | |
1172 | } | |
1173 | ||
c906108c SS |
1174 | /* Simulate the C operator == by returning a 1 |
1175 | iff ARG1 and ARG2 have equal contents. */ | |
1176 | ||
1177 | int | |
f23631e4 | 1178 | value_equal (struct value *arg1, struct value *arg2) |
c906108c SS |
1179 | { |
1180 | register int len; | |
1181 | register char *p1, *p2; | |
1182 | struct type *type1, *type2; | |
1183 | enum type_code code1; | |
1184 | enum type_code code2; | |
1185 | ||
1186 | COERCE_NUMBER (arg1); | |
1187 | COERCE_NUMBER (arg2); | |
1188 | ||
1189 | type1 = check_typedef (VALUE_TYPE (arg1)); | |
1190 | type2 = check_typedef (VALUE_TYPE (arg2)); | |
1191 | code1 = TYPE_CODE (type1); | |
1192 | code2 = TYPE_CODE (type2); | |
1193 | ||
1194 | if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) && | |
1195 | (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL)) | |
1196 | return longest_to_int (value_as_long (value_binop (arg1, arg2, | |
1197 | BINOP_EQUAL))); | |
1198 | else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) | |
1199 | && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL)) | |
1200 | return value_as_double (arg1) == value_as_double (arg2); | |
1201 | ||
1202 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
1203 | is bigger. */ | |
1204 | else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL)) | |
1aa20aa8 | 1205 | return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2); |
c906108c | 1206 | else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)) |
1aa20aa8 | 1207 | return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2); |
c906108c SS |
1208 | |
1209 | else if (code1 == code2 | |
1210 | && ((len = (int) TYPE_LENGTH (type1)) | |
1211 | == (int) TYPE_LENGTH (type2))) | |
1212 | { | |
1213 | p1 = VALUE_CONTENTS (arg1); | |
1214 | p2 = VALUE_CONTENTS (arg2); | |
1215 | while (--len >= 0) | |
1216 | { | |
1217 | if (*p1++ != *p2++) | |
1218 | break; | |
1219 | } | |
1220 | return len < 0; | |
1221 | } | |
c4093a6a JM |
1222 | else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) |
1223 | { | |
1224 | return value_strcmp (arg1, arg2) == 0; | |
1225 | } | |
c906108c SS |
1226 | else |
1227 | { | |
1228 | error ("Invalid type combination in equality test."); | |
c5aa993b | 1229 | return 0; /* For lint -- never reached */ |
c906108c SS |
1230 | } |
1231 | } | |
1232 | ||
1233 | /* Simulate the C operator < by returning 1 | |
1234 | iff ARG1's contents are less than ARG2's. */ | |
1235 | ||
1236 | int | |
f23631e4 | 1237 | value_less (struct value *arg1, struct value *arg2) |
c906108c SS |
1238 | { |
1239 | register enum type_code code1; | |
1240 | register enum type_code code2; | |
1241 | struct type *type1, *type2; | |
1242 | ||
1243 | COERCE_NUMBER (arg1); | |
1244 | COERCE_NUMBER (arg2); | |
1245 | ||
1246 | type1 = check_typedef (VALUE_TYPE (arg1)); | |
1247 | type2 = check_typedef (VALUE_TYPE (arg2)); | |
1248 | code1 = TYPE_CODE (type1); | |
1249 | code2 = TYPE_CODE (type2); | |
1250 | ||
1251 | if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) && | |
1252 | (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL)) | |
1253 | return longest_to_int (value_as_long (value_binop (arg1, arg2, | |
1254 | BINOP_LESS))); | |
1255 | else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) | |
1256 | && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL)) | |
1257 | return value_as_double (arg1) < value_as_double (arg2); | |
1258 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) | |
1aa20aa8 | 1259 | return value_as_address (arg1) < value_as_address (arg2); |
c906108c SS |
1260 | |
1261 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
1262 | is bigger. */ | |
1263 | else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL)) | |
1aa20aa8 | 1264 | return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2); |
c906108c | 1265 | else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)) |
1aa20aa8 | 1266 | return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2); |
c4093a6a JM |
1267 | else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) |
1268 | return value_strcmp (arg1, arg2) < 0; | |
c906108c SS |
1269 | else |
1270 | { | |
1271 | error ("Invalid type combination in ordering comparison."); | |
1272 | return 0; | |
1273 | } | |
1274 | } | |
1275 | \f | |
1276 | /* The unary operators - and ~. Both free the argument ARG1. */ | |
1277 | ||
f23631e4 AC |
1278 | struct value * |
1279 | value_neg (struct value *arg1) | |
c906108c SS |
1280 | { |
1281 | register struct type *type; | |
1282 | register struct type *result_type = VALUE_TYPE (arg1); | |
1283 | ||
1284 | COERCE_REF (arg1); | |
1285 | COERCE_ENUM (arg1); | |
1286 | ||
1287 | type = check_typedef (VALUE_TYPE (arg1)); | |
1288 | ||
1289 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
c5aa993b | 1290 | return value_from_double (result_type, -value_as_double (arg1)); |
c906108c SS |
1291 | else if (TYPE_CODE (type) == TYPE_CODE_INT || TYPE_CODE (type) == TYPE_CODE_BOOL) |
1292 | { | |
1293 | /* Perform integral promotion for ANSI C/C++. | |
c5aa993b | 1294 | FIXME: What about FORTRAN and chill ? */ |
c906108c SS |
1295 | if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int)) |
1296 | result_type = builtin_type_int; | |
1297 | ||
c5aa993b JM |
1298 | return value_from_longest (result_type, -value_as_long (arg1)); |
1299 | } | |
1300 | else | |
1301 | { | |
1302 | error ("Argument to negate operation not a number."); | |
1303 | return 0; /* For lint -- never reached */ | |
c906108c | 1304 | } |
c906108c SS |
1305 | } |
1306 | ||
f23631e4 AC |
1307 | struct value * |
1308 | value_complement (struct value *arg1) | |
c906108c SS |
1309 | { |
1310 | register struct type *type; | |
1311 | register struct type *result_type = VALUE_TYPE (arg1); | |
c5aa993b | 1312 | int typecode; |
c906108c SS |
1313 | |
1314 | COERCE_REF (arg1); | |
1315 | COERCE_ENUM (arg1); | |
1316 | ||
1317 | type = check_typedef (VALUE_TYPE (arg1)); | |
1318 | ||
1319 | typecode = TYPE_CODE (type); | |
1320 | if ((typecode != TYPE_CODE_INT) && (typecode != TYPE_CODE_BOOL)) | |
1321 | error ("Argument to complement operation not an integer or boolean."); | |
1322 | ||
1323 | /* Perform integral promotion for ANSI C/C++. | |
1324 | FIXME: What about FORTRAN ? */ | |
1325 | if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int)) | |
1326 | result_type = builtin_type_int; | |
1327 | ||
c5aa993b | 1328 | return value_from_longest (result_type, ~value_as_long (arg1)); |
c906108c SS |
1329 | } |
1330 | \f | |
1331 | /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE, | |
1332 | and whose VALUE_CONTENTS is valaddr. | |
1333 | Return -1 if out of range, -2 other error. */ | |
1334 | ||
1335 | int | |
fba45db2 | 1336 | value_bit_index (struct type *type, char *valaddr, int index) |
c906108c SS |
1337 | { |
1338 | LONGEST low_bound, high_bound; | |
1339 | LONGEST word; | |
1340 | unsigned rel_index; | |
1341 | struct type *range = TYPE_FIELD_TYPE (type, 0); | |
1342 | if (get_discrete_bounds (range, &low_bound, &high_bound) < 0) | |
1343 | return -2; | |
1344 | if (index < low_bound || index > high_bound) | |
1345 | return -1; | |
1346 | rel_index = index - low_bound; | |
1347 | word = unpack_long (builtin_type_unsigned_char, | |
1348 | valaddr + (rel_index / TARGET_CHAR_BIT)); | |
1349 | rel_index %= TARGET_CHAR_BIT; | |
1350 | if (BITS_BIG_ENDIAN) | |
1351 | rel_index = TARGET_CHAR_BIT - 1 - rel_index; | |
1352 | return (word >> rel_index) & 1; | |
1353 | } | |
1354 | ||
f23631e4 AC |
1355 | struct value * |
1356 | value_in (struct value *element, struct value *set) | |
c906108c SS |
1357 | { |
1358 | int member; | |
1359 | struct type *settype = check_typedef (VALUE_TYPE (set)); | |
1360 | struct type *eltype = check_typedef (VALUE_TYPE (element)); | |
1361 | if (TYPE_CODE (eltype) == TYPE_CODE_RANGE) | |
1362 | eltype = TYPE_TARGET_TYPE (eltype); | |
1363 | if (TYPE_CODE (settype) != TYPE_CODE_SET) | |
1364 | error ("Second argument of 'IN' has wrong type"); | |
1365 | if (TYPE_CODE (eltype) != TYPE_CODE_INT | |
1366 | && TYPE_CODE (eltype) != TYPE_CODE_CHAR | |
1367 | && TYPE_CODE (eltype) != TYPE_CODE_ENUM | |
1368 | && TYPE_CODE (eltype) != TYPE_CODE_BOOL) | |
1369 | error ("First argument of 'IN' has wrong type"); | |
1370 | member = value_bit_index (settype, VALUE_CONTENTS (set), | |
1371 | value_as_long (element)); | |
1372 | if (member < 0) | |
1373 | error ("First argument of 'IN' not in range"); | |
1374 | return value_from_longest (LA_BOOL_TYPE, member); | |
1375 | } | |
1376 | ||
1377 | void | |
fba45db2 | 1378 | _initialize_valarith (void) |
c906108c SS |
1379 | { |
1380 | } |