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/>. */
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 (type
)->code () == code
57 && TYPE_TARGET_TYPE (type
)->is_unsigned () == flag_unsigned
58 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) == el_length
59 && TYPE_LENGTH (type
) == length
60 && highb
- lowb
+ 1 == n
);
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
= TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)));
125 LONGEST offset
= value_offset (v
);
126 LONGEST elsize
= TYPE_LENGTH (eltype
);
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
) + j
++ * elsize
,
143 value_contents (c
->val
) + c
->indices
[i
] * elsize
,
148 lval_func_write (struct value
*v
, struct value
*fromval
)
150 struct value
*mark
= value_mark ();
151 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
152 struct type
*type
= check_typedef (value_type (v
));
153 struct type
*eltype
= TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)));
154 LONGEST offset
= value_offset (v
);
155 LONGEST elsize
= TYPE_LENGTH (eltype
);
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
= allocate_value (eltype
);
180 struct value
*to_elm_val
= value_subscript (c
->val
, c
->indices
[i
]);
182 memcpy (value_contents_writeable (from_elm_val
),
183 value_contents (fromval
) + j
++ * elsize
,
185 value_assign (to_elm_val
, from_elm_val
);
188 value_free_to_mark (mark
);
191 /* Return nonzero if bits in V from OFFSET and LENGTH represent a
192 synthetic pointer. */
195 lval_func_check_synthetic_pointer (const struct value
*v
,
196 LONGEST offset
, int length
)
198 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
199 /* Size of the target type in bits. */
201 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)))) * 8;
202 int startrest
= offset
% elsize
;
203 int start
= offset
/ elsize
;
204 int endrest
= (offset
+ length
) % elsize
;
205 int end
= (offset
+ length
) / elsize
;
214 for (i
= start
; i
< end
; i
++)
216 int comp_offset
= (i
== start
) ? startrest
: 0;
217 int comp_length
= (i
== end
) ? endrest
: elsize
;
219 if (!value_bits_synthetic_pointer (c
->val
,
220 c
->indices
[i
] * elsize
+ comp_offset
,
229 lval_func_copy_closure (const struct value
*v
)
231 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
239 lval_func_free_closure (struct value
*v
)
241 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
247 value_decref (c
->val
); /* Decrement the reference counter of the value. */
253 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 (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 (),
291 TYPE_LENGTH (elm_type
),
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
)
318 + (i
* TYPE_LENGTH (elm_type
)),
320 + (indices
[i
] * TYPE_LENGTH (elm_type
)),
321 TYPE_LENGTH (elm_type
));
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 (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 TYPE_LENGTH (eltype
), 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
) + i
* TYPE_LENGTH (eltype
),
476 tmp
, TYPE_LENGTH (eltype
));
481 rettype
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
482 ret
= value_from_longest (rettype
, value_logical_not (arg
));
488 /* Perform a relational operation on two scalar operands. */
491 scalar_relop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
498 ret
= value_equal (val1
, val2
);
501 ret
= !value_equal (val1
, val2
);
504 ret
= value_less (val1
, val2
);
507 ret
= value_less (val2
, val1
);
510 ret
= value_less (val2
, val1
) || value_equal (val1
, val2
);
513 ret
= value_less (val1
, val2
) || value_equal (val1
, val2
);
515 case BINOP_LOGICAL_AND
:
516 ret
= !value_logical_not (val1
) && !value_logical_not (val2
);
518 case BINOP_LOGICAL_OR
:
519 ret
= !value_logical_not (val1
) || !value_logical_not (val2
);
522 error (_("Attempt to perform an unsupported operation"));
528 /* Perform a relational operation on two vector operands. */
530 static struct value
*
531 vector_relop (struct expression
*exp
, struct value
*val1
, struct value
*val2
,
535 struct type
*type1
, *type2
, *eltype1
, *eltype2
, *rettype
;
536 int t1_is_vec
, t2_is_vec
, i
;
537 LONGEST lowb1
, lowb2
, highb1
, highb2
;
539 type1
= check_typedef (value_type (val1
));
540 type2
= check_typedef (value_type (val2
));
542 t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
&& type1
->is_vector ());
543 t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
&& type2
->is_vector ());
545 if (!t1_is_vec
|| !t2_is_vec
)
546 error (_("Vector operations are not supported on scalar types"));
548 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
549 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
551 if (!get_array_bounds (type1
,&lowb1
, &highb1
)
552 || !get_array_bounds (type2
, &lowb2
, &highb2
))
553 error (_("Could not determine the vector bounds"));
555 /* Check whether the vector types are compatible. */
556 if (eltype1
->code () != eltype2
->code ()
557 || TYPE_LENGTH (eltype1
) != TYPE_LENGTH (eltype2
)
558 || eltype1
->is_unsigned () != eltype2
->is_unsigned ()
559 || lowb1
!= lowb2
|| highb1
!= highb2
)
560 error (_("Cannot perform operation on vectors with different types"));
562 /* Determine the resulting type of the operation and allocate the value. */
563 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
564 TYPE_LENGTH (eltype1
), 0,
566 ret
= allocate_value (rettype
);
568 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
570 /* For vector types, the relational, equality and logical operators shall
571 return 0 if the specified relation is false and -1 (i.e. all bits set)
572 if the specified relation is true. */
573 int tmp
= scalar_relop (value_subscript (val1
, i
),
574 value_subscript (val2
, i
), op
) ? -1 : 0;
575 memset (value_contents_writeable (ret
) + i
* TYPE_LENGTH (eltype1
),
576 tmp
, TYPE_LENGTH (eltype1
));
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 behaviour 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
!= value_type (arg
))
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 (value_type (arg
))->code ();
606 if (code2
== TYPE_CODE_REF
)
607 code2
= check_typedef (value_type (coerce_ref(arg
)))->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 (TYPE_TARGET_TYPE (to_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 (value_type (arg1
));
643 struct type
*type2
= check_typedef (value_type (arg2
));
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_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
687 struct type
*type1
= value_type (arg1
);
688 if (deprecated_value_modifiable (arg1
)
689 && VALUE_LVAL (arg1
) != 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 (value_type (arg1
));
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
, NULL
,
712 std::get
<1> (m_storage
).c_str (),
715 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
716 v
= value_zero (value_type (v
), VALUE_LVAL (v
));
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 (value_type (arg1
));
737 struct type
*type2
= check_typedef (value_type (arg2
));
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 int 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 (value_type (arg1
));
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 (value_type (arg2
));
786 type3
= check_typedef (value_type (arg3
));
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
= value_type (arg3
);
798 else if (!t2_is_vec
|| t3_is_vec
)
800 arg2
= opencl_value_cast (type3
, arg2
);
801 type2
= value_type (arg2
);
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 (TYPE_TARGET_TYPE (type2
));
811 eltype3
= check_typedef (TYPE_TARGET_TYPE (type3
));
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 || TYPE_LENGTH (eltype2
) != TYPE_LENGTH (eltype3
)
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
= allocate_value (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 (value_contents_writeable (ret
) +
839 i
* TYPE_LENGTH (eltype2
), value_contents_all (tmp
),
840 TYPE_LENGTH (eltype2
));
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 TYPE_LENGTH (tmp) = 4 * TYPE_LENGTH (ELEMENT_TYPE); \
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 char_type
= el_type
= add (arch_integer_type (gdbarch
, 8, 0, "char"));
912 BUILD_OCL_VTYPES (char, el_type
);
913 el_type
= add (arch_integer_type (gdbarch
, 8, 1, "uchar"));
914 BUILD_OCL_VTYPES (uchar
, el_type
);
915 el_type
= add (arch_integer_type (gdbarch
, 16, 0, "short"));
916 BUILD_OCL_VTYPES (short, el_type
);
917 el_type
= add (arch_integer_type (gdbarch
, 16, 1, "ushort"));
918 BUILD_OCL_VTYPES (ushort
, el_type
);
919 int_type
= el_type
= add (arch_integer_type (gdbarch
, 32, 0, "int"));
920 BUILD_OCL_VTYPES (int, el_type
);
921 el_type
= add (arch_integer_type (gdbarch
, 32, 1, "uint"));
922 BUILD_OCL_VTYPES (uint
, el_type
);
923 el_type
= add (arch_integer_type (gdbarch
, 64, 0, "long"));
924 BUILD_OCL_VTYPES (long, el_type
);
925 el_type
= add (arch_integer_type (gdbarch
, 64, 1, "ulong"));
926 BUILD_OCL_VTYPES (ulong
, el_type
);
927 el_type
= add (arch_float_type (gdbarch
, 16, "half", floatformats_ieee_half
));
928 BUILD_OCL_VTYPES (half
, el_type
);
929 el_type
= add (arch_float_type (gdbarch
, 32, "float", floatformats_ieee_single
));
930 BUILD_OCL_VTYPES (float, el_type
);
931 el_type
= add (arch_float_type (gdbarch
, 64, "double", floatformats_ieee_double
));
932 BUILD_OCL_VTYPES (double, el_type
);
934 add (arch_boolean_type (gdbarch
, 8, 1, "bool"));
935 add (arch_integer_type (gdbarch
, 8, 1, "unsigned char"));
936 add (arch_integer_type (gdbarch
, 16, 1, "unsigned short"));
937 add (arch_integer_type (gdbarch
, 32, 1, "unsigned int"));
938 add (arch_integer_type (gdbarch
, 64, 1, "unsigned long"));
939 add (arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "size_t"));
940 add (arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "ptrdiff_t"));
941 add (arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "intptr_t"));
942 add (arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "uintptr_t"));
943 add (arch_type (gdbarch
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, "void"));
945 /* Type of elements of strings. */
946 lai
->set_string_char_type (char_type
);
948 /* Specifies the return type of logical and relational operations. */
949 lai
->set_bool_type (int_type
, "int");
952 /* See language.h. */
954 void print_type (struct type
*type
, const char *varstring
,
955 struct ui_file
*stream
, int show
, int level
,
956 const struct type_print_options
*flags
) const override
958 /* We nearly always defer to C type printing, except that vector types
959 are considered primitive in OpenCL, and should always be printed
960 using their TYPE_NAME. */
963 type
= check_typedef (type
);
964 if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ()
965 && type
->name () != NULL
)
969 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
972 /* See language.h. */
974 enum macro_expansion
macro_expansion () const override
975 { return macro_expansion_c
; }
977 /* See language.h. */
979 const struct op_print
*opcode_print_table () const override
980 { return c_op_print_tab
; }
983 /* Single instance of the OpenCL language class. */
985 static opencl_language opencl_language_defn
;