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