]> Git Repo - binutils.git/blob - gdb/opencl-lang.c
5bd3edb4adc96c81541fa80d2ddf1eb6418b996e
[binutils.git] / gdb / opencl-lang.c
1 /* OpenCL language support for GDB, the GNU debugger.
2    Copyright (C) 2010-2022 Free Software Foundation, Inc.
3
4    Contributed by Ken Werner <[email protected]>.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "gdbtypes.h"
23 #include "symtab.h"
24 #include "expression.h"
25 #include "parser-defs.h"
26 #include "language.h"
27 #include "varobj.h"
28 #include "c-lang.h"
29 #include "gdbarch.h"
30 #include "c-exp.h"
31
32 /* Returns the corresponding OpenCL vector type from the given type code,
33    the length of the element type, the unsigned flag and the amount of
34    elements (N).  */
35
36 static struct type *
37 lookup_opencl_vector_type (struct gdbarch *gdbarch, enum type_code code,
38                            unsigned int el_length, unsigned int flag_unsigned,
39                            int n)
40 {
41   unsigned int length;
42
43   /* Check if n describes a valid OpenCL vector size (2, 3, 4, 8, 16).  */
44   if (n != 2 && n != 3 && n != 4 && n != 8 && n != 16)
45     error (_("Invalid OpenCL vector size: %d"), n);
46
47   /* Triple vectors have the size of a quad vector.  */
48   length = (n == 3) ?  el_length * 4 : el_length * n;
49
50   auto filter = [&] (struct type *type)
51   {
52     LONGEST lowb, highb;
53
54     return (type->code () == TYPE_CODE_ARRAY && type->is_vector ()
55             && get_array_bounds (type, &lowb, &highb)
56             && type->target_type ()->code () == code
57             && type->target_type ()->is_unsigned () == flag_unsigned
58             && TYPE_LENGTH (type->target_type ()) == el_length
59             && TYPE_LENGTH (type) == length
60             && highb - lowb + 1 == n);
61   };
62   const struct language_defn *lang = language_def (language_opencl);
63   return language_lookup_primitive_type (lang, gdbarch, filter);
64 }
65
66 /* Returns nonzero if the array ARR contains duplicates within
67      the first N elements.  */
68
69 static int
70 array_has_dups (int *arr, int n)
71 {
72   int i, j;
73
74   for (i = 0; i < n; i++)
75     {
76       for (j = i + 1; j < n; j++)
77         {
78           if (arr[i] == arr[j])
79             return 1;
80         }
81     }
82
83   return 0;
84 }
85
86 /* The OpenCL component access syntax allows to create lvalues referring to
87    selected elements of an original OpenCL vector in arbitrary order.  This
88    structure holds the information to describe such lvalues.  */
89
90 struct lval_closure
91 {
92   /* Reference count.  */
93   int refc;
94   /* The number of indices.  */
95   int n;
96   /* The element indices themselves.  */
97   int *indices;
98   /* A pointer to the original value.  */
99   struct value *val;
100 };
101
102 /* Allocates an instance of struct lval_closure.  */
103
104 static struct lval_closure *
105 allocate_lval_closure (int *indices, int n, struct value *val)
106 {
107   struct lval_closure *c = XCNEW (struct lval_closure);
108
109   c->refc = 1;
110   c->n = n;
111   c->indices = XCNEWVEC (int, n);
112   memcpy (c->indices, indices, n * sizeof (int));
113   value_incref (val); /* Increment the reference counter of the value.  */
114   c->val = val;
115
116   return c;
117 }
118
119 static void
120 lval_func_read (struct value *v)
121 {
122   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
123   struct type *type = check_typedef (value_type (v));
124   struct type *eltype = check_typedef (value_type (c->val))->target_type ();
125   LONGEST offset = value_offset (v);
126   LONGEST elsize = TYPE_LENGTH (eltype);
127   int n, i, j = 0;
128   LONGEST lowb = 0;
129   LONGEST highb = 0;
130
131   if (type->code () == TYPE_CODE_ARRAY
132       && !get_array_bounds (type, &lowb, &highb))
133     error (_("Could not determine the vector bounds"));
134
135   /* Assume elsize aligned offset.  */
136   gdb_assert (offset % elsize == 0);
137   offset /= elsize;
138   n = offset + highb - lowb + 1;
139   gdb_assert (n <= c->n);
140
141   for (i = offset; i < n; i++)
142     memcpy (value_contents_raw (v).data () + j++ * elsize,
143             value_contents (c->val).data () + c->indices[i] * elsize,
144             elsize);
145 }
146
147 static void
148 lval_func_write (struct value *v, struct value *fromval)
149 {
150   struct value *mark = value_mark ();
151   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
152   struct type *type = check_typedef (value_type (v));
153   struct type *eltype = check_typedef (value_type (c->val))->target_type ();
154   LONGEST offset = value_offset (v);
155   LONGEST elsize = TYPE_LENGTH (eltype);
156   int n, i, j = 0;
157   LONGEST lowb = 0;
158   LONGEST highb = 0;
159
160   if (type->code () == TYPE_CODE_ARRAY
161       && !get_array_bounds (type, &lowb, &highb))
162     error (_("Could not determine the vector bounds"));
163
164   /* Assume elsize aligned offset.  */
165   gdb_assert (offset % elsize == 0);
166   offset /= elsize;
167   n = offset + highb - lowb + 1;
168
169   /* Since accesses to the fourth component of a triple vector is undefined we
170      just skip writes to the fourth element.  Imagine something like this:
171        int3 i3 = (int3)(0, 1, 2);
172        i3.hi.hi = 5;
173      In this case n would be 4 (offset=12/4 + 1) while c->n would be 3.  */
174   if (n > c->n)
175     n = c->n;
176
177   for (i = offset; i < n; i++)
178     {
179       struct value *from_elm_val = allocate_value (eltype);
180       struct value *to_elm_val = value_subscript (c->val, c->indices[i]);
181
182       memcpy (value_contents_writeable (from_elm_val).data (),
183               value_contents (fromval).data () + j++ * elsize,
184               elsize);
185       value_assign (to_elm_val, from_elm_val);
186     }
187
188   value_free_to_mark (mark);
189 }
190
191 /* Return nonzero if bits in V from OFFSET and LENGTH represent a
192    synthetic pointer.  */
193
194 static int
195 lval_func_check_synthetic_pointer (const struct value *v,
196                                    LONGEST offset, int length)
197 {
198   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
199   /* Size of the target type in bits.  */
200   int elsize =
201       TYPE_LENGTH (check_typedef (value_type (c->val))->target_type ()) * 8;
202   int startrest = offset % elsize;
203   int start = offset / elsize;
204   int endrest = (offset + length) % elsize;
205   int end = (offset + length) / elsize;
206   int i;
207
208   if (endrest)
209     end++;
210
211   if (end > c->n)
212     return 0;
213
214   for (i = start; i < end; i++)
215     {
216       int comp_offset = (i == start) ? startrest : 0;
217       int comp_length = (i == end) ? endrest : elsize;
218
219       if (!value_bits_synthetic_pointer (c->val,
220                                          c->indices[i] * elsize + comp_offset,
221                                          comp_length))
222         return 0;
223     }
224
225   return 1;
226 }
227
228 static void *
229 lval_func_copy_closure (const struct value *v)
230 {
231   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
232
233   ++c->refc;
234
235   return c;
236 }
237
238 static void
239 lval_func_free_closure (struct value *v)
240 {
241   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
242
243   --c->refc;
244
245   if (c->refc == 0)
246     {
247       value_decref (c->val); /* Decrement the reference counter of the value.  */
248       xfree (c->indices);
249       xfree (c);
250     }
251 }
252
253 static const struct lval_funcs opencl_value_funcs =
254   {
255     lval_func_read,
256     lval_func_write,
257     nullptr,
258     NULL,       /* indirect */
259     NULL,       /* coerce_ref */
260     lval_func_check_synthetic_pointer,
261     lval_func_copy_closure,
262     lval_func_free_closure
263   };
264
265 /* Creates a sub-vector from VAL.  The elements are selected by the indices of
266    an array with the length of N.  Supported values for NOSIDE are
267    EVAL_NORMAL and EVAL_AVOID_SIDE_EFFECTS.  */
268
269 static struct value *
270 create_value (struct gdbarch *gdbarch, struct value *val, enum noside noside,
271               int *indices, int n)
272 {
273   struct type *type = check_typedef (value_type (val));
274   struct type *elm_type = type->target_type ();
275   struct value *ret;
276
277   /* Check if a single component of a vector is requested which means
278      the resulting type is a (primitive) scalar type.  */
279   if (n == 1)
280     {
281       if (noside == EVAL_AVOID_SIDE_EFFECTS)
282         ret = value_zero (elm_type, not_lval);
283       else
284         ret = value_subscript (val, indices[0]);
285     }
286   else
287     {
288       /* Multiple components of the vector are requested which means the
289          resulting type is a vector as well.  */
290       struct type *dst_type =
291         lookup_opencl_vector_type (gdbarch, elm_type->code (),
292                                    TYPE_LENGTH (elm_type),
293                                    elm_type->is_unsigned (), n);
294
295       if (dst_type == NULL)
296         dst_type = init_vector_type (elm_type, n);
297
298       make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type), dst_type, NULL);
299
300       if (noside == EVAL_AVOID_SIDE_EFFECTS)
301         ret = allocate_value (dst_type);
302       else
303         {
304           /* Check whether to create a lvalue or not.  */
305           if (VALUE_LVAL (val) != not_lval && !array_has_dups (indices, n))
306             {
307               struct lval_closure *c = allocate_lval_closure (indices, n, val);
308               ret = allocate_computed_value (dst_type, &opencl_value_funcs, c);
309             }
310           else
311             {
312               int i;
313
314               ret = allocate_value (dst_type);
315
316               /* Copy src val contents into the destination value.  */
317               for (i = 0; i < n; i++)
318                 memcpy (value_contents_writeable (ret).data ()
319                         + (i * TYPE_LENGTH (elm_type)),
320                         value_contents (val).data ()
321                         + (indices[i] * TYPE_LENGTH (elm_type)),
322                         TYPE_LENGTH (elm_type));
323             }
324         }
325     }
326   return ret;
327 }
328
329 /* OpenCL vector component access.  */
330
331 static struct value *
332 opencl_component_ref (struct expression *exp, struct value *val,
333                       const char *comps, enum noside noside)
334 {
335   LONGEST lowb, highb;
336   int src_len;
337   struct value *v;
338   int indices[16], i;
339   int dst_len;
340
341   if (!get_array_bounds (check_typedef (value_type (val)), &lowb, &highb))
342     error (_("Could not determine the vector bounds"));
343
344   src_len = highb - lowb + 1;
345
346   /* Throw an error if the amount of array elements does not fit a
347      valid OpenCL vector size (2, 3, 4, 8, 16).  */
348   if (src_len != 2 && src_len != 3 && src_len != 4 && src_len != 8
349       && src_len != 16)
350     error (_("Invalid OpenCL vector size"));
351
352   if (strcmp (comps, "lo") == 0 )
353     {
354       dst_len = (src_len == 3) ? 2 : src_len / 2;
355
356       for (i = 0; i < dst_len; i++)
357         indices[i] = i;
358     }
359   else if (strcmp (comps, "hi") == 0)
360     {
361       dst_len = (src_len == 3) ? 2 : src_len / 2;
362
363       for (i = 0; i < dst_len; i++)
364         indices[i] = dst_len + i;
365     }
366   else if (strcmp (comps, "even") == 0)
367     {
368       dst_len = (src_len == 3) ? 2 : src_len / 2;
369
370       for (i = 0; i < dst_len; i++)
371         indices[i] = i*2;
372     }
373   else if (strcmp (comps, "odd") == 0)
374     {
375       dst_len = (src_len == 3) ? 2 : src_len / 2;
376
377       for (i = 0; i < dst_len; i++)
378         indices[i] = i*2+1;
379     }
380   else if (strncasecmp (comps, "s", 1) == 0)
381     {
382 #define HEXCHAR_TO_INT(C) ((C >= '0' && C <= '9') ? \
383                            C-'0' : ((C >= 'A' && C <= 'F') ? \
384                            C-'A'+10 : ((C >= 'a' && C <= 'f') ? \
385                            C-'a'+10 : -1)))
386
387       dst_len = strlen (comps);
388       /* Skip the s/S-prefix.  */
389       dst_len--;
390
391       for (i = 0; i < dst_len; i++)
392         {
393           indices[i] = HEXCHAR_TO_INT(comps[i+1]);
394           /* Check if the requested component is invalid or exceeds
395              the vector.  */
396           if (indices[i] < 0 || indices[i] >= src_len)
397             error (_("Invalid OpenCL vector component accessor %s"), comps);
398         }
399     }
400   else
401     {
402       dst_len = strlen (comps);
403
404       for (i = 0; i < dst_len; i++)
405         {
406           /* x, y, z, w */
407           switch (comps[i])
408           {
409           case 'x':
410             indices[i] = 0;
411             break;
412           case 'y':
413             indices[i] = 1;
414             break;
415           case 'z':
416             if (src_len < 3)
417               error (_("Invalid OpenCL vector component accessor %s"), comps);
418             indices[i] = 2;
419             break;
420           case 'w':
421             if (src_len < 4)
422               error (_("Invalid OpenCL vector component accessor %s"), comps);
423             indices[i] = 3;
424             break;
425           default:
426             error (_("Invalid OpenCL vector component accessor %s"), comps);
427             break;
428           }
429         }
430     }
431
432   /* Throw an error if the amount of requested components does not
433      result in a valid length (1, 2, 3, 4, 8, 16).  */
434   if (dst_len != 1 && dst_len != 2 && dst_len != 3 && dst_len != 4
435       && dst_len != 8 && dst_len != 16)
436     error (_("Invalid OpenCL vector component accessor %s"), comps);
437
438   v = create_value (exp->gdbarch, val, noside, indices, dst_len);
439
440   return v;
441 }
442
443 /* Perform the unary logical not (!) operation.  */
444
445 struct value *
446 opencl_logical_not (struct type *expect_type, struct expression *exp,
447                     enum noside noside, enum exp_opcode op,
448                     struct value *arg)
449 {
450   struct type *type = check_typedef (value_type (arg));
451   struct type *rettype;
452   struct value *ret;
453
454   if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
455     {
456       struct type *eltype = check_typedef (type->target_type ());
457       LONGEST lowb, highb;
458       int i;
459
460       if (!get_array_bounds (type, &lowb, &highb))
461         error (_("Could not determine the vector bounds"));
462
463       /* Determine the resulting type of the operation and allocate the
464          value.  */
465       rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
466                                            TYPE_LENGTH (eltype), 0,
467                                            highb - lowb + 1);
468       ret = allocate_value (rettype);
469
470       for (i = 0; i < highb - lowb + 1; i++)
471         {
472           /* For vector types, the unary operator shall return a 0 if the
473           value of its operand compares unequal to 0, and -1 (i.e. all bits
474           set) if the value of its operand compares equal to 0.  */
475           int tmp = value_logical_not (value_subscript (arg, i)) ? -1 : 0;
476           memset ((value_contents_writeable (ret).data ()
477                    + i * TYPE_LENGTH (eltype)),
478                   tmp, TYPE_LENGTH (eltype));
479         }
480     }
481   else
482     {
483       rettype = language_bool_type (exp->language_defn, exp->gdbarch);
484       ret = value_from_longest (rettype, value_logical_not (arg));
485     }
486
487   return ret;
488 }
489
490 /* Perform a relational operation on two scalar operands.  */
491
492 static int
493 scalar_relop (struct value *val1, struct value *val2, enum exp_opcode op)
494 {
495   int ret;
496
497   switch (op)
498     {
499     case BINOP_EQUAL:
500       ret = value_equal (val1, val2);
501       break;
502     case BINOP_NOTEQUAL:
503       ret = !value_equal (val1, val2);
504       break;
505     case BINOP_LESS:
506       ret = value_less (val1, val2);
507       break;
508     case BINOP_GTR:
509       ret = value_less (val2, val1);
510       break;
511     case BINOP_GEQ:
512       ret = value_less (val2, val1) || value_equal (val1, val2);
513       break;
514     case BINOP_LEQ:
515       ret = value_less (val1, val2) || value_equal (val1, val2);
516       break;
517     case BINOP_LOGICAL_AND:
518       ret = !value_logical_not (val1) && !value_logical_not (val2);
519       break;
520     case BINOP_LOGICAL_OR:
521       ret = !value_logical_not (val1) || !value_logical_not (val2);
522       break;
523     default:
524       error (_("Attempt to perform an unsupported operation"));
525       break;
526     }
527   return ret;
528 }
529
530 /* Perform a relational operation on two vector operands.  */
531
532 static struct value *
533 vector_relop (struct expression *exp, struct value *val1, struct value *val2,
534               enum exp_opcode op)
535 {
536   struct value *ret;
537   struct type *type1, *type2, *eltype1, *eltype2, *rettype;
538   int t1_is_vec, t2_is_vec, i;
539   LONGEST lowb1, lowb2, highb1, highb2;
540
541   type1 = check_typedef (value_type (val1));
542   type2 = check_typedef (value_type (val2));
543
544   t1_is_vec = (type1->code () == TYPE_CODE_ARRAY && type1->is_vector ());
545   t2_is_vec = (type2->code () == TYPE_CODE_ARRAY && type2->is_vector ());
546
547   if (!t1_is_vec || !t2_is_vec)
548     error (_("Vector operations are not supported on scalar types"));
549
550   eltype1 = check_typedef (type1->target_type ());
551   eltype2 = check_typedef (type2->target_type ());
552
553   if (!get_array_bounds (type1,&lowb1, &highb1)
554       || !get_array_bounds (type2, &lowb2, &highb2))
555     error (_("Could not determine the vector bounds"));
556
557   /* Check whether the vector types are compatible.  */
558   if (eltype1->code () != eltype2->code ()
559       || TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
560       || eltype1->is_unsigned () != eltype2->is_unsigned ()
561       || lowb1 != lowb2 || highb1 != highb2)
562     error (_("Cannot perform operation on vectors with different types"));
563
564   /* Determine the resulting type of the operation and allocate the value.  */
565   rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
566                                        TYPE_LENGTH (eltype1), 0,
567                                        highb1 - lowb1 + 1);
568   ret = allocate_value (rettype);
569
570   for (i = 0; i < highb1 - lowb1 + 1; i++)
571     {
572       /* For vector types, the relational, equality and logical operators shall
573          return 0 if the specified relation is false and -1 (i.e. all bits set)
574          if the specified relation is true.  */
575       int tmp = scalar_relop (value_subscript (val1, i),
576                               value_subscript (val2, i), op) ? -1 : 0;
577       memset ((value_contents_writeable (ret).data ()
578                + i * TYPE_LENGTH (eltype1)),
579               tmp, TYPE_LENGTH (eltype1));
580      }
581
582   return ret;
583 }
584
585 /* Perform a cast of ARG into TYPE.  There's sadly a lot of duplication in
586    here from valops.c:value_cast, opencl is different only in the
587    behaviour of scalar to vector casting.  As far as possibly we're going
588    to try and delegate back to the standard value_cast function. */
589
590 struct value *
591 opencl_value_cast (struct type *type, struct value *arg)
592 {
593   if (type != value_type (arg))
594     {
595       /* Casting scalar to vector is a special case for OpenCL, scalar
596          is cast to element type of vector then replicated into each
597          element of the vector.  First though, we need to work out if
598          this is a scalar to vector cast; code lifted from
599          valops.c:value_cast.  */
600       enum type_code code1, code2;
601       struct type *to_type;
602       int scalar;
603
604       to_type = check_typedef (type);
605
606       code1 = to_type->code ();
607       code2 = check_typedef (value_type (arg))->code ();
608
609       if (code2 == TYPE_CODE_REF)
610         code2 = check_typedef (value_type (coerce_ref(arg)))->code ();
611
612       scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL
613                 || code2 == TYPE_CODE_CHAR || code2 == TYPE_CODE_FLT
614                 || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
615                 || code2 == TYPE_CODE_RANGE);
616
617       if (code1 == TYPE_CODE_ARRAY && to_type->is_vector () && scalar)
618         {
619           struct type *eltype;
620
621           /* Cast to the element type of the vector here as
622              value_vector_widen will error if the scalar value is
623              truncated by the cast.  To avoid the error, cast (and
624              possibly truncate) here.  */
625           eltype = check_typedef (to_type->target_type ());
626           arg = value_cast (eltype, arg);
627
628           return value_vector_widen (arg, type);
629         }
630       else
631         /* Standard cast handler.  */
632         arg = value_cast (type, arg);
633     }
634   return arg;
635 }
636
637 /* Perform a relational operation on two operands.  */
638
639 struct value *
640 opencl_relop (struct type *expect_type, struct expression *exp,
641               enum noside noside, enum exp_opcode op,
642               struct value *arg1, struct value *arg2)
643 {
644   struct value *val;
645   struct type *type1 = check_typedef (value_type (arg1));
646   struct type *type2 = check_typedef (value_type (arg2));
647   int t1_is_vec = (type1->code () == TYPE_CODE_ARRAY
648                    && type1->is_vector ());
649   int t2_is_vec = (type2->code () == TYPE_CODE_ARRAY
650                    && type2->is_vector ());
651
652   if (!t1_is_vec && !t2_is_vec)
653     {
654       int tmp = scalar_relop (arg1, arg2, op);
655       struct type *type =
656         language_bool_type (exp->language_defn, exp->gdbarch);
657
658       val = value_from_longest (type, tmp);
659     }
660   else if (t1_is_vec && t2_is_vec)
661     {
662       val = vector_relop (exp, arg1, arg2, op);
663     }
664   else
665     {
666       /* Widen the scalar operand to a vector.  */
667       struct value **v = t1_is_vec ? &arg2 : &arg1;
668       struct type *t = t1_is_vec ? type2 : type1;
669
670       if (t->code () != TYPE_CODE_FLT && !is_integral_type (t))
671         error (_("Argument to operation not a number or boolean."));
672
673       *v = opencl_value_cast (t1_is_vec ? type1 : type2, *v);
674       val = vector_relop (exp, arg1, arg2, op);
675     }
676
677   return val;
678 }
679
680 /* A helper function for BINOP_ASSIGN.  */
681
682 struct value *
683 eval_opencl_assign (struct type *expect_type, struct expression *exp,
684                     enum noside noside, enum exp_opcode op,
685                     struct value *arg1, struct value *arg2)
686 {
687   if (noside == EVAL_AVOID_SIDE_EFFECTS)
688     return arg1;
689
690   struct type *type1 = value_type (arg1);
691   if (deprecated_value_modifiable (arg1)
692       && VALUE_LVAL (arg1) != lval_internalvar)
693     arg2 = opencl_value_cast (type1, arg2);
694
695   return value_assign (arg1, arg2);
696 }
697
698 namespace expr
699 {
700
701 value *
702 opencl_structop_operation::evaluate (struct type *expect_type,
703                                      struct expression *exp,
704                                      enum noside noside)
705 {
706   value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
707   struct type *type1 = check_typedef (value_type (arg1));
708
709   if (type1->code () == TYPE_CODE_ARRAY && type1->is_vector ())
710     return opencl_component_ref (exp, arg1, std::get<1> (m_storage).c_str (),
711                                  noside);
712   else
713     {
714       struct value *v = value_struct_elt (&arg1, {},
715                                           std::get<1> (m_storage).c_str (),
716                                           NULL, "structure");
717
718       if (noside == EVAL_AVOID_SIDE_EFFECTS)
719         v = value_zero (value_type (v), VALUE_LVAL (v));
720       return v;
721     }
722 }
723
724 value *
725 opencl_logical_binop_operation::evaluate (struct type *expect_type,
726                                           struct expression *exp,
727                                           enum noside noside)
728 {
729   enum exp_opcode op = std::get<0> (m_storage);
730   value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
731
732   /* For scalar operations we need to avoid evaluating operands
733      unnecessarily.  However, for vector operations we always need to
734      evaluate both operands.  Unfortunately we only know which of the
735      two cases apply after we know the type of the second operand.
736      Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS.  */
737   value *arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp,
738                                                    EVAL_AVOID_SIDE_EFFECTS);
739   struct type *type1 = check_typedef (value_type (arg1));
740   struct type *type2 = check_typedef (value_type (arg2));
741
742   if ((type1->code () == TYPE_CODE_ARRAY && type1->is_vector ())
743       || (type2->code () == TYPE_CODE_ARRAY && type2->is_vector ()))
744     {
745       arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
746
747       return opencl_relop (nullptr, exp, noside, op, arg1, arg2);
748     }
749   else
750     {
751       /* For scalar built-in types, only evaluate the right
752          hand operand if the left hand operand compares
753          unequal(&&)/equal(||) to 0.  */
754       bool tmp = value_logical_not (arg1);
755
756       if (op == BINOP_LOGICAL_OR)
757         tmp = !tmp;
758
759       if (!tmp)
760         {
761           arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
762           tmp = value_logical_not (arg2);
763           if (op == BINOP_LOGICAL_OR)
764             tmp = !tmp;
765         }
766
767       type1 = language_bool_type (exp->language_defn, exp->gdbarch);
768       return value_from_longest (type1, tmp);
769     }
770 }
771
772 value *
773 opencl_ternop_cond_operation::evaluate (struct type *expect_type,
774                                         struct expression *exp,
775                                         enum noside noside)
776 {
777   value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
778   struct type *type1 = check_typedef (value_type (arg1));
779   if (type1->code () == TYPE_CODE_ARRAY && type1->is_vector ())
780     {
781       struct value *arg2, *arg3, *tmp, *ret;
782       struct type *eltype2, *type2, *type3, *eltype3;
783       int t2_is_vec, t3_is_vec, i;
784       LONGEST lowb1, lowb2, lowb3, highb1, highb2, highb3;
785
786       arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
787       arg3 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
788       type2 = check_typedef (value_type (arg2));
789       type3 = check_typedef (value_type (arg3));
790       t2_is_vec
791         = type2->code () == TYPE_CODE_ARRAY && type2->is_vector ();
792       t3_is_vec
793         = type3->code () == TYPE_CODE_ARRAY && type3->is_vector ();
794
795       /* Widen the scalar operand to a vector if necessary.  */
796       if (t2_is_vec || !t3_is_vec)
797         {
798           arg3 = opencl_value_cast (type2, arg3);
799           type3 = value_type (arg3);
800         }
801       else if (!t2_is_vec || t3_is_vec)
802         {
803           arg2 = opencl_value_cast (type3, arg2);
804           type2 = value_type (arg2);
805         }
806       else if (!t2_is_vec || !t3_is_vec)
807         {
808           /* Throw an error if arg2 or arg3 aren't vectors.  */
809           error (_("\
810 Cannot perform conditional operation on incompatible types"));
811         }
812
813       eltype2 = check_typedef (type2->target_type ());
814       eltype3 = check_typedef (type3->target_type ());
815
816       if (!get_array_bounds (type1, &lowb1, &highb1)
817           || !get_array_bounds (type2, &lowb2, &highb2)
818           || !get_array_bounds (type3, &lowb3, &highb3))
819         error (_("Could not determine the vector bounds"));
820
821       /* Throw an error if the types of arg2 or arg3 are incompatible.  */
822       if (eltype2->code () != eltype3->code ()
823           || TYPE_LENGTH (eltype2) != TYPE_LENGTH (eltype3)
824           || eltype2->is_unsigned () != eltype3->is_unsigned ()
825           || lowb2 != lowb3 || highb2 != highb3)
826         error (_("\
827 Cannot perform operation on vectors with different types"));
828
829       /* Throw an error if the sizes of arg1 and arg2/arg3 differ.  */
830       if (lowb1 != lowb2 || lowb1 != lowb3
831           || highb1 != highb2 || highb1 != highb3)
832         error (_("\
833 Cannot perform conditional operation on vectors with different sizes"));
834
835       ret = allocate_value (type2);
836
837       for (i = 0; i < highb1 - lowb1 + 1; i++)
838         {
839           tmp = value_logical_not (value_subscript (arg1, i)) ?
840             value_subscript (arg3, i) : value_subscript (arg2, i);
841           memcpy (value_contents_writeable (ret).data () +
842                   i * TYPE_LENGTH (eltype2), value_contents_all (tmp).data (),
843                   TYPE_LENGTH (eltype2));
844         }
845
846       return ret;
847     }
848   else
849     {
850       if (value_logical_not (arg1))
851         return std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
852       else
853         return std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
854     }
855 }
856
857 } /* namespace expr */
858
859 /* Class representing the OpenCL language.  */
860
861 class opencl_language : public language_defn
862 {
863 public:
864   opencl_language ()
865     : language_defn (language_opencl)
866   { /* Nothing.  */ }
867
868   /* See language.h.  */
869
870   const char *name () const override
871   { return "opencl"; }
872
873   /* See language.h.  */
874
875   const char *natural_name () const override
876   { return "OpenCL C"; }
877
878   /* See language.h.  */
879   void language_arch_info (struct gdbarch *gdbarch,
880                            struct language_arch_info *lai) const override
881   {
882     /* Helper function to allow shorter lines below.  */
883     auto add  = [&] (struct type * t) -> struct type *
884     {
885       lai->add_primitive_type (t);
886       return t;
887     };
888
889 /* Helper macro to create strings.  */
890 #define OCL_STRING(S) #S
891
892 /* This macro allocates and assigns the type struct pointers
893    for the vector types.  */
894 #define BUILD_OCL_VTYPES(TYPE, ELEMENT_TYPE)                    \
895     do                                                          \
896       {                                                         \
897         struct type *tmp;                                       \
898         tmp = add (init_vector_type (ELEMENT_TYPE, 2));         \
899         tmp->set_name (OCL_STRING(TYPE ## 2));                  \
900         tmp = add (init_vector_type (ELEMENT_TYPE, 3));         \
901         tmp->set_name (OCL_STRING(TYPE ## 3));                  \
902         tmp->set_length (4 * TYPE_LENGTH (ELEMENT_TYPE));       \
903         tmp = add (init_vector_type (ELEMENT_TYPE, 4));         \
904         tmp->set_name (OCL_STRING(TYPE ## 4));                  \
905         tmp = add (init_vector_type (ELEMENT_TYPE, 8));         \
906         tmp->set_name (OCL_STRING(TYPE ## 8));                  \
907         tmp = init_vector_type (ELEMENT_TYPE, 16);              \
908         tmp->set_name (OCL_STRING(TYPE ## 16));                 \
909       }                                                         \
910     while (false)
911
912     struct type *el_type, *char_type, *int_type;
913
914     char_type = el_type = add (arch_integer_type (gdbarch, 8, 0, "char"));
915     BUILD_OCL_VTYPES (char, el_type);
916     el_type = add (arch_integer_type (gdbarch, 8, 1, "uchar"));
917     BUILD_OCL_VTYPES (uchar, el_type);
918     el_type = add (arch_integer_type (gdbarch, 16, 0, "short"));
919     BUILD_OCL_VTYPES (short, el_type);
920     el_type = add (arch_integer_type (gdbarch, 16, 1, "ushort"));
921     BUILD_OCL_VTYPES (ushort, el_type);
922     int_type = el_type = add (arch_integer_type (gdbarch, 32, 0, "int"));
923     BUILD_OCL_VTYPES (int, el_type);
924     el_type = add (arch_integer_type (gdbarch, 32, 1, "uint"));
925     BUILD_OCL_VTYPES (uint, el_type);
926     el_type = add (arch_integer_type (gdbarch, 64, 0, "long"));
927     BUILD_OCL_VTYPES (long, el_type);
928     el_type = add (arch_integer_type (gdbarch, 64, 1, "ulong"));
929     BUILD_OCL_VTYPES (ulong, el_type);
930     el_type = add (arch_float_type (gdbarch, 16, "half", floatformats_ieee_half));
931     BUILD_OCL_VTYPES (half, el_type);
932     el_type = add (arch_float_type (gdbarch, 32, "float", floatformats_ieee_single));
933     BUILD_OCL_VTYPES (float, el_type);
934     el_type = add (arch_float_type (gdbarch, 64, "double", floatformats_ieee_double));
935     BUILD_OCL_VTYPES (double, el_type);
936
937     add (arch_boolean_type (gdbarch, 8, 1, "bool"));
938     add (arch_integer_type (gdbarch, 8, 1, "unsigned char"));
939     add (arch_integer_type (gdbarch, 16, 1, "unsigned short"));
940     add (arch_integer_type (gdbarch, 32, 1, "unsigned int"));
941     add (arch_integer_type (gdbarch, 64, 1, "unsigned long"));
942     add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "size_t"));
943     add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "ptrdiff_t"));
944     add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "intptr_t"));
945     add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr_t"));
946     add (arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void"));
947
948     /* Type of elements of strings.  */
949     lai->set_string_char_type (char_type);
950
951     /* Specifies the return type of logical and relational operations.  */
952     lai->set_bool_type (int_type, "int");
953   }
954
955   /* See language.h.  */
956
957   void print_type (struct type *type, const char *varstring,
958                    struct ui_file *stream, int show, int level,
959                    const struct type_print_options *flags) const override
960   {
961     /* We nearly always defer to C type printing, except that vector types
962        are considered primitive in OpenCL, and should always be printed
963        using their TYPE_NAME.  */
964     if (show > 0)
965       {
966         type = check_typedef (type);
967         if (type->code () == TYPE_CODE_ARRAY && type->is_vector ()
968             && type->name () != NULL)
969           show = 0;
970       }
971
972     c_print_type (type, varstring, stream, show, level, la_language, flags);
973   }
974
975   /* See language.h.  */
976
977   enum macro_expansion macro_expansion () const override
978   { return macro_expansion_c; }
979 };
980
981 /* Single instance of the OpenCL language class.  */
982
983 static opencl_language opencl_language_defn;
This page took 0.071594 seconds and 2 git commands to generate.