1 /* OpenCL language support for GDB, the GNU debugger.
2 Copyright (C) 2010-2012 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/>. */
22 #include "gdb_string.h"
25 #include "expression.h"
26 #include "parser-defs.h"
30 #include "gdb_assert.h"
32 extern void _initialize_opencl_language (void);
34 /* This macro generates enum values from a given type. */
36 #define OCL_P_TYPE(TYPE)\
37 opencl_primitive_type_##TYPE,\
38 opencl_primitive_type_##TYPE##2,\
39 opencl_primitive_type_##TYPE##3,\
40 opencl_primitive_type_##TYPE##4,\
41 opencl_primitive_type_##TYPE##8,\
42 opencl_primitive_type_##TYPE##16
44 enum opencl_primitive_types
{
56 opencl_primitive_type_bool
,
57 opencl_primitive_type_unsigned_char
,
58 opencl_primitive_type_unsigned_short
,
59 opencl_primitive_type_unsigned_int
,
60 opencl_primitive_type_unsigned_long
,
61 opencl_primitive_type_size_t
,
62 opencl_primitive_type_ptrdiff_t
,
63 opencl_primitive_type_intptr_t
,
64 opencl_primitive_type_uintptr_t
,
65 opencl_primitive_type_void
,
66 nr_opencl_primitive_types
69 static struct gdbarch_data
*opencl_type_data
;
72 builtin_opencl_type (struct gdbarch
*gdbarch
)
74 return gdbarch_data (gdbarch
, opencl_type_data
);
77 /* Returns the corresponding OpenCL vector type from the given type code,
78 the length of the element type, the unsigned flag and the amount of
82 lookup_opencl_vector_type (struct gdbarch
*gdbarch
, enum type_code code
,
83 unsigned int el_length
, unsigned int flag_unsigned
,
88 struct type
*type
= NULL
;
89 struct type
**types
= builtin_opencl_type (gdbarch
);
91 /* Check if n describes a valid OpenCL vector size (2, 3, 4, 8, 16). */
92 if (n
!= 2 && n
!= 3 && n
!= 4 && n
!= 8 && n
!= 16)
93 error (_("Invalid OpenCL vector size: %d"), n
);
95 /* Triple vectors have the size of a quad vector. */
96 length
= (n
== 3) ? el_length
* 4 : el_length
* n
;
98 for (i
= 0; i
< nr_opencl_primitive_types
; i
++)
102 if (TYPE_CODE (types
[i
]) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (types
[i
])
103 && get_array_bounds (types
[i
], &lowb
, &highb
)
104 && TYPE_CODE (TYPE_TARGET_TYPE (types
[i
])) == code
105 && TYPE_UNSIGNED (TYPE_TARGET_TYPE (types
[i
])) == flag_unsigned
106 && TYPE_LENGTH (TYPE_TARGET_TYPE (types
[i
])) == el_length
107 && TYPE_LENGTH (types
[i
]) == length
108 && highb
- lowb
+ 1 == n
)
118 /* Returns nonzero if the array ARR contains duplicates within
119 the first N elements. */
122 array_has_dups (int *arr
, int n
)
126 for (i
= 0; i
< n
; i
++)
128 for (j
= i
+ 1; j
< n
; j
++)
130 if (arr
[i
] == arr
[j
])
138 /* The OpenCL component access syntax allows to create lvalues referring to
139 selected elements of an original OpenCL vector in arbitrary order. This
140 structure holds the information to describe such lvalues. */
144 /* Reference count. */
146 /* The number of indices. */
148 /* The element indices themselves. */
150 /* A pointer to the original value. */
154 /* Allocates an instance of struct lval_closure. */
156 static struct lval_closure
*
157 allocate_lval_closure (int *indices
, int n
, struct value
*val
)
159 struct lval_closure
*c
= XZALLOC (struct lval_closure
);
163 c
->indices
= XCALLOC (n
, int);
164 memcpy (c
->indices
, indices
, n
* sizeof (int));
165 value_incref (val
); /* Increment the reference counter of the value. */
172 lval_func_read (struct value
*v
)
174 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
175 struct type
*type
= check_typedef (value_type (v
));
176 struct type
*eltype
= TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)));
177 int offset
= value_offset (v
);
178 int elsize
= TYPE_LENGTH (eltype
);
183 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
184 && !get_array_bounds (type
, &lowb
, &highb
))
185 error (_("Could not determine the vector bounds"));
187 /* Assume elsize aligned offset. */
188 gdb_assert (offset
% elsize
== 0);
190 n
= offset
+ highb
- lowb
+ 1;
191 gdb_assert (n
<= c
->n
);
193 for (i
= offset
; i
< n
; i
++)
194 memcpy (value_contents_raw (v
) + j
++ * elsize
,
195 value_contents (c
->val
) + c
->indices
[i
] * elsize
,
200 lval_func_write (struct value
*v
, struct value
*fromval
)
202 struct value
*mark
= value_mark ();
203 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
204 struct type
*type
= check_typedef (value_type (v
));
205 struct type
*eltype
= TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)));
206 int offset
= value_offset (v
);
207 int elsize
= TYPE_LENGTH (eltype
);
212 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
213 && !get_array_bounds (type
, &lowb
, &highb
))
214 error (_("Could not determine the vector bounds"));
216 /* Assume elsize aligned offset. */
217 gdb_assert (offset
% elsize
== 0);
219 n
= offset
+ highb
- lowb
+ 1;
221 /* Since accesses to the fourth component of a triple vector is undefined we
222 just skip writes to the fourth element. Imagine something like this:
223 int3 i3 = (int3)(0, 1, 2);
225 In this case n would be 4 (offset=12/4 + 1) while c->n would be 3. */
229 for (i
= offset
; i
< n
; i
++)
231 struct value
*from_elm_val
= allocate_value (eltype
);
232 struct value
*to_elm_val
= value_subscript (c
->val
, c
->indices
[i
]);
234 memcpy (value_contents_writeable (from_elm_val
),
235 value_contents (fromval
) + j
++ * elsize
,
237 value_assign (to_elm_val
, from_elm_val
);
240 value_free_to_mark (mark
);
243 /* Return nonzero if all bits in V within OFFSET and LENGTH are valid. */
246 lval_func_check_validity (const struct value
*v
, int offset
, int length
)
248 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
249 /* Size of the target type in bits. */
251 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)))) * 8;
252 int startrest
= offset
% elsize
;
253 int start
= offset
/ elsize
;
254 int endrest
= (offset
+ length
) % elsize
;
255 int end
= (offset
+ length
) / elsize
;
264 for (i
= start
; i
< end
; i
++)
266 int comp_offset
= (i
== start
) ? startrest
: 0;
267 int comp_length
= (i
== end
) ? endrest
: elsize
;
269 if (!value_bits_valid (c
->val
, c
->indices
[i
] * elsize
+ comp_offset
,
277 /* Return nonzero if any bit in V is valid. */
280 lval_func_check_any_valid (const struct value
*v
)
282 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
283 /* Size of the target type in bits. */
285 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)))) * 8;
288 for (i
= 0; i
< c
->n
; i
++)
289 if (value_bits_valid (c
->val
, c
->indices
[i
] * elsize
, elsize
))
295 /* Return nonzero if bits in V from OFFSET and LENGTH represent a
296 synthetic pointer. */
299 lval_func_check_synthetic_pointer (const struct value
*v
,
300 int offset
, int length
)
302 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
303 /* Size of the target type in bits. */
305 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)))) * 8;
306 int startrest
= offset
% elsize
;
307 int start
= offset
/ elsize
;
308 int endrest
= (offset
+ length
) % elsize
;
309 int end
= (offset
+ length
) / elsize
;
318 for (i
= start
; i
< end
; i
++)
320 int comp_offset
= (i
== start
) ? startrest
: 0;
321 int comp_length
= (i
== end
) ? endrest
: elsize
;
323 if (!value_bits_synthetic_pointer (c
->val
,
324 c
->indices
[i
] * elsize
+ comp_offset
,
333 lval_func_copy_closure (const struct value
*v
)
335 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
343 lval_func_free_closure (struct value
*v
)
345 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
351 value_free (c
->val
); /* Decrement the reference counter of the value. */
357 static const struct lval_funcs opencl_value_funcs
=
361 lval_func_check_validity
,
362 lval_func_check_any_valid
,
364 NULL
, /* coerce_ref */
365 lval_func_check_synthetic_pointer
,
366 lval_func_copy_closure
,
367 lval_func_free_closure
370 /* Creates a sub-vector from VAL. The elements are selected by the indices of
371 an array with the length of N. Supported values for NOSIDE are
372 EVAL_NORMAL and EVAL_AVOID_SIDE_EFFECTS. */
374 static struct value
*
375 create_value (struct gdbarch
*gdbarch
, struct value
*val
, enum noside noside
,
378 struct type
*type
= check_typedef (value_type (val
));
379 struct type
*elm_type
= TYPE_TARGET_TYPE (type
);
382 /* Check if a single component of a vector is requested which means
383 the resulting type is a (primitive) scalar type. */
386 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
387 ret
= value_zero (elm_type
, not_lval
);
389 ret
= value_subscript (val
, indices
[0]);
393 /* Multiple components of the vector are requested which means the
394 resulting type is a vector as well. */
395 struct type
*dst_type
=
396 lookup_opencl_vector_type (gdbarch
, TYPE_CODE (elm_type
),
397 TYPE_LENGTH (elm_type
),
398 TYPE_UNSIGNED (elm_type
), n
);
400 if (dst_type
== NULL
)
401 dst_type
= init_vector_type (elm_type
, n
);
403 make_cv_type (TYPE_CONST (type
), TYPE_VOLATILE (type
), dst_type
, NULL
);
405 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
406 ret
= allocate_value (dst_type
);
409 /* Check whether to create a lvalue or not. */
410 if (VALUE_LVAL (val
) != not_lval
&& !array_has_dups (indices
, n
))
412 struct lval_closure
*c
= allocate_lval_closure (indices
, n
, val
);
413 ret
= allocate_computed_value (dst_type
, &opencl_value_funcs
, c
);
419 ret
= allocate_value (dst_type
);
421 /* Copy src val contents into the destination value. */
422 for (i
= 0; i
< n
; i
++)
423 memcpy (value_contents_writeable (ret
)
424 + (i
* TYPE_LENGTH (elm_type
)),
426 + (indices
[i
] * TYPE_LENGTH (elm_type
)),
427 TYPE_LENGTH (elm_type
));
434 /* OpenCL vector component access. */
436 static struct value
*
437 opencl_component_ref (struct expression
*exp
, struct value
*val
, char *comps
,
446 if (!get_array_bounds (check_typedef (value_type (val
)), &lowb
, &highb
))
447 error (_("Could not determine the vector bounds"));
449 src_len
= highb
- lowb
+ 1;
451 /* Throw an error if the amount of array elements does not fit a
452 valid OpenCL vector size (2, 3, 4, 8, 16). */
453 if (src_len
!= 2 && src_len
!= 3 && src_len
!= 4 && src_len
!= 8
455 error (_("Invalid OpenCL vector size"));
457 if (strcmp (comps
, "lo") == 0 )
459 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
461 for (i
= 0; i
< dst_len
; i
++)
464 else if (strcmp (comps
, "hi") == 0)
466 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
468 for (i
= 0; i
< dst_len
; i
++)
469 indices
[i
] = dst_len
+ i
;
471 else if (strcmp (comps
, "even") == 0)
473 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
475 for (i
= 0; i
< dst_len
; i
++)
478 else if (strcmp (comps
, "odd") == 0)
480 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
482 for (i
= 0; i
< dst_len
; i
++)
485 else if (strncasecmp (comps
, "s", 1) == 0)
487 #define HEXCHAR_TO_INT(C) ((C >= '0' && C <= '9') ? \
488 C-'0' : ((C >= 'A' && C <= 'F') ? \
489 C-'A'+10 : ((C >= 'a' && C <= 'f') ? \
492 dst_len
= strlen (comps
);
493 /* Skip the s/S-prefix. */
496 for (i
= 0; i
< dst_len
; i
++)
498 indices
[i
] = HEXCHAR_TO_INT(comps
[i
+1]);
499 /* Check if the requested component is invalid or exceeds
501 if (indices
[i
] < 0 || indices
[i
] >= src_len
)
502 error (_("Invalid OpenCL vector component accessor %s"), comps
);
507 dst_len
= strlen (comps
);
509 for (i
= 0; i
< dst_len
; i
++)
522 error (_("Invalid OpenCL vector component accessor %s"), comps
);
527 error (_("Invalid OpenCL vector component accessor %s"), comps
);
531 error (_("Invalid OpenCL vector component accessor %s"), comps
);
537 /* Throw an error if the amount of requested components does not
538 result in a valid length (1, 2, 3, 4, 8, 16). */
539 if (dst_len
!= 1 && dst_len
!= 2 && dst_len
!= 3 && dst_len
!= 4
540 && dst_len
!= 8 && dst_len
!= 16)
541 error (_("Invalid OpenCL vector component accessor %s"), comps
);
543 v
= create_value (exp
->gdbarch
, val
, noside
, indices
, dst_len
);
548 /* Perform the unary logical not (!) operation. */
550 static struct value
*
551 opencl_logical_not (struct expression
*exp
, struct value
*arg
)
553 struct type
*type
= check_typedef (value_type (arg
));
554 struct type
*rettype
;
557 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
559 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
563 if (!get_array_bounds (type
, &lowb
, &highb
))
564 error (_("Could not determine the vector bounds"));
566 /* Determine the resulting type of the operation and allocate the
568 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
569 TYPE_LENGTH (eltype
), 0,
571 ret
= allocate_value (rettype
);
573 for (i
= 0; i
< highb
- lowb
+ 1; i
++)
575 /* For vector types, the unary operator shall return a 0 if the
576 value of its operand compares unequal to 0, and -1 (i.e. all bits
577 set) if the value of its operand compares equal to 0. */
578 int tmp
= value_logical_not (value_subscript (arg
, i
)) ? -1 : 0;
579 memset (value_contents_writeable (ret
) + i
* TYPE_LENGTH (eltype
),
580 tmp
, TYPE_LENGTH (eltype
));
585 rettype
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
586 ret
= value_from_longest (rettype
, value_logical_not (arg
));
592 /* Perform a relational operation on two scalar operands. */
595 scalar_relop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
602 ret
= value_equal (val1
, val2
);
605 ret
= !value_equal (val1
, val2
);
608 ret
= value_less (val1
, val2
);
611 ret
= value_less (val2
, val1
);
614 ret
= value_less (val2
, val1
) || value_equal (val1
, val2
);
617 ret
= value_less (val1
, val2
) || value_equal (val1
, val2
);
619 case BINOP_LOGICAL_AND
:
620 ret
= !value_logical_not (val1
) && !value_logical_not (val2
);
622 case BINOP_LOGICAL_OR
:
623 ret
= !value_logical_not (val1
) || !value_logical_not (val2
);
626 error (_("Attempt to perform an unsupported operation"));
632 /* Perform a relational operation on two vector operands. */
634 static struct value
*
635 vector_relop (struct expression
*exp
, struct value
*val1
, struct value
*val2
,
639 struct type
*type1
, *type2
, *eltype1
, *eltype2
, *rettype
;
640 int t1_is_vec
, t2_is_vec
, i
;
641 LONGEST lowb1
, lowb2
, highb1
, highb2
;
643 type1
= check_typedef (value_type (val1
));
644 type2
= check_typedef (value_type (val2
));
646 t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
));
647 t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
));
649 if (!t1_is_vec
|| !t2_is_vec
)
650 error (_("Vector operations are not supported on scalar types"));
652 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
653 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
655 if (!get_array_bounds (type1
,&lowb1
, &highb1
)
656 || !get_array_bounds (type2
, &lowb2
, &highb2
))
657 error (_("Could not determine the vector bounds"));
659 /* Check whether the vector types are compatible. */
660 if (TYPE_CODE (eltype1
) != TYPE_CODE (eltype2
)
661 || TYPE_LENGTH (eltype1
) != TYPE_LENGTH (eltype2
)
662 || TYPE_UNSIGNED (eltype1
) != TYPE_UNSIGNED (eltype2
)
663 || lowb1
!= lowb2
|| highb1
!= highb2
)
664 error (_("Cannot perform operation on vectors with different types"));
666 /* Determine the resulting type of the operation and allocate the value. */
667 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
668 TYPE_LENGTH (eltype1
), 0,
670 ret
= allocate_value (rettype
);
672 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
674 /* For vector types, the relational, equality and logical operators shall
675 return 0 if the specified relation is false and -1 (i.e. all bits set)
676 if the specified relation is true. */
677 int tmp
= scalar_relop (value_subscript (val1
, i
),
678 value_subscript (val2
, i
), op
) ? -1 : 0;
679 memset (value_contents_writeable (ret
) + i
* TYPE_LENGTH (eltype1
),
680 tmp
, TYPE_LENGTH (eltype1
));
686 /* Perform a relational operation on two operands. */
688 static struct value
*
689 opencl_relop (struct expression
*exp
, struct value
*arg1
, struct value
*arg2
,
693 struct type
*type1
= check_typedef (value_type (arg1
));
694 struct type
*type2
= check_typedef (value_type (arg2
));
695 int t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
696 && TYPE_VECTOR (type1
));
697 int t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
698 && TYPE_VECTOR (type2
));
700 if (!t1_is_vec
&& !t2_is_vec
)
702 int tmp
= scalar_relop (arg1
, arg2
, op
);
704 language_bool_type (exp
->language_defn
, exp
->gdbarch
);
706 val
= value_from_longest (type
, tmp
);
708 else if (t1_is_vec
&& t2_is_vec
)
710 val
= vector_relop (exp
, arg1
, arg2
, op
);
714 /* Widen the scalar operand to a vector. */
715 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
716 struct type
*t
= t1_is_vec
? type2
: type1
;
718 if (TYPE_CODE (t
) != TYPE_CODE_FLT
&& !is_integral_type (t
))
719 error (_("Argument to operation not a number or boolean."));
721 *v
= value_cast (t1_is_vec
? type1
: type2
, *v
);
722 val
= vector_relop (exp
, arg1
, arg2
, op
);
728 /* Expression evaluator for the OpenCL. Most operations are delegated to
729 evaluate_subexp_standard; see that function for a description of the
732 static struct value
*
733 evaluate_subexp_opencl (struct type
*expect_type
, struct expression
*exp
,
734 int *pos
, enum noside noside
)
736 enum exp_opcode op
= exp
->elts
[*pos
].opcode
;
737 struct value
*arg1
= NULL
;
738 struct value
*arg2
= NULL
;
739 struct type
*type1
, *type2
;
743 /* Handle binary relational and equality operators that are either not
744 or differently defined for GNU vectors. */
752 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
753 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
755 if (noside
== EVAL_SKIP
)
756 return value_from_longest (builtin_type (exp
->gdbarch
)->
759 return opencl_relop (exp
, arg1
, arg2
, op
);
761 /* Handle the logical unary operator not(!). */
762 case UNOP_LOGICAL_NOT
:
764 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
766 if (noside
== EVAL_SKIP
)
767 return value_from_longest (builtin_type (exp
->gdbarch
)->
770 return opencl_logical_not (exp
, arg1
);
772 /* Handle the logical operator and(&&) and or(||). */
773 case BINOP_LOGICAL_AND
:
774 case BINOP_LOGICAL_OR
:
776 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
778 if (noside
== EVAL_SKIP
)
780 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
782 return value_from_longest (builtin_type (exp
->gdbarch
)->
787 /* For scalar operations we need to avoid evaluating operands
788 unecessarily. However, for vector operations we always need to
789 evaluate both operands. Unfortunately we only know which of the
790 two cases apply after we know the type of the second operand.
791 Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS. */
794 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
795 EVAL_AVOID_SIDE_EFFECTS
);
797 type1
= check_typedef (value_type (arg1
));
798 type2
= check_typedef (value_type (arg2
));
800 if ((TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
801 || (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
)))
803 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
805 return opencl_relop (exp
, arg1
, arg2
, op
);
809 /* For scalar built-in types, only evaluate the right
810 hand operand if the left hand operand compares
811 unequal(&&)/equal(||) to 0. */
813 int tmp
= value_logical_not (arg1
);
815 if (op
== BINOP_LOGICAL_OR
)
818 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
819 tmp
? EVAL_SKIP
: noside
);
820 type1
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
822 if (op
== BINOP_LOGICAL_AND
)
823 res
= !tmp
&& !value_logical_not (arg2
);
824 else /* BINOP_LOGICAL_OR */
825 res
= tmp
|| !value_logical_not (arg2
);
827 return value_from_longest (type1
, res
);
831 /* Handle the ternary selection operator. */
834 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
835 type1
= check_typedef (value_type (arg1
));
836 if (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
838 struct value
*arg3
, *tmp
, *ret
;
839 struct type
*eltype2
, *type3
, *eltype3
;
840 int t2_is_vec
, t3_is_vec
, i
;
841 LONGEST lowb1
, lowb2
, lowb3
, highb1
, highb2
, highb3
;
843 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
844 arg3
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
845 type2
= check_typedef (value_type (arg2
));
846 type3
= check_typedef (value_type (arg3
));
848 = TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
);
850 = TYPE_CODE (type3
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type3
);
852 /* Widen the scalar operand to a vector if necessary. */
853 if (t2_is_vec
|| !t3_is_vec
)
855 arg3
= value_cast (type2
, arg3
);
856 type3
= value_type (arg3
);
858 else if (!t2_is_vec
|| t3_is_vec
)
860 arg2
= value_cast (type3
, arg2
);
861 type2
= value_type (arg2
);
863 else if (!t2_is_vec
|| !t3_is_vec
)
865 /* Throw an error if arg2 or arg3 aren't vectors. */
867 Cannot perform conditional operation on incompatible types"));
870 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
871 eltype3
= check_typedef (TYPE_TARGET_TYPE (type3
));
873 if (!get_array_bounds (type1
, &lowb1
, &highb1
)
874 || !get_array_bounds (type2
, &lowb2
, &highb2
)
875 || !get_array_bounds (type3
, &lowb3
, &highb3
))
876 error (_("Could not determine the vector bounds"));
878 /* Throw an error if the types of arg2 or arg3 are incompatible. */
879 if (TYPE_CODE (eltype2
) != TYPE_CODE (eltype3
)
880 || TYPE_LENGTH (eltype2
) != TYPE_LENGTH (eltype3
)
881 || TYPE_UNSIGNED (eltype2
) != TYPE_UNSIGNED (eltype3
)
882 || lowb2
!= lowb3
|| highb2
!= highb3
)
884 Cannot perform operation on vectors with different types"));
886 /* Throw an error if the sizes of arg1 and arg2/arg3 differ. */
887 if (lowb1
!= lowb2
|| lowb1
!= lowb3
888 || highb1
!= highb2
|| highb1
!= highb3
)
890 Cannot perform conditional operation on vectors with different sizes"));
892 ret
= allocate_value (type2
);
894 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
896 tmp
= value_logical_not (value_subscript (arg1
, i
)) ?
897 value_subscript (arg3
, i
) : value_subscript (arg2
, i
);
898 memcpy (value_contents_writeable (ret
) +
899 i
* TYPE_LENGTH (eltype2
), value_contents_all (tmp
),
900 TYPE_LENGTH (eltype2
));
907 if (value_logical_not (arg1
))
909 /* Skip the second operand. */
910 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
912 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
916 /* Skip the third operand. */
917 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
918 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
924 /* Handle STRUCTOP_STRUCT to allow component access on OpenCL vectors. */
925 case STRUCTOP_STRUCT
:
928 int tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
930 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
931 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
932 type1
= check_typedef (value_type (arg1
));
934 if (noside
== EVAL_SKIP
)
936 return value_from_longest (builtin_type (exp
->gdbarch
)->
939 else if (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
941 return opencl_component_ref (exp
, arg1
, &exp
->elts
[pc
+ 2].string
,
946 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
948 value_zero (lookup_struct_elt_type
949 (value_type (arg1
),&exp
->elts
[pc
+ 2].string
, 0),
952 return value_struct_elt (&arg1
, NULL
,
953 &exp
->elts
[pc
+ 2].string
, NULL
,
961 return evaluate_subexp_c (expect_type
, exp
, pos
, noside
);
965 opencl_language_arch_info (struct gdbarch
*gdbarch
,
966 struct language_arch_info
*lai
)
968 struct type
**types
= builtin_opencl_type (gdbarch
);
970 /* Copy primitive types vector from gdbarch. */
971 lai
->primitive_type_vector
= types
;
973 /* Type of elements of strings. */
974 lai
->string_char_type
= types
[opencl_primitive_type_char
];
976 /* Specifies the return type of logical and relational operations. */
977 lai
->bool_type_symbol
= "int";
978 lai
->bool_type_default
= types
[opencl_primitive_type_int
];
981 const struct exp_descriptor exp_descriptor_opencl
=
983 print_subexp_standard
,
984 operator_length_standard
,
985 operator_check_standard
,
987 dump_subexp_body_standard
,
988 evaluate_subexp_opencl
991 const struct language_defn opencl_language_defn
=
993 "opencl", /* Language name */
1000 &exp_descriptor_opencl
,
1004 c_printchar
, /* Print a character constant */
1005 c_printstr
, /* Function to print string constant */
1006 c_emit_char
, /* Print a single char */
1007 c_print_type
, /* Print a type using appropriate syntax */
1008 c_print_typedef
, /* Print a typedef using appropriate syntax */
1009 c_val_print
, /* Print a value using appropriate syntax */
1010 c_value_print
, /* Print a top-level value */
1011 NULL
, /* Language specific skip_trampoline */
1012 NULL
, /* name_of_this */
1013 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
1014 basic_lookup_transparent_type
,/* lookup_transparent_type */
1015 NULL
, /* Language specific symbol demangler */
1016 NULL
, /* Language specific
1017 class_name_from_physname */
1018 c_op_print_tab
, /* expression operators for printing */
1019 1, /* c-style arrays */
1020 0, /* String lower bound */
1021 default_word_break_characters
,
1022 default_make_symbol_completion_list
,
1023 opencl_language_arch_info
,
1024 default_print_array_index
,
1025 default_pass_by_reference
,
1027 NULL
, /* la_get_symbol_name_match_p */
1028 iterate_over_symbols
,
1033 build_opencl_types (struct gdbarch
*gdbarch
)
1036 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_opencl_primitive_types
+ 1,
1039 /* Helper macro to create strings. */
1040 #define OCL_STRING(S) #S
1041 /* This macro allocates and assigns the type struct pointers
1042 for the vector types. */
1043 #define BUILD_OCL_VTYPES(TYPE)\
1044 types[opencl_primitive_type_##TYPE##2] \
1045 = init_vector_type (types[opencl_primitive_type_##TYPE], 2); \
1046 TYPE_NAME (types[opencl_primitive_type_##TYPE##2]) = OCL_STRING(TYPE ## 2); \
1047 types[opencl_primitive_type_##TYPE##3] \
1048 = init_vector_type (types[opencl_primitive_type_##TYPE], 3); \
1049 TYPE_NAME (types[opencl_primitive_type_##TYPE##3]) = OCL_STRING(TYPE ## 3); \
1050 TYPE_LENGTH (types[opencl_primitive_type_##TYPE##3]) \
1051 = 4 * TYPE_LENGTH (types[opencl_primitive_type_##TYPE]); \
1052 types[opencl_primitive_type_##TYPE##4] \
1053 = init_vector_type (types[opencl_primitive_type_##TYPE], 4); \
1054 TYPE_NAME (types[opencl_primitive_type_##TYPE##4]) = OCL_STRING(TYPE ## 4); \
1055 types[opencl_primitive_type_##TYPE##8] \
1056 = init_vector_type (types[opencl_primitive_type_##TYPE], 8); \
1057 TYPE_NAME (types[opencl_primitive_type_##TYPE##8]) = OCL_STRING(TYPE ## 8); \
1058 types[opencl_primitive_type_##TYPE##16] \
1059 = init_vector_type (types[opencl_primitive_type_##TYPE], 16); \
1060 TYPE_NAME (types[opencl_primitive_type_##TYPE##16]) = OCL_STRING(TYPE ## 16)
1062 types
[opencl_primitive_type_char
]
1063 = arch_integer_type (gdbarch
, 8, 0, "char");
1064 BUILD_OCL_VTYPES (char);
1065 types
[opencl_primitive_type_uchar
]
1066 = arch_integer_type (gdbarch
, 8, 1, "uchar");
1067 BUILD_OCL_VTYPES (uchar
);
1068 types
[opencl_primitive_type_short
]
1069 = arch_integer_type (gdbarch
, 16, 0, "short");
1070 BUILD_OCL_VTYPES (short);
1071 types
[opencl_primitive_type_ushort
]
1072 = arch_integer_type (gdbarch
, 16, 1, "ushort");
1073 BUILD_OCL_VTYPES (ushort
);
1074 types
[opencl_primitive_type_int
]
1075 = arch_integer_type (gdbarch
, 32, 0, "int");
1076 BUILD_OCL_VTYPES (int);
1077 types
[opencl_primitive_type_uint
]
1078 = arch_integer_type (gdbarch
, 32, 1, "uint");
1079 BUILD_OCL_VTYPES (uint
);
1080 types
[opencl_primitive_type_long
]
1081 = arch_integer_type (gdbarch
, 64, 0, "long");
1082 BUILD_OCL_VTYPES (long);
1083 types
[opencl_primitive_type_ulong
]
1084 = arch_integer_type (gdbarch
, 64, 1, "ulong");
1085 BUILD_OCL_VTYPES (ulong
);
1086 types
[opencl_primitive_type_half
]
1087 = arch_float_type (gdbarch
, 16, "half", floatformats_ieee_half
);
1088 BUILD_OCL_VTYPES (half
);
1089 types
[opencl_primitive_type_float
]
1090 = arch_float_type (gdbarch
, 32, "float", floatformats_ieee_single
);
1091 BUILD_OCL_VTYPES (float);
1092 types
[opencl_primitive_type_double
]
1093 = arch_float_type (gdbarch
, 64, "double", floatformats_ieee_double
);
1094 BUILD_OCL_VTYPES (double);
1095 types
[opencl_primitive_type_bool
]
1096 = arch_boolean_type (gdbarch
, 8, 1, "bool");
1097 types
[opencl_primitive_type_unsigned_char
]
1098 = arch_integer_type (gdbarch
, 8, 1, "unsigned char");
1099 types
[opencl_primitive_type_unsigned_short
]
1100 = arch_integer_type (gdbarch
, 16, 1, "unsigned short");
1101 types
[opencl_primitive_type_unsigned_int
]
1102 = arch_integer_type (gdbarch
, 32, 1, "unsigned int");
1103 types
[opencl_primitive_type_unsigned_long
]
1104 = arch_integer_type (gdbarch
, 64, 1, "unsigned long");
1105 types
[opencl_primitive_type_size_t
]
1106 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "size_t");
1107 types
[opencl_primitive_type_ptrdiff_t
]
1108 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "ptrdiff_t");
1109 types
[opencl_primitive_type_intptr_t
]
1110 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "intptr_t");
1111 types
[opencl_primitive_type_uintptr_t
]
1112 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "uintptr_t");
1113 types
[opencl_primitive_type_void
]
1114 = arch_type (gdbarch
, TYPE_CODE_VOID
, 1, "void");
1120 _initialize_opencl_language (void)
1122 opencl_type_data
= gdbarch_data_register_post_init (build_opencl_types
);
1123 add_language (&opencl_language_defn
);