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"
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 (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. */
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 value_incref (val
); /* Increment the reference counter of the value. */
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
);
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 (value_contents_raw (v
) + j
++ * elsize
,
142 value_contents (c
->val
) + c
->indices
[i
] * elsize
,
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
);
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);
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);
172 In this case n would be 4 (offset=12/4 + 1) while c->n would be 3. */
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
,
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. */
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 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
;
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
=
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
,
271 struct type
*type
= check_typedef (value_type (val
));
272 struct type
*elm_type
= TYPE_TARGET_TYPE (type
);
275 /* Check if a single component of a vector is requested which means
276 the resulting type is a (primitive) scalar type. */
279 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
280 ret
= value_zero (elm_type
, not_lval
);
282 ret
= value_subscript (val
, indices
[0]);
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
);
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
);
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
)),
319 + (indices
[i
] * TYPE_LENGTH (elm_type
)),
320 TYPE_LENGTH (elm_type
));
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
)
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
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
++)
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
++)
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
++)
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') ? \
385 dst_len
= strlen (comps
);
386 /* Skip the s/S-prefix. */
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
394 if (indices
[i
] < 0 || indices
[i
] >= src_len
)
395 error (_("Invalid OpenCL vector component accessor %s"), comps
);
400 dst_len
= strlen (comps
);
402 for (i
= 0; i
< dst_len
; i
++)
415 error (_("Invalid OpenCL vector component accessor %s"), comps
);
420 error (_("Invalid OpenCL vector component accessor %s"), comps
);
424 error (_("Invalid OpenCL vector component accessor %s"), comps
);
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
);
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
;
450 if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
452 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
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
461 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
462 TYPE_LENGTH (eltype
), 0,
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
));
478 rettype
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
479 ret
= value_from_longest (rettype
, value_logical_not (arg
));
485 /* Perform a relational operation on two scalar operands. */
488 scalar_relop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
495 ret
= value_equal (val1
, val2
);
498 ret
= !value_equal (val1
, val2
);
501 ret
= value_less (val1
, val2
);
504 ret
= value_less (val2
, val1
);
507 ret
= value_less (val2
, val1
) || value_equal (val1
, val2
);
510 ret
= value_less (val1
, val2
) || value_equal (val1
, val2
);
512 case BINOP_LOGICAL_AND
:
513 ret
= !value_logical_not (val1
) && !value_logical_not (val2
);
515 case BINOP_LOGICAL_OR
:
516 ret
= !value_logical_not (val1
) || !value_logical_not (val2
);
519 error (_("Attempt to perform an unsupported operation"));
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
,
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,
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
));
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
;
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
)
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
);
625 /* Standard cast handler. */
626 arg
= value_cast (type
, 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
,
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
);
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
);
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
);
673 /* A helper function for BINOP_ASSIGN. */
675 static struct value
*
676 eval_opencl_assign (struct type
*expect_type
, struct expression
*exp
,
678 struct value
*arg1
, struct value
*arg2
)
680 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
683 struct type
*type1
= value_type (arg1
);
684 if (deprecated_value_modifiable (arg1
)
685 && VALUE_LVAL (arg1
) != lval_internalvar
)
686 arg2
= opencl_value_cast (type1
, arg2
);
688 return value_assign (arg1
, arg2
);
691 /* Expression evaluator for the OpenCL. Most operations are delegated to
692 evaluate_subexp_standard; see that function for a description of the
695 static struct value
*
696 evaluate_subexp_opencl (struct type
*expect_type
, struct expression
*exp
,
697 int *pos
, enum noside noside
)
699 enum exp_opcode op
= exp
->elts
[*pos
].opcode
;
700 struct value
*arg1
= NULL
;
701 struct value
*arg2
= NULL
;
702 struct type
*type1
, *type2
;
706 /* Handle assignment and cast operators to support OpenCL-style
707 scalar-to-vector widening. */
710 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
711 type1
= value_type (arg1
);
712 arg2
= evaluate_subexp (type1
, exp
, pos
, noside
);
714 return eval_opencl_assign (expect_type
, exp
, noside
, arg1
, arg2
);
717 type1
= exp
->elts
[*pos
+ 1].type
;
719 arg1
= evaluate_subexp (type1
, exp
, pos
, noside
);
721 if (noside
== EVAL_SKIP
)
722 return value_from_longest (builtin_type (exp
->gdbarch
)->
725 return opencl_value_cast (type1
, arg1
);
729 arg1
= evaluate_subexp (NULL
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
730 type1
= value_type (arg1
);
731 arg1
= evaluate_subexp (type1
, exp
, pos
, noside
);
733 if (noside
== EVAL_SKIP
)
734 return value_from_longest (builtin_type (exp
->gdbarch
)->
737 return opencl_value_cast (type1
, arg1
);
739 /* Handle binary relational and equality operators that are either not
740 or differently defined for GNU vectors. */
748 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
749 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
751 if (noside
== EVAL_SKIP
)
752 return value_from_longest (builtin_type (exp
->gdbarch
)->
755 return opencl_relop (exp
, arg1
, arg2
, op
);
757 /* Handle the logical unary operator not(!). */
758 case UNOP_LOGICAL_NOT
:
760 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
762 if (noside
== EVAL_SKIP
)
763 return value_from_longest (builtin_type (exp
->gdbarch
)->
766 return opencl_logical_not (exp
, arg1
);
768 /* Handle the logical operator and(&&) and or(||). */
769 case BINOP_LOGICAL_AND
:
770 case BINOP_LOGICAL_OR
:
772 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
774 if (noside
== EVAL_SKIP
)
776 evaluate_subexp (nullptr, exp
, pos
, noside
);
778 return value_from_longest (builtin_type (exp
->gdbarch
)->
783 /* For scalar operations we need to avoid evaluating operands
784 unnecessarily. However, for vector operations we always need to
785 evaluate both operands. Unfortunately we only know which of the
786 two cases apply after we know the type of the second operand.
787 Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS. */
790 arg2
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
792 type1
= check_typedef (value_type (arg1
));
793 type2
= check_typedef (value_type (arg2
));
795 if ((type1
->code () == TYPE_CODE_ARRAY
&& type1
->is_vector ())
796 || (type2
->code () == TYPE_CODE_ARRAY
&& type2
->is_vector ()))
798 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
800 return opencl_relop (exp
, arg1
, arg2
, op
);
804 /* For scalar built-in types, only evaluate the right
805 hand operand if the left hand operand compares
806 unequal(&&)/equal(||) to 0. */
808 int tmp
= value_logical_not (arg1
);
810 if (op
== BINOP_LOGICAL_OR
)
814 = evaluate_subexp (nullptr, exp
, pos
, tmp
? EVAL_SKIP
: noside
);
815 type1
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
817 if (op
== BINOP_LOGICAL_AND
)
818 res
= !tmp
&& !value_logical_not (arg2
);
819 else /* BINOP_LOGICAL_OR */
820 res
= tmp
|| !value_logical_not (arg2
);
822 return value_from_longest (type1
, res
);
826 /* Handle the ternary selection operator. */
829 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
830 type1
= check_typedef (value_type (arg1
));
831 if (type1
->code () == TYPE_CODE_ARRAY
&& type1
->is_vector ())
833 struct value
*arg3
, *tmp
, *ret
;
834 struct type
*eltype2
, *type3
, *eltype3
;
835 int t2_is_vec
, t3_is_vec
, i
;
836 LONGEST lowb1
, lowb2
, lowb3
, highb1
, highb2
, highb3
;
838 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
839 arg3
= evaluate_subexp (nullptr, exp
, pos
, noside
);
840 type2
= check_typedef (value_type (arg2
));
841 type3
= check_typedef (value_type (arg3
));
843 = type2
->code () == TYPE_CODE_ARRAY
&& type2
->is_vector ();
845 = type3
->code () == TYPE_CODE_ARRAY
&& type3
->is_vector ();
847 /* Widen the scalar operand to a vector if necessary. */
848 if (t2_is_vec
|| !t3_is_vec
)
850 arg3
= opencl_value_cast (type2
, arg3
);
851 type3
= value_type (arg3
);
853 else if (!t2_is_vec
|| t3_is_vec
)
855 arg2
= opencl_value_cast (type3
, arg2
);
856 type2
= value_type (arg2
);
858 else if (!t2_is_vec
|| !t3_is_vec
)
860 /* Throw an error if arg2 or arg3 aren't vectors. */
862 Cannot perform conditional operation on incompatible types"));
865 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
866 eltype3
= check_typedef (TYPE_TARGET_TYPE (type3
));
868 if (!get_array_bounds (type1
, &lowb1
, &highb1
)
869 || !get_array_bounds (type2
, &lowb2
, &highb2
)
870 || !get_array_bounds (type3
, &lowb3
, &highb3
))
871 error (_("Could not determine the vector bounds"));
873 /* Throw an error if the types of arg2 or arg3 are incompatible. */
874 if (eltype2
->code () != eltype3
->code ()
875 || TYPE_LENGTH (eltype2
) != TYPE_LENGTH (eltype3
)
876 || eltype2
->is_unsigned () != eltype3
->is_unsigned ()
877 || lowb2
!= lowb3
|| highb2
!= highb3
)
879 Cannot perform operation on vectors with different types"));
881 /* Throw an error if the sizes of arg1 and arg2/arg3 differ. */
882 if (lowb1
!= lowb2
|| lowb1
!= lowb3
883 || highb1
!= highb2
|| highb1
!= highb3
)
885 Cannot perform conditional operation on vectors with different sizes"));
887 ret
= allocate_value (type2
);
889 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
891 tmp
= value_logical_not (value_subscript (arg1
, i
)) ?
892 value_subscript (arg3
, i
) : value_subscript (arg2
, i
);
893 memcpy (value_contents_writeable (ret
) +
894 i
* TYPE_LENGTH (eltype2
), value_contents_all (tmp
),
895 TYPE_LENGTH (eltype2
));
902 if (value_logical_not (arg1
))
904 /* Skip the second operand. */
905 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
907 return evaluate_subexp (nullptr, exp
, pos
, noside
);
911 /* Skip the third operand. */
912 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
913 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
919 /* Handle STRUCTOP_STRUCT to allow component access on OpenCL vectors. */
920 case STRUCTOP_STRUCT
:
923 int tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
925 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
926 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
927 type1
= check_typedef (value_type (arg1
));
929 if (noside
== EVAL_SKIP
)
931 return value_from_longest (builtin_type (exp
->gdbarch
)->
934 else if (type1
->code () == TYPE_CODE_ARRAY
&& type1
->is_vector ())
936 return opencl_component_ref (exp
, arg1
, &exp
->elts
[pc
+ 2].string
,
941 struct value
*v
= value_struct_elt (&arg1
, NULL
,
942 &exp
->elts
[pc
+ 2].string
, NULL
,
945 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
946 v
= value_zero (value_type (v
), VALUE_LVAL (v
));
954 return evaluate_subexp_c (expect_type
, exp
, pos
, noside
);
957 const struct exp_descriptor exp_descriptor_opencl
=
959 print_subexp_standard
,
960 operator_length_standard
,
961 operator_check_standard
,
962 dump_subexp_body_standard
,
963 evaluate_subexp_opencl
966 /* Class representing the OpenCL language. */
968 class opencl_language
: public language_defn
972 : language_defn (language_opencl
)
975 /* See language.h. */
977 const char *name () const override
980 /* See language.h. */
982 const char *natural_name () const override
983 { return "OpenCL C"; }
985 /* See language.h. */
986 void language_arch_info (struct gdbarch
*gdbarch
,
987 struct language_arch_info
*lai
) const override
989 /* Helper function to allow shorter lines below. */
990 auto add
= [&] (struct type
* t
) -> struct type
*
992 lai
->add_primitive_type (t
);
996 /* Helper macro to create strings. */
997 #define OCL_STRING(S) #S
999 /* This macro allocates and assigns the type struct pointers
1000 for the vector types. */
1001 #define BUILD_OCL_VTYPES(TYPE, ELEMENT_TYPE) \
1005 tmp = add (init_vector_type (ELEMENT_TYPE, 2)); \
1006 tmp->set_name (OCL_STRING(TYPE ## 2)); \
1007 tmp = add (init_vector_type (ELEMENT_TYPE, 3)); \
1008 tmp->set_name (OCL_STRING(TYPE ## 3)); \
1009 TYPE_LENGTH (tmp) = 4 * TYPE_LENGTH (ELEMENT_TYPE); \
1010 tmp = add (init_vector_type (ELEMENT_TYPE, 4)); \
1011 tmp->set_name (OCL_STRING(TYPE ## 4)); \
1012 tmp = add (init_vector_type (ELEMENT_TYPE, 8)); \
1013 tmp->set_name (OCL_STRING(TYPE ## 8)); \
1014 tmp = init_vector_type (ELEMENT_TYPE, 16); \
1015 tmp->set_name (OCL_STRING(TYPE ## 16)); \
1019 struct type
*el_type
, *char_type
, *int_type
;
1021 char_type
= el_type
= add (arch_integer_type (gdbarch
, 8, 0, "char"));
1022 BUILD_OCL_VTYPES (char, el_type
);
1023 el_type
= add (arch_integer_type (gdbarch
, 8, 1, "uchar"));
1024 BUILD_OCL_VTYPES (uchar
, el_type
);
1025 el_type
= add (arch_integer_type (gdbarch
, 16, 0, "short"));
1026 BUILD_OCL_VTYPES (short, el_type
);
1027 el_type
= add (arch_integer_type (gdbarch
, 16, 1, "ushort"));
1028 BUILD_OCL_VTYPES (ushort
, el_type
);
1029 int_type
= el_type
= add (arch_integer_type (gdbarch
, 32, 0, "int"));
1030 BUILD_OCL_VTYPES (int, el_type
);
1031 el_type
= add (arch_integer_type (gdbarch
, 32, 1, "uint"));
1032 BUILD_OCL_VTYPES (uint
, el_type
);
1033 el_type
= add (arch_integer_type (gdbarch
, 64, 0, "long"));
1034 BUILD_OCL_VTYPES (long, el_type
);
1035 el_type
= add (arch_integer_type (gdbarch
, 64, 1, "ulong"));
1036 BUILD_OCL_VTYPES (ulong
, el_type
);
1037 el_type
= add (arch_float_type (gdbarch
, 16, "half", floatformats_ieee_half
));
1038 BUILD_OCL_VTYPES (half
, el_type
);
1039 el_type
= add (arch_float_type (gdbarch
, 32, "float", floatformats_ieee_single
));
1040 BUILD_OCL_VTYPES (float, el_type
);
1041 el_type
= add (arch_float_type (gdbarch
, 64, "double", floatformats_ieee_double
));
1042 BUILD_OCL_VTYPES (double, el_type
);
1044 add (arch_boolean_type (gdbarch
, 8, 1, "bool"));
1045 add (arch_integer_type (gdbarch
, 8, 1, "unsigned char"));
1046 add (arch_integer_type (gdbarch
, 16, 1, "unsigned short"));
1047 add (arch_integer_type (gdbarch
, 32, 1, "unsigned int"));
1048 add (arch_integer_type (gdbarch
, 64, 1, "unsigned long"));
1049 add (arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "size_t"));
1050 add (arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "ptrdiff_t"));
1051 add (arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "intptr_t"));
1052 add (arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "uintptr_t"));
1053 add (arch_type (gdbarch
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, "void"));
1055 /* Type of elements of strings. */
1056 lai
->set_string_char_type (char_type
);
1058 /* Specifies the return type of logical and relational operations. */
1059 lai
->set_bool_type (int_type
, "int");
1062 /* See language.h. */
1064 void print_type (struct type
*type
, const char *varstring
,
1065 struct ui_file
*stream
, int show
, int level
,
1066 const struct type_print_options
*flags
) const override
1068 /* We nearly always defer to C type printing, except that vector types
1069 are considered primitive in OpenCL, and should always be printed
1070 using their TYPE_NAME. */
1073 type
= check_typedef (type
);
1074 if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ()
1075 && type
->name () != NULL
)
1079 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
1082 /* See language.h. */
1084 enum macro_expansion
macro_expansion () const override
1085 { return macro_expansion_c
; }
1087 /* See language.h. */
1089 const struct exp_descriptor
*expression_ops () const override
1090 { return &exp_descriptor_opencl
; }
1092 /* See language.h. */
1094 const struct op_print
*opcode_print_table () const override
1095 { return c_op_print_tab
; }
1098 /* Single instance of the OpenCL language class. */
1100 static opencl_language opencl_language_defn
;