]> Git Repo - binutils.git/blob - gdb/valarith.c
* partial-stab.h (N_TEXT): Put back GDB_TARGET_IS_HPPA kludge,
[binutils.git] / gdb / valarith.c
1 /* Perform arithmetic and other operations on values, for GDB.
2    Copyright 1986, 1989, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
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 "demangle.h"
28 #include <string.h>
29
30 /* Define whether or not the C operator '/' truncates towards zero for
31    differently signed operands (truncation direction is undefined in C). */
32
33 #ifndef TRUNCATION_TOWARDS_ZERO
34 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
35 #endif
36
37 static value
38 value_subscripted_rvalue PARAMS ((value, value));
39
40 \f
41 value
42 value_add (arg1, arg2)
43         value arg1, arg2;
44 {
45   register value valint, valptr;
46   register int len;
47
48   COERCE_ARRAY (arg1);
49   COERCE_ARRAY (arg2);
50
51   if ((TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
52        || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_PTR)
53       &&
54       (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT
55        || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT))
56     /* Exactly one argument is a pointer, and one is an integer.  */
57     {
58       if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
59         {
60           valptr = arg1;
61           valint = arg2;
62         }
63       else
64         {
65           valptr = arg2;
66           valint = arg1;
67         }
68       len = TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (valptr)));
69       if (len == 0) len = 1;    /* For (void *) */
70       return value_from_longest (VALUE_TYPE (valptr),
71                               value_as_long (valptr)
72                               + (len * value_as_long (valint)));
73     }
74
75   return value_binop (arg1, arg2, BINOP_ADD);
76 }
77
78 value
79 value_sub (arg1, arg2)
80         value arg1, arg2;
81 {
82
83   COERCE_ARRAY (arg1);
84   COERCE_ARRAY (arg2);
85
86   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
87     {
88       if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT)
89         {
90           /* pointer - integer.  */
91           return value_from_longest
92             (VALUE_TYPE (arg1),
93              value_as_long (arg1)
94              - (TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)))
95                 * value_as_long (arg2)));
96         }
97       else if (VALUE_TYPE (arg1) == VALUE_TYPE (arg2))
98         {
99           /* pointer to <type x> - pointer to <type x>.  */
100           return value_from_longest
101             (builtin_type_long,         /* FIXME -- should be ptrdiff_t */
102              (value_as_long (arg1) - value_as_long (arg2))
103              / TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))));
104         }
105       else
106         {
107           error ("\
108 First argument of `-' is a pointer and second argument is neither\n\
109 an integer nor a pointer of the same type.");
110         }
111     }
112
113   return value_binop (arg1, arg2, BINOP_SUB);
114 }
115
116 /* Return the value of ARRAY[IDX].
117    See comments in value_coerce_array() for rationale for reason for
118    doing lower bounds adjustment here rather than there.
119    FIXME:  Perhaps we should validate that the index is valid and if
120    verbosity is set, warn about invalid indices (but still use them). */
121
122 value
123 value_subscript (array, idx)
124      value array, idx;
125 {
126   int lowerbound;
127   value bound;
128   struct type *range_type;
129
130   COERCE_REF (array);
131
132   if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_ARRAY
133       || TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_STRING)
134     {
135       range_type = TYPE_FIELD_TYPE (VALUE_TYPE (array), 0);
136       lowerbound = TYPE_FIELD_BITPOS (range_type, 0);
137       if (lowerbound != 0)
138         {
139           bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
140           idx = value_sub (idx, bound);
141         }
142       if (VALUE_LVAL (array) != lval_memory)
143         {
144           return value_subscripted_rvalue (array, idx);
145         }
146       array = value_coerce_array (array);
147     }
148   return value_ind (value_add (array, idx));
149 }
150
151 /* Return the value of EXPR[IDX], expr an aggregate rvalue
152    (eg, a vector register).  This routine used to promote floats
153    to doubles, but no longer does.  */
154
155 static value
156 value_subscripted_rvalue (array, idx)
157      value array, idx;
158 {
159   struct type *elt_type = TYPE_TARGET_TYPE (VALUE_TYPE (array));
160   int elt_size = TYPE_LENGTH (elt_type);
161   int elt_offs = elt_size * longest_to_int (value_as_long (idx));
162   value v;
163
164   if (elt_offs >= TYPE_LENGTH (VALUE_TYPE (array)))
165     error ("no such vector element");
166
167   v = allocate_value (elt_type);
168   memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size);
169
170   if (VALUE_LVAL (array) == lval_internalvar)
171     VALUE_LVAL (v) = lval_internalvar_component;
172   else
173     VALUE_LVAL (v) = not_lval;
174   VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
175   VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
176   VALUE_BITSIZE (v) = elt_size * 8;
177   return v;
178 }
179 \f
180 /* Check to see if either argument is a structure.  This is called so
181    we know whether to go ahead with the normal binop or look for a 
182    user defined function instead.
183
184    For now, we do not overload the `=' operator.  */
185
186 int
187 binop_user_defined_p (op, arg1, arg2)
188      enum exp_opcode op;
189      value arg1, arg2;
190 {
191   if (op == BINOP_ASSIGN)
192     return 0;
193   return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
194           || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_STRUCT
195           || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
196               && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT)
197           || (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_REF
198               && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_STRUCT));
199 }
200
201 /* Check to see if argument is a structure.  This is called so
202    we know whether to go ahead with the normal unop or look for a 
203    user defined function instead.
204
205    For now, we do not overload the `&' operator.  */
206
207 int unop_user_defined_p (op, arg1)
208      enum exp_opcode op;
209      value arg1;
210 {
211   if (op == UNOP_ADDR)
212     return 0;
213   return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
214           || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
215               && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT));
216 }
217
218 /* We know either arg1 or arg2 is a structure, so try to find the right
219    user defined function.  Create an argument vector that calls 
220    arg1.operator @ (arg1,arg2) and return that value (where '@' is any
221    binary operator which is legal for GNU C++).
222
223    OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
224    is the opcode saying how to modify it.  Otherwise, OTHEROP is
225    unused.  */
226
227 value
228 value_x_binop (arg1, arg2, op, otherop)
229      value arg1, arg2;
230      enum exp_opcode op, otherop;
231 {
232   value * argvec;
233   char *ptr, *mangle_ptr;
234   char tstr[13], mangle_tstr[13];
235   int static_memfuncp;
236
237   COERCE_REF (arg1);
238   COERCE_REF (arg2);
239   COERCE_ENUM (arg1);
240   COERCE_ENUM (arg2);
241
242   /* now we know that what we have to do is construct our
243      arg vector and find the right function to call it with.  */
244
245   if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
246     error ("Can't do that binary op on that type");  /* FIXME be explicit */
247
248   argvec = (value *) alloca (sizeof (value) * 4);
249   argvec[1] = value_addr (arg1);
250   argvec[2] = arg2;
251   argvec[3] = 0;
252
253   /* make the right function name up */  
254   strcpy(tstr, "operator__");
255   ptr = tstr+8;
256   switch (op)
257     {
258     case BINOP_ADD:             strcpy(ptr,"+"); break;
259     case BINOP_SUB:             strcpy(ptr,"-"); break;
260     case BINOP_MUL:             strcpy(ptr,"*"); break;
261     case BINOP_DIV:             strcpy(ptr,"/"); break;
262     case BINOP_REM:             strcpy(ptr,"%"); break;
263     case BINOP_LSH:             strcpy(ptr,"<<"); break;
264     case BINOP_RSH:             strcpy(ptr,">>"); break;
265     case BINOP_BITWISE_AND:     strcpy(ptr,"&"); break;
266     case BINOP_BITWISE_IOR:     strcpy(ptr,"|"); break;
267     case BINOP_BITWISE_XOR:     strcpy(ptr,"^"); break;
268     case BINOP_LOGICAL_AND:     strcpy(ptr,"&&"); break;
269     case BINOP_LOGICAL_OR:      strcpy(ptr,"||"); break;
270     case BINOP_MIN:             strcpy(ptr,"<?"); break;
271     case BINOP_MAX:             strcpy(ptr,">?"); break;
272     case BINOP_ASSIGN:          strcpy(ptr,"="); break;
273     case BINOP_ASSIGN_MODIFY:   
274       switch (otherop)
275         {
276         case BINOP_ADD:         strcpy(ptr,"+="); break;
277         case BINOP_SUB:         strcpy(ptr,"-="); break;
278         case BINOP_MUL:         strcpy(ptr,"*="); break;
279         case BINOP_DIV:         strcpy(ptr,"/="); break;
280         case BINOP_REM:         strcpy(ptr,"%="); break;
281         case BINOP_BITWISE_AND: strcpy(ptr,"&="); break;
282         case BINOP_BITWISE_IOR: strcpy(ptr,"|="); break;
283         case BINOP_BITWISE_XOR: strcpy(ptr,"^="); break;
284         case BINOP_MOD:         /* invalid */
285         default:
286           error ("Invalid binary operation specified.");
287         }
288       break;
289     case BINOP_SUBSCRIPT: strcpy(ptr,"[]"); break;
290     case BINOP_EQUAL:     strcpy(ptr,"=="); break;
291     case BINOP_NOTEQUAL:  strcpy(ptr,"!="); break;
292     case BINOP_LESS:      strcpy(ptr,"<"); break;
293     case BINOP_GTR:       strcpy(ptr,">"); break;
294     case BINOP_GEQ:       strcpy(ptr,">="); break;
295     case BINOP_LEQ:       strcpy(ptr,"<="); break;
296     case BINOP_MOD:       /* invalid */
297     default:
298       error ("Invalid binary operation specified.");
299     }
300
301   argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
302   
303   if (argvec[0])
304     {
305       if (static_memfuncp)
306         {
307           argvec[1] = argvec[0];
308           argvec++;
309         }
310       return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
311     }
312   error ("member function %s not found", tstr);
313 #ifdef lint
314   return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
315 #endif
316 }
317
318 /* We know that arg1 is a structure, so try to find a unary user
319    defined operator that matches the operator in question.  
320    Create an argument vector that calls arg1.operator @ (arg1)
321    and return that value (where '@' is (almost) any unary operator which
322    is legal for GNU C++).  */
323
324 value
325 value_x_unop (arg1, op)
326      value arg1;
327      enum exp_opcode op;
328 {
329   value * argvec;
330   char *ptr, *mangle_ptr;
331   char tstr[13], mangle_tstr[13];
332   int static_memfuncp;
333
334   COERCE_ENUM (arg1);
335
336   /* now we know that what we have to do is construct our
337      arg vector and find the right function to call it with.  */
338
339   if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
340     error ("Can't do that unary op on that type");  /* FIXME be explicit */
341
342   argvec = (value *) alloca (sizeof (value) * 3);
343   argvec[1] = value_addr (arg1);
344   argvec[2] = 0;
345
346   /* make the right function name up */  
347   strcpy(tstr,"operator__");
348   ptr = tstr+8;
349   strcpy(mangle_tstr, "__");
350   mangle_ptr = mangle_tstr+2;
351   switch (op)
352     {
353     case UNOP_PREINCREMENT:     strcpy(ptr,"++"); break;
354     case UNOP_PREDECREMENT:     strcpy(ptr,"++"); break;
355     case UNOP_POSTINCREMENT:    strcpy(ptr,"++"); break;
356     case UNOP_POSTDECREMENT:    strcpy(ptr,"++"); break;
357     case UNOP_LOGICAL_NOT:      strcpy(ptr,"!"); break;
358     case UNOP_COMPLEMENT:       strcpy(ptr,"~"); break;
359     case UNOP_NEG:              strcpy(ptr,"-"); break;
360     default:
361       error ("Invalid binary operation specified.");
362     }
363
364   argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
365
366   if (argvec[0])
367     {
368       if (static_memfuncp)
369         {
370           argvec[1] = argvec[0];
371           argvec++;
372         }
373       return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1);
374     }
375   error ("member function %s not found", tstr);
376   return 0;  /* For lint -- never reached */
377 }
378
379 \f
380 /* Concatenate two values with the following conditions:
381
382    (1)  Both values must be either bitstring values or character string
383         values and the resulting value consists of the concatenation of
384         ARG1 followed by ARG2.
385
386         or
387
388         One value must be an integer value and the other value must be
389         either a bitstring value or character string value, which is
390         to be repeated by the number of times specified by the integer
391         value.
392
393
394     (2) Boolean values are also allowed and are treated as bit string
395         values of length 1.
396
397     (3) Character values are also allowed and are treated as character
398         string values of length 1.
399 */
400
401 value
402 value_concat (arg1, arg2)
403      value arg1, arg2;
404 {
405   register value inval1, inval2, outval;
406   int inval1len, inval2len;
407   int count, idx;
408   char *ptr;
409   char inchar;
410
411   /* First figure out if we are dealing with two values to be concatenated
412      or a repeat count and a value to be repeated.  INVAL1 is set to the
413      first of two concatenated values, or the repeat count.  INVAL2 is set
414      to the second of the two concatenated values or the value to be 
415      repeated. */
416
417   if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT)
418     {
419       inval1 = arg2;
420       inval2 = arg1;
421     }
422   else
423     {
424       inval1 = arg1;
425       inval2 = arg2;
426     }
427
428   /* Now process the input values. */
429
430   if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_INT)
431     {
432       /* We have a repeat count.  Validate the second value and then
433          construct a value repeated that many times. */
434       if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_STRING
435           || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
436         {
437           count = longest_to_int (value_as_long (inval1));
438           inval2len = TYPE_LENGTH (VALUE_TYPE (inval2));
439           ptr = (char *) alloca (count * inval2len);
440           if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
441             {
442               inchar = (char) unpack_long (VALUE_TYPE (inval2),
443                                            VALUE_CONTENTS (inval2));
444               for (idx = 0; idx < count; idx++)
445                 {
446                   *(ptr + idx) = inchar;
447                 }
448             }
449           else
450             {
451               for (idx = 0; idx < count; idx++)
452                 {
453                   memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2),
454                           inval2len);
455                 }
456             }
457           outval = value_string (ptr, count * inval2len);
458         }
459       else if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BITSTRING
460                || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BOOL)
461         {
462           error ("unimplemented support for bitstring/boolean repeats");
463         }
464       else
465         {
466           error ("can't repeat values of that type");
467         }
468     }
469   else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_STRING
470       || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR)
471     {
472       /* We have two character strings to concatenate. */
473       if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_STRING
474           && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_CHAR)
475         {
476           error ("Strings can only be concatenated with other strings.");
477         }
478       inval1len = TYPE_LENGTH (VALUE_TYPE (inval1));
479       inval2len = TYPE_LENGTH (VALUE_TYPE (inval2));
480       ptr = (char *) alloca (inval1len + inval2len);
481       if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR)
482         {
483           *ptr = (char) unpack_long (VALUE_TYPE (inval1), VALUE_CONTENTS (inval1));
484         }
485       else
486         {
487           memcpy (ptr, VALUE_CONTENTS (inval1), inval1len);
488         }
489       if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
490         {
491           *(ptr + inval1len) = 
492             (char) unpack_long (VALUE_TYPE (inval2), VALUE_CONTENTS (inval2));
493         }
494       else
495         {
496           memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len);
497         }
498       outval = value_string (ptr, inval1len + inval2len);
499     }
500   else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BITSTRING
501            || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BOOL)
502     {
503       /* We have two bitstrings to concatenate. */
504       if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BITSTRING
505           && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BOOL)
506         {
507           error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
508         }
509       error ("unimplemented support for bitstring/boolean concatenation.");
510     }      
511   else
512     {
513       /* We don't know how to concatenate these operands. */
514       error ("illegal operands for concatenation.");
515     }
516   return (outval);
517 }
518
519 \f
520 /* The type we give to value_binop results.  This is a kludge to get around
521    the fact that we don't know how to determine the result type from
522    the types of the operands.  (I'm not really sure how much we feel
523    the need to duplicate the exact rules of the current language.
524    They can get really hairy.  But not to do so makes it hard to document
525    just what we *do* do).  */
526 static struct type *signed_operation_result;
527 static struct type *unsigned_operation_result;
528
529 /* Perform a binary operation on two operands which have reasonable
530    representations as integers or floats.  This includes booleans,
531    characters, integers, or floats.
532    Does not support addition and subtraction on pointers;
533    use value_add or value_sub if you want to handle those possibilities.  */
534
535 value
536 value_binop (arg1, arg2, op)
537      value arg1, arg2;
538      enum exp_opcode op;
539 {
540   register value val;
541
542   COERCE_ENUM (arg1);
543   COERCE_ENUM (arg2);
544
545   if ((TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_FLT
546        &&
547        TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_CHAR
548        &&
549        TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT
550        &&
551        TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_BOOL)
552       ||
553       (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_FLT
554        &&
555        TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_CHAR
556        &&
557        TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT
558        &&
559        TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_BOOL))
560     error ("Argument to arithmetic operation not a number or boolean.");
561
562   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT
563       ||
564       TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_FLT)
565     {
566       /* FIXME-if-picky-about-floating-accuracy: Should be doing this
567          in target format.  real.c in GCC probably has the necessary
568          code.  */
569       double v1, v2, v;
570       v1 = value_as_double (arg1);
571       v2 = value_as_double (arg2);
572       switch (op)
573         {
574         case BINOP_ADD:
575           v = v1 + v2;
576           break;
577
578         case BINOP_SUB:
579           v = v1 - v2;
580           break;
581
582         case BINOP_MUL:
583           v = v1 * v2;
584           break;
585
586         case BINOP_DIV:
587           v = v1 / v2;
588           break;
589
590         default:
591           error ("Integer-only operation on floating point number.");
592         }
593
594       val = allocate_value (builtin_type_double);
595       store_floating (VALUE_CONTENTS_RAW (val), TYPE_LENGTH (VALUE_TYPE (val)),
596                       v);
597     }
598   else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_BOOL
599            &&
600            TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_BOOL)
601       {
602           LONGEST v1, v2, v;
603           v1 = value_as_long (arg1);
604           v2 = value_as_long (arg2);
605           
606           switch (op)
607             {
608             case BINOP_BITWISE_AND:
609               v = v1 & v2;
610               break;
611               
612             case BINOP_BITWISE_IOR:
613               v = v1 | v2;
614               break;
615               
616             case BINOP_BITWISE_XOR:
617               v = v1 ^ v2;
618               break;
619               
620             default:
621               error ("Invalid operation on booleans.");
622             }
623           
624           val = allocate_value (builtin_type_chill_bool);
625           store_signed_integer (VALUE_CONTENTS_RAW (val),
626                                 TYPE_LENGTH (VALUE_TYPE (val)),
627                                 v);
628       }
629   else
630     /* Integral operations here.  */
631     /* FIXME:  Also mixed integral/booleans, with result an integer. */
632     {
633       /* Should we promote to unsigned longest?  */
634       if ((TYPE_UNSIGNED (VALUE_TYPE (arg1))
635            || TYPE_UNSIGNED (VALUE_TYPE (arg2)))
636           && (TYPE_LENGTH (VALUE_TYPE (arg1)) >= sizeof (unsigned LONGEST)
637               || TYPE_LENGTH (VALUE_TYPE (arg2)) >= sizeof (unsigned LONGEST)))
638         {
639           unsigned LONGEST v1, v2, v;
640           v1 = (unsigned LONGEST) value_as_long (arg1);
641           v2 = (unsigned LONGEST) value_as_long (arg2);
642           
643           switch (op)
644             {
645             case BINOP_ADD:
646               v = v1 + v2;
647               break;
648               
649             case BINOP_SUB:
650               v = v1 - v2;
651               break;
652               
653             case BINOP_MUL:
654               v = v1 * v2;
655               break;
656               
657             case BINOP_DIV:
658               v = v1 / v2;
659               break;
660               
661             case BINOP_REM:
662               v = v1 % v2;
663               break;
664               
665             case BINOP_MOD:
666               /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
667                  v1 mod 0 has a defined value, v1. */
668               /* Chill specifies that v2 must be > 0, so check for that. */
669               if (current_language -> la_language == language_chill
670                   && value_as_long (arg2) <= 0)
671                 {
672                   error ("Second operand of MOD must be greater than zero.");
673                 }
674               if (v2 == 0)
675                 {
676                   v = v1;
677                 }
678               else
679                 {
680                   v = v1/v2;
681                   /* Note floor(v1/v2) == v1/v2 for unsigned. */
682                   v = v1 - (v2 * v);
683                 }
684               break;
685               
686             case BINOP_LSH:
687               v = v1 << v2;
688               break;
689               
690             case BINOP_RSH:
691               v = v1 >> v2;
692               break;
693               
694             case BINOP_BITWISE_AND:
695               v = v1 & v2;
696               break;
697               
698             case BINOP_BITWISE_IOR:
699               v = v1 | v2;
700               break;
701               
702             case BINOP_BITWISE_XOR:
703               v = v1 ^ v2;
704               break;
705               
706             case BINOP_LOGICAL_AND:
707               v = v1 && v2;
708               break;
709               
710             case BINOP_LOGICAL_OR:
711               v = v1 || v2;
712               break;
713               
714             case BINOP_MIN:
715               v = v1 < v2 ? v1 : v2;
716               break;
717               
718             case BINOP_MAX:
719               v = v1 > v2 ? v1 : v2;
720               break;
721               
722             default:
723               error ("Invalid binary operation on numbers.");
724             }
725
726           val = allocate_value (unsigned_operation_result);
727           store_unsigned_integer (VALUE_CONTENTS_RAW (val),
728                                   TYPE_LENGTH (VALUE_TYPE (val)),
729                                   v);
730         }
731       else
732         {
733           LONGEST v1, v2, v;
734           v1 = value_as_long (arg1);
735           v2 = value_as_long (arg2);
736           
737           switch (op)
738             {
739             case BINOP_ADD:
740               v = v1 + v2;
741               break;
742               
743             case BINOP_SUB:
744               v = v1 - v2;
745               break;
746               
747             case BINOP_MUL:
748               v = v1 * v2;
749               break;
750               
751             case BINOP_DIV:
752               v = v1 / v2;
753               break;
754               
755             case BINOP_REM:
756               v = v1 % v2;
757               break;
758               
759             case BINOP_MOD:
760               /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
761                  X mod 0 has a defined value, X. */
762               /* Chill specifies that v2 must be > 0, so check for that. */
763               if (current_language -> la_language == language_chill
764                   && v2 <= 0)
765                 {
766                   error ("Second operand of MOD must be greater than zero.");
767                 }
768               if (v2 == 0)
769                 {
770                   v = v1;
771                 }
772               else
773                 {
774                   v = v1/v2;
775                   /* Compute floor. */
776                   if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
777                     {
778                       v--;
779                     }
780                   v = v1 - (v2 * v);
781                 }
782               break;
783               
784             case BINOP_LSH:
785               v = v1 << v2;
786               break;
787               
788             case BINOP_RSH:
789               v = v1 >> v2;
790               break;
791               
792             case BINOP_BITWISE_AND:
793               v = v1 & v2;
794               break;
795               
796             case BINOP_BITWISE_IOR:
797               v = v1 | v2;
798               break;
799               
800             case BINOP_BITWISE_XOR:
801               v = v1 ^ v2;
802               break;
803               
804             case BINOP_LOGICAL_AND:
805               v = v1 && v2;
806               break;
807               
808             case BINOP_LOGICAL_OR:
809               v = v1 || v2;
810               break;
811               
812             case BINOP_MIN:
813               v = v1 < v2 ? v1 : v2;
814               break;
815               
816             case BINOP_MAX:
817               v = v1 > v2 ? v1 : v2;
818               break;
819               
820             default:
821               error ("Invalid binary operation on numbers.");
822             }
823           
824           val = allocate_value (signed_operation_result);
825           store_signed_integer (VALUE_CONTENTS_RAW (val),
826                                 TYPE_LENGTH (VALUE_TYPE (val)),
827                                 v);
828         }
829     }
830
831   return val;
832 }
833 \f
834 /* Simulate the C operator ! -- return 1 if ARG1 contains zero.  */
835
836 int
837 value_logical_not (arg1)
838      value arg1;
839 {
840   register int len;
841   register char *p;
842
843   COERCE_ARRAY (arg1);
844
845   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT)
846     return 0 == value_as_double (arg1);
847
848   len = TYPE_LENGTH (VALUE_TYPE (arg1));
849   p = VALUE_CONTENTS (arg1);
850
851   while (--len >= 0)
852     {
853       if (*p++)
854         break;
855     }
856
857   return len < 0;
858 }
859
860 /* Simulate the C operator == by returning a 1
861    iff ARG1 and ARG2 have equal contents.  */
862
863 int
864 value_equal (arg1, arg2)
865      register value arg1, arg2;
866
867 {
868   register int len;
869   register char *p1, *p2;
870   enum type_code code1;
871   enum type_code code2;
872
873   COERCE_ARRAY (arg1);
874   COERCE_ARRAY (arg2);
875
876   code1 = TYPE_CODE (VALUE_TYPE (arg1));
877   code2 = TYPE_CODE (VALUE_TYPE (arg2));
878
879   if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
880     return value_as_long (arg1) == value_as_long (arg2);
881   else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
882            && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
883     return value_as_double (arg1) == value_as_double (arg2);
884
885   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
886      is bigger.  */
887   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
888     return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2);
889   else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
890     return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2);
891
892   else if (code1 == code2
893            && ((len = TYPE_LENGTH (VALUE_TYPE (arg1)))
894                == TYPE_LENGTH (VALUE_TYPE (arg2))))
895     {
896       p1 = VALUE_CONTENTS (arg1);
897       p2 = VALUE_CONTENTS (arg2);
898       while (--len >= 0)
899         {
900           if (*p1++ != *p2++)
901             break;
902         }
903       return len < 0;
904     }
905   else
906     {
907       error ("Invalid type combination in equality test.");
908       return 0;  /* For lint -- never reached */
909     }
910 }
911
912 /* Simulate the C operator < by returning 1
913    iff ARG1's contents are less than ARG2's.  */
914
915 int
916 value_less (arg1, arg2)
917      register value arg1, arg2;
918 {
919   register enum type_code code1;
920   register enum type_code code2;
921
922   COERCE_ARRAY (arg1);
923   COERCE_ARRAY (arg2);
924
925   code1 = TYPE_CODE (VALUE_TYPE (arg1));
926   code2 = TYPE_CODE (VALUE_TYPE (arg2));
927
928   if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
929     {
930       if (TYPE_UNSIGNED (VALUE_TYPE (arg1))
931        || TYPE_UNSIGNED (VALUE_TYPE (arg2)))
932         return ((unsigned LONGEST) value_as_long (arg1)
933                 < (unsigned LONGEST) value_as_long (arg2));
934       else
935         return value_as_long (arg1) < value_as_long (arg2);
936     }
937   else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
938            && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
939     return value_as_double (arg1) < value_as_double (arg2);
940   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
941     return value_as_pointer (arg1) < value_as_pointer (arg2);
942
943   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
944      is bigger.  */
945   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
946     return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2);
947   else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
948     return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2);
949
950   else
951     {
952       error ("Invalid type combination in ordering comparison.");
953       return 0;
954     }
955 }
956 \f
957 /* The unary operators - and ~.  Both free the argument ARG1.  */
958
959 value
960 value_neg (arg1)
961      register value arg1;
962 {
963   register struct type *type;
964
965   COERCE_ENUM (arg1);
966
967   type = VALUE_TYPE (arg1);
968
969   if (TYPE_CODE (type) == TYPE_CODE_FLT)
970     return value_from_double (type, - value_as_double (arg1));
971   else if (TYPE_CODE (type) == TYPE_CODE_INT)
972     return value_from_longest (type, - value_as_long (arg1));
973   else {
974     error ("Argument to negate operation not a number.");
975     return 0;  /* For lint -- never reached */
976   }
977 }
978
979 value
980 value_complement (arg1)
981      register value arg1;
982 {
983   COERCE_ENUM (arg1);
984
985   if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT)
986     error ("Argument to complement operation not an integer.");
987
988   return value_from_longest (VALUE_TYPE (arg1), ~ value_as_long (arg1));
989 }
990 \f
991 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
992    and whose VALUE_CONTENTS is valaddr.
993    Return -1 if out of range, -2 other error. */
994
995 int
996 value_bit_index (type, valaddr, index)
997      struct type *type;
998      char *valaddr;
999      int index;
1000 {
1001   struct type *range;
1002   int low_bound, high_bound, bit_length;
1003   LONGEST word;
1004   range = TYPE_FIELD_TYPE (type, 0);
1005   if (TYPE_CODE (range) != TYPE_CODE_RANGE)
1006     return -2;
1007   low_bound = TYPE_LOW_BOUND (range);
1008   high_bound = TYPE_HIGH_BOUND (range);
1009   if (index < low_bound || index > high_bound)
1010     return -1;
1011   bit_length = high_bound - low_bound + 1;
1012   index -= low_bound;
1013   if (bit_length <= TARGET_CHAR_BIT)
1014     word = unpack_long (builtin_type_unsigned_char, valaddr);
1015   else if (bit_length <= TARGET_SHORT_BIT)
1016     word = unpack_long (builtin_type_unsigned_short, valaddr);
1017   else
1018     {
1019       int word_start_index = (index / TARGET_INT_BIT) * TARGET_INT_BIT;
1020       index -= word_start_index;
1021       word = unpack_long (builtin_type_unsigned_int,
1022                           valaddr + (word_start_index / HOST_CHAR_BIT));
1023     }
1024 #if BITS_BIG_ENDIAN
1025   if (bit_length <= TARGET_CHAR_BIT)
1026     index = TARGET_CHAR_BIT - 1 - index;
1027   else if (bit_length <= TARGET_SHORT_BIT)
1028     index = TARGET_SHORT_BIT - 1 - index;
1029   else
1030     index = TARGET_INT_BIT - 1 - index;
1031 #endif
1032   return (word >> index) & 1;
1033 }
1034
1035 value
1036 value_in (element, set)
1037      value element, set;
1038 {
1039   int member;
1040   if (TYPE_CODE (VALUE_TYPE (set)) != TYPE_CODE_SET)
1041     error ("Second argument of 'IN' has wrong type");
1042   if (TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_INT
1043       && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_CHAR
1044       && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_ENUM
1045       && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_BOOL)
1046     error ("First argument of 'IN' has wrong type");
1047   member = value_bit_index (VALUE_TYPE (set), VALUE_CONTENTS (set),
1048                             value_as_long (element));
1049   if (member < 0)
1050     error ("First argument of 'IN' not in range");
1051   return value_from_longest (builtin_type_int, member);
1052 }
1053
1054 void
1055 _initialize_valarith ()
1056 {
1057   /* Can't just call init_type because we wouldn't know what names to give
1058      them.  */
1059   if (sizeof (LONGEST) > TARGET_LONG_BIT / HOST_CHAR_BIT)
1060     {
1061       unsigned_operation_result = builtin_type_unsigned_long_long;
1062       signed_operation_result = builtin_type_long_long;
1063     }
1064   else
1065     {
1066       unsigned_operation_result = builtin_type_unsigned_long;
1067       signed_operation_result = builtin_type_long;
1068     }
1069 }
This page took 0.093643 seconds and 4 git commands to generate.