gdb/opencl-lang.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* OpenCL language support for GDB, the GNU debugger.
  2.    Copyright (C) 2010-2015 Free Software Foundation, Inc.

  3.    Contributed by Ken Werner <ken.werner@de.ibm.com>.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "gdbtypes.h"
  17. #include "symtab.h"
  18. #include "expression.h"
  19. #include "parser-defs.h"
  20. #include "language.h"
  21. #include "varobj.h"
  22. #include "c-lang.h"

  23. extern void _initialize_opencl_language (void);

  24. /* This macro generates enum values from a given type.  */

  25. #define OCL_P_TYPE(TYPE)\
  26.   opencl_primitive_type_##TYPE,\
  27.   opencl_primitive_type_##TYPE##2,\
  28.   opencl_primitive_type_##TYPE##3,\
  29.   opencl_primitive_type_##TYPE##4,\
  30.   opencl_primitive_type_##TYPE##8,\
  31.   opencl_primitive_type_##TYPE##16

  32. enum opencl_primitive_types {
  33.   OCL_P_TYPE (char),
  34.   OCL_P_TYPE (uchar),
  35.   OCL_P_TYPE (short),
  36.   OCL_P_TYPE (ushort),
  37.   OCL_P_TYPE (int),
  38.   OCL_P_TYPE (uint),
  39.   OCL_P_TYPE (long),
  40.   OCL_P_TYPE (ulong),
  41.   OCL_P_TYPE (half),
  42.   OCL_P_TYPE (float),
  43.   OCL_P_TYPE (double),
  44.   opencl_primitive_type_bool,
  45.   opencl_primitive_type_unsigned_char,
  46.   opencl_primitive_type_unsigned_short,
  47.   opencl_primitive_type_unsigned_int,
  48.   opencl_primitive_type_unsigned_long,
  49.   opencl_primitive_type_size_t,
  50.   opencl_primitive_type_ptrdiff_t,
  51.   opencl_primitive_type_intptr_t,
  52.   opencl_primitive_type_uintptr_t,
  53.   opencl_primitive_type_void,
  54.   nr_opencl_primitive_types
  55. };

  56. static struct gdbarch_data *opencl_type_data;

  57. static struct type **
  58. builtin_opencl_type (struct gdbarch *gdbarch)
  59. {
  60.   return gdbarch_data (gdbarch, opencl_type_data);
  61. }

  62. /* Returns the corresponding OpenCL vector type from the given type code,
  63.    the length of the element type, the unsigned flag and the amount of
  64.    elements (N).  */

  65. static struct type *
  66. lookup_opencl_vector_type (struct gdbarch *gdbarch, enum type_code code,
  67.                            unsigned int el_length, unsigned int flag_unsigned,
  68.                            int n)
  69. {
  70.   int i;
  71.   unsigned int length;
  72.   struct type *type = NULL;
  73.   struct type **types = builtin_opencl_type (gdbarch);

  74.   /* Check if n describes a valid OpenCL vector size (2, 3, 4, 8, 16).  */
  75.   if (n != 2 && n != 3 && n != 4 && n != 8 && n != 16)
  76.     error (_("Invalid OpenCL vector size: %d"), n);

  77.   /* Triple vectors have the size of a quad vector.  */
  78.   length = (n == 3) ?  el_length * 4 : el_length * n;

  79.   for (i = 0; i < nr_opencl_primitive_types; i++)
  80.     {
  81.       LONGEST lowb, highb;

  82.       if (TYPE_CODE (types[i]) == TYPE_CODE_ARRAY && TYPE_VECTOR (types[i])
  83.           && get_array_bounds (types[i], &lowb, &highb)
  84.           && TYPE_CODE (TYPE_TARGET_TYPE (types[i])) == code
  85.           && TYPE_UNSIGNED (TYPE_TARGET_TYPE (types[i])) == flag_unsigned
  86.           && TYPE_LENGTH (TYPE_TARGET_TYPE (types[i])) == el_length
  87.           && TYPE_LENGTH (types[i]) == length
  88.           && highb - lowb + 1 == n)
  89.         {
  90.           type = types[i];
  91.           break;
  92.         }
  93.     }

  94.   return type;
  95. }

  96. /* Returns nonzero if the array ARR contains duplicates within
  97.      the first N elements.  */

  98. static int
  99. array_has_dups (int *arr, int n)
  100. {
  101.   int i, j;

  102.   for (i = 0; i < n; i++)
  103.     {
  104.       for (j = i + 1; j < n; j++)
  105.         {
  106.           if (arr[i] == arr[j])
  107.             return 1;
  108.         }
  109.     }

  110.   return 0;
  111. }

  112. /* The OpenCL component access syntax allows to create lvalues referring to
  113.    selected elements of an original OpenCL vector in arbitrary order.  This
  114.    structure holds the information to describe such lvalues.  */

  115. struct lval_closure
  116. {
  117.   /* Reference count.  */
  118.   int refc;
  119.   /* The number of indices.  */
  120.   int n;
  121.   /* The element indices themselves.  */
  122.   int *indices;
  123.   /* A pointer to the original value.  */
  124.   struct value *val;
  125. };

  126. /* Allocates an instance of struct lval_closure.  */

  127. static struct lval_closure *
  128. allocate_lval_closure (int *indices, int n, struct value *val)
  129. {
  130.   struct lval_closure *c = XCNEW (struct lval_closure);

  131.   c->refc = 1;
  132.   c->n = n;
  133.   c->indices = XCNEWVEC (int, n);
  134.   memcpy (c->indices, indices, n * sizeof (int));
  135.   value_incref (val); /* Increment the reference counter of the value.  */
  136.   c->val = val;

  137.   return c;
  138. }

  139. static void
  140. lval_func_read (struct value *v)
  141. {
  142.   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
  143.   struct type *type = check_typedef (value_type (v));
  144.   struct type *eltype = TYPE_TARGET_TYPE (check_typedef (value_type (c->val)));
  145.   int offset = value_offset (v);
  146.   int elsize = TYPE_LENGTH (eltype);
  147.   int n, i, j = 0;
  148.   LONGEST lowb = 0;
  149.   LONGEST highb = 0;

  150.   if (TYPE_CODE (type) == TYPE_CODE_ARRAY
  151.       && !get_array_bounds (type, &lowb, &highb))
  152.     error (_("Could not determine the vector bounds"));

  153.   /* Assume elsize aligned offset.  */
  154.   gdb_assert (offset % elsize == 0);
  155.   offset /= elsize;
  156.   n = offset + highb - lowb + 1;
  157.   gdb_assert (n <= c->n);

  158.   for (i = offset; i < n; i++)
  159.     memcpy (value_contents_raw (v) + j++ * elsize,
  160.             value_contents (c->val) + c->indices[i] * elsize,
  161.             elsize);
  162. }

  163. static void
  164. lval_func_write (struct value *v, struct value *fromval)
  165. {
  166.   struct value *mark = value_mark ();
  167.   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
  168.   struct type *type = check_typedef (value_type (v));
  169.   struct type *eltype = TYPE_TARGET_TYPE (check_typedef (value_type (c->val)));
  170.   int offset = value_offset (v);
  171.   int elsize = TYPE_LENGTH (eltype);
  172.   int n, i, j = 0;
  173.   LONGEST lowb = 0;
  174.   LONGEST highb = 0;

  175.   if (TYPE_CODE (type) == TYPE_CODE_ARRAY
  176.       && !get_array_bounds (type, &lowb, &highb))
  177.     error (_("Could not determine the vector bounds"));

  178.   /* Assume elsize aligned offset.  */
  179.   gdb_assert (offset % elsize == 0);
  180.   offset /= elsize;
  181.   n = offset + highb - lowb + 1;

  182.   /* Since accesses to the fourth component of a triple vector is undefined we
  183.      just skip writes to the fourth element.  Imagine something like this:
  184.        int3 i3 = (int3)(0, 1, 2);
  185.        i3.hi.hi = 5;
  186.      In this case n would be 4 (offset=12/4 + 1) while c->n would be 3.  */
  187.   if (n > c->n)
  188.     n = c->n;

  189.   for (i = offset; i < n; i++)
  190.     {
  191.       struct value *from_elm_val = allocate_value (eltype);
  192.       struct value *to_elm_val = value_subscript (c->val, c->indices[i]);

  193.       memcpy (value_contents_writeable (from_elm_val),
  194.               value_contents (fromval) + j++ * elsize,
  195.               elsize);
  196.       value_assign (to_elm_val, from_elm_val);
  197.     }

  198.   value_free_to_mark (mark);
  199. }

  200. /* Return nonzero if bits in V from OFFSET and LENGTH represent a
  201.    synthetic pointer.  */

  202. static int
  203. lval_func_check_synthetic_pointer (const struct value *v,
  204.                                    int offset, int length)
  205. {
  206.   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
  207.   /* Size of the target type in bits.  */
  208.   int elsize =
  209.       TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c->val)))) * 8;
  210.   int startrest = offset % elsize;
  211.   int start = offset / elsize;
  212.   int endrest = (offset + length) % elsize;
  213.   int end = (offset + length) / elsize;
  214.   int i;

  215.   if (endrest)
  216.     end++;

  217.   if (end > c->n)
  218.     return 0;

  219.   for (i = start; i < end; i++)
  220.     {
  221.       int comp_offset = (i == start) ? startrest : 0;
  222.       int comp_length = (i == end) ? endrest : elsize;

  223.       if (!value_bits_synthetic_pointer (c->val,
  224.                                          c->indices[i] * elsize + comp_offset,
  225.                                          comp_length))
  226.         return 0;
  227.     }

  228.   return 1;
  229. }

  230. static void *
  231. lval_func_copy_closure (const struct value *v)
  232. {
  233.   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);

  234.   ++c->refc;

  235.   return c;
  236. }

  237. static void
  238. lval_func_free_closure (struct value *v)
  239. {
  240.   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);

  241.   --c->refc;

  242.   if (c->refc == 0)
  243.     {
  244.       value_free (c->val); /* Decrement the reference counter of the value.  */
  245.       xfree (c->indices);
  246.       xfree (c);
  247.     }
  248. }

  249. static const struct lval_funcs opencl_value_funcs =
  250.   {
  251.     lval_func_read,
  252.     lval_func_write,
  253.     NULL,        /* indirect */
  254.     NULL,        /* coerce_ref */
  255.     lval_func_check_synthetic_pointer,
  256.     lval_func_copy_closure,
  257.     lval_func_free_closure
  258.   };

  259. /* Creates a sub-vector from VAL.  The elements are selected by the indices of
  260.    an array with the length of N.  Supported values for NOSIDE are
  261.    EVAL_NORMAL and EVAL_AVOID_SIDE_EFFECTS.  */

  262. static struct value *
  263. create_value (struct gdbarch *gdbarch, struct value *val, enum noside noside,
  264.               int *indices, int n)
  265. {
  266.   struct type *type = check_typedef (value_type (val));
  267.   struct type *elm_type = TYPE_TARGET_TYPE (type);
  268.   struct value *ret;

  269.   /* Check if a single component of a vector is requested which means
  270.      the resulting type is a (primitive) scalar type.  */
  271.   if (n == 1)
  272.     {
  273.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  274.         ret = value_zero (elm_type, not_lval);
  275.       else
  276.         ret = value_subscript (val, indices[0]);
  277.     }
  278.   else
  279.     {
  280.       /* Multiple components of the vector are requested which means the
  281.          resulting type is a vector as well.  */
  282.       struct type *dst_type =
  283.         lookup_opencl_vector_type (gdbarch, TYPE_CODE (elm_type),
  284.                                    TYPE_LENGTH (elm_type),
  285.                                    TYPE_UNSIGNED (elm_type), n);

  286.       if (dst_type == NULL)
  287.         dst_type = init_vector_type (elm_type, n);

  288.       make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type), dst_type, NULL);

  289.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  290.         ret = allocate_value (dst_type);
  291.       else
  292.         {
  293.           /* Check whether to create a lvalue or not.  */
  294.           if (VALUE_LVAL (val) != not_lval && !array_has_dups (indices, n))
  295.             {
  296.               struct lval_closure *c = allocate_lval_closure (indices, n, val);
  297.               ret = allocate_computed_value (dst_type, &opencl_value_funcs, c);
  298.             }
  299.           else
  300.             {
  301.               int i;

  302.               ret = allocate_value (dst_type);

  303.               /* Copy src val contents into the destination value.  */
  304.               for (i = 0; i < n; i++)
  305.                 memcpy (value_contents_writeable (ret)
  306.                         + (i * TYPE_LENGTH (elm_type)),
  307.                         value_contents (val)
  308.                         + (indices[i] * TYPE_LENGTH (elm_type)),
  309.                         TYPE_LENGTH (elm_type));
  310.             }
  311.         }
  312.     }
  313.   return ret;
  314. }

  315. /* OpenCL vector component access.  */

  316. static struct value *
  317. opencl_component_ref (struct expression *exp, struct value *val, char *comps,
  318.                       enum noside noside)
  319. {
  320.   LONGEST lowb, highb;
  321.   int src_len;
  322.   struct value *v;
  323.   int indices[16], i;
  324.   int dst_len;

  325.   if (!get_array_bounds (check_typedef (value_type (val)), &lowb, &highb))
  326.     error (_("Could not determine the vector bounds"));

  327.   src_len = highb - lowb + 1;

  328.   /* Throw an error if the amount of array elements does not fit a
  329.      valid OpenCL vector size (2, 3, 4, 8, 16).  */
  330.   if (src_len != 2 && src_len != 3 && src_len != 4 && src_len != 8
  331.       && src_len != 16)
  332.     error (_("Invalid OpenCL vector size"));

  333.   if (strcmp (comps, "lo") == 0 )
  334.     {
  335.       dst_len = (src_len == 3) ? 2 : src_len / 2;

  336.       for (i = 0; i < dst_len; i++)
  337.         indices[i] = i;
  338.     }
  339.   else if (strcmp (comps, "hi") == 0)
  340.     {
  341.       dst_len = (src_len == 3) ? 2 : src_len / 2;

  342.       for (i = 0; i < dst_len; i++)
  343.         indices[i] = dst_len + i;
  344.     }
  345.   else if (strcmp (comps, "even") == 0)
  346.     {
  347.       dst_len = (src_len == 3) ? 2 : src_len / 2;

  348.       for (i = 0; i < dst_len; i++)
  349.         indices[i] = i*2;
  350.     }
  351.   else if (strcmp (comps, "odd") == 0)
  352.     {
  353.       dst_len = (src_len == 3) ? 2 : src_len / 2;

  354.       for (i = 0; i < dst_len; i++)
  355.         indices[i] = i*2+1;
  356.     }
  357.   else if (strncasecmp (comps, "s", 1) == 0)
  358.     {
  359. #define HEXCHAR_TO_INT(C) ((C >= '0' && C <= '9') ? \
  360.                            C-'0' : ((C >= 'A' && C <= 'F') ? \
  361.                            C-'A'+10 : ((C >= 'a' && C <= 'f') ? \
  362.                            C-'a'+10 : -1)))

  363.       dst_len = strlen (comps);
  364.       /* Skip the s/S-prefix.  */
  365.       dst_len--;

  366.       for (i = 0; i < dst_len; i++)
  367.         {
  368.           indices[i] = HEXCHAR_TO_INT(comps[i+1]);
  369.           /* Check if the requested component is invalid or exceeds
  370.              the vector.  */
  371.           if (indices[i] < 0 || indices[i] >= src_len)
  372.             error (_("Invalid OpenCL vector component accessor %s"), comps);
  373.         }
  374.     }
  375.   else
  376.     {
  377.       dst_len = strlen (comps);

  378.       for (i = 0; i < dst_len; i++)
  379.         {
  380.           /* x, y, z, w */
  381.           switch (comps[i])
  382.           {
  383.           case 'x':
  384.             indices[i] = 0;
  385.             break;
  386.           case 'y':
  387.             indices[i] = 1;
  388.             break;
  389.           case 'z':
  390.             if (src_len < 3)
  391.               error (_("Invalid OpenCL vector component accessor %s"), comps);
  392.             indices[i] = 2;
  393.             break;
  394.           case 'w':
  395.             if (src_len < 4)
  396.               error (_("Invalid OpenCL vector component accessor %s"), comps);
  397.             indices[i] = 3;
  398.             break;
  399.           default:
  400.             error (_("Invalid OpenCL vector component accessor %s"), comps);
  401.             break;
  402.           }
  403.         }
  404.     }

  405.   /* Throw an error if the amount of requested components does not
  406.      result in a valid length (1, 2, 3, 4, 8, 16).  */
  407.   if (dst_len != 1 && dst_len != 2 && dst_len != 3 && dst_len != 4
  408.       && dst_len != 8 && dst_len != 16)
  409.     error (_("Invalid OpenCL vector component accessor %s"), comps);

  410.   v = create_value (exp->gdbarch, val, noside, indices, dst_len);

  411.   return v;
  412. }

  413. /* Perform the unary logical not (!) operation.  */

  414. static struct value *
  415. opencl_logical_not (struct expression *exp, struct value *arg)
  416. {
  417.   struct type *type = check_typedef (value_type (arg));
  418.   struct type *rettype;
  419.   struct value *ret;

  420.   if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
  421.     {
  422.       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
  423.       LONGEST lowb, highb;
  424.       int i;

  425.       if (!get_array_bounds (type, &lowb, &highb))
  426.         error (_("Could not determine the vector bounds"));

  427.       /* Determine the resulting type of the operation and allocate the
  428.          value.  */
  429.       rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
  430.                                            TYPE_LENGTH (eltype), 0,
  431.                                            highb - lowb + 1);
  432.       ret = allocate_value (rettype);

  433.       for (i = 0; i < highb - lowb + 1; i++)
  434.         {
  435.           /* For vector types, the unary operator shall return a 0 if the
  436.           value of its operand compares unequal to 0, and -1 (i.e. all bits
  437.           set) if the value of its operand compares equal to 0.  */
  438.           int tmp = value_logical_not (value_subscript (arg, i)) ? -1 : 0;
  439.           memset (value_contents_writeable (ret) + i * TYPE_LENGTH (eltype),
  440.                   tmp, TYPE_LENGTH (eltype));
  441.         }
  442.     }
  443.   else
  444.     {
  445.       rettype = language_bool_type (exp->language_defn, exp->gdbarch);
  446.       ret = value_from_longest (rettype, value_logical_not (arg));
  447.     }

  448.   return ret;
  449. }

  450. /* Perform a relational operation on two scalar operands.  */

  451. static int
  452. scalar_relop (struct value *val1, struct value *val2, enum exp_opcode op)
  453. {
  454.   int ret;

  455.   switch (op)
  456.     {
  457.     case BINOP_EQUAL:
  458.       ret = value_equal (val1, val2);
  459.       break;
  460.     case BINOP_NOTEQUAL:
  461.       ret = !value_equal (val1, val2);
  462.       break;
  463.     case BINOP_LESS:
  464.       ret = value_less (val1, val2);
  465.       break;
  466.     case BINOP_GTR:
  467.       ret = value_less (val2, val1);
  468.       break;
  469.     case BINOP_GEQ:
  470.       ret = value_less (val2, val1) || value_equal (val1, val2);
  471.       break;
  472.     case BINOP_LEQ:
  473.       ret = value_less (val1, val2) || value_equal (val1, val2);
  474.       break;
  475.     case BINOP_LOGICAL_AND:
  476.       ret = !value_logical_not (val1) && !value_logical_not (val2);
  477.       break;
  478.     case BINOP_LOGICAL_OR:
  479.       ret = !value_logical_not (val1) || !value_logical_not (val2);
  480.       break;
  481.     default:
  482.       error (_("Attempt to perform an unsupported operation"));
  483.       break;
  484.     }
  485.   return ret;
  486. }

  487. /* Perform a relational operation on two vector operands.  */

  488. static struct value *
  489. vector_relop (struct expression *exp, struct value *val1, struct value *val2,
  490.               enum exp_opcode op)
  491. {
  492.   struct value *ret;
  493.   struct type *type1, *type2, *eltype1, *eltype2, *rettype;
  494.   int t1_is_vec, t2_is_vec, i;
  495.   LONGEST lowb1, lowb2, highb1, highb2;

  496.   type1 = check_typedef (value_type (val1));
  497.   type2 = check_typedef (value_type (val2));

  498.   t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1));
  499.   t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY && TYPE_VECTOR (type2));

  500.   if (!t1_is_vec || !t2_is_vec)
  501.     error (_("Vector operations are not supported on scalar types"));

  502.   eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
  503.   eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));

  504.   if (!get_array_bounds (type1,&lowb1, &highb1)
  505.       || !get_array_bounds (type2, &lowb2, &highb2))
  506.     error (_("Could not determine the vector bounds"));

  507.   /* Check whether the vector types are compatible.  */
  508.   if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
  509.       || TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
  510.       || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
  511.       || lowb1 != lowb2 || highb1 != highb2)
  512.     error (_("Cannot perform operation on vectors with different types"));

  513.   /* Determine the resulting type of the operation and allocate the value.  */
  514.   rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
  515.                                        TYPE_LENGTH (eltype1), 0,
  516.                                        highb1 - lowb1 + 1);
  517.   ret = allocate_value (rettype);

  518.   for (i = 0; i < highb1 - lowb1 + 1; i++)
  519.     {
  520.       /* For vector types, the relational, equality and logical operators shall
  521.          return 0 if the specified relation is false and -1 (i.e. all bits set)
  522.          if the specified relation is true.  */
  523.       int tmp = scalar_relop (value_subscript (val1, i),
  524.                               value_subscript (val2, i), op) ? -1 : 0;
  525.       memset (value_contents_writeable (ret) + i * TYPE_LENGTH (eltype1),
  526.               tmp, TYPE_LENGTH (eltype1));
  527.      }

  528.   return ret;
  529. }

  530. /* Perform a cast of ARG into TYPE.  There's sadly a lot of duplication in
  531.    here from valops.c:value_cast, opencl is different only in the
  532.    behaviour of scalar to vector casting.  As far as possibly we're going
  533.    to try and delegate back to the standard value_cast function. */

  534. static struct value *
  535. opencl_value_cast (struct type *type, struct value *arg)
  536. {
  537.   if (type != value_type (arg))
  538.     {
  539.       /* Casting scalar to vector is a special case for OpenCL, scalar
  540.          is cast to element type of vector then replicated into each
  541.          element of the vectorFirst though, we need to work out if
  542.          this is a scalar to vector cast; code lifted from
  543.          valops.c:value_cast.  */
  544.       enum type_code code1, code2;
  545.       struct type *to_type;
  546.       int scalar;

  547.       to_type = check_typedef (type);

  548.       code1 = TYPE_CODE (to_type);
  549.       code2 = TYPE_CODE (check_typedef (value_type (arg)));

  550.       if (code2 == TYPE_CODE_REF)
  551.         code2 = TYPE_CODE (check_typedef (value_type (coerce_ref (arg))));

  552.       scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL
  553.                 || code2 == TYPE_CODE_CHAR || code2 == TYPE_CODE_FLT
  554.                 || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
  555.                 || code2 == TYPE_CODE_RANGE);

  556.       if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (to_type) && scalar)
  557.         {
  558.           struct type *eltype;

  559.           /* Cast to the element type of the vector here as
  560.              value_vector_widen will error if the scalar value is
  561.              truncated by the cast.  To avoid the error, cast (and
  562.              possibly truncate) here.  */
  563.           eltype = check_typedef (TYPE_TARGET_TYPE (to_type));
  564.           arg = value_cast (eltype, arg);

  565.           return value_vector_widen (arg, type);
  566.         }
  567.       else
  568.         /* Standard cast handler.  */
  569.         arg = value_cast (type, arg);
  570.     }
  571.   return arg;
  572. }

  573. /* Perform a relational operation on two operands.  */

  574. static struct value *
  575. opencl_relop (struct expression *exp, struct value *arg1, struct value *arg2,
  576.               enum exp_opcode op)
  577. {
  578.   struct value *val;
  579.   struct type *type1 = check_typedef (value_type (arg1));
  580.   struct type *type2 = check_typedef (value_type (arg2));
  581.   int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
  582.                    && TYPE_VECTOR (type1));
  583.   int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
  584.                    && TYPE_VECTOR (type2));

  585.   if (!t1_is_vec && !t2_is_vec)
  586.     {
  587.       int tmp = scalar_relop (arg1, arg2, op);
  588.       struct type *type =
  589.         language_bool_type (exp->language_defn, exp->gdbarch);

  590.       val = value_from_longest (type, tmp);
  591.     }
  592.   else if (t1_is_vec && t2_is_vec)
  593.     {
  594.       val = vector_relop (exp, arg1, arg2, op);
  595.     }
  596.   else
  597.     {
  598.       /* Widen the scalar operand to a vector.  */
  599.       struct value **v = t1_is_vec ? &arg2 : &arg1;
  600.       struct type *t = t1_is_vec ? type2 : type1;

  601.       if (TYPE_CODE (t) != TYPE_CODE_FLT && !is_integral_type (t))
  602.         error (_("Argument to operation not a number or boolean."));

  603.       *v = opencl_value_cast (t1_is_vec ? type1 : type2, *v);
  604.       val = vector_relop (exp, arg1, arg2, op);
  605.     }

  606.   return val;
  607. }

  608. /* Expression evaluator for the OpenCL.  Most operations are delegated to
  609.    evaluate_subexp_standard; see that function for a description of the
  610.    arguments.  */

  611. static struct value *
  612. evaluate_subexp_opencl (struct type *expect_type, struct expression *exp,
  613.                    int *pos, enum noside noside)
  614. {
  615.   enum exp_opcode op = exp->elts[*pos].opcode;
  616.   struct value *arg1 = NULL;
  617.   struct value *arg2 = NULL;
  618.   struct type *type1, *type2;

  619.   switch (op)
  620.     {
  621.     /* Handle assignment and cast operators to support OpenCL-style
  622.        scalar-to-vector widening.  */
  623.     case BINOP_ASSIGN:
  624.       (*pos)++;
  625.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  626.       type1 = value_type (arg1);
  627.       arg2 = evaluate_subexp (type1, exp, pos, noside);

  628.       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  629.         return arg1;

  630.       if (deprecated_value_modifiable (arg1)
  631.           && VALUE_LVAL (arg1) != lval_internalvar)
  632.         arg2 = opencl_value_cast (type1, arg2);

  633.       return value_assign (arg1, arg2);

  634.     case UNOP_CAST:
  635.       type1 = exp->elts[*pos + 1].type;
  636.       (*pos) += 2;
  637.       arg1 = evaluate_subexp (type1, exp, pos, noside);

  638.       if (noside == EVAL_SKIP)
  639.         return value_from_longest (builtin_type (exp->gdbarch)->
  640.                                    builtin_int, 1);

  641.       return opencl_value_cast (type1, arg1);

  642.     case UNOP_CAST_TYPE:
  643.       (*pos)++;
  644.       arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
  645.       type1 = value_type (arg1);
  646.       arg1 = evaluate_subexp (type1, exp, pos, noside);

  647.       if (noside == EVAL_SKIP)
  648.         return value_from_longest (builtin_type (exp->gdbarch)->
  649.                                    builtin_int, 1);

  650.       return opencl_value_cast (type1, arg1);

  651.     /* Handle binary relational and equality operators that are either not
  652.        or differently defined for GNU vectors.  */
  653.     case BINOP_EQUAL:
  654.     case BINOP_NOTEQUAL:
  655.     case BINOP_LESS:
  656.     case BINOP_GTR:
  657.     case BINOP_GEQ:
  658.     case BINOP_LEQ:
  659.       (*pos)++;
  660.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  661.       arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);

  662.       if (noside == EVAL_SKIP)
  663.         return value_from_longest (builtin_type (exp->gdbarch)->
  664.                                    builtin_int, 1);

  665.       return opencl_relop (exp, arg1, arg2, op);

  666.     /* Handle the logical unary operator not(!).  */
  667.     case UNOP_LOGICAL_NOT:
  668.       (*pos)++;
  669.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);

  670.       if (noside == EVAL_SKIP)
  671.         return value_from_longest (builtin_type (exp->gdbarch)->
  672.                                    builtin_int, 1);

  673.       return opencl_logical_not (exp, arg1);

  674.     /* Handle the logical operator and(&&) and or(||).  */
  675.     case BINOP_LOGICAL_AND:
  676.     case BINOP_LOGICAL_OR:
  677.       (*pos)++;
  678.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);

  679.       if (noside == EVAL_SKIP)
  680.         {
  681.           evaluate_subexp (NULL_TYPE, exp, pos, noside);

  682.           return value_from_longest (builtin_type (exp->gdbarch)->
  683.                                      builtin_int, 1);
  684.         }
  685.       else
  686.         {
  687.           /* For scalar operations we need to avoid evaluating operands
  688.              unecessarily.  However, for vector operations we always need to
  689.              evaluate both operands.  Unfortunately we only know which of the
  690.              two cases apply after we know the type of the second operand.
  691.              Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS.  */
  692.           int oldpos = *pos;

  693.           arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
  694.                                   EVAL_AVOID_SIDE_EFFECTS);
  695.           *pos = oldpos;
  696.           type1 = check_typedef (value_type (arg1));
  697.           type2 = check_typedef (value_type (arg2));

  698.           if ((TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
  699.               || (TYPE_CODE (type2) == TYPE_CODE_ARRAY && TYPE_VECTOR (type2)))
  700.             {
  701.               arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);

  702.               return opencl_relop (exp, arg1, arg2, op);
  703.             }
  704.           else
  705.             {
  706.               /* For scalar built-in types, only evaluate the right
  707.                  hand operand if the left hand operand compares
  708.                  unequal(&&)/equal(||) to 0.  */
  709.               int res;
  710.               int tmp = value_logical_not (arg1);

  711.               if (op == BINOP_LOGICAL_OR)
  712.                 tmp = !tmp;

  713.               arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
  714.                                       tmp ? EVAL_SKIP : noside);
  715.               type1 = language_bool_type (exp->language_defn, exp->gdbarch);

  716.               if (op == BINOP_LOGICAL_AND)
  717.                 res = !tmp && !value_logical_not (arg2);
  718.               else /* BINOP_LOGICAL_OR */
  719.                 res = tmp || !value_logical_not (arg2);

  720.               return value_from_longest (type1, res);
  721.             }
  722.         }

  723.     /* Handle the ternary selection operator.  */
  724.     case TERNOP_COND:
  725.       (*pos)++;
  726.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  727.       type1 = check_typedef (value_type (arg1));
  728.       if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
  729.         {
  730.           struct value *arg3, *tmp, *ret;
  731.           struct type *eltype2, *type3, *eltype3;
  732.           int t2_is_vec, t3_is_vec, i;
  733.           LONGEST lowb1, lowb2, lowb3, highb1, highb2, highb3;

  734.           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  735.           arg3 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  736.           type2 = check_typedef (value_type (arg2));
  737.           type3 = check_typedef (value_type (arg3));
  738.           t2_is_vec
  739.             = TYPE_CODE (type2) == TYPE_CODE_ARRAY && TYPE_VECTOR (type2);
  740.           t3_is_vec
  741.             = TYPE_CODE (type3) == TYPE_CODE_ARRAY && TYPE_VECTOR (type3);

  742.           /* Widen the scalar operand to a vector if necessary.  */
  743.           if (t2_is_vec || !t3_is_vec)
  744.             {
  745.               arg3 = opencl_value_cast (type2, arg3);
  746.               type3 = value_type (arg3);
  747.             }
  748.           else if (!t2_is_vec || t3_is_vec)
  749.             {
  750.               arg2 = opencl_value_cast (type3, arg2);
  751.               type2 = value_type (arg2);
  752.             }
  753.           else if (!t2_is_vec || !t3_is_vec)
  754.             {
  755.               /* Throw an error if arg2 or arg3 aren't vectors.  */
  756.               error (_("\
  757. Cannot perform conditional operation on incompatible types"));
  758.             }

  759.           eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
  760.           eltype3 = check_typedef (TYPE_TARGET_TYPE (type3));

  761.           if (!get_array_bounds (type1, &lowb1, &highb1)
  762.               || !get_array_bounds (type2, &lowb2, &highb2)
  763.               || !get_array_bounds (type3, &lowb3, &highb3))
  764.             error (_("Could not determine the vector bounds"));

  765.           /* Throw an error if the types of arg2 or arg3 are incompatible.  */
  766.           if (TYPE_CODE (eltype2) != TYPE_CODE (eltype3)
  767.               || TYPE_LENGTH (eltype2) != TYPE_LENGTH (eltype3)
  768.               || TYPE_UNSIGNED (eltype2) != TYPE_UNSIGNED (eltype3)
  769.               || lowb2 != lowb3 || highb2 != highb3)
  770.             error (_("\
  771. Cannot perform operation on vectors with different types"));

  772.           /* Throw an error if the sizes of arg1 and arg2/arg3 differ.  */
  773.           if (lowb1 != lowb2 || lowb1 != lowb3
  774.               || highb1 != highb2 || highb1 != highb3)
  775.             error (_("\
  776. Cannot perform conditional operation on vectors with different sizes"));

  777.           ret = allocate_value (type2);

  778.           for (i = 0; i < highb1 - lowb1 + 1; i++)
  779.             {
  780.               tmp = value_logical_not (value_subscript (arg1, i)) ?
  781.                     value_subscript (arg3, i) : value_subscript (arg2, i);
  782.               memcpy (value_contents_writeable (ret) +
  783.                       i * TYPE_LENGTH (eltype2), value_contents_all (tmp),
  784.                       TYPE_LENGTH (eltype2));
  785.             }

  786.           return ret;
  787.         }
  788.       else
  789.         {
  790.           if (value_logical_not (arg1))
  791.             {
  792.               /* Skip the second operand.  */
  793.               evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);

  794.               return evaluate_subexp (NULL_TYPE, exp, pos, noside);
  795.             }
  796.           else
  797.             {
  798.               /* Skip the third operand.  */
  799.               arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  800.               evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);

  801.               return arg2;
  802.             }
  803.         }

  804.     /* Handle STRUCTOP_STRUCT to allow component access on OpenCL vectors.  */
  805.     case STRUCTOP_STRUCT:
  806.       {
  807.         int pc = (*pos)++;
  808.         int tem = longest_to_int (exp->elts[pc + 1].longconst);

  809.         (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
  810.         arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  811.         type1 = check_typedef (value_type (arg1));

  812.         if (noside == EVAL_SKIP)
  813.           {
  814.             return value_from_longest (builtin_type (exp->gdbarch)->
  815.                                        builtin_int, 1);
  816.           }
  817.         else if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
  818.           {
  819.             return opencl_component_ref (exp, arg1, &exp->elts[pc + 2].string,
  820.                                          noside);
  821.           }
  822.         else
  823.           {
  824.             struct value *v = value_struct_elt (&arg1, NULL,
  825.                                                 &exp->elts[pc + 2].string, NULL,
  826.                                                 "structure");

  827.             if (noside == EVAL_AVOID_SIDE_EFFECTS)
  828.               v = value_zero (value_type (v), not_lval);
  829.             return v;
  830.           }
  831.       }
  832.     default:
  833.       break;
  834.     }

  835.   return evaluate_subexp_c (expect_type, exp, pos, noside);
  836. }

  837. /* Print OpenCL types.  */

  838. static void
  839. opencl_print_type (struct type *type, const char *varstring,
  840.                    struct ui_file *stream, int show, int level,
  841.                    const struct type_print_options *flags)
  842. {
  843.   /* We nearly always defer to C type printing, except that vector
  844.      types are considered primitive in OpenCL, and should always
  845.      be printed using their TYPE_NAME.  */
  846.   if (show > 0)
  847.     {
  848.       CHECK_TYPEDEF (type);
  849.       if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
  850.           && TYPE_NAME (type) != NULL)
  851.         show = 0;
  852.     }

  853.   c_print_type (type, varstring, stream, show, level, flags);
  854. }

  855. static void
  856. opencl_language_arch_info (struct gdbarch *gdbarch,
  857.                            struct language_arch_info *lai)
  858. {
  859.   struct type **types = builtin_opencl_type (gdbarch);

  860.   /* Copy primitive types vector from gdbarch.  */
  861.   lai->primitive_type_vector = types;

  862.   /* Type of elements of strings.  */
  863.   lai->string_char_type = types [opencl_primitive_type_char];

  864.   /* Specifies the return type of logical and relational operations.  */
  865.   lai->bool_type_symbol = "int";
  866.   lai->bool_type_default = types [opencl_primitive_type_int];
  867. }

  868. const struct exp_descriptor exp_descriptor_opencl =
  869. {
  870.   print_subexp_standard,
  871.   operator_length_standard,
  872.   operator_check_standard,
  873.   op_name_standard,
  874.   dump_subexp_body_standard,
  875.   evaluate_subexp_opencl
  876. };

  877. const struct language_defn opencl_language_defn =
  878. {
  879.   "opencl",                        /* Language name */
  880.   "OpenCL C",
  881.   language_opencl,
  882.   range_check_off,
  883.   case_sensitive_on,
  884.   array_row_major,
  885.   macro_expansion_c,
  886.   &exp_descriptor_opencl,
  887.   c_parse,
  888.   c_error,
  889.   null_post_parser,
  890.   c_printchar,                        /* Print a character constant */
  891.   c_printstr,                        /* Function to print string constant */
  892.   c_emit_char,                        /* Print a single char */
  893.   opencl_print_type,                /* Print a type using appropriate syntax */
  894.   c_print_typedef,                /* Print a typedef using appropriate syntax */
  895.   c_val_print,                        /* Print a value using appropriate syntax */
  896.   c_value_print,                /* Print a top-level value */
  897.   default_read_var_value,        /* la_read_var_value */
  898.   NULL,                                /* Language specific skip_trampoline */
  899.   NULL,                         /* name_of_this */
  900.   basic_lookup_symbol_nonlocal,        /* lookup_symbol_nonlocal */
  901.   basic_lookup_transparent_type,/* lookup_transparent_type */
  902.   NULL,                                /* Language specific symbol demangler */
  903.   NULL,                                /* Language specific
  904.                                    class_name_from_physname */
  905.   c_op_print_tab,                /* expression operators for printing */
  906.   1,                                /* c-style arrays */
  907.   0,                                /* String lower bound */
  908.   default_word_break_characters,
  909.   default_make_symbol_completion_list,
  910.   opencl_language_arch_info,
  911.   default_print_array_index,
  912.   default_pass_by_reference,
  913.   c_get_string,
  914.   NULL,                                /* la_get_symbol_name_cmp */
  915.   iterate_over_symbols,
  916.   &default_varobj_ops,
  917.   NULL,
  918.   NULL,
  919.   LANG_MAGIC
  920. };

  921. static void *
  922. build_opencl_types (struct gdbarch *gdbarch)
  923. {
  924.   struct type **types
  925.     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_opencl_primitive_types + 1,
  926.                               struct type *);

  927. /* Helper macro to create strings.  */
  928. #define OCL_STRING(S) #S
  929. /* This macro allocates and assigns the type struct pointers
  930.    for the vector types.  */
  931. #define BUILD_OCL_VTYPES(TYPE)\
  932.   types[opencl_primitive_type_##TYPE##2] \
  933.     = init_vector_type (types[opencl_primitive_type_##TYPE], 2); \
  934.   TYPE_NAME (types[opencl_primitive_type_##TYPE##2]) = OCL_STRING(TYPE ## 2); \
  935.   types[opencl_primitive_type_##TYPE##3] \
  936.     = init_vector_type (types[opencl_primitive_type_##TYPE], 3); \
  937.   TYPE_NAME (types[opencl_primitive_type_##TYPE##3]) = OCL_STRING(TYPE ## 3); \
  938.   TYPE_LENGTH (types[opencl_primitive_type_##TYPE##3]) \
  939.     = 4 * TYPE_LENGTH (types[opencl_primitive_type_##TYPE]); \
  940.   types[opencl_primitive_type_##TYPE##4] \
  941.     = init_vector_type (types[opencl_primitive_type_##TYPE], 4); \
  942.   TYPE_NAME (types[opencl_primitive_type_##TYPE##4]) = OCL_STRING(TYPE ## 4); \
  943.   types[opencl_primitive_type_##TYPE##8] \
  944.     = init_vector_type (types[opencl_primitive_type_##TYPE], 8); \
  945.   TYPE_NAME (types[opencl_primitive_type_##TYPE##8]) = OCL_STRING(TYPE ## 8); \
  946.   types[opencl_primitive_type_##TYPE##16] \
  947.     = init_vector_type (types[opencl_primitive_type_##TYPE], 16); \
  948.   TYPE_NAME (types[opencl_primitive_type_##TYPE##16]) = OCL_STRING(TYPE ## 16)

  949.   types[opencl_primitive_type_char]
  950.     = arch_integer_type (gdbarch, 8, 0, "char");
  951.   BUILD_OCL_VTYPES (char);
  952.   types[opencl_primitive_type_uchar]
  953.     = arch_integer_type (gdbarch, 8, 1, "uchar");
  954.   BUILD_OCL_VTYPES (uchar);
  955.   types[opencl_primitive_type_short]
  956.     = arch_integer_type (gdbarch, 16, 0, "short");
  957.   BUILD_OCL_VTYPES (short);
  958.   types[opencl_primitive_type_ushort]
  959.     = arch_integer_type (gdbarch, 16, 1, "ushort");
  960.   BUILD_OCL_VTYPES (ushort);
  961.   types[opencl_primitive_type_int]
  962.     = arch_integer_type (gdbarch, 32, 0, "int");
  963.   BUILD_OCL_VTYPES (int);
  964.   types[opencl_primitive_type_uint]
  965.     = arch_integer_type (gdbarch, 32, 1, "uint");
  966.   BUILD_OCL_VTYPES (uint);
  967.   types[opencl_primitive_type_long]
  968.     = arch_integer_type (gdbarch, 64, 0, "long");
  969.   BUILD_OCL_VTYPES (long);
  970.   types[opencl_primitive_type_ulong]
  971.     = arch_integer_type (gdbarch, 64, 1, "ulong");
  972.   BUILD_OCL_VTYPES (ulong);
  973.   types[opencl_primitive_type_half]
  974.     = arch_float_type (gdbarch, 16, "half", floatformats_ieee_half);
  975.   BUILD_OCL_VTYPES (half);
  976.   types[opencl_primitive_type_float]
  977.     = arch_float_type (gdbarch, 32, "float", floatformats_ieee_single);
  978.   BUILD_OCL_VTYPES (float);
  979.   types[opencl_primitive_type_double]
  980.     = arch_float_type (gdbarch, 64, "double", floatformats_ieee_double);
  981.   BUILD_OCL_VTYPES (double);
  982.   types[opencl_primitive_type_bool]
  983.     = arch_boolean_type (gdbarch, 8, 1, "bool");
  984.   types[opencl_primitive_type_unsigned_char]
  985.     = arch_integer_type (gdbarch, 8, 1, "unsigned char");
  986.   types[opencl_primitive_type_unsigned_short]
  987.     = arch_integer_type (gdbarch, 16, 1, "unsigned short");
  988.   types[opencl_primitive_type_unsigned_int]
  989.     = arch_integer_type (gdbarch, 32, 1, "unsigned int");
  990.   types[opencl_primitive_type_unsigned_long]
  991.     = arch_integer_type (gdbarch, 64, 1, "unsigned long");
  992.   types[opencl_primitive_type_size_t]
  993.     = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "size_t");
  994.   types[opencl_primitive_type_ptrdiff_t]
  995.     = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "ptrdiff_t");
  996.   types[opencl_primitive_type_intptr_t]
  997.     = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "intptr_t");
  998.   types[opencl_primitive_type_uintptr_t]
  999.     = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr_t");
  1000.   types[opencl_primitive_type_void]
  1001.     = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");

  1002.   return types;
  1003. }

  1004. /* Provide a prototype to silence -Wmissing-prototypes.  */
  1005. extern initialize_file_ftype _initialize_opencl_language;

  1006. void
  1007. _initialize_opencl_language (void)
  1008. {
  1009.   opencl_type_data = gdbarch_data_register_post_init (build_opencl_types);
  1010.   add_language (&opencl_language_defn);
  1011. }