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