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