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