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