772e59069991797c00de7d06a2cf8cddf17aae54
1 /* Perform arithmetic and other operations on values, for GDB.
2 Copyright (C) 1986 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
25 #include "expression.h"
28 value
value_x_binop ();
31 value_add (arg1
, arg2
)
34 register value val
, valint
, valptr
;
40 if ((TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
41 || TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_PTR
)
43 (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
44 || TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_INT
))
45 /* Exactly one argument is a pointer, and one is an integer. */
47 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
)
57 len
= TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (valptr
)));
58 if (len
== 0) len
= 1; /* For (void *) */
59 val
= value_from_long (builtin_type_long
,
60 value_as_long (valptr
)
61 + (len
* value_as_long (valint
)));
62 VALUE_TYPE (val
) = VALUE_TYPE (valptr
);
66 return value_binop (arg1
, arg2
, BINOP_ADD
);
70 value_sub (arg1
, arg2
)
78 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
80 TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_INT
)
82 val
= value_from_long (builtin_type_long
,
84 - TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
))) * value_as_long (arg2
));
85 VALUE_TYPE (val
) = VALUE_TYPE (arg1
);
89 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
91 VALUE_TYPE (arg1
) == VALUE_TYPE (arg2
))
93 val
= value_from_long (builtin_type_long
,
94 (value_as_long (arg1
) - value_as_long (arg2
))
95 / TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
))));
99 return value_binop (arg1
, arg2
, BINOP_SUB
);
102 /* Return the value of ARRAY[IDX]. */
105 value_subscript (array
, idx
)
108 return value_ind (value_add (array
, idx
));
111 /* Check to see if either argument is a structure. This is called so
112 we know whether to go ahead with the normal binop or look for a
113 user defined function instead.
115 For now, we do not overload the `=' operator. */
118 binop_user_defined_p (op
, arg1
, arg2
)
122 if (op
== BINOP_ASSIGN
)
124 return (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_STRUCT
125 || TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_STRUCT
126 || (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
127 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
))) == TYPE_CODE_STRUCT
)
128 || (TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_REF
129 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
))) == TYPE_CODE_STRUCT
));
132 /* Check to see if argument is a structure. This is called so
133 we know whether to go ahead with the normal unop or look for a
134 user defined function instead.
136 For now, we do not overload the `&' operator. */
138 int unop_user_defined_p (op
, arg1
)
144 return (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_STRUCT
145 || (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
146 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
))) == TYPE_CODE_STRUCT
));
149 /* We know either arg1 or arg2 is a structure, so try to find the right
150 user defined function. Create an argument vector that calls
151 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
152 binary operator which is legal for GNU C++). */
155 value_x_binop (arg1
, arg2
, op
, otherop
)
166 /* now we know that what we have to do is construct our
167 arg vector and find the right function to call it with. */
169 if (TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_STRUCT
)
170 error ("friend functions not implemented yet");
172 argvec
= (value
*) alloca (sizeof (value
) * 4);
173 argvec
[1] = value_addr (arg1
);
177 /* make the right function name up */
178 strcpy(tstr
, "operator __");
182 case BINOP_ADD
: strcpy(ptr
,"+"); break;
183 case BINOP_SUB
: strcpy(ptr
,"-"); break;
184 case BINOP_MUL
: strcpy(ptr
,"*"); break;
185 case BINOP_DIV
: strcpy(ptr
,"/"); break;
186 case BINOP_REM
: strcpy(ptr
,"%"); break;
187 case BINOP_LSH
: strcpy(ptr
,"<<"); break;
188 case BINOP_RSH
: strcpy(ptr
,">>"); break;
189 case BINOP_LOGAND
: strcpy(ptr
,"&"); break;
190 case BINOP_LOGIOR
: strcpy(ptr
,"|"); break;
191 case BINOP_LOGXOR
: strcpy(ptr
,"^"); break;
192 case BINOP_AND
: strcpy(ptr
,"&&"); break;
193 case BINOP_OR
: strcpy(ptr
,"||"); break;
194 case BINOP_MIN
: strcpy(ptr
,"<?"); break;
195 case BINOP_MAX
: strcpy(ptr
,">?"); break;
196 case BINOP_ASSIGN
: strcpy(ptr
,"="); break;
197 case BINOP_ASSIGN_MODIFY
:
200 case BINOP_ADD
: strcpy(ptr
,"+="); break;
201 case BINOP_SUB
: strcpy(ptr
,"-="); break;
202 case BINOP_MUL
: strcpy(ptr
,"*="); break;
203 case BINOP_DIV
: strcpy(ptr
,"/="); break;
204 case BINOP_REM
: strcpy(ptr
,"%="); break;
205 case BINOP_LOGAND
: strcpy(ptr
,"&="); break;
206 case BINOP_LOGIOR
: strcpy(ptr
,"|="); break;
207 case BINOP_LOGXOR
: strcpy(ptr
,"^="); break;
209 error ("Invalid binary operation specified.");
212 case BINOP_SUBSCRIPT
: strcpy(ptr
,"[]"); break;
213 case BINOP_EQUAL
: strcpy(ptr
,"=="); break;
214 case BINOP_NOTEQUAL
: strcpy(ptr
,"!="); break;
215 case BINOP_LESS
: strcpy(ptr
,"<"); break;
216 case BINOP_GTR
: strcpy(ptr
,">"); break;
217 case BINOP_GEQ
: strcpy(ptr
,">="); break;
218 case BINOP_LEQ
: strcpy(ptr
,"<="); break;
220 error ("Invalid binary operation specified.");
222 argvec
[0] = value_struct_elt (arg1
, argvec
+1, tstr
, "structure");
224 return call_function (argvec
[0], 2, argvec
+ 1);
225 else error ("member function %s not found", tstr
);
228 /* We know that arg1 is a structure, so try to find a unary user
229 defined operator that matches the operator in question.
230 Create an argument vector that calls arg1.operator @ (arg1)
231 and return that value (where '@' is (almost) any unary operator which
232 is legal for GNU C++). */
235 value_x_unop (arg1
, op
)
245 /* now we know that what we have to do is construct our
246 arg vector and find the right function to call it with. */
248 if (TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_STRUCT
)
249 error ("friend functions not implemented yet");
251 argvec
= (value
*) alloca (sizeof (value
) * 3);
252 argvec
[1] = value_addr (arg1
);
255 /* make the right function name up */
256 strcpy(tstr
,"operator __");
260 case UNOP_PREINCREMENT
: strcpy(ptr
,"++"); break;
261 case UNOP_PREDECREMENT
: strcpy(ptr
,"++"); break;
262 case UNOP_POSTINCREMENT
: strcpy(ptr
,"++"); break;
263 case UNOP_POSTDECREMENT
: strcpy(ptr
,"++"); break;
264 case UNOP_ZEROP
: strcpy(ptr
,"!"); break;
265 case UNOP_LOGNOT
: strcpy(ptr
,"~"); break;
266 case UNOP_NEG
: strcpy(ptr
,"-"); break;
268 error ("Invalid binary operation specified.");
270 argvec
[0] = value_struct_elt (arg1
, argvec
+1, tstr
, "structure");
272 return call_function (argvec
[0], 1, argvec
+ 1);
273 else error ("member function %s not found", tstr
);
276 /* Perform a binary operation on two integers or two floats.
277 Does not support addition and subtraction on pointers;
278 use value_add or value_sub if you want to handle those possibilities. */
281 value_binop (arg1
, arg2
, op
)
290 if ((TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_FLT
292 TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_INT
)
294 (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_FLT
296 TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
))
297 error ("Argument to arithmetic operation not a number.");
299 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_FLT
301 TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_FLT
)
304 v1
= value_as_double (arg1
);
305 v2
= value_as_double (arg2
);
325 error ("Integer-only operation on floating point number.");
328 val
= allocate_value (builtin_type_double
);
329 *(double *) VALUE_CONTENTS (val
) = v
;
334 v1
= value_as_long (arg1
);
335 v2
= value_as_long (arg2
);
388 v
= v1
< v2
? v1
: v2
;
392 v
= v1
> v2
? v1
: v2
;
396 error ("Invalid binary operation on numbers.");
399 val
= allocate_value (BUILTIN_TYPE_LONGEST
);
400 *(LONGEST
*) VALUE_CONTENTS (val
) = v
;
406 /* Simulate the C operator ! -- return 1 if ARG1 contains zeros. */
417 len
= TYPE_LENGTH (VALUE_TYPE (arg1
));
418 p
= VALUE_CONTENTS (arg1
);
429 /* Simulate the C operator == by returning a 1
430 iff ARG1 and ARG2 have equal contents. */
433 value_equal (arg1
, arg2
)
434 register value arg1
, arg2
;
438 register char *p1
, *p2
;
439 enum type_code code1
;
440 enum type_code code2
;
445 code1
= TYPE_CODE (VALUE_TYPE (arg1
));
446 code2
= TYPE_CODE (VALUE_TYPE (arg2
));
448 if (code1
== TYPE_CODE_INT
&& code2
== TYPE_CODE_INT
)
449 return value_as_long (arg1
) == value_as_long (arg2
);
450 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
)
451 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
))
452 return value_as_double (arg1
) == value_as_double (arg2
);
453 else if ((code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_INT
)
454 || (code2
== TYPE_CODE_PTR
&& code1
== TYPE_CODE_INT
))
455 return value_as_long (arg1
) == value_as_long (arg2
);
456 else if (code1
== code2
457 && ((len
= TYPE_LENGTH (VALUE_TYPE (arg1
)))
458 == TYPE_LENGTH (VALUE_TYPE (arg2
))))
460 p1
= VALUE_CONTENTS (arg1
);
461 p2
= VALUE_CONTENTS (arg2
);
470 error ("Invalid type combination in equality test.");
473 /* Simulate the C operator < by returning 1
474 iff ARG1's contents are less than ARG2's. */
477 value_less (arg1
, arg2
)
478 register value arg1
, arg2
;
480 register enum type_code code1
;
481 register enum type_code code2
;
486 code1
= TYPE_CODE (VALUE_TYPE (arg1
));
487 code2
= TYPE_CODE (VALUE_TYPE (arg2
));
489 if (code1
== TYPE_CODE_INT
&& code2
== TYPE_CODE_INT
)
490 return value_as_long (arg1
) < value_as_long (arg2
);
491 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
)
492 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
))
493 return value_as_double (arg1
) < value_as_double (arg2
);
494 else if ((code1
== TYPE_CODE_PTR
|| code1
== TYPE_CODE_INT
)
495 && (code2
== TYPE_CODE_PTR
|| code2
== TYPE_CODE_INT
))
496 return value_as_long (arg1
) < value_as_long (arg2
);
498 error ("Invalid type combination in ordering comparison.");
501 /* The unary operators - and ~. Both free the argument ARG1. */
507 register struct type
*type
;
511 type
= VALUE_TYPE (arg1
);
513 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
514 return value_from_double (type
, - value_as_double (arg1
));
515 else if (TYPE_CODE (type
) == TYPE_CODE_INT
)
516 return value_from_long (type
, - value_as_long (arg1
));
518 error ("Argument to negate operation not a number.");
527 if (TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_INT
)
528 error ("Argument to complement operation not an integer.");
530 return value_from_long (VALUE_TYPE (arg1
), ~ value_as_long (arg1
));
This page took 0.040646 seconds and 4 git commands to generate.