nm: Add --quiet to suppress "no symbols" diagnostic
[binutils-gdb.git] / gdb / opencl-lang.c
blob183d67897f01ebbc87f6a8a5c304441838a1aae4
1 /* OpenCL language support for GDB, the GNU debugger.
2 Copyright (C) 2010-2021 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 /* Returns the corresponding OpenCL vector type from the given type code,
32 the length of the element type, the unsigned flag and the amount of
33 elements (N). */
35 static struct type *
36 lookup_opencl_vector_type (struct gdbarch *gdbarch, enum type_code code,
37 unsigned int el_length, unsigned int flag_unsigned,
38 int n)
40 unsigned int length;
42 /* Check if n describes a valid OpenCL vector size (2, 3, 4, 8, 16). */
43 if (n != 2 && n != 3 && n != 4 && n != 8 && n != 16)
44 error (_("Invalid OpenCL vector size: %d"), n);
46 /* Triple vectors have the size of a quad vector. */
47 length = (n == 3) ? el_length * 4 : el_length * n;
49 auto filter = [&] (struct type *type)
51 LONGEST lowb, highb;
53 return (type->code () == TYPE_CODE_ARRAY && type->is_vector ()
54 && get_array_bounds (type, &lowb, &highb)
55 && TYPE_TARGET_TYPE (type)->code () == code
56 && TYPE_TARGET_TYPE (type)->is_unsigned () == flag_unsigned
57 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == el_length
58 && TYPE_LENGTH (type) == length
59 && highb - lowb + 1 == n);
61 const struct language_defn *lang = language_def (language_opencl);
62 return language_lookup_primitive_type (lang, gdbarch, filter);
65 /* Returns nonzero if the array ARR contains duplicates within
66 the first N elements. */
68 static int
69 array_has_dups (int *arr, int n)
71 int i, j;
73 for (i = 0; i < n; i++)
75 for (j = i + 1; j < n; j++)
77 if (arr[i] == arr[j])
78 return 1;
82 return 0;
85 /* The OpenCL component access syntax allows to create lvalues referring to
86 selected elements of an original OpenCL vector in arbitrary order. This
87 structure holds the information to describe such lvalues. */
89 struct lval_closure
91 /* Reference count. */
92 int refc;
93 /* The number of indices. */
94 int n;
95 /* The element indices themselves. */
96 int *indices;
97 /* A pointer to the original value. */
98 struct value *val;
101 /* Allocates an instance of struct lval_closure. */
103 static struct lval_closure *
104 allocate_lval_closure (int *indices, int n, struct value *val)
106 struct lval_closure *c = XCNEW (struct lval_closure);
108 c->refc = 1;
109 c->n = n;
110 c->indices = XCNEWVEC (int, n);
111 memcpy (c->indices, indices, n * sizeof (int));
112 value_incref (val); /* Increment the reference counter of the value. */
113 c->val = val;
115 return c;
118 static void
119 lval_func_read (struct value *v)
121 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
122 struct type *type = check_typedef (value_type (v));
123 struct type *eltype = TYPE_TARGET_TYPE (check_typedef (value_type (c->val)));
124 LONGEST offset = value_offset (v);
125 LONGEST elsize = TYPE_LENGTH (eltype);
126 int n, i, j = 0;
127 LONGEST lowb = 0;
128 LONGEST highb = 0;
130 if (type->code () == TYPE_CODE_ARRAY
131 && !get_array_bounds (type, &lowb, &highb))
132 error (_("Could not determine the vector bounds"));
134 /* Assume elsize aligned offset. */
135 gdb_assert (offset % elsize == 0);
136 offset /= elsize;
137 n = offset + highb - lowb + 1;
138 gdb_assert (n <= c->n);
140 for (i = offset; i < n; i++)
141 memcpy (value_contents_raw (v) + j++ * elsize,
142 value_contents (c->val) + c->indices[i] * elsize,
143 elsize);
146 static void
147 lval_func_write (struct value *v, struct value *fromval)
149 struct value *mark = value_mark ();
150 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
151 struct type *type = check_typedef (value_type (v));
152 struct type *eltype = TYPE_TARGET_TYPE (check_typedef (value_type (c->val)));
153 LONGEST offset = value_offset (v);
154 LONGEST elsize = TYPE_LENGTH (eltype);
155 int n, i, j = 0;
156 LONGEST lowb = 0;
157 LONGEST highb = 0;
159 if (type->code () == TYPE_CODE_ARRAY
160 && !get_array_bounds (type, &lowb, &highb))
161 error (_("Could not determine the vector bounds"));
163 /* Assume elsize aligned offset. */
164 gdb_assert (offset % elsize == 0);
165 offset /= elsize;
166 n = offset + highb - lowb + 1;
168 /* Since accesses to the fourth component of a triple vector is undefined we
169 just skip writes to the fourth element. Imagine something like this:
170 int3 i3 = (int3)(0, 1, 2);
171 i3.hi.hi = 5;
172 In this case n would be 4 (offset=12/4 + 1) while c->n would be 3. */
173 if (n > c->n)
174 n = c->n;
176 for (i = offset; i < n; i++)
178 struct value *from_elm_val = allocate_value (eltype);
179 struct value *to_elm_val = value_subscript (c->val, c->indices[i]);
181 memcpy (value_contents_writeable (from_elm_val),
182 value_contents (fromval) + j++ * elsize,
183 elsize);
184 value_assign (to_elm_val, from_elm_val);
187 value_free_to_mark (mark);
190 /* Return nonzero if bits in V from OFFSET and LENGTH represent a
191 synthetic pointer. */
193 static int
194 lval_func_check_synthetic_pointer (const struct value *v,
195 LONGEST offset, int length)
197 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
198 /* Size of the target type in bits. */
199 int elsize =
200 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c->val)))) * 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;
207 if (endrest)
208 end++;
210 if (end > c->n)
211 return 0;
213 for (i = start; i < end; i++)
215 int comp_offset = (i == start) ? startrest : 0;
216 int comp_length = (i == end) ? endrest : elsize;
218 if (!value_bits_synthetic_pointer (c->val,
219 c->indices[i] * elsize + comp_offset,
220 comp_length))
221 return 0;
224 return 1;
227 static void *
228 lval_func_copy_closure (const struct value *v)
230 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
232 ++c->refc;
234 return c;
237 static void
238 lval_func_free_closure (struct value *v)
240 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
242 --c->refc;
244 if (c->refc == 0)
246 value_decref (c->val); /* Decrement the reference counter of the value. */
247 xfree (c->indices);
248 xfree (c);
252 static const struct lval_funcs opencl_value_funcs =
254 lval_func_read,
255 lval_func_write,
256 NULL, /* indirect */
257 NULL, /* coerce_ref */
258 lval_func_check_synthetic_pointer,
259 lval_func_copy_closure,
260 lval_func_free_closure
263 /* Creates a sub-vector from VAL. The elements are selected by the indices of
264 an array with the length of N. Supported values for NOSIDE are
265 EVAL_NORMAL and EVAL_AVOID_SIDE_EFFECTS. */
267 static struct value *
268 create_value (struct gdbarch *gdbarch, struct value *val, enum noside noside,
269 int *indices, int n)
271 struct type *type = check_typedef (value_type (val));
272 struct type *elm_type = TYPE_TARGET_TYPE (type);
273 struct value *ret;
275 /* Check if a single component of a vector is requested which means
276 the resulting type is a (primitive) scalar type. */
277 if (n == 1)
279 if (noside == EVAL_AVOID_SIDE_EFFECTS)
280 ret = value_zero (elm_type, not_lval);
281 else
282 ret = value_subscript (val, indices[0]);
284 else
286 /* Multiple components of the vector are requested which means the
287 resulting type is a vector as well. */
288 struct type *dst_type =
289 lookup_opencl_vector_type (gdbarch, elm_type->code (),
290 TYPE_LENGTH (elm_type),
291 elm_type->is_unsigned (), n);
293 if (dst_type == NULL)
294 dst_type = init_vector_type (elm_type, n);
296 make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type), dst_type, NULL);
298 if (noside == EVAL_AVOID_SIDE_EFFECTS)
299 ret = allocate_value (dst_type);
300 else
302 /* Check whether to create a lvalue or not. */
303 if (VALUE_LVAL (val) != not_lval && !array_has_dups (indices, n))
305 struct lval_closure *c = allocate_lval_closure (indices, n, val);
306 ret = allocate_computed_value (dst_type, &opencl_value_funcs, c);
308 else
310 int i;
312 ret = allocate_value (dst_type);
314 /* Copy src val contents into the destination value. */
315 for (i = 0; i < n; i++)
316 memcpy (value_contents_writeable (ret)
317 + (i * TYPE_LENGTH (elm_type)),
318 value_contents (val)
319 + (indices[i] * TYPE_LENGTH (elm_type)),
320 TYPE_LENGTH (elm_type));
324 return ret;
327 /* OpenCL vector component access. */
329 static struct value *
330 opencl_component_ref (struct expression *exp, struct value *val,
331 const char *comps, enum noside noside)
333 LONGEST lowb, highb;
334 int src_len;
335 struct value *v;
336 int indices[16], i;
337 int dst_len;
339 if (!get_array_bounds (check_typedef (value_type (val)), &lowb, &highb))
340 error (_("Could not determine the vector bounds"));
342 src_len = highb - lowb + 1;
344 /* Throw an error if the amount of array elements does not fit a
345 valid OpenCL vector size (2, 3, 4, 8, 16). */
346 if (src_len != 2 && src_len != 3 && src_len != 4 && src_len != 8
347 && src_len != 16)
348 error (_("Invalid OpenCL vector size"));
350 if (strcmp (comps, "lo") == 0 )
352 dst_len = (src_len == 3) ? 2 : src_len / 2;
354 for (i = 0; i < dst_len; i++)
355 indices[i] = i;
357 else if (strcmp (comps, "hi") == 0)
359 dst_len = (src_len == 3) ? 2 : src_len / 2;
361 for (i = 0; i < dst_len; i++)
362 indices[i] = dst_len + i;
364 else if (strcmp (comps, "even") == 0)
366 dst_len = (src_len == 3) ? 2 : src_len / 2;
368 for (i = 0; i < dst_len; i++)
369 indices[i] = i*2;
371 else if (strcmp (comps, "odd") == 0)
373 dst_len = (src_len == 3) ? 2 : src_len / 2;
375 for (i = 0; i < dst_len; i++)
376 indices[i] = i*2+1;
378 else if (strncasecmp (comps, "s", 1) == 0)
380 #define HEXCHAR_TO_INT(C) ((C >= '0' && C <= '9') ? \
381 C-'0' : ((C >= 'A' && C <= 'F') ? \
382 C-'A'+10 : ((C >= 'a' && C <= 'f') ? \
383 C-'a'+10 : -1)))
385 dst_len = strlen (comps);
386 /* Skip the s/S-prefix. */
387 dst_len--;
389 for (i = 0; i < dst_len; i++)
391 indices[i] = HEXCHAR_TO_INT(comps[i+1]);
392 /* Check if the requested component is invalid or exceeds
393 the vector. */
394 if (indices[i] < 0 || indices[i] >= src_len)
395 error (_("Invalid OpenCL vector component accessor %s"), comps);
398 else
400 dst_len = strlen (comps);
402 for (i = 0; i < dst_len; i++)
404 /* x, y, z, w */
405 switch (comps[i])
407 case 'x':
408 indices[i] = 0;
409 break;
410 case 'y':
411 indices[i] = 1;
412 break;
413 case 'z':
414 if (src_len < 3)
415 error (_("Invalid OpenCL vector component accessor %s"), comps);
416 indices[i] = 2;
417 break;
418 case 'w':
419 if (src_len < 4)
420 error (_("Invalid OpenCL vector component accessor %s"), comps);
421 indices[i] = 3;
422 break;
423 default:
424 error (_("Invalid OpenCL vector component accessor %s"), comps);
425 break;
430 /* Throw an error if the amount of requested components does not
431 result in a valid length (1, 2, 3, 4, 8, 16). */
432 if (dst_len != 1 && dst_len != 2 && dst_len != 3 && dst_len != 4
433 && dst_len != 8 && dst_len != 16)
434 error (_("Invalid OpenCL vector component accessor %s"), comps);
436 v = create_value (exp->gdbarch, val, noside, indices, dst_len);
438 return v;
441 /* Perform the unary logical not (!) operation. */
443 static struct value *
444 opencl_logical_not (struct expression *exp, struct value *arg)
446 struct type *type = check_typedef (value_type (arg));
447 struct type *rettype;
448 struct value *ret;
450 if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
452 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
453 LONGEST lowb, highb;
454 int i;
456 if (!get_array_bounds (type, &lowb, &highb))
457 error (_("Could not determine the vector bounds"));
459 /* Determine the resulting type of the operation and allocate the
460 value. */
461 rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
462 TYPE_LENGTH (eltype), 0,
463 highb - lowb + 1);
464 ret = allocate_value (rettype);
466 for (i = 0; i < highb - lowb + 1; i++)
468 /* For vector types, the unary operator shall return a 0 if the
469 value of its operand compares unequal to 0, and -1 (i.e. all bits
470 set) if the value of its operand compares equal to 0. */
471 int tmp = value_logical_not (value_subscript (arg, i)) ? -1 : 0;
472 memset (value_contents_writeable (ret) + i * TYPE_LENGTH (eltype),
473 tmp, TYPE_LENGTH (eltype));
476 else
478 rettype = language_bool_type (exp->language_defn, exp->gdbarch);
479 ret = value_from_longest (rettype, value_logical_not (arg));
482 return ret;
485 /* Perform a relational operation on two scalar operands. */
487 static int
488 scalar_relop (struct value *val1, struct value *val2, enum exp_opcode op)
490 int ret;
492 switch (op)
494 case BINOP_EQUAL:
495 ret = value_equal (val1, val2);
496 break;
497 case BINOP_NOTEQUAL:
498 ret = !value_equal (val1, val2);
499 break;
500 case BINOP_LESS:
501 ret = value_less (val1, val2);
502 break;
503 case BINOP_GTR:
504 ret = value_less (val2, val1);
505 break;
506 case BINOP_GEQ:
507 ret = value_less (val2, val1) || value_equal (val1, val2);
508 break;
509 case BINOP_LEQ:
510 ret = value_less (val1, val2) || value_equal (val1, val2);
511 break;
512 case BINOP_LOGICAL_AND:
513 ret = !value_logical_not (val1) && !value_logical_not (val2);
514 break;
515 case BINOP_LOGICAL_OR:
516 ret = !value_logical_not (val1) || !value_logical_not (val2);
517 break;
518 default:
519 error (_("Attempt to perform an unsupported operation"));
520 break;
522 return ret;
525 /* Perform a relational operation on two vector operands. */
527 static struct value *
528 vector_relop (struct expression *exp, struct value *val1, struct value *val2,
529 enum exp_opcode op)
531 struct value *ret;
532 struct type *type1, *type2, *eltype1, *eltype2, *rettype;
533 int t1_is_vec, t2_is_vec, i;
534 LONGEST lowb1, lowb2, highb1, highb2;
536 type1 = check_typedef (value_type (val1));
537 type2 = check_typedef (value_type (val2));
539 t1_is_vec = (type1->code () == TYPE_CODE_ARRAY && type1->is_vector ());
540 t2_is_vec = (type2->code () == TYPE_CODE_ARRAY && type2->is_vector ());
542 if (!t1_is_vec || !t2_is_vec)
543 error (_("Vector operations are not supported on scalar types"));
545 eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
546 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
548 if (!get_array_bounds (type1,&lowb1, &highb1)
549 || !get_array_bounds (type2, &lowb2, &highb2))
550 error (_("Could not determine the vector bounds"));
552 /* Check whether the vector types are compatible. */
553 if (eltype1->code () != eltype2->code ()
554 || TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
555 || eltype1->is_unsigned () != eltype2->is_unsigned ()
556 || lowb1 != lowb2 || highb1 != highb2)
557 error (_("Cannot perform operation on vectors with different types"));
559 /* Determine the resulting type of the operation and allocate the value. */
560 rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
561 TYPE_LENGTH (eltype1), 0,
562 highb1 - lowb1 + 1);
563 ret = allocate_value (rettype);
565 for (i = 0; i < highb1 - lowb1 + 1; i++)
567 /* For vector types, the relational, equality and logical operators shall
568 return 0 if the specified relation is false and -1 (i.e. all bits set)
569 if the specified relation is true. */
570 int tmp = scalar_relop (value_subscript (val1, i),
571 value_subscript (val2, i), op) ? -1 : 0;
572 memset (value_contents_writeable (ret) + i * TYPE_LENGTH (eltype1),
573 tmp, TYPE_LENGTH (eltype1));
576 return ret;
579 /* Perform a cast of ARG into TYPE. There's sadly a lot of duplication in
580 here from valops.c:value_cast, opencl is different only in the
581 behaviour of scalar to vector casting. As far as possibly we're going
582 to try and delegate back to the standard value_cast function. */
584 static struct value *
585 opencl_value_cast (struct type *type, struct value *arg)
587 if (type != value_type (arg))
589 /* Casting scalar to vector is a special case for OpenCL, scalar
590 is cast to element type of vector then replicated into each
591 element of the vector. First though, we need to work out if
592 this is a scalar to vector cast; code lifted from
593 valops.c:value_cast. */
594 enum type_code code1, code2;
595 struct type *to_type;
596 int scalar;
598 to_type = check_typedef (type);
600 code1 = to_type->code ();
601 code2 = check_typedef (value_type (arg))->code ();
603 if (code2 == TYPE_CODE_REF)
604 code2 = check_typedef (value_type (coerce_ref(arg)))->code ();
606 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL
607 || code2 == TYPE_CODE_CHAR || code2 == TYPE_CODE_FLT
608 || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
609 || code2 == TYPE_CODE_RANGE);
611 if (code1 == TYPE_CODE_ARRAY && to_type->is_vector () && scalar)
613 struct type *eltype;
615 /* Cast to the element type of the vector here as
616 value_vector_widen will error if the scalar value is
617 truncated by the cast. To avoid the error, cast (and
618 possibly truncate) here. */
619 eltype = check_typedef (TYPE_TARGET_TYPE (to_type));
620 arg = value_cast (eltype, arg);
622 return value_vector_widen (arg, type);
624 else
625 /* Standard cast handler. */
626 arg = value_cast (type, arg);
628 return arg;
631 /* Perform a relational operation on two operands. */
633 static struct value *
634 opencl_relop (struct expression *exp, struct value *arg1, struct value *arg2,
635 enum exp_opcode op)
637 struct value *val;
638 struct type *type1 = check_typedef (value_type (arg1));
639 struct type *type2 = check_typedef (value_type (arg2));
640 int t1_is_vec = (type1->code () == TYPE_CODE_ARRAY
641 && type1->is_vector ());
642 int t2_is_vec = (type2->code () == TYPE_CODE_ARRAY
643 && type2->is_vector ());
645 if (!t1_is_vec && !t2_is_vec)
647 int tmp = scalar_relop (arg1, arg2, op);
648 struct type *type =
649 language_bool_type (exp->language_defn, exp->gdbarch);
651 val = value_from_longest (type, tmp);
653 else if (t1_is_vec && t2_is_vec)
655 val = vector_relop (exp, arg1, arg2, op);
657 else
659 /* Widen the scalar operand to a vector. */
660 struct value **v = t1_is_vec ? &arg2 : &arg1;
661 struct type *t = t1_is_vec ? type2 : type1;
663 if (t->code () != TYPE_CODE_FLT && !is_integral_type (t))
664 error (_("Argument to operation not a number or boolean."));
666 *v = opencl_value_cast (t1_is_vec ? type1 : type2, *v);
667 val = vector_relop (exp, arg1, arg2, op);
670 return val;
673 /* Expression evaluator for the OpenCL. Most operations are delegated to
674 evaluate_subexp_standard; see that function for a description of the
675 arguments. */
677 static struct value *
678 evaluate_subexp_opencl (struct type *expect_type, struct expression *exp,
679 int *pos, enum noside noside)
681 enum exp_opcode op = exp->elts[*pos].opcode;
682 struct value *arg1 = NULL;
683 struct value *arg2 = NULL;
684 struct type *type1, *type2;
686 switch (op)
688 /* Handle assignment and cast operators to support OpenCL-style
689 scalar-to-vector widening. */
690 case BINOP_ASSIGN:
691 (*pos)++;
692 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
693 type1 = value_type (arg1);
694 arg2 = evaluate_subexp (type1, exp, pos, noside);
696 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
697 return arg1;
699 if (deprecated_value_modifiable (arg1)
700 && VALUE_LVAL (arg1) != lval_internalvar)
701 arg2 = opencl_value_cast (type1, arg2);
703 return value_assign (arg1, arg2);
705 case UNOP_CAST:
706 type1 = exp->elts[*pos + 1].type;
707 (*pos) += 2;
708 arg1 = evaluate_subexp (type1, exp, pos, noside);
710 if (noside == EVAL_SKIP)
711 return value_from_longest (builtin_type (exp->gdbarch)->
712 builtin_int, 1);
714 return opencl_value_cast (type1, arg1);
716 case UNOP_CAST_TYPE:
717 (*pos)++;
718 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
719 type1 = value_type (arg1);
720 arg1 = evaluate_subexp (type1, exp, pos, noside);
722 if (noside == EVAL_SKIP)
723 return value_from_longest (builtin_type (exp->gdbarch)->
724 builtin_int, 1);
726 return opencl_value_cast (type1, arg1);
728 /* Handle binary relational and equality operators that are either not
729 or differently defined for GNU vectors. */
730 case BINOP_EQUAL:
731 case BINOP_NOTEQUAL:
732 case BINOP_LESS:
733 case BINOP_GTR:
734 case BINOP_GEQ:
735 case BINOP_LEQ:
736 (*pos)++;
737 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
738 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
740 if (noside == EVAL_SKIP)
741 return value_from_longest (builtin_type (exp->gdbarch)->
742 builtin_int, 1);
744 return opencl_relop (exp, arg1, arg2, op);
746 /* Handle the logical unary operator not(!). */
747 case UNOP_LOGICAL_NOT:
748 (*pos)++;
749 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
751 if (noside == EVAL_SKIP)
752 return value_from_longest (builtin_type (exp->gdbarch)->
753 builtin_int, 1);
755 return opencl_logical_not (exp, arg1);
757 /* Handle the logical operator and(&&) and or(||). */
758 case BINOP_LOGICAL_AND:
759 case BINOP_LOGICAL_OR:
760 (*pos)++;
761 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
763 if (noside == EVAL_SKIP)
765 evaluate_subexp (nullptr, exp, pos, noside);
767 return value_from_longest (builtin_type (exp->gdbarch)->
768 builtin_int, 1);
770 else
772 /* For scalar operations we need to avoid evaluating operands
773 unnecessarily. However, for vector operations we always need to
774 evaluate both operands. Unfortunately we only know which of the
775 two cases apply after we know the type of the second operand.
776 Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS. */
777 int oldpos = *pos;
779 arg2 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
780 *pos = oldpos;
781 type1 = check_typedef (value_type (arg1));
782 type2 = check_typedef (value_type (arg2));
784 if ((type1->code () == TYPE_CODE_ARRAY && type1->is_vector ())
785 || (type2->code () == TYPE_CODE_ARRAY && type2->is_vector ()))
787 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
789 return opencl_relop (exp, arg1, arg2, op);
791 else
793 /* For scalar built-in types, only evaluate the right
794 hand operand if the left hand operand compares
795 unequal(&&)/equal(||) to 0. */
796 int res;
797 int tmp = value_logical_not (arg1);
799 if (op == BINOP_LOGICAL_OR)
800 tmp = !tmp;
802 arg2
803 = evaluate_subexp (nullptr, exp, pos, tmp ? EVAL_SKIP : noside);
804 type1 = language_bool_type (exp->language_defn, exp->gdbarch);
806 if (op == BINOP_LOGICAL_AND)
807 res = !tmp && !value_logical_not (arg2);
808 else /* BINOP_LOGICAL_OR */
809 res = tmp || !value_logical_not (arg2);
811 return value_from_longest (type1, res);
815 /* Handle the ternary selection operator. */
816 case TERNOP_COND:
817 (*pos)++;
818 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
819 type1 = check_typedef (value_type (arg1));
820 if (type1->code () == TYPE_CODE_ARRAY && type1->is_vector ())
822 struct value *arg3, *tmp, *ret;
823 struct type *eltype2, *type3, *eltype3;
824 int t2_is_vec, t3_is_vec, i;
825 LONGEST lowb1, lowb2, lowb3, highb1, highb2, highb3;
827 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
828 arg3 = evaluate_subexp (nullptr, exp, pos, noside);
829 type2 = check_typedef (value_type (arg2));
830 type3 = check_typedef (value_type (arg3));
831 t2_is_vec
832 = type2->code () == TYPE_CODE_ARRAY && type2->is_vector ();
833 t3_is_vec
834 = type3->code () == TYPE_CODE_ARRAY && type3->is_vector ();
836 /* Widen the scalar operand to a vector if necessary. */
837 if (t2_is_vec || !t3_is_vec)
839 arg3 = opencl_value_cast (type2, arg3);
840 type3 = value_type (arg3);
842 else if (!t2_is_vec || t3_is_vec)
844 arg2 = opencl_value_cast (type3, arg2);
845 type2 = value_type (arg2);
847 else if (!t2_is_vec || !t3_is_vec)
849 /* Throw an error if arg2 or arg3 aren't vectors. */
850 error (_("\
851 Cannot perform conditional operation on incompatible types"));
854 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
855 eltype3 = check_typedef (TYPE_TARGET_TYPE (type3));
857 if (!get_array_bounds (type1, &lowb1, &highb1)
858 || !get_array_bounds (type2, &lowb2, &highb2)
859 || !get_array_bounds (type3, &lowb3, &highb3))
860 error (_("Could not determine the vector bounds"));
862 /* Throw an error if the types of arg2 or arg3 are incompatible. */
863 if (eltype2->code () != eltype3->code ()
864 || TYPE_LENGTH (eltype2) != TYPE_LENGTH (eltype3)
865 || eltype2->is_unsigned () != eltype3->is_unsigned ()
866 || lowb2 != lowb3 || highb2 != highb3)
867 error (_("\
868 Cannot perform operation on vectors with different types"));
870 /* Throw an error if the sizes of arg1 and arg2/arg3 differ. */
871 if (lowb1 != lowb2 || lowb1 != lowb3
872 || highb1 != highb2 || highb1 != highb3)
873 error (_("\
874 Cannot perform conditional operation on vectors with different sizes"));
876 ret = allocate_value (type2);
878 for (i = 0; i < highb1 - lowb1 + 1; i++)
880 tmp = value_logical_not (value_subscript (arg1, i)) ?
881 value_subscript (arg3, i) : value_subscript (arg2, i);
882 memcpy (value_contents_writeable (ret) +
883 i * TYPE_LENGTH (eltype2), value_contents_all (tmp),
884 TYPE_LENGTH (eltype2));
887 return ret;
889 else
891 if (value_logical_not (arg1))
893 /* Skip the second operand. */
894 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
896 return evaluate_subexp (nullptr, exp, pos, noside);
898 else
900 /* Skip the third operand. */
901 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
902 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
904 return arg2;
908 /* Handle STRUCTOP_STRUCT to allow component access on OpenCL vectors. */
909 case STRUCTOP_STRUCT:
911 int pc = (*pos)++;
912 int tem = longest_to_int (exp->elts[pc + 1].longconst);
914 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
915 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
916 type1 = check_typedef (value_type (arg1));
918 if (noside == EVAL_SKIP)
920 return value_from_longest (builtin_type (exp->gdbarch)->
921 builtin_int, 1);
923 else if (type1->code () == TYPE_CODE_ARRAY && type1->is_vector ())
925 return opencl_component_ref (exp, arg1, &exp->elts[pc + 2].string,
926 noside);
928 else
930 struct value *v = value_struct_elt (&arg1, NULL,
931 &exp->elts[pc + 2].string, NULL,
932 "structure");
934 if (noside == EVAL_AVOID_SIDE_EFFECTS)
935 v = value_zero (value_type (v), VALUE_LVAL (v));
936 return v;
939 default:
940 break;
943 return evaluate_subexp_c (expect_type, exp, pos, noside);
946 const struct exp_descriptor exp_descriptor_opencl =
948 print_subexp_standard,
949 operator_length_standard,
950 operator_check_standard,
951 dump_subexp_body_standard,
952 evaluate_subexp_opencl
955 /* Class representing the OpenCL language. */
957 class opencl_language : public language_defn
959 public:
960 opencl_language ()
961 : language_defn (language_opencl)
962 { /* Nothing. */ }
964 /* See language.h. */
966 const char *name () const override
967 { return "opencl"; }
969 /* See language.h. */
971 const char *natural_name () const override
972 { return "OpenCL C"; }
974 /* See language.h. */
975 void language_arch_info (struct gdbarch *gdbarch,
976 struct language_arch_info *lai) const override
978 /* Helper function to allow shorter lines below. */
979 auto add = [&] (struct type * t) -> struct type *
981 lai->add_primitive_type (t);
982 return t;
985 /* Helper macro to create strings. */
986 #define OCL_STRING(S) #S
988 /* This macro allocates and assigns the type struct pointers
989 for the vector types. */
990 #define BUILD_OCL_VTYPES(TYPE, ELEMENT_TYPE) \
991 do \
993 struct type *tmp; \
994 tmp = add (init_vector_type (ELEMENT_TYPE, 2)); \
995 tmp->set_name (OCL_STRING(TYPE ## 2)); \
996 tmp = add (init_vector_type (ELEMENT_TYPE, 3)); \
997 tmp->set_name (OCL_STRING(TYPE ## 3)); \
998 TYPE_LENGTH (tmp) = 4 * TYPE_LENGTH (ELEMENT_TYPE); \
999 tmp = add (init_vector_type (ELEMENT_TYPE, 4)); \
1000 tmp->set_name (OCL_STRING(TYPE ## 4)); \
1001 tmp = add (init_vector_type (ELEMENT_TYPE, 8)); \
1002 tmp->set_name (OCL_STRING(TYPE ## 8)); \
1003 tmp = init_vector_type (ELEMENT_TYPE, 16); \
1004 tmp->set_name (OCL_STRING(TYPE ## 16)); \
1006 while (false)
1008 struct type *el_type, *char_type, *int_type;
1010 char_type = el_type = add (arch_integer_type (gdbarch, 8, 0, "char"));
1011 BUILD_OCL_VTYPES (char, el_type);
1012 el_type = add (arch_integer_type (gdbarch, 8, 1, "uchar"));
1013 BUILD_OCL_VTYPES (uchar, el_type);
1014 el_type = add (arch_integer_type (gdbarch, 16, 0, "short"));
1015 BUILD_OCL_VTYPES (short, el_type);
1016 el_type = add (arch_integer_type (gdbarch, 16, 1, "ushort"));
1017 BUILD_OCL_VTYPES (ushort, el_type);
1018 int_type = el_type = add (arch_integer_type (gdbarch, 32, 0, "int"));
1019 BUILD_OCL_VTYPES (int, el_type);
1020 el_type = add (arch_integer_type (gdbarch, 32, 1, "uint"));
1021 BUILD_OCL_VTYPES (uint, el_type);
1022 el_type = add (arch_integer_type (gdbarch, 64, 0, "long"));
1023 BUILD_OCL_VTYPES (long, el_type);
1024 el_type = add (arch_integer_type (gdbarch, 64, 1, "ulong"));
1025 BUILD_OCL_VTYPES (ulong, el_type);
1026 el_type = add (arch_float_type (gdbarch, 16, "half", floatformats_ieee_half));
1027 BUILD_OCL_VTYPES (half, el_type);
1028 el_type = add (arch_float_type (gdbarch, 32, "float", floatformats_ieee_single));
1029 BUILD_OCL_VTYPES (float, el_type);
1030 el_type = add (arch_float_type (gdbarch, 64, "double", floatformats_ieee_double));
1031 BUILD_OCL_VTYPES (double, el_type);
1033 add (arch_boolean_type (gdbarch, 8, 1, "bool"));
1034 add (arch_integer_type (gdbarch, 8, 1, "unsigned char"));
1035 add (arch_integer_type (gdbarch, 16, 1, "unsigned short"));
1036 add (arch_integer_type (gdbarch, 32, 1, "unsigned int"));
1037 add (arch_integer_type (gdbarch, 64, 1, "unsigned long"));
1038 add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "size_t"));
1039 add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "ptrdiff_t"));
1040 add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "intptr_t"));
1041 add (arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr_t"));
1042 add (arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void"));
1044 /* Type of elements of strings. */
1045 lai->set_string_char_type (char_type);
1047 /* Specifies the return type of logical and relational operations. */
1048 lai->set_bool_type (int_type, "int");
1051 /* See language.h. */
1053 void print_type (struct type *type, const char *varstring,
1054 struct ui_file *stream, int show, int level,
1055 const struct type_print_options *flags) const override
1057 /* We nearly always defer to C type printing, except that vector types
1058 are considered primitive in OpenCL, and should always be printed
1059 using their TYPE_NAME. */
1060 if (show > 0)
1062 type = check_typedef (type);
1063 if (type->code () == TYPE_CODE_ARRAY && type->is_vector ()
1064 && type->name () != NULL)
1065 show = 0;
1068 c_print_type (type, varstring, stream, show, level, flags);
1071 /* See language.h. */
1073 enum macro_expansion macro_expansion () const override
1074 { return macro_expansion_c; }
1076 /* See language.h. */
1078 const struct exp_descriptor *expression_ops () const override
1079 { return &exp_descriptor_opencl; }
1081 /* See language.h. */
1083 const struct op_print *opcode_print_table () const override
1084 { return c_op_print_tab; }
1087 /* Single instance of the OpenCL language class. */
1089 static opencl_language opencl_language_defn;