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