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
))
330 /* Return nonzero if bits in V from OFFSET and LENGTH represent a
331 synthetic pointer. */
334 lval_func_check_synthetic_pointer (const struct value
*v
,
335 int offset
, int length
)
337 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
338 /* Size of the target type in bits. */
340 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c
->val
)))) * 8;
341 int startrest
= offset
% elsize
;
342 int start
= offset
/ elsize
;
343 int endrest
= (offset
+ length
) % elsize
;
344 int end
= (offset
+ length
) / elsize
;
353 for (i
= start
; i
< end
; i
++)
355 int startoffset
= (i
== start
) ? startrest
: 0;
356 int length
= (i
== end
) ? endrest
: elsize
;
358 if (!value_bits_synthetic_pointer (c
->val
,
359 c
->indices
[i
] * elsize
+ startoffset
,
368 lval_func_copy_closure (const struct value
*v
)
370 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
378 lval_func_free_closure (struct value
*v
)
380 struct lval_closure
*c
= (struct lval_closure
*) value_computed_closure (v
);
388 value_free (c
->val
); /* Decrement the reference counter of the value. */
392 static struct lval_funcs opencl_value_funcs
=
396 lval_func_check_validity
,
397 lval_func_check_any_valid
,
399 lval_func_check_synthetic_pointer
,
400 lval_func_copy_closure
,
401 lval_func_free_closure
404 /* Creates a sub-vector from VAL. The elements are selected by the indices of
405 an array with the length of N. Supported values for NOSIDE are
406 EVAL_NORMAL and EVAL_AVOID_SIDE_EFFECTS. */
408 static struct value
*
409 create_value (struct gdbarch
*gdbarch
, struct value
*val
, enum noside noside
,
412 struct type
*type
= check_typedef (value_type (val
));
413 struct type
*elm_type
= TYPE_TARGET_TYPE (type
);
416 /* Check if a single component of a vector is requested which means
417 the resulting type is a (primitive) scalar type. */
420 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
421 ret
= value_zero (elm_type
, not_lval
);
423 ret
= value_subscript (val
, indices
[0]);
427 /* Multiple components of the vector are requested which means the
428 resulting type is a vector as well. */
429 struct type
*dst_type
=
430 lookup_opencl_vector_type (gdbarch
, TYPE_CODE (elm_type
),
431 TYPE_LENGTH (elm_type
),
432 TYPE_UNSIGNED (elm_type
), n
);
434 if (dst_type
== NULL
)
435 dst_type
= init_vector_type (elm_type
, n
);
437 make_cv_type (TYPE_CONST (type
), TYPE_VOLATILE (type
), dst_type
, NULL
);
439 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
440 ret
= allocate_value (dst_type
);
443 /* Check whether to create a lvalue or not. */
444 if (VALUE_LVAL (val
) != not_lval
&& !array_has_dups (indices
, n
))
446 struct lval_closure
*c
= allocate_lval_closure (indices
, n
, val
);
447 ret
= allocate_computed_value (dst_type
, &opencl_value_funcs
, c
);
453 ret
= allocate_value (dst_type
);
455 /* Copy src val contents into the destination value. */
456 for (i
= 0; i
< n
; i
++)
457 memcpy (value_contents_writeable (ret
)
458 + (i
* TYPE_LENGTH (elm_type
)),
460 + (indices
[i
] * TYPE_LENGTH (elm_type
)),
461 TYPE_LENGTH (elm_type
));
468 /* OpenCL vector component access. */
470 static struct value
*
471 opencl_component_ref (struct expression
*exp
, struct value
*val
, char *comps
,
480 if (!get_array_bounds (check_typedef (value_type (val
)), &lowb
, &highb
))
481 error (_("Could not determine the vector bounds"));
483 src_len
= highb
- lowb
+ 1;
485 /* Throw an error if the amount of array elements does not fit a
486 valid OpenCL vector size (2, 3, 4, 8, 16). */
487 if (src_len
!= 2 && src_len
!= 3 && src_len
!= 4 && src_len
!= 8
489 error (_("Invalid OpenCL vector size"));
491 if (strcmp (comps
, "lo") == 0 )
493 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
495 for (i
= 0; i
< dst_len
; i
++)
498 else if (strcmp (comps
, "hi") == 0)
500 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
502 for (i
= 0; i
< dst_len
; i
++)
503 indices
[i
] = dst_len
+ i
;
505 else if (strcmp (comps
, "even") == 0)
507 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
509 for (i
= 0; i
< dst_len
; i
++)
512 else if (strcmp (comps
, "odd") == 0)
514 dst_len
= (src_len
== 3) ? 2 : src_len
/ 2;
516 for (i
= 0; i
< dst_len
; i
++)
519 else if (strncasecmp (comps
, "s", 1) == 0)
521 #define HEXCHAR_TO_INT(C) ((C >= '0' && C <= '9') ? \
522 C-'0' : ((C >= 'A' && C <= 'F') ? \
523 C-'A'+10 : ((C >= 'a' && C <= 'f') ? \
526 dst_len
= strlen (comps
);
527 /* Skip the s/S-prefix. */
530 for (i
= 0; i
< dst_len
; i
++)
532 indices
[i
] = HEXCHAR_TO_INT(comps
[i
+1]);
533 /* Check if the requested component is invalid or exceeds
535 if (indices
[i
] < 0 || indices
[i
] >= src_len
)
536 error (_("Invalid OpenCL vector component accessor %s"), comps
);
541 dst_len
= strlen (comps
);
543 for (i
= 0; i
< dst_len
; i
++)
556 error (_("Invalid OpenCL vector component accessor %s"), comps
);
561 error (_("Invalid OpenCL vector component accessor %s"), comps
);
565 error (_("Invalid OpenCL vector component accessor %s"), comps
);
571 /* Throw an error if the amount of requested components does not
572 result in a valid length (1, 2, 3, 4, 8, 16). */
573 if (dst_len
!= 1 && dst_len
!= 2 && dst_len
!= 3 && dst_len
!= 4
574 && dst_len
!= 8 && dst_len
!= 16)
575 error (_("Invalid OpenCL vector component accessor %s"), comps
);
577 v
= create_value (exp
->gdbarch
, val
, noside
, indices
, dst_len
);
582 /* Perform the unary logical not (!) operation. */
584 static struct value
*
585 opencl_logical_not (struct expression
*exp
, struct value
*arg
)
587 struct type
*type
= check_typedef (value_type (arg
));
588 struct type
*rettype
;
591 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
593 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
597 if (!get_array_bounds (type
, &lowb
, &highb
))
598 error (_("Could not determine the vector bounds"));
600 /* Determine the resulting type of the operation and allocate the
602 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
603 TYPE_LENGTH (eltype
), 0,
605 ret
= allocate_value (rettype
);
607 for (i
= 0; i
< highb
- lowb
+ 1; i
++)
609 /* For vector types, the unary operator shall return a 0 if the
610 value of its operand compares unequal to 0, and -1 (i.e. all bits
611 set) if the value of its operand compares equal to 0. */
612 int tmp
= value_logical_not (value_subscript (arg
, i
)) ? -1 : 0;
613 memset (value_contents_writeable (ret
) + i
* TYPE_LENGTH (eltype
),
614 tmp
, TYPE_LENGTH (eltype
));
619 rettype
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
620 ret
= value_from_longest (rettype
, value_logical_not (arg
));
626 /* Perform a relational operation on two scalar operands. */
629 scalar_relop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
636 ret
= value_equal (val1
, val2
);
639 ret
= !value_equal (val1
, val2
);
642 ret
= value_less (val1
, val2
);
645 ret
= value_less (val2
, val1
);
648 ret
= value_less (val2
, val1
) || value_equal (val1
, val2
);
651 ret
= value_less (val1
, val2
) || value_equal (val1
, val2
);
653 case BINOP_LOGICAL_AND
:
654 ret
= !value_logical_not (val1
) && !value_logical_not (val2
);
656 case BINOP_LOGICAL_OR
:
657 ret
= !value_logical_not (val1
) || !value_logical_not (val2
);
660 error (_("Attempt to perform an unsupported operation"));
666 /* Perform a relational operation on two vector operands. */
668 static struct value
*
669 vector_relop (struct expression
*exp
, struct value
*val1
, struct value
*val2
,
673 struct type
*type1
, *type2
, *eltype1
, *eltype2
, *rettype
;
674 int t1_is_vec
, t2_is_vec
, i
;
675 LONGEST lowb1
, lowb2
, highb1
, highb2
;
677 type1
= check_typedef (value_type (val1
));
678 type2
= check_typedef (value_type (val2
));
680 t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
));
681 t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
));
683 if (!t1_is_vec
|| !t2_is_vec
)
684 error (_("Vector operations are not supported on scalar types"));
686 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
687 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
689 if (!get_array_bounds (type1
,&lowb1
, &highb1
)
690 || !get_array_bounds (type2
, &lowb2
, &highb2
))
691 error (_("Could not determine the vector bounds"));
693 /* Check whether the vector types are compatible. */
694 if (TYPE_CODE (eltype1
) != TYPE_CODE (eltype2
)
695 || TYPE_LENGTH (eltype1
) != TYPE_LENGTH (eltype2
)
696 || TYPE_UNSIGNED (eltype1
) != TYPE_UNSIGNED (eltype2
)
697 || lowb1
!= lowb2
|| highb1
!= highb2
)
698 error (_("Cannot perform operation on vectors with different types"));
700 /* Determine the resulting type of the operation and allocate the value. */
701 rettype
= lookup_opencl_vector_type (exp
->gdbarch
, TYPE_CODE_INT
,
702 TYPE_LENGTH (eltype1
), 0,
704 ret
= allocate_value (rettype
);
706 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
708 /* For vector types, the relational, equality and logical operators shall
709 return 0 if the specified relation is false and -1 (i.e. all bits set)
710 if the specified relation is true. */
711 int tmp
= scalar_relop (value_subscript (val1
, i
),
712 value_subscript (val2
, i
), op
) ? -1 : 0;
713 memset (value_contents_writeable (ret
) + i
* TYPE_LENGTH (eltype1
),
714 tmp
, TYPE_LENGTH (eltype1
));
720 /* Perform a relational operation on two operands. */
722 static struct value
*
723 opencl_relop (struct expression
*exp
, struct value
*arg1
, struct value
*arg2
,
727 struct type
*type1
= check_typedef (value_type (arg1
));
728 struct type
*type2
= check_typedef (value_type (arg2
));
729 int t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
730 && TYPE_VECTOR (type1
));
731 int t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
732 && TYPE_VECTOR (type2
));
734 if (!t1_is_vec
&& !t2_is_vec
)
736 int tmp
= scalar_relop (arg1
, arg2
, op
);
738 language_bool_type (exp
->language_defn
, exp
->gdbarch
);
740 val
= value_from_longest (type
, tmp
);
742 else if (t1_is_vec
&& t2_is_vec
)
744 val
= vector_relop (exp
, arg1
, arg2
, op
);
748 /* Widen the scalar operand to a vector. */
749 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
750 struct type
*t
= t1_is_vec
? type2
: type1
;
752 if (TYPE_CODE (t
) != TYPE_CODE_FLT
&& !is_integral_type (t
))
753 error (_("Argument to operation not a number or boolean."));
755 *v
= value_cast (t1_is_vec
? type1
: type2
, *v
);
756 val
= vector_relop (exp
, arg1
, arg2
, op
);
762 /* Expression evaluator for the OpenCL. Most operations are delegated to
763 evaluate_subexp_standard; see that function for a description of the
766 static struct value
*
767 evaluate_subexp_opencl (struct type
*expect_type
, struct expression
*exp
,
768 int *pos
, enum noside noside
)
770 enum exp_opcode op
= exp
->elts
[*pos
].opcode
;
771 struct value
*arg1
= NULL
;
772 struct value
*arg2
= NULL
;
773 struct type
*type1
, *type2
;
777 /* Handle binary relational and equality operators that are either not
778 or differently defined for GNU vectors. */
786 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
787 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
789 if (noside
== EVAL_SKIP
)
790 return value_from_longest (builtin_type (exp
->gdbarch
)->
793 return opencl_relop (exp
, arg1
, arg2
, op
);
795 /* Handle the logical unary operator not(!). */
796 case UNOP_LOGICAL_NOT
:
798 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
800 if (noside
== EVAL_SKIP
)
801 return value_from_longest (builtin_type (exp
->gdbarch
)->
804 return opencl_logical_not (exp
, arg1
);
806 /* Handle the logical operator and(&&) and or(||). */
807 case BINOP_LOGICAL_AND
:
808 case BINOP_LOGICAL_OR
:
810 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
812 if (noside
== EVAL_SKIP
)
814 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
816 return value_from_longest (builtin_type (exp
->gdbarch
)->
821 /* For scalar operations we need to avoid evaluating operands
822 unecessarily. However, for vector operations we always need to
823 evaluate both operands. Unfortunately we only know which of the
824 two cases apply after we know the type of the second operand.
825 Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS. */
828 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
830 type1
= check_typedef (value_type (arg1
));
831 type2
= check_typedef (value_type (arg2
));
833 if ((TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
834 || (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
)))
836 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
838 return opencl_relop (exp
, arg1
, arg2
, op
);
842 /* For scalar built-in types, only evaluate the right
843 hand operand if the left hand operand compares
844 unequal(&&)/equal(||) to 0. */
846 int tmp
= value_logical_not (arg1
);
848 if (op
== BINOP_LOGICAL_OR
)
851 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
852 tmp
? EVAL_SKIP
: noside
);
853 type1
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
855 if (op
== BINOP_LOGICAL_AND
)
856 res
= !tmp
&& !value_logical_not (arg2
);
857 else /* BINOP_LOGICAL_OR */
858 res
= tmp
|| !value_logical_not (arg2
);
860 return value_from_longest (type1
, res
);
864 /* Handle the ternary selection operator. */
867 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
868 type1
= check_typedef (value_type (arg1
));
869 if (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
871 struct value
*arg3
, *tmp
, *ret
;
872 struct type
*eltype2
, *type3
, *eltype3
;
873 int t2_is_vec
, t3_is_vec
, i
;
874 LONGEST lowb1
, lowb2
, lowb3
, highb1
, highb2
, highb3
;
876 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
877 arg3
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
878 type2
= check_typedef (value_type (arg2
));
879 type3
= check_typedef (value_type (arg3
));
881 = TYPE_CODE (type2
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type2
);
883 = TYPE_CODE (type3
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type3
);
885 /* Widen the scalar operand to a vector if necessary. */
886 if (t2_is_vec
|| !t3_is_vec
)
888 arg3
= value_cast (type2
, arg3
);
889 type3
= value_type (arg3
);
891 else if (!t2_is_vec
|| t3_is_vec
)
893 arg2
= value_cast (type3
, arg2
);
894 type2
= value_type (arg2
);
896 else if (!t2_is_vec
|| !t3_is_vec
)
898 /* Throw an error if arg2 or arg3 aren't vectors. */
900 Cannot perform conditional operation on incompatible types"));
903 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
904 eltype3
= check_typedef (TYPE_TARGET_TYPE (type3
));
906 if (!get_array_bounds (type1
, &lowb1
, &highb1
)
907 || !get_array_bounds (type2
, &lowb2
, &highb2
)
908 || !get_array_bounds (type3
, &lowb3
, &highb3
))
909 error (_("Could not determine the vector bounds"));
911 /* Throw an error if the types of arg2 or arg3 are incompatible. */
912 if (TYPE_CODE (eltype2
) != TYPE_CODE (eltype3
)
913 || TYPE_LENGTH (eltype2
) != TYPE_LENGTH (eltype3
)
914 || TYPE_UNSIGNED (eltype2
) != TYPE_UNSIGNED (eltype3
)
915 || lowb2
!= lowb3
|| highb2
!= highb3
)
917 Cannot perform operation on vectors with different types"));
919 /* Throw an error if the sizes of arg1 and arg2/arg3 differ. */
920 if (lowb1
!= lowb2
|| lowb1
!= lowb3
921 || highb1
!= highb2
|| highb1
!= highb3
)
923 Cannot perform conditional operation on vectors with different sizes"));
925 ret
= allocate_value (type2
);
927 for (i
= 0; i
< highb1
- lowb1
+ 1; i
++)
929 tmp
= value_logical_not (value_subscript (arg1
, i
)) ?
930 value_subscript (arg3
, i
) : value_subscript (arg2
, i
);
931 memcpy (value_contents_writeable (ret
) +
932 i
* TYPE_LENGTH (eltype2
), value_contents_all (tmp
),
933 TYPE_LENGTH (eltype2
));
940 if (value_logical_not (arg1
))
942 /* Skip the second operand. */
943 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
945 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
949 /* Skip the third operand. */
950 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
951 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
957 /* Handle STRUCTOP_STRUCT to allow component access on OpenCL vectors. */
958 case STRUCTOP_STRUCT
:
961 int tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
963 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
964 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
965 type1
= check_typedef (value_type (arg1
));
967 if (noside
== EVAL_SKIP
)
969 return value_from_longest (builtin_type (exp
->gdbarch
)->
972 else if (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type1
))
974 return opencl_component_ref (exp
, arg1
, &exp
->elts
[pc
+ 2].string
,
979 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
981 value_zero (lookup_struct_elt_type
982 (value_type (arg1
),&exp
->elts
[pc
+ 2].string
, 0),
985 return value_struct_elt (&arg1
, NULL
,
986 &exp
->elts
[pc
+ 2].string
, NULL
,
994 return evaluate_subexp_c (expect_type
, exp
, pos
, noside
);
998 opencl_language_arch_info (struct gdbarch
*gdbarch
,
999 struct language_arch_info
*lai
)
1001 const struct builtin_opencl_type
*builtin
= builtin_opencl_type (gdbarch
);
1003 lai
->string_char_type
= builtin
->builtin_char
;
1004 lai
->primitive_type_vector
1005 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_opencl_primitive_types
+ 1,
1008 /* This macro fills the primitive_type_vector from a given type. */
1009 #define FILL_TYPE_VECTOR(LAI, TYPE)\
1010 LAI->primitive_type_vector [opencl_primitive_type_##TYPE]\
1011 = builtin->builtin_##TYPE;\
1012 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##2]\
1013 = builtin->builtin_##TYPE##2;\
1014 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##3]\
1015 = builtin->builtin_##TYPE##3;\
1016 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##4]\
1017 = builtin->builtin_##TYPE##4;\
1018 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##8]\
1019 = builtin->builtin_##TYPE##8;\
1020 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##16]\
1021 = builtin->builtin_##TYPE##16
1023 FILL_TYPE_VECTOR (lai
, char);
1024 FILL_TYPE_VECTOR (lai
, uchar
);
1025 FILL_TYPE_VECTOR (lai
, short);
1026 FILL_TYPE_VECTOR (lai
, ushort
);
1027 FILL_TYPE_VECTOR (lai
, int);
1028 FILL_TYPE_VECTOR (lai
, uint
);
1029 FILL_TYPE_VECTOR (lai
, long);
1030 FILL_TYPE_VECTOR (lai
, ulong
);
1031 FILL_TYPE_VECTOR (lai
, half
);
1032 FILL_TYPE_VECTOR (lai
, float);
1033 FILL_TYPE_VECTOR (lai
, double);
1034 lai
->primitive_type_vector
[opencl_primitive_type_bool
]
1035 = builtin
->builtin_bool
;
1036 lai
->primitive_type_vector
[opencl_primitive_type_unsigned_char
]
1037 = builtin
->builtin_unsigned_char
;
1038 lai
->primitive_type_vector
[opencl_primitive_type_unsigned_short
]
1039 = builtin
->builtin_unsigned_short
;
1040 lai
->primitive_type_vector
[opencl_primitive_type_unsigned_int
]
1041 = builtin
->builtin_unsigned_int
;
1042 lai
->primitive_type_vector
[opencl_primitive_type_unsigned_long
]
1043 = builtin
->builtin_unsigned_long
;
1044 lai
->primitive_type_vector
[opencl_primitive_type_half
]
1045 = builtin
->builtin_half
;
1046 lai
->primitive_type_vector
[opencl_primitive_type_size_t
]
1047 = builtin
->builtin_size_t
;
1048 lai
->primitive_type_vector
[opencl_primitive_type_ptrdiff_t
]
1049 = builtin
->builtin_ptrdiff_t
;
1050 lai
->primitive_type_vector
[opencl_primitive_type_intptr_t
]
1051 = builtin
->builtin_intptr_t
;
1052 lai
->primitive_type_vector
[opencl_primitive_type_uintptr_t
]
1053 = builtin
->builtin_uintptr_t
;
1054 lai
->primitive_type_vector
[opencl_primitive_type_void
]
1055 = builtin
->builtin_void
;
1057 /* Specifies the return type of logical and relational operations. */
1058 lai
->bool_type_symbol
= "int";
1059 lai
->bool_type_default
= builtin
->builtin_int
;
1062 const struct exp_descriptor exp_descriptor_opencl
=
1064 print_subexp_standard
,
1065 operator_length_standard
,
1066 operator_check_standard
,
1068 dump_subexp_body_standard
,
1069 evaluate_subexp_opencl
1072 const struct language_defn opencl_language_defn
=
1074 "opencl", /* Language name */
1081 &exp_descriptor_opencl
,
1085 c_printchar
, /* Print a character constant */
1086 c_printstr
, /* Function to print string constant */
1087 c_emit_char
, /* Print a single char */
1088 c_print_type
, /* Print a type using appropriate syntax */
1089 c_print_typedef
, /* Print a typedef using appropriate syntax */
1090 c_val_print
, /* Print a value using appropriate syntax */
1091 c_value_print
, /* Print a top-level value */
1092 NULL
, /* Language specific skip_trampoline */
1093 NULL
, /* name_of_this */
1094 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
1095 basic_lookup_transparent_type
,/* lookup_transparent_type */
1096 NULL
, /* Language specific symbol demangler */
1097 NULL
, /* Language specific class_name_from_physname */
1098 c_op_print_tab
, /* expression operators for printing */
1099 1, /* c-style arrays */
1100 0, /* String lower bound */
1101 default_word_break_characters
,
1102 default_make_symbol_completion_list
,
1103 opencl_language_arch_info
,
1104 default_print_array_index
,
1105 default_pass_by_reference
,
1111 build_opencl_types (struct gdbarch
*gdbarch
)
1113 struct builtin_opencl_type
*builtin_opencl_type
1114 = GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct builtin_opencl_type
);
1116 /* Helper macro to create strings. */
1117 #define STRINGIFY(S) #S
1118 /* This macro allocates and assigns the type struct pointers
1119 for the vector types. */
1120 #define BUILD_OCL_VTYPES(TYPE)\
1121 builtin_opencl_type->builtin_##TYPE##2\
1122 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 2);\
1123 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##2) = STRINGIFY(TYPE ## 2);\
1124 builtin_opencl_type->builtin_##TYPE##3\
1125 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 3);\
1126 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##3) = STRINGIFY(TYPE ## 3);\
1127 TYPE_LENGTH (builtin_opencl_type->builtin_##TYPE##3)\
1128 = 4 * TYPE_LENGTH (builtin_opencl_type->builtin_##TYPE);\
1129 builtin_opencl_type->builtin_##TYPE##4\
1130 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 4);\
1131 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##4) = STRINGIFY(TYPE ## 4);\
1132 builtin_opencl_type->builtin_##TYPE##8\
1133 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 8);\
1134 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##8) = STRINGIFY(TYPE ## 8);\
1135 builtin_opencl_type->builtin_##TYPE##16\
1136 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 16);\
1137 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##16) = STRINGIFY(TYPE ## 16)
1139 builtin_opencl_type
->builtin_char
1140 = arch_integer_type (gdbarch
, 8, 0, "char");
1141 BUILD_OCL_VTYPES (char);
1142 builtin_opencl_type
->builtin_uchar
1143 = arch_integer_type (gdbarch
, 8, 1, "uchar");
1144 BUILD_OCL_VTYPES (uchar
);
1145 builtin_opencl_type
->builtin_short
1146 = arch_integer_type (gdbarch
, 16, 0, "short");
1147 BUILD_OCL_VTYPES (short);
1148 builtin_opencl_type
->builtin_ushort
1149 = arch_integer_type (gdbarch
, 16, 1, "ushort");
1150 BUILD_OCL_VTYPES (ushort
);
1151 builtin_opencl_type
->builtin_int
1152 = arch_integer_type (gdbarch
, 32, 0, "int");
1153 BUILD_OCL_VTYPES (int);
1154 builtin_opencl_type
->builtin_uint
1155 = arch_integer_type (gdbarch
, 32, 1, "uint");
1156 BUILD_OCL_VTYPES (uint
);
1157 builtin_opencl_type
->builtin_long
1158 = arch_integer_type (gdbarch
, 64, 0, "long");
1159 BUILD_OCL_VTYPES (long);
1160 builtin_opencl_type
->builtin_ulong
1161 = arch_integer_type (gdbarch
, 64, 1, "ulong");
1162 BUILD_OCL_VTYPES (ulong
);
1163 builtin_opencl_type
->builtin_half
1164 = arch_float_type (gdbarch
, 16, "half", floatformats_ieee_half
);
1165 BUILD_OCL_VTYPES (half
);
1166 builtin_opencl_type
->builtin_float
1167 = arch_float_type (gdbarch
, 32, "float", floatformats_ieee_single
);
1168 BUILD_OCL_VTYPES (float);
1169 builtin_opencl_type
->builtin_double
1170 = arch_float_type (gdbarch
, 64, "double", floatformats_ieee_double
);
1171 BUILD_OCL_VTYPES (double);
1172 builtin_opencl_type
->builtin_bool
1173 = arch_boolean_type (gdbarch
, 32, 1, "bool");
1174 builtin_opencl_type
->builtin_unsigned_char
1175 = arch_integer_type (gdbarch
, 8, 1, "unsigned char");
1176 builtin_opencl_type
->builtin_unsigned_short
1177 = arch_integer_type (gdbarch
, 16, 1, "unsigned short");
1178 builtin_opencl_type
->builtin_unsigned_int
1179 = arch_integer_type (gdbarch
, 32, 1, "unsigned int");
1180 builtin_opencl_type
->builtin_unsigned_long
1181 = arch_integer_type (gdbarch
, 64, 1, "unsigned long");
1182 builtin_opencl_type
->builtin_size_t
1183 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "size_t");
1184 builtin_opencl_type
->builtin_ptrdiff_t
1185 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "ptrdiff_t");
1186 builtin_opencl_type
->builtin_intptr_t
1187 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 0, "intptr_t");
1188 builtin_opencl_type
->builtin_uintptr_t
1189 = arch_integer_type (gdbarch
, gdbarch_ptr_bit (gdbarch
), 1, "uintptr_t");
1190 builtin_opencl_type
->builtin_void
1191 = arch_type (gdbarch
, TYPE_CODE_VOID
, 1, "void");
1193 return builtin_opencl_type
;
1197 _initialize_opencl_language (void)
1199 opencl_type_data
= gdbarch_data_register_post_init (build_opencl_types
);
1200 add_language (&opencl_language_defn
);