Update Copyright year range in all files maintained by GDB.
[external/binutils.git] / gdb / opencl-lang.c
1 /* OpenCL language support for GDB, the GNU debugger.
2    Copyright (C) 2010-2014 Free Software Foundation, Inc.
3
4    Contributed by Ken Werner <ken.werner@de.ibm.com>.
5
6    This file is part of GDB.
7
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.
12
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.
17
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/>.  */
20
21 #include "defs.h"
22 #include <string.h>
23 #include "gdbtypes.h"
24 #include "symtab.h"
25 #include "expression.h"
26 #include "parser-defs.h"
27 #include "symtab.h"
28 #include "language.h"
29 #include "varobj.h"
30 #include "c-lang.h"
31 #include "gdb_assert.h"
32
33 extern void _initialize_opencl_language (void);
34
35 /* This macro generates enum values from a given type.  */
36
37 #define OCL_P_TYPE(TYPE)\
38   opencl_primitive_type_##TYPE,\
39   opencl_primitive_type_##TYPE##2,\
40   opencl_primitive_type_##TYPE##3,\
41   opencl_primitive_type_##TYPE##4,\
42   opencl_primitive_type_##TYPE##8,\
43   opencl_primitive_type_##TYPE##16
44
45 enum opencl_primitive_types {
46   OCL_P_TYPE (char),
47   OCL_P_TYPE (uchar),
48   OCL_P_TYPE (short),
49   OCL_P_TYPE (ushort),
50   OCL_P_TYPE (int),
51   OCL_P_TYPE (uint),
52   OCL_P_TYPE (long),
53   OCL_P_TYPE (ulong),
54   OCL_P_TYPE (half),
55   OCL_P_TYPE (float),
56   OCL_P_TYPE (double),
57   opencl_primitive_type_bool,
58   opencl_primitive_type_unsigned_char,
59   opencl_primitive_type_unsigned_short,
60   opencl_primitive_type_unsigned_int,
61   opencl_primitive_type_unsigned_long,
62   opencl_primitive_type_size_t,
63   opencl_primitive_type_ptrdiff_t,
64   opencl_primitive_type_intptr_t,
65   opencl_primitive_type_uintptr_t,
66   opencl_primitive_type_void,
67   nr_opencl_primitive_types
68 };
69
70 static struct gdbarch_data *opencl_type_data;
71
72 static struct type **
73 builtin_opencl_type (struct gdbarch *gdbarch)
74 {
75   return gdbarch_data (gdbarch, opencl_type_data);
76 }
77
78 /* Returns the corresponding OpenCL vector type from the given type code,
79    the length of the element type, the unsigned flag and the amount of
80    elements (N).  */
81
82 static struct type *
83 lookup_opencl_vector_type (struct gdbarch *gdbarch, enum type_code code,
84                            unsigned int el_length, unsigned int flag_unsigned,
85                            int n)
86 {
87   int i;
88   unsigned int length;
89   struct type *type = NULL;
90   struct type **types = builtin_opencl_type (gdbarch);
91
92   /* Check if n describes a valid OpenCL vector size (2, 3, 4, 8, 16).  */
93   if (n != 2 && n != 3 && n != 4 && n != 8 && n != 16)
94     error (_("Invalid OpenCL vector size: %d"), n);
95
96   /* Triple vectors have the size of a quad vector.  */
97   length = (n == 3) ?  el_length * 4 : el_length * n;
98
99   for (i = 0; i < nr_opencl_primitive_types; i++)
100     {
101       LONGEST lowb, highb;
102
103       if (TYPE_CODE (types[i]) == TYPE_CODE_ARRAY && TYPE_VECTOR (types[i])
104           && get_array_bounds (types[i], &lowb, &highb)
105           && TYPE_CODE (TYPE_TARGET_TYPE (types[i])) == code
106           && TYPE_UNSIGNED (TYPE_TARGET_TYPE (types[i])) == flag_unsigned
107           && TYPE_LENGTH (TYPE_TARGET_TYPE (types[i])) == el_length
108           && TYPE_LENGTH (types[i]) == length
109           && highb - lowb + 1 == n)
110         {
111           type = types[i];
112           break;
113         }
114     }
115
116   return type;
117 }
118
119 /* Returns nonzero if the array ARR contains duplicates within
120      the first N elements.  */
121
122 static int
123 array_has_dups (int *arr, int n)
124 {
125   int i, j;
126
127   for (i = 0; i < n; i++)
128     {
129       for (j = i + 1; j < n; j++)
130         {
131           if (arr[i] == arr[j])
132             return 1;
133         }
134     }
135
136   return 0;
137 }
138
139 /* The OpenCL component access syntax allows to create lvalues referring to
140    selected elements of an original OpenCL vector in arbitrary order.  This
141    structure holds the information to describe such lvalues.  */
142
143 struct lval_closure
144 {
145   /* Reference count.  */
146   int refc;
147   /* The number of indices.  */
148   int n;
149   /* The element indices themselves.  */
150   int *indices;
151   /* A pointer to the original value.  */
152   struct value *val;
153 };
154
155 /* Allocates an instance of struct lval_closure.  */
156
157 static struct lval_closure *
158 allocate_lval_closure (int *indices, int n, struct value *val)
159 {
160   struct lval_closure *c = XZALLOC (struct lval_closure);
161
162   c->refc = 1;
163   c->n = n;
164   c->indices = XCALLOC (n, int);
165   memcpy (c->indices, indices, n * sizeof (int));
166   value_incref (val); /* Increment the reference counter of the value.  */
167   c->val = val;
168
169   return c;
170 }
171
172 static void
173 lval_func_read (struct value *v)
174 {
175   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
176   struct type *type = check_typedef (value_type (v));
177   struct type *eltype = TYPE_TARGET_TYPE (check_typedef (value_type (c->val)));
178   int offset = value_offset (v);
179   int elsize = TYPE_LENGTH (eltype);
180   int n, i, j = 0;
181   LONGEST lowb = 0;
182   LONGEST highb = 0;
183
184   if (TYPE_CODE (type) == TYPE_CODE_ARRAY
185       && !get_array_bounds (type, &lowb, &highb))
186     error (_("Could not determine the vector bounds"));
187
188   /* Assume elsize aligned offset.  */
189   gdb_assert (offset % elsize == 0);
190   offset /= elsize;
191   n = offset + highb - lowb + 1;
192   gdb_assert (n <= c->n);
193
194   for (i = offset; i < n; i++)
195     memcpy (value_contents_raw (v) + j++ * elsize,
196             value_contents (c->val) + c->indices[i] * elsize,
197             elsize);
198 }
199
200 static void
201 lval_func_write (struct value *v, struct value *fromval)
202 {
203   struct value *mark = value_mark ();
204   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
205   struct type *type = check_typedef (value_type (v));
206   struct type *eltype = TYPE_TARGET_TYPE (check_typedef (value_type (c->val)));
207   int offset = value_offset (v);
208   int elsize = TYPE_LENGTH (eltype);
209   int n, i, j = 0;
210   LONGEST lowb = 0;
211   LONGEST highb = 0;
212
213   if (TYPE_CODE (type) == TYPE_CODE_ARRAY
214       && !get_array_bounds (type, &lowb, &highb))
215     error (_("Could not determine the vector bounds"));
216
217   /* Assume elsize aligned offset.  */
218   gdb_assert (offset % elsize == 0);
219   offset /= elsize;
220   n = offset + highb - lowb + 1;
221
222   /* Since accesses to the fourth component of a triple vector is undefined we
223      just skip writes to the fourth element.  Imagine something like this:
224        int3 i3 = (int3)(0, 1, 2);
225        i3.hi.hi = 5;
226      In this case n would be 4 (offset=12/4 + 1) while c->n would be 3.  */
227   if (n > c->n)
228     n = c->n;
229
230   for (i = offset; i < n; i++)
231     {
232       struct value *from_elm_val = allocate_value (eltype);
233       struct value *to_elm_val = value_subscript (c->val, c->indices[i]);
234
235       memcpy (value_contents_writeable (from_elm_val),
236               value_contents (fromval) + j++ * elsize,
237               elsize);
238       value_assign (to_elm_val, from_elm_val);
239     }
240
241   value_free_to_mark (mark);
242 }
243
244 /* Return nonzero if all bits in V within OFFSET and LENGTH are valid.  */
245
246 static int
247 lval_func_check_validity (const struct value *v, int offset, int length)
248 {
249   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
250   /* Size of the target type in bits.  */
251   int elsize =
252       TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c->val)))) * 8;
253   int startrest = offset % elsize;
254   int start = offset / elsize;
255   int endrest = (offset + length) % elsize;
256   int end = (offset + length) / elsize;
257   int i;
258
259   if (endrest)
260     end++;
261
262   if (end > c->n)
263     return 0;
264
265   for (i = start; i < end; i++)
266     {
267       int comp_offset = (i == start) ? startrest : 0;
268       int comp_length = (i == end) ? endrest : elsize;
269
270       if (!value_bits_valid (c->val, c->indices[i] * elsize + comp_offset,
271                              comp_length))
272         return 0;
273     }
274
275   return 1;
276 }
277
278 /* Return nonzero if any bit in V is valid.  */
279
280 static int
281 lval_func_check_any_valid (const struct value *v)
282 {
283   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
284   /* Size of the target type in bits.  */
285   int elsize =
286       TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c->val)))) * 8;
287   int i;
288
289   for (i = 0; i < c->n; i++)
290     if (value_bits_valid (c->val, c->indices[i] * elsize, elsize))
291       return 1;
292
293   return 0;
294 }
295
296 /* Return nonzero if bits in V from OFFSET and LENGTH represent a
297    synthetic pointer.  */
298
299 static int
300 lval_func_check_synthetic_pointer (const struct value *v,
301                                    int offset, int length)
302 {
303   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
304   /* Size of the target type in bits.  */
305   int elsize =
306       TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c->val)))) * 8;
307   int startrest = offset % elsize;
308   int start = offset / elsize;
309   int endrest = (offset + length) % elsize;
310   int end = (offset + length) / elsize;
311   int i;
312
313   if (endrest)
314     end++;
315
316   if (end > c->n)
317     return 0;
318
319   for (i = start; i < end; i++)
320     {
321       int comp_offset = (i == start) ? startrest : 0;
322       int comp_length = (i == end) ? endrest : elsize;
323
324       if (!value_bits_synthetic_pointer (c->val,
325                                          c->indices[i] * elsize + comp_offset,
326                                          comp_length))
327         return 0;
328     }
329
330   return 1;
331 }
332
333 static void *
334 lval_func_copy_closure (const struct value *v)
335 {
336   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
337
338   ++c->refc;
339
340   return c;
341 }
342
343 static void
344 lval_func_free_closure (struct value *v)
345 {
346   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
347
348   --c->refc;
349
350   if (c->refc == 0)
351     {
352       value_free (c->val); /* Decrement the reference counter of the value.  */
353       xfree (c->indices);
354       xfree (c);
355     }
356 }
357
358 static const struct lval_funcs opencl_value_funcs =
359   {
360     lval_func_read,
361     lval_func_write,
362     lval_func_check_validity,
363     lval_func_check_any_valid,
364     NULL,       /* indirect */
365     NULL,       /* coerce_ref */
366     lval_func_check_synthetic_pointer,
367     lval_func_copy_closure,
368     lval_func_free_closure
369   };
370
371 /* Creates a sub-vector from VAL.  The elements are selected by the indices of
372    an array with the length of N.  Supported values for NOSIDE are
373    EVAL_NORMAL and EVAL_AVOID_SIDE_EFFECTS.  */
374
375 static struct value *
376 create_value (struct gdbarch *gdbarch, struct value *val, enum noside noside,
377               int *indices, int n)
378 {
379   struct type *type = check_typedef (value_type (val));
380   struct type *elm_type = TYPE_TARGET_TYPE (type);
381   struct value *ret;
382
383   /* Check if a single component of a vector is requested which means
384      the resulting type is a (primitive) scalar type.  */
385   if (n == 1)
386     {
387       if (noside == EVAL_AVOID_SIDE_EFFECTS)
388         ret = value_zero (elm_type, not_lval);
389       else
390         ret = value_subscript (val, indices[0]);
391     }
392   else
393     {
394       /* Multiple components of the vector are requested which means the
395          resulting type is a vector as well.  */
396       struct type *dst_type =
397         lookup_opencl_vector_type (gdbarch, TYPE_CODE (elm_type),
398                                    TYPE_LENGTH (elm_type),
399                                    TYPE_UNSIGNED (elm_type), n);
400
401       if (dst_type == NULL)
402         dst_type = init_vector_type (elm_type, n);
403
404       make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type), dst_type, NULL);
405
406       if (noside == EVAL_AVOID_SIDE_EFFECTS)
407         ret = allocate_value (dst_type);
408       else
409         {
410           /* Check whether to create a lvalue or not.  */
411           if (VALUE_LVAL (val) != not_lval && !array_has_dups (indices, n))
412             {
413               struct lval_closure *c = allocate_lval_closure (indices, n, val);
414               ret = allocate_computed_value (dst_type, &opencl_value_funcs, c);
415             }
416           else
417             {
418               int i;
419
420               ret = allocate_value (dst_type);
421
422               /* Copy src val contents into the destination value.  */
423               for (i = 0; i < n; i++)
424                 memcpy (value_contents_writeable (ret)
425                         + (i * TYPE_LENGTH (elm_type)),
426                         value_contents (val)
427                         + (indices[i] * TYPE_LENGTH (elm_type)),
428                         TYPE_LENGTH (elm_type));
429             }
430         }
431     }
432   return ret;
433 }
434
435 /* OpenCL vector component access.  */
436
437 static struct value *
438 opencl_component_ref (struct expression *exp, struct value *val, char *comps,
439                       enum noside noside)
440 {
441   LONGEST lowb, highb;
442   int src_len;
443   struct value *v;
444   int indices[16], i;
445   int dst_len;
446
447   if (!get_array_bounds (check_typedef (value_type (val)), &lowb, &highb))
448     error (_("Could not determine the vector bounds"));
449
450   src_len = highb - lowb + 1;
451
452   /* Throw an error if the amount of array elements does not fit a
453      valid OpenCL vector size (2, 3, 4, 8, 16).  */
454   if (src_len != 2 && src_len != 3 && src_len != 4 && src_len != 8
455       && src_len != 16)
456     error (_("Invalid OpenCL vector size"));
457
458   if (strcmp (comps, "lo") == 0 )
459     {
460       dst_len = (src_len == 3) ? 2 : src_len / 2;
461
462       for (i = 0; i < dst_len; i++)
463         indices[i] = i;
464     }
465   else if (strcmp (comps, "hi") == 0)
466     {
467       dst_len = (src_len == 3) ? 2 : src_len / 2;
468
469       for (i = 0; i < dst_len; i++)
470         indices[i] = dst_len + i;
471     }
472   else if (strcmp (comps, "even") == 0)
473     {
474       dst_len = (src_len == 3) ? 2 : src_len / 2;
475
476       for (i = 0; i < dst_len; i++)
477         indices[i] = i*2;
478     }
479   else if (strcmp (comps, "odd") == 0)
480     {
481       dst_len = (src_len == 3) ? 2 : src_len / 2;
482
483       for (i = 0; i < dst_len; i++)
484         indices[i] = i*2+1;
485     }
486   else if (strncasecmp (comps, "s", 1) == 0)
487     {
488 #define HEXCHAR_TO_INT(C) ((C >= '0' && C <= '9') ? \
489                            C-'0' : ((C >= 'A' && C <= 'F') ? \
490                            C-'A'+10 : ((C >= 'a' && C <= 'f') ? \
491                            C-'a'+10 : -1)))
492
493       dst_len = strlen (comps);
494       /* Skip the s/S-prefix.  */
495       dst_len--;
496
497       for (i = 0; i < dst_len; i++)
498         {
499           indices[i] = HEXCHAR_TO_INT(comps[i+1]);
500           /* Check if the requested component is invalid or exceeds
501              the vector.  */
502           if (indices[i] < 0 || indices[i] >= src_len)
503             error (_("Invalid OpenCL vector component accessor %s"), comps);
504         }
505     }
506   else
507     {
508       dst_len = strlen (comps);
509
510       for (i = 0; i < dst_len; i++)
511         {
512           /* x, y, z, w */
513           switch (comps[i])
514           {
515           case 'x':
516             indices[i] = 0;
517             break;
518           case 'y':
519             indices[i] = 1;
520             break;
521           case 'z':
522             if (src_len < 3)
523               error (_("Invalid OpenCL vector component accessor %s"), comps);
524             indices[i] = 2;
525             break;
526           case 'w':
527             if (src_len < 4)
528               error (_("Invalid OpenCL vector component accessor %s"), comps);
529             indices[i] = 3;
530             break;
531           default:
532             error (_("Invalid OpenCL vector component accessor %s"), comps);
533             break;
534           }
535         }
536     }
537
538   /* Throw an error if the amount of requested components does not
539      result in a valid length (1, 2, 3, 4, 8, 16).  */
540   if (dst_len != 1 && dst_len != 2 && dst_len != 3 && dst_len != 4
541       && dst_len != 8 && dst_len != 16)
542     error (_("Invalid OpenCL vector component accessor %s"), comps);
543
544   v = create_value (exp->gdbarch, val, noside, indices, dst_len);
545
546   return v;
547 }
548
549 /* Perform the unary logical not (!) operation.  */
550
551 static struct value *
552 opencl_logical_not (struct expression *exp, struct value *arg)
553 {
554   struct type *type = check_typedef (value_type (arg));
555   struct type *rettype;
556   struct value *ret;
557
558   if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
559     {
560       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
561       LONGEST lowb, highb;
562       int i;
563
564       if (!get_array_bounds (type, &lowb, &highb))
565         error (_("Could not determine the vector bounds"));
566
567       /* Determine the resulting type of the operation and allocate the
568          value.  */
569       rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
570                                            TYPE_LENGTH (eltype), 0,
571                                            highb - lowb + 1);
572       ret = allocate_value (rettype);
573
574       for (i = 0; i < highb - lowb + 1; i++)
575         {
576           /* For vector types, the unary operator shall return a 0 if the
577           value of its operand compares unequal to 0, and -1 (i.e. all bits
578           set) if the value of its operand compares equal to 0.  */
579           int tmp = value_logical_not (value_subscript (arg, i)) ? -1 : 0;
580           memset (value_contents_writeable (ret) + i * TYPE_LENGTH (eltype),
581                   tmp, TYPE_LENGTH (eltype));
582         }
583     }
584   else
585     {
586       rettype = language_bool_type (exp->language_defn, exp->gdbarch);
587       ret = value_from_longest (rettype, value_logical_not (arg));
588     }
589
590   return ret;
591 }
592
593 /* Perform a relational operation on two scalar operands.  */
594
595 static int
596 scalar_relop (struct value *val1, struct value *val2, enum exp_opcode op)
597 {
598   int ret;
599
600   switch (op)
601     {
602     case BINOP_EQUAL:
603       ret = value_equal (val1, val2);
604       break;
605     case BINOP_NOTEQUAL:
606       ret = !value_equal (val1, val2);
607       break;
608     case BINOP_LESS:
609       ret = value_less (val1, val2);
610       break;
611     case BINOP_GTR:
612       ret = value_less (val2, val1);
613       break;
614     case BINOP_GEQ:
615       ret = value_less (val2, val1) || value_equal (val1, val2);
616       break;
617     case BINOP_LEQ:
618       ret = value_less (val1, val2) || value_equal (val1, val2);
619       break;
620     case BINOP_LOGICAL_AND:
621       ret = !value_logical_not (val1) && !value_logical_not (val2);
622       break;
623     case BINOP_LOGICAL_OR:
624       ret = !value_logical_not (val1) || !value_logical_not (val2);
625       break;
626     default:
627       error (_("Attempt to perform an unsupported operation"));
628       break;
629     }
630   return ret;
631 }
632
633 /* Perform a relational operation on two vector operands.  */
634
635 static struct value *
636 vector_relop (struct expression *exp, struct value *val1, struct value *val2,
637               enum exp_opcode op)
638 {
639   struct value *ret;
640   struct type *type1, *type2, *eltype1, *eltype2, *rettype;
641   int t1_is_vec, t2_is_vec, i;
642   LONGEST lowb1, lowb2, highb1, highb2;
643
644   type1 = check_typedef (value_type (val1));
645   type2 = check_typedef (value_type (val2));
646
647   t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1));
648   t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY && TYPE_VECTOR (type2));
649
650   if (!t1_is_vec || !t2_is_vec)
651     error (_("Vector operations are not supported on scalar types"));
652
653   eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
654   eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
655
656   if (!get_array_bounds (type1,&lowb1, &highb1)
657       || !get_array_bounds (type2, &lowb2, &highb2))
658     error (_("Could not determine the vector bounds"));
659
660   /* Check whether the vector types are compatible.  */
661   if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
662       || TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
663       || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
664       || lowb1 != lowb2 || highb1 != highb2)
665     error (_("Cannot perform operation on vectors with different types"));
666
667   /* Determine the resulting type of the operation and allocate the value.  */
668   rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
669                                        TYPE_LENGTH (eltype1), 0,
670                                        highb1 - lowb1 + 1);
671   ret = allocate_value (rettype);
672
673   for (i = 0; i < highb1 - lowb1 + 1; i++)
674     {
675       /* For vector types, the relational, equality and logical operators shall
676          return 0 if the specified relation is false and -1 (i.e. all bits set)
677          if the specified relation is true.  */
678       int tmp = scalar_relop (value_subscript (val1, i),
679                               value_subscript (val2, i), op) ? -1 : 0;
680       memset (value_contents_writeable (ret) + i * TYPE_LENGTH (eltype1),
681               tmp, TYPE_LENGTH (eltype1));
682      }
683
684   return ret;
685 }
686
687 /* Perform a cast of ARG into TYPE.  There's sadly a lot of duplication in
688    here from valops.c:value_cast, opencl is different only in the
689    behaviour of scalar to vector casting.  As far as possibly we're going
690    to try and delegate back to the standard value_cast function. */
691
692 static struct value *
693 opencl_value_cast (struct type *type, struct value *arg)
694 {
695   if (type != value_type (arg))
696     {
697       /* Casting scalar to vector is a special case for OpenCL, scalar
698          is cast to element type of vector then replicated into each
699          element of the vector.  First though, we need to work out if
700          this is a scalar to vector cast; code lifted from
701          valops.c:value_cast.  */
702       enum type_code code1, code2;
703       struct type *to_type;
704       int scalar;
705
706       to_type = check_typedef (type);
707
708       code1 = TYPE_CODE (to_type);
709       code2 = TYPE_CODE (check_typedef (value_type (arg)));
710
711       if (code2 == TYPE_CODE_REF)
712         code2 = TYPE_CODE (check_typedef (value_type (coerce_ref (arg))));
713
714       scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL
715                 || code2 == TYPE_CODE_CHAR || code2 == TYPE_CODE_FLT
716                 || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
717                 || code2 == TYPE_CODE_RANGE);
718
719       if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (to_type) && scalar)
720         {
721           struct type *eltype;
722
723           /* Cast to the element type of the vector here as
724              value_vector_widen will error if the scalar value is
725              truncated by the cast.  To avoid the error, cast (and
726              possibly truncate) here.  */
727           eltype = check_typedef (TYPE_TARGET_TYPE (to_type));
728           arg = value_cast (eltype, arg);
729
730           return value_vector_widen (arg, type);
731         }
732       else
733         /* Standard cast handler.  */
734         arg = value_cast (type, arg);
735     }
736   return arg;
737 }
738
739 /* Perform a relational operation on two operands.  */
740
741 static struct value *
742 opencl_relop (struct expression *exp, struct value *arg1, struct value *arg2,
743               enum exp_opcode op)
744 {
745   struct value *val;
746   struct type *type1 = check_typedef (value_type (arg1));
747   struct type *type2 = check_typedef (value_type (arg2));
748   int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
749                    && TYPE_VECTOR (type1));
750   int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
751                    && TYPE_VECTOR (type2));
752
753   if (!t1_is_vec && !t2_is_vec)
754     {
755       int tmp = scalar_relop (arg1, arg2, op);
756       struct type *type =
757         language_bool_type (exp->language_defn, exp->gdbarch);
758
759       val = value_from_longest (type, tmp);
760     }
761   else if (t1_is_vec && t2_is_vec)
762     {
763       val = vector_relop (exp, arg1, arg2, op);
764     }
765   else
766     {
767       /* Widen the scalar operand to a vector.  */
768       struct value **v = t1_is_vec ? &arg2 : &arg1;
769       struct type *t = t1_is_vec ? type2 : type1;
770
771       if (TYPE_CODE (t) != TYPE_CODE_FLT && !is_integral_type (t))
772         error (_("Argument to operation not a number or boolean."));
773
774       *v = opencl_value_cast (t1_is_vec ? type1 : type2, *v);
775       val = vector_relop (exp, arg1, arg2, op);
776     }
777
778   return val;
779 }
780
781 /* Expression evaluator for the OpenCL.  Most operations are delegated to
782    evaluate_subexp_standard; see that function for a description of the
783    arguments.  */
784
785 static struct value *
786 evaluate_subexp_opencl (struct type *expect_type, struct expression *exp,
787                    int *pos, enum noside noside)
788 {
789   enum exp_opcode op = exp->elts[*pos].opcode;
790   struct value *arg1 = NULL;
791   struct value *arg2 = NULL;
792   struct type *type1, *type2;
793
794   switch (op)
795     {
796     /* Handle assignment and cast operators to support OpenCL-style
797        scalar-to-vector widening.  */
798     case BINOP_ASSIGN:
799       (*pos)++;
800       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
801       type1 = value_type (arg1);
802       arg2 = evaluate_subexp (type1, exp, pos, noside);
803
804       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
805         return arg1;
806
807       if (deprecated_value_modifiable (arg1)
808           && VALUE_LVAL (arg1) != lval_internalvar)
809         arg2 = opencl_value_cast (type1, arg2);
810
811       return value_assign (arg1, arg2);
812
813     case UNOP_CAST:
814       type1 = exp->elts[*pos + 1].type;
815       (*pos) += 2;
816       arg1 = evaluate_subexp (type1, exp, pos, noside);
817
818       if (noside == EVAL_SKIP)
819         return value_from_longest (builtin_type (exp->gdbarch)->
820                                    builtin_int, 1);
821
822       return opencl_value_cast (type1, arg1);
823
824     case UNOP_CAST_TYPE:
825       (*pos)++;
826       arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
827       type1 = value_type (arg1);
828       arg1 = evaluate_subexp (type1, exp, pos, noside);
829
830       if (noside == EVAL_SKIP)
831         return value_from_longest (builtin_type (exp->gdbarch)->
832                                    builtin_int, 1);
833
834       return opencl_value_cast (type1, arg1);
835
836     /* Handle binary relational and equality operators that are either not
837        or differently defined for GNU vectors.  */
838     case BINOP_EQUAL:
839     case BINOP_NOTEQUAL:
840     case BINOP_LESS:
841     case BINOP_GTR:
842     case BINOP_GEQ:
843     case BINOP_LEQ:
844       (*pos)++;
845       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
846       arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
847
848       if (noside == EVAL_SKIP)
849         return value_from_longest (builtin_type (exp->gdbarch)->
850                                    builtin_int, 1);
851
852       return opencl_relop (exp, arg1, arg2, op);
853
854     /* Handle the logical unary operator not(!).  */
855     case UNOP_LOGICAL_NOT:
856       (*pos)++;
857       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
858
859       if (noside == EVAL_SKIP)
860         return value_from_longest (builtin_type (exp->gdbarch)->
861                                    builtin_int, 1);
862
863       return opencl_logical_not (exp, arg1);
864
865     /* Handle the logical operator and(&&) and or(||).  */
866     case BINOP_LOGICAL_AND:
867     case BINOP_LOGICAL_OR:
868       (*pos)++;
869       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
870
871       if (noside == EVAL_SKIP)
872         {
873           evaluate_subexp (NULL_TYPE, exp, pos, noside);
874
875           return value_from_longest (builtin_type (exp->gdbarch)->
876                                      builtin_int, 1);
877         }
878       else
879         {
880           /* For scalar operations we need to avoid evaluating operands
881              unecessarily.  However, for vector operations we always need to
882              evaluate both operands.  Unfortunately we only know which of the
883              two cases apply after we know the type of the second operand.
884              Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS.  */
885           int oldpos = *pos;
886
887           arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
888                                   EVAL_AVOID_SIDE_EFFECTS);
889           *pos = oldpos;
890           type1 = check_typedef (value_type (arg1));
891           type2 = check_typedef (value_type (arg2));
892
893           if ((TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
894               || (TYPE_CODE (type2) == TYPE_CODE_ARRAY && TYPE_VECTOR (type2)))
895             {
896               arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
897
898               return opencl_relop (exp, arg1, arg2, op);
899             }
900           else
901             {
902               /* For scalar built-in types, only evaluate the right
903                  hand operand if the left hand operand compares
904                  unequal(&&)/equal(||) to 0.  */
905               int res;
906               int tmp = value_logical_not (arg1);
907
908               if (op == BINOP_LOGICAL_OR)
909                 tmp = !tmp;
910
911               arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
912                                       tmp ? EVAL_SKIP : noside);
913               type1 = language_bool_type (exp->language_defn, exp->gdbarch);
914
915               if (op == BINOP_LOGICAL_AND)
916                 res = !tmp && !value_logical_not (arg2);
917               else /* BINOP_LOGICAL_OR */
918                 res = tmp || !value_logical_not (arg2);
919
920               return value_from_longest (type1, res);
921             }
922         }
923
924     /* Handle the ternary selection operator.  */
925     case TERNOP_COND:
926       (*pos)++;
927       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
928       type1 = check_typedef (value_type (arg1));
929       if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
930         {
931           struct value *arg3, *tmp, *ret;
932           struct type *eltype2, *type3, *eltype3;
933           int t2_is_vec, t3_is_vec, i;
934           LONGEST lowb1, lowb2, lowb3, highb1, highb2, highb3;
935
936           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
937           arg3 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
938           type2 = check_typedef (value_type (arg2));
939           type3 = check_typedef (value_type (arg3));
940           t2_is_vec
941             = TYPE_CODE (type2) == TYPE_CODE_ARRAY && TYPE_VECTOR (type2);
942           t3_is_vec
943             = TYPE_CODE (type3) == TYPE_CODE_ARRAY && TYPE_VECTOR (type3);
944
945           /* Widen the scalar operand to a vector if necessary.  */
946           if (t2_is_vec || !t3_is_vec)
947             {
948               arg3 = opencl_value_cast (type2, arg3);
949               type3 = value_type (arg3);
950             }
951           else if (!t2_is_vec || t3_is_vec)
952             {
953               arg2 = opencl_value_cast (type3, arg2);
954               type2 = value_type (arg2);
955             }
956           else if (!t2_is_vec || !t3_is_vec)
957             {
958               /* Throw an error if arg2 or arg3 aren't vectors.  */
959               error (_("\
960 Cannot perform conditional operation on incompatible types"));
961             }
962
963           eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
964           eltype3 = check_typedef (TYPE_TARGET_TYPE (type3));
965
966           if (!get_array_bounds (type1, &lowb1, &highb1)
967               || !get_array_bounds (type2, &lowb2, &highb2)
968               || !get_array_bounds (type3, &lowb3, &highb3))
969             error (_("Could not determine the vector bounds"));
970
971           /* Throw an error if the types of arg2 or arg3 are incompatible.  */
972           if (TYPE_CODE (eltype2) != TYPE_CODE (eltype3)
973               || TYPE_LENGTH (eltype2) != TYPE_LENGTH (eltype3)
974               || TYPE_UNSIGNED (eltype2) != TYPE_UNSIGNED (eltype3)
975               || lowb2 != lowb3 || highb2 != highb3)
976             error (_("\
977 Cannot perform operation on vectors with different types"));
978
979           /* Throw an error if the sizes of arg1 and arg2/arg3 differ.  */
980           if (lowb1 != lowb2 || lowb1 != lowb3
981               || highb1 != highb2 || highb1 != highb3)
982             error (_("\
983 Cannot perform conditional operation on vectors with different sizes"));
984
985           ret = allocate_value (type2);
986
987           for (i = 0; i < highb1 - lowb1 + 1; i++)
988             {
989               tmp = value_logical_not (value_subscript (arg1, i)) ?
990                     value_subscript (arg3, i) : value_subscript (arg2, i);
991               memcpy (value_contents_writeable (ret) +
992                       i * TYPE_LENGTH (eltype2), value_contents_all (tmp),
993                       TYPE_LENGTH (eltype2));
994             }
995
996           return ret;
997         }
998       else
999         {
1000           if (value_logical_not (arg1))
1001             {
1002               /* Skip the second operand.  */
1003               evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1004
1005               return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1006             }
1007           else
1008             {
1009               /* Skip the third operand.  */
1010               arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1011               evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1012
1013               return arg2;
1014             }
1015         }
1016
1017     /* Handle STRUCTOP_STRUCT to allow component access on OpenCL vectors.  */
1018     case STRUCTOP_STRUCT:
1019       {
1020         int pc = (*pos)++;
1021         int tem = longest_to_int (exp->elts[pc + 1].longconst);
1022
1023         (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1024         arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1025         type1 = check_typedef (value_type (arg1));
1026
1027         if (noside == EVAL_SKIP)
1028           {
1029             return value_from_longest (builtin_type (exp->gdbarch)->
1030                                        builtin_int, 1);
1031           }
1032         else if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
1033           {
1034             return opencl_component_ref (exp, arg1, &exp->elts[pc + 2].string,
1035                                          noside);
1036           }
1037         else
1038           {
1039             struct value *v = value_struct_elt (&arg1, NULL,
1040                                                 &exp->elts[pc + 2].string, NULL,
1041                                                 "structure");
1042
1043             if (noside == EVAL_AVOID_SIDE_EFFECTS)
1044               v = value_zero (value_type (v), not_lval);
1045             return v;
1046           }
1047       }
1048     default:
1049       break;
1050     }
1051
1052   return evaluate_subexp_c (expect_type, exp, pos, noside);
1053 }
1054
1055 /* Print OpenCL types.  */
1056
1057 static void
1058 opencl_print_type (struct type *type, const char *varstring,
1059                    struct ui_file *stream, int show, int level,
1060                    const struct type_print_options *flags)
1061 {
1062   /* We nearly always defer to C type printing, except that vector
1063      types are considered primitive in OpenCL, and should always
1064      be printed using their TYPE_NAME.  */
1065   if (show > 0)
1066     {
1067       CHECK_TYPEDEF (type);
1068       if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1069           && TYPE_NAME (type) != NULL)
1070         show = 0;
1071     }
1072
1073   c_print_type (type, varstring, stream, show, level, flags); 
1074 }
1075
1076 static void
1077 opencl_language_arch_info (struct gdbarch *gdbarch,
1078                            struct language_arch_info *lai)
1079 {
1080   struct type **types = builtin_opencl_type (gdbarch);
1081
1082   /* Copy primitive types vector from gdbarch.  */
1083   lai->primitive_type_vector = types;
1084
1085   /* Type of elements of strings.  */
1086   lai->string_char_type = types [opencl_primitive_type_char];
1087
1088   /* Specifies the return type of logical and relational operations.  */
1089   lai->bool_type_symbol = "int";
1090   lai->bool_type_default = types [opencl_primitive_type_int];
1091 }
1092
1093 const struct exp_descriptor exp_descriptor_opencl =
1094 {
1095   print_subexp_standard,
1096   operator_length_standard,
1097   operator_check_standard,
1098   op_name_standard,
1099   dump_subexp_body_standard,
1100   evaluate_subexp_opencl
1101 };
1102
1103 const struct language_defn opencl_language_defn =
1104 {
1105   "opencl",                     /* Language name */
1106   "OpenCL C",
1107   language_opencl,
1108   range_check_off,
1109   case_sensitive_on,
1110   array_row_major,
1111   macro_expansion_c,
1112   &exp_descriptor_opencl,
1113   c_parse,
1114   c_error,
1115   null_post_parser,
1116   c_printchar,                  /* Print a character constant */
1117   c_printstr,                   /* Function to print string constant */
1118   c_emit_char,                  /* Print a single char */
1119   opencl_print_type,            /* Print a type using appropriate syntax */
1120   c_print_typedef,              /* Print a typedef using appropriate syntax */
1121   c_val_print,                  /* Print a value using appropriate syntax */
1122   c_value_print,                /* Print a top-level value */
1123   default_read_var_value,       /* la_read_var_value */
1124   NULL,                         /* Language specific skip_trampoline */
1125   NULL,                         /* name_of_this */
1126   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1127   basic_lookup_transparent_type,/* lookup_transparent_type */
1128   NULL,                         /* Language specific symbol demangler */
1129   NULL,                         /* Language specific
1130                                    class_name_from_physname */
1131   c_op_print_tab,               /* expression operators for printing */
1132   1,                            /* c-style arrays */
1133   0,                            /* String lower bound */
1134   default_word_break_characters,
1135   default_make_symbol_completion_list,
1136   opencl_language_arch_info,
1137   default_print_array_index,
1138   default_pass_by_reference,
1139   c_get_string,
1140   NULL,                         /* la_get_symbol_name_cmp */
1141   iterate_over_symbols,
1142   &default_varobj_ops,
1143   LANG_MAGIC
1144 };
1145
1146 static void *
1147 build_opencl_types (struct gdbarch *gdbarch)
1148 {
1149   struct type **types
1150     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_opencl_primitive_types + 1,
1151                               struct type *);
1152
1153 /* Helper macro to create strings.  */
1154 #define OCL_STRING(S) #S
1155 /* This macro allocates and assigns the type struct pointers
1156    for the vector types.  */
1157 #define BUILD_OCL_VTYPES(TYPE)\
1158   types[opencl_primitive_type_##TYPE##2] \
1159     = init_vector_type (types[opencl_primitive_type_##TYPE], 2); \
1160   TYPE_NAME (types[opencl_primitive_type_##TYPE##2]) = OCL_STRING(TYPE ## 2); \
1161   types[opencl_primitive_type_##TYPE##3] \
1162     = init_vector_type (types[opencl_primitive_type_##TYPE], 3); \
1163   TYPE_NAME (types[opencl_primitive_type_##TYPE##3]) = OCL_STRING(TYPE ## 3); \
1164   TYPE_LENGTH (types[opencl_primitive_type_##TYPE##3]) \
1165     = 4 * TYPE_LENGTH (types[opencl_primitive_type_##TYPE]); \
1166   types[opencl_primitive_type_##TYPE##4] \
1167     = init_vector_type (types[opencl_primitive_type_##TYPE], 4); \
1168   TYPE_NAME (types[opencl_primitive_type_##TYPE##4]) = OCL_STRING(TYPE ## 4); \
1169   types[opencl_primitive_type_##TYPE##8] \
1170     = init_vector_type (types[opencl_primitive_type_##TYPE], 8); \
1171   TYPE_NAME (types[opencl_primitive_type_##TYPE##8]) = OCL_STRING(TYPE ## 8); \
1172   types[opencl_primitive_type_##TYPE##16] \
1173     = init_vector_type (types[opencl_primitive_type_##TYPE], 16); \
1174   TYPE_NAME (types[opencl_primitive_type_##TYPE##16]) = OCL_STRING(TYPE ## 16)
1175
1176   types[opencl_primitive_type_char]
1177     = arch_integer_type (gdbarch, 8, 0, "char");
1178   BUILD_OCL_VTYPES (char);
1179   types[opencl_primitive_type_uchar]
1180     = arch_integer_type (gdbarch, 8, 1, "uchar");
1181   BUILD_OCL_VTYPES (uchar);
1182   types[opencl_primitive_type_short]
1183     = arch_integer_type (gdbarch, 16, 0, "short");
1184   BUILD_OCL_VTYPES (short);
1185   types[opencl_primitive_type_ushort]
1186     = arch_integer_type (gdbarch, 16, 1, "ushort");
1187   BUILD_OCL_VTYPES (ushort);
1188   types[opencl_primitive_type_int]
1189     = arch_integer_type (gdbarch, 32, 0, "int");
1190   BUILD_OCL_VTYPES (int);
1191   types[opencl_primitive_type_uint]
1192     = arch_integer_type (gdbarch, 32, 1, "uint");
1193   BUILD_OCL_VTYPES (uint);
1194   types[opencl_primitive_type_long]
1195     = arch_integer_type (gdbarch, 64, 0, "long");
1196   BUILD_OCL_VTYPES (long);
1197   types[opencl_primitive_type_ulong]
1198     = arch_integer_type (gdbarch, 64, 1, "ulong");
1199   BUILD_OCL_VTYPES (ulong);
1200   types[opencl_primitive_type_half]
1201     = arch_float_type (gdbarch, 16, "half", floatformats_ieee_half);
1202   BUILD_OCL_VTYPES (half);
1203   types[opencl_primitive_type_float]
1204     = arch_float_type (gdbarch, 32, "float", floatformats_ieee_single);
1205   BUILD_OCL_VTYPES (float);
1206   types[opencl_primitive_type_double]
1207     = arch_float_type (gdbarch, 64, "double", floatformats_ieee_double);
1208   BUILD_OCL_VTYPES (double);
1209   types[opencl_primitive_type_bool]
1210     = arch_boolean_type (gdbarch, 8, 1, "bool");
1211   types[opencl_primitive_type_unsigned_char]
1212     = arch_integer_type (gdbarch, 8, 1, "unsigned char");
1213   types[opencl_primitive_type_unsigned_short]
1214     = arch_integer_type (gdbarch, 16, 1, "unsigned short");
1215   types[opencl_primitive_type_unsigned_int]
1216     = arch_integer_type (gdbarch, 32, 1, "unsigned int");
1217   types[opencl_primitive_type_unsigned_long]
1218     = arch_integer_type (gdbarch, 64, 1, "unsigned long");
1219   types[opencl_primitive_type_size_t]
1220     = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "size_t");
1221   types[opencl_primitive_type_ptrdiff_t]
1222     = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "ptrdiff_t");
1223   types[opencl_primitive_type_intptr_t]
1224     = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "intptr_t");
1225   types[opencl_primitive_type_uintptr_t]
1226     = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr_t");
1227   types[opencl_primitive_type_void]
1228     = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
1229
1230   return types;
1231 }
1232
1233 /* Provide a prototype to silence -Wmissing-prototypes.  */
1234 extern initialize_file_ftype _initialize_opencl_language;
1235
1236 void
1237 _initialize_opencl_language (void)
1238 {
1239   opencl_type_data = gdbarch_data_register_post_init (build_opencl_types);
1240   add_language (&opencl_language_defn);
1241 }