Update NEWS post GDB 10 branch creation.
[binutils-gdb.git] / gdb / opencl-lang.c
blobff2cfd001d16e8be915799840f818933180ad2b6
1 /* OpenCL language support for GDB, the GNU debugger.
2 Copyright (C) 2010-2020 Free Software Foundation, Inc.
4 Contributed by Ken Werner <ken.werner@de.ibm.com>.
6 This file is part of GDB.
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.
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.
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/>. */
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"
31 /* This macro generates enum values from a given type. */
33 #define OCL_P_TYPE(TYPE)\
34 opencl_primitive_type_##TYPE,\
35 opencl_primitive_type_##TYPE##2,\
36 opencl_primitive_type_##TYPE##3,\
37 opencl_primitive_type_##TYPE##4,\
38 opencl_primitive_type_##TYPE##8,\
39 opencl_primitive_type_##TYPE##16
41 enum opencl_primitive_types {
42 OCL_P_TYPE (char),
43 OCL_P_TYPE (uchar),
44 OCL_P_TYPE (short),
45 OCL_P_TYPE (ushort),
46 OCL_P_TYPE (int),
47 OCL_P_TYPE (uint),
48 OCL_P_TYPE (long),
49 OCL_P_TYPE (ulong),
50 OCL_P_TYPE (half),
51 OCL_P_TYPE (float),
52 OCL_P_TYPE (double),
53 opencl_primitive_type_bool,
54 opencl_primitive_type_unsigned_char,
55 opencl_primitive_type_unsigned_short,
56 opencl_primitive_type_unsigned_int,
57 opencl_primitive_type_unsigned_long,
58 opencl_primitive_type_size_t,
59 opencl_primitive_type_ptrdiff_t,
60 opencl_primitive_type_intptr_t,
61 opencl_primitive_type_uintptr_t,
62 opencl_primitive_type_void,
63 nr_opencl_primitive_types
66 static struct gdbarch_data *opencl_type_data;
68 static struct type **
69 builtin_opencl_type (struct gdbarch *gdbarch)
71 return (struct type **) gdbarch_data (gdbarch, opencl_type_data);
74 /* Returns the corresponding OpenCL vector type from the given type code,
75 the length of the element type, the unsigned flag and the amount of
76 elements (N). */
78 static struct type *
79 lookup_opencl_vector_type (struct gdbarch *gdbarch, enum type_code code,
80 unsigned int el_length, unsigned int flag_unsigned,
81 int n)
83 int i;
84 unsigned int length;
85 struct type *type = NULL;
86 struct type **types = builtin_opencl_type (gdbarch);
88 /* Check if n describes a valid OpenCL vector size (2, 3, 4, 8, 16). */
89 if (n != 2 && n != 3 && n != 4 && n != 8 && n != 16)
90 error (_("Invalid OpenCL vector size: %d"), n);
92 /* Triple vectors have the size of a quad vector. */
93 length = (n == 3) ? el_length * 4 : el_length * n;
95 for (i = 0; i < nr_opencl_primitive_types; i++)
97 LONGEST lowb, highb;
99 if (types[i]->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (types[i])
100 && get_array_bounds (types[i], &lowb, &highb)
101 && TYPE_TARGET_TYPE (types[i])->code () == code
102 && TYPE_UNSIGNED (TYPE_TARGET_TYPE (types[i])) == flag_unsigned
103 && TYPE_LENGTH (TYPE_TARGET_TYPE (types[i])) == el_length
104 && TYPE_LENGTH (types[i]) == length
105 && highb - lowb + 1 == n)
107 type = types[i];
108 break;
112 return type;
115 /* Returns nonzero if the array ARR contains duplicates within
116 the first N elements. */
118 static int
119 array_has_dups (int *arr, int n)
121 int i, j;
123 for (i = 0; i < n; i++)
125 for (j = i + 1; j < n; j++)
127 if (arr[i] == arr[j])
128 return 1;
132 return 0;
135 /* The OpenCL component access syntax allows to create lvalues referring to
136 selected elements of an original OpenCL vector in arbitrary order. This
137 structure holds the information to describe such lvalues. */
139 struct lval_closure
141 /* Reference count. */
142 int refc;
143 /* The number of indices. */
144 int n;
145 /* The element indices themselves. */
146 int *indices;
147 /* A pointer to the original value. */
148 struct value *val;
151 /* Allocates an instance of struct lval_closure. */
153 static struct lval_closure *
154 allocate_lval_closure (int *indices, int n, struct value *val)
156 struct lval_closure *c = XCNEW (struct lval_closure);
158 c->refc = 1;
159 c->n = n;
160 c->indices = XCNEWVEC (int, n);
161 memcpy (c->indices, indices, n * sizeof (int));
162 value_incref (val); /* Increment the reference counter of the value. */
163 c->val = val;
165 return c;
168 static void
169 lval_func_read (struct value *v)
171 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
172 struct type *type = check_typedef (value_type (v));
173 struct type *eltype = TYPE_TARGET_TYPE (check_typedef (value_type (c->val)));
174 LONGEST offset = value_offset (v);
175 LONGEST elsize = TYPE_LENGTH (eltype);
176 int n, i, j = 0;
177 LONGEST lowb = 0;
178 LONGEST highb = 0;
180 if (type->code () == TYPE_CODE_ARRAY
181 && !get_array_bounds (type, &lowb, &highb))
182 error (_("Could not determine the vector bounds"));
184 /* Assume elsize aligned offset. */
185 gdb_assert (offset % elsize == 0);
186 offset /= elsize;
187 n = offset + highb - lowb + 1;
188 gdb_assert (n <= c->n);
190 for (i = offset; i < n; i++)
191 memcpy (value_contents_raw (v) + j++ * elsize,
192 value_contents (c->val) + c->indices[i] * elsize,
193 elsize);
196 static void
197 lval_func_write (struct value *v, struct value *fromval)
199 struct value *mark = value_mark ();
200 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
201 struct type *type = check_typedef (value_type (v));
202 struct type *eltype = TYPE_TARGET_TYPE (check_typedef (value_type (c->val)));
203 LONGEST offset = value_offset (v);
204 LONGEST elsize = TYPE_LENGTH (eltype);
205 int n, i, j = 0;
206 LONGEST lowb = 0;
207 LONGEST highb = 0;
209 if (type->code () == TYPE_CODE_ARRAY
210 && !get_array_bounds (type, &lowb, &highb))
211 error (_("Could not determine the vector bounds"));
213 /* Assume elsize aligned offset. */
214 gdb_assert (offset % elsize == 0);
215 offset /= elsize;
216 n = offset + highb - lowb + 1;
218 /* Since accesses to the fourth component of a triple vector is undefined we
219 just skip writes to the fourth element. Imagine something like this:
220 int3 i3 = (int3)(0, 1, 2);
221 i3.hi.hi = 5;
222 In this case n would be 4 (offset=12/4 + 1) while c->n would be 3. */
223 if (n > c->n)
224 n = c->n;
226 for (i = offset; i < n; i++)
228 struct value *from_elm_val = allocate_value (eltype);
229 struct value *to_elm_val = value_subscript (c->val, c->indices[i]);
231 memcpy (value_contents_writeable (from_elm_val),
232 value_contents (fromval) + j++ * elsize,
233 elsize);
234 value_assign (to_elm_val, from_elm_val);
237 value_free_to_mark (mark);
240 /* Return nonzero if bits in V from OFFSET and LENGTH represent a
241 synthetic pointer. */
243 static int
244 lval_func_check_synthetic_pointer (const struct value *v,
245 LONGEST offset, int length)
247 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
248 /* Size of the target type in bits. */
249 int elsize =
250 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c->val)))) * 8;
251 int startrest = offset % elsize;
252 int start = offset / elsize;
253 int endrest = (offset + length) % elsize;
254 int end = (offset + length) / elsize;
255 int i;
257 if (endrest)
258 end++;
260 if (end > c->n)
261 return 0;
263 for (i = start; i < end; i++)
265 int comp_offset = (i == start) ? startrest : 0;
266 int comp_length = (i == end) ? endrest : elsize;
268 if (!value_bits_synthetic_pointer (c->val,
269 c->indices[i] * elsize + comp_offset,
270 comp_length))
271 return 0;
274 return 1;
277 static void *
278 lval_func_copy_closure (const struct value *v)
280 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
282 ++c->refc;
284 return c;
287 static void
288 lval_func_free_closure (struct value *v)
290 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
292 --c->refc;
294 if (c->refc == 0)
296 value_decref (c->val); /* Decrement the reference counter of the value. */
297 xfree (c->indices);
298 xfree (c);
302 static const struct lval_funcs opencl_value_funcs =
304 lval_func_read,
305 lval_func_write,
306 NULL, /* indirect */
307 NULL, /* coerce_ref */
308 lval_func_check_synthetic_pointer,
309 lval_func_copy_closure,
310 lval_func_free_closure
313 /* Creates a sub-vector from VAL. The elements are selected by the indices of
314 an array with the length of N. Supported values for NOSIDE are
315 EVAL_NORMAL and EVAL_AVOID_SIDE_EFFECTS. */
317 static struct value *
318 create_value (struct gdbarch *gdbarch, struct value *val, enum noside noside,
319 int *indices, int n)
321 struct type *type = check_typedef (value_type (val));
322 struct type *elm_type = TYPE_TARGET_TYPE (type);
323 struct value *ret;
325 /* Check if a single component of a vector is requested which means
326 the resulting type is a (primitive) scalar type. */
327 if (n == 1)
329 if (noside == EVAL_AVOID_SIDE_EFFECTS)
330 ret = value_zero (elm_type, not_lval);
331 else
332 ret = value_subscript (val, indices[0]);
334 else
336 /* Multiple components of the vector are requested which means the
337 resulting type is a vector as well. */
338 struct type *dst_type =
339 lookup_opencl_vector_type (gdbarch, elm_type->code (),
340 TYPE_LENGTH (elm_type),
341 TYPE_UNSIGNED (elm_type), n);
343 if (dst_type == NULL)
344 dst_type = init_vector_type (elm_type, n);
346 make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type), dst_type, NULL);
348 if (noside == EVAL_AVOID_SIDE_EFFECTS)
349 ret = allocate_value (dst_type);
350 else
352 /* Check whether to create a lvalue or not. */
353 if (VALUE_LVAL (val) != not_lval && !array_has_dups (indices, n))
355 struct lval_closure *c = allocate_lval_closure (indices, n, val);
356 ret = allocate_computed_value (dst_type, &opencl_value_funcs, c);
358 else
360 int i;
362 ret = allocate_value (dst_type);
364 /* Copy src val contents into the destination value. */
365 for (i = 0; i < n; i++)
366 memcpy (value_contents_writeable (ret)
367 + (i * TYPE_LENGTH (elm_type)),
368 value_contents (val)
369 + (indices[i] * TYPE_LENGTH (elm_type)),
370 TYPE_LENGTH (elm_type));
374 return ret;
377 /* OpenCL vector component access. */
379 static struct value *
380 opencl_component_ref (struct expression *exp, struct value *val, char *comps,
381 enum noside noside)
383 LONGEST lowb, highb;
384 int src_len;
385 struct value *v;
386 int indices[16], i;
387 int dst_len;
389 if (!get_array_bounds (check_typedef (value_type (val)), &lowb, &highb))
390 error (_("Could not determine the vector bounds"));
392 src_len = highb - lowb + 1;
394 /* Throw an error if the amount of array elements does not fit a
395 valid OpenCL vector size (2, 3, 4, 8, 16). */
396 if (src_len != 2 && src_len != 3 && src_len != 4 && src_len != 8
397 && src_len != 16)
398 error (_("Invalid OpenCL vector size"));
400 if (strcmp (comps, "lo") == 0 )
402 dst_len = (src_len == 3) ? 2 : src_len / 2;
404 for (i = 0; i < dst_len; i++)
405 indices[i] = i;
407 else if (strcmp (comps, "hi") == 0)
409 dst_len = (src_len == 3) ? 2 : src_len / 2;
411 for (i = 0; i < dst_len; i++)
412 indices[i] = dst_len + i;
414 else if (strcmp (comps, "even") == 0)
416 dst_len = (src_len == 3) ? 2 : src_len / 2;
418 for (i = 0; i < dst_len; i++)
419 indices[i] = i*2;
421 else if (strcmp (comps, "odd") == 0)
423 dst_len = (src_len == 3) ? 2 : src_len / 2;
425 for (i = 0; i < dst_len; i++)
426 indices[i] = i*2+1;
428 else if (strncasecmp (comps, "s", 1) == 0)
430 #define HEXCHAR_TO_INT(C) ((C >= '0' && C <= '9') ? \
431 C-'0' : ((C >= 'A' && C <= 'F') ? \
432 C-'A'+10 : ((C >= 'a' && C <= 'f') ? \
433 C-'a'+10 : -1)))
435 dst_len = strlen (comps);
436 /* Skip the s/S-prefix. */
437 dst_len--;
439 for (i = 0; i < dst_len; i++)
441 indices[i] = HEXCHAR_TO_INT(comps[i+1]);
442 /* Check if the requested component is invalid or exceeds
443 the vector. */
444 if (indices[i] < 0 || indices[i] >= src_len)
445 error (_("Invalid OpenCL vector component accessor %s"), comps);
448 else
450 dst_len = strlen (comps);
452 for (i = 0; i < dst_len; i++)
454 /* x, y, z, w */
455 switch (comps[i])
457 case 'x':
458 indices[i] = 0;
459 break;
460 case 'y':
461 indices[i] = 1;
462 break;
463 case 'z':
464 if (src_len < 3)
465 error (_("Invalid OpenCL vector component accessor %s"), comps);
466 indices[i] = 2;
467 break;
468 case 'w':
469 if (src_len < 4)
470 error (_("Invalid OpenCL vector component accessor %s"), comps);
471 indices[i] = 3;
472 break;
473 default:
474 error (_("Invalid OpenCL vector component accessor %s"), comps);
475 break;
480 /* Throw an error if the amount of requested components does not
481 result in a valid length (1, 2, 3, 4, 8, 16). */
482 if (dst_len != 1 && dst_len != 2 && dst_len != 3 && dst_len != 4
483 && dst_len != 8 && dst_len != 16)
484 error (_("Invalid OpenCL vector component accessor %s"), comps);
486 v = create_value (exp->gdbarch, val, noside, indices, dst_len);
488 return v;
491 /* Perform the unary logical not (!) operation. */
493 static struct value *
494 opencl_logical_not (struct expression *exp, struct value *arg)
496 struct type *type = check_typedef (value_type (arg));
497 struct type *rettype;
498 struct value *ret;
500 if (type->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
502 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
503 LONGEST lowb, highb;
504 int i;
506 if (!get_array_bounds (type, &lowb, &highb))
507 error (_("Could not determine the vector bounds"));
509 /* Determine the resulting type of the operation and allocate the
510 value. */
511 rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
512 TYPE_LENGTH (eltype), 0,
513 highb - lowb + 1);
514 ret = allocate_value (rettype);
516 for (i = 0; i < highb - lowb + 1; i++)
518 /* For vector types, the unary operator shall return a 0 if the
519 value of its operand compares unequal to 0, and -1 (i.e. all bits
520 set) if the value of its operand compares equal to 0. */
521 int tmp = value_logical_not (value_subscript (arg, i)) ? -1 : 0;
522 memset (value_contents_writeable (ret) + i * TYPE_LENGTH (eltype),
523 tmp, TYPE_LENGTH (eltype));
526 else
528 rettype = language_bool_type (exp->language_defn, exp->gdbarch);
529 ret = value_from_longest (rettype, value_logical_not (arg));
532 return ret;
535 /* Perform a relational operation on two scalar operands. */
537 static int
538 scalar_relop (struct value *val1, struct value *val2, enum exp_opcode op)
540 int ret;
542 switch (op)
544 case BINOP_EQUAL:
545 ret = value_equal (val1, val2);
546 break;
547 case BINOP_NOTEQUAL:
548 ret = !value_equal (val1, val2);
549 break;
550 case BINOP_LESS:
551 ret = value_less (val1, val2);
552 break;
553 case BINOP_GTR:
554 ret = value_less (val2, val1);
555 break;
556 case BINOP_GEQ:
557 ret = value_less (val2, val1) || value_equal (val1, val2);
558 break;
559 case BINOP_LEQ:
560 ret = value_less (val1, val2) || value_equal (val1, val2);
561 break;
562 case BINOP_LOGICAL_AND:
563 ret = !value_logical_not (val1) && !value_logical_not (val2);
564 break;
565 case BINOP_LOGICAL_OR:
566 ret = !value_logical_not (val1) || !value_logical_not (val2);
567 break;
568 default:
569 error (_("Attempt to perform an unsupported operation"));
570 break;
572 return ret;
575 /* Perform a relational operation on two vector operands. */
577 static struct value *
578 vector_relop (struct expression *exp, struct value *val1, struct value *val2,
579 enum exp_opcode op)
581 struct value *ret;
582 struct type *type1, *type2, *eltype1, *eltype2, *rettype;
583 int t1_is_vec, t2_is_vec, i;
584 LONGEST lowb1, lowb2, highb1, highb2;
586 type1 = check_typedef (value_type (val1));
587 type2 = check_typedef (value_type (val2));
589 t1_is_vec = (type1->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (type1));
590 t2_is_vec = (type2->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (type2));
592 if (!t1_is_vec || !t2_is_vec)
593 error (_("Vector operations are not supported on scalar types"));
595 eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
596 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
598 if (!get_array_bounds (type1,&lowb1, &highb1)
599 || !get_array_bounds (type2, &lowb2, &highb2))
600 error (_("Could not determine the vector bounds"));
602 /* Check whether the vector types are compatible. */
603 if (eltype1->code () != eltype2->code ()
604 || TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
605 || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
606 || lowb1 != lowb2 || highb1 != highb2)
607 error (_("Cannot perform operation on vectors with different types"));
609 /* Determine the resulting type of the operation and allocate the value. */
610 rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
611 TYPE_LENGTH (eltype1), 0,
612 highb1 - lowb1 + 1);
613 ret = allocate_value (rettype);
615 for (i = 0; i < highb1 - lowb1 + 1; i++)
617 /* For vector types, the relational, equality and logical operators shall
618 return 0 if the specified relation is false and -1 (i.e. all bits set)
619 if the specified relation is true. */
620 int tmp = scalar_relop (value_subscript (val1, i),
621 value_subscript (val2, i), op) ? -1 : 0;
622 memset (value_contents_writeable (ret) + i * TYPE_LENGTH (eltype1),
623 tmp, TYPE_LENGTH (eltype1));
626 return ret;
629 /* Perform a cast of ARG into TYPE. There's sadly a lot of duplication in
630 here from valops.c:value_cast, opencl is different only in the
631 behaviour of scalar to vector casting. As far as possibly we're going
632 to try and delegate back to the standard value_cast function. */
634 static struct value *
635 opencl_value_cast (struct type *type, struct value *arg)
637 if (type != value_type (arg))
639 /* Casting scalar to vector is a special case for OpenCL, scalar
640 is cast to element type of vector then replicated into each
641 element of the vector. First though, we need to work out if
642 this is a scalar to vector cast; code lifted from
643 valops.c:value_cast. */
644 enum type_code code1, code2;
645 struct type *to_type;
646 int scalar;
648 to_type = check_typedef (type);
650 code1 = to_type->code ();
651 code2 = check_typedef (value_type (arg))->code ();
653 if (code2 == TYPE_CODE_REF)
654 code2 = check_typedef (value_type (coerce_ref(arg)))->code ();
656 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL
657 || code2 == TYPE_CODE_CHAR || code2 == TYPE_CODE_FLT
658 || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
659 || code2 == TYPE_CODE_RANGE);
661 if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (to_type) && scalar)
663 struct type *eltype;
665 /* Cast to the element type of the vector here as
666 value_vector_widen will error if the scalar value is
667 truncated by the cast. To avoid the error, cast (and
668 possibly truncate) here. */
669 eltype = check_typedef (TYPE_TARGET_TYPE (to_type));
670 arg = value_cast (eltype, arg);
672 return value_vector_widen (arg, type);
674 else
675 /* Standard cast handler. */
676 arg = value_cast (type, arg);
678 return arg;
681 /* Perform a relational operation on two operands. */
683 static struct value *
684 opencl_relop (struct expression *exp, struct value *arg1, struct value *arg2,
685 enum exp_opcode op)
687 struct value *val;
688 struct type *type1 = check_typedef (value_type (arg1));
689 struct type *type2 = check_typedef (value_type (arg2));
690 int t1_is_vec = (type1->code () == TYPE_CODE_ARRAY
691 && TYPE_VECTOR (type1));
692 int t2_is_vec = (type2->code () == TYPE_CODE_ARRAY
693 && TYPE_VECTOR (type2));
695 if (!t1_is_vec && !t2_is_vec)
697 int tmp = scalar_relop (arg1, arg2, op);
698 struct type *type =
699 language_bool_type (exp->language_defn, exp->gdbarch);
701 val = value_from_longest (type, tmp);
703 else if (t1_is_vec && t2_is_vec)
705 val = vector_relop (exp, arg1, arg2, op);
707 else
709 /* Widen the scalar operand to a vector. */
710 struct value **v = t1_is_vec ? &arg2 : &arg1;
711 struct type *t = t1_is_vec ? type2 : type1;
713 if (t->code () != TYPE_CODE_FLT && !is_integral_type (t))
714 error (_("Argument to operation not a number or boolean."));
716 *v = opencl_value_cast (t1_is_vec ? type1 : type2, *v);
717 val = vector_relop (exp, arg1, arg2, op);
720 return val;
723 /* Expression evaluator for the OpenCL. Most operations are delegated to
724 evaluate_subexp_standard; see that function for a description of the
725 arguments. */
727 static struct value *
728 evaluate_subexp_opencl (struct type *expect_type, struct expression *exp,
729 int *pos, enum noside noside)
731 enum exp_opcode op = exp->elts[*pos].opcode;
732 struct value *arg1 = NULL;
733 struct value *arg2 = NULL;
734 struct type *type1, *type2;
736 switch (op)
738 /* Handle assignment and cast operators to support OpenCL-style
739 scalar-to-vector widening. */
740 case BINOP_ASSIGN:
741 (*pos)++;
742 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
743 type1 = value_type (arg1);
744 arg2 = evaluate_subexp (type1, exp, pos, noside);
746 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
747 return arg1;
749 if (deprecated_value_modifiable (arg1)
750 && VALUE_LVAL (arg1) != lval_internalvar)
751 arg2 = opencl_value_cast (type1, arg2);
753 return value_assign (arg1, arg2);
755 case UNOP_CAST:
756 type1 = exp->elts[*pos + 1].type;
757 (*pos) += 2;
758 arg1 = evaluate_subexp (type1, exp, pos, noside);
760 if (noside == EVAL_SKIP)
761 return value_from_longest (builtin_type (exp->gdbarch)->
762 builtin_int, 1);
764 return opencl_value_cast (type1, arg1);
766 case UNOP_CAST_TYPE:
767 (*pos)++;
768 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
769 type1 = value_type (arg1);
770 arg1 = evaluate_subexp (type1, exp, pos, noside);
772 if (noside == EVAL_SKIP)
773 return value_from_longest (builtin_type (exp->gdbarch)->
774 builtin_int, 1);
776 return opencl_value_cast (type1, arg1);
778 /* Handle binary relational and equality operators that are either not
779 or differently defined for GNU vectors. */
780 case BINOP_EQUAL:
781 case BINOP_NOTEQUAL:
782 case BINOP_LESS:
783 case BINOP_GTR:
784 case BINOP_GEQ:
785 case BINOP_LEQ:
786 (*pos)++;
787 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
788 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
790 if (noside == EVAL_SKIP)
791 return value_from_longest (builtin_type (exp->gdbarch)->
792 builtin_int, 1);
794 return opencl_relop (exp, arg1, arg2, op);
796 /* Handle the logical unary operator not(!). */
797 case UNOP_LOGICAL_NOT:
798 (*pos)++;
799 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
801 if (noside == EVAL_SKIP)
802 return value_from_longest (builtin_type (exp->gdbarch)->
803 builtin_int, 1);
805 return opencl_logical_not (exp, arg1);
807 /* Handle the logical operator and(&&) and or(||). */
808 case BINOP_LOGICAL_AND:
809 case BINOP_LOGICAL_OR:
810 (*pos)++;
811 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
813 if (noside == EVAL_SKIP)
815 evaluate_subexp (nullptr, exp, pos, noside);
817 return value_from_longest (builtin_type (exp->gdbarch)->
818 builtin_int, 1);
820 else
822 /* For scalar operations we need to avoid evaluating operands
823 unnecessarily. However, for vector operations we always need to
824 evaluate both operands. Unfortunately we only know which of the
825 two cases apply after we know the type of the second operand.
826 Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS. */
827 int oldpos = *pos;
829 arg2 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
830 *pos = oldpos;
831 type1 = check_typedef (value_type (arg1));
832 type2 = check_typedef (value_type (arg2));
834 if ((type1->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
835 || (type2->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (type2)))
837 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
839 return opencl_relop (exp, arg1, arg2, op);
841 else
843 /* For scalar built-in types, only evaluate the right
844 hand operand if the left hand operand compares
845 unequal(&&)/equal(||) to 0. */
846 int res;
847 int tmp = value_logical_not (arg1);
849 if (op == BINOP_LOGICAL_OR)
850 tmp = !tmp;
852 arg2
853 = evaluate_subexp (nullptr, exp, pos, tmp ? EVAL_SKIP : noside);
854 type1 = language_bool_type (exp->language_defn, exp->gdbarch);
856 if (op == BINOP_LOGICAL_AND)
857 res = !tmp && !value_logical_not (arg2);
858 else /* BINOP_LOGICAL_OR */
859 res = tmp || !value_logical_not (arg2);
861 return value_from_longest (type1, res);
865 /* Handle the ternary selection operator. */
866 case TERNOP_COND:
867 (*pos)++;
868 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
869 type1 = check_typedef (value_type (arg1));
870 if (type1->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
872 struct value *arg3, *tmp, *ret;
873 struct type *eltype2, *type3, *eltype3;
874 int t2_is_vec, t3_is_vec, i;
875 LONGEST lowb1, lowb2, lowb3, highb1, highb2, highb3;
877 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
878 arg3 = evaluate_subexp (nullptr, exp, pos, noside);
879 type2 = check_typedef (value_type (arg2));
880 type3 = check_typedef (value_type (arg3));
881 t2_is_vec
882 = type2->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (type2);
883 t3_is_vec
884 = type3->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (type3);
886 /* Widen the scalar operand to a vector if necessary. */
887 if (t2_is_vec || !t3_is_vec)
889 arg3 = opencl_value_cast (type2, arg3);
890 type3 = value_type (arg3);
892 else if (!t2_is_vec || t3_is_vec)
894 arg2 = opencl_value_cast (type3, arg2);
895 type2 = value_type (arg2);
897 else if (!t2_is_vec || !t3_is_vec)
899 /* Throw an error if arg2 or arg3 aren't vectors. */
900 error (_("\
901 Cannot perform conditional operation on incompatible types"));
904 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
905 eltype3 = check_typedef (TYPE_TARGET_TYPE (type3));
907 if (!get_array_bounds (type1, &lowb1, &highb1)
908 || !get_array_bounds (type2, &lowb2, &highb2)
909 || !get_array_bounds (type3, &lowb3, &highb3))
910 error (_("Could not determine the vector bounds"));
912 /* Throw an error if the types of arg2 or arg3 are incompatible. */
913 if (eltype2->code () != eltype3->code ()
914 || TYPE_LENGTH (eltype2) != TYPE_LENGTH (eltype3)
915 || TYPE_UNSIGNED (eltype2) != TYPE_UNSIGNED (eltype3)
916 || lowb2 != lowb3 || highb2 != highb3)
917 error (_("\
918 Cannot perform operation on vectors with different types"));
920 /* Throw an error if the sizes of arg1 and arg2/arg3 differ. */
921 if (lowb1 != lowb2 || lowb1 != lowb3
922 || highb1 != highb2 || highb1 != highb3)
923 error (_("\
924 Cannot perform conditional operation on vectors with different sizes"));
926 ret = allocate_value (type2);
928 for (i = 0; i < highb1 - lowb1 + 1; i++)
930 tmp = value_logical_not (value_subscript (arg1, i)) ?
931 value_subscript (arg3, i) : value_subscript (arg2, i);
932 memcpy (value_contents_writeable (ret) +
933 i * TYPE_LENGTH (eltype2), value_contents_all (tmp),
934 TYPE_LENGTH (eltype2));
937 return ret;
939 else
941 if (value_logical_not (arg1))
943 /* Skip the second operand. */
944 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
946 return evaluate_subexp (nullptr, exp, pos, noside);
948 else
950 /* Skip the third operand. */
951 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
952 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
954 return arg2;
958 /* Handle STRUCTOP_STRUCT to allow component access on OpenCL vectors. */
959 case STRUCTOP_STRUCT:
961 int pc = (*pos)++;
962 int tem = longest_to_int (exp->elts[pc + 1].longconst);
964 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
965 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
966 type1 = check_typedef (value_type (arg1));
968 if (noside == EVAL_SKIP)
970 return value_from_longest (builtin_type (exp->gdbarch)->
971 builtin_int, 1);
973 else if (type1->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
975 return opencl_component_ref (exp, arg1, &exp->elts[pc + 2].string,
976 noside);
978 else
980 struct value *v = value_struct_elt (&arg1, NULL,
981 &exp->elts[pc + 2].string, NULL,
982 "structure");
984 if (noside == EVAL_AVOID_SIDE_EFFECTS)
985 v = value_zero (value_type (v), VALUE_LVAL (v));
986 return v;
989 default:
990 break;
993 return evaluate_subexp_c (expect_type, exp, pos, noside);
996 const struct exp_descriptor exp_descriptor_opencl =
998 print_subexp_standard,
999 operator_length_standard,
1000 operator_check_standard,
1001 op_name_standard,
1002 dump_subexp_body_standard,
1003 evaluate_subexp_opencl
1006 /* Constant data representing the OpenCL language. */
1007 extern const struct language_data opencl_language_data =
1009 "opencl", /* Language name */
1010 "OpenCL C",
1011 language_opencl,
1012 range_check_off,
1013 case_sensitive_on,
1014 array_row_major,
1015 macro_expansion_c,
1016 NULL,
1017 &exp_descriptor_opencl,
1018 NULL, /* name_of_this */
1019 false, /* la_store_sym_names_in_linkage_form_p */
1020 c_op_print_tab, /* expression operators for printing */
1021 1, /* c-style arrays */
1022 0, /* String lower bound */
1023 &default_varobj_ops,
1024 "{...}" /* la_struct_too_deep_ellipsis */
1027 /* Class representing the OpenCL language. */
1029 class opencl_language : public language_defn
1031 public:
1032 opencl_language ()
1033 : language_defn (language_opencl, opencl_language_data)
1034 { /* Nothing. */ }
1036 /* See language.h. */
1037 void language_arch_info (struct gdbarch *gdbarch,
1038 struct language_arch_info *lai) const override
1040 struct type **types = builtin_opencl_type (gdbarch);
1042 /* Copy primitive types vector from gdbarch. */
1043 lai->primitive_type_vector = types;
1045 /* Type of elements of strings. */
1046 lai->string_char_type = types [opencl_primitive_type_char];
1048 /* Specifies the return type of logical and relational operations. */
1049 lai->bool_type_symbol = "int";
1050 lai->bool_type_default = types [opencl_primitive_type_int];
1053 /* See language.h. */
1055 void print_type (struct type *type, const char *varstring,
1056 struct ui_file *stream, int show, int level,
1057 const struct type_print_options *flags) const override
1059 /* We nearly always defer to C type printing, except that vector types
1060 are considered primitive in OpenCL, and should always be printed
1061 using their TYPE_NAME. */
1062 if (show > 0)
1064 type = check_typedef (type);
1065 if (type->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1066 && type->name () != NULL)
1067 show = 0;
1070 c_print_type (type, varstring, stream, show, level, flags);
1074 /* Single instance of the OpenCL language class. */
1076 static opencl_language opencl_language_defn;
1078 static void *
1079 build_opencl_types (struct gdbarch *gdbarch)
1081 struct type **types
1082 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_opencl_primitive_types + 1,
1083 struct type *);
1085 /* Helper macro to create strings. */
1086 #define OCL_STRING(S) #S
1087 /* This macro allocates and assigns the type struct pointers
1088 for the vector types. */
1089 #define BUILD_OCL_VTYPES(TYPE)\
1090 types[opencl_primitive_type_##TYPE##2] \
1091 = init_vector_type (types[opencl_primitive_type_##TYPE], 2); \
1092 types[opencl_primitive_type_##TYPE##2]->set_name (OCL_STRING(TYPE ## 2)); \
1093 types[opencl_primitive_type_##TYPE##3] \
1094 = init_vector_type (types[opencl_primitive_type_##TYPE], 3); \
1095 types[opencl_primitive_type_##TYPE##3]->set_name (OCL_STRING(TYPE ## 3)); \
1096 TYPE_LENGTH (types[opencl_primitive_type_##TYPE##3]) \
1097 = 4 * TYPE_LENGTH (types[opencl_primitive_type_##TYPE]); \
1098 types[opencl_primitive_type_##TYPE##4] \
1099 = init_vector_type (types[opencl_primitive_type_##TYPE], 4); \
1100 types[opencl_primitive_type_##TYPE##4]->set_name (OCL_STRING(TYPE ## 4)); \
1101 types[opencl_primitive_type_##TYPE##8] \
1102 = init_vector_type (types[opencl_primitive_type_##TYPE], 8); \
1103 types[opencl_primitive_type_##TYPE##8]->set_name (OCL_STRING(TYPE ## 8)); \
1104 types[opencl_primitive_type_##TYPE##16] \
1105 = init_vector_type (types[opencl_primitive_type_##TYPE], 16); \
1106 types[opencl_primitive_type_##TYPE##16]->set_name (OCL_STRING(TYPE ## 16))
1108 types[opencl_primitive_type_char]
1109 = arch_integer_type (gdbarch, 8, 0, "char");
1110 BUILD_OCL_VTYPES (char);
1111 types[opencl_primitive_type_uchar]
1112 = arch_integer_type (gdbarch, 8, 1, "uchar");
1113 BUILD_OCL_VTYPES (uchar);
1114 types[opencl_primitive_type_short]
1115 = arch_integer_type (gdbarch, 16, 0, "short");
1116 BUILD_OCL_VTYPES (short);
1117 types[opencl_primitive_type_ushort]
1118 = arch_integer_type (gdbarch, 16, 1, "ushort");
1119 BUILD_OCL_VTYPES (ushort);
1120 types[opencl_primitive_type_int]
1121 = arch_integer_type (gdbarch, 32, 0, "int");
1122 BUILD_OCL_VTYPES (int);
1123 types[opencl_primitive_type_uint]
1124 = arch_integer_type (gdbarch, 32, 1, "uint");
1125 BUILD_OCL_VTYPES (uint);
1126 types[opencl_primitive_type_long]
1127 = arch_integer_type (gdbarch, 64, 0, "long");
1128 BUILD_OCL_VTYPES (long);
1129 types[opencl_primitive_type_ulong]
1130 = arch_integer_type (gdbarch, 64, 1, "ulong");
1131 BUILD_OCL_VTYPES (ulong);
1132 types[opencl_primitive_type_half]
1133 = arch_float_type (gdbarch, 16, "half", floatformats_ieee_half);
1134 BUILD_OCL_VTYPES (half);
1135 types[opencl_primitive_type_float]
1136 = arch_float_type (gdbarch, 32, "float", floatformats_ieee_single);
1137 BUILD_OCL_VTYPES (float);
1138 types[opencl_primitive_type_double]
1139 = arch_float_type (gdbarch, 64, "double", floatformats_ieee_double);
1140 BUILD_OCL_VTYPES (double);
1141 types[opencl_primitive_type_bool]
1142 = arch_boolean_type (gdbarch, 8, 1, "bool");
1143 types[opencl_primitive_type_unsigned_char]
1144 = arch_integer_type (gdbarch, 8, 1, "unsigned char");
1145 types[opencl_primitive_type_unsigned_short]
1146 = arch_integer_type (gdbarch, 16, 1, "unsigned short");
1147 types[opencl_primitive_type_unsigned_int]
1148 = arch_integer_type (gdbarch, 32, 1, "unsigned int");
1149 types[opencl_primitive_type_unsigned_long]
1150 = arch_integer_type (gdbarch, 64, 1, "unsigned long");
1151 types[opencl_primitive_type_size_t]
1152 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "size_t");
1153 types[opencl_primitive_type_ptrdiff_t]
1154 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "ptrdiff_t");
1155 types[opencl_primitive_type_intptr_t]
1156 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "intptr_t");
1157 types[opencl_primitive_type_uintptr_t]
1158 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr_t");
1159 types[opencl_primitive_type_void]
1160 = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
1162 return types;
1165 void _initialize_opencl_language ();
1166 void
1167 _initialize_opencl_language ()
1169 opencl_type_data = gdbarch_data_register_post_init (build_opencl_types);