[ARM] Update knowledge of bfd architectures
[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 (gdb::array_view<value *> args, 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, 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, gdb::array_view<value *> args,
316                        char *name, int *static_memfuncp, 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, name, static_memfuncp,
323                                           noside);
324     }
325   else
326     result = value_struct_elt (argp, args.data (), 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   char *ptr;
346   char tstr[13];
347   int static_memfuncp;
348
349   arg1 = coerce_ref (arg1);
350   arg2 = coerce_ref (arg2);
351
352   /* now we know that what we have to do is construct our
353      arg vector and find the right function to call it with.  */
354
355   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
356     error (_("Can't do that binary op on that type"));  /* FIXME be explicit */
357
358   value *argvec_storage[3];
359   gdb::array_view<value *> argvec = argvec_storage;
360
361   argvec[1] = value_addr (arg1);
362   argvec[2] = arg2;
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.slice (1), tstr,
473                                      &static_memfuncp, noside);
474
475   if (argvec[0])
476     {
477       if (static_memfuncp)
478         {
479           argvec[1] = argvec[0];
480           argvec = argvec.slice (1);
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], argvec.slice (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], argvec.slice (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,
506                                     argvec.slice (1, 2 - static_memfuncp));
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   char *ptr;
523   char tstr[13], mangle_tstr[13];
524   int static_memfuncp, nargs;
525
526   arg1 = coerce_ref (arg1);
527
528   /* now we know that what we have to do is construct our
529      arg vector and find the right function to call it with.  */
530
531   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
532     error (_("Can't do that unary op on that type"));   /* FIXME be explicit */
533
534   value *argvec_storage[3];
535   gdb::array_view<value *> argvec = argvec_storage;
536
537   argvec[1] = value_addr (arg1);
538   argvec[2] = 0;
539
540   nargs = 1;
541
542   /* Make the right function name up.  */
543   strcpy (tstr, "operator__");
544   ptr = tstr + 8;
545   strcpy (mangle_tstr, "__");
546   switch (op)
547     {
548     case UNOP_PREINCREMENT:
549       strcpy (ptr, "++");
550       break;
551     case UNOP_PREDECREMENT:
552       strcpy (ptr, "--");
553       break;
554     case UNOP_POSTINCREMENT:
555       strcpy (ptr, "++");
556       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
557       argvec[3] = 0;
558       nargs ++;
559       break;
560     case UNOP_POSTDECREMENT:
561       strcpy (ptr, "--");
562       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
563       argvec[3] = 0;
564       nargs ++;
565       break;
566     case UNOP_LOGICAL_NOT:
567       strcpy (ptr, "!");
568       break;
569     case UNOP_COMPLEMENT:
570       strcpy (ptr, "~");
571       break;
572     case UNOP_NEG:
573       strcpy (ptr, "-");
574       break;
575     case UNOP_PLUS:
576       strcpy (ptr, "+");
577       break;
578     case UNOP_IND:
579       strcpy (ptr, "*");
580       break;
581     case STRUCTOP_PTR:
582       strcpy (ptr, "->");
583       break;
584     default:
585       error (_("Invalid unary operation specified."));
586     }
587
588   argvec[0] = value_user_defined_op (&arg1, argvec.slice (1, nargs), tstr,
589                                      &static_memfuncp, noside);
590
591   if (argvec[0])
592     {
593       if (static_memfuncp)
594         {
595           argvec[1] = argvec[0];
596           argvec = argvec.slice (1);
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], 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], 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,
622                                     argvec.slice (1, nargs));
623     }
624   throw_error (NOT_FOUND_ERROR,
625                _("member function %s not found"), tstr);
626 }
627 \f
628
629 /* Concatenate two values with the following conditions:
630
631    (1)  Both values must be either bitstring values or character string
632    values and the resulting value consists of the concatenation of
633    ARG1 followed by ARG2.
634
635    or
636
637    One value must be an integer value and the other value must be
638    either a bitstring value or character string value, which is
639    to be repeated by the number of times specified by the integer
640    value.
641
642
643    (2)  Boolean values are also allowed and are treated as bit string
644    values of length 1.
645
646    (3)  Character values are also allowed and are treated as character
647    string values of length 1.  */
648
649 struct value *
650 value_concat (struct value *arg1, struct value *arg2)
651 {
652   struct value *inval1;
653   struct value *inval2;
654   struct value *outval = NULL;
655   int inval1len, inval2len;
656   int count, idx;
657   char inchar;
658   struct type *type1 = check_typedef (value_type (arg1));
659   struct type *type2 = check_typedef (value_type (arg2));
660   struct type *char_type;
661
662   /* First figure out if we are dealing with two values to be concatenated
663      or a repeat count and a value to be repeated.  INVAL1 is set to the
664      first of two concatenated values, or the repeat count.  INVAL2 is set
665      to the second of the two concatenated values or the value to be 
666      repeated.  */
667
668   if (TYPE_CODE (type2) == TYPE_CODE_INT)
669     {
670       struct type *tmp = type1;
671
672       type1 = tmp;
673       tmp = type2;
674       inval1 = arg2;
675       inval2 = arg1;
676     }
677   else
678     {
679       inval1 = arg1;
680       inval2 = arg2;
681     }
682
683   /* Now process the input values.  */
684
685   if (TYPE_CODE (type1) == TYPE_CODE_INT)
686     {
687       /* We have a repeat count.  Validate the second value and then
688          construct a value repeated that many times.  */
689       if (TYPE_CODE (type2) == TYPE_CODE_STRING
690           || TYPE_CODE (type2) == TYPE_CODE_CHAR)
691         {
692           count = longest_to_int (value_as_long (inval1));
693           inval2len = TYPE_LENGTH (type2);
694           std::vector<char> ptr (count * inval2len);
695           if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
696             {
697               char_type = type2;
698
699               inchar = (char) unpack_long (type2,
700                                            value_contents (inval2));
701               for (idx = 0; idx < count; idx++)
702                 {
703                   ptr[idx] = inchar;
704                 }
705             }
706           else
707             {
708               char_type = TYPE_TARGET_TYPE (type2);
709
710               for (idx = 0; idx < count; idx++)
711                 {
712                   memcpy (&ptr[idx * inval2len], value_contents (inval2),
713                           inval2len);
714                 }
715             }
716           outval = value_string (ptr.data (), count * inval2len, char_type);
717         }
718       else if (TYPE_CODE (type2) == TYPE_CODE_BOOL)
719         {
720           error (_("unimplemented support for boolean repeats"));
721         }
722       else
723         {
724           error (_("can't repeat values of that type"));
725         }
726     }
727   else if (TYPE_CODE (type1) == TYPE_CODE_STRING
728            || TYPE_CODE (type1) == TYPE_CODE_CHAR)
729     {
730       /* We have two character strings to concatenate.  */
731       if (TYPE_CODE (type2) != TYPE_CODE_STRING
732           && TYPE_CODE (type2) != TYPE_CODE_CHAR)
733         {
734           error (_("Strings can only be concatenated with other strings."));
735         }
736       inval1len = TYPE_LENGTH (type1);
737       inval2len = TYPE_LENGTH (type2);
738       std::vector<char> ptr (inval1len + inval2len);
739       if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
740         {
741           char_type = type1;
742
743           ptr[0] = (char) unpack_long (type1, value_contents (inval1));
744         }
745       else
746         {
747           char_type = TYPE_TARGET_TYPE (type1);
748
749           memcpy (ptr.data (), value_contents (inval1), inval1len);
750         }
751       if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
752         {
753           ptr[inval1len] =
754             (char) unpack_long (type2, value_contents (inval2));
755         }
756       else
757         {
758           memcpy (&ptr[inval1len], value_contents (inval2), inval2len);
759         }
760       outval = value_string (ptr.data (), inval1len + inval2len, char_type);
761     }
762   else if (TYPE_CODE (type1) == TYPE_CODE_BOOL)
763     {
764       /* We have two bitstrings to concatenate.  */
765       if (TYPE_CODE (type2) != TYPE_CODE_BOOL)
766         {
767           error (_("Booleans can only be concatenated "
768                    "with other bitstrings or booleans."));
769         }
770       error (_("unimplemented support for boolean concatenation."));
771     }
772   else
773     {
774       /* We don't know how to concatenate these operands.  */
775       error (_("illegal operands for concatenation."));
776     }
777   return (outval);
778 }
779 \f
780 /* Integer exponentiation: V1**V2, where both arguments are
781    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
782
783 static LONGEST
784 integer_pow (LONGEST v1, LONGEST v2)
785 {
786   if (v2 < 0)
787     {
788       if (v1 == 0)
789         error (_("Attempt to raise 0 to negative power."));
790       else
791         return 0;
792     }
793   else 
794     {
795       /* The Russian Peasant's Algorithm.  */
796       LONGEST v;
797       
798       v = 1;
799       for (;;)
800         {
801           if (v2 & 1L) 
802             v *= v1;
803           v2 >>= 1;
804           if (v2 == 0)
805             return v;
806           v1 *= v1;
807         }
808     }
809 }
810
811 /* Integer exponentiation: V1**V2, where both arguments are
812    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
813
814 static ULONGEST
815 uinteger_pow (ULONGEST v1, LONGEST v2)
816 {
817   if (v2 < 0)
818     {
819       if (v1 == 0)
820         error (_("Attempt to raise 0 to negative power."));
821       else
822         return 0;
823     }
824   else 
825     {
826       /* The Russian Peasant's Algorithm.  */
827       ULONGEST v;
828       
829       v = 1;
830       for (;;)
831         {
832           if (v2 & 1L) 
833             v *= v1;
834           v2 >>= 1;
835           if (v2 == 0)
836             return v;
837           v1 *= v1;
838         }
839     }
840 }
841
842 /* Obtain argument values for binary operation, converting from
843    other types if one of them is not floating point.  */
844 static void
845 value_args_as_target_float (struct value *arg1, struct value *arg2,
846                             gdb_byte *x, struct type **eff_type_x,
847                             gdb_byte *y, struct type **eff_type_y)
848 {
849   struct type *type1, *type2;
850
851   type1 = check_typedef (value_type (arg1));
852   type2 = check_typedef (value_type (arg2));
853
854   /* At least one of the arguments must be of floating-point type.  */
855   gdb_assert (is_floating_type (type1) || is_floating_type (type2));
856
857   if (is_floating_type (type1) && is_floating_type (type2)
858       && TYPE_CODE (type1) != TYPE_CODE (type2))
859     /* The DFP extension to the C language does not allow mixing of
860      * decimal float types with other float types in expressions
861      * (see WDTR 24732, page 12).  */
862     error (_("Mixing decimal floating types with "
863              "other floating types is not allowed."));
864
865   /* Obtain value of arg1, converting from other types if necessary.  */
866
867   if (is_floating_type (type1))
868     {
869       *eff_type_x = type1;
870       memcpy (x, value_contents (arg1), TYPE_LENGTH (type1));
871     }
872   else if (is_integral_type (type1))
873     {
874       *eff_type_x = type2;
875       if (TYPE_UNSIGNED (type1))
876         target_float_from_ulongest (x, *eff_type_x, value_as_long (arg1));
877       else
878         target_float_from_longest (x, *eff_type_x, value_as_long (arg1));
879     }
880   else
881     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
882              TYPE_NAME (type2));
883
884   /* Obtain value of arg2, converting from other types if necessary.  */
885
886   if (is_floating_type (type2))
887     {
888       *eff_type_y = type2;
889       memcpy (y, value_contents (arg2), TYPE_LENGTH (type2));
890     }
891   else if (is_integral_type (type2))
892     {
893       *eff_type_y = type1;
894       if (TYPE_UNSIGNED (type2))
895         target_float_from_ulongest (y, *eff_type_y, value_as_long (arg2));
896       else
897         target_float_from_longest (y, *eff_type_y, value_as_long (arg2));
898     }
899   else
900     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
901              TYPE_NAME (type2));
902 }
903
904 /* Perform a binary operation on two operands which have reasonable
905    representations as integers or floats.  This includes booleans,
906    characters, integers, or floats.
907    Does not support addition and subtraction on pointers;
908    use value_ptradd, value_ptrsub or value_ptrdiff for those operations.  */
909
910 static struct value *
911 scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
912 {
913   struct value *val;
914   struct type *type1, *type2, *result_type;
915
916   arg1 = coerce_ref (arg1);
917   arg2 = coerce_ref (arg2);
918
919   type1 = check_typedef (value_type (arg1));
920   type2 = check_typedef (value_type (arg2));
921
922   if ((!is_floating_value (arg1) && !is_integral_type (type1))
923       || (!is_floating_value (arg2) && !is_integral_type (type2)))
924     error (_("Argument to arithmetic operation not a number or boolean."));
925
926   if (is_floating_type (type1) || is_floating_type (type2))
927     {
928       /* If only one type is floating-point, use its type.
929          Otherwise use the bigger type.  */
930       if (!is_floating_type (type1))
931         result_type = type2;
932       else if (!is_floating_type (type2))
933         result_type = type1;
934       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
935         result_type = type2;
936       else
937         result_type = type1;
938
939       val = allocate_value (result_type);
940
941       struct type *eff_type_v1, *eff_type_v2;
942       gdb::byte_vector v1, v2;
943       v1.resize (TYPE_LENGTH (result_type));
944       v2.resize (TYPE_LENGTH (result_type));
945
946       value_args_as_target_float (arg1, arg2,
947                                   v1.data (), &eff_type_v1,
948                                   v2.data (), &eff_type_v2);
949       target_float_binop (op, v1.data (), eff_type_v1,
950                               v2.data (), eff_type_v2,
951                               value_contents_raw (val), result_type);
952     }
953   else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
954            || TYPE_CODE (type2) == TYPE_CODE_BOOL)
955     {
956       LONGEST v1, v2, v = 0;
957
958       v1 = value_as_long (arg1);
959       v2 = value_as_long (arg2);
960
961       switch (op)
962         {
963         case BINOP_BITWISE_AND:
964           v = v1 & v2;
965           break;
966
967         case BINOP_BITWISE_IOR:
968           v = v1 | v2;
969           break;
970
971         case BINOP_BITWISE_XOR:
972           v = v1 ^ v2;
973           break;
974               
975         case BINOP_EQUAL:
976           v = v1 == v2;
977           break;
978           
979         case BINOP_NOTEQUAL:
980           v = v1 != v2;
981           break;
982
983         default:
984           error (_("Invalid operation on booleans."));
985         }
986
987       result_type = type1;
988
989       val = allocate_value (result_type);
990       store_signed_integer (value_contents_raw (val),
991                             TYPE_LENGTH (result_type),
992                             gdbarch_byte_order (get_type_arch (result_type)),
993                             v);
994     }
995   else
996     /* Integral operations here.  */
997     {
998       /* Determine type length of the result, and if the operation should
999          be done unsigned.  For exponentiation and shift operators,
1000          use the length and type of the left operand.  Otherwise,
1001          use the signedness of the operand with the greater length.
1002          If both operands are of equal length, use unsigned operation
1003          if one of the operands is unsigned.  */
1004       if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
1005         result_type = type1;
1006       else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
1007         result_type = type1;
1008       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1009         result_type = type2;
1010       else if (TYPE_UNSIGNED (type1))
1011         result_type = type1;
1012       else if (TYPE_UNSIGNED (type2))
1013         result_type = type2;
1014       else
1015         result_type = type1;
1016
1017       if (TYPE_UNSIGNED (result_type))
1018         {
1019           LONGEST v2_signed = value_as_long (arg2);
1020           ULONGEST v1, v2, v = 0;
1021
1022           v1 = (ULONGEST) value_as_long (arg1);
1023           v2 = (ULONGEST) v2_signed;
1024
1025           switch (op)
1026             {
1027             case BINOP_ADD:
1028               v = v1 + v2;
1029               break;
1030
1031             case BINOP_SUB:
1032               v = v1 - v2;
1033               break;
1034
1035             case BINOP_MUL:
1036               v = v1 * v2;
1037               break;
1038
1039             case BINOP_DIV:
1040             case BINOP_INTDIV:
1041               if (v2 != 0)
1042                 v = v1 / v2;
1043               else
1044                 error (_("Division by zero"));
1045               break;
1046
1047             case BINOP_EXP:
1048               v = uinteger_pow (v1, v2_signed);
1049               break;
1050
1051             case BINOP_REM:
1052               if (v2 != 0)
1053                 v = v1 % v2;
1054               else
1055                 error (_("Division by zero"));
1056               break;
1057
1058             case BINOP_MOD:
1059               /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
1060                  v1 mod 0 has a defined value, v1.  */
1061               if (v2 == 0)
1062                 {
1063                   v = v1;
1064                 }
1065               else
1066                 {
1067                   v = v1 / v2;
1068                   /* Note floor(v1/v2) == v1/v2 for unsigned.  */
1069                   v = v1 - (v2 * v);
1070                 }
1071               break;
1072
1073             case BINOP_LSH:
1074               v = v1 << v2;
1075               break;
1076
1077             case BINOP_RSH:
1078               v = v1 >> v2;
1079               break;
1080
1081             case BINOP_BITWISE_AND:
1082               v = v1 & v2;
1083               break;
1084
1085             case BINOP_BITWISE_IOR:
1086               v = v1 | v2;
1087               break;
1088
1089             case BINOP_BITWISE_XOR:
1090               v = v1 ^ v2;
1091               break;
1092
1093             case BINOP_LOGICAL_AND:
1094               v = v1 && v2;
1095               break;
1096
1097             case BINOP_LOGICAL_OR:
1098               v = v1 || v2;
1099               break;
1100
1101             case BINOP_MIN:
1102               v = v1 < v2 ? v1 : v2;
1103               break;
1104
1105             case BINOP_MAX:
1106               v = v1 > v2 ? v1 : v2;
1107               break;
1108
1109             case BINOP_EQUAL:
1110               v = v1 == v2;
1111               break;
1112
1113             case BINOP_NOTEQUAL:
1114               v = v1 != v2;
1115               break;
1116
1117             case BINOP_LESS:
1118               v = v1 < v2;
1119               break;
1120
1121             case BINOP_GTR:
1122               v = v1 > v2;
1123               break;
1124
1125             case BINOP_LEQ:
1126               v = v1 <= v2;
1127               break;
1128
1129             case BINOP_GEQ:
1130               v = v1 >= v2;
1131               break;
1132
1133             default:
1134               error (_("Invalid binary operation on numbers."));
1135             }
1136
1137           val = allocate_value (result_type);
1138           store_unsigned_integer (value_contents_raw (val),
1139                                   TYPE_LENGTH (value_type (val)),
1140                                   gdbarch_byte_order
1141                                     (get_type_arch (result_type)),
1142                                   v);
1143         }
1144       else
1145         {
1146           LONGEST v1, v2, v = 0;
1147
1148           v1 = value_as_long (arg1);
1149           v2 = value_as_long (arg2);
1150
1151           switch (op)
1152             {
1153             case BINOP_ADD:
1154               v = v1 + v2;
1155               break;
1156
1157             case BINOP_SUB:
1158               v = v1 - v2;
1159               break;
1160
1161             case BINOP_MUL:
1162               v = v1 * v2;
1163               break;
1164
1165             case BINOP_DIV:
1166             case BINOP_INTDIV:
1167               if (v2 != 0)
1168                 v = v1 / v2;
1169               else
1170                 error (_("Division by zero"));
1171               break;
1172
1173             case BINOP_EXP:
1174               v = integer_pow (v1, v2);
1175               break;
1176
1177             case BINOP_REM:
1178               if (v2 != 0)
1179                 v = v1 % v2;
1180               else
1181                 error (_("Division by zero"));
1182               break;
1183
1184             case BINOP_MOD:
1185               /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
1186                  X mod 0 has a defined value, X.  */
1187               if (v2 == 0)
1188                 {
1189                   v = v1;
1190                 }
1191               else
1192                 {
1193                   v = v1 / v2;
1194                   /* Compute floor.  */
1195                   if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1196                     {
1197                       v--;
1198                     }
1199                   v = v1 - (v2 * v);
1200                 }
1201               break;
1202
1203             case BINOP_LSH:
1204               v = v1 << v2;
1205               break;
1206
1207             case BINOP_RSH:
1208               v = v1 >> v2;
1209               break;
1210
1211             case BINOP_BITWISE_AND:
1212               v = v1 & v2;
1213               break;
1214
1215             case BINOP_BITWISE_IOR:
1216               v = v1 | v2;
1217               break;
1218
1219             case BINOP_BITWISE_XOR:
1220               v = v1 ^ v2;
1221               break;
1222
1223             case BINOP_LOGICAL_AND:
1224               v = v1 && v2;
1225               break;
1226
1227             case BINOP_LOGICAL_OR:
1228               v = v1 || v2;
1229               break;
1230
1231             case BINOP_MIN:
1232               v = v1 < v2 ? v1 : v2;
1233               break;
1234
1235             case BINOP_MAX:
1236               v = v1 > v2 ? v1 : v2;
1237               break;
1238
1239             case BINOP_EQUAL:
1240               v = v1 == v2;
1241               break;
1242
1243             case BINOP_NOTEQUAL:
1244               v = v1 != v2;
1245               break;
1246
1247             case BINOP_LESS:
1248               v = v1 < v2;
1249               break;
1250
1251             case BINOP_GTR:
1252               v = v1 > v2;
1253               break;
1254
1255             case BINOP_LEQ:
1256               v = v1 <= v2;
1257               break;
1258
1259             case BINOP_GEQ:
1260               v = v1 >= v2;
1261               break;
1262
1263             default:
1264               error (_("Invalid binary operation on numbers."));
1265             }
1266
1267           val = allocate_value (result_type);
1268           store_signed_integer (value_contents_raw (val),
1269                                 TYPE_LENGTH (value_type (val)),
1270                                 gdbarch_byte_order
1271                                   (get_type_arch (result_type)),
1272                                 v);
1273         }
1274     }
1275
1276   return val;
1277 }
1278
1279 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1280    replicating SCALAR_VALUE for each element of the vector.  Only scalar
1281    types that can be cast to the type of one element of the vector are
1282    acceptable.  The newly created vector value is returned upon success,
1283    otherwise an error is thrown.  */
1284
1285 struct value *
1286 value_vector_widen (struct value *scalar_value, struct type *vector_type)
1287 {
1288   /* Widen the scalar to a vector.  */
1289   struct type *eltype, *scalar_type;
1290   struct value *val, *elval;
1291   LONGEST low_bound, high_bound;
1292   int i;
1293
1294   vector_type = check_typedef (vector_type);
1295
1296   gdb_assert (TYPE_CODE (vector_type) == TYPE_CODE_ARRAY
1297               && TYPE_VECTOR (vector_type));
1298
1299   if (!get_array_bounds (vector_type, &low_bound, &high_bound))
1300     error (_("Could not determine the vector bounds"));
1301
1302   eltype = check_typedef (TYPE_TARGET_TYPE (vector_type));
1303   elval = value_cast (eltype, scalar_value);
1304
1305   scalar_type = check_typedef (value_type (scalar_value));
1306
1307   /* If we reduced the length of the scalar then check we didn't loose any
1308      important bits.  */
1309   if (TYPE_LENGTH (eltype) < TYPE_LENGTH (scalar_type)
1310       && !value_equal (elval, scalar_value))
1311     error (_("conversion of scalar to vector involves truncation"));
1312
1313   val = allocate_value (vector_type);
1314   for (i = 0; i < high_bound - low_bound + 1; i++)
1315     /* Duplicate the contents of elval into the destination vector.  */
1316     memcpy (value_contents_writeable (val) + (i * TYPE_LENGTH (eltype)),
1317             value_contents_all (elval), TYPE_LENGTH (eltype));
1318
1319   return val;
1320 }
1321
1322 /* Performs a binary operation on two vector operands by calling scalar_binop
1323    for each pair of vector components.  */
1324
1325 static struct value *
1326 vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1327 {
1328   struct value *val, *tmp, *mark;
1329   struct type *type1, *type2, *eltype1, *eltype2;
1330   int t1_is_vec, t2_is_vec, elsize, i;
1331   LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
1332
1333   type1 = check_typedef (value_type (val1));
1334   type2 = check_typedef (value_type (val2));
1335
1336   t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1337                && TYPE_VECTOR (type1)) ? 1 : 0;
1338   t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1339                && TYPE_VECTOR (type2)) ? 1 : 0;
1340
1341   if (!t1_is_vec || !t2_is_vec)
1342     error (_("Vector operations are only supported among vectors"));
1343
1344   if (!get_array_bounds (type1, &low_bound1, &high_bound1)
1345       || !get_array_bounds (type2, &low_bound2, &high_bound2))
1346     error (_("Could not determine the vector bounds"));
1347
1348   eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
1349   eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
1350   elsize = TYPE_LENGTH (eltype1);
1351
1352   if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
1353       || elsize != TYPE_LENGTH (eltype2)
1354       || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
1355       || low_bound1 != low_bound2 || high_bound1 != high_bound2)
1356     error (_("Cannot perform operation on vectors with different types"));
1357
1358   val = allocate_value (type1);
1359   mark = value_mark ();
1360   for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
1361     {
1362       tmp = value_binop (value_subscript (val1, i),
1363                          value_subscript (val2, i), op);
1364       memcpy (value_contents_writeable (val) + i * elsize,
1365               value_contents_all (tmp),
1366               elsize);
1367      }
1368   value_free_to_mark (mark);
1369
1370   return val;
1371 }
1372
1373 /* Perform a binary operation on two operands.  */
1374
1375 struct value *
1376 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1377 {
1378   struct value *val;
1379   struct type *type1 = check_typedef (value_type (arg1));
1380   struct type *type2 = check_typedef (value_type (arg2));
1381   int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1382                    && TYPE_VECTOR (type1));
1383   int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1384                    && TYPE_VECTOR (type2));
1385
1386   if (!t1_is_vec && !t2_is_vec)
1387     val = scalar_binop (arg1, arg2, op);
1388   else if (t1_is_vec && t2_is_vec)
1389     val = vector_binop (arg1, arg2, op);
1390   else
1391     {
1392       /* Widen the scalar operand to a vector.  */
1393       struct value **v = t1_is_vec ? &arg2 : &arg1;
1394       struct type *t = t1_is_vec ? type2 : type1;
1395       
1396       if (TYPE_CODE (t) != TYPE_CODE_FLT
1397           && TYPE_CODE (t) != TYPE_CODE_DECFLOAT
1398           && !is_integral_type (t))
1399         error (_("Argument to operation not a number or boolean."));
1400
1401       /* Replicate the scalar value to make a vector value.  */
1402       *v = value_vector_widen (*v, t1_is_vec ? type1 : type2);
1403
1404       val = vector_binop (arg1, arg2, op);
1405     }
1406
1407   return val;
1408 }
1409 \f
1410 /* Simulate the C operator ! -- return 1 if ARG1 contains zero.  */
1411
1412 int
1413 value_logical_not (struct value *arg1)
1414 {
1415   int len;
1416   const gdb_byte *p;
1417   struct type *type1;
1418
1419   arg1 = coerce_array (arg1);
1420   type1 = check_typedef (value_type (arg1));
1421
1422   if (is_floating_value (arg1))
1423     return target_float_is_zero (value_contents (arg1), type1);
1424
1425   len = TYPE_LENGTH (type1);
1426   p = value_contents (arg1);
1427
1428   while (--len >= 0)
1429     {
1430       if (*p++)
1431         break;
1432     }
1433
1434   return len < 0;
1435 }
1436
1437 /* Perform a comparison on two string values (whose content are not
1438    necessarily null terminated) based on their length.  */
1439
1440 static int
1441 value_strcmp (struct value *arg1, struct value *arg2)
1442 {
1443   int len1 = TYPE_LENGTH (value_type (arg1));
1444   int len2 = TYPE_LENGTH (value_type (arg2));
1445   const gdb_byte *s1 = value_contents (arg1);
1446   const gdb_byte *s2 = value_contents (arg2);
1447   int i, len = len1 < len2 ? len1 : len2;
1448
1449   for (i = 0; i < len; i++)
1450     {
1451       if (s1[i] < s2[i])
1452         return -1;
1453       else if (s1[i] > s2[i])
1454         return 1;
1455       else
1456         continue;
1457     }
1458
1459   if (len1 < len2)
1460     return -1;
1461   else if (len1 > len2)
1462     return 1;
1463   else
1464     return 0;
1465 }
1466
1467 /* Simulate the C operator == by returning a 1
1468    iff ARG1 and ARG2 have equal contents.  */
1469
1470 int
1471 value_equal (struct value *arg1, struct value *arg2)
1472 {
1473   int len;
1474   const gdb_byte *p1;
1475   const gdb_byte *p2;
1476   struct type *type1, *type2;
1477   enum type_code code1;
1478   enum type_code code2;
1479   int is_int1, is_int2;
1480
1481   arg1 = coerce_array (arg1);
1482   arg2 = coerce_array (arg2);
1483
1484   type1 = check_typedef (value_type (arg1));
1485   type2 = check_typedef (value_type (arg2));
1486   code1 = TYPE_CODE (type1);
1487   code2 = TYPE_CODE (type2);
1488   is_int1 = is_integral_type (type1);
1489   is_int2 = is_integral_type (type2);
1490
1491   if (is_int1 && is_int2)
1492     return longest_to_int (value_as_long (value_binop (arg1, arg2,
1493                                                        BINOP_EQUAL)));
1494   else if ((is_floating_value (arg1) || is_int1)
1495            && (is_floating_value (arg2) || is_int2))
1496     {
1497       struct type *eff_type_v1, *eff_type_v2;
1498       gdb::byte_vector v1, v2;
1499       v1.resize (std::max (TYPE_LENGTH (type1), TYPE_LENGTH (type2)));
1500       v2.resize (std::max (TYPE_LENGTH (type1), TYPE_LENGTH (type2)));
1501
1502       value_args_as_target_float (arg1, arg2,
1503                                   v1.data (), &eff_type_v1,
1504                                   v2.data (), &eff_type_v2);
1505
1506       return target_float_compare (v1.data (), eff_type_v1,
1507                                    v2.data (), eff_type_v2) == 0;
1508     }
1509
1510   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1511      is bigger.  */
1512   else if (code1 == TYPE_CODE_PTR && is_int2)
1513     return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1514   else if (code2 == TYPE_CODE_PTR && is_int1)
1515     return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1516
1517   else if (code1 == code2
1518            && ((len = (int) TYPE_LENGTH (type1))
1519                == (int) TYPE_LENGTH (type2)))
1520     {
1521       p1 = value_contents (arg1);
1522       p2 = value_contents (arg2);
1523       while (--len >= 0)
1524         {
1525           if (*p1++ != *p2++)
1526             break;
1527         }
1528       return len < 0;
1529     }
1530   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1531     {
1532       return value_strcmp (arg1, arg2) == 0;
1533     }
1534   else
1535     error (_("Invalid type combination in equality test."));
1536 }
1537
1538 /* Compare values based on their raw contents.  Useful for arrays since
1539    value_equal coerces them to pointers, thus comparing just the address
1540    of the array instead of its contents.  */
1541
1542 int
1543 value_equal_contents (struct value *arg1, struct value *arg2)
1544 {
1545   struct type *type1, *type2;
1546
1547   type1 = check_typedef (value_type (arg1));
1548   type2 = check_typedef (value_type (arg2));
1549
1550   return (TYPE_CODE (type1) == TYPE_CODE (type2)
1551           && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
1552           && memcmp (value_contents (arg1), value_contents (arg2),
1553                      TYPE_LENGTH (type1)) == 0);
1554 }
1555
1556 /* Simulate the C operator < by returning 1
1557    iff ARG1's contents are less than ARG2's.  */
1558
1559 int
1560 value_less (struct value *arg1, struct value *arg2)
1561 {
1562   enum type_code code1;
1563   enum type_code code2;
1564   struct type *type1, *type2;
1565   int is_int1, is_int2;
1566
1567   arg1 = coerce_array (arg1);
1568   arg2 = coerce_array (arg2);
1569
1570   type1 = check_typedef (value_type (arg1));
1571   type2 = check_typedef (value_type (arg2));
1572   code1 = TYPE_CODE (type1);
1573   code2 = TYPE_CODE (type2);
1574   is_int1 = is_integral_type (type1);
1575   is_int2 = is_integral_type (type2);
1576
1577   if (is_int1 && is_int2)
1578     return longest_to_int (value_as_long (value_binop (arg1, arg2,
1579                                                        BINOP_LESS)));
1580   else if ((is_floating_value (arg1) || is_int1)
1581            && (is_floating_value (arg2) || is_int2))
1582     {
1583       struct type *eff_type_v1, *eff_type_v2;
1584       gdb::byte_vector v1, v2;
1585       v1.resize (std::max (TYPE_LENGTH (type1), TYPE_LENGTH (type2)));
1586       v2.resize (std::max (TYPE_LENGTH (type1), TYPE_LENGTH (type2)));
1587
1588       value_args_as_target_float (arg1, arg2,
1589                                   v1.data (), &eff_type_v1,
1590                                   v2.data (), &eff_type_v2);
1591
1592       return target_float_compare (v1.data (), eff_type_v1,
1593                                    v2.data (), eff_type_v2) == -1;
1594     }
1595   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1596     return value_as_address (arg1) < value_as_address (arg2);
1597
1598   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1599      is bigger.  */
1600   else if (code1 == TYPE_CODE_PTR && is_int2)
1601     return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1602   else if (code2 == TYPE_CODE_PTR && is_int1)
1603     return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1604   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1605     return value_strcmp (arg1, arg2) < 0;
1606   else
1607     {
1608       error (_("Invalid type combination in ordering comparison."));
1609       return 0;
1610     }
1611 }
1612 \f
1613 /* The unary operators +, - and ~.  They free the argument ARG1.  */
1614
1615 struct value *
1616 value_pos (struct value *arg1)
1617 {
1618   struct type *type;
1619
1620   arg1 = coerce_ref (arg1);
1621   type = check_typedef (value_type (arg1));
1622
1623   if (is_integral_type (type) || is_floating_value (arg1)
1624       || (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)))
1625     return value_from_contents (type, value_contents (arg1));
1626   else
1627     error (_("Argument to positive operation not a number."));
1628 }
1629
1630 struct value *
1631 value_neg (struct value *arg1)
1632 {
1633   struct type *type;
1634
1635   arg1 = coerce_ref (arg1);
1636   type = check_typedef (value_type (arg1));
1637
1638   if (is_integral_type (type) || is_floating_type (type))
1639     return value_binop (value_from_longest (type, 0), arg1, BINOP_SUB);
1640   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1641     {
1642       struct value *tmp, *val = allocate_value (type);
1643       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1644       int i;
1645       LONGEST low_bound, high_bound;
1646
1647       if (!get_array_bounds (type, &low_bound, &high_bound))
1648         error (_("Could not determine the vector bounds"));
1649
1650       for (i = 0; i < high_bound - low_bound + 1; i++)
1651         {
1652           tmp = value_neg (value_subscript (arg1, i));
1653           memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1654                   value_contents_all (tmp), TYPE_LENGTH (eltype));
1655         }
1656       return val;
1657     }
1658   else
1659     error (_("Argument to negate operation not a number."));
1660 }
1661
1662 struct value *
1663 value_complement (struct value *arg1)
1664 {
1665   struct type *type;
1666   struct value *val;
1667
1668   arg1 = coerce_ref (arg1);
1669   type = check_typedef (value_type (arg1));
1670
1671   if (is_integral_type (type))
1672     val = value_from_longest (type, ~value_as_long (arg1));
1673   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1674     {
1675       struct value *tmp;
1676       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1677       int i;
1678       LONGEST low_bound, high_bound;
1679
1680       if (!get_array_bounds (type, &low_bound, &high_bound))
1681         error (_("Could not determine the vector bounds"));
1682
1683       val = allocate_value (type);
1684       for (i = 0; i < high_bound - low_bound + 1; i++)
1685         {
1686           tmp = value_complement (value_subscript (arg1, i));
1687           memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1688                   value_contents_all (tmp), TYPE_LENGTH (eltype));
1689         }
1690     }
1691   else
1692     error (_("Argument to complement operation not an integer, boolean."));
1693
1694   return val;
1695 }
1696 \f
1697 /* The INDEX'th bit of SET value whose value_type is TYPE,
1698    and whose value_contents is valaddr.
1699    Return -1 if out of range, -2 other error.  */
1700
1701 int
1702 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1703 {
1704   struct gdbarch *gdbarch = get_type_arch (type);
1705   LONGEST low_bound, high_bound;
1706   LONGEST word;
1707   unsigned rel_index;
1708   struct type *range = TYPE_INDEX_TYPE (type);
1709
1710   if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1711     return -2;
1712   if (index < low_bound || index > high_bound)
1713     return -1;
1714   rel_index = index - low_bound;
1715   word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
1716                                    gdbarch_byte_order (gdbarch));
1717   rel_index %= TARGET_CHAR_BIT;
1718   if (gdbarch_bits_big_endian (gdbarch))
1719     rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1720   return (word >> rel_index) & 1;
1721 }
1722
1723 int
1724 value_in (struct value *element, struct value *set)
1725 {
1726   int member;
1727   struct type *settype = check_typedef (value_type (set));
1728   struct type *eltype = check_typedef (value_type (element));
1729
1730   if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1731     eltype = TYPE_TARGET_TYPE (eltype);
1732   if (TYPE_CODE (settype) != TYPE_CODE_SET)
1733     error (_("Second argument of 'IN' has wrong type"));
1734   if (TYPE_CODE (eltype) != TYPE_CODE_INT
1735       && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1736       && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1737       && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1738     error (_("First argument of 'IN' has wrong type"));
1739   member = value_bit_index (settype, value_contents (set),
1740                             value_as_long (element));
1741   if (member < 0)
1742     error (_("First argument of 'IN' not in range"));
1743   return member;
1744 }
1745
1746 void
1747 _initialize_valarith (void)
1748 {
1749 }