1 /* OpenCL language support for GDB, the GNU debugger.
2 Copyright (C) 2010 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 /* This macro generates the type struct declarations from a given type. */
71 #define STRUCT_OCL_TYPE(TYPE)\
72 struct type *builtin_##TYPE;\
73 struct type *builtin_##TYPE##2;\
74 struct type *builtin_##TYPE##3;\
75 struct type *builtin_##TYPE##4;\
76 struct type *builtin_##TYPE##8;\
77 struct type *builtin_##TYPE##16
79 struct builtin_opencl_type
81 STRUCT_OCL_TYPE (char);
82 STRUCT_OCL_TYPE (uchar
);
83 STRUCT_OCL_TYPE (short);
84 STRUCT_OCL_TYPE (ushort
);
85 STRUCT_OCL_TYPE (int);
86 STRUCT_OCL_TYPE (uint
);
87 STRUCT_OCL_TYPE (long);
88 STRUCT_OCL_TYPE (ulong
);
89 STRUCT_OCL_TYPE (half
);
90 STRUCT_OCL_TYPE (float);
91 STRUCT_OCL_TYPE (double);
92 struct type
*builtin_bool
;
93 struct type
*builtin_unsigned_char
;
94 struct type
*builtin_unsigned_short
;
95 struct type
*builtin_unsigned_int
;
96 struct type
*builtin_unsigned_long
;
97 struct type
*builtin_size_t
;
98 struct type
*builtin_ptrdiff_t
;
99 struct type
*builtin_intptr_t
;
100 struct type
*builtin_uintptr_t
;
101 struct type
*builtin_void
;
104 static struct gdbarch_data
*opencl_type_data
;
106 const struct builtin_opencl_type
*
107 builtin_opencl_type (struct gdbarch
*gdbarch
)
109 return gdbarch_data (gdbarch
, opencl_type_data
);
112 /* Returns the corresponding OpenCL vector type from the given type code,
113 the length of the element type, the unsigned flag and the amount of
117 lookup_opencl_vector_type (struct gdbarch
*gdbarch
, enum type_code code
,
118 unsigned int el_length
, unsigned int flag_unsigned
,
123 struct type
*type
= NULL
;
124 struct type
**types
= (struct type
**) builtin_opencl_type (gdbarch
);
126 /* Check if n describes a valid OpenCL vector size (2, 3, 4, 8, 16). */
127 if (n
!= 2 && n
!= 3 && n
!= 4 && n
!= 8 && n
!= 16)
128 error (_("Invalid OpenCL vector size: %d"), n
);
130 /* Triple vectors have the size of a quad vector. */
131 length
= (n
== 3) ? el_length
* 4 : el_length
* n
;
133 for (i
= 0; i
< nr_opencl_primitive_types
; i
++)
137 if (TYPE_CODE (types
[i
]) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (types
[i
])
138 && get_array_bounds (types
[i
], &lowb
, &highb
)
139 && TYPE_CODE (TYPE_TARGET_TYPE (types
[i
])) == code
140 && TYPE_UNSIGNED (TYPE_TARGET_TYPE (types
[i
])) == flag_unsigned
141 && TYPE_LENGTH (TYPE_TARGET_TYPE (types
[i
])) == el_length
142 && TYPE_LENGTH (types
[i
]) == length
143 && highb
- lowb
+ 1 == n
)
153 /* Returns nonzero if the array ARR contains duplicates within
154 the first N elements. */
157 array_has_dups (int *arr
, int n
)
161 for (i
= 0; i
< n
; i
++)
163 for (j
= i
+ 1; j
< n
; j
++)
165 if (arr
[i
] == arr
[j
])
173 /* The OpenCL component access syntax allows to create lvalues referring to
174 selected elements of an original OpenCL vector in arbitrary order. This
175 structure holds the information to describe such lvalues. */
179 /* Reference count. */
181 /* The number of indices. */
183 /* The element indices themselves. */
185 /* A pointer to the original value. */
189 /* Allocates an instance of struct lval_closure. */
191 static struct lval_closure
*
192 allocate_lval_closure (int *indices
, int n
, struct value
*val
)
194 struct lval_closure
*c
= XZALLOC (struct lval_closure
);
198 c
->indices
= XCALLOC (n
, int);
199 memcpy (c
->indices
, indices
, n
* sizeof (int));
200 value_incref (val
); /* Increment the reference counter of the value. */
207 lval_func_read (struct value
*v
)
209 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
210 struct type
*type
= check_typedef (value_type (v
));
211 struct type
*eltype
= TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)));
212 int offset
= value_offset (v
);
213 int elsize
= TYPE_LENGTH (eltype
);
218 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
219 && !get_array_bounds (type
, &lowb
, &highb
))
220 error (_("Could not determine the vector bounds"));
222 /* Assume elsize aligned offset. */
223 gdb_assert (offset
% elsize
== 0);
225 n
= offset
+ highb
- lowb
+ 1;
226 gdb_assert (n
<= c
->n
);
228 for (i
= offset
; i
< n
; i
++)
229 memcpy (value_contents_raw (v
) + j
++ * elsize
,
230 value_contents (c
->val
) + c
->indices
[i
] * elsize
,
235 lval_func_write (struct value
*v
, struct value
*fromval
)
237 struct value
*mark
= value_mark ();
238 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
239 struct type
*type
= check_typedef (value_type (v
));
240 struct type
*eltype
= TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)));
241 int offset
= value_offset (v
);
242 int elsize
= TYPE_LENGTH (eltype
);
247 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
248 && !get_array_bounds (type
, &lowb
, &highb
))
249 error (_("Could not determine the vector bounds"));
251 /* Assume elsize aligned offset. */
252 gdb_assert (offset
% elsize
== 0);
254 n
= offset
+ highb
- lowb
+ 1;
256 /* Since accesses to the fourth component of a triple vector is undefined we
257 just skip writes to the fourth element. Imagine something like this:
258 int3 i3 = (int3)(0, 1, 2);
260 In this case n would be 4 (offset=12/4 + 1) while c->n would be 3. */
264 for (i
= offset
; i
< n
; i
++)
266 struct value
*from_elm_val
= allocate_value (eltype
);
267 struct value
*to_elm_val
= value_subscript (c
->val
, c
->indices
[i
]);
269 memcpy (value_contents_writeable (from_elm_val
),
270 value_contents (fromval
) + j
++ * elsize
,
272 value_assign (to_elm_val
, from_elm_val
);
275 value_free_to_mark (mark
);
278 /* Return nonzero if all bits in V within OFFSET and LENGTH are valid. */
281 lval_func_check_validity (const struct value
*v
, int offset
, int length
)
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;
287 int startrest
= offset
% elsize
;
288 int start
= offset
/ elsize
;
289 int endrest
= (offset
+ length
) % elsize
;
290 int end
= (offset
+ length
) / elsize
;
299 for (i
= start
; i
< end
; i
++)
301 int startoffset
= (i
== start
) ? startrest
: 0;
302 int length
= (i
== end
) ? endrest
: elsize
;
304 if (!value_bits_valid (c
->val
, c
->indices
[i
] * elsize
+ startoffset
,
312 /* Return nonzero if any bit in V is valid. */
315 lval_func_check_any_valid (const struct value
*v
)
317 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
318 /* Size of the target type in bits. */
320 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)))) * 8;
323 for (i
= 0; i
< c
->n
; i
++)
324 if (value_bits_valid (c
->val
, c
->indices
[i
] * elsize
, elsize
))
331 lval_func_copy_closure (const struct value
*v
)
333 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
341 lval_func_free_closure (struct value
*v
)
343 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
351 value_free (c
->val
); /* Decrement the reference counter of the value. */
355 static struct lval_funcs opencl_value_funcs
=
359 lval_func_check_validity
,
360 lval_func_check_any_valid
,
361 lval_func_copy_closure
,
362 lval_func_free_closure
365 /* Creates a sub-vector from VAL. The elements are selected by the indices of
366 an array with the length of N. Supported values for NOSIDE are
367 EVAL_NORMAL and EVAL_AVOID_SIDE_EFFECTS. */
369 static struct value
*
370 create_value (struct gdbarch
*gdbarch
, struct value
*val
, enum noside noside
,
373 struct type
*type
= check_typedef (value_type (val
));
374 struct type
*elm_type
= TYPE_TARGET_TYPE (type
);
377 /* Check if a single component of a vector is requested which means
378 the resulting type is a (primitive) scalar type. */
381 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
382 ret
= value_zero (elm_type
, not_lval
);
384 ret
= value_subscript (val
, indices
[0]);
388 /* Multiple components of the vector are requested which means the
389 resulting type is a vector as well. */
390 struct type
*dst_type
=
391 lookup_opencl_vector_type (gdbarch
, TYPE_CODE (elm_type
),
392 TYPE_LENGTH (elm_type
),
393 TYPE_UNSIGNED (elm_type
), n
);
395 if (dst_type
== NULL
)
396 dst_type
= init_vector_type (elm_type
, n
);
398 make_cv_type (TYPE_CONST (type
), TYPE_VOLATILE (type
), dst_type
, NULL
);
400 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
401 ret
= allocate_value (dst_type
);
404 /* Check whether to create a lvalue or not. */
405 if (VALUE_LVAL (val
) != not_lval
&& !array_has_dups (indices
, n
))
407 struct lval_closure
*c
= allocate_lval_closure (indices
, n
, val
);
408 ret
= allocate_computed_value (dst_type
, &opencl_value_funcs
, c
);
414 ret
= allocate_value (dst_type
);
416 /* Copy src val contents into the destination value. */
417 for (i
= 0; i
< n
; i
++)
418 memcpy (value_contents_writeable (ret
)
419 + (i
* TYPE_LENGTH (elm_type
)),
421 + (indices
[i
] * TYPE_LENGTH (elm_type
)),
422 TYPE_LENGTH (elm_type
));
429 /* OpenCL vector component access. */
431 static struct value
*
432 opencl_component_ref (struct expression
*exp
, struct value
*val
, char *comps
,
441 if (!get_array_bounds (check_typedef (value_type (val
)), &lowb
, &highb
))
442 error (_("Could not determine the vector bounds"));
444 src_len
= highb
- lowb
+ 1;
446 /* Throw an error if the amount of array elements does not fit a
447 valid OpenCL vector size (2, 3, 4, 8, 16). */
448 if (src_len
!= 2 && src_len
!= 3 && src_len
!= 4 && src_len
!= 8
450 error (_("Invalid OpenCL vector size"));
452 if (strcmp (comps
, "lo") == 0 )
454 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
456 for (i
= 0; i
< dst_len
; i
++)
459 else if (strcmp (comps
, "hi") == 0)
461 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
463 for (i
= 0; i
< dst_len
; i
++)
464 indices
[i
] = dst_len
+ i
;
466 else if (strcmp (comps
, "even") == 0)
468 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
470 for (i
= 0; i
< dst_len
; i
++)
473 else if (strcmp (comps
, "odd") == 0)
475 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
477 for (i
= 0; i
< dst_len
; i
++)
480 else if (strncasecmp (comps
, "s", 1) == 0)
482 #define HEXCHAR_TO_INT(C) ((C >= '0' && C <= '9') ? \
483 C-'0' : ((C >= 'A' && C <= 'F') ? \
484 C-'A'+10 : ((C >= 'a' && C <= 'f') ? \
487 dst_len
= strlen (comps
);
488 /* Skip the s/S-prefix. */
491 for (i
= 0; i
< dst_len
; i
++)
493 indices
[i
] = HEXCHAR_TO_INT(comps
[i
+1]);
494 /* Check if the requested component is invalid or exceeds
496 if (indices
[i
] < 0 || indices
[i
] >= src_len
)
497 error (_("Invalid OpenCL vector component accessor %s"), comps
);
502 dst_len
= strlen (comps
);
504 for (i
= 0; i
< dst_len
; i
++)
517 error (_("Invalid OpenCL vector component accessor %s"), comps
);
522 error (_("Invalid OpenCL vector component accessor %s"), comps
);
526 error (_("Invalid OpenCL vector component accessor %s"), comps
);
532 /* Throw an error if the amount of requested components does not
533 result in a valid length (1, 2, 3, 4, 8, 16). */
534 if (dst_len
!= 1 && dst_len
!= 2 && dst_len
!= 3 && dst_len
!= 4
535 && dst_len
!= 8 && dst_len
!= 16)
536 error (_("Invalid OpenCL vector component accessor %s"), comps
);
538 v
= create_value (exp
->gdbarch
, val
, noside
, indices
, dst_len
);
543 /* Perform the unary logical not (!) operation. */
545 static struct value
*
546 opencl_logical_not (struct expression
*exp
, struct value
*arg
)
548 struct type
*type
= check_typedef (value_type (arg
));
549 struct type
*rettype
;
552 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
554 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
558 if (!get_array_bounds (type
, &lowb
, &highb
))
559 error (_("Could not determine the vector bounds"));
561 /* Determine the resulting type of the operation and allocate the
563 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
564 TYPE_LENGTH (eltype
), 0,
566 ret
= allocate_value (rettype
);
568 for (i
= 0; i
< highb
- lowb
+ 1; i
++)
570 /* For vector types, the unary operator shall return a 0 if the
571 value of its operand compares unequal to 0, and -1 (i.e. all bits
572 set) if the value of its operand compares equal to 0. */
573 int tmp
= value_logical_not (value_subscript (arg
, i
)) ? -1 : 0;
574 memset (value_contents_writeable (ret
) + i
* TYPE_LENGTH (eltype
),
575 tmp
, TYPE_LENGTH (eltype
));
580 rettype
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
581 ret
= value_from_longest (rettype
, value_logical_not (arg
));
587 /* Perform a relational operation on two scalar operands. */
590 scalar_relop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
597 ret
= value_equal (val1
, val2
);
600 ret
= !value_equal (val1
, val2
);
603 ret
= value_less (val1
, val2
);
606 ret
= value_less (val2
, val1
);
609 ret
= value_less (val2
, val1
) || value_equal (val1
, val2
);
612 ret
= value_less (val1
, val2
) || value_equal (val1
, val2
);
614 case BINOP_LOGICAL_AND
:
615 ret
= !value_logical_not (val1
) && !value_logical_not (val2
);
617 case BINOP_LOGICAL_OR
:
618 ret
= !value_logical_not (val1
) || !value_logical_not (val2
);
621 error (_("Attempt to perform an unsupported operation"));
627 /* Perform a relational operation on two vector operands. */
629 static struct value
*
630 vector_relop (struct expression
*exp
, struct value
*val1
, struct value
*val2
,
634 struct type
*type1
, *type2
, *eltype1
, *eltype2
, *rettype
;
635 int t1_is_vec
, t2_is_vec
, i
;
636 LONGEST lowb1
, lowb2
, highb1
, highb2
;
638 type1
= check_typedef (value_type (val1
));
639 type2
= check_typedef (value_type (val2
));
641 t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
));
642 t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
));
644 if (!t1_is_vec
|| !t2_is_vec
)
645 error (_("Vector operations are not supported on scalar types"));
647 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
648 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
650 if (!get_array_bounds (type1
,&lowb1
, &highb1
)
651 || !get_array_bounds (type2
, &lowb2
, &highb2
))
652 error (_("Could not determine the vector bounds"));
654 /* Check whether the vector types are compatible. */
655 if (TYPE_CODE (eltype1
) != TYPE_CODE (eltype2
)
656 || TYPE_LENGTH (eltype1
) != TYPE_LENGTH (eltype2
)
657 || TYPE_UNSIGNED (eltype1
) != TYPE_UNSIGNED (eltype2
)
658 || lowb1
!= lowb2
|| highb1
!= highb2
)
659 error (_("Cannot perform operation on vectors with different types"));
661 /* Determine the resulting type of the operation and allocate the value. */
662 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
663 TYPE_LENGTH (eltype1
), 0,
665 ret
= allocate_value (rettype
);
667 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
669 /* For vector types, the relational, equality and logical operators shall
670 return 0 if the specified relation is false and -1 (i.e. all bits set)
671 if the specified relation is true. */
672 int tmp
= scalar_relop (value_subscript (val1
, i
),
673 value_subscript (val2
, i
), op
) ? -1 : 0;
674 memset (value_contents_writeable (ret
) + i
* TYPE_LENGTH (eltype1
),
675 tmp
, TYPE_LENGTH (eltype1
));
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
= 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 binary relational and equality operators that are either not
739 or differently defined for GNU vectors. */
747 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
748 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
750 if (noside
== EVAL_SKIP
)
751 return value_from_longest (builtin_type (exp
->gdbarch
)->
754 return opencl_relop (exp
, arg1
, arg2
, op
);
756 /* Handle the logical unary operator not(!). */
757 case UNOP_LOGICAL_NOT
:
759 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
761 if (noside
== EVAL_SKIP
)
762 return value_from_longest (builtin_type (exp
->gdbarch
)->
765 return opencl_logical_not (exp
, arg1
);
767 /* Handle the logical operator and(&&) and or(||). */
768 case BINOP_LOGICAL_AND
:
769 case BINOP_LOGICAL_OR
:
771 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
773 if (noside
== EVAL_SKIP
)
775 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
777 return value_from_longest (builtin_type (exp
->gdbarch
)->
782 /* For scalar operations we need to avoid evaluating operands
783 unecessarily. However, for vector operations we always need to
784 evaluate both operands. Unfortunately we only know which of the
785 two cases apply after we know the type of the second operand.
786 Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS. */
789 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
791 type1
= check_typedef (value_type (arg1
));
792 type2
= check_typedef (value_type (arg2
));
794 if ((TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
795 || (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
)))
797 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
799 return opencl_relop (exp
, arg1
, arg2
, op
);
803 /* For scalar built-in types, only evaluate the right
804 hand operand if the left hand operand compares
805 unequal(&&)/equal(||) to 0. */
807 int tmp
= value_logical_not (arg1
);
809 if (op
== BINOP_LOGICAL_OR
)
812 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
813 tmp
? EVAL_SKIP
: noside
);
814 type1
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
816 if (op
== BINOP_LOGICAL_AND
)
817 res
= !tmp
&& !value_logical_not (arg2
);
818 else /* BINOP_LOGICAL_OR */
819 res
= tmp
|| !value_logical_not (arg2
);
821 return value_from_longest (type1
, res
);
825 /* Handle the ternary selection operator. */
828 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
829 type1
= check_typedef (value_type (arg1
));
830 if (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
832 struct value
*arg3
, *tmp
, *ret
;
833 struct type
*eltype2
, *type3
, *eltype3
;
834 int t2_is_vec
, t3_is_vec
, i
;
835 LONGEST lowb1
, lowb2
, lowb3
, highb1
, highb2
, highb3
;
837 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
838 arg3
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
839 type2
= check_typedef (value_type (arg2
));
840 type3
= check_typedef (value_type (arg3
));
842 = TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
);
844 = TYPE_CODE (type3
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type3
);
846 /* Widen the scalar operand to a vector if necessary. */
847 if (t2_is_vec
|| !t3_is_vec
)
849 arg3
= value_cast (type2
, arg3
);
850 type3
= value_type (arg3
);
852 else if (!t2_is_vec
|| t3_is_vec
)
854 arg2
= value_cast (type3
, arg2
);
855 type2
= value_type (arg2
);
857 else if (!t2_is_vec
|| !t3_is_vec
)
859 /* Throw an error if arg2 or arg3 aren't vectors. */
861 Cannot perform conditional operation on incompatible types"));
864 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
865 eltype3
= check_typedef (TYPE_TARGET_TYPE (type3
));
867 if (!get_array_bounds (type1
, &lowb1
, &highb1
)
868 || !get_array_bounds (type2
, &lowb2
, &highb2
)
869 || !get_array_bounds (type3
, &lowb3
, &highb3
))
870 error (_("Could not determine the vector bounds"));
872 /* Throw an error if the types of arg2 or arg3 are incompatible. */
873 if (TYPE_CODE (eltype2
) != TYPE_CODE (eltype3
)
874 || TYPE_LENGTH (eltype2
) != TYPE_LENGTH (eltype3
)
875 || TYPE_UNSIGNED (eltype2
) != TYPE_UNSIGNED (eltype3
)
876 || lowb2
!= lowb3
|| highb2
!= highb3
)
878 Cannot perform operation on vectors with different types"));
880 /* Throw an error if the sizes of arg1 and arg2/arg3 differ. */
881 if (lowb1
!= lowb2
|| lowb1
!= lowb3
882 || highb1
!= highb2
|| highb1
!= highb3
)
884 Cannot perform conditional operation on vectors with different sizes"));
886 ret
= allocate_value (type2
);
888 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
890 tmp
= value_logical_not (value_subscript (arg1
, i
)) ?
891 value_subscript (arg3
, i
) : value_subscript (arg2
, i
);
892 memcpy (value_contents_writeable (ret
) +
893 i
* TYPE_LENGTH (eltype2
), value_contents_all (tmp
),
894 TYPE_LENGTH (eltype2
));
901 if (value_logical_not (arg1
))
903 /* Skip the second operand. */
904 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
906 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
910 /* Skip the third operand. */
911 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
912 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
918 /* Handle STRUCTOP_STRUCT to allow component access on OpenCL vectors. */
919 case STRUCTOP_STRUCT
:
922 int tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
924 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
925 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
926 type1
= check_typedef (value_type (arg1
));
928 if (noside
== EVAL_SKIP
)
930 return value_from_longest (builtin_type (exp
->gdbarch
)->
933 else if (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
935 return opencl_component_ref (exp
, arg1
, &exp
->elts
[pc
+ 2].string
,
940 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
942 value_zero (lookup_struct_elt_type
943 (value_type (arg1
),&exp
->elts
[pc
+ 2].string
, 0),
946 return value_struct_elt (&arg1
, NULL
,
947 &exp
->elts
[pc
+ 2].string
, NULL
,
955 return evaluate_subexp_c (expect_type
, exp
, pos
, noside
);
959 opencl_language_arch_info (struct gdbarch
*gdbarch
,
960 struct language_arch_info
*lai
)
962 const struct builtin_opencl_type
*builtin
= builtin_opencl_type (gdbarch
);
964 lai
->string_char_type
= builtin
->builtin_char
;
965 lai
->primitive_type_vector
966 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_opencl_primitive_types
+ 1,
969 /* This macro fills the primitive_type_vector from a given type. */
970 #define FILL_TYPE_VECTOR(LAI, TYPE)\
971 LAI->primitive_type_vector [opencl_primitive_type_##TYPE]\
972 = builtin->builtin_##TYPE;\
973 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##2]\
974 = builtin->builtin_##TYPE##2;\
975 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##3]\
976 = builtin->builtin_##TYPE##3;\
977 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##4]\
978 = builtin->builtin_##TYPE##4;\
979 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##8]\
980 = builtin->builtin_##TYPE##8;\
981 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##16]\
982 = builtin->builtin_##TYPE##16
984 FILL_TYPE_VECTOR (lai
, char);
985 FILL_TYPE_VECTOR (lai
, uchar
);
986 FILL_TYPE_VECTOR (lai
, short);
987 FILL_TYPE_VECTOR (lai
, ushort
);
988 FILL_TYPE_VECTOR (lai
, int);
989 FILL_TYPE_VECTOR (lai
, uint
);
990 FILL_TYPE_VECTOR (lai
, long);
991 FILL_TYPE_VECTOR (lai
, ulong
);
992 FILL_TYPE_VECTOR (lai
, half
);
993 FILL_TYPE_VECTOR (lai
, float);
994 FILL_TYPE_VECTOR (lai
, double);
995 lai
->primitive_type_vector
[opencl_primitive_type_bool
]
996 = builtin
->builtin_bool
;
997 lai
->primitive_type_vector
[opencl_primitive_type_unsigned_char
]
998 = builtin
->builtin_unsigned_char
;
999 lai
->primitive_type_vector
[opencl_primitive_type_unsigned_short
]
1000 = builtin
->builtin_unsigned_short
;
1001 lai
->primitive_type_vector
[opencl_primitive_type_unsigned_int
]
1002 = builtin
->builtin_unsigned_int
;
1003 lai
->primitive_type_vector
[opencl_primitive_type_unsigned_long
]
1004 = builtin
->builtin_unsigned_long
;
1005 lai
->primitive_type_vector
[opencl_primitive_type_half
]
1006 = builtin
->builtin_half
;
1007 lai
->primitive_type_vector
[opencl_primitive_type_size_t
]
1008 = builtin
->builtin_size_t
;
1009 lai
->primitive_type_vector
[opencl_primitive_type_ptrdiff_t
]
1010 = builtin
->builtin_ptrdiff_t
;
1011 lai
->primitive_type_vector
[opencl_primitive_type_intptr_t
]
1012 = builtin
->builtin_intptr_t
;
1013 lai
->primitive_type_vector
[opencl_primitive_type_uintptr_t
]
1014 = builtin
->builtin_uintptr_t
;
1015 lai
->primitive_type_vector
[opencl_primitive_type_void
]
1016 = builtin
->builtin_void
;
1018 /* Specifies the return type of logical and relational operations. */
1019 lai
->bool_type_symbol
= "int";
1020 lai
->bool_type_default
= builtin
->builtin_int
;
1023 const struct exp_descriptor exp_descriptor_opencl
=
1025 print_subexp_standard
,
1026 operator_length_standard
,
1027 operator_check_standard
,
1029 dump_subexp_body_standard
,
1030 evaluate_subexp_opencl
1033 const struct language_defn opencl_language_defn
=
1035 "opencl", /* Language name */
1042 &exp_descriptor_opencl
,
1046 c_printchar
, /* Print a character constant */
1047 c_printstr
, /* Function to print string constant */
1048 c_emit_char
, /* Print a single char */
1049 c_print_type
, /* Print a type using appropriate syntax */
1050 c_print_typedef
, /* Print a typedef using appropriate syntax */
1051 c_val_print
, /* Print a value using appropriate syntax */
1052 c_value_print
, /* Print a top-level value */
1053 NULL
, /* Language specific skip_trampoline */
1054 NULL
, /* name_of_this */
1055 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
1056 basic_lookup_transparent_type
,/* lookup_transparent_type */
1057 NULL
, /* Language specific symbol demangler */
1058 NULL
, /* Language specific class_name_from_physname */
1059 c_op_print_tab
, /* expression operators for printing */
1060 1, /* c-style arrays */
1061 0, /* String lower bound */
1062 default_word_break_characters
,
1063 default_make_symbol_completion_list
,
1064 opencl_language_arch_info
,
1065 default_print_array_index
,
1066 default_pass_by_reference
,
1072 build_opencl_types (struct gdbarch
*gdbarch
)
1074 struct builtin_opencl_type
*builtin_opencl_type
1075 = GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct builtin_opencl_type
);
1077 /* Helper macro to create strings. */
1078 #define STRINGIFY(S) #S
1079 /* This macro allocates and assigns the type struct pointers
1080 for the vector types. */
1081 #define BUILD_OCL_VTYPES(TYPE)\
1082 builtin_opencl_type->builtin_##TYPE##2\
1083 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 2);\
1084 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##2) = STRINGIFY(TYPE ## 2);\
1085 builtin_opencl_type->builtin_##TYPE##3\
1086 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 3);\
1087 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##3) = STRINGIFY(TYPE ## 3);\
1088 TYPE_LENGTH (builtin_opencl_type->builtin_##TYPE##3)\
1089 = 4 * TYPE_LENGTH (builtin_opencl_type->builtin_##TYPE);\
1090 builtin_opencl_type->builtin_##TYPE##4\
1091 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 4);\
1092 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##4) = STRINGIFY(TYPE ## 4);\
1093 builtin_opencl_type->builtin_##TYPE##8\
1094 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 8);\
1095 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##8) = STRINGIFY(TYPE ## 8);\
1096 builtin_opencl_type->builtin_##TYPE##16\
1097 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 16);\
1098 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##16) = STRINGIFY(TYPE ## 16)
1100 builtin_opencl_type
->builtin_char
1101 = arch_integer_type (gdbarch
, 8, 0, "char");
1102 BUILD_OCL_VTYPES (char);
1103 builtin_opencl_type
->builtin_uchar
1104 = arch_integer_type (gdbarch
, 8, 1, "uchar");
1105 BUILD_OCL_VTYPES (uchar
);
1106 builtin_opencl_type
->builtin_short
1107 = arch_integer_type (gdbarch
, 16, 0, "short");
1108 BUILD_OCL_VTYPES (short);
1109 builtin_opencl_type
->builtin_ushort
1110 = arch_integer_type (gdbarch
, 16, 1, "ushort");
1111 BUILD_OCL_VTYPES (ushort
);
1112 builtin_opencl_type
->builtin_int
1113 = arch_integer_type (gdbarch
, 32, 0, "int");
1114 BUILD_OCL_VTYPES (int);
1115 builtin_opencl_type
->builtin_uint
1116 = arch_integer_type (gdbarch
, 32, 1, "uint");
1117 BUILD_OCL_VTYPES (uint
);
1118 builtin_opencl_type
->builtin_long
1119 = arch_integer_type (gdbarch
, 64, 0, "long");
1120 BUILD_OCL_VTYPES (long);
1121 builtin_opencl_type
->builtin_ulong
1122 = arch_integer_type (gdbarch
, 64, 1, "ulong");
1123 BUILD_OCL_VTYPES (ulong
);
1124 builtin_opencl_type
->builtin_half
1125 = arch_float_type (gdbarch
, 16, "half", floatformats_ieee_half
);
1126 BUILD_OCL_VTYPES (half
);
1127 builtin_opencl_type
->builtin_float
1128 = arch_float_type (gdbarch
, 32, "float", floatformats_ieee_single
);
1129 BUILD_OCL_VTYPES (float);
1130 builtin_opencl_type
->builtin_double
1131 = arch_float_type (gdbarch
, 64, "double", floatformats_ieee_double
);
1132 BUILD_OCL_VTYPES (double);
1133 builtin_opencl_type
->builtin_bool
1134 = arch_boolean_type (gdbarch
, 32, 1, "bool");
1135 builtin_opencl_type
->builtin_unsigned_char
1136 = arch_integer_type (gdbarch
, 8, 1, "unsigned char");
1137 builtin_opencl_type
->builtin_unsigned_short
1138 = arch_integer_type (gdbarch
, 16, 1, "unsigned short");
1139 builtin_opencl_type
->builtin_unsigned_int
1140 = arch_integer_type (gdbarch
, 32, 1, "unsigned int");
1141 builtin_opencl_type
->builtin_unsigned_long
1142 = arch_integer_type (gdbarch
, 64, 1, "unsigned long");
1143 builtin_opencl_type
->builtin_size_t
1144 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "size_t");
1145 builtin_opencl_type
->builtin_ptrdiff_t
1146 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "ptrdiff_t");
1147 builtin_opencl_type
->builtin_intptr_t
1148 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "intptr_t");
1149 builtin_opencl_type
->builtin_uintptr_t
1150 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "uintptr_t");
1151 builtin_opencl_type
->builtin_void
1152 = arch_type (gdbarch
, TYPE_CODE_VOID
, 1, "void");
1154 return builtin_opencl_type
;
1158 _initialize_opencl_language (void)
1160 opencl_type_data
= gdbarch_data_register_post_init (build_opencl_types
);
1161 add_language (&opencl_language_defn
);