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 | ||
107 | /* Return the value of ARRAY[IDX]. */ | |
108 | ||
109 | value | |
110 | value_subscript (array, idx) | |
111 | value array, idx; | |
112 | { | |
113 | if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_ARRAY | |
114 | && VALUE_LVAL (array) != lval_memory) | |
115 | return value_subscripted_rvalue (array, idx); | |
116 | else | |
117 | return value_ind (value_add (array, idx)); | |
118 | } | |
119 | ||
120 | /* Return the value of EXPR[IDX], expr an aggregate rvalue | |
121 | (eg, a vector register). This routine used to promote floats | |
122 | to doubles, but no longer does. */ | |
123 | ||
088c3a0b | 124 | static value |
bd5635a1 RP |
125 | value_subscripted_rvalue (array, idx) |
126 | value array, idx; | |
127 | { | |
128 | struct type *elt_type = TYPE_TARGET_TYPE (VALUE_TYPE (array)); | |
129 | int elt_size = TYPE_LENGTH (elt_type); | |
088c3a0b | 130 | int elt_offs = elt_size * longest_to_int (value_as_long (idx)); |
bd5635a1 RP |
131 | value v; |
132 | ||
133 | if (elt_offs >= TYPE_LENGTH (VALUE_TYPE (array))) | |
134 | error ("no such vector element"); | |
135 | ||
136 | v = allocate_value (elt_type); | |
e58de8a2 | 137 | memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size); |
bd5635a1 RP |
138 | |
139 | if (VALUE_LVAL (array) == lval_internalvar) | |
140 | VALUE_LVAL (v) = lval_internalvar_component; | |
141 | else | |
142 | VALUE_LVAL (v) = not_lval; | |
143 | VALUE_ADDRESS (v) = VALUE_ADDRESS (array); | |
144 | VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs; | |
145 | VALUE_BITSIZE (v) = elt_size * 8; | |
146 | return v; | |
147 | } | |
148 | \f | |
149 | /* Check to see if either argument is a structure. This is called so | |
150 | we know whether to go ahead with the normal binop or look for a | |
151 | user defined function instead. | |
152 | ||
153 | For now, we do not overload the `=' operator. */ | |
154 | ||
155 | int | |
156 | binop_user_defined_p (op, arg1, arg2) | |
157 | enum exp_opcode op; | |
158 | value arg1, arg2; | |
159 | { | |
160 | if (op == BINOP_ASSIGN) | |
161 | return 0; | |
162 | return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT | |
163 | || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_STRUCT | |
164 | || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF | |
165 | && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT) | |
166 | || (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_REF | |
167 | && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_STRUCT)); | |
168 | } | |
169 | ||
170 | /* Check to see if argument is a structure. This is called so | |
171 | we know whether to go ahead with the normal unop or look for a | |
172 | user defined function instead. | |
173 | ||
174 | For now, we do not overload the `&' operator. */ | |
175 | ||
176 | int unop_user_defined_p (op, arg1) | |
177 | enum exp_opcode op; | |
178 | value arg1; | |
179 | { | |
180 | if (op == UNOP_ADDR) | |
181 | return 0; | |
182 | return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT | |
183 | || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF | |
184 | && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT)); | |
185 | } | |
186 | ||
187 | /* We know either arg1 or arg2 is a structure, so try to find the right | |
188 | user defined function. Create an argument vector that calls | |
189 | arg1.operator @ (arg1,arg2) and return that value (where '@' is any | |
088c3a0b JG |
190 | binary operator which is legal for GNU C++). |
191 | ||
192 | OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP | |
193 | is the opcode saying how to modify it. Otherwise, OTHEROP is | |
194 | unused. */ | |
bd5635a1 RP |
195 | |
196 | value | |
197 | value_x_binop (arg1, arg2, op, otherop) | |
198 | value arg1, arg2; | |
199 | enum exp_opcode op, otherop; | |
200 | { | |
201 | value * argvec; | |
202 | char *ptr; | |
203 | char tstr[13]; | |
204 | int static_memfuncp; | |
205 | ||
088c3a0b JG |
206 | COERCE_REF (arg1); |
207 | COERCE_REF (arg2); | |
bd5635a1 RP |
208 | COERCE_ENUM (arg1); |
209 | COERCE_ENUM (arg2); | |
210 | ||
211 | /* now we know that what we have to do is construct our | |
212 | arg vector and find the right function to call it with. */ | |
213 | ||
214 | if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT) | |
215 | error ("Can't do that binary op on that type"); /* FIXME be explicit */ | |
216 | ||
217 | argvec = (value *) alloca (sizeof (value) * 4); | |
218 | argvec[1] = value_addr (arg1); | |
219 | argvec[2] = arg2; | |
220 | argvec[3] = 0; | |
221 | ||
222 | /* make the right function name up */ | |
223 | strcpy(tstr, "operator__"); | |
224 | ptr = tstr+8; | |
225 | switch (op) | |
226 | { | |
e58de8a2 FF |
227 | case BINOP_ADD: strcpy(ptr,"+"); break; |
228 | case BINOP_SUB: strcpy(ptr,"-"); break; | |
229 | case BINOP_MUL: strcpy(ptr,"*"); break; | |
230 | case BINOP_DIV: strcpy(ptr,"/"); break; | |
231 | case BINOP_REM: strcpy(ptr,"%"); break; | |
232 | case BINOP_LSH: strcpy(ptr,"<<"); break; | |
233 | case BINOP_RSH: strcpy(ptr,">>"); break; | |
234 | case BINOP_BITWISE_AND: strcpy(ptr,"&"); break; | |
235 | case BINOP_BITWISE_IOR: strcpy(ptr,"|"); break; | |
236 | case BINOP_BITWISE_XOR: strcpy(ptr,"^"); break; | |
237 | case BINOP_LOGICAL_AND: strcpy(ptr,"&&"); break; | |
238 | case BINOP_LOGICAL_OR: strcpy(ptr,"||"); break; | |
239 | case BINOP_MIN: strcpy(ptr,"<?"); break; | |
240 | case BINOP_MAX: strcpy(ptr,">?"); break; | |
241 | case BINOP_ASSIGN: strcpy(ptr,"="); break; | |
bd5635a1 RP |
242 | case BINOP_ASSIGN_MODIFY: |
243 | switch (otherop) | |
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_BITWISE_AND: strcpy(ptr,"&="); break; | |
251 | case BINOP_BITWISE_IOR: strcpy(ptr,"|="); break; | |
252 | case BINOP_BITWISE_XOR: strcpy(ptr,"^="); break; | |
bd5635a1 RP |
253 | default: |
254 | error ("Invalid binary operation specified."); | |
255 | } | |
256 | break; | |
257 | case BINOP_SUBSCRIPT: strcpy(ptr,"[]"); break; | |
258 | case BINOP_EQUAL: strcpy(ptr,"=="); break; | |
259 | case BINOP_NOTEQUAL: strcpy(ptr,"!="); break; | |
260 | case BINOP_LESS: strcpy(ptr,"<"); break; | |
261 | case BINOP_GTR: strcpy(ptr,">"); break; | |
262 | case BINOP_GEQ: strcpy(ptr,">="); break; | |
263 | case BINOP_LEQ: strcpy(ptr,"<="); break; | |
264 | default: | |
265 | error ("Invalid binary operation specified."); | |
266 | } | |
267 | argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure"); | |
268 | if (argvec[0]) | |
269 | { | |
270 | if (static_memfuncp) | |
271 | { | |
272 | argvec[1] = argvec[0]; | |
273 | argvec++; | |
274 | } | |
e17960fb | 275 | return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); |
bd5635a1 RP |
276 | } |
277 | error ("member function %s not found", tstr); | |
278 | #ifdef lint | |
e17960fb | 279 | return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); |
bd5635a1 RP |
280 | #endif |
281 | } | |
282 | ||
283 | /* We know that arg1 is a structure, so try to find a unary user | |
284 | defined operator that matches the operator in question. | |
285 | Create an argument vector that calls arg1.operator @ (arg1) | |
286 | and return that value (where '@' is (almost) any unary operator which | |
287 | is legal for GNU C++). */ | |
288 | ||
289 | value | |
290 | value_x_unop (arg1, op) | |
291 | value arg1; | |
292 | enum exp_opcode op; | |
293 | { | |
294 | value * argvec; | |
295 | char *ptr; | |
296 | char tstr[13]; | |
297 | int static_memfuncp; | |
298 | ||
299 | COERCE_ENUM (arg1); | |
300 | ||
301 | /* now we know that what we have to do is construct our | |
302 | arg vector and find the right function to call it with. */ | |
303 | ||
304 | if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT) | |
305 | error ("Can't do that unary op on that type"); /* FIXME be explicit */ | |
306 | ||
307 | argvec = (value *) alloca (sizeof (value) * 3); | |
308 | argvec[1] = value_addr (arg1); | |
309 | argvec[2] = 0; | |
310 | ||
311 | /* make the right function name up */ | |
312 | strcpy(tstr,"operator__"); | |
313 | ptr = tstr+8; | |
314 | switch (op) | |
315 | { | |
316 | case UNOP_PREINCREMENT: strcpy(ptr,"++"); break; | |
317 | case UNOP_PREDECREMENT: strcpy(ptr,"++"); break; | |
318 | case UNOP_POSTINCREMENT: strcpy(ptr,"++"); break; | |
319 | case UNOP_POSTDECREMENT: strcpy(ptr,"++"); break; | |
e58de8a2 FF |
320 | case UNOP_LOGICAL_NOT: strcpy(ptr,"!"); break; |
321 | case UNOP_COMPLEMENT: strcpy(ptr,"~"); break; | |
322 | case UNOP_NEG: strcpy(ptr,"-"); break; | |
bd5635a1 RP |
323 | default: |
324 | error ("Invalid binary operation specified."); | |
325 | } | |
326 | argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure"); | |
327 | if (argvec[0]) | |
328 | { | |
329 | if (static_memfuncp) | |
330 | { | |
331 | argvec[1] = argvec[0]; | |
332 | argvec++; | |
333 | } | |
e17960fb | 334 | return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1); |
bd5635a1 RP |
335 | } |
336 | error ("member function %s not found", tstr); | |
337 | return 0; /* For lint -- never reached */ | |
338 | } | |
339 | \f | |
340 | /* Perform a binary operation on two integers or two floats. | |
341 | Does not support addition and subtraction on pointers; | |
342 | use value_add or value_sub if you want to handle those possibilities. */ | |
343 | ||
344 | value | |
345 | value_binop (arg1, arg2, op) | |
346 | value arg1, arg2; | |
088c3a0b | 347 | enum exp_opcode op; |
bd5635a1 RP |
348 | { |
349 | register value val; | |
350 | ||
351 | COERCE_ENUM (arg1); | |
352 | COERCE_ENUM (arg2); | |
353 | ||
354 | if ((TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_FLT | |
355 | && | |
e58de8a2 FF |
356 | TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT |
357 | && | |
358 | TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_BOOL) | |
bd5635a1 RP |
359 | || |
360 | (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_FLT | |
361 | && | |
e58de8a2 FF |
362 | TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT |
363 | && | |
364 | TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_BOOL)) | |
365 | error ("Argument to arithmetic operation not a number or boolean."); | |
bd5635a1 RP |
366 | |
367 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT | |
368 | || | |
369 | TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_FLT) | |
370 | { | |
371 | double v1, v2, v; | |
372 | v1 = value_as_double (arg1); | |
373 | v2 = value_as_double (arg2); | |
374 | switch (op) | |
375 | { | |
376 | case BINOP_ADD: | |
377 | v = v1 + v2; | |
378 | break; | |
379 | ||
380 | case BINOP_SUB: | |
381 | v = v1 - v2; | |
382 | break; | |
383 | ||
384 | case BINOP_MUL: | |
385 | v = v1 * v2; | |
386 | break; | |
387 | ||
388 | case BINOP_DIV: | |
389 | v = v1 / v2; | |
390 | break; | |
391 | ||
392 | default: | |
393 | error ("Integer-only operation on floating point number."); | |
394 | } | |
395 | ||
396 | val = allocate_value (builtin_type_double); | |
397 | SWAP_TARGET_AND_HOST (&v, sizeof (v)); | |
398 | *(double *) VALUE_CONTENTS_RAW (val) = v; | |
399 | } | |
e58de8a2 FF |
400 | else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_BOOL |
401 | && | |
402 | TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_BOOL) | |
403 | { | |
404 | LONGEST v1, v2, v; | |
405 | v1 = value_as_long (arg1); | |
406 | v2 = value_as_long (arg2); | |
407 | ||
408 | switch (op) | |
409 | { | |
410 | case BINOP_BITWISE_AND: | |
411 | v = v1 & v2; | |
412 | break; | |
413 | ||
414 | case BINOP_BITWISE_IOR: | |
415 | v = v1 | v2; | |
416 | break; | |
417 | ||
418 | case BINOP_BITWISE_XOR: | |
419 | v = v1 ^ v2; | |
420 | break; | |
421 | ||
422 | default: | |
423 | error ("Invalid operation on booleans."); | |
424 | } | |
425 | ||
426 | val = allocate_value (builtin_type_chill_bool); | |
427 | SWAP_TARGET_AND_HOST (&v, sizeof (v)); | |
428 | *(LONGEST *) VALUE_CONTENTS_RAW (val) = v; | |
429 | } | |
bd5635a1 RP |
430 | else |
431 | /* Integral operations here. */ | |
e58de8a2 | 432 | /* FIXME: Also mixed integral/booleans, with result an integer. */ |
bd5635a1 RP |
433 | { |
434 | /* Should we promote to unsigned longest? */ | |
435 | if ((TYPE_UNSIGNED (VALUE_TYPE (arg1)) | |
436 | || TYPE_UNSIGNED (VALUE_TYPE (arg2))) | |
437 | && (TYPE_LENGTH (VALUE_TYPE (arg1)) >= sizeof (unsigned LONGEST) | |
438 | || TYPE_LENGTH (VALUE_TYPE (arg1)) >= sizeof (unsigned LONGEST))) | |
439 | { | |
440 | unsigned LONGEST v1, v2, v; | |
441 | v1 = (unsigned LONGEST) value_as_long (arg1); | |
442 | v2 = (unsigned LONGEST) value_as_long (arg2); | |
443 | ||
444 | switch (op) | |
445 | { | |
446 | case BINOP_ADD: | |
447 | v = v1 + v2; | |
448 | break; | |
449 | ||
450 | case BINOP_SUB: | |
451 | v = v1 - v2; | |
452 | break; | |
453 | ||
454 | case BINOP_MUL: | |
455 | v = v1 * v2; | |
456 | break; | |
457 | ||
458 | case BINOP_DIV: | |
459 | v = v1 / v2; | |
460 | break; | |
461 | ||
462 | case BINOP_REM: | |
463 | v = v1 % v2; | |
464 | break; | |
465 | ||
466 | case BINOP_LSH: | |
467 | v = v1 << v2; | |
468 | break; | |
469 | ||
470 | case BINOP_RSH: | |
471 | v = v1 >> v2; | |
472 | break; | |
473 | ||
e58de8a2 | 474 | case BINOP_BITWISE_AND: |
bd5635a1 RP |
475 | v = v1 & v2; |
476 | break; | |
477 | ||
e58de8a2 | 478 | case BINOP_BITWISE_IOR: |
bd5635a1 RP |
479 | v = v1 | v2; |
480 | break; | |
481 | ||
e58de8a2 | 482 | case BINOP_BITWISE_XOR: |
bd5635a1 RP |
483 | v = v1 ^ v2; |
484 | break; | |
485 | ||
e58de8a2 | 486 | case BINOP_LOGICAL_AND: |
bd5635a1 RP |
487 | v = v1 && v2; |
488 | break; | |
489 | ||
e58de8a2 | 490 | case BINOP_LOGICAL_OR: |
bd5635a1 RP |
491 | v = v1 || v2; |
492 | break; | |
493 | ||
494 | case BINOP_MIN: | |
495 | v = v1 < v2 ? v1 : v2; | |
496 | break; | |
497 | ||
498 | case BINOP_MAX: | |
499 | v = v1 > v2 ? v1 : v2; | |
500 | break; | |
501 | ||
502 | default: | |
503 | error ("Invalid binary operation on numbers."); | |
504 | } | |
505 | ||
506 | val = allocate_value (BUILTIN_TYPE_UNSIGNED_LONGEST); | |
507 | SWAP_TARGET_AND_HOST (&v, sizeof (v)); | |
508 | *(unsigned LONGEST *) VALUE_CONTENTS_RAW (val) = v; | |
509 | } | |
510 | else | |
511 | { | |
512 | LONGEST v1, v2, v; | |
513 | v1 = value_as_long (arg1); | |
514 | v2 = value_as_long (arg2); | |
515 | ||
516 | switch (op) | |
517 | { | |
518 | case BINOP_ADD: | |
519 | v = v1 + v2; | |
520 | break; | |
521 | ||
522 | case BINOP_SUB: | |
523 | v = v1 - v2; | |
524 | break; | |
525 | ||
526 | case BINOP_MUL: | |
527 | v = v1 * v2; | |
528 | break; | |
529 | ||
530 | case BINOP_DIV: | |
531 | v = v1 / v2; | |
532 | break; | |
533 | ||
534 | case BINOP_REM: | |
535 | v = v1 % v2; | |
536 | break; | |
537 | ||
538 | case BINOP_LSH: | |
539 | v = v1 << v2; | |
540 | break; | |
541 | ||
542 | case BINOP_RSH: | |
543 | v = v1 >> v2; | |
544 | break; | |
545 | ||
e58de8a2 | 546 | case BINOP_BITWISE_AND: |
bd5635a1 RP |
547 | v = v1 & v2; |
548 | break; | |
549 | ||
e58de8a2 | 550 | case BINOP_BITWISE_IOR: |
bd5635a1 RP |
551 | v = v1 | v2; |
552 | break; | |
553 | ||
e58de8a2 | 554 | case BINOP_BITWISE_XOR: |
bd5635a1 RP |
555 | v = v1 ^ v2; |
556 | break; | |
557 | ||
e58de8a2 | 558 | case BINOP_LOGICAL_AND: |
bd5635a1 RP |
559 | v = v1 && v2; |
560 | break; | |
561 | ||
e58de8a2 | 562 | case BINOP_LOGICAL_OR: |
bd5635a1 RP |
563 | v = v1 || v2; |
564 | break; | |
565 | ||
566 | case BINOP_MIN: | |
567 | v = v1 < v2 ? v1 : v2; | |
568 | break; | |
569 | ||
570 | case BINOP_MAX: | |
571 | v = v1 > v2 ? v1 : v2; | |
572 | break; | |
573 | ||
574 | default: | |
575 | error ("Invalid binary operation on numbers."); | |
576 | } | |
577 | ||
578 | val = allocate_value (BUILTIN_TYPE_LONGEST); | |
579 | SWAP_TARGET_AND_HOST (&v, sizeof (v)); | |
580 | *(LONGEST *) VALUE_CONTENTS_RAW (val) = v; | |
581 | } | |
582 | } | |
583 | ||
584 | return val; | |
585 | } | |
586 | \f | |
51b57ded | 587 | /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */ |
bd5635a1 RP |
588 | |
589 | int | |
e58de8a2 | 590 | value_logical_not (arg1) |
bd5635a1 RP |
591 | value arg1; |
592 | { | |
593 | register int len; | |
594 | register char *p; | |
595 | ||
596 | COERCE_ARRAY (arg1); | |
597 | ||
51b57ded FF |
598 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT) |
599 | return 0 == value_as_double (arg1); | |
600 | ||
bd5635a1 RP |
601 | len = TYPE_LENGTH (VALUE_TYPE (arg1)); |
602 | p = VALUE_CONTENTS (arg1); | |
603 | ||
604 | while (--len >= 0) | |
605 | { | |
606 | if (*p++) | |
607 | break; | |
608 | } | |
609 | ||
610 | return len < 0; | |
611 | } | |
612 | ||
613 | /* Simulate the C operator == by returning a 1 | |
614 | iff ARG1 and ARG2 have equal contents. */ | |
615 | ||
616 | int | |
617 | value_equal (arg1, arg2) | |
618 | register value arg1, arg2; | |
619 | ||
620 | { | |
621 | register int len; | |
622 | register char *p1, *p2; | |
623 | enum type_code code1; | |
624 | enum type_code code2; | |
625 | ||
626 | COERCE_ARRAY (arg1); | |
627 | COERCE_ARRAY (arg2); | |
628 | ||
629 | code1 = TYPE_CODE (VALUE_TYPE (arg1)); | |
630 | code2 = TYPE_CODE (VALUE_TYPE (arg2)); | |
631 | ||
632 | if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT) | |
633 | return value_as_long (arg1) == value_as_long (arg2); | |
634 | else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT) | |
635 | && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT)) | |
636 | return value_as_double (arg1) == value_as_double (arg2); | |
088c3a0b JG |
637 | |
638 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
639 | is bigger. */ | |
640 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT) | |
641 | return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2); | |
642 | else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT) | |
643 | return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2); | |
644 | ||
bd5635a1 RP |
645 | else if (code1 == code2 |
646 | && ((len = TYPE_LENGTH (VALUE_TYPE (arg1))) | |
647 | == TYPE_LENGTH (VALUE_TYPE (arg2)))) | |
648 | { | |
649 | p1 = VALUE_CONTENTS (arg1); | |
650 | p2 = VALUE_CONTENTS (arg2); | |
651 | while (--len >= 0) | |
652 | { | |
653 | if (*p1++ != *p2++) | |
654 | break; | |
655 | } | |
656 | return len < 0; | |
657 | } | |
658 | else | |
659 | { | |
660 | error ("Invalid type combination in equality test."); | |
661 | return 0; /* For lint -- never reached */ | |
662 | } | |
663 | } | |
664 | ||
665 | /* Simulate the C operator < by returning 1 | |
666 | iff ARG1's contents are less than ARG2's. */ | |
667 | ||
668 | int | |
669 | value_less (arg1, arg2) | |
670 | register value arg1, arg2; | |
671 | { | |
672 | register enum type_code code1; | |
673 | register enum type_code code2; | |
674 | ||
675 | COERCE_ARRAY (arg1); | |
676 | COERCE_ARRAY (arg2); | |
677 | ||
678 | code1 = TYPE_CODE (VALUE_TYPE (arg1)); | |
679 | code2 = TYPE_CODE (VALUE_TYPE (arg2)); | |
680 | ||
681 | if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT) | |
682 | { | |
683 | if (TYPE_UNSIGNED (VALUE_TYPE (arg1)) | |
684 | || TYPE_UNSIGNED (VALUE_TYPE (arg2))) | |
088c3a0b JG |
685 | return ((unsigned LONGEST) value_as_long (arg1) |
686 | < (unsigned LONGEST) value_as_long (arg2)); | |
bd5635a1 RP |
687 | else |
688 | return value_as_long (arg1) < value_as_long (arg2); | |
689 | } | |
690 | else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT) | |
691 | && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT)) | |
692 | return value_as_double (arg1) < value_as_double (arg2); | |
088c3a0b JG |
693 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) |
694 | return value_as_pointer (arg1) < value_as_pointer (arg2); | |
695 | ||
696 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever | |
697 | is bigger. */ | |
698 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT) | |
699 | return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2); | |
700 | else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT) | |
701 | return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2); | |
702 | ||
bd5635a1 RP |
703 | else |
704 | { | |
705 | error ("Invalid type combination in ordering comparison."); | |
706 | return 0; | |
707 | } | |
708 | } | |
709 | \f | |
710 | /* The unary operators - and ~. Both free the argument ARG1. */ | |
711 | ||
712 | value | |
713 | value_neg (arg1) | |
714 | register value arg1; | |
715 | { | |
716 | register struct type *type; | |
717 | ||
718 | COERCE_ENUM (arg1); | |
719 | ||
720 | type = VALUE_TYPE (arg1); | |
721 | ||
722 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
723 | return value_from_double (type, - value_as_double (arg1)); | |
724 | else if (TYPE_CODE (type) == TYPE_CODE_INT) | |
088c3a0b | 725 | return value_from_longest (type, - value_as_long (arg1)); |
bd5635a1 RP |
726 | else { |
727 | error ("Argument to negate operation not a number."); | |
728 | return 0; /* For lint -- never reached */ | |
729 | } | |
730 | } | |
731 | ||
732 | value | |
e58de8a2 | 733 | value_complement (arg1) |
bd5635a1 RP |
734 | register value arg1; |
735 | { | |
736 | COERCE_ENUM (arg1); | |
737 | ||
738 | if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT) | |
739 | error ("Argument to complement operation not an integer."); | |
740 | ||
088c3a0b | 741 | return value_from_longest (VALUE_TYPE (arg1), ~ value_as_long (arg1)); |
bd5635a1 RP |
742 | } |
743 | \f |