1 /* OpenCL language support for GDB, the GNU debugger.
2 Copyright (C) 2010, 2011 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,
829 EVAL_AVOID_SIDE_EFFECTS);
831 type1 = check_typedef (value_type (arg1));
832 type2 = check_typedef (value_type (arg2));
834 if ((TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
835 || (TYPE_CODE (type2) == TYPE_CODE_ARRAY && TYPE_VECTOR (type2)))
837 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
839 return opencl_relop (exp, arg1, arg2, op);
843 /* For scalar built-in types, only evaluate the right
844 hand operand if the left hand operand compares
845 unequal(&&)/equal(||) to 0. */
847 int tmp = value_logical_not (arg1);
849 if (op == BINOP_LOGICAL_OR)
852 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
853 tmp ? EVAL_SKIP : noside);
854 type1 = language_bool_type (exp->language_defn, exp->gdbarch);
856 if (op == BINOP_LOGICAL_AND)
857 res = !tmp && !value_logical_not (arg2);
858 else /* BINOP_LOGICAL_OR */
859 res = tmp || !value_logical_not (arg2);
861 return value_from_longest (type1, res);
865 /* Handle the ternary selection operator. */
868 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
869 type1 = check_typedef (value_type (arg1));
870 if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
872 struct value *arg3, *tmp, *ret;
873 struct type *eltype2, *type3, *eltype3;
874 int t2_is_vec, t3_is_vec, i;
875 LONGEST lowb1, lowb2, lowb3, highb1, highb2, highb3;
877 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
878 arg3 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
879 type2 = check_typedef (value_type (arg2));
880 type3 = check_typedef (value_type (arg3));
882 = TYPE_CODE (type2) == TYPE_CODE_ARRAY && TYPE_VECTOR (type2);
884 = TYPE_CODE (type3) == TYPE_CODE_ARRAY && TYPE_VECTOR (type3);
886 /* Widen the scalar operand to a vector if necessary. */
887 if (t2_is_vec || !t3_is_vec)
889 arg3 = value_cast (type2, arg3);
890 type3 = value_type (arg3);
892 else if (!t2_is_vec || t3_is_vec)
894 arg2 = value_cast (type3, arg2);
895 type2 = value_type (arg2);
897 else if (!t2_is_vec || !t3_is_vec)
899 /* Throw an error if arg2 or arg3 aren't vectors. */
901 Cannot perform conditional operation on incompatible types"));
904 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
905 eltype3 = check_typedef (TYPE_TARGET_TYPE (type3));
907 if (!get_array_bounds (type1, &lowb1, &highb1)
908 || !get_array_bounds (type2, &lowb2, &highb2)
909 || !get_array_bounds (type3, &lowb3, &highb3))
910 error (_("Could not determine the vector bounds"));
912 /* Throw an error if the types of arg2 or arg3 are incompatible. */
913 if (TYPE_CODE (eltype2) != TYPE_CODE (eltype3)
914 || TYPE_LENGTH (eltype2) != TYPE_LENGTH (eltype3)
915 || TYPE_UNSIGNED (eltype2) != TYPE_UNSIGNED (eltype3)
916 || lowb2 != lowb3 || highb2 != highb3)
918 Cannot perform operation on vectors with different types"));
920 /* Throw an error if the sizes of arg1 and arg2/arg3 differ. */
921 if (lowb1 != lowb2 || lowb1 != lowb3
922 || highb1 != highb2 || highb1 != highb3)
924 Cannot perform conditional operation on vectors with different sizes"));
926 ret = allocate_value (type2);
928 for (i = 0; i < highb1 - lowb1 + 1; i++)
930 tmp = value_logical_not (value_subscript (arg1, i)) ?
931 value_subscript (arg3, i) : value_subscript (arg2, i);
932 memcpy (value_contents_writeable (ret) +
933 i * TYPE_LENGTH (eltype2), value_contents_all (tmp),
934 TYPE_LENGTH (eltype2));
941 if (value_logical_not (arg1))
943 /* Skip the second operand. */
944 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
946 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
950 /* Skip the third operand. */
951 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
952 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
958 /* Handle STRUCTOP_STRUCT to allow component access on OpenCL vectors. */
959 case STRUCTOP_STRUCT:
962 int tem = longest_to_int (exp->elts[pc + 1].longconst);
964 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
965 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
966 type1 = check_typedef (value_type (arg1));
968 if (noside == EVAL_SKIP)
970 return value_from_longest (builtin_type (exp->gdbarch)->
973 else if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
975 return opencl_component_ref (exp, arg1, &exp->elts[pc + 2].string,
980 if (noside == EVAL_AVOID_SIDE_EFFECTS)
982 value_zero (lookup_struct_elt_type
983 (value_type (arg1),&exp->elts[pc + 2].string, 0),
986 return value_struct_elt (&arg1, NULL,
987 &exp->elts[pc + 2].string, NULL,
995 return evaluate_subexp_c (expect_type, exp, pos, noside);
999 opencl_language_arch_info (struct gdbarch *gdbarch,
1000 struct language_arch_info *lai)
1002 const struct builtin_opencl_type *builtin = builtin_opencl_type (gdbarch);
1004 lai->string_char_type = builtin->builtin_char;
1005 lai->primitive_type_vector
1006 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_opencl_primitive_types + 1,
1009 /* This macro fills the primitive_type_vector from a given type. */
1010 #define FILL_TYPE_VECTOR(LAI, TYPE)\
1011 LAI->primitive_type_vector [opencl_primitive_type_##TYPE]\
1012 = builtin->builtin_##TYPE;\
1013 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##2]\
1014 = builtin->builtin_##TYPE##2;\
1015 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##3]\
1016 = builtin->builtin_##TYPE##3;\
1017 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##4]\
1018 = builtin->builtin_##TYPE##4;\
1019 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##8]\
1020 = builtin->builtin_##TYPE##8;\
1021 LAI->primitive_type_vector [opencl_primitive_type_##TYPE##16]\
1022 = builtin->builtin_##TYPE##16
1024 FILL_TYPE_VECTOR (lai, char);
1025 FILL_TYPE_VECTOR (lai, uchar);
1026 FILL_TYPE_VECTOR (lai, short);
1027 FILL_TYPE_VECTOR (lai, ushort);
1028 FILL_TYPE_VECTOR (lai, int);
1029 FILL_TYPE_VECTOR (lai, uint);
1030 FILL_TYPE_VECTOR (lai, long);
1031 FILL_TYPE_VECTOR (lai, ulong);
1032 FILL_TYPE_VECTOR (lai, half);
1033 FILL_TYPE_VECTOR (lai, float);
1034 FILL_TYPE_VECTOR (lai, double);
1035 lai->primitive_type_vector [opencl_primitive_type_bool]
1036 = builtin->builtin_bool;
1037 lai->primitive_type_vector [opencl_primitive_type_unsigned_char]
1038 = builtin->builtin_unsigned_char;
1039 lai->primitive_type_vector [opencl_primitive_type_unsigned_short]
1040 = builtin->builtin_unsigned_short;
1041 lai->primitive_type_vector [opencl_primitive_type_unsigned_int]
1042 = builtin->builtin_unsigned_int;
1043 lai->primitive_type_vector [opencl_primitive_type_unsigned_long]
1044 = builtin->builtin_unsigned_long;
1045 lai->primitive_type_vector [opencl_primitive_type_half]
1046 = builtin->builtin_half;
1047 lai->primitive_type_vector [opencl_primitive_type_size_t]
1048 = builtin->builtin_size_t;
1049 lai->primitive_type_vector [opencl_primitive_type_ptrdiff_t]
1050 = builtin->builtin_ptrdiff_t;
1051 lai->primitive_type_vector [opencl_primitive_type_intptr_t]
1052 = builtin->builtin_intptr_t;
1053 lai->primitive_type_vector [opencl_primitive_type_uintptr_t]
1054 = builtin->builtin_uintptr_t;
1055 lai->primitive_type_vector [opencl_primitive_type_void]
1056 = builtin->builtin_void;
1058 /* Specifies the return type of logical and relational operations. */
1059 lai->bool_type_symbol = "int";
1060 lai->bool_type_default = builtin->builtin_int;
1063 const struct exp_descriptor exp_descriptor_opencl =
1065 print_subexp_standard,
1066 operator_length_standard,
1067 operator_check_standard,
1069 dump_subexp_body_standard,
1070 evaluate_subexp_opencl
1073 const struct language_defn opencl_language_defn =
1075 "opencl", /* Language name */
1082 &exp_descriptor_opencl,
1086 c_printchar, /* Print a character constant */
1087 c_printstr, /* Function to print string constant */
1088 c_emit_char, /* Print a single char */
1089 c_print_type, /* Print a type using appropriate syntax */
1090 c_print_typedef, /* Print a typedef using appropriate syntax */
1091 c_val_print, /* Print a value using appropriate syntax */
1092 c_value_print, /* Print a top-level value */
1093 NULL, /* Language specific skip_trampoline */
1094 NULL, /* name_of_this */
1095 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1096 basic_lookup_transparent_type,/* lookup_transparent_type */
1097 NULL, /* Language specific symbol demangler */
1098 NULL, /* Language specific
1099 class_name_from_physname */
1100 c_op_print_tab, /* expression operators for printing */
1101 1, /* c-style arrays */
1102 0, /* String lower bound */
1103 default_word_break_characters,
1104 default_make_symbol_completion_list,
1105 opencl_language_arch_info,
1106 default_print_array_index,
1107 default_pass_by_reference,
1113 build_opencl_types (struct gdbarch *gdbarch)
1115 struct builtin_opencl_type *builtin_opencl_type
1116 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_opencl_type);
1118 /* Helper macro to create strings. */
1119 #define OCL_STRING(S) #S
1120 /* This macro allocates and assigns the type struct pointers
1121 for the vector types. */
1122 #define BUILD_OCL_VTYPES(TYPE)\
1123 builtin_opencl_type->builtin_##TYPE##2\
1124 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 2);\
1125 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##2) = OCL_STRING(TYPE ## 2);\
1126 builtin_opencl_type->builtin_##TYPE##3\
1127 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 3);\
1128 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##3) = OCL_STRING(TYPE ## 3);\
1129 TYPE_LENGTH (builtin_opencl_type->builtin_##TYPE##3)\
1130 = 4 * TYPE_LENGTH (builtin_opencl_type->builtin_##TYPE);\
1131 builtin_opencl_type->builtin_##TYPE##4\
1132 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 4);\
1133 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##4) = OCL_STRING(TYPE ## 4);\
1134 builtin_opencl_type->builtin_##TYPE##8\
1135 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 8);\
1136 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##8) = OCL_STRING(TYPE ## 8);\
1137 builtin_opencl_type->builtin_##TYPE##16\
1138 = init_vector_type (builtin_opencl_type->builtin_##TYPE, 16);\
1139 TYPE_NAME (builtin_opencl_type->builtin_##TYPE##16) = OCL_STRING(TYPE ## 16)
1141 builtin_opencl_type->builtin_char
1142 = arch_integer_type (gdbarch, 8, 0, "char");
1143 BUILD_OCL_VTYPES (char);
1144 builtin_opencl_type->builtin_uchar
1145 = arch_integer_type (gdbarch, 8, 1, "uchar");
1146 BUILD_OCL_VTYPES (uchar);
1147 builtin_opencl_type->builtin_short
1148 = arch_integer_type (gdbarch, 16, 0, "short");
1149 BUILD_OCL_VTYPES (short);
1150 builtin_opencl_type->builtin_ushort
1151 = arch_integer_type (gdbarch, 16, 1, "ushort");
1152 BUILD_OCL_VTYPES (ushort);
1153 builtin_opencl_type->builtin_int
1154 = arch_integer_type (gdbarch, 32, 0, "int");
1155 BUILD_OCL_VTYPES (int);
1156 builtin_opencl_type->builtin_uint
1157 = arch_integer_type (gdbarch, 32, 1, "uint");
1158 BUILD_OCL_VTYPES (uint);
1159 builtin_opencl_type->builtin_long
1160 = arch_integer_type (gdbarch, 64, 0, "long");
1161 BUILD_OCL_VTYPES (long);
1162 builtin_opencl_type->builtin_ulong
1163 = arch_integer_type (gdbarch, 64, 1, "ulong");
1164 BUILD_OCL_VTYPES (ulong);
1165 builtin_opencl_type->builtin_half
1166 = arch_float_type (gdbarch, 16, "half", floatformats_ieee_half);
1167 BUILD_OCL_VTYPES (half);
1168 builtin_opencl_type->builtin_float
1169 = arch_float_type (gdbarch, 32, "float", floatformats_ieee_single);
1170 BUILD_OCL_VTYPES (float);
1171 builtin_opencl_type->builtin_double
1172 = arch_float_type (gdbarch, 64, "double", floatformats_ieee_double);
1173 BUILD_OCL_VTYPES (double);
1174 builtin_opencl_type->builtin_bool
1175 = arch_boolean_type (gdbarch, 8, 1, "bool");
1176 builtin_opencl_type->builtin_unsigned_char
1177 = arch_integer_type (gdbarch, 8, 1, "unsigned char");
1178 builtin_opencl_type->builtin_unsigned_short
1179 = arch_integer_type (gdbarch, 16, 1, "unsigned short");
1180 builtin_opencl_type->builtin_unsigned_int
1181 = arch_integer_type (gdbarch, 32, 1, "unsigned int");
1182 builtin_opencl_type->builtin_unsigned_long
1183 = arch_integer_type (gdbarch, 64, 1, "unsigned long");
1184 builtin_opencl_type->builtin_size_t
1185 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "size_t");
1186 builtin_opencl_type->builtin_ptrdiff_t
1187 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "ptrdiff_t");
1188 builtin_opencl_type->builtin_intptr_t
1189 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "intptr_t");
1190 builtin_opencl_type->builtin_uintptr_t
1191 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr_t");
1192 builtin_opencl_type->builtin_void
1193 = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
1195 return builtin_opencl_type;
1199 _initialize_opencl_language (void)
1201 opencl_type_data = gdbarch_data_register_post_init (build_opencl_types);
1202 add_language (&opencl_language_defn);