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