OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-6.8/] [gdb/] [valarith.c] - Blame information for rev 258

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.