2011-02-28 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / opencl-lang.c
1 /* OpenCL language support for GDB, the GNU debugger.
2 Copyright (C) 2010, 2011 Free Software Foundation, Inc.
3
4 Contributed by Ken Werner <ken.werner@de.ibm.com>.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include "gdbtypes.h"
24 #include "symtab.h"
25 #include "expression.h"
26 #include "parser-defs.h"
27 #include "symtab.h"
28 #include "language.h"
29 #include "c-lang.h"
30 #include "gdb_assert.h"
31
32 extern void _initialize_opencl_language (void);
33
34 /* This macro generates enum values from a given type. */
35
36 #define OCL_P_TYPE(TYPE)\
37 opencl_primitive_type_##TYPE,\
38 opencl_primitive_type_##TYPE##2,\
39 opencl_primitive_type_##TYPE##3,\
40 opencl_primitive_type_##TYPE##4,\
41 opencl_primitive_type_##TYPE##8,\
42 opencl_primitive_type_##TYPE##16
43
44 enum opencl_primitive_types {
45 OCL_P_TYPE (char),
46 OCL_P_TYPE (uchar),
47 OCL_P_TYPE (short),
48 OCL_P_TYPE (ushort),
49 OCL_P_TYPE (int),
50 OCL_P_TYPE (uint),
51 OCL_P_TYPE (long),
52 OCL_P_TYPE (ulong),
53 OCL_P_TYPE (half),
54 OCL_P_TYPE (float),
55 OCL_P_TYPE (double),
56 opencl_primitive_type_bool,
57 opencl_primitive_type_unsigned_char,
58 opencl_primitive_type_unsigned_short,
59 opencl_primitive_type_unsigned_int,
60 opencl_primitive_type_unsigned_long,
61 opencl_primitive_type_size_t,
62 opencl_primitive_type_ptrdiff_t,
63 opencl_primitive_type_intptr_t,
64 opencl_primitive_type_uintptr_t,
65 opencl_primitive_type_void,
66 nr_opencl_primitive_types
67 };
68
69 static struct gdbarch_data *opencl_type_data;
70
71 struct type **
72 builtin_opencl_type (struct gdbarch *gdbarch)
73 {
74 return gdbarch_data (gdbarch, opencl_type_data);
75 }
76
77 /* Returns the corresponding OpenCL vector type from the given type code,
78 the length of the element type, the unsigned flag and the amount of
79 elements (N). */
80
81 static struct type *
82 lookup_opencl_vector_type (struct gdbarch *gdbarch, enum type_code code,
83 unsigned int el_length, unsigned int flag_unsigned,
84 int n)
85 {
86 int i;
87 unsigned int length;
88 struct type *type = NULL;
89 struct type **types = builtin_opencl_type (gdbarch);
90
91 /* Check if n describes a valid OpenCL vector size (2, 3, 4, 8, 16). */
92 if (n != 2 && n != 3 && n != 4 && n != 8 && n != 16)
93 error (_("Invalid OpenCL vector size: %d"), n);
94
95 /* Triple vectors have the size of a quad vector. */
96 length = (n == 3) ? el_length * 4 : el_length * n;
97
98 for (i = 0; i < nr_opencl_primitive_types; i++)
99 {
100 LONGEST lowb, highb;
101
102 if (TYPE_CODE (types[i]) == TYPE_CODE_ARRAY && TYPE_VECTOR (types[i])
103 && get_array_bounds (types[i], &lowb, &highb)
104 && TYPE_CODE (TYPE_TARGET_TYPE (types[i])) == code
105 && TYPE_UNSIGNED (TYPE_TARGET_TYPE (types[i])) == flag_unsigned
106 && TYPE_LENGTH (TYPE_TARGET_TYPE (types[i])) == el_length
107 && TYPE_LENGTH (types[i]) == length
108 && highb - lowb + 1 == n)
109 {
110 type = types[i];
111 break;
112 }
113 }
114
115 return type;
116 }
117
118 /* Returns nonzero if the array ARR contains duplicates within
119 the first N elements. */
120
121 static int
122 array_has_dups (int *arr, int n)
123 {
124 int i, j;
125
126 for (i = 0; i < n; i++)
127 {
128 for (j = i + 1; j < n; j++)
129 {
130 if (arr[i] == arr[j])
131 return 1;
132 }
133 }
134
135 return 0;
136 }
137
138 /* The OpenCL component access syntax allows to create lvalues referring to
139 selected elements of an original OpenCL vector in arbitrary order. This
140 structure holds the information to describe such lvalues. */
141
142 struct lval_closure
143 {
144 /* Reference count. */
145 int refc;
146 /* The number of indices. */
147 int n;
148 /* The element indices themselves. */
149 int *indices;
150 /* A pointer to the original value. */
151 struct value *val;
152 };
153
154 /* Allocates an instance of struct lval_closure. */
155
156 static struct lval_closure *
157 allocate_lval_closure (int *indices, int n, struct value *val)
158 {
159 struct lval_closure *c = XZALLOC (struct lval_closure);
160
161 c->refc = 1;
162 c->n = n;
163 c->indices = XCALLOC (n, int);
164 memcpy (c->indices, indices, n * sizeof (int));
165 value_incref (val); /* Increment the reference counter of the value. */
166 c->val = val;
167
168 return c;
169 }
170
171 static void
172 lval_func_read (struct value *v)
173 {
174 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
175 struct type *type = check_typedef (value_type (v));
176 struct type *eltype = TYPE_TARGET_TYPE (check_typedef (value_type (c->val)));
177 int offset = value_offset (v);
178 int elsize = TYPE_LENGTH (eltype);
179 int n, i, j = 0;
180 LONGEST lowb = 0;
181 LONGEST highb = 0;
182
183 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
184 && !get_array_bounds (type, &lowb, &highb))
185 error (_("Could not determine the vector bounds"));
186
187 /* Assume elsize aligned offset. */
188 gdb_assert (offset % elsize == 0);
189 offset /= elsize;
190 n = offset + highb - lowb + 1;
191 gdb_assert (n <= c->n);
192
193 for (i = offset; i < n; i++)
194 memcpy (value_contents_raw (v) + j++ * elsize,
195 value_contents (c->val) + c->indices[i] * elsize,
196 elsize);
197 }
198
199 static void
200 lval_func_write (struct value *v, struct value *fromval)
201 {
202 struct value *mark = value_mark ();
203 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
204 struct type *type = check_typedef (value_type (v));
205 struct type *eltype = TYPE_TARGET_TYPE (check_typedef (value_type (c->val)));
206 int offset = value_offset (v);
207 int elsize = TYPE_LENGTH (eltype);
208 int n, i, j = 0;
209 LONGEST lowb = 0;
210 LONGEST highb = 0;
211
212 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
213 && !get_array_bounds (type, &lowb, &highb))
214 error (_("Could not determine the vector bounds"));
215
216 /* Assume elsize aligned offset. */
217 gdb_assert (offset % elsize == 0);
218 offset /= elsize;
219 n = offset + highb - lowb + 1;
220
221 /* Since accesses to the fourth component of a triple vector is undefined we
222 just skip writes to the fourth element. Imagine something like this:
223 int3 i3 = (int3)(0, 1, 2);
224 i3.hi.hi = 5;
225 In this case n would be 4 (offset=12/4 + 1) while c->n would be 3. */
226 if (n > c->n)
227 n = c->n;
228
229 for (i = offset; i < n; i++)
230 {
231 struct value *from_elm_val = allocate_value (eltype);
232 struct value *to_elm_val = value_subscript (c->val, c->indices[i]);
233
234 memcpy (value_contents_writeable (from_elm_val),
235 value_contents (fromval) + j++ * elsize,
236 elsize);
237 value_assign (to_elm_val, from_elm_val);
238 }
239
240 value_free_to_mark (mark);
241 }
242
243 /* Return nonzero if all bits in V within OFFSET and LENGTH are valid. */
244
245 static int
246 lval_func_check_validity (const struct value *v, int offset, int length)
247 {
248 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
249 /* Size of the target type in bits. */
250 int elsize =
251 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c->val)))) * 8;
252 int startrest = offset % elsize;
253 int start = offset / elsize;
254 int endrest = (offset + length) % elsize;
255 int end = (offset + length) / elsize;
256 int i;
257
258 if (endrest)
259 end++;
260
261 if (end > c->n)
262 return 0;
263
264 for (i = start; i < end; i++)
265 {
266 int comp_offset = (i == start) ? startrest : 0;
267 int comp_length = (i == end) ? endrest : elsize;
268
269 if (!value_bits_valid (c->val, c->indices[i] * elsize + comp_offset,
270 comp_length))
271 return 0;
272 }
273
274 return 1;
275 }
276
277 /* Return nonzero if any bit in V is valid. */
278
279 static int
280 lval_func_check_any_valid (const struct value *v)
281 {
282 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
283 /* Size of the target type in bits. */
284 int elsize =
285 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c->val)))) * 8;
286 int i;
287
288 for (i = 0; i < c->n; i++)
289 if (value_bits_valid (c->val, c->indices[i] * elsize, elsize))
290 return 1;
291
292 return 0;
293 }
294
295 /* Return nonzero if bits in V from OFFSET and LENGTH represent a
296 synthetic pointer. */
297
298 static int
299 lval_func_check_synthetic_pointer (const struct value *v,
300 int offset, int length)
301 {
302 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
303 /* Size of the target type in bits. */
304 int elsize =
305 TYPE_LENGTH (TYPE_TARGET_TYPE (check_typedef (value_type (c->val)))) * 8;
306 int startrest = offset % elsize;
307 int start = offset / elsize;
308 int endrest = (offset + length) % elsize;
309 int end = (offset + length) / elsize;
310 int i;
311
312 if (endrest)
313 end++;
314
315 if (end > c->n)
316 return 0;
317
318 for (i = start; i < end; i++)
319 {
320 int comp_offset = (i == start) ? startrest : 0;
321 int comp_length = (i == end) ? endrest : elsize;
322
323 if (!value_bits_synthetic_pointer (c->val,
324 c->indices[i] * elsize + comp_offset,
325 comp_length))
326 return 0;
327 }
328
329 return 1;
330 }
331
332 static void *
333 lval_func_copy_closure (const struct value *v)
334 {
335 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
336
337 ++c->refc;
338
339 return c;
340 }
341
342 static void
343 lval_func_free_closure (struct value *v)
344 {
345 struct lval_closure *c = (struct lval_closure *) value_computed_closure (v);
346
347 --c->refc;
348
349 if (c->refc == 0)
350 {
351 value_free (c->val); /* Decrement the reference counter of the value. */
352 xfree (c->indices);
353 xfree (c);
354 }
355 }
356
357 static struct lval_funcs opencl_value_funcs =
358 {
359 lval_func_read,
360 lval_func_write,
361 lval_func_check_validity,
362 lval_func_check_any_valid,
363 NULL,
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 relational operation on two operands. */
686
687 static struct value *
688 opencl_relop (struct expression *exp, struct value *arg1, struct value *arg2,
689 enum exp_opcode op)
690 {
691 struct value *val;
692 struct type *type1 = check_typedef (value_type (arg1));
693 struct type *type2 = check_typedef (value_type (arg2));
694 int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
695 && TYPE_VECTOR (type1));
696 int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
697 && TYPE_VECTOR (type2));
698
699 if (!t1_is_vec && !t2_is_vec)
700 {
701 int tmp = scalar_relop (arg1, arg2, op);
702 struct type *type =
703 language_bool_type (exp->language_defn, exp->gdbarch);
704
705 val = value_from_longest (type, tmp);
706 }
707 else if (t1_is_vec && t2_is_vec)
708 {
709 val = vector_relop (exp, arg1, arg2, op);
710 }
711 else
712 {
713 /* Widen the scalar operand to a vector. */
714 struct value **v = t1_is_vec ? &arg2 : &arg1;
715 struct type *t = t1_is_vec ? type2 : type1;
716
717 if (TYPE_CODE (t) != TYPE_CODE_FLT && !is_integral_type (t))
718 error (_("Argument to operation not a number or boolean."));
719
720 *v = value_cast (t1_is_vec ? type1 : type2, *v);
721 val = vector_relop (exp, arg1, arg2, op);
722 }
723
724 return val;
725 }
726
727 /* Expression evaluator for the OpenCL. Most operations are delegated to
728 evaluate_subexp_standard; see that function for a description of the
729 arguments. */
730
731 static struct value *
732 evaluate_subexp_opencl (struct type *expect_type, struct expression *exp,
733 int *pos, enum noside noside)
734 {
735 enum exp_opcode op = exp->elts[*pos].opcode;
736 struct value *arg1 = NULL;
737 struct value *arg2 = NULL;
738 struct type *type1, *type2;
739
740 switch (op)
741 {
742 /* Handle binary relational and equality operators that are either not
743 or differently defined for GNU vectors. */
744 case BINOP_EQUAL:
745 case BINOP_NOTEQUAL:
746 case BINOP_LESS:
747 case BINOP_GTR:
748 case BINOP_GEQ:
749 case BINOP_LEQ:
750 (*pos)++;
751 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
752 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
753
754 if (noside == EVAL_SKIP)
755 return value_from_longest (builtin_type (exp->gdbarch)->
756 builtin_int, 1);
757
758 return opencl_relop (exp, arg1, arg2, op);
759
760 /* Handle the logical unary operator not(!). */
761 case UNOP_LOGICAL_NOT:
762 (*pos)++;
763 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
764
765 if (noside == EVAL_SKIP)
766 return value_from_longest (builtin_type (exp->gdbarch)->
767 builtin_int, 1);
768
769 return opencl_logical_not (exp, arg1);
770
771 /* Handle the logical operator and(&&) and or(||). */
772 case BINOP_LOGICAL_AND:
773 case BINOP_LOGICAL_OR:
774 (*pos)++;
775 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
776
777 if (noside == EVAL_SKIP)
778 {
779 evaluate_subexp (NULL_TYPE, exp, pos, noside);
780
781 return value_from_longest (builtin_type (exp->gdbarch)->
782 builtin_int, 1);
783 }
784 else
785 {
786 /* For scalar operations we need to avoid evaluating operands
787 unecessarily. However, for vector operations we always need to
788 evaluate both operands. Unfortunately we only know which of the
789 two cases apply after we know the type of the second operand.
790 Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS. */
791 int oldpos = *pos;
792
793 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
794 EVAL_AVOID_SIDE_EFFECTS);
795 *pos = oldpos;
796 type1 = check_typedef (value_type (arg1));
797 type2 = check_typedef (value_type (arg2));
798
799 if ((TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
800 || (TYPE_CODE (type2) == TYPE_CODE_ARRAY && TYPE_VECTOR (type2)))
801 {
802 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
803
804 return opencl_relop (exp, arg1, arg2, op);
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
817 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
818 tmp ? EVAL_SKIP : noside);
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)++;
833 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
834 type1 = check_typedef (value_type (arg1));
835 if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
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
842 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
843 arg3 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
844 type2 = check_typedef (value_type (arg2));
845 type3 = check_typedef (value_type (arg3));
846 t2_is_vec
847 = TYPE_CODE (type2) == TYPE_CODE_ARRAY && TYPE_VECTOR (type2);
848 t3_is_vec
849 = TYPE_CODE (type3) == TYPE_CODE_ARRAY && TYPE_VECTOR (type3);
850
851 /* Widen the scalar operand to a vector if necessary. */
852 if (t2_is_vec || !t3_is_vec)
853 {
854 arg3 = value_cast (type2, arg3);
855 type3 = value_type (arg3);
856 }
857 else if (!t2_is_vec || t3_is_vec)
858 {
859 arg2 = value_cast (type3, arg2);
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 (_("\
866 Cannot 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. */
878 if (TYPE_CODE (eltype2) != TYPE_CODE (eltype3)
879 || TYPE_LENGTH (eltype2) != TYPE_LENGTH (eltype3)
880 || TYPE_UNSIGNED (eltype2) != TYPE_UNSIGNED (eltype3)
881 || lowb2 != lowb3 || highb2 != highb3)
882 error (_("\
883 Cannot 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 (_("\
889 Cannot 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. */
909 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
910
911 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
912 }
913 else
914 {
915 /* Skip the third operand. */
916 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
917 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
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);
930 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
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 }
938 else if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
939 {
940 return opencl_component_ref (exp, arg1, &exp->elts[pc + 2].string,
941 noside);
942 }
943 else
944 {
945 if (noside == EVAL_AVOID_SIDE_EFFECTS)
946 return
947 value_zero (lookup_struct_elt_type
948 (value_type (arg1),&exp->elts[pc + 2].string, 0),
949 lval_memory);
950 else
951 return value_struct_elt (&arg1, NULL,
952 &exp->elts[pc + 2].string, NULL,
953 "structure");
954 }
955 }
956 default:
957 break;
958 }
959
960 return evaluate_subexp_c (expect_type, exp, pos, noside);
961 }
962
963 void
964 opencl_language_arch_info (struct gdbarch *gdbarch,
965 struct language_arch_info *lai)
966 {
967 struct type **types = builtin_opencl_type (gdbarch);
968
969 /* Copy primitive types vector from gdbarch. */
970 lai->primitive_type_vector = types;
971
972 /* Type of elements of strings. */
973 lai->string_char_type = types [opencl_primitive_type_char];
974
975 /* Specifies the return type of logical and relational operations. */
976 lai->bool_type_symbol = "int";
977 lai->bool_type_default = types [opencl_primitive_type_int];
978 }
979
980 const struct exp_descriptor exp_descriptor_opencl =
981 {
982 print_subexp_standard,
983 operator_length_standard,
984 operator_check_standard,
985 op_name_standard,
986 dump_subexp_body_standard,
987 evaluate_subexp_opencl
988 };
989
990 const struct language_defn opencl_language_defn =
991 {
992 "opencl", /* Language name */
993 language_opencl,
994 range_check_off,
995 type_check_off,
996 case_sensitive_on,
997 array_row_major,
998 macro_expansion_c,
999 &exp_descriptor_opencl,
1000 c_parse,
1001 c_error,
1002 null_post_parser,
1003 c_printchar, /* Print a character constant */
1004 c_printstr, /* Function to print string constant */
1005 c_emit_char, /* Print a single char */
1006 c_print_type, /* Print a type using appropriate syntax */
1007 c_print_typedef, /* Print a typedef using appropriate syntax */
1008 c_val_print, /* Print a value using appropriate syntax */
1009 c_value_print, /* Print a top-level value */
1010 NULL, /* Language specific skip_trampoline */
1011 NULL, /* name_of_this */
1012 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1013 basic_lookup_transparent_type,/* lookup_transparent_type */
1014 NULL, /* Language specific symbol demangler */
1015 NULL, /* Language specific
1016 class_name_from_physname */
1017 c_op_print_tab, /* expression operators for printing */
1018 1, /* c-style arrays */
1019 0, /* String lower bound */
1020 default_word_break_characters,
1021 default_make_symbol_completion_list,
1022 opencl_language_arch_info,
1023 default_print_array_index,
1024 default_pass_by_reference,
1025 c_get_string,
1026 LANG_MAGIC
1027 };
1028
1029 static void *
1030 build_opencl_types (struct gdbarch *gdbarch)
1031 {
1032 struct type **types
1033 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_opencl_primitive_types + 1,
1034 struct type *);
1035
1036 /* Helper macro to create strings. */
1037 #define OCL_STRING(S) #S
1038 /* This macro allocates and assigns the type struct pointers
1039 for the vector types. */
1040 #define BUILD_OCL_VTYPES(TYPE)\
1041 types[opencl_primitive_type_##TYPE##2] \
1042 = init_vector_type (types[opencl_primitive_type_##TYPE], 2); \
1043 TYPE_NAME (types[opencl_primitive_type_##TYPE##2]) = OCL_STRING(TYPE ## 2); \
1044 types[opencl_primitive_type_##TYPE##3] \
1045 = init_vector_type (types[opencl_primitive_type_##TYPE], 3); \
1046 TYPE_NAME (types[opencl_primitive_type_##TYPE##3]) = OCL_STRING(TYPE ## 3); \
1047 TYPE_LENGTH (types[opencl_primitive_type_##TYPE##3]) \
1048 = 4 * TYPE_LENGTH (types[opencl_primitive_type_##TYPE]); \
1049 types[opencl_primitive_type_##TYPE##4] \
1050 = init_vector_type (types[opencl_primitive_type_##TYPE], 4); \
1051 TYPE_NAME (types[opencl_primitive_type_##TYPE##4]) = OCL_STRING(TYPE ## 4); \
1052 types[opencl_primitive_type_##TYPE##8] \
1053 = init_vector_type (types[opencl_primitive_type_##TYPE], 8); \
1054 TYPE_NAME (types[opencl_primitive_type_##TYPE##8]) = OCL_STRING(TYPE ## 8); \
1055 types[opencl_primitive_type_##TYPE##16] \
1056 = init_vector_type (types[opencl_primitive_type_##TYPE], 16); \
1057 TYPE_NAME (types[opencl_primitive_type_##TYPE##16]) = OCL_STRING(TYPE ## 16)
1058
1059 types[opencl_primitive_type_char]
1060 = arch_integer_type (gdbarch, 8, 0, "char");
1061 BUILD_OCL_VTYPES (char);
1062 types[opencl_primitive_type_uchar]
1063 = arch_integer_type (gdbarch, 8, 1, "uchar");
1064 BUILD_OCL_VTYPES (uchar);
1065 types[opencl_primitive_type_short]
1066 = arch_integer_type (gdbarch, 16, 0, "short");
1067 BUILD_OCL_VTYPES (short);
1068 types[opencl_primitive_type_ushort]
1069 = arch_integer_type (gdbarch, 16, 1, "ushort");
1070 BUILD_OCL_VTYPES (ushort);
1071 types[opencl_primitive_type_int]
1072 = arch_integer_type (gdbarch, 32, 0, "int");
1073 BUILD_OCL_VTYPES (int);
1074 types[opencl_primitive_type_uint]
1075 = arch_integer_type (gdbarch, 32, 1, "uint");
1076 BUILD_OCL_VTYPES (uint);
1077 types[opencl_primitive_type_long]
1078 = arch_integer_type (gdbarch, 64, 0, "long");
1079 BUILD_OCL_VTYPES (long);
1080 types[opencl_primitive_type_ulong]
1081 = arch_integer_type (gdbarch, 64, 1, "ulong");
1082 BUILD_OCL_VTYPES (ulong);
1083 types[opencl_primitive_type_half]
1084 = arch_float_type (gdbarch, 16, "half", floatformats_ieee_half);
1085 BUILD_OCL_VTYPES (half);
1086 types[opencl_primitive_type_float]
1087 = arch_float_type (gdbarch, 32, "float", floatformats_ieee_single);
1088 BUILD_OCL_VTYPES (float);
1089 types[opencl_primitive_type_double]
1090 = arch_float_type (gdbarch, 64, "double", floatformats_ieee_double);
1091 BUILD_OCL_VTYPES (double);
1092 types[opencl_primitive_type_bool]
1093 = arch_boolean_type (gdbarch, 8, 1, "bool");
1094 types[opencl_primitive_type_unsigned_char]
1095 = arch_integer_type (gdbarch, 8, 1, "unsigned char");
1096 types[opencl_primitive_type_unsigned_short]
1097 = arch_integer_type (gdbarch, 16, 1, "unsigned short");
1098 types[opencl_primitive_type_unsigned_int]
1099 = arch_integer_type (gdbarch, 32, 1, "unsigned int");
1100 types[opencl_primitive_type_unsigned_long]
1101 = arch_integer_type (gdbarch, 64, 1, "unsigned long");
1102 types[opencl_primitive_type_size_t]
1103 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "size_t");
1104 types[opencl_primitive_type_ptrdiff_t]
1105 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "ptrdiff_t");
1106 types[opencl_primitive_type_intptr_t]
1107 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 0, "intptr_t");
1108 types[opencl_primitive_type_uintptr_t]
1109 = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch), 1, "uintptr_t");
1110 types[opencl_primitive_type_void]
1111 = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
1112
1113 return types;
1114 }
1115
1116 void
1117 _initialize_opencl_language (void)
1118 {
1119 opencl_type_data = gdbarch_data_register_post_init (build_opencl_types);
1120 add_language (&opencl_language_defn);
1121 }
This page took 0.053771 seconds and 5 git commands to generate.