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