PR sanitizer/65400
[official-gcc.git] / gcc / fortran / target-memory.c
blob4d636368d565b231d60d1820d2c2c864e9541153
1 /* Simulate storage of variables into target memory.
2 Copyright (C) 2007-2015 Free Software Foundation, Inc.
3 Contributed by Paul Thomas and Brooks Moses
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "flags.h"
25 #include "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "gfortran.h"
38 #include "arith.h"
39 #include "constructor.h"
40 #include "trans.h"
41 #include "trans-const.h"
42 #include "trans-types.h"
43 #include "target-memory.h"
45 /* --------------------------------------------------------------- */
46 /* Calculate the size of an expression. */
49 static size_t
50 size_integer (int kind)
52 return GET_MODE_SIZE (TYPE_MODE (gfc_get_int_type (kind)));;
56 static size_t
57 size_float (int kind)
59 return GET_MODE_SIZE (TYPE_MODE (gfc_get_real_type (kind)));;
63 static size_t
64 size_complex (int kind)
66 return 2 * size_float (kind);
70 static size_t
71 size_logical (int kind)
73 return GET_MODE_SIZE (TYPE_MODE (gfc_get_logical_type (kind)));;
77 static size_t
78 size_character (int length, int kind)
80 int i = gfc_validate_kind (BT_CHARACTER, kind, false);
81 return length * gfc_character_kinds[i].bit_size / 8;
85 /* Return the size of a single element of the given expression.
86 Identical to gfc_target_expr_size for scalars. */
88 size_t
89 gfc_element_size (gfc_expr *e)
91 tree type;
93 switch (e->ts.type)
95 case BT_INTEGER:
96 return size_integer (e->ts.kind);
97 case BT_REAL:
98 return size_float (e->ts.kind);
99 case BT_COMPLEX:
100 return size_complex (e->ts.kind);
101 case BT_LOGICAL:
102 return size_logical (e->ts.kind);
103 case BT_CHARACTER:
104 if (e->expr_type == EXPR_CONSTANT)
105 return size_character (e->value.character.length, e->ts.kind);
106 else if (e->ts.u.cl != NULL && e->ts.u.cl->length != NULL
107 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
108 && e->ts.u.cl->length->ts.type == BT_INTEGER)
110 int length;
112 gfc_extract_int (e->ts.u.cl->length, &length);
113 return size_character (length, e->ts.kind);
115 else
116 return 0;
118 case BT_HOLLERITH:
119 return e->representation.length;
120 case BT_DERIVED:
121 case BT_CLASS:
122 case BT_VOID:
123 case BT_ASSUMED:
125 /* Determine type size without clobbering the typespec for ISO C
126 binding types. */
127 gfc_typespec ts;
128 HOST_WIDE_INT size;
129 ts = e->ts;
130 type = gfc_typenode_for_spec (&ts);
131 size = int_size_in_bytes (type);
132 gcc_assert (size >= 0);
133 return size;
135 default:
136 gfc_internal_error ("Invalid expression in gfc_element_size.");
137 return 0;
142 /* Return the size of an expression in its target representation. */
144 size_t
145 gfc_target_expr_size (gfc_expr *e)
147 mpz_t tmp;
148 size_t asz;
150 gcc_assert (e != NULL);
152 if (e->rank)
154 if (gfc_array_size (e, &tmp))
155 asz = mpz_get_ui (tmp);
156 else
157 asz = 0;
159 else
160 asz = 1;
162 return asz * gfc_element_size (e);
166 /* The encode_* functions export a value into a buffer, and
167 return the number of bytes of the buffer that have been
168 used. */
170 static unsigned HOST_WIDE_INT
171 encode_array (gfc_expr *expr, unsigned char *buffer, size_t buffer_size)
173 mpz_t array_size;
174 int i;
175 int ptr = 0;
177 gfc_constructor_base ctor = expr->value.constructor;
179 gfc_array_size (expr, &array_size);
180 for (i = 0; i < (int)mpz_get_ui (array_size); i++)
182 ptr += gfc_target_encode_expr (gfc_constructor_lookup_expr (ctor, i),
183 &buffer[ptr], buffer_size - ptr);
186 mpz_clear (array_size);
187 return ptr;
191 static int
192 encode_integer (int kind, mpz_t integer, unsigned char *buffer,
193 size_t buffer_size)
195 return native_encode_expr (gfc_conv_mpz_to_tree (integer, kind),
196 buffer, buffer_size);
200 static int
201 encode_float (int kind, mpfr_t real, unsigned char *buffer, size_t buffer_size)
203 return native_encode_expr (gfc_conv_mpfr_to_tree (real, kind, 0), buffer,
204 buffer_size);
208 static int
209 encode_complex (int kind, mpc_t cmplx,
210 unsigned char *buffer, size_t buffer_size)
212 int size;
213 size = encode_float (kind, mpc_realref (cmplx), &buffer[0], buffer_size);
214 size += encode_float (kind, mpc_imagref (cmplx),
215 &buffer[size], buffer_size - size);
216 return size;
220 static int
221 encode_logical (int kind, int logical, unsigned char *buffer, size_t buffer_size)
223 return native_encode_expr (build_int_cst (gfc_get_logical_type (kind),
224 logical),
225 buffer, buffer_size);
230 gfc_encode_character (int kind, int length, const gfc_char_t *string,
231 unsigned char *buffer, size_t buffer_size)
233 size_t elsize = size_character (1, kind);
234 tree type = gfc_get_char_type (kind);
235 int i;
237 gcc_assert (buffer_size >= size_character (length, kind));
239 for (i = 0; i < length; i++)
240 native_encode_expr (build_int_cst (type, string[i]), &buffer[i*elsize],
241 elsize);
243 return length;
247 static unsigned HOST_WIDE_INT
248 encode_derived (gfc_expr *source, unsigned char *buffer, size_t buffer_size)
250 gfc_constructor *c;
251 gfc_component *cmp;
252 int ptr;
253 tree type;
254 HOST_WIDE_INT size;
256 type = gfc_typenode_for_spec (&source->ts);
258 for (c = gfc_constructor_first (source->value.constructor),
259 cmp = source->ts.u.derived->components;
261 c = gfc_constructor_next (c), cmp = cmp->next)
263 gcc_assert (cmp);
264 if (!c->expr)
265 continue;
266 ptr = TREE_INT_CST_LOW(DECL_FIELD_OFFSET(cmp->backend_decl))
267 + TREE_INT_CST_LOW(DECL_FIELD_BIT_OFFSET(cmp->backend_decl))/8;
269 if (c->expr->expr_type == EXPR_NULL)
271 size = int_size_in_bytes (TREE_TYPE (cmp->backend_decl));
272 gcc_assert (size >= 0);
273 memset (&buffer[ptr], 0, size);
275 else
276 gfc_target_encode_expr (c->expr, &buffer[ptr],
277 buffer_size - ptr);
280 size = int_size_in_bytes (type);
281 gcc_assert (size >= 0);
282 return size;
286 /* Write a constant expression in binary form to a buffer. */
287 unsigned HOST_WIDE_INT
288 gfc_target_encode_expr (gfc_expr *source, unsigned char *buffer,
289 size_t buffer_size)
291 if (source == NULL)
292 return 0;
294 if (source->expr_type == EXPR_ARRAY)
295 return encode_array (source, buffer, buffer_size);
297 gcc_assert (source->expr_type == EXPR_CONSTANT
298 || source->expr_type == EXPR_STRUCTURE
299 || source->expr_type == EXPR_SUBSTRING);
301 /* If we already have a target-memory representation, we use that rather
302 than recreating one. */
303 if (source->representation.string)
305 memcpy (buffer, source->representation.string,
306 source->representation.length);
307 return source->representation.length;
310 switch (source->ts.type)
312 case BT_INTEGER:
313 return encode_integer (source->ts.kind, source->value.integer, buffer,
314 buffer_size);
315 case BT_REAL:
316 return encode_float (source->ts.kind, source->value.real, buffer,
317 buffer_size);
318 case BT_COMPLEX:
319 return encode_complex (source->ts.kind, source->value.complex,
320 buffer, buffer_size);
321 case BT_LOGICAL:
322 return encode_logical (source->ts.kind, source->value.logical, buffer,
323 buffer_size);
324 case BT_CHARACTER:
325 if (source->expr_type == EXPR_CONSTANT || source->ref == NULL)
326 return gfc_encode_character (source->ts.kind,
327 source->value.character.length,
328 source->value.character.string,
329 buffer, buffer_size);
330 else
332 int start, end;
334 gcc_assert (source->expr_type == EXPR_SUBSTRING);
335 gfc_extract_int (source->ref->u.ss.start, &start);
336 gfc_extract_int (source->ref->u.ss.end, &end);
337 return gfc_encode_character (source->ts.kind, MAX(end - start + 1, 0),
338 &source->value.character.string[start-1],
339 buffer, buffer_size);
342 case BT_DERIVED:
343 if (source->ts.u.derived->ts.f90_type == BT_VOID)
345 gfc_constructor *c;
346 gcc_assert (source->expr_type == EXPR_STRUCTURE);
347 c = gfc_constructor_first (source->value.constructor);
348 gcc_assert (c->expr->expr_type == EXPR_CONSTANT
349 && c->expr->ts.type == BT_INTEGER);
350 return encode_integer (gfc_index_integer_kind, c->expr->value.integer,
351 buffer, buffer_size);
354 return encode_derived (source, buffer, buffer_size);
355 default:
356 gfc_internal_error ("Invalid expression in gfc_target_encode_expr.");
357 return 0;
362 static int
363 interpret_array (unsigned char *buffer, size_t buffer_size, gfc_expr *result)
365 gfc_constructor_base base = NULL;
366 int array_size = 1;
367 int i;
368 int ptr = 0;
370 /* Calculate array size from its shape and rank. */
371 gcc_assert (result->rank > 0 && result->shape);
373 for (i = 0; i < result->rank; i++)
374 array_size *= (int)mpz_get_ui (result->shape[i]);
376 /* Iterate over array elements, producing constructors. */
377 for (i = 0; i < array_size; i++)
379 gfc_expr *e = gfc_get_constant_expr (result->ts.type, result->ts.kind,
380 &result->where);
381 e->ts = result->ts;
383 if (e->ts.type == BT_CHARACTER)
384 e->value.character.length = result->value.character.length;
386 gfc_constructor_append_expr (&base, e, &result->where);
388 ptr += gfc_target_interpret_expr (&buffer[ptr], buffer_size - ptr, e,
389 true);
392 result->value.constructor = base;
393 return ptr;
398 gfc_interpret_integer (int kind, unsigned char *buffer, size_t buffer_size,
399 mpz_t integer)
401 mpz_init (integer);
402 gfc_conv_tree_to_mpz (integer,
403 native_interpret_expr (gfc_get_int_type (kind),
404 buffer, buffer_size));
405 return size_integer (kind);
410 gfc_interpret_float (int kind, unsigned char *buffer, size_t buffer_size,
411 mpfr_t real)
413 gfc_set_model_kind (kind);
414 mpfr_init (real);
415 gfc_conv_tree_to_mpfr (real,
416 native_interpret_expr (gfc_get_real_type (kind),
417 buffer, buffer_size));
419 return size_float (kind);
424 gfc_interpret_complex (int kind, unsigned char *buffer, size_t buffer_size,
425 mpc_t complex)
427 int size;
428 size = gfc_interpret_float (kind, &buffer[0], buffer_size,
429 mpc_realref (complex));
430 size += gfc_interpret_float (kind, &buffer[size], buffer_size - size,
431 mpc_imagref (complex));
432 return size;
437 gfc_interpret_logical (int kind, unsigned char *buffer, size_t buffer_size,
438 int *logical)
440 tree t = native_interpret_expr (gfc_get_logical_type (kind), buffer,
441 buffer_size);
442 *logical = wi::eq_p (t, 0) ? 0 : 1;
443 return size_logical (kind);
448 gfc_interpret_character (unsigned char *buffer, size_t buffer_size,
449 gfc_expr *result)
451 int i;
453 if (result->ts.u.cl && result->ts.u.cl->length)
454 result->value.character.length =
455 (int) mpz_get_ui (result->ts.u.cl->length->value.integer);
457 gcc_assert (buffer_size >= size_character (result->value.character.length,
458 result->ts.kind));
459 result->value.character.string =
460 gfc_get_wide_string (result->value.character.length + 1);
462 if (result->ts.kind == gfc_default_character_kind)
463 for (i = 0; i < result->value.character.length; i++)
464 result->value.character.string[i] = (gfc_char_t) buffer[i];
465 else
467 mpz_t integer;
468 unsigned bytes = size_character (1, result->ts.kind);
469 mpz_init (integer);
470 gcc_assert (bytes <= sizeof (unsigned long));
472 for (i = 0; i < result->value.character.length; i++)
474 gfc_conv_tree_to_mpz (integer,
475 native_interpret_expr (gfc_get_char_type (result->ts.kind),
476 &buffer[bytes*i], buffer_size-bytes*i));
477 result->value.character.string[i]
478 = (gfc_char_t) mpz_get_ui (integer);
481 mpz_clear (integer);
484 result->value.character.string[result->value.character.length] = '\0';
486 return result->value.character.length;
491 gfc_interpret_derived (unsigned char *buffer, size_t buffer_size, gfc_expr *result)
493 gfc_component *cmp;
494 int ptr;
495 tree type;
497 /* The attributes of the derived type need to be bolted to the floor. */
498 result->expr_type = EXPR_STRUCTURE;
500 cmp = result->ts.u.derived->components;
502 if (result->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
503 && (result->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
504 || result->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
506 gfc_constructor *c;
507 gfc_expr *e;
508 /* Needed as gfc_typenode_for_spec as gfc_typenode_for_spec
509 sets this to BT_INTEGER. */
510 result->ts.type = BT_DERIVED;
511 e = gfc_get_constant_expr (cmp->ts.type, cmp->ts.kind, &result->where);
512 c = gfc_constructor_append_expr (&result->value.constructor, e, NULL);
513 c->n.component = cmp;
514 gfc_target_interpret_expr (buffer, buffer_size, e, true);
515 e->ts.is_iso_c = 1;
516 return int_size_in_bytes (ptr_type_node);
519 type = gfc_typenode_for_spec (&result->ts);
521 /* Run through the derived type components. */
522 for (;cmp; cmp = cmp->next)
524 gfc_constructor *c;
525 gfc_expr *e = gfc_get_constant_expr (cmp->ts.type, cmp->ts.kind,
526 &result->where);
527 e->ts = cmp->ts;
529 /* Copy shape, if needed. */
530 if (cmp->as && cmp->as->rank)
532 int n;
534 e->expr_type = EXPR_ARRAY;
535 e->rank = cmp->as->rank;
537 e->shape = gfc_get_shape (e->rank);
538 for (n = 0; n < e->rank; n++)
540 mpz_init_set_ui (e->shape[n], 1);
541 mpz_add (e->shape[n], e->shape[n],
542 cmp->as->upper[n]->value.integer);
543 mpz_sub (e->shape[n], e->shape[n],
544 cmp->as->lower[n]->value.integer);
548 c = gfc_constructor_append_expr (&result->value.constructor, e, NULL);
550 /* The constructor points to the component. */
551 c->n.component = cmp;
553 /* Calculate the offset, which consists of the FIELD_OFFSET in
554 bytes, which appears in multiples of DECL_OFFSET_ALIGN-bit-sized,
555 and additional bits of FIELD_BIT_OFFSET. The code assumes that all
556 sizes of the components are multiples of BITS_PER_UNIT,
557 i.e. there are, e.g., no bit fields. */
559 gcc_assert (cmp->backend_decl);
560 ptr = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (cmp->backend_decl));
561 gcc_assert (ptr % 8 == 0);
562 ptr = ptr/8 + TREE_INT_CST_LOW (DECL_FIELD_OFFSET (cmp->backend_decl));
564 gfc_target_interpret_expr (&buffer[ptr], buffer_size - ptr, e, true);
567 return int_size_in_bytes (type);
571 /* Read a binary buffer to a constant expression. */
573 gfc_target_interpret_expr (unsigned char *buffer, size_t buffer_size,
574 gfc_expr *result, bool convert_widechar)
576 if (result->expr_type == EXPR_ARRAY)
577 return interpret_array (buffer, buffer_size, result);
579 switch (result->ts.type)
581 case BT_INTEGER:
582 result->representation.length =
583 gfc_interpret_integer (result->ts.kind, buffer, buffer_size,
584 result->value.integer);
585 break;
587 case BT_REAL:
588 result->representation.length =
589 gfc_interpret_float (result->ts.kind, buffer, buffer_size,
590 result->value.real);
591 break;
593 case BT_COMPLEX:
594 result->representation.length =
595 gfc_interpret_complex (result->ts.kind, buffer, buffer_size,
596 result->value.complex);
597 break;
599 case BT_LOGICAL:
600 result->representation.length =
601 gfc_interpret_logical (result->ts.kind, buffer, buffer_size,
602 &result->value.logical);
603 break;
605 case BT_CHARACTER:
606 result->representation.length =
607 gfc_interpret_character (buffer, buffer_size, result);
608 break;
610 case BT_CLASS:
611 result->ts = CLASS_DATA (result)->ts;
612 /* Fall through. */
613 case BT_DERIVED:
614 result->representation.length =
615 gfc_interpret_derived (buffer, buffer_size, result);
616 gcc_assert (result->representation.length >= 0);
617 break;
619 default:
620 gfc_internal_error ("Invalid expression in gfc_target_interpret_expr.");
621 break;
624 if (result->ts.type == BT_CHARACTER && convert_widechar)
625 result->representation.string
626 = gfc_widechar_to_char (result->value.character.string,
627 result->value.character.length);
628 else
630 result->representation.string =
631 XCNEWVEC (char, result->representation.length + 1);
632 memcpy (result->representation.string, buffer,
633 result->representation.length);
634 result->representation.string[result->representation.length] = '\0';
637 return result->representation.length;
641 /* --------------------------------------------------------------- */
642 /* Two functions used by trans-common.c to write overlapping
643 equivalence initializers to a buffer. This is added to the union
644 and the original initializers freed. */
647 /* Writes the values of a constant expression to a char buffer. If another
648 unequal initializer has already been written to the buffer, this is an
649 error. */
651 static size_t
652 expr_to_char (gfc_expr *e, unsigned char *data, unsigned char *chk, size_t len)
654 int i;
655 int ptr;
656 gfc_constructor *c;
657 gfc_component *cmp;
658 unsigned char *buffer;
660 if (e == NULL)
661 return 0;
663 /* Take a derived type, one component at a time, using the offsets from the backend
664 declaration. */
665 if (e->ts.type == BT_DERIVED)
667 for (c = gfc_constructor_first (e->value.constructor),
668 cmp = e->ts.u.derived->components;
669 c; c = gfc_constructor_next (c), cmp = cmp->next)
671 gcc_assert (cmp && cmp->backend_decl);
672 if (!c->expr)
673 continue;
674 ptr = TREE_INT_CST_LOW(DECL_FIELD_OFFSET(cmp->backend_decl))
675 + TREE_INT_CST_LOW(DECL_FIELD_BIT_OFFSET(cmp->backend_decl))/8;
676 expr_to_char (c->expr, &data[ptr], &chk[ptr], len);
678 return len;
681 /* Otherwise, use the target-memory machinery to write a bitwise image, appropriate
682 to the target, in a buffer and check off the initialized part of the buffer. */
683 len = gfc_target_expr_size (e);
684 buffer = (unsigned char*)alloca (len);
685 len = gfc_target_encode_expr (e, buffer, len);
687 for (i = 0; i < (int)len; i++)
689 if (chk[i] && (buffer[i] != data[i]))
691 gfc_error ("Overlapping unequal initializers in EQUIVALENCE "
692 "at %L", &e->where);
693 return 0;
695 chk[i] = 0xFF;
698 memcpy (data, buffer, len);
699 return len;
703 /* Writes the values from the equivalence initializers to a char* array
704 that will be written to the constructor to make the initializer for
705 the union declaration. */
707 size_t
708 gfc_merge_initializers (gfc_typespec ts, gfc_expr *e, unsigned char *data,
709 unsigned char *chk, size_t length)
711 size_t len = 0;
712 gfc_constructor * c;
714 switch (e->expr_type)
716 case EXPR_CONSTANT:
717 case EXPR_STRUCTURE:
718 len = expr_to_char (e, &data[0], &chk[0], length);
720 break;
722 case EXPR_ARRAY:
723 for (c = gfc_constructor_first (e->value.constructor);
724 c; c = gfc_constructor_next (c))
726 size_t elt_size = gfc_target_expr_size (c->expr);
728 if (mpz_cmp_si (c->offset, 0) != 0)
729 len = elt_size * (size_t)mpz_get_si (c->offset);
731 len = len + gfc_merge_initializers (ts, c->expr, &data[len],
732 &chk[len], length - len);
734 break;
736 default:
737 return 0;
740 return len;
744 /* Transfer the bitpattern of a (integer) BOZ to real or complex variables.
745 When successful, no BOZ or nothing to do, true is returned. */
747 bool
748 gfc_convert_boz (gfc_expr *expr, gfc_typespec *ts)
750 size_t buffer_size, boz_bit_size, ts_bit_size;
751 int index;
752 unsigned char *buffer;
754 if (!expr->is_boz)
755 return true;
757 gcc_assert (expr->expr_type == EXPR_CONSTANT
758 && expr->ts.type == BT_INTEGER);
760 /* Don't convert BOZ to logical, character, derived etc. */
761 if (ts->type == BT_REAL)
763 buffer_size = size_float (ts->kind);
764 ts_bit_size = buffer_size * 8;
766 else if (ts->type == BT_COMPLEX)
768 buffer_size = size_complex (ts->kind);
769 ts_bit_size = buffer_size * 8 / 2;
771 else
772 return true;
774 /* Convert BOZ to the smallest possible integer kind. */
775 boz_bit_size = mpz_sizeinbase (expr->value.integer, 2);
777 if (boz_bit_size > ts_bit_size)
779 gfc_error_now ("BOZ constant at %L is too large (%ld vs %ld bits)",
780 &expr->where, (long) boz_bit_size, (long) ts_bit_size);
781 return false;
784 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
785 if ((unsigned) gfc_integer_kinds[index].bit_size >= ts_bit_size)
786 break;
788 expr->ts.kind = gfc_integer_kinds[index].kind;
789 buffer_size = MAX (buffer_size, size_integer (expr->ts.kind));
791 buffer = (unsigned char*)alloca (buffer_size);
792 encode_integer (expr->ts.kind, expr->value.integer, buffer, buffer_size);
793 mpz_clear (expr->value.integer);
795 if (ts->type == BT_REAL)
797 mpfr_init (expr->value.real);
798 gfc_interpret_float (ts->kind, buffer, buffer_size, expr->value.real);
800 else
802 mpc_init2 (expr->value.complex, mpfr_get_default_prec());
803 gfc_interpret_complex (ts->kind, buffer, buffer_size,
804 expr->value.complex);
806 expr->is_boz = 0;
807 expr->ts.type = ts->type;
808 expr->ts.kind = ts->kind;
810 return true;