1 /* OpenCL language support for GDB, the GNU debugger.
2 Copyright (C) 2010-2014 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/>. */
25 #include "expression.h"
26 #include "parser-defs.h"
31 #include "gdb_assert.h"
33 extern void _initialize_opencl_language (void);
35 /* This macro generates enum values from a given type. */
37 #define OCL_P_TYPE(TYPE)\
38 opencl_primitive_type_##TYPE,\
39 opencl_primitive_type_##TYPE##2,\
40 opencl_primitive_type_##TYPE##3,\
41 opencl_primitive_type_##TYPE##4,\
42 opencl_primitive_type_##TYPE##8,\
43 opencl_primitive_type_##TYPE##16
45 enum opencl_primitive_types
{
57 opencl_primitive_type_bool
,
58 opencl_primitive_type_unsigned_char
,
59 opencl_primitive_type_unsigned_short
,
60 opencl_primitive_type_unsigned_int
,
61 opencl_primitive_type_unsigned_long
,
62 opencl_primitive_type_size_t
,
63 opencl_primitive_type_ptrdiff_t
,
64 opencl_primitive_type_intptr_t
,
65 opencl_primitive_type_uintptr_t
,
66 opencl_primitive_type_void
,
67 nr_opencl_primitive_types
70 static struct gdbarch_data
*opencl_type_data
;
73 builtin_opencl_type (struct gdbarch
*gdbarch
)
75 return gdbarch_data (gdbarch
, opencl_type_data
);
78 /* Returns the corresponding OpenCL vector type from the given type code,
79 the length of the element type, the unsigned flag and the amount of
83 lookup_opencl_vector_type (struct gdbarch
*gdbarch
, enum type_code code
,
84 unsigned int el_length
, unsigned int flag_unsigned
,
89 struct type
*type
= NULL
;
90 struct type
**types
= builtin_opencl_type (gdbarch
);
92 /* Check if n describes a valid OpenCL vector size (2, 3, 4, 8, 16). */
93 if (n
!= 2 && n
!= 3 && n
!= 4 && n
!= 8 && n
!= 16)
94 error (_("Invalid OpenCL vector size: %d"), n
);
96 /* Triple vectors have the size of a quad vector. */
97 length
= (n
== 3) ? el_length
* 4 : el_length
* n
;
99 for (i
= 0; i
< nr_opencl_primitive_types
; i
++)
103 if (TYPE_CODE (types
[i
]) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (types
[i
])
104 && get_array_bounds (types
[i
], &lowb
, &highb
)
105 && TYPE_CODE (TYPE_TARGET_TYPE (types
[i
])) == code
106 && TYPE_UNSIGNED (TYPE_TARGET_TYPE (types
[i
])) == flag_unsigned
107 && TYPE_LENGTH (TYPE_TARGET_TYPE (types
[i
])) == el_length
108 && TYPE_LENGTH (types
[i
]) == length
109 && highb
- lowb
+ 1 == n
)
119 /* Returns nonzero if the array ARR contains duplicates within
120 the first N elements. */
123 array_has_dups (int *arr
, int n
)
127 for (i
= 0; i
< n
; i
++)
129 for (j
= i
+ 1; j
< n
; j
++)
131 if (arr
[i
] == arr
[j
])
139 /* The OpenCL component access syntax allows to create lvalues referring to
140 selected elements of an original OpenCL vector in arbitrary order. This
141 structure holds the information to describe such lvalues. */
145 /* Reference count. */
147 /* The number of indices. */
149 /* The element indices themselves. */
151 /* A pointer to the original value. */
155 /* Allocates an instance of struct lval_closure. */
157 static struct lval_closure
*
158 allocate_lval_closure (int *indices
, int n
, struct value
*val
)
160 struct lval_closure
*c
= XZALLOC (struct lval_closure
);
164 c
->indices
= XCALLOC (n
, int);
165 memcpy (c
->indices
, indices
, n
* sizeof (int));
166 value_incref (val
); /* Increment the reference counter of the value. */
173 lval_func_read (struct value
*v
)
175 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
176 struct type
*type
= check_typedef (value_type (v
));
177 struct type
*eltype
= TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)));
178 int offset
= value_offset (v
);
179 int elsize
= TYPE_LENGTH (eltype
);
184 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
185 && !get_array_bounds (type
, &lowb
, &highb
))
186 error (_("Could not determine the vector bounds"));
188 /* Assume elsize aligned offset. */
189 gdb_assert (offset
% elsize
== 0);
191 n
= offset
+ highb
- lowb
+ 1;
192 gdb_assert (n
<= c
->n
);
194 for (i
= offset
; i
< n
; i
++)
195 memcpy (value_contents_raw (v
) + j
++ * elsize
,
196 value_contents (c
->val
) + c
->indices
[i
] * elsize
,
201 lval_func_write (struct value
*v
, struct value
*fromval
)
203 struct value
*mark
= value_mark ();
204 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
205 struct type
*type
= check_typedef (value_type (v
));
206 struct type
*eltype
= TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)));
207 int offset
= value_offset (v
);
208 int elsize
= TYPE_LENGTH (eltype
);
213 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
214 && !get_array_bounds (type
, &lowb
, &highb
))
215 error (_("Could not determine the vector bounds"));
217 /* Assume elsize aligned offset. */
218 gdb_assert (offset
% elsize
== 0);
220 n
= offset
+ highb
- lowb
+ 1;
222 /* Since accesses to the fourth component of a triple vector is undefined we
223 just skip writes to the fourth element. Imagine something like this:
224 int3 i3 = (int3)(0, 1, 2);
226 In this case n would be 4 (offset=12/4 + 1) while c->n would be 3. */
230 for (i
= offset
; i
< n
; i
++)
232 struct value
*from_elm_val
= allocate_value (eltype
);
233 struct value
*to_elm_val
= value_subscript (c
->val
, c
->indices
[i
]);
235 memcpy (value_contents_writeable (from_elm_val
),
236 value_contents (fromval
) + j
++ * elsize
,
238 value_assign (to_elm_val
, from_elm_val
);
241 value_free_to_mark (mark
);
244 /* Return nonzero if all bits in V within OFFSET and LENGTH are valid. */
247 lval_func_check_validity (const struct value
*v
, int offset
, int length
)
249 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
250 /* Size of the target type in bits. */
252 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)))) * 8;
253 int startrest
= offset
% elsize
;
254 int start
= offset
/ elsize
;
255 int endrest
= (offset
+ length
) % elsize
;
256 int end
= (offset
+ length
) / elsize
;
265 for (i
= start
; i
< end
; i
++)
267 int comp_offset
= (i
== start
) ? startrest
: 0;
268 int comp_length
= (i
== end
) ? endrest
: elsize
;
270 if (!value_bits_valid (c
->val
, c
->indices
[i
] * elsize
+ comp_offset
,
278 /* Return nonzero if any bit in V is valid. */
281 lval_func_check_any_valid (const struct value
*v
)
283 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
284 /* Size of the target type in bits. */
286 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)))) * 8;
289 for (i
= 0; i
< c
->n
; i
++)
290 if (value_bits_valid (c
->val
, c
->indices
[i
] * elsize
, elsize
))
296 /* Return nonzero if bits in V from OFFSET and LENGTH represent a
297 synthetic pointer. */
300 lval_func_check_synthetic_pointer (const struct value
*v
,
301 int offset
, int length
)
303 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
304 /* Size of the target type in bits. */
306 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)))) * 8;
307 int startrest
= offset
% elsize
;
308 int start
= offset
/ elsize
;
309 int endrest
= (offset
+ length
) % elsize
;
310 int end
= (offset
+ length
) / elsize
;
319 for (i
= start
; i
< end
; i
++)
321 int comp_offset
= (i
== start
) ? startrest
: 0;
322 int comp_length
= (i
== end
) ? endrest
: elsize
;
324 if (!value_bits_synthetic_pointer (c
->val
,
325 c
->indices
[i
] * elsize
+ comp_offset
,
334 lval_func_copy_closure (const struct value
*v
)
336 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
344 lval_func_free_closure (struct value
*v
)
346 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
352 value_free (c
->val
); /* Decrement the reference counter of the value. */
358 static const struct lval_funcs opencl_value_funcs
=
362 lval_func_check_validity
,
363 lval_func_check_any_valid
,
365 NULL
, /* coerce_ref */
366 lval_func_check_synthetic_pointer
,
367 lval_func_copy_closure
,
368 lval_func_free_closure
371 /* Creates a sub-vector from VAL. The elements are selected by the indices of
372 an array with the length of N. Supported values for NOSIDE are
373 EVAL_NORMAL and EVAL_AVOID_SIDE_EFFECTS. */
375 static struct value
*
376 create_value (struct gdbarch
*gdbarch
, struct value
*val
, enum noside noside
,
379 struct type
*type
= check_typedef (value_type (val
));
380 struct type
*elm_type
= TYPE_TARGET_TYPE (type
);
383 /* Check if a single component of a vector is requested which means
384 the resulting type is a (primitive) scalar type. */
387 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
388 ret
= value_zero (elm_type
, not_lval
);
390 ret
= value_subscript (val
, indices
[0]);
394 /* Multiple components of the vector are requested which means the
395 resulting type is a vector as well. */
396 struct type
*dst_type
=
397 lookup_opencl_vector_type (gdbarch
, TYPE_CODE (elm_type
),
398 TYPE_LENGTH (elm_type
),
399 TYPE_UNSIGNED (elm_type
), n
);
401 if (dst_type
== NULL
)
402 dst_type
= init_vector_type (elm_type
, n
);
404 make_cv_type (TYPE_CONST (type
), TYPE_VOLATILE (type
), dst_type
, NULL
);
406 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
407 ret
= allocate_value (dst_type
);
410 /* Check whether to create a lvalue or not. */
411 if (VALUE_LVAL (val
) != not_lval
&& !array_has_dups (indices
, n
))
413 struct lval_closure
*c
= allocate_lval_closure (indices
, n
, val
);
414 ret
= allocate_computed_value (dst_type
, &opencl_value_funcs
, c
);
420 ret
= allocate_value (dst_type
);
422 /* Copy src val contents into the destination value. */
423 for (i
= 0; i
< n
; i
++)
424 memcpy (value_contents_writeable (ret
)
425 + (i
* TYPE_LENGTH (elm_type
)),
427 + (indices
[i
] * TYPE_LENGTH (elm_type
)),
428 TYPE_LENGTH (elm_type
));
435 /* OpenCL vector component access. */
437 static struct value
*
438 opencl_component_ref (struct expression
*exp
, struct value
*val
, char *comps
,
447 if (!get_array_bounds (check_typedef (value_type (val
)), &lowb
, &highb
))
448 error (_("Could not determine the vector bounds"));
450 src_len
= highb
- lowb
+ 1;
452 /* Throw an error if the amount of array elements does not fit a
453 valid OpenCL vector size (2, 3, 4, 8, 16). */
454 if (src_len
!= 2 && src_len
!= 3 && src_len
!= 4 && src_len
!= 8
456 error (_("Invalid OpenCL vector size"));
458 if (strcmp (comps
, "lo") == 0 )
460 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
462 for (i
= 0; i
< dst_len
; i
++)
465 else if (strcmp (comps
, "hi") == 0)
467 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
469 for (i
= 0; i
< dst_len
; i
++)
470 indices
[i
] = dst_len
+ i
;
472 else if (strcmp (comps
, "even") == 0)
474 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
476 for (i
= 0; i
< dst_len
; i
++)
479 else if (strcmp (comps
, "odd") == 0)
481 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
483 for (i
= 0; i
< dst_len
; i
++)
486 else if (strncasecmp (comps
, "s", 1) == 0)
488 #define HEXCHAR_TO_INT(C) ((C >= '0' && C <= '9') ? \
489 C-'0' : ((C >= 'A' && C <= 'F') ? \
490 C-'A'+10 : ((C >= 'a' && C <= 'f') ? \
493 dst_len
= strlen (comps
);
494 /* Skip the s/S-prefix. */
497 for (i
= 0; i
< dst_len
; i
++)
499 indices
[i
] = HEXCHAR_TO_INT(comps
[i
+1]);
500 /* Check if the requested component is invalid or exceeds
502 if (indices
[i
] < 0 || indices
[i
] >= src_len
)
503 error (_("Invalid OpenCL vector component accessor %s"), comps
);
508 dst_len
= strlen (comps
);
510 for (i
= 0; i
< dst_len
; i
++)
523 error (_("Invalid OpenCL vector component accessor %s"), comps
);
528 error (_("Invalid OpenCL vector component accessor %s"), comps
);
532 error (_("Invalid OpenCL vector component accessor %s"), comps
);
538 /* Throw an error if the amount of requested components does not
539 result in a valid length (1, 2, 3, 4, 8, 16). */
540 if (dst_len
!= 1 && dst_len
!= 2 && dst_len
!= 3 && dst_len
!= 4
541 && dst_len
!= 8 && dst_len
!= 16)
542 error (_("Invalid OpenCL vector component accessor %s"), comps
);
544 v
= create_value (exp
->gdbarch
, val
, noside
, indices
, dst_len
);
549 /* Perform the unary logical not (!) operation. */
551 static struct value
*
552 opencl_logical_not (struct expression
*exp
, struct value
*arg
)
554 struct type
*type
= check_typedef (value_type (arg
));
555 struct type
*rettype
;
558 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
560 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
564 if (!get_array_bounds (type
, &lowb
, &highb
))
565 error (_("Could not determine the vector bounds"));
567 /* Determine the resulting type of the operation and allocate the
569 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
570 TYPE_LENGTH (eltype
), 0,
572 ret
= allocate_value (rettype
);
574 for (i
= 0; i
< highb
- lowb
+ 1; i
++)
576 /* For vector types, the unary operator shall return a 0 if the
577 value of its operand compares unequal to 0, and -1 (i.e. all bits
578 set) if the value of its operand compares equal to 0. */
579 int tmp
= value_logical_not (value_subscript (arg
, i
)) ? -1 : 0;
580 memset (value_contents_writeable (ret
) + i
* TYPE_LENGTH (eltype
),
581 tmp
, TYPE_LENGTH (eltype
));
586 rettype
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
587 ret
= value_from_longest (rettype
, value_logical_not (arg
));
593 /* Perform a relational operation on two scalar operands. */
596 scalar_relop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
603 ret
= value_equal (val1
, val2
);
606 ret
= !value_equal (val1
, val2
);
609 ret
= value_less (val1
, val2
);
612 ret
= value_less (val2
, val1
);
615 ret
= value_less (val2
, val1
) || value_equal (val1
, val2
);
618 ret
= value_less (val1
, val2
) || value_equal (val1
, val2
);
620 case BINOP_LOGICAL_AND
:
621 ret
= !value_logical_not (val1
) && !value_logical_not (val2
);
623 case BINOP_LOGICAL_OR
:
624 ret
= !value_logical_not (val1
) || !value_logical_not (val2
);
627 error (_("Attempt to perform an unsupported operation"));
633 /* Perform a relational operation on two vector operands. */
635 static struct value
*
636 vector_relop (struct expression
*exp
, struct value
*val1
, struct value
*val2
,
640 struct type
*type1
, *type2
, *eltype1
, *eltype2
, *rettype
;
641 int t1_is_vec
, t2_is_vec
, i
;
642 LONGEST lowb1
, lowb2
, highb1
, highb2
;
644 type1
= check_typedef (value_type (val1
));
645 type2
= check_typedef (value_type (val2
));
647 t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
));
648 t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
));
650 if (!t1_is_vec
|| !t2_is_vec
)
651 error (_("Vector operations are not supported on scalar types"));
653 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
654 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
656 if (!get_array_bounds (type1
,&lowb1
, &highb1
)
657 || !get_array_bounds (type2
, &lowb2
, &highb2
))
658 error (_("Could not determine the vector bounds"));
660 /* Check whether the vector types are compatible. */
661 if (TYPE_CODE (eltype1
) != TYPE_CODE (eltype2
)
662 || TYPE_LENGTH (eltype1
) != TYPE_LENGTH (eltype2
)
663 || TYPE_UNSIGNED (eltype1
) != TYPE_UNSIGNED (eltype2
)
664 || lowb1
!= lowb2
|| highb1
!= highb2
)
665 error (_("Cannot perform operation on vectors with different types"));
667 /* Determine the resulting type of the operation and allocate the value. */
668 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
669 TYPE_LENGTH (eltype1
), 0,
671 ret
= allocate_value (rettype
);
673 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
675 /* For vector types, the relational, equality and logical operators shall
676 return 0 if the specified relation is false and -1 (i.e. all bits set)
677 if the specified relation is true. */
678 int tmp
= scalar_relop (value_subscript (val1
, i
),
679 value_subscript (val2
, i
), op
) ? -1 : 0;
680 memset (value_contents_writeable (ret
) + i
* TYPE_LENGTH (eltype1
),
681 tmp
, TYPE_LENGTH (eltype1
));
687 /* Perform a cast of ARG into TYPE. There's sadly a lot of duplication in
688 here from valops.c:value_cast, opencl is different only in the
689 behaviour of scalar to vector casting. As far as possibly we're going
690 to try and delegate back to the standard value_cast function. */
692 static struct value
*
693 opencl_value_cast (struct type
*type
, struct value
*arg
)
695 if (type
!= value_type (arg
))
697 /* Casting scalar to vector is a special case for OpenCL, scalar
698 is cast to element type of vector then replicated into each
699 element of the vector. First though, we need to work out if
700 this is a scalar to vector cast; code lifted from
701 valops.c:value_cast. */
702 enum type_code code1
, code2
;
703 struct type
*to_type
;
706 to_type
= check_typedef (type
);
708 code1
= TYPE_CODE (to_type
);
709 code2
= TYPE_CODE (check_typedef (value_type (arg
)));
711 if (code2
== TYPE_CODE_REF
)
712 code2
= TYPE_CODE (check_typedef (value_type (coerce_ref (arg
))));
714 scalar
= (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
715 || code2
== TYPE_CODE_CHAR
|| code2
== TYPE_CODE_FLT
716 || code2
== TYPE_CODE_DECFLOAT
|| code2
== TYPE_CODE_ENUM
717 || code2
== TYPE_CODE_RANGE
);
719 if (code1
== TYPE_CODE_ARRAY
&& TYPE_VECTOR (to_type
) && scalar
)
723 /* Cast to the element type of the vector here as
724 value_vector_widen will error if the scalar value is
725 truncated by the cast. To avoid the error, cast (and
726 possibly truncate) here. */
727 eltype
= check_typedef (TYPE_TARGET_TYPE (to_type
));
728 arg
= value_cast (eltype
, arg
);
730 return value_vector_widen (arg
, type
);
733 /* Standard cast handler. */
734 arg
= value_cast (type
, arg
);
739 /* Perform a relational operation on two operands. */
741 static struct value
*
742 opencl_relop (struct expression
*exp
, struct value
*arg1
, struct value
*arg2
,
746 struct type
*type1
= check_typedef (value_type (arg1
));
747 struct type
*type2
= check_typedef (value_type (arg2
));
748 int t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
749 && TYPE_VECTOR (type1
));
750 int t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
751 && TYPE_VECTOR (type2
));
753 if (!t1_is_vec
&& !t2_is_vec
)
755 int tmp
= scalar_relop (arg1
, arg2
, op
);
757 language_bool_type (exp
->language_defn
, exp
->gdbarch
);
759 val
= value_from_longest (type
, tmp
);
761 else if (t1_is_vec
&& t2_is_vec
)
763 val
= vector_relop (exp
, arg1
, arg2
, op
);
767 /* Widen the scalar operand to a vector. */
768 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
769 struct type
*t
= t1_is_vec
? type2
: type1
;
771 if (TYPE_CODE (t
) != TYPE_CODE_FLT
&& !is_integral_type (t
))
772 error (_("Argument to operation not a number or boolean."));
774 *v
= opencl_value_cast (t1_is_vec
? type1
: type2
, *v
);
775 val
= vector_relop (exp
, arg1
, arg2
, op
);
781 /* Expression evaluator for the OpenCL. Most operations are delegated to
782 evaluate_subexp_standard; see that function for a description of the
785 static struct value
*
786 evaluate_subexp_opencl (struct type
*expect_type
, struct expression
*exp
,
787 int *pos
, enum noside noside
)
789 enum exp_opcode op
= exp
->elts
[*pos
].opcode
;
790 struct value
*arg1
= NULL
;
791 struct value
*arg2
= NULL
;
792 struct type
*type1
, *type2
;
796 /* Handle assignment and cast operators to support OpenCL-style
797 scalar-to-vector widening. */
800 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
801 type1
= value_type (arg1
);
802 arg2
= evaluate_subexp (type1
, exp
, pos
, noside
);
804 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
807 if (deprecated_value_modifiable (arg1
)
808 && VALUE_LVAL (arg1
) != lval_internalvar
)
809 arg2
= opencl_value_cast (type1
, arg2
);
811 return value_assign (arg1
, arg2
);
814 type1
= exp
->elts
[*pos
+ 1].type
;
816 arg1
= evaluate_subexp (type1
, exp
, pos
, noside
);
818 if (noside
== EVAL_SKIP
)
819 return value_from_longest (builtin_type (exp
->gdbarch
)->
822 return opencl_value_cast (type1
, arg1
);
826 arg1
= evaluate_subexp (NULL
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
827 type1
= value_type (arg1
);
828 arg1
= evaluate_subexp (type1
, exp
, pos
, noside
);
830 if (noside
== EVAL_SKIP
)
831 return value_from_longest (builtin_type (exp
->gdbarch
)->
834 return opencl_value_cast (type1
, arg1
);
836 /* Handle binary relational and equality operators that are either not
837 or differently defined for GNU vectors. */
845 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
846 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
848 if (noside
== EVAL_SKIP
)
849 return value_from_longest (builtin_type (exp
->gdbarch
)->
852 return opencl_relop (exp
, arg1
, arg2
, op
);
854 /* Handle the logical unary operator not(!). */
855 case UNOP_LOGICAL_NOT
:
857 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
859 if (noside
== EVAL_SKIP
)
860 return value_from_longest (builtin_type (exp
->gdbarch
)->
863 return opencl_logical_not (exp
, arg1
);
865 /* Handle the logical operator and(&&) and or(||). */
866 case BINOP_LOGICAL_AND
:
867 case BINOP_LOGICAL_OR
:
869 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
871 if (noside
== EVAL_SKIP
)
873 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
875 return value_from_longest (builtin_type (exp
->gdbarch
)->
880 /* For scalar operations we need to avoid evaluating operands
881 unecessarily. However, for vector operations we always need to
882 evaluate both operands. Unfortunately we only know which of the
883 two cases apply after we know the type of the second operand.
884 Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS. */
887 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
888 EVAL_AVOID_SIDE_EFFECTS
);
890 type1
= check_typedef (value_type (arg1
));
891 type2
= check_typedef (value_type (arg2
));
893 if ((TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
894 || (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
)))
896 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
898 return opencl_relop (exp
, arg1
, arg2
, op
);
902 /* For scalar built-in types, only evaluate the right
903 hand operand if the left hand operand compares
904 unequal(&&)/equal(||) to 0. */
906 int tmp
= value_logical_not (arg1
);
908 if (op
== BINOP_LOGICAL_OR
)
911 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
912 tmp
? EVAL_SKIP
: noside
);
913 type1
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
915 if (op
== BINOP_LOGICAL_AND
)
916 res
= !tmp
&& !value_logical_not (arg2
);
917 else /* BINOP_LOGICAL_OR */
918 res
= tmp
|| !value_logical_not (arg2
);
920 return value_from_longest (type1
, res
);
924 /* Handle the ternary selection operator. */
927 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
928 type1
= check_typedef (value_type (arg1
));
929 if (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
931 struct value
*arg3
, *tmp
, *ret
;
932 struct type
*eltype2
, *type3
, *eltype3
;
933 int t2_is_vec
, t3_is_vec
, i
;
934 LONGEST lowb1
, lowb2
, lowb3
, highb1
, highb2
, highb3
;
936 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
937 arg3
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
938 type2
= check_typedef (value_type (arg2
));
939 type3
= check_typedef (value_type (arg3
));
941 = TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
);
943 = TYPE_CODE (type3
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type3
);
945 /* Widen the scalar operand to a vector if necessary. */
946 if (t2_is_vec
|| !t3_is_vec
)
948 arg3
= opencl_value_cast (type2
, arg3
);
949 type3
= value_type (arg3
);
951 else if (!t2_is_vec
|| t3_is_vec
)
953 arg2
= opencl_value_cast (type3
, arg2
);
954 type2
= value_type (arg2
);
956 else if (!t2_is_vec
|| !t3_is_vec
)
958 /* Throw an error if arg2 or arg3 aren't vectors. */
960 Cannot perform conditional operation on incompatible types"));
963 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
964 eltype3
= check_typedef (TYPE_TARGET_TYPE (type3
));
966 if (!get_array_bounds (type1
, &lowb1
, &highb1
)
967 || !get_array_bounds (type2
, &lowb2
, &highb2
)
968 || !get_array_bounds (type3
, &lowb3
, &highb3
))
969 error (_("Could not determine the vector bounds"));
971 /* Throw an error if the types of arg2 or arg3 are incompatible. */
972 if (TYPE_CODE (eltype2
) != TYPE_CODE (eltype3
)
973 || TYPE_LENGTH (eltype2
) != TYPE_LENGTH (eltype3
)
974 || TYPE_UNSIGNED (eltype2
) != TYPE_UNSIGNED (eltype3
)
975 || lowb2
!= lowb3
|| highb2
!= highb3
)
977 Cannot perform operation on vectors with different types"));
979 /* Throw an error if the sizes of arg1 and arg2/arg3 differ. */
980 if (lowb1
!= lowb2
|| lowb1
!= lowb3
981 || highb1
!= highb2
|| highb1
!= highb3
)
983 Cannot perform conditional operation on vectors with different sizes"));
985 ret
= allocate_value (type2
);
987 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
989 tmp
= value_logical_not (value_subscript (arg1
, i
)) ?
990 value_subscript (arg3
, i
) : value_subscript (arg2
, i
);
991 memcpy (value_contents_writeable (ret
) +
992 i
* TYPE_LENGTH (eltype2
), value_contents_all (tmp
),
993 TYPE_LENGTH (eltype2
));
1000 if (value_logical_not (arg1
))
1002 /* Skip the second operand. */
1003 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
1005 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1009 /* Skip the third operand. */
1010 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1011 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
1017 /* Handle STRUCTOP_STRUCT to allow component access on OpenCL vectors. */
1018 case STRUCTOP_STRUCT
:
1021 int tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1023 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1024 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1025 type1
= check_typedef (value_type (arg1
));
1027 if (noside
== EVAL_SKIP
)
1029 return value_from_longest (builtin_type (exp
->gdbarch
)->
1032 else if (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
1034 return opencl_component_ref (exp
, arg1
, &exp
->elts
[pc
+ 2].string
,
1039 struct value
*v
= value_struct_elt (&arg1
, NULL
,
1040 &exp
->elts
[pc
+ 2].string
, NULL
,
1043 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1044 v
= value_zero (value_type (v
), not_lval
);
1052 return evaluate_subexp_c (expect_type
, exp
, pos
, noside
);
1055 /* Print OpenCL types. */
1058 opencl_print_type (struct type
*type
, const char *varstring
,
1059 struct ui_file
*stream
, int show
, int level
,
1060 const struct type_print_options
*flags
)
1062 /* We nearly always defer to C type printing, except that vector
1063 types are considered primitive in OpenCL, and should always
1064 be printed using their TYPE_NAME. */
1067 CHECK_TYPEDEF (type
);
1068 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
)
1069 && TYPE_NAME (type
) != NULL
)
1073 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
1077 opencl_language_arch_info (struct gdbarch
*gdbarch
,
1078 struct language_arch_info
*lai
)
1080 struct type
**types
= builtin_opencl_type (gdbarch
);
1082 /* Copy primitive types vector from gdbarch. */
1083 lai
->primitive_type_vector
= types
;
1085 /* Type of elements of strings. */
1086 lai
->string_char_type
= types
[opencl_primitive_type_char
];
1088 /* Specifies the return type of logical and relational operations. */
1089 lai
->bool_type_symbol
= "int";
1090 lai
->bool_type_default
= types
[opencl_primitive_type_int
];
1093 const struct exp_descriptor exp_descriptor_opencl
=
1095 print_subexp_standard
,
1096 operator_length_standard
,
1097 operator_check_standard
,
1099 dump_subexp_body_standard
,
1100 evaluate_subexp_opencl
1103 const struct language_defn opencl_language_defn
=
1105 "opencl", /* Language name */
1112 &exp_descriptor_opencl
,
1116 c_printchar
, /* Print a character constant */
1117 c_printstr
, /* Function to print string constant */
1118 c_emit_char
, /* Print a single char */
1119 opencl_print_type
, /* Print a type using appropriate syntax */
1120 c_print_typedef
, /* Print a typedef using appropriate syntax */
1121 c_val_print
, /* Print a value using appropriate syntax */
1122 c_value_print
, /* Print a top-level value */
1123 default_read_var_value
, /* la_read_var_value */
1124 NULL
, /* Language specific skip_trampoline */
1125 NULL
, /* name_of_this */
1126 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
1127 basic_lookup_transparent_type
,/* lookup_transparent_type */
1128 NULL
, /* Language specific symbol demangler */
1129 NULL
, /* Language specific
1130 class_name_from_physname */
1131 c_op_print_tab
, /* expression operators for printing */
1132 1, /* c-style arrays */
1133 0, /* String lower bound */
1134 default_word_break_characters
,
1135 default_make_symbol_completion_list
,
1136 opencl_language_arch_info
,
1137 default_print_array_index
,
1138 default_pass_by_reference
,
1140 NULL
, /* la_get_symbol_name_cmp */
1141 iterate_over_symbols
,
1142 &default_varobj_ops
,
1147 build_opencl_types (struct gdbarch
*gdbarch
)
1150 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_opencl_primitive_types
+ 1,
1153 /* Helper macro to create strings. */
1154 #define OCL_STRING(S) #S
1155 /* This macro allocates and assigns the type struct pointers
1156 for the vector types. */
1157 #define BUILD_OCL_VTYPES(TYPE)\
1158 types[opencl_primitive_type_##TYPE##2] \
1159 = init_vector_type (types[opencl_primitive_type_##TYPE], 2); \
1160 TYPE_NAME (types[opencl_primitive_type_##TYPE##2]) = OCL_STRING(TYPE ## 2); \
1161 types[opencl_primitive_type_##TYPE##3] \
1162 = init_vector_type (types[opencl_primitive_type_##TYPE], 3); \
1163 TYPE_NAME (types[opencl_primitive_type_##TYPE##3]) = OCL_STRING(TYPE ## 3); \
1164 TYPE_LENGTH (types[opencl_primitive_type_##TYPE##3]) \
1165 = 4 * TYPE_LENGTH (types[opencl_primitive_type_##TYPE]); \
1166 types[opencl_primitive_type_##TYPE##4] \
1167 = init_vector_type (types[opencl_primitive_type_##TYPE], 4); \
1168 TYPE_NAME (types[opencl_primitive_type_##TYPE##4]) = OCL_STRING(TYPE ## 4); \
1169 types[opencl_primitive_type_##TYPE##8] \
1170 = init_vector_type (types[opencl_primitive_type_##TYPE], 8); \
1171 TYPE_NAME (types[opencl_primitive_type_##TYPE##8]) = OCL_STRING(TYPE ## 8); \
1172 types[opencl_primitive_type_##TYPE##16] \
1173 = init_vector_type (types[opencl_primitive_type_##TYPE], 16); \
1174 TYPE_NAME (types[opencl_primitive_type_##TYPE##16]) = OCL_STRING(TYPE ## 16)
1176 types
[opencl_primitive_type_char
]
1177 = arch_integer_type (gdbarch
, 8, 0, "char");
1178 BUILD_OCL_VTYPES (char);
1179 types
[opencl_primitive_type_uchar
]
1180 = arch_integer_type (gdbarch
, 8, 1, "uchar");
1181 BUILD_OCL_VTYPES (uchar
);
1182 types
[opencl_primitive_type_short
]
1183 = arch_integer_type (gdbarch
, 16, 0, "short");
1184 BUILD_OCL_VTYPES (short);
1185 types
[opencl_primitive_type_ushort
]
1186 = arch_integer_type (gdbarch
, 16, 1, "ushort");
1187 BUILD_OCL_VTYPES (ushort
);
1188 types
[opencl_primitive_type_int
]
1189 = arch_integer_type (gdbarch
, 32, 0, "int");
1190 BUILD_OCL_VTYPES (int);
1191 types
[opencl_primitive_type_uint
]
1192 = arch_integer_type (gdbarch
, 32, 1, "uint");
1193 BUILD_OCL_VTYPES (uint
);
1194 types
[opencl_primitive_type_long
]
1195 = arch_integer_type (gdbarch
, 64, 0, "long");
1196 BUILD_OCL_VTYPES (long);
1197 types
[opencl_primitive_type_ulong
]
1198 = arch_integer_type (gdbarch
, 64, 1, "ulong");
1199 BUILD_OCL_VTYPES (ulong
);
1200 types
[opencl_primitive_type_half
]
1201 = arch_float_type (gdbarch
, 16, "half", floatformats_ieee_half
);
1202 BUILD_OCL_VTYPES (half
);
1203 types
[opencl_primitive_type_float
]
1204 = arch_float_type (gdbarch
, 32, "float", floatformats_ieee_single
);
1205 BUILD_OCL_VTYPES (float);
1206 types
[opencl_primitive_type_double
]
1207 = arch_float_type (gdbarch
, 64, "double", floatformats_ieee_double
);
1208 BUILD_OCL_VTYPES (double);
1209 types
[opencl_primitive_type_bool
]
1210 = arch_boolean_type (gdbarch
, 8, 1, "bool");
1211 types
[opencl_primitive_type_unsigned_char
]
1212 = arch_integer_type (gdbarch
, 8, 1, "unsigned char");
1213 types
[opencl_primitive_type_unsigned_short
]
1214 = arch_integer_type (gdbarch
, 16, 1, "unsigned short");
1215 types
[opencl_primitive_type_unsigned_int
]
1216 = arch_integer_type (gdbarch
, 32, 1, "unsigned int");
1217 types
[opencl_primitive_type_unsigned_long
]
1218 = arch_integer_type (gdbarch
, 64, 1, "unsigned long");
1219 types
[opencl_primitive_type_size_t
]
1220 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "size_t");
1221 types
[opencl_primitive_type_ptrdiff_t
]
1222 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "ptrdiff_t");
1223 types
[opencl_primitive_type_intptr_t
]
1224 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "intptr_t");
1225 types
[opencl_primitive_type_uintptr_t
]
1226 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "uintptr_t");
1227 types
[opencl_primitive_type_void
]
1228 = arch_type (gdbarch
, TYPE_CODE_VOID
, 1, "void");
1233 /* Provide a prototype to silence -Wmissing-prototypes. */
1234 extern initialize_file_ftype _initialize_opencl_language
;
1237 _initialize_opencl_language (void)
1239 opencl_type_data
= gdbarch_data_register_post_init (build_opencl_types
);
1240 add_language (&opencl_language_defn
);