use do_align () directly in tc-ia64.c
[external/binutils.git] / gdb / valarith.c
1 /* Perform arithmetic and other operations on values, for GDB.
2
3    Copyright (C) 1986-2016 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "value.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "target.h"
26 #include "language.h"
27 #include "doublest.h"
28 #include "dfp.h"
29 #include <math.h>
30 #include "infcall.h"
31
32 /* Define whether or not the C operator '/' truncates towards zero for
33    differently signed operands (truncation direction is undefined in C).  */
34
35 #ifndef TRUNCATION_TOWARDS_ZERO
36 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
37 #endif
38
39 void _initialize_valarith (void);
40 \f
41
42 /* Given a pointer, return the size of its target.
43    If the pointer type is void *, then return 1.
44    If the target type is incomplete, then error out.
45    This isn't a general purpose function, but just a 
46    helper for value_ptradd.  */
47
48 static LONGEST
49 find_size_for_pointer_math (struct type *ptr_type)
50 {
51   LONGEST sz = -1;
52   struct type *ptr_target;
53
54   gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR);
55   ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
56
57   sz = type_length_units (ptr_target);
58   if (sz == 0)
59     {
60       if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
61         sz = 1;
62       else
63         {
64           const char *name;
65           
66           name = TYPE_NAME (ptr_target);
67           if (name == NULL)
68             name = TYPE_TAG_NAME (ptr_target);
69           if (name == NULL)
70             error (_("Cannot perform pointer math on incomplete types, "
71                    "try casting to a known type, or void *."));
72           else
73             error (_("Cannot perform pointer math on incomplete type \"%s\", "
74                    "try casting to a known type, or void *."), name);
75         }
76     }
77   return sz;
78 }
79
80 /* Given a pointer ARG1 and an integral value ARG2, return the
81    result of C-style pointer arithmetic ARG1 + ARG2.  */
82
83 struct value *
84 value_ptradd (struct value *arg1, LONGEST arg2)
85 {
86   struct type *valptrtype;
87   LONGEST sz;
88   struct value *result;
89
90   arg1 = coerce_array (arg1);
91   valptrtype = check_typedef (value_type (arg1));
92   sz = find_size_for_pointer_math (valptrtype);
93
94   result = value_from_pointer (valptrtype,
95                                value_as_address (arg1) + sz * arg2);
96   if (VALUE_LVAL (result) != lval_internalvar)
97     set_value_component_location (result, arg1);
98   return result;
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 (_("First argument of `-' is a pointer and "
121              "second argument is neither\n"
122              "an integer nor a pointer of the same type."));
123
124   sz = type_length_units (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
140    See comments in value_coerce_array() for rationale for reason for
141    doing lower bounds adjustment here rather than there.
142    FIXME:  Perhaps we should validate that the index is valid and if
143    verbosity is set, warn about invalid indices (but still use them).  */
144
145 struct value *
146 value_subscript (struct value *array, LONGEST index)
147 {
148   int c_style = current_language->c_style_arrays;
149   struct type *tarray;
150
151   array = coerce_ref (array);
152   tarray = check_typedef (value_type (array));
153
154   if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
155       || TYPE_CODE (tarray) == TYPE_CODE_STRING)
156     {
157       struct type *range_type = TYPE_INDEX_TYPE (tarray);
158       LONGEST lowerbound, upperbound;
159
160       get_discrete_bounds (range_type, &lowerbound, &upperbound);
161       if (VALUE_LVAL (array) != lval_memory)
162         return value_subscripted_rvalue (array, index, lowerbound);
163
164       if (c_style == 0)
165         {
166           if (index >= lowerbound && index <= upperbound)
167             return value_subscripted_rvalue (array, index, lowerbound);
168           /* Emit warning unless we have an array of unknown size.
169              An array of unknown size has lowerbound 0 and upperbound -1.  */
170           if (upperbound > -1)
171             warning (_("array or string index out of range"));
172           /* fall doing C stuff */
173           c_style = 1;
174         }
175
176       index -= lowerbound;
177       array = value_coerce_array (array);
178     }
179
180   if (c_style)
181     return value_ind (value_ptradd (array, index));
182   else
183     error (_("not an array or string"));
184 }
185
186 /* Return the value of EXPR[IDX], expr an aggregate rvalue
187    (eg, a vector register).  This routine used to promote floats
188    to doubles, but no longer does.  */
189
190 struct value *
191 value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
192 {
193   struct type *array_type = check_typedef (value_type (array));
194   struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
195   unsigned int elt_size = type_length_units (elt_type);
196   unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
197   struct value *v;
198
199   if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type)
200                              && elt_offs >= type_length_units (array_type)))
201     {
202       if (type_not_associated (array_type))
203         error (_("no such vector element (vector not associated)"));
204       else if (type_not_allocated (array_type))
205         error (_("no such vector element (vector not allocated)"));
206       else
207         error (_("no such vector element"));
208     }
209
210   if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
211     v = allocate_value_lazy (elt_type);
212   else
213     {
214       v = allocate_value (elt_type);
215       value_contents_copy (v, value_embedded_offset (v),
216                            array, value_embedded_offset (array) + elt_offs,
217                            elt_size);
218     }
219
220   set_value_component_location (v, array);
221   VALUE_REGNUM (v) = VALUE_REGNUM (array);
222   VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
223   set_value_offset (v, value_offset (array) + elt_offs);
224   return v;
225 }
226
227 \f
228 /* Check to see if either argument is a structure, or a reference to
229    one.  This is called so we know whether to go ahead with the normal
230    binop or look for a user defined function instead.
231
232    For now, we do not overload the `=' operator.  */
233
234 int
235 binop_types_user_defined_p (enum exp_opcode op,
236                             struct type *type1, struct type *type2)
237 {
238   if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
239     return 0;
240
241   type1 = check_typedef (type1);
242   if (TYPE_CODE (type1) == TYPE_CODE_REF)
243     type1 = check_typedef (TYPE_TARGET_TYPE (type1));
244
245   type2 = check_typedef (type2);
246   if (TYPE_CODE (type2) == TYPE_CODE_REF)
247     type2 = check_typedef (TYPE_TARGET_TYPE (type2));
248
249   return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
250           || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
251 }
252
253 /* Check to see if either argument is a structure, or a reference to
254    one.  This is called so we know whether to go ahead with the normal
255    binop or look for a user defined function instead.
256
257    For now, we do not overload the `=' operator.  */
258
259 int
260 binop_user_defined_p (enum exp_opcode op,
261                       struct value *arg1, struct value *arg2)
262 {
263   return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2));
264 }
265
266 /* Check to see if argument is a structure.  This is called so
267    we know whether to go ahead with the normal unop or look for a 
268    user defined function instead.
269
270    For now, we do not overload the `&' operator.  */
271
272 int
273 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
274 {
275   struct type *type1;
276
277   if (op == UNOP_ADDR)
278     return 0;
279   type1 = check_typedef (value_type (arg1));
280   if (TYPE_CODE (type1) == TYPE_CODE_REF)
281     type1 = check_typedef (TYPE_TARGET_TYPE (type1));
282   return TYPE_CODE (type1) == TYPE_CODE_STRUCT;
283 }
284
285 /* Try to find an operator named OPERATOR which takes NARGS arguments
286    specified in ARGS.  If the operator found is a static member operator
287    *STATIC_MEMFUNP will be set to 1, and otherwise 0.
288    The search if performed through find_overload_match which will handle
289    member operators, non member operators, operators imported implicitly or
290    explicitly, and perform correct overload resolution in all of the above
291    situations or combinations thereof.  */
292
293 static struct value *
294 value_user_defined_cpp_op (struct value **args, int nargs, char *oper,
295                            int *static_memfuncp, enum noside noside)
296 {
297
298   struct symbol *symp = NULL;
299   struct value *valp = NULL;
300
301   find_overload_match (args, nargs, oper, BOTH /* could be method */,
302                        &args[0] /* objp */,
303                        NULL /* pass NULL symbol since symbol is unknown */,
304                        &valp, &symp, static_memfuncp, 0, noside);
305
306   if (valp)
307     return valp;
308
309   if (symp)
310     {
311       /* This is a non member function and does not
312          expect a reference as its first argument
313          rather the explicit structure.  */
314       args[0] = value_ind (args[0]);
315       return value_of_variable (symp, 0);
316     }
317
318   error (_("Could not find %s."), oper);
319 }
320
321 /* Lookup user defined operator NAME.  Return a value representing the
322    function, otherwise return NULL.  */
323
324 static struct value *
325 value_user_defined_op (struct value **argp, struct value **args, char *name,
326                        int *static_memfuncp, int nargs, enum noside noside)
327 {
328   struct value *result = NULL;
329
330   if (current_language->la_language == language_cplus)
331     {
332       result = value_user_defined_cpp_op (args, nargs, name, static_memfuncp,
333                                           noside);
334     }
335   else
336     result = value_struct_elt (argp, args, name, static_memfuncp,
337                                "structure");
338
339   return result;
340 }
341
342 /* We know either arg1 or arg2 is a structure, so try to find the right
343    user defined function.  Create an argument vector that calls 
344    arg1.operator @ (arg1,arg2) and return that value (where '@' is any
345    binary operator which is legal for GNU C++).
346
347    OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
348    is the opcode saying how to modify it.  Otherwise, OTHEROP is
349    unused.  */
350
351 struct value *
352 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
353                enum exp_opcode otherop, enum noside noside)
354 {
355   struct value **argvec;
356   char *ptr;
357   char tstr[13];
358   int static_memfuncp;
359
360   arg1 = coerce_ref (arg1);
361   arg2 = coerce_ref (arg2);
362
363   /* now we know that what we have to do is construct our
364      arg vector and find the right function to call it with.  */
365
366   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
367     error (_("Can't do that binary op on that type"));  /* FIXME be explicit */
368
369   argvec = (struct value **) alloca (sizeof (struct value *) * 4);
370   argvec[1] = value_addr (arg1);
371   argvec[2] = arg2;
372   argvec[3] = 0;
373
374   /* Make the right function name up.  */
375   strcpy (tstr, "operator__");
376   ptr = tstr + 8;
377   switch (op)
378     {
379     case BINOP_ADD:
380       strcpy (ptr, "+");
381       break;
382     case BINOP_SUB:
383       strcpy (ptr, "-");
384       break;
385     case BINOP_MUL:
386       strcpy (ptr, "*");
387       break;
388     case BINOP_DIV:
389       strcpy (ptr, "/");
390       break;
391     case BINOP_REM:
392       strcpy (ptr, "%");
393       break;
394     case BINOP_LSH:
395       strcpy (ptr, "<<");
396       break;
397     case BINOP_RSH:
398       strcpy (ptr, ">>");
399       break;
400     case BINOP_BITWISE_AND:
401       strcpy (ptr, "&");
402       break;
403     case BINOP_BITWISE_IOR:
404       strcpy (ptr, "|");
405       break;
406     case BINOP_BITWISE_XOR:
407       strcpy (ptr, "^");
408       break;
409     case BINOP_LOGICAL_AND:
410       strcpy (ptr, "&&");
411       break;
412     case BINOP_LOGICAL_OR:
413       strcpy (ptr, "||");
414       break;
415     case BINOP_MIN:
416       strcpy (ptr, "<?");
417       break;
418     case BINOP_MAX:
419       strcpy (ptr, ">?");
420       break;
421     case BINOP_ASSIGN:
422       strcpy (ptr, "=");
423       break;
424     case BINOP_ASSIGN_MODIFY:
425       switch (otherop)
426         {
427         case BINOP_ADD:
428           strcpy (ptr, "+=");
429           break;
430         case BINOP_SUB:
431           strcpy (ptr, "-=");
432           break;
433         case BINOP_MUL:
434           strcpy (ptr, "*=");
435           break;
436         case BINOP_DIV:
437           strcpy (ptr, "/=");
438           break;
439         case BINOP_REM:
440           strcpy (ptr, "%=");
441           break;
442         case BINOP_BITWISE_AND:
443           strcpy (ptr, "&=");
444           break;
445         case BINOP_BITWISE_IOR:
446           strcpy (ptr, "|=");
447           break;
448         case BINOP_BITWISE_XOR:
449           strcpy (ptr, "^=");
450           break;
451         case BINOP_MOD: /* invalid */
452         default:
453           error (_("Invalid binary operation specified."));
454         }
455       break;
456     case BINOP_SUBSCRIPT:
457       strcpy (ptr, "[]");
458       break;
459     case BINOP_EQUAL:
460       strcpy (ptr, "==");
461       break;
462     case BINOP_NOTEQUAL:
463       strcpy (ptr, "!=");
464       break;
465     case BINOP_LESS:
466       strcpy (ptr, "<");
467       break;
468     case BINOP_GTR:
469       strcpy (ptr, ">");
470       break;
471     case BINOP_GEQ:
472       strcpy (ptr, ">=");
473       break;
474     case BINOP_LEQ:
475       strcpy (ptr, "<=");
476       break;
477     case BINOP_MOD:             /* invalid */
478     default:
479       error (_("Invalid binary operation specified."));
480     }
481
482   argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
483                                      &static_memfuncp, 2, noside);
484
485   if (argvec[0])
486     {
487       if (static_memfuncp)
488         {
489           argvec[1] = argvec[0];
490           argvec++;
491         }
492       if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_XMETHOD)
493         {
494           /* Static xmethods are not supported yet.  */
495           gdb_assert (static_memfuncp == 0);
496           if (noside == EVAL_AVOID_SIDE_EFFECTS)
497             {
498               struct type *return_type
499                 = result_type_of_xmethod (argvec[0], 2, argvec + 1);
500
501               if (return_type == NULL)
502                 error (_("Xmethod is missing return type."));
503               return value_zero (return_type, VALUE_LVAL (arg1));
504             }
505           return call_xmethod (argvec[0], 2, argvec + 1);
506         }
507       if (noside == EVAL_AVOID_SIDE_EFFECTS)
508         {
509           struct type *return_type;
510
511           return_type
512             = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
513           return value_zero (return_type, VALUE_LVAL (arg1));
514         }
515       return call_function_by_hand (argvec[0], 2 - static_memfuncp,
516                                     argvec + 1);
517     }
518   throw_error (NOT_FOUND_ERROR,
519                _("member function %s not found"), tstr);
520 #ifdef lint
521   return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
522 #endif
523 }
524
525 /* We know that arg1 is a structure, so try to find a unary user
526    defined operator that matches the operator in question.
527    Create an argument vector that calls arg1.operator @ (arg1)
528    and return that value (where '@' is (almost) any unary operator which
529    is legal for GNU C++).  */
530
531 struct value *
532 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
533 {
534   struct gdbarch *gdbarch = get_type_arch (value_type (arg1));
535   struct value **argvec;
536   char *ptr;
537   char tstr[13], mangle_tstr[13];
538   int static_memfuncp, nargs;
539
540   arg1 = coerce_ref (arg1);
541
542   /* now we know that what we have to do is construct our
543      arg vector and find the right function to call it with.  */
544
545   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
546     error (_("Can't do that unary op on that type"));   /* FIXME be explicit */
547
548   argvec = (struct value **) alloca (sizeof (struct value *) * 4);
549   argvec[1] = value_addr (arg1);
550   argvec[2] = 0;
551
552   nargs = 1;
553
554   /* Make the right function name up.  */
555   strcpy (tstr, "operator__");
556   ptr = tstr + 8;
557   strcpy (mangle_tstr, "__");
558   switch (op)
559     {
560     case UNOP_PREINCREMENT:
561       strcpy (ptr, "++");
562       break;
563     case UNOP_PREDECREMENT:
564       strcpy (ptr, "--");
565       break;
566     case UNOP_POSTINCREMENT:
567       strcpy (ptr, "++");
568       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
569       argvec[3] = 0;
570       nargs ++;
571       break;
572     case UNOP_POSTDECREMENT:
573       strcpy (ptr, "--");
574       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
575       argvec[3] = 0;
576       nargs ++;
577       break;
578     case UNOP_LOGICAL_NOT:
579       strcpy (ptr, "!");
580       break;
581     case UNOP_COMPLEMENT:
582       strcpy (ptr, "~");
583       break;
584     case UNOP_NEG:
585       strcpy (ptr, "-");
586       break;
587     case UNOP_PLUS:
588       strcpy (ptr, "+");
589       break;
590     case UNOP_IND:
591       strcpy (ptr, "*");
592       break;
593     case STRUCTOP_PTR:
594       strcpy (ptr, "->");
595       break;
596     default:
597       error (_("Invalid unary operation specified."));
598     }
599
600   argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
601                                      &static_memfuncp, nargs, noside);
602
603   if (argvec[0])
604     {
605       if (static_memfuncp)
606         {
607           argvec[1] = argvec[0];
608           nargs --;
609           argvec++;
610         }
611       if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_XMETHOD)
612         {
613           /* Static xmethods are not supported yet.  */
614           gdb_assert (static_memfuncp == 0);
615           if (noside == EVAL_AVOID_SIDE_EFFECTS)
616             {
617               struct type *return_type
618                 = result_type_of_xmethod (argvec[0], 1, argvec + 1);
619
620               if (return_type == NULL)
621                 error (_("Xmethod is missing return type."));
622               return value_zero (return_type, VALUE_LVAL (arg1));
623             }
624           return call_xmethod (argvec[0], 1, argvec + 1);
625         }
626       if (noside == EVAL_AVOID_SIDE_EFFECTS)
627         {
628           struct type *return_type;
629
630           return_type
631             = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
632           return value_zero (return_type, VALUE_LVAL (arg1));
633         }
634       return call_function_by_hand (argvec[0], nargs, argvec + 1);
635     }
636   throw_error (NOT_FOUND_ERROR,
637                _("member function %s not found"), tstr);
638
639   return 0;                     /* For lint -- never reached */
640 }
641 \f
642
643 /* Concatenate two values with the following conditions:
644
645    (1)  Both values must be either bitstring values or character string
646    values and the resulting value consists of the concatenation of
647    ARG1 followed by ARG2.
648
649    or
650
651    One value must be an integer value and the other value must be
652    either a bitstring value or character string value, which is
653    to be repeated by the number of times specified by the integer
654    value.
655
656
657    (2)  Boolean values are also allowed and are treated as bit string
658    values of length 1.
659
660    (3)  Character values are also allowed and are treated as character
661    string values of length 1.  */
662
663 struct value *
664 value_concat (struct value *arg1, struct value *arg2)
665 {
666   struct value *inval1;
667   struct value *inval2;
668   struct value *outval = NULL;
669   int inval1len, inval2len;
670   int count, idx;
671   char *ptr;
672   char inchar;
673   struct type *type1 = check_typedef (value_type (arg1));
674   struct type *type2 = check_typedef (value_type (arg2));
675   struct type *char_type;
676
677   /* First figure out if we are dealing with two values to be concatenated
678      or a repeat count and a value to be repeated.  INVAL1 is set to the
679      first of two concatenated values, or the repeat count.  INVAL2 is set
680      to the second of the two concatenated values or the value to be 
681      repeated.  */
682
683   if (TYPE_CODE (type2) == TYPE_CODE_INT)
684     {
685       struct type *tmp = type1;
686
687       type1 = tmp;
688       tmp = type2;
689       inval1 = arg2;
690       inval2 = arg1;
691     }
692   else
693     {
694       inval1 = arg1;
695       inval2 = arg2;
696     }
697
698   /* Now process the input values.  */
699
700   if (TYPE_CODE (type1) == TYPE_CODE_INT)
701     {
702       /* We have a repeat count.  Validate the second value and then
703          construct a value repeated that many times.  */
704       if (TYPE_CODE (type2) == TYPE_CODE_STRING
705           || TYPE_CODE (type2) == TYPE_CODE_CHAR)
706         {
707           struct cleanup *back_to;
708
709           count = longest_to_int (value_as_long (inval1));
710           inval2len = TYPE_LENGTH (type2);
711           ptr = (char *) xmalloc (count * inval2len);
712           back_to = make_cleanup (xfree, ptr);
713           if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
714             {
715               char_type = type2;
716
717               inchar = (char) unpack_long (type2,
718                                            value_contents (inval2));
719               for (idx = 0; idx < count; idx++)
720                 {
721                   *(ptr + idx) = inchar;
722                 }
723             }
724           else
725             {
726               char_type = TYPE_TARGET_TYPE (type2);
727
728               for (idx = 0; idx < count; idx++)
729                 {
730                   memcpy (ptr + (idx * inval2len), value_contents (inval2),
731                           inval2len);
732                 }
733             }
734           outval = value_string (ptr, count * inval2len, char_type);
735           do_cleanups (back_to);
736         }
737       else if (TYPE_CODE (type2) == TYPE_CODE_BOOL)
738         {
739           error (_("unimplemented support for boolean repeats"));
740         }
741       else
742         {
743           error (_("can't repeat values of that type"));
744         }
745     }
746   else if (TYPE_CODE (type1) == TYPE_CODE_STRING
747            || TYPE_CODE (type1) == TYPE_CODE_CHAR)
748     {
749       struct cleanup *back_to;
750
751       /* We have two character strings to concatenate.  */
752       if (TYPE_CODE (type2) != TYPE_CODE_STRING
753           && TYPE_CODE (type2) != TYPE_CODE_CHAR)
754         {
755           error (_("Strings can only be concatenated with other strings."));
756         }
757       inval1len = TYPE_LENGTH (type1);
758       inval2len = TYPE_LENGTH (type2);
759       ptr = (char *) xmalloc (inval1len + inval2len);
760       back_to = make_cleanup (xfree, ptr);
761       if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
762         {
763           char_type = type1;
764
765           *ptr = (char) unpack_long (type1, value_contents (inval1));
766         }
767       else
768         {
769           char_type = TYPE_TARGET_TYPE (type1);
770
771           memcpy (ptr, value_contents (inval1), inval1len);
772         }
773       if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
774         {
775           *(ptr + inval1len) =
776             (char) unpack_long (type2, value_contents (inval2));
777         }
778       else
779         {
780           memcpy (ptr + inval1len, value_contents (inval2), inval2len);
781         }
782       outval = value_string (ptr, inval1len + inval2len, char_type);
783       do_cleanups (back_to);
784     }
785   else if (TYPE_CODE (type1) == TYPE_CODE_BOOL)
786     {
787       /* We have two bitstrings to concatenate.  */
788       if (TYPE_CODE (type2) != TYPE_CODE_BOOL)
789         {
790           error (_("Booleans can only be concatenated "
791                    "with other bitstrings or booleans."));
792         }
793       error (_("unimplemented support for boolean concatenation."));
794     }
795   else
796     {
797       /* We don't know how to concatenate these operands.  */
798       error (_("illegal operands for concatenation."));
799     }
800   return (outval);
801 }
802 \f
803 /* Integer exponentiation: V1**V2, where both arguments are
804    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
805
806 static LONGEST
807 integer_pow (LONGEST v1, LONGEST v2)
808 {
809   if (v2 < 0)
810     {
811       if (v1 == 0)
812         error (_("Attempt to raise 0 to negative power."));
813       else
814         return 0;
815     }
816   else 
817     {
818       /* The Russian Peasant's Algorithm.  */
819       LONGEST v;
820       
821       v = 1;
822       for (;;)
823         {
824           if (v2 & 1L) 
825             v *= v1;
826           v2 >>= 1;
827           if (v2 == 0)
828             return v;
829           v1 *= v1;
830         }
831     }
832 }
833
834 /* Integer exponentiation: V1**V2, where both arguments are
835    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
836
837 static ULONGEST
838 uinteger_pow (ULONGEST v1, LONGEST v2)
839 {
840   if (v2 < 0)
841     {
842       if (v1 == 0)
843         error (_("Attempt to raise 0 to negative power."));
844       else
845         return 0;
846     }
847   else 
848     {
849       /* The Russian Peasant's Algorithm.  */
850       ULONGEST v;
851       
852       v = 1;
853       for (;;)
854         {
855           if (v2 & 1L) 
856             v *= v1;
857           v2 >>= 1;
858           if (v2 == 0)
859             return v;
860           v1 *= v1;
861         }
862     }
863 }
864
865 /* Obtain decimal value of arguments for binary operation, converting from
866    other types if one of them is not decimal floating point.  */
867 static void
868 value_args_as_decimal (struct value *arg1, struct value *arg2,
869                        gdb_byte *x, int *len_x, enum bfd_endian *byte_order_x,
870                        gdb_byte *y, int *len_y, enum bfd_endian *byte_order_y)
871 {
872   struct type *type1, *type2;
873
874   type1 = check_typedef (value_type (arg1));
875   type2 = check_typedef (value_type (arg2));
876
877   /* At least one of the arguments must be of decimal float type.  */
878   gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
879               || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
880
881   if (TYPE_CODE (type1) == TYPE_CODE_FLT
882       || TYPE_CODE (type2) == TYPE_CODE_FLT)
883     /* The DFP extension to the C language does not allow mixing of
884      * decimal float types with other float types in expressions
885      * (see WDTR 24732, page 12).  */
886     error (_("Mixing decimal floating types with "
887              "other floating types is not allowed."));
888
889   /* Obtain decimal value of arg1, converting from other types
890      if necessary.  */
891
892   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
893     {
894       *byte_order_x = gdbarch_byte_order (get_type_arch (type1));
895       *len_x = TYPE_LENGTH (type1);
896       memcpy (x, value_contents (arg1), *len_x);
897     }
898   else if (is_integral_type (type1))
899     {
900       *byte_order_x = gdbarch_byte_order (get_type_arch (type2));
901       *len_x = TYPE_LENGTH (type2);
902       decimal_from_integral (arg1, x, *len_x, *byte_order_x);
903     }
904   else
905     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
906              TYPE_NAME (type2));
907
908   /* Obtain decimal value of arg2, converting from other types
909      if necessary.  */
910
911   if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
912     {
913       *byte_order_y = gdbarch_byte_order (get_type_arch (type2));
914       *len_y = TYPE_LENGTH (type2);
915       memcpy (y, value_contents (arg2), *len_y);
916     }
917   else if (is_integral_type (type2))
918     {
919       *byte_order_y = gdbarch_byte_order (get_type_arch (type1));
920       *len_y = TYPE_LENGTH (type1);
921       decimal_from_integral (arg2, y, *len_y, *byte_order_y);
922     }
923   else
924     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
925              TYPE_NAME (type2));
926 }
927
928 /* Perform a binary operation on two operands which have reasonable
929    representations as integers or floats.  This includes booleans,
930    characters, integers, or floats.
931    Does not support addition and subtraction on pointers;
932    use value_ptradd, value_ptrsub or value_ptrdiff for those operations.  */
933
934 static struct value *
935 scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
936 {
937   struct value *val;
938   struct type *type1, *type2, *result_type;
939
940   arg1 = coerce_ref (arg1);
941   arg2 = coerce_ref (arg2);
942
943   type1 = check_typedef (value_type (arg1));
944   type2 = check_typedef (value_type (arg2));
945
946   if ((TYPE_CODE (type1) != TYPE_CODE_FLT
947        && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
948        && !is_integral_type (type1))
949       || (TYPE_CODE (type2) != TYPE_CODE_FLT
950           && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
951           && !is_integral_type (type2)))
952     error (_("Argument to arithmetic operation not a number or boolean."));
953
954   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
955       || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
956     {
957       int len_v1, len_v2, len_v;
958       enum bfd_endian byte_order_v1, byte_order_v2, byte_order_v;
959       gdb_byte v1[16], v2[16];
960       gdb_byte v[16];
961
962       /* If only one type is decimal float, use its type.
963          Otherwise use the bigger type.  */
964       if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
965         result_type = type2;
966       else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
967         result_type = type1;
968       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
969         result_type = type2;
970       else
971         result_type = type1;
972
973       len_v = TYPE_LENGTH (result_type);
974       byte_order_v = gdbarch_byte_order (get_type_arch (result_type));
975
976       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
977                                          v2, &len_v2, &byte_order_v2);
978
979       switch (op)
980         {
981         case BINOP_ADD:
982         case BINOP_SUB:
983         case BINOP_MUL:
984         case BINOP_DIV:
985         case BINOP_EXP:
986           decimal_binop (op, v1, len_v1, byte_order_v1,
987                              v2, len_v2, byte_order_v2,
988                              v, len_v, byte_order_v);
989           break;
990
991         default:
992           error (_("Operation not valid for decimal floating point number."));
993         }
994
995       val = value_from_decfloat (result_type, v);
996     }
997   else if (TYPE_CODE (type1) == TYPE_CODE_FLT
998            || TYPE_CODE (type2) == TYPE_CODE_FLT)
999     {
1000       /* FIXME-if-picky-about-floating-accuracy: Should be doing this
1001          in target format.  real.c in GCC probably has the necessary
1002          code.  */
1003       DOUBLEST v1, v2, v = 0;
1004
1005       v1 = value_as_double (arg1);
1006       v2 = value_as_double (arg2);
1007
1008       switch (op)
1009         {
1010         case BINOP_ADD:
1011           v = v1 + v2;
1012           break;
1013
1014         case BINOP_SUB:
1015           v = v1 - v2;
1016           break;
1017
1018         case BINOP_MUL:
1019           v = v1 * v2;
1020           break;
1021
1022         case BINOP_DIV:
1023           v = v1 / v2;
1024           break;
1025
1026         case BINOP_EXP:
1027           errno = 0;
1028           v = pow (v1, v2);
1029           if (errno)
1030             error (_("Cannot perform exponentiation: %s"),
1031                    safe_strerror (errno));
1032           break;
1033
1034         case BINOP_MIN:
1035           v = v1 < v2 ? v1 : v2;
1036           break;
1037               
1038         case BINOP_MAX:
1039           v = v1 > v2 ? v1 : v2;
1040           break;
1041
1042         default:
1043           error (_("Integer-only operation on floating point number."));
1044         }
1045
1046       /* If only one type is float, use its type.
1047          Otherwise use the bigger type.  */
1048       if (TYPE_CODE (type1) != TYPE_CODE_FLT)
1049         result_type = type2;
1050       else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
1051         result_type = type1;
1052       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1053         result_type = type2;
1054       else
1055         result_type = type1;
1056
1057       val = allocate_value (result_type);
1058       store_typed_floating (value_contents_raw (val), value_type (val), v);
1059     }
1060   else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
1061            || TYPE_CODE (type2) == TYPE_CODE_BOOL)
1062     {
1063       LONGEST v1, v2, v = 0;
1064
1065       v1 = value_as_long (arg1);
1066       v2 = value_as_long (arg2);
1067
1068       switch (op)
1069         {
1070         case BINOP_BITWISE_AND:
1071           v = v1 & v2;
1072           break;
1073
1074         case BINOP_BITWISE_IOR:
1075           v = v1 | v2;
1076           break;
1077
1078         case BINOP_BITWISE_XOR:
1079           v = v1 ^ v2;
1080           break;
1081               
1082         case BINOP_EQUAL:
1083           v = v1 == v2;
1084           break;
1085           
1086         case BINOP_NOTEQUAL:
1087           v = v1 != v2;
1088           break;
1089
1090         default:
1091           error (_("Invalid operation on booleans."));
1092         }
1093
1094       result_type = type1;
1095
1096       val = allocate_value (result_type);
1097       store_signed_integer (value_contents_raw (val),
1098                             TYPE_LENGTH (result_type),
1099                             gdbarch_byte_order (get_type_arch (result_type)),
1100                             v);
1101     }
1102   else
1103     /* Integral operations here.  */
1104     {
1105       /* Determine type length of the result, and if the operation should
1106          be done unsigned.  For exponentiation and shift operators,
1107          use the length and type of the left operand.  Otherwise,
1108          use the signedness of the operand with the greater length.
1109          If both operands are of equal length, use unsigned operation
1110          if one of the operands is unsigned.  */
1111       if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
1112         result_type = type1;
1113       else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
1114         result_type = type1;
1115       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1116         result_type = type2;
1117       else if (TYPE_UNSIGNED (type1))
1118         result_type = type1;
1119       else if (TYPE_UNSIGNED (type2))
1120         result_type = type2;
1121       else
1122         result_type = type1;
1123
1124       if (TYPE_UNSIGNED (result_type))
1125         {
1126           LONGEST v2_signed = value_as_long (arg2);
1127           ULONGEST v1, v2, v = 0;
1128
1129           v1 = (ULONGEST) value_as_long (arg1);
1130           v2 = (ULONGEST) v2_signed;
1131
1132           switch (op)
1133             {
1134             case BINOP_ADD:
1135               v = v1 + v2;
1136               break;
1137
1138             case BINOP_SUB:
1139               v = v1 - v2;
1140               break;
1141
1142             case BINOP_MUL:
1143               v = v1 * v2;
1144               break;
1145
1146             case BINOP_DIV:
1147             case BINOP_INTDIV:
1148               if (v2 != 0)
1149                 v = v1 / v2;
1150               else
1151                 error (_("Division by zero"));
1152               break;
1153
1154             case BINOP_EXP:
1155               v = uinteger_pow (v1, v2_signed);
1156               break;
1157
1158             case BINOP_REM:
1159               if (v2 != 0)
1160                 v = v1 % v2;
1161               else
1162                 error (_("Division by zero"));
1163               break;
1164
1165             case BINOP_MOD:
1166               /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
1167                  v1 mod 0 has a defined value, v1.  */
1168               if (v2 == 0)
1169                 {
1170                   v = v1;
1171                 }
1172               else
1173                 {
1174                   v = v1 / v2;
1175                   /* Note floor(v1/v2) == v1/v2 for unsigned.  */
1176                   v = v1 - (v2 * v);
1177                 }
1178               break;
1179
1180             case BINOP_LSH:
1181               v = v1 << v2;
1182               break;
1183
1184             case BINOP_RSH:
1185               v = v1 >> v2;
1186               break;
1187
1188             case BINOP_BITWISE_AND:
1189               v = v1 & v2;
1190               break;
1191
1192             case BINOP_BITWISE_IOR:
1193               v = v1 | v2;
1194               break;
1195
1196             case BINOP_BITWISE_XOR:
1197               v = v1 ^ v2;
1198               break;
1199
1200             case BINOP_LOGICAL_AND:
1201               v = v1 && v2;
1202               break;
1203
1204             case BINOP_LOGICAL_OR:
1205               v = v1 || v2;
1206               break;
1207
1208             case BINOP_MIN:
1209               v = v1 < v2 ? v1 : v2;
1210               break;
1211
1212             case BINOP_MAX:
1213               v = v1 > v2 ? v1 : v2;
1214               break;
1215
1216             case BINOP_EQUAL:
1217               v = v1 == v2;
1218               break;
1219
1220             case BINOP_NOTEQUAL:
1221               v = v1 != v2;
1222               break;
1223
1224             case BINOP_LESS:
1225               v = v1 < v2;
1226               break;
1227
1228             case BINOP_GTR:
1229               v = v1 > v2;
1230               break;
1231
1232             case BINOP_LEQ:
1233               v = v1 <= v2;
1234               break;
1235
1236             case BINOP_GEQ:
1237               v = v1 >= v2;
1238               break;
1239
1240             default:
1241               error (_("Invalid binary operation on numbers."));
1242             }
1243
1244           val = allocate_value (result_type);
1245           store_unsigned_integer (value_contents_raw (val),
1246                                   TYPE_LENGTH (value_type (val)),
1247                                   gdbarch_byte_order
1248                                     (get_type_arch (result_type)),
1249                                   v);
1250         }
1251       else
1252         {
1253           LONGEST v1, v2, v = 0;
1254
1255           v1 = value_as_long (arg1);
1256           v2 = value_as_long (arg2);
1257
1258           switch (op)
1259             {
1260             case BINOP_ADD:
1261               v = v1 + v2;
1262               break;
1263
1264             case BINOP_SUB:
1265               v = v1 - v2;
1266               break;
1267
1268             case BINOP_MUL:
1269               v = v1 * v2;
1270               break;
1271
1272             case BINOP_DIV:
1273             case BINOP_INTDIV:
1274               if (v2 != 0)
1275                 v = v1 / v2;
1276               else
1277                 error (_("Division by zero"));
1278               break;
1279
1280             case BINOP_EXP:
1281               v = integer_pow (v1, v2);
1282               break;
1283
1284             case BINOP_REM:
1285               if (v2 != 0)
1286                 v = v1 % v2;
1287               else
1288                 error (_("Division by zero"));
1289               break;
1290
1291             case BINOP_MOD:
1292               /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
1293                  X mod 0 has a defined value, X.  */
1294               if (v2 == 0)
1295                 {
1296                   v = v1;
1297                 }
1298               else
1299                 {
1300                   v = v1 / v2;
1301                   /* Compute floor.  */
1302                   if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1303                     {
1304                       v--;
1305                     }
1306                   v = v1 - (v2 * v);
1307                 }
1308               break;
1309
1310             case BINOP_LSH:
1311               v = v1 << v2;
1312               break;
1313
1314             case BINOP_RSH:
1315               v = v1 >> v2;
1316               break;
1317
1318             case BINOP_BITWISE_AND:
1319               v = v1 & v2;
1320               break;
1321
1322             case BINOP_BITWISE_IOR:
1323               v = v1 | v2;
1324               break;
1325
1326             case BINOP_BITWISE_XOR:
1327               v = v1 ^ v2;
1328               break;
1329
1330             case BINOP_LOGICAL_AND:
1331               v = v1 && v2;
1332               break;
1333
1334             case BINOP_LOGICAL_OR:
1335               v = v1 || v2;
1336               break;
1337
1338             case BINOP_MIN:
1339               v = v1 < v2 ? v1 : v2;
1340               break;
1341
1342             case BINOP_MAX:
1343               v = v1 > v2 ? v1 : v2;
1344               break;
1345
1346             case BINOP_EQUAL:
1347               v = v1 == v2;
1348               break;
1349
1350             case BINOP_NOTEQUAL:
1351               v = v1 != v2;
1352               break;
1353
1354             case BINOP_LESS:
1355               v = v1 < v2;
1356               break;
1357
1358             case BINOP_GTR:
1359               v = v1 > v2;
1360               break;
1361
1362             case BINOP_LEQ:
1363               v = v1 <= v2;
1364               break;
1365
1366             case BINOP_GEQ:
1367               v = v1 >= v2;
1368               break;
1369
1370             default:
1371               error (_("Invalid binary operation on numbers."));
1372             }
1373
1374           val = allocate_value (result_type);
1375           store_signed_integer (value_contents_raw (val),
1376                                 TYPE_LENGTH (value_type (val)),
1377                                 gdbarch_byte_order
1378                                   (get_type_arch (result_type)),
1379                                 v);
1380         }
1381     }
1382
1383   return val;
1384 }
1385
1386 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1387    replicating SCALAR_VALUE for each element of the vector.  Only scalar
1388    types that can be cast to the type of one element of the vector are
1389    acceptable.  The newly created vector value is returned upon success,
1390    otherwise an error is thrown.  */
1391
1392 struct value *
1393 value_vector_widen (struct value *scalar_value, struct type *vector_type)
1394 {
1395   /* Widen the scalar to a vector.  */
1396   struct type *eltype, *scalar_type;
1397   struct value *val, *elval;
1398   LONGEST low_bound, high_bound;
1399   int i;
1400
1401   vector_type = check_typedef (vector_type);
1402
1403   gdb_assert (TYPE_CODE (vector_type) == TYPE_CODE_ARRAY
1404               && TYPE_VECTOR (vector_type));
1405
1406   if (!get_array_bounds (vector_type, &low_bound, &high_bound))
1407     error (_("Could not determine the vector bounds"));
1408
1409   eltype = check_typedef (TYPE_TARGET_TYPE (vector_type));
1410   elval = value_cast (eltype, scalar_value);
1411
1412   scalar_type = check_typedef (value_type (scalar_value));
1413
1414   /* If we reduced the length of the scalar then check we didn't loose any
1415      important bits.  */
1416   if (TYPE_LENGTH (eltype) < TYPE_LENGTH (scalar_type)
1417       && !value_equal (elval, scalar_value))
1418     error (_("conversion of scalar to vector involves truncation"));
1419
1420   val = allocate_value (vector_type);
1421   for (i = 0; i < high_bound - low_bound + 1; i++)
1422     /* Duplicate the contents of elval into the destination vector.  */
1423     memcpy (value_contents_writeable (val) + (i * TYPE_LENGTH (eltype)),
1424             value_contents_all (elval), TYPE_LENGTH (eltype));
1425
1426   return val;
1427 }
1428
1429 /* Performs a binary operation on two vector operands by calling scalar_binop
1430    for each pair of vector components.  */
1431
1432 static struct value *
1433 vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1434 {
1435   struct value *val, *tmp, *mark;
1436   struct type *type1, *type2, *eltype1, *eltype2;
1437   int t1_is_vec, t2_is_vec, elsize, i;
1438   LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
1439
1440   type1 = check_typedef (value_type (val1));
1441   type2 = check_typedef (value_type (val2));
1442
1443   t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1444                && TYPE_VECTOR (type1)) ? 1 : 0;
1445   t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1446                && TYPE_VECTOR (type2)) ? 1 : 0;
1447
1448   if (!t1_is_vec || !t2_is_vec)
1449     error (_("Vector operations are only supported among vectors"));
1450
1451   if (!get_array_bounds (type1, &low_bound1, &high_bound1)
1452       || !get_array_bounds (type2, &low_bound2, &high_bound2))
1453     error (_("Could not determine the vector bounds"));
1454
1455   eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
1456   eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
1457   elsize = TYPE_LENGTH (eltype1);
1458
1459   if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
1460       || elsize != TYPE_LENGTH (eltype2)
1461       || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
1462       || low_bound1 != low_bound2 || high_bound1 != high_bound2)
1463     error (_("Cannot perform operation on vectors with different types"));
1464
1465   val = allocate_value (type1);
1466   mark = value_mark ();
1467   for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
1468     {
1469       tmp = value_binop (value_subscript (val1, i),
1470                          value_subscript (val2, i), op);
1471       memcpy (value_contents_writeable (val) + i * elsize,
1472               value_contents_all (tmp),
1473               elsize);
1474      }
1475   value_free_to_mark (mark);
1476
1477   return val;
1478 }
1479
1480 /* Perform a binary operation on two operands.  */
1481
1482 struct value *
1483 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1484 {
1485   struct value *val;
1486   struct type *type1 = check_typedef (value_type (arg1));
1487   struct type *type2 = check_typedef (value_type (arg2));
1488   int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1489                    && TYPE_VECTOR (type1));
1490   int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1491                    && TYPE_VECTOR (type2));
1492
1493   if (!t1_is_vec && !t2_is_vec)
1494     val = scalar_binop (arg1, arg2, op);
1495   else if (t1_is_vec && t2_is_vec)
1496     val = vector_binop (arg1, arg2, op);
1497   else
1498     {
1499       /* Widen the scalar operand to a vector.  */
1500       struct value **v = t1_is_vec ? &arg2 : &arg1;
1501       struct type *t = t1_is_vec ? type2 : type1;
1502       
1503       if (TYPE_CODE (t) != TYPE_CODE_FLT
1504           && TYPE_CODE (t) != TYPE_CODE_DECFLOAT
1505           && !is_integral_type (t))
1506         error (_("Argument to operation not a number or boolean."));
1507
1508       /* Replicate the scalar value to make a vector value.  */
1509       *v = value_vector_widen (*v, t1_is_vec ? type1 : type2);
1510
1511       val = vector_binop (arg1, arg2, op);
1512     }
1513
1514   return val;
1515 }
1516 \f
1517 /* Simulate the C operator ! -- return 1 if ARG1 contains zero.  */
1518
1519 int
1520 value_logical_not (struct value *arg1)
1521 {
1522   int len;
1523   const gdb_byte *p;
1524   struct type *type1;
1525
1526   arg1 = coerce_array (arg1);
1527   type1 = check_typedef (value_type (arg1));
1528
1529   if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1530     return 0 == value_as_double (arg1);
1531   else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1532     return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1),
1533                             gdbarch_byte_order (get_type_arch (type1)));
1534
1535   len = TYPE_LENGTH (type1);
1536   p = value_contents (arg1);
1537
1538   while (--len >= 0)
1539     {
1540       if (*p++)
1541         break;
1542     }
1543
1544   return len < 0;
1545 }
1546
1547 /* Perform a comparison on two string values (whose content are not
1548    necessarily null terminated) based on their length.  */
1549
1550 static int
1551 value_strcmp (struct value *arg1, struct value *arg2)
1552 {
1553   int len1 = TYPE_LENGTH (value_type (arg1));
1554   int len2 = TYPE_LENGTH (value_type (arg2));
1555   const gdb_byte *s1 = value_contents (arg1);
1556   const gdb_byte *s2 = value_contents (arg2);
1557   int i, len = len1 < len2 ? len1 : len2;
1558
1559   for (i = 0; i < len; i++)
1560     {
1561       if (s1[i] < s2[i])
1562         return -1;
1563       else if (s1[i] > s2[i])
1564         return 1;
1565       else
1566         continue;
1567     }
1568
1569   if (len1 < len2)
1570     return -1;
1571   else if (len1 > len2)
1572     return 1;
1573   else
1574     return 0;
1575 }
1576
1577 /* Simulate the C operator == by returning a 1
1578    iff ARG1 and ARG2 have equal contents.  */
1579
1580 int
1581 value_equal (struct value *arg1, struct value *arg2)
1582 {
1583   int len;
1584   const gdb_byte *p1;
1585   const gdb_byte *p2;
1586   struct type *type1, *type2;
1587   enum type_code code1;
1588   enum type_code code2;
1589   int is_int1, is_int2;
1590
1591   arg1 = coerce_array (arg1);
1592   arg2 = coerce_array (arg2);
1593
1594   type1 = check_typedef (value_type (arg1));
1595   type2 = check_typedef (value_type (arg2));
1596   code1 = TYPE_CODE (type1);
1597   code2 = TYPE_CODE (type2);
1598   is_int1 = is_integral_type (type1);
1599   is_int2 = is_integral_type (type2);
1600
1601   if (is_int1 && is_int2)
1602     return longest_to_int (value_as_long (value_binop (arg1, arg2,
1603                                                        BINOP_EQUAL)));
1604   else if ((code1 == TYPE_CODE_FLT || is_int1)
1605            && (code2 == TYPE_CODE_FLT || is_int2))
1606     {
1607       /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1608          `long double' values are returned in static storage (m68k).  */
1609       DOUBLEST d = value_as_double (arg1);
1610
1611       return d == value_as_double (arg2);
1612     }
1613   else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1614            && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1615     {
1616       gdb_byte v1[16], v2[16];
1617       int len_v1, len_v2;
1618       enum bfd_endian byte_order_v1, byte_order_v2;
1619
1620       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1621                                          v2, &len_v2, &byte_order_v2);
1622
1623       return decimal_compare (v1, len_v1, byte_order_v1,
1624                               v2, len_v2, byte_order_v2) == 0;
1625     }
1626
1627   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1628      is bigger.  */
1629   else if (code1 == TYPE_CODE_PTR && is_int2)
1630     return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1631   else if (code2 == TYPE_CODE_PTR && is_int1)
1632     return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1633
1634   else if (code1 == code2
1635            && ((len = (int) TYPE_LENGTH (type1))
1636                == (int) TYPE_LENGTH (type2)))
1637     {
1638       p1 = value_contents (arg1);
1639       p2 = value_contents (arg2);
1640       while (--len >= 0)
1641         {
1642           if (*p1++ != *p2++)
1643             break;
1644         }
1645       return len < 0;
1646     }
1647   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1648     {
1649       return value_strcmp (arg1, arg2) == 0;
1650     }
1651   else
1652     {
1653       error (_("Invalid type combination in equality test."));
1654       return 0;                 /* For lint -- never reached.  */
1655     }
1656 }
1657
1658 /* Compare values based on their raw contents.  Useful for arrays since
1659    value_equal coerces them to pointers, thus comparing just the address
1660    of the array instead of its contents.  */
1661
1662 int
1663 value_equal_contents (struct value *arg1, struct value *arg2)
1664 {
1665   struct type *type1, *type2;
1666
1667   type1 = check_typedef (value_type (arg1));
1668   type2 = check_typedef (value_type (arg2));
1669
1670   return (TYPE_CODE (type1) == TYPE_CODE (type2)
1671           && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
1672           && memcmp (value_contents (arg1), value_contents (arg2),
1673                      TYPE_LENGTH (type1)) == 0);
1674 }
1675
1676 /* Simulate the C operator < by returning 1
1677    iff ARG1's contents are less than ARG2's.  */
1678
1679 int
1680 value_less (struct value *arg1, struct value *arg2)
1681 {
1682   enum type_code code1;
1683   enum type_code code2;
1684   struct type *type1, *type2;
1685   int is_int1, is_int2;
1686
1687   arg1 = coerce_array (arg1);
1688   arg2 = coerce_array (arg2);
1689
1690   type1 = check_typedef (value_type (arg1));
1691   type2 = check_typedef (value_type (arg2));
1692   code1 = TYPE_CODE (type1);
1693   code2 = TYPE_CODE (type2);
1694   is_int1 = is_integral_type (type1);
1695   is_int2 = is_integral_type (type2);
1696
1697   if (is_int1 && is_int2)
1698     return longest_to_int (value_as_long (value_binop (arg1, arg2,
1699                                                        BINOP_LESS)));
1700   else if ((code1 == TYPE_CODE_FLT || is_int1)
1701            && (code2 == TYPE_CODE_FLT || is_int2))
1702     {
1703       /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1704          `long double' values are returned in static storage (m68k).  */
1705       DOUBLEST d = value_as_double (arg1);
1706
1707       return d < value_as_double (arg2);
1708     }
1709   else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1710            && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1711     {
1712       gdb_byte v1[16], v2[16];
1713       int len_v1, len_v2;
1714       enum bfd_endian byte_order_v1, byte_order_v2;
1715
1716       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1717                                          v2, &len_v2, &byte_order_v2);
1718
1719       return decimal_compare (v1, len_v1, byte_order_v1,
1720                               v2, len_v2, byte_order_v2) == -1;
1721     }
1722   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1723     return value_as_address (arg1) < value_as_address (arg2);
1724
1725   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1726      is bigger.  */
1727   else if (code1 == TYPE_CODE_PTR && is_int2)
1728     return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1729   else if (code2 == TYPE_CODE_PTR && is_int1)
1730     return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1731   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1732     return value_strcmp (arg1, arg2) < 0;
1733   else
1734     {
1735       error (_("Invalid type combination in ordering comparison."));
1736       return 0;
1737     }
1738 }
1739 \f
1740 /* The unary operators +, - and ~.  They free the argument ARG1.  */
1741
1742 struct value *
1743 value_pos (struct value *arg1)
1744 {
1745   struct type *type;
1746
1747   arg1 = coerce_ref (arg1);
1748   type = check_typedef (value_type (arg1));
1749
1750   if (TYPE_CODE (type) == TYPE_CODE_FLT)
1751     return value_from_double (type, value_as_double (arg1));
1752   else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1753     return value_from_decfloat (type, value_contents (arg1));
1754   else if (is_integral_type (type))
1755     {
1756       return value_from_longest (type, value_as_long (arg1));
1757     }
1758   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1759     {
1760       struct value *val = allocate_value (type);
1761
1762       memcpy (value_contents_raw (val), value_contents (arg1),
1763               TYPE_LENGTH (type));
1764       return val;
1765     }
1766   else
1767     {
1768       error (_("Argument to positive operation not a number."));
1769       return 0;                 /* For lint -- never reached.  */
1770     }
1771 }
1772
1773 struct value *
1774 value_neg (struct value *arg1)
1775 {
1776   struct type *type;
1777
1778   arg1 = coerce_ref (arg1);
1779   type = check_typedef (value_type (arg1));
1780
1781   if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1782     {
1783       struct value *val = allocate_value (type);
1784       int len = TYPE_LENGTH (type);
1785       gdb_byte decbytes[16];  /* a decfloat is at most 128 bits long.  */
1786
1787       memcpy (decbytes, value_contents (arg1), len);
1788
1789       if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE)
1790         decbytes[len-1] = decbytes[len - 1] | 0x80;
1791       else
1792         decbytes[0] = decbytes[0] | 0x80;
1793
1794       memcpy (value_contents_raw (val), decbytes, len);
1795       return val;
1796     }
1797   else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1798     return value_from_double (type, -value_as_double (arg1));
1799   else if (is_integral_type (type))
1800     {
1801       return value_from_longest (type, -value_as_long (arg1));
1802     }
1803   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1804     {
1805       struct value *tmp, *val = allocate_value (type);
1806       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1807       int i;
1808       LONGEST low_bound, high_bound;
1809
1810       if (!get_array_bounds (type, &low_bound, &high_bound))
1811         error (_("Could not determine the vector bounds"));
1812
1813       for (i = 0; i < high_bound - low_bound + 1; i++)
1814         {
1815           tmp = value_neg (value_subscript (arg1, i));
1816           memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1817                   value_contents_all (tmp), TYPE_LENGTH (eltype));
1818         }
1819       return val;
1820     }
1821   else
1822     {
1823       error (_("Argument to negate operation not a number."));
1824       return 0;                 /* For lint -- never reached.  */
1825     }
1826 }
1827
1828 struct value *
1829 value_complement (struct value *arg1)
1830 {
1831   struct type *type;
1832   struct value *val;
1833
1834   arg1 = coerce_ref (arg1);
1835   type = check_typedef (value_type (arg1));
1836
1837   if (is_integral_type (type))
1838     val = value_from_longest (type, ~value_as_long (arg1));
1839   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1840     {
1841       struct value *tmp;
1842       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1843       int i;
1844       LONGEST low_bound, high_bound;
1845
1846       if (!get_array_bounds (type, &low_bound, &high_bound))
1847         error (_("Could not determine the vector bounds"));
1848
1849       val = allocate_value (type);
1850       for (i = 0; i < high_bound - low_bound + 1; i++)
1851         {
1852           tmp = value_complement (value_subscript (arg1, i));
1853           memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1854                   value_contents_all (tmp), TYPE_LENGTH (eltype));
1855         }
1856     }
1857   else
1858     error (_("Argument to complement operation not an integer, boolean."));
1859
1860   return val;
1861 }
1862 \f
1863 /* The INDEX'th bit of SET value whose value_type is TYPE,
1864    and whose value_contents is valaddr.
1865    Return -1 if out of range, -2 other error.  */
1866
1867 int
1868 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1869 {
1870   struct gdbarch *gdbarch = get_type_arch (type);
1871   LONGEST low_bound, high_bound;
1872   LONGEST word;
1873   unsigned rel_index;
1874   struct type *range = TYPE_INDEX_TYPE (type);
1875
1876   if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1877     return -2;
1878   if (index < low_bound || index > high_bound)
1879     return -1;
1880   rel_index = index - low_bound;
1881   word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
1882                                    gdbarch_byte_order (gdbarch));
1883   rel_index %= TARGET_CHAR_BIT;
1884   if (gdbarch_bits_big_endian (gdbarch))
1885     rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1886   return (word >> rel_index) & 1;
1887 }
1888
1889 int
1890 value_in (struct value *element, struct value *set)
1891 {
1892   int member;
1893   struct type *settype = check_typedef (value_type (set));
1894   struct type *eltype = check_typedef (value_type (element));
1895
1896   if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1897     eltype = TYPE_TARGET_TYPE (eltype);
1898   if (TYPE_CODE (settype) != TYPE_CODE_SET)
1899     error (_("Second argument of 'IN' has wrong type"));
1900   if (TYPE_CODE (eltype) != TYPE_CODE_INT
1901       && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1902       && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1903       && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1904     error (_("First argument of 'IN' has wrong type"));
1905   member = value_bit_index (settype, value_contents (set),
1906                             value_as_long (element));
1907   if (member < 0)
1908     error (_("First argument of 'IN' not in range"));
1909   return member;
1910 }
1911
1912 void
1913 _initialize_valarith (void)
1914 {
1915 }