* ada-lang.c (assign_component): Use platform-specific integer type
[external/binutils.git] / gdb / valarith.c
1 /* Perform arithmetic and other operations on values, for GDB.
2
3    Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4    1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
5    Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "value.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "target.h"
28 #include "language.h"
29 #include "gdb_string.h"
30 #include "doublest.h"
31 #include "dfp.h"
32 #include <math.h>
33 #include "infcall.h"
34
35 /* Define whether or not the C operator '/' truncates towards zero for
36    differently signed operands (truncation direction is undefined in C). */
37
38 #ifndef TRUNCATION_TOWARDS_ZERO
39 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
40 #endif
41
42 void _initialize_valarith (void);
43 \f
44
45 /* Given a pointer, return the size of its target.
46    If the pointer type is void *, then return 1.
47    If the target type is incomplete, then error out.
48    This isn't a general purpose function, but just a 
49    helper for value_ptradd.
50 */
51
52 static LONGEST
53 find_size_for_pointer_math (struct type *ptr_type)
54 {
55   LONGEST sz = -1;
56   struct type *ptr_target;
57
58   gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR);
59   ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
60
61   sz = TYPE_LENGTH (ptr_target);
62   if (sz == 0)
63     {
64       if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
65         sz = 1;
66       else
67         {
68           char *name;
69           
70           name = TYPE_NAME (ptr_target);
71           if (name == NULL)
72             name = TYPE_TAG_NAME (ptr_target);
73           if (name == NULL)
74             error (_("Cannot perform pointer math on incomplete types, "
75                    "try casting to a known type, or void *."));
76           else
77             error (_("Cannot perform pointer math on incomplete type \"%s\", "
78                    "try casting to a known type, or void *."), name);
79         }
80     }
81   return sz;
82 }
83
84 /* Given a pointer ARG1 and an integral value ARG2, return the
85    result of C-style pointer arithmetic ARG1 + ARG2.  */
86
87 struct value *
88 value_ptradd (struct value *arg1, LONGEST arg2)
89 {
90   struct type *valptrtype;
91   LONGEST sz;
92
93   arg1 = coerce_array (arg1);
94   valptrtype = check_typedef (value_type (arg1));
95   sz = find_size_for_pointer_math (valptrtype);
96
97   return value_from_pointer (valptrtype,
98                              value_as_address (arg1) + sz * arg2);
99 }
100
101 /* Given two compatible pointer values ARG1 and ARG2, return the
102    result of C-style pointer arithmetic ARG1 - ARG2.  */
103
104 LONGEST
105 value_ptrdiff (struct value *arg1, struct value *arg2)
106 {
107   struct type *type1, *type2;
108   LONGEST sz;
109
110   arg1 = coerce_array (arg1);
111   arg2 = coerce_array (arg2);
112   type1 = check_typedef (value_type (arg1));
113   type2 = check_typedef (value_type (arg2));
114
115   gdb_assert (TYPE_CODE (type1) == TYPE_CODE_PTR);
116   gdb_assert (TYPE_CODE (type2) == TYPE_CODE_PTR);
117
118   if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
119       != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
120     error (_("\
121 First argument of `-' is a pointer and second argument is neither\n\
122 an integer nor a pointer of the same type."));
123
124   sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
125   return (value_as_long (arg1) - value_as_long (arg2)) / sz;
126 }
127
128 /* Return the value of ARRAY[IDX].
129
130    ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING.  If the
131    current language supports C-style arrays, it may also be TYPE_CODE_PTR.
132    To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript.
133
134    See comments in value_coerce_array() for rationale for reason for
135    doing lower bounds adjustment here rather than there.
136    FIXME:  Perhaps we should validate that the index is valid and if
137    verbosity is set, warn about invalid indices (but still use them). */
138
139 struct value *
140 value_subscript (struct value *array, LONGEST index)
141 {
142   struct value *bound;
143   int c_style = current_language->c_style_arrays;
144   struct type *tarray;
145
146   array = coerce_ref (array);
147   tarray = check_typedef (value_type (array));
148
149   if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
150       || TYPE_CODE (tarray) == TYPE_CODE_STRING)
151     {
152       struct type *range_type = TYPE_INDEX_TYPE (tarray);
153       LONGEST lowerbound, upperbound;
154       get_discrete_bounds (range_type, &lowerbound, &upperbound);
155
156       if (VALUE_LVAL (array) != lval_memory)
157         return value_subscripted_rvalue (array, index, lowerbound);
158
159       if (c_style == 0)
160         {
161           if (index >= lowerbound && index <= upperbound)
162             return value_subscripted_rvalue (array, index, lowerbound);
163           /* Emit warning unless we have an array of unknown size.
164              An array of unknown size has lowerbound 0 and upperbound -1.  */
165           if (upperbound > -1)
166             warning (_("array or string index out of range"));
167           /* fall doing C stuff */
168           c_style = 1;
169         }
170
171       index -= lowerbound;
172       array = value_coerce_array (array);
173     }
174
175   if (c_style)
176     return value_ind (value_ptradd (array, index));
177   else
178     error (_("not an array or string"));
179 }
180
181 /* Return the value of EXPR[IDX], expr an aggregate rvalue
182    (eg, a vector register).  This routine used to promote floats
183    to doubles, but no longer does.  */
184
185 struct value *
186 value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
187 {
188   struct type *array_type = check_typedef (value_type (array));
189   struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
190   unsigned int elt_size = TYPE_LENGTH (elt_type);
191   unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
192   struct value *v;
193
194   if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
195     error (_("no such vector element"));
196
197   v = allocate_value (elt_type);
198   if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
199     set_value_lazy (v, 1);
200   else
201     memcpy (value_contents_writeable (v),
202             value_contents (array) + elt_offs, elt_size);
203
204   set_value_component_location (v, array);
205   VALUE_REGNUM (v) = VALUE_REGNUM (array);
206   VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
207   set_value_offset (v, value_offset (array) + elt_offs);
208   return v;
209 }
210
211 /* Return the value of BITSTRING[IDX] as (boolean) type TYPE.  */
212
213 struct value *
214 value_bitstring_subscript (struct type *type,
215                            struct value *bitstring, LONGEST index)
216 {
217
218   struct type *bitstring_type, *range_type;
219   struct value *v;
220   int offset, byte, bit_index;
221   LONGEST lowerbound, upperbound;
222
223   bitstring_type = check_typedef (value_type (bitstring));
224   gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING);
225
226   range_type = TYPE_INDEX_TYPE (bitstring_type);
227   get_discrete_bounds (range_type, &lowerbound, &upperbound);
228   if (index < lowerbound || index > upperbound)
229     error (_("bitstring index out of range"));
230
231   index -= lowerbound;
232   offset = index / TARGET_CHAR_BIT;
233   byte = *((char *) value_contents (bitstring) + offset);
234
235   bit_index = index % TARGET_CHAR_BIT;
236   byte >>= (gdbarch_bits_big_endian (current_gdbarch) ?
237             TARGET_CHAR_BIT - 1 - bit_index : bit_index);
238
239   v = value_from_longest (type, byte & 1);
240
241   set_value_bitpos (v, bit_index);
242   set_value_bitsize (v, 1);
243   set_value_component_location (v, bitstring);
244   VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring);
245
246   set_value_offset (v, offset + value_offset (bitstring));
247
248   return v;
249 }
250
251 \f
252 /* Check to see if either argument is a structure, or a reference to
253    one.  This is called so we know whether to go ahead with the normal
254    binop or look for a user defined function instead.
255
256    For now, we do not overload the `=' operator.  */
257
258 int
259 binop_user_defined_p (enum exp_opcode op, struct value *arg1, struct value *arg2)
260 {
261   struct type *type1, *type2;
262   if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
263     return 0;
264
265   type1 = check_typedef (value_type (arg1));
266   if (TYPE_CODE (type1) == TYPE_CODE_REF)
267     type1 = check_typedef (TYPE_TARGET_TYPE (type1));
268
269   type2 = check_typedef (value_type (arg2));
270   if (TYPE_CODE (type2) == TYPE_CODE_REF)
271     type2 = check_typedef (TYPE_TARGET_TYPE (type2));
272
273   return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
274           || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
275 }
276
277 /* Check to see if argument is a structure.  This is called so
278    we know whether to go ahead with the normal unop or look for a 
279    user defined function instead.
280
281    For now, we do not overload the `&' operator.  */
282
283 int
284 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
285 {
286   struct type *type1;
287   if (op == UNOP_ADDR)
288     return 0;
289   type1 = check_typedef (value_type (arg1));
290   for (;;)
291     {
292       if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
293         return 1;
294       else if (TYPE_CODE (type1) == TYPE_CODE_REF)
295         type1 = TYPE_TARGET_TYPE (type1);
296       else
297         return 0;
298     }
299 }
300
301 /* We know either arg1 or arg2 is a structure, so try to find the right
302    user defined function.  Create an argument vector that calls 
303    arg1.operator @ (arg1,arg2) and return that value (where '@' is any
304    binary operator which is legal for GNU C++).
305
306    OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
307    is the opcode saying how to modify it.  Otherwise, OTHEROP is
308    unused.  */
309
310 struct value *
311 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
312                enum exp_opcode otherop, enum noside noside)
313 {
314   struct value **argvec;
315   char *ptr;
316   char tstr[13];
317   int static_memfuncp;
318
319   arg1 = coerce_ref (arg1);
320   arg2 = coerce_ref (arg2);
321
322   /* now we know that what we have to do is construct our
323      arg vector and find the right function to call it with.  */
324
325   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
326     error (_("Can't do that binary op on that type"));  /* FIXME be explicit */
327
328   argvec = (struct value **) alloca (sizeof (struct value *) * 4);
329   argvec[1] = value_addr (arg1);
330   argvec[2] = arg2;
331   argvec[3] = 0;
332
333   /* make the right function name up */
334   strcpy (tstr, "operator__");
335   ptr = tstr + 8;
336   switch (op)
337     {
338     case BINOP_ADD:
339       strcpy (ptr, "+");
340       break;
341     case BINOP_SUB:
342       strcpy (ptr, "-");
343       break;
344     case BINOP_MUL:
345       strcpy (ptr, "*");
346       break;
347     case BINOP_DIV:
348       strcpy (ptr, "/");
349       break;
350     case BINOP_REM:
351       strcpy (ptr, "%");
352       break;
353     case BINOP_LSH:
354       strcpy (ptr, "<<");
355       break;
356     case BINOP_RSH:
357       strcpy (ptr, ">>");
358       break;
359     case BINOP_BITWISE_AND:
360       strcpy (ptr, "&");
361       break;
362     case BINOP_BITWISE_IOR:
363       strcpy (ptr, "|");
364       break;
365     case BINOP_BITWISE_XOR:
366       strcpy (ptr, "^");
367       break;
368     case BINOP_LOGICAL_AND:
369       strcpy (ptr, "&&");
370       break;
371     case BINOP_LOGICAL_OR:
372       strcpy (ptr, "||");
373       break;
374     case BINOP_MIN:
375       strcpy (ptr, "<?");
376       break;
377     case BINOP_MAX:
378       strcpy (ptr, ">?");
379       break;
380     case BINOP_ASSIGN:
381       strcpy (ptr, "=");
382       break;
383     case BINOP_ASSIGN_MODIFY:
384       switch (otherop)
385         {
386         case BINOP_ADD:
387           strcpy (ptr, "+=");
388           break;
389         case BINOP_SUB:
390           strcpy (ptr, "-=");
391           break;
392         case BINOP_MUL:
393           strcpy (ptr, "*=");
394           break;
395         case BINOP_DIV:
396           strcpy (ptr, "/=");
397           break;
398         case BINOP_REM:
399           strcpy (ptr, "%=");
400           break;
401         case BINOP_BITWISE_AND:
402           strcpy (ptr, "&=");
403           break;
404         case BINOP_BITWISE_IOR:
405           strcpy (ptr, "|=");
406           break;
407         case BINOP_BITWISE_XOR:
408           strcpy (ptr, "^=");
409           break;
410         case BINOP_MOD: /* invalid */
411         default:
412           error (_("Invalid binary operation specified."));
413         }
414       break;
415     case BINOP_SUBSCRIPT:
416       strcpy (ptr, "[]");
417       break;
418     case BINOP_EQUAL:
419       strcpy (ptr, "==");
420       break;
421     case BINOP_NOTEQUAL:
422       strcpy (ptr, "!=");
423       break;
424     case BINOP_LESS:
425       strcpy (ptr, "<");
426       break;
427     case BINOP_GTR:
428       strcpy (ptr, ">");
429       break;
430     case BINOP_GEQ:
431       strcpy (ptr, ">=");
432       break;
433     case BINOP_LEQ:
434       strcpy (ptr, "<=");
435       break;
436     case BINOP_MOD:             /* invalid */
437     default:
438       error (_("Invalid binary operation specified."));
439     }
440
441   argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
442
443   if (argvec[0])
444     {
445       if (static_memfuncp)
446         {
447           argvec[1] = argvec[0];
448           argvec++;
449         }
450       if (noside == EVAL_AVOID_SIDE_EFFECTS)
451         {
452           struct type *return_type;
453           return_type
454             = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
455           return value_zero (return_type, VALUE_LVAL (arg1));
456         }
457       return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
458     }
459   error (_("member function %s not found"), tstr);
460 #ifdef lint
461   return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
462 #endif
463 }
464
465 /* We know that arg1 is a structure, so try to find a unary user
466    defined operator that matches the operator in question.  
467    Create an argument vector that calls arg1.operator @ (arg1)
468    and return that value (where '@' is (almost) any unary operator which
469    is legal for GNU C++).  */
470
471 struct value *
472 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
473 {
474   struct gdbarch *gdbarch = current_gdbarch;
475   struct value **argvec;
476   char *ptr, *mangle_ptr;
477   char tstr[13], mangle_tstr[13];
478   int static_memfuncp, nargs;
479
480   arg1 = coerce_ref (arg1);
481
482   /* now we know that what we have to do is construct our
483      arg vector and find the right function to call it with.  */
484
485   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
486     error (_("Can't do that unary op on that type"));   /* FIXME be explicit */
487
488   argvec = (struct value **) alloca (sizeof (struct value *) * 4);
489   argvec[1] = value_addr (arg1);
490   argvec[2] = 0;
491
492   nargs = 1;
493
494   /* make the right function name up */
495   strcpy (tstr, "operator__");
496   ptr = tstr + 8;
497   strcpy (mangle_tstr, "__");
498   mangle_ptr = mangle_tstr + 2;
499   switch (op)
500     {
501     case UNOP_PREINCREMENT:
502       strcpy (ptr, "++");
503       break;
504     case UNOP_PREDECREMENT:
505       strcpy (ptr, "--");
506       break;
507     case UNOP_POSTINCREMENT:
508       strcpy (ptr, "++");
509       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
510       argvec[3] = 0;
511       nargs ++;
512       break;
513     case UNOP_POSTDECREMENT:
514       strcpy (ptr, "--");
515       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
516       argvec[3] = 0;
517       nargs ++;
518       break;
519     case UNOP_LOGICAL_NOT:
520       strcpy (ptr, "!");
521       break;
522     case UNOP_COMPLEMENT:
523       strcpy (ptr, "~");
524       break;
525     case UNOP_NEG:
526       strcpy (ptr, "-");
527       break;
528     case UNOP_PLUS:
529       strcpy (ptr, "+");
530       break;
531     case UNOP_IND:
532       strcpy (ptr, "*");
533       break;
534     default:
535       error (_("Invalid unary operation specified."));
536     }
537
538   argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
539
540   if (argvec[0])
541     {
542       if (static_memfuncp)
543         {
544           argvec[1] = argvec[0];
545           nargs --;
546           argvec++;
547         }
548       if (noside == EVAL_AVOID_SIDE_EFFECTS)
549         {
550           struct type *return_type;
551           return_type
552             = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
553           return value_zero (return_type, VALUE_LVAL (arg1));
554         }
555       return call_function_by_hand (argvec[0], nargs, argvec + 1);
556     }
557   error (_("member function %s not found"), tstr);
558   return 0;                     /* For lint -- never reached */
559 }
560 \f
561
562 /* Concatenate two values with the following conditions:
563
564    (1)  Both values must be either bitstring values or character string
565    values and the resulting value consists of the concatenation of
566    ARG1 followed by ARG2.
567
568    or
569
570    One value must be an integer value and the other value must be
571    either a bitstring value or character string value, which is
572    to be repeated by the number of times specified by the integer
573    value.
574
575
576    (2)  Boolean values are also allowed and are treated as bit string
577    values of length 1.
578
579    (3)  Character values are also allowed and are treated as character
580    string values of length 1.
581  */
582
583 struct value *
584 value_concat (struct value *arg1, struct value *arg2)
585 {
586   struct value *inval1;
587   struct value *inval2;
588   struct value *outval = NULL;
589   int inval1len, inval2len;
590   int count, idx;
591   char *ptr;
592   char inchar;
593   struct type *type1 = check_typedef (value_type (arg1));
594   struct type *type2 = check_typedef (value_type (arg2));
595   struct type *char_type;
596
597   /* First figure out if we are dealing with two values to be concatenated
598      or a repeat count and a value to be repeated.  INVAL1 is set to the
599      first of two concatenated values, or the repeat count.  INVAL2 is set
600      to the second of the two concatenated values or the value to be 
601      repeated. */
602
603   if (TYPE_CODE (type2) == TYPE_CODE_INT)
604     {
605       struct type *tmp = type1;
606       type1 = tmp;
607       tmp = type2;
608       inval1 = arg2;
609       inval2 = arg1;
610     }
611   else
612     {
613       inval1 = arg1;
614       inval2 = arg2;
615     }
616
617   /* Now process the input values. */
618
619   if (TYPE_CODE (type1) == TYPE_CODE_INT)
620     {
621       /* We have a repeat count.  Validate the second value and then
622          construct a value repeated that many times. */
623       if (TYPE_CODE (type2) == TYPE_CODE_STRING
624           || TYPE_CODE (type2) == TYPE_CODE_CHAR)
625         {
626           count = longest_to_int (value_as_long (inval1));
627           inval2len = TYPE_LENGTH (type2);
628           ptr = (char *) alloca (count * inval2len);
629           if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
630             {
631               char_type = type2;
632               inchar = (char) unpack_long (type2,
633                                            value_contents (inval2));
634               for (idx = 0; idx < count; idx++)
635                 {
636                   *(ptr + idx) = inchar;
637                 }
638             }
639           else
640             {
641               char_type = TYPE_TARGET_TYPE (type2);
642               for (idx = 0; idx < count; idx++)
643                 {
644                   memcpy (ptr + (idx * inval2len), value_contents (inval2),
645                           inval2len);
646                 }
647             }
648           outval = value_string (ptr, count * inval2len, char_type);
649         }
650       else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
651                || TYPE_CODE (type2) == TYPE_CODE_BOOL)
652         {
653           error (_("unimplemented support for bitstring/boolean repeats"));
654         }
655       else
656         {
657           error (_("can't repeat values of that type"));
658         }
659     }
660   else if (TYPE_CODE (type1) == TYPE_CODE_STRING
661            || TYPE_CODE (type1) == TYPE_CODE_CHAR)
662     {
663       /* We have two character strings to concatenate. */
664       if (TYPE_CODE (type2) != TYPE_CODE_STRING
665           && TYPE_CODE (type2) != TYPE_CODE_CHAR)
666         {
667           error (_("Strings can only be concatenated with other strings."));
668         }
669       inval1len = TYPE_LENGTH (type1);
670       inval2len = TYPE_LENGTH (type2);
671       ptr = (char *) alloca (inval1len + inval2len);
672       if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
673         {
674           char_type = type1;
675           *ptr = (char) unpack_long (type1, value_contents (inval1));
676         }
677       else
678         {
679           char_type = TYPE_TARGET_TYPE (type1);
680           memcpy (ptr, value_contents (inval1), inval1len);
681         }
682       if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
683         {
684           *(ptr + inval1len) =
685             (char) unpack_long (type2, value_contents (inval2));
686         }
687       else
688         {
689           memcpy (ptr + inval1len, value_contents (inval2), inval2len);
690         }
691       outval = value_string (ptr, inval1len + inval2len, char_type);
692     }
693   else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
694            || TYPE_CODE (type1) == TYPE_CODE_BOOL)
695     {
696       /* We have two bitstrings to concatenate. */
697       if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
698           && TYPE_CODE (type2) != TYPE_CODE_BOOL)
699         {
700           error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
701         }
702       error (_("unimplemented support for bitstring/boolean concatenation."));
703     }
704   else
705     {
706       /* We don't know how to concatenate these operands. */
707       error (_("illegal operands for concatenation."));
708     }
709   return (outval);
710 }
711 \f
712 /* Integer exponentiation: V1**V2, where both arguments are
713    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
714 static LONGEST
715 integer_pow (LONGEST v1, LONGEST v2)
716 {
717   if (v2 < 0)
718     {
719       if (v1 == 0)
720         error (_("Attempt to raise 0 to negative power."));
721       else
722         return 0;
723     }
724   else 
725     {
726       /* The Russian Peasant's Algorithm */
727       LONGEST v;
728       
729       v = 1;
730       for (;;)
731         {
732           if (v2 & 1L) 
733             v *= v1;
734           v2 >>= 1;
735           if (v2 == 0)
736             return v;
737           v1 *= v1;
738         }
739     }
740 }
741
742 /* Integer exponentiation: V1**V2, where both arguments are
743    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
744 static ULONGEST
745 uinteger_pow (ULONGEST v1, LONGEST v2)
746 {
747   if (v2 < 0)
748     {
749       if (v1 == 0)
750         error (_("Attempt to raise 0 to negative power."));
751       else
752         return 0;
753     }
754   else 
755     {
756       /* The Russian Peasant's Algorithm */
757       ULONGEST v;
758       
759       v = 1;
760       for (;;)
761         {
762           if (v2 & 1L) 
763             v *= v1;
764           v2 >>= 1;
765           if (v2 == 0)
766             return v;
767           v1 *= v1;
768         }
769     }
770 }
771
772 /* Obtain decimal value of arguments for binary operation, converting from
773    other types if one of them is not decimal floating point.  */
774 static void
775 value_args_as_decimal (struct value *arg1, struct value *arg2,
776                        gdb_byte *x, int *len_x, gdb_byte *y, int *len_y)
777 {
778   struct type *type1, *type2;
779
780   type1 = check_typedef (value_type (arg1));
781   type2 = check_typedef (value_type (arg2));
782
783   /* At least one of the arguments must be of decimal float type.  */
784   gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
785               || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
786
787   if (TYPE_CODE (type1) == TYPE_CODE_FLT
788       || TYPE_CODE (type2) == TYPE_CODE_FLT)
789     /* The DFP extension to the C language does not allow mixing of
790      * decimal float types with other float types in expressions
791      * (see WDTR 24732, page 12).  */
792     error (_("Mixing decimal floating types with other floating types is not allowed."));
793
794   /* Obtain decimal value of arg1, converting from other types
795      if necessary.  */
796
797   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
798     {
799       *len_x = TYPE_LENGTH (type1);
800       memcpy (x, value_contents (arg1), *len_x);
801     }
802   else if (is_integral_type (type1))
803     {
804       *len_x = TYPE_LENGTH (type2);
805       decimal_from_integral (arg1, x, *len_x);
806     }
807   else
808     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
809              TYPE_NAME (type2));
810
811   /* Obtain decimal value of arg2, converting from other types
812      if necessary.  */
813
814   if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
815     {
816       *len_y = TYPE_LENGTH (type2);
817       memcpy (y, value_contents (arg2), *len_y);
818     }
819   else if (is_integral_type (type2))
820     {
821       *len_y = TYPE_LENGTH (type1);
822       decimal_from_integral (arg2, y, *len_y);
823     }
824   else
825     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
826              TYPE_NAME (type2));
827 }
828
829 /* Perform a binary operation on two operands which have reasonable
830    representations as integers or floats.  This includes booleans,
831    characters, integers, or floats.
832    Does not support addition and subtraction on pointers;
833    use value_ptradd, value_ptrsub or value_ptrdiff for those operations.  */
834
835 struct value *
836 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
837 {
838   struct value *val;
839   struct type *type1, *type2, *result_type;
840
841   arg1 = coerce_ref (arg1);
842   arg2 = coerce_ref (arg2);
843
844   type1 = check_typedef (value_type (arg1));
845   type2 = check_typedef (value_type (arg2));
846
847   if ((TYPE_CODE (type1) != TYPE_CODE_FLT
848        && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
849        && !is_integral_type (type1))
850       || (TYPE_CODE (type2) != TYPE_CODE_FLT
851           && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
852           && !is_integral_type (type2)))
853     error (_("Argument to arithmetic operation not a number or boolean."));
854
855   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
856       || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
857     {
858       struct type *v_type;
859       int len_v1, len_v2, len_v;
860       gdb_byte v1[16], v2[16];
861       gdb_byte v[16];
862
863       /* If only one type is decimal float, use its type.
864          Otherwise use the bigger type.  */
865       if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
866         result_type = type2;
867       else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
868         result_type = type1;
869       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
870         result_type = type2;
871       else
872         result_type = type1;
873
874       len_v = TYPE_LENGTH (result_type);
875
876       value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
877
878       switch (op)
879         {
880         case BINOP_ADD:
881         case BINOP_SUB:
882         case BINOP_MUL:
883         case BINOP_DIV:
884         case BINOP_EXP:
885           decimal_binop (op, v1, len_v1, v2, len_v2, v, len_v);
886           break;
887
888         default:
889           error (_("Operation not valid for decimal floating point number."));
890         }
891
892       val = value_from_decfloat (result_type, v);
893     }
894   else if (TYPE_CODE (type1) == TYPE_CODE_FLT
895            || TYPE_CODE (type2) == TYPE_CODE_FLT)
896     {
897       /* FIXME-if-picky-about-floating-accuracy: Should be doing this
898          in target format.  real.c in GCC probably has the necessary
899          code.  */
900       DOUBLEST v1, v2, v = 0;
901       v1 = value_as_double (arg1);
902       v2 = value_as_double (arg2);
903
904       switch (op)
905         {
906         case BINOP_ADD:
907           v = v1 + v2;
908           break;
909
910         case BINOP_SUB:
911           v = v1 - v2;
912           break;
913
914         case BINOP_MUL:
915           v = v1 * v2;
916           break;
917
918         case BINOP_DIV:
919           v = v1 / v2;
920           break;
921
922         case BINOP_EXP:
923           errno = 0;
924           v = pow (v1, v2);
925           if (errno)
926             error (_("Cannot perform exponentiation: %s"), safe_strerror (errno));
927           break;
928
929         case BINOP_MIN:
930           v = v1 < v2 ? v1 : v2;
931           break;
932               
933         case BINOP_MAX:
934           v = v1 > v2 ? v1 : v2;
935           break;
936
937         default:
938           error (_("Integer-only operation on floating point number."));
939         }
940
941       /* If only one type is float, use its type.
942          Otherwise use the bigger type.  */
943       if (TYPE_CODE (type1) != TYPE_CODE_FLT)
944         result_type = type2;
945       else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
946         result_type = type1;
947       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
948         result_type = type2;
949       else
950         result_type = type1;
951
952       val = allocate_value (result_type);
953       store_typed_floating (value_contents_raw (val), value_type (val), v);
954     }
955   else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
956            || TYPE_CODE (type2) == TYPE_CODE_BOOL)
957     {
958       LONGEST v1, v2, v = 0;
959       v1 = value_as_long (arg1);
960       v2 = value_as_long (arg2);
961
962       switch (op)
963         {
964         case BINOP_BITWISE_AND:
965           v = v1 & v2;
966           break;
967
968         case BINOP_BITWISE_IOR:
969           v = v1 | v2;
970           break;
971
972         case BINOP_BITWISE_XOR:
973           v = v1 ^ v2;
974           break;
975               
976         case BINOP_EQUAL:
977           v = v1 == v2;
978           break;
979           
980         case BINOP_NOTEQUAL:
981           v = v1 != v2;
982           break;
983
984         default:
985           error (_("Invalid operation on booleans."));
986         }
987
988       result_type = type1;
989
990       val = allocate_value (result_type);
991       store_signed_integer (value_contents_raw (val),
992                             TYPE_LENGTH (result_type),
993                             v);
994     }
995   else
996     /* Integral operations here.  */
997     {
998       /* Determine type length of the result, and if the operation should
999          be done unsigned.  For exponentiation and shift operators,
1000          use the length and type of the left operand.  Otherwise,
1001          use the signedness of the operand with the greater length.
1002          If both operands are of equal length, use unsigned operation
1003          if one of the operands is unsigned.  */
1004       if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
1005         result_type = type1;
1006       else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
1007         result_type = type1;
1008       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1009         result_type = type2;
1010       else if (TYPE_UNSIGNED (type1))
1011         result_type = type1;
1012       else if (TYPE_UNSIGNED (type2))
1013         result_type = type2;
1014       else
1015         result_type = type1;
1016
1017       if (TYPE_UNSIGNED (result_type))
1018         {
1019           LONGEST v2_signed = value_as_long (arg2);
1020           ULONGEST v1, v2, v = 0;
1021           v1 = (ULONGEST) value_as_long (arg1);
1022           v2 = (ULONGEST) v2_signed;
1023
1024           switch (op)
1025             {
1026             case BINOP_ADD:
1027               v = v1 + v2;
1028               break;
1029
1030             case BINOP_SUB:
1031               v = v1 - v2;
1032               break;
1033
1034             case BINOP_MUL:
1035               v = v1 * v2;
1036               break;
1037
1038             case BINOP_DIV:
1039             case BINOP_INTDIV:
1040               if (v2 != 0)
1041                 v = v1 / v2;
1042               else
1043                 error (_("Division by zero"));
1044               break;
1045
1046             case BINOP_EXP:
1047               v = uinteger_pow (v1, v2_signed);
1048               break;
1049
1050             case BINOP_REM:
1051               if (v2 != 0)
1052                 v = v1 % v2;
1053               else
1054                 error (_("Division by zero"));
1055               break;
1056
1057             case BINOP_MOD:
1058               /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
1059                  v1 mod 0 has a defined value, v1. */
1060               if (v2 == 0)
1061                 {
1062                   v = v1;
1063                 }
1064               else
1065                 {
1066                   v = v1 / v2;
1067                   /* Note floor(v1/v2) == v1/v2 for unsigned. */
1068                   v = v1 - (v2 * v);
1069                 }
1070               break;
1071
1072             case BINOP_LSH:
1073               v = v1 << v2;
1074               break;
1075
1076             case BINOP_RSH:
1077               v = v1 >> v2;
1078               break;
1079
1080             case BINOP_BITWISE_AND:
1081               v = v1 & v2;
1082               break;
1083
1084             case BINOP_BITWISE_IOR:
1085               v = v1 | v2;
1086               break;
1087
1088             case BINOP_BITWISE_XOR:
1089               v = v1 ^ v2;
1090               break;
1091
1092             case BINOP_LOGICAL_AND:
1093               v = v1 && v2;
1094               break;
1095
1096             case BINOP_LOGICAL_OR:
1097               v = v1 || v2;
1098               break;
1099
1100             case BINOP_MIN:
1101               v = v1 < v2 ? v1 : v2;
1102               break;
1103
1104             case BINOP_MAX:
1105               v = v1 > v2 ? v1 : v2;
1106               break;
1107
1108             case BINOP_EQUAL:
1109               v = v1 == v2;
1110               break;
1111
1112             case BINOP_NOTEQUAL:
1113               v = v1 != v2;
1114               break;
1115
1116             case BINOP_LESS:
1117               v = v1 < v2;
1118               break;
1119
1120             default:
1121               error (_("Invalid binary operation on numbers."));
1122             }
1123
1124           val = allocate_value (result_type);
1125           store_unsigned_integer (value_contents_raw (val),
1126                                   TYPE_LENGTH (value_type (val)),
1127                                   v);
1128         }
1129       else
1130         {
1131           LONGEST v1, v2, v = 0;
1132           v1 = value_as_long (arg1);
1133           v2 = value_as_long (arg2);
1134
1135           switch (op)
1136             {
1137             case BINOP_ADD:
1138               v = v1 + v2;
1139               break;
1140
1141             case BINOP_SUB:
1142               v = v1 - v2;
1143               break;
1144
1145             case BINOP_MUL:
1146               v = v1 * v2;
1147               break;
1148
1149             case BINOP_DIV:
1150             case BINOP_INTDIV:
1151               if (v2 != 0)
1152                 v = v1 / v2;
1153               else
1154                 error (_("Division by zero"));
1155               break;
1156
1157             case BINOP_EXP:
1158               v = integer_pow (v1, v2);
1159               break;
1160
1161             case BINOP_REM:
1162               if (v2 != 0)
1163                 v = v1 % v2;
1164               else
1165                 error (_("Division by zero"));
1166               break;
1167
1168             case BINOP_MOD:
1169               /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
1170                  X mod 0 has a defined value, X. */
1171               if (v2 == 0)
1172                 {
1173                   v = v1;
1174                 }
1175               else
1176                 {
1177                   v = v1 / v2;
1178                   /* Compute floor. */
1179                   if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1180                     {
1181                       v--;
1182                     }
1183                   v = v1 - (v2 * v);
1184                 }
1185               break;
1186
1187             case BINOP_LSH:
1188               v = v1 << v2;
1189               break;
1190
1191             case BINOP_RSH:
1192               v = v1 >> v2;
1193               break;
1194
1195             case BINOP_BITWISE_AND:
1196               v = v1 & v2;
1197               break;
1198
1199             case BINOP_BITWISE_IOR:
1200               v = v1 | v2;
1201               break;
1202
1203             case BINOP_BITWISE_XOR:
1204               v = v1 ^ v2;
1205               break;
1206
1207             case BINOP_LOGICAL_AND:
1208               v = v1 && v2;
1209               break;
1210
1211             case BINOP_LOGICAL_OR:
1212               v = v1 || v2;
1213               break;
1214
1215             case BINOP_MIN:
1216               v = v1 < v2 ? v1 : v2;
1217               break;
1218
1219             case BINOP_MAX:
1220               v = v1 > v2 ? v1 : v2;
1221               break;
1222
1223             case BINOP_EQUAL:
1224               v = v1 == v2;
1225               break;
1226
1227             case BINOP_LESS:
1228               v = v1 < v2;
1229               break;
1230
1231             default:
1232               error (_("Invalid binary operation on numbers."));
1233             }
1234
1235           val = allocate_value (result_type);
1236           store_signed_integer (value_contents_raw (val),
1237                                 TYPE_LENGTH (value_type (val)),
1238                                 v);
1239         }
1240     }
1241
1242   return val;
1243 }
1244 \f
1245 /* Simulate the C operator ! -- return 1 if ARG1 contains zero.  */
1246
1247 int
1248 value_logical_not (struct value *arg1)
1249 {
1250   int len;
1251   const gdb_byte *p;
1252   struct type *type1;
1253
1254   arg1 = coerce_array (arg1);
1255   type1 = check_typedef (value_type (arg1));
1256
1257   if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1258     return 0 == value_as_double (arg1);
1259   else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1260     return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1));
1261
1262   len = TYPE_LENGTH (type1);
1263   p = value_contents (arg1);
1264
1265   while (--len >= 0)
1266     {
1267       if (*p++)
1268         break;
1269     }
1270
1271   return len < 0;
1272 }
1273
1274 /* Perform a comparison on two string values (whose content are not
1275    necessarily null terminated) based on their length */
1276
1277 static int
1278 value_strcmp (struct value *arg1, struct value *arg2)
1279 {
1280   int len1 = TYPE_LENGTH (value_type (arg1));
1281   int len2 = TYPE_LENGTH (value_type (arg2));
1282   const gdb_byte *s1 = value_contents (arg1);
1283   const gdb_byte *s2 = value_contents (arg2);
1284   int i, len = len1 < len2 ? len1 : len2;
1285
1286   for (i = 0; i < len; i++)
1287     {
1288       if (s1[i] < s2[i])
1289         return -1;
1290       else if (s1[i] > s2[i])
1291         return 1;
1292       else
1293         continue;
1294     }
1295
1296   if (len1 < len2)
1297     return -1;
1298   else if (len1 > len2)
1299     return 1;
1300   else
1301     return 0;
1302 }
1303
1304 /* Simulate the C operator == by returning a 1
1305    iff ARG1 and ARG2 have equal contents.  */
1306
1307 int
1308 value_equal (struct value *arg1, struct value *arg2)
1309 {
1310   int len;
1311   const gdb_byte *p1;
1312   const gdb_byte *p2;
1313   struct type *type1, *type2;
1314   enum type_code code1;
1315   enum type_code code2;
1316   int is_int1, is_int2;
1317
1318   arg1 = coerce_array (arg1);
1319   arg2 = coerce_array (arg2);
1320
1321   type1 = check_typedef (value_type (arg1));
1322   type2 = check_typedef (value_type (arg2));
1323   code1 = TYPE_CODE (type1);
1324   code2 = TYPE_CODE (type2);
1325   is_int1 = is_integral_type (type1);
1326   is_int2 = is_integral_type (type2);
1327
1328   if (is_int1 && is_int2)
1329     return longest_to_int (value_as_long (value_binop (arg1, arg2,
1330                                                        BINOP_EQUAL)));
1331   else if ((code1 == TYPE_CODE_FLT || is_int1)
1332            && (code2 == TYPE_CODE_FLT || is_int2))
1333     {
1334       /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1335          `long double' values are returned in static storage (m68k).  */
1336       DOUBLEST d = value_as_double (arg1);
1337       return d == value_as_double (arg2);
1338     }
1339   else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1340            && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1341     {
1342       gdb_byte v1[16], v2[16];
1343       int len_v1, len_v2;
1344
1345       value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1346
1347       return decimal_compare (v1, len_v1, v2, len_v2) == 0;
1348     }
1349
1350   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1351      is bigger.  */
1352   else if (code1 == TYPE_CODE_PTR && is_int2)
1353     return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1354   else if (code2 == TYPE_CODE_PTR && is_int1)
1355     return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1356
1357   else if (code1 == code2
1358            && ((len = (int) TYPE_LENGTH (type1))
1359                == (int) TYPE_LENGTH (type2)))
1360     {
1361       p1 = value_contents (arg1);
1362       p2 = value_contents (arg2);
1363       while (--len >= 0)
1364         {
1365           if (*p1++ != *p2++)
1366             break;
1367         }
1368       return len < 0;
1369     }
1370   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1371     {
1372       return value_strcmp (arg1, arg2) == 0;
1373     }
1374   else
1375     {
1376       error (_("Invalid type combination in equality test."));
1377       return 0;                 /* For lint -- never reached */
1378     }
1379 }
1380
1381 /* Simulate the C operator < by returning 1
1382    iff ARG1's contents are less than ARG2's.  */
1383
1384 int
1385 value_less (struct value *arg1, struct value *arg2)
1386 {
1387   enum type_code code1;
1388   enum type_code code2;
1389   struct type *type1, *type2;
1390   int is_int1, is_int2;
1391
1392   arg1 = coerce_array (arg1);
1393   arg2 = coerce_array (arg2);
1394
1395   type1 = check_typedef (value_type (arg1));
1396   type2 = check_typedef (value_type (arg2));
1397   code1 = TYPE_CODE (type1);
1398   code2 = TYPE_CODE (type2);
1399   is_int1 = is_integral_type (type1);
1400   is_int2 = is_integral_type (type2);
1401
1402   if (is_int1 && is_int2)
1403     return longest_to_int (value_as_long (value_binop (arg1, arg2,
1404                                                        BINOP_LESS)));
1405   else if ((code1 == TYPE_CODE_FLT || is_int1)
1406            && (code2 == TYPE_CODE_FLT || is_int2))
1407     {
1408       /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1409          `long double' values are returned in static storage (m68k).  */
1410       DOUBLEST d = value_as_double (arg1);
1411       return d < value_as_double (arg2);
1412     }
1413   else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1414            && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1415     {
1416       gdb_byte v1[16], v2[16];
1417       int len_v1, len_v2;
1418
1419       value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1420
1421       return decimal_compare (v1, len_v1, v2, len_v2) == -1;
1422     }
1423   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1424     return value_as_address (arg1) < value_as_address (arg2);
1425
1426   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1427      is bigger.  */
1428   else if (code1 == TYPE_CODE_PTR && is_int2)
1429     return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1430   else if (code2 == TYPE_CODE_PTR && is_int1)
1431     return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1432   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1433     return value_strcmp (arg1, arg2) < 0;
1434   else
1435     {
1436       error (_("Invalid type combination in ordering comparison."));
1437       return 0;
1438     }
1439 }
1440 \f
1441 /* The unary operators +, - and ~.  They free the argument ARG1.  */
1442
1443 struct value *
1444 value_pos (struct value *arg1)
1445 {
1446   struct type *type;
1447
1448   arg1 = coerce_ref (arg1);
1449   type = check_typedef (value_type (arg1));
1450
1451   if (TYPE_CODE (type) == TYPE_CODE_FLT)
1452     return value_from_double (type, value_as_double (arg1));
1453   else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1454     return value_from_decfloat (type, value_contents (arg1));
1455   else if (is_integral_type (type))
1456     {
1457       return value_from_longest (type, value_as_long (arg1));
1458     }
1459   else
1460     {
1461       error ("Argument to positive operation not a number.");
1462       return 0;                 /* For lint -- never reached */
1463     }
1464 }
1465
1466 struct value *
1467 value_neg (struct value *arg1)
1468 {
1469   struct type *type;
1470
1471   arg1 = coerce_ref (arg1);
1472   type = check_typedef (value_type (arg1));
1473
1474   if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1475     {
1476       struct value *val = allocate_value (type);
1477       int len = TYPE_LENGTH (type);
1478       gdb_byte decbytes[16];  /* a decfloat is at most 128 bits long */
1479
1480       memcpy (decbytes, value_contents (arg1), len);
1481
1482       if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_LITTLE)
1483         decbytes[len-1] = decbytes[len - 1] | 0x80;
1484       else
1485         decbytes[0] = decbytes[0] | 0x80;
1486
1487       memcpy (value_contents_raw (val), decbytes, len);
1488       return val;
1489     }
1490   else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1491     return value_from_double (type, -value_as_double (arg1));
1492   else if (is_integral_type (type))
1493     {
1494       return value_from_longest (type, -value_as_long (arg1));
1495     }
1496   else
1497     {
1498       error (_("Argument to negate operation not a number."));
1499       return 0;                 /* For lint -- never reached */
1500     }
1501 }
1502
1503 struct value *
1504 value_complement (struct value *arg1)
1505 {
1506   struct type *type;
1507
1508   arg1 = coerce_ref (arg1);
1509   type = check_typedef (value_type (arg1));
1510
1511   if (!is_integral_type (type))
1512     error (_("Argument to complement operation not an integer or boolean."));
1513
1514   return value_from_longest (type, ~value_as_long (arg1));
1515 }
1516 \f
1517 /* The INDEX'th bit of SET value whose value_type is TYPE,
1518    and whose value_contents is valaddr.
1519    Return -1 if out of range, -2 other error. */
1520
1521 int
1522 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1523 {
1524   LONGEST low_bound, high_bound;
1525   LONGEST word;
1526   unsigned rel_index;
1527   struct type *range = TYPE_INDEX_TYPE (type);
1528   if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1529     return -2;
1530   if (index < low_bound || index > high_bound)
1531     return -1;
1532   rel_index = index - low_bound;
1533   word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1);
1534   rel_index %= TARGET_CHAR_BIT;
1535   if (gdbarch_bits_big_endian (current_gdbarch))
1536     rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1537   return (word >> rel_index) & 1;
1538 }
1539
1540 int
1541 value_in (struct value *element, struct value *set)
1542 {
1543   int member;
1544   struct type *settype = check_typedef (value_type (set));
1545   struct type *eltype = check_typedef (value_type (element));
1546   if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1547     eltype = TYPE_TARGET_TYPE (eltype);
1548   if (TYPE_CODE (settype) != TYPE_CODE_SET)
1549     error (_("Second argument of 'IN' has wrong type"));
1550   if (TYPE_CODE (eltype) != TYPE_CODE_INT
1551       && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1552       && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1553       && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1554     error (_("First argument of 'IN' has wrong type"));
1555   member = value_bit_index (settype, value_contents (set),
1556                             value_as_long (element));
1557   if (member < 0)
1558     error (_("First argument of 'IN' not in range"));
1559   return member;
1560 }
1561
1562 void
1563 _initialize_valarith (void)
1564 {
1565 }