1 /* OpenCL language support for GDB, the GNU debugger.
2 Copyright (C) 2010-2024 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/>. */
23 #include "expression.h"
24 #include "parser-defs.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
36 lookup_opencl_vector_type (struct gdbarch
*gdbarch
, enum type_code code
,
37 unsigned int el_length
, unsigned int flag_unsigned
,
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
)
53 return (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ()
54 && get_array_bounds (type
, &lowb
, &highb
)
55 && type
->target_type ()->code () == code
56 && type
->target_type ()->is_unsigned () == flag_unsigned
57 && type
->target_type ()->length () == el_length
58 && type
->length () == 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. */
69 array_has_dups (int *arr
, int n
)
73 for (i
= 0; i
< n
; i
++)
75 for (j
= i
+ 1; j
< n
; j
++)
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. */
91 /* Reference count. */
93 /* The number of indices. */
95 /* The element indices themselves. */
97 /* A pointer to the original value. */
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
);
110 c
->indices
= XCNEWVEC (int, n
);
111 memcpy (c
->indices
, indices
, n
* sizeof (int));
112 val
->incref (); /* Increment the reference counter of the value. */
119 lval_func_read (struct value
*v
)
121 struct lval_closure
*c
= (struct lval_closure
*) v
->computed_closure ();
122 struct type
*type
= check_typedef (v
->type ());
123 struct type
*eltype
= check_typedef (c
->val
->type ())->target_type ();
124 LONGEST offset
= v
->offset ();
125 LONGEST elsize
= eltype
->length ();
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);
137 n
= offset
+ highb
- lowb
+ 1;
138 gdb_assert (n
<= c
->n
);
140 for (i
= offset
; i
< n
; i
++)
141 memcpy (v
->contents_raw ().data () + j
++ * elsize
,
142 c
->val
->contents ().data () + c
->indices
[i
] * elsize
,
147 lval_func_write (struct value
*v
, struct value
*fromval
)
149 scoped_value_mark mark
;
151 struct lval_closure
*c
= (struct lval_closure
*) v
->computed_closure ();
152 struct type
*type
= check_typedef (v
->type ());
153 struct type
*eltype
= check_typedef (c
->val
->type ())->target_type ();
154 LONGEST offset
= v
->offset ();
155 LONGEST elsize
= eltype
->length ();
160 if (type
->code () == TYPE_CODE_ARRAY
161 && !get_array_bounds (type
, &lowb
, &highb
))
162 error (_("Could not determine the vector bounds"));
164 /* Assume elsize aligned offset. */
165 gdb_assert (offset
% elsize
== 0);
167 n
= offset
+ highb
- lowb
+ 1;
169 /* Since accesses to the fourth component of a triple vector is undefined we
170 just skip writes to the fourth element. Imagine something like this:
171 int3 i3 = (int3)(0, 1, 2);
173 In this case n would be 4 (offset=12/4 + 1) while c->n would be 3. */
177 for (i
= offset
; i
< n
; i
++)
179 struct value
*from_elm_val
= value::allocate (eltype
);
180 struct value
*to_elm_val
= value_subscript (c
->val
, c
->indices
[i
]);
182 memcpy (from_elm_val
->contents_writeable ().data (),
183 fromval
->contents ().data () + j
++ * elsize
,
185 value_assign (to_elm_val
, from_elm_val
);
189 /* Return true if bits in V from OFFSET and LENGTH represent a
190 synthetic pointer. */
193 lval_func_check_synthetic_pointer (const struct value
*v
,
194 LONGEST offset
, int length
)
196 struct lval_closure
*c
= (struct lval_closure
*) v
->computed_closure ();
197 /* Size of the target type in bits. */
199 check_typedef (c
->val
->type ())->target_type ()->length () * 8;
200 int startrest
= offset
% elsize
;
201 int start
= offset
/ elsize
;
202 int endrest
= (offset
+ length
) % elsize
;
203 int end
= (offset
+ length
) / elsize
;
212 for (i
= start
; i
< end
; i
++)
214 int comp_offset
= (i
== start
) ? startrest
: 0;
215 int comp_length
= (i
== end
) ? endrest
: elsize
;
217 if (!c
->val
->bits_synthetic_pointer (c
->indices
[i
] * elsize
+ comp_offset
,
226 lval_func_copy_closure (const struct value
*v
)
228 struct lval_closure
*c
= (struct lval_closure
*) v
->computed_closure ();
236 lval_func_free_closure (struct value
*v
)
238 struct lval_closure
*c
= (struct lval_closure
*) v
->computed_closure ();
244 c
->val
->decref (); /* Decrement the reference counter of the value. */
250 static const struct lval_funcs opencl_value_funcs
=
256 NULL
, /* coerce_ref */
257 lval_func_check_synthetic_pointer
,
258 lval_func_copy_closure
,
259 lval_func_free_closure
262 /* Creates a sub-vector from VAL. The elements are selected by the indices of
263 an array with the length of N. Supported values for NOSIDE are
264 EVAL_NORMAL and EVAL_AVOID_SIDE_EFFECTS. */
266 static struct value
*
267 create_value (struct gdbarch
*gdbarch
, struct value
*val
, enum noside noside
,
270 struct type
*type
= check_typedef (val
->type ());
271 struct type
*elm_type
= type
->target_type ();
274 /* Check if a single component of a vector is requested which means
275 the resulting type is a (primitive) scalar type. */
278 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
279 ret
= value::zero (elm_type
, not_lval
);
281 ret
= value_subscript (val
, indices
[0]);
285 /* Multiple components of the vector are requested which means the
286 resulting type is a vector as well. */
287 struct type
*dst_type
=
288 lookup_opencl_vector_type (gdbarch
, elm_type
->code (),
290 elm_type
->is_unsigned (), n
);
292 if (dst_type
== NULL
)
293 dst_type
= init_vector_type (elm_type
, n
);
295 make_cv_type (TYPE_CONST (type
), TYPE_VOLATILE (type
), dst_type
, NULL
);
297 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
298 ret
= value::allocate (dst_type
);
301 /* Check whether to create a lvalue or not. */
302 if (val
->lval () != not_lval
&& !array_has_dups (indices
, n
))
304 struct lval_closure
*c
= allocate_lval_closure (indices
, n
, val
);
305 ret
= value::allocate_computed (dst_type
, &opencl_value_funcs
, c
);
311 ret
= value::allocate (dst_type
);
313 /* Copy src val contents into the destination value. */
314 for (i
= 0; i
< n
; i
++)
315 memcpy (ret
->contents_writeable ().data ()
316 + (i
* elm_type
->length ()),
317 val
->contents ().data ()
318 + (indices
[i
] * elm_type
->length ()),
319 elm_type
->length ());
326 /* OpenCL vector component access. */
328 static struct value
*
329 opencl_component_ref (struct expression
*exp
, struct value
*val
,
330 const char *comps
, enum noside noside
)
338 if (!get_array_bounds (check_typedef (val
->type ()), &lowb
, &highb
))
339 error (_("Could not determine the vector bounds"));
341 src_len
= highb
- lowb
+ 1;
343 /* Throw an error if the amount of array elements does not fit a
344 valid OpenCL vector size (2, 3, 4, 8, 16). */
345 if (src_len
!= 2 && src_len
!= 3 && src_len
!= 4 && src_len
!= 8
347 error (_("Invalid OpenCL vector size"));
349 if (strcmp (comps
, "lo") == 0 )
351 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
353 for (i
= 0; i
< dst_len
; i
++)
356 else if (strcmp (comps
, "hi") == 0)
358 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
360 for (i
= 0; i
< dst_len
; i
++)
361 indices
[i
] = dst_len
+ i
;
363 else if (strcmp (comps
, "even") == 0)
365 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
367 for (i
= 0; i
< dst_len
; i
++)
370 else if (strcmp (comps
, "odd") == 0)
372 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
374 for (i
= 0; i
< dst_len
; i
++)
377 else if (strncasecmp (comps
, "s", 1) == 0)
379 #define HEXCHAR_TO_INT(C) ((C >= '0' && C <= '9') ? \
380 C-'0' : ((C >= 'A' && C <= 'F') ? \
381 C-'A'+10 : ((C >= 'a' && C <= 'f') ? \
384 dst_len
= strlen (comps
);
385 /* Skip the s/S-prefix. */
388 for (i
= 0; i
< dst_len
; i
++)
390 indices
[i
] = HEXCHAR_TO_INT(comps
[i
+1]);
391 /* Check if the requested component is invalid or exceeds
393 if (indices
[i
] < 0 || indices
[i
] >= src_len
)
394 error (_("Invalid OpenCL vector component accessor %s"), comps
);
399 dst_len
= strlen (comps
);
401 for (i
= 0; i
< dst_len
; i
++)
414 error (_("Invalid OpenCL vector component accessor %s"), comps
);
419 error (_("Invalid OpenCL vector component accessor %s"), comps
);
423 error (_("Invalid OpenCL vector component accessor %s"), comps
);
429 /* Throw an error if the amount of requested components does not
430 result in a valid length (1, 2, 3, 4, 8, 16). */
431 if (dst_len
!= 1 && dst_len
!= 2 && dst_len
!= 3 && dst_len
!= 4
432 && dst_len
!= 8 && dst_len
!= 16)
433 error (_("Invalid OpenCL vector component accessor %s"), comps
);
435 v
= create_value (exp
->gdbarch
, val
, noside
, indices
, dst_len
);
440 /* Perform the unary logical not (!) operation. */
443 opencl_logical_not (struct type
*expect_type
, struct expression
*exp
,
444 enum noside noside
, enum exp_opcode op
,
447 struct type
*type
= check_typedef (arg
->type ());
448 struct type
*rettype
;
451 if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
453 struct type
*eltype
= check_typedef (type
->target_type ());
457 if (!get_array_bounds (type
, &lowb
, &highb
))
458 error (_("Could not determine the vector bounds"));
460 /* Determine the resulting type of the operation and allocate the
462 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
463 eltype
->length (), 0,
465 ret
= value::allocate (rettype
);
467 for (i
= 0; i
< highb
- lowb
+ 1; i
++)
469 /* For vector types, the unary operator shall return a 0 if the
470 value of its operand compares unequal to 0, and -1 (i.e. all bits
471 set) if the value of its operand compares equal to 0. */
472 int tmp
= value_logical_not (value_subscript (arg
, i
)) ? -1 : 0;
473 memset ((ret
->contents_writeable ().data ()
474 + i
* eltype
->length ()),
475 tmp
, eltype
->length ());
480 rettype
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
481 ret
= value_from_longest (rettype
, value_logical_not (arg
));
487 /* Perform a relational operation on two scalar operands. */
490 scalar_relop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
497 ret
= value_equal (val1
, val2
);
500 ret
= !value_equal (val1
, val2
);
503 ret
= value_less (val1
, val2
);
506 ret
= value_less (val2
, val1
);
509 ret
= value_less (val2
, val1
) || value_equal (val1
, val2
);
512 ret
= value_less (val1
, val2
) || value_equal (val1
, val2
);
514 case BINOP_LOGICAL_AND
:
515 ret
= !value_logical_not (val1
) && !value_logical_not (val2
);
517 case BINOP_LOGICAL_OR
:
518 ret
= !value_logical_not (val1
) || !value_logical_not (val2
);
521 error (_("Attempt to perform an unsupported operation"));
527 /* Perform a relational operation on two vector operands. */
529 static struct value
*
530 vector_relop (struct expression
*exp
, struct value
*val1
, struct value
*val2
,
534 struct type
*type1
, *type2
, *eltype1
, *eltype2
, *rettype
;
535 int t1_is_vec
, t2_is_vec
, i
;
536 LONGEST lowb1
, lowb2
, highb1
, highb2
;
538 type1
= check_typedef (val1
->type ());
539 type2
= check_typedef (val2
->type ());
541 t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
&& type1
->is_vector ());
542 t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
&& type2
->is_vector ());
544 if (!t1_is_vec
|| !t2_is_vec
)
545 error (_("Vector operations are not supported on scalar types"));
547 eltype1
= check_typedef (type1
->target_type ());
548 eltype2
= check_typedef (type2
->target_type ());
550 if (!get_array_bounds (type1
,&lowb1
, &highb1
)
551 || !get_array_bounds (type2
, &lowb2
, &highb2
))
552 error (_("Could not determine the vector bounds"));
554 /* Check whether the vector types are compatible. */
555 if (eltype1
->code () != eltype2
->code ()
556 || eltype1
->length () != eltype2
->length ()
557 || eltype1
->is_unsigned () != eltype2
->is_unsigned ()
558 || lowb1
!= lowb2
|| highb1
!= highb2
)
559 error (_("Cannot perform operation on vectors with different types"));
561 /* Determine the resulting type of the operation and allocate the value. */
562 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
563 eltype1
->length (), 0,
565 ret
= value::allocate (rettype
);
567 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
569 /* For vector types, the relational, equality and logical operators shall
570 return 0 if the specified relation is false and -1 (i.e. all bits set)
571 if the specified relation is true. */
572 int tmp
= scalar_relop (value_subscript (val1
, i
),
573 value_subscript (val2
, i
), op
) ? -1 : 0;
574 memset ((ret
->contents_writeable ().data ()
575 + i
* eltype1
->length ()),
576 tmp
, eltype1
->length ());
582 /* Perform a cast of ARG into TYPE. There's sadly a lot of duplication in
583 here from valops.c:value_cast, opencl is different only in the
584 behavior of scalar to vector casting. As far as possibly we're going
585 to try and delegate back to the standard value_cast function. */
588 opencl_value_cast (struct type
*type
, struct value
*arg
)
590 if (type
!= arg
->type ())
592 /* Casting scalar to vector is a special case for OpenCL, scalar
593 is cast to element type of vector then replicated into each
594 element of the vector. First though, we need to work out if
595 this is a scalar to vector cast; code lifted from
596 valops.c:value_cast. */
597 enum type_code code1
, code2
;
598 struct type
*to_type
;
601 to_type
= check_typedef (type
);
603 code1
= to_type
->code ();
604 code2
= check_typedef (arg
->type ())->code ();
606 if (code2
== TYPE_CODE_REF
)
607 code2
= check_typedef (coerce_ref(arg
)->type ())->code ();
609 scalar
= (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
610 || code2
== TYPE_CODE_CHAR
|| code2
== TYPE_CODE_FLT
611 || code2
== TYPE_CODE_DECFLOAT
|| code2
== TYPE_CODE_ENUM
612 || code2
== TYPE_CODE_RANGE
);
614 if (code1
== TYPE_CODE_ARRAY
&& to_type
->is_vector () && scalar
)
618 /* Cast to the element type of the vector here as
619 value_vector_widen will error if the scalar value is
620 truncated by the cast. To avoid the error, cast (and
621 possibly truncate) here. */
622 eltype
= check_typedef (to_type
->target_type ());
623 arg
= value_cast (eltype
, arg
);
625 return value_vector_widen (arg
, type
);
628 /* Standard cast handler. */
629 arg
= value_cast (type
, arg
);
634 /* Perform a relational operation on two operands. */
637 opencl_relop (struct type
*expect_type
, struct expression
*exp
,
638 enum noside noside
, enum exp_opcode op
,
639 struct value
*arg1
, struct value
*arg2
)
642 struct type
*type1
= check_typedef (arg1
->type ());
643 struct type
*type2
= check_typedef (arg2
->type ());
644 int t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
645 && type1
->is_vector ());
646 int t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
647 && type2
->is_vector ());
649 if (!t1_is_vec
&& !t2_is_vec
)
651 int tmp
= scalar_relop (arg1
, arg2
, op
);
653 language_bool_type (exp
->language_defn
, exp
->gdbarch
);
655 val
= value_from_longest (type
, tmp
);
657 else if (t1_is_vec
&& t2_is_vec
)
659 val
= vector_relop (exp
, arg1
, arg2
, op
);
663 /* Widen the scalar operand to a vector. */
664 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
665 struct type
*t
= t1_is_vec
? type2
: type1
;
667 if (t
->code () != TYPE_CODE_FLT
&& !is_integral_type (t
))
668 error (_("Argument to operation not a number or boolean."));
670 *v
= opencl_value_cast (t1_is_vec
? type1
: type2
, *v
);
671 val
= vector_relop (exp
, arg1
, arg2
, op
);
677 /* A helper function for BINOP_ASSIGN. */
680 eval_opencl_assign (struct type
*expect_type
, struct expression
*exp
,
681 enum noside noside
, enum exp_opcode op
,
682 struct value
*arg1
, struct value
*arg2
)
684 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
687 struct type
*type1
= arg1
->type ();
688 if (arg1
->deprecated_modifiable ()
689 && arg1
->lval () != lval_internalvar
)
690 arg2
= opencl_value_cast (type1
, arg2
);
692 return value_assign (arg1
, arg2
);
699 opencl_structop_operation::evaluate (struct type
*expect_type
,
700 struct expression
*exp
,
703 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
704 struct type
*type1
= check_typedef (arg1
->type ());
706 if (type1
->code () == TYPE_CODE_ARRAY
&& type1
->is_vector ())
707 return opencl_component_ref (exp
, arg1
, std::get
<1> (m_storage
).c_str (),
711 struct value
*v
= value_struct_elt (&arg1
, {},
712 std::get
<1> (m_storage
).c_str (),
715 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
716 v
= value::zero (v
->type (), v
->lval ());
722 opencl_logical_binop_operation::evaluate (struct type
*expect_type
,
723 struct expression
*exp
,
726 enum exp_opcode op
= std::get
<0> (m_storage
);
727 value
*arg1
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
729 /* For scalar operations we need to avoid evaluating operands
730 unnecessarily. However, for vector operations we always need to
731 evaluate both operands. Unfortunately we only know which of the
732 two cases apply after we know the type of the second operand.
733 Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS. */
734 value
*arg2
= std::get
<2> (m_storage
)->evaluate (nullptr, exp
,
735 EVAL_AVOID_SIDE_EFFECTS
);
736 struct type
*type1
= check_typedef (arg1
->type ());
737 struct type
*type2
= check_typedef (arg2
->type ());
739 if ((type1
->code () == TYPE_CODE_ARRAY
&& type1
->is_vector ())
740 || (type2
->code () == TYPE_CODE_ARRAY
&& type2
->is_vector ()))
742 arg2
= std::get
<2> (m_storage
)->evaluate (nullptr, exp
, noside
);
744 return opencl_relop (nullptr, exp
, noside
, op
, arg1
, arg2
);
748 /* For scalar built-in types, only evaluate the right
749 hand operand if the left hand operand compares
750 unequal(&&)/equal(||) to 0. */
751 bool tmp
= value_logical_not (arg1
);
753 if (op
== BINOP_LOGICAL_OR
)
758 arg2
= std::get
<2> (m_storage
)->evaluate (nullptr, exp
, noside
);
759 tmp
= value_logical_not (arg2
);
760 if (op
== BINOP_LOGICAL_OR
)
764 type1
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
765 return value_from_longest (type1
, tmp
);
770 opencl_ternop_cond_operation::evaluate (struct type
*expect_type
,
771 struct expression
*exp
,
774 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
775 struct type
*type1
= check_typedef (arg1
->type ());
776 if (type1
->code () == TYPE_CODE_ARRAY
&& type1
->is_vector ())
778 struct value
*arg2
, *arg3
, *tmp
, *ret
;
779 struct type
*eltype2
, *type2
, *type3
, *eltype3
;
780 int t2_is_vec
, t3_is_vec
, i
;
781 LONGEST lowb1
, lowb2
, lowb3
, highb1
, highb2
, highb3
;
783 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
784 arg3
= std::get
<2> (m_storage
)->evaluate (nullptr, exp
, noside
);
785 type2
= check_typedef (arg2
->type ());
786 type3
= check_typedef (arg3
->type ());
788 = type2
->code () == TYPE_CODE_ARRAY
&& type2
->is_vector ();
790 = type3
->code () == TYPE_CODE_ARRAY
&& type3
->is_vector ();
792 /* Widen the scalar operand to a vector if necessary. */
793 if (t2_is_vec
|| !t3_is_vec
)
795 arg3
= opencl_value_cast (type2
, arg3
);
796 type3
= arg3
->type ();
798 else if (!t2_is_vec
|| t3_is_vec
)
800 arg2
= opencl_value_cast (type3
, arg2
);
801 type2
= arg2
->type ();
803 else if (!t2_is_vec
|| !t3_is_vec
)
805 /* Throw an error if arg2 or arg3 aren't vectors. */
807 Cannot perform conditional operation on incompatible types"));
810 eltype2
= check_typedef (type2
->target_type ());
811 eltype3
= check_typedef (type3
->target_type ());
813 if (!get_array_bounds (type1
, &lowb1
, &highb1
)
814 || !get_array_bounds (type2
, &lowb2
, &highb2
)
815 || !get_array_bounds (type3
, &lowb3
, &highb3
))
816 error (_("Could not determine the vector bounds"));
818 /* Throw an error if the types of arg2 or arg3 are incompatible. */
819 if (eltype2
->code () != eltype3
->code ()
820 || eltype2
->length () != eltype3
->length ()
821 || eltype2
->is_unsigned () != eltype3
->is_unsigned ()
822 || lowb2
!= lowb3
|| highb2
!= highb3
)
824 Cannot perform operation on vectors with different types"));
826 /* Throw an error if the sizes of arg1 and arg2/arg3 differ. */
827 if (lowb1
!= lowb2
|| lowb1
!= lowb3
828 || highb1
!= highb2
|| highb1
!= highb3
)
830 Cannot perform conditional operation on vectors with different sizes"));
832 ret
= value::allocate (type2
);
834 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
836 tmp
= value_logical_not (value_subscript (arg1
, i
)) ?
837 value_subscript (arg3
, i
) : value_subscript (arg2
, i
);
838 memcpy (ret
->contents_writeable ().data () +
839 i
* eltype2
->length (), tmp
->contents_all ().data (),
847 if (value_logical_not (arg1
))
848 return std::get
<2> (m_storage
)->evaluate (nullptr, exp
, noside
);
850 return std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
854 } /* namespace expr */
856 /* Class representing the OpenCL language. */
858 class opencl_language
: public language_defn
862 : language_defn (language_opencl
)
865 /* See language.h. */
867 const char *name () const override
870 /* See language.h. */
872 const char *natural_name () const override
873 { return "OpenCL C"; }
875 /* See language.h. */
876 void language_arch_info (struct gdbarch
*gdbarch
,
877 struct language_arch_info
*lai
) const override
879 /* Helper function to allow shorter lines below. */
880 auto add
= [&] (struct type
* t
) -> struct type
*
882 lai
->add_primitive_type (t
);
886 /* Helper macro to create strings. */
887 #define OCL_STRING(S) #S
889 /* This macro allocates and assigns the type struct pointers
890 for the vector types. */
891 #define BUILD_OCL_VTYPES(TYPE, ELEMENT_TYPE) \
895 tmp = add (init_vector_type (ELEMENT_TYPE, 2)); \
896 tmp->set_name (OCL_STRING(TYPE ## 2)); \
897 tmp = add (init_vector_type (ELEMENT_TYPE, 3)); \
898 tmp->set_name (OCL_STRING(TYPE ## 3)); \
899 tmp->set_length (4 * (ELEMENT_TYPE)->length ()); \
900 tmp = add (init_vector_type (ELEMENT_TYPE, 4)); \
901 tmp->set_name (OCL_STRING(TYPE ## 4)); \
902 tmp = add (init_vector_type (ELEMENT_TYPE, 8)); \
903 tmp->set_name (OCL_STRING(TYPE ## 8)); \
904 tmp = init_vector_type (ELEMENT_TYPE, 16); \
905 tmp->set_name (OCL_STRING(TYPE ## 16)); \
909 struct type
*el_type
, *char_type
, *int_type
;
911 type_allocator
alloc (gdbarch
);
912 char_type
= el_type
= add (init_integer_type (alloc
, 8, 0, "char"));
913 BUILD_OCL_VTYPES (char, el_type
);
914 el_type
= add (init_integer_type (alloc
, 8, 1, "uchar"));
915 BUILD_OCL_VTYPES (uchar
, el_type
);
916 el_type
= add (init_integer_type (alloc
, 16, 0, "short"));
917 BUILD_OCL_VTYPES (short, el_type
);
918 el_type
= add (init_integer_type (alloc
, 16, 1, "ushort"));
919 BUILD_OCL_VTYPES (ushort
, el_type
);
920 int_type
= el_type
= add (init_integer_type (alloc
, 32, 0, "int"));
921 BUILD_OCL_VTYPES (int, el_type
);
922 el_type
= add (init_integer_type (alloc
, 32, 1, "uint"));
923 BUILD_OCL_VTYPES (uint
, el_type
);
924 el_type
= add (init_integer_type (alloc
, 64, 0, "long"));
925 BUILD_OCL_VTYPES (long, el_type
);
926 el_type
= add (init_integer_type (alloc
, 64, 1, "ulong"));
927 BUILD_OCL_VTYPES (ulong
, el_type
);
928 el_type
= add (init_float_type (alloc
, 16, "half", floatformats_ieee_half
));
929 BUILD_OCL_VTYPES (half
, el_type
);
930 el_type
= add (init_float_type (alloc
, 32, "float", floatformats_ieee_single
));
931 BUILD_OCL_VTYPES (float, el_type
);
932 el_type
= add (init_float_type (alloc
, 64, "double", floatformats_ieee_double
));
933 BUILD_OCL_VTYPES (double, el_type
);
935 add (init_boolean_type (alloc
, 8, 1, "bool"));
936 add (init_integer_type (alloc
, 8, 1, "unsigned char"));
937 add (init_integer_type (alloc
, 16, 1, "unsigned short"));
938 add (init_integer_type (alloc
, 32, 1, "unsigned int"));
939 add (init_integer_type (alloc
, 64, 1, "unsigned long"));
940 add (init_integer_type (alloc
, gdbarch_ptr_bit (gdbarch
), 1, "size_t"));
941 add (init_integer_type (alloc
, gdbarch_ptr_bit (gdbarch
), 0, "ptrdiff_t"));
942 add (init_integer_type (alloc
, gdbarch_ptr_bit (gdbarch
), 0, "intptr_t"));
943 add (init_integer_type (alloc
, gdbarch_ptr_bit (gdbarch
), 1, "uintptr_t"));
944 add (builtin_type (gdbarch
)->builtin_void
);
946 /* Type of elements of strings. */
947 lai
->set_string_char_type (char_type
);
949 /* Specifies the return type of logical and relational operations. */
950 lai
->set_bool_type (int_type
, "int");
953 /* See language.h. */
955 bool can_print_type_offsets () const override
960 /* See language.h. */
962 void print_type (struct type
*type
, const char *varstring
,
963 struct ui_file
*stream
, int show
, int level
,
964 const struct type_print_options
*flags
) const override
966 /* We nearly always defer to C type printing, except that vector types
967 are considered primitive in OpenCL, and should always be printed
968 using their TYPE_NAME. */
971 type
= check_typedef (type
);
972 if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ()
973 && type
->name () != NULL
)
977 c_print_type (type
, varstring
, stream
, show
, level
, la_language
, flags
);
980 /* See language.h. */
982 enum macro_expansion
macro_expansion () const override
983 { return macro_expansion_c
; }
986 /* Single instance of the OpenCL language class. */
988 static opencl_language opencl_language_defn
;