01456055015c53bbafcc0a7fb2446412f1c21260
[binutils-gdb.git] / gdb / opencl-lang.c
1 /* OpenCL language support for GDB, the GNU debugger.
2 Copyright (C) 2010-2021 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 (type)->code () == code
57 && TYPE_TARGET_TYPE (type)->is_unsigned () == flag_unsigned
58 && TYPE_LENGTH (TYPE_TARGET_TYPE (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 = TYPE_TARGET_TYPE (check_typedef (value_type (c->val)));
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) + j++ * elsize,
143 value_contents (c->val) + 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 = TYPE_TARGET_TYPE (check_typedef (value_type (c->val)));
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),
183 value_contents (fromval) + 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 (TYPE_TARGET_TYPE (check_typedef (value_type (c->val)))) * 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 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 (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 TYPE_LENGTH (elm_type),
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)
318 + (i * TYPE_LENGTH (elm_type)),
319 value_contents (val)
320 + (indices[i] * TYPE_LENGTH (elm_type)),
321 TYPE_LENGTH (elm_type));
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 static struct value *
445 opencl_logical_not (struct expression *exp, struct value *arg)
446 {
447 struct type *type = check_typedef (value_type (arg));
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 (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 TYPE_LENGTH (eltype), 0,
464 highb - lowb + 1);
465 ret = allocate_value (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 (value_contents_writeable (ret) + i * TYPE_LENGTH (eltype),
474 tmp, TYPE_LENGTH (eltype));
475 }
476 }
477 else
478 {
479 rettype = language_bool_type (exp->language_defn, exp->gdbarch);
480 ret = value_from_longest (rettype, value_logical_not (arg));
481 }
482
483 return ret;
484 }
485
486 /* Perform a relational operation on two scalar operands. */
487
488 static int
489 scalar_relop (struct value *val1, struct value *val2, enum exp_opcode op)
490 {
491 int ret;
492
493 switch (op)
494 {
495 case BINOP_EQUAL:
496 ret = value_equal (val1, val2);
497 break;
498 case BINOP_NOTEQUAL:
499 ret = !value_equal (val1, val2);
500 break;
501 case BINOP_LESS:
502 ret = value_less (val1, val2);
503 break;
504 case BINOP_GTR:
505 ret = value_less (val2, val1);
506 break;
507 case BINOP_GEQ:
508 ret = value_less (val2, val1) || value_equal (val1, val2);
509 break;
510 case BINOP_LEQ:
511 ret = value_less (val1, val2) || value_equal (val1, val2);
512 break;
513 case BINOP_LOGICAL_AND:
514 ret = !value_logical_not (val1) && !value_logical_not (val2);
515 break;
516 case BINOP_LOGICAL_OR:
517 ret = !value_logical_not (val1) || !value_logical_not (val2);
518 break;
519 default:
520 error (_("Attempt to perform an unsupported operation"));
521 break;
522 }
523 return ret;
524 }
525
526 /* Perform a relational operation on two vector operands. */
527
528 static struct value *
529 vector_relop (struct expression *exp, struct value *val1, struct value *val2,
530 enum exp_opcode op)
531 {
532 struct value *ret;
533 struct type *type1, *type2, *eltype1, *eltype2, *rettype;
534 int t1_is_vec, t2_is_vec, i;
535 LONGEST lowb1, lowb2, highb1, highb2;
536
537 type1 = check_typedef (value_type (val1));
538 type2 = check_typedef (value_type (val2));
539
540 t1_is_vec = (type1->code () == TYPE_CODE_ARRAY && type1->is_vector ());
541 t2_is_vec = (type2->code () == TYPE_CODE_ARRAY && type2->is_vector ());
542
543 if (!t1_is_vec || !t2_is_vec)
544 error (_("Vector operations are not supported on scalar types"));
545
546 eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
547 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
548
549 if (!get_array_bounds (type1,&lowb1, &highb1)
550 || !get_array_bounds (type2, &lowb2, &highb2))
551 error (_("Could not determine the vector bounds"));
552
553 /* Check whether the vector types are compatible. */
554 if (eltype1->code () != eltype2->code ()
555 || TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
556 || eltype1->is_unsigned () != eltype2->is_unsigned ()
557 || lowb1 != lowb2 || highb1 != highb2)
558 error (_("Cannot perform operation on vectors with different types"));
559
560 /* Determine the resulting type of the operation and allocate the value. */
561 rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
562 TYPE_LENGTH (eltype1), 0,
563 highb1 - lowb1 + 1);
564 ret = allocate_value (rettype);
565
566 for (i = 0; i < highb1 - lowb1 + 1; i++)
567 {
568 /* For vector types, the relational, equality and logical operators shall
569 return 0 if the specified relation is false and -1 (i.e. all bits set)
570 if the specified relation is true. */
571 int tmp = scalar_relop (value_subscript (val1, i),
572 value_subscript (val2, i), op) ? -1 : 0;
573 memset (value_contents_writeable (ret) + i * TYPE_LENGTH (eltype1),
574 tmp, TYPE_LENGTH (eltype1));
575 }
576
577 return ret;
578 }
579
580 /* Perform a cast of ARG into TYPE. There's sadly a lot of duplication in
581 here from valops.c:value_cast, opencl is different only in the
582 behaviour of scalar to vector casting. As far as possibly we're going
583 to try and delegate back to the standard value_cast function. */
584
585 struct value *
586 opencl_value_cast (struct type *type, struct value *arg)
587 {
588 if (type != value_type (arg))
589 {
590 /* Casting scalar to vector is a special case for OpenCL, scalar
591 is cast to element type of vector then replicated into each
592 element of the vector. First though, we need to work out if
593 this is a scalar to vector cast; code lifted from
594 valops.c:value_cast. */
595 enum type_code code1, code2;
596 struct type *to_type;
597 int scalar;
598
599 to_type = check_typedef (type);
600
601 code1 = to_type->code ();
602 code2 = check_typedef (value_type (arg))->code ();
603
604 if (code2 == TYPE_CODE_REF)
605 code2 = check_typedef (value_type (coerce_ref(arg)))->code ();
606
607 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL
608 || code2 == TYPE_CODE_CHAR || code2 == TYPE_CODE_FLT
609 || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
610 || code2 == TYPE_CODE_RANGE);
611
612 if (code1 == TYPE_CODE_ARRAY && to_type->is_vector () && scalar)
613 {
614 struct type *eltype;
615
616 /* Cast to the element type of the vector here as
617 value_vector_widen will error if the scalar value is
618 truncated by the cast. To avoid the error, cast (and
619 possibly truncate) here. */
620 eltype = check_typedef (TYPE_TARGET_TYPE (to_type));
621 arg = value_cast (eltype, arg);
622
623 return value_vector_widen (arg, type);
624 }
625 else
626 /* Standard cast handler. */
627 arg = value_cast (type, arg);
628 }
629 return arg;
630 }
631
632 /* Perform a relational operation on two operands. */
633
634 struct value *
635 opencl_relop (struct type *expect_type, struct expression *exp,
636 enum noside noside, enum exp_opcode op,
637 struct value *arg1, struct value *arg2)
638 {
639 struct value *val;
640 struct type *type1 = check_typedef (value_type (arg1));
641 struct type *type2 = check_typedef (value_type (arg2));
642 int t1_is_vec = (type1->code () == TYPE_CODE_ARRAY
643 && type1->is_vector ());
644 int t2_is_vec = (type2->code () == TYPE_CODE_ARRAY
645 && type2->is_vector ());
646
647 if (!t1_is_vec && !t2_is_vec)
648 {
649 int tmp = scalar_relop (arg1, arg2, op);
650 struct type *type =
651 language_bool_type (exp->language_defn, exp->gdbarch);
652
653 val = value_from_longest (type, tmp);
654 }
655 else if (t1_is_vec && t2_is_vec)
656 {
657 val = vector_relop (exp, arg1, arg2, op);
658 }
659 else
660 {
661 /* Widen the scalar operand to a vector. */
662 struct value **v = t1_is_vec ? &arg2 : &arg1;
663 struct type *t = t1_is_vec ? type2 : type1;
664
665 if (t->code () != TYPE_CODE_FLT && !is_integral_type (t))
666 error (_("Argument to operation not a number or boolean."));
667
668 *v = opencl_value_cast (t1_is_vec ? type1 : type2, *v);
669 val = vector_relop (exp, arg1, arg2, op);
670 }
671
672 return val;
673 }
674
675 /* A helper function for BINOP_ASSIGN. */
676
677 struct value *
678 eval_opencl_assign (struct type *expect_type, struct expression *exp,
679 enum noside noside, enum exp_opcode op,
680 struct value *arg1, struct value *arg2)
681 {
682 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
683 return arg1;
684
685 struct type *type1 = value_type (arg1);
686 if (deprecated_value_modifiable (arg1)
687 && VALUE_LVAL (arg1) != lval_internalvar)
688 arg2 = opencl_value_cast (type1, arg2);
689
690 return value_assign (arg1, arg2);
691 }
692
693 /* Expression evaluator for the OpenCL. Most operations are delegated to
694 evaluate_subexp_standard; see that function for a description of the
695 arguments. */
696
697 static struct value *
698 evaluate_subexp_opencl (struct type *expect_type, struct expression *exp,
699 int *pos, enum noside noside)
700 {
701 enum exp_opcode op = exp->elts[*pos].opcode;
702 struct value *arg1 = NULL;
703 struct value *arg2 = NULL;
704 struct type *type1, *type2;
705
706 switch (op)
707 {
708 /* Handle assignment and cast operators to support OpenCL-style
709 scalar-to-vector widening. */
710 case BINOP_ASSIGN:
711 (*pos)++;
712 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
713 type1 = value_type (arg1);
714 arg2 = evaluate_subexp (type1, exp, pos, noside);
715
716 return eval_opencl_assign (expect_type, exp, noside, op, arg1, arg2);
717
718 case UNOP_CAST:
719 type1 = exp->elts[*pos + 1].type;
720 (*pos) += 2;
721 arg1 = evaluate_subexp (type1, exp, pos, noside);
722
723 if (noside == EVAL_SKIP)
724 return value_from_longest (builtin_type (exp->gdbarch)->
725 builtin_int, 1);
726
727 return opencl_value_cast (type1, arg1);
728
729 case UNOP_CAST_TYPE:
730 (*pos)++;
731 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
732 type1 = value_type (arg1);
733 arg1 = evaluate_subexp (type1, exp, pos, noside);
734
735 if (noside == EVAL_SKIP)
736 return value_from_longest (builtin_type (exp->gdbarch)->
737 builtin_int, 1);
738
739 return opencl_value_cast (type1, arg1);
740
741 /* Handle binary relational and equality operators that are either not
742 or differently defined for GNU vectors. */
743 case BINOP_EQUAL:
744 case BINOP_NOTEQUAL:
745 case BINOP_LESS:
746 case BINOP_GTR:
747 case BINOP_GEQ:
748 case BINOP_LEQ:
749 (*pos)++;
750 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
751 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
752
753 if (noside == EVAL_SKIP)
754 return value_from_longest (builtin_type (exp->gdbarch)->
755 builtin_int, 1);
756
757 return opencl_relop (expect_type, exp, noside, op, arg1, arg2);
758
759 /* Handle the logical unary operator not(!). */
760 case UNOP_LOGICAL_NOT:
761 (*pos)++;
762 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
763
764 if (noside == EVAL_SKIP)
765 return value_from_longest (builtin_type (exp->gdbarch)->
766 builtin_int, 1);
767
768 return opencl_logical_not (exp, arg1);
769
770 /* Handle the logical operator and(&&) and or(||). */
771 case BINOP_LOGICAL_AND:
772 case BINOP_LOGICAL_OR:
773 (*pos)++;
774 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
775
776 if (noside == EVAL_SKIP)
777 {
778 evaluate_subexp (nullptr, exp, pos, noside);
779
780 return value_from_longest (builtin_type (exp->gdbarch)->
781 builtin_int, 1);
782 }
783 else
784 {
785 /* For scalar operations we need to avoid evaluating operands
786 unnecessarily. However, for vector operations we always need to
787 evaluate both operands. Unfortunately we only know which of the
788 two cases apply after we know the type of the second operand.
789 Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS. */
790 int oldpos = *pos;
791
792 arg2 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
793 *pos = oldpos;
794 type1 = check_typedef (value_type (arg1));
795 type2 = check_typedef (value_type (arg2));
796
797 if ((type1->code () == TYPE_CODE_ARRAY && type1->is_vector ())
798 || (type2->code () == TYPE_CODE_ARRAY && type2->is_vector ()))
799 {
800 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
801
802 return opencl_relop (nullptr, exp, noside, op, arg1, arg2);
803 }
804 else
805 {
806 /* For scalar built-in types, only evaluate the right
807 hand operand if the left hand operand compares
808 unequal(&&)/equal(||) to 0. */
809 int res;
810 int tmp = value_logical_not (arg1);
811
812 if (op == BINOP_LOGICAL_OR)
813 tmp = !tmp;
814
815 arg2
816 = evaluate_subexp (nullptr, exp, pos, tmp ? EVAL_SKIP : noside);
817 type1 = language_bool_type (exp->language_defn, exp->gdbarch);
818
819 if (op == BINOP_LOGICAL_AND)
820 res = !tmp && !value_logical_not (arg2);
821 else /* BINOP_LOGICAL_OR */
822 res = tmp || !value_logical_not (arg2);
823
824 return value_from_longest (type1, res);
825 }
826 }
827
828 /* Handle the ternary selection operator. */
829 case TERNOP_COND:
830 (*pos)++;
831 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
832 type1 = check_typedef (value_type (arg1));
833 if (type1->code () == TYPE_CODE_ARRAY && type1->is_vector ())
834 {
835 struct value *arg3, *tmp, *ret;
836 struct type *eltype2, *type3, *eltype3;
837 int t2_is_vec, t3_is_vec, i;
838 LONGEST lowb1, lowb2, lowb3, highb1, highb2, highb3;
839
840 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
841 arg3 = evaluate_subexp (nullptr, exp, pos, noside);
842 type2 = check_typedef (value_type (arg2));
843 type3 = check_typedef (value_type (arg3));
844 t2_is_vec
845 = type2->code () == TYPE_CODE_ARRAY && type2->is_vector ();
846 t3_is_vec
847 = type3->code () == TYPE_CODE_ARRAY && type3->is_vector ();
848
849 /* Widen the scalar operand to a vector if necessary. */
850 if (t2_is_vec || !t3_is_vec)
851 {
852 arg3 = opencl_value_cast (type2, arg3);
853 type3 = value_type (arg3);
854 }
855 else if (!t2_is_vec || t3_is_vec)
856 {
857 arg2 = opencl_value_cast (type3, arg2);
858 type2 = value_type (arg2);
859 }
860 else if (!t2_is_vec || !t3_is_vec)
861 {
862 /* Throw an error if arg2 or arg3 aren't vectors. */
863 error (_("\
864 Cannot perform conditional operation on incompatible types"));
865 }
866
867 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
868 eltype3 = check_typedef (TYPE_TARGET_TYPE (type3));
869
870 if (!get_array_bounds (type1, &lowb1, &highb1)
871 || !get_array_bounds (type2, &lowb2, &highb2)
872 || !get_array_bounds (type3, &lowb3, &highb3))
873 error (_("Could not determine the vector bounds"));
874
875 /* Throw an error if the types of arg2 or arg3 are incompatible. */
876 if (eltype2->code () != eltype3->code ()
877 || TYPE_LENGTH (eltype2) != TYPE_LENGTH (eltype3)
878 || eltype2->is_unsigned () != eltype3->is_unsigned ()
879 || lowb2 != lowb3 || highb2 != highb3)
880 error (_("\
881 Cannot perform operation on vectors with different types"));
882
883 /* Throw an error if the sizes of arg1 and arg2/arg3 differ. */
884 if (lowb1 != lowb2 || lowb1 != lowb3
885 || highb1 != highb2 || highb1 != highb3)
886 error (_("\
887 Cannot perform conditional operation on vectors with different sizes"));
888
889 ret = allocate_value (type2);
890
891 for (i = 0; i < highb1 - lowb1 + 1; i++)
892 {
893 tmp = value_logical_not (value_subscript (arg1, i)) ?
894 value_subscript (arg3, i) : value_subscript (arg2, i);
895 memcpy (value_contents_writeable (ret) +
896 i * TYPE_LENGTH (eltype2), value_contents_all (tmp),
897 TYPE_LENGTH (eltype2));
898 }
899
900 return ret;
901 }
902 else
903 {
904 if (value_logical_not (arg1))
905 {
906 /* Skip the second operand. */
907 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
908
909 return evaluate_subexp (nullptr, exp, pos, noside);
910 }
911 else
912 {
913 /* Skip the third operand. */
914 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
915 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
916
917 return arg2;
918 }
919 }
920
921 /* Handle STRUCTOP_STRUCT to allow component access on OpenCL vectors. */
922 case STRUCTOP_STRUCT:
923 {
924 int pc = (*pos)++;
925 int tem = longest_to_int (exp->elts[pc + 1].longconst);
926
927 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
928 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
929 type1 = check_typedef (value_type (arg1));
930
931 if (noside == EVAL_SKIP)
932 {
933 return value_from_longest (builtin_type (exp->gdbarch)->
934 builtin_int, 1);
935 }
936 else if (type1->code () == TYPE_CODE_ARRAY && type1->is_vector ())
937 {
938 return opencl_component_ref (exp, arg1, &exp->elts[pc + 2].string,
939 noside);
940 }
941 else
942 {
943 struct value *v = value_struct_elt (&arg1, NULL,
944 &exp->elts[pc + 2].string, NULL,
945 "structure");
946
947 if (noside == EVAL_AVOID_SIDE_EFFECTS)
948 v = value_zero (value_type (v), VALUE_LVAL (v));
949 return v;
950 }
951 }
952 default:
953 break;
954 }
955
956 return evaluate_subexp_c (expect_type, exp, pos, noside);
957 }
958
959 const struct exp_descriptor exp_descriptor_opencl =
960 {
961 print_subexp_standard,
962 operator_length_standard,
963 operator_check_standard,
964 dump_subexp_body_standard,
965 evaluate_subexp_opencl
966 };
967
968 /* Class representing the OpenCL language. */
969
970 class opencl_language : public language_defn
971 {
972 public:
973 opencl_language ()
974 : language_defn (language_opencl)
975 { /* Nothing. */ }
976
977 /* See language.h. */
978
979 const char *name () const override
980 { return "opencl"; }
981
982 /* See language.h. */
983
984 const char *natural_name () const override
985 { return "OpenCL C"; }
986
987 /* See language.h. */
988 void language_arch_info (struct gdbarch *gdbarch,
989 struct language_arch_info *lai) const override
990 {
991 /* Helper function to allow shorter lines below. */
992 auto add = [&] (struct type * t) -> struct type *
993 {
994 lai->add_primitive_type (t);
995 return t;
996 };
997
998 /* Helper macro to create strings. */
999 #define OCL_STRING(S) #S
1000
1001 /* This macro allocates and assigns the type struct pointers
1002 for the vector types. */
1003 #define BUILD_OCL_VTYPES(TYPE, ELEMENT_TYPE) \
1004 do \
1005 { \
1006 struct type *tmp; \
1007 tmp = add (init_vector_type (ELEMENT_TYPE, 2)); \
1008 tmp->set_name (OCL_STRING(TYPE ## 2)); \
1009 tmp = add (init_vector_type (ELEMENT_TYPE, 3)); \
1010 tmp->set_name (OCL_STRING(TYPE ## 3)); \
1011 TYPE_LENGTH (tmp) = 4 * TYPE_LENGTH (ELEMENT_TYPE); \
1012 tmp = add (init_vector_type (ELEMENT_TYPE, 4)); \
1013 tmp->set_name (OCL_STRING(TYPE ## 4)); \
1014 tmp = add (init_vector_type (ELEMENT_TYPE, 8)); \
1015 tmp->set_name (OCL_STRING(TYPE ## 8)); \
1016 tmp = init_vector_type (ELEMENT_TYPE, 16); \
1017 tmp->set_name (OCL_STRING(TYPE ## 16)); \
1018 } \
1019 while (false)
1020
1021 struct type *el_type, *char_type, *int_type;
1022
1023 char_type = el_type = add (arch_integer_type (gdbarch, 8, 0, "char"));
1024 BUILD_OCL_VTYPES (char, el_type);
1025 el_type = add (arch_integer_type (gdbarch, 8, 1, "uchar"));
1026 BUILD_OCL_VTYPES (uchar, el_type);
1027 el_type = add (arch_integer_type (gdbarch, 16, 0, "short"));
1028 BUILD_OCL_VTYPES (short, el_type);
1029 el_type = add (arch_integer_type (gdbarch, 16, 1, "ushort"));
1030 BUILD_OCL_VTYPES (ushort, el_type);
1031 int_type = el_type = add (arch_integer_type (gdbarch, 32, 0, "int"));
1032 BUILD_OCL_VTYPES (int, el_type);
1033 el_type = add (arch_integer_type (gdbarch, 32, 1, "uint"));
1034 BUILD_OCL_VTYPES (uint, el_type);
1035 el_type = add (arch_integer_type (gdbarch, 64, 0, "long"));
1036 BUILD_OCL_VTYPES (long, el_type);
1037 el_type = add (arch_integer_type (gdbarch, 64, 1, "ulong"));
1038 BUILD_OCL_VTYPES (ulong, el_type);
1039 el_type = add (arch_float_type (gdbarch, 16, "half", floatformats_ieee_half));
1040 BUILD_OCL_VTYPES (half, el_type);
1041 el_type = add (arch_float_type (gdbarch, 32, "float", floatformats_ieee_single));
1042 BUILD_OCL_VTYPES (float, el_type);
1043 el_type = add (arch_float_type (gdbarch, 64, "double", floatformats_ieee_double));
1044 BUILD_OCL_VTYPES (double, el_type);
1045
1046 add (arch_boolean_type (gdbarch, 8, 1, "bool"));
1047 add (arch_integer_type (gdbarch, 8, 1, "unsigned char"));
1048 add (arch_integer_type (gdbarch, 16, 1, "unsigned short"));
1049 add (arch_integer_type (gdbarch, 32, 1, "unsigned int"));
1050 add (arch_integer_type (gdbarch, 64, 1, "unsigned long"));
1051 add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "size_t"));
1052 add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "ptrdiff_t"));
1053 add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "intptr_t"));
1054 add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr_t"));
1055 add (arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void"));
1056
1057 /* Type of elements of strings. */
1058 lai->set_string_char_type (char_type);
1059
1060 /* Specifies the return type of logical and relational operations. */
1061 lai->set_bool_type (int_type, "int");
1062 }
1063
1064 /* See language.h. */
1065
1066 void print_type (struct type *type, const char *varstring,
1067 struct ui_file *stream, int show, int level,
1068 const struct type_print_options *flags) const override
1069 {
1070 /* We nearly always defer to C type printing, except that vector types
1071 are considered primitive in OpenCL, and should always be printed
1072 using their TYPE_NAME. */
1073 if (show > 0)
1074 {
1075 type = check_typedef (type);
1076 if (type->code () == TYPE_CODE_ARRAY && type->is_vector ()
1077 && type->name () != NULL)
1078 show = 0;
1079 }
1080
1081 c_print_type (type, varstring, stream, show, level, flags);
1082 }
1083
1084 /* See language.h. */
1085
1086 enum macro_expansion macro_expansion () const override
1087 { return macro_expansion_c; }
1088
1089 /* See language.h. */
1090
1091 const struct exp_descriptor *expression_ops () const override
1092 { return &exp_descriptor_opencl; }
1093
1094 /* See language.h. */
1095
1096 const struct op_print *opcode_print_table () const override
1097 { return c_op_print_tab; }
1098 };
1099
1100 /* Single instance of the OpenCL language class. */
1101
1102 static opencl_language opencl_language_defn;