run copyright.sh for 2011.
[external/binutils.git] / gdb / opencl-lang.c
1 /* OpenCL language support for GDB, the GNU debugger.
2    Copyright (C) 2010, 2011 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 /* Return nonzero if bits in V from OFFSET and LENGTH represent a
331    synthetic pointer.  */
332
333 static int
334 lval_func_check_synthetic_pointer (const struct value *v,
335                                    int offset, int length)
336 {
337   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
338   /* Size of the target type in bits.  */
339   int elsize =
340       TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c->val)))) * 8;
341   int startrest = offset % elsize;
342   int start = offset / elsize;
343   int endrest = (offset + length) % elsize;
344   int end = (offset + length) / elsize;
345   int i;
346
347   if (endrest)
348     end++;
349
350   if (end > c->n)
351     return 0;
352
353   for (i = start; i < end; i++)
354     {
355       int startoffset = (i == start) ? startrest : 0;
356       int length = (i == end) ? endrest : elsize;
357
358       if (!value_bits_synthetic_pointer (c->val,
359                                          c->indices[i] * elsize + startoffset,
360                                          length))
361         return 0;
362     }
363
364   return 1;
365 }
366
367 static void *
368 lval_func_copy_closure (const struct value *v)
369 {
370   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
371
372   ++c->refc;
373
374   return c;
375 }
376
377 static void
378 lval_func_free_closure (struct value *v)
379 {
380   struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
381
382   --c->refc;
383
384   if (c->refc == 0)
385     {
386       xfree (c->indices);
387       xfree (c);
388       value_free (c->val); /* Decrement the reference counter of the value.  */
389     }
390 }
391
392 static struct lval_funcs opencl_value_funcs =
393   {
394     lval_func_read,
395     lval_func_write,
396     lval_func_check_validity,
397     lval_func_check_any_valid,
398     NULL,
399     lval_func_check_synthetic_pointer,
400     lval_func_copy_closure,
401     lval_func_free_closure
402   };
403
404 /* Creates a sub-vector from VAL.  The elements are selected by the indices of
405    an array with the length of N.  Supported values for NOSIDE are
406    EVAL_NORMAL and EVAL_AVOID_SIDE_EFFECTS.  */
407
408 static struct value *
409 create_value (struct gdbarch *gdbarch, struct value *val, enum noside noside,
410               int *indices, int n)
411 {
412   struct type *type = check_typedef (value_type (val));
413   struct type *elm_type = TYPE_TARGET_TYPE (type);
414   struct value *ret;
415
416   /* Check if a single component of a vector is requested which means
417      the resulting type is a (primitive) scalar type.  */
418   if (n == 1)
419     {
420       if (noside == EVAL_AVOID_SIDE_EFFECTS)
421         ret = value_zero (elm_type, not_lval);
422       else
423         ret = value_subscript (val, indices[0]);
424     }
425   else
426     {
427       /* Multiple components of the vector are requested which means the
428          resulting type is a vector as well.  */
429       struct type *dst_type =
430         lookup_opencl_vector_type (gdbarch, TYPE_CODE (elm_type),
431                                    TYPE_LENGTH (elm_type),
432                                    TYPE_UNSIGNED (elm_type), n);
433
434       if (dst_type == NULL)
435         dst_type = init_vector_type (elm_type, n);
436
437       make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type), dst_type, NULL);
438
439       if (noside == EVAL_AVOID_SIDE_EFFECTS)
440         ret = allocate_value (dst_type);
441       else
442         {
443           /* Check whether to create a lvalue or not.  */
444           if (VALUE_LVAL (val) != not_lval && !array_has_dups (indices, n))
445             {
446               struct lval_closure *c = allocate_lval_closure (indices, n, val);
447               ret = allocate_computed_value (dst_type, &opencl_value_funcs, c);
448             }
449           else
450             {
451               int i;
452
453               ret = allocate_value (dst_type);
454
455               /* Copy src val contents into the destination value.  */
456               for (i = 0; i < n; i++)
457                 memcpy (value_contents_writeable (ret)
458                         + (i * TYPE_LENGTH (elm_type)),
459                         value_contents (val)
460                         + (indices[i] * TYPE_LENGTH (elm_type)),
461                         TYPE_LENGTH (elm_type));
462             }
463         }
464     }
465   return ret;
466 }
467
468 /* OpenCL vector component access.  */
469
470 static struct value *
471 opencl_component_ref (struct expression *exp, struct value *val, char *comps,
472                       enum noside noside)
473 {
474   LONGEST lowb, highb;
475   int src_len;
476   struct value *v;
477   int indices[16], i;
478   int dst_len;
479
480   if (!get_array_bounds (check_typedef (value_type (val)), &lowb, &highb))
481     error (_("Could not determine the vector bounds"));
482
483   src_len = highb - lowb + 1;
484
485   /* Throw an error if the amount of array elements does not fit a
486      valid OpenCL vector size (2, 3, 4, 8, 16).  */
487   if (src_len != 2 && src_len != 3 && src_len != 4 && src_len != 8
488       && src_len != 16)
489     error (_("Invalid OpenCL vector size"));
490
491   if (strcmp (comps, "lo") == 0 )
492     {
493       dst_len = (src_len == 3) ? 2 : src_len / 2;
494
495       for (i = 0; i < dst_len; i++)
496         indices[i] = i;
497     }
498   else if (strcmp (comps, "hi") == 0)
499     {
500       dst_len = (src_len == 3) ? 2 : src_len / 2;
501
502       for (i = 0; i < dst_len; i++)
503         indices[i] = dst_len + i;
504     }
505   else if (strcmp (comps, "even") == 0)
506     {
507       dst_len = (src_len == 3) ? 2 : src_len / 2;
508
509       for (i = 0; i < dst_len; i++)
510         indices[i] = i*2;
511     }
512   else if (strcmp (comps, "odd") == 0)
513     {
514       dst_len = (src_len == 3) ? 2 : src_len / 2;
515
516       for (i = 0; i < dst_len; i++)
517         indices[i] = i*2+1;
518     }
519   else if (strncasecmp (comps, "s", 1) == 0)
520     {
521 #define HEXCHAR_TO_INT(C) ((C >= '0' && C <= '9') ? \
522                            C-'0' : ((C >= 'A' && C <= 'F') ? \
523                            C-'A'+10 : ((C >= 'a' && C <= 'f') ? \
524                            C-'a'+10 : -1)))
525
526       dst_len = strlen (comps);
527       /* Skip the s/S-prefix.  */
528       dst_len--;
529
530       for (i = 0; i < dst_len; i++)
531         {
532           indices[i] = HEXCHAR_TO_INT(comps[i+1]);
533           /* Check if the requested component is invalid or exceeds
534              the vector.  */
535           if (indices[i] < 0 || indices[i] >= src_len)
536             error (_("Invalid OpenCL vector component accessor %s"), comps);
537         }
538     }
539   else
540     {
541       dst_len = strlen (comps);
542
543       for (i = 0; i < dst_len; i++)
544         {
545           /* x, y, z, w */
546           switch (comps[i])
547           {
548           case 'x':
549             indices[i] = 0;
550             break;
551           case 'y':
552             indices[i] = 1;
553             break;
554           case 'z':
555             if (src_len < 3)
556               error (_("Invalid OpenCL vector component accessor %s"), comps);
557             indices[i] = 2;
558             break;
559           case 'w':
560             if (src_len < 4)
561               error (_("Invalid OpenCL vector component accessor %s"), comps);
562             indices[i] = 3;
563             break;
564           default:
565             error (_("Invalid OpenCL vector component accessor %s"), comps);
566             break;
567           }
568         }
569     }
570
571   /* Throw an error if the amount of requested components does not
572      result in a valid length (1, 2, 3, 4, 8, 16).  */
573   if (dst_len != 1 && dst_len != 2 && dst_len != 3 && dst_len != 4
574       && dst_len != 8 && dst_len != 16)
575     error (_("Invalid OpenCL vector component accessor %s"), comps);
576
577   v = create_value (exp->gdbarch, val, noside, indices, dst_len);
578
579   return v;
580 }
581
582 /* Perform the unary logical not (!) operation.  */
583
584 static struct value *
585 opencl_logical_not (struct expression *exp, struct value *arg)
586 {
587   struct type *type = check_typedef (value_type (arg));
588   struct type *rettype;
589   struct value *ret;
590
591   if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
592     {
593       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
594       LONGEST lowb, highb;
595       int i;
596
597       if (!get_array_bounds (type, &lowb, &highb))
598         error (_("Could not determine the vector bounds"));
599
600       /* Determine the resulting type of the operation and allocate the
601          value.  */
602       rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
603                                            TYPE_LENGTH (eltype), 0,
604                                            highb - lowb + 1);
605       ret = allocate_value (rettype);
606
607       for (i = 0; i < highb - lowb + 1; i++)
608         {
609           /* For vector types, the unary operator shall return a 0 if the
610           value of its operand compares unequal to 0, and -1 (i.e. all bits
611           set) if the value of its operand compares equal to 0.  */
612           int tmp = value_logical_not (value_subscript (arg, i)) ? -1 : 0;
613           memset (value_contents_writeable (ret) + i * TYPE_LENGTH (eltype),
614                   tmp, TYPE_LENGTH (eltype));
615         }
616     }
617   else
618     {
619       rettype = language_bool_type (exp->language_defn, exp->gdbarch);
620       ret = value_from_longest (rettype, value_logical_not (arg));
621     }
622
623   return ret;
624 }
625
626 /* Perform a relational operation on two scalar operands.  */
627
628 static int
629 scalar_relop (struct value *val1, struct value *val2, enum exp_opcode op)
630 {
631   int ret;
632
633   switch (op)
634     {
635     case BINOP_EQUAL:
636       ret = value_equal (val1, val2);
637       break;
638     case BINOP_NOTEQUAL:
639       ret = !value_equal (val1, val2);
640       break;
641     case BINOP_LESS:
642       ret = value_less (val1, val2);
643       break;
644     case BINOP_GTR:
645       ret = value_less (val2, val1);
646       break;
647     case BINOP_GEQ:
648       ret = value_less (val2, val1) || value_equal (val1, val2);
649       break;
650     case BINOP_LEQ:
651       ret = value_less (val1, val2) || value_equal (val1, val2);
652       break;
653     case BINOP_LOGICAL_AND:
654       ret = !value_logical_not (val1) && !value_logical_not (val2);
655       break;
656     case BINOP_LOGICAL_OR:
657       ret = !value_logical_not (val1) || !value_logical_not (val2);
658       break;
659     default:
660       error (_("Attempt to perform an unsupported operation"));
661       break;
662     }
663   return ret;
664 }
665
666 /* Perform a relational operation on two vector operands.  */
667
668 static struct value *
669 vector_relop (struct expression *exp, struct value *val1, struct value *val2,
670               enum exp_opcode op)
671 {
672   struct value *ret;
673   struct type *type1, *type2, *eltype1, *eltype2, *rettype;
674   int t1_is_vec, t2_is_vec, i;
675   LONGEST lowb1, lowb2, highb1, highb2;
676
677   type1 = check_typedef (value_type (val1));
678   type2 = check_typedef (value_type (val2));
679
680   t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1));
681   t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY && TYPE_VECTOR (type2));
682
683   if (!t1_is_vec || !t2_is_vec)
684     error (_("Vector operations are not supported on scalar types"));
685
686   eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
687   eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
688
689   if (!get_array_bounds (type1,&lowb1, &highb1)
690       || !get_array_bounds (type2, &lowb2, &highb2))
691     error (_("Could not determine the vector bounds"));
692
693   /* Check whether the vector types are compatible.  */
694   if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
695       || TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
696       || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
697       || lowb1 != lowb2 || highb1 != highb2)
698     error (_("Cannot perform operation on vectors with different types"));
699
700   /* Determine the resulting type of the operation and allocate the value.  */
701   rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
702                                        TYPE_LENGTH (eltype1), 0,
703                                        highb1 - lowb1 + 1);
704   ret = allocate_value (rettype);
705
706   for (i = 0; i < highb1 - lowb1 + 1; i++)
707     {
708       /* For vector types, the relational, equality and logical operators shall
709          return 0 if the specified relation is false and -1 (i.e. all bits set)
710          if the specified relation is true.  */
711       int tmp = scalar_relop (value_subscript (val1, i),
712                               value_subscript (val2, i), op) ? -1 : 0;
713       memset (value_contents_writeable (ret) + i * TYPE_LENGTH (eltype1),
714               tmp, TYPE_LENGTH (eltype1));
715      }
716
717   return ret;
718 }
719
720 /* Perform a relational operation on two operands.  */
721
722 static struct value *
723 opencl_relop (struct expression *exp, struct value *arg1, struct value *arg2,
724               enum exp_opcode op)
725 {
726   struct value *val;
727   struct type *type1 = check_typedef (value_type (arg1));
728   struct type *type2 = check_typedef (value_type (arg2));
729   int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
730                    && TYPE_VECTOR (type1));
731   int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
732                    && TYPE_VECTOR (type2));
733
734   if (!t1_is_vec && !t2_is_vec)
735     {
736       int tmp = scalar_relop (arg1, arg2, op);
737       struct type *type =
738         language_bool_type (exp->language_defn, exp->gdbarch);
739
740       val = value_from_longest (type, tmp);
741     }
742   else if (t1_is_vec && t2_is_vec)
743     {
744       val = vector_relop (exp, arg1, arg2, op);
745     }
746   else
747     {
748       /* Widen the scalar operand to a vector.  */
749       struct value **v = t1_is_vec ? &arg2 : &arg1;
750       struct type *t = t1_is_vec ? type2 : type1;
751
752       if (TYPE_CODE (t) != TYPE_CODE_FLT && !is_integral_type (t))
753         error (_("Argument to operation not a number or boolean."));
754
755       *v = value_cast (t1_is_vec ? type1 : type2, *v);
756       val = vector_relop (exp, arg1, arg2, op);
757     }
758
759   return val;
760 }
761
762 /* Expression evaluator for the OpenCL.  Most operations are delegated to
763    evaluate_subexp_standard; see that function for a description of the
764    arguments.  */
765
766 static struct value *
767 evaluate_subexp_opencl (struct type *expect_type, struct expression *exp,
768                    int *pos, enum noside noside)
769 {
770   enum exp_opcode op = exp->elts[*pos].opcode;
771   struct value *arg1 = NULL;
772   struct value *arg2 = NULL;
773   struct type *type1, *type2;
774
775   switch (op)
776     {
777     /* Handle binary relational and equality operators that are either not
778        or differently defined for GNU vectors.  */
779     case BINOP_EQUAL:
780     case BINOP_NOTEQUAL:
781     case BINOP_LESS:
782     case BINOP_GTR:
783     case BINOP_GEQ:
784     case BINOP_LEQ:
785       (*pos)++;
786       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
787       arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
788
789       if (noside == EVAL_SKIP)
790         return value_from_longest (builtin_type (exp->gdbarch)->
791                                    builtin_int, 1);
792
793       return opencl_relop (exp, arg1, arg2, op);
794
795     /* Handle the logical unary operator not(!).  */
796     case UNOP_LOGICAL_NOT:
797       (*pos)++;
798       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
799
800       if (noside == EVAL_SKIP)
801         return value_from_longest (builtin_type (exp->gdbarch)->
802                                    builtin_int, 1);
803
804       return opencl_logical_not (exp, arg1);
805
806     /* Handle the logical operator and(&&) and or(||).  */
807     case BINOP_LOGICAL_AND:
808     case BINOP_LOGICAL_OR:
809       (*pos)++;
810       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
811
812       if (noside == EVAL_SKIP)
813         {
814           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
815
816           return value_from_longest (builtin_type (exp->gdbarch)->
817                                      builtin_int, 1);
818         }
819       else
820         {
821           /* For scalar operations we need to avoid evaluating operands
822              unecessarily.  However, for vector operations we always need to
823              evaluate both operands.  Unfortunately we only know which of the
824              two cases apply after we know the type of the second operand.
825              Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS.  */
826           int oldpos = *pos;
827
828           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
829           *pos = oldpos;
830           type1 = check_typedef (value_type (arg1));
831           type2 = check_typedef (value_type (arg2));
832
833           if ((TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
834               || (TYPE_CODE (type2) == TYPE_CODE_ARRAY && TYPE_VECTOR (type2)))
835             {
836               arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
837
838               return opencl_relop (exp, arg1, arg2, op);
839             }
840           else
841             {
842               /* For scalar built-in types, only evaluate the right
843                  hand operand if the left hand operand compares
844                  unequal(&&)/equal(||) to 0.  */
845               int res;
846               int tmp = value_logical_not (arg1);
847
848               if (op == BINOP_LOGICAL_OR)
849                 tmp = !tmp;
850
851               arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
852                                       tmp ? EVAL_SKIP : noside);
853               type1 = language_bool_type (exp->language_defn, exp->gdbarch);
854
855               if (op == BINOP_LOGICAL_AND)
856                 res = !tmp && !value_logical_not (arg2);
857               else /* BINOP_LOGICAL_OR */
858                 res = tmp || !value_logical_not (arg2);
859
860               return value_from_longest (type1, res);
861             }
862         }
863
864     /* Handle the ternary selection operator.  */
865     case TERNOP_COND:
866       (*pos)++;
867       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
868       type1 = check_typedef (value_type (arg1));
869       if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
870         {
871           struct value *arg3, *tmp, *ret;
872           struct type *eltype2, *type3, *eltype3;
873           int t2_is_vec, t3_is_vec, i;
874           LONGEST lowb1, lowb2, lowb3, highb1, highb2, highb3;
875
876           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
877           arg3 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
878           type2 = check_typedef (value_type (arg2));
879           type3 = check_typedef (value_type (arg3));
880           t2_is_vec
881             = TYPE_CODE (type2) == TYPE_CODE_ARRAY && TYPE_VECTOR (type2);
882           t3_is_vec
883             = TYPE_CODE (type3) == TYPE_CODE_ARRAY && TYPE_VECTOR (type3);
884
885           /* Widen the scalar operand to a vector if necessary.  */
886           if (t2_is_vec || !t3_is_vec)
887             {
888               arg3 = value_cast (type2, arg3);
889               type3 = value_type (arg3);
890             }
891           else if (!t2_is_vec || t3_is_vec)
892             {
893               arg2 = value_cast (type3, arg2);
894               type2 = value_type (arg2);
895             }
896           else if (!t2_is_vec || !t3_is_vec)
897             {
898               /* Throw an error if arg2 or arg3 aren't vectors.  */
899               error (_("\
900 Cannot perform conditional operation on incompatible types"));
901             }
902
903           eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
904           eltype3 = check_typedef (TYPE_TARGET_TYPE (type3));
905
906           if (!get_array_bounds (type1, &lowb1, &highb1)
907               || !get_array_bounds (type2, &lowb2, &highb2)
908               || !get_array_bounds (type3, &lowb3, &highb3))
909             error (_("Could not determine the vector bounds"));
910
911           /* Throw an error if the types of arg2 or arg3 are incompatible.  */
912           if (TYPE_CODE (eltype2) != TYPE_CODE (eltype3)
913               || TYPE_LENGTH (eltype2) != TYPE_LENGTH (eltype3)
914               || TYPE_UNSIGNED (eltype2) != TYPE_UNSIGNED (eltype3)
915               || lowb2 != lowb3 || highb2 != highb3)
916             error (_("\
917 Cannot perform operation on vectors with different types"));
918
919           /* Throw an error if the sizes of arg1 and arg2/arg3 differ.  */
920           if (lowb1 != lowb2 || lowb1 != lowb3
921               || highb1 != highb2 || highb1 != highb3)
922             error (_("\
923 Cannot perform conditional operation on vectors with different sizes"));
924
925           ret = allocate_value (type2);
926
927           for (i = 0; i < highb1 - lowb1 + 1; i++)
928             {
929               tmp = value_logical_not (value_subscript (arg1, i)) ?
930                     value_subscript (arg3, i) : value_subscript (arg2, i);
931               memcpy (value_contents_writeable (ret) +
932                       i * TYPE_LENGTH (eltype2), value_contents_all (tmp),
933                       TYPE_LENGTH (eltype2));
934             }
935
936           return ret;
937         }
938       else
939         {
940           if (value_logical_not (arg1))
941             {
942               /* Skip the second operand.  */
943               evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
944
945               return evaluate_subexp (NULL_TYPE, exp, pos, noside);
946             }
947           else
948             {
949               /* Skip the third operand.  */
950               arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
951               evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
952
953               return arg2;
954             }
955         }
956
957     /* Handle STRUCTOP_STRUCT to allow component access on OpenCL vectors.  */
958     case STRUCTOP_STRUCT:
959       {
960         int pc = (*pos)++;
961         int tem = longest_to_int (exp->elts[pc + 1].longconst);
962
963         (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
964         arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
965         type1 = check_typedef (value_type (arg1));
966
967         if (noside == EVAL_SKIP)
968           {
969             return value_from_longest (builtin_type (exp->gdbarch)->
970                                        builtin_int, 1);
971           }
972         else if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
973           {
974             return opencl_component_ref (exp, arg1, &exp->elts[pc + 2].string,
975                                          noside);
976           }
977         else
978           {
979             if (noside == EVAL_AVOID_SIDE_EFFECTS)
980               return
981                   value_zero (lookup_struct_elt_type
982                               (value_type (arg1),&exp->elts[pc + 2].string, 0),
983                               lval_memory);
984             else
985               return value_struct_elt (&arg1, NULL,
986                                        &exp->elts[pc + 2].string, NULL,
987                                        "structure");
988           }
989       }
990     default:
991       break;
992     }
993
994   return evaluate_subexp_c (expect_type, exp, pos, noside);
995 }
996
997 void
998 opencl_language_arch_info (struct gdbarch *gdbarch,
999                       struct language_arch_info *lai)
1000 {
1001   const struct builtin_opencl_type *builtin = builtin_opencl_type (gdbarch);
1002
1003   lai->string_char_type = builtin->builtin_char;
1004   lai->primitive_type_vector
1005     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_opencl_primitive_types + 1,
1006                               struct type *);
1007
1008 /* This macro fills the primitive_type_vector from a given type.  */
1009 #define FILL_TYPE_VECTOR(LAI, TYPE)\
1010   LAI->primitive_type_vector [opencl_primitive_type_##TYPE]\
1011     = builtin->builtin_##TYPE;\
1012   LAI->primitive_type_vector [opencl_primitive_type_##TYPE##2]\
1013     = builtin->builtin_##TYPE##2;\
1014   LAI->primitive_type_vector [opencl_primitive_type_##TYPE##3]\
1015     = builtin->builtin_##TYPE##3;\
1016   LAI->primitive_type_vector [opencl_primitive_type_##TYPE##4]\
1017     = builtin->builtin_##TYPE##4;\
1018   LAI->primitive_type_vector [opencl_primitive_type_##TYPE##8]\
1019     = builtin->builtin_##TYPE##8;\
1020   LAI->primitive_type_vector [opencl_primitive_type_##TYPE##16]\
1021     = builtin->builtin_##TYPE##16
1022
1023   FILL_TYPE_VECTOR (lai, char);
1024   FILL_TYPE_VECTOR (lai, uchar);
1025   FILL_TYPE_VECTOR (lai, short);
1026   FILL_TYPE_VECTOR (lai, ushort);
1027   FILL_TYPE_VECTOR (lai, int);
1028   FILL_TYPE_VECTOR (lai, uint);
1029   FILL_TYPE_VECTOR (lai, long);
1030   FILL_TYPE_VECTOR (lai, ulong);
1031   FILL_TYPE_VECTOR (lai, half);
1032   FILL_TYPE_VECTOR (lai, float);
1033   FILL_TYPE_VECTOR (lai, double);
1034   lai->primitive_type_vector [opencl_primitive_type_bool]
1035     = builtin->builtin_bool;
1036   lai->primitive_type_vector [opencl_primitive_type_unsigned_char]
1037     = builtin->builtin_unsigned_char;
1038   lai->primitive_type_vector [opencl_primitive_type_unsigned_short]
1039     = builtin->builtin_unsigned_short;
1040   lai->primitive_type_vector [opencl_primitive_type_unsigned_int]
1041     = builtin->builtin_unsigned_int;
1042   lai->primitive_type_vector [opencl_primitive_type_unsigned_long]
1043     = builtin->builtin_unsigned_long;
1044   lai->primitive_type_vector [opencl_primitive_type_half]
1045     = builtin->builtin_half;
1046   lai->primitive_type_vector [opencl_primitive_type_size_t]
1047     = builtin->builtin_size_t;
1048   lai->primitive_type_vector [opencl_primitive_type_ptrdiff_t]
1049     = builtin->builtin_ptrdiff_t;
1050   lai->primitive_type_vector [opencl_primitive_type_intptr_t]
1051     = builtin->builtin_intptr_t;
1052   lai->primitive_type_vector [opencl_primitive_type_uintptr_t]
1053     = builtin->builtin_uintptr_t;
1054   lai->primitive_type_vector [opencl_primitive_type_void]
1055     = builtin->builtin_void;
1056
1057   /* Specifies the return type of logical and relational operations.  */
1058   lai->bool_type_symbol = "int";
1059   lai->bool_type_default = builtin->builtin_int;
1060 }
1061
1062 const struct exp_descriptor exp_descriptor_opencl =
1063 {
1064   print_subexp_standard,
1065   operator_length_standard,
1066   operator_check_standard,
1067   op_name_standard,
1068   dump_subexp_body_standard,
1069   evaluate_subexp_opencl
1070 };
1071
1072 const struct language_defn opencl_language_defn =
1073 {
1074   "opencl",                     /* Language name */
1075   language_opencl,
1076   range_check_off,
1077   type_check_off,
1078   case_sensitive_on,
1079   array_row_major,
1080   macro_expansion_c,
1081   &exp_descriptor_opencl,
1082   c_parse,
1083   c_error,
1084   null_post_parser,
1085   c_printchar,                  /* Print a character constant */
1086   c_printstr,                   /* Function to print string constant */
1087   c_emit_char,                  /* Print a single char */
1088   c_print_type,                 /* Print a type using appropriate syntax */
1089   c_print_typedef,              /* Print a typedef using appropriate syntax */
1090   c_val_print,                  /* Print a value using appropriate syntax */
1091   c_value_print,                /* Print a top-level value */
1092   NULL,                         /* Language specific skip_trampoline */
1093   NULL,                         /* name_of_this */
1094   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1095   basic_lookup_transparent_type,/* lookup_transparent_type */
1096   NULL,                         /* Language specific symbol demangler */
1097   NULL,                         /* Language specific class_name_from_physname */
1098   c_op_print_tab,               /* expression operators for printing */
1099   1,                            /* c-style arrays */
1100   0,                            /* String lower bound */
1101   default_word_break_characters,
1102   default_make_symbol_completion_list,
1103   opencl_language_arch_info,
1104   default_print_array_index,
1105   default_pass_by_reference,
1106   c_get_string,
1107   LANG_MAGIC
1108 };
1109
1110 static void *
1111 build_opencl_types (struct gdbarch *gdbarch)
1112 {
1113   struct builtin_opencl_type *builtin_opencl_type
1114     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_opencl_type);
1115
1116 /* Helper macro to create strings.  */
1117 #define STRINGIFY(S) #S
1118 /* This macro allocates and assigns the type struct pointers
1119    for the vector types.  */
1120 #define BUILD_OCL_VTYPES(TYPE)\
1121   builtin_opencl_type->builtin_##TYPE##2\
1122     = init_vector_type (builtin_opencl_type->builtin_##TYPE, 2);\
1123   TYPE_NAME (builtin_opencl_type->builtin_##TYPE##2) = STRINGIFY(TYPE ## 2);\
1124   builtin_opencl_type->builtin_##TYPE##3\
1125     = init_vector_type (builtin_opencl_type->builtin_##TYPE, 3);\
1126   TYPE_NAME (builtin_opencl_type->builtin_##TYPE##3) = STRINGIFY(TYPE ## 3);\
1127   TYPE_LENGTH (builtin_opencl_type->builtin_##TYPE##3)\
1128     = 4 * TYPE_LENGTH (builtin_opencl_type->builtin_##TYPE);\
1129   builtin_opencl_type->builtin_##TYPE##4\
1130     = init_vector_type (builtin_opencl_type->builtin_##TYPE, 4);\
1131   TYPE_NAME (builtin_opencl_type->builtin_##TYPE##4) = STRINGIFY(TYPE ## 4);\
1132   builtin_opencl_type->builtin_##TYPE##8\
1133     = init_vector_type (builtin_opencl_type->builtin_##TYPE, 8);\
1134   TYPE_NAME (builtin_opencl_type->builtin_##TYPE##8) = STRINGIFY(TYPE ## 8);\
1135   builtin_opencl_type->builtin_##TYPE##16\
1136     = init_vector_type (builtin_opencl_type->builtin_##TYPE, 16);\
1137   TYPE_NAME (builtin_opencl_type->builtin_##TYPE##16) = STRINGIFY(TYPE ## 16)
1138
1139   builtin_opencl_type->builtin_char
1140     = arch_integer_type (gdbarch, 8, 0, "char");
1141   BUILD_OCL_VTYPES (char);
1142   builtin_opencl_type->builtin_uchar
1143     = arch_integer_type (gdbarch, 8, 1, "uchar");
1144   BUILD_OCL_VTYPES (uchar);
1145   builtin_opencl_type->builtin_short
1146     = arch_integer_type (gdbarch, 16, 0, "short");
1147   BUILD_OCL_VTYPES (short);
1148   builtin_opencl_type->builtin_ushort
1149     = arch_integer_type (gdbarch, 16, 1, "ushort");
1150   BUILD_OCL_VTYPES (ushort);
1151   builtin_opencl_type->builtin_int
1152     = arch_integer_type (gdbarch, 32, 0, "int");
1153   BUILD_OCL_VTYPES (int);
1154   builtin_opencl_type->builtin_uint
1155     = arch_integer_type (gdbarch, 32, 1, "uint");
1156   BUILD_OCL_VTYPES (uint);
1157   builtin_opencl_type->builtin_long
1158     = arch_integer_type (gdbarch, 64, 0, "long");
1159   BUILD_OCL_VTYPES (long);
1160   builtin_opencl_type->builtin_ulong
1161     = arch_integer_type (gdbarch, 64, 1, "ulong");
1162   BUILD_OCL_VTYPES (ulong);
1163   builtin_opencl_type->builtin_half
1164     = arch_float_type (gdbarch, 16, "half", floatformats_ieee_half);
1165   BUILD_OCL_VTYPES (half);
1166   builtin_opencl_type->builtin_float
1167     = arch_float_type (gdbarch, 32, "float", floatformats_ieee_single);
1168   BUILD_OCL_VTYPES (float);
1169   builtin_opencl_type->builtin_double
1170     = arch_float_type (gdbarch, 64, "double", floatformats_ieee_double);
1171   BUILD_OCL_VTYPES (double);
1172   builtin_opencl_type->builtin_bool
1173     = arch_boolean_type (gdbarch, 32, 1, "bool");
1174   builtin_opencl_type->builtin_unsigned_char
1175     = arch_integer_type (gdbarch, 8, 1, "unsigned char");
1176   builtin_opencl_type->builtin_unsigned_short
1177     = arch_integer_type (gdbarch, 16, 1, "unsigned short");
1178   builtin_opencl_type->builtin_unsigned_int
1179     = arch_integer_type (gdbarch, 32, 1, "unsigned int");
1180   builtin_opencl_type->builtin_unsigned_long
1181     = arch_integer_type (gdbarch, 64, 1, "unsigned long");
1182   builtin_opencl_type->builtin_size_t
1183     = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "size_t");
1184   builtin_opencl_type->builtin_ptrdiff_t
1185     = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "ptrdiff_t");
1186   builtin_opencl_type->builtin_intptr_t
1187     = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "intptr_t");
1188   builtin_opencl_type->builtin_uintptr_t
1189     = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr_t");
1190   builtin_opencl_type->builtin_void
1191     = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
1192
1193   return builtin_opencl_type;
1194 }
1195
1196 void
1197 _initialize_opencl_language (void)
1198 {
1199   opencl_type_data = gdbarch_data_register_post_init (build_opencl_types);
1200   add_language (&opencl_language_defn);
1201 }