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