1 /* OpenCL language support for GDB, the GNU debugger.
2 Copyright (C) 2010-2022 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/>. */
24 #include "expression.h"
25 #include "parser-defs.h"
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
37 lookup_opencl_vector_type (struct gdbarch
*gdbarch
, enum type_code code
,
38 unsigned int el_length
, unsigned int flag_unsigned
,
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
);
47 /* Triple vectors have the size of a quad vector. */
48 length
= (n
== 3) ? el_length
* 4 : el_length
* n
;
50 auto filter
= [&] (struct type
*type
)
54 return (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ()
55 && get_array_bounds (type
, &lowb
, &highb
)
56 && type
->target_type ()->code () == code
57 && type
->target_type ()->is_unsigned () == flag_unsigned
58 && type
->target_type ()->length () == el_length
59 && type
->length () == length
60 && highb
- lowb
+ 1 == n
);
62 const struct language_defn
*lang
= language_def (language_opencl
);
63 return language_lookup_primitive_type (lang
, gdbarch
, filter
);
66 /* Returns nonzero if the array ARR contains duplicates within
67 the first N elements. */
70 array_has_dups (int *arr
, int n
)
74 for (i
= 0; i
< n
; i
++)
76 for (j
= i
+ 1; j
< n
; j
++)
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. */
92 /* Reference count. */
94 /* The number of indices. */
96 /* The element indices themselves. */
98 /* A pointer to the original value. */
102 /* Allocates an instance of struct lval_closure. */
104 static struct lval_closure
*
105 allocate_lval_closure (int *indices
, int n
, struct value
*val
)
107 struct lval_closure
*c
= XCNEW (struct lval_closure
);
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. */
120 lval_func_read (struct value
*v
)
122 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
123 struct type
*type
= check_typedef (value_type (v
));
124 struct type
*eltype
= check_typedef (value_type (c
->val
))->target_type ();
125 LONGEST offset
= value_offset (v
);
126 LONGEST elsize
= eltype
->length ();
131 if (type
->code () == TYPE_CODE_ARRAY
132 && !get_array_bounds (type
, &lowb
, &highb
))
133 error (_("Could not determine the vector bounds"));
135 /* Assume elsize aligned offset. */
136 gdb_assert (offset
% elsize
== 0);
138 n
= offset
+ highb
- lowb
+ 1;
139 gdb_assert (n
<= c
->n
);
141 for (i
= offset
; i
< n
; i
++)
142 memcpy (value_contents_raw (v
).data () + j
++ * elsize
,
143 value_contents (c
->val
).data () + c
->indices
[i
] * elsize
,
148 lval_func_write (struct value
*v
, struct value
*fromval
)
150 scoped_value_mark mark
;
152 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
153 struct type
*type
= check_typedef (value_type (v
));
154 struct type
*eltype
= check_typedef (value_type (c
->val
))->target_type ();
155 LONGEST offset
= value_offset (v
);
156 LONGEST elsize
= eltype
->length ();
161 if (type
->code () == TYPE_CODE_ARRAY
162 && !get_array_bounds (type
, &lowb
, &highb
))
163 error (_("Could not determine the vector bounds"));
165 /* Assume elsize aligned offset. */
166 gdb_assert (offset
% elsize
== 0);
168 n
= offset
+ highb
- lowb
+ 1;
170 /* Since accesses to the fourth component of a triple vector is undefined we
171 just skip writes to the fourth element. Imagine something like this:
172 int3 i3 = (int3)(0, 1, 2);
174 In this case n would be 4 (offset=12/4 + 1) while c->n would be 3. */
178 for (i
= offset
; i
< n
; i
++)
180 struct value
*from_elm_val
= allocate_value (eltype
);
181 struct value
*to_elm_val
= value_subscript (c
->val
, c
->indices
[i
]);
183 memcpy (value_contents_writeable (from_elm_val
).data (),
184 value_contents (fromval
).data () + j
++ * elsize
,
186 value_assign (to_elm_val
, from_elm_val
);
190 /* Return nonzero if bits in V from OFFSET and LENGTH represent a
191 synthetic pointer. */
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. */
200 check_typedef (value_type (c
->val
))->target_type ()->length () * 8;
201 int startrest
= offset
% elsize
;
202 int start
= offset
/ elsize
;
203 int endrest
= (offset
+ length
) % elsize
;
204 int end
= (offset
+ length
) / elsize
;
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
,
228 lval_func_copy_closure (const struct value
*v
)
230 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
238 lval_func_free_closure (struct value
*v
)
240 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
246 value_decref (c
->val
); /* Decrement the reference counter of the value. */
252 static const struct lval_funcs opencl_value_funcs
=
258 NULL
, /* coerce_ref */
259 lval_func_check_synthetic_pointer
,
260 lval_func_copy_closure
,
261 lval_func_free_closure
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. */
268 static struct value
*
269 create_value (struct gdbarch
*gdbarch
, struct value
*val
, enum noside noside
,
272 struct type
*type
= check_typedef (value_type (val
));
273 struct type
*elm_type
= type
->target_type ();
276 /* Check if a single component of a vector is requested which means
277 the resulting type is a (primitive) scalar type. */
280 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
281 ret
= value_zero (elm_type
, not_lval
);
283 ret
= value_subscript (val
, indices
[0]);
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 (),
292 elm_type
->is_unsigned (), n
);
294 if (dst_type
== NULL
)
295 dst_type
= init_vector_type (elm_type
, n
);
297 make_cv_type (TYPE_CONST (type
), TYPE_VOLATILE (type
), dst_type
, NULL
);
299 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
300 ret
= allocate_value (dst_type
);
303 /* Check whether to create a lvalue or not. */
304 if (VALUE_LVAL (val
) != not_lval
&& !array_has_dups (indices
, n
))
306 struct lval_closure
*c
= allocate_lval_closure (indices
, n
, val
);
307 ret
= allocate_computed_value (dst_type
, &opencl_value_funcs
, c
);
313 ret
= allocate_value (dst_type
);
315 /* Copy src val contents into the destination value. */
316 for (i
= 0; i
< n
; i
++)
317 memcpy (value_contents_writeable (ret
).data ()
318 + (i
* elm_type
->length ()),
319 value_contents (val
).data ()
320 + (indices
[i
] * elm_type
->length ()),
321 elm_type
->length ());
328 /* OpenCL vector component access. */
330 static struct value
*
331 opencl_component_ref (struct expression
*exp
, struct value
*val
,
332 const char *comps
, enum noside noside
)
340 if (!get_array_bounds (check_typedef (value_type (val
)), &lowb
, &highb
))
341 error (_("Could not determine the vector bounds"));
343 src_len
= highb
- lowb
+ 1;
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
349 error (_("Invalid OpenCL vector size"));
351 if (strcmp (comps
, "lo") == 0 )
353 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
355 for (i
= 0; i
< dst_len
; i
++)
358 else if (strcmp (comps
, "hi") == 0)
360 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
362 for (i
= 0; i
< dst_len
; i
++)
363 indices
[i
] = dst_len
+ i
;
365 else if (strcmp (comps
, "even") == 0)
367 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
369 for (i
= 0; i
< dst_len
; i
++)
372 else if (strcmp (comps
, "odd") == 0)
374 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
376 for (i
= 0; i
< dst_len
; i
++)
379 else if (strncasecmp (comps
, "s", 1) == 0)
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') ? \
386 dst_len
= strlen (comps
);
387 /* Skip the s/S-prefix. */
390 for (i
= 0; i
< dst_len
; i
++)
392 indices
[i
] = HEXCHAR_TO_INT(comps
[i
+1]);
393 /* Check if the requested component is invalid or exceeds
395 if (indices
[i
] < 0 || indices
[i
] >= src_len
)
396 error (_("Invalid OpenCL vector component accessor %s"), comps
);
401 dst_len
= strlen (comps
);
403 for (i
= 0; i
< dst_len
; i
++)
416 error (_("Invalid OpenCL vector component accessor %s"), comps
);
421 error (_("Invalid OpenCL vector component accessor %s"), comps
);
425 error (_("Invalid OpenCL vector component accessor %s"), comps
);
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
);
437 v
= create_value (exp
->gdbarch
, val
, noside
, indices
, dst_len
);
442 /* Perform the unary logical not (!) operation. */
445 opencl_logical_not (struct type
*expect_type
, struct expression
*exp
,
446 enum noside noside
, enum exp_opcode op
,
449 struct type
*type
= check_typedef (value_type (arg
));
450 struct type
*rettype
;
453 if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
455 struct type
*eltype
= check_typedef (type
->target_type ());
459 if (!get_array_bounds (type
, &lowb
, &highb
))
460 error (_("Could not determine the vector bounds"));
462 /* Determine the resulting type of the operation and allocate the
464 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
465 eltype
->length (), 0,
467 ret
= allocate_value (rettype
);
469 for (i
= 0; i
< highb
- lowb
+ 1; i
++)
471 /* For vector types, the unary operator shall return a 0 if the
472 value of its operand compares unequal to 0, and -1 (i.e. all bits
473 set) if the value of its operand compares equal to 0. */
474 int tmp
= value_logical_not (value_subscript (arg
, i
)) ? -1 : 0;
475 memset ((value_contents_writeable (ret
).data ()
476 + i
* eltype
->length ()),
477 tmp
, eltype
->length ());
482 rettype
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
483 ret
= value_from_longest (rettype
, value_logical_not (arg
));
489 /* Perform a relational operation on two scalar operands. */
492 scalar_relop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
499 ret
= value_equal (val1
, val2
);
502 ret
= !value_equal (val1
, val2
);
505 ret
= value_less (val1
, val2
);
508 ret
= value_less (val2
, val1
);
511 ret
= value_less (val2
, val1
) || value_equal (val1
, val2
);
514 ret
= value_less (val1
, val2
) || value_equal (val1
, val2
);
516 case BINOP_LOGICAL_AND
:
517 ret
= !value_logical_not (val1
) && !value_logical_not (val2
);
519 case BINOP_LOGICAL_OR
:
520 ret
= !value_logical_not (val1
) || !value_logical_not (val2
);
523 error (_("Attempt to perform an unsupported operation"));
529 /* Perform a relational operation on two vector operands. */
531 static struct value
*
532 vector_relop (struct expression
*exp
, struct value
*val1
, struct value
*val2
,
536 struct type
*type1
, *type2
, *eltype1
, *eltype2
, *rettype
;
537 int t1_is_vec
, t2_is_vec
, i
;
538 LONGEST lowb1
, lowb2
, highb1
, highb2
;
540 type1
= check_typedef (value_type (val1
));
541 type2
= check_typedef (value_type (val2
));
543 t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
&& type1
->is_vector ());
544 t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
&& type2
->is_vector ());
546 if (!t1_is_vec
|| !t2_is_vec
)
547 error (_("Vector operations are not supported on scalar types"));
549 eltype1
= check_typedef (type1
->target_type ());
550 eltype2
= check_typedef (type2
->target_type ());
552 if (!get_array_bounds (type1
,&lowb1
, &highb1
)
553 || !get_array_bounds (type2
, &lowb2
, &highb2
))
554 error (_("Could not determine the vector bounds"));
556 /* Check whether the vector types are compatible. */
557 if (eltype1
->code () != eltype2
->code ()
558 || eltype1
->length () != eltype2
->length ()
559 || eltype1
->is_unsigned () != eltype2
->is_unsigned ()
560 || lowb1
!= lowb2
|| highb1
!= highb2
)
561 error (_("Cannot perform operation on vectors with different types"));
563 /* Determine the resulting type of the operation and allocate the value. */
564 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
565 eltype1
->length (), 0,
567 ret
= allocate_value (rettype
);
569 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
571 /* For vector types, the relational, equality and logical operators shall
572 return 0 if the specified relation is false and -1 (i.e. all bits set)
573 if the specified relation is true. */
574 int tmp
= scalar_relop (value_subscript (val1
, i
),
575 value_subscript (val2
, i
), op
) ? -1 : 0;
576 memset ((value_contents_writeable (ret
).data ()
577 + i
* eltype1
->length ()),
578 tmp
, eltype1
->length ());
584 /* Perform a cast of ARG into TYPE. There's sadly a lot of duplication in
585 here from valops.c:value_cast, opencl is different only in the
586 behaviour of scalar to vector casting. As far as possibly we're going
587 to try and delegate back to the standard value_cast function. */
590 opencl_value_cast (struct type
*type
, struct value
*arg
)
592 if (type
!= value_type (arg
))
594 /* Casting scalar to vector is a special case for OpenCL, scalar
595 is cast to element type of vector then replicated into each
596 element of the vector. First though, we need to work out if
597 this is a scalar to vector cast; code lifted from
598 valops.c:value_cast. */
599 enum type_code code1
, code2
;
600 struct type
*to_type
;
603 to_type
= check_typedef (type
);
605 code1
= to_type
->code ();
606 code2
= check_typedef (value_type (arg
))->code ();
608 if (code2
== TYPE_CODE_REF
)
609 code2
= check_typedef (value_type (coerce_ref(arg
)))->code ();
611 scalar
= (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
612 || code2
== TYPE_CODE_CHAR
|| code2
== TYPE_CODE_FLT
613 || code2
== TYPE_CODE_DECFLOAT
|| code2
== TYPE_CODE_ENUM
614 || code2
== TYPE_CODE_RANGE
);
616 if (code1
== TYPE_CODE_ARRAY
&& to_type
->is_vector () && scalar
)
620 /* Cast to the element type of the vector here as
621 value_vector_widen will error if the scalar value is
622 truncated by the cast. To avoid the error, cast (and
623 possibly truncate) here. */
624 eltype
= check_typedef (to_type
->target_type ());
625 arg
= value_cast (eltype
, arg
);
627 return value_vector_widen (arg
, type
);
630 /* Standard cast handler. */
631 arg
= value_cast (type
, arg
);
636 /* Perform a relational operation on two operands. */
639 opencl_relop (struct type
*expect_type
, struct expression
*exp
,
640 enum noside noside
, enum exp_opcode op
,
641 struct value
*arg1
, struct value
*arg2
)
644 struct type
*type1
= check_typedef (value_type (arg1
));
645 struct type
*type2
= check_typedef (value_type (arg2
));
646 int t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
647 && type1
->is_vector ());
648 int t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
649 && type2
->is_vector ());
651 if (!t1_is_vec
&& !t2_is_vec
)
653 int tmp
= scalar_relop (arg1
, arg2
, op
);
655 language_bool_type (exp
->language_defn
, exp
->gdbarch
);
657 val
= value_from_longest (type
, tmp
);
659 else if (t1_is_vec
&& t2_is_vec
)
661 val
= vector_relop (exp
, arg1
, arg2
, op
);
665 /* Widen the scalar operand to a vector. */
666 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
667 struct type
*t
= t1_is_vec
? type2
: type1
;
669 if (t
->code () != TYPE_CODE_FLT
&& !is_integral_type (t
))
670 error (_("Argument to operation not a number or boolean."));
672 *v
= opencl_value_cast (t1_is_vec
? type1
: type2
, *v
);
673 val
= vector_relop (exp
, arg1
, arg2
, op
);
679 /* A helper function for BINOP_ASSIGN. */
682 eval_opencl_assign (struct type
*expect_type
, struct expression
*exp
,
683 enum noside noside
, enum exp_opcode op
,
684 struct value
*arg1
, struct value
*arg2
)
686 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
689 struct type
*type1
= value_type (arg1
);
690 if (deprecated_value_modifiable (arg1
)
691 && VALUE_LVAL (arg1
) != lval_internalvar
)
692 arg2
= opencl_value_cast (type1
, arg2
);
694 return value_assign (arg1
, arg2
);
701 opencl_structop_operation::evaluate (struct type
*expect_type
,
702 struct expression
*exp
,
705 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
706 struct type
*type1
= check_typedef (value_type (arg1
));
708 if (type1
->code () == TYPE_CODE_ARRAY
&& type1
->is_vector ())
709 return opencl_component_ref (exp
, arg1
, std::get
<1> (m_storage
).c_str (),
713 struct value
*v
= value_struct_elt (&arg1
, {},
714 std::get
<1> (m_storage
).c_str (),
717 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
718 v
= value_zero (value_type (v
), VALUE_LVAL (v
));
724 opencl_logical_binop_operation::evaluate (struct type
*expect_type
,
725 struct expression
*exp
,
728 enum exp_opcode op
= std::get
<0> (m_storage
);
729 value
*arg1
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
731 /* For scalar operations we need to avoid evaluating operands
732 unnecessarily. However, for vector operations we always need to
733 evaluate both operands. Unfortunately we only know which of the
734 two cases apply after we know the type of the second operand.
735 Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS. */
736 value
*arg2
= std::get
<2> (m_storage
)->evaluate (nullptr, exp
,
737 EVAL_AVOID_SIDE_EFFECTS
);
738 struct type
*type1
= check_typedef (value_type (arg1
));
739 struct type
*type2
= check_typedef (value_type (arg2
));
741 if ((type1
->code () == TYPE_CODE_ARRAY
&& type1
->is_vector ())
742 || (type2
->code () == TYPE_CODE_ARRAY
&& type2
->is_vector ()))
744 arg2
= std::get
<2> (m_storage
)->evaluate (nullptr, exp
, noside
);
746 return opencl_relop (nullptr, exp
, noside
, op
, arg1
, arg2
);
750 /* For scalar built-in types, only evaluate the right
751 hand operand if the left hand operand compares
752 unequal(&&)/equal(||) to 0. */
753 bool tmp
= value_logical_not (arg1
);
755 if (op
== BINOP_LOGICAL_OR
)
760 arg2
= std::get
<2> (m_storage
)->evaluate (nullptr, exp
, noside
);
761 tmp
= value_logical_not (arg2
);
762 if (op
== BINOP_LOGICAL_OR
)
766 type1
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
767 return value_from_longest (type1
, tmp
);
772 opencl_ternop_cond_operation::evaluate (struct type
*expect_type
,
773 struct expression
*exp
,
776 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
777 struct type
*type1
= check_typedef (value_type (arg1
));
778 if (type1
->code () == TYPE_CODE_ARRAY
&& type1
->is_vector ())
780 struct value
*arg2
, *arg3
, *tmp
, *ret
;
781 struct type
*eltype2
, *type2
, *type3
, *eltype3
;
782 int t2_is_vec
, t3_is_vec
, i
;
783 LONGEST lowb1
, lowb2
, lowb3
, highb1
, highb2
, highb3
;
785 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
786 arg3
= std::get
<2> (m_storage
)->evaluate (nullptr, exp
, noside
);
787 type2
= check_typedef (value_type (arg2
));
788 type3
= check_typedef (value_type (arg3
));
790 = type2
->code () == TYPE_CODE_ARRAY
&& type2
->is_vector ();
792 = type3
->code () == TYPE_CODE_ARRAY
&& type3
->is_vector ();
794 /* Widen the scalar operand to a vector if necessary. */
795 if (t2_is_vec
|| !t3_is_vec
)
797 arg3
= opencl_value_cast (type2
, arg3
);
798 type3
= value_type (arg3
);
800 else if (!t2_is_vec
|| t3_is_vec
)
802 arg2
= opencl_value_cast (type3
, arg2
);
803 type2
= value_type (arg2
);
805 else if (!t2_is_vec
|| !t3_is_vec
)
807 /* Throw an error if arg2 or arg3 aren't vectors. */
809 Cannot perform conditional operation on incompatible types"));
812 eltype2
= check_typedef (type2
->target_type ());
813 eltype3
= check_typedef (type3
->target_type ());
815 if (!get_array_bounds (type1
, &lowb1
, &highb1
)
816 || !get_array_bounds (type2
, &lowb2
, &highb2
)
817 || !get_array_bounds (type3
, &lowb3
, &highb3
))
818 error (_("Could not determine the vector bounds"));
820 /* Throw an error if the types of arg2 or arg3 are incompatible. */
821 if (eltype2
->code () != eltype3
->code ()
822 || eltype2
->length () != eltype3
->length ()
823 || eltype2
->is_unsigned () != eltype3
->is_unsigned ()
824 || lowb2
!= lowb3
|| highb2
!= highb3
)
826 Cannot perform operation on vectors with different types"));
828 /* Throw an error if the sizes of arg1 and arg2/arg3 differ. */
829 if (lowb1
!= lowb2
|| lowb1
!= lowb3
830 || highb1
!= highb2
|| highb1
!= highb3
)
832 Cannot perform conditional operation on vectors with different sizes"));
834 ret
= allocate_value (type2
);
836 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
838 tmp
= value_logical_not (value_subscript (arg1
, i
)) ?
839 value_subscript (arg3
, i
) : value_subscript (arg2
, i
);
840 memcpy (value_contents_writeable (ret
).data () +
841 i
* eltype2
->length (), value_contents_all (tmp
).data (),
849 if (value_logical_not (arg1
))
850 return std::get
<2> (m_storage
)->evaluate (nullptr, exp
, noside
);
852 return std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
856 } /* namespace expr */
858 /* Class representing the OpenCL language. */
860 class opencl_language
: public language_defn
864 : language_defn (language_opencl
)
867 /* See language.h. */
869 const char *name () const override
872 /* See language.h. */
874 const char *natural_name () const override
875 { return "OpenCL C"; }
877 /* See language.h. */
878 void language_arch_info (struct gdbarch
*gdbarch
,
879 struct language_arch_info
*lai
) const override
881 /* Helper function to allow shorter lines below. */
882 auto add
= [&] (struct type
* t
) -> struct type
*
884 lai
->add_primitive_type (t
);
888 /* Helper macro to create strings. */
889 #define OCL_STRING(S) #S
891 /* This macro allocates and assigns the type struct pointers
892 for the vector types. */
893 #define BUILD_OCL_VTYPES(TYPE, ELEMENT_TYPE) \
897 tmp = add (init_vector_type (ELEMENT_TYPE, 2)); \
898 tmp->set_name (OCL_STRING(TYPE ## 2)); \
899 tmp = add (init_vector_type (ELEMENT_TYPE, 3)); \
900 tmp->set_name (OCL_STRING(TYPE ## 3)); \
901 tmp->set_length (4 * (ELEMENT_TYPE)->length ()); \
902 tmp = add (init_vector_type (ELEMENT_TYPE, 4)); \
903 tmp->set_name (OCL_STRING(TYPE ## 4)); \
904 tmp = add (init_vector_type (ELEMENT_TYPE, 8)); \
905 tmp->set_name (OCL_STRING(TYPE ## 8)); \
906 tmp = init_vector_type (ELEMENT_TYPE, 16); \
907 tmp->set_name (OCL_STRING(TYPE ## 16)); \
911 struct type
*el_type
, *char_type
, *int_type
;
913 char_type
= el_type
= add (arch_integer_type (gdbarch
, 8, 0, "char"));
914 BUILD_OCL_VTYPES (char, el_type
);
915 el_type
= add (arch_integer_type (gdbarch
, 8, 1, "uchar"));
916 BUILD_OCL_VTYPES (uchar
, el_type
);
917 el_type
= add (arch_integer_type (gdbarch
, 16, 0, "short"));
918 BUILD_OCL_VTYPES (short, el_type
);
919 el_type
= add (arch_integer_type (gdbarch
, 16, 1, "ushort"));
920 BUILD_OCL_VTYPES (ushort
, el_type
);
921 int_type
= el_type
= add (arch_integer_type (gdbarch
, 32, 0, "int"));
922 BUILD_OCL_VTYPES (int, el_type
);
923 el_type
= add (arch_integer_type (gdbarch
, 32, 1, "uint"));
924 BUILD_OCL_VTYPES (uint
, el_type
);
925 el_type
= add (arch_integer_type (gdbarch
, 64, 0, "long"));
926 BUILD_OCL_VTYPES (long, el_type
);
927 el_type
= add (arch_integer_type (gdbarch
, 64, 1, "ulong"));
928 BUILD_OCL_VTYPES (ulong
, el_type
);
929 el_type
= add (arch_float_type (gdbarch
, 16, "half", floatformats_ieee_half
));
930 BUILD_OCL_VTYPES (half
, el_type
);
931 el_type
= add (arch_float_type (gdbarch
, 32, "float", floatformats_ieee_single
));
932 BUILD_OCL_VTYPES (float, el_type
);
933 el_type
= add (arch_float_type (gdbarch
, 64, "double", floatformats_ieee_double
));
934 BUILD_OCL_VTYPES (double, el_type
);
936 add (arch_boolean_type (gdbarch
, 8, 1, "bool"));
937 add (arch_integer_type (gdbarch
, 8, 1, "unsigned char"));
938 add (arch_integer_type (gdbarch
, 16, 1, "unsigned short"));
939 add (arch_integer_type (gdbarch
, 32, 1, "unsigned int"));
940 add (arch_integer_type (gdbarch
, 64, 1, "unsigned long"));
941 add (arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "size_t"));
942 add (arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "ptrdiff_t"));
943 add (arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "intptr_t"));
944 add (arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "uintptr_t"));
945 add (arch_type (gdbarch
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, "void"));
947 /* Type of elements of strings. */
948 lai
->set_string_char_type (char_type
);
950 /* Specifies the return type of logical and relational operations. */
951 lai
->set_bool_type (int_type
, "int");
954 /* See language.h. */
956 bool can_print_type_offsets () const override
961 /* See language.h. */
963 void print_type (struct type
*type
, const char *varstring
,
964 struct ui_file
*stream
, int show
, int level
,
965 const struct type_print_options
*flags
) const override
967 /* We nearly always defer to C type printing, except that vector types
968 are considered primitive in OpenCL, and should always be printed
969 using their TYPE_NAME. */
972 type
= check_typedef (type
);
973 if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ()
974 && type
->name () != NULL
)
978 c_print_type (type
, varstring
, stream
, show
, level
, la_language
, flags
);
981 /* See language.h. */
983 enum macro_expansion
macro_expansion () const override
984 { return macro_expansion_c
; }
987 /* Single instance of the OpenCL language class. */
989 static opencl_language opencl_language_defn
;