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