Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* Perform arithmetic and other operations on values, for GDB. |
e17960fb | 2 | Copyright 1986, 1989, 1991, 1992 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
088c3a0b | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
088c3a0b JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
088c3a0b | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
088c3a0b JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
bd5635a1 | 20 | #include "defs.h" |
bd5635a1 | 21 | #include "value.h" |
088c3a0b | 22 | #include "symtab.h" |
51b57ded | 23 | #include "gdbtypes.h" |
bd5635a1 RP |
24 | #include "expression.h" |
25 | #include "target.h" | |
26 | #include <string.h> | |
27 | ||
088c3a0b JG |
28 | static value |
29 | value_subscripted_rvalue PARAMS ((value, value)); | |
bd5635a1 | 30 | |
088c3a0b | 31 | \f |
bd5635a1 RP |
32 | value |
33 | value_add (arg1, arg2) | |
34 | value arg1, arg2; | |
35 | { | |
088c3a0b | 36 | register value valint, valptr; |
bd5635a1 RP |
37 | register int len; |
38 | ||
39 | COERCE_ARRAY (arg1); | |
40 | COERCE_ARRAY (arg2); | |
41 | ||
42 | if ((TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR | |
43 | || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_PTR) | |
44 | && | |
45 | (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT | |
46 | || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT)) | |
47 | /* Exactly one argument is a pointer, and one is an integer. */ | |
48 | { | |
49 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR) | |
50 | { | |
51 | valptr = arg1; | |
52 | valint = arg2; | |
53 | } | |
54 | else | |
55 | { | |
56 | valptr = arg2; | |
57 | valint = arg1; | |
58 | } | |
59 | len = TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (valptr))); | |
60 | if (len == 0) len = 1; /* For (void *) */ | |
088c3a0b JG |
61 | return value_from_longest (VALUE_TYPE (valptr), |
62 | value_as_long (valptr) | |
63 | + (len * value_as_long (valint))); | |
bd5635a1 RP |
64 | } |
65 | ||
66 | return value_binop (arg1, arg2, BINOP_ADD); | |
67 | } | |
68 | ||
69 | value | |
70 | value_sub (arg1, arg2) | |
71 | value arg1, arg2; | |
72 | { | |
bd5635a1 RP |
73 | |
74 | COERCE_ARRAY (arg1); | |
75 | COERCE_ARRAY (arg2); | |
76 | ||
77 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR) | |
78 | { | |
79 | if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT) | |
80 | { | |
81 | /* pointer - integer. */ | |
088c3a0b JG |
82 | return value_from_longest |
83 | (VALUE_TYPE (arg1), | |
bd5635a1 RP |
84 | value_as_long (arg1) |
85 | - (TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) | |
86 | * value_as_long (arg2))); | |
bd5635a1 RP |
87 | } |
88 | else if (VALUE_TYPE (arg1) == VALUE_TYPE (arg2)) | |
89 | { | |
90 | /* pointer to <type x> - pointer to <type x>. */ | |
088c3a0b JG |
91 | return value_from_longest |
92 | (builtin_type_long, /* FIXME -- should be ptrdiff_t */ | |
bd5635a1 RP |
93 | (value_as_long (arg1) - value_as_long (arg2)) |
94 | / TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)))); | |
bd5635a1 RP |
95 | } |
96 | else | |
97 | { | |
98 | error ("\ | |
99 | First argument of `-' is a pointer and second argument is neither\n\ | |
100 | an integer nor a pointer of the same type."); | |
101 | } | |
102 | } | |
103 | ||
104 | return value_binop (arg1, arg2, BINOP_SUB); | |
105 | } | |
106 | ||
fb6e675f FF |
107 | /* Return the value of ARRAY[IDX]. |
108 | See comments in value_coerce_array() for rationale for reason for | |
109 | doing lower bounds adjustment here rather than there. | |
110 | FIXME: Perhaps we should validate that the index is valid and if | |
111 | verbosity is set, warn about invalid indices (but still use them). */ | |
bd5635a1 RP |
112 | |
113 | value | |
114 | value_subscript (array, idx) | |
115 | value array, idx; | |
116 | { | |
fb6e675f FF |
117 | int lowerbound; |
118 | value bound; | |
119 | struct type *range_type; | |
120 | ||
121 | if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_ARRAY) | |
122 | { | |
123 | range_type = TYPE_FIELD_TYPE (VALUE_TYPE (array), 0); | |
124 | lowerbound = TYPE_FIELD_BITPOS (range_type, 0); | |
125 | if (lowerbound != 0) | |
126 | { | |
127 | bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound); | |
128 | idx = value_sub (idx, bound); | |
129 | } | |
130 | if (VALUE_LVAL (array) != lval_memory) | |
131 | { | |
132 | return value_subscripted_rvalue (array, idx); | |
133 | } | |
134 | } | |
135 | return value_ind (value_add (array, idx)); | |
bd5635a1 RP |
136 | } |
137 | ||
138 | /* Return the value of EXPR[IDX], expr an aggregate rvalue | |
139 | (eg, a vector register). This routine used to promote floats | |
140 | to doubles, but no longer does. */ | |
141 | ||
088c3a0b | 142 | static value |
bd5635a1 RP |
143 | value_subscripted_rvalue (array, idx) |
144 | value array, idx; | |
145 | { | |
146 | struct type *elt_type = TYPE_TARGET_TYPE (VALUE_TYPE (array)); | |
147 | int elt_size = TYPE_LENGTH (elt_type); | |
088c3a0b | 148 | int elt_offs = elt_size * longest_to_int (value_as_long (idx)); |
bd5635a1 RP |
149 | value v; |
150 | ||
151 | if (elt_offs >= TYPE_LENGTH (VALUE_TYPE (array))) | |
152 | error ("no such vector element"); | |
153 | ||
154 | v = allocate_value (elt_type); | |
e58de8a2 | 155 | memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size); |
bd5635a1 RP |
156 | |
157 | if (VALUE_LVAL (array) == lval_internalvar) | |
158 | VALUE_LVAL (v) = lval_internalvar_component; | |
159 | else | |
160 | VALUE_LVAL (v) = not_lval; | |
161 | VALUE_ADDRESS (v) = VALUE_ADDRESS (array); | |
162 | VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs; | |
163 | VALUE_BITSIZE (v) = elt_size * 8; | |
164 | return v; | |
165 | } | |
166 | \f | |
167 | /* Check to see if either argument is a structure. This is called so | |
168 | we know whether to go ahead with the normal binop or look for a | |
169 | user defined function instead. | |
170 | ||
171 | For now, we do not overload the `=' operator. */ | |
172 | ||
173 | int | |
174 | binop_user_defined_p (op, arg1, arg2) | |
175 | enum exp_opcode op; | |
176 | value arg1, arg2; | |
177 | { | |
178 | if (op == BINOP_ASSIGN) | |
179 | return 0; | |
180 | return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT | |
181 | || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_STRUCT | |
182 | || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF | |
183 | && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT) | |
184 | || (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_REF | |
185 | && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_STRUCT)); | |
186 | } | |
187 | ||
188 | /* Check to see if argument is a structure. This is called so | |
189 | we know whether to go ahead with the normal unop or look for a | |
190 | user defined function instead. | |
191 | ||
192 | For now, we do not overload the `&' operator. */ | |
193 | ||
194 | int unop_user_defined_p (op, arg1) | |
195 | enum exp_opcode op; | |
196 | value arg1; | |
197 | { | |
198 | if (op == UNOP_ADDR) | |
199 | return 0; | |
200 | return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT | |
201 | || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF | |
202 | && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT)); | |
203 | } | |
204 | ||
205 | /* We know either arg1 or arg2 is a structure, so try to find the right | |
206 | user defined function. Create an argument vector that calls | |
207 | arg1.operator @ (arg1,arg2) and return that value (where '@' is any | |
088c3a0b JG |
208 | binary operator which is legal for GNU C++). |
209 | ||
210 | OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP | |
211 | is the opcode saying how to modify it. Otherwise, OTHEROP is | |
212 | unused. */ | |
bd5635a1 RP |
213 | |
214 | value | |
215 | value_x_binop (arg1, arg2, op, otherop) | |
216 | value arg1, arg2; | |
217 | enum exp_opcode op, otherop; | |
218 | { | |
219 | value * argvec; | |
220 | char *ptr; | |
221 | char tstr[13]; | |
222 | int static_memfuncp; | |
223 | ||
088c3a0b JG |
224 | COERCE_REF (arg1); |
225 | COERCE_REF (arg2); | |
bd5635a1 RP |
226 | COERCE_ENUM (arg1); |
227 | COERCE_ENUM (arg2); | |
228 | ||
229 | /* now we know that what we have to do is construct our | |
230 | arg vector and find the right function to call it with. */ | |
231 | ||
232 | if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT) | |
233 | error ("Can't do that binary op on that type"); /* FIXME be explicit */ | |
234 | ||
235 | argvec = (value *) alloca (sizeof (value) * 4); | |
236 | argvec[1] = value_addr (arg1); | |
237 | argvec[2] = arg2; | |
238 | argvec[3] = 0; | |
239 | ||
240 | /* make the right function name up */ | |
241 | strcpy(tstr, "operator__"); | |
242 | ptr = tstr+8; | |
243 | switch (op) | |
244 | { | |
e58de8a2 FF |
245 | case BINOP_ADD: strcpy(ptr,"+"); break; |
246 | case BINOP_SUB: strcpy(ptr,"-"); break; | |
247 | case BINOP_MUL: strcpy(ptr,"*"); break; | |
248 | case BINOP_DIV: strcpy(ptr,"/"); break; | |
249 | case BINOP_REM: strcpy(ptr,"%"); break; | |
250 | case BINOP_LSH: strcpy(ptr,"<<"); break; | |
251 | case BINOP_RSH: strcpy(ptr,">>"); break; | |
252 | case BINOP_BITWISE_AND: strcpy(ptr,"&"); break; | |
253 | case BINOP_BITWISE_IOR: strcpy(ptr,"|"); break; | |
254 | case BINOP_BITWISE_XOR: strcpy(ptr,"^"); break; | |
255 | case BINOP_LOGICAL_AND: strcpy(ptr,"&&"); break; | |
256 | case BINOP_LOGICAL_OR: strcpy(ptr,"||"); break; | |
257 | case BINOP_MIN: strcpy(ptr,"<?"); break; | |
258 | case BINOP_MAX: strcpy(ptr,">?"); break; | |
259 | case BINOP_ASSIGN: strcpy(ptr,"="); break; | |
bd5635a1 RP |
260 | case BINOP_ASSIGN_MODIFY: |
261 | switch (otherop) | |
262 | { | |
e58de8a2 FF |
263 | case BINOP_ADD: strcpy(ptr,"+="); break; |
264 | case BINOP_SUB: strcpy(ptr,"-="); break; | |
265 | case BINOP_MUL: strcpy(ptr,"*="); break; | |
266 | case BINOP_DIV: strcpy(ptr,"/="); break; | |
267 | case BINOP_REM: strcpy(ptr,"%="); break; | |
268 | case BINOP_BITWISE_AND: strcpy(ptr,"&="); break; | |
269 | case BINOP_BITWISE_IOR: strcpy(ptr,"|="); break; | |
270 | case BINOP_BITWISE_XOR: strcpy(ptr,"^="); break; | |
bd5635a1 RP |
271 | default: |
272 | error ("Invalid binary operation specified."); | |
273 | } | |
274 | break; | |
275 | case BINOP_SUBSCRIPT: strcpy(ptr,"[]"); break; | |
276 | case BINOP_EQUAL: strcpy(ptr,"=="); break; | |
277 | case BINOP_NOTEQUAL: strcpy(ptr,"!="); break; | |
278 | case BINOP_LESS: strcpy(ptr,"<"); break; | |
279 | case BINOP_GTR: strcpy(ptr,">"); break; | |
280 | case BINOP_GEQ: strcpy(ptr,">="); break; | |
281 | case BINOP_LEQ: strcpy(ptr,"<="); break; | |
282 | default: | |
283 | error ("Invalid binary operation specified."); | |
284 | } | |
285 | argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure"); | |
286 | if (argvec[0]) | |
287 | { | |
288 | if (static_memfuncp) | |
289 | { | |
290 | argvec[1] = argvec[0]; | |
291 | argvec++; | |
292 | } | |
e17960fb | 293 | return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); |
bd5635a1 RP |
294 | } |
295 | error ("member function %s not found", tstr); | |
296 | #ifdef lint | |
e17960fb | 297 | return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); |
bd5635a1 RP |
298 | #endif |
299 | } | |
300 | ||
301 | /* We know that arg1 is a structure, so try to find a unary user | |
302 | defined operator that matches the operator in question. | |
303 | Create an argument vector that calls arg1.operator @ (arg1) | |
304 | and return that value (where '@' is (almost) any unary operator which | |
305 | is legal for GNU C++). */ | |
306 | ||
307 | value | |
308 | value_x_unop (arg1, op) | |
309 | value arg1; | |
310 | enum exp_opcode op; | |
311 | { | |
312 | value * argvec; | |
313 | char *ptr; | |
314 | char tstr[13]; | |
315 | int static_memfuncp; | |
316 | ||
317 | COERCE_ENUM (arg1); | |
318 | ||
319 | /* now we know that what we have to do is construct our | |
320 | arg vector and find the right function to call it with. */ | |
321 | ||
322 | if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT) | |
323 | error ("Can't do that unary op on that type"); /* FIXME be explicit */ | |
324 | ||
325 | argvec = (value *) alloca (sizeof (value) * 3); | |
326 | argvec[1] = value_addr (arg1); | |
327 | argvec[2] = 0; | |
328 | ||
329 | /* make the right function name up */ | |
330 | strcpy(tstr,"operator__"); | |
331 | ptr = tstr+8; | |
332 | switch (op) | |
333 | { | |
334 | case UNOP_PREINCREMENT: strcpy(ptr,"++"); break; | |
335 | case UNOP_PREDECREMENT: strcpy(ptr,"++"); break; | |
336 | case UNOP_POSTINCREMENT: strcpy(ptr,"++"); break; | |
337 | case UNOP_POSTDECREMENT: strcpy(ptr,"++"); break; | |
e58de8a2 FF |
338 | case UNOP_LOGICAL_NOT: strcpy(ptr,"!"); break; |
339 | case UNOP_COMPLEMENT: strcpy(ptr,"~"); break; | |
340 | case UNOP_NEG: strcpy(ptr,"-"); break; | |
bd5635a1 RP |
341 | default: |
342 | error ("Invalid binary operation specified."); | |
343 | } | |
344 | argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure"); | |
345 | if (argvec[0]) | |
346 | { | |
347 | if (static_memfuncp) | |
348 | { | |
349 | argvec[1] = argvec[0]; | |
350 | argvec++; | |
351 | } | |
e17960fb | 352 | return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1); |
bd5635a1 RP |
353 | } |
354 | error ("member function %s not found", tstr); | |
355 | return 0; /* For lint -- never reached */ | |
356 | } | |
357 | \f | |
358 | /* Perform a binary operation on two integers or two floats. | |
359 | Does not support addition and subtraction on pointers; | |
360 | use value_add or value_sub if you want to handle those possibilities. */ | |
361 | ||
362 | value | |
363 | value_binop (arg1, arg2, op) | |
364 | value arg1, arg2; | |
088c3a0b | 365 | enum exp_opcode op; |
bd5635a1 RP |
366 | { |
367 | register value val; | |
368 | ||
369 | COERCE_ENUM (arg1); | |
370 | COERCE_ENUM (arg2); | |
371 | ||
372 | if ((TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_FLT | |
373 | && | |
e58de8a2 FF |
374 | TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT |
375 | && | |
376 | TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_BOOL) | |
bd5635a1 RP |
377 | || |
378 | (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_FLT | |
379 | && | |
e58de8a2 FF |
380 | TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT |
381 | && | |
382 | TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_BOOL)) | |
383 | error ("Argument to arithmetic operation not a number or boolean."); | |
bd5635a1 RP |
384 | |
385 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT | |
386 | || | |
387 | TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_FLT) | |
388 | { | |
389 | double v1, v2, v; | |
390 | v1 = value_as_double (arg1); | |
391 | v2 = value_as_double (arg2); | |
392 | switch (op) | |
393 | { | |
394 | case BINOP_ADD: | |
395 | v = v1 + v2; | |
396 | break; | |
397 | ||
398 | case BINOP_SUB: | |
399 | v = v1 - v2; | |
400 | break; | |
401 | ||
402 | case BINOP_MUL: | |
403 | v = v1 * v2; | |
404 | break; | |
405 | ||
406 | case BINOP_DIV: | |
407 | v = v1 / v2; | |
408 | break; | |
409 | ||
410 | default: | |
411 | error ("Integer-only operation on floating point number."); | |
412 | } | |
413 | ||
414 | val = allocate_value (builtin_type_double); | |
415 | SWAP_TARGET_AND_HOST (&v, sizeof (v)); | |
416 | *(double *) VALUE_CONTENTS_RAW (val) = v; | |
417 | } | |
e58de8a2 FF |
418 | else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_BOOL |
419 | && | |
420 | TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_BOOL) | |
421 | { | |
422 | LONGEST v1, v2, v; | |
423 | v1 = value_as_long (arg1); | |
424 | v2 = value_as_long (arg2); | |
425 | ||
426 | switch (op) | |
427 | { | |
428 | case BINOP_BITWISE_AND: | |
429 | v = v1 & v2; | |
430 | break; | |
431 | ||
432 | case BINOP_BITWISE_IOR: | |
433 | v = v1 | v2; | |
434 | break; | |
435 | ||
436 | case BINOP_BITWISE_XOR: | |
437 | v = v1 ^ v2; | |
438 | break; | |
439 | ||
440 | default: | |
441 | error ("Invalid operation on booleans."); | |
442 | } | |
443 | ||
fb6e675f | 444 | /* start-sanitize-chill (FIXME!) */ |
e58de8a2 | 445 | val = allocate_value (builtin_type_chill_bool); |
fb6e675f | 446 | /* end-sanitize-chill */ |
e58de8a2 FF |
447 | SWAP_TARGET_AND_HOST (&v, sizeof (v)); |
448 | *(LONGEST *) VALUE_CONTENTS_RAW (val) = v; | |
449 | } | |
bd5635a1 RP |
450 | else |
451 | /* Integral operations here. */ | |
e58de8a2 | 452 | /* FIXME: Also mixed integral/booleans, with result an integer. */ |
bd5635a1 RP |
453 | { |
454 | /* Should we promote to unsigned longest? */ | |
455 | if ((TYPE_UNSIGNED (VALUE_TYPE (arg1)) | |
456 | || TYPE_UNSIGNED (VALUE_TYPE (arg2))) | |
457 | && (TYPE_LENGTH (VALUE_TYPE (arg1)) >= sizeof (unsigned LONGEST) | |
458 | || TYPE_LENGTH (VALUE_TYPE (arg1)) >= sizeof (unsigned LONGEST))) | |
459 | { | |
460 | unsigned LONGEST v1, v2, v; | |
461 | v1 = (unsigned LONGEST) value_as_long (arg1); | |
462 | v2 = (unsigned LONGEST) value_as_long (arg2); | |
463 | ||
464 | switch (op) | |
465 | { | |
466 | case BINOP_ADD: | |
467 | v = v1 + v2; | |
468 | break; | |
469 | ||
470 | case BINOP_SUB: | |
471 | v = v1 - v2; | |
472 | break; | |
473 | ||
474 | case BINOP_MUL: | |
475 | v = v1 * v2; | |
476 | break; | |
477 | ||
478 | case BINOP_DIV: | |
479 | v = v1 / v2; | |
480 | break; | |
481 | ||
482 | case BINOP_REM: | |
483 | v = v1 % v2; | |
484 | break; | |
485 | ||
486 | case BINOP_LSH: | |
487 | v = v1 << v2; | |
488 | break; | |
489 | ||
490 | case BINOP_RSH: | |
491 | v = v1 >> v2; | |
492 | break; | |
493 | ||
e58de8a2 | 494 | case BINOP_BITWISE_AND: |
bd5635a1 RP |
495 | v = v1 & v2; |
496 | break; | |
497 | ||
e58de8a2 | 498 | case BINOP_BITWISE_IOR: |
bd5635a1 RP |
499 | v = v1 | v2; |
500 | break; | |
501 | ||
e58de8a2 | 502 | case BINOP_BITWISE_XOR: |
bd5635a1 RP |
503 | v = v1 ^ v2; |
504 | break; | |
505 | ||
e58de8a2 | 506 | case BINOP_LOGICAL_AND: |
bd5635a1 RP |
507 | v = v1 && v2; |
508 | break; | |
509 | ||
e58de8a2 | 510 | case BINOP_LOGICAL_OR: |
bd5635a1 RP |
511 | v = v1 || v2; |
512 | break; | |
513 | ||
514 | case BINOP_MIN: | |
515 | v = v1 < v2 ? v1 : v2; | |
516 | break; | |
517 | ||
518 | case BINOP_MAX: | |
519 | v = v1 > v2 ? v1 : v2; | |
520 | break; | |
521 | ||
522 | default: | |
523 | error ("Invalid binary operation on numbers."); | |
524 | } | |
525 | ||
526 | val = allocate_value (BUILTIN_TYPE_UNSIGNED_LONGEST); | |
527 | SWAP_TARGET_AND_HOST (&v, sizeof (v)); | |
528 | *(unsigned LONGEST *) VALUE_CONTENTS_RAW (val) = v; | |
529 | } | |
530 | else | |
531 | { | |
532 | LONGEST v1, v2, v; | |
533 | v1 = value_as_long (arg1); | |
534 | v2 = value_as_long (arg2); | |
535 | ||
536 | switch (op) | |
537 | { | |
538 | case BINOP_ADD: | |
539 | v = v1 + v2; | |
540 | break; | |
541 | ||
542 | case BINOP_SUB: | |
543 | v = v1 - v2; | |
544 | break; | |
545 | ||
546 | case BINOP_MUL: | |
547 | v = v1 * v2; | |
548 | break; | |
549 | ||
550 | case BINOP_DIV: | |
551 | v = v1 / v2; | |
552 | break; | |
553 | ||
554 | case BINOP_REM: | |
555 | v = v1 % v2; | |
556 | break; | |
557 | ||
558 | case BINOP_LSH: | |
559 | v = v1 << v2; | |
560 | break; | |
561 | ||
562 | case BINOP_RSH: | |
563 | v = v1 >> v2; | |
564 | break; | |
565 | ||
e58de8a2 | 566 | case BINOP_BITWISE_AND: |
bd5635a1 RP |
567 | v = v1 & v2; |
568 | break; | |
569 | ||
e58de8a2 | 570 | case BINOP_BITWISE_IOR: |
bd5635a1 RP |
571 | v = v1 | v2; |
572 | break; | |
573 | ||
e58de8a2 | 574 | case BINOP_BITWISE_XOR: |
bd5635a1 RP |
575 | v = v1 ^ v2; |
576 | break; | |
577 | ||
e58de8a2 | 578 | case BINOP_LOGICAL_AND: |
bd5635a1 RP |
579 | v = v1 && v2; |
580 | break; | |
581 | ||
e58de8a2 | 582 | case BINOP_LOGICAL_OR: |
bd5635a1 RP |
583 | v = v1 || v2; |
584 | break; | |
585 | ||
586 | case BINOP_MIN: | |
587 | v = v1 < v2 ? v1 : v2; | |
588 | break; | |
589 | ||
590 | case BINOP_MAX: | |
591 | v = v1 > v2 ? v1 : v2; | |
592 | break; | |
593 | ||
594 | default: | |
595 | error ("Invalid binary operation on numbers."); | |
596 | } | |
597 | ||
598 | val = allocate_value (BUILTIN_TYPE_LONGEST); | |
599 | SWAP_TARGET_AND_HOST (&v, sizeof (v)); | |
600 | *(LONGEST *) VALUE_CONTENTS_RAW (val) = v; | |
601 | } | |
602 | } | |
603 | ||
604 | return val; | |
605 | } | |
606 | \f | |
51b57ded | 607 | /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */ |
bd5635a1 RP |
608 | |
609 | int | |
e58de8a2 | 610 | value_logical_not (arg1) |
bd5635a1 RP |
611 | value arg1; |
612 | { | |
613 | register int len; | |
614 | register char *p; | |
615 | ||
616 | COERCE_ARRAY (arg1); | |
617 | ||
51b57ded FF |
618 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT) |
619 | return 0 == value_as_double (arg1); | |
620 | ||
bd5635a1 RP |
621 | len = TYPE_LENGTH (VALUE_TYPE (arg1)); |
622 | p = VALUE_CONTENTS (arg1); | |
623 | ||
624 | while (--len >= 0) | |
625 | { | |
626 | if (*p++) | |
627 | break; | |
628 | } | |
629 | ||
630 | return len < 0; | |
631 | } | |
632 | ||
633 | /* Simulate the C operator == by returning a 1 | |
634 | iff ARG1 and ARG2 have equal contents. */ | |
635 | ||
636 | int | |
637 | value_equal (arg1, arg2) | |
638 | register value arg1, arg2; | |
639 | ||
640 | { | |
641 | register int len; | |
642 | register char *p1, *p2; | |
643 | enum type_code code1; | |
644 | enum type_code code2; | |
645 | ||
646 | COERCE_ARRAY (arg1); | |
647 | COERCE_ARRAY (arg2); | |
648 | ||
649 | code1 = TYPE_CODE (VALUE_TYPE (arg1)); | |
650 | code2 = TYPE_CODE (VALUE_TYPE (arg2)); | |
651 | ||
652 | if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT) | |
653 | return value_as_long (arg1) == value_as_long (arg2); | |
654 | else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT) | |
655 | && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT)) | |
656 | return value_as_double (arg1) == value_as_double (arg2); | |
088c3a0b JG |
657 | |
658 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
659 | is bigger. */ | |
660 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT) | |
661 | return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2); | |
662 | else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT) | |
663 | return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2); | |
664 | ||
bd5635a1 RP |
665 | else if (code1 == code2 |
666 | && ((len = TYPE_LENGTH (VALUE_TYPE (arg1))) | |
667 | == TYPE_LENGTH (VALUE_TYPE (arg2)))) | |
668 | { | |
669 | p1 = VALUE_CONTENTS (arg1); | |
670 | p2 = VALUE_CONTENTS (arg2); | |
671 | while (--len >= 0) | |
672 | { | |
673 | if (*p1++ != *p2++) | |
674 | break; | |
675 | } | |
676 | return len < 0; | |
677 | } | |
678 | else | |
679 | { | |
680 | error ("Invalid type combination in equality test."); | |
681 | return 0; /* For lint -- never reached */ | |
682 | } | |
683 | } | |
684 | ||
685 | /* Simulate the C operator < by returning 1 | |
686 | iff ARG1's contents are less than ARG2's. */ | |
687 | ||
688 | int | |
689 | value_less (arg1, arg2) | |
690 | register value arg1, arg2; | |
691 | { | |
692 | register enum type_code code1; | |
693 | register enum type_code code2; | |
694 | ||
695 | COERCE_ARRAY (arg1); | |
696 | COERCE_ARRAY (arg2); | |
697 | ||
698 | code1 = TYPE_CODE (VALUE_TYPE (arg1)); | |
699 | code2 = TYPE_CODE (VALUE_TYPE (arg2)); | |
700 | ||
701 | if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT) | |
702 | { | |
703 | if (TYPE_UNSIGNED (VALUE_TYPE (arg1)) | |
704 | || TYPE_UNSIGNED (VALUE_TYPE (arg2))) | |
088c3a0b JG |
705 | return ((unsigned LONGEST) value_as_long (arg1) |
706 | < (unsigned LONGEST) value_as_long (arg2)); | |
bd5635a1 RP |
707 | else |
708 | return value_as_long (arg1) < value_as_long (arg2); | |
709 | } | |
710 | else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT) | |
711 | && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT)) | |
712 | return value_as_double (arg1) < value_as_double (arg2); | |
088c3a0b JG |
713 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) |
714 | return value_as_pointer (arg1) < value_as_pointer (arg2); | |
715 | ||
716 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
717 | is bigger. */ | |
718 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT) | |
719 | return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2); | |
720 | else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT) | |
721 | return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2); | |
722 | ||
bd5635a1 RP |
723 | else |
724 | { | |
725 | error ("Invalid type combination in ordering comparison."); | |
726 | return 0; | |
727 | } | |
728 | } | |
729 | \f | |
730 | /* The unary operators - and ~. Both free the argument ARG1. */ | |
731 | ||
732 | value | |
733 | value_neg (arg1) | |
734 | register value arg1; | |
735 | { | |
736 | register struct type *type; | |
737 | ||
738 | COERCE_ENUM (arg1); | |
739 | ||
740 | type = VALUE_TYPE (arg1); | |
741 | ||
742 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
743 | return value_from_double (type, - value_as_double (arg1)); | |
744 | else if (TYPE_CODE (type) == TYPE_CODE_INT) | |
088c3a0b | 745 | return value_from_longest (type, - value_as_long (arg1)); |
bd5635a1 RP |
746 | else { |
747 | error ("Argument to negate operation not a number."); | |
748 | return 0; /* For lint -- never reached */ | |
749 | } | |
750 | } | |
751 | ||
752 | value | |
e58de8a2 | 753 | value_complement (arg1) |
bd5635a1 RP |
754 | register value arg1; |
755 | { | |
756 | COERCE_ENUM (arg1); | |
757 | ||
758 | if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT) | |
759 | error ("Argument to complement operation not an integer."); | |
760 | ||
088c3a0b | 761 | return value_from_longest (VALUE_TYPE (arg1), ~ value_as_long (arg1)); |
bd5635a1 RP |
762 | } |
763 | \f |