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