1 /* OpenCL language support for GDB, the GNU debugger.
2 Copyright (C) 2010-2019 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 /* This macro generates enum values from a given type. */
33 #define OCL_P_TYPE(TYPE)\
34 opencl_primitive_type_##TYPE,\
35 opencl_primitive_type_##TYPE##2,\
36 opencl_primitive_type_##TYPE##3,\
37 opencl_primitive_type_##TYPE##4,\
38 opencl_primitive_type_##TYPE##8,\
39 opencl_primitive_type_##TYPE##16
41 enum opencl_primitive_types
{
53 opencl_primitive_type_bool
,
54 opencl_primitive_type_unsigned_char
,
55 opencl_primitive_type_unsigned_short
,
56 opencl_primitive_type_unsigned_int
,
57 opencl_primitive_type_unsigned_long
,
58 opencl_primitive_type_size_t
,
59 opencl_primitive_type_ptrdiff_t
,
60 opencl_primitive_type_intptr_t
,
61 opencl_primitive_type_uintptr_t
,
62 opencl_primitive_type_void
,
63 nr_opencl_primitive_types
66 static struct gdbarch_data
*opencl_type_data
;
69 builtin_opencl_type (struct gdbarch
*gdbarch
)
71 return (struct type
**) gdbarch_data (gdbarch
, opencl_type_data
);
74 /* Returns the corresponding OpenCL vector type from the given type code,
75 the length of the element type, the unsigned flag and the amount of
79 lookup_opencl_vector_type (struct gdbarch
*gdbarch
, enum type_code code
,
80 unsigned int el_length
, unsigned int flag_unsigned
,
85 struct type
*type
= NULL
;
86 struct type
**types
= builtin_opencl_type (gdbarch
);
88 /* Check if n describes a valid OpenCL vector size (2, 3, 4, 8, 16). */
89 if (n
!= 2 && n
!= 3 && n
!= 4 && n
!= 8 && n
!= 16)
90 error (_("Invalid OpenCL vector size: %d"), n
);
92 /* Triple vectors have the size of a quad vector. */
93 length
= (n
== 3) ? el_length
* 4 : el_length
* n
;
95 for (i
= 0; i
< nr_opencl_primitive_types
; i
++)
99 if (TYPE_CODE (types
[i
]) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (types
[i
])
100 && get_array_bounds (types
[i
], &lowb
, &highb
)
101 && TYPE_CODE (TYPE_TARGET_TYPE (types
[i
])) == code
102 && TYPE_UNSIGNED (TYPE_TARGET_TYPE (types
[i
])) == flag_unsigned
103 && TYPE_LENGTH (TYPE_TARGET_TYPE (types
[i
])) == el_length
104 && TYPE_LENGTH (types
[i
]) == length
105 && highb
- lowb
+ 1 == n
)
115 /* Returns nonzero if the array ARR contains duplicates within
116 the first N elements. */
119 array_has_dups (int *arr
, int n
)
123 for (i
= 0; i
< n
; i
++)
125 for (j
= i
+ 1; j
< n
; j
++)
127 if (arr
[i
] == arr
[j
])
135 /* The OpenCL component access syntax allows to create lvalues referring to
136 selected elements of an original OpenCL vector in arbitrary order. This
137 structure holds the information to describe such lvalues. */
141 /* Reference count. */
143 /* The number of indices. */
145 /* The element indices themselves. */
147 /* A pointer to the original value. */
151 /* Allocates an instance of struct lval_closure. */
153 static struct lval_closure
*
154 allocate_lval_closure (int *indices
, int n
, struct value
*val
)
156 struct lval_closure
*c
= XCNEW (struct lval_closure
);
160 c
->indices
= XCNEWVEC (int, n
);
161 memcpy (c
->indices
, indices
, n
* sizeof (int));
162 value_incref (val
); /* Increment the reference counter of the value. */
169 lval_func_read (struct value
*v
)
171 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
172 struct type
*type
= check_typedef (value_type (v
));
173 struct type
*eltype
= TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)));
174 LONGEST offset
= value_offset (v
);
175 LONGEST elsize
= TYPE_LENGTH (eltype
);
180 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
181 && !get_array_bounds (type
, &lowb
, &highb
))
182 error (_("Could not determine the vector bounds"));
184 /* Assume elsize aligned offset. */
185 gdb_assert (offset
% elsize
== 0);
187 n
= offset
+ highb
- lowb
+ 1;
188 gdb_assert (n
<= c
->n
);
190 for (i
= offset
; i
< n
; i
++)
191 memcpy (value_contents_raw (v
) + j
++ * elsize
,
192 value_contents (c
->val
) + c
->indices
[i
] * elsize
,
197 lval_func_write (struct value
*v
, struct value
*fromval
)
199 struct value
*mark
= value_mark ();
200 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
201 struct type
*type
= check_typedef (value_type (v
));
202 struct type
*eltype
= TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)));
203 LONGEST offset
= value_offset (v
);
204 LONGEST elsize
= TYPE_LENGTH (eltype
);
209 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
210 && !get_array_bounds (type
, &lowb
, &highb
))
211 error (_("Could not determine the vector bounds"));
213 /* Assume elsize aligned offset. */
214 gdb_assert (offset
% elsize
== 0);
216 n
= offset
+ highb
- lowb
+ 1;
218 /* Since accesses to the fourth component of a triple vector is undefined we
219 just skip writes to the fourth element. Imagine something like this:
220 int3 i3 = (int3)(0, 1, 2);
222 In this case n would be 4 (offset=12/4 + 1) while c->n would be 3. */
226 for (i
= offset
; i
< n
; i
++)
228 struct value
*from_elm_val
= allocate_value (eltype
);
229 struct value
*to_elm_val
= value_subscript (c
->val
, c
->indices
[i
]);
231 memcpy (value_contents_writeable (from_elm_val
),
232 value_contents (fromval
) + j
++ * elsize
,
234 value_assign (to_elm_val
, from_elm_val
);
237 value_free_to_mark (mark
);
240 /* Return nonzero if bits in V from OFFSET and LENGTH represent a
241 synthetic pointer. */
244 lval_func_check_synthetic_pointer (const struct value
*v
,
245 LONGEST offset
, int length
)
247 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
248 /* Size of the target type in bits. */
250 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)))) * 8;
251 int startrest
= offset
% elsize
;
252 int start
= offset
/ elsize
;
253 int endrest
= (offset
+ length
) % elsize
;
254 int end
= (offset
+ length
) / elsize
;
263 for (i
= start
; i
< end
; i
++)
265 int comp_offset
= (i
== start
) ? startrest
: 0;
266 int comp_length
= (i
== end
) ? endrest
: elsize
;
268 if (!value_bits_synthetic_pointer (c
->val
,
269 c
->indices
[i
] * elsize
+ comp_offset
,
278 lval_func_copy_closure (const struct value
*v
)
280 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
288 lval_func_free_closure (struct value
*v
)
290 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
296 value_decref (c
->val
); /* Decrement the reference counter of the value. */
302 static const struct lval_funcs opencl_value_funcs
=
307 NULL
, /* coerce_ref */
308 lval_func_check_synthetic_pointer
,
309 lval_func_copy_closure
,
310 lval_func_free_closure
313 /* Creates a sub-vector from VAL. The elements are selected by the indices of
314 an array with the length of N. Supported values for NOSIDE are
315 EVAL_NORMAL and EVAL_AVOID_SIDE_EFFECTS. */
317 static struct value
*
318 create_value (struct gdbarch
*gdbarch
, struct value
*val
, enum noside noside
,
321 struct type
*type
= check_typedef (value_type (val
));
322 struct type
*elm_type
= TYPE_TARGET_TYPE (type
);
325 /* Check if a single component of a vector is requested which means
326 the resulting type is a (primitive) scalar type. */
329 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
330 ret
= value_zero (elm_type
, not_lval
);
332 ret
= value_subscript (val
, indices
[0]);
336 /* Multiple components of the vector are requested which means the
337 resulting type is a vector as well. */
338 struct type
*dst_type
=
339 lookup_opencl_vector_type (gdbarch
, TYPE_CODE (elm_type
),
340 TYPE_LENGTH (elm_type
),
341 TYPE_UNSIGNED (elm_type
), n
);
343 if (dst_type
== NULL
)
344 dst_type
= init_vector_type (elm_type
, n
);
346 make_cv_type (TYPE_CONST (type
), TYPE_VOLATILE (type
), dst_type
, NULL
);
348 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
349 ret
= allocate_value (dst_type
);
352 /* Check whether to create a lvalue or not. */
353 if (VALUE_LVAL (val
) != not_lval
&& !array_has_dups (indices
, n
))
355 struct lval_closure
*c
= allocate_lval_closure (indices
, n
, val
);
356 ret
= allocate_computed_value (dst_type
, &opencl_value_funcs
, c
);
362 ret
= allocate_value (dst_type
);
364 /* Copy src val contents into the destination value. */
365 for (i
= 0; i
< n
; i
++)
366 memcpy (value_contents_writeable (ret
)
367 + (i
* TYPE_LENGTH (elm_type
)),
369 + (indices
[i
] * TYPE_LENGTH (elm_type
)),
370 TYPE_LENGTH (elm_type
));
377 /* OpenCL vector component access. */
379 static struct value
*
380 opencl_component_ref (struct expression
*exp
, struct value
*val
, char *comps
,
389 if (!get_array_bounds (check_typedef (value_type (val
)), &lowb
, &highb
))
390 error (_("Could not determine the vector bounds"));
392 src_len
= highb
- lowb
+ 1;
394 /* Throw an error if the amount of array elements does not fit a
395 valid OpenCL vector size (2, 3, 4, 8, 16). */
396 if (src_len
!= 2 && src_len
!= 3 && src_len
!= 4 && src_len
!= 8
398 error (_("Invalid OpenCL vector size"));
400 if (strcmp (comps
, "lo") == 0 )
402 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
404 for (i
= 0; i
< dst_len
; i
++)
407 else if (strcmp (comps
, "hi") == 0)
409 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
411 for (i
= 0; i
< dst_len
; i
++)
412 indices
[i
] = dst_len
+ i
;
414 else if (strcmp (comps
, "even") == 0)
416 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
418 for (i
= 0; i
< dst_len
; i
++)
421 else if (strcmp (comps
, "odd") == 0)
423 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
425 for (i
= 0; i
< dst_len
; i
++)
428 else if (strncasecmp (comps
, "s", 1) == 0)
430 #define HEXCHAR_TO_INT(C) ((C >= '0' && C <= '9') ? \
431 C-'0' : ((C >= 'A' && C <= 'F') ? \
432 C-'A'+10 : ((C >= 'a' && C <= 'f') ? \
435 dst_len
= strlen (comps
);
436 /* Skip the s/S-prefix. */
439 for (i
= 0; i
< dst_len
; i
++)
441 indices
[i
] = HEXCHAR_TO_INT(comps
[i
+1]);
442 /* Check if the requested component is invalid or exceeds
444 if (indices
[i
] < 0 || indices
[i
] >= src_len
)
445 error (_("Invalid OpenCL vector component accessor %s"), comps
);
450 dst_len
= strlen (comps
);
452 for (i
= 0; i
< dst_len
; i
++)
465 error (_("Invalid OpenCL vector component accessor %s"), comps
);
470 error (_("Invalid OpenCL vector component accessor %s"), comps
);
474 error (_("Invalid OpenCL vector component accessor %s"), comps
);
480 /* Throw an error if the amount of requested components does not
481 result in a valid length (1, 2, 3, 4, 8, 16). */
482 if (dst_len
!= 1 && dst_len
!= 2 && dst_len
!= 3 && dst_len
!= 4
483 && dst_len
!= 8 && dst_len
!= 16)
484 error (_("Invalid OpenCL vector component accessor %s"), comps
);
486 v
= create_value (exp
->gdbarch
, val
, noside
, indices
, dst_len
);
491 /* Perform the unary logical not (!) operation. */
493 static struct value
*
494 opencl_logical_not (struct expression
*exp
, struct value
*arg
)
496 struct type
*type
= check_typedef (value_type (arg
));
497 struct type
*rettype
;
500 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
502 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
506 if (!get_array_bounds (type
, &lowb
, &highb
))
507 error (_("Could not determine the vector bounds"));
509 /* Determine the resulting type of the operation and allocate the
511 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
512 TYPE_LENGTH (eltype
), 0,
514 ret
= allocate_value (rettype
);
516 for (i
= 0; i
< highb
- lowb
+ 1; i
++)
518 /* For vector types, the unary operator shall return a 0 if the
519 value of its operand compares unequal to 0, and -1 (i.e. all bits
520 set) if the value of its operand compares equal to 0. */
521 int tmp
= value_logical_not (value_subscript (arg
, i
)) ? -1 : 0;
522 memset (value_contents_writeable (ret
) + i
* TYPE_LENGTH (eltype
),
523 tmp
, TYPE_LENGTH (eltype
));
528 rettype
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
529 ret
= value_from_longest (rettype
, value_logical_not (arg
));
535 /* Perform a relational operation on two scalar operands. */
538 scalar_relop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
545 ret
= value_equal (val1
, val2
);
548 ret
= !value_equal (val1
, val2
);
551 ret
= value_less (val1
, val2
);
554 ret
= value_less (val2
, val1
);
557 ret
= value_less (val2
, val1
) || value_equal (val1
, val2
);
560 ret
= value_less (val1
, val2
) || value_equal (val1
, val2
);
562 case BINOP_LOGICAL_AND
:
563 ret
= !value_logical_not (val1
) && !value_logical_not (val2
);
565 case BINOP_LOGICAL_OR
:
566 ret
= !value_logical_not (val1
) || !value_logical_not (val2
);
569 error (_("Attempt to perform an unsupported operation"));
575 /* Perform a relational operation on two vector operands. */
577 static struct value
*
578 vector_relop (struct expression
*exp
, struct value
*val1
, struct value
*val2
,
582 struct type
*type1
, *type2
, *eltype1
, *eltype2
, *rettype
;
583 int t1_is_vec
, t2_is_vec
, i
;
584 LONGEST lowb1
, lowb2
, highb1
, highb2
;
586 type1
= check_typedef (value_type (val1
));
587 type2
= check_typedef (value_type (val2
));
589 t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
));
590 t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
));
592 if (!t1_is_vec
|| !t2_is_vec
)
593 error (_("Vector operations are not supported on scalar types"));
595 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
596 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
598 if (!get_array_bounds (type1
,&lowb1
, &highb1
)
599 || !get_array_bounds (type2
, &lowb2
, &highb2
))
600 error (_("Could not determine the vector bounds"));
602 /* Check whether the vector types are compatible. */
603 if (TYPE_CODE (eltype1
) != TYPE_CODE (eltype2
)
604 || TYPE_LENGTH (eltype1
) != TYPE_LENGTH (eltype2
)
605 || TYPE_UNSIGNED (eltype1
) != TYPE_UNSIGNED (eltype2
)
606 || lowb1
!= lowb2
|| highb1
!= highb2
)
607 error (_("Cannot perform operation on vectors with different types"));
609 /* Determine the resulting type of the operation and allocate the value. */
610 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
611 TYPE_LENGTH (eltype1
), 0,
613 ret
= allocate_value (rettype
);
615 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
617 /* For vector types, the relational, equality and logical operators shall
618 return 0 if the specified relation is false and -1 (i.e. all bits set)
619 if the specified relation is true. */
620 int tmp
= scalar_relop (value_subscript (val1
, i
),
621 value_subscript (val2
, i
), op
) ? -1 : 0;
622 memset (value_contents_writeable (ret
) + i
* TYPE_LENGTH (eltype1
),
623 tmp
, TYPE_LENGTH (eltype1
));
629 /* Perform a cast of ARG into TYPE. There's sadly a lot of duplication in
630 here from valops.c:value_cast, opencl is different only in the
631 behaviour of scalar to vector casting. As far as possibly we're going
632 to try and delegate back to the standard value_cast function. */
634 static struct value
*
635 opencl_value_cast (struct type
*type
, struct value
*arg
)
637 if (type
!= value_type (arg
))
639 /* Casting scalar to vector is a special case for OpenCL, scalar
640 is cast to element type of vector then replicated into each
641 element of the vector. First though, we need to work out if
642 this is a scalar to vector cast; code lifted from
643 valops.c:value_cast. */
644 enum type_code code1
, code2
;
645 struct type
*to_type
;
648 to_type
= check_typedef (type
);
650 code1
= TYPE_CODE (to_type
);
651 code2
= TYPE_CODE (check_typedef (value_type (arg
)));
653 if (code2
== TYPE_CODE_REF
)
654 code2
= TYPE_CODE (check_typedef (value_type (coerce_ref (arg
))));
656 scalar
= (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
657 || code2
== TYPE_CODE_CHAR
|| code2
== TYPE_CODE_FLT
658 || code2
== TYPE_CODE_DECFLOAT
|| code2
== TYPE_CODE_ENUM
659 || code2
== TYPE_CODE_RANGE
);
661 if (code1
== TYPE_CODE_ARRAY
&& TYPE_VECTOR (to_type
) && scalar
)
665 /* Cast to the element type of the vector here as
666 value_vector_widen will error if the scalar value is
667 truncated by the cast. To avoid the error, cast (and
668 possibly truncate) here. */
669 eltype
= check_typedef (TYPE_TARGET_TYPE (to_type
));
670 arg
= value_cast (eltype
, arg
);
672 return value_vector_widen (arg
, type
);
675 /* Standard cast handler. */
676 arg
= value_cast (type
, arg
);
681 /* Perform a relational operation on two operands. */
683 static struct value
*
684 opencl_relop (struct expression
*exp
, struct value
*arg1
, struct value
*arg2
,
688 struct type
*type1
= check_typedef (value_type (arg1
));
689 struct type
*type2
= check_typedef (value_type (arg2
));
690 int t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
691 && TYPE_VECTOR (type1
));
692 int t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
693 && TYPE_VECTOR (type2
));
695 if (!t1_is_vec
&& !t2_is_vec
)
697 int tmp
= scalar_relop (arg1
, arg2
, op
);
699 language_bool_type (exp
->language_defn
, exp
->gdbarch
);
701 val
= value_from_longest (type
, tmp
);
703 else if (t1_is_vec
&& t2_is_vec
)
705 val
= vector_relop (exp
, arg1
, arg2
, op
);
709 /* Widen the scalar operand to a vector. */
710 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
711 struct type
*t
= t1_is_vec
? type2
: type1
;
713 if (TYPE_CODE (t
) != TYPE_CODE_FLT
&& !is_integral_type (t
))
714 error (_("Argument to operation not a number or boolean."));
716 *v
= opencl_value_cast (t1_is_vec
? type1
: type2
, *v
);
717 val
= vector_relop (exp
, arg1
, arg2
, op
);
723 /* Expression evaluator for the OpenCL. Most operations are delegated to
724 evaluate_subexp_standard; see that function for a description of the
727 static struct value
*
728 evaluate_subexp_opencl (struct type
*expect_type
, struct expression
*exp
,
729 int *pos
, enum noside noside
)
731 enum exp_opcode op
= exp
->elts
[*pos
].opcode
;
732 struct value
*arg1
= NULL
;
733 struct value
*arg2
= NULL
;
734 struct type
*type1
, *type2
;
738 /* Handle assignment and cast operators to support OpenCL-style
739 scalar-to-vector widening. */
742 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
743 type1
= value_type (arg1
);
744 arg2
= evaluate_subexp (type1
, exp
, pos
, noside
);
746 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
749 if (deprecated_value_modifiable (arg1
)
750 && VALUE_LVAL (arg1
) != lval_internalvar
)
751 arg2
= opencl_value_cast (type1
, arg2
);
753 return value_assign (arg1
, arg2
);
756 type1
= exp
->elts
[*pos
+ 1].type
;
758 arg1
= evaluate_subexp (type1
, exp
, pos
, noside
);
760 if (noside
== EVAL_SKIP
)
761 return value_from_longest (builtin_type (exp
->gdbarch
)->
764 return opencl_value_cast (type1
, arg1
);
768 arg1
= evaluate_subexp (NULL
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
769 type1
= value_type (arg1
);
770 arg1
= evaluate_subexp (type1
, exp
, pos
, noside
);
772 if (noside
== EVAL_SKIP
)
773 return value_from_longest (builtin_type (exp
->gdbarch
)->
776 return opencl_value_cast (type1
, arg1
);
778 /* Handle binary relational and equality operators that are either not
779 or differently defined for GNU vectors. */
787 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
788 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
790 if (noside
== EVAL_SKIP
)
791 return value_from_longest (builtin_type (exp
->gdbarch
)->
794 return opencl_relop (exp
, arg1
, arg2
, op
);
796 /* Handle the logical unary operator not(!). */
797 case UNOP_LOGICAL_NOT
:
799 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
801 if (noside
== EVAL_SKIP
)
802 return value_from_longest (builtin_type (exp
->gdbarch
)->
805 return opencl_logical_not (exp
, arg1
);
807 /* Handle the logical operator and(&&) and or(||). */
808 case BINOP_LOGICAL_AND
:
809 case BINOP_LOGICAL_OR
:
811 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
813 if (noside
== EVAL_SKIP
)
815 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
817 return value_from_longest (builtin_type (exp
->gdbarch
)->
822 /* For scalar operations we need to avoid evaluating operands
823 unnecessarily. However, for vector operations we always need to
824 evaluate both operands. Unfortunately we only know which of the
825 two cases apply after we know the type of the second operand.
826 Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS. */
829 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
830 EVAL_AVOID_SIDE_EFFECTS
);
832 type1
= check_typedef (value_type (arg1
));
833 type2
= check_typedef (value_type (arg2
));
835 if ((TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
836 || (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
)))
838 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
840 return opencl_relop (exp
, arg1
, arg2
, op
);
844 /* For scalar built-in types, only evaluate the right
845 hand operand if the left hand operand compares
846 unequal(&&)/equal(||) to 0. */
848 int tmp
= value_logical_not (arg1
);
850 if (op
== BINOP_LOGICAL_OR
)
853 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
854 tmp
? EVAL_SKIP
: noside
);
855 type1
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
857 if (op
== BINOP_LOGICAL_AND
)
858 res
= !tmp
&& !value_logical_not (arg2
);
859 else /* BINOP_LOGICAL_OR */
860 res
= tmp
|| !value_logical_not (arg2
);
862 return value_from_longest (type1
, res
);
866 /* Handle the ternary selection operator. */
869 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
870 type1
= check_typedef (value_type (arg1
));
871 if (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
873 struct value
*arg3
, *tmp
, *ret
;
874 struct type
*eltype2
, *type3
, *eltype3
;
875 int t2_is_vec
, t3_is_vec
, i
;
876 LONGEST lowb1
, lowb2
, lowb3
, highb1
, highb2
, highb3
;
878 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
879 arg3
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
880 type2
= check_typedef (value_type (arg2
));
881 type3
= check_typedef (value_type (arg3
));
883 = TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
);
885 = TYPE_CODE (type3
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type3
);
887 /* Widen the scalar operand to a vector if necessary. */
888 if (t2_is_vec
|| !t3_is_vec
)
890 arg3
= opencl_value_cast (type2
, arg3
);
891 type3
= value_type (arg3
);
893 else if (!t2_is_vec
|| t3_is_vec
)
895 arg2
= opencl_value_cast (type3
, arg2
);
896 type2
= value_type (arg2
);
898 else if (!t2_is_vec
|| !t3_is_vec
)
900 /* Throw an error if arg2 or arg3 aren't vectors. */
902 Cannot perform conditional operation on incompatible types"));
905 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
906 eltype3
= check_typedef (TYPE_TARGET_TYPE (type3
));
908 if (!get_array_bounds (type1
, &lowb1
, &highb1
)
909 || !get_array_bounds (type2
, &lowb2
, &highb2
)
910 || !get_array_bounds (type3
, &lowb3
, &highb3
))
911 error (_("Could not determine the vector bounds"));
913 /* Throw an error if the types of arg2 or arg3 are incompatible. */
914 if (TYPE_CODE (eltype2
) != TYPE_CODE (eltype3
)
915 || TYPE_LENGTH (eltype2
) != TYPE_LENGTH (eltype3
)
916 || TYPE_UNSIGNED (eltype2
) != TYPE_UNSIGNED (eltype3
)
917 || lowb2
!= lowb3
|| highb2
!= highb3
)
919 Cannot perform operation on vectors with different types"));
921 /* Throw an error if the sizes of arg1 and arg2/arg3 differ. */
922 if (lowb1
!= lowb2
|| lowb1
!= lowb3
923 || highb1
!= highb2
|| highb1
!= highb3
)
925 Cannot perform conditional operation on vectors with different sizes"));
927 ret
= allocate_value (type2
);
929 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
931 tmp
= value_logical_not (value_subscript (arg1
, i
)) ?
932 value_subscript (arg3
, i
) : value_subscript (arg2
, i
);
933 memcpy (value_contents_writeable (ret
) +
934 i
* TYPE_LENGTH (eltype2
), value_contents_all (tmp
),
935 TYPE_LENGTH (eltype2
));
942 if (value_logical_not (arg1
))
944 /* Skip the second operand. */
945 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
947 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
951 /* Skip the third operand. */
952 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
953 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
959 /* Handle STRUCTOP_STRUCT to allow component access on OpenCL vectors. */
960 case STRUCTOP_STRUCT
:
963 int tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
965 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
966 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
967 type1
= check_typedef (value_type (arg1
));
969 if (noside
== EVAL_SKIP
)
971 return value_from_longest (builtin_type (exp
->gdbarch
)->
974 else if (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
976 return opencl_component_ref (exp
, arg1
, &exp
->elts
[pc
+ 2].string
,
981 struct value
*v
= value_struct_elt (&arg1
, NULL
,
982 &exp
->elts
[pc
+ 2].string
, NULL
,
985 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
986 v
= value_zero (value_type (v
), VALUE_LVAL (v
));
994 return evaluate_subexp_c (expect_type
, exp
, pos
, noside
);
997 /* Print OpenCL types. */
1000 opencl_print_type (struct type
*type
, const char *varstring
,
1001 struct ui_file
*stream
, int show
, int level
,
1002 const struct type_print_options
*flags
)
1004 /* We nearly always defer to C type printing, except that vector
1005 types are considered primitive in OpenCL, and should always
1006 be printed using their TYPE_NAME. */
1009 type
= check_typedef (type
);
1010 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
)
1011 && TYPE_NAME (type
) != NULL
)
1015 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
1019 opencl_language_arch_info (struct gdbarch
*gdbarch
,
1020 struct language_arch_info
*lai
)
1022 struct type
**types
= builtin_opencl_type (gdbarch
);
1024 /* Copy primitive types vector from gdbarch. */
1025 lai
->primitive_type_vector
= types
;
1027 /* Type of elements of strings. */
1028 lai
->string_char_type
= types
[opencl_primitive_type_char
];
1030 /* Specifies the return type of logical and relational operations. */
1031 lai
->bool_type_symbol
= "int";
1032 lai
->bool_type_default
= types
[opencl_primitive_type_int
];
1035 const struct exp_descriptor exp_descriptor_opencl
=
1037 print_subexp_standard
,
1038 operator_length_standard
,
1039 operator_check_standard
,
1041 dump_subexp_body_standard
,
1042 evaluate_subexp_opencl
1045 extern const struct language_defn opencl_language_defn
=
1047 "opencl", /* Language name */
1055 &exp_descriptor_opencl
,
1058 c_printchar
, /* Print a character constant */
1059 c_printstr
, /* Function to print string constant */
1060 c_emit_char
, /* Print a single char */
1061 opencl_print_type
, /* Print a type using appropriate syntax */
1062 c_print_typedef
, /* Print a typedef using appropriate syntax */
1063 c_val_print
, /* Print a value using appropriate syntax */
1064 c_value_print
, /* Print a top-level value */
1065 default_read_var_value
, /* la_read_var_value */
1066 NULL
, /* Language specific skip_trampoline */
1067 NULL
, /* name_of_this */
1068 false, /* la_store_sym_names_in_linkage_form_p */
1069 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
1070 basic_lookup_transparent_type
,/* lookup_transparent_type */
1071 NULL
, /* Language specific symbol demangler */
1073 NULL
, /* Language specific
1074 class_name_from_physname */
1075 c_op_print_tab
, /* expression operators for printing */
1076 1, /* c-style arrays */
1077 0, /* String lower bound */
1078 default_word_break_characters
,
1079 default_collect_symbol_completion_matches
,
1080 opencl_language_arch_info
,
1081 default_print_array_index
,
1082 default_pass_by_reference
,
1083 c_watch_location_expression
,
1084 NULL
, /* la_get_symbol_name_matcher */
1085 iterate_over_symbols
,
1086 default_search_name_hash
,
1087 &default_varobj_ops
,
1091 "{...}" /* la_struct_too_deep_ellipsis */
1095 build_opencl_types (struct gdbarch
*gdbarch
)
1098 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_opencl_primitive_types
+ 1,
1101 /* Helper macro to create strings. */
1102 #define OCL_STRING(S) #S
1103 /* This macro allocates and assigns the type struct pointers
1104 for the vector types. */
1105 #define BUILD_OCL_VTYPES(TYPE)\
1106 types[opencl_primitive_type_##TYPE##2] \
1107 = init_vector_type (types[opencl_primitive_type_##TYPE], 2); \
1108 TYPE_NAME (types[opencl_primitive_type_##TYPE##2]) = OCL_STRING(TYPE ## 2); \
1109 types[opencl_primitive_type_##TYPE##3] \
1110 = init_vector_type (types[opencl_primitive_type_##TYPE], 3); \
1111 TYPE_NAME (types[opencl_primitive_type_##TYPE##3]) = OCL_STRING(TYPE ## 3); \
1112 TYPE_LENGTH (types[opencl_primitive_type_##TYPE##3]) \
1113 = 4 * TYPE_LENGTH (types[opencl_primitive_type_##TYPE]); \
1114 types[opencl_primitive_type_##TYPE##4] \
1115 = init_vector_type (types[opencl_primitive_type_##TYPE], 4); \
1116 TYPE_NAME (types[opencl_primitive_type_##TYPE##4]) = OCL_STRING(TYPE ## 4); \
1117 types[opencl_primitive_type_##TYPE##8] \
1118 = init_vector_type (types[opencl_primitive_type_##TYPE], 8); \
1119 TYPE_NAME (types[opencl_primitive_type_##TYPE##8]) = OCL_STRING(TYPE ## 8); \
1120 types[opencl_primitive_type_##TYPE##16] \
1121 = init_vector_type (types[opencl_primitive_type_##TYPE], 16); \
1122 TYPE_NAME (types[opencl_primitive_type_##TYPE##16]) = OCL_STRING(TYPE ## 16)
1124 types
[opencl_primitive_type_char
]
1125 = arch_integer_type (gdbarch
, 8, 0, "char");
1126 BUILD_OCL_VTYPES (char);
1127 types
[opencl_primitive_type_uchar
]
1128 = arch_integer_type (gdbarch
, 8, 1, "uchar");
1129 BUILD_OCL_VTYPES (uchar
);
1130 types
[opencl_primitive_type_short
]
1131 = arch_integer_type (gdbarch
, 16, 0, "short");
1132 BUILD_OCL_VTYPES (short);
1133 types
[opencl_primitive_type_ushort
]
1134 = arch_integer_type (gdbarch
, 16, 1, "ushort");
1135 BUILD_OCL_VTYPES (ushort
);
1136 types
[opencl_primitive_type_int
]
1137 = arch_integer_type (gdbarch
, 32, 0, "int");
1138 BUILD_OCL_VTYPES (int);
1139 types
[opencl_primitive_type_uint
]
1140 = arch_integer_type (gdbarch
, 32, 1, "uint");
1141 BUILD_OCL_VTYPES (uint
);
1142 types
[opencl_primitive_type_long
]
1143 = arch_integer_type (gdbarch
, 64, 0, "long");
1144 BUILD_OCL_VTYPES (long);
1145 types
[opencl_primitive_type_ulong
]
1146 = arch_integer_type (gdbarch
, 64, 1, "ulong");
1147 BUILD_OCL_VTYPES (ulong
);
1148 types
[opencl_primitive_type_half
]
1149 = arch_float_type (gdbarch
, 16, "half", floatformats_ieee_half
);
1150 BUILD_OCL_VTYPES (half
);
1151 types
[opencl_primitive_type_float
]
1152 = arch_float_type (gdbarch
, 32, "float", floatformats_ieee_single
);
1153 BUILD_OCL_VTYPES (float);
1154 types
[opencl_primitive_type_double
]
1155 = arch_float_type (gdbarch
, 64, "double", floatformats_ieee_double
);
1156 BUILD_OCL_VTYPES (double);
1157 types
[opencl_primitive_type_bool
]
1158 = arch_boolean_type (gdbarch
, 8, 1, "bool");
1159 types
[opencl_primitive_type_unsigned_char
]
1160 = arch_integer_type (gdbarch
, 8, 1, "unsigned char");
1161 types
[opencl_primitive_type_unsigned_short
]
1162 = arch_integer_type (gdbarch
, 16, 1, "unsigned short");
1163 types
[opencl_primitive_type_unsigned_int
]
1164 = arch_integer_type (gdbarch
, 32, 1, "unsigned int");
1165 types
[opencl_primitive_type_unsigned_long
]
1166 = arch_integer_type (gdbarch
, 64, 1, "unsigned long");
1167 types
[opencl_primitive_type_size_t
]
1168 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "size_t");
1169 types
[opencl_primitive_type_ptrdiff_t
]
1170 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "ptrdiff_t");
1171 types
[opencl_primitive_type_intptr_t
]
1172 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "intptr_t");
1173 types
[opencl_primitive_type_uintptr_t
]
1174 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "uintptr_t");
1175 types
[opencl_primitive_type_void
]
1176 = arch_type (gdbarch
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, "void");
1182 _initialize_opencl_language (void)
1184 opencl_type_data
= gdbarch_data_register_post_init (build_opencl_types
);