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